28 #include "llvm/ADT/Optional.h"
29 #include "llvm/IR/CFG.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/GetElementPtrTypeIterator.h"
34 #include "llvm/IR/GlobalVariable.h"
35 #include "llvm/IR/Intrinsics.h"
36 #include "llvm/IR/Module.h"
39 using namespace clang;
40 using namespace CodeGen;
54 bool mayHaveIntegerOverflow(llvm::ConstantInt *LHS, llvm::ConstantInt *RHS,
56 llvm::APInt &Result) {
59 const auto &LHSAP = LHS->getValue();
60 const auto &RHSAP = RHS->getValue();
61 if (Opcode == BO_Add) {
63 Result = LHSAP.sadd_ov(RHSAP, Overflow);
65 Result = LHSAP.uadd_ov(RHSAP, Overflow);
66 }
else if (Opcode == BO_Sub) {
68 Result = LHSAP.ssub_ov(RHSAP, Overflow);
70 Result = LHSAP.usub_ov(RHSAP, Overflow);
71 }
else if (Opcode == BO_Mul) {
73 Result = LHSAP.smul_ov(RHSAP, Overflow);
75 Result = LHSAP.umul_ov(RHSAP, Overflow);
76 }
else if (Opcode == BO_Div || Opcode == BO_Rem) {
77 if (Signed && !RHS->isZero())
78 Result = LHSAP.sdiv_ov(RHSAP, Overflow);
94 bool mayHaveIntegerOverflow()
const {
96 auto *LHSCI = dyn_cast<llvm::ConstantInt>(LHS);
97 auto *RHSCI = dyn_cast<llvm::ConstantInt>(RHS);
102 return ::mayHaveIntegerOverflow(
103 LHSCI, RHSCI, Opcode, Ty->hasSignedIntegerRepresentation(), Result);
107 bool isDivremOp()
const {
108 return Opcode == BO_Div || Opcode == BO_Rem || Opcode == BO_DivAssign ||
109 Opcode == BO_RemAssign;
113 bool mayHaveIntegerDivisionByZero()
const {
115 if (
auto *CI = dyn_cast<llvm::ConstantInt>(RHS))
121 bool mayHaveFloatDivisionByZero()
const {
123 if (
auto *CFP = dyn_cast<llvm::ConstantFP>(RHS))
124 return CFP->isZero();
129 static bool MustVisitNullValue(
const Expr *
E) {
152 static bool IsWidenedIntegerOp(
const ASTContext &Ctx,
const Expr *E) {
153 return getUnwidenedIntegerType(Ctx, E).hasValue();
157 static bool CanElideOverflowCheck(
const ASTContext &Ctx,
const BinOpInfo &Op) {
158 assert((isa<UnaryOperator>(Op.E) || isa<BinaryOperator>(Op.E)) &&
159 "Expected a unary or binary operator");
163 if (!Op.mayHaveIntegerOverflow())
167 if (
const auto *UO = dyn_cast<UnaryOperator>(Op.E))
168 return IsWidenedIntegerOp(Ctx, UO->getSubExpr());
172 const auto *BO = cast<BinaryOperator>(Op.E);
173 auto OptionalLHSTy = getUnwidenedIntegerType(Ctx, BO->getLHS());
177 auto OptionalRHSTy = getUnwidenedIntegerType(Ctx, BO->getRHS());
186 if ((Op.Opcode != BO_Mul && Op.Opcode != BO_MulAssign) ||
192 unsigned PromotedSize = Ctx.
getTypeSize(Op.E->getType());
193 return (2 * Ctx.
getTypeSize(LHSTy)) < PromotedSize ||
198 static void updateFastMathFlags(llvm::FastMathFlags &FMF,
204 static Value *propagateFMFlags(
Value *V,
const BinOpInfo &Op) {
205 if (
auto *
I = dyn_cast<llvm::Instruction>(V)) {
206 llvm::FastMathFlags FMF =
I->getFastMathFlags();
207 updateFastMathFlags(FMF, Op.FPFeatures);
208 I->setFastMathFlags(FMF);
213 class ScalarExprEmitter
217 bool IgnoreResultAssign;
218 llvm::LLVMContext &VMContext;
223 VMContext(cgf.getLLVMContext()) {
230 bool TestAndClearIgnoreResultAssign() {
231 bool I = IgnoreResultAssign;
232 IgnoreResultAssign =
false;
237 LValue EmitLValue(
const Expr *E) {
return CGF.EmitLValue(E); }
239 return CGF.EmitCheckedLValue(E, TCK);
242 void EmitBinOpCheck(
ArrayRef<std::pair<Value *, SanitizerMask>> Checks,
243 const BinOpInfo &Info);
246 return CGF.EmitLoadOfLValue(LV, Loc).getScalarVal();
249 void EmitLValueAlignmentAssumption(
const Expr *E,
Value *V) {
250 const AlignValueAttr *AVAttr =
nullptr;
251 if (
const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
255 if (
const auto *TTy =
257 AVAttr = TTy->getDecl()->
getAttr<AlignValueAttr>();
261 if (isa<ParmVarDecl>(VD))
264 AVAttr = VD->
getAttr<AlignValueAttr>();
269 if (
const auto *TTy =
270 dyn_cast<TypedefType>(E->
getType()))
271 AVAttr = TTy->getDecl()->getAttr<AlignValueAttr>();
276 Value *AlignmentValue = CGF.EmitScalarExpr(AVAttr->getAlignment());
277 llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(AlignmentValue);
278 CGF.EmitAlignmentAssumption(V, AlignmentCI->getZExtValue());
288 EmitLValueAlignmentAssumption(E, V);
298 void EmitFloatConversionCheck(
Value *OrigSrc,
QualType OrigSrcType,
322 llvm::Value *Zero = llvm::Constant::getNullValue(V->getType());
323 return Builder.CreateFCmpUNE(V, Zero,
"tobool");
328 Value *Zero = CGF.CGM.getNullPointer(cast<llvm::PointerType>(V->getType()), QT);
330 return Builder.CreateICmpNE(V, Zero,
"tobool");
337 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(V)) {
338 if (ZI->getOperand(0)->getType() ==
Builder.getInt1Ty()) {
339 Value *Result = ZI->getOperand(0);
344 ZI->eraseFromParent();
349 return Builder.CreateIsNotNull(V,
"tobool");
362 S->
dump(CGF.getContext().getSourceManager());
363 llvm_unreachable(
"Stmt can't have complex result type!");
377 return CGF.EmitCoawaitExpr(*S).getScalarVal();
380 return CGF.EmitCoyieldExpr(*S).getScalarVal();
391 return llvm::ConstantFP::get(VMContext, E->
getValue());
394 return llvm::ConstantInt::get(ConvertType(E->
getType()), E->
getValue());
397 return llvm::ConstantInt::get(ConvertType(E->
getType()), E->
getValue());
400 return llvm::ConstantInt::get(ConvertType(E->
getType()), E->
getValue());
403 return EmitNullValue(E->
getType());
406 return EmitNullValue(E->
getType());
420 return CGF.EmitPseudoObjectRValue(E).getScalarVal();
425 return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E), E->
getExprLoc());
428 return CGF.getOpaqueRValueMapping(E).getScalarVal();
434 if (result.isReference())
435 return EmitLoadOfLValue(result.getReferenceLValue(CGF, E),
437 return result.getValue();
439 return EmitLoadOfLValue(E);
443 return CGF.EmitObjCSelectorExpr(E);
446 return CGF.EmitObjCProtocolExpr(E);
449 return EmitLoadOfLValue(E);
454 return EmitLoadOfLValue(E);
455 return CGF.EmitObjCMessageExpr(E).getScalarVal();
459 LValue LV = CGF.EmitObjCIsaExpr(E);
469 if (Version <= CGF.CGM.getTarget().getPlatformMinVersion())
470 return llvm::ConstantInt::get(
Builder.getInt1Ty(), 1);
474 llvm::ConstantInt::get(CGF.CGM.Int32Ty, Version.
getMajor()),
475 llvm::ConstantInt::get(CGF.CGM.Int32Ty, Min ? *Min : 0),
476 llvm::ConstantInt::get(CGF.CGM.Int32Ty, SMin ? *SMin : 0),
479 return CGF.EmitBuiltinAvailable(Args);
486 Value *VisitExtVectorElementExpr(
Expr *E) {
return EmitLoadOfLValue(E); }
488 return EmitLoadOfLValue(E);
494 assert(CGF.getArrayInitIndex() &&
495 "ArrayInitIndexExpr not inside an ArrayInitLoopExpr?");
496 return CGF.getArrayInitIndex();
500 return EmitNullValue(E->
getType());
503 CGF.CGM.EmitExplicitCastExprType(E, &CGF);
504 return VisitCastExpr(E);
510 return EmitLoadOfLValue(E);
512 Value *V = CGF.EmitCallExpr(E).getScalarVal();
514 EmitLValueAlignmentAssumption(E, V);
523 return EmitScalarPrePostIncDec(E, LV,
false,
false);
527 return EmitScalarPrePostIncDec(E, LV,
true,
false);
531 return EmitScalarPrePostIncDec(E, LV,
false,
true);
535 return EmitScalarPrePostIncDec(E, LV,
true,
true);
543 bool isInc,
bool isPre);
547 if (isa<MemberPointerType>(E->
getType()))
548 return CGF.CGM.getMemberPointerConstant(E);
550 return EmitLValue(E->
getSubExpr()).getPointer();
555 return EmitLoadOfLValue(E);
559 TestAndClearIgnoreResultAssign();
573 return EmitLoadOfLValue(E);
584 return CGF.LoadCXXThis();
589 return CGF.EmitCXXNewExpr(E);
592 CGF.EmitCXXDeleteExpr(E);
597 return llvm::ConstantInt::get(ConvertType(E->
getType()), E->
getValue());
614 CGF.EmitScalarExpr(E->
getBase());
619 return EmitNullValue(E->
getType());
623 CGF.EmitCXXThrowExpr(E);
632 Value *EmitMul(
const BinOpInfo &Ops) {
633 if (Ops.Ty->isSignedIntegerOrEnumerationType()) {
634 switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
636 return Builder.CreateMul(Ops.LHS, Ops.RHS,
"mul");
638 if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow))
639 return Builder.CreateNSWMul(Ops.LHS, Ops.RHS,
"mul");
642 if (CanElideOverflowCheck(CGF.getContext(), Ops))
643 return Builder.CreateNSWMul(Ops.LHS, Ops.RHS,
"mul");
644 return EmitOverflowCheckedBinOp(Ops);
648 if (Ops.Ty->isUnsignedIntegerType() &&
649 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow) &&
650 !CanElideOverflowCheck(CGF.getContext(), Ops))
651 return EmitOverflowCheckedBinOp(Ops);
653 if (Ops.LHS->getType()->isFPOrFPVectorTy()) {
655 return propagateFMFlags(V, Ops);
657 return Builder.CreateMul(Ops.LHS, Ops.RHS,
"mul");
661 Value *EmitOverflowCheckedBinOp(
const BinOpInfo &Ops);
664 void EmitUndefinedBehaviorIntegerDivAndRemCheck(
const BinOpInfo &Ops,
668 Value *EmitDiv(
const BinOpInfo &Ops);
669 Value *EmitRem(
const BinOpInfo &Ops);
670 Value *EmitAdd(
const BinOpInfo &Ops);
671 Value *EmitSub(
const BinOpInfo &Ops);
672 Value *EmitShl(
const BinOpInfo &Ops);
673 Value *EmitShr(
const BinOpInfo &Ops);
674 Value *EmitAnd(
const BinOpInfo &Ops) {
675 return Builder.CreateAnd(Ops.LHS, Ops.RHS,
"and");
677 Value *EmitXor(
const BinOpInfo &Ops) {
678 return Builder.CreateXor(Ops.LHS, Ops.RHS,
"xor");
680 Value *EmitOr (
const BinOpInfo &Ops) {
681 return Builder.CreateOr(Ops.LHS, Ops.RHS,
"or");
686 Value *(ScalarExprEmitter::*F)(
const BinOpInfo &),
690 Value *(ScalarExprEmitter::*F)(
const BinOpInfo &));
693 #define HANDLEBINOP(OP) \
694 Value *VisitBin ## OP(const BinaryOperator *E) { \
695 return Emit ## OP(EmitBinOps(E)); \
697 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
698 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
714 llvm::CmpInst::Predicate SICmpOpc,
715 llvm::CmpInst::Predicate FCmpOpc);
716 #define VISITCOMP(CODE, UI, SI, FP) \
717 Value *VisitBin##CODE(const BinaryOperator *E) { \
718 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
719 llvm::FCmpInst::FP); }
720 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT)
721 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT)
722 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE)
723 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE)
724 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ)
725 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE)
734 Value *VisitBinPtrMemD(
const Expr *E) {
return EmitLoadOfLValue(E); }
735 Value *VisitBinPtrMemI(
const Expr *E) {
return EmitLoadOfLValue(E); }
743 return CGF.EmitObjCStringLiteral(E);
746 return CGF.EmitObjCBoxedExpr(E);
749 return CGF.EmitObjCArrayLiteral(E);
752 return CGF.EmitObjCDictionaryLiteral(E);
766 assert(SrcType.
isCanonical() &&
"EmitScalarConversion strips typedefs");
769 return EmitFloatToBoolConversion(Src);
772 return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, Src, MPT);
774 assert((SrcType->
isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
775 "Unknown scalar type to convert");
777 if (isa<llvm::IntegerType>(Src->getType()))
778 return EmitIntToBoolConversion(Src);
780 assert(isa<llvm::PointerType>(Src->getType()));
781 return EmitPointerToBoolConversion(Src, SrcType);
784 void ScalarExprEmitter::EmitFloatConversionCheck(
794 if (llvm::IntegerType *IntTy = dyn_cast<llvm::IntegerType>(SrcTy)) {
800 APFloat LargestFloat =
801 APFloat::getLargest(CGF.getContext().getFloatTypeSemantics(DstType));
802 APSInt LargestInt(IntTy->getBitWidth(), SrcIsUnsigned);
805 if (LargestFloat.convertToInteger(LargestInt, APFloat::rmTowardZero,
806 &IsExact) != APFloat::opOK)
811 llvm::Value *Max = llvm::ConstantInt::get(VMContext, LargestInt);
813 Check =
Builder.CreateICmpULE(Src, Max);
815 llvm::Value *Min = llvm::ConstantInt::get(VMContext, -LargestInt);
818 Check =
Builder.CreateAnd(GE, LE);
821 const llvm::fltSemantics &SrcSema =
822 CGF.getContext().getFloatTypeSemantics(OrigSrcType);
823 if (isa<llvm::IntegerType>(DstTy)) {
827 unsigned Width = CGF.getContext().getIntWidth(DstType);
830 APSInt Min = APSInt::getMinValue(Width, Unsigned);
831 APFloat MinSrc(SrcSema, APFloat::uninitialized);
832 if (MinSrc.convertFromAPInt(Min, !Unsigned, APFloat::rmTowardZero) &
836 MinSrc = APFloat::getInf(SrcSema,
true);
840 MinSrc.subtract(APFloat(SrcSema, 1), APFloat::rmTowardNegative);
842 APSInt Max = APSInt::getMaxValue(Width, Unsigned);
843 APFloat MaxSrc(SrcSema, APFloat::uninitialized);
844 if (MaxSrc.convertFromAPInt(Max, !Unsigned, APFloat::rmTowardZero) &
848 MaxSrc = APFloat::getInf(SrcSema,
false);
852 MaxSrc.add(APFloat(SrcSema, 1), APFloat::rmTowardPositive);
857 const llvm::fltSemantics &
Sema =
858 CGF.getContext().getFloatTypeSemantics(SrcType);
860 MinSrc.convert(Sema, APFloat::rmTowardZero, &IsInexact);
861 MaxSrc.convert(Sema, APFloat::rmTowardZero, &IsInexact);
865 Builder.CreateFCmpOGT(Src, llvm::ConstantFP::get(VMContext, MinSrc));
867 Builder.CreateFCmpOLT(Src, llvm::ConstantFP::get(VMContext, MaxSrc));
868 Check =
Builder.CreateAnd(GE, LE);
885 if (CGF.getContext().getFloatingTypeOrder(OrigSrcType, DstType) != 1)
889 "should not check conversion from __half, it has the lowest rank");
891 const llvm::fltSemantics &DstSema =
892 CGF.getContext().getFloatTypeSemantics(DstType);
893 APFloat MinBad = APFloat::getLargest(DstSema,
false);
894 APFloat MaxBad = APFloat::getInf(DstSema,
false);
897 MinBad.convert(SrcSema, APFloat::rmTowardZero, &IsInexact);
898 MaxBad.convert(SrcSema, APFloat::rmTowardZero, &IsInexact);
900 Value *AbsSrc = CGF.EmitNounwindRuntimeCall(
903 Builder.CreateFCmpOGT(AbsSrc, llvm::ConstantFP::get(VMContext, MinBad));
905 Builder.CreateFCmpOLT(AbsSrc, llvm::ConstantFP::get(VMContext, MaxBad));
910 llvm::Constant *StaticArgs[] = {CGF.EmitCheckSourceLocation(Loc),
911 CGF.EmitCheckTypeDescriptor(OrigSrcType),
912 CGF.EmitCheckTypeDescriptor(DstType)};
913 CGF.EmitCheck(std::make_pair(Check, SanitizerKind::FloatCastOverflow),
914 SanitizerHandler::FloatCastOverflow, StaticArgs, OrigSrc);
922 return EmitScalarConversion(Src, SrcType, DstType, Loc,
false);
928 bool TreatBooleanAsSigned) {
931 if (SrcType == DstType)
return Src;
941 return EmitConversionToBool(Src, SrcType);
946 if (SrcType->
isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) {
948 if (DstTy->isFloatingPointTy()) {
949 if (!CGF.getContext().getLangOpts().HalfArgsAndReturns)
951 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16, DstTy),
957 if (!CGF.getContext().getLangOpts().HalfArgsAndReturns) {
959 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16,
963 Src =
Builder.CreateFPExt(Src, CGF.CGM.FloatTy,
"conv");
965 SrcType = CGF.getContext().FloatTy;
977 if (
auto DstPT = dyn_cast<llvm::PointerType>(DstTy)) {
979 if (isa<llvm::PointerType>(SrcTy))
980 return Builder.CreateBitCast(Src, DstTy,
"conv");
982 assert(SrcType->
isIntegerType() &&
"Not ptr->ptr or int->ptr conversion?");
985 llvm::Type *MiddleTy = CGF.CGM.getDataLayout().getIntPtrType(DstPT);
988 Builder.CreateIntCast(Src, MiddleTy, InputSigned,
"conv");
990 return Builder.CreateIntToPtr(IntResult, DstTy,
"conv");
993 if (isa<llvm::PointerType>(SrcTy)) {
995 assert(isa<llvm::IntegerType>(DstTy) &&
"not ptr->int?");
996 return Builder.CreatePtrToInt(Src, DstTy,
"conv");
1005 "Splatted expr doesn't match with vector element type?");
1008 unsigned NumElements = DstTy->getVectorNumElements();
1009 return Builder.CreateVectorSplat(NumElements, Src,
"splat");
1013 if (isa<llvm::VectorType>(SrcTy) ||
1014 isa<llvm::VectorType>(DstTy))
1015 return Builder.CreateBitCast(Src, DstTy,
"conv");
1018 Value *Res =
nullptr;
1023 if (CGF.SanOpts.has(SanitizerKind::FloatCastOverflow) &&
1025 EmitFloatConversionCheck(OrigSrc, OrigSrcType, Src, SrcType, DstType, DstTy,
1029 if (DstType->
isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) {
1031 if (SrcTy->isFloatingPointTy()) {
1034 if (!CGF.getContext().getLangOpts().HalfArgsAndReturns)
1036 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16, SrcTy), Src);
1038 return Builder.CreateFPTrunc(Src, DstTy);
1040 DstTy = CGF.FloatTy;
1043 if (isa<llvm::IntegerType>(SrcTy)) {
1048 if (isa<llvm::IntegerType>(DstTy))
1049 Res =
Builder.CreateIntCast(Src, DstTy, InputSigned,
"conv");
1050 else if (InputSigned)
1051 Res =
Builder.CreateSIToFP(Src, DstTy,
"conv");
1053 Res =
Builder.CreateUIToFP(Src, DstTy,
"conv");
1054 }
else if (isa<llvm::IntegerType>(DstTy)) {
1055 assert(SrcTy->isFloatingPointTy() &&
"Unknown real conversion");
1057 Res =
Builder.CreateFPToSI(Src, DstTy,
"conv");
1059 Res =
Builder.CreateFPToUI(Src, DstTy,
"conv");
1061 assert(SrcTy->isFloatingPointTy() && DstTy->isFloatingPointTy() &&
1062 "Unknown real conversion");
1063 if (DstTy->getTypeID() < SrcTy->getTypeID())
1064 Res =
Builder.CreateFPTrunc(Src, DstTy,
"conv");
1066 Res =
Builder.CreateFPExt(Src, DstTy,
"conv");
1069 if (DstTy != ResTy) {
1070 if (!CGF.getContext().getLangOpts().HalfArgsAndReturns) {
1071 assert(ResTy->isIntegerTy(16) &&
"Only half FP requires extra conversion");
1073 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16, CGF.CGM.FloatTy),
1076 Res =
Builder.CreateFPTrunc(Res, ResTy,
"conv");
1085 Value *ScalarExprEmitter::EmitComplexToScalarConversion(
1094 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy, Loc);
1095 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy, Loc);
1096 return Builder.CreateOr(Src.first, Src.second,
"tobool");
1103 return EmitScalarConversion(Src.first, SrcTy, DstTy, Loc);
1107 return CGF.EmitFromMemory(CGF.CGM.EmitNullConstant(Ty), Ty);
1114 void ScalarExprEmitter::EmitBinOpCheck(
1115 ArrayRef<std::pair<Value *, SanitizerMask>> Checks,
const BinOpInfo &Info) {
1116 assert(CGF.IsSanitizerScope);
1125 StaticData.push_back(CGF.EmitCheckSourceLocation(Info.E->getExprLoc()));
1127 if (UO && UO->
getOpcode() == UO_Minus) {
1128 Check = SanitizerHandler::NegateOverflow;
1129 StaticData.push_back(CGF.EmitCheckTypeDescriptor(UO->
getType()));
1130 DynamicData.push_back(Info.RHS);
1134 Check = SanitizerHandler::ShiftOutOfBounds;
1136 StaticData.push_back(
1138 StaticData.push_back(
1140 }
else if (Opcode == BO_Div || Opcode == BO_Rem) {
1142 Check = SanitizerHandler::DivremOverflow;
1143 StaticData.push_back(CGF.EmitCheckTypeDescriptor(Info.Ty));
1147 case BO_Add: Check = SanitizerHandler::AddOverflow;
break;
1148 case BO_Sub: Check = SanitizerHandler::SubOverflow;
break;
1149 case BO_Mul: Check = SanitizerHandler::MulOverflow;
break;
1150 default: llvm_unreachable(
"unexpected opcode for bin op check");
1152 StaticData.push_back(CGF.EmitCheckTypeDescriptor(Info.Ty));
1154 DynamicData.push_back(Info.LHS);
1155 DynamicData.push_back(Info.RHS);
1158 CGF.EmitCheck(Checks, Check, StaticData, DynamicData);
1165 Value *ScalarExprEmitter::VisitExpr(
Expr *E) {
1166 CGF.ErrorUnsupported(E,
"scalar expression");
1169 return llvm::UndefValue::get(CGF.ConvertType(E->
getType()));
1179 llvm::VectorType *LTy = cast<llvm::VectorType>(LHS->getType());
1180 unsigned LHSElts = LTy->getNumElements();
1184 llvm::VectorType *MTy = cast<llvm::VectorType>(Mask->getType());
1188 llvm::ConstantInt::get(MTy, llvm::NextPowerOf2(LHSElts - 1) - 1);
1189 Mask =
Builder.CreateAnd(Mask, MaskBits,
"mask");
1197 llvm::VectorType *RTy = llvm::VectorType::get(LTy->getElementType(),
1198 MTy->getNumElements());
1199 Value* NewV = llvm::UndefValue::get(RTy);
1200 for (
unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) {
1201 Value *IIndx = llvm::ConstantInt::get(CGF.SizeTy, i);
1202 Value *Indx =
Builder.CreateExtractElement(Mask, IIndx,
"shuf_idx");
1204 Value *VExt =
Builder.CreateExtractElement(LHS, Indx,
"shuf_elt");
1205 NewV =
Builder.CreateInsertElement(NewV, VExt, IIndx,
"shuf_ins");
1217 if (Idx.isSigned() && Idx.isAllOnesValue())
1218 indices.push_back(llvm::UndefValue::get(CGF.Int32Ty));
1220 indices.push_back(
Builder.getInt32(Idx.getZExtValue()));
1223 Value *SV = llvm::ConstantVector::get(indices);
1224 return Builder.CreateShuffleVector(V1, V2, SV,
"shuffle");
1235 if (SrcType == DstType)
return Src;
1238 "ConvertVector source type must be a vector");
1240 "ConvertVector destination type must be a vector");
1252 assert(SrcTy->isVectorTy() &&
1253 "ConvertVector source IR type must be a vector");
1254 assert(DstTy->isVectorTy() &&
1255 "ConvertVector destination IR type must be a vector");
1257 llvm::Type *SrcEltTy = SrcTy->getVectorElementType(),
1258 *DstEltTy = DstTy->getVectorElementType();
1260 if (DstEltType->isBooleanType()) {
1261 assert((SrcEltTy->isFloatingPointTy() ||
1262 isa<llvm::IntegerType>(SrcEltTy)) &&
"Unknown boolean conversion");
1264 llvm::Value *Zero = llvm::Constant::getNullValue(SrcTy);
1265 if (SrcEltTy->isFloatingPointTy()) {
1266 return Builder.CreateFCmpUNE(Src, Zero,
"tobool");
1268 return Builder.CreateICmpNE(Src, Zero,
"tobool");
1273 Value *Res =
nullptr;
1275 if (isa<llvm::IntegerType>(SrcEltTy)) {
1277 if (isa<llvm::IntegerType>(DstEltTy))
1278 Res =
Builder.CreateIntCast(Src, DstTy, InputSigned,
"conv");
1279 else if (InputSigned)
1280 Res =
Builder.CreateSIToFP(Src, DstTy,
"conv");
1282 Res =
Builder.CreateUIToFP(Src, DstTy,
"conv");
1283 }
else if (isa<llvm::IntegerType>(DstEltTy)) {
1284 assert(SrcEltTy->isFloatingPointTy() &&
"Unknown real conversion");
1285 if (DstEltType->isSignedIntegerOrEnumerationType())
1286 Res =
Builder.CreateFPToSI(Src, DstTy,
"conv");
1288 Res =
Builder.CreateFPToUI(Src, DstTy,
"conv");
1290 assert(SrcEltTy->isFloatingPointTy() && DstEltTy->isFloatingPointTy() &&
1291 "Unknown real conversion");
1292 if (DstEltTy->getTypeID() < SrcEltTy->getTypeID())
1293 Res =
Builder.CreateFPTrunc(Src, DstTy,
"conv");
1295 Res =
Builder.CreateFPExt(Src, DstTy,
"conv");
1305 CGF.EmitScalarExpr(E->
getBase());
1311 return EmitLoadOfLValue(E);
1315 TestAndClearIgnoreResultAssign();
1322 return EmitLoadOfLValue(E);
1326 Value *Base = Visit(E->
getBase());
1327 Value *Idx = Visit(E->
getIdx());
1330 if (CGF.SanOpts.has(SanitizerKind::ArrayBounds))
1331 CGF.EmitBoundsCheck(E, E->
getBase(), Idx, IdxTy,
true);
1333 return Builder.CreateExtractElement(Base, Idx,
"vecext");
1336 static llvm::Constant *
getMaskElt(llvm::ShuffleVectorInst *SVI,
unsigned Idx,
1338 int MV = SVI->getMaskValue(Idx);
1340 return llvm::UndefValue::get(I32Ty);
1341 return llvm::ConstantInt::get(I32Ty, Off+MV);
1345 if (C->getBitWidth() != 32) {
1346 assert(llvm::ConstantInt::isValueValidForType(I32Ty,
1347 C->getZExtValue()) &&
1348 "Index operand too large for shufflevector mask!");
1349 return llvm::ConstantInt::get(I32Ty, C->getZExtValue());
1354 Value *ScalarExprEmitter::VisitInitListExpr(
InitListExpr *E) {
1355 bool Ignore = TestAndClearIgnoreResultAssign();
1357 assert (Ignore ==
false &&
"init list ignored");
1361 CGF.ErrorUnsupported(E,
"GNU array range designator extension");
1363 llvm::VectorType *VType =
1364 dyn_cast<llvm::VectorType>(ConvertType(E->
getType()));
1367 if (NumInitElements == 0) {
1369 return EmitNullValue(E->
getType());
1375 unsigned ResElts = VType->getNumElements();
1382 unsigned CurIdx = 0;
1383 bool VIsUndefShuffle =
false;
1385 for (
unsigned i = 0; i != NumInitElements; ++i) {
1387 Value *Init = Visit(IE);
1390 llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType());
1396 if (isa<ExtVectorElementExpr>(IE)) {
1397 llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init);
1399 if (EI->getVectorOperandType()->getNumElements() == ResElts) {
1400 llvm::ConstantInt *
C = cast<llvm::ConstantInt>(EI->getIndexOperand());
1401 Value *LHS =
nullptr, *RHS =
nullptr;
1406 Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
1408 LHS = EI->getVectorOperand();
1410 VIsUndefShuffle =
true;
1411 }
else if (VIsUndefShuffle) {
1413 llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V);
1414 for (
unsigned j = 0; j != CurIdx; ++j)
1415 Args.push_back(
getMaskElt(SVV, j, 0, CGF.Int32Ty));
1416 Args.push_back(
Builder.getInt32(ResElts + C->getZExtValue()));
1417 Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
1419 LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
1420 RHS = EI->getVectorOperand();
1421 VIsUndefShuffle =
false;
1423 if (!Args.empty()) {
1424 llvm::Constant *Mask = llvm::ConstantVector::get(Args);
1425 V =
Builder.CreateShuffleVector(LHS, RHS, Mask);
1431 V =
Builder.CreateInsertElement(V, Init,
Builder.getInt32(CurIdx),
1433 VIsUndefShuffle =
false;
1438 unsigned InitElts = VVT->getNumElements();
1443 unsigned Offset = (CurIdx == 0) ? 0 : ResElts;
1444 if (isa<ExtVectorElementExpr>(IE)) {
1445 llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init);
1446 Value *SVOp = SVI->getOperand(0);
1447 llvm::VectorType *OpTy = cast<llvm::VectorType>(SVOp->getType());
1449 if (OpTy->getNumElements() == ResElts) {
1450 for (
unsigned j = 0; j != CurIdx; ++j) {
1453 if (VIsUndefShuffle) {
1454 Args.push_back(
getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0,
1457 Args.push_back(
Builder.getInt32(j));
1460 for (
unsigned j = 0, je = InitElts; j != je; ++j)
1461 Args.push_back(
getMaskElt(SVI, j, Offset, CGF.Int32Ty));
1462 Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
1464 if (VIsUndefShuffle)
1465 V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
1474 for (
unsigned j = 0; j != InitElts; ++j)
1475 Args.push_back(
Builder.getInt32(j));
1476 Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
1477 llvm::Constant *Mask = llvm::ConstantVector::get(Args);
1478 Init =
Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT),
1482 for (
unsigned j = 0; j != CurIdx; ++j)
1483 Args.push_back(
Builder.getInt32(j));
1484 for (
unsigned j = 0; j != InitElts; ++j)
1485 Args.push_back(
Builder.getInt32(j+Offset));
1486 Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
1493 llvm::Constant *Mask = llvm::ConstantVector::get(Args);
1494 V =
Builder.CreateShuffleVector(V, Init, Mask,
"vecinit");
1495 VIsUndefShuffle = isa<llvm::UndefValue>(Init);
1504 for (; CurIdx < ResElts; ++CurIdx) {
1505 Value *Idx =
Builder.getInt32(CurIdx);
1506 llvm::Value *Init = llvm::Constant::getNullValue(EltTy);
1507 V =
Builder.CreateInsertElement(V, Init, Idx,
"vecinit");
1515 if (CE->
getCastKind() == CK_UncheckedDerivedToBase)
1535 Value *ScalarExprEmitter::VisitCastExpr(
CastExpr *CE) {
1542 bool Ignored = TestAndClearIgnoreResultAssign();
1548 case CK_Dependent: llvm_unreachable(
"dependent cast kind in IR gen!");
1549 case CK_BuiltinFnToFnPtr:
1550 llvm_unreachable(
"builtin functions are handled elsewhere");
1552 case CK_LValueBitCast:
1553 case CK_ObjCObjectLValueCast: {
1554 Address Addr = EmitLValue(E).getAddress();
1555 Addr =
Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(DestTy));
1556 LValue LV = CGF.MakeAddrLValue(Addr, DestTy);
1557 return EmitLoadOfLValue(LV, CE->
getExprLoc());
1560 case CK_CPointerToObjCPointerCast:
1561 case CK_BlockPointerToObjCPointerCast:
1562 case CK_AnyPointerToBlockPointerCast:
1564 Value *Src = Visit(const_cast<Expr*>(E));
1567 if (SrcTy->isPtrOrPtrVectorTy() && DstTy->isPtrOrPtrVectorTy() &&
1568 SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) {
1569 llvm_unreachable(
"wrong cast for pointers in different address spaces"
1570 "(must be an address space cast)!");
1573 if (CGF.SanOpts.has(SanitizerKind::CFIUnrelatedCast)) {
1575 CGF.EmitVTablePtrCheckForCast(PT->getPointeeType(), Src,
1581 return Builder.CreateBitCast(Src, DstTy);
1583 case CK_AddressSpaceConversion: {
1592 return CGF.CGM.getNullPointer(cast<llvm::PointerType>(
1593 ConvertType(DestTy)), DestTy);
1597 return CGF.CGM.getTargetCodeGenInfo().performAddrSpaceCast(
1601 case CK_AtomicToNonAtomic:
1602 case CK_NonAtomicToAtomic:
1604 case CK_UserDefinedConversion:
1605 return Visit(const_cast<Expr*>(E));
1607 case CK_BaseToDerived: {
1609 assert(DerivedClassDecl &&
"BaseToDerived arg isn't a C++ object pointer!");
1611 Address Base = CGF.EmitPointerWithAlignment(E);
1613 CGF.GetAddressOfDerivedClass(Base, DerivedClassDecl,
1615 CGF.ShouldNullCheckClassCastValue(CE));
1619 if (CGF.sanitizePerformTypeCheck())
1623 if (CGF.SanOpts.has(SanitizerKind::CFIDerivedCast))
1632 case CK_UncheckedDerivedToBase:
1633 case CK_DerivedToBase: {
1636 return CGF.EmitPointerWithAlignment(CE).getPointer();
1640 Address V = CGF.EmitPointerWithAlignment(E);
1642 return CGF.EmitDynamicCast(V, DCE);
1645 case CK_ArrayToPointerDecay:
1646 return CGF.EmitArrayToPointerDecay(E).getPointer();
1647 case CK_FunctionToPointerDecay:
1648 return EmitLValue(E).getPointer();
1650 case CK_NullToPointer:
1651 if (MustVisitNullValue(E))
1654 return CGF.CGM.getNullPointer(cast<llvm::PointerType>(ConvertType(DestTy)),
1657 case CK_NullToMemberPointer: {
1658 if (MustVisitNullValue(E))
1662 return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT);
1665 case CK_ReinterpretMemberPointer:
1666 case CK_BaseToDerivedMemberPointer:
1667 case CK_DerivedToBaseMemberPointer: {
1668 Value *Src = Visit(E);
1676 return CGF.CGM.getCXXABI().EmitMemberPointerConversion(CGF, CE, Src);
1679 case CK_ARCProduceObject:
1680 return CGF.EmitARCRetainScalarExpr(E);
1681 case CK_ARCConsumeObject:
1682 return CGF.EmitObjCConsumeObject(E->
getType(), Visit(E));
1683 case CK_ARCReclaimReturnedObject:
1684 return CGF.EmitARCReclaimReturnedObject(E, Ignored);
1685 case CK_ARCExtendBlockObject:
1686 return CGF.EmitARCExtendBlockObject(E);
1688 case CK_CopyAndAutoreleaseBlockObject:
1689 return CGF.EmitBlockCopyAndAutorelease(Visit(E), E->
getType());
1691 case CK_FloatingRealToComplex:
1692 case CK_FloatingComplexCast:
1693 case CK_IntegralRealToComplex:
1694 case CK_IntegralComplexCast:
1695 case CK_IntegralComplexToFloatingComplex:
1696 case CK_FloatingComplexToIntegralComplex:
1697 case CK_ConstructorConversion:
1699 llvm_unreachable(
"scalar cast to non-scalar value");
1701 case CK_LValueToRValue:
1702 assert(CGF.getContext().hasSameUnqualifiedType(E->
getType(), DestTy));
1703 assert(E->
isGLValue() &&
"lvalue-to-rvalue applied to r-value!");
1704 return Visit(const_cast<Expr*>(E));
1706 case CK_IntegralToPointer: {
1707 Value *Src = Visit(const_cast<Expr*>(E));
1711 auto DestLLVMTy = ConvertType(DestTy);
1712 llvm::Type *MiddleTy = CGF.CGM.getDataLayout().getIntPtrType(DestLLVMTy);
1715 Builder.CreateIntCast(Src, MiddleTy, InputSigned,
"conv");
1717 return Builder.CreateIntToPtr(IntResult, DestLLVMTy);
1719 case CK_PointerToIntegral:
1720 assert(!DestTy->
isBooleanType() &&
"bool should use PointerToBool");
1721 return Builder.CreatePtrToInt(Visit(E), ConvertType(DestTy));
1724 CGF.EmitIgnoredExpr(E);
1727 case CK_VectorSplat: {
1729 Value *Elt = Visit(const_cast<Expr*>(E));
1731 unsigned NumElements = DstTy->getVectorNumElements();
1732 return Builder.CreateVectorSplat(NumElements, Elt,
"splat");
1735 case CK_IntegralCast:
1736 case CK_IntegralToFloating:
1737 case CK_FloatingToIntegral:
1738 case CK_FloatingCast:
1739 return EmitScalarConversion(Visit(E), E->
getType(), DestTy,
1741 case CK_BooleanToSignedIntegral:
1742 return EmitScalarConversion(Visit(E), E->
getType(), DestTy,
1745 case CK_IntegralToBoolean:
1746 return EmitIntToBoolConversion(Visit(E));
1747 case CK_PointerToBoolean:
1748 return EmitPointerToBoolConversion(Visit(E), E->
getType());
1749 case CK_FloatingToBoolean:
1750 return EmitFloatToBoolConversion(Visit(E));
1751 case CK_MemberPointerToBoolean: {
1754 return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT);
1757 case CK_FloatingComplexToReal:
1758 case CK_IntegralComplexToReal:
1759 return CGF.EmitComplexExpr(E,
false,
true).first;
1761 case CK_FloatingComplexToBoolean:
1762 case CK_IntegralComplexToBoolean: {
1766 return EmitComplexToScalarConversion(V, E->
getType(), DestTy,
1770 case CK_ZeroToOCLEvent: {
1771 assert(DestTy->
isEventT() &&
"CK_ZeroToOCLEvent cast on non-event type");
1772 return llvm::Constant::getNullValue(ConvertType(DestTy));
1775 case CK_ZeroToOCLQueue: {
1776 assert(DestTy->
isQueueT() &&
"CK_ZeroToOCLQueue cast on non queue_t type");
1777 return llvm::Constant::getNullValue(ConvertType(DestTy));
1780 case CK_IntToOCLSampler:
1781 return CGF.CGM.createOpenCLIntToSamplerConversion(E, CGF);
1785 llvm_unreachable(
"unknown scalar cast");
1788 Value *ScalarExprEmitter::VisitStmtExpr(
const StmtExpr *E) {
1794 return CGF.EmitLoadOfScalar(CGF.MakeAddrLValue(RetAlloca, E->
getType()),
1799 CGF.enterFullExpression(E);
1804 Scope.ForceCleanup({&V});
1816 BinOp.RHS = llvm::ConstantInt::get(InVal->getType(), 1,
false);
1818 BinOp.Opcode = IsInc ? BO_Add : BO_Sub;
1824 llvm::Value *ScalarExprEmitter::EmitIncDecConsiderOverflowBehavior(
1827 llvm::ConstantInt::get(InVal->getType(), IsInc ? 1 : -1,
true);
1828 StringRef
Name = IsInc ?
"inc" :
"dec";
1829 switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
1831 return Builder.CreateAdd(InVal, Amount, Name);
1833 if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow))
1834 return Builder.CreateNSWAdd(InVal, Amount, Name);
1837 if (IsWidenedIntegerOp(CGF.getContext(), E->
getSubExpr()))
1838 return Builder.CreateNSWAdd(InVal, Amount, Name);
1841 llvm_unreachable(
"Unknown SignedOverflowBehaviorTy");
1846 bool isInc,
bool isPre) {
1849 llvm::PHINode *atomicPHI =
nullptr;
1853 int amount = (isInc ? 1 : -1);
1854 bool isSubtraction = !isInc;
1857 type = atomicTy->getValueType();
1862 ->setAtomic(llvm::AtomicOrdering::SequentiallyConsistent);
1867 return Builder.CreateAtomicRMW(
1868 llvm::AtomicRMWInst::Xchg, LV.
getPointer(), True,
1869 llvm::AtomicOrdering::SequentiallyConsistent);
1876 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
1877 CGF.getLangOpts().getSignedOverflowBehavior() !=
1879 llvm::AtomicRMWInst::BinOp aop = isInc ? llvm::AtomicRMWInst::Add :
1880 llvm::AtomicRMWInst::Sub;
1881 llvm::Instruction::BinaryOps op = isInc ? llvm::Instruction::Add :
1882 llvm::Instruction::Sub;
1884 llvm::ConstantInt::get(ConvertType(type), 1,
true), type);
1886 LV.
getPointer(), amt, llvm::AtomicOrdering::SequentiallyConsistent);
1887 return isPre ?
Builder.CreateBinOp(op, old, amt) : old;
1889 value = EmitLoadOfLValue(LV, E->
getExprLoc());
1892 llvm::BasicBlock *startBB =
Builder.GetInsertBlock();
1893 llvm::BasicBlock *opBB = CGF.createBasicBlock(
"atomic_op", CGF.CurFn);
1894 value = CGF.EmitToMemory(value, type);
1897 atomicPHI =
Builder.CreatePHI(value->getType(), 2);
1898 atomicPHI->addIncoming(value, startBB);
1901 value = EmitLoadOfLValue(LV, E->
getExprLoc());
1919 bool CanOverflow = value->getType()->getIntegerBitWidth() >=
1920 CGF.IntTy->getIntegerBitWidth();
1922 value = EmitIncDecConsiderOverflowBehavior(E, value, isInc);
1924 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) {
1928 llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount,
true);
1929 value =
Builder.CreateAdd(value, amt, isInc ?
"inc" :
"dec");
1938 = CGF.getContext().getAsVariableArrayType(type)) {
1940 if (!isInc) numElts =
Builder.CreateNSWNeg(numElts,
"vla.negsize");
1941 if (CGF.getLangOpts().isSignedOverflowDefined())
1942 value =
Builder.CreateGEP(value, numElts,
"vla.inc");
1944 value = CGF.EmitCheckedInBoundsGEP(
1945 value, numElts,
false, isSubtraction,
1952 value = CGF.EmitCastToVoidPtr(value);
1953 if (CGF.getLangOpts().isSignedOverflowDefined())
1954 value =
Builder.CreateGEP(value, amt,
"incdec.funcptr");
1956 value = CGF.EmitCheckedInBoundsGEP(value, amt,
false,
1959 value =
Builder.CreateBitCast(value, input->getType());
1964 if (CGF.getLangOpts().isSignedOverflowDefined())
1965 value =
Builder.CreateGEP(value, amt,
"incdec.ptr");
1967 value = CGF.EmitCheckedInBoundsGEP(value, amt,
false,
1975 llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount);
1977 value =
Builder.CreateAdd(value, amt, isInc ?
"inc" :
"dec");
1981 llvm::ConstantFP::get(value->getType(), amount),
1982 isInc ?
"inc" :
"dec");
1990 if (type->
isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) {
1992 if (!CGF.getContext().getLangOpts().HalfArgsAndReturns) {
1994 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16,
1996 input,
"incdec.conv");
1998 value =
Builder.CreateFPExt(input, CGF.CGM.FloatTy,
"incdec.conv");
2002 if (value->getType()->isFloatTy())
2003 amt = llvm::ConstantFP::get(VMContext,
2004 llvm::APFloat(static_cast<float>(amount)));
2005 else if (value->getType()->isDoubleTy())
2006 amt = llvm::ConstantFP::get(VMContext,
2007 llvm::APFloat(static_cast<double>(amount)));
2010 llvm::APFloat F(static_cast<float>(amount));
2012 const llvm::fltSemantics *FS;
2015 if (value->getType()->isFP128Ty())
2016 FS = &CGF.getTarget().getFloat128Format();
2017 else if (value->getType()->isHalfTy())
2018 FS = &CGF.getTarget().getHalfFormat();
2020 FS = &CGF.getTarget().getLongDoubleFormat();
2021 F.convert(*FS, llvm::APFloat::rmTowardZero, &ignored);
2022 amt = llvm::ConstantFP::get(VMContext, F);
2024 value =
Builder.CreateFAdd(value, amt, isInc ?
"inc" :
"dec");
2026 if (type->
isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) {
2027 if (!CGF.getContext().getLangOpts().HalfArgsAndReturns) {
2029 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16,
2031 value,
"incdec.conv");
2033 value =
Builder.CreateFPTrunc(value, input->getType(),
"incdec.conv");
2040 value = CGF.EmitCastToVoidPtr(value);
2043 if (!isInc) size = -size;
2045 llvm::ConstantInt::get(CGF.SizeTy, size.
getQuantity());
2047 if (CGF.getLangOpts().isSignedOverflowDefined())
2048 value =
Builder.CreateGEP(value, sizeValue,
"incdec.objptr");
2050 value = CGF.EmitCheckedInBoundsGEP(value, sizeValue,
2051 false, isSubtraction,
2053 value =
Builder.CreateBitCast(value, input->getType());
2057 llvm::BasicBlock *opBB =
Builder.GetInsertBlock();
2058 llvm::BasicBlock *contBB = CGF.createBasicBlock(
"atomic_cont", CGF.CurFn);
2059 auto Pair = CGF.EmitAtomicCompareExchange(
2063 atomicPHI->addIncoming(old, opBB);
2064 Builder.CreateCondBr(success, contBB, opBB);
2065 Builder.SetInsertPoint(contBB);
2066 return isPre ? value : input;
2071 CGF.EmitStoreThroughBitfieldLValue(
RValue::get(value), LV, &value);
2073 CGF.EmitStoreThroughLValue(
RValue::get(value), LV);
2077 return isPre ? value : input;
2082 Value *ScalarExprEmitter::VisitUnaryMinus(
const UnaryOperator *E) {
2083 TestAndClearIgnoreResultAssign();
2088 if (BinOp.RHS->getType()->isFPOrFPVectorTy())
2089 BinOp.LHS = llvm::ConstantFP::getZeroValueForNegation(BinOp.RHS->getType());
2091 BinOp.LHS = llvm::Constant::getNullValue(BinOp.RHS->getType());
2093 BinOp.Opcode = BO_Sub;
2096 return EmitSub(BinOp);
2099 Value *ScalarExprEmitter::VisitUnaryNot(
const UnaryOperator *E) {
2100 TestAndClearIgnoreResultAssign();
2102 return Builder.CreateNot(Op,
"neg");
2105 Value *ScalarExprEmitter::VisitUnaryLNot(
const UnaryOperator *E) {
2109 Value *Zero = llvm::Constant::getNullValue(Oper->getType());
2111 if (Oper->getType()->isFPOrFPVectorTy())
2112 Result =
Builder.CreateFCmp(llvm::CmpInst::FCMP_OEQ, Oper, Zero,
"cmp");
2114 Result =
Builder.CreateICmp(llvm::CmpInst::ICMP_EQ, Oper, Zero,
"cmp");
2115 return Builder.CreateSExt(Result, ConvertType(E->
getType()),
"sext");
2119 Value *BoolVal = CGF.EvaluateExprAsBool(E->
getSubExpr());
2124 BoolVal =
Builder.CreateNot(BoolVal,
"lnot");
2127 return Builder.CreateZExt(BoolVal, ConvertType(E->
getType()),
"lnot.ext");
2130 Value *ScalarExprEmitter::VisitOffsetOfExpr(
OffsetOfExpr *E) {
2139 llvm::Value* Result = llvm::Constant::getNullValue(ResultType);
2141 for (
unsigned i = 0; i != n; ++i) {
2150 Idx =
Builder.CreateIntCast(Idx, ResultType, IdxSigned,
"conv");
2154 CGF.getContext().getAsArrayType(CurrentType)->getElementType();
2157 llvm::Value* ElemSize = llvm::ConstantInt::get(ResultType,
2158 CGF.getContext().getTypeSizeInChars(CurrentType).getQuantity());
2161 Offset =
Builder.CreateMul(Idx, ElemSize);
2176 if (*Field == MemberDecl)
2179 assert(i < RL.
getFieldCount() &&
"offsetof field in wrong type");
2183 CGF.getContext().getCharWidth();
2184 Offset = llvm::ConstantInt::get(ResultType, OffsetInt);
2187 CurrentType = MemberDecl->
getType();
2192 llvm_unreachable(
"dependent __builtin_offsetof");
2196 CGF.ErrorUnsupported(E,
"virtual base in offsetof");
2210 Offset = llvm::ConstantInt::get(ResultType, OffsetInt.getQuantity());
2214 Result =
Builder.CreateAdd(Result, Offset);
2222 ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
2227 CGF.getContext().getAsVariableArrayType(TypeToSize)) {
2230 CGF.EmitVariablyModifiedType(TypeToSize);
2239 std::tie(numElts, eltType) = CGF.getVLASize(VAT);
2244 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(eltType);
2245 if (!eltSize.
isOne())
2246 size = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), numElts);
2253 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign(
2256 return llvm::ConstantInt::get(CGF.SizeTy, Alignment);
2264 Value *ScalarExprEmitter::VisitUnaryReal(
const UnaryOperator *E) {
2271 return CGF.EmitLoadOfLValue(CGF.EmitLValue(E),
2275 return CGF.EmitComplexExpr(Op,
false,
true).first;
2281 Value *ScalarExprEmitter::VisitUnaryImag(
const UnaryOperator *E) {
2288 return CGF.EmitLoadOfLValue(CGF.EmitLValue(E),
2292 return CGF.EmitComplexExpr(Op,
true,
false).second;
2300 CGF.EmitScalarExpr(Op,
true);
2301 return llvm::Constant::getNullValue(ConvertType(E->
getType()));
2308 BinOpInfo ScalarExprEmitter::EmitBinOps(
const BinaryOperator *E) {
2309 TestAndClearIgnoreResultAssign();
2311 Result.LHS = Visit(E->
getLHS());
2312 Result.RHS = Visit(E->
getRHS());
2320 LValue ScalarExprEmitter::EmitCompoundAssignLValue(
2322 Value *(ScalarExprEmitter::*Func)(
const BinOpInfo &),
2328 return CGF.EmitScalarCompoundAssignWithComplex(E, Result);
2332 OpInfo.RHS = Visit(E->
getRHS());
2340 llvm::PHINode *atomicPHI =
nullptr;
2342 QualType type = atomicTy->getValueType();
2345 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
2346 CGF.getLangOpts().getSignedOverflowBehavior() !=
2348 llvm::AtomicRMWInst::BinOp aop = llvm::AtomicRMWInst::BAD_BINOP;
2349 switch (OpInfo.Opcode) {
2351 case BO_MulAssign:
case BO_DivAssign:
2357 aop = llvm::AtomicRMWInst::Add;
2360 aop = llvm::AtomicRMWInst::Sub;
2366 aop = llvm::AtomicRMWInst::Xor;
2369 aop = llvm::AtomicRMWInst::Or;
2372 llvm_unreachable(
"Invalid compound assignment type");
2374 if (aop != llvm::AtomicRMWInst::BAD_BINOP) {
2376 EmitScalarConversion(OpInfo.RHS, E->
getRHS()->
getType(), LHSTy,
2380 llvm::AtomicOrdering::SequentiallyConsistent);
2386 llvm::BasicBlock *startBB =
Builder.GetInsertBlock();
2387 llvm::BasicBlock *opBB = CGF.createBasicBlock(
"atomic_op", CGF.CurFn);
2388 OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->
getExprLoc());
2389 OpInfo.LHS = CGF.EmitToMemory(OpInfo.LHS, type);
2392 atomicPHI =
Builder.CreatePHI(OpInfo.LHS->getType(), 2);
2393 atomicPHI->addIncoming(OpInfo.LHS, startBB);
2394 OpInfo.LHS = atomicPHI;
2397 OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->
getExprLoc());
2404 Result = (this->*Func)(OpInfo);
2411 llvm::BasicBlock *opBB =
Builder.GetInsertBlock();
2412 llvm::BasicBlock *contBB = CGF.createBasicBlock(
"atomic_cont", CGF.CurFn);
2413 auto Pair = CGF.EmitAtomicCompareExchange(
2415 llvm::Value *old = CGF.EmitToMemory(Pair.first.getScalarVal(), LHSTy);
2417 atomicPHI->addIncoming(old, opBB);
2418 Builder.CreateCondBr(success, contBB, opBB);
2419 Builder.SetInsertPoint(contBB);
2428 CGF.EmitStoreThroughBitfieldLValue(
RValue::get(Result), LHSLV, &Result);
2430 CGF.EmitStoreThroughLValue(
RValue::get(Result), LHSLV);
2436 Value *(ScalarExprEmitter::*Func)(
const BinOpInfo &)) {
2437 bool Ignore = TestAndClearIgnoreResultAssign();
2439 LValue LHS = EmitCompoundAssignLValue(E, Func, RHS);
2446 if (!CGF.getLangOpts().CPlusPlus)
2454 return EmitLoadOfLValue(LHS, E->
getExprLoc());
2457 void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck(
2458 const BinOpInfo &Ops,
llvm::Value *Zero,
bool isDiv) {
2461 if (CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero)) {
2462 Checks.push_back(std::make_pair(
Builder.CreateICmpNE(Ops.RHS, Zero),
2463 SanitizerKind::IntegerDivideByZero));
2466 const auto *BO = cast<BinaryOperator>(Ops.E);
2467 if (CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow) &&
2468 Ops.Ty->hasSignedIntegerRepresentation() &&
2469 !IsWidenedIntegerOp(CGF.getContext(), BO->
getLHS()) &&
2470 Ops.mayHaveIntegerOverflow()) {
2471 llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType());
2474 Builder.getInt(llvm::APInt::getSignedMinValue(Ty->getBitWidth()));
2475 llvm::Value *NegOne = llvm::ConstantInt::get(Ty, -1ULL);
2481 std::make_pair(NotOverflow, SanitizerKind::SignedIntegerOverflow));
2484 if (Checks.size() > 0)
2485 EmitBinOpCheck(Checks, Ops);
2488 Value *ScalarExprEmitter::EmitDiv(
const BinOpInfo &Ops) {
2491 if ((CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero) ||
2492 CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) &&
2493 Ops.Ty->isIntegerType() &&
2494 (Ops.mayHaveIntegerDivisionByZero() || Ops.mayHaveIntegerOverflow())) {
2495 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
2496 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero,
true);
2497 }
else if (CGF.SanOpts.has(SanitizerKind::FloatDivideByZero) &&
2498 Ops.Ty->isRealFloatingType() &&
2499 Ops.mayHaveFloatDivisionByZero()) {
2500 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
2502 EmitBinOpCheck(std::make_pair(NonZero, SanitizerKind::FloatDivideByZero),
2507 if (Ops.LHS->getType()->isFPOrFPVectorTy()) {
2509 if (CGF.getLangOpts().OpenCL &&
2510 !CGF.CGM.getCodeGenOpts().CorrectlyRoundedDivSqrt) {
2517 if (ValTy->isFloatTy() ||
2518 (isa<llvm::VectorType>(ValTy) &&
2519 cast<llvm::VectorType>(ValTy)->getElementType()->isFloatTy()))
2520 CGF.SetFPAccuracy(Val, 2.5);
2524 else if (Ops.Ty->hasUnsignedIntegerRepresentation())
2525 return Builder.CreateUDiv(Ops.LHS, Ops.RHS,
"div");
2527 return Builder.CreateSDiv(Ops.LHS, Ops.RHS,
"div");
2530 Value *ScalarExprEmitter::EmitRem(
const BinOpInfo &Ops) {
2532 if ((CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero) ||
2533 CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) &&
2534 Ops.Ty->isIntegerType() &&
2535 (Ops.mayHaveIntegerDivisionByZero() || Ops.mayHaveIntegerOverflow())) {
2537 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
2538 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero,
false);
2541 if (Ops.Ty->hasUnsignedIntegerRepresentation())
2542 return Builder.CreateURem(Ops.LHS, Ops.RHS,
"rem");
2544 return Builder.CreateSRem(Ops.LHS, Ops.RHS,
"rem");
2547 Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(
const BinOpInfo &Ops) {
2551 bool isSigned = Ops.Ty->isSignedIntegerOrEnumerationType();
2552 switch (Ops.Opcode) {
2556 IID = isSigned ? llvm::Intrinsic::sadd_with_overflow :
2557 llvm::Intrinsic::uadd_with_overflow;
2562 IID = isSigned ? llvm::Intrinsic::ssub_with_overflow :
2563 llvm::Intrinsic::usub_with_overflow;
2568 IID = isSigned ? llvm::Intrinsic::smul_with_overflow :
2569 llvm::Intrinsic::umul_with_overflow;
2572 llvm_unreachable(
"Unsupported operation for overflow detection");
2579 llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
2581 llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, opTy);
2583 Value *resultAndOverflow =
Builder.CreateCall(intrinsic, {Ops.LHS, Ops.RHS});
2584 Value *result =
Builder.CreateExtractValue(resultAndOverflow, 0);
2585 Value *overflow =
Builder.CreateExtractValue(resultAndOverflow, 1);
2588 const std::string *handlerName =
2589 &CGF.getLangOpts().OverflowHandler;
2590 if (handlerName->empty()) {
2593 if (!isSigned || CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) {
2595 SanitizerMask Kind = isSigned ? SanitizerKind::SignedIntegerOverflow
2596 : SanitizerKind::UnsignedIntegerOverflow;
2597 EmitBinOpCheck(std::make_pair(NotOverflow, Kind), Ops);
2599 CGF.EmitTrapCheck(
Builder.CreateNot(overflow));
2604 llvm::BasicBlock *initialBB =
Builder.GetInsertBlock();
2605 llvm::BasicBlock *continueBB =
2606 CGF.createBasicBlock(
"nooverflow", CGF.CurFn, initialBB->getNextNode());
2607 llvm::BasicBlock *overflowBB = CGF.createBasicBlock(
"overflow", CGF.CurFn);
2609 Builder.CreateCondBr(overflow, overflowBB, continueBB);
2613 Builder.SetInsertPoint(overflowBB);
2617 llvm::Type *argTypes[] = { CGF.Int64Ty, CGF.Int64Ty, Int8Ty, Int8Ty };
2618 llvm::FunctionType *handlerTy =
2619 llvm::FunctionType::get(CGF.Int64Ty, argTypes,
true);
2620 llvm::Value *handler = CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName);
2633 Builder.getInt8(cast<llvm::IntegerType>(opTy)->getBitWidth())
2636 CGF.EmitNounwindRuntimeCall(handler, handlerArgs);
2639 handlerResult =
Builder.CreateTrunc(handlerResult, opTy);
2642 Builder.SetInsertPoint(continueBB);
2643 llvm::PHINode *phi =
Builder.CreatePHI(opTy, 2);
2644 phi->addIncoming(result, initialBB);
2645 phi->addIncoming(handlerResult, overflowBB);
2652 const BinOpInfo &op,
2653 bool isSubtraction) {
2658 Value *pointer = op.LHS;
2660 Value *index = op.RHS;
2664 if (!isSubtraction && !pointer->getType()->isPointerTy()) {
2665 std::swap(pointer, index);
2666 std::swap(pointerOperand, indexOperand);
2671 unsigned width = cast<llvm::IntegerType>(index->getType())->getBitWidth();
2673 auto PtrTy = cast<llvm::PointerType>(pointer->getType());
2674 if (width != DL.getTypeSizeInBits(PtrTy)) {
2677 index = CGF.
Builder.CreateIntCast(index, DL.getIntPtrType(PtrTy), isSigned,
2683 index = CGF.
Builder.CreateNeg(index,
"idx.neg");
2685 if (CGF.
SanOpts.
has(SanitizerKind::ArrayBounds))
2698 index = CGF.
Builder.CreateMul(index, objectSize);
2701 result = CGF.
Builder.CreateGEP(result, index,
"add.ptr");
2716 index = CGF.
Builder.CreateMul(index, numElements,
"vla.index");
2717 pointer = CGF.
Builder.CreateGEP(pointer, index,
"add.ptr");
2719 index = CGF.
Builder.CreateNSWMul(index, numElements,
"vla.index");
2722 op.E->getExprLoc(),
"add.ptr");
2732 result = CGF.
Builder.CreateGEP(result, index,
"add.ptr");
2737 return CGF.
Builder.CreateGEP(pointer, index,
"add.ptr");
2740 op.E->getExprLoc(),
"add.ptr");
2750 bool negMul,
bool negAdd) {
2751 assert(!(negMul && negAdd) &&
"Only one of negMul and negAdd should be set.");
2753 Value *MulOp0 = MulOp->getOperand(0);
2754 Value *MulOp1 = MulOp->getOperand(1);
2758 llvm::ConstantFP::getZeroValueForNegation(MulOp0->getType()), MulOp0,
2760 }
else if (negAdd) {
2763 llvm::ConstantFP::getZeroValueForNegation(Addend->getType()), Addend,
2767 Value *FMulAdd = Builder.CreateCall(
2769 {MulOp0, MulOp1, Addend});
2770 MulOp->eraseFromParent();
2785 assert((op.Opcode == BO_Add || op.Opcode == BO_AddAssign ||
2786 op.Opcode == BO_Sub || op.Opcode == BO_SubAssign) &&
2787 "Only fadd/fsub can be the root of an fmuladd.");
2790 if (!op.FPFeatures.allowFPContractWithinStatement())
2796 if (
auto *LHSBinOp = dyn_cast<llvm::BinaryOperator>(op.LHS)) {
2797 if (LHSBinOp->getOpcode() == llvm::Instruction::FMul &&
2798 LHSBinOp->use_empty())
2799 return buildFMulAdd(LHSBinOp, op.RHS, CGF, Builder,
false, isSub);
2801 if (
auto *RHSBinOp = dyn_cast<llvm::BinaryOperator>(op.RHS)) {
2802 if (RHSBinOp->getOpcode() == llvm::Instruction::FMul &&
2803 RHSBinOp->use_empty())
2804 return buildFMulAdd(RHSBinOp, op.LHS, CGF, Builder, isSub,
false);
2810 Value *ScalarExprEmitter::EmitAdd(
const BinOpInfo &op) {
2811 if (op.LHS->getType()->isPointerTy() ||
2812 op.RHS->getType()->isPointerTy())
2815 if (op.Ty->isSignedIntegerOrEnumerationType()) {
2816 switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
2818 return Builder.CreateAdd(op.LHS, op.RHS,
"add");
2820 if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow))
2821 return Builder.CreateNSWAdd(op.LHS, op.RHS,
"add");
2824 if (CanElideOverflowCheck(CGF.getContext(), op))
2825 return Builder.CreateNSWAdd(op.LHS, op.RHS,
"add");
2826 return EmitOverflowCheckedBinOp(op);
2830 if (op.Ty->isUnsignedIntegerType() &&
2831 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow) &&
2832 !CanElideOverflowCheck(CGF.getContext(), op))
2833 return EmitOverflowCheckedBinOp(op);
2835 if (op.LHS->getType()->isFPOrFPVectorTy()) {
2840 Value *V =
Builder.CreateFAdd(op.LHS, op.RHS,
"add");
2841 return propagateFMFlags(V, op);
2844 return Builder.CreateAdd(op.LHS, op.RHS,
"add");
2847 Value *ScalarExprEmitter::EmitSub(
const BinOpInfo &op) {
2849 if (!op.LHS->getType()->isPointerTy()) {
2850 if (op.Ty->isSignedIntegerOrEnumerationType()) {
2851 switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
2853 return Builder.CreateSub(op.LHS, op.RHS,
"sub");
2855 if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow))
2856 return Builder.CreateNSWSub(op.LHS, op.RHS,
"sub");
2859 if (CanElideOverflowCheck(CGF.getContext(), op))
2860 return Builder.CreateNSWSub(op.LHS, op.RHS,
"sub");
2861 return EmitOverflowCheckedBinOp(op);
2865 if (op.Ty->isUnsignedIntegerType() &&
2866 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow) &&
2867 !CanElideOverflowCheck(CGF.getContext(), op))
2868 return EmitOverflowCheckedBinOp(op);
2870 if (op.LHS->getType()->isFPOrFPVectorTy()) {
2874 Value *V =
Builder.CreateFSub(op.LHS, op.RHS,
"sub");
2875 return propagateFMFlags(V, op);
2878 return Builder.CreateSub(op.LHS, op.RHS,
"sub");
2883 if (!op.RHS->getType()->isPointerTy())
2890 =
Builder.CreatePtrToInt(op.LHS, CGF.PtrDiffTy,
"sub.ptr.lhs.cast");
2892 =
Builder.CreatePtrToInt(op.RHS, CGF.PtrDiffTy,
"sub.ptr.rhs.cast");
2893 Value *diffInChars =
Builder.CreateSub(LHS, RHS,
"sub.ptr.sub");
2903 = CGF.getContext().getAsVariableArrayType(elementType)) {
2905 std::tie(numElements, elementType) = CGF.getVLASize(vla);
2907 divisor = numElements;
2910 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(elementType);
2911 if (!eltSize.
isOne())
2912 divisor = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), divisor);
2921 if (elementType->isVoidType() || elementType->isFunctionType())
2924 elementSize = CGF.getContext().getTypeSizeInChars(elementType);
2927 if (elementSize.
isOne())
2930 divisor = CGF.CGM.getSize(elementSize);
2936 return Builder.CreateExactSDiv(diffInChars, divisor,
"sub.ptr.div");
2939 Value *ScalarExprEmitter::GetWidthMinusOneValue(Value* LHS,Value* RHS) {
2940 llvm::IntegerType *Ty;
2941 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(LHS->getType()))
2942 Ty = cast<llvm::IntegerType>(VT->getElementType());
2944 Ty = cast<llvm::IntegerType>(LHS->getType());
2945 return llvm::ConstantInt::get(RHS->getType(), Ty->getBitWidth() - 1);
2948 Value *ScalarExprEmitter::EmitShl(
const BinOpInfo &Ops) {
2951 Value *RHS = Ops.RHS;
2952 if (Ops.LHS->getType() != RHS->getType())
2953 RHS =
Builder.CreateIntCast(RHS, Ops.LHS->getType(),
false,
"sh_prom");
2955 bool SanitizeBase = CGF.SanOpts.has(SanitizerKind::ShiftBase) &&
2956 Ops.Ty->hasSignedIntegerRepresentation() &&
2957 !CGF.getLangOpts().isSignedOverflowDefined();
2958 bool SanitizeExponent = CGF.SanOpts.has(SanitizerKind::ShiftExponent);
2960 if (CGF.getLangOpts().OpenCL)
2962 Builder.CreateAnd(RHS, GetWidthMinusOneValue(Ops.LHS, RHS),
"shl.mask");
2963 else if ((SanitizeBase || SanitizeExponent) &&
2964 isa<llvm::IntegerType>(Ops.LHS->getType())) {
2967 llvm::Value *WidthMinusOne = GetWidthMinusOneValue(Ops.LHS, Ops.RHS);
2970 if (SanitizeExponent) {
2972 std::make_pair(ValidExponent, SanitizerKind::ShiftExponent));
2979 llvm::BasicBlock *Orig =
Builder.GetInsertBlock();
2980 llvm::BasicBlock *Cont = CGF.createBasicBlock(
"cont");
2981 llvm::BasicBlock *CheckShiftBase = CGF.createBasicBlock(
"check");
2982 Builder.CreateCondBr(ValidExponent, CheckShiftBase, Cont);
2984 (RHS == Ops.RHS) ? WidthMinusOne
2985 : GetWidthMinusOneValue(Ops.LHS, RHS);
2986 CGF.EmitBlock(CheckShiftBase);
2988 Ops.LHS,
Builder.CreateSub(PromotedWidthMinusOne, RHS,
"shl.zeros",
2991 if (CGF.getLangOpts().CPlusPlus) {
2996 llvm::Value *One = llvm::ConstantInt::get(BitsShiftedOff->getType(), 1);
2997 BitsShiftedOff =
Builder.CreateLShr(BitsShiftedOff, One);
2999 llvm::Value *Zero = llvm::ConstantInt::get(BitsShiftedOff->getType(), 0);
3001 CGF.EmitBlock(Cont);
3002 llvm::PHINode *BaseCheck =
Builder.CreatePHI(ValidBase->getType(), 2);
3003 BaseCheck->addIncoming(
Builder.getTrue(), Orig);
3004 BaseCheck->addIncoming(ValidBase, CheckShiftBase);
3005 Checks.push_back(std::make_pair(BaseCheck, SanitizerKind::ShiftBase));
3008 assert(!Checks.empty());
3009 EmitBinOpCheck(Checks, Ops);
3012 return Builder.CreateShl(Ops.LHS, RHS,
"shl");
3015 Value *ScalarExprEmitter::EmitShr(
const BinOpInfo &Ops) {
3018 Value *RHS = Ops.RHS;
3019 if (Ops.LHS->getType() != RHS->getType())
3020 RHS =
Builder.CreateIntCast(RHS, Ops.LHS->getType(),
false,
"sh_prom");
3023 if (CGF.getLangOpts().OpenCL)
3025 Builder.CreateAnd(RHS, GetWidthMinusOneValue(Ops.LHS, RHS),
"shr.mask");
3026 else if (CGF.SanOpts.has(SanitizerKind::ShiftExponent) &&
3027 isa<llvm::IntegerType>(Ops.LHS->getType())) {
3030 Builder.CreateICmpULE(RHS, GetWidthMinusOneValue(Ops.LHS, RHS));
3031 EmitBinOpCheck(std::make_pair(Valid, SanitizerKind::ShiftExponent), Ops);
3034 if (Ops.Ty->hasUnsignedIntegerRepresentation())
3035 return Builder.CreateLShr(Ops.LHS, RHS,
"shr");
3036 return Builder.CreateAShr(Ops.LHS, RHS,
"shr");
3044 default: llvm_unreachable(
"unexpected element type");
3045 case BuiltinType::Char_U:
3046 case BuiltinType::UChar:
3047 return (IT ==
VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
3048 llvm::Intrinsic::ppc_altivec_vcmpgtub_p;
3049 case BuiltinType::Char_S:
3050 case BuiltinType::SChar:
3051 return (IT ==
VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
3052 llvm::Intrinsic::ppc_altivec_vcmpgtsb_p;
3053 case BuiltinType::UShort:
3054 return (IT ==
VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
3055 llvm::Intrinsic::ppc_altivec_vcmpgtuh_p;
3056 case BuiltinType::Short:
3057 return (IT ==
VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
3058 llvm::Intrinsic::ppc_altivec_vcmpgtsh_p;
3059 case BuiltinType::UInt:
3060 case BuiltinType::ULong:
3061 return (IT ==
VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
3062 llvm::Intrinsic::ppc_altivec_vcmpgtuw_p;
3063 case BuiltinType::Int:
3064 case BuiltinType::Long:
3065 return (IT ==
VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
3066 llvm::Intrinsic::ppc_altivec_vcmpgtsw_p;
3067 case BuiltinType::Float:
3068 return (IT ==
VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpeqfp_p :
3069 llvm::Intrinsic::ppc_altivec_vcmpgtfp_p;
3074 llvm::CmpInst::Predicate UICmpOpc,
3075 llvm::CmpInst::Predicate SICmpOpc,
3076 llvm::CmpInst::Predicate FCmpOpc) {
3077 TestAndClearIgnoreResultAssign();
3084 Value *LHS = CGF.EmitScalarExpr(E->
getLHS());
3085 Value *RHS = CGF.EmitScalarExpr(E->
getRHS());
3086 Result = CGF.CGM.getCXXABI().EmitMemberPointerComparison(
3087 CGF, LHS, RHS, MPT, E->
getOpcode() == BO_NE);
3089 Value *LHS = Visit(E->
getLHS());
3090 Value *RHS = Visit(E->
getRHS());
3096 enum { CR6_EQ=0, CR6_EQ_REV, CR6_LT, CR6_LT_REV } CR6;
3101 Value *FirstVecArg = LHS,
3102 *SecondVecArg = RHS;
3109 default: llvm_unreachable(
"is not a comparison operation");
3121 std::swap(FirstVecArg, SecondVecArg);
3128 if (ElementKind == BuiltinType::Float) {
3130 ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
3131 std::swap(FirstVecArg, SecondVecArg);
3139 if (ElementKind == BuiltinType::Float) {
3141 ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
3146 std::swap(FirstVecArg, SecondVecArg);
3151 Value *CR6Param =
Builder.getInt32(CR6);
3152 llvm::Function *F = CGF.CGM.getIntrinsic(ID);
3153 Result =
Builder.CreateCall(F, {CR6Param, FirstVecArg, SecondVecArg});
3154 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->
getType(),
3158 if (LHS->getType()->isFPOrFPVectorTy()) {
3159 Result =
Builder.CreateFCmp(FCmpOpc, LHS, RHS,
"cmp");
3161 Result =
Builder.CreateICmp(SICmpOpc, LHS, RHS,
"cmp");
3164 Result =
Builder.CreateICmp(UICmpOpc, LHS, RHS,
"cmp");
3170 return Builder.CreateSExt(Result, ConvertType(E->
getType()),
"sext");
3177 LHS = CGF.EmitComplexExpr(E->
getLHS());
3178 CETy = CTy->getElementType();
3180 LHS.first = Visit(E->
getLHS());
3181 LHS.second = llvm::Constant::getNullValue(LHS.first->getType());
3185 RHS = CGF.EmitComplexExpr(E->
getRHS());
3186 assert(CGF.getContext().hasSameUnqualifiedType(CETy,
3187 CTy->getElementType()) &&
3188 "The element types must always match.");
3191 RHS.first = Visit(E->
getRHS());
3192 RHS.second = llvm::Constant::getNullValue(RHS.first->getType());
3193 assert(CGF.getContext().hasSameUnqualifiedType(CETy, RHSTy) &&
3194 "The element types must always match.");
3197 Value *ResultR, *ResultI;
3199 ResultR =
Builder.CreateFCmp(FCmpOpc, LHS.first, RHS.first,
"cmp.r");
3200 ResultI =
Builder.CreateFCmp(FCmpOpc, LHS.second, RHS.second,
"cmp.i");
3204 ResultR =
Builder.CreateICmp(UICmpOpc, LHS.first, RHS.first,
"cmp.r");
3205 ResultI =
Builder.CreateICmp(UICmpOpc, LHS.second, RHS.second,
"cmp.i");
3209 Result =
Builder.CreateAnd(ResultR, ResultI,
"and.ri");
3212 "Complex comparison other than == or != ?");
3213 Result =
Builder.CreateOr(ResultR, ResultI,
"or.ri");
3217 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->
getType(),
3221 Value *ScalarExprEmitter::VisitBinAssign(
const BinaryOperator *E) {
3222 bool Ignore = TestAndClearIgnoreResultAssign();
3229 std::tie(LHS, RHS) = CGF.EmitARCStoreStrong(E, Ignore);
3233 std::tie(LHS, RHS) = CGF.EmitARCStoreAutoreleasing(E);
3237 std::tie(LHS, RHS) = CGF.EmitARCStoreUnsafeUnretained(E, Ignore);
3241 RHS = Visit(E->
getRHS());
3243 RHS = CGF.EmitARCStoreWeak(LHS.getAddress(), RHS, Ignore);
3249 RHS = Visit(E->
getRHS());
3256 if (LHS.isBitField()) {
3257 CGF.EmitStoreThroughBitfieldLValue(
RValue::get(RHS), LHS, &RHS);
3259 CGF.EmitNullabilityCheck(LHS, RHS, E->
getExprLoc());
3260 CGF.EmitStoreThroughLValue(
RValue::get(RHS), LHS);
3269 if (!CGF.getLangOpts().CPlusPlus)
3273 if (!LHS.isVolatileQualified())
3277 return EmitLoadOfLValue(LHS, E->
getExprLoc());
3280 Value *ScalarExprEmitter::VisitBinLAnd(
const BinaryOperator *E) {
3283 CGF.incrementProfileCounter(E);
3285 Value *LHS = Visit(E->
getLHS());
3286 Value *RHS = Visit(E->
getRHS());
3287 Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
3288 if (LHS->getType()->isFPOrFPVectorTy()) {
3289 LHS =
Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero,
"cmp");
3290 RHS =
Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero,
"cmp");
3292 LHS =
Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero,
"cmp");
3293 RHS =
Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero,
"cmp");
3296 return Builder.CreateSExt(And, ConvertType(E->
getType()),
"sext");
3304 if (CGF.ConstantFoldsToSimpleInteger(E->
getLHS(), LHSCondVal)) {
3306 CGF.incrementProfileCounter(E);
3308 Value *RHSCond = CGF.EvaluateExprAsBool(E->
getRHS());
3310 return Builder.CreateZExtOrBitCast(RHSCond, ResTy,
"land.ext");
3314 if (!CGF.ContainsLabel(E->
getRHS()))
3315 return llvm::Constant::getNullValue(ResTy);
3318 llvm::BasicBlock *ContBlock = CGF.createBasicBlock(
"land.end");
3319 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock(
"land.rhs");
3324 CGF.EmitBranchOnBoolExpr(E->
getLHS(), RHSBlock, ContBlock,
3325 CGF.getProfileCount(E->
getRHS()));
3332 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
3334 PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI);
3337 CGF.EmitBlock(RHSBlock);
3338 CGF.incrementProfileCounter(E);
3339 Value *RHSCond = CGF.EvaluateExprAsBool(E->
getRHS());
3343 RHSBlock =
Builder.GetInsertBlock();
3349 CGF.EmitBlock(ContBlock);
3352 PN->addIncoming(RHSCond, RHSBlock);
3355 return Builder.CreateZExtOrBitCast(PN, ResTy,
"land.ext");
3361 CGF.incrementProfileCounter(E);
3363 Value *LHS = Visit(E->
getLHS());
3364 Value *RHS = Visit(E->
getRHS());
3365 Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
3366 if (LHS->getType()->isFPOrFPVectorTy()) {
3367 LHS =
Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero,
"cmp");
3368 RHS =
Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero,
"cmp");
3370 LHS =
Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero,
"cmp");
3371 RHS =
Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero,
"cmp");
3373 Value *Or =
Builder.CreateOr(LHS, RHS);
3374 return Builder.CreateSExt(Or, ConvertType(E->
getType()),
"sext");
3382 if (CGF.ConstantFoldsToSimpleInteger(E->
getLHS(), LHSCondVal)) {
3384 CGF.incrementProfileCounter(E);
3386 Value *RHSCond = CGF.EvaluateExprAsBool(E->
getRHS());
3388 return Builder.CreateZExtOrBitCast(RHSCond, ResTy,
"lor.ext");
3392 if (!CGF.ContainsLabel(E->
getRHS()))
3393 return llvm::ConstantInt::get(ResTy, 1);
3396 llvm::BasicBlock *ContBlock = CGF.createBasicBlock(
"lor.end");
3397 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock(
"lor.rhs");
3402 CGF.EmitBranchOnBoolExpr(E->
getLHS(), ContBlock, RHSBlock,
3403 CGF.getCurrentProfileCount() -
3404 CGF.getProfileCount(E->
getRHS()));
3411 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
3413 PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI);
3418 CGF.EmitBlock(RHSBlock);
3419 CGF.incrementProfileCounter(E);
3420 Value *RHSCond = CGF.EvaluateExprAsBool(E->
getRHS());
3425 RHSBlock =
Builder.GetInsertBlock();
3429 CGF.EmitBlock(ContBlock);
3430 PN->addIncoming(RHSCond, RHSBlock);
3433 return Builder.CreateZExtOrBitCast(PN, ResTy,
"lor.ext");
3436 Value *ScalarExprEmitter::VisitBinComma(
const BinaryOperator *E) {
3437 CGF.EmitIgnoredExpr(E->
getLHS());
3438 CGF.EnsureInsertPoint();
3439 return Visit(E->
getRHS());
3464 Value *ScalarExprEmitter::
3466 TestAndClearIgnoreResultAssign();
3478 if (CGF.ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
3479 Expr *live = lhsExpr, *dead = rhsExpr;
3480 if (!CondExprBool) std::swap(live, dead);
3483 if (!CGF.ContainsLabel(dead)) {
3485 CGF.incrementProfileCounter(E);
3486 Value *Result = Visit(live);
3492 Result = llvm::UndefValue::get(CGF.ConvertType(E->
getType()));
3500 if (CGF.getLangOpts().OpenCL
3502 CGF.incrementProfileCounter(E);
3504 llvm::Value *CondV = CGF.EmitScalarExpr(condExpr);
3509 llvm::VectorType *vecTy = cast<llvm::VectorType>(condType);
3511 unsigned numElem = vecTy->getNumElements();
3512 llvm::Type *elemType = vecTy->getElementType();
3514 llvm::Value *zeroVec = llvm::Constant::getNullValue(vecTy);
3517 llvm::VectorType::get(elemType,
3525 bool wasCast =
false;
3526 llvm::VectorType *rhsVTy = cast<llvm::VectorType>(RHS->getType());
3527 if (rhsVTy->getElementType()->isFloatingPointTy()) {
3528 RHSTmp =
Builder.CreateBitCast(RHS, tmp2->getType());
3529 LHSTmp =
Builder.CreateBitCast(LHS, tmp->getType());
3537 tmp5 =
Builder.CreateBitCast(tmp5, RHS->getType());
3547 llvm::Value *CondV = CGF.EvaluateExprAsBool(condExpr);
3550 CGF.incrementProfileCounter(E, StepV);
3556 assert(!RHS &&
"LHS and RHS types must match");
3559 return Builder.CreateSelect(CondV, LHS, RHS,
"cond");
3562 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock(
"cond.true");
3563 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock(
"cond.false");
3564 llvm::BasicBlock *ContBlock = CGF.createBasicBlock(
"cond.end");
3567 CGF.EmitBranchOnBoolExpr(condExpr, LHSBlock, RHSBlock,
3568 CGF.getProfileCount(lhsExpr));
3570 CGF.EmitBlock(LHSBlock);
3571 CGF.incrementProfileCounter(E);
3573 Value *LHS = Visit(lhsExpr);
3576 LHSBlock =
Builder.GetInsertBlock();
3579 CGF.EmitBlock(RHSBlock);
3581 Value *RHS = Visit(rhsExpr);
3584 RHSBlock =
Builder.GetInsertBlock();
3585 CGF.EmitBlock(ContBlock);
3594 llvm::PHINode *PN =
Builder.CreatePHI(LHS->getType(), 2,
"cond");
3595 PN->addIncoming(LHS, LHSBlock);
3596 PN->addIncoming(RHS, RHSBlock);
3600 Value *ScalarExprEmitter::VisitChooseExpr(
ChooseExpr *E) {
3604 Value *ScalarExprEmitter::VisitVAArgExpr(
VAArgExpr *VE) {
3608 CGF.EmitVariablyModifiedType(Ty);
3611 Address ArgPtr = CGF.EmitVAArg(VE, ArgValue);
3617 CGF.ErrorUnsupported(VE,
"va_arg expression");
3618 return llvm::UndefValue::get(ArgTy);
3625 if (ArgTy != Val->getType()) {
3626 if (ArgTy->isPointerTy() && !Val->getType()->isPointerTy())
3627 Val =
Builder.CreateIntToPtr(Val, ArgTy);
3629 Val =
Builder.CreateTrunc(Val, ArgTy);
3635 Value *ScalarExprEmitter::VisitBlockExpr(
const BlockExpr *block) {
3636 return CGF.EmitBlockLiteral(block);
3641 Value *Src,
unsigned NumElementsDst) {
3642 llvm::Value *UnV = llvm::UndefValue::get(Src->getType());
3644 Args.push_back(Builder.getInt32(0));
3645 Args.push_back(Builder.getInt32(1));
3646 Args.push_back(Builder.getInt32(2));
3647 if (NumElementsDst == 4)
3648 Args.push_back(llvm::UndefValue::get(CGF.
Int32Ty));
3649 llvm::Constant *Mask = llvm::ConstantVector::get(Args);
3650 return Builder.CreateShuffleVector(Src, UnV, Mask);
3670 const llvm::DataLayout &DL,
3672 StringRef Name =
"") {
3673 auto SrcTy = Src->getType();
3676 if (!SrcTy->isPointerTy() && !DstTy->isPointerTy())
3680 if (SrcTy->isPointerTy() && DstTy->isPointerTy())
3684 if (SrcTy->isPointerTy() && !DstTy->isPointerTy()) {
3686 if (!DstTy->isIntegerTy())
3687 Src = Builder.CreatePtrToInt(Src, DL.getIntPtrType(SrcTy));
3689 return Builder.CreateBitOrPointerCast(Src, DstTy, Name);
3693 if (!SrcTy->isIntegerTy())
3696 return Builder.CreateIntToPtr(Src, DstTy, Name);
3699 Value *ScalarExprEmitter::VisitAsTypeExpr(
AsTypeExpr *E) {
3700 Value *Src = CGF.EmitScalarExpr(E->
getSrcExpr());
3704 unsigned NumElementsSrc = isa<llvm::VectorType>(SrcTy) ?
3705 cast<llvm::VectorType>(SrcTy)->getNumElements() : 0;
3706 unsigned NumElementsDst = isa<llvm::VectorType>(DstTy) ?
3707 cast<llvm::VectorType>(DstTy)->getNumElements() : 0;
3711 if (NumElementsSrc == 3 && NumElementsDst != 3) {
3714 if (!CGF.CGM.getCodeGenOpts().PreserveVec3Type) {
3719 Src->setName(
"astype");
3726 if (NumElementsSrc != 3 && NumElementsDst == 3) {
3727 if (!CGF.CGM.getCodeGenOpts().PreserveVec3Type) {
3728 auto Vec4Ty = llvm::VectorType::get(DstTy->getVectorElementType(), 4);
3734 Src->setName(
"astype");
3739 Src, DstTy,
"astype");
3742 Value *ScalarExprEmitter::VisitAtomicExpr(
AtomicExpr *E) {
3743 return CGF.EmitAtomicExpr(E).getScalarVal();
3754 "Invalid scalar expression to emit");
3756 return ScalarExprEmitter(*
this, IgnoreResultAssign)
3757 .Visit(const_cast<Expr *>(E));
3766 "Invalid scalar expression to emit");
3767 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy, Loc);
3777 "Invalid complex -> scalar conversion");
3778 return ScalarExprEmitter(*
this)
3779 .EmitComplexToScalarConversion(Src, SrcTy, DstTy, Loc);
3785 bool isInc,
bool isPre) {
3786 return ScalarExprEmitter(*this).EmitScalarPrePostIncDec(E, LV, isInc, isPre);
3809 ScalarExprEmitter Scalar(*
this);
3810 Value *Result =
nullptr;
3812 #define COMPOUND_OP(Op) \
3813 case BO_##Op##Assign: \
3814 return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \
3850 llvm_unreachable(
"Not valid compound assignment operators");
3853 llvm_unreachable(
"Unhandled compound assignment operator");
3861 const Twine &Name) {
3862 Value *GEPVal =
Builder.CreateInBoundsGEP(Ptr, IdxList, Name);
3865 if (!
SanOpts.
has(SanitizerKind::PointerOverflow))
3869 if (isa<llvm::Constant>(GEPVal))
3873 if (GEPVal->getType()->getPointerAddressSpace())
3876 auto *GEP = cast<llvm::GEPOperator>(GEPVal);
3877 assert(GEP->isInBounds() &&
"Expected inbounds GEP");
3882 auto *
IntPtrTy = DL.getIntPtrType(GEP->getPointerOperandType());
3885 auto *Zero = llvm::ConstantInt::getNullValue(
IntPtrTy);
3886 auto *SAddIntrinsic =
3888 auto *SMulIntrinsic =
3899 assert((Opcode == BO_Add || Opcode == BO_Mul) &&
"Can't eval binop");
3902 if (
auto *LHSCI = dyn_cast<llvm::ConstantInt>(LHS)) {
3903 if (
auto *RHSCI = dyn_cast<llvm::ConstantInt>(RHS)) {
3905 bool HasOverflow = mayHaveIntegerOverflow(LHSCI, RHSCI, Opcode,
3908 OffsetOverflows =
Builder.getTrue();
3909 return llvm::ConstantInt::get(VMContext, N);
3914 auto *ResultAndOverflow =
Builder.CreateCall(
3915 (Opcode == BO_Add) ? SAddIntrinsic : SMulIntrinsic, {LHS, RHS});
3916 OffsetOverflows =
Builder.CreateOr(
3917 Builder.CreateExtractValue(ResultAndOverflow, 1), OffsetOverflows);
3918 return Builder.CreateExtractValue(ResultAndOverflow, 0);
3922 for (
auto GTI = llvm::gep_type_begin(GEP), GTE = llvm::gep_type_end(GEP);
3923 GTI != GTE; ++GTI) {
3925 auto *Index = GTI.getOperand();
3927 if (
auto *STy = GTI.getStructTypeOrNull()) {
3930 unsigned FieldNo = cast<llvm::ConstantInt>(Index)->getZExtValue();
3931 LocalOffset = llvm::ConstantInt::get(
3932 IntPtrTy, DL.getStructLayout(STy)->getElementOffset(FieldNo));
3936 auto *ElementSize = llvm::ConstantInt::get(
3937 IntPtrTy, DL.getTypeAllocSize(GTI.getIndexedType()));
3939 LocalOffset = eval(BO_Mul, ElementSize, IndexS);
3944 if (!TotalOffset || TotalOffset == Zero)
3945 TotalOffset = LocalOffset;
3947 TotalOffset = eval(BO_Add, TotalOffset, LocalOffset);
3951 if (TotalOffset == Zero)
3956 auto *IntPtr =
Builder.CreatePtrToInt(GEP->getPointerOperand(),
IntPtrTy);
3957 auto *ComputedGEP =
Builder.CreateAdd(IntPtr, TotalOffset);
3964 auto *NoOffsetOverflow =
Builder.CreateNot(OffsetOverflows);
3965 if (SignedIndices) {
3966 auto *PosOrZeroValid =
Builder.CreateICmpUGE(ComputedGEP, IntPtr);
3967 auto *PosOrZeroOffset =
Builder.CreateICmpSGE(TotalOffset, Zero);
3970 Builder.CreateSelect(PosOrZeroOffset, PosOrZeroValid, NegValid),
3972 }
else if (!SignedIndices && !IsSubtraction) {
3973 auto *PosOrZeroValid =
Builder.CreateICmpUGE(ComputedGEP, IntPtr);
3974 ValidGEP =
Builder.CreateAnd(PosOrZeroValid, NoOffsetOverflow);
3976 auto *NegOrZeroValid =
Builder.CreateICmpULE(ComputedGEP, IntPtr);
3977 ValidGEP =
Builder.CreateAnd(NegOrZeroValid, NoOffsetOverflow);
3982 llvm::Value *DynamicArgs[] = {IntPtr, ComputedGEP};
3983 EmitCheck(std::make_pair(ValidGEP, SanitizerKind::PointerOverflow),
3984 SanitizerHandler::PointerOverflow, StaticArgs, DynamicArgs);
unsigned getAddressSpace() const
Return the address space of this type.
Defines the clang::ASTContext interface.
unsigned getNumInits() const
CastKind getCastKind() const
The null pointer literal (C++11 [lex.nullptr])
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
static BinOpInfo createBinOpInfoFromIncDec(const UnaryOperator *E, llvm::Value *InVal, bool IsInc)
bool isNullPtrType() const
bool isSignedOverflowDefined() const
PointerType - C99 6.7.5.1 - Pointer Declarators.
A (possibly-)qualified type.
bool isVirtual() const
Determines whether the base class is a virtual base class (or not).
llvm::Value * getPointer() const
unsigned getFieldCount() const
getFieldCount - Get the number of fields in the layout.
static Opcode getOpForCompoundAssignment(Opcode Opc)
Represents a version number in the form major[.minor[.subminor[.build]]].
QualType getType() const
Retrieves the type of the base class.
Expr * getExpr(unsigned Index)
getExpr - Return the Expr at the specified index.
A type trait used in the implementation of various C++11 and Library TR1 trait templates.
CompoundStmt * getSubStmt()
LValue EmitObjCIsaExpr(const ObjCIsaExpr *E)
bool isOne() const
isOne - Test whether the quantity equals one.
static llvm::Constant * getAsInt32(llvm::ConstantInt *C, llvm::Type *I32Ty)
Stmt - This represents one statement.
bool isArgumentType() const
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
TypeSourceInfo * getTypeSourceInfo() const
unsigned getPackLength() const
Retrieve the length of the parameter pack.
Address getAddress() const
Represents the index of the current element of an array being initialized by an ArrayInitLoopExpr.
ParenExpr - This represents a parethesized expression, e.g.
const llvm::DataLayout & getDataLayout() const
Optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
unsigned getArrayExprIndex() const
For an array element node, returns the index into the array of expressions.
const Expr * getResultExpr() const
The generic selection's result expression.
const ObjCObjectType * getObjectType() const
Gets the type pointed to by this ObjC pointer.
An Embarcadero array type trait, as used in the implementation of __array_rank and __array_extent...
bool isBooleanType() const
Floating point control options.
const LangOptions & getLangOpts() const
Represents a prvalue temporary that is written into memory so that a reference can bind to it...
static Value * buildFMulAdd(llvm::BinaryOperator *MulOp, Value *Addend, const CodeGenFunction &CGF, CGBuilderTy &Builder, bool negMul, bool negAdd)
Expr * getIndexExpr(unsigned Idx)
bool hadArrayRangeDesignator() const
ObjCIsaExpr - Represent X->isa and X.isa when X is an ObjC 'id' type.
CompoundLiteralExpr - [C99 6.5.2.5].
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
field_iterator field_begin() const
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
UnaryExprOrTypeTrait getKind() const
unsigned getValue() const
A C++ throw-expression (C++ [except.throw]).
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Expr * IgnoreImpCasts() LLVM_READONLY
IgnoreImpCasts - Skip past any implicit casts which might surround this expression.
void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index, QualType IndexType, bool Accessed)
Emit a check that Base points into an array object, which we can access at index Index.
RecordDecl - Represents a struct/union/class.
An object to manage conditionally-evaluated expressions.
bool isNullPointer() const
llvm::Value * EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, bool isInc, bool isPre)
void EmitCheck(ArrayRef< std::pair< llvm::Value *, SanitizerMask >> Checked, SanitizerHandler Check, ArrayRef< llvm::Constant * > StaticArgs, ArrayRef< llvm::Value * > DynamicArgs)
Create a basic block that will call a handler function in a sanitizer runtime with the provided argum...
ShuffleVectorExpr - clang-specific builtin-in function __builtin_shufflevector.
bool isVolatileQualified() const
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
bool isReferenceType() const
FieldDecl - An instance of this class is created by Sema::ActOnField to represent a member of a struc...
An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
const CXXRecordDecl * getPointeeCXXRecordDecl() const
If this is a pointer or reference to a RecordType, return the CXXRecordDecl that that type refers to...
GNUNullExpr - Implements the GNU __null extension, which is a name for a null pointer constant that h...
llvm::APSInt getShuffleMaskIdx(const ASTContext &Ctx, unsigned N) const
ObjCArrayLiteral - used for objective-c array containers; as in: @["Hello", NSApp, [NSNumber numberWithInt:42]];.
An r-value expression (a pr-value in the C++11 taxonomy) produces a temporary value.
Describes an C or C++ initializer list.
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
static bool hasScalarEvaluationKind(QualType T)
Expr * getTrueExpr() const
Address CreateElementBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Cast the element type of the given address to a different type, preserving information like the align...
CharUnits - This is an opaque type for sizes expressed in character units.
APValue Val
Val - This is the value the expression can be folded to.
path_iterator path_begin()
llvm::PointerType * VoidPtrTy
A builtin binary operation expression such as "x + y" or "x <= y".
RecordDecl * getDecl() const
bool isUnsignedIntegerType() const
Return true if this is an integer type that is unsigned, according to C99 6.2.5p6 [which returns true...
static Value * tryEmitFMulAdd(const BinOpInfo &op, const CodeGenFunction &CGF, CGBuilderTy &Builder, bool isSub=false)
ObjCStringLiteral, used for Objective-C string literals i.e.
static llvm::Constant * getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx, unsigned Off, llvm::Type *I32Ty)
Scope - A scope is a transient data structure that is used while parsing the program.
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Helper class for OffsetOfExpr.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
bool isExtVectorType() const
detail::InMemoryDirectory::const_iterator I
llvm::Value * EmitCheckedInBoundsGEP(llvm::Value *Ptr, ArrayRef< llvm::Value * > IdxList, bool SignedIndices, bool IsSubtraction, SourceLocation Loc, const Twine &Name="")
Same as IRBuilder::CreateInBoundsGEP, but additionally emits a check to detect undefined behavior whe...
A default argument (C++ [dcl.fct.default]).
static bool ShouldNullCheckClassCastValue(const CastExpr *Cast)
Checking the operand of a load. Must be suitably sized and aligned.
This object can be modified without requiring retains or releases.
Represents the this expression in C++.
field_iterator field_end() const
VersionTuple getVersion()
Sema - This implements semantic analysis and AST building for C.
static CharUnits One()
One - Construct a CharUnits quantity of one.
llvm::APInt getValue() const
Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const
EvaluateAsRValue - Return true if this is a constant which we can fold to an rvalue using any crazy t...
CastKind
CastKind - The kind of operation required for a conversion.
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand...
const Expr * getExpr() const
Get the initialization expression that will be used.
Represents a call to the builtin function __builtin_va_arg.
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
bool isRealFloatingType() const
Floating point categories.
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
const ObjCMethodDecl * getMethodDecl() const
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
An expression "T()" which creates a value-initialized rvalue of type T, which is a non-class type...
llvm::Value * getPointer() const
ValueDecl - Represent the declaration of a variable (in which case it is an lvalue) a function (in wh...
Expr - This represents one expression.
Allow any unmodeled side effect.
bool isAnyComplexType() const
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
llvm::Value * EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, QualType DstTy, SourceLocation Loc)
Emit a conversion from the specified complex type to the specified destination type, where the destination type is an LLVM scalar type.
SourceLocation getExprLoc() const LLVM_READONLY
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
ObjCDictionaryLiteral - AST node to represent objective-c dictionary literals; as in:"name" : NSUserN...
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
bool isFloatingType() const
ObjCSelectorExpr used for @selector in Objective-C.
Represents an expression that computes the length of a parameter pack.
AsTypeExpr - Clang builtin function __builtin_astype [OpenCL 6.2.4.2] This AST node provides support ...
llvm::LLVMContext & getLLVMContext()
llvm::IntegerType * Int32Ty
An RAII object to record that we're evaluating a statement expression.
void dump() const
Dumps the specified AST fragment and all subtrees to llvm::errs().
Expr * getSubExpr() const
bool EvaluateAsInt(llvm::APSInt &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer...
Expr * getSrcExpr() const
getSrcExpr - Return the Expr to be converted.
An expression that sends a message to the given Objective-C object or class.
unsigned getNumComponents() const
CXXBaseSpecifier * getBase() const
For a base class node, returns the base specifier.
UnaryOperator - This represents the unary-expression's (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
Represents a GCC generic vector type.
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=None)
Represents a reference to a non-type template parameter that has been substituted with a template arg...
The scope of a CXXDefaultInitExpr.
QualType getElementType() const
QualType getComputationLHSType() const
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
QualType getComputationResultType() const
unsigned getNumSubExprs() const
getNumSubExprs - Return the size of the SubExprs array.
The l-value was considered opaque, so the alignment was determined from a type.
There is no lifetime qualification on this type.
A C++ dynamic_cast expression (C++ [expr.dynamic.cast]).
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class...
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
ConvertVectorExpr - Clang builtin function __builtin_convertvector This AST node provides support for...
Assigning into this object requires the old value to be released and the new value to be retained...
A field in a dependent type, known only by its name.
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
ASTContext & getContext() const
Encodes a location in the source.
CharUnits getPointerAlign() const
bool hasIntegerRepresentation() const
Determine whether this type has an integer representation of some sort, e.g., it is an integer type o...
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
SourceLocation getExprLoc() const LLVM_READONLY
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)"...
static OMPLinearClause * Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef< Expr * > VL, ArrayRef< Expr * > PL, ArrayRef< Expr * > IL, Expr *Step, Expr *CalcStep, Stmt *PreInit, Expr *PostUpdate)
Creates clause with a list of variables VL and a linear step Step.
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx, SmallVectorImpl< PartialDiagnosticAt > *Diag=nullptr) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
A scoped helper to set the current debug location to the specified location or preferred location of ...
Expr * getSrcExpr() const
getSrcExpr - Return the Expr to be converted.
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
bool isCompoundAssignmentOp() const
SanitizerSet SanOpts
Sanitizers enabled for this function.
AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, __atomic_load, __atomic_store, and __atomic_compare_exchange_*, for the similarly-named C++11 instructions, and __c11 variants for <stdatomic.h>.
unsigned getMajor() const
Retrieve the major version number.
ObjCProtocolExpr used for protocol expression in Objective-C.
TypeCheckKind
Situations in which we might emit a check for the suitability of a pointer or glvalue.
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
QualType getReturnType() const
const T * castAs() const
Member-template castAs<specific type>.
bool isVectorType() const
An expression trait intrinsic.
bool isPromotableIntegerType() const
More type predicates useful for type checking/promotion.
uint64_t getValue() const
Assigning into this object requires a lifetime extension.
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
ObjCBoxedExpr - used for generalized expression boxing.
QualType getType() const
Return the type wrapped by this type source info.
static Value * createCastsForTypeOfSameSize(CGBuilderTy &Builder, const llvm::DataLayout &DL, Value *Src, llvm::Type *DstTy, StringRef Name="")
const OffsetOfNode & getComponent(unsigned Idx) const
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
QualType getPointeeType() const
CompoundAssignOperator - For compound assignments (e.g.
Represents a C11 generic selection.
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type, returning the result.
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
AddrLabelExpr - The GNU address of label extension, representing &&label.
Expr * getReplacement() const
SourceLocation getExprLoc() const LLVM_READONLY
bool allowFPContractAcrossStatement() const
const Expr * getExpr() const
EvalResult is a struct with detailed info about an evaluated expression.
Represents a delete expression for memory deallocation and destructor calls, e.g. ...
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
A runtime availability query.
llvm::IntegerType * IntPtrTy
Represents a 'co_yield' expression.
static llvm::Intrinsic::ID GetIntrinsic(IntrinsicType IT, BuiltinType::Kind ElemKind)
static Value * emitPointerArithmetic(CodeGenFunction &CGF, const BinOpInfo &op, bool isSubtraction)
Emit pointer + index arithmetic.
Checking the destination of a store. Must be suitably sized and aligned.
detail::InMemoryDirectory::const_iterator E
A pointer to member type per C++ 8.3.3 - Pointers to members.
ExplicitCastExpr - An explicit cast written in the source code.
static Value * ConvertVec3AndVec4(CGBuilderTy &Builder, CodeGenFunction &CGF, Value *Src, unsigned NumElementsDst)
specific_decl_iterator - Iterates over a subrange of declarations stored in a DeclContext, providing only those that are of type SpecificDecl (or a class derived from it).
llvm::APFloat getValue() const
const VariableArrayType * getAsVariableArrayType(QualType T) const
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
#define VISITCOMP(CODE, UI, SI, FP)
Represents a pointer to an Objective C object.
Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
bool isEvaluatable(const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects) const
isEvaluatable - Call EvaluateAsRValue to see if this expression can be constant folded without side-e...
bool HasSideEffects
Whether the evaluated expression has side effects.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Complex values, per C99 6.2.5p11.
const T * getAs() const
Member-template getAs<specific type>'.
Checking the operand of a static_cast to a derived pointer type.
Expr * getFalseExpr() const
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
QualType getTypeOfArgument() const
Gets the argument type, or the type of the argument expression, whichever is appropriate.
QualType getCanonicalType() const
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
An implicit indirection through a C++ base class, when the field found is in a base class...
Represents a 'co_await' expression.
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
bool isFunctionType() const
ExtVectorType - Extended vector type.
Optional< unsigned > getSubminor() const
Retrieve the subminor version number, if provided.
SourceLocation getExprLoc() const LLVM_READONLY
LabelDecl * getLabel() const
ObjCIvarRefExpr - A reference to an ObjC instance variable.
llvm::ConstantInt * getSize(CharUnits numChars)
Emit the given number of characters as a value of type size_t.
A use of a default initializer in a constructor or in aggregate initialization.
Reading or writing from this object requires a barrier call.
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
const Expr * getSubExpr() const
Represents a C++ struct/union/class.
BoundNodesTreeBuilder *const Builder
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
llvm::Type * ConvertType(QualType T)
FPOptions getFPFeatures() const
LValue MakeAddrLValue(Address Addr, QualType T, LValueBaseInfo BaseInfo=LValueBaseInfo(AlignmentSource::Type))
LValue EmitLValue(const Expr *E)
EmitLValue - Emit code to compute a designator that specifies the location of the expression...
FieldDecl * getField() const
For a field offsetof node, returns the field.
This class is used for builtin types like 'int'.
std::pair< llvm::Value *, QualType > getVLASize(const VariableArrayType *vla)
getVLASize - Returns an LLVM value that corresponds to the size, in non-variably-sized elements...
Defines the clang::TargetInfo interface.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E)
llvm::Value * EmitScalarConversion(llvm::Value *Src, QualType SrcTy, QualType DstTy, SourceLocation Loc)
Emit a conversion from the specified type to the specified destination type, both of which are LLVM s...
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
A reference to a declared variable, function, enum, etc.
static RValue get(llvm::Value *V)
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF)
Set the IRBuilder to not attach debug locations.
const Expr * getInit(unsigned Init) const
llvm::Constant * EmitCheckSourceLocation(SourceLocation Loc)
Emit a description of a source location in a format suitable for passing to a runtime sanitizer handl...
LValue - This represents an lvalue references.
A boolean literal, per ([C++ lex.bool] Boolean literals).
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type, member-designator).
Represents a C array with a specified size that is not an integer-constant-expression.
bool hasSignedIntegerRepresentation() const
Determine whether this type has an signed integer representation of some sort, e.g., it is an signed integer type or a vector.
Address CreatePointerBitCastOrAddrSpaceCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
SourceLocation getLocStart() const LLVM_READONLY
static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E, CodeGenFunction &CGF)
isCheapEnoughToEvaluateUnconditionally - Return true if the specified expression is cheap enough and ...
Represents an implicitly-generated value initialization of an object of a given type.
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Kind getKind() const
Determine what kind of offsetof node this is.
Expr * IgnoreParens() LLVM_READONLY
IgnoreParens - Ignore parentheses.