vicis 0.1.0

Manipulate LLVM-IR in Pure Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use crate::codegen::{
    function::{
        basic_block::BasicBlockId,
        instruction::{Instruction, InstructionData},
        Function,
    },
    isa::TargetIsa,
    // module::Module,
    register::{RegUnit, RegisterInfo, VReg},
};
use rustc_hash::{FxHashMap, FxHashSet};
use std::cmp::Ordering;

pub struct Liveness {
    pub block_data: FxHashMap<BasicBlockId, BlockData>,
    pub vreg_lrs_map: FxHashMap<VReg, LiveRanges>,
    pub reg_lrs_map: FxHashMap<RegUnit, LiveRanges>,
}

#[derive(Debug, Clone)]
pub struct LiveRanges(pub Vec<LiveRange>);

#[derive(Debug, Clone)]
pub struct LiveRange {
    pub start: ProgramPoint,
    pub end: ProgramPoint,
}

#[derive(Debug)]
pub struct BlockData {
    vreg_def: FxHashSet<VReg>,
    vreg_live_in: FxHashSet<VReg>,
    vreg_live_out: FxHashSet<VReg>,
    reg_def: FxHashSet<RegUnit>,
    reg_live_in: FxHashSet<RegUnit>,
    reg_live_out: FxHashSet<RegUnit>,
}

// pub type ProgramPointId = Id<ProgramPoint>;

#[derive(Debug, Clone, Copy)]
pub struct ProgramPoint(pub u32, pub u32);

impl PartialOrd for ProgramPoint {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        if self.0 < other.0 {
            return Some(Ordering::Less);
        }

        if self.0 > other.0 {
            return Some(Ordering::Greater);
        }

        assert_eq!(self.0, other.0);

        if self.1 < other.1 {
            return Some(Ordering::Less);
        }

        if self.1 == other.1 {
            return Some(Ordering::Equal);
        }

        assert!(self.1 > other.1);

        Some(Ordering::Greater)
    }
}

impl Ord for ProgramPoint {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap()
    }
}

impl PartialEq for ProgramPoint {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0 && self.1 == other.1
    }
}

impl Eq for ProgramPoint {}

// pub fn run_on_module<T: TargetIsa>(module: &mut Module<T>) {
//     for (_, func) in &mut module.functions {
//         run_on_function(func);
//     }
// }

// pub fn run_on_function<T: TargetIsa>(function: &mut Function<T>) -> Liveness {
//     // for block_id in function.layout.block_iter() {
//     //     for inst_id in function.layout.inst_iter(block_id) {
//     //         let inst = function.data.inst_ref(inst_id);
//     //     }
//     // }
//     todo!()
// }

impl Liveness {
    pub fn new() -> Self {
        Self {
            block_data: FxHashMap::default(),
            vreg_lrs_map: FxHashMap::default(),
            reg_lrs_map: FxHashMap::default(),
        }
    }

    pub fn analyze_function<T: TargetIsa>(&mut self, func: &Function<T>) {
        // Analyze live-in and live-out virutal registers
        self.set_def(func);
        self.visit(func);

        println!("{:#?}", self.block_data);

        // Compute program points
        self.compute_program_points(func);
    }

    ////////

