[LoopDeletion] Exploit undef Phi inputs when symbolically executing 1st iteration

Follow-up on Roman's idea expressed in D103959.
- If a Phi has undefined inputs from live blocks:
   - and no other inputs, assume it is undef itself;
   - and exactly one non-undef input, we can assume that all undefs are equal to this input.

Differential Revision: https://reviews.llvm.org/D104618
Reviewed By: lebedev.ri, nikic
This commit is contained in:
Max Kazantsev 2021-06-23 11:51:36 +07:00
parent f681fd927e
commit 842b4c83cb
2 changed files with 22 additions and 12 deletions

View File

@ -251,20 +251,28 @@ static bool canProveExitOnFirstIteration(Loop *L, DominatorTree &DT,
// (non-latch) predecessors. // (non-latch) predecessors.
auto GetSoleInputOnFirstIteration = [&](PHINode & PN)->Value * { auto GetSoleInputOnFirstIteration = [&](PHINode & PN)->Value * {
BasicBlock *BB = PN.getParent(); BasicBlock *BB = PN.getParent();
bool HasLivePreds = false;
(void)HasLivePreds;
if (BB == Header) if (BB == Header)
return PN.getIncomingValueForBlock(Predecessor); return PN.getIncomingValueForBlock(Predecessor);
Value *OnlyInput = nullptr; Value *OnlyInput = nullptr;
for (auto *Pred : predecessors(BB)) for (auto *Pred : predecessors(BB))
if (LiveEdges.count({ Pred, BB })) { if (LiveEdges.count({ Pred, BB })) {
HasLivePreds = true;
Value *Incoming = PN.getIncomingValueForBlock(Pred); Value *Incoming = PN.getIncomingValueForBlock(Pred);
// Skip undefs. If they are present, we can assume they are equal to
// the non-undef input.
if (isa<UndefValue>(Incoming))
continue;
// Two inputs. // Two inputs.
if (OnlyInput && OnlyInput != Incoming) if (OnlyInput && OnlyInput != Incoming)
return nullptr; return nullptr;
OnlyInput = Incoming; OnlyInput = Incoming;
} }
assert(OnlyInput && "No live predecessors?"); assert(HasLivePreds && "No live predecessors?");
return OnlyInput; // If all incoming live value were undefs, return undef.
return OnlyInput ? OnlyInput : UndefValue::get(PN.getType());
}; };
DenseMap<Value *, Value *> FirstIterValue; DenseMap<Value *, Value *> FirstIterValue;

View File

@ -731,20 +731,19 @@ failure:
unreachable unreachable
} }
; TODO: We can break the backedge here by assuming that undef = sub.
define i32 @test_multiple_pred_undef_1(i1 %cond, i1 %cond2) { define i32 @test_multiple_pred_undef_1(i1 %cond, i1 %cond2) {
; CHECK-LABEL: @test_multiple_pred_undef_1( ; CHECK-LABEL: @test_multiple_pred_undef_1(
; CHECK-NEXT: entry: ; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP:%.*]] ; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop: ; CHECK: loop:
; CHECK-NEXT: [[SUM:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[SUM_NEXT:%.*]], [[BACKEDGE:%.*]] ] ; CHECK-NEXT: [[SUM:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT: [[SUB:%.*]] = sub i32 4, [[SUM]] ; CHECK-NEXT: [[SUB:%.*]] = sub i32 4, [[SUM]]
; CHECK-NEXT: [[IS_POSITIVE:%.*]] = icmp sgt i32 [[SUB]], 0 ; CHECK-NEXT: [[IS_POSITIVE:%.*]] = icmp sgt i32 [[SUB]], 0
; CHECK-NEXT: br i1 [[IS_POSITIVE]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]] ; CHECK-NEXT: br i1 [[IS_POSITIVE]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
; CHECK: if.true: ; CHECK: if.true:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_TRUE_2:%.*]] ; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_TRUE_2:%.*]]
; CHECK: if.true.1: ; CHECK: if.true.1:
; CHECK-NEXT: br label [[BACKEDGE]] ; CHECK-NEXT: br label [[BACKEDGE:%.*]]
; CHECK: if.true.2: ; CHECK: if.true.2:
; CHECK-NEXT: br label [[BACKEDGE]] ; CHECK-NEXT: br label [[BACKEDGE]]
; CHECK: if.false: ; CHECK: if.false:
@ -755,9 +754,11 @@ define i32 @test_multiple_pred_undef_1(i1 %cond, i1 %cond2) {
; CHECK-NEXT: br label [[BACKEDGE]] ; CHECK-NEXT: br label [[BACKEDGE]]
; CHECK: backedge: ; CHECK: backedge:
; CHECK-NEXT: [[MERGE_PHI:%.*]] = phi i32 [ 0, [[IF_FALSE_1]] ], [ 0, [[IF_FALSE_2]] ], [ [[SUB]], [[IF_TRUE_1]] ], [ undef, [[IF_TRUE_2]] ] ; CHECK-NEXT: [[MERGE_PHI:%.*]] = phi i32 [ 0, [[IF_FALSE_1]] ], [ 0, [[IF_FALSE_2]] ], [ [[SUB]], [[IF_TRUE_1]] ], [ undef, [[IF_TRUE_2]] ]
; CHECK-NEXT: [[SUM_NEXT]] = add i32 [[SUM]], [[MERGE_PHI]] ; CHECK-NEXT: [[SUM_NEXT:%.*]] = add i32 [[SUM]], [[MERGE_PHI]]
; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ne i32 [[SUM_NEXT]], 4 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ne i32 [[SUM_NEXT]], 4
; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[DONE:%.*]] ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[BACKEDGE_LOOP_CRIT_EDGE:%.*]], label [[DONE:%.*]]
; CHECK: backedge.loop_crit_edge:
; CHECK-NEXT: unreachable
; CHECK: done: ; CHECK: done:
; CHECK-NEXT: [[SUM_NEXT_LCSSA:%.*]] = phi i32 [ [[SUM_NEXT]], [[BACKEDGE]] ] ; CHECK-NEXT: [[SUM_NEXT_LCSSA:%.*]] = phi i32 [ [[SUM_NEXT]], [[BACKEDGE]] ]
; CHECK-NEXT: ret i32 [[SUM_NEXT_LCSSA]] ; CHECK-NEXT: ret i32 [[SUM_NEXT_LCSSA]]
@ -805,20 +806,19 @@ failure:
unreachable unreachable
} }
; TODO: We can break the backedge here by assuming that undef = sub.
define i32 @test_multiple_pred_undef_2(i1 %cond, i1 %cond2) { define i32 @test_multiple_pred_undef_2(i1 %cond, i1 %cond2) {
; CHECK-LABEL: @test_multiple_pred_undef_2( ; CHECK-LABEL: @test_multiple_pred_undef_2(
; CHECK-NEXT: entry: ; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP:%.*]] ; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop: ; CHECK: loop:
; CHECK-NEXT: [[SUM:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[SUM_NEXT:%.*]], [[BACKEDGE:%.*]] ] ; CHECK-NEXT: [[SUM:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT: [[SUB:%.*]] = sub i32 4, [[SUM]] ; CHECK-NEXT: [[SUB:%.*]] = sub i32 4, [[SUM]]
; CHECK-NEXT: [[IS_POSITIVE:%.*]] = icmp sgt i32 [[SUB]], 0 ; CHECK-NEXT: [[IS_POSITIVE:%.*]] = icmp sgt i32 [[SUB]], 0
; CHECK-NEXT: br i1 [[IS_POSITIVE]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]] ; CHECK-NEXT: br i1 [[IS_POSITIVE]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
; CHECK: if.true: ; CHECK: if.true:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_TRUE_2:%.*]] ; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_TRUE_2:%.*]]
; CHECK: if.true.1: ; CHECK: if.true.1:
; CHECK-NEXT: br label [[BACKEDGE]] ; CHECK-NEXT: br label [[BACKEDGE:%.*]]
; CHECK: if.true.2: ; CHECK: if.true.2:
; CHECK-NEXT: br label [[BACKEDGE]] ; CHECK-NEXT: br label [[BACKEDGE]]
; CHECK: if.false: ; CHECK: if.false:
@ -829,9 +829,11 @@ define i32 @test_multiple_pred_undef_2(i1 %cond, i1 %cond2) {
; CHECK-NEXT: br label [[BACKEDGE]] ; CHECK-NEXT: br label [[BACKEDGE]]
; CHECK: backedge: ; CHECK: backedge:
; CHECK-NEXT: [[MERGE_PHI:%.*]] = phi i32 [ 0, [[IF_FALSE_1]] ], [ 0, [[IF_FALSE_2]] ], [ undef, [[IF_TRUE_1]] ], [ [[SUB]], [[IF_TRUE_2]] ] ; CHECK-NEXT: [[MERGE_PHI:%.*]] = phi i32 [ 0, [[IF_FALSE_1]] ], [ 0, [[IF_FALSE_2]] ], [ undef, [[IF_TRUE_1]] ], [ [[SUB]], [[IF_TRUE_2]] ]
; CHECK-NEXT: [[SUM_NEXT]] = add i32 [[SUM]], [[MERGE_PHI]] ; CHECK-NEXT: [[SUM_NEXT:%.*]] = add i32 [[SUM]], [[MERGE_PHI]]
; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ne i32 [[SUM_NEXT]], 4 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ne i32 [[SUM_NEXT]], 4
; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[DONE:%.*]] ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[BACKEDGE_LOOP_CRIT_EDGE:%.*]], label [[DONE:%.*]]
; CHECK: backedge.loop_crit_edge:
; CHECK-NEXT: unreachable
; CHECK: done: ; CHECK: done:
; CHECK-NEXT: [[SUM_NEXT_LCSSA:%.*]] = phi i32 [ [[SUM_NEXT]], [[BACKEDGE]] ] ; CHECK-NEXT: [[SUM_NEXT_LCSSA:%.*]] = phi i32 [ [[SUM_NEXT]], [[BACKEDGE]] ]
; CHECK-NEXT: ret i32 [[SUM_NEXT_LCSSA]] ; CHECK-NEXT: ret i32 [[SUM_NEXT_LCSSA]]