12
12
// The pass relies in constraints LLVM imposes on the placement of
13
13
// save/restore points (cf. ShrinkWrap) and has certain preconditions about
14
14
// placement of CFI instructions:
15
- // * for any two CFI instructions of the function prologue one dominates
16
- // and is post-dominated by the other
17
- // * possibly multiple epilogue blocks, where each epilogue block is
18
- // complete and self-contained, i.e. CSR restore instructions (and the
19
- // corresponding CFI instructions are not split across two or more blocks.
20
- // * CFI instructions are not contained in any loops
15
+ // * For any two CFI instructions of the function prologue one dominates
16
+ // and is post-dominated by the other.
17
+ // * The function possibly contains multiple epilogue blocks, where each
18
+ // epilogue block is complete and self-contained, i.e. CSR restore
19
+ // instructions (and the corresponding CFI instructions)
20
+ // are not split across two or more blocks.
21
+ // * CFI instructions are not contained in any loops.
22
+
21
23
// Thus, during execution, at the beginning and at the end of each basic block,
22
24
// following the prologue, the function can be in one of two states:
23
25
// - "has a call frame", if the function has executed the prologue, and
27
29
// which can be computed by a single RPO traversal.
28
30
29
31
// The location of the prologue is determined by finding the first block in the
30
- // post-order traversal which contains CFI instructions.
32
+ // reverse traversal which contains CFI instructions.
31
33
32
34
// In order to accommodate backends which do not generate unwind info in
33
35
// epilogues we compute an additional property "strong no call frame on entry",
@@ -99,14 +101,16 @@ static bool containsEpilogue(const MachineBasicBlock &MBB) {
99
101
100
102
static MachineBasicBlock *
101
103
findPrologueEnd (MachineFunction &MF, MachineBasicBlock::iterator &PrologueEnd) {
102
- for (auto It = po_begin (&MF.front ()), End = po_end (&MF.front ()); It != End;
103
- ++It) {
104
- MachineBasicBlock *MBB = *It;
105
- for (MachineInstr &MI : reverse (MBB->instrs ())) {
104
+ // Even though we should theoretically traverse the blocks in post-order, we
105
+ // can't encode correctly cases where prologue blocks are not laid out in
106
+ // topological order. Then, assuming topological order, we can just traverse
107
+ // the function in reverse.
108
+ for (MachineBasicBlock &MBB : reverse (MF)) {
109
+ for (MachineInstr &MI : reverse (MBB.instrs ())) {
106
110
if (!isPrologueCFIInstruction (MI))
107
111
continue ;
108
112
PrologueEnd = std::next (MI.getIterator ());
109
- return MBB;
113
+ return & MBB;
110
114
}
111
115
}
112
116
return nullptr ;
@@ -123,7 +127,6 @@ bool CFIFixup::runOnMachineFunction(MachineFunction &MF) {
123
127
124
128
// Find the prologue and the point where we can issue the first
125
129
// `.cfi_remember_state`.
126
-
127
130
MachineBasicBlock::iterator PrologueEnd;
128
131
MachineBasicBlock *PrologueBlock = findPrologueEnd (MF, PrologueEnd);
129
132
if (PrologueBlock == nullptr )
0 commit comments