Skip to content

Commit 6bab515

Browse files
committed
[InstCombine] Simplify phi using KnownBits of condition
Simplify the arms of a phi based on the KnownBits implied by the condition for the predecessor basic block.
1 parent 72436b3 commit 6bab515

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,44 @@ Instruction *InstCombinerImpl::visitPHINode(PHINode &PN) {
16251625
return replaceInstUsesWith(PN, &IdenticalPN);
16261626
}
16271627

1628+
if (PN.getType()->isIntegerTy()) {
1629+
BasicBlock *PNBB = PN.getParent();
1630+
KnownBits Known(PN.getType()->getScalarSizeInBits());
1631+
bool MadeChange = false;
1632+
for (unsigned I = 0, E = PN.getNumIncomingValues(); I != E; ++I) {
1633+
Value *V = PN.getIncomingValue(I);
1634+
if (isa<ConstantInt>(V))
1635+
continue;
1636+
1637+
ArrayRef<BranchInst *> BRs = SQ.DC->conditionsFor(V);
1638+
if (BRs.empty())
1639+
continue;
1640+
1641+
Known.resetAll();
1642+
Instruction *CtxI = PN.getIncomingBlock(I)->getTerminator();
1643+
SimplifyQuery Q = SQ.getWithInstruction(CtxI);
1644+
BranchInst *BI = dyn_cast<BranchInst>(CtxI);
1645+
if (BI && BI->isConditional() && llvm::is_contained(BRs, BI)) {
1646+
CondContext CC(BI->getCondition());
1647+
CC.AffectedValues.insert(V);
1648+
if (BI->getSuccessor(1) == PNBB)
1649+
CC.Invert = true;
1650+
llvm::computeKnownBits(V, Known, /* Depth */ 0,
1651+
Q.getWithCondContext(CC));
1652+
} else {
1653+
llvm::computeKnownBits(V, Known, /* Depth */ 0, Q);
1654+
}
1655+
1656+
if (Known.isConstant()) {
1657+
replaceOperand(PN, I,
1658+
ConstantInt::get(V->getType(), Known.getConstant()));
1659+
MadeChange = true;
1660+
}
1661+
}
1662+
if (MadeChange)
1663+
return &PN;
1664+
}
1665+
16281666
// If this is an integer PHI and we know that it has an illegal type, see if
16291667
// it is only used by trunc or trunc(lshr) operations. If so, we split the
16301668
// PHI into the various pieces being extracted. This sort of thing is

llvm/test/Transforms/InstCombine/fold-aggregate-reconstruction.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ define {ptr, i64} @test4(i1 %cond1, i1 %cond2, ptr %p1, ptr %p2) {
178178
; CHECK-NEXT: br i1 [[COND3_NOT]], label %[[EXIT]], label %[[BBB4]]
179179
; CHECK: [[EXIT]]:
180180
; CHECK-NEXT: [[VAL1:%.*]] = phi ptr [ [[VAL11]], %[[BBB2]] ], [ [[VAL21]], %[[BBB3]] ], [ [[VAL31]], %[[BBB4]] ]
181-
; CHECK-NEXT: [[VAL2:%.*]] = phi i64 [ [[VAL12]], %[[BBB2]] ], [ [[VAL22]], %[[BBB3]] ], [ [[VAL32]], %[[BBB4]] ]
181+
; CHECK-NEXT: [[VAL2:%.*]] = phi i64 [ [[VAL12]], %[[BBB2]] ], [ [[VAL22]], %[[BBB3]] ], [ 0, %[[BBB4]] ]
182182
; CHECK-NEXT: [[TMP:%.*]] = insertvalue { ptr, i64 } poison, ptr [[VAL1]], 0
183183
; CHECK-NEXT: [[RES:%.*]] = insertvalue { ptr, i64 } [[TMP]], i64 [[VAL2]], 1
184184
; CHECK-NEXT: ret { ptr, i64 } [[RES]]

llvm/test/Transforms/InstCombine/known-phi-br.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ define i64 @limit_i64_eq_7(i64 %x) {
1515
; CHECK: body:
1616
; CHECK-NEXT: br label [[END]]
1717
; CHECK: end:
18-
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[X]], [[ENTRY:%.*]] ], [ 7, [[BODY]] ]
19-
; CHECK-NEXT: ret i64 [[RES]]
18+
; CHECK-NEXT: ret i64 7
2019
;
2120
entry:
2221
%cmp = icmp eq i64 %x, 7
@@ -36,8 +35,7 @@ define i64 @limit_i64_eq_7_commuted(i64 %x) {
3635
; CHECK: body:
3736
; CHECK-NEXT: br label [[BODY]]
3837
; CHECK: end:
39-
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[X]], [[END]] ], [ 7, [[ENTRY:%.*]] ]
40-
; CHECK-NEXT: ret i64 [[RES]]
38+
; CHECK-NEXT: ret i64 7
4139
;
4240
entry:
4341
%cmp = icmp eq i64 %x, 7
@@ -80,8 +78,7 @@ define i64 @limit_i64_ne_255(i64 %x) {
8078
; CHECK: body:
8179
; CHECK-NEXT: br label [[END]]
8280
; CHECK: end:
83-
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[X]], [[ENTRY:%.*]] ], [ 255, [[BODY]] ]
84-
; CHECK-NEXT: ret i64 [[RES]]
81+
; CHECK-NEXT: ret i64 255
8582
;
8683
entry:
8784
%cmp = icmp ne i64 %x, 255

llvm/test/Transforms/LoopVectorize/induction.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3176,7 +3176,7 @@ define i32 @testoverflowcheck() {
31763176
; IND-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[TMP3]], [[N_VEC]]
31773177
; IND-NEXT: br i1 [[CMP_N]], label [[LOOPEXIT:%.*]], label [[SCALAR_PH]]
31783178
; IND: scalar.ph:
3179-
; IND-NEXT: [[BC_RESUME_VAL:%.*]] = phi i8 [ [[IND_END]], [[MIDDLE_BLOCK]] ], [ [[DOTPR_I]], [[ENTRY:%.*]] ]
3179+
; IND-NEXT: [[BC_RESUME_VAL:%.*]] = phi i8 [ [[IND_END]], [[MIDDLE_BLOCK]] ], [ -1, [[ENTRY:%.*]] ]
31803180
; IND-NEXT: [[BC_MERGE_RDX:%.*]] = phi i32 [ [[TMP7]], [[MIDDLE_BLOCK]] ], [ [[C_PROMOTED_I]], [[ENTRY]] ]
31813181
; IND-NEXT: br label [[COND_END_I:%.*]]
31823182
; IND: cond.end.i:

llvm/test/Transforms/PGOProfile/chr.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,7 @@ define i32 @test_chr_17(i32 %i, i1 %j) !prof !14 {
15841584
; CHECK-NEXT: [[S:%.*]] = add nuw nsw i32 [[TMP0]], [[I]]
15851585
; CHECK-NEXT: br label [[BB1]]
15861586
; CHECK: bb1:
1587-
; CHECK-NEXT: [[P:%.*]] = phi i32 [ [[I]], [[BBQ]] ], [ [[TMP0]], [[BBE]] ], [ [[S]], [[BB0]] ]
1587+
; CHECK-NEXT: [[P:%.*]] = phi i32 [ [[I]], [[BBQ]] ], [ 0, [[BBE]] ], [ [[S]], [[BB0]] ]
15881588
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[I]], 2
15891589
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0
15901590
; CHECK-NEXT: br i1 [[TMP3]], label [[BB3]], label [[BB2:%.*]], !prof [[PROF16]]

0 commit comments

Comments
 (0)