@@ -79,14 +79,22 @@ LivenessAnalysis::LivenessAnalysis(Function *Func) {
79
79
if (!I.getType ()->isVoidTy ())
80
80
Defs[&I].insert (cast<Value>(&I));
81
81
82
- // Record what this instruction uses.
82
+ // For normal instructions we just iterate over all operands and mark
83
+ // them as used. We can't do this for PHI nodes though, since depending
84
+ // on the control flow (i.e. which block we are coming from) only one of
85
+ // the operands is used at a time. For example:
83
86
//
84
- // In order to track the operands of PHI nodes we need to be a bit crafty
85
- // as otherwise we end up with live values in blocks where they actually
86
- // don't exist. To avoid this, we need to mark as live any PHI operand
87
- // from one block at the end of all other blocks. This leads to those
88
- // values being killed immediately after entering the block in the
89
- // backwards pass and thus the value never being live there.
87
+ // phi [%1, bb1], [%2, bb2], [%3, bb3]
88
+ //
89
+ // Here, when the previous block was bb1, then only %1 is used, %2 if we
90
+ // come from bb2, and so on.
91
+ //
92
+ // The next problem is that we can't mark the operand as used in the
93
+ // current block, since that use would perculate upwards when looking at
94
+ // the predecessor blocks, and thus consider the operand used in blocks
95
+ // where it isn't. Instead, we want to mark the operand as used
96
+ // only in the predecssor block: %1 is marked as used at the end of bb1,
97
+ // %2 at the end of bb2, and so on.
90
98
//
91
99
// The book doesn't cover this quirk, as it explains liveness for
92
100
// non-SSA form, and thus doesn't need to worry about Phi nodes.
@@ -98,24 +106,15 @@ LivenessAnalysis::LivenessAnalysis(Function *Func) {
98
106
if (isa<Constant>(IV)) {
99
107
continue ;
100
108
}
101
- // For each block that isn't the incoming block create a Def for this
102
- // value. This means when we do the backwards liveness pass this
103
- // value is immediately killed in the block.
104
- for (auto *BBB = P->block_begin (); BBB != P->block_end (); BBB++) {
105
- if (*BBB != IBB) {
106
- Instruction *Last = &((*BBB)->back ());
107
- Defs[Last].insert (IV);
108
- }
109
- }
110
- Uses[&I].insert (IV);
109
+ Instruction *Last = &IBB->back ();
110
+ Uses[Last].insert (IV);
111
111
}
112
- continue ;
112
+ } else {
113
+ for (auto *U = I.op_begin (); U < I.op_end (); U++)
114
+ if ((!isa<Constant>(U)) && (!isa<BasicBlock>(U)) &&
115
+ (!isa<MetadataAsValue>(U)) && (!isa<InlineAsm>(U)))
116
+ Uses[&I].insert (*U);
113
117
}
114
-
115
- for (auto *U = I.op_begin (); U < I.op_end (); U++)
116
- if ((!isa<Constant>(U)) && (!isa<BasicBlock>(U)) &&
117
- (!isa<MetadataAsValue>(U)) && (!isa<InlineAsm>(U)))
118
- Uses[&I].insert (*U);
119
118
}
120
119
}
121
120
0 commit comments