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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333042
91177308-0d34-0410-b5e6-
96231b3b80d8
}
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) {
FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
- : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
\ No newline at end of file
+ : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
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
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
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
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
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