[PATCH] D81151: [InstCombine] Simplify compare of Phi with constant inputs against a constant
Max Kazantsev via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 4 05:32:12 PDT 2020
mkazantsev created this revision.
mkazantsev added reviewers: spatel, fhahn, asbirlea, yrouban.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
We can simplify
icmp <pred> phi(C1, C2, ...), CI
with
phi(icmp(C1, CI), icmp(C2, CI), ...)
provided that all comparison of constants are constants themselves.
https://reviews.llvm.org/D81151
Files:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp-constant-phi.ll
Index: llvm/test/Transforms/InstCombine/icmp-constant-phi.ll
===================================================================
--- llvm/test/Transforms/InstCombine/icmp-constant-phi.ll
+++ llvm/test/Transforms/InstCombine/icmp-constant-phi.ll
@@ -2,8 +2,6 @@
; RUN: opt < %s -instcombine -S | FileCheck %s
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
-; TODO: Replace with boolean Phi.
-
define i1 @test_eq(i1 %cond) {
; CHECK-LABEL: @test_eq(
; CHECK-NEXT: entry:
@@ -13,11 +11,10 @@
; CHECK: if.false:
; CHECK-NEXT: br label [[MERGE]]
; CHECK: merge:
-; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ 123, [[IF_TRUE]] ], [ 456, [[IF_FALSE]] ]
+; CHECK-NEXT: [[COMPARE1:%.*]] = phi i1 [ true, [[IF_FALSE]] ], [ false, [[IF_TRUE]] ]
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: exit:
-; CHECK-NEXT: [[COMPARE:%.*]] = icmp eq i32 [[PHI]], 456
-; CHECK-NEXT: ret i1 [[COMPARE]]
+; CHECK-NEXT: ret i1 [[COMPARE1]]
;
entry:
br i1 %cond, label %if.true, label %if.false
@@ -46,11 +43,10 @@
; CHECK: if.false:
; CHECK-NEXT: br label [[MERGE]]
; CHECK: merge:
-; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ 123, [[IF_TRUE]] ], [ 456, [[IF_FALSE]] ]
+; CHECK-NEXT: [[COMPARE1:%.*]] = phi i1 [ false, [[IF_FALSE]] ], [ true, [[IF_TRUE]] ]
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: exit:
-; CHECK-NEXT: [[COMPARE:%.*]] = icmp ult i32 [[PHI]], 456
-; CHECK-NEXT: ret i1 [[COMPARE]]
+; CHECK-NEXT: ret i1 [[COMPARE1]]
;
entry:
br i1 %cond, label %if.true, label %if.false
@@ -110,11 +106,10 @@
; CHECK: if.false:
; CHECK-NEXT: br label [[MERGE]]
; CHECK: merge:
-; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ 123, [[IF_TRUE]] ], [ 456, [[IF_FALSE]] ]
+; CHECK-NEXT: [[COMPARE1:%.*]] = phi i1 [ false, [[IF_FALSE]] ], [ true, [[IF_TRUE]] ]
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: exit:
-; CHECK-NEXT: [[COMPARE:%.*]] = icmp ne i32 [[PHI]], 456
-; CHECK-NEXT: ret i1 [[COMPARE]]
+; CHECK-NEXT: ret i1 [[COMPARE1]]
;
entry:
br i1 %cond, label %if.true, label %if.false
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1458,6 +1458,24 @@
if (Instruction *Res = processUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this))
return Res;
+ // icmp(phi(C1, C2, ...), CI) -> phi(icmp(C1, CI), icmp(C2, CI), ...).
+ if (auto *Phi = dyn_cast<PHINode>(Op0))
+ if (Phi->hasOneUse() &&
+ all_of(Phi->operands(), [](Value *V) { return isa<ConstantInt>(V); })) {
+ Type *Ty = Cmp.getType();
+ Builder.SetInsertPoint(Phi);
+ PHINode *NewPhi =
+ Builder.CreatePHI(Ty, Phi->getNumOperands(), Cmp.getName());
+ const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
+ for (BasicBlock *Predecessor : predecessors(Phi->getParent())) {
+ auto *Input =
+ cast<ConstantInt>(Phi->getIncomingValueForBlock(Predecessor));
+ auto *BoolInput = SimplifyICmpInst(Pred, Input, CI, Q);
+ NewPhi->addIncoming(BoolInput, Predecessor);
+ }
+ return replaceInstUsesWith(Cmp, NewPhi);
+ }
+
return nullptr;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81151.268437.patch
Type: text/x-patch
Size: 3308 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200604/4797d6a6/attachment.bin>
More information about the llvm-commits
mailing list