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(Op0)) + if (Phi->hasOneUse() && + all_of(Phi->operands(), [](Value *V) { return isa(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(Phi->getIncomingValueForBlock(Predecessor)); + auto *BoolInput = SimplifyICmpInst(Pred, Input, CI, Q); + NewPhi->addIncoming(BoolInput, Predecessor); + } + return replaceInstUsesWith(Cmp, NewPhi); + } + return nullptr; } 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