    pub fn compute_program_points<T: TargetIsa>(&mut self, func: &Function<T>) {
        let mut block_num = 0;
        for block_id in func.layout.block_iter() {
            const STEP: u32 = 16;
            let mut inst_num = 0u32;
            let mut local_vreg_lr_map = FxHashMap::default();
            let mut local_reg_lr_map = FxHashMap::default();

            // live-in
            for &live_in in &self.block_data[&block_id].vreg_live_in {
                local_vreg_lr_map.insert(
                    live_in,
                    LiveRange {
                        start: ProgramPoint(block_num, 0),
                        end: ProgramPoint(block_num, 0),
                    },
                );
            }
            for &live_in in &self.block_data[&block_id].reg_live_in {
                local_reg_lr_map.insert(
                    live_in,
                    LiveRanges(vec![LiveRange {
                        start: ProgramPoint(block_num, 0),
                        end: ProgramPoint(block_num, 0),
                    }]),
                );
            }

            inst_num += STEP;

            for inst_id in func.layout.inst_iter(block_id) {
                let inst = func.data.inst_ref(inst_id);

                // inputs
                for input in inst.data.input_vregs() {
                    local_vreg_lr_map.get_mut(&input).unwrap().end =
                        ProgramPoint(block_num, inst_num);
                    local_vreg_lr_map.get_mut(&input).unwrap().end =
                        ProgramPoint(block_num, inst_num);
                }
                for input in inst.data.input_regs() {
                    local_reg_lr_map
                        .get_mut(&T::RegInfo::to_reg_unit(input))
                        .unwrap()
                        .0
                        .last_mut()
                        .unwrap()
                        .end = ProgramPoint(block_num, inst_num);
                }

                // outputs
                for output in inst.data.output_vregs() {
                    local_vreg_lr_map
                        .entry(output)
                        .or_insert(LiveRange {
                            start: ProgramPoint(block_num, inst_num),
                            end: ProgramPoint(block_num, inst_num),
                        })
                        .end = ProgramPoint(block_num, inst_num);
                }
                for output in inst.data.output_regs() {
                    local_reg_lr_map
                        .entry(T::RegInfo::to_reg_unit(output))
                        .or_insert(LiveRanges(vec![]))
                        .0
                        .push(LiveRange {
                            start: ProgramPoint(block_num, inst_num),
                            end: ProgramPoint(block_num, inst_num),
                        })
                }

                inst_num += STEP;
            }

            // live-out
            for live_out in &self.block_data[&block_id].vreg_live_out {
                local_vreg_lr_map.get_mut(live_out).unwrap().end =
                    ProgramPoint(block_num, inst_num);
            }
            for live_out in &self.block_data[&block_id].reg_live_out {
                local_reg_lr_map
                    .get_mut(live_out)
                    .unwrap()
                    .0
                    .last_mut()
                    .unwrap()
                    .end = ProgramPoint(block_num, inst_num);
            }

            // merge local lr_map into lrs_map
            for (vreg, local_lr) in local_vreg_lr_map {
                self.vreg_lrs_map
                    .entry(vreg)
                    .or_insert(LiveRanges(vec![]))
                    .0
                    .push(local_lr)
            }
            for (reg, local_lr) in local_reg_lr_map {
                self.reg_lrs_map
                    .entry(reg)
                    .or_insert(LiveRanges(vec![]))
                    .0
                    .extend(local_lr.0.into_iter())
            }

            block_num += 1;
        }
    }

    ///////////

    // pub fn get_or_create_live_ranges(&mut self, vreg: VReg) -> &mut LiveRanges {
    //     self.lrs_map.entry(vreg).or_insert(LiveRanges(vec![]))
    // }

    ////////

    fn set_def<T: TargetIsa>(&mut self, func: &Function<T>) {
        for block_id in func.layout.block_iter() {
            self.block_data.entry(block_id).or_insert(BlockData::new());
            for inst_id in func.layout.inst_iter(block_id) {
                let inst = func.data.inst_ref(inst_id);
                self.set_def_on_inst::<T>(inst, block_id);
            }
        }
    }

    fn set_def_on_inst<T: TargetIsa>(
        &mut self,
        inst: &Instruction<T::InstData>,
        block_id: BasicBlockId,
    ) {
        for output in inst.data.output_vregs() {
            self.block_data
                .entry(block_id)
                .or_insert_with(|| BlockData::new())
                .vreg_def
                .insert(output);
        }
        for output in inst.data.output_regs() {
            self.block_data
                .entry(block_id)
                .or_insert_with(|| BlockData::new())
                .reg_def
                .insert(T::RegInfo::to_reg_unit(output));
        }
    }

    fn visit<T: TargetIsa>(&mut self, func: &Function<T>) {
        for block_id in func.layout.block_iter() {
            for inst_id in func.layout.inst_iter(block_id) {
                let inst = func.data.inst_ref(inst_id);
                self.visit_inst(func, inst, block_id);
            }
        }
    }

