Index: llvm/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/lib/Analysis/InstructionSimplify.cpp +++ llvm/lib/Analysis/InstructionSimplify.cpp @@ -4557,6 +4557,10 @@ Value *X, *Y; if (match(Cond, m_LogicalOr(m_Value(X), m_Value(Y)))) { + // (X || Y) ? X : Y --> X + if ((X == TrueVal && Y == FalseVal) || (X == FalseVal && Y == TrueVal)) + return TrueVal; + // (X || Y) ? false : X --> false (commuted 2 ways) if (match(TrueVal, m_ZeroInt()) && (X == FalseVal || Y == FalseVal)) return ConstantInt::getFalse(Cond->getType()); Index: llvm/test/Transforms/InstSimplify/select-logical.ll =================================================================== --- llvm/test/Transforms/InstSimplify/select-logical.ll +++ llvm/test/Transforms/InstSimplify/select-logical.ll @@ -381,9 +381,7 @@ define i1 @select_or_same_op(i1 %x, i1 %y) { ; CHECK-LABEL: @select_or_same_op( -; CHECK-NEXT: [[OR:%.*]] = or i1 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 [[X]], i1 [[Y]] -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: ret i1 [[X:%.*]] ; %or = or i1 %x, %y %r = select i1 %or, i1 %x, i1 %y @@ -393,9 +391,7 @@ define i1 @select_or_same_op_commute(i1 %x, i1 %y) { ; CHECK-LABEL: @select_or_same_op_commute( -; CHECK-NEXT: [[OR:%.*]] = or i1 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 [[Y]], i1 [[X]] -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: ret i1 [[Y:%.*]] ; %or = or i1 %x, %y %r = select i1 %or, i1 %y, i1 %x @@ -405,9 +401,7 @@ define <2 x i1> @select_or_same_op_vector1(<2 x i1> %x, <2 x i1> %y) { ; CHECK-LABEL: @select_or_same_op_vector1( -; CHECK-NEXT: [[OR:%.*]] = or <2 x i1> [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[OR]], <2 x i1> [[X]], <2 x i1> [[Y]] -; CHECK-NEXT: ret <2 x i1> [[R]] +; CHECK-NEXT: ret <2 x i1> [[X:%.*]] ; %or = or <2 x i1> %x, %y %r = select <2 x i1> %or, <2 x i1> %x, <2 x i1> %y @@ -417,9 +411,7 @@ define i1 @select_logic_or1_same_op(i1 %x, i1 %y) { ; CHECK-LABEL: @select_logic_or1_same_op( -; CHECK-NEXT: [[OR:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 [[X]], i1 [[Y]] -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: ret i1 [[X:%.*]] ; %or = select i1 %x, i1 true, i1 %y %r = select i1 %or, i1 %x, i1 %y @@ -429,9 +421,7 @@ define i1 @select_logic_or2_same_op(i1 %x, i1 %y) { ; CHECK-LABEL: @select_logic_or2_same_op( -; CHECK-NEXT: [[OR:%.*]] = select i1 [[Y:%.*]], i1 true, i1 [[X:%.*]] -; CHECK-NEXT: [[R:%.*]] = select i1 [[OR]], i1 [[X]], i1 [[Y]] -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: ret i1 [[X:%.*]] ; %or = select i1 %y, i1 true, i1 %x %r = select i1 %or, i1 %x, i1 %y @@ -441,9 +431,7 @@ define <2 x i1> @select_or_same_op_vector2(<2 x i1> %x, <2 x i1> %y) { ; CHECK-LABEL: @select_or_same_op_vector2( -; CHECK-NEXT: [[OR:%.*]] = select <2 x i1> [[X:%.*]], <2 x i1> , <2 x i1> [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[OR]], <2 x i1> [[X]], <2 x i1> [[Y]] -; CHECK-NEXT: ret <2 x i1> [[R]] +; CHECK-NEXT: ret <2 x i1> [[X:%.*]] ; %or = select <2 x i1> %x, <2 x i1> , <2 x i1> %y %r = select <2 x i1> %or, <2 x i1> %x, <2 x i1> %y