[PATCH] D85055: [InstCombine] Fold abs(-x) -> abs(x)
Craig Topper via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 1 13:25:19 PDT 2020
This revision was automatically updated to reflect the committed changes.
Closed by commit rG4a19e6156ed5: [InstCombine] Fold abs(-x) -> abs(x) (authored by craig.topper).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D85055/new/
https://reviews.llvm.org/D85055
Files:
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
Index: llvm/test/Transforms/InstCombine/abs-intrinsic.ll
===================================================================
--- llvm/test/Transforms/InstCombine/abs-intrinsic.ll
+++ llvm/test/Transforms/InstCombine/abs-intrinsic.ll
@@ -165,3 +165,23 @@
%c2 = icmp sge <4 x i32> %abs, zeroinitializer
ret <4 x i1> %c2
}
+
+define i32 @abs_of_neg(i32 %x) {
+; CHECK-LABEL: @abs_of_neg(
+; CHECK-NEXT: [[B:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
+; CHECK-NEXT: ret i32 [[B]]
+;
+ %a = sub i32 0, %x
+ %b = call i32 @llvm.abs.i32(i32 %a, i1 false)
+ ret i32 %b
+}
+
+define <4 x i32> @abs_of_neg_vec(<4 x i32> %x) {
+; CHECK-LABEL: @abs_of_neg_vec(
+; CHECK-NEXT: [[B:%.*]] = call <4 x i32> @llvm.abs.v4i32(<4 x i32> [[X:%.*]], i1 false)
+; CHECK-NEXT: ret <4 x i32> [[B]]
+;
+ %a = sub nsw <4 x i32> zeroinitializer, %x
+ %b = call <4 x i32> @llvm.abs.v4i32(<4 x i32> %a, i1 false)
+ ret <4 x i32> %b
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -769,6 +769,16 @@
if (Value *V = lowerObjectSizeCall(II, DL, &TLI, /*MustSucceed=*/false))
return replaceInstUsesWith(CI, V);
return nullptr;
+ case Intrinsic::abs: {
+ Value *IIOperand = II->getArgOperand(0);
+ // abs(-x) -> abs(x)
+ // TODO: Copy nsw if it was present on the neg?
+ Value *X;
+ if (match(IIOperand, m_Neg(m_Value(X))))
+ return replaceOperand(*II, 0, X);
+
+ break;
+ }
case Intrinsic::bswap: {
Value *IIOperand = II->getArgOperand(0);
Value *X = nullptr;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85055.282413.patch
Type: text/x-patch
Size: 1718 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200801/b0742bf9/attachment.bin>
More information about the llvm-commits
mailing list