r332989 - [CodeGen] produce the LLVM canonical form of abs
Sanjay Patel via cfe-commits
cfe-commits at lists.llvm.org
Tue May 22 08:36:51 PDT 2018
Author: spatel
Date: Tue May 22 08:36:50 2018
New Revision: 332989
URL: http://llvm.org/viewvc/llvm-project?rev=332989&view=rev
Log:
[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.
Modified:
cfe/trunk/lib/CodeGen/CGBuiltin.cpp
cfe/trunk/test/CodeGen/builtin-abs.c
Modified: cfe/trunk/lib/CodeGen/CGBuiltin.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=332989&r1=332988&r2=332989&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGBuiltin.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp Tue May 22 08:36:50 2018
@@ -1251,16 +1251,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(
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:
Modified: cfe/trunk/test/CodeGen/builtin-abs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/builtin-abs.c?rev=332989&r1=332988&r2=332989&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/builtin-abs.c (original)
+++ cfe/trunk/test/CodeGen/builtin-abs.c Tue May 22 08:36:50 2018
@@ -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);
}
More information about the cfe-commits
mailing list