[CodeGen] produce the LLVM canonical form of abs
We chose the 'slt' form as canonical in IR with:
rL332819
...so we should generate that form directly for efficiency.
llvm-svn: 332989
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index c6f0b30..7f1e125 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -1251,16 +1251,12 @@
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:
diff --git a/clang/test/CodeGen/builtin-abs.c b/clang/test/CodeGen/builtin-abs.c
index b68d465..a6ae36c 100644
--- a/clang/test/CodeGen/builtin-abs.c
+++ b/clang/test/CodeGen/builtin-abs.c
@@ -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 @@
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 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);
}