[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
This commit is contained in:
Sanjay Patel 2018-05-22 15:36:50 +00:00
parent b6e5d4ead1
commit 1ff6b27940
2 changed files with 10 additions and 14 deletions

View 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:

View 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);
}