]> granicus.if.org Git - clang/commitdiff
[CodeGen] use nsw negation for builtin abs
authorSanjay Patel <spatel@rotateright.com>
Tue, 22 May 2018 23:02:13 +0000 (23:02 +0000)
committerSanjay Patel <spatel@rotateright.com>
Tue, 22 May 2018 23:02:13 +0000 (23:02 +0000)
The clang builtins have the same semantics as the stdlib functions.
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.

Differential Revision: https://reviews.llvm.org/D47202

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@333038 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGBuiltin.cpp
test/CodeGen/builtin-abs.c

index 724b3d234a9f3665c159ee2a4addd9579e0d7895..5d8db1032104a21a521abfbbdf1b9dcd36367b63 100644 (file)
@@ -1252,8 +1252,9 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
   case Builtin::BI__builtin_labs:
   case Builtin::BI__builtin_llabs: {
     // X < 0 ? -X : X
+    // The negation has 'nsw' because abs of INT_MIN is undefined.
     Value *ArgValue = EmitScalarExpr(E->getArg(0));
-    Value *NegOp = Builder.CreateNeg(ArgValue, "neg");
+    Value *NegOp = Builder.CreateNSWNeg(ArgValue, "neg");
     Constant *Zero = llvm::Constant::getNullValue(ArgValue->getType());
     Value *CmpResult = Builder.CreateICmpSLT(ArgValue, Zero, "abscond");
     Value *Result = Builder.CreateSelect(CmpResult, NegOp, ArgValue, "abs");
index a6ae36c8fb8542e217e1023e57b3d6a84ac296f2..791395368a8c01441c7a410ebbeab0ff056ec16c 100644 (file)
@@ -2,7 +2,7 @@
 
 int absi(int x) {
 // CHECK-LABEL: @absi(
-// CHECK:   [[NEG:%.*]] = sub i32 0, [[X:%.*]]
+// CHECK:   [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
 // CHECK:   [[CMP:%.*]] = icmp slt i32 [[X]], 0
 // CHECK:   [[SEL:%.*]] = select i1 [[CMP]], i32 [[NEG]], i32 [[X]]
 //
@@ -11,7 +11,7 @@ int absi(int x) {
 
 long absl(long x) {
 // CHECK-LABEL: @absl(
-// CHECK:   [[NEG:%.*]] = sub i64 0, [[X:%.*]]
+// CHECK:   [[NEG:%.*]] = sub nsw i64 0, [[X:%.*]]
 // CHECK:   [[CMP:%.*]] = icmp slt i64 [[X]], 0
 // CHECK:   [[SEL:%.*]] = select i1 [[CMP]], i64 [[NEG]], i64 [[X]]
 //
@@ -20,7 +20,7 @@ long absl(long x) {
 
 long long absll(long long x) {
 // CHECK-LABEL: @absll(
-// CHECK:   [[NEG:%.*]] = sub i64 0, [[X:%.*]]
+// CHECK:   [[NEG:%.*]] = sub nsw i64 0, [[X:%.*]]
 // CHECK:   [[CMP:%.*]] = icmp slt i64 [[X]], 0
 // CHECK:   [[SEL:%.*]] = select i1 [[CMP]], i64 [[NEG]], i64 [[X]]
 //