[PATCH] D47202: [CodeGen] use nsw negation for abs

Sanjay Patel via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 22 16:06:27 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL333038: [CodeGen] use nsw negation for builtin abs (authored by spatel, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D47202?vs=148020&id=148123#toc

Repository:
  rC Clang

https://reviews.llvm.org/D47202

Files:
  cfe/trunk/lib/CodeGen/CGBuiltin.cpp
  cfe/trunk/test/CodeGen/builtin-abs.c


Index: cfe/trunk/test/CodeGen/builtin-abs.c
===================================================================
--- cfe/trunk/test/CodeGen/builtin-abs.c
+++ cfe/trunk/test/CodeGen/builtin-abs.c
@@ -2,25 +2,25 @@
 
 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]]
 //
   return __builtin_abs(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]]
 //
   return __builtin_labs(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]]
 //
Index: cfe/trunk/lib/CodeGen/CGBuiltin.cpp
===================================================================
--- cfe/trunk/lib/CodeGen/CGBuiltin.cpp
+++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp
@@ -1252,8 +1252,9 @@
   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");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47202.148123.patch
Type: text/x-patch
Size: 1862 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180522/39c0bb4e/attachment.bin>


More information about the cfe-commits mailing list