fix phi patching in cloneBB
based on a patch by Juneyoung Lee
MODIFY
ir/function.cpp
ir/function.cpp
@@ -390,14 +390,34 @@ cloneBB(Function &F, const BasicBlock &BB, const char *suffix,
unordered_map<const Value*, vector<pair<BasicBlock*, Value*>>> &vmap) {
string bb_name = BB.getName() + suffix;
auto &newbb = F.getBB(bb_name);
+ unordered_set<const Value *> phis_from_orig_bb;
+
for (auto &i : BB.instrs()) {
+ if (dynamic_cast<const Phi *>(&i))
+ phis_from_orig_bb.insert(&i);
+
auto d = i.dup(suffix);
for (auto &op : d->operands()) {
auto it = vmap.find(op);
if (it != vmap.end()) {
+ // consider this case:
+ // loop:
+ // %op = phi ...
+ // %k = phi [%op, %loop], ...
+ //
+ // In iteration i, %k should point to iteration (i-1)'s %k.
+ // If is_phi_to_phi is true, %op is the phi in this block.
+ bool is_phi_to_phi = dynamic_cast<const Phi *>(&i) &&
+ phis_from_orig_bb.count(op);
+ if (is_phi_to_phi) {
+ if (it->second.size() > 2) {
+ d->rauw(*op, *it->second[it->second.size()-2].second);
+ }
+ } else {
d->rauw(*op, *it->second.back().second);
}
}
+ }
if (!i.isVoid())
vmap[&i].emplace_back(&newbb, d.get());
newbb.addInstr(move(d));

ADD
tests/alive-tv/loops/phi5-fail.srctgt.ll
+; TEST-ARGS: -src-unroll=5 -tgt-unroll=5
+; ERROR: Value mismatch
+
+define i32 @src() {
+entry:
+ br label %loop
+loop:
+ %i = phi i32 [0, %entry], [%i.next, %loop]
+ %j = phi i32 [0, %entry], [%i, %loop] ; should point to the previous iteration's %i
+ %i.next = add i32 %i, 1
+ %c = icmp ne i32 %i, 4
+ br i1 %c, label %loop, label %exit
+exit:
+ ret i32 %j
+}
+
+define i32 @tgt() {
+ ret i32 4
+}

ADD
tests/alive-tv/loops/phi5.srctgt.ll
+; TEST-ARGS: -src-unroll=5 -tgt-unroll=5
+
+define i32 @src() {
+entry:
+ br label %loop
+loop:
+ %i = phi i32 [0, %entry], [%i.next, %loop]
+ %j = phi i32 [0, %entry], [%i, %loop] ; should point to the previous iteration's %i
+ %i.next = add i32 %i, 1
+ %c = icmp ne i32 %i, 4
+ br i1 %c, label %loop, label %exit
+exit:
+ ret i32 %j
+}
+
+define i32 @tgt() {
+ ret i32 3
+}

fix phi patching in cloneBB
based on a patch by Juneyoung Lee (#570)