@@ -1768,49 +1768,6 @@ Value *CodeGenFunction::EmitCheckedArgForBuiltin(const Expr *E,
1768
1768
return ArgValue;
1769
1769
}
1770
1770
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
-
1814
1771
/// Get the argument type for arguments to os_log_helper.
1815
1772
static CanQualType getOSLogArgType(ASTContext &C, int Size) {
1816
1773
QualType UnsignedTy = C.getIntTypeForBitwidth(Size * 8, /*Signed=*/false);
@@ -2669,29 +2626,16 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
2669
2626
Builder.CreateCall(CGM.getIntrinsic(Intrinsic::vacopy), {DstPtr, SrcPtr});
2670
2627
return RValue::get(nullptr);
2671
2628
}
2672
- case Builtin::BIabs:
2673
- case Builtin::BIlabs:
2674
- case Builtin::BIllabs:
2675
2629
case Builtin::BI__builtin_abs:
2676
2630
case Builtin::BI__builtin_labs:
2677
2631
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");
2695
2639
return RValue::get(Result);
2696
2640
}
2697
2641
case Builtin::BI__builtin_complex: {
0 commit comments