[llvm] r333101 - [InstCombine] Negate ABS/NABS patterns by swapping the select operands to remove the negation
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Wed May 23 10:29:03 PDT 2018
Author: ctopper
Date: Wed May 23 10:29:03 2018
New Revision: 333101
URL: http://llvm.org/viewvc/llvm-project?rev=333101&view=rev
Log:
[InstCombine] Negate ABS/NABS patterns by swapping the select operands to remove the negation
Differential Revision: https://reviews.llvm.org/D47236
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/trunk/test/Transforms/InstCombine/abs-1.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=333101&r1=333100&r2=333101&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Wed May 23 10:29:03 2018
@@ -1628,6 +1628,22 @@ Instruction *InstCombiner::visitSub(Bina
Value *ShAmtOp = cast<Instruction>(Op1)->getOperand(1);
return BinaryOperator::CreateLShr(X, ShAmtOp);
}
+
+ if (Op1->hasOneUse()) {
+ Value *LHS, *RHS;
+ SelectPatternFlavor SPF = matchSelectPattern(Op1, LHS, RHS).Flavor;
+ if (SPF == SPF_ABS || SPF == SPF_NABS) {
+ // This is a negate of an ABS/NABS pattern. Just swap the operands
+ // of the select.
+ SelectInst *SI = cast<SelectInst>(Op1);
+ Value *TrueVal = SI->getTrueValue();
+ Value *FalseVal = SI->getFalseValue();
+ SI->setTrueValue(FalseVal);
+ SI->setFalseValue(TrueVal);
+ // Don't swap prof metadata, we didn't change the branch behavior.
+ return replaceInstUsesWith(I, SI);
+ }
+ }
}
// Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
Modified: llvm/trunk/test/Transforms/InstCombine/abs-1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/abs-1.ll?rev=333101&r1=333100&r2=333101&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/abs-1.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/abs-1.ll Wed May 23 10:29:03 2018
@@ -266,3 +266,30 @@ define i8 @shifty_abs_too_many_uses(i8 %
ret i8 %abs
}
+define i8 @negate_abs(i8 %x) {
+; CHECK-LABEL: @negate_abs(
+; CHECK-NEXT: [[N:%.*]] = sub i8 0, [[X:%.*]]
+; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[X]], 0
+; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], i8 [[X]], i8 [[N]]
+; CHECK-NEXT: ret i8 [[S]]
+;
+ %n = sub i8 0, %x
+ %c = icmp slt i8 %x, 0
+ %s = select i1 %c, i8 %n, i8 %x
+ %r = sub i8 0, %s
+ ret i8 %r
+}
+
+define <2 x i8> @negate_nabs(<2 x i8> %x) {
+; CHECK-LABEL: @negate_nabs(
+; CHECK-NEXT: [[N:%.*]] = sub <2 x i8> zeroinitializer, [[X:%.*]]
+; CHECK-NEXT: [[C:%.*]] = icmp slt <2 x i8> [[X]], zeroinitializer
+; CHECK-NEXT: [[S:%.*]] = select <2 x i1> [[C]], <2 x i8> [[N]], <2 x i8> [[X]]
+; CHECK-NEXT: ret <2 x i8> [[S]]
+;
+ %n = sub <2 x i8> zeroinitializer, %x
+ %c = icmp slt <2 x i8> %x, zeroinitializer
+ %s = select <2 x i1> %c, <2 x i8> %x, <2 x i8> %n
+ %r = sub <2 x i8> zeroinitializer, %s
+ ret <2 x i8> %r
+}
More information about the llvm-commits
mailing list