Skip to content

Commit fc06cce

Browse files
committed
Revert "Respect integer overflow handling in abs builtin"
This reverts commit 1783185, which broke the buildbots, starting with when it was first built in https://lab.llvm.org/buildbot/#/builders/85/builds/18390 (N.B. I think the patch is uncovering real bugs; the revert is simply to keep the tree green and the buildbots useful, because I'm not confident how to fix-forward all the found bugs.)
1 parent 9a44eed commit fc06cce

File tree

4 files changed

+7
-146
lines changed

4 files changed

+7
-146
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,6 @@ Bug Fixes in This Version
154154
- Fix a hang on valid C code passing a function type as an argument to
155155
``typeof`` to form a function declaration.
156156
(`#64713 <https://github.com/llvm/llvm-project/issues/64713>_`)
157-
- Clang now respects ``-fwrapv`` and ``-ftrapv`` for ``__builtin_abs`` and
158-
``abs`` builtins.
159-
(`#45129 <https://github.com/llvm/llvm-project/issues/45129>`_,
160-
`#45794 <https://github.com/llvm/llvm-project/issues/45794>`_)
161157

162158
Bug Fixes to Compiler Builtins
163159
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -293,9 +289,6 @@ Static Analyzer
293289

294290
Sanitizers
295291
----------
296-
- ``-fsanitize=signed-integer-overflow`` now instruments ``__builtin_abs`` and
297-
``abs`` builtins.
298-
299292

300293
Python Binding Changes
301294
----------------------

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,49 +1768,6 @@ Value *CodeGenFunction::EmitCheckedArgForBuiltin(const Expr *E,
17681768
return ArgValue;
17691769
}
17701770

1771-
static Value *EmitAbs(CodeGenFunction &CGF, Value *ArgValue, bool HasNSW) {
1772-
// X < 0 ? -X : X
1773-
// TODO: Use phi-node (for better SimplifyCFGPass)
1774-
Value *NegOp = CGF.Builder.CreateNeg(ArgValue, "neg", false, HasNSW);
1775-
Constant *Zero = llvm::Constant::getNullValue(ArgValue->getType());
1776-
Value *CmpResult = CGF.Builder.CreateICmpSLT(ArgValue, Zero, "abscond");
1777-
return CGF.Builder.CreateSelect(CmpResult, NegOp, ArgValue, "abs");
1778-
}
1779-
1780-
static Value *EmitOverflowCheckedAbs(CodeGenFunction &CGF, const CallExpr *E,
1781-
bool SanitizeOverflow) {
1782-
Value *ArgValue = CGF.EmitScalarExpr(E->getArg(0));
1783-
1784-
// Try to eliminate overflow check.
1785-
if (const auto *VCI = dyn_cast<llvm::ConstantInt>(ArgValue)) {
1786-
if (!VCI->isMinSignedValue()) {
1787-
return EmitAbs(CGF, ArgValue, true);
1788-
}
1789-
}
1790-
1791-
CodeGenFunction::SanitizerScope SanScope(&CGF);
1792-
1793-
Constant *Zero = Constant::getNullValue(ArgValue->getType());
1794-
Value *ResultAndOverflow = CGF.Builder.CreateBinaryIntrinsic(
1795-
Intrinsic::ssub_with_overflow, Zero, ArgValue);
1796-
Value *Result = CGF.Builder.CreateExtractValue(ResultAndOverflow, 0);
1797-
Value *NotOverflow = CGF.Builder.CreateNot(
1798-
CGF.Builder.CreateExtractValue(ResultAndOverflow, 1));
1799-
1800-
// TODO: support -ftrapv-handler.
1801-
if (SanitizeOverflow) {
1802-
CGF.EmitCheck({{NotOverflow, SanitizerKind::SignedIntegerOverflow}},
1803-
SanitizerHandler::NegateOverflow,
1804-
{CGF.EmitCheckSourceLocation(E->getArg(0)->getExprLoc()),
1805-
CGF.EmitCheckTypeDescriptor(E->getType())},
1806-
{ArgValue});
1807-
} else
1808-
CGF.EmitTrapCheck(NotOverflow, SanitizerHandler::SubOverflow);
1809-
1810-
Value *CmpResult = CGF.Builder.CreateICmpSLT(ArgValue, Zero, "abscond");
1811-
return CGF.Builder.CreateSelect(CmpResult, Result, ArgValue, "abs");
1812-
}
1813-
18141771
/// Get the argument type for arguments to os_log_helper.
18151772
static CanQualType getOSLogArgType(ASTContext &C, int Size) {
18161773
QualType UnsignedTy = C.getIntTypeForBitwidth(Size * 8, /*Signed=*/false);
@@ -2669,29 +2626,16 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
26692626
Builder.CreateCall(CGM.getIntrinsic(Intrinsic::vacopy), {DstPtr, SrcPtr});
26702627
return RValue::get(nullptr);
26712628
}
2672-
case Builtin::BIabs:
2673-
case Builtin::BIlabs:
2674-
case Builtin::BIllabs:
26752629
case Builtin::BI__builtin_abs:
26762630
case Builtin::BI__builtin_labs:
26772631
case Builtin::BI__builtin_llabs: {
2678-
bool SanitizeOverflow = SanOpts.has(SanitizerKind::SignedIntegerOverflow);
2679-
2680-
Value *Result;
2681-
switch (getLangOpts().getSignedOverflowBehavior()) {
2682-
case LangOptions::SOB_Defined:
2683-
Result = EmitAbs(*this, EmitScalarExpr(E->getArg(0)), false);
2684-
break;
2685-
case LangOptions::SOB_Undefined:
2686-
if (!SanitizeOverflow) {
2687-
Result = EmitAbs(*this, EmitScalarExpr(E->getArg(0)), true);
2688-
break;
2689-
}
2690-
[[fallthrough]];
2691-
case LangOptions::SOB_Trapping:
2692-
Result = EmitOverflowCheckedAbs(*this, E, SanitizeOverflow);
2693-
break;
2694-
}
2632+
// X < 0 ? -X : X
2633+
// The negation has 'nsw' because abs of INT_MIN is undefined.
2634+
Value *ArgValue = EmitScalarExpr(E->getArg(0));
2635+
Value *NegOp = Builder.CreateNSWNeg(ArgValue, "neg");
2636+
Constant *Zero = llvm::Constant::getNullValue(ArgValue->getType());
2637+
Value *CmpResult = Builder.CreateICmpSLT(ArgValue, Zero, "abscond");
2638+
Value *Result = Builder.CreateSelect(CmpResult, NegOp, ArgValue, "abs");
26952639
return RValue::get(Result);
26962640
}
26972641
case Builtin::BI__builtin_complex: {

clang/test/CodeGen/abs-overflow.c

Lines changed: 0 additions & 46 deletions
This file was deleted.

clang/test/ubsan/TestCases/Misc/abs.cpp

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)