diff options
author | Sanjay Patel <spatel@rotateright.com> | 2018-05-22 23:02:13 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2018-05-22 23:02:13 +0000 |
commit | 74c7fb002f7ac84ae745c0e41313c953e40ffd76 (patch) | |
tree | 77d3be0cde0b325df1d1c3970fcf7f1f498d23ae /clang | |
parent | aa6d13e501a56279ee8aa289fbad2b4c7c2a22c9 (diff) | |
download | bcm5719-llvm-74c7fb002f7ac84ae745c0e41313c953e40ffd76.tar.gz bcm5719-llvm-74c7fb002f7ac84ae745c0e41313c953e40ffd76.zip |
[CodeGen] use nsw negation for builtin abs
The clang builtins have the same semantics as the stdlib functions.
The stdlib functions are defined in section 7.20.6.1 of the C standard with:
"If the result cannot be represented, the behavior is undefined."
That lets us mark the negation with 'nsw' because "sub i32 0, INT_MIN" would
be UB/poison.
Differential Revision: https://reviews.llvm.org/D47202
llvm-svn: 333038
Diffstat (limited to 'clang')
-rw-r--r-- | clang/lib/CodeGen/CGBuiltin.cpp | 3 | ||||
-rw-r--r-- | clang/test/CodeGen/builtin-abs.c | 6 |
2 files changed, 5 insertions, 4 deletions
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 724b3d234a9..5d8db103210 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -1252,8 +1252,9 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, 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"); diff --git a/clang/test/CodeGen/builtin-abs.c b/clang/test/CodeGen/builtin-abs.c index a6ae36c8fb8..791395368a8 100644 --- a/clang/test/CodeGen/builtin-abs.c +++ b/clang/test/CodeGen/builtin-abs.c @@ -2,7 +2,7 @@ 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]] // @@ -11,7 +11,7 @@ int absi(int 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]] // @@ -20,7 +20,7 @@ long absl(long 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]] // |