diff --git a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp --- a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp +++ b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp @@ -1033,9 +1033,14 @@ auto *TorF = (BI->getSuccessor(0) == BB) ? ConstantInt::getTrue(BB->getContext()) : ConstantInt::getFalse(BB->getContext()); - auto MatchBinOp = [](Instruction *I, unsigned Opcode) { - if (BinaryOperator *BOp = dyn_cast(I)) - return BOp->getOpcode() == Opcode; + auto MatchBinOp = [](Instruction *I, unsigned Opcode, Value *&LHS, + Value *&RHS) { + if (Opcode == Instruction::And && + match(I, m_LogicalAnd(m_Value(LHS), m_Value(RHS)))) + return true; + else if (Opcode == Instruction::Or && + match(I, m_LogicalOr(m_Value(LHS), m_Value(RHS)))) + return true; return false; }; // If the condition is AND operation, we can propagate its operands into the @@ -1066,8 +1071,9 @@ } } - if (MatchBinOp(Curr, PropagateOpcode)) - for (auto &Op : cast(Curr)->operands()) + Value *LHS, *RHS; + if (MatchBinOp(Curr, PropagateOpcode, LHS, RHS)) + for (auto &Op : { LHS, RHS }) if (Instruction *OPI = dyn_cast(Op)) if (SimpleValue::canHandle(OPI) && Visited.insert(OPI).second) WorkList.push_back(OPI); diff --git a/llvm/test/Transforms/EarlyCSE/and_or.ll b/llvm/test/Transforms/EarlyCSE/and_or.ll --- a/llvm/test/Transforms/EarlyCSE/and_or.ll +++ b/llvm/test/Transforms/EarlyCSE/and_or.ll @@ -62,8 +62,7 @@ ; CHECK-NEXT: [[AND_COND:%.*]] = select i1 [[COND]], i1 [[C:%.*]], i1 false ; CHECK-NEXT: br i1 [[AND_COND]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]] ; CHECK: if.true: -; CHECK-NEXT: [[X:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[B]] -; CHECK-NEXT: ret i32 [[X]] +; CHECK-NEXT: ret i32 [[A]] ; CHECK: if.false: ; CHECK-NEXT: [[Y:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[B]] ; CHECK-NEXT: ret i32 [[Y]] @@ -122,8 +121,7 @@ ; CHECK-NEXT: [[X:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[B]] ; CHECK-NEXT: ret i32 [[X]] ; CHECK: if.false: -; CHECK-NEXT: [[Y:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[B]] -; CHECK-NEXT: ret i32 [[Y]] +; CHECK-NEXT: ret i32 [[B]] ; entry: %cond = icmp slt i32 %a, %b @@ -179,8 +177,7 @@ ; CHECK-NEXT: [[AND_COND2:%.*]] = select i1 [[AND_COND1]], i1 [[C2:%.*]], i1 false ; CHECK-NEXT: br i1 [[AND_COND2]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]] ; CHECK: if.true: -; CHECK-NEXT: [[X:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[B]] -; CHECK-NEXT: ret i32 [[X]] +; CHECK-NEXT: ret i32 [[A]] ; CHECK: if.false: ; CHECK-NEXT: [[Y:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[B]] ; CHECK-NEXT: ret i32 [[Y]] @@ -243,8 +240,7 @@ ; CHECK-NEXT: [[X:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[B]] ; CHECK-NEXT: ret i32 [[X]] ; CHECK: if.false: -; CHECK-NEXT: [[Y:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[B]] -; CHECK-NEXT: ret i32 [[Y]] +; CHECK-NEXT: ret i32 [[B]] ; entry: %cond = icmp slt i32 %a, %b