[llvm] 0213c6d - [InstCombine] Use DL-aware constant folding for phi compare
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 1 07:02:46 PDT 2023
Author: Nikita Popov
Date: 2023-06-01T16:02:36+02:00
New Revision: 0213c6d0df1e073b046a3cb6d46c1808ddfa423e
URL: https://github.com/llvm/llvm-project/commit/0213c6d0df1e073b046a3cb6d46c1808ddfa423e
DIFF: https://github.com/llvm/llvm-project/commit/0213c6d0df1e073b046a3cb6d46c1808ddfa423e.diff
LOG: [InstCombine] Use DL-aware constant folding for phi compare
Serves the dual purpose of avoiding an extra InstCombine iteration
for the DL-aware folding and removing one icmp constexpr use.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp-constant-phi.ll
llvm/test/Transforms/InstCombine/indexed-gep-compares.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 7fb3f16b346b..b11f13ab40e1 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1382,17 +1382,18 @@ Instruction *InstCombinerImpl::foldICmpWithConstant(ICmpInst &Cmp) {
if (auto *Phi = dyn_cast<PHINode>(Op0))
if (all_of(Phi->operands(), [](Value *V) { return isa<Constant>(V); })) {
- Type *Ty = Cmp.getType();
- Builder.SetInsertPoint(Phi);
- PHINode *NewPhi =
- Builder.CreatePHI(Ty, Phi->getNumOperands());
- for (BasicBlock *Predecessor : predecessors(Phi->getParent())) {
- auto *Input =
- cast<Constant>(Phi->getIncomingValueForBlock(Predecessor));
- auto *BoolInput = ConstantExpr::getCompare(Pred, Input, C);
- NewPhi->addIncoming(BoolInput, Predecessor);
+ SmallVector<Constant *> Ops;
+ for (Value *V : Phi->incoming_values()) {
+ Constant *Res =
+ ConstantFoldCompareInstOperands(Pred, cast<Constant>(V), C, DL);
+ if (!Res)
+ return nullptr;
+ Ops.push_back(Res);
}
- NewPhi->takeName(&Cmp);
+ Builder.SetInsertPoint(Phi);
+ PHINode *NewPhi = Builder.CreatePHI(Cmp.getType(), Phi->getNumOperands());
+ for (auto [V, Pred] : zip(Ops, Phi->blocks()))
+ NewPhi->addIncoming(V, Pred);
return replaceInstUsesWith(Cmp, NewPhi);
}
diff --git a/llvm/test/Transforms/InstCombine/icmp-constant-phi.ll b/llvm/test/Transforms/InstCombine/icmp-constant-phi.ll
index 050033b93eda..95e1af4e7ee5 100644
--- a/llvm/test/Transforms/InstCombine/icmp-constant-phi.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-constant-phi.ll
@@ -166,7 +166,7 @@ define <2 x i1> @test_ne_int_vector(i1 %cond) {
; CHECK: if.false:
; CHECK-NEXT: br label [[MERGE]]
; CHECK: merge:
-; CHECK-NEXT: [[COMPARE:%.*]] = phi <2 x i1> [ <i1 true, i1 false>, [[IF_FALSE]] ], [ <i1 false, i1 true>, [[IF_TRUE]] ]
+; CHECK-NEXT: [[COMPARE:%.*]] = phi <2 x i1> [ <i1 false, i1 true>, [[IF_TRUE]] ], [ <i1 true, i1 false>, [[IF_FALSE]] ]
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: ret <2 x i1> [[COMPARE]]
diff --git a/llvm/test/Transforms/InstCombine/indexed-gep-compares.ll b/llvm/test/Transforms/InstCombine/indexed-gep-compares.ll
index c894587d24fc..6490a94a5270 100644
--- a/llvm/test/Transforms/InstCombine/indexed-gep-compares.ll
+++ b/llvm/test/Transforms/InstCombine/indexed-gep-compares.ll
@@ -226,7 +226,7 @@ define i1 @test7() {
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[BB7:%.*]]
; CHECK: bb7:
-; CHECK-NEXT: [[CMP:%.*]] = phi i1 [ true, [[BB7]] ], [ false, [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[CMP:%.*]] = phi i1 [ false, [[ENTRY:%.*]] ], [ true, [[BB7]] ]
; CHECK-NEXT: br i1 [[CMP]], label [[BB10:%.*]], label [[BB7]]
; CHECK: bb10:
; CHECK-NEXT: ret i1 [[CMP]]
More information about the llvm-commits
mailing list