Skip to content

[InstCombine] Simplify phi using KnownBits of condition #134712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,44 @@ Instruction *InstCombinerImpl::visitPHINode(PHINode &PN) {
return replaceInstUsesWith(PN, &IdenticalPN);
}

if (PN.getType()->isIntegerTy()) {
BasicBlock *PNBB = PN.getParent();
KnownBits Known(PN.getType()->getScalarSizeInBits());
bool MadeChange = false;
for (unsigned I = 0, E = PN.getNumIncomingValues(); I != E; ++I) {
Value *V = PN.getIncomingValue(I);
if (isa<ConstantInt>(V))
continue;

ArrayRef<BranchInst *> BRs = SQ.DC->conditionsFor(V);
if (BRs.empty())
continue;

Known.resetAll();
Instruction *CtxI = PN.getIncomingBlock(I)->getTerminator();
SimplifyQuery Q = SQ.getWithInstruction(CtxI);
BranchInst *BI = dyn_cast<BranchInst>(CtxI);
if (BI && BI->isConditional() && llvm::is_contained(BRs, BI)) {
CondContext CC(BI->getCondition());
CC.AffectedValues.insert(V);
if (BI->getSuccessor(1) == PNBB)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that the two branched did not have the same label but found this

if ((TrueSucc == CxtPhi->getParent()) !=
(FalseSucc == CxtPhi->getParent())) {

But I'm not able to create a test that have the same label for the two branches https://godbolt.org/z/avb161nYe
so is this a case that I shall handle?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://godbolt.org/z/vacTYPoha is the way to spell it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help with how to make the test.
I do not think that it needs to be handled here as the condition in the branch will be replaced by a constant so it will not be in the DomConditionCache.
see

// If the condition is irrelevant, remove the use so that other
// transforms on the condition become more effective.
if (!isa<ConstantInt>(Cond) && BI.getSuccessor(0) == BI.getSuccessor(1))
return replaceOperand(BI, 0, ConstantInt::getFalse(Cond->getType()));

CC.Invert = true;
llvm::computeKnownBits(V, Known, /* Depth */ 0,
Q.getWithCondContext(CC));
} else {
llvm::computeKnownBits(V, Known, /* Depth */ 0, Q);
}

if (Known.isConstant()) {
replaceOperand(PN, I,
ConstantInt::get(V->getType(), Known.getConstant()));
MadeChange = true;
}
}
if (MadeChange)
return &PN;
}

// If this is an integer PHI and we know that it has an illegal type, see if
// it is only used by trunc or trunc(lshr) operations. If so, we split the
// PHI into the various pieces being extracted. This sort of thing is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ define {ptr, i64} @test4(i1 %cond1, i1 %cond2, ptr %p1, ptr %p2) {
; CHECK-NEXT: br i1 [[COND3_NOT]], label %[[EXIT]], label %[[BBB4]]
; CHECK: [[EXIT]]:
; CHECK-NEXT: [[VAL1:%.*]] = phi ptr [ [[VAL11]], %[[BBB2]] ], [ [[VAL21]], %[[BBB3]] ], [ [[VAL31]], %[[BBB4]] ]
; CHECK-NEXT: [[VAL2:%.*]] = phi i64 [ [[VAL12]], %[[BBB2]] ], [ [[VAL22]], %[[BBB3]] ], [ [[VAL32]], %[[BBB4]] ]
; CHECK-NEXT: [[VAL2:%.*]] = phi i64 [ [[VAL12]], %[[BBB2]] ], [ [[VAL22]], %[[BBB3]] ], [ 0, %[[BBB4]] ]
; CHECK-NEXT: [[TMP:%.*]] = insertvalue { ptr, i64 } poison, ptr [[VAL1]], 0
; CHECK-NEXT: [[RES:%.*]] = insertvalue { ptr, i64 } [[TMP]], i64 [[VAL2]], 1
; CHECK-NEXT: ret { ptr, i64 } [[RES]]
Expand Down
9 changes: 3 additions & 6 deletions llvm/test/Transforms/InstCombine/known-phi-br.ll
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ define i64 @limit_i64_eq_7(i64 %x) {
; CHECK: body:
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[X]], [[ENTRY:%.*]] ], [ 7, [[BODY]] ]
; CHECK-NEXT: ret i64 [[RES]]
; CHECK-NEXT: ret i64 7
;
entry:
%cmp = icmp eq i64 %x, 7
Expand All @@ -36,8 +35,7 @@ define i64 @limit_i64_eq_7_commuted(i64 %x) {
; CHECK: body:
; CHECK-NEXT: br label [[BODY]]
; CHECK: end:
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[X]], [[END]] ], [ 7, [[ENTRY:%.*]] ]
; CHECK-NEXT: ret i64 [[RES]]
; CHECK-NEXT: ret i64 7
;
entry:
%cmp = icmp eq i64 %x, 7
Expand Down Expand Up @@ -80,8 +78,7 @@ define i64 @limit_i64_ne_255(i64 %x) {
; CHECK: body:
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[X]], [[ENTRY:%.*]] ], [ 255, [[BODY]] ]
; CHECK-NEXT: ret i64 [[RES]]
; CHECK-NEXT: ret i64 255
;
entry:
%cmp = icmp ne i64 %x, 255
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/LoopVectorize/induction.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3176,7 +3176,7 @@ define i32 @testoverflowcheck() {
; IND-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[TMP3]], [[N_VEC]]
; IND-NEXT: br i1 [[CMP_N]], label [[LOOPEXIT:%.*]], label [[SCALAR_PH]]
; IND: scalar.ph:
; IND-NEXT: [[BC_RESUME_VAL:%.*]] = phi i8 [ [[IND_END]], [[MIDDLE_BLOCK]] ], [ [[DOTPR_I]], [[ENTRY:%.*]] ]
; IND-NEXT: [[BC_RESUME_VAL:%.*]] = phi i8 [ [[IND_END]], [[MIDDLE_BLOCK]] ], [ -1, [[ENTRY:%.*]] ]
; IND-NEXT: [[BC_MERGE_RDX:%.*]] = phi i32 [ [[TMP7]], [[MIDDLE_BLOCK]] ], [ [[C_PROMOTED_I]], [[ENTRY]] ]
; IND-NEXT: br label [[COND_END_I:%.*]]
; IND: cond.end.i:
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/PGOProfile/chr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ define i32 @test_chr_17(i32 %i, i1 %j) !prof !14 {
; CHECK-NEXT: [[S:%.*]] = add nuw nsw i32 [[TMP0]], [[I]]
; CHECK-NEXT: br label [[BB1]]
; CHECK: bb1:
; CHECK-NEXT: [[P:%.*]] = phi i32 [ [[I]], [[BBQ]] ], [ [[TMP0]], [[BBE]] ], [ [[S]], [[BB0]] ]
; CHECK-NEXT: [[P:%.*]] = phi i32 [ [[I]], [[BBQ]] ], [ 0, [[BBE]] ], [ [[S]], [[BB0]] ]
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[I]], 2
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0
; CHECK-NEXT: br i1 [[TMP3]], label [[BB3]], label [[BB2:%.*]], !prof [[PROF16]]
Expand Down