Skip to content

Commit f9998a9

Browse files
authored
Merge pull request llvm#192 from ptersilie/fix_liveness
Fix liveness analysis when dealing with phi nodes.
2 parents 4c8b98f + 7fc95eb commit f9998a9

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

llvm/lib/Transforms/Yk/LivenessAnalysis.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,22 @@ LivenessAnalysis::LivenessAnalysis(Function *Func) {
7979
if (!I.getType()->isVoidTy())
8080
Defs[&I].insert(cast<Value>(&I));
8181

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:
8386
//
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.
9098
//
9199
// The book doesn't cover this quirk, as it explains liveness for
92100
// non-SSA form, and thus doesn't need to worry about Phi nodes.
@@ -98,24 +106,15 @@ LivenessAnalysis::LivenessAnalysis(Function *Func) {
98106
if (isa<Constant>(IV)) {
99107
continue;
100108
}
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);
111111
}
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);
113117
}
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);
119118
}
120119
}
121120

0 commit comments

Comments
 (0)