[llvm] r242235 - [InstCombine] Generalize sub of selects optimization to all BinaryOperators
David Majnemer
david.majnemer at gmail.com
Tue Jul 14 15:39:23 PDT 2015
Author: majnemer
Date: Tue Jul 14 17:39:23 2015
New Revision: 242235
URL: http://llvm.org/viewvc/llvm-project?rev=242235&view=rev
Log:
[InstCombine] Generalize sub of selects optimization to all BinaryOperators
This exposes further optimization opportunities if the selects are
correlated.
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/trunk/test/Transforms/InstCombine/add2.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=242235&r1=242234&r2=242235&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Tue Jul 14 17:39:23 2015
@@ -1611,32 +1611,6 @@ Instruction *InstCombiner::visitSub(Bina
return BinaryOperator::CreateAnd(A, B);
}
- // (sub (select (a, c, b)), (select (a, d, b))) -> (select (a, (sub c, d), 0))
- // (sub (select (a, b, c)), (select (a, b, d))) -> (select (a, 0, (sub c, d)))
- if (auto *SI0 = dyn_cast<SelectInst>(Op0)) {
- if (auto *SI1 = dyn_cast<SelectInst>(Op1)) {
- if (SI0->getCondition() == SI1->getCondition()) {
- if (Value *V = SimplifySubInst(
- SI0->getFalseValue(), SI1->getFalseValue(), I.hasNoSignedWrap(),
- I.hasNoUnsignedWrap(), DL, TLI, DT, AC))
- return SelectInst::Create(
- SI0->getCondition(),
- Builder->CreateSub(SI0->getTrueValue(), SI1->getTrueValue(), "",
- /*HasNUW=*/I.hasNoUnsignedWrap(),
- /*HasNSW=*/I.hasNoSignedWrap()),
- V);
- if (Value *V = SimplifySubInst(SI0->getTrueValue(), SI1->getTrueValue(),
- I.hasNoSignedWrap(),
- I.hasNoUnsignedWrap(), DL, TLI, DT, AC))
- return SelectInst::Create(
- SI0->getCondition(), V,
- Builder->CreateSub(SI0->getFalseValue(), SI1->getFalseValue(), "",
- /*HasNUW=*/I.hasNoUnsignedWrap(),
- /*HasNSW=*/I.hasNoSignedWrap()));
- }
- }
- }
-
if (Op0->hasOneUse()) {
Value *Y = nullptr;
// ((X | Y) - X) --> (~X & Y)
Modified: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=242235&r1=242234&r2=242235&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp Tue Jul 14 17:39:23 2015
@@ -623,6 +623,33 @@ Value *InstCombiner::SimplifyUsingDistri
}
}
+ // (op (select (a, c, b)), (select (a, d, b))) -> (select (a, (op c, d), 0))
+ // (op (select (a, b, c)), (select (a, b, d))) -> (select (a, 0, (op c, d)))
+ if (auto *SI0 = dyn_cast<SelectInst>(LHS)) {
+ if (auto *SI1 = dyn_cast<SelectInst>(RHS)) {
+ if (SI0->getCondition() == SI1->getCondition()) {
+ Value *SI = nullptr;
+ if (Value *V = SimplifyBinOp(TopLevelOpcode, SI0->getFalseValue(),
+ SI1->getFalseValue(), DL, TLI, DT, AC))
+ SI = Builder->CreateSelect(SI0->getCondition(),
+ Builder->CreateBinOp(TopLevelOpcode,
+ SI0->getTrueValue(),
+ SI1->getTrueValue()),
+ V);
+ if (Value *V = SimplifyBinOp(TopLevelOpcode, SI0->getTrueValue(),
+ SI1->getTrueValue(), DL, TLI, DT, AC))
+ SI = Builder->CreateSelect(
+ SI0->getCondition(), V,
+ Builder->CreateBinOp(TopLevelOpcode, SI0->getFalseValue(),
+ SI1->getFalseValue()));
+ if (SI) {
+ SI->takeName(&I);
+ return SI;
+ }
+ }
+ }
+ }
+
return nullptr;
}
Modified: llvm/trunk/test/Transforms/InstCombine/add2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/add2.ll?rev=242235&r1=242234&r2=242235&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/add2.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/add2.ll Tue Jul 14 17:39:23 2015
@@ -395,3 +395,13 @@ define i8 @add_of_mul(i8 %x, i8 %y, i8 %
%sum = add nsw i8 %mA, %mB
ret i8 %sum
}
+
+define i32 @add_of_selects(i1 %A, i32 %B) {
+ %sel0 = select i1 %A, i32 0, i32 -2
+ %sel1 = select i1 %A, i32 %B, i32 2
+ %add = add i32 %sel0, %sel1
+ ret i32 %add
+; CHECK-LABEL: @add_of_selects(
+; CHECK-NEXT: %[[sel:.*]] = select i1 %A, i32 %B, i32 0
+; CHECK-NEXT: ret i32 %[[sel]]
+}
More information about the llvm-commits
mailing list