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
// Copyright (c) 2017-2019 Fabian Schuiki

//! Instruction and BB ordering.

use crate::{
    ir::{Block, Inst},
    table::SecondaryTable,
};
use std::collections::HashMap;

/// Determines the order of instructions and BBs in a `Function` or `Process`.
#[derive(Default)]
pub struct FunctionLayout {
    /// A linked list of BBs in layout order.
    bbs: SecondaryTable<Block, BlockNode>,
    /// The first BB in the layout.
    first_bb: Option<Block>,
    /// The last BB in the layout.
    last_bb: Option<Block>,
    /// Lookup table to find the BB that contains an instruction.
    inst_map: HashMap<Inst, Block>,
}

/// A node in the layout's double-linked list of BBs.
#[derive(Default)]
struct BlockNode {
    prev: Option<Block>,
    next: Option<Block>,
    layout: InstLayout,
}

impl FunctionLayout {
    /// Create a new function layout.
    pub fn new() -> Self {
        Default::default()
    }
}

/// Basic block arrangement.
///
/// The following functions are used for laying out the basic blocks within a
/// `Function` or `Process`.
impl FunctionLayout {
    /// Check whether a BB has been placed in the layout.
    pub fn is_block_inserted(&self, bb: Block) -> bool {
        self.bbs.contains(bb)
    }

    /// Append a BB to the end of the function.
    pub fn append_block(&mut self, bb: Block) {
        self.bbs.add(
            bb,
            BlockNode {
                prev: self.last_bb,
                next: None,
                layout: Default::default(),
            },
        );
        if let Some(prev) = self.last_bb {
            self.bbs[prev].next = Some(bb);
        }
        if self.first_bb.is_none() {
            self.first_bb = Some(bb);
        }
        self.last_bb = Some(bb);
    }

    /// Prepend a BB to the beginning of a function.
    ///
    /// This effectively makes `bb` the new entry block.
    pub fn prepend_block(&mut self, bb: Block) {
        self.bbs.add(
            bb,
            BlockNode {
                prev: None,
                next: self.first_bb,
                layout: Default::default(),
            },
        );
        if let Some(next) = self.first_bb {
            self.bbs[next].prev = Some(bb);
        }
        if self.last_bb.is_none() {
            self.last_bb = Some(bb);
        }
        self.first_bb = Some(bb);
    }

    /// Insert a BB after another BB.
    pub fn insert_block_after(&mut self, bb: Block, after: Block) {
        self.bbs.add(
            bb,
            BlockNode {
                prev: Some(after),
                next: self.bbs[after].next,
                layout: Default::default(),
            },
        );
        if let Some(next) = self.bbs[after].next {
            self.bbs[next].prev = Some(bb);
        }
        self.bbs[after].next = Some(bb);
        if self.last_bb == Some(after) {
            self.last_bb = Some(bb);
        }
    }

    /// Insert a BB before another BB.
    pub fn insert_block_before(&mut self, bb: Block, before: Block) {
        self.bbs.add(
            bb,
            BlockNode {
                prev: self.bbs[before].prev,
                next: Some(before),
                layout: Default::default(),
            },
        );
        if let Some(prev) = self.bbs[before].prev {
            self.bbs[prev].next = Some(bb);
        }
        self.bbs[before].prev = Some(bb);
        if self.first_bb == Some(before) {
            self.first_bb = Some(bb);
        }
    }

    /// Remove a BB from the function.
    pub fn remove_block(&mut self, bb: Block) {
        let node = self.bbs.remove(bb).unwrap();
        if let Some(next) = node.next {
            self.bbs[next].prev = node.prev;
        }
        if let Some(prev) = node.prev {
            self.bbs[prev].next = node.next;
        }
        if self.first_bb == Some(bb) {
            self.first_bb = node.next;
        }
        if self.last_bb == Some(bb) {
            self.last_bb = node.prev;
        }
    }

    /// Swap the position of two BBs.
    pub fn swap_blocks(&mut self, bb0: Block, bb1: Block) {
        if bb0 == bb1 {
            return;
        }

        let mut bb0_next = self.bbs[bb0].next;
        let mut bb0_prev = self.bbs[bb0].prev;
        let mut bb1_next = self.bbs[bb1].next;
        let mut bb1_prev = self.bbs[bb1].prev;
        if bb0_next == Some(bb1) {
            bb0_next = Some(bb0);
        }
        if bb0_prev == Some(bb1) {
            bb0_prev = Some(bb0);
        }
        if bb1_next == Some(bb0) {
            bb1_next = Some(bb1);
        }
        if bb1_prev == Some(bb0) {
            bb1_prev = Some(bb1);
        }
        self.bbs[bb0].next = bb1_next;
        self.bbs[bb0].prev = bb1_prev;
        self.bbs[bb1].next = bb0_next;
        self.bbs[bb1].prev = bb0_prev;

        if let Some(next) = bb0_next {
            self.bbs[next].prev = Some(bb1);
        }
        if let Some(prev) = bb0_prev {
            self.bbs[prev].next = Some(bb1);
        }
        if let Some(next) = bb1_next {
            self.bbs[next].prev = Some(bb0);
        }
        if let Some(prev) = bb1_prev {
            self.bbs[prev].next = Some(bb0);
        }

        if self.first_bb == Some(bb0) {
            self.first_bb = Some(bb1);
        } else if self.first_bb == Some(bb1) {
            self.first_bb = Some(bb0);
        }
        if self.last_bb == Some(bb0) {
            self.last_bb = Some(bb1);
        } else if self.last_bb == Some(bb1) {
            self.last_bb = Some(bb0);
        }
    }

    /// Return an iterator over all BBs in layout order.
    pub fn blocks<'a>(&'a self) -> impl Iterator<Item = Block> + 'a {
        std::iter::successors(self.first_bb, move |&bb| self.next_block(bb))
    }

    /// Get the first BB in the layout. This is the entry block.
    pub fn first_block(&self) -> Option<Block> {
        self.first_bb
    }

    /// Get the last BB in the layout.
    pub fn last_block(&self) -> Option<Block> {
        self.last_bb
    }

    /// Get the BB preceding `bb` in the layout.
    pub fn prev_block(&self, bb: Block) -> Option<Block> {
        self.bbs[bb].prev
    }

    /// Get the BB following `bb` in the layout.
    pub fn next_block(&self, bb: Block) -> Option<Block> {
        self.bbs[bb].next
    }
}

/// Determines the order of instructions.
#[derive(Default)]
pub struct InstLayout {
    /// A linked list of instructions in layout order.
    insts: SecondaryTable<Inst, InstNode>,
    /// The first instruction in the layout.
    first_inst: Option<Inst>,
    /// The last instruction in the layout.
    last_inst: Option<Inst>,
}

/// A node in the layout's double-linked list of BBs.
#[derive(Default)]
struct InstNode {
    prev: Option<Inst>,
    next: Option<Inst>,
}

impl InstLayout {
    /// Create a new instruction layout.
    pub fn new() -> Self {
        Default::default()
    }

    /// Check whether an instruction has been placed in the layout.
    pub fn is_inst_inserted(&self, inst: Inst) -> bool {
        self.insts.contains(inst)
    }

    /// Append an instruction to the end of the function.
    pub fn append_inst(&mut self, inst: Inst) {
        self.insts.add(
            inst,
            InstNode {
                prev: self.last_inst,
                next: None,
            },
        );
        if let Some(prev) = self.last_inst {
            self.insts[prev].next = Some(inst);
        }
        if self.first_inst.is_none() {
            self.first_inst = Some(inst);
        }
        self.last_inst = Some(inst);
    }

    /// Prepend an instruction to the beginning of the function.
    pub fn prepend_inst(&mut self, inst: Inst) {
        self.insts.add(
            inst,
            InstNode {
                prev: None,
                next: self.first_inst,
            },
        );
        if let Some(next) = self.first_inst {
            self.insts[next].prev = Some(inst);
        }
        if self.last_inst.is_none() {
            self.last_inst = Some(inst);
        }
        self.first_inst = Some(inst);
    }

    /// Insert an instruction after another instruction.
    pub fn insert_inst_after(&mut self, inst: Inst, after: Inst) {
        self.insts.add(
            inst,
            InstNode {
                prev: Some(after),
                next: self.insts[after].next,
            },
        );
        if let Some(next) = self.insts[after].next {
            self.insts[next].prev = Some(inst);
        }
        self.insts[after].next = Some(inst);
        if self.last_inst == Some(after) {
            self.last_inst = Some(inst);
        }
    }

    /// Insert an instruction before another instruction.
    pub fn insert_inst_before(&mut self, inst: Inst, before: Inst) {
        self.insts.add(
            inst,
            InstNode {
                prev: self.insts[before].prev,
                next: Some(before),
            },
        );
        self.insts[before].prev = Some(inst);
        if let Some(prev) = self.insts[before].prev {
            self.insts[prev].next = Some(inst);
        }
        if self.first_inst == Some(before) {
            self.first_inst = Some(inst);
        }
    }

    /// Remove an instruction from the function.
    pub fn remove_inst(&mut self, inst: Inst) {
        let node = self.insts.remove(inst).unwrap();
        if let Some(next) = node.next {
            self.insts[next].prev = node.prev;
        }
        if let Some(prev) = node.prev {
            self.insts[prev].next = node.next;
        }
        if self.first_inst == Some(inst) {
            self.first_inst = node.next;
        }
        if self.last_inst == Some(inst) {
            self.last_inst = node.prev;
        }
    }

    /// Return an iterator over all instructions in layout order.
    pub fn insts<'a>(&'a self) -> impl Iterator<Item = Inst> + 'a {
        std::iter::successors(self.first_inst, move |&inst| self.next_inst(inst))
    }

    /// Get the first instruction in the layout.
    pub fn first_inst(&self) -> Option<Inst> {
        self.first_inst
    }

    /// Get the last instruction in the layout.
    pub fn last_inst(&self) -> Option<Inst> {
        self.last_inst
    }

    /// Get the instruction preceding `inst` in the layout.
    pub fn prev_inst(&self, inst: Inst) -> Option<Inst> {
        self.insts[inst].prev
    }

    /// Get the instruction following `inst` in the layout.
    pub fn next_inst(&self, inst: Inst) -> Option<Inst> {
        self.insts[inst].next
    }
}

/// Instruction arrangement.
///
/// The following functions are used for laying out the instructions within a
/// `Function` or `Process`.
impl FunctionLayout {
    /// Get the BB which contains `inst`, or `None` if `inst` is not inserted.
    pub fn inst_block(&self, inst: Inst) -> Option<Block> {
        self.inst_map.get(&inst).cloned()
    }

    /// Append an instruction to the end of a BB.
    pub fn append_inst(&mut self, inst: Inst, bb: Block) {
        self.bbs[bb].layout.append_inst(inst);
        self.inst_map.insert(inst, bb);
    }

    /// Prepend an instruction to the beginning of a BB.
    pub fn prepend_inst(&mut self, inst: Inst, bb: Block) {
        self.bbs[bb].layout.prepend_inst(inst);
        self.inst_map.insert(inst, bb);
    }

    /// Insert an instruction after another instruction.
    pub fn insert_inst_after(&mut self, inst: Inst, after: Inst) {
        let bb = self.inst_block(after).expect("`after` not inserted");
        self.bbs[bb].layout.insert_inst_after(inst, after);
        self.inst_map.insert(inst, bb);
    }

    /// Insert an instruction before another instruction.
    pub fn insert_inst_before(&mut self, inst: Inst, before: Inst) {
        let bb = self.inst_block(before).expect("`before` not inserted");
        self.bbs[bb].layout.insert_inst_before(inst, before);
        self.inst_map.insert(inst, bb);
    }

    /// Remove an instruction from the function.
    pub fn remove_inst(&mut self, inst: Inst) {
        let bb = self.inst_block(inst).expect("`inst` not inserted");
        self.bbs[bb].layout.remove_inst(inst);
        self.inst_map.remove(&inst);
    }

    /// Return an iterator over all instructions in a block in layout order.
    pub fn insts<'a>(&'a self, bb: Block) -> impl Iterator<Item = Inst> + 'a {
        self.bbs[bb].layout.insts()
    }

    /// Get the first instruction in the layout.
    pub fn first_inst(&self, bb: Block) -> Option<Inst> {
        self.bbs[bb].layout.first_inst()
    }

    /// Get the last instruction in the layout.
    pub fn last_inst(&self, bb: Block) -> Option<Inst> {
        self.bbs[bb].layout.last_inst()
    }

    /// Get the instruction preceding `inst` in the layout.
    pub fn prev_inst(&self, inst: Inst) -> Option<Inst> {
        let bb = self.inst_block(inst).unwrap();
        self.bbs[bb].layout.prev_inst(inst)
    }

    /// Get the instruction following `inst` in the layout.
    pub fn next_inst(&self, inst: Inst) -> Option<Inst> {
        let bb = self.inst_block(inst).unwrap();
        self.bbs[bb].layout.next_inst(inst)
    }
}