]> granicus.if.org Git - clang/commitdiff
[CodeGen] produce the LLVM canonical form of abs
authorSanjay Patel <spatel@rotateright.com>
Tue, 22 May 2018 15:36:50 +0000 (15:36 +0000)
committerSanjay Patel <spatel@rotateright.com>
Tue, 22 May 2018 15:36:50 +0000 (15:36 +0000)
We chose the 'slt' form as canonical in IR with:
rL332819
...so we should generate that form directly for efficiency.

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

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

index c6f0b30bc76b38ae2320515aeb9ca3429cabab39..7f1e12556048e95209cd90da6a5bca92039a9092 100644 (file)
@@ -1251,16 +1251,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
   case Builtin::BI__builtin_abs:
   case Builtin::BI__builtin_labs:
   case Builtin::BI__builtin_llabs: {
+    // X < 0 ? -X : X
     Value *ArgValue = EmitScalarExpr(E->getArg(0));
-
     Value *NegOp = Builder.CreateNeg(ArgValue, "neg");
-    Value *CmpResult =
-    Builder.CreateICmpSGE(ArgValue,
-                          llvm::Constant::getNullValue(ArgValue->getType()),
-                                                            "abscond");
-    Value *Result =
-      Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs");
-
+    Constant *Zero = llvm::Constant::getNullValue(ArgValue->getType());
+    Value *CmpResult = Builder.CreateICmpSLT(ArgValue, Zero, "abscond");
+    Value *Result = Builder.CreateSelect(CmpResult, NegOp, ArgValue, "abs");
     return RValue::get(Result);
   }
   case Builtin::BI__builtin_conj:
index b68d465d2f35cd953d60484dd0819636581a149d..a6ae36c8fb8542e217e1023e57b3d6a84ac296f2 100644 (file)
@@ -3,8 +3,8 @@
 int absi(int x) {
 // CHECK-LABEL: @absi(
 // CHECK:   [[NEG:%.*]] = sub i32 0, [[X:%.*]]
-// CHECK:   [[CMP:%.*]] = icmp sge i32 [[X]], 0
-// CHECK:   [[SEL:%.*]] = select i1 [[CMP]], i32 [[X]], i32 [[NEG]]
+// CHECK:   [[CMP:%.*]] = icmp slt i32 [[X]], 0
+// CHECK:   [[SEL:%.*]] = select i1 [[CMP]], i32 [[NEG]], i32 [[X]]
 //
   return __builtin_abs(x);
 }
@@ -12,8 +12,8 @@ int absi(int x) {
 long absl(long x) {
 // CHECK-LABEL: @absl(
 // CHECK:   [[NEG:%.*]] = sub i64 0, [[X:%.*]]
-// CHECK:   [[CMP:%.*]] = icmp sge i64 [[X]], 0
-// CHECK:   [[SEL:%.*]] = select i1 [[CMP]], i64 [[X]], i64 [[NEG]]
+// CHECK:   [[CMP:%.*]] = icmp slt i64 [[X]], 0
+// CHECK:   [[SEL:%.*]] = select i1 [[CMP]], i64 [[NEG]], i64 [[X]]
 //
   return __builtin_labs(x);
 }
@@ -21,8 +21,8 @@ long absl(long x) {
 long long absll(long long x) {
 // CHECK-LABEL: @absll(
 // CHECK:   [[NEG:%.*]] = sub i64 0, [[X:%.*]]
-// CHECK:   [[CMP:%.*]] = icmp sge i64 [[X]], 0
-// CHECK:   [[SEL:%.*]] = select i1 [[CMP]], i64 [[X]], i64 [[NEG]]
+// CHECK:   [[CMP:%.*]] = icmp slt i64 [[X]], 0
+// CHECK:   [[SEL:%.*]] = select i1 [[CMP]], i64 [[NEG]], i64 [[X]]
 //
   return __builtin_llabs(x);
 }