    fn visit_inst<T: TargetIsa>(
        &mut self,
        func: &Function<T>,
        inst: &Instruction<T::InstData>,
        block_id: BasicBlockId,
    ) {
        for input in inst.data.input_vregs() {
            self.propagate_vreg(func, input, block_id);
        }
        for input in inst.data.input_regs() {
            self.propagate_reg(func, T::RegInfo::to_reg_unit(input), block_id);
        }
    }

    fn propagate_vreg<T: TargetIsa>(
        &mut self,
        func: &Function<T>,
        input: VReg,
        block_id: BasicBlockId,
    ) {
        {
            let data = self.block_data.get_mut(&block_id).unwrap();

            if data.vreg_def.contains(&input) {
                return;
            }

            if !data.vreg_live_in.insert(input) {
                return;
            }
        }

        for pred_id in &func.data.basic_blocks[block_id].preds {
            if self
                .block_data
                .get_mut(pred_id)
                .unwrap()
                .vreg_live_out
                .insert(input)
            {
                self.propagate_vreg(func, input, *pred_id);
            }
        }
    }

    fn propagate_reg<T: TargetIsa>(
        &mut self,
        func: &Function<T>,
        input: RegUnit,
        block_id: BasicBlockId,
    ) {
        {
            let data = self.block_data.get_mut(&block_id).unwrap();

            if data.reg_def.contains(&input) {
                return;
            }

            if !data.reg_live_in.insert(input) {
                return;
            }
        }

        for pred_id in &func.data.basic_blocks[block_id].preds {
            if self
                .block_data
                .get_mut(pred_id)
                .unwrap()
                .reg_live_out
                .insert(input)
            {
                self.propagate_reg(func, input, *pred_id);
            }
        }
    }
}

impl LiveRanges {
    pub fn interfere(&self, other: &Self) -> bool {
        for x in &self.0 {
            for y in &other.0 {
                if x.interfere(y) {
                    return true;
                }
            }
        }
        false
    }

    pub fn merge(&mut self, other: &Self) {
        if other.0.len() == 0 {
            return;
        }

        if self.0.len() == 0 {
            *self = other.clone();
            return;
        }

        let mut new = vec![];

        let mut z = vec![];
        let mut yi = 0;
        for x in &self.0 {
            while yi < other.0.len() {
                let y = &other.0[yi];
                if x.start.0 < y.start.0 {
                    new.push(y.clone());
                    break;
                }
                if x.start.0 == y.start.0 {
                    if x.interfere(y) {
                        new.push(LiveRange {
                            start: ::std::cmp::min(x.start, y.start),
                            end: ::std::cmp::max(x.end, y.end),
                        });
                    } else {
                        if x.start.1 < y.start.1 {
                            if x.end.1 == y.start.1 {
                                new.push(LiveRange {
                                    start: x.start,
                                    end: y.end,
                                });
                            } else {
                                new.push(x.clone());
                                new.push(y.clone())
                            }
                        } else {
                            if y.end.1 == x.start.1 {
                                new.push(LiveRange {
                                    start: y.start,
                                    end: x.end,
                                });
                            } else {
                                new.push(y.clone());
                                new.push(x.clone());
                            }
                        }
                    }
                    yi += 1;
                    break;
                }
                if x.start.0 > y.start.0 {
                    new.push(x.clone());
                    z.push(y.clone());
                    yi += 1;
                    continue;
                }
            }
        }

        for (i, z) in z.into_iter().enumerate() {
            new.insert(i, z)
        }
        if yi < other.0.len() - 1 {
            new.append(&mut other.0[yi..].to_vec())
        }

        self.0 = new;
    }
}

impl LiveRange {
    pub fn interfere(&self, other: &Self) -> bool {
        self.start < other.end && self.end > other.start
    }
}

impl BlockData {
    pub fn new() -> Self {
        BlockData {
            vreg_def: FxHashSet::default(),
            reg_def: FxHashSet::default(),
            vreg_live_in: FxHashSet::default(),
            vreg_live_out: FxHashSet::default(),
            reg_live_in: FxHashSet::default(),
            reg_live_out: FxHashSet::default(),
        }
    }
}