[llvm] r333042 - [InstCombine] use nsw negation for abs libcalls
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Tue May 22 16:29:40 PDT 2018
Author: spatel
Date: Tue May 22 16:29:40 2018
New Revision: 333042
URL: http://llvm.org/viewvc/llvm-project?rev=333042&view=rev
Log:
[InstCombine] use nsw negation for abs libcalls
Also, produce the canonical IR abs (s<0) to be more efficient.
This is the libcall equivalent of the clang builtin change from:
rL333038
Pasting from that commit message:
The stdlib functions are defined in section 7.20.6.1 of the C standard with:
"If the result cannot be represented, the behavior is undefined."
That lets us mark the negation with 'nsw' because "sub i32 0, INT_MIN" would
be UB/poison.
Modified:
llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
llvm/trunk/test/Transforms/InstCombine/abs-1.ll
llvm/trunk/test/Transforms/InstCombine/call-callconv.ll
Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=333042&r1=333041&r2=333042&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Tue May 22 16:29:40 2018
@@ -1682,12 +1682,12 @@ Value *LibCallSimplifier::optimizeFls(Ca
}
Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilder<> &B) {
- // abs(x) -> x >s -1 ? x : -x
- Value *Op = CI->getArgOperand(0);
- Value *Pos =
- B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()), "ispos");
- Value *Neg = B.CreateNeg(Op, "neg");
- return B.CreateSelect(Pos, Op, Neg);
+ // abs(x) -> x <s 0 ? -x : x
+ // The negation has 'nsw' because abs of INT_MIN is undefined.
+ Value *X = CI->getArgOperand(0);
+ Value *IsNeg = B.CreateICmpSLT(X, Constant::getNullValue(X->getType()));
+ Value *NegX = B.CreateNSWNeg(X, "neg");
+ return B.CreateSelect(IsNeg, NegX, X);
}
Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilder<> &B) {
@@ -2716,4 +2716,4 @@ Value *FortifiedLibCallSimplifier::optim
FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
- : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
\ No newline at end of file
+ : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
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=333042&r1=333041&r2=333042&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/abs-1.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/abs-1.ll Tue May 22 16:29:40 2018
@@ -12,10 +12,10 @@ declare i64 @llabs(i64)
define i32 @test_abs(i32 %x) {
; CHECK-LABEL: @test_abs(
-; CHECK-NEXT: [[ISPOS:%.*]] = icmp slt i32 [[X:%.*]], 0
-; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X]]
-; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[ISPOS]], i32 [[NEG]], i32 [[X]]
-; CHECK-NEXT: ret i32 [[TMP1]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 0
+; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[NEG]], i32 [[X]]
+; CHECK-NEXT: ret i32 [[TMP2]]
;
%ret = call i32 @abs(i32 %x)
ret i32 %ret
@@ -23,10 +23,10 @@ define i32 @test_abs(i32 %x) {
define i64 @test_labs(i64 %x) {
; CHECK-LABEL: @test_labs(
-; CHECK-NEXT: [[ISPOS:%.*]] = icmp slt i64 [[X:%.*]], 0
-; CHECK-NEXT: [[NEG:%.*]] = sub i64 0, [[X]]
-; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[ISPOS]], i64 [[NEG]], i64 [[X]]
-; CHECK-NEXT: ret i64 [[TMP1]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i64 [[X:%.*]], 0
+; CHECK-NEXT: [[NEG:%.*]] = sub nsw i64 0, [[X]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i64 [[NEG]], i64 [[X]]
+; CHECK-NEXT: ret i64 [[TMP2]]
;
%ret = call i64 @labs(i64 %x)
ret i64 %ret
@@ -34,10 +34,10 @@ define i64 @test_labs(i64 %x) {
define i64 @test_llabs(i64 %x) {
; CHECK-LABEL: @test_llabs(
-; CHECK-NEXT: [[ISPOS:%.*]] = icmp slt i64 [[X:%.*]], 0
-; CHECK-NEXT: [[NEG:%.*]] = sub i64 0, [[X]]
-; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[ISPOS]], i64 [[NEG]], i64 [[X]]
-; CHECK-NEXT: ret i64 [[TMP1]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i64 [[X:%.*]], 0
+; CHECK-NEXT: [[NEG:%.*]] = sub nsw i64 0, [[X]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i64 [[NEG]], i64 [[X]]
+; CHECK-NEXT: ret i64 [[TMP2]]
;
%ret = call i64 @llabs(i64 %x)
ret i64 %ret
Modified: llvm/trunk/test/Transforms/InstCombine/call-callconv.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/call-callconv.ll?rev=333042&r1=333041&r2=333042&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/call-callconv.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/call-callconv.ll Tue May 22 16:29:40 2018
@@ -6,10 +6,10 @@
define arm_aapcscc i32 @_abs(i32 %i) nounwind readnone {
; CHECK-LABEL: @_abs(
-; CHECK-NEXT: [[ISPOS:%.*]] = icmp slt i32 [[I:%.*]], 0
-; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[I]]
-; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[ISPOS]], i32 [[NEG]], i32 [[I]]
-; CHECK-NEXT: ret i32 [[TMP1]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[I:%.*]], 0
+; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[I]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[NEG]], i32 [[I]]
+; CHECK-NEXT: ret i32 [[TMP2]]
;
%call = tail call arm_aapcscc i32 @abs(i32 %i) nounwind readnone
ret i32 %call
@@ -19,10 +19,10 @@ declare arm_aapcscc i32 @abs(i32) nounwi
define arm_aapcscc i32 @_labs(i32 %i) nounwind readnone {
; CHECK-LABEL: @_labs(
-; CHECK-NEXT: [[ISPOS:%.*]] = icmp slt i32 [[I:%.*]], 0
-; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[I]]
-; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[ISPOS]], i32 [[NEG]], i32 [[I]]
-; CHECK-NEXT: ret i32 [[TMP1]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[I:%.*]], 0
+; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[I]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[NEG]], i32 [[I]]
+; CHECK-NEXT: ret i32 [[TMP2]]
;
%call = tail call arm_aapcscc i32 @labs(i32 %i) nounwind readnone
ret i32 %call
More information about the llvm-commits
mailing list