1f4a2713aSLionel Sambuc //===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This contains code to emit Expr nodes as LLVM code.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "CodeGenFunction.h"
15f4a2713aSLionel Sambuc #include "CGCXXABI.h"
16f4a2713aSLionel Sambuc #include "CGCall.h"
17f4a2713aSLionel Sambuc #include "CGDebugInfo.h"
18f4a2713aSLionel Sambuc #include "CGObjCRuntime.h"
19*0a6a1f1dSLionel Sambuc #include "CGOpenMPRuntime.h"
20f4a2713aSLionel Sambuc #include "CGRecordLayout.h"
21f4a2713aSLionel Sambuc #include "CodeGenModule.h"
22f4a2713aSLionel Sambuc #include "TargetInfo.h"
23f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
24*0a6a1f1dSLionel Sambuc #include "clang/AST/Attr.h"
25f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
26f4a2713aSLionel Sambuc #include "clang/Frontend/CodeGenOptions.h"
27f4a2713aSLionel Sambuc #include "llvm/ADT/Hashing.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringExtras.h"
29f4a2713aSLionel Sambuc #include "llvm/IR/DataLayout.h"
30f4a2713aSLionel Sambuc #include "llvm/IR/Intrinsics.h"
31f4a2713aSLionel Sambuc #include "llvm/IR/LLVMContext.h"
32f4a2713aSLionel Sambuc #include "llvm/IR/MDBuilder.h"
33f4a2713aSLionel Sambuc #include "llvm/Support/ConvertUTF.h"
34f4a2713aSLionel Sambuc
35f4a2713aSLionel Sambuc using namespace clang;
36f4a2713aSLionel Sambuc using namespace CodeGen;
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc //===--------------------------------------------------------------------===//
39f4a2713aSLionel Sambuc // Miscellaneous Helper Methods
40f4a2713aSLionel Sambuc //===--------------------------------------------------------------------===//
41f4a2713aSLionel Sambuc
EmitCastToVoidPtr(llvm::Value * value)42f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) {
43f4a2713aSLionel Sambuc unsigned addressSpace =
44f4a2713aSLionel Sambuc cast<llvm::PointerType>(value->getType())->getAddressSpace();
45f4a2713aSLionel Sambuc
46f4a2713aSLionel Sambuc llvm::PointerType *destType = Int8PtrTy;
47f4a2713aSLionel Sambuc if (addressSpace)
48f4a2713aSLionel Sambuc destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace);
49f4a2713aSLionel Sambuc
50f4a2713aSLionel Sambuc if (value->getType() == destType) return value;
51f4a2713aSLionel Sambuc return Builder.CreateBitCast(value, destType);
52f4a2713aSLionel Sambuc }
53f4a2713aSLionel Sambuc
54f4a2713aSLionel Sambuc /// CreateTempAlloca - This creates a alloca and inserts it into the entry
55f4a2713aSLionel Sambuc /// block.
CreateTempAlloca(llvm::Type * Ty,const Twine & Name)56f4a2713aSLionel Sambuc llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
57f4a2713aSLionel Sambuc const Twine &Name) {
58f4a2713aSLionel Sambuc if (!Builder.isNamePreserving())
59*0a6a1f1dSLionel Sambuc return new llvm::AllocaInst(Ty, nullptr, "", AllocaInsertPt);
60*0a6a1f1dSLionel Sambuc return new llvm::AllocaInst(Ty, nullptr, Name, AllocaInsertPt);
61f4a2713aSLionel Sambuc }
62f4a2713aSLionel Sambuc
InitTempAlloca(llvm::AllocaInst * Var,llvm::Value * Init)63f4a2713aSLionel Sambuc void CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var,
64f4a2713aSLionel Sambuc llvm::Value *Init) {
65*0a6a1f1dSLionel Sambuc auto *Store = new llvm::StoreInst(Init, Var);
66f4a2713aSLionel Sambuc llvm::BasicBlock *Block = AllocaInsertPt->getParent();
67f4a2713aSLionel Sambuc Block->getInstList().insertAfter(&*AllocaInsertPt, Store);
68f4a2713aSLionel Sambuc }
69f4a2713aSLionel Sambuc
CreateIRTemp(QualType Ty,const Twine & Name)70f4a2713aSLionel Sambuc llvm::AllocaInst *CodeGenFunction::CreateIRTemp(QualType Ty,
71f4a2713aSLionel Sambuc const Twine &Name) {
72f4a2713aSLionel Sambuc llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name);
73f4a2713aSLionel Sambuc // FIXME: Should we prefer the preferred type alignment here?
74f4a2713aSLionel Sambuc CharUnits Align = getContext().getTypeAlignInChars(Ty);
75f4a2713aSLionel Sambuc Alloc->setAlignment(Align.getQuantity());
76f4a2713aSLionel Sambuc return Alloc;
77f4a2713aSLionel Sambuc }
78f4a2713aSLionel Sambuc
CreateMemTemp(QualType Ty,const Twine & Name)79f4a2713aSLionel Sambuc llvm::AllocaInst *CodeGenFunction::CreateMemTemp(QualType Ty,
80f4a2713aSLionel Sambuc const Twine &Name) {
81f4a2713aSLionel Sambuc llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name);
82f4a2713aSLionel Sambuc // FIXME: Should we prefer the preferred type alignment here?
83f4a2713aSLionel Sambuc CharUnits Align = getContext().getTypeAlignInChars(Ty);
84f4a2713aSLionel Sambuc Alloc->setAlignment(Align.getQuantity());
85f4a2713aSLionel Sambuc return Alloc;
86f4a2713aSLionel Sambuc }
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
89f4a2713aSLionel Sambuc /// expression and compare the result against zero, returning an Int1Ty value.
EvaluateExprAsBool(const Expr * E)90f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
91*0a6a1f1dSLionel Sambuc PGO.setCurrentStmt(E);
92f4a2713aSLionel Sambuc if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
93f4a2713aSLionel Sambuc llvm::Value *MemPtr = EmitScalarExpr(E);
94f4a2713aSLionel Sambuc return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT);
95f4a2713aSLionel Sambuc }
96f4a2713aSLionel Sambuc
97f4a2713aSLionel Sambuc QualType BoolTy = getContext().BoolTy;
98f4a2713aSLionel Sambuc if (!E->getType()->isAnyComplexType())
99f4a2713aSLionel Sambuc return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
100f4a2713aSLionel Sambuc
101f4a2713aSLionel Sambuc return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
102f4a2713aSLionel Sambuc }
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc /// EmitIgnoredExpr - Emit code to compute the specified expression,
105f4a2713aSLionel Sambuc /// ignoring the result.
EmitIgnoredExpr(const Expr * E)106f4a2713aSLionel Sambuc void CodeGenFunction::EmitIgnoredExpr(const Expr *E) {
107f4a2713aSLionel Sambuc if (E->isRValue())
108f4a2713aSLionel Sambuc return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true);
109f4a2713aSLionel Sambuc
110f4a2713aSLionel Sambuc // Just emit it as an l-value and drop the result.
111f4a2713aSLionel Sambuc EmitLValue(E);
112f4a2713aSLionel Sambuc }
113f4a2713aSLionel Sambuc
114f4a2713aSLionel Sambuc /// EmitAnyExpr - Emit code to compute the specified expression which
115f4a2713aSLionel Sambuc /// can have any type. The result is returned as an RValue struct.
116f4a2713aSLionel Sambuc /// If this is an aggregate expression, AggSlot indicates where the
117f4a2713aSLionel Sambuc /// result should be returned.
EmitAnyExpr(const Expr * E,AggValueSlot aggSlot,bool ignoreResult)118f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitAnyExpr(const Expr *E,
119f4a2713aSLionel Sambuc AggValueSlot aggSlot,
120f4a2713aSLionel Sambuc bool ignoreResult) {
121f4a2713aSLionel Sambuc switch (getEvaluationKind(E->getType())) {
122f4a2713aSLionel Sambuc case TEK_Scalar:
123f4a2713aSLionel Sambuc return RValue::get(EmitScalarExpr(E, ignoreResult));
124f4a2713aSLionel Sambuc case TEK_Complex:
125f4a2713aSLionel Sambuc return RValue::getComplex(EmitComplexExpr(E, ignoreResult, ignoreResult));
126f4a2713aSLionel Sambuc case TEK_Aggregate:
127f4a2713aSLionel Sambuc if (!ignoreResult && aggSlot.isIgnored())
128f4a2713aSLionel Sambuc aggSlot = CreateAggTemp(E->getType(), "agg-temp");
129f4a2713aSLionel Sambuc EmitAggExpr(E, aggSlot);
130f4a2713aSLionel Sambuc return aggSlot.asRValue();
131f4a2713aSLionel Sambuc }
132f4a2713aSLionel Sambuc llvm_unreachable("bad evaluation kind");
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc
135f4a2713aSLionel Sambuc /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
136f4a2713aSLionel Sambuc /// always be accessible even if no aggregate location is provided.
EmitAnyExprToTemp(const Expr * E)137f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) {
138f4a2713aSLionel Sambuc AggValueSlot AggSlot = AggValueSlot::ignored();
139f4a2713aSLionel Sambuc
140f4a2713aSLionel Sambuc if (hasAggregateEvaluationKind(E->getType()))
141f4a2713aSLionel Sambuc AggSlot = CreateAggTemp(E->getType(), "agg.tmp");
142f4a2713aSLionel Sambuc return EmitAnyExpr(E, AggSlot);
143f4a2713aSLionel Sambuc }
144f4a2713aSLionel Sambuc
145f4a2713aSLionel Sambuc /// EmitAnyExprToMem - Evaluate an expression into a given memory
146f4a2713aSLionel Sambuc /// location.
EmitAnyExprToMem(const Expr * E,llvm::Value * Location,Qualifiers Quals,bool IsInit)147f4a2713aSLionel Sambuc void CodeGenFunction::EmitAnyExprToMem(const Expr *E,
148f4a2713aSLionel Sambuc llvm::Value *Location,
149f4a2713aSLionel Sambuc Qualifiers Quals,
150f4a2713aSLionel Sambuc bool IsInit) {
151f4a2713aSLionel Sambuc // FIXME: This function should take an LValue as an argument.
152f4a2713aSLionel Sambuc switch (getEvaluationKind(E->getType())) {
153f4a2713aSLionel Sambuc case TEK_Complex:
154f4a2713aSLionel Sambuc EmitComplexExprIntoLValue(E,
155f4a2713aSLionel Sambuc MakeNaturalAlignAddrLValue(Location, E->getType()),
156f4a2713aSLionel Sambuc /*isInit*/ false);
157f4a2713aSLionel Sambuc return;
158f4a2713aSLionel Sambuc
159f4a2713aSLionel Sambuc case TEK_Aggregate: {
160f4a2713aSLionel Sambuc CharUnits Alignment = getContext().getTypeAlignInChars(E->getType());
161f4a2713aSLionel Sambuc EmitAggExpr(E, AggValueSlot::forAddr(Location, Alignment, Quals,
162f4a2713aSLionel Sambuc AggValueSlot::IsDestructed_t(IsInit),
163f4a2713aSLionel Sambuc AggValueSlot::DoesNotNeedGCBarriers,
164f4a2713aSLionel Sambuc AggValueSlot::IsAliased_t(!IsInit)));
165f4a2713aSLionel Sambuc return;
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc
168f4a2713aSLionel Sambuc case TEK_Scalar: {
169f4a2713aSLionel Sambuc RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
170f4a2713aSLionel Sambuc LValue LV = MakeAddrLValue(Location, E->getType());
171f4a2713aSLionel Sambuc EmitStoreThroughLValue(RV, LV);
172f4a2713aSLionel Sambuc return;
173f4a2713aSLionel Sambuc }
174f4a2713aSLionel Sambuc }
175f4a2713aSLionel Sambuc llvm_unreachable("bad evaluation kind");
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc
178f4a2713aSLionel Sambuc static void
pushTemporaryCleanup(CodeGenFunction & CGF,const MaterializeTemporaryExpr * M,const Expr * E,llvm::Value * ReferenceTemporary)179f4a2713aSLionel Sambuc pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
180f4a2713aSLionel Sambuc const Expr *E, llvm::Value *ReferenceTemporary) {
181f4a2713aSLionel Sambuc // Objective-C++ ARC:
182f4a2713aSLionel Sambuc // If we are binding a reference to a temporary that has ownership, we
183f4a2713aSLionel Sambuc // need to perform retain/release operations on the temporary.
184f4a2713aSLionel Sambuc //
185f4a2713aSLionel Sambuc // FIXME: This should be looking at E, not M.
186f4a2713aSLionel Sambuc if (CGF.getLangOpts().ObjCAutoRefCount &&
187f4a2713aSLionel Sambuc M->getType()->isObjCLifetimeType()) {
188f4a2713aSLionel Sambuc QualType ObjCARCReferenceLifetimeType = M->getType();
189f4a2713aSLionel Sambuc switch (Qualifiers::ObjCLifetime Lifetime =
190f4a2713aSLionel Sambuc ObjCARCReferenceLifetimeType.getObjCLifetime()) {
191f4a2713aSLionel Sambuc case Qualifiers::OCL_None:
192f4a2713aSLionel Sambuc case Qualifiers::OCL_ExplicitNone:
193f4a2713aSLionel Sambuc // Carry on to normal cleanup handling.
194f4a2713aSLionel Sambuc break;
195f4a2713aSLionel Sambuc
196f4a2713aSLionel Sambuc case Qualifiers::OCL_Autoreleasing:
197f4a2713aSLionel Sambuc // Nothing to do; cleaned up by an autorelease pool.
198f4a2713aSLionel Sambuc return;
199f4a2713aSLionel Sambuc
200f4a2713aSLionel Sambuc case Qualifiers::OCL_Strong:
201f4a2713aSLionel Sambuc case Qualifiers::OCL_Weak:
202f4a2713aSLionel Sambuc switch (StorageDuration Duration = M->getStorageDuration()) {
203f4a2713aSLionel Sambuc case SD_Static:
204f4a2713aSLionel Sambuc // Note: we intentionally do not register a cleanup to release
205f4a2713aSLionel Sambuc // the object on program termination.
206f4a2713aSLionel Sambuc return;
207f4a2713aSLionel Sambuc
208f4a2713aSLionel Sambuc case SD_Thread:
209f4a2713aSLionel Sambuc // FIXME: We should probably register a cleanup in this case.
210f4a2713aSLionel Sambuc return;
211f4a2713aSLionel Sambuc
212f4a2713aSLionel Sambuc case SD_Automatic:
213f4a2713aSLionel Sambuc case SD_FullExpression:
214f4a2713aSLionel Sambuc CodeGenFunction::Destroyer *Destroy;
215f4a2713aSLionel Sambuc CleanupKind CleanupKind;
216f4a2713aSLionel Sambuc if (Lifetime == Qualifiers::OCL_Strong) {
217f4a2713aSLionel Sambuc const ValueDecl *VD = M->getExtendingDecl();
218f4a2713aSLionel Sambuc bool Precise =
219f4a2713aSLionel Sambuc VD && isa<VarDecl>(VD) && VD->hasAttr<ObjCPreciseLifetimeAttr>();
220f4a2713aSLionel Sambuc CleanupKind = CGF.getARCCleanupKind();
221f4a2713aSLionel Sambuc Destroy = Precise ? &CodeGenFunction::destroyARCStrongPrecise
222f4a2713aSLionel Sambuc : &CodeGenFunction::destroyARCStrongImprecise;
223f4a2713aSLionel Sambuc } else {
224f4a2713aSLionel Sambuc // __weak objects always get EH cleanups; otherwise, exceptions
225f4a2713aSLionel Sambuc // could cause really nasty crashes instead of mere leaks.
226f4a2713aSLionel Sambuc CleanupKind = NormalAndEHCleanup;
227f4a2713aSLionel Sambuc Destroy = &CodeGenFunction::destroyARCWeak;
228f4a2713aSLionel Sambuc }
229f4a2713aSLionel Sambuc if (Duration == SD_FullExpression)
230f4a2713aSLionel Sambuc CGF.pushDestroy(CleanupKind, ReferenceTemporary,
231f4a2713aSLionel Sambuc ObjCARCReferenceLifetimeType, *Destroy,
232f4a2713aSLionel Sambuc CleanupKind & EHCleanup);
233f4a2713aSLionel Sambuc else
234f4a2713aSLionel Sambuc CGF.pushLifetimeExtendedDestroy(CleanupKind, ReferenceTemporary,
235f4a2713aSLionel Sambuc ObjCARCReferenceLifetimeType,
236f4a2713aSLionel Sambuc *Destroy, CleanupKind & EHCleanup);
237f4a2713aSLionel Sambuc return;
238f4a2713aSLionel Sambuc
239f4a2713aSLionel Sambuc case SD_Dynamic:
240f4a2713aSLionel Sambuc llvm_unreachable("temporary cannot have dynamic storage duration");
241f4a2713aSLionel Sambuc }
242f4a2713aSLionel Sambuc llvm_unreachable("unknown storage duration");
243f4a2713aSLionel Sambuc }
244f4a2713aSLionel Sambuc }
245f4a2713aSLionel Sambuc
246*0a6a1f1dSLionel Sambuc CXXDestructorDecl *ReferenceTemporaryDtor = nullptr;
247f4a2713aSLionel Sambuc if (const RecordType *RT =
248f4a2713aSLionel Sambuc E->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) {
249f4a2713aSLionel Sambuc // Get the destructor for the reference temporary.
250*0a6a1f1dSLionel Sambuc auto *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
251f4a2713aSLionel Sambuc if (!ClassDecl->hasTrivialDestructor())
252f4a2713aSLionel Sambuc ReferenceTemporaryDtor = ClassDecl->getDestructor();
253f4a2713aSLionel Sambuc }
254f4a2713aSLionel Sambuc
255f4a2713aSLionel Sambuc if (!ReferenceTemporaryDtor)
256f4a2713aSLionel Sambuc return;
257f4a2713aSLionel Sambuc
258f4a2713aSLionel Sambuc // Call the destructor for the temporary.
259f4a2713aSLionel Sambuc switch (M->getStorageDuration()) {
260f4a2713aSLionel Sambuc case SD_Static:
261f4a2713aSLionel Sambuc case SD_Thread: {
262f4a2713aSLionel Sambuc llvm::Constant *CleanupFn;
263f4a2713aSLionel Sambuc llvm::Constant *CleanupArg;
264f4a2713aSLionel Sambuc if (E->getType()->isArrayType()) {
265f4a2713aSLionel Sambuc CleanupFn = CodeGenFunction(CGF.CGM).generateDestroyHelper(
266f4a2713aSLionel Sambuc cast<llvm::Constant>(ReferenceTemporary), E->getType(),
267f4a2713aSLionel Sambuc CodeGenFunction::destroyCXXObject, CGF.getLangOpts().Exceptions,
268f4a2713aSLionel Sambuc dyn_cast_or_null<VarDecl>(M->getExtendingDecl()));
269f4a2713aSLionel Sambuc CleanupArg = llvm::Constant::getNullValue(CGF.Int8PtrTy);
270f4a2713aSLionel Sambuc } else {
271*0a6a1f1dSLionel Sambuc CleanupFn = CGF.CGM.getAddrOfCXXStructor(ReferenceTemporaryDtor,
272*0a6a1f1dSLionel Sambuc StructorType::Complete);
273f4a2713aSLionel Sambuc CleanupArg = cast<llvm::Constant>(ReferenceTemporary);
274f4a2713aSLionel Sambuc }
275f4a2713aSLionel Sambuc CGF.CGM.getCXXABI().registerGlobalDtor(
276f4a2713aSLionel Sambuc CGF, *cast<VarDecl>(M->getExtendingDecl()), CleanupFn, CleanupArg);
277f4a2713aSLionel Sambuc break;
278f4a2713aSLionel Sambuc }
279f4a2713aSLionel Sambuc
280f4a2713aSLionel Sambuc case SD_FullExpression:
281f4a2713aSLionel Sambuc CGF.pushDestroy(NormalAndEHCleanup, ReferenceTemporary, E->getType(),
282f4a2713aSLionel Sambuc CodeGenFunction::destroyCXXObject,
283f4a2713aSLionel Sambuc CGF.getLangOpts().Exceptions);
284f4a2713aSLionel Sambuc break;
285f4a2713aSLionel Sambuc
286f4a2713aSLionel Sambuc case SD_Automatic:
287f4a2713aSLionel Sambuc CGF.pushLifetimeExtendedDestroy(NormalAndEHCleanup,
288f4a2713aSLionel Sambuc ReferenceTemporary, E->getType(),
289f4a2713aSLionel Sambuc CodeGenFunction::destroyCXXObject,
290f4a2713aSLionel Sambuc CGF.getLangOpts().Exceptions);
291f4a2713aSLionel Sambuc break;
292f4a2713aSLionel Sambuc
293f4a2713aSLionel Sambuc case SD_Dynamic:
294f4a2713aSLionel Sambuc llvm_unreachable("temporary cannot have dynamic storage duration");
295f4a2713aSLionel Sambuc }
296f4a2713aSLionel Sambuc }
297f4a2713aSLionel Sambuc
298f4a2713aSLionel Sambuc static llvm::Value *
createReferenceTemporary(CodeGenFunction & CGF,const MaterializeTemporaryExpr * M,const Expr * Inner)299f4a2713aSLionel Sambuc createReferenceTemporary(CodeGenFunction &CGF,
300f4a2713aSLionel Sambuc const MaterializeTemporaryExpr *M, const Expr *Inner) {
301f4a2713aSLionel Sambuc switch (M->getStorageDuration()) {
302f4a2713aSLionel Sambuc case SD_FullExpression:
303f4a2713aSLionel Sambuc case SD_Automatic:
304f4a2713aSLionel Sambuc return CGF.CreateMemTemp(Inner->getType(), "ref.tmp");
305f4a2713aSLionel Sambuc
306f4a2713aSLionel Sambuc case SD_Thread:
307f4a2713aSLionel Sambuc case SD_Static:
308f4a2713aSLionel Sambuc return CGF.CGM.GetAddrOfGlobalTemporary(M, Inner);
309f4a2713aSLionel Sambuc
310f4a2713aSLionel Sambuc case SD_Dynamic:
311f4a2713aSLionel Sambuc llvm_unreachable("temporary can't have dynamic storage duration");
312f4a2713aSLionel Sambuc }
313f4a2713aSLionel Sambuc llvm_unreachable("unknown storage duration");
314f4a2713aSLionel Sambuc }
315f4a2713aSLionel Sambuc
316*0a6a1f1dSLionel Sambuc LValue CodeGenFunction::
EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr * M)317*0a6a1f1dSLionel Sambuc EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) {
318f4a2713aSLionel Sambuc const Expr *E = M->GetTemporaryExpr();
319f4a2713aSLionel Sambuc
320*0a6a1f1dSLionel Sambuc // FIXME: ideally this would use EmitAnyExprToMem, however, we cannot do so
321*0a6a1f1dSLionel Sambuc // as that will cause the lifetime adjustment to be lost for ARC
322f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount &&
323f4a2713aSLionel Sambuc M->getType()->isObjCLifetimeType() &&
324f4a2713aSLionel Sambuc M->getType().getObjCLifetime() != Qualifiers::OCL_None &&
325f4a2713aSLionel Sambuc M->getType().getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
326f4a2713aSLionel Sambuc llvm::Value *Object = createReferenceTemporary(*this, M, E);
327f4a2713aSLionel Sambuc LValue RefTempDst = MakeAddrLValue(Object, M->getType());
328f4a2713aSLionel Sambuc
329*0a6a1f1dSLionel Sambuc if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object)) {
330f4a2713aSLionel Sambuc // We should not have emitted the initializer for this temporary as a
331f4a2713aSLionel Sambuc // constant.
332f4a2713aSLionel Sambuc assert(!Var->hasInitializer());
333f4a2713aSLionel Sambuc Var->setInitializer(CGM.EmitNullConstant(E->getType()));
334f4a2713aSLionel Sambuc }
335f4a2713aSLionel Sambuc
336*0a6a1f1dSLionel Sambuc switch (getEvaluationKind(E->getType())) {
337*0a6a1f1dSLionel Sambuc default: llvm_unreachable("expected scalar or aggregate expression");
338*0a6a1f1dSLionel Sambuc case TEK_Scalar:
339f4a2713aSLionel Sambuc EmitScalarInit(E, M->getExtendingDecl(), RefTempDst, false);
340*0a6a1f1dSLionel Sambuc break;
341*0a6a1f1dSLionel Sambuc case TEK_Aggregate: {
342*0a6a1f1dSLionel Sambuc CharUnits Alignment = getContext().getTypeAlignInChars(E->getType());
343*0a6a1f1dSLionel Sambuc EmitAggExpr(E, AggValueSlot::forAddr(Object, Alignment,
344*0a6a1f1dSLionel Sambuc E->getType().getQualifiers(),
345*0a6a1f1dSLionel Sambuc AggValueSlot::IsDestructed,
346*0a6a1f1dSLionel Sambuc AggValueSlot::DoesNotNeedGCBarriers,
347*0a6a1f1dSLionel Sambuc AggValueSlot::IsNotAliased));
348*0a6a1f1dSLionel Sambuc break;
349*0a6a1f1dSLionel Sambuc }
350*0a6a1f1dSLionel Sambuc }
351f4a2713aSLionel Sambuc
352f4a2713aSLionel Sambuc pushTemporaryCleanup(*this, M, E, Object);
353f4a2713aSLionel Sambuc return RefTempDst;
354f4a2713aSLionel Sambuc }
355f4a2713aSLionel Sambuc
356f4a2713aSLionel Sambuc SmallVector<const Expr *, 2> CommaLHSs;
357f4a2713aSLionel Sambuc SmallVector<SubobjectAdjustment, 2> Adjustments;
358f4a2713aSLionel Sambuc E = E->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
359f4a2713aSLionel Sambuc
360*0a6a1f1dSLionel Sambuc for (const auto &Ignored : CommaLHSs)
361*0a6a1f1dSLionel Sambuc EmitIgnoredExpr(Ignored);
362f4a2713aSLionel Sambuc
363*0a6a1f1dSLionel Sambuc if (const auto *opaque = dyn_cast<OpaqueValueExpr>(E)) {
364f4a2713aSLionel Sambuc if (opaque->getType()->isRecordType()) {
365f4a2713aSLionel Sambuc assert(Adjustments.empty());
366f4a2713aSLionel Sambuc return EmitOpaqueValueLValue(opaque);
367f4a2713aSLionel Sambuc }
368f4a2713aSLionel Sambuc }
369f4a2713aSLionel Sambuc
370f4a2713aSLionel Sambuc // Create and initialize the reference temporary.
371f4a2713aSLionel Sambuc llvm::Value *Object = createReferenceTemporary(*this, M, E);
372*0a6a1f1dSLionel Sambuc if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object)) {
373f4a2713aSLionel Sambuc // If the temporary is a global and has a constant initializer, we may
374f4a2713aSLionel Sambuc // have already initialized it.
375f4a2713aSLionel Sambuc if (!Var->hasInitializer()) {
376f4a2713aSLionel Sambuc Var->setInitializer(CGM.EmitNullConstant(E->getType()));
377f4a2713aSLionel Sambuc EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
378f4a2713aSLionel Sambuc }
379f4a2713aSLionel Sambuc } else {
380f4a2713aSLionel Sambuc EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
381f4a2713aSLionel Sambuc }
382f4a2713aSLionel Sambuc pushTemporaryCleanup(*this, M, E, Object);
383f4a2713aSLionel Sambuc
384f4a2713aSLionel Sambuc // Perform derived-to-base casts and/or field accesses, to get from the
385f4a2713aSLionel Sambuc // temporary object we created (and, potentially, for which we extended
386f4a2713aSLionel Sambuc // the lifetime) to the subobject we're binding the reference to.
387f4a2713aSLionel Sambuc for (unsigned I = Adjustments.size(); I != 0; --I) {
388f4a2713aSLionel Sambuc SubobjectAdjustment &Adjustment = Adjustments[I-1];
389f4a2713aSLionel Sambuc switch (Adjustment.Kind) {
390f4a2713aSLionel Sambuc case SubobjectAdjustment::DerivedToBaseAdjustment:
391f4a2713aSLionel Sambuc Object =
392f4a2713aSLionel Sambuc GetAddressOfBaseClass(Object, Adjustment.DerivedToBase.DerivedClass,
393f4a2713aSLionel Sambuc Adjustment.DerivedToBase.BasePath->path_begin(),
394f4a2713aSLionel Sambuc Adjustment.DerivedToBase.BasePath->path_end(),
395*0a6a1f1dSLionel Sambuc /*NullCheckValue=*/ false, E->getExprLoc());
396f4a2713aSLionel Sambuc break;
397f4a2713aSLionel Sambuc
398f4a2713aSLionel Sambuc case SubobjectAdjustment::FieldAdjustment: {
399f4a2713aSLionel Sambuc LValue LV = MakeAddrLValue(Object, E->getType());
400f4a2713aSLionel Sambuc LV = EmitLValueForField(LV, Adjustment.Field);
401f4a2713aSLionel Sambuc assert(LV.isSimple() &&
402f4a2713aSLionel Sambuc "materialized temporary field is not a simple lvalue");
403f4a2713aSLionel Sambuc Object = LV.getAddress();
404f4a2713aSLionel Sambuc break;
405f4a2713aSLionel Sambuc }
406f4a2713aSLionel Sambuc
407f4a2713aSLionel Sambuc case SubobjectAdjustment::MemberPointerAdjustment: {
408f4a2713aSLionel Sambuc llvm::Value *Ptr = EmitScalarExpr(Adjustment.Ptr.RHS);
409f4a2713aSLionel Sambuc Object = CGM.getCXXABI().EmitMemberDataPointerAddress(
410*0a6a1f1dSLionel Sambuc *this, E, Object, Ptr, Adjustment.Ptr.MPT);
411f4a2713aSLionel Sambuc break;
412f4a2713aSLionel Sambuc }
413f4a2713aSLionel Sambuc }
414f4a2713aSLionel Sambuc }
415f4a2713aSLionel Sambuc
416f4a2713aSLionel Sambuc return MakeAddrLValue(Object, M->getType());
417f4a2713aSLionel Sambuc }
418f4a2713aSLionel Sambuc
419f4a2713aSLionel Sambuc RValue
EmitReferenceBindingToExpr(const Expr * E)420f4a2713aSLionel Sambuc CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E) {
421f4a2713aSLionel Sambuc // Emit the expression as an lvalue.
422f4a2713aSLionel Sambuc LValue LV = EmitLValue(E);
423f4a2713aSLionel Sambuc assert(LV.isSimple());
424f4a2713aSLionel Sambuc llvm::Value *Value = LV.getAddress();
425f4a2713aSLionel Sambuc
426*0a6a1f1dSLionel Sambuc if (sanitizePerformTypeCheck() && !E->getType()->isFunctionType()) {
427f4a2713aSLionel Sambuc // C++11 [dcl.ref]p5 (as amended by core issue 453):
428f4a2713aSLionel Sambuc // If a glvalue to which a reference is directly bound designates neither
429f4a2713aSLionel Sambuc // an existing object or function of an appropriate type nor a region of
430f4a2713aSLionel Sambuc // storage of suitable size and alignment to contain an object of the
431f4a2713aSLionel Sambuc // reference's type, the behavior is undefined.
432f4a2713aSLionel Sambuc QualType Ty = E->getType();
433f4a2713aSLionel Sambuc EmitTypeCheck(TCK_ReferenceBinding, E->getExprLoc(), Value, Ty);
434f4a2713aSLionel Sambuc }
435f4a2713aSLionel Sambuc
436f4a2713aSLionel Sambuc return RValue::get(Value);
437f4a2713aSLionel Sambuc }
438f4a2713aSLionel Sambuc
439f4a2713aSLionel Sambuc
440f4a2713aSLionel Sambuc /// getAccessedFieldNo - Given an encoded value and a result number, return the
441f4a2713aSLionel Sambuc /// input field number being accessed.
getAccessedFieldNo(unsigned Idx,const llvm::Constant * Elts)442f4a2713aSLionel Sambuc unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
443f4a2713aSLionel Sambuc const llvm::Constant *Elts) {
444f4a2713aSLionel Sambuc return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx))
445f4a2713aSLionel Sambuc ->getZExtValue();
446f4a2713aSLionel Sambuc }
447f4a2713aSLionel Sambuc
448f4a2713aSLionel Sambuc /// Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h.
emitHash16Bytes(CGBuilderTy & Builder,llvm::Value * Low,llvm::Value * High)449f4a2713aSLionel Sambuc static llvm::Value *emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low,
450f4a2713aSLionel Sambuc llvm::Value *High) {
451f4a2713aSLionel Sambuc llvm::Value *KMul = Builder.getInt64(0x9ddfea08eb382d69ULL);
452f4a2713aSLionel Sambuc llvm::Value *K47 = Builder.getInt64(47);
453f4a2713aSLionel Sambuc llvm::Value *A0 = Builder.CreateMul(Builder.CreateXor(Low, High), KMul);
454f4a2713aSLionel Sambuc llvm::Value *A1 = Builder.CreateXor(Builder.CreateLShr(A0, K47), A0);
455f4a2713aSLionel Sambuc llvm::Value *B0 = Builder.CreateMul(Builder.CreateXor(High, A1), KMul);
456f4a2713aSLionel Sambuc llvm::Value *B1 = Builder.CreateXor(Builder.CreateLShr(B0, K47), B0);
457f4a2713aSLionel Sambuc return Builder.CreateMul(B1, KMul);
458f4a2713aSLionel Sambuc }
459f4a2713aSLionel Sambuc
sanitizePerformTypeCheck() const460*0a6a1f1dSLionel Sambuc bool CodeGenFunction::sanitizePerformTypeCheck() const {
461*0a6a1f1dSLionel Sambuc return SanOpts.has(SanitizerKind::Null) |
462*0a6a1f1dSLionel Sambuc SanOpts.has(SanitizerKind::Alignment) |
463*0a6a1f1dSLionel Sambuc SanOpts.has(SanitizerKind::ObjectSize) |
464*0a6a1f1dSLionel Sambuc SanOpts.has(SanitizerKind::Vptr);
465*0a6a1f1dSLionel Sambuc }
466*0a6a1f1dSLionel Sambuc
EmitTypeCheck(TypeCheckKind TCK,SourceLocation Loc,llvm::Value * Address,QualType Ty,CharUnits Alignment,bool SkipNullCheck)467f4a2713aSLionel Sambuc void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
468*0a6a1f1dSLionel Sambuc llvm::Value *Address, QualType Ty,
469*0a6a1f1dSLionel Sambuc CharUnits Alignment, bool SkipNullCheck) {
470*0a6a1f1dSLionel Sambuc if (!sanitizePerformTypeCheck())
471f4a2713aSLionel Sambuc return;
472f4a2713aSLionel Sambuc
473f4a2713aSLionel Sambuc // Don't check pointers outside the default address space. The null check
474f4a2713aSLionel Sambuc // isn't correct, the object-size check isn't supported by LLVM, and we can't
475f4a2713aSLionel Sambuc // communicate the addresses to the runtime handler for the vptr check.
476f4a2713aSLionel Sambuc if (Address->getType()->getPointerAddressSpace())
477f4a2713aSLionel Sambuc return;
478f4a2713aSLionel Sambuc
479*0a6a1f1dSLionel Sambuc SanitizerScope SanScope(this);
480f4a2713aSLionel Sambuc
481*0a6a1f1dSLionel Sambuc SmallVector<std::pair<llvm::Value *, SanitizerKind>, 3> Checks;
482*0a6a1f1dSLionel Sambuc llvm::BasicBlock *Done = nullptr;
483*0a6a1f1dSLionel Sambuc
484*0a6a1f1dSLionel Sambuc bool AllowNullPointers = TCK == TCK_DowncastPointer || TCK == TCK_Upcast ||
485*0a6a1f1dSLionel Sambuc TCK == TCK_UpcastToVirtualBase;
486*0a6a1f1dSLionel Sambuc if ((SanOpts.has(SanitizerKind::Null) || AllowNullPointers) &&
487*0a6a1f1dSLionel Sambuc !SkipNullCheck) {
488f4a2713aSLionel Sambuc // The glvalue must not be an empty glvalue.
489*0a6a1f1dSLionel Sambuc llvm::Value *IsNonNull = Builder.CreateICmpNE(
490f4a2713aSLionel Sambuc Address, llvm::Constant::getNullValue(Address->getType()));
491f4a2713aSLionel Sambuc
492*0a6a1f1dSLionel Sambuc if (AllowNullPointers) {
493*0a6a1f1dSLionel Sambuc // When performing pointer casts, it's OK if the value is null.
494f4a2713aSLionel Sambuc // Skip the remaining checks in that case.
495f4a2713aSLionel Sambuc Done = createBasicBlock("null");
496f4a2713aSLionel Sambuc llvm::BasicBlock *Rest = createBasicBlock("not.null");
497*0a6a1f1dSLionel Sambuc Builder.CreateCondBr(IsNonNull, Rest, Done);
498f4a2713aSLionel Sambuc EmitBlock(Rest);
499*0a6a1f1dSLionel Sambuc } else {
500*0a6a1f1dSLionel Sambuc Checks.push_back(std::make_pair(IsNonNull, SanitizerKind::Null));
501f4a2713aSLionel Sambuc }
502f4a2713aSLionel Sambuc }
503f4a2713aSLionel Sambuc
504*0a6a1f1dSLionel Sambuc if (SanOpts.has(SanitizerKind::ObjectSize) && !Ty->isIncompleteType()) {
505f4a2713aSLionel Sambuc uint64_t Size = getContext().getTypeSizeInChars(Ty).getQuantity();
506f4a2713aSLionel Sambuc
507f4a2713aSLionel Sambuc // The glvalue must refer to a large enough storage region.
508f4a2713aSLionel Sambuc // FIXME: If Address Sanitizer is enabled, insert dynamic instrumentation
509f4a2713aSLionel Sambuc // to check this.
510f4a2713aSLionel Sambuc // FIXME: Get object address space
511f4a2713aSLionel Sambuc llvm::Type *Tys[2] = { IntPtrTy, Int8PtrTy };
512f4a2713aSLionel Sambuc llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, Tys);
513f4a2713aSLionel Sambuc llvm::Value *Min = Builder.getFalse();
514f4a2713aSLionel Sambuc llvm::Value *CastAddr = Builder.CreateBitCast(Address, Int8PtrTy);
515f4a2713aSLionel Sambuc llvm::Value *LargeEnough =
516f4a2713aSLionel Sambuc Builder.CreateICmpUGE(Builder.CreateCall2(F, CastAddr, Min),
517f4a2713aSLionel Sambuc llvm::ConstantInt::get(IntPtrTy, Size));
518*0a6a1f1dSLionel Sambuc Checks.push_back(std::make_pair(LargeEnough, SanitizerKind::ObjectSize));
519f4a2713aSLionel Sambuc }
520f4a2713aSLionel Sambuc
521f4a2713aSLionel Sambuc uint64_t AlignVal = 0;
522f4a2713aSLionel Sambuc
523*0a6a1f1dSLionel Sambuc if (SanOpts.has(SanitizerKind::Alignment)) {
524f4a2713aSLionel Sambuc AlignVal = Alignment.getQuantity();
525f4a2713aSLionel Sambuc if (!Ty->isIncompleteType() && !AlignVal)
526f4a2713aSLionel Sambuc AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity();
527f4a2713aSLionel Sambuc
528f4a2713aSLionel Sambuc // The glvalue must be suitably aligned.
529f4a2713aSLionel Sambuc if (AlignVal) {
530f4a2713aSLionel Sambuc llvm::Value *Align =
531f4a2713aSLionel Sambuc Builder.CreateAnd(Builder.CreatePtrToInt(Address, IntPtrTy),
532f4a2713aSLionel Sambuc llvm::ConstantInt::get(IntPtrTy, AlignVal - 1));
533f4a2713aSLionel Sambuc llvm::Value *Aligned =
534f4a2713aSLionel Sambuc Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
535*0a6a1f1dSLionel Sambuc Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment));
536f4a2713aSLionel Sambuc }
537f4a2713aSLionel Sambuc }
538f4a2713aSLionel Sambuc
539*0a6a1f1dSLionel Sambuc if (Checks.size() > 0) {
540f4a2713aSLionel Sambuc llvm::Constant *StaticData[] = {
541f4a2713aSLionel Sambuc EmitCheckSourceLocation(Loc),
542f4a2713aSLionel Sambuc EmitCheckTypeDescriptor(Ty),
543f4a2713aSLionel Sambuc llvm::ConstantInt::get(SizeTy, AlignVal),
544f4a2713aSLionel Sambuc llvm::ConstantInt::get(Int8Ty, TCK)
545f4a2713aSLionel Sambuc };
546*0a6a1f1dSLionel Sambuc EmitCheck(Checks, "type_mismatch", StaticData, Address);
547f4a2713aSLionel Sambuc }
548f4a2713aSLionel Sambuc
549f4a2713aSLionel Sambuc // If possible, check that the vptr indicates that there is a subobject of
550f4a2713aSLionel Sambuc // type Ty at offset zero within this object.
551f4a2713aSLionel Sambuc //
552f4a2713aSLionel Sambuc // C++11 [basic.life]p5,6:
553f4a2713aSLionel Sambuc // [For storage which does not refer to an object within its lifetime]
554f4a2713aSLionel Sambuc // The program has undefined behavior if:
555f4a2713aSLionel Sambuc // -- the [pointer or glvalue] is used to access a non-static data member
556f4a2713aSLionel Sambuc // or call a non-static member function
557f4a2713aSLionel Sambuc CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
558*0a6a1f1dSLionel Sambuc if (SanOpts.has(SanitizerKind::Vptr) &&
559f4a2713aSLionel Sambuc (TCK == TCK_MemberAccess || TCK == TCK_MemberCall ||
560*0a6a1f1dSLionel Sambuc TCK == TCK_DowncastPointer || TCK == TCK_DowncastReference ||
561*0a6a1f1dSLionel Sambuc TCK == TCK_UpcastToVirtualBase) &&
562f4a2713aSLionel Sambuc RD && RD->hasDefinition() && RD->isDynamicClass()) {
563f4a2713aSLionel Sambuc // Compute a hash of the mangled name of the type.
564f4a2713aSLionel Sambuc //
565f4a2713aSLionel Sambuc // FIXME: This is not guaranteed to be deterministic! Move to a
566f4a2713aSLionel Sambuc // fingerprinting mechanism once LLVM provides one. For the time
567f4a2713aSLionel Sambuc // being the implementation happens to be deterministic.
568f4a2713aSLionel Sambuc SmallString<64> MangledName;
569f4a2713aSLionel Sambuc llvm::raw_svector_ostream Out(MangledName);
570f4a2713aSLionel Sambuc CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty.getUnqualifiedType(),
571f4a2713aSLionel Sambuc Out);
572*0a6a1f1dSLionel Sambuc
573*0a6a1f1dSLionel Sambuc // Blacklist based on the mangled type.
574*0a6a1f1dSLionel Sambuc if (!CGM.getContext().getSanitizerBlacklist().isBlacklistedType(
575*0a6a1f1dSLionel Sambuc Out.str())) {
576f4a2713aSLionel Sambuc llvm::hash_code TypeHash = hash_value(Out.str());
577f4a2713aSLionel Sambuc
578f4a2713aSLionel Sambuc // Load the vptr, and compute hash_16_bytes(TypeHash, vptr).
579f4a2713aSLionel Sambuc llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash);
580f4a2713aSLionel Sambuc llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0);
581f4a2713aSLionel Sambuc llvm::Value *VPtrAddr = Builder.CreateBitCast(Address, VPtrTy);
582f4a2713aSLionel Sambuc llvm::Value *VPtrVal = Builder.CreateLoad(VPtrAddr);
583f4a2713aSLionel Sambuc llvm::Value *High = Builder.CreateZExt(VPtrVal, Int64Ty);
584f4a2713aSLionel Sambuc
585f4a2713aSLionel Sambuc llvm::Value *Hash = emitHash16Bytes(Builder, Low, High);
586f4a2713aSLionel Sambuc Hash = Builder.CreateTrunc(Hash, IntPtrTy);
587f4a2713aSLionel Sambuc
588f4a2713aSLionel Sambuc // Look the hash up in our cache.
589f4a2713aSLionel Sambuc const int CacheSize = 128;
590f4a2713aSLionel Sambuc llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize);
591f4a2713aSLionel Sambuc llvm::Value *Cache = CGM.CreateRuntimeVariable(HashTable,
592f4a2713aSLionel Sambuc "__ubsan_vptr_type_cache");
593f4a2713aSLionel Sambuc llvm::Value *Slot = Builder.CreateAnd(Hash,
594f4a2713aSLionel Sambuc llvm::ConstantInt::get(IntPtrTy,
595f4a2713aSLionel Sambuc CacheSize-1));
596f4a2713aSLionel Sambuc llvm::Value *Indices[] = { Builder.getInt32(0), Slot };
597f4a2713aSLionel Sambuc llvm::Value *CacheVal =
598f4a2713aSLionel Sambuc Builder.CreateLoad(Builder.CreateInBoundsGEP(Cache, Indices));
599f4a2713aSLionel Sambuc
600f4a2713aSLionel Sambuc // If the hash isn't in the cache, call a runtime handler to perform the
601f4a2713aSLionel Sambuc // hard work of checking whether the vptr is for an object of the right
602f4a2713aSLionel Sambuc // type. This will either fill in the cache and return, or produce a
603f4a2713aSLionel Sambuc // diagnostic.
604*0a6a1f1dSLionel Sambuc llvm::Value *EqualHash = Builder.CreateICmpEQ(CacheVal, Hash);
605f4a2713aSLionel Sambuc llvm::Constant *StaticData[] = {
606f4a2713aSLionel Sambuc EmitCheckSourceLocation(Loc),
607f4a2713aSLionel Sambuc EmitCheckTypeDescriptor(Ty),
608f4a2713aSLionel Sambuc CGM.GetAddrOfRTTIDescriptor(Ty.getUnqualifiedType()),
609f4a2713aSLionel Sambuc llvm::ConstantInt::get(Int8Ty, TCK)
610f4a2713aSLionel Sambuc };
611f4a2713aSLionel Sambuc llvm::Value *DynamicData[] = { Address, Hash };
612*0a6a1f1dSLionel Sambuc EmitCheck(std::make_pair(EqualHash, SanitizerKind::Vptr),
613*0a6a1f1dSLionel Sambuc "dynamic_type_cache_miss", StaticData, DynamicData);
614*0a6a1f1dSLionel Sambuc }
615f4a2713aSLionel Sambuc }
616f4a2713aSLionel Sambuc
617f4a2713aSLionel Sambuc if (Done) {
618f4a2713aSLionel Sambuc Builder.CreateBr(Done);
619f4a2713aSLionel Sambuc EmitBlock(Done);
620f4a2713aSLionel Sambuc }
621f4a2713aSLionel Sambuc }
622f4a2713aSLionel Sambuc
623f4a2713aSLionel Sambuc /// Determine whether this expression refers to a flexible array member in a
624f4a2713aSLionel Sambuc /// struct. We disable array bounds checks for such members.
isFlexibleArrayMemberExpr(const Expr * E)625f4a2713aSLionel Sambuc static bool isFlexibleArrayMemberExpr(const Expr *E) {
626f4a2713aSLionel Sambuc // For compatibility with existing code, we treat arrays of length 0 or
627f4a2713aSLionel Sambuc // 1 as flexible array members.
628f4a2713aSLionel Sambuc const ArrayType *AT = E->getType()->castAsArrayTypeUnsafe();
629*0a6a1f1dSLionel Sambuc if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) {
630f4a2713aSLionel Sambuc if (CAT->getSize().ugt(1))
631f4a2713aSLionel Sambuc return false;
632f4a2713aSLionel Sambuc } else if (!isa<IncompleteArrayType>(AT))
633f4a2713aSLionel Sambuc return false;
634f4a2713aSLionel Sambuc
635f4a2713aSLionel Sambuc E = E->IgnoreParens();
636f4a2713aSLionel Sambuc
637f4a2713aSLionel Sambuc // A flexible array member must be the last member in the class.
638*0a6a1f1dSLionel Sambuc if (const auto *ME = dyn_cast<MemberExpr>(E)) {
639f4a2713aSLionel Sambuc // FIXME: If the base type of the member expr is not FD->getParent(),
640f4a2713aSLionel Sambuc // this should not be treated as a flexible array member access.
641*0a6a1f1dSLionel Sambuc if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
642f4a2713aSLionel Sambuc RecordDecl::field_iterator FI(
643f4a2713aSLionel Sambuc DeclContext::decl_iterator(const_cast<FieldDecl *>(FD)));
644f4a2713aSLionel Sambuc return ++FI == FD->getParent()->field_end();
645f4a2713aSLionel Sambuc }
646f4a2713aSLionel Sambuc }
647f4a2713aSLionel Sambuc
648f4a2713aSLionel Sambuc return false;
649f4a2713aSLionel Sambuc }
650f4a2713aSLionel Sambuc
651f4a2713aSLionel Sambuc /// If Base is known to point to the start of an array, return the length of
652f4a2713aSLionel Sambuc /// that array. Return 0 if the length cannot be determined.
getArrayIndexingBound(CodeGenFunction & CGF,const Expr * Base,QualType & IndexedType)653f4a2713aSLionel Sambuc static llvm::Value *getArrayIndexingBound(
654f4a2713aSLionel Sambuc CodeGenFunction &CGF, const Expr *Base, QualType &IndexedType) {
655f4a2713aSLionel Sambuc // For the vector indexing extension, the bound is the number of elements.
656f4a2713aSLionel Sambuc if (const VectorType *VT = Base->getType()->getAs<VectorType>()) {
657f4a2713aSLionel Sambuc IndexedType = Base->getType();
658f4a2713aSLionel Sambuc return CGF.Builder.getInt32(VT->getNumElements());
659f4a2713aSLionel Sambuc }
660f4a2713aSLionel Sambuc
661f4a2713aSLionel Sambuc Base = Base->IgnoreParens();
662f4a2713aSLionel Sambuc
663*0a6a1f1dSLionel Sambuc if (const auto *CE = dyn_cast<CastExpr>(Base)) {
664f4a2713aSLionel Sambuc if (CE->getCastKind() == CK_ArrayToPointerDecay &&
665f4a2713aSLionel Sambuc !isFlexibleArrayMemberExpr(CE->getSubExpr())) {
666f4a2713aSLionel Sambuc IndexedType = CE->getSubExpr()->getType();
667f4a2713aSLionel Sambuc const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe();
668*0a6a1f1dSLionel Sambuc if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
669f4a2713aSLionel Sambuc return CGF.Builder.getInt(CAT->getSize());
670*0a6a1f1dSLionel Sambuc else if (const auto *VAT = dyn_cast<VariableArrayType>(AT))
671f4a2713aSLionel Sambuc return CGF.getVLASize(VAT).first;
672f4a2713aSLionel Sambuc }
673f4a2713aSLionel Sambuc }
674f4a2713aSLionel Sambuc
675*0a6a1f1dSLionel Sambuc return nullptr;
676f4a2713aSLionel Sambuc }
677f4a2713aSLionel Sambuc
EmitBoundsCheck(const Expr * E,const Expr * Base,llvm::Value * Index,QualType IndexType,bool Accessed)678f4a2713aSLionel Sambuc void CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base,
679f4a2713aSLionel Sambuc llvm::Value *Index, QualType IndexType,
680f4a2713aSLionel Sambuc bool Accessed) {
681*0a6a1f1dSLionel Sambuc assert(SanOpts.has(SanitizerKind::ArrayBounds) &&
682f4a2713aSLionel Sambuc "should not be called unless adding bounds checks");
683*0a6a1f1dSLionel Sambuc SanitizerScope SanScope(this);
684f4a2713aSLionel Sambuc
685f4a2713aSLionel Sambuc QualType IndexedType;
686f4a2713aSLionel Sambuc llvm::Value *Bound = getArrayIndexingBound(*this, Base, IndexedType);
687f4a2713aSLionel Sambuc if (!Bound)
688f4a2713aSLionel Sambuc return;
689f4a2713aSLionel Sambuc
690f4a2713aSLionel Sambuc bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType();
691f4a2713aSLionel Sambuc llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned);
692f4a2713aSLionel Sambuc llvm::Value *BoundVal = Builder.CreateIntCast(Bound, SizeTy, false);
693f4a2713aSLionel Sambuc
694f4a2713aSLionel Sambuc llvm::Constant *StaticData[] = {
695f4a2713aSLionel Sambuc EmitCheckSourceLocation(E->getExprLoc()),
696f4a2713aSLionel Sambuc EmitCheckTypeDescriptor(IndexedType),
697f4a2713aSLionel Sambuc EmitCheckTypeDescriptor(IndexType)
698f4a2713aSLionel Sambuc };
699f4a2713aSLionel Sambuc llvm::Value *Check = Accessed ? Builder.CreateICmpULT(IndexVal, BoundVal)
700f4a2713aSLionel Sambuc : Builder.CreateICmpULE(IndexVal, BoundVal);
701*0a6a1f1dSLionel Sambuc EmitCheck(std::make_pair(Check, SanitizerKind::ArrayBounds), "out_of_bounds",
702*0a6a1f1dSLionel Sambuc StaticData, Index);
703f4a2713aSLionel Sambuc }
704f4a2713aSLionel Sambuc
705f4a2713aSLionel Sambuc
706f4a2713aSLionel Sambuc CodeGenFunction::ComplexPairTy CodeGenFunction::
EmitComplexPrePostIncDec(const UnaryOperator * E,LValue LV,bool isInc,bool isPre)707f4a2713aSLionel Sambuc EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
708f4a2713aSLionel Sambuc bool isInc, bool isPre) {
709f4a2713aSLionel Sambuc ComplexPairTy InVal = EmitLoadOfComplex(LV, E->getExprLoc());
710f4a2713aSLionel Sambuc
711f4a2713aSLionel Sambuc llvm::Value *NextVal;
712f4a2713aSLionel Sambuc if (isa<llvm::IntegerType>(InVal.first->getType())) {
713f4a2713aSLionel Sambuc uint64_t AmountVal = isInc ? 1 : -1;
714f4a2713aSLionel Sambuc NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
715f4a2713aSLionel Sambuc
716f4a2713aSLionel Sambuc // Add the inc/dec to the real part.
717f4a2713aSLionel Sambuc NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
718f4a2713aSLionel Sambuc } else {
719f4a2713aSLionel Sambuc QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
720f4a2713aSLionel Sambuc llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
721f4a2713aSLionel Sambuc if (!isInc)
722f4a2713aSLionel Sambuc FVal.changeSign();
723f4a2713aSLionel Sambuc NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
724f4a2713aSLionel Sambuc
725f4a2713aSLionel Sambuc // Add the inc/dec to the real part.
726f4a2713aSLionel Sambuc NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
727f4a2713aSLionel Sambuc }
728f4a2713aSLionel Sambuc
729f4a2713aSLionel Sambuc ComplexPairTy IncVal(NextVal, InVal.second);
730f4a2713aSLionel Sambuc
731f4a2713aSLionel Sambuc // Store the updated result through the lvalue.
732f4a2713aSLionel Sambuc EmitStoreOfComplex(IncVal, LV, /*init*/ false);
733f4a2713aSLionel Sambuc
734f4a2713aSLionel Sambuc // If this is a postinc, return the value read from memory, otherwise use the
735f4a2713aSLionel Sambuc // updated value.
736f4a2713aSLionel Sambuc return isPre ? IncVal : InVal;
737f4a2713aSLionel Sambuc }
738f4a2713aSLionel Sambuc
739f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
740f4a2713aSLionel Sambuc // LValue Expression Emission
741f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
742f4a2713aSLionel Sambuc
GetUndefRValue(QualType Ty)743f4a2713aSLionel Sambuc RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
744f4a2713aSLionel Sambuc if (Ty->isVoidType())
745*0a6a1f1dSLionel Sambuc return RValue::get(nullptr);
746f4a2713aSLionel Sambuc
747f4a2713aSLionel Sambuc switch (getEvaluationKind(Ty)) {
748f4a2713aSLionel Sambuc case TEK_Complex: {
749f4a2713aSLionel Sambuc llvm::Type *EltTy =
750f4a2713aSLionel Sambuc ConvertType(Ty->castAs<ComplexType>()->getElementType());
751f4a2713aSLionel Sambuc llvm::Value *U = llvm::UndefValue::get(EltTy);
752f4a2713aSLionel Sambuc return RValue::getComplex(std::make_pair(U, U));
753f4a2713aSLionel Sambuc }
754f4a2713aSLionel Sambuc
755f4a2713aSLionel Sambuc // If this is a use of an undefined aggregate type, the aggregate must have an
756f4a2713aSLionel Sambuc // identifiable address. Just because the contents of the value are undefined
757f4a2713aSLionel Sambuc // doesn't mean that the address can't be taken and compared.
758f4a2713aSLionel Sambuc case TEK_Aggregate: {
759f4a2713aSLionel Sambuc llvm::Value *DestPtr = CreateMemTemp(Ty, "undef.agg.tmp");
760f4a2713aSLionel Sambuc return RValue::getAggregate(DestPtr);
761f4a2713aSLionel Sambuc }
762f4a2713aSLionel Sambuc
763f4a2713aSLionel Sambuc case TEK_Scalar:
764f4a2713aSLionel Sambuc return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
765f4a2713aSLionel Sambuc }
766f4a2713aSLionel Sambuc llvm_unreachable("bad evaluation kind");
767f4a2713aSLionel Sambuc }
768f4a2713aSLionel Sambuc
EmitUnsupportedRValue(const Expr * E,const char * Name)769f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
770f4a2713aSLionel Sambuc const char *Name) {
771f4a2713aSLionel Sambuc ErrorUnsupported(E, Name);
772f4a2713aSLionel Sambuc return GetUndefRValue(E->getType());
773f4a2713aSLionel Sambuc }
774f4a2713aSLionel Sambuc
EmitUnsupportedLValue(const Expr * E,const char * Name)775f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
776f4a2713aSLionel Sambuc const char *Name) {
777f4a2713aSLionel Sambuc ErrorUnsupported(E, Name);
778f4a2713aSLionel Sambuc llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
779f4a2713aSLionel Sambuc return MakeAddrLValue(llvm::UndefValue::get(Ty), E->getType());
780f4a2713aSLionel Sambuc }
781f4a2713aSLionel Sambuc
EmitCheckedLValue(const Expr * E,TypeCheckKind TCK)782f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitCheckedLValue(const Expr *E, TypeCheckKind TCK) {
783f4a2713aSLionel Sambuc LValue LV;
784*0a6a1f1dSLionel Sambuc if (SanOpts.has(SanitizerKind::ArrayBounds) && isa<ArraySubscriptExpr>(E))
785f4a2713aSLionel Sambuc LV = EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E), /*Accessed*/true);
786f4a2713aSLionel Sambuc else
787f4a2713aSLionel Sambuc LV = EmitLValue(E);
788f4a2713aSLionel Sambuc if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
789f4a2713aSLionel Sambuc EmitTypeCheck(TCK, E->getExprLoc(), LV.getAddress(),
790f4a2713aSLionel Sambuc E->getType(), LV.getAlignment());
791f4a2713aSLionel Sambuc return LV;
792f4a2713aSLionel Sambuc }
793f4a2713aSLionel Sambuc
794f4a2713aSLionel Sambuc /// EmitLValue - Emit code to compute a designator that specifies the location
795f4a2713aSLionel Sambuc /// of the expression.
796f4a2713aSLionel Sambuc ///
797f4a2713aSLionel Sambuc /// This can return one of two things: a simple address or a bitfield reference.
798f4a2713aSLionel Sambuc /// In either case, the LLVM Value* in the LValue structure is guaranteed to be
799f4a2713aSLionel Sambuc /// an LLVM pointer type.
800f4a2713aSLionel Sambuc ///
801f4a2713aSLionel Sambuc /// If this returns a bitfield reference, nothing about the pointee type of the
802f4a2713aSLionel Sambuc /// LLVM value is known: For example, it may not be a pointer to an integer.
803f4a2713aSLionel Sambuc ///
804f4a2713aSLionel Sambuc /// If this returns a normal address, and if the lvalue's C type is fixed size,
805f4a2713aSLionel Sambuc /// this method guarantees that the returned pointer type will point to an LLVM
806f4a2713aSLionel Sambuc /// type of the same size of the lvalue's type. If the lvalue has a variable
807f4a2713aSLionel Sambuc /// length type, this is not possible.
808f4a2713aSLionel Sambuc ///
EmitLValue(const Expr * E)809f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitLValue(const Expr *E) {
810f4a2713aSLionel Sambuc switch (E->getStmtClass()) {
811f4a2713aSLionel Sambuc default: return EmitUnsupportedLValue(E, "l-value expression");
812f4a2713aSLionel Sambuc
813f4a2713aSLionel Sambuc case Expr::ObjCPropertyRefExprClass:
814f4a2713aSLionel Sambuc llvm_unreachable("cannot emit a property reference directly");
815f4a2713aSLionel Sambuc
816f4a2713aSLionel Sambuc case Expr::ObjCSelectorExprClass:
817f4a2713aSLionel Sambuc return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
818f4a2713aSLionel Sambuc case Expr::ObjCIsaExprClass:
819f4a2713aSLionel Sambuc return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
820f4a2713aSLionel Sambuc case Expr::BinaryOperatorClass:
821f4a2713aSLionel Sambuc return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
822f4a2713aSLionel Sambuc case Expr::CompoundAssignOperatorClass:
823f4a2713aSLionel Sambuc if (!E->getType()->isAnyComplexType())
824f4a2713aSLionel Sambuc return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
825f4a2713aSLionel Sambuc return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
826f4a2713aSLionel Sambuc case Expr::CallExprClass:
827f4a2713aSLionel Sambuc case Expr::CXXMemberCallExprClass:
828f4a2713aSLionel Sambuc case Expr::CXXOperatorCallExprClass:
829f4a2713aSLionel Sambuc case Expr::UserDefinedLiteralClass:
830f4a2713aSLionel Sambuc return EmitCallExprLValue(cast<CallExpr>(E));
831f4a2713aSLionel Sambuc case Expr::VAArgExprClass:
832f4a2713aSLionel Sambuc return EmitVAArgExprLValue(cast<VAArgExpr>(E));
833f4a2713aSLionel Sambuc case Expr::DeclRefExprClass:
834f4a2713aSLionel Sambuc return EmitDeclRefLValue(cast<DeclRefExpr>(E));
835f4a2713aSLionel Sambuc case Expr::ParenExprClass:
836f4a2713aSLionel Sambuc return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
837f4a2713aSLionel Sambuc case Expr::GenericSelectionExprClass:
838f4a2713aSLionel Sambuc return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr());
839f4a2713aSLionel Sambuc case Expr::PredefinedExprClass:
840f4a2713aSLionel Sambuc return EmitPredefinedLValue(cast<PredefinedExpr>(E));
841f4a2713aSLionel Sambuc case Expr::StringLiteralClass:
842f4a2713aSLionel Sambuc return EmitStringLiteralLValue(cast<StringLiteral>(E));
843f4a2713aSLionel Sambuc case Expr::ObjCEncodeExprClass:
844f4a2713aSLionel Sambuc return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
845f4a2713aSLionel Sambuc case Expr::PseudoObjectExprClass:
846f4a2713aSLionel Sambuc return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E));
847f4a2713aSLionel Sambuc case Expr::InitListExprClass:
848f4a2713aSLionel Sambuc return EmitInitListLValue(cast<InitListExpr>(E));
849f4a2713aSLionel Sambuc case Expr::CXXTemporaryObjectExprClass:
850f4a2713aSLionel Sambuc case Expr::CXXConstructExprClass:
851f4a2713aSLionel Sambuc return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
852f4a2713aSLionel Sambuc case Expr::CXXBindTemporaryExprClass:
853f4a2713aSLionel Sambuc return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
854f4a2713aSLionel Sambuc case Expr::CXXUuidofExprClass:
855f4a2713aSLionel Sambuc return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E));
856f4a2713aSLionel Sambuc case Expr::LambdaExprClass:
857f4a2713aSLionel Sambuc return EmitLambdaLValue(cast<LambdaExpr>(E));
858f4a2713aSLionel Sambuc
859f4a2713aSLionel Sambuc case Expr::ExprWithCleanupsClass: {
860*0a6a1f1dSLionel Sambuc const auto *cleanups = cast<ExprWithCleanups>(E);
861f4a2713aSLionel Sambuc enterFullExpression(cleanups);
862f4a2713aSLionel Sambuc RunCleanupsScope Scope(*this);
863f4a2713aSLionel Sambuc return EmitLValue(cleanups->getSubExpr());
864f4a2713aSLionel Sambuc }
865f4a2713aSLionel Sambuc
866f4a2713aSLionel Sambuc case Expr::CXXDefaultArgExprClass:
867f4a2713aSLionel Sambuc return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
868f4a2713aSLionel Sambuc case Expr::CXXDefaultInitExprClass: {
869f4a2713aSLionel Sambuc CXXDefaultInitExprScope Scope(*this);
870f4a2713aSLionel Sambuc return EmitLValue(cast<CXXDefaultInitExpr>(E)->getExpr());
871f4a2713aSLionel Sambuc }
872f4a2713aSLionel Sambuc case Expr::CXXTypeidExprClass:
873f4a2713aSLionel Sambuc return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
874f4a2713aSLionel Sambuc
875f4a2713aSLionel Sambuc case Expr::ObjCMessageExprClass:
876f4a2713aSLionel Sambuc return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
877f4a2713aSLionel Sambuc case Expr::ObjCIvarRefExprClass:
878f4a2713aSLionel Sambuc return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
879f4a2713aSLionel Sambuc case Expr::StmtExprClass:
880f4a2713aSLionel Sambuc return EmitStmtExprLValue(cast<StmtExpr>(E));
881f4a2713aSLionel Sambuc case Expr::UnaryOperatorClass:
882f4a2713aSLionel Sambuc return EmitUnaryOpLValue(cast<UnaryOperator>(E));
883f4a2713aSLionel Sambuc case Expr::ArraySubscriptExprClass:
884f4a2713aSLionel Sambuc return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
885f4a2713aSLionel Sambuc case Expr::ExtVectorElementExprClass:
886f4a2713aSLionel Sambuc return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
887f4a2713aSLionel Sambuc case Expr::MemberExprClass:
888f4a2713aSLionel Sambuc return EmitMemberExpr(cast<MemberExpr>(E));
889f4a2713aSLionel Sambuc case Expr::CompoundLiteralExprClass:
890f4a2713aSLionel Sambuc return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
891f4a2713aSLionel Sambuc case Expr::ConditionalOperatorClass:
892f4a2713aSLionel Sambuc return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
893f4a2713aSLionel Sambuc case Expr::BinaryConditionalOperatorClass:
894f4a2713aSLionel Sambuc return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E));
895f4a2713aSLionel Sambuc case Expr::ChooseExprClass:
896f4a2713aSLionel Sambuc return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr());
897f4a2713aSLionel Sambuc case Expr::OpaqueValueExprClass:
898f4a2713aSLionel Sambuc return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E));
899f4a2713aSLionel Sambuc case Expr::SubstNonTypeTemplateParmExprClass:
900f4a2713aSLionel Sambuc return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
901f4a2713aSLionel Sambuc case Expr::ImplicitCastExprClass:
902f4a2713aSLionel Sambuc case Expr::CStyleCastExprClass:
903f4a2713aSLionel Sambuc case Expr::CXXFunctionalCastExprClass:
904f4a2713aSLionel Sambuc case Expr::CXXStaticCastExprClass:
905f4a2713aSLionel Sambuc case Expr::CXXDynamicCastExprClass:
906f4a2713aSLionel Sambuc case Expr::CXXReinterpretCastExprClass:
907f4a2713aSLionel Sambuc case Expr::CXXConstCastExprClass:
908f4a2713aSLionel Sambuc case Expr::ObjCBridgedCastExprClass:
909f4a2713aSLionel Sambuc return EmitCastLValue(cast<CastExpr>(E));
910f4a2713aSLionel Sambuc
911f4a2713aSLionel Sambuc case Expr::MaterializeTemporaryExprClass:
912f4a2713aSLionel Sambuc return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E));
913f4a2713aSLionel Sambuc }
914f4a2713aSLionel Sambuc }
915f4a2713aSLionel Sambuc
916f4a2713aSLionel Sambuc /// Given an object of the given canonical type, can we safely copy a
917f4a2713aSLionel Sambuc /// value out of it based on its initializer?
isConstantEmittableObjectType(QualType type)918f4a2713aSLionel Sambuc static bool isConstantEmittableObjectType(QualType type) {
919f4a2713aSLionel Sambuc assert(type.isCanonical());
920f4a2713aSLionel Sambuc assert(!type->isReferenceType());
921f4a2713aSLionel Sambuc
922f4a2713aSLionel Sambuc // Must be const-qualified but non-volatile.
923f4a2713aSLionel Sambuc Qualifiers qs = type.getLocalQualifiers();
924f4a2713aSLionel Sambuc if (!qs.hasConst() || qs.hasVolatile()) return false;
925f4a2713aSLionel Sambuc
926f4a2713aSLionel Sambuc // Otherwise, all object types satisfy this except C++ classes with
927f4a2713aSLionel Sambuc // mutable subobjects or non-trivial copy/destroy behavior.
928*0a6a1f1dSLionel Sambuc if (const auto *RT = dyn_cast<RecordType>(type))
929*0a6a1f1dSLionel Sambuc if (const auto *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
930f4a2713aSLionel Sambuc if (RD->hasMutableFields() || !RD->isTrivial())
931f4a2713aSLionel Sambuc return false;
932f4a2713aSLionel Sambuc
933f4a2713aSLionel Sambuc return true;
934f4a2713aSLionel Sambuc }
935f4a2713aSLionel Sambuc
936f4a2713aSLionel Sambuc /// Can we constant-emit a load of a reference to a variable of the
937f4a2713aSLionel Sambuc /// given type? This is different from predicates like
938f4a2713aSLionel Sambuc /// Decl::isUsableInConstantExpressions because we do want it to apply
939f4a2713aSLionel Sambuc /// in situations that don't necessarily satisfy the language's rules
940f4a2713aSLionel Sambuc /// for this (e.g. C++'s ODR-use rules). For example, we want to able
941f4a2713aSLionel Sambuc /// to do this with const float variables even if those variables
942f4a2713aSLionel Sambuc /// aren't marked 'constexpr'.
943f4a2713aSLionel Sambuc enum ConstantEmissionKind {
944f4a2713aSLionel Sambuc CEK_None,
945f4a2713aSLionel Sambuc CEK_AsReferenceOnly,
946f4a2713aSLionel Sambuc CEK_AsValueOrReference,
947f4a2713aSLionel Sambuc CEK_AsValueOnly
948f4a2713aSLionel Sambuc };
checkVarTypeForConstantEmission(QualType type)949f4a2713aSLionel Sambuc static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type) {
950f4a2713aSLionel Sambuc type = type.getCanonicalType();
951*0a6a1f1dSLionel Sambuc if (const auto *ref = dyn_cast<ReferenceType>(type)) {
952f4a2713aSLionel Sambuc if (isConstantEmittableObjectType(ref->getPointeeType()))
953f4a2713aSLionel Sambuc return CEK_AsValueOrReference;
954f4a2713aSLionel Sambuc return CEK_AsReferenceOnly;
955f4a2713aSLionel Sambuc }
956f4a2713aSLionel Sambuc if (isConstantEmittableObjectType(type))
957f4a2713aSLionel Sambuc return CEK_AsValueOnly;
958f4a2713aSLionel Sambuc return CEK_None;
959f4a2713aSLionel Sambuc }
960f4a2713aSLionel Sambuc
961f4a2713aSLionel Sambuc /// Try to emit a reference to the given value without producing it as
962f4a2713aSLionel Sambuc /// an l-value. This is actually more than an optimization: we can't
963f4a2713aSLionel Sambuc /// produce an l-value for variables that we never actually captured
964f4a2713aSLionel Sambuc /// in a block or lambda, which means const int variables or constexpr
965f4a2713aSLionel Sambuc /// literals or similar.
966f4a2713aSLionel Sambuc CodeGenFunction::ConstantEmission
tryEmitAsConstant(DeclRefExpr * refExpr)967f4a2713aSLionel Sambuc CodeGenFunction::tryEmitAsConstant(DeclRefExpr *refExpr) {
968f4a2713aSLionel Sambuc ValueDecl *value = refExpr->getDecl();
969f4a2713aSLionel Sambuc
970f4a2713aSLionel Sambuc // The value needs to be an enum constant or a constant variable.
971f4a2713aSLionel Sambuc ConstantEmissionKind CEK;
972f4a2713aSLionel Sambuc if (isa<ParmVarDecl>(value)) {
973f4a2713aSLionel Sambuc CEK = CEK_None;
974*0a6a1f1dSLionel Sambuc } else if (auto *var = dyn_cast<VarDecl>(value)) {
975f4a2713aSLionel Sambuc CEK = checkVarTypeForConstantEmission(var->getType());
976f4a2713aSLionel Sambuc } else if (isa<EnumConstantDecl>(value)) {
977f4a2713aSLionel Sambuc CEK = CEK_AsValueOnly;
978f4a2713aSLionel Sambuc } else {
979f4a2713aSLionel Sambuc CEK = CEK_None;
980f4a2713aSLionel Sambuc }
981f4a2713aSLionel Sambuc if (CEK == CEK_None) return ConstantEmission();
982f4a2713aSLionel Sambuc
983f4a2713aSLionel Sambuc Expr::EvalResult result;
984f4a2713aSLionel Sambuc bool resultIsReference;
985f4a2713aSLionel Sambuc QualType resultType;
986f4a2713aSLionel Sambuc
987f4a2713aSLionel Sambuc // It's best to evaluate all the way as an r-value if that's permitted.
988f4a2713aSLionel Sambuc if (CEK != CEK_AsReferenceOnly &&
989f4a2713aSLionel Sambuc refExpr->EvaluateAsRValue(result, getContext())) {
990f4a2713aSLionel Sambuc resultIsReference = false;
991f4a2713aSLionel Sambuc resultType = refExpr->getType();
992f4a2713aSLionel Sambuc
993f4a2713aSLionel Sambuc // Otherwise, try to evaluate as an l-value.
994f4a2713aSLionel Sambuc } else if (CEK != CEK_AsValueOnly &&
995f4a2713aSLionel Sambuc refExpr->EvaluateAsLValue(result, getContext())) {
996f4a2713aSLionel Sambuc resultIsReference = true;
997f4a2713aSLionel Sambuc resultType = value->getType();
998f4a2713aSLionel Sambuc
999f4a2713aSLionel Sambuc // Failure.
1000f4a2713aSLionel Sambuc } else {
1001f4a2713aSLionel Sambuc return ConstantEmission();
1002f4a2713aSLionel Sambuc }
1003f4a2713aSLionel Sambuc
1004f4a2713aSLionel Sambuc // In any case, if the initializer has side-effects, abandon ship.
1005f4a2713aSLionel Sambuc if (result.HasSideEffects)
1006f4a2713aSLionel Sambuc return ConstantEmission();
1007f4a2713aSLionel Sambuc
1008f4a2713aSLionel Sambuc // Emit as a constant.
1009f4a2713aSLionel Sambuc llvm::Constant *C = CGM.EmitConstantValue(result.Val, resultType, this);
1010f4a2713aSLionel Sambuc
1011f4a2713aSLionel Sambuc // Make sure we emit a debug reference to the global variable.
1012f4a2713aSLionel Sambuc // This should probably fire even for
1013f4a2713aSLionel Sambuc if (isa<VarDecl>(value)) {
1014f4a2713aSLionel Sambuc if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value)))
1015f4a2713aSLionel Sambuc EmitDeclRefExprDbgValue(refExpr, C);
1016f4a2713aSLionel Sambuc } else {
1017f4a2713aSLionel Sambuc assert(isa<EnumConstantDecl>(value));
1018f4a2713aSLionel Sambuc EmitDeclRefExprDbgValue(refExpr, C);
1019f4a2713aSLionel Sambuc }
1020f4a2713aSLionel Sambuc
1021f4a2713aSLionel Sambuc // If we emitted a reference constant, we need to dereference that.
1022f4a2713aSLionel Sambuc if (resultIsReference)
1023f4a2713aSLionel Sambuc return ConstantEmission::forReference(C);
1024f4a2713aSLionel Sambuc
1025f4a2713aSLionel Sambuc return ConstantEmission::forValue(C);
1026f4a2713aSLionel Sambuc }
1027f4a2713aSLionel Sambuc
EmitLoadOfScalar(LValue lvalue,SourceLocation Loc)1028f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue,
1029f4a2713aSLionel Sambuc SourceLocation Loc) {
1030f4a2713aSLionel Sambuc return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
1031f4a2713aSLionel Sambuc lvalue.getAlignment().getQuantity(),
1032f4a2713aSLionel Sambuc lvalue.getType(), Loc, lvalue.getTBAAInfo(),
1033f4a2713aSLionel Sambuc lvalue.getTBAABaseType(), lvalue.getTBAAOffset());
1034f4a2713aSLionel Sambuc }
1035f4a2713aSLionel Sambuc
hasBooleanRepresentation(QualType Ty)1036f4a2713aSLionel Sambuc static bool hasBooleanRepresentation(QualType Ty) {
1037f4a2713aSLionel Sambuc if (Ty->isBooleanType())
1038f4a2713aSLionel Sambuc return true;
1039f4a2713aSLionel Sambuc
1040f4a2713aSLionel Sambuc if (const EnumType *ET = Ty->getAs<EnumType>())
1041f4a2713aSLionel Sambuc return ET->getDecl()->getIntegerType()->isBooleanType();
1042f4a2713aSLionel Sambuc
1043f4a2713aSLionel Sambuc if (const AtomicType *AT = Ty->getAs<AtomicType>())
1044f4a2713aSLionel Sambuc return hasBooleanRepresentation(AT->getValueType());
1045f4a2713aSLionel Sambuc
1046f4a2713aSLionel Sambuc return false;
1047f4a2713aSLionel Sambuc }
1048f4a2713aSLionel Sambuc
getRangeForType(CodeGenFunction & CGF,QualType Ty,llvm::APInt & Min,llvm::APInt & End,bool StrictEnums)1049f4a2713aSLionel Sambuc static bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
1050f4a2713aSLionel Sambuc llvm::APInt &Min, llvm::APInt &End,
1051f4a2713aSLionel Sambuc bool StrictEnums) {
1052f4a2713aSLionel Sambuc const EnumType *ET = Ty->getAs<EnumType>();
1053f4a2713aSLionel Sambuc bool IsRegularCPlusPlusEnum = CGF.getLangOpts().CPlusPlus && StrictEnums &&
1054f4a2713aSLionel Sambuc ET && !ET->getDecl()->isFixed();
1055f4a2713aSLionel Sambuc bool IsBool = hasBooleanRepresentation(Ty);
1056f4a2713aSLionel Sambuc if (!IsBool && !IsRegularCPlusPlusEnum)
1057f4a2713aSLionel Sambuc return false;
1058f4a2713aSLionel Sambuc
1059f4a2713aSLionel Sambuc if (IsBool) {
1060f4a2713aSLionel Sambuc Min = llvm::APInt(CGF.getContext().getTypeSize(Ty), 0);
1061f4a2713aSLionel Sambuc End = llvm::APInt(CGF.getContext().getTypeSize(Ty), 2);
1062f4a2713aSLionel Sambuc } else {
1063f4a2713aSLionel Sambuc const EnumDecl *ED = ET->getDecl();
1064f4a2713aSLionel Sambuc llvm::Type *LTy = CGF.ConvertTypeForMem(ED->getIntegerType());
1065f4a2713aSLionel Sambuc unsigned Bitwidth = LTy->getScalarSizeInBits();
1066f4a2713aSLionel Sambuc unsigned NumNegativeBits = ED->getNumNegativeBits();
1067f4a2713aSLionel Sambuc unsigned NumPositiveBits = ED->getNumPositiveBits();
1068f4a2713aSLionel Sambuc
1069f4a2713aSLionel Sambuc if (NumNegativeBits) {
1070f4a2713aSLionel Sambuc unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1);
1071f4a2713aSLionel Sambuc assert(NumBits <= Bitwidth);
1072f4a2713aSLionel Sambuc End = llvm::APInt(Bitwidth, 1) << (NumBits - 1);
1073f4a2713aSLionel Sambuc Min = -End;
1074f4a2713aSLionel Sambuc } else {
1075f4a2713aSLionel Sambuc assert(NumPositiveBits <= Bitwidth);
1076f4a2713aSLionel Sambuc End = llvm::APInt(Bitwidth, 1) << NumPositiveBits;
1077f4a2713aSLionel Sambuc Min = llvm::APInt(Bitwidth, 0);
1078f4a2713aSLionel Sambuc }
1079f4a2713aSLionel Sambuc }
1080f4a2713aSLionel Sambuc return true;
1081f4a2713aSLionel Sambuc }
1082f4a2713aSLionel Sambuc
getRangeForLoadFromType(QualType Ty)1083f4a2713aSLionel Sambuc llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) {
1084f4a2713aSLionel Sambuc llvm::APInt Min, End;
1085f4a2713aSLionel Sambuc if (!getRangeForType(*this, Ty, Min, End,
1086f4a2713aSLionel Sambuc CGM.getCodeGenOpts().StrictEnums))
1087*0a6a1f1dSLionel Sambuc return nullptr;
1088f4a2713aSLionel Sambuc
1089f4a2713aSLionel Sambuc llvm::MDBuilder MDHelper(getLLVMContext());
1090f4a2713aSLionel Sambuc return MDHelper.createRange(Min, End);
1091f4a2713aSLionel Sambuc }
1092f4a2713aSLionel Sambuc
EmitLoadOfScalar(llvm::Value * Addr,bool Volatile,unsigned Alignment,QualType Ty,SourceLocation Loc,llvm::MDNode * TBAAInfo,QualType TBAABaseType,uint64_t TBAAOffset)1093f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
1094f4a2713aSLionel Sambuc unsigned Alignment, QualType Ty,
1095f4a2713aSLionel Sambuc SourceLocation Loc,
1096f4a2713aSLionel Sambuc llvm::MDNode *TBAAInfo,
1097f4a2713aSLionel Sambuc QualType TBAABaseType,
1098f4a2713aSLionel Sambuc uint64_t TBAAOffset) {
1099f4a2713aSLionel Sambuc // For better performance, handle vector loads differently.
1100f4a2713aSLionel Sambuc if (Ty->isVectorType()) {
1101f4a2713aSLionel Sambuc llvm::Value *V;
1102f4a2713aSLionel Sambuc const llvm::Type *EltTy =
1103f4a2713aSLionel Sambuc cast<llvm::PointerType>(Addr->getType())->getElementType();
1104f4a2713aSLionel Sambuc
1105*0a6a1f1dSLionel Sambuc const auto *VTy = cast<llvm::VectorType>(EltTy);
1106f4a2713aSLionel Sambuc
1107f4a2713aSLionel Sambuc // Handle vectors of size 3, like size 4 for better performance.
1108f4a2713aSLionel Sambuc if (VTy->getNumElements() == 3) {
1109f4a2713aSLionel Sambuc
1110f4a2713aSLionel Sambuc // Bitcast to vec4 type.
1111f4a2713aSLionel Sambuc llvm::VectorType *vec4Ty = llvm::VectorType::get(VTy->getElementType(),
1112f4a2713aSLionel Sambuc 4);
1113f4a2713aSLionel Sambuc llvm::PointerType *ptVec4Ty =
1114f4a2713aSLionel Sambuc llvm::PointerType::get(vec4Ty,
1115f4a2713aSLionel Sambuc (cast<llvm::PointerType>(
1116f4a2713aSLionel Sambuc Addr->getType()))->getAddressSpace());
1117f4a2713aSLionel Sambuc llvm::Value *Cast = Builder.CreateBitCast(Addr, ptVec4Ty,
1118f4a2713aSLionel Sambuc "castToVec4");
1119f4a2713aSLionel Sambuc // Now load value.
1120f4a2713aSLionel Sambuc llvm::Value *LoadVal = Builder.CreateLoad(Cast, Volatile, "loadVec4");
1121f4a2713aSLionel Sambuc
1122f4a2713aSLionel Sambuc // Shuffle vector to get vec3.
1123f4a2713aSLionel Sambuc llvm::Constant *Mask[] = {
1124f4a2713aSLionel Sambuc llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 0),
1125f4a2713aSLionel Sambuc llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 1),
1126f4a2713aSLionel Sambuc llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 2)
1127f4a2713aSLionel Sambuc };
1128f4a2713aSLionel Sambuc
1129f4a2713aSLionel Sambuc llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1130f4a2713aSLionel Sambuc V = Builder.CreateShuffleVector(LoadVal,
1131f4a2713aSLionel Sambuc llvm::UndefValue::get(vec4Ty),
1132f4a2713aSLionel Sambuc MaskV, "extractVec");
1133f4a2713aSLionel Sambuc return EmitFromMemory(V, Ty);
1134f4a2713aSLionel Sambuc }
1135f4a2713aSLionel Sambuc }
1136f4a2713aSLionel Sambuc
1137f4a2713aSLionel Sambuc // Atomic operations have to be done on integral types.
1138f4a2713aSLionel Sambuc if (Ty->isAtomicType()) {
1139f4a2713aSLionel Sambuc LValue lvalue = LValue::MakeAddr(Addr, Ty,
1140f4a2713aSLionel Sambuc CharUnits::fromQuantity(Alignment),
1141f4a2713aSLionel Sambuc getContext(), TBAAInfo);
1142f4a2713aSLionel Sambuc return EmitAtomicLoad(lvalue, Loc).getScalarVal();
1143f4a2713aSLionel Sambuc }
1144f4a2713aSLionel Sambuc
1145f4a2713aSLionel Sambuc llvm::LoadInst *Load = Builder.CreateLoad(Addr);
1146f4a2713aSLionel Sambuc if (Volatile)
1147f4a2713aSLionel Sambuc Load->setVolatile(true);
1148f4a2713aSLionel Sambuc if (Alignment)
1149f4a2713aSLionel Sambuc Load->setAlignment(Alignment);
1150f4a2713aSLionel Sambuc if (TBAAInfo) {
1151f4a2713aSLionel Sambuc llvm::MDNode *TBAAPath = CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo,
1152f4a2713aSLionel Sambuc TBAAOffset);
1153f4a2713aSLionel Sambuc if (TBAAPath)
1154f4a2713aSLionel Sambuc CGM.DecorateInstruction(Load, TBAAPath, false/*ConvertTypeToTag*/);
1155f4a2713aSLionel Sambuc }
1156f4a2713aSLionel Sambuc
1157*0a6a1f1dSLionel Sambuc bool NeedsBoolCheck =
1158*0a6a1f1dSLionel Sambuc SanOpts.has(SanitizerKind::Bool) && hasBooleanRepresentation(Ty);
1159*0a6a1f1dSLionel Sambuc bool NeedsEnumCheck =
1160*0a6a1f1dSLionel Sambuc SanOpts.has(SanitizerKind::Enum) && Ty->getAs<EnumType>();
1161*0a6a1f1dSLionel Sambuc if (NeedsBoolCheck || NeedsEnumCheck) {
1162*0a6a1f1dSLionel Sambuc SanitizerScope SanScope(this);
1163f4a2713aSLionel Sambuc llvm::APInt Min, End;
1164f4a2713aSLionel Sambuc if (getRangeForType(*this, Ty, Min, End, true)) {
1165f4a2713aSLionel Sambuc --End;
1166f4a2713aSLionel Sambuc llvm::Value *Check;
1167f4a2713aSLionel Sambuc if (!Min)
1168f4a2713aSLionel Sambuc Check = Builder.CreateICmpULE(
1169f4a2713aSLionel Sambuc Load, llvm::ConstantInt::get(getLLVMContext(), End));
1170f4a2713aSLionel Sambuc else {
1171f4a2713aSLionel Sambuc llvm::Value *Upper = Builder.CreateICmpSLE(
1172f4a2713aSLionel Sambuc Load, llvm::ConstantInt::get(getLLVMContext(), End));
1173f4a2713aSLionel Sambuc llvm::Value *Lower = Builder.CreateICmpSGE(
1174f4a2713aSLionel Sambuc Load, llvm::ConstantInt::get(getLLVMContext(), Min));
1175f4a2713aSLionel Sambuc Check = Builder.CreateAnd(Upper, Lower);
1176f4a2713aSLionel Sambuc }
1177f4a2713aSLionel Sambuc llvm::Constant *StaticArgs[] = {
1178f4a2713aSLionel Sambuc EmitCheckSourceLocation(Loc),
1179f4a2713aSLionel Sambuc EmitCheckTypeDescriptor(Ty)
1180f4a2713aSLionel Sambuc };
1181*0a6a1f1dSLionel Sambuc SanitizerKind Kind = NeedsEnumCheck ? SanitizerKind::Enum : SanitizerKind::Bool;
1182*0a6a1f1dSLionel Sambuc EmitCheck(std::make_pair(Check, Kind), "load_invalid_value", StaticArgs,
1183*0a6a1f1dSLionel Sambuc EmitCheckValue(Load));
1184f4a2713aSLionel Sambuc }
1185f4a2713aSLionel Sambuc } else if (CGM.getCodeGenOpts().OptimizationLevel > 0)
1186f4a2713aSLionel Sambuc if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty))
1187f4a2713aSLionel Sambuc Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo);
1188f4a2713aSLionel Sambuc
1189f4a2713aSLionel Sambuc return EmitFromMemory(Load, Ty);
1190f4a2713aSLionel Sambuc }
1191f4a2713aSLionel Sambuc
EmitToMemory(llvm::Value * Value,QualType Ty)1192f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
1193f4a2713aSLionel Sambuc // Bool has a different representation in memory than in registers.
1194f4a2713aSLionel Sambuc if (hasBooleanRepresentation(Ty)) {
1195f4a2713aSLionel Sambuc // This should really always be an i1, but sometimes it's already
1196f4a2713aSLionel Sambuc // an i8, and it's awkward to track those cases down.
1197f4a2713aSLionel Sambuc if (Value->getType()->isIntegerTy(1))
1198f4a2713aSLionel Sambuc return Builder.CreateZExt(Value, ConvertTypeForMem(Ty), "frombool");
1199f4a2713aSLionel Sambuc assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&
1200f4a2713aSLionel Sambuc "wrong value rep of bool");
1201f4a2713aSLionel Sambuc }
1202f4a2713aSLionel Sambuc
1203f4a2713aSLionel Sambuc return Value;
1204f4a2713aSLionel Sambuc }
1205f4a2713aSLionel Sambuc
EmitFromMemory(llvm::Value * Value,QualType Ty)1206f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
1207f4a2713aSLionel Sambuc // Bool has a different representation in memory than in registers.
1208f4a2713aSLionel Sambuc if (hasBooleanRepresentation(Ty)) {
1209f4a2713aSLionel Sambuc assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&
1210f4a2713aSLionel Sambuc "wrong value rep of bool");
1211f4a2713aSLionel Sambuc return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool");
1212f4a2713aSLionel Sambuc }
1213f4a2713aSLionel Sambuc
1214f4a2713aSLionel Sambuc return Value;
1215f4a2713aSLionel Sambuc }
1216f4a2713aSLionel Sambuc
EmitStoreOfScalar(llvm::Value * Value,llvm::Value * Addr,bool Volatile,unsigned Alignment,QualType Ty,llvm::MDNode * TBAAInfo,bool isInit,QualType TBAABaseType,uint64_t TBAAOffset)1217f4a2713aSLionel Sambuc void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
1218f4a2713aSLionel Sambuc bool Volatile, unsigned Alignment,
1219f4a2713aSLionel Sambuc QualType Ty, llvm::MDNode *TBAAInfo,
1220f4a2713aSLionel Sambuc bool isInit, QualType TBAABaseType,
1221f4a2713aSLionel Sambuc uint64_t TBAAOffset) {
1222f4a2713aSLionel Sambuc
1223f4a2713aSLionel Sambuc // Handle vectors differently to get better performance.
1224f4a2713aSLionel Sambuc if (Ty->isVectorType()) {
1225f4a2713aSLionel Sambuc llvm::Type *SrcTy = Value->getType();
1226*0a6a1f1dSLionel Sambuc auto *VecTy = cast<llvm::VectorType>(SrcTy);
1227f4a2713aSLionel Sambuc // Handle vec3 special.
1228f4a2713aSLionel Sambuc if (VecTy->getNumElements() == 3) {
1229f4a2713aSLionel Sambuc llvm::LLVMContext &VMContext = getLLVMContext();
1230f4a2713aSLionel Sambuc
1231f4a2713aSLionel Sambuc // Our source is a vec3, do a shuffle vector to make it a vec4.
1232f4a2713aSLionel Sambuc SmallVector<llvm::Constant*, 4> Mask;
1233f4a2713aSLionel Sambuc Mask.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
1234f4a2713aSLionel Sambuc 0));
1235f4a2713aSLionel Sambuc Mask.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
1236f4a2713aSLionel Sambuc 1));
1237f4a2713aSLionel Sambuc Mask.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
1238f4a2713aSLionel Sambuc 2));
1239f4a2713aSLionel Sambuc Mask.push_back(llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext)));
1240f4a2713aSLionel Sambuc
1241f4a2713aSLionel Sambuc llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1242f4a2713aSLionel Sambuc Value = Builder.CreateShuffleVector(Value,
1243f4a2713aSLionel Sambuc llvm::UndefValue::get(VecTy),
1244f4a2713aSLionel Sambuc MaskV, "extractVec");
1245f4a2713aSLionel Sambuc SrcTy = llvm::VectorType::get(VecTy->getElementType(), 4);
1246f4a2713aSLionel Sambuc }
1247*0a6a1f1dSLionel Sambuc auto *DstPtr = cast<llvm::PointerType>(Addr->getType());
1248f4a2713aSLionel Sambuc if (DstPtr->getElementType() != SrcTy) {
1249f4a2713aSLionel Sambuc llvm::Type *MemTy =
1250f4a2713aSLionel Sambuc llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
1251f4a2713aSLionel Sambuc Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
1252f4a2713aSLionel Sambuc }
1253f4a2713aSLionel Sambuc }
1254f4a2713aSLionel Sambuc
1255f4a2713aSLionel Sambuc Value = EmitToMemory(Value, Ty);
1256f4a2713aSLionel Sambuc
1257f4a2713aSLionel Sambuc if (Ty->isAtomicType()) {
1258f4a2713aSLionel Sambuc EmitAtomicStore(RValue::get(Value),
1259f4a2713aSLionel Sambuc LValue::MakeAddr(Addr, Ty,
1260f4a2713aSLionel Sambuc CharUnits::fromQuantity(Alignment),
1261f4a2713aSLionel Sambuc getContext(), TBAAInfo),
1262f4a2713aSLionel Sambuc isInit);
1263f4a2713aSLionel Sambuc return;
1264f4a2713aSLionel Sambuc }
1265f4a2713aSLionel Sambuc
1266f4a2713aSLionel Sambuc llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
1267f4a2713aSLionel Sambuc if (Alignment)
1268f4a2713aSLionel Sambuc Store->setAlignment(Alignment);
1269f4a2713aSLionel Sambuc if (TBAAInfo) {
1270f4a2713aSLionel Sambuc llvm::MDNode *TBAAPath = CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo,
1271f4a2713aSLionel Sambuc TBAAOffset);
1272f4a2713aSLionel Sambuc if (TBAAPath)
1273f4a2713aSLionel Sambuc CGM.DecorateInstruction(Store, TBAAPath, false/*ConvertTypeToTag*/);
1274f4a2713aSLionel Sambuc }
1275f4a2713aSLionel Sambuc }
1276f4a2713aSLionel Sambuc
EmitStoreOfScalar(llvm::Value * value,LValue lvalue,bool isInit)1277f4a2713aSLionel Sambuc void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue,
1278f4a2713aSLionel Sambuc bool isInit) {
1279f4a2713aSLionel Sambuc EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(),
1280f4a2713aSLionel Sambuc lvalue.getAlignment().getQuantity(), lvalue.getType(),
1281f4a2713aSLionel Sambuc lvalue.getTBAAInfo(), isInit, lvalue.getTBAABaseType(),
1282f4a2713aSLionel Sambuc lvalue.getTBAAOffset());
1283f4a2713aSLionel Sambuc }
1284f4a2713aSLionel Sambuc
1285f4a2713aSLionel Sambuc /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
1286f4a2713aSLionel Sambuc /// method emits the address of the lvalue, then loads the result as an rvalue,
1287f4a2713aSLionel Sambuc /// returning the rvalue.
EmitLoadOfLValue(LValue LV,SourceLocation Loc)1288f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, SourceLocation Loc) {
1289f4a2713aSLionel Sambuc if (LV.isObjCWeak()) {
1290f4a2713aSLionel Sambuc // load of a __weak object.
1291f4a2713aSLionel Sambuc llvm::Value *AddrWeakObj = LV.getAddress();
1292f4a2713aSLionel Sambuc return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
1293f4a2713aSLionel Sambuc AddrWeakObj));
1294f4a2713aSLionel Sambuc }
1295f4a2713aSLionel Sambuc if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
1296f4a2713aSLionel Sambuc llvm::Value *Object = EmitARCLoadWeakRetained(LV.getAddress());
1297f4a2713aSLionel Sambuc Object = EmitObjCConsumeObject(LV.getType(), Object);
1298f4a2713aSLionel Sambuc return RValue::get(Object);
1299f4a2713aSLionel Sambuc }
1300f4a2713aSLionel Sambuc
1301f4a2713aSLionel Sambuc if (LV.isSimple()) {
1302f4a2713aSLionel Sambuc assert(!LV.getType()->isFunctionType());
1303f4a2713aSLionel Sambuc
1304f4a2713aSLionel Sambuc // Everything needs a load.
1305f4a2713aSLionel Sambuc return RValue::get(EmitLoadOfScalar(LV, Loc));
1306f4a2713aSLionel Sambuc }
1307f4a2713aSLionel Sambuc
1308f4a2713aSLionel Sambuc if (LV.isVectorElt()) {
1309f4a2713aSLionel Sambuc llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddr(),
1310f4a2713aSLionel Sambuc LV.isVolatileQualified());
1311f4a2713aSLionel Sambuc Load->setAlignment(LV.getAlignment().getQuantity());
1312f4a2713aSLionel Sambuc return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(),
1313f4a2713aSLionel Sambuc "vecext"));
1314f4a2713aSLionel Sambuc }
1315f4a2713aSLionel Sambuc
1316f4a2713aSLionel Sambuc // If this is a reference to a subset of the elements of a vector, either
1317f4a2713aSLionel Sambuc // shuffle the input or extract/insert them as appropriate.
1318f4a2713aSLionel Sambuc if (LV.isExtVectorElt())
1319f4a2713aSLionel Sambuc return EmitLoadOfExtVectorElementLValue(LV);
1320f4a2713aSLionel Sambuc
1321*0a6a1f1dSLionel Sambuc // Global Register variables always invoke intrinsics
1322*0a6a1f1dSLionel Sambuc if (LV.isGlobalReg())
1323*0a6a1f1dSLionel Sambuc return EmitLoadOfGlobalRegLValue(LV);
1324*0a6a1f1dSLionel Sambuc
1325f4a2713aSLionel Sambuc assert(LV.isBitField() && "Unknown LValue type!");
1326f4a2713aSLionel Sambuc return EmitLoadOfBitfieldLValue(LV);
1327f4a2713aSLionel Sambuc }
1328f4a2713aSLionel Sambuc
EmitLoadOfBitfieldLValue(LValue LV)1329f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV) {
1330f4a2713aSLionel Sambuc const CGBitFieldInfo &Info = LV.getBitFieldInfo();
1331f4a2713aSLionel Sambuc
1332f4a2713aSLionel Sambuc // Get the output type.
1333f4a2713aSLionel Sambuc llvm::Type *ResLTy = ConvertType(LV.getType());
1334f4a2713aSLionel Sambuc
1335f4a2713aSLionel Sambuc llvm::Value *Ptr = LV.getBitFieldAddr();
1336f4a2713aSLionel Sambuc llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(),
1337f4a2713aSLionel Sambuc "bf.load");
1338f4a2713aSLionel Sambuc cast<llvm::LoadInst>(Val)->setAlignment(Info.StorageAlignment);
1339f4a2713aSLionel Sambuc
1340f4a2713aSLionel Sambuc if (Info.IsSigned) {
1341f4a2713aSLionel Sambuc assert(static_cast<unsigned>(Info.Offset + Info.Size) <= Info.StorageSize);
1342f4a2713aSLionel Sambuc unsigned HighBits = Info.StorageSize - Info.Offset - Info.Size;
1343f4a2713aSLionel Sambuc if (HighBits)
1344f4a2713aSLionel Sambuc Val = Builder.CreateShl(Val, HighBits, "bf.shl");
1345f4a2713aSLionel Sambuc if (Info.Offset + HighBits)
1346f4a2713aSLionel Sambuc Val = Builder.CreateAShr(Val, Info.Offset + HighBits, "bf.ashr");
1347f4a2713aSLionel Sambuc } else {
1348f4a2713aSLionel Sambuc if (Info.Offset)
1349f4a2713aSLionel Sambuc Val = Builder.CreateLShr(Val, Info.Offset, "bf.lshr");
1350f4a2713aSLionel Sambuc if (static_cast<unsigned>(Info.Offset) + Info.Size < Info.StorageSize)
1351f4a2713aSLionel Sambuc Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(Info.StorageSize,
1352f4a2713aSLionel Sambuc Info.Size),
1353f4a2713aSLionel Sambuc "bf.clear");
1354f4a2713aSLionel Sambuc }
1355f4a2713aSLionel Sambuc Val = Builder.CreateIntCast(Val, ResLTy, Info.IsSigned, "bf.cast");
1356f4a2713aSLionel Sambuc
1357f4a2713aSLionel Sambuc return RValue::get(Val);
1358f4a2713aSLionel Sambuc }
1359f4a2713aSLionel Sambuc
1360f4a2713aSLionel Sambuc // If this is a reference to a subset of the elements of a vector, create an
1361f4a2713aSLionel Sambuc // appropriate shufflevector.
EmitLoadOfExtVectorElementLValue(LValue LV)1362f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) {
1363f4a2713aSLionel Sambuc llvm::LoadInst *Load = Builder.CreateLoad(LV.getExtVectorAddr(),
1364f4a2713aSLionel Sambuc LV.isVolatileQualified());
1365f4a2713aSLionel Sambuc Load->setAlignment(LV.getAlignment().getQuantity());
1366f4a2713aSLionel Sambuc llvm::Value *Vec = Load;
1367f4a2713aSLionel Sambuc
1368f4a2713aSLionel Sambuc const llvm::Constant *Elts = LV.getExtVectorElts();
1369f4a2713aSLionel Sambuc
1370f4a2713aSLionel Sambuc // If the result of the expression is a non-vector type, we must be extracting
1371f4a2713aSLionel Sambuc // a single element. Just codegen as an extractelement.
1372f4a2713aSLionel Sambuc const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
1373f4a2713aSLionel Sambuc if (!ExprVT) {
1374f4a2713aSLionel Sambuc unsigned InIdx = getAccessedFieldNo(0, Elts);
1375*0a6a1f1dSLionel Sambuc llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
1376f4a2713aSLionel Sambuc return RValue::get(Builder.CreateExtractElement(Vec, Elt));
1377f4a2713aSLionel Sambuc }
1378f4a2713aSLionel Sambuc
1379f4a2713aSLionel Sambuc // Always use shuffle vector to try to retain the original program structure
1380f4a2713aSLionel Sambuc unsigned NumResultElts = ExprVT->getNumElements();
1381f4a2713aSLionel Sambuc
1382f4a2713aSLionel Sambuc SmallVector<llvm::Constant*, 4> Mask;
1383f4a2713aSLionel Sambuc for (unsigned i = 0; i != NumResultElts; ++i)
1384f4a2713aSLionel Sambuc Mask.push_back(Builder.getInt32(getAccessedFieldNo(i, Elts)));
1385f4a2713aSLionel Sambuc
1386f4a2713aSLionel Sambuc llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1387f4a2713aSLionel Sambuc Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()),
1388f4a2713aSLionel Sambuc MaskV);
1389f4a2713aSLionel Sambuc return RValue::get(Vec);
1390f4a2713aSLionel Sambuc }
1391f4a2713aSLionel Sambuc
1392*0a6a1f1dSLionel Sambuc /// @brief Generates lvalue for partial ext_vector access.
EmitExtVectorElementLValue(LValue LV)1393*0a6a1f1dSLionel Sambuc llvm::Value *CodeGenFunction::EmitExtVectorElementLValue(LValue LV) {
1394*0a6a1f1dSLionel Sambuc llvm::Value *VectorAddress = LV.getExtVectorAddr();
1395*0a6a1f1dSLionel Sambuc const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
1396*0a6a1f1dSLionel Sambuc QualType EQT = ExprVT->getElementType();
1397*0a6a1f1dSLionel Sambuc llvm::Type *VectorElementTy = CGM.getTypes().ConvertType(EQT);
1398*0a6a1f1dSLionel Sambuc llvm::Type *VectorElementPtrToTy = VectorElementTy->getPointerTo();
1399*0a6a1f1dSLionel Sambuc
1400*0a6a1f1dSLionel Sambuc llvm::Value *CastToPointerElement =
1401*0a6a1f1dSLionel Sambuc Builder.CreateBitCast(VectorAddress,
1402*0a6a1f1dSLionel Sambuc VectorElementPtrToTy, "conv.ptr.element");
1403*0a6a1f1dSLionel Sambuc
1404*0a6a1f1dSLionel Sambuc const llvm::Constant *Elts = LV.getExtVectorElts();
1405*0a6a1f1dSLionel Sambuc unsigned ix = getAccessedFieldNo(0, Elts);
1406*0a6a1f1dSLionel Sambuc
1407*0a6a1f1dSLionel Sambuc llvm::Value *VectorBasePtrPlusIx =
1408*0a6a1f1dSLionel Sambuc Builder.CreateInBoundsGEP(CastToPointerElement,
1409*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(SizeTy, ix), "add.ptr");
1410*0a6a1f1dSLionel Sambuc
1411*0a6a1f1dSLionel Sambuc return VectorBasePtrPlusIx;
1412*0a6a1f1dSLionel Sambuc }
1413*0a6a1f1dSLionel Sambuc
1414*0a6a1f1dSLionel Sambuc /// @brief Load of global gamed gegisters are always calls to intrinsics.
EmitLoadOfGlobalRegLValue(LValue LV)1415*0a6a1f1dSLionel Sambuc RValue CodeGenFunction::EmitLoadOfGlobalRegLValue(LValue LV) {
1416*0a6a1f1dSLionel Sambuc assert((LV.getType()->isIntegerType() || LV.getType()->isPointerType()) &&
1417*0a6a1f1dSLionel Sambuc "Bad type for register variable");
1418*0a6a1f1dSLionel Sambuc llvm::MDNode *RegName = cast<llvm::MDNode>(
1419*0a6a1f1dSLionel Sambuc cast<llvm::MetadataAsValue>(LV.getGlobalReg())->getMetadata());
1420*0a6a1f1dSLionel Sambuc
1421*0a6a1f1dSLionel Sambuc // We accept integer and pointer types only
1422*0a6a1f1dSLionel Sambuc llvm::Type *OrigTy = CGM.getTypes().ConvertType(LV.getType());
1423*0a6a1f1dSLionel Sambuc llvm::Type *Ty = OrigTy;
1424*0a6a1f1dSLionel Sambuc if (OrigTy->isPointerTy())
1425*0a6a1f1dSLionel Sambuc Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
1426*0a6a1f1dSLionel Sambuc llvm::Type *Types[] = { Ty };
1427*0a6a1f1dSLionel Sambuc
1428*0a6a1f1dSLionel Sambuc llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::read_register, Types);
1429*0a6a1f1dSLionel Sambuc llvm::Value *Call = Builder.CreateCall(
1430*0a6a1f1dSLionel Sambuc F, llvm::MetadataAsValue::get(Ty->getContext(), RegName));
1431*0a6a1f1dSLionel Sambuc if (OrigTy->isPointerTy())
1432*0a6a1f1dSLionel Sambuc Call = Builder.CreateIntToPtr(Call, OrigTy);
1433*0a6a1f1dSLionel Sambuc return RValue::get(Call);
1434*0a6a1f1dSLionel Sambuc }
1435f4a2713aSLionel Sambuc
1436f4a2713aSLionel Sambuc
1437f4a2713aSLionel Sambuc /// EmitStoreThroughLValue - Store the specified rvalue into the specified
1438f4a2713aSLionel Sambuc /// lvalue, where both are guaranteed to the have the same type, and that type
1439f4a2713aSLionel Sambuc /// is 'Ty'.
EmitStoreThroughLValue(RValue Src,LValue Dst,bool isInit)1440f4a2713aSLionel Sambuc void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
1441f4a2713aSLionel Sambuc bool isInit) {
1442f4a2713aSLionel Sambuc if (!Dst.isSimple()) {
1443f4a2713aSLionel Sambuc if (Dst.isVectorElt()) {
1444f4a2713aSLionel Sambuc // Read/modify/write the vector, inserting the new element.
1445f4a2713aSLionel Sambuc llvm::LoadInst *Load = Builder.CreateLoad(Dst.getVectorAddr(),
1446f4a2713aSLionel Sambuc Dst.isVolatileQualified());
1447f4a2713aSLionel Sambuc Load->setAlignment(Dst.getAlignment().getQuantity());
1448f4a2713aSLionel Sambuc llvm::Value *Vec = Load;
1449f4a2713aSLionel Sambuc Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
1450f4a2713aSLionel Sambuc Dst.getVectorIdx(), "vecins");
1451f4a2713aSLionel Sambuc llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getVectorAddr(),
1452f4a2713aSLionel Sambuc Dst.isVolatileQualified());
1453f4a2713aSLionel Sambuc Store->setAlignment(Dst.getAlignment().getQuantity());
1454f4a2713aSLionel Sambuc return;
1455f4a2713aSLionel Sambuc }
1456f4a2713aSLionel Sambuc
1457f4a2713aSLionel Sambuc // If this is an update of extended vector elements, insert them as
1458f4a2713aSLionel Sambuc // appropriate.
1459f4a2713aSLionel Sambuc if (Dst.isExtVectorElt())
1460f4a2713aSLionel Sambuc return EmitStoreThroughExtVectorComponentLValue(Src, Dst);
1461f4a2713aSLionel Sambuc
1462*0a6a1f1dSLionel Sambuc if (Dst.isGlobalReg())
1463*0a6a1f1dSLionel Sambuc return EmitStoreThroughGlobalRegLValue(Src, Dst);
1464*0a6a1f1dSLionel Sambuc
1465f4a2713aSLionel Sambuc assert(Dst.isBitField() && "Unknown LValue type");
1466f4a2713aSLionel Sambuc return EmitStoreThroughBitfieldLValue(Src, Dst);
1467f4a2713aSLionel Sambuc }
1468f4a2713aSLionel Sambuc
1469f4a2713aSLionel Sambuc // There's special magic for assigning into an ARC-qualified l-value.
1470f4a2713aSLionel Sambuc if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) {
1471f4a2713aSLionel Sambuc switch (Lifetime) {
1472f4a2713aSLionel Sambuc case Qualifiers::OCL_None:
1473f4a2713aSLionel Sambuc llvm_unreachable("present but none");
1474f4a2713aSLionel Sambuc
1475f4a2713aSLionel Sambuc case Qualifiers::OCL_ExplicitNone:
1476f4a2713aSLionel Sambuc // nothing special
1477f4a2713aSLionel Sambuc break;
1478f4a2713aSLionel Sambuc
1479f4a2713aSLionel Sambuc case Qualifiers::OCL_Strong:
1480f4a2713aSLionel Sambuc EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true);
1481f4a2713aSLionel Sambuc return;
1482f4a2713aSLionel Sambuc
1483f4a2713aSLionel Sambuc case Qualifiers::OCL_Weak:
1484f4a2713aSLionel Sambuc EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true);
1485f4a2713aSLionel Sambuc return;
1486f4a2713aSLionel Sambuc
1487f4a2713aSLionel Sambuc case Qualifiers::OCL_Autoreleasing:
1488f4a2713aSLionel Sambuc Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(),
1489f4a2713aSLionel Sambuc Src.getScalarVal()));
1490f4a2713aSLionel Sambuc // fall into the normal path
1491f4a2713aSLionel Sambuc break;
1492f4a2713aSLionel Sambuc }
1493f4a2713aSLionel Sambuc }
1494f4a2713aSLionel Sambuc
1495f4a2713aSLionel Sambuc if (Dst.isObjCWeak() && !Dst.isNonGC()) {
1496f4a2713aSLionel Sambuc // load of a __weak object.
1497f4a2713aSLionel Sambuc llvm::Value *LvalueDst = Dst.getAddress();
1498f4a2713aSLionel Sambuc llvm::Value *src = Src.getScalarVal();
1499f4a2713aSLionel Sambuc CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
1500f4a2713aSLionel Sambuc return;
1501f4a2713aSLionel Sambuc }
1502f4a2713aSLionel Sambuc
1503f4a2713aSLionel Sambuc if (Dst.isObjCStrong() && !Dst.isNonGC()) {
1504f4a2713aSLionel Sambuc // load of a __strong object.
1505f4a2713aSLionel Sambuc llvm::Value *LvalueDst = Dst.getAddress();
1506f4a2713aSLionel Sambuc llvm::Value *src = Src.getScalarVal();
1507f4a2713aSLionel Sambuc if (Dst.isObjCIvar()) {
1508f4a2713aSLionel Sambuc assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
1509f4a2713aSLionel Sambuc llvm::Type *ResultType = ConvertType(getContext().LongTy);
1510f4a2713aSLionel Sambuc llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
1511f4a2713aSLionel Sambuc llvm::Value *dst = RHS;
1512f4a2713aSLionel Sambuc RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1513f4a2713aSLionel Sambuc llvm::Value *LHS =
1514f4a2713aSLionel Sambuc Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
1515f4a2713aSLionel Sambuc llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
1516f4a2713aSLionel Sambuc CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
1517f4a2713aSLionel Sambuc BytesBetween);
1518f4a2713aSLionel Sambuc } else if (Dst.isGlobalObjCRef()) {
1519f4a2713aSLionel Sambuc CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst,
1520f4a2713aSLionel Sambuc Dst.isThreadLocalRef());
1521f4a2713aSLionel Sambuc }
1522f4a2713aSLionel Sambuc else
1523f4a2713aSLionel Sambuc CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
1524f4a2713aSLionel Sambuc return;
1525f4a2713aSLionel Sambuc }
1526f4a2713aSLionel Sambuc
1527f4a2713aSLionel Sambuc assert(Src.isScalar() && "Can't emit an agg store with this method");
1528f4a2713aSLionel Sambuc EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit);
1529f4a2713aSLionel Sambuc }
1530f4a2713aSLionel Sambuc
EmitStoreThroughBitfieldLValue(RValue Src,LValue Dst,llvm::Value ** Result)1531f4a2713aSLionel Sambuc void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
1532f4a2713aSLionel Sambuc llvm::Value **Result) {
1533f4a2713aSLionel Sambuc const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
1534f4a2713aSLionel Sambuc llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType());
1535f4a2713aSLionel Sambuc llvm::Value *Ptr = Dst.getBitFieldAddr();
1536f4a2713aSLionel Sambuc
1537f4a2713aSLionel Sambuc // Get the source value, truncated to the width of the bit-field.
1538f4a2713aSLionel Sambuc llvm::Value *SrcVal = Src.getScalarVal();
1539f4a2713aSLionel Sambuc
1540f4a2713aSLionel Sambuc // Cast the source to the storage type and shift it into place.
1541f4a2713aSLionel Sambuc SrcVal = Builder.CreateIntCast(SrcVal,
1542f4a2713aSLionel Sambuc Ptr->getType()->getPointerElementType(),
1543f4a2713aSLionel Sambuc /*IsSigned=*/false);
1544f4a2713aSLionel Sambuc llvm::Value *MaskedVal = SrcVal;
1545f4a2713aSLionel Sambuc
1546f4a2713aSLionel Sambuc // See if there are other bits in the bitfield's storage we'll need to load
1547f4a2713aSLionel Sambuc // and mask together with source before storing.
1548f4a2713aSLionel Sambuc if (Info.StorageSize != Info.Size) {
1549f4a2713aSLionel Sambuc assert(Info.StorageSize > Info.Size && "Invalid bitfield size.");
1550f4a2713aSLionel Sambuc llvm::Value *Val = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
1551f4a2713aSLionel Sambuc "bf.load");
1552f4a2713aSLionel Sambuc cast<llvm::LoadInst>(Val)->setAlignment(Info.StorageAlignment);
1553f4a2713aSLionel Sambuc
1554f4a2713aSLionel Sambuc // Mask the source value as needed.
1555f4a2713aSLionel Sambuc if (!hasBooleanRepresentation(Dst.getType()))
1556f4a2713aSLionel Sambuc SrcVal = Builder.CreateAnd(SrcVal,
1557f4a2713aSLionel Sambuc llvm::APInt::getLowBitsSet(Info.StorageSize,
1558f4a2713aSLionel Sambuc Info.Size),
1559f4a2713aSLionel Sambuc "bf.value");
1560f4a2713aSLionel Sambuc MaskedVal = SrcVal;
1561f4a2713aSLionel Sambuc if (Info.Offset)
1562f4a2713aSLionel Sambuc SrcVal = Builder.CreateShl(SrcVal, Info.Offset, "bf.shl");
1563f4a2713aSLionel Sambuc
1564f4a2713aSLionel Sambuc // Mask out the original value.
1565f4a2713aSLionel Sambuc Val = Builder.CreateAnd(Val,
1566f4a2713aSLionel Sambuc ~llvm::APInt::getBitsSet(Info.StorageSize,
1567f4a2713aSLionel Sambuc Info.Offset,
1568f4a2713aSLionel Sambuc Info.Offset + Info.Size),
1569f4a2713aSLionel Sambuc "bf.clear");
1570f4a2713aSLionel Sambuc
1571f4a2713aSLionel Sambuc // Or together the unchanged values and the source value.
1572f4a2713aSLionel Sambuc SrcVal = Builder.CreateOr(Val, SrcVal, "bf.set");
1573f4a2713aSLionel Sambuc } else {
1574f4a2713aSLionel Sambuc assert(Info.Offset == 0);
1575f4a2713aSLionel Sambuc }
1576f4a2713aSLionel Sambuc
1577f4a2713aSLionel Sambuc // Write the new value back out.
1578f4a2713aSLionel Sambuc llvm::StoreInst *Store = Builder.CreateStore(SrcVal, Ptr,
1579f4a2713aSLionel Sambuc Dst.isVolatileQualified());
1580f4a2713aSLionel Sambuc Store->setAlignment(Info.StorageAlignment);
1581f4a2713aSLionel Sambuc
1582f4a2713aSLionel Sambuc // Return the new value of the bit-field, if requested.
1583f4a2713aSLionel Sambuc if (Result) {
1584f4a2713aSLionel Sambuc llvm::Value *ResultVal = MaskedVal;
1585f4a2713aSLionel Sambuc
1586f4a2713aSLionel Sambuc // Sign extend the value if needed.
1587f4a2713aSLionel Sambuc if (Info.IsSigned) {
1588f4a2713aSLionel Sambuc assert(Info.Size <= Info.StorageSize);
1589f4a2713aSLionel Sambuc unsigned HighBits = Info.StorageSize - Info.Size;
1590f4a2713aSLionel Sambuc if (HighBits) {
1591f4a2713aSLionel Sambuc ResultVal = Builder.CreateShl(ResultVal, HighBits, "bf.result.shl");
1592f4a2713aSLionel Sambuc ResultVal = Builder.CreateAShr(ResultVal, HighBits, "bf.result.ashr");
1593f4a2713aSLionel Sambuc }
1594f4a2713aSLionel Sambuc }
1595f4a2713aSLionel Sambuc
1596f4a2713aSLionel Sambuc ResultVal = Builder.CreateIntCast(ResultVal, ResLTy, Info.IsSigned,
1597f4a2713aSLionel Sambuc "bf.result.cast");
1598f4a2713aSLionel Sambuc *Result = EmitFromMemory(ResultVal, Dst.getType());
1599f4a2713aSLionel Sambuc }
1600f4a2713aSLionel Sambuc }
1601f4a2713aSLionel Sambuc
EmitStoreThroughExtVectorComponentLValue(RValue Src,LValue Dst)1602f4a2713aSLionel Sambuc void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
1603f4a2713aSLionel Sambuc LValue Dst) {
1604f4a2713aSLionel Sambuc // This access turns into a read/modify/write of the vector. Load the input
1605f4a2713aSLionel Sambuc // value now.
1606f4a2713aSLionel Sambuc llvm::LoadInst *Load = Builder.CreateLoad(Dst.getExtVectorAddr(),
1607f4a2713aSLionel Sambuc Dst.isVolatileQualified());
1608f4a2713aSLionel Sambuc Load->setAlignment(Dst.getAlignment().getQuantity());
1609f4a2713aSLionel Sambuc llvm::Value *Vec = Load;
1610f4a2713aSLionel Sambuc const llvm::Constant *Elts = Dst.getExtVectorElts();
1611f4a2713aSLionel Sambuc
1612f4a2713aSLionel Sambuc llvm::Value *SrcVal = Src.getScalarVal();
1613f4a2713aSLionel Sambuc
1614f4a2713aSLionel Sambuc if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) {
1615f4a2713aSLionel Sambuc unsigned NumSrcElts = VTy->getNumElements();
1616f4a2713aSLionel Sambuc unsigned NumDstElts =
1617f4a2713aSLionel Sambuc cast<llvm::VectorType>(Vec->getType())->getNumElements();
1618f4a2713aSLionel Sambuc if (NumDstElts == NumSrcElts) {
1619f4a2713aSLionel Sambuc // Use shuffle vector is the src and destination are the same number of
1620f4a2713aSLionel Sambuc // elements and restore the vector mask since it is on the side it will be
1621f4a2713aSLionel Sambuc // stored.
1622f4a2713aSLionel Sambuc SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
1623f4a2713aSLionel Sambuc for (unsigned i = 0; i != NumSrcElts; ++i)
1624f4a2713aSLionel Sambuc Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i);
1625f4a2713aSLionel Sambuc
1626f4a2713aSLionel Sambuc llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1627f4a2713aSLionel Sambuc Vec = Builder.CreateShuffleVector(SrcVal,
1628f4a2713aSLionel Sambuc llvm::UndefValue::get(Vec->getType()),
1629f4a2713aSLionel Sambuc MaskV);
1630f4a2713aSLionel Sambuc } else if (NumDstElts > NumSrcElts) {
1631f4a2713aSLionel Sambuc // Extended the source vector to the same length and then shuffle it
1632f4a2713aSLionel Sambuc // into the destination.
1633f4a2713aSLionel Sambuc // FIXME: since we're shuffling with undef, can we just use the indices
1634f4a2713aSLionel Sambuc // into that? This could be simpler.
1635f4a2713aSLionel Sambuc SmallVector<llvm::Constant*, 4> ExtMask;
1636f4a2713aSLionel Sambuc for (unsigned i = 0; i != NumSrcElts; ++i)
1637f4a2713aSLionel Sambuc ExtMask.push_back(Builder.getInt32(i));
1638f4a2713aSLionel Sambuc ExtMask.resize(NumDstElts, llvm::UndefValue::get(Int32Ty));
1639f4a2713aSLionel Sambuc llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask);
1640f4a2713aSLionel Sambuc llvm::Value *ExtSrcVal =
1641f4a2713aSLionel Sambuc Builder.CreateShuffleVector(SrcVal,
1642f4a2713aSLionel Sambuc llvm::UndefValue::get(SrcVal->getType()),
1643f4a2713aSLionel Sambuc ExtMaskV);
1644f4a2713aSLionel Sambuc // build identity
1645f4a2713aSLionel Sambuc SmallVector<llvm::Constant*, 4> Mask;
1646f4a2713aSLionel Sambuc for (unsigned i = 0; i != NumDstElts; ++i)
1647f4a2713aSLionel Sambuc Mask.push_back(Builder.getInt32(i));
1648f4a2713aSLionel Sambuc
1649f4a2713aSLionel Sambuc // When the vector size is odd and .odd or .hi is used, the last element
1650f4a2713aSLionel Sambuc // of the Elts constant array will be one past the size of the vector.
1651f4a2713aSLionel Sambuc // Ignore the last element here, if it is greater than the mask size.
1652f4a2713aSLionel Sambuc if (getAccessedFieldNo(NumSrcElts - 1, Elts) == Mask.size())
1653f4a2713aSLionel Sambuc NumSrcElts--;
1654f4a2713aSLionel Sambuc
1655f4a2713aSLionel Sambuc // modify when what gets shuffled in
1656f4a2713aSLionel Sambuc for (unsigned i = 0; i != NumSrcElts; ++i)
1657f4a2713aSLionel Sambuc Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i+NumDstElts);
1658f4a2713aSLionel Sambuc llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1659f4a2713aSLionel Sambuc Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV);
1660f4a2713aSLionel Sambuc } else {
1661f4a2713aSLionel Sambuc // We should never shorten the vector
1662f4a2713aSLionel Sambuc llvm_unreachable("unexpected shorten vector length");
1663f4a2713aSLionel Sambuc }
1664f4a2713aSLionel Sambuc } else {
1665f4a2713aSLionel Sambuc // If the Src is a scalar (not a vector) it must be updating one element.
1666f4a2713aSLionel Sambuc unsigned InIdx = getAccessedFieldNo(0, Elts);
1667*0a6a1f1dSLionel Sambuc llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
1668f4a2713aSLionel Sambuc Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt);
1669f4a2713aSLionel Sambuc }
1670f4a2713aSLionel Sambuc
1671f4a2713aSLionel Sambuc llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getExtVectorAddr(),
1672f4a2713aSLionel Sambuc Dst.isVolatileQualified());
1673f4a2713aSLionel Sambuc Store->setAlignment(Dst.getAlignment().getQuantity());
1674f4a2713aSLionel Sambuc }
1675f4a2713aSLionel Sambuc
1676*0a6a1f1dSLionel Sambuc /// @brief Store of global named registers are always calls to intrinsics.
EmitStoreThroughGlobalRegLValue(RValue Src,LValue Dst)1677*0a6a1f1dSLionel Sambuc void CodeGenFunction::EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst) {
1678*0a6a1f1dSLionel Sambuc assert((Dst.getType()->isIntegerType() || Dst.getType()->isPointerType()) &&
1679*0a6a1f1dSLionel Sambuc "Bad type for register variable");
1680*0a6a1f1dSLionel Sambuc llvm::MDNode *RegName = cast<llvm::MDNode>(
1681*0a6a1f1dSLionel Sambuc cast<llvm::MetadataAsValue>(Dst.getGlobalReg())->getMetadata());
1682*0a6a1f1dSLionel Sambuc assert(RegName && "Register LValue is not metadata");
1683*0a6a1f1dSLionel Sambuc
1684*0a6a1f1dSLionel Sambuc // We accept integer and pointer types only
1685*0a6a1f1dSLionel Sambuc llvm::Type *OrigTy = CGM.getTypes().ConvertType(Dst.getType());
1686*0a6a1f1dSLionel Sambuc llvm::Type *Ty = OrigTy;
1687*0a6a1f1dSLionel Sambuc if (OrigTy->isPointerTy())
1688*0a6a1f1dSLionel Sambuc Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
1689*0a6a1f1dSLionel Sambuc llvm::Type *Types[] = { Ty };
1690*0a6a1f1dSLionel Sambuc
1691*0a6a1f1dSLionel Sambuc llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::write_register, Types);
1692*0a6a1f1dSLionel Sambuc llvm::Value *Value = Src.getScalarVal();
1693*0a6a1f1dSLionel Sambuc if (OrigTy->isPointerTy())
1694*0a6a1f1dSLionel Sambuc Value = Builder.CreatePtrToInt(Value, Ty);
1695*0a6a1f1dSLionel Sambuc Builder.CreateCall2(F, llvm::MetadataAsValue::get(Ty->getContext(), RegName),
1696*0a6a1f1dSLionel Sambuc Value);
1697*0a6a1f1dSLionel Sambuc }
1698*0a6a1f1dSLionel Sambuc
1699*0a6a1f1dSLionel Sambuc // setObjCGCLValueClass - sets class of the lvalue for the purpose of
1700f4a2713aSLionel Sambuc // generating write-barries API. It is currently a global, ivar,
1701f4a2713aSLionel Sambuc // or neither.
setObjCGCLValueClass(const ASTContext & Ctx,const Expr * E,LValue & LV,bool IsMemberAccess=false)1702f4a2713aSLionel Sambuc static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
1703f4a2713aSLionel Sambuc LValue &LV,
1704f4a2713aSLionel Sambuc bool IsMemberAccess=false) {
1705f4a2713aSLionel Sambuc if (Ctx.getLangOpts().getGC() == LangOptions::NonGC)
1706f4a2713aSLionel Sambuc return;
1707f4a2713aSLionel Sambuc
1708f4a2713aSLionel Sambuc if (isa<ObjCIvarRefExpr>(E)) {
1709f4a2713aSLionel Sambuc QualType ExpTy = E->getType();
1710f4a2713aSLionel Sambuc if (IsMemberAccess && ExpTy->isPointerType()) {
1711f4a2713aSLionel Sambuc // If ivar is a structure pointer, assigning to field of
1712f4a2713aSLionel Sambuc // this struct follows gcc's behavior and makes it a non-ivar
1713f4a2713aSLionel Sambuc // writer-barrier conservatively.
1714f4a2713aSLionel Sambuc ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1715f4a2713aSLionel Sambuc if (ExpTy->isRecordType()) {
1716f4a2713aSLionel Sambuc LV.setObjCIvar(false);
1717f4a2713aSLionel Sambuc return;
1718f4a2713aSLionel Sambuc }
1719f4a2713aSLionel Sambuc }
1720f4a2713aSLionel Sambuc LV.setObjCIvar(true);
1721*0a6a1f1dSLionel Sambuc auto *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr *>(E));
1722f4a2713aSLionel Sambuc LV.setBaseIvarExp(Exp->getBase());
1723f4a2713aSLionel Sambuc LV.setObjCArray(E->getType()->isArrayType());
1724f4a2713aSLionel Sambuc return;
1725f4a2713aSLionel Sambuc }
1726f4a2713aSLionel Sambuc
1727*0a6a1f1dSLionel Sambuc if (const auto *Exp = dyn_cast<DeclRefExpr>(E)) {
1728*0a6a1f1dSLionel Sambuc if (const auto *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
1729f4a2713aSLionel Sambuc if (VD->hasGlobalStorage()) {
1730f4a2713aSLionel Sambuc LV.setGlobalObjCRef(true);
1731f4a2713aSLionel Sambuc LV.setThreadLocalRef(VD->getTLSKind() != VarDecl::TLS_None);
1732f4a2713aSLionel Sambuc }
1733f4a2713aSLionel Sambuc }
1734f4a2713aSLionel Sambuc LV.setObjCArray(E->getType()->isArrayType());
1735f4a2713aSLionel Sambuc return;
1736f4a2713aSLionel Sambuc }
1737f4a2713aSLionel Sambuc
1738*0a6a1f1dSLionel Sambuc if (const auto *Exp = dyn_cast<UnaryOperator>(E)) {
1739f4a2713aSLionel Sambuc setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1740f4a2713aSLionel Sambuc return;
1741f4a2713aSLionel Sambuc }
1742f4a2713aSLionel Sambuc
1743*0a6a1f1dSLionel Sambuc if (const auto *Exp = dyn_cast<ParenExpr>(E)) {
1744f4a2713aSLionel Sambuc setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1745f4a2713aSLionel Sambuc if (LV.isObjCIvar()) {
1746f4a2713aSLionel Sambuc // If cast is to a structure pointer, follow gcc's behavior and make it
1747f4a2713aSLionel Sambuc // a non-ivar write-barrier.
1748f4a2713aSLionel Sambuc QualType ExpTy = E->getType();
1749f4a2713aSLionel Sambuc if (ExpTy->isPointerType())
1750f4a2713aSLionel Sambuc ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1751f4a2713aSLionel Sambuc if (ExpTy->isRecordType())
1752f4a2713aSLionel Sambuc LV.setObjCIvar(false);
1753f4a2713aSLionel Sambuc }
1754f4a2713aSLionel Sambuc return;
1755f4a2713aSLionel Sambuc }
1756f4a2713aSLionel Sambuc
1757*0a6a1f1dSLionel Sambuc if (const auto *Exp = dyn_cast<GenericSelectionExpr>(E)) {
1758f4a2713aSLionel Sambuc setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV);
1759f4a2713aSLionel Sambuc return;
1760f4a2713aSLionel Sambuc }
1761f4a2713aSLionel Sambuc
1762*0a6a1f1dSLionel Sambuc if (const auto *Exp = dyn_cast<ImplicitCastExpr>(E)) {
1763f4a2713aSLionel Sambuc setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1764f4a2713aSLionel Sambuc return;
1765f4a2713aSLionel Sambuc }
1766f4a2713aSLionel Sambuc
1767*0a6a1f1dSLionel Sambuc if (const auto *Exp = dyn_cast<CStyleCastExpr>(E)) {
1768f4a2713aSLionel Sambuc setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1769f4a2713aSLionel Sambuc return;
1770f4a2713aSLionel Sambuc }
1771f4a2713aSLionel Sambuc
1772*0a6a1f1dSLionel Sambuc if (const auto *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) {
1773f4a2713aSLionel Sambuc setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1774f4a2713aSLionel Sambuc return;
1775f4a2713aSLionel Sambuc }
1776f4a2713aSLionel Sambuc
1777*0a6a1f1dSLionel Sambuc if (const auto *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
1778f4a2713aSLionel Sambuc setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
1779f4a2713aSLionel Sambuc if (LV.isObjCIvar() && !LV.isObjCArray())
1780f4a2713aSLionel Sambuc // Using array syntax to assigning to what an ivar points to is not
1781f4a2713aSLionel Sambuc // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1782f4a2713aSLionel Sambuc LV.setObjCIvar(false);
1783f4a2713aSLionel Sambuc else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1784f4a2713aSLionel Sambuc // Using array syntax to assigning to what global points to is not
1785f4a2713aSLionel Sambuc // same as assigning to the global itself. {id *G;} G[i] = 0;
1786f4a2713aSLionel Sambuc LV.setGlobalObjCRef(false);
1787f4a2713aSLionel Sambuc return;
1788f4a2713aSLionel Sambuc }
1789f4a2713aSLionel Sambuc
1790*0a6a1f1dSLionel Sambuc if (const auto *Exp = dyn_cast<MemberExpr>(E)) {
1791f4a2713aSLionel Sambuc setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true);
1792f4a2713aSLionel Sambuc // We don't know if member is an 'ivar', but this flag is looked at
1793f4a2713aSLionel Sambuc // only in the context of LV.isObjCIvar().
1794f4a2713aSLionel Sambuc LV.setObjCArray(E->getType()->isArrayType());
1795f4a2713aSLionel Sambuc return;
1796f4a2713aSLionel Sambuc }
1797f4a2713aSLionel Sambuc }
1798f4a2713aSLionel Sambuc
1799f4a2713aSLionel Sambuc static llvm::Value *
EmitBitCastOfLValueToProperType(CodeGenFunction & CGF,llvm::Value * V,llvm::Type * IRType,StringRef Name=StringRef ())1800f4a2713aSLionel Sambuc EmitBitCastOfLValueToProperType(CodeGenFunction &CGF,
1801f4a2713aSLionel Sambuc llvm::Value *V, llvm::Type *IRType,
1802f4a2713aSLionel Sambuc StringRef Name = StringRef()) {
1803f4a2713aSLionel Sambuc unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
1804f4a2713aSLionel Sambuc return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name);
1805f4a2713aSLionel Sambuc }
1806f4a2713aSLionel Sambuc
EmitThreadPrivateVarDeclLValue(CodeGenFunction & CGF,const VarDecl * VD,QualType T,llvm::Value * V,llvm::Type * RealVarTy,CharUnits Alignment,SourceLocation Loc)1807*0a6a1f1dSLionel Sambuc static LValue EmitThreadPrivateVarDeclLValue(
1808*0a6a1f1dSLionel Sambuc CodeGenFunction &CGF, const VarDecl *VD, QualType T, llvm::Value *V,
1809*0a6a1f1dSLionel Sambuc llvm::Type *RealVarTy, CharUnits Alignment, SourceLocation Loc) {
1810*0a6a1f1dSLionel Sambuc V = CGF.CGM.getOpenMPRuntime().getOMPAddrOfThreadPrivate(CGF, VD, V, Loc);
1811*0a6a1f1dSLionel Sambuc V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy);
1812*0a6a1f1dSLionel Sambuc return CGF.MakeAddrLValue(V, T, Alignment);
1813*0a6a1f1dSLionel Sambuc }
1814*0a6a1f1dSLionel Sambuc
EmitGlobalVarDeclLValue(CodeGenFunction & CGF,const Expr * E,const VarDecl * VD)1815f4a2713aSLionel Sambuc static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1816f4a2713aSLionel Sambuc const Expr *E, const VarDecl *VD) {
1817*0a6a1f1dSLionel Sambuc QualType T = E->getType();
1818*0a6a1f1dSLionel Sambuc
1819*0a6a1f1dSLionel Sambuc // If it's thread_local, emit a call to its wrapper function instead.
1820*0a6a1f1dSLionel Sambuc if (VD->getTLSKind() == VarDecl::TLS_Dynamic &&
1821*0a6a1f1dSLionel Sambuc CGF.CGM.getCXXABI().usesThreadWrapperFunction())
1822*0a6a1f1dSLionel Sambuc return CGF.CGM.getCXXABI().EmitThreadLocalVarDeclLValue(CGF, VD, T);
1823*0a6a1f1dSLionel Sambuc
1824f4a2713aSLionel Sambuc llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1825f4a2713aSLionel Sambuc llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType());
1826f4a2713aSLionel Sambuc V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy);
1827f4a2713aSLionel Sambuc CharUnits Alignment = CGF.getContext().getDeclAlign(VD);
1828f4a2713aSLionel Sambuc LValue LV;
1829*0a6a1f1dSLionel Sambuc // Emit reference to the private copy of the variable if it is an OpenMP
1830*0a6a1f1dSLionel Sambuc // threadprivate variable.
1831*0a6a1f1dSLionel Sambuc if (CGF.getLangOpts().OpenMP && VD->hasAttr<OMPThreadPrivateDeclAttr>())
1832*0a6a1f1dSLionel Sambuc return EmitThreadPrivateVarDeclLValue(CGF, VD, T, V, RealVarTy, Alignment,
1833*0a6a1f1dSLionel Sambuc E->getExprLoc());
1834f4a2713aSLionel Sambuc if (VD->getType()->isReferenceType()) {
1835f4a2713aSLionel Sambuc llvm::LoadInst *LI = CGF.Builder.CreateLoad(V);
1836f4a2713aSLionel Sambuc LI->setAlignment(Alignment.getQuantity());
1837f4a2713aSLionel Sambuc V = LI;
1838f4a2713aSLionel Sambuc LV = CGF.MakeNaturalAlignAddrLValue(V, T);
1839f4a2713aSLionel Sambuc } else {
1840*0a6a1f1dSLionel Sambuc LV = CGF.MakeAddrLValue(V, T, Alignment);
1841f4a2713aSLionel Sambuc }
1842f4a2713aSLionel Sambuc setObjCGCLValueClass(CGF.getContext(), E, LV);
1843f4a2713aSLionel Sambuc return LV;
1844f4a2713aSLionel Sambuc }
1845f4a2713aSLionel Sambuc
EmitFunctionDeclLValue(CodeGenFunction & CGF,const Expr * E,const FunctionDecl * FD)1846f4a2713aSLionel Sambuc static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1847f4a2713aSLionel Sambuc const Expr *E, const FunctionDecl *FD) {
1848f4a2713aSLionel Sambuc llvm::Value *V = CGF.CGM.GetAddrOfFunction(FD);
1849f4a2713aSLionel Sambuc if (!FD->hasPrototype()) {
1850f4a2713aSLionel Sambuc if (const FunctionProtoType *Proto =
1851f4a2713aSLionel Sambuc FD->getType()->getAs<FunctionProtoType>()) {
1852f4a2713aSLionel Sambuc // Ugly case: for a K&R-style definition, the type of the definition
1853f4a2713aSLionel Sambuc // isn't the same as the type of a use. Correct for this with a
1854f4a2713aSLionel Sambuc // bitcast.
1855f4a2713aSLionel Sambuc QualType NoProtoType =
1856*0a6a1f1dSLionel Sambuc CGF.getContext().getFunctionNoProtoType(Proto->getReturnType());
1857f4a2713aSLionel Sambuc NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1858f4a2713aSLionel Sambuc V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType));
1859f4a2713aSLionel Sambuc }
1860f4a2713aSLionel Sambuc }
1861f4a2713aSLionel Sambuc CharUnits Alignment = CGF.getContext().getDeclAlign(FD);
1862f4a2713aSLionel Sambuc return CGF.MakeAddrLValue(V, E->getType(), Alignment);
1863f4a2713aSLionel Sambuc }
1864f4a2713aSLionel Sambuc
EmitCapturedFieldLValue(CodeGenFunction & CGF,const FieldDecl * FD,llvm::Value * ThisValue)1865f4a2713aSLionel Sambuc static LValue EmitCapturedFieldLValue(CodeGenFunction &CGF, const FieldDecl *FD,
1866f4a2713aSLionel Sambuc llvm::Value *ThisValue) {
1867f4a2713aSLionel Sambuc QualType TagType = CGF.getContext().getTagDeclType(FD->getParent());
1868f4a2713aSLionel Sambuc LValue LV = CGF.MakeNaturalAlignAddrLValue(ThisValue, TagType);
1869f4a2713aSLionel Sambuc return CGF.EmitLValueForField(LV, FD);
1870f4a2713aSLionel Sambuc }
1871f4a2713aSLionel Sambuc
1872*0a6a1f1dSLionel Sambuc /// Named Registers are named metadata pointing to the register name
1873*0a6a1f1dSLionel Sambuc /// which will be read from/written to as an argument to the intrinsic
1874*0a6a1f1dSLionel Sambuc /// @llvm.read/write_register.
1875*0a6a1f1dSLionel Sambuc /// So far, only the name is being passed down, but other options such as
1876*0a6a1f1dSLionel Sambuc /// register type, allocation type or even optimization options could be
1877*0a6a1f1dSLionel Sambuc /// passed down via the metadata node.
EmitGlobalNamedRegister(const VarDecl * VD,CodeGenModule & CGM,CharUnits Alignment)1878*0a6a1f1dSLionel Sambuc static LValue EmitGlobalNamedRegister(const VarDecl *VD,
1879*0a6a1f1dSLionel Sambuc CodeGenModule &CGM,
1880*0a6a1f1dSLionel Sambuc CharUnits Alignment) {
1881*0a6a1f1dSLionel Sambuc SmallString<64> Name("llvm.named.register.");
1882*0a6a1f1dSLionel Sambuc AsmLabelAttr *Asm = VD->getAttr<AsmLabelAttr>();
1883*0a6a1f1dSLionel Sambuc assert(Asm->getLabel().size() < 64-Name.size() &&
1884*0a6a1f1dSLionel Sambuc "Register name too big");
1885*0a6a1f1dSLionel Sambuc Name.append(Asm->getLabel());
1886*0a6a1f1dSLionel Sambuc llvm::NamedMDNode *M =
1887*0a6a1f1dSLionel Sambuc CGM.getModule().getOrInsertNamedMetadata(Name);
1888*0a6a1f1dSLionel Sambuc if (M->getNumOperands() == 0) {
1889*0a6a1f1dSLionel Sambuc llvm::MDString *Str = llvm::MDString::get(CGM.getLLVMContext(),
1890*0a6a1f1dSLionel Sambuc Asm->getLabel());
1891*0a6a1f1dSLionel Sambuc llvm::Metadata *Ops[] = {Str};
1892*0a6a1f1dSLionel Sambuc M->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
1893*0a6a1f1dSLionel Sambuc }
1894*0a6a1f1dSLionel Sambuc return LValue::MakeGlobalReg(
1895*0a6a1f1dSLionel Sambuc llvm::MetadataAsValue::get(CGM.getLLVMContext(), M->getOperand(0)),
1896*0a6a1f1dSLionel Sambuc VD->getType(), Alignment);
1897*0a6a1f1dSLionel Sambuc }
1898*0a6a1f1dSLionel Sambuc
EmitDeclRefLValue(const DeclRefExpr * E)1899f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
1900f4a2713aSLionel Sambuc const NamedDecl *ND = E->getDecl();
1901f4a2713aSLionel Sambuc CharUnits Alignment = getContext().getDeclAlign(ND);
1902f4a2713aSLionel Sambuc QualType T = E->getType();
1903f4a2713aSLionel Sambuc
1904*0a6a1f1dSLionel Sambuc if (const auto *VD = dyn_cast<VarDecl>(ND)) {
1905*0a6a1f1dSLionel Sambuc // Global Named registers access via intrinsics only
1906*0a6a1f1dSLionel Sambuc if (VD->getStorageClass() == SC_Register &&
1907*0a6a1f1dSLionel Sambuc VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())
1908*0a6a1f1dSLionel Sambuc return EmitGlobalNamedRegister(VD, CGM, Alignment);
1909*0a6a1f1dSLionel Sambuc
1910f4a2713aSLionel Sambuc // A DeclRefExpr for a reference initialized by a constant expression can
1911f4a2713aSLionel Sambuc // appear without being odr-used. Directly emit the constant initializer.
1912f4a2713aSLionel Sambuc const Expr *Init = VD->getAnyInitializer(VD);
1913f4a2713aSLionel Sambuc if (Init && !isa<ParmVarDecl>(VD) && VD->getType()->isReferenceType() &&
1914f4a2713aSLionel Sambuc VD->isUsableInConstantExpressions(getContext()) &&
1915f4a2713aSLionel Sambuc VD->checkInitIsICE()) {
1916f4a2713aSLionel Sambuc llvm::Constant *Val =
1917f4a2713aSLionel Sambuc CGM.EmitConstantValue(*VD->evaluateValue(), VD->getType(), this);
1918f4a2713aSLionel Sambuc assert(Val && "failed to emit reference constant expression");
1919f4a2713aSLionel Sambuc // FIXME: Eventually we will want to emit vector element references.
1920f4a2713aSLionel Sambuc return MakeAddrLValue(Val, T, Alignment);
1921f4a2713aSLionel Sambuc }
1922*0a6a1f1dSLionel Sambuc
1923*0a6a1f1dSLionel Sambuc // Check for captured variables.
1924*0a6a1f1dSLionel Sambuc if (E->refersToEnclosingVariableOrCapture()) {
1925*0a6a1f1dSLionel Sambuc if (auto *FD = LambdaCaptureFields.lookup(VD))
1926*0a6a1f1dSLionel Sambuc return EmitCapturedFieldLValue(*this, FD, CXXABIThisValue);
1927*0a6a1f1dSLionel Sambuc else if (CapturedStmtInfo) {
1928*0a6a1f1dSLionel Sambuc if (auto *V = LocalDeclMap.lookup(VD))
1929*0a6a1f1dSLionel Sambuc return MakeAddrLValue(V, T, Alignment);
1930*0a6a1f1dSLionel Sambuc else
1931*0a6a1f1dSLionel Sambuc return EmitCapturedFieldLValue(*this, CapturedStmtInfo->lookup(VD),
1932*0a6a1f1dSLionel Sambuc CapturedStmtInfo->getContextValue());
1933*0a6a1f1dSLionel Sambuc }
1934*0a6a1f1dSLionel Sambuc assert(isa<BlockDecl>(CurCodeDecl));
1935*0a6a1f1dSLionel Sambuc return MakeAddrLValue(GetAddrOfBlockDecl(VD, VD->hasAttr<BlocksAttr>()),
1936*0a6a1f1dSLionel Sambuc T, Alignment);
1937*0a6a1f1dSLionel Sambuc }
1938f4a2713aSLionel Sambuc }
1939f4a2713aSLionel Sambuc
1940f4a2713aSLionel Sambuc // FIXME: We should be able to assert this for FunctionDecls as well!
1941f4a2713aSLionel Sambuc // FIXME: We should be able to assert this for all DeclRefExprs, not just
1942f4a2713aSLionel Sambuc // those with a valid source location.
1943f4a2713aSLionel Sambuc assert((ND->isUsed(false) || !isa<VarDecl>(ND) ||
1944f4a2713aSLionel Sambuc !E->getLocation().isValid()) &&
1945f4a2713aSLionel Sambuc "Should not use decl without marking it used!");
1946f4a2713aSLionel Sambuc
1947f4a2713aSLionel Sambuc if (ND->hasAttr<WeakRefAttr>()) {
1948*0a6a1f1dSLionel Sambuc const auto *VD = cast<ValueDecl>(ND);
1949f4a2713aSLionel Sambuc llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1950f4a2713aSLionel Sambuc return MakeAddrLValue(Aliasee, T, Alignment);
1951f4a2713aSLionel Sambuc }
1952f4a2713aSLionel Sambuc
1953*0a6a1f1dSLionel Sambuc if (const auto *VD = dyn_cast<VarDecl>(ND)) {
1954f4a2713aSLionel Sambuc // Check if this is a global variable.
1955*0a6a1f1dSLionel Sambuc if (VD->hasLinkage() || VD->isStaticDataMember())
1956f4a2713aSLionel Sambuc return EmitGlobalVarDeclLValue(*this, E, VD);
1957f4a2713aSLionel Sambuc
1958f4a2713aSLionel Sambuc bool isBlockVariable = VD->hasAttr<BlocksAttr>();
1959f4a2713aSLionel Sambuc
1960f4a2713aSLionel Sambuc llvm::Value *V = LocalDeclMap.lookup(VD);
1961f4a2713aSLionel Sambuc if (!V && VD->isStaticLocal())
1962*0a6a1f1dSLionel Sambuc V = CGM.getOrCreateStaticVarDecl(
1963*0a6a1f1dSLionel Sambuc *VD, CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false));
1964f4a2713aSLionel Sambuc
1965*0a6a1f1dSLionel Sambuc // Check if variable is threadprivate.
1966*0a6a1f1dSLionel Sambuc if (V && getLangOpts().OpenMP && VD->hasAttr<OMPThreadPrivateDeclAttr>())
1967*0a6a1f1dSLionel Sambuc return EmitThreadPrivateVarDeclLValue(
1968*0a6a1f1dSLionel Sambuc *this, VD, T, V, getTypes().ConvertTypeForMem(VD->getType()),
1969*0a6a1f1dSLionel Sambuc Alignment, E->getExprLoc());
1970f4a2713aSLionel Sambuc
1971f4a2713aSLionel Sambuc assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1972f4a2713aSLionel Sambuc
1973f4a2713aSLionel Sambuc if (isBlockVariable)
1974f4a2713aSLionel Sambuc V = BuildBlockByrefAddress(V, VD);
1975f4a2713aSLionel Sambuc
1976f4a2713aSLionel Sambuc LValue LV;
1977f4a2713aSLionel Sambuc if (VD->getType()->isReferenceType()) {
1978f4a2713aSLionel Sambuc llvm::LoadInst *LI = Builder.CreateLoad(V);
1979f4a2713aSLionel Sambuc LI->setAlignment(Alignment.getQuantity());
1980f4a2713aSLionel Sambuc V = LI;
1981f4a2713aSLionel Sambuc LV = MakeNaturalAlignAddrLValue(V, T);
1982f4a2713aSLionel Sambuc } else {
1983f4a2713aSLionel Sambuc LV = MakeAddrLValue(V, T, Alignment);
1984f4a2713aSLionel Sambuc }
1985f4a2713aSLionel Sambuc
1986f4a2713aSLionel Sambuc bool isLocalStorage = VD->hasLocalStorage();
1987f4a2713aSLionel Sambuc
1988f4a2713aSLionel Sambuc bool NonGCable = isLocalStorage &&
1989f4a2713aSLionel Sambuc !VD->getType()->isReferenceType() &&
1990f4a2713aSLionel Sambuc !isBlockVariable;
1991f4a2713aSLionel Sambuc if (NonGCable) {
1992f4a2713aSLionel Sambuc LV.getQuals().removeObjCGCAttr();
1993f4a2713aSLionel Sambuc LV.setNonGC(true);
1994f4a2713aSLionel Sambuc }
1995f4a2713aSLionel Sambuc
1996f4a2713aSLionel Sambuc bool isImpreciseLifetime =
1997f4a2713aSLionel Sambuc (isLocalStorage && !VD->hasAttr<ObjCPreciseLifetimeAttr>());
1998f4a2713aSLionel Sambuc if (isImpreciseLifetime)
1999f4a2713aSLionel Sambuc LV.setARCPreciseLifetime(ARCImpreciseLifetime);
2000f4a2713aSLionel Sambuc setObjCGCLValueClass(getContext(), E, LV);
2001f4a2713aSLionel Sambuc return LV;
2002f4a2713aSLionel Sambuc }
2003f4a2713aSLionel Sambuc
2004*0a6a1f1dSLionel Sambuc if (const auto *FD = dyn_cast<FunctionDecl>(ND))
2005f4a2713aSLionel Sambuc return EmitFunctionDeclLValue(*this, E, FD);
2006f4a2713aSLionel Sambuc
2007f4a2713aSLionel Sambuc llvm_unreachable("Unhandled DeclRefExpr");
2008f4a2713aSLionel Sambuc }
2009f4a2713aSLionel Sambuc
EmitUnaryOpLValue(const UnaryOperator * E)2010f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
2011f4a2713aSLionel Sambuc // __extension__ doesn't affect lvalue-ness.
2012f4a2713aSLionel Sambuc if (E->getOpcode() == UO_Extension)
2013f4a2713aSLionel Sambuc return EmitLValue(E->getSubExpr());
2014f4a2713aSLionel Sambuc
2015f4a2713aSLionel Sambuc QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
2016f4a2713aSLionel Sambuc switch (E->getOpcode()) {
2017f4a2713aSLionel Sambuc default: llvm_unreachable("Unknown unary operator lvalue!");
2018f4a2713aSLionel Sambuc case UO_Deref: {
2019f4a2713aSLionel Sambuc QualType T = E->getSubExpr()->getType()->getPointeeType();
2020f4a2713aSLionel Sambuc assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
2021f4a2713aSLionel Sambuc
2022f4a2713aSLionel Sambuc LValue LV = MakeNaturalAlignAddrLValue(EmitScalarExpr(E->getSubExpr()), T);
2023f4a2713aSLionel Sambuc LV.getQuals().setAddressSpace(ExprTy.getAddressSpace());
2024f4a2713aSLionel Sambuc
2025f4a2713aSLionel Sambuc // We should not generate __weak write barrier on indirect reference
2026f4a2713aSLionel Sambuc // of a pointer to object; as in void foo (__weak id *param); *param = 0;
2027f4a2713aSLionel Sambuc // But, we continue to generate __strong write barrier on indirect write
2028f4a2713aSLionel Sambuc // into a pointer to object.
2029f4a2713aSLionel Sambuc if (getLangOpts().ObjC1 &&
2030f4a2713aSLionel Sambuc getLangOpts().getGC() != LangOptions::NonGC &&
2031f4a2713aSLionel Sambuc LV.isObjCWeak())
2032f4a2713aSLionel Sambuc LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
2033f4a2713aSLionel Sambuc return LV;
2034f4a2713aSLionel Sambuc }
2035f4a2713aSLionel Sambuc case UO_Real:
2036f4a2713aSLionel Sambuc case UO_Imag: {
2037f4a2713aSLionel Sambuc LValue LV = EmitLValue(E->getSubExpr());
2038f4a2713aSLionel Sambuc assert(LV.isSimple() && "real/imag on non-ordinary l-value");
2039f4a2713aSLionel Sambuc llvm::Value *Addr = LV.getAddress();
2040f4a2713aSLionel Sambuc
2041f4a2713aSLionel Sambuc // __real is valid on scalars. This is a faster way of testing that.
2042f4a2713aSLionel Sambuc // __imag can only produce an rvalue on scalars.
2043f4a2713aSLionel Sambuc if (E->getOpcode() == UO_Real &&
2044f4a2713aSLionel Sambuc !cast<llvm::PointerType>(Addr->getType())
2045f4a2713aSLionel Sambuc ->getElementType()->isStructTy()) {
2046f4a2713aSLionel Sambuc assert(E->getSubExpr()->getType()->isArithmeticType());
2047f4a2713aSLionel Sambuc return LV;
2048f4a2713aSLionel Sambuc }
2049f4a2713aSLionel Sambuc
2050f4a2713aSLionel Sambuc assert(E->getSubExpr()->getType()->isAnyComplexType());
2051f4a2713aSLionel Sambuc
2052f4a2713aSLionel Sambuc unsigned Idx = E->getOpcode() == UO_Imag;
2053f4a2713aSLionel Sambuc return MakeAddrLValue(Builder.CreateStructGEP(LV.getAddress(),
2054f4a2713aSLionel Sambuc Idx, "idx"),
2055f4a2713aSLionel Sambuc ExprTy);
2056f4a2713aSLionel Sambuc }
2057f4a2713aSLionel Sambuc case UO_PreInc:
2058f4a2713aSLionel Sambuc case UO_PreDec: {
2059f4a2713aSLionel Sambuc LValue LV = EmitLValue(E->getSubExpr());
2060f4a2713aSLionel Sambuc bool isInc = E->getOpcode() == UO_PreInc;
2061f4a2713aSLionel Sambuc
2062f4a2713aSLionel Sambuc if (E->getType()->isAnyComplexType())
2063f4a2713aSLionel Sambuc EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
2064f4a2713aSLionel Sambuc else
2065f4a2713aSLionel Sambuc EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
2066f4a2713aSLionel Sambuc return LV;
2067f4a2713aSLionel Sambuc }
2068f4a2713aSLionel Sambuc }
2069f4a2713aSLionel Sambuc }
2070f4a2713aSLionel Sambuc
EmitStringLiteralLValue(const StringLiteral * E)2071f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
2072f4a2713aSLionel Sambuc return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E),
2073f4a2713aSLionel Sambuc E->getType());
2074f4a2713aSLionel Sambuc }
2075f4a2713aSLionel Sambuc
EmitObjCEncodeExprLValue(const ObjCEncodeExpr * E)2076f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
2077f4a2713aSLionel Sambuc return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E),
2078f4a2713aSLionel Sambuc E->getType());
2079f4a2713aSLionel Sambuc }
2080f4a2713aSLionel Sambuc
EmitPredefinedLValue(const PredefinedExpr * E)2081f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
2082*0a6a1f1dSLionel Sambuc auto SL = E->getFunctionName();
2083*0a6a1f1dSLionel Sambuc assert(SL != nullptr && "No StringLiteral name in PredefinedExpr");
2084f4a2713aSLionel Sambuc StringRef FnName = CurFn->getName();
2085f4a2713aSLionel Sambuc if (FnName.startswith("\01"))
2086f4a2713aSLionel Sambuc FnName = FnName.substr(1);
2087*0a6a1f1dSLionel Sambuc StringRef NameItems[] = {
2088*0a6a1f1dSLionel Sambuc PredefinedExpr::getIdentTypeName(E->getIdentType()), FnName};
2089*0a6a1f1dSLionel Sambuc std::string GVName = llvm::join(NameItems, NameItems + 2, ".");
2090*0a6a1f1dSLionel Sambuc if (CurCodeDecl && isa<BlockDecl>(CurCodeDecl)) {
2091*0a6a1f1dSLionel Sambuc auto C = CGM.GetAddrOfConstantCString(FnName, GVName.c_str(), 1);
2092f4a2713aSLionel Sambuc return MakeAddrLValue(C, E->getType());
2093f4a2713aSLionel Sambuc }
2094*0a6a1f1dSLionel Sambuc auto C = CGM.GetAddrOfConstantStringFromLiteral(SL, GVName);
2095*0a6a1f1dSLionel Sambuc return MakeAddrLValue(C, E->getType());
2096f4a2713aSLionel Sambuc }
2097f4a2713aSLionel Sambuc
2098f4a2713aSLionel Sambuc /// Emit a type description suitable for use by a runtime sanitizer library. The
2099f4a2713aSLionel Sambuc /// format of a type descriptor is
2100f4a2713aSLionel Sambuc ///
2101f4a2713aSLionel Sambuc /// \code
2102f4a2713aSLionel Sambuc /// { i16 TypeKind, i16 TypeInfo }
2103f4a2713aSLionel Sambuc /// \endcode
2104f4a2713aSLionel Sambuc ///
2105f4a2713aSLionel Sambuc /// followed by an array of i8 containing the type name. TypeKind is 0 for an
2106f4a2713aSLionel Sambuc /// integer, 1 for a floating point value, and -1 for anything else.
EmitCheckTypeDescriptor(QualType T)2107f4a2713aSLionel Sambuc llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
2108f4a2713aSLionel Sambuc // Only emit each type's descriptor once.
2109*0a6a1f1dSLionel Sambuc if (llvm::Constant *C = CGM.getTypeDescriptorFromMap(T))
2110f4a2713aSLionel Sambuc return C;
2111f4a2713aSLionel Sambuc
2112f4a2713aSLionel Sambuc uint16_t TypeKind = -1;
2113f4a2713aSLionel Sambuc uint16_t TypeInfo = 0;
2114f4a2713aSLionel Sambuc
2115f4a2713aSLionel Sambuc if (T->isIntegerType()) {
2116f4a2713aSLionel Sambuc TypeKind = 0;
2117f4a2713aSLionel Sambuc TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) |
2118f4a2713aSLionel Sambuc (T->isSignedIntegerType() ? 1 : 0);
2119f4a2713aSLionel Sambuc } else if (T->isFloatingType()) {
2120f4a2713aSLionel Sambuc TypeKind = 1;
2121f4a2713aSLionel Sambuc TypeInfo = getContext().getTypeSize(T);
2122f4a2713aSLionel Sambuc }
2123f4a2713aSLionel Sambuc
2124f4a2713aSLionel Sambuc // Format the type name as if for a diagnostic, including quotes and
2125f4a2713aSLionel Sambuc // optionally an 'aka'.
2126f4a2713aSLionel Sambuc SmallString<32> Buffer;
2127f4a2713aSLionel Sambuc CGM.getDiags().ConvertArgToString(DiagnosticsEngine::ak_qualtype,
2128f4a2713aSLionel Sambuc (intptr_t)T.getAsOpaquePtr(),
2129*0a6a1f1dSLionel Sambuc StringRef(), StringRef(), None, Buffer,
2130*0a6a1f1dSLionel Sambuc None);
2131f4a2713aSLionel Sambuc
2132f4a2713aSLionel Sambuc llvm::Constant *Components[] = {
2133f4a2713aSLionel Sambuc Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo),
2134f4a2713aSLionel Sambuc llvm::ConstantDataArray::getString(getLLVMContext(), Buffer)
2135f4a2713aSLionel Sambuc };
2136f4a2713aSLionel Sambuc llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components);
2137f4a2713aSLionel Sambuc
2138*0a6a1f1dSLionel Sambuc auto *GV = new llvm::GlobalVariable(
2139*0a6a1f1dSLionel Sambuc CGM.getModule(), Descriptor->getType(),
2140*0a6a1f1dSLionel Sambuc /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, Descriptor);
2141f4a2713aSLionel Sambuc GV->setUnnamedAddr(true);
2142*0a6a1f1dSLionel Sambuc CGM.getSanitizerMetadata()->disableSanitizerForGlobal(GV);
2143f4a2713aSLionel Sambuc
2144f4a2713aSLionel Sambuc // Remember the descriptor for this type.
2145*0a6a1f1dSLionel Sambuc CGM.setTypeDescriptorInMap(T, GV);
2146f4a2713aSLionel Sambuc
2147f4a2713aSLionel Sambuc return GV;
2148f4a2713aSLionel Sambuc }
2149f4a2713aSLionel Sambuc
EmitCheckValue(llvm::Value * V)2150f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) {
2151f4a2713aSLionel Sambuc llvm::Type *TargetTy = IntPtrTy;
2152f4a2713aSLionel Sambuc
2153f4a2713aSLionel Sambuc // Floating-point types which fit into intptr_t are bitcast to integers
2154f4a2713aSLionel Sambuc // and then passed directly (after zero-extension, if necessary).
2155f4a2713aSLionel Sambuc if (V->getType()->isFloatingPointTy()) {
2156f4a2713aSLionel Sambuc unsigned Bits = V->getType()->getPrimitiveSizeInBits();
2157f4a2713aSLionel Sambuc if (Bits <= TargetTy->getIntegerBitWidth())
2158f4a2713aSLionel Sambuc V = Builder.CreateBitCast(V, llvm::Type::getIntNTy(getLLVMContext(),
2159f4a2713aSLionel Sambuc Bits));
2160f4a2713aSLionel Sambuc }
2161f4a2713aSLionel Sambuc
2162f4a2713aSLionel Sambuc // Integers which fit in intptr_t are zero-extended and passed directly.
2163f4a2713aSLionel Sambuc if (V->getType()->isIntegerTy() &&
2164f4a2713aSLionel Sambuc V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth())
2165f4a2713aSLionel Sambuc return Builder.CreateZExt(V, TargetTy);
2166f4a2713aSLionel Sambuc
2167f4a2713aSLionel Sambuc // Pointers are passed directly, everything else is passed by address.
2168f4a2713aSLionel Sambuc if (!V->getType()->isPointerTy()) {
2169f4a2713aSLionel Sambuc llvm::Value *Ptr = CreateTempAlloca(V->getType());
2170f4a2713aSLionel Sambuc Builder.CreateStore(V, Ptr);
2171f4a2713aSLionel Sambuc V = Ptr;
2172f4a2713aSLionel Sambuc }
2173f4a2713aSLionel Sambuc return Builder.CreatePtrToInt(V, TargetTy);
2174f4a2713aSLionel Sambuc }
2175f4a2713aSLionel Sambuc
2176f4a2713aSLionel Sambuc /// \brief Emit a representation of a SourceLocation for passing to a handler
2177f4a2713aSLionel Sambuc /// in a sanitizer runtime library. The format for this data is:
2178f4a2713aSLionel Sambuc /// \code
2179f4a2713aSLionel Sambuc /// struct SourceLocation {
2180f4a2713aSLionel Sambuc /// const char *Filename;
2181f4a2713aSLionel Sambuc /// int32_t Line, Column;
2182f4a2713aSLionel Sambuc /// };
2183f4a2713aSLionel Sambuc /// \endcode
2184f4a2713aSLionel Sambuc /// For an invalid SourceLocation, the Filename pointer is null.
EmitCheckSourceLocation(SourceLocation Loc)2185f4a2713aSLionel Sambuc llvm::Constant *CodeGenFunction::EmitCheckSourceLocation(SourceLocation Loc) {
2186*0a6a1f1dSLionel Sambuc llvm::Constant *Filename;
2187*0a6a1f1dSLionel Sambuc int Line, Column;
2188f4a2713aSLionel Sambuc
2189*0a6a1f1dSLionel Sambuc PresumedLoc PLoc = getContext().getSourceManager().getPresumedLoc(Loc);
2190*0a6a1f1dSLionel Sambuc if (PLoc.isValid()) {
2191*0a6a1f1dSLionel Sambuc auto FilenameGV = CGM.GetAddrOfConstantCString(PLoc.getFilename(), ".src");
2192*0a6a1f1dSLionel Sambuc CGM.getSanitizerMetadata()->disableSanitizerForGlobal(FilenameGV);
2193*0a6a1f1dSLionel Sambuc Filename = FilenameGV;
2194*0a6a1f1dSLionel Sambuc Line = PLoc.getLine();
2195*0a6a1f1dSLionel Sambuc Column = PLoc.getColumn();
2196*0a6a1f1dSLionel Sambuc } else {
2197*0a6a1f1dSLionel Sambuc Filename = llvm::Constant::getNullValue(Int8PtrTy);
2198*0a6a1f1dSLionel Sambuc Line = Column = 0;
2199*0a6a1f1dSLionel Sambuc }
2200*0a6a1f1dSLionel Sambuc
2201*0a6a1f1dSLionel Sambuc llvm::Constant *Data[] = {Filename, Builder.getInt32(Line),
2202*0a6a1f1dSLionel Sambuc Builder.getInt32(Column)};
2203f4a2713aSLionel Sambuc
2204f4a2713aSLionel Sambuc return llvm::ConstantStruct::getAnon(Data);
2205f4a2713aSLionel Sambuc }
2206f4a2713aSLionel Sambuc
2207*0a6a1f1dSLionel Sambuc namespace {
2208*0a6a1f1dSLionel Sambuc /// \brief Specify under what conditions this check can be recovered
2209*0a6a1f1dSLionel Sambuc enum class CheckRecoverableKind {
2210*0a6a1f1dSLionel Sambuc /// Always terminate program execution if this check fails.
2211*0a6a1f1dSLionel Sambuc Unrecoverable,
2212*0a6a1f1dSLionel Sambuc /// Check supports recovering, runtime has both fatal (noreturn) and
2213*0a6a1f1dSLionel Sambuc /// non-fatal handlers for this check.
2214*0a6a1f1dSLionel Sambuc Recoverable,
2215*0a6a1f1dSLionel Sambuc /// Runtime conditionally aborts, always need to support recovery.
2216*0a6a1f1dSLionel Sambuc AlwaysRecoverable
2217*0a6a1f1dSLionel Sambuc };
2218*0a6a1f1dSLionel Sambuc }
2219*0a6a1f1dSLionel Sambuc
getRecoverableKind(SanitizerKind Kind)2220*0a6a1f1dSLionel Sambuc static CheckRecoverableKind getRecoverableKind(SanitizerKind Kind) {
2221*0a6a1f1dSLionel Sambuc switch (Kind) {
2222*0a6a1f1dSLionel Sambuc case SanitizerKind::Vptr:
2223*0a6a1f1dSLionel Sambuc return CheckRecoverableKind::AlwaysRecoverable;
2224*0a6a1f1dSLionel Sambuc case SanitizerKind::Return:
2225*0a6a1f1dSLionel Sambuc case SanitizerKind::Unreachable:
2226*0a6a1f1dSLionel Sambuc return CheckRecoverableKind::Unrecoverable;
2227*0a6a1f1dSLionel Sambuc default:
2228*0a6a1f1dSLionel Sambuc return CheckRecoverableKind::Recoverable;
2229*0a6a1f1dSLionel Sambuc }
2230*0a6a1f1dSLionel Sambuc }
2231*0a6a1f1dSLionel Sambuc
emitCheckHandlerCall(CodeGenFunction & CGF,llvm::FunctionType * FnType,ArrayRef<llvm::Value * > FnArgs,StringRef CheckName,CheckRecoverableKind RecoverKind,bool IsFatal,llvm::BasicBlock * ContBB)2232*0a6a1f1dSLionel Sambuc static void emitCheckHandlerCall(CodeGenFunction &CGF,
2233*0a6a1f1dSLionel Sambuc llvm::FunctionType *FnType,
2234*0a6a1f1dSLionel Sambuc ArrayRef<llvm::Value *> FnArgs,
2235*0a6a1f1dSLionel Sambuc StringRef CheckName,
2236*0a6a1f1dSLionel Sambuc CheckRecoverableKind RecoverKind, bool IsFatal,
2237*0a6a1f1dSLionel Sambuc llvm::BasicBlock *ContBB) {
2238*0a6a1f1dSLionel Sambuc assert(IsFatal || RecoverKind != CheckRecoverableKind::Unrecoverable);
2239*0a6a1f1dSLionel Sambuc bool NeedsAbortSuffix =
2240*0a6a1f1dSLionel Sambuc IsFatal && RecoverKind != CheckRecoverableKind::Unrecoverable;
2241*0a6a1f1dSLionel Sambuc std::string FnName = ("__ubsan_handle_" + CheckName +
2242*0a6a1f1dSLionel Sambuc (NeedsAbortSuffix ? "_abort" : "")).str();
2243*0a6a1f1dSLionel Sambuc bool MayReturn =
2244*0a6a1f1dSLionel Sambuc !IsFatal || RecoverKind == CheckRecoverableKind::AlwaysRecoverable;
2245*0a6a1f1dSLionel Sambuc
2246*0a6a1f1dSLionel Sambuc llvm::AttrBuilder B;
2247*0a6a1f1dSLionel Sambuc if (!MayReturn) {
2248*0a6a1f1dSLionel Sambuc B.addAttribute(llvm::Attribute::NoReturn)
2249*0a6a1f1dSLionel Sambuc .addAttribute(llvm::Attribute::NoUnwind);
2250*0a6a1f1dSLionel Sambuc }
2251*0a6a1f1dSLionel Sambuc B.addAttribute(llvm::Attribute::UWTable);
2252*0a6a1f1dSLionel Sambuc
2253*0a6a1f1dSLionel Sambuc llvm::Value *Fn = CGF.CGM.CreateRuntimeFunction(
2254*0a6a1f1dSLionel Sambuc FnType, FnName,
2255*0a6a1f1dSLionel Sambuc llvm::AttributeSet::get(CGF.getLLVMContext(),
2256*0a6a1f1dSLionel Sambuc llvm::AttributeSet::FunctionIndex, B));
2257*0a6a1f1dSLionel Sambuc llvm::CallInst *HandlerCall = CGF.EmitNounwindRuntimeCall(Fn, FnArgs);
2258*0a6a1f1dSLionel Sambuc if (!MayReturn) {
2259*0a6a1f1dSLionel Sambuc HandlerCall->setDoesNotReturn();
2260*0a6a1f1dSLionel Sambuc CGF.Builder.CreateUnreachable();
2261*0a6a1f1dSLionel Sambuc } else {
2262*0a6a1f1dSLionel Sambuc CGF.Builder.CreateBr(ContBB);
2263*0a6a1f1dSLionel Sambuc }
2264*0a6a1f1dSLionel Sambuc }
2265*0a6a1f1dSLionel Sambuc
EmitCheck(ArrayRef<std::pair<llvm::Value *,SanitizerKind>> Checked,StringRef CheckName,ArrayRef<llvm::Constant * > StaticArgs,ArrayRef<llvm::Value * > DynamicArgs)2266*0a6a1f1dSLionel Sambuc void CodeGenFunction::EmitCheck(
2267*0a6a1f1dSLionel Sambuc ArrayRef<std::pair<llvm::Value *, SanitizerKind>> Checked,
2268*0a6a1f1dSLionel Sambuc StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs,
2269*0a6a1f1dSLionel Sambuc ArrayRef<llvm::Value *> DynamicArgs) {
2270*0a6a1f1dSLionel Sambuc assert(IsSanitizerScope);
2271*0a6a1f1dSLionel Sambuc assert(Checked.size() > 0);
2272*0a6a1f1dSLionel Sambuc
2273*0a6a1f1dSLionel Sambuc llvm::Value *FatalCond = nullptr;
2274*0a6a1f1dSLionel Sambuc llvm::Value *RecoverableCond = nullptr;
2275*0a6a1f1dSLionel Sambuc for (int i = 0, n = Checked.size(); i < n; ++i) {
2276*0a6a1f1dSLionel Sambuc llvm::Value *Check = Checked[i].first;
2277*0a6a1f1dSLionel Sambuc llvm::Value *&Cond =
2278*0a6a1f1dSLionel Sambuc CGM.getCodeGenOpts().SanitizeRecover.has(Checked[i].second)
2279*0a6a1f1dSLionel Sambuc ? RecoverableCond
2280*0a6a1f1dSLionel Sambuc : FatalCond;
2281*0a6a1f1dSLionel Sambuc Cond = Cond ? Builder.CreateAnd(Cond, Check) : Check;
2282*0a6a1f1dSLionel Sambuc }
2283*0a6a1f1dSLionel Sambuc
2284*0a6a1f1dSLionel Sambuc llvm::Value *JointCond;
2285*0a6a1f1dSLionel Sambuc if (FatalCond && RecoverableCond)
2286*0a6a1f1dSLionel Sambuc JointCond = Builder.CreateAnd(FatalCond, RecoverableCond);
2287*0a6a1f1dSLionel Sambuc else
2288*0a6a1f1dSLionel Sambuc JointCond = FatalCond ? FatalCond : RecoverableCond;
2289*0a6a1f1dSLionel Sambuc assert(JointCond);
2290*0a6a1f1dSLionel Sambuc
2291*0a6a1f1dSLionel Sambuc CheckRecoverableKind RecoverKind = getRecoverableKind(Checked[0].second);
2292*0a6a1f1dSLionel Sambuc assert(SanOpts.has(Checked[0].second));
2293*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
2294*0a6a1f1dSLionel Sambuc for (int i = 1, n = Checked.size(); i < n; ++i) {
2295*0a6a1f1dSLionel Sambuc assert(RecoverKind == getRecoverableKind(Checked[i].second) &&
2296*0a6a1f1dSLionel Sambuc "All recoverable kinds in a single check must be same!");
2297*0a6a1f1dSLionel Sambuc assert(SanOpts.has(Checked[i].second));
2298*0a6a1f1dSLionel Sambuc }
2299*0a6a1f1dSLionel Sambuc #endif
2300f4a2713aSLionel Sambuc
2301f4a2713aSLionel Sambuc if (CGM.getCodeGenOpts().SanitizeUndefinedTrapOnError) {
2302*0a6a1f1dSLionel Sambuc assert(RecoverKind != CheckRecoverableKind::AlwaysRecoverable &&
2303f4a2713aSLionel Sambuc "Runtime call required for AlwaysRecoverable kind!");
2304*0a6a1f1dSLionel Sambuc // Assume that -fsanitize-undefined-trap-on-error overrides
2305*0a6a1f1dSLionel Sambuc // -fsanitize-recover= options, as we can only print meaningful error
2306*0a6a1f1dSLionel Sambuc // message and recover if we have a runtime support.
2307*0a6a1f1dSLionel Sambuc return EmitTrapCheck(JointCond);
2308f4a2713aSLionel Sambuc }
2309f4a2713aSLionel Sambuc
2310f4a2713aSLionel Sambuc llvm::BasicBlock *Cont = createBasicBlock("cont");
2311*0a6a1f1dSLionel Sambuc llvm::BasicBlock *Handlers = createBasicBlock("handler." + CheckName);
2312*0a6a1f1dSLionel Sambuc llvm::Instruction *Branch = Builder.CreateCondBr(JointCond, Cont, Handlers);
2313f4a2713aSLionel Sambuc // Give hint that we very much don't expect to execute the handler
2314f4a2713aSLionel Sambuc // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
2315f4a2713aSLionel Sambuc llvm::MDBuilder MDHelper(getLLVMContext());
2316f4a2713aSLionel Sambuc llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1);
2317f4a2713aSLionel Sambuc Branch->setMetadata(llvm::LLVMContext::MD_prof, Node);
2318*0a6a1f1dSLionel Sambuc EmitBlock(Handlers);
2319f4a2713aSLionel Sambuc
2320*0a6a1f1dSLionel Sambuc // Emit handler arguments and create handler function type.
2321f4a2713aSLionel Sambuc llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
2322*0a6a1f1dSLionel Sambuc auto *InfoPtr =
2323f4a2713aSLionel Sambuc new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false,
2324f4a2713aSLionel Sambuc llvm::GlobalVariable::PrivateLinkage, Info);
2325f4a2713aSLionel Sambuc InfoPtr->setUnnamedAddr(true);
2326*0a6a1f1dSLionel Sambuc CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr);
2327f4a2713aSLionel Sambuc
2328f4a2713aSLionel Sambuc SmallVector<llvm::Value *, 4> Args;
2329f4a2713aSLionel Sambuc SmallVector<llvm::Type *, 4> ArgTypes;
2330f4a2713aSLionel Sambuc Args.reserve(DynamicArgs.size() + 1);
2331f4a2713aSLionel Sambuc ArgTypes.reserve(DynamicArgs.size() + 1);
2332f4a2713aSLionel Sambuc
2333f4a2713aSLionel Sambuc // Handler functions take an i8* pointing to the (handler-specific) static
2334f4a2713aSLionel Sambuc // information block, followed by a sequence of intptr_t arguments
2335f4a2713aSLionel Sambuc // representing operand values.
2336f4a2713aSLionel Sambuc Args.push_back(Builder.CreateBitCast(InfoPtr, Int8PtrTy));
2337f4a2713aSLionel Sambuc ArgTypes.push_back(Int8PtrTy);
2338f4a2713aSLionel Sambuc for (size_t i = 0, n = DynamicArgs.size(); i != n; ++i) {
2339f4a2713aSLionel Sambuc Args.push_back(EmitCheckValue(DynamicArgs[i]));
2340f4a2713aSLionel Sambuc ArgTypes.push_back(IntPtrTy);
2341f4a2713aSLionel Sambuc }
2342f4a2713aSLionel Sambuc
2343f4a2713aSLionel Sambuc llvm::FunctionType *FnType =
2344f4a2713aSLionel Sambuc llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false);
2345f4a2713aSLionel Sambuc
2346*0a6a1f1dSLionel Sambuc if (!FatalCond || !RecoverableCond) {
2347*0a6a1f1dSLionel Sambuc // Simple case: we need to generate a single handler call, either
2348*0a6a1f1dSLionel Sambuc // fatal, or non-fatal.
2349*0a6a1f1dSLionel Sambuc emitCheckHandlerCall(*this, FnType, Args, CheckName, RecoverKind,
2350*0a6a1f1dSLionel Sambuc (FatalCond != nullptr), Cont);
2351f4a2713aSLionel Sambuc } else {
2352*0a6a1f1dSLionel Sambuc // Emit two handler calls: first one for set of unrecoverable checks,
2353*0a6a1f1dSLionel Sambuc // another one for recoverable.
2354*0a6a1f1dSLionel Sambuc llvm::BasicBlock *NonFatalHandlerBB =
2355*0a6a1f1dSLionel Sambuc createBasicBlock("non_fatal." + CheckName);
2356*0a6a1f1dSLionel Sambuc llvm::BasicBlock *FatalHandlerBB = createBasicBlock("fatal." + CheckName);
2357*0a6a1f1dSLionel Sambuc Builder.CreateCondBr(FatalCond, NonFatalHandlerBB, FatalHandlerBB);
2358*0a6a1f1dSLionel Sambuc EmitBlock(FatalHandlerBB);
2359*0a6a1f1dSLionel Sambuc emitCheckHandlerCall(*this, FnType, Args, CheckName, RecoverKind, true,
2360*0a6a1f1dSLionel Sambuc NonFatalHandlerBB);
2361*0a6a1f1dSLionel Sambuc EmitBlock(NonFatalHandlerBB);
2362*0a6a1f1dSLionel Sambuc emitCheckHandlerCall(*this, FnType, Args, CheckName, RecoverKind, false,
2363*0a6a1f1dSLionel Sambuc Cont);
2364f4a2713aSLionel Sambuc }
2365f4a2713aSLionel Sambuc
2366f4a2713aSLionel Sambuc EmitBlock(Cont);
2367f4a2713aSLionel Sambuc }
2368f4a2713aSLionel Sambuc
EmitTrapCheck(llvm::Value * Checked)2369f4a2713aSLionel Sambuc void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked) {
2370f4a2713aSLionel Sambuc llvm::BasicBlock *Cont = createBasicBlock("cont");
2371f4a2713aSLionel Sambuc
2372f4a2713aSLionel Sambuc // If we're optimizing, collapse all calls to trap down to just one per
2373f4a2713aSLionel Sambuc // function to save on code size.
2374f4a2713aSLionel Sambuc if (!CGM.getCodeGenOpts().OptimizationLevel || !TrapBB) {
2375f4a2713aSLionel Sambuc TrapBB = createBasicBlock("trap");
2376f4a2713aSLionel Sambuc Builder.CreateCondBr(Checked, Cont, TrapBB);
2377f4a2713aSLionel Sambuc EmitBlock(TrapBB);
2378f4a2713aSLionel Sambuc llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap);
2379f4a2713aSLionel Sambuc llvm::CallInst *TrapCall = Builder.CreateCall(F);
2380f4a2713aSLionel Sambuc TrapCall->setDoesNotReturn();
2381f4a2713aSLionel Sambuc TrapCall->setDoesNotThrow();
2382f4a2713aSLionel Sambuc Builder.CreateUnreachable();
2383f4a2713aSLionel Sambuc } else {
2384f4a2713aSLionel Sambuc Builder.CreateCondBr(Checked, Cont, TrapBB);
2385f4a2713aSLionel Sambuc }
2386f4a2713aSLionel Sambuc
2387f4a2713aSLionel Sambuc EmitBlock(Cont);
2388f4a2713aSLionel Sambuc }
2389f4a2713aSLionel Sambuc
2390f4a2713aSLionel Sambuc /// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
2391f4a2713aSLionel Sambuc /// array to pointer, return the array subexpression.
isSimpleArrayDecayOperand(const Expr * E)2392f4a2713aSLionel Sambuc static const Expr *isSimpleArrayDecayOperand(const Expr *E) {
2393f4a2713aSLionel Sambuc // If this isn't just an array->pointer decay, bail out.
2394*0a6a1f1dSLionel Sambuc const auto *CE = dyn_cast<CastExpr>(E);
2395*0a6a1f1dSLionel Sambuc if (!CE || CE->getCastKind() != CK_ArrayToPointerDecay)
2396*0a6a1f1dSLionel Sambuc return nullptr;
2397f4a2713aSLionel Sambuc
2398f4a2713aSLionel Sambuc // If this is a decay from variable width array, bail out.
2399f4a2713aSLionel Sambuc const Expr *SubExpr = CE->getSubExpr();
2400f4a2713aSLionel Sambuc if (SubExpr->getType()->isVariableArrayType())
2401*0a6a1f1dSLionel Sambuc return nullptr;
2402f4a2713aSLionel Sambuc
2403f4a2713aSLionel Sambuc return SubExpr;
2404f4a2713aSLionel Sambuc }
2405f4a2713aSLionel Sambuc
EmitArraySubscriptExpr(const ArraySubscriptExpr * E,bool Accessed)2406f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
2407f4a2713aSLionel Sambuc bool Accessed) {
2408f4a2713aSLionel Sambuc // The index must always be an integer, which is not an aggregate. Emit it.
2409f4a2713aSLionel Sambuc llvm::Value *Idx = EmitScalarExpr(E->getIdx());
2410f4a2713aSLionel Sambuc QualType IdxTy = E->getIdx()->getType();
2411f4a2713aSLionel Sambuc bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType();
2412f4a2713aSLionel Sambuc
2413*0a6a1f1dSLionel Sambuc if (SanOpts.has(SanitizerKind::ArrayBounds))
2414f4a2713aSLionel Sambuc EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, Accessed);
2415f4a2713aSLionel Sambuc
2416f4a2713aSLionel Sambuc // If the base is a vector type, then we are forming a vector element lvalue
2417f4a2713aSLionel Sambuc // with this subscript.
2418*0a6a1f1dSLionel Sambuc if (E->getBase()->getType()->isVectorType() &&
2419*0a6a1f1dSLionel Sambuc !isa<ExtVectorElementExpr>(E->getBase())) {
2420f4a2713aSLionel Sambuc // Emit the vector as an lvalue to get its address.
2421f4a2713aSLionel Sambuc LValue LHS = EmitLValue(E->getBase());
2422f4a2713aSLionel Sambuc assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
2423f4a2713aSLionel Sambuc return LValue::MakeVectorElt(LHS.getAddress(), Idx,
2424f4a2713aSLionel Sambuc E->getBase()->getType(), LHS.getAlignment());
2425f4a2713aSLionel Sambuc }
2426f4a2713aSLionel Sambuc
2427f4a2713aSLionel Sambuc // Extend or truncate the index type to 32 or 64-bits.
2428f4a2713aSLionel Sambuc if (Idx->getType() != IntPtrTy)
2429f4a2713aSLionel Sambuc Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
2430f4a2713aSLionel Sambuc
2431f4a2713aSLionel Sambuc // We know that the pointer points to a type of the correct size, unless the
2432f4a2713aSLionel Sambuc // size is a VLA or Objective-C interface.
2433*0a6a1f1dSLionel Sambuc llvm::Value *Address = nullptr;
2434f4a2713aSLionel Sambuc CharUnits ArrayAlignment;
2435*0a6a1f1dSLionel Sambuc if (isa<ExtVectorElementExpr>(E->getBase())) {
2436*0a6a1f1dSLionel Sambuc LValue LV = EmitLValue(E->getBase());
2437*0a6a1f1dSLionel Sambuc Address = EmitExtVectorElementLValue(LV);
2438*0a6a1f1dSLionel Sambuc Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx");
2439*0a6a1f1dSLionel Sambuc const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
2440*0a6a1f1dSLionel Sambuc QualType EQT = ExprVT->getElementType();
2441*0a6a1f1dSLionel Sambuc return MakeAddrLValue(Address, EQT,
2442*0a6a1f1dSLionel Sambuc getContext().getTypeAlignInChars(EQT));
2443*0a6a1f1dSLionel Sambuc }
2444*0a6a1f1dSLionel Sambuc else if (const VariableArrayType *vla =
2445f4a2713aSLionel Sambuc getContext().getAsVariableArrayType(E->getType())) {
2446f4a2713aSLionel Sambuc // The base must be a pointer, which is not an aggregate. Emit
2447f4a2713aSLionel Sambuc // it. It needs to be emitted first in case it's what captures
2448f4a2713aSLionel Sambuc // the VLA bounds.
2449f4a2713aSLionel Sambuc Address = EmitScalarExpr(E->getBase());
2450f4a2713aSLionel Sambuc
2451f4a2713aSLionel Sambuc // The element count here is the total number of non-VLA elements.
2452f4a2713aSLionel Sambuc llvm::Value *numElements = getVLASize(vla).first;
2453f4a2713aSLionel Sambuc
2454f4a2713aSLionel Sambuc // Effectively, the multiply by the VLA size is part of the GEP.
2455f4a2713aSLionel Sambuc // GEP indexes are signed, and scaling an index isn't permitted to
2456f4a2713aSLionel Sambuc // signed-overflow, so we use the same semantics for our explicit
2457f4a2713aSLionel Sambuc // multiply. We suppress this if overflow is not undefined behavior.
2458f4a2713aSLionel Sambuc if (getLangOpts().isSignedOverflowDefined()) {
2459f4a2713aSLionel Sambuc Idx = Builder.CreateMul(Idx, numElements);
2460f4a2713aSLionel Sambuc Address = Builder.CreateGEP(Address, Idx, "arrayidx");
2461f4a2713aSLionel Sambuc } else {
2462f4a2713aSLionel Sambuc Idx = Builder.CreateNSWMul(Idx, numElements);
2463f4a2713aSLionel Sambuc Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx");
2464f4a2713aSLionel Sambuc }
2465f4a2713aSLionel Sambuc } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
2466f4a2713aSLionel Sambuc // Indexing over an interface, as in "NSString *P; P[4];"
2467f4a2713aSLionel Sambuc llvm::Value *InterfaceSize =
2468f4a2713aSLionel Sambuc llvm::ConstantInt::get(Idx->getType(),
2469f4a2713aSLionel Sambuc getContext().getTypeSizeInChars(OIT).getQuantity());
2470f4a2713aSLionel Sambuc
2471f4a2713aSLionel Sambuc Idx = Builder.CreateMul(Idx, InterfaceSize);
2472f4a2713aSLionel Sambuc
2473f4a2713aSLionel Sambuc // The base must be a pointer, which is not an aggregate. Emit it.
2474f4a2713aSLionel Sambuc llvm::Value *Base = EmitScalarExpr(E->getBase());
2475f4a2713aSLionel Sambuc Address = EmitCastToVoidPtr(Base);
2476f4a2713aSLionel Sambuc Address = Builder.CreateGEP(Address, Idx, "arrayidx");
2477f4a2713aSLionel Sambuc Address = Builder.CreateBitCast(Address, Base->getType());
2478f4a2713aSLionel Sambuc } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
2479f4a2713aSLionel Sambuc // If this is A[i] where A is an array, the frontend will have decayed the
2480f4a2713aSLionel Sambuc // base to be a ArrayToPointerDecay implicit cast. While correct, it is
2481f4a2713aSLionel Sambuc // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
2482f4a2713aSLionel Sambuc // "gep x, i" here. Emit one "gep A, 0, i".
2483f4a2713aSLionel Sambuc assert(Array->getType()->isArrayType() &&
2484f4a2713aSLionel Sambuc "Array to pointer decay must have array source type!");
2485f4a2713aSLionel Sambuc LValue ArrayLV;
2486f4a2713aSLionel Sambuc // For simple multidimensional array indexing, set the 'accessed' flag for
2487f4a2713aSLionel Sambuc // better bounds-checking of the base expression.
2488*0a6a1f1dSLionel Sambuc if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array))
2489f4a2713aSLionel Sambuc ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true);
2490f4a2713aSLionel Sambuc else
2491f4a2713aSLionel Sambuc ArrayLV = EmitLValue(Array);
2492f4a2713aSLionel Sambuc llvm::Value *ArrayPtr = ArrayLV.getAddress();
2493f4a2713aSLionel Sambuc llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
2494f4a2713aSLionel Sambuc llvm::Value *Args[] = { Zero, Idx };
2495f4a2713aSLionel Sambuc
2496f4a2713aSLionel Sambuc // Propagate the alignment from the array itself to the result.
2497f4a2713aSLionel Sambuc ArrayAlignment = ArrayLV.getAlignment();
2498f4a2713aSLionel Sambuc
2499f4a2713aSLionel Sambuc if (getLangOpts().isSignedOverflowDefined())
2500f4a2713aSLionel Sambuc Address = Builder.CreateGEP(ArrayPtr, Args, "arrayidx");
2501f4a2713aSLionel Sambuc else
2502f4a2713aSLionel Sambuc Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, "arrayidx");
2503f4a2713aSLionel Sambuc } else {
2504f4a2713aSLionel Sambuc // The base must be a pointer, which is not an aggregate. Emit it.
2505f4a2713aSLionel Sambuc llvm::Value *Base = EmitScalarExpr(E->getBase());
2506f4a2713aSLionel Sambuc if (getLangOpts().isSignedOverflowDefined())
2507f4a2713aSLionel Sambuc Address = Builder.CreateGEP(Base, Idx, "arrayidx");
2508f4a2713aSLionel Sambuc else
2509f4a2713aSLionel Sambuc Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
2510f4a2713aSLionel Sambuc }
2511f4a2713aSLionel Sambuc
2512f4a2713aSLionel Sambuc QualType T = E->getBase()->getType()->getPointeeType();
2513f4a2713aSLionel Sambuc assert(!T.isNull() &&
2514f4a2713aSLionel Sambuc "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
2515f4a2713aSLionel Sambuc
2516f4a2713aSLionel Sambuc
2517f4a2713aSLionel Sambuc // Limit the alignment to that of the result type.
2518f4a2713aSLionel Sambuc LValue LV;
2519f4a2713aSLionel Sambuc if (!ArrayAlignment.isZero()) {
2520f4a2713aSLionel Sambuc CharUnits Align = getContext().getTypeAlignInChars(T);
2521f4a2713aSLionel Sambuc ArrayAlignment = std::min(Align, ArrayAlignment);
2522f4a2713aSLionel Sambuc LV = MakeAddrLValue(Address, T, ArrayAlignment);
2523f4a2713aSLionel Sambuc } else {
2524f4a2713aSLionel Sambuc LV = MakeNaturalAlignAddrLValue(Address, T);
2525f4a2713aSLionel Sambuc }
2526f4a2713aSLionel Sambuc
2527f4a2713aSLionel Sambuc LV.getQuals().setAddressSpace(E->getBase()->getType().getAddressSpace());
2528f4a2713aSLionel Sambuc
2529f4a2713aSLionel Sambuc if (getLangOpts().ObjC1 &&
2530f4a2713aSLionel Sambuc getLangOpts().getGC() != LangOptions::NonGC) {
2531f4a2713aSLionel Sambuc LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
2532f4a2713aSLionel Sambuc setObjCGCLValueClass(getContext(), E, LV);
2533f4a2713aSLionel Sambuc }
2534f4a2713aSLionel Sambuc return LV;
2535f4a2713aSLionel Sambuc }
2536f4a2713aSLionel Sambuc
2537f4a2713aSLionel Sambuc static
GenerateConstantVector(CGBuilderTy & Builder,SmallVectorImpl<unsigned> & Elts)2538f4a2713aSLionel Sambuc llvm::Constant *GenerateConstantVector(CGBuilderTy &Builder,
2539f4a2713aSLionel Sambuc SmallVectorImpl<unsigned> &Elts) {
2540f4a2713aSLionel Sambuc SmallVector<llvm::Constant*, 4> CElts;
2541f4a2713aSLionel Sambuc for (unsigned i = 0, e = Elts.size(); i != e; ++i)
2542f4a2713aSLionel Sambuc CElts.push_back(Builder.getInt32(Elts[i]));
2543f4a2713aSLionel Sambuc
2544f4a2713aSLionel Sambuc return llvm::ConstantVector::get(CElts);
2545f4a2713aSLionel Sambuc }
2546f4a2713aSLionel Sambuc
2547f4a2713aSLionel Sambuc LValue CodeGenFunction::
EmitExtVectorElementExpr(const ExtVectorElementExpr * E)2548f4a2713aSLionel Sambuc EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
2549f4a2713aSLionel Sambuc // Emit the base vector as an l-value.
2550f4a2713aSLionel Sambuc LValue Base;
2551f4a2713aSLionel Sambuc
2552f4a2713aSLionel Sambuc // ExtVectorElementExpr's base can either be a vector or pointer to vector.
2553f4a2713aSLionel Sambuc if (E->isArrow()) {
2554f4a2713aSLionel Sambuc // If it is a pointer to a vector, emit the address and form an lvalue with
2555f4a2713aSLionel Sambuc // it.
2556f4a2713aSLionel Sambuc llvm::Value *Ptr = EmitScalarExpr(E->getBase());
2557f4a2713aSLionel Sambuc const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
2558f4a2713aSLionel Sambuc Base = MakeAddrLValue(Ptr, PT->getPointeeType());
2559f4a2713aSLionel Sambuc Base.getQuals().removeObjCGCAttr();
2560f4a2713aSLionel Sambuc } else if (E->getBase()->isGLValue()) {
2561f4a2713aSLionel Sambuc // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
2562f4a2713aSLionel Sambuc // emit the base as an lvalue.
2563f4a2713aSLionel Sambuc assert(E->getBase()->getType()->isVectorType());
2564f4a2713aSLionel Sambuc Base = EmitLValue(E->getBase());
2565f4a2713aSLionel Sambuc } else {
2566f4a2713aSLionel Sambuc // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
2567f4a2713aSLionel Sambuc assert(E->getBase()->getType()->isVectorType() &&
2568f4a2713aSLionel Sambuc "Result must be a vector");
2569f4a2713aSLionel Sambuc llvm::Value *Vec = EmitScalarExpr(E->getBase());
2570f4a2713aSLionel Sambuc
2571f4a2713aSLionel Sambuc // Store the vector to memory (because LValue wants an address).
2572f4a2713aSLionel Sambuc llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
2573f4a2713aSLionel Sambuc Builder.CreateStore(Vec, VecMem);
2574f4a2713aSLionel Sambuc Base = MakeAddrLValue(VecMem, E->getBase()->getType());
2575f4a2713aSLionel Sambuc }
2576f4a2713aSLionel Sambuc
2577f4a2713aSLionel Sambuc QualType type =
2578f4a2713aSLionel Sambuc E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers());
2579f4a2713aSLionel Sambuc
2580f4a2713aSLionel Sambuc // Encode the element access list into a vector of unsigned indices.
2581f4a2713aSLionel Sambuc SmallVector<unsigned, 4> Indices;
2582f4a2713aSLionel Sambuc E->getEncodedElementAccess(Indices);
2583f4a2713aSLionel Sambuc
2584f4a2713aSLionel Sambuc if (Base.isSimple()) {
2585f4a2713aSLionel Sambuc llvm::Constant *CV = GenerateConstantVector(Builder, Indices);
2586f4a2713aSLionel Sambuc return LValue::MakeExtVectorElt(Base.getAddress(), CV, type,
2587f4a2713aSLionel Sambuc Base.getAlignment());
2588f4a2713aSLionel Sambuc }
2589f4a2713aSLionel Sambuc assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
2590f4a2713aSLionel Sambuc
2591f4a2713aSLionel Sambuc llvm::Constant *BaseElts = Base.getExtVectorElts();
2592f4a2713aSLionel Sambuc SmallVector<llvm::Constant *, 4> CElts;
2593f4a2713aSLionel Sambuc
2594f4a2713aSLionel Sambuc for (unsigned i = 0, e = Indices.size(); i != e; ++i)
2595f4a2713aSLionel Sambuc CElts.push_back(BaseElts->getAggregateElement(Indices[i]));
2596f4a2713aSLionel Sambuc llvm::Constant *CV = llvm::ConstantVector::get(CElts);
2597f4a2713aSLionel Sambuc return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, type,
2598f4a2713aSLionel Sambuc Base.getAlignment());
2599f4a2713aSLionel Sambuc }
2600f4a2713aSLionel Sambuc
EmitMemberExpr(const MemberExpr * E)2601f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
2602f4a2713aSLionel Sambuc Expr *BaseExpr = E->getBase();
2603f4a2713aSLionel Sambuc
2604f4a2713aSLionel Sambuc // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
2605f4a2713aSLionel Sambuc LValue BaseLV;
2606f4a2713aSLionel Sambuc if (E->isArrow()) {
2607f4a2713aSLionel Sambuc llvm::Value *Ptr = EmitScalarExpr(BaseExpr);
2608f4a2713aSLionel Sambuc QualType PtrTy = BaseExpr->getType()->getPointeeType();
2609f4a2713aSLionel Sambuc EmitTypeCheck(TCK_MemberAccess, E->getExprLoc(), Ptr, PtrTy);
2610f4a2713aSLionel Sambuc BaseLV = MakeNaturalAlignAddrLValue(Ptr, PtrTy);
2611f4a2713aSLionel Sambuc } else
2612f4a2713aSLionel Sambuc BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess);
2613f4a2713aSLionel Sambuc
2614f4a2713aSLionel Sambuc NamedDecl *ND = E->getMemberDecl();
2615*0a6a1f1dSLionel Sambuc if (auto *Field = dyn_cast<FieldDecl>(ND)) {
2616f4a2713aSLionel Sambuc LValue LV = EmitLValueForField(BaseLV, Field);
2617f4a2713aSLionel Sambuc setObjCGCLValueClass(getContext(), E, LV);
2618f4a2713aSLionel Sambuc return LV;
2619f4a2713aSLionel Sambuc }
2620f4a2713aSLionel Sambuc
2621*0a6a1f1dSLionel Sambuc if (auto *VD = dyn_cast<VarDecl>(ND))
2622f4a2713aSLionel Sambuc return EmitGlobalVarDeclLValue(*this, E, VD);
2623f4a2713aSLionel Sambuc
2624*0a6a1f1dSLionel Sambuc if (const auto *FD = dyn_cast<FunctionDecl>(ND))
2625f4a2713aSLionel Sambuc return EmitFunctionDeclLValue(*this, E, FD);
2626f4a2713aSLionel Sambuc
2627f4a2713aSLionel Sambuc llvm_unreachable("Unhandled member declaration!");
2628f4a2713aSLionel Sambuc }
2629f4a2713aSLionel Sambuc
2630f4a2713aSLionel Sambuc /// Given that we are currently emitting a lambda, emit an l-value for
2631f4a2713aSLionel Sambuc /// one of its members.
EmitLValueForLambdaField(const FieldDecl * Field)2632f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitLValueForLambdaField(const FieldDecl *Field) {
2633f4a2713aSLionel Sambuc assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent()->isLambda());
2634f4a2713aSLionel Sambuc assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent() == Field->getParent());
2635f4a2713aSLionel Sambuc QualType LambdaTagType =
2636f4a2713aSLionel Sambuc getContext().getTagDeclType(Field->getParent());
2637f4a2713aSLionel Sambuc LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue, LambdaTagType);
2638f4a2713aSLionel Sambuc return EmitLValueForField(LambdaLV, Field);
2639f4a2713aSLionel Sambuc }
2640f4a2713aSLionel Sambuc
EmitLValueForField(LValue base,const FieldDecl * field)2641f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitLValueForField(LValue base,
2642f4a2713aSLionel Sambuc const FieldDecl *field) {
2643f4a2713aSLionel Sambuc if (field->isBitField()) {
2644f4a2713aSLionel Sambuc const CGRecordLayout &RL =
2645f4a2713aSLionel Sambuc CGM.getTypes().getCGRecordLayout(field->getParent());
2646f4a2713aSLionel Sambuc const CGBitFieldInfo &Info = RL.getBitFieldInfo(field);
2647f4a2713aSLionel Sambuc llvm::Value *Addr = base.getAddress();
2648f4a2713aSLionel Sambuc unsigned Idx = RL.getLLVMFieldNo(field);
2649f4a2713aSLionel Sambuc if (Idx != 0)
2650f4a2713aSLionel Sambuc // For structs, we GEP to the field that the record layout suggests.
2651f4a2713aSLionel Sambuc Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
2652f4a2713aSLionel Sambuc // Get the access type.
2653f4a2713aSLionel Sambuc llvm::Type *PtrTy = llvm::Type::getIntNPtrTy(
2654f4a2713aSLionel Sambuc getLLVMContext(), Info.StorageSize,
2655f4a2713aSLionel Sambuc CGM.getContext().getTargetAddressSpace(base.getType()));
2656f4a2713aSLionel Sambuc if (Addr->getType() != PtrTy)
2657f4a2713aSLionel Sambuc Addr = Builder.CreateBitCast(Addr, PtrTy);
2658f4a2713aSLionel Sambuc
2659f4a2713aSLionel Sambuc QualType fieldType =
2660f4a2713aSLionel Sambuc field->getType().withCVRQualifiers(base.getVRQualifiers());
2661f4a2713aSLionel Sambuc return LValue::MakeBitfield(Addr, Info, fieldType, base.getAlignment());
2662f4a2713aSLionel Sambuc }
2663f4a2713aSLionel Sambuc
2664f4a2713aSLionel Sambuc const RecordDecl *rec = field->getParent();
2665f4a2713aSLionel Sambuc QualType type = field->getType();
2666f4a2713aSLionel Sambuc CharUnits alignment = getContext().getDeclAlign(field);
2667f4a2713aSLionel Sambuc
2668f4a2713aSLionel Sambuc // FIXME: It should be impossible to have an LValue without alignment for a
2669f4a2713aSLionel Sambuc // complete type.
2670f4a2713aSLionel Sambuc if (!base.getAlignment().isZero())
2671f4a2713aSLionel Sambuc alignment = std::min(alignment, base.getAlignment());
2672f4a2713aSLionel Sambuc
2673f4a2713aSLionel Sambuc bool mayAlias = rec->hasAttr<MayAliasAttr>();
2674f4a2713aSLionel Sambuc
2675f4a2713aSLionel Sambuc llvm::Value *addr = base.getAddress();
2676f4a2713aSLionel Sambuc unsigned cvr = base.getVRQualifiers();
2677f4a2713aSLionel Sambuc bool TBAAPath = CGM.getCodeGenOpts().StructPathTBAA;
2678f4a2713aSLionel Sambuc if (rec->isUnion()) {
2679f4a2713aSLionel Sambuc // For unions, there is no pointer adjustment.
2680f4a2713aSLionel Sambuc assert(!type->isReferenceType() && "union has reference member");
2681f4a2713aSLionel Sambuc // TODO: handle path-aware TBAA for union.
2682f4a2713aSLionel Sambuc TBAAPath = false;
2683f4a2713aSLionel Sambuc } else {
2684f4a2713aSLionel Sambuc // For structs, we GEP to the field that the record layout suggests.
2685f4a2713aSLionel Sambuc unsigned idx = CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
2686f4a2713aSLionel Sambuc addr = Builder.CreateStructGEP(addr, idx, field->getName());
2687f4a2713aSLionel Sambuc
2688f4a2713aSLionel Sambuc // If this is a reference field, load the reference right now.
2689f4a2713aSLionel Sambuc if (const ReferenceType *refType = type->getAs<ReferenceType>()) {
2690f4a2713aSLionel Sambuc llvm::LoadInst *load = Builder.CreateLoad(addr, "ref");
2691f4a2713aSLionel Sambuc if (cvr & Qualifiers::Volatile) load->setVolatile(true);
2692f4a2713aSLionel Sambuc load->setAlignment(alignment.getQuantity());
2693f4a2713aSLionel Sambuc
2694f4a2713aSLionel Sambuc // Loading the reference will disable path-aware TBAA.
2695f4a2713aSLionel Sambuc TBAAPath = false;
2696f4a2713aSLionel Sambuc if (CGM.shouldUseTBAA()) {
2697f4a2713aSLionel Sambuc llvm::MDNode *tbaa;
2698f4a2713aSLionel Sambuc if (mayAlias)
2699f4a2713aSLionel Sambuc tbaa = CGM.getTBAAInfo(getContext().CharTy);
2700f4a2713aSLionel Sambuc else
2701f4a2713aSLionel Sambuc tbaa = CGM.getTBAAInfo(type);
2702f4a2713aSLionel Sambuc if (tbaa)
2703f4a2713aSLionel Sambuc CGM.DecorateInstruction(load, tbaa);
2704f4a2713aSLionel Sambuc }
2705f4a2713aSLionel Sambuc
2706f4a2713aSLionel Sambuc addr = load;
2707f4a2713aSLionel Sambuc mayAlias = false;
2708f4a2713aSLionel Sambuc type = refType->getPointeeType();
2709f4a2713aSLionel Sambuc if (type->isIncompleteType())
2710f4a2713aSLionel Sambuc alignment = CharUnits();
2711f4a2713aSLionel Sambuc else
2712f4a2713aSLionel Sambuc alignment = getContext().getTypeAlignInChars(type);
2713f4a2713aSLionel Sambuc cvr = 0; // qualifiers don't recursively apply to referencee
2714f4a2713aSLionel Sambuc }
2715f4a2713aSLionel Sambuc }
2716f4a2713aSLionel Sambuc
2717f4a2713aSLionel Sambuc // Make sure that the address is pointing to the right type. This is critical
2718f4a2713aSLionel Sambuc // for both unions and structs. A union needs a bitcast, a struct element
2719f4a2713aSLionel Sambuc // will need a bitcast if the LLVM type laid out doesn't match the desired
2720f4a2713aSLionel Sambuc // type.
2721f4a2713aSLionel Sambuc addr = EmitBitCastOfLValueToProperType(*this, addr,
2722f4a2713aSLionel Sambuc CGM.getTypes().ConvertTypeForMem(type),
2723f4a2713aSLionel Sambuc field->getName());
2724f4a2713aSLionel Sambuc
2725f4a2713aSLionel Sambuc if (field->hasAttr<AnnotateAttr>())
2726f4a2713aSLionel Sambuc addr = EmitFieldAnnotations(field, addr);
2727f4a2713aSLionel Sambuc
2728f4a2713aSLionel Sambuc LValue LV = MakeAddrLValue(addr, type, alignment);
2729f4a2713aSLionel Sambuc LV.getQuals().addCVRQualifiers(cvr);
2730f4a2713aSLionel Sambuc if (TBAAPath) {
2731f4a2713aSLionel Sambuc const ASTRecordLayout &Layout =
2732f4a2713aSLionel Sambuc getContext().getASTRecordLayout(field->getParent());
2733f4a2713aSLionel Sambuc // Set the base type to be the base type of the base LValue and
2734f4a2713aSLionel Sambuc // update offset to be relative to the base type.
2735f4a2713aSLionel Sambuc LV.setTBAABaseType(mayAlias ? getContext().CharTy : base.getTBAABaseType());
2736f4a2713aSLionel Sambuc LV.setTBAAOffset(mayAlias ? 0 : base.getTBAAOffset() +
2737f4a2713aSLionel Sambuc Layout.getFieldOffset(field->getFieldIndex()) /
2738f4a2713aSLionel Sambuc getContext().getCharWidth());
2739f4a2713aSLionel Sambuc }
2740f4a2713aSLionel Sambuc
2741f4a2713aSLionel Sambuc // __weak attribute on a field is ignored.
2742f4a2713aSLionel Sambuc if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak)
2743f4a2713aSLionel Sambuc LV.getQuals().removeObjCGCAttr();
2744f4a2713aSLionel Sambuc
2745f4a2713aSLionel Sambuc // Fields of may_alias structs act like 'char' for TBAA purposes.
2746f4a2713aSLionel Sambuc // FIXME: this should get propagated down through anonymous structs
2747f4a2713aSLionel Sambuc // and unions.
2748f4a2713aSLionel Sambuc if (mayAlias && LV.getTBAAInfo())
2749f4a2713aSLionel Sambuc LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy));
2750f4a2713aSLionel Sambuc
2751f4a2713aSLionel Sambuc return LV;
2752f4a2713aSLionel Sambuc }
2753f4a2713aSLionel Sambuc
2754f4a2713aSLionel Sambuc LValue
EmitLValueForFieldInitialization(LValue Base,const FieldDecl * Field)2755f4a2713aSLionel Sambuc CodeGenFunction::EmitLValueForFieldInitialization(LValue Base,
2756f4a2713aSLionel Sambuc const FieldDecl *Field) {
2757f4a2713aSLionel Sambuc QualType FieldType = Field->getType();
2758f4a2713aSLionel Sambuc
2759f4a2713aSLionel Sambuc if (!FieldType->isReferenceType())
2760f4a2713aSLionel Sambuc return EmitLValueForField(Base, Field);
2761f4a2713aSLionel Sambuc
2762f4a2713aSLionel Sambuc const CGRecordLayout &RL =
2763f4a2713aSLionel Sambuc CGM.getTypes().getCGRecordLayout(Field->getParent());
2764f4a2713aSLionel Sambuc unsigned idx = RL.getLLVMFieldNo(Field);
2765f4a2713aSLionel Sambuc llvm::Value *V = Builder.CreateStructGEP(Base.getAddress(), idx);
2766f4a2713aSLionel Sambuc assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
2767f4a2713aSLionel Sambuc
2768f4a2713aSLionel Sambuc // Make sure that the address is pointing to the right type. This is critical
2769f4a2713aSLionel Sambuc // for both unions and structs. A union needs a bitcast, a struct element
2770f4a2713aSLionel Sambuc // will need a bitcast if the LLVM type laid out doesn't match the desired
2771f4a2713aSLionel Sambuc // type.
2772f4a2713aSLionel Sambuc llvm::Type *llvmType = ConvertTypeForMem(FieldType);
2773f4a2713aSLionel Sambuc V = EmitBitCastOfLValueToProperType(*this, V, llvmType, Field->getName());
2774f4a2713aSLionel Sambuc
2775f4a2713aSLionel Sambuc CharUnits Alignment = getContext().getDeclAlign(Field);
2776f4a2713aSLionel Sambuc
2777f4a2713aSLionel Sambuc // FIXME: It should be impossible to have an LValue without alignment for a
2778f4a2713aSLionel Sambuc // complete type.
2779f4a2713aSLionel Sambuc if (!Base.getAlignment().isZero())
2780f4a2713aSLionel Sambuc Alignment = std::min(Alignment, Base.getAlignment());
2781f4a2713aSLionel Sambuc
2782f4a2713aSLionel Sambuc return MakeAddrLValue(V, FieldType, Alignment);
2783f4a2713aSLionel Sambuc }
2784f4a2713aSLionel Sambuc
EmitCompoundLiteralLValue(const CompoundLiteralExpr * E)2785f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
2786f4a2713aSLionel Sambuc if (E->isFileScope()) {
2787f4a2713aSLionel Sambuc llvm::Value *GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E);
2788f4a2713aSLionel Sambuc return MakeAddrLValue(GlobalPtr, E->getType());
2789f4a2713aSLionel Sambuc }
2790f4a2713aSLionel Sambuc if (E->getType()->isVariablyModifiedType())
2791f4a2713aSLionel Sambuc // make sure to emit the VLA size.
2792f4a2713aSLionel Sambuc EmitVariablyModifiedType(E->getType());
2793f4a2713aSLionel Sambuc
2794f4a2713aSLionel Sambuc llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
2795f4a2713aSLionel Sambuc const Expr *InitExpr = E->getInitializer();
2796f4a2713aSLionel Sambuc LValue Result = MakeAddrLValue(DeclPtr, E->getType());
2797f4a2713aSLionel Sambuc
2798f4a2713aSLionel Sambuc EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(),
2799f4a2713aSLionel Sambuc /*Init*/ true);
2800f4a2713aSLionel Sambuc
2801f4a2713aSLionel Sambuc return Result;
2802f4a2713aSLionel Sambuc }
2803f4a2713aSLionel Sambuc
EmitInitListLValue(const InitListExpr * E)2804f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitInitListLValue(const InitListExpr *E) {
2805f4a2713aSLionel Sambuc if (!E->isGLValue())
2806f4a2713aSLionel Sambuc // Initializing an aggregate temporary in C++11: T{...}.
2807f4a2713aSLionel Sambuc return EmitAggExprToLValue(E);
2808f4a2713aSLionel Sambuc
2809f4a2713aSLionel Sambuc // An lvalue initializer list must be initializing a reference.
2810f4a2713aSLionel Sambuc assert(E->getNumInits() == 1 && "reference init with multiple values");
2811f4a2713aSLionel Sambuc return EmitLValue(E->getInit(0));
2812f4a2713aSLionel Sambuc }
2813f4a2713aSLionel Sambuc
2814*0a6a1f1dSLionel Sambuc /// Emit the operand of a glvalue conditional operator. This is either a glvalue
2815*0a6a1f1dSLionel Sambuc /// or a (possibly-parenthesized) throw-expression. If this is a throw, no
2816*0a6a1f1dSLionel Sambuc /// LValue is returned and the current block has been terminated.
EmitLValueOrThrowExpression(CodeGenFunction & CGF,const Expr * Operand)2817*0a6a1f1dSLionel Sambuc static Optional<LValue> EmitLValueOrThrowExpression(CodeGenFunction &CGF,
2818*0a6a1f1dSLionel Sambuc const Expr *Operand) {
2819*0a6a1f1dSLionel Sambuc if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Operand->IgnoreParens())) {
2820*0a6a1f1dSLionel Sambuc CGF.EmitCXXThrowExpr(ThrowExpr, /*KeepInsertionPoint*/false);
2821*0a6a1f1dSLionel Sambuc return None;
2822*0a6a1f1dSLionel Sambuc }
2823*0a6a1f1dSLionel Sambuc
2824*0a6a1f1dSLionel Sambuc return CGF.EmitLValue(Operand);
2825*0a6a1f1dSLionel Sambuc }
2826*0a6a1f1dSLionel Sambuc
2827f4a2713aSLionel Sambuc LValue CodeGenFunction::
EmitConditionalOperatorLValue(const AbstractConditionalOperator * expr)2828f4a2713aSLionel Sambuc EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) {
2829f4a2713aSLionel Sambuc if (!expr->isGLValue()) {
2830f4a2713aSLionel Sambuc // ?: here should be an aggregate.
2831f4a2713aSLionel Sambuc assert(hasAggregateEvaluationKind(expr->getType()) &&
2832f4a2713aSLionel Sambuc "Unexpected conditional operator!");
2833f4a2713aSLionel Sambuc return EmitAggExprToLValue(expr);
2834f4a2713aSLionel Sambuc }
2835f4a2713aSLionel Sambuc
2836f4a2713aSLionel Sambuc OpaqueValueMapping binding(*this, expr);
2837*0a6a1f1dSLionel Sambuc RegionCounter Cnt = getPGORegionCounter(expr);
2838f4a2713aSLionel Sambuc
2839f4a2713aSLionel Sambuc const Expr *condExpr = expr->getCond();
2840f4a2713aSLionel Sambuc bool CondExprBool;
2841f4a2713aSLionel Sambuc if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
2842f4a2713aSLionel Sambuc const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr();
2843f4a2713aSLionel Sambuc if (!CondExprBool) std::swap(live, dead);
2844f4a2713aSLionel Sambuc
2845*0a6a1f1dSLionel Sambuc if (!ContainsLabel(dead)) {
2846*0a6a1f1dSLionel Sambuc // If the true case is live, we need to track its region.
2847*0a6a1f1dSLionel Sambuc if (CondExprBool)
2848*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
2849f4a2713aSLionel Sambuc return EmitLValue(live);
2850f4a2713aSLionel Sambuc }
2851*0a6a1f1dSLionel Sambuc }
2852f4a2713aSLionel Sambuc
2853f4a2713aSLionel Sambuc llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true");
2854f4a2713aSLionel Sambuc llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false");
2855f4a2713aSLionel Sambuc llvm::BasicBlock *contBlock = createBasicBlock("cond.end");
2856f4a2713aSLionel Sambuc
2857f4a2713aSLionel Sambuc ConditionalEvaluation eval(*this);
2858*0a6a1f1dSLionel Sambuc EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock, Cnt.getCount());
2859f4a2713aSLionel Sambuc
2860f4a2713aSLionel Sambuc // Any temporaries created here are conditional.
2861f4a2713aSLionel Sambuc EmitBlock(lhsBlock);
2862*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
2863f4a2713aSLionel Sambuc eval.begin(*this);
2864*0a6a1f1dSLionel Sambuc Optional<LValue> lhs =
2865*0a6a1f1dSLionel Sambuc EmitLValueOrThrowExpression(*this, expr->getTrueExpr());
2866f4a2713aSLionel Sambuc eval.end(*this);
2867f4a2713aSLionel Sambuc
2868*0a6a1f1dSLionel Sambuc if (lhs && !lhs->isSimple())
2869f4a2713aSLionel Sambuc return EmitUnsupportedLValue(expr, "conditional operator");
2870f4a2713aSLionel Sambuc
2871f4a2713aSLionel Sambuc lhsBlock = Builder.GetInsertBlock();
2872*0a6a1f1dSLionel Sambuc if (lhs)
2873f4a2713aSLionel Sambuc Builder.CreateBr(contBlock);
2874f4a2713aSLionel Sambuc
2875f4a2713aSLionel Sambuc // Any temporaries created here are conditional.
2876f4a2713aSLionel Sambuc EmitBlock(rhsBlock);
2877f4a2713aSLionel Sambuc eval.begin(*this);
2878*0a6a1f1dSLionel Sambuc Optional<LValue> rhs =
2879*0a6a1f1dSLionel Sambuc EmitLValueOrThrowExpression(*this, expr->getFalseExpr());
2880f4a2713aSLionel Sambuc eval.end(*this);
2881*0a6a1f1dSLionel Sambuc if (rhs && !rhs->isSimple())
2882f4a2713aSLionel Sambuc return EmitUnsupportedLValue(expr, "conditional operator");
2883f4a2713aSLionel Sambuc rhsBlock = Builder.GetInsertBlock();
2884f4a2713aSLionel Sambuc
2885f4a2713aSLionel Sambuc EmitBlock(contBlock);
2886f4a2713aSLionel Sambuc
2887*0a6a1f1dSLionel Sambuc if (lhs && rhs) {
2888*0a6a1f1dSLionel Sambuc llvm::PHINode *phi = Builder.CreatePHI(lhs->getAddress()->getType(),
2889*0a6a1f1dSLionel Sambuc 2, "cond-lvalue");
2890*0a6a1f1dSLionel Sambuc phi->addIncoming(lhs->getAddress(), lhsBlock);
2891*0a6a1f1dSLionel Sambuc phi->addIncoming(rhs->getAddress(), rhsBlock);
2892f4a2713aSLionel Sambuc return MakeAddrLValue(phi, expr->getType());
2893*0a6a1f1dSLionel Sambuc } else {
2894*0a6a1f1dSLionel Sambuc assert((lhs || rhs) &&
2895*0a6a1f1dSLionel Sambuc "both operands of glvalue conditional are throw-expressions?");
2896*0a6a1f1dSLionel Sambuc return lhs ? *lhs : *rhs;
2897*0a6a1f1dSLionel Sambuc }
2898f4a2713aSLionel Sambuc }
2899f4a2713aSLionel Sambuc
2900f4a2713aSLionel Sambuc /// EmitCastLValue - Casts are never lvalues unless that cast is to a reference
2901f4a2713aSLionel Sambuc /// type. If the cast is to a reference, we can have the usual lvalue result,
2902f4a2713aSLionel Sambuc /// otherwise if a cast is needed by the code generator in an lvalue context,
2903f4a2713aSLionel Sambuc /// then it must mean that we need the address of an aggregate in order to
2904f4a2713aSLionel Sambuc /// access one of its members. This can happen for all the reasons that casts
2905f4a2713aSLionel Sambuc /// are permitted with aggregate result, including noop aggregate casts, and
2906f4a2713aSLionel Sambuc /// cast from scalar to union.
EmitCastLValue(const CastExpr * E)2907f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
2908f4a2713aSLionel Sambuc switch (E->getCastKind()) {
2909f4a2713aSLionel Sambuc case CK_ToVoid:
2910f4a2713aSLionel Sambuc case CK_BitCast:
2911f4a2713aSLionel Sambuc case CK_ArrayToPointerDecay:
2912f4a2713aSLionel Sambuc case CK_FunctionToPointerDecay:
2913f4a2713aSLionel Sambuc case CK_NullToMemberPointer:
2914f4a2713aSLionel Sambuc case CK_NullToPointer:
2915f4a2713aSLionel Sambuc case CK_IntegralToPointer:
2916f4a2713aSLionel Sambuc case CK_PointerToIntegral:
2917f4a2713aSLionel Sambuc case CK_PointerToBoolean:
2918f4a2713aSLionel Sambuc case CK_VectorSplat:
2919f4a2713aSLionel Sambuc case CK_IntegralCast:
2920f4a2713aSLionel Sambuc case CK_IntegralToBoolean:
2921f4a2713aSLionel Sambuc case CK_IntegralToFloating:
2922f4a2713aSLionel Sambuc case CK_FloatingToIntegral:
2923f4a2713aSLionel Sambuc case CK_FloatingToBoolean:
2924f4a2713aSLionel Sambuc case CK_FloatingCast:
2925f4a2713aSLionel Sambuc case CK_FloatingRealToComplex:
2926f4a2713aSLionel Sambuc case CK_FloatingComplexToReal:
2927f4a2713aSLionel Sambuc case CK_FloatingComplexToBoolean:
2928f4a2713aSLionel Sambuc case CK_FloatingComplexCast:
2929f4a2713aSLionel Sambuc case CK_FloatingComplexToIntegralComplex:
2930f4a2713aSLionel Sambuc case CK_IntegralRealToComplex:
2931f4a2713aSLionel Sambuc case CK_IntegralComplexToReal:
2932f4a2713aSLionel Sambuc case CK_IntegralComplexToBoolean:
2933f4a2713aSLionel Sambuc case CK_IntegralComplexCast:
2934f4a2713aSLionel Sambuc case CK_IntegralComplexToFloatingComplex:
2935f4a2713aSLionel Sambuc case CK_DerivedToBaseMemberPointer:
2936f4a2713aSLionel Sambuc case CK_BaseToDerivedMemberPointer:
2937f4a2713aSLionel Sambuc case CK_MemberPointerToBoolean:
2938f4a2713aSLionel Sambuc case CK_ReinterpretMemberPointer:
2939f4a2713aSLionel Sambuc case CK_AnyPointerToBlockPointerCast:
2940f4a2713aSLionel Sambuc case CK_ARCProduceObject:
2941f4a2713aSLionel Sambuc case CK_ARCConsumeObject:
2942f4a2713aSLionel Sambuc case CK_ARCReclaimReturnedObject:
2943f4a2713aSLionel Sambuc case CK_ARCExtendBlockObject:
2944f4a2713aSLionel Sambuc case CK_CopyAndAutoreleaseBlockObject:
2945*0a6a1f1dSLionel Sambuc case CK_AddressSpaceConversion:
2946f4a2713aSLionel Sambuc return EmitUnsupportedLValue(E, "unexpected cast lvalue");
2947f4a2713aSLionel Sambuc
2948f4a2713aSLionel Sambuc case CK_Dependent:
2949f4a2713aSLionel Sambuc llvm_unreachable("dependent cast kind in IR gen!");
2950f4a2713aSLionel Sambuc
2951f4a2713aSLionel Sambuc case CK_BuiltinFnToFnPtr:
2952f4a2713aSLionel Sambuc llvm_unreachable("builtin functions are handled elsewhere");
2953f4a2713aSLionel Sambuc
2954f4a2713aSLionel Sambuc // These are never l-values; just use the aggregate emission code.
2955f4a2713aSLionel Sambuc case CK_NonAtomicToAtomic:
2956f4a2713aSLionel Sambuc case CK_AtomicToNonAtomic:
2957f4a2713aSLionel Sambuc return EmitAggExprToLValue(E);
2958f4a2713aSLionel Sambuc
2959f4a2713aSLionel Sambuc case CK_Dynamic: {
2960f4a2713aSLionel Sambuc LValue LV = EmitLValue(E->getSubExpr());
2961f4a2713aSLionel Sambuc llvm::Value *V = LV.getAddress();
2962*0a6a1f1dSLionel Sambuc const auto *DCE = cast<CXXDynamicCastExpr>(E);
2963f4a2713aSLionel Sambuc return MakeAddrLValue(EmitDynamicCast(V, DCE), E->getType());
2964f4a2713aSLionel Sambuc }
2965f4a2713aSLionel Sambuc
2966f4a2713aSLionel Sambuc case CK_ConstructorConversion:
2967f4a2713aSLionel Sambuc case CK_UserDefinedConversion:
2968f4a2713aSLionel Sambuc case CK_CPointerToObjCPointerCast:
2969f4a2713aSLionel Sambuc case CK_BlockPointerToObjCPointerCast:
2970f4a2713aSLionel Sambuc case CK_NoOp:
2971f4a2713aSLionel Sambuc case CK_LValueToRValue:
2972f4a2713aSLionel Sambuc return EmitLValue(E->getSubExpr());
2973f4a2713aSLionel Sambuc
2974f4a2713aSLionel Sambuc case CK_UncheckedDerivedToBase:
2975f4a2713aSLionel Sambuc case CK_DerivedToBase: {
2976f4a2713aSLionel Sambuc const RecordType *DerivedClassTy =
2977f4a2713aSLionel Sambuc E->getSubExpr()->getType()->getAs<RecordType>();
2978*0a6a1f1dSLionel Sambuc auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2979f4a2713aSLionel Sambuc
2980f4a2713aSLionel Sambuc LValue LV = EmitLValue(E->getSubExpr());
2981f4a2713aSLionel Sambuc llvm::Value *This = LV.getAddress();
2982f4a2713aSLionel Sambuc
2983f4a2713aSLionel Sambuc // Perform the derived-to-base conversion
2984*0a6a1f1dSLionel Sambuc llvm::Value *Base = GetAddressOfBaseClass(
2985*0a6a1f1dSLionel Sambuc This, DerivedClassDecl, E->path_begin(), E->path_end(),
2986*0a6a1f1dSLionel Sambuc /*NullCheckValue=*/false, E->getExprLoc());
2987f4a2713aSLionel Sambuc
2988f4a2713aSLionel Sambuc return MakeAddrLValue(Base, E->getType());
2989f4a2713aSLionel Sambuc }
2990f4a2713aSLionel Sambuc case CK_ToUnion:
2991f4a2713aSLionel Sambuc return EmitAggExprToLValue(E);
2992f4a2713aSLionel Sambuc case CK_BaseToDerived: {
2993f4a2713aSLionel Sambuc const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
2994*0a6a1f1dSLionel Sambuc auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2995f4a2713aSLionel Sambuc
2996f4a2713aSLionel Sambuc LValue LV = EmitLValue(E->getSubExpr());
2997f4a2713aSLionel Sambuc
2998f4a2713aSLionel Sambuc // Perform the base-to-derived conversion
2999f4a2713aSLionel Sambuc llvm::Value *Derived =
3000f4a2713aSLionel Sambuc GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl,
3001f4a2713aSLionel Sambuc E->path_begin(), E->path_end(),
3002f4a2713aSLionel Sambuc /*NullCheckValue=*/false);
3003f4a2713aSLionel Sambuc
3004f4a2713aSLionel Sambuc // C++11 [expr.static.cast]p2: Behavior is undefined if a downcast is
3005f4a2713aSLionel Sambuc // performed and the object is not of the derived type.
3006*0a6a1f1dSLionel Sambuc if (sanitizePerformTypeCheck())
3007f4a2713aSLionel Sambuc EmitTypeCheck(TCK_DowncastReference, E->getExprLoc(),
3008f4a2713aSLionel Sambuc Derived, E->getType());
3009f4a2713aSLionel Sambuc
3010f4a2713aSLionel Sambuc return MakeAddrLValue(Derived, E->getType());
3011f4a2713aSLionel Sambuc }
3012f4a2713aSLionel Sambuc case CK_LValueBitCast: {
3013f4a2713aSLionel Sambuc // This must be a reinterpret_cast (or c-style equivalent).
3014*0a6a1f1dSLionel Sambuc const auto *CE = cast<ExplicitCastExpr>(E);
3015f4a2713aSLionel Sambuc
3016f4a2713aSLionel Sambuc LValue LV = EmitLValue(E->getSubExpr());
3017f4a2713aSLionel Sambuc llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
3018f4a2713aSLionel Sambuc ConvertType(CE->getTypeAsWritten()));
3019f4a2713aSLionel Sambuc return MakeAddrLValue(V, E->getType());
3020f4a2713aSLionel Sambuc }
3021f4a2713aSLionel Sambuc case CK_ObjCObjectLValueCast: {
3022f4a2713aSLionel Sambuc LValue LV = EmitLValue(E->getSubExpr());
3023f4a2713aSLionel Sambuc QualType ToType = getContext().getLValueReferenceType(E->getType());
3024f4a2713aSLionel Sambuc llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
3025f4a2713aSLionel Sambuc ConvertType(ToType));
3026f4a2713aSLionel Sambuc return MakeAddrLValue(V, E->getType());
3027f4a2713aSLionel Sambuc }
3028f4a2713aSLionel Sambuc case CK_ZeroToOCLEvent:
3029f4a2713aSLionel Sambuc llvm_unreachable("NULL to OpenCL event lvalue cast is not valid");
3030f4a2713aSLionel Sambuc }
3031f4a2713aSLionel Sambuc
3032f4a2713aSLionel Sambuc llvm_unreachable("Unhandled lvalue cast kind?");
3033f4a2713aSLionel Sambuc }
3034f4a2713aSLionel Sambuc
EmitOpaqueValueLValue(const OpaqueValueExpr * e)3035f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) {
3036f4a2713aSLionel Sambuc assert(OpaqueValueMappingData::shouldBindAsLValue(e));
3037f4a2713aSLionel Sambuc return getOpaqueLValueMapping(e);
3038f4a2713aSLionel Sambuc }
3039f4a2713aSLionel Sambuc
EmitRValueForField(LValue LV,const FieldDecl * FD,SourceLocation Loc)3040f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitRValueForField(LValue LV,
3041f4a2713aSLionel Sambuc const FieldDecl *FD,
3042f4a2713aSLionel Sambuc SourceLocation Loc) {
3043f4a2713aSLionel Sambuc QualType FT = FD->getType();
3044f4a2713aSLionel Sambuc LValue FieldLV = EmitLValueForField(LV, FD);
3045f4a2713aSLionel Sambuc switch (getEvaluationKind(FT)) {
3046f4a2713aSLionel Sambuc case TEK_Complex:
3047f4a2713aSLionel Sambuc return RValue::getComplex(EmitLoadOfComplex(FieldLV, Loc));
3048f4a2713aSLionel Sambuc case TEK_Aggregate:
3049f4a2713aSLionel Sambuc return FieldLV.asAggregateRValue();
3050f4a2713aSLionel Sambuc case TEK_Scalar:
3051f4a2713aSLionel Sambuc return EmitLoadOfLValue(FieldLV, Loc);
3052f4a2713aSLionel Sambuc }
3053f4a2713aSLionel Sambuc llvm_unreachable("bad evaluation kind");
3054f4a2713aSLionel Sambuc }
3055f4a2713aSLionel Sambuc
3056f4a2713aSLionel Sambuc //===--------------------------------------------------------------------===//
3057f4a2713aSLionel Sambuc // Expression Emission
3058f4a2713aSLionel Sambuc //===--------------------------------------------------------------------===//
3059f4a2713aSLionel Sambuc
EmitCallExpr(const CallExpr * E,ReturnValueSlot ReturnValue)3060f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
3061f4a2713aSLionel Sambuc ReturnValueSlot ReturnValue) {
3062f4a2713aSLionel Sambuc // Force column info to be generated so we can differentiate
3063f4a2713aSLionel Sambuc // multiple call sites on the same line in the debug info.
3064*0a6a1f1dSLionel Sambuc // FIXME: This is insufficient. Two calls coming from the same macro
3065*0a6a1f1dSLionel Sambuc // expansion will still get the same line/column and break debug info. It's
3066*0a6a1f1dSLionel Sambuc // possible that LLVM can be fixed to not rely on this uniqueness, at which
3067*0a6a1f1dSLionel Sambuc // point this workaround can be removed.
3068*0a6a1f1dSLionel Sambuc ApplyDebugLocation DL(*this, E->getLocStart(),
3069*0a6a1f1dSLionel Sambuc E->getDirectCallee() &&
3070*0a6a1f1dSLionel Sambuc E->getDirectCallee()->isInlineSpecified());
3071f4a2713aSLionel Sambuc
3072f4a2713aSLionel Sambuc // Builtins never have block type.
3073f4a2713aSLionel Sambuc if (E->getCallee()->getType()->isBlockPointerType())
3074f4a2713aSLionel Sambuc return EmitBlockCallExpr(E, ReturnValue);
3075f4a2713aSLionel Sambuc
3076*0a6a1f1dSLionel Sambuc if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E))
3077f4a2713aSLionel Sambuc return EmitCXXMemberCallExpr(CE, ReturnValue);
3078f4a2713aSLionel Sambuc
3079*0a6a1f1dSLionel Sambuc if (const auto *CE = dyn_cast<CUDAKernelCallExpr>(E))
3080f4a2713aSLionel Sambuc return EmitCUDAKernelCallExpr(CE, ReturnValue);
3081f4a2713aSLionel Sambuc
3082f4a2713aSLionel Sambuc const Decl *TargetDecl = E->getCalleeDecl();
3083f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
3084f4a2713aSLionel Sambuc if (unsigned builtinID = FD->getBuiltinID())
3085*0a6a1f1dSLionel Sambuc return EmitBuiltinExpr(FD, builtinID, E, ReturnValue);
3086f4a2713aSLionel Sambuc }
3087f4a2713aSLionel Sambuc
3088*0a6a1f1dSLionel Sambuc if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E))
3089f4a2713aSLionel Sambuc if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
3090f4a2713aSLionel Sambuc return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
3091f4a2713aSLionel Sambuc
3092*0a6a1f1dSLionel Sambuc if (const auto *PseudoDtor =
3093*0a6a1f1dSLionel Sambuc dyn_cast<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
3094f4a2713aSLionel Sambuc QualType DestroyedType = PseudoDtor->getDestroyedType();
3095f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount &&
3096f4a2713aSLionel Sambuc DestroyedType->isObjCLifetimeType() &&
3097f4a2713aSLionel Sambuc (DestroyedType.getObjCLifetime() == Qualifiers::OCL_Strong ||
3098f4a2713aSLionel Sambuc DestroyedType.getObjCLifetime() == Qualifiers::OCL_Weak)) {
3099f4a2713aSLionel Sambuc // Automatic Reference Counting:
3100f4a2713aSLionel Sambuc // If the pseudo-expression names a retainable object with weak or
3101f4a2713aSLionel Sambuc // strong lifetime, the object shall be released.
3102f4a2713aSLionel Sambuc Expr *BaseExpr = PseudoDtor->getBase();
3103*0a6a1f1dSLionel Sambuc llvm::Value *BaseValue = nullptr;
3104f4a2713aSLionel Sambuc Qualifiers BaseQuals;
3105f4a2713aSLionel Sambuc
3106f4a2713aSLionel Sambuc // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
3107f4a2713aSLionel Sambuc if (PseudoDtor->isArrow()) {
3108f4a2713aSLionel Sambuc BaseValue = EmitScalarExpr(BaseExpr);
3109f4a2713aSLionel Sambuc const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>();
3110f4a2713aSLionel Sambuc BaseQuals = PTy->getPointeeType().getQualifiers();
3111f4a2713aSLionel Sambuc } else {
3112f4a2713aSLionel Sambuc LValue BaseLV = EmitLValue(BaseExpr);
3113f4a2713aSLionel Sambuc BaseValue = BaseLV.getAddress();
3114f4a2713aSLionel Sambuc QualType BaseTy = BaseExpr->getType();
3115f4a2713aSLionel Sambuc BaseQuals = BaseTy.getQualifiers();
3116f4a2713aSLionel Sambuc }
3117f4a2713aSLionel Sambuc
3118f4a2713aSLionel Sambuc switch (PseudoDtor->getDestroyedType().getObjCLifetime()) {
3119f4a2713aSLionel Sambuc case Qualifiers::OCL_None:
3120f4a2713aSLionel Sambuc case Qualifiers::OCL_ExplicitNone:
3121f4a2713aSLionel Sambuc case Qualifiers::OCL_Autoreleasing:
3122f4a2713aSLionel Sambuc break;
3123f4a2713aSLionel Sambuc
3124f4a2713aSLionel Sambuc case Qualifiers::OCL_Strong:
3125f4a2713aSLionel Sambuc EmitARCRelease(Builder.CreateLoad(BaseValue,
3126f4a2713aSLionel Sambuc PseudoDtor->getDestroyedType().isVolatileQualified()),
3127f4a2713aSLionel Sambuc ARCPreciseLifetime);
3128f4a2713aSLionel Sambuc break;
3129f4a2713aSLionel Sambuc
3130f4a2713aSLionel Sambuc case Qualifiers::OCL_Weak:
3131f4a2713aSLionel Sambuc EmitARCDestroyWeak(BaseValue);
3132f4a2713aSLionel Sambuc break;
3133f4a2713aSLionel Sambuc }
3134f4a2713aSLionel Sambuc } else {
3135f4a2713aSLionel Sambuc // C++ [expr.pseudo]p1:
3136f4a2713aSLionel Sambuc // The result shall only be used as the operand for the function call
3137f4a2713aSLionel Sambuc // operator (), and the result of such a call has type void. The only
3138f4a2713aSLionel Sambuc // effect is the evaluation of the postfix-expression before the dot or
3139f4a2713aSLionel Sambuc // arrow.
3140f4a2713aSLionel Sambuc EmitScalarExpr(E->getCallee());
3141f4a2713aSLionel Sambuc }
3142f4a2713aSLionel Sambuc
3143*0a6a1f1dSLionel Sambuc return RValue::get(nullptr);
3144f4a2713aSLionel Sambuc }
3145f4a2713aSLionel Sambuc
3146f4a2713aSLionel Sambuc llvm::Value *Callee = EmitScalarExpr(E->getCallee());
3147*0a6a1f1dSLionel Sambuc return EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue,
3148*0a6a1f1dSLionel Sambuc TargetDecl);
3149f4a2713aSLionel Sambuc }
3150f4a2713aSLionel Sambuc
EmitBinaryOperatorLValue(const BinaryOperator * E)3151f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
3152f4a2713aSLionel Sambuc // Comma expressions just emit their LHS then their RHS as an l-value.
3153f4a2713aSLionel Sambuc if (E->getOpcode() == BO_Comma) {
3154f4a2713aSLionel Sambuc EmitIgnoredExpr(E->getLHS());
3155f4a2713aSLionel Sambuc EnsureInsertPoint();
3156f4a2713aSLionel Sambuc return EmitLValue(E->getRHS());
3157f4a2713aSLionel Sambuc }
3158f4a2713aSLionel Sambuc
3159f4a2713aSLionel Sambuc if (E->getOpcode() == BO_PtrMemD ||
3160f4a2713aSLionel Sambuc E->getOpcode() == BO_PtrMemI)
3161f4a2713aSLionel Sambuc return EmitPointerToDataMemberBinaryExpr(E);
3162f4a2713aSLionel Sambuc
3163f4a2713aSLionel Sambuc assert(E->getOpcode() == BO_Assign && "unexpected binary l-value");
3164f4a2713aSLionel Sambuc
3165f4a2713aSLionel Sambuc // Note that in all of these cases, __block variables need the RHS
3166f4a2713aSLionel Sambuc // evaluated first just in case the variable gets moved by the RHS.
3167f4a2713aSLionel Sambuc
3168f4a2713aSLionel Sambuc switch (getEvaluationKind(E->getType())) {
3169f4a2713aSLionel Sambuc case TEK_Scalar: {
3170f4a2713aSLionel Sambuc switch (E->getLHS()->getType().getObjCLifetime()) {
3171f4a2713aSLionel Sambuc case Qualifiers::OCL_Strong:
3172f4a2713aSLionel Sambuc return EmitARCStoreStrong(E, /*ignored*/ false).first;
3173f4a2713aSLionel Sambuc
3174f4a2713aSLionel Sambuc case Qualifiers::OCL_Autoreleasing:
3175f4a2713aSLionel Sambuc return EmitARCStoreAutoreleasing(E).first;
3176f4a2713aSLionel Sambuc
3177f4a2713aSLionel Sambuc // No reason to do any of these differently.
3178f4a2713aSLionel Sambuc case Qualifiers::OCL_None:
3179f4a2713aSLionel Sambuc case Qualifiers::OCL_ExplicitNone:
3180f4a2713aSLionel Sambuc case Qualifiers::OCL_Weak:
3181f4a2713aSLionel Sambuc break;
3182f4a2713aSLionel Sambuc }
3183f4a2713aSLionel Sambuc
3184f4a2713aSLionel Sambuc RValue RV = EmitAnyExpr(E->getRHS());
3185f4a2713aSLionel Sambuc LValue LV = EmitCheckedLValue(E->getLHS(), TCK_Store);
3186f4a2713aSLionel Sambuc EmitStoreThroughLValue(RV, LV);
3187f4a2713aSLionel Sambuc return LV;
3188f4a2713aSLionel Sambuc }
3189f4a2713aSLionel Sambuc
3190f4a2713aSLionel Sambuc case TEK_Complex:
3191f4a2713aSLionel Sambuc return EmitComplexAssignmentLValue(E);
3192f4a2713aSLionel Sambuc
3193f4a2713aSLionel Sambuc case TEK_Aggregate:
3194f4a2713aSLionel Sambuc return EmitAggExprToLValue(E);
3195f4a2713aSLionel Sambuc }
3196f4a2713aSLionel Sambuc llvm_unreachable("bad evaluation kind");
3197f4a2713aSLionel Sambuc }
3198f4a2713aSLionel Sambuc
EmitCallExprLValue(const CallExpr * E)3199f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
3200f4a2713aSLionel Sambuc RValue RV = EmitCallExpr(E);
3201f4a2713aSLionel Sambuc
3202f4a2713aSLionel Sambuc if (!RV.isScalar())
3203f4a2713aSLionel Sambuc return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
3204f4a2713aSLionel Sambuc
3205f4a2713aSLionel Sambuc assert(E->getCallReturnType()->isReferenceType() &&
3206f4a2713aSLionel Sambuc "Can't have a scalar return unless the return type is a "
3207f4a2713aSLionel Sambuc "reference type!");
3208f4a2713aSLionel Sambuc
3209f4a2713aSLionel Sambuc return MakeAddrLValue(RV.getScalarVal(), E->getType());
3210f4a2713aSLionel Sambuc }
3211f4a2713aSLionel Sambuc
EmitVAArgExprLValue(const VAArgExpr * E)3212f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
3213f4a2713aSLionel Sambuc // FIXME: This shouldn't require another copy.
3214f4a2713aSLionel Sambuc return EmitAggExprToLValue(E);
3215f4a2713aSLionel Sambuc }
3216f4a2713aSLionel Sambuc
EmitCXXConstructLValue(const CXXConstructExpr * E)3217f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
3218f4a2713aSLionel Sambuc assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor()
3219f4a2713aSLionel Sambuc && "binding l-value to type which needs a temporary");
3220f4a2713aSLionel Sambuc AggValueSlot Slot = CreateAggTemp(E->getType());
3221f4a2713aSLionel Sambuc EmitCXXConstructExpr(E, Slot);
3222f4a2713aSLionel Sambuc return MakeAddrLValue(Slot.getAddr(), E->getType());
3223f4a2713aSLionel Sambuc }
3224f4a2713aSLionel Sambuc
3225f4a2713aSLionel Sambuc LValue
EmitCXXTypeidLValue(const CXXTypeidExpr * E)3226f4a2713aSLionel Sambuc CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
3227f4a2713aSLionel Sambuc return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType());
3228f4a2713aSLionel Sambuc }
3229f4a2713aSLionel Sambuc
EmitCXXUuidofExpr(const CXXUuidofExpr * E)3230f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) {
3231f4a2713aSLionel Sambuc return Builder.CreateBitCast(CGM.GetAddrOfUuidDescriptor(E),
3232f4a2713aSLionel Sambuc ConvertType(E->getType())->getPointerTo());
3233f4a2713aSLionel Sambuc }
3234f4a2713aSLionel Sambuc
EmitCXXUuidofLValue(const CXXUuidofExpr * E)3235f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitCXXUuidofLValue(const CXXUuidofExpr *E) {
3236f4a2713aSLionel Sambuc return MakeAddrLValue(EmitCXXUuidofExpr(E), E->getType());
3237f4a2713aSLionel Sambuc }
3238f4a2713aSLionel Sambuc
3239f4a2713aSLionel Sambuc LValue
EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr * E)3240f4a2713aSLionel Sambuc CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
3241f4a2713aSLionel Sambuc AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
3242f4a2713aSLionel Sambuc Slot.setExternallyDestructed();
3243f4a2713aSLionel Sambuc EmitAggExpr(E->getSubExpr(), Slot);
3244f4a2713aSLionel Sambuc EmitCXXTemporary(E->getTemporary(), E->getType(), Slot.getAddr());
3245f4a2713aSLionel Sambuc return MakeAddrLValue(Slot.getAddr(), E->getType());
3246f4a2713aSLionel Sambuc }
3247f4a2713aSLionel Sambuc
3248f4a2713aSLionel Sambuc LValue
EmitLambdaLValue(const LambdaExpr * E)3249f4a2713aSLionel Sambuc CodeGenFunction::EmitLambdaLValue(const LambdaExpr *E) {
3250f4a2713aSLionel Sambuc AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
3251f4a2713aSLionel Sambuc EmitLambdaExpr(E, Slot);
3252f4a2713aSLionel Sambuc return MakeAddrLValue(Slot.getAddr(), E->getType());
3253f4a2713aSLionel Sambuc }
3254f4a2713aSLionel Sambuc
EmitObjCMessageExprLValue(const ObjCMessageExpr * E)3255f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
3256f4a2713aSLionel Sambuc RValue RV = EmitObjCMessageExpr(E);
3257f4a2713aSLionel Sambuc
3258f4a2713aSLionel Sambuc if (!RV.isScalar())
3259f4a2713aSLionel Sambuc return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
3260f4a2713aSLionel Sambuc
3261*0a6a1f1dSLionel Sambuc assert(E->getMethodDecl()->getReturnType()->isReferenceType() &&
3262f4a2713aSLionel Sambuc "Can't have a scalar return unless the return type is a "
3263f4a2713aSLionel Sambuc "reference type!");
3264f4a2713aSLionel Sambuc
3265f4a2713aSLionel Sambuc return MakeAddrLValue(RV.getScalarVal(), E->getType());
3266f4a2713aSLionel Sambuc }
3267f4a2713aSLionel Sambuc
EmitObjCSelectorLValue(const ObjCSelectorExpr * E)3268f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) {
3269f4a2713aSLionel Sambuc llvm::Value *V =
3270f4a2713aSLionel Sambuc CGM.getObjCRuntime().GetSelector(*this, E->getSelector(), true);
3271f4a2713aSLionel Sambuc return MakeAddrLValue(V, E->getType());
3272f4a2713aSLionel Sambuc }
3273f4a2713aSLionel Sambuc
EmitIvarOffset(const ObjCInterfaceDecl * Interface,const ObjCIvarDecl * Ivar)3274f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
3275f4a2713aSLionel Sambuc const ObjCIvarDecl *Ivar) {
3276f4a2713aSLionel Sambuc return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
3277f4a2713aSLionel Sambuc }
3278f4a2713aSLionel Sambuc
EmitLValueForIvar(QualType ObjectTy,llvm::Value * BaseValue,const ObjCIvarDecl * Ivar,unsigned CVRQualifiers)3279f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
3280f4a2713aSLionel Sambuc llvm::Value *BaseValue,
3281f4a2713aSLionel Sambuc const ObjCIvarDecl *Ivar,
3282f4a2713aSLionel Sambuc unsigned CVRQualifiers) {
3283f4a2713aSLionel Sambuc return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
3284f4a2713aSLionel Sambuc Ivar, CVRQualifiers);
3285f4a2713aSLionel Sambuc }
3286f4a2713aSLionel Sambuc
EmitObjCIvarRefLValue(const ObjCIvarRefExpr * E)3287f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
3288f4a2713aSLionel Sambuc // FIXME: A lot of the code below could be shared with EmitMemberExpr.
3289*0a6a1f1dSLionel Sambuc llvm::Value *BaseValue = nullptr;
3290f4a2713aSLionel Sambuc const Expr *BaseExpr = E->getBase();
3291f4a2713aSLionel Sambuc Qualifiers BaseQuals;
3292f4a2713aSLionel Sambuc QualType ObjectTy;
3293f4a2713aSLionel Sambuc if (E->isArrow()) {
3294f4a2713aSLionel Sambuc BaseValue = EmitScalarExpr(BaseExpr);
3295f4a2713aSLionel Sambuc ObjectTy = BaseExpr->getType()->getPointeeType();
3296f4a2713aSLionel Sambuc BaseQuals = ObjectTy.getQualifiers();
3297f4a2713aSLionel Sambuc } else {
3298f4a2713aSLionel Sambuc LValue BaseLV = EmitLValue(BaseExpr);
3299f4a2713aSLionel Sambuc // FIXME: this isn't right for bitfields.
3300f4a2713aSLionel Sambuc BaseValue = BaseLV.getAddress();
3301f4a2713aSLionel Sambuc ObjectTy = BaseExpr->getType();
3302f4a2713aSLionel Sambuc BaseQuals = ObjectTy.getQualifiers();
3303f4a2713aSLionel Sambuc }
3304f4a2713aSLionel Sambuc
3305f4a2713aSLionel Sambuc LValue LV =
3306f4a2713aSLionel Sambuc EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
3307f4a2713aSLionel Sambuc BaseQuals.getCVRQualifiers());
3308f4a2713aSLionel Sambuc setObjCGCLValueClass(getContext(), E, LV);
3309f4a2713aSLionel Sambuc return LV;
3310f4a2713aSLionel Sambuc }
3311f4a2713aSLionel Sambuc
EmitStmtExprLValue(const StmtExpr * E)3312f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
3313f4a2713aSLionel Sambuc // Can only get l-value for message expression returning aggregate type
3314f4a2713aSLionel Sambuc RValue RV = EmitAnyExprToTemp(E);
3315f4a2713aSLionel Sambuc return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
3316f4a2713aSLionel Sambuc }
3317f4a2713aSLionel Sambuc
EmitCall(QualType CalleeType,llvm::Value * Callee,const CallExpr * E,ReturnValueSlot ReturnValue,const Decl * TargetDecl,llvm::Value * Chain)3318f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
3319*0a6a1f1dSLionel Sambuc const CallExpr *E, ReturnValueSlot ReturnValue,
3320*0a6a1f1dSLionel Sambuc const Decl *TargetDecl, llvm::Value *Chain) {
3321f4a2713aSLionel Sambuc // Get the actual function type. The callee type will always be a pointer to
3322f4a2713aSLionel Sambuc // function type or a block pointer type.
3323f4a2713aSLionel Sambuc assert(CalleeType->isFunctionPointerType() &&
3324f4a2713aSLionel Sambuc "Call must have function pointer type!");
3325f4a2713aSLionel Sambuc
3326f4a2713aSLionel Sambuc CalleeType = getContext().getCanonicalType(CalleeType);
3327f4a2713aSLionel Sambuc
3328*0a6a1f1dSLionel Sambuc const auto *FnType =
3329*0a6a1f1dSLionel Sambuc cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
3330f4a2713aSLionel Sambuc
3331f4a2713aSLionel Sambuc // Force column info to differentiate multiple inlined call sites on
3332f4a2713aSLionel Sambuc // the same line, analoguous to EmitCallExpr.
3333*0a6a1f1dSLionel Sambuc // FIXME: This is insufficient. Two calls coming from the same macro expansion
3334*0a6a1f1dSLionel Sambuc // will still get the same line/column and break debug info. It's possible
3335*0a6a1f1dSLionel Sambuc // that LLVM can be fixed to not rely on this uniqueness, at which point this
3336*0a6a1f1dSLionel Sambuc // workaround can be removed.
3337f4a2713aSLionel Sambuc bool ForceColumnInfo = false;
3338f4a2713aSLionel Sambuc if (const FunctionDecl* FD = dyn_cast_or_null<const FunctionDecl>(TargetDecl))
3339f4a2713aSLionel Sambuc ForceColumnInfo = FD->isInlineSpecified();
3340f4a2713aSLionel Sambuc
3341*0a6a1f1dSLionel Sambuc if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function) &&
3342f4a2713aSLionel Sambuc (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
3343f4a2713aSLionel Sambuc if (llvm::Constant *PrefixSig =
3344f4a2713aSLionel Sambuc CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) {
3345*0a6a1f1dSLionel Sambuc SanitizerScope SanScope(this);
3346f4a2713aSLionel Sambuc llvm::Constant *FTRTTIConst =
3347f4a2713aSLionel Sambuc CGM.GetAddrOfRTTIDescriptor(QualType(FnType, 0), /*ForEH=*/true);
3348f4a2713aSLionel Sambuc llvm::Type *PrefixStructTyElems[] = {
3349f4a2713aSLionel Sambuc PrefixSig->getType(),
3350f4a2713aSLionel Sambuc FTRTTIConst->getType()
3351f4a2713aSLionel Sambuc };
3352f4a2713aSLionel Sambuc llvm::StructType *PrefixStructTy = llvm::StructType::get(
3353f4a2713aSLionel Sambuc CGM.getLLVMContext(), PrefixStructTyElems, /*isPacked=*/true);
3354f4a2713aSLionel Sambuc
3355f4a2713aSLionel Sambuc llvm::Value *CalleePrefixStruct = Builder.CreateBitCast(
3356f4a2713aSLionel Sambuc Callee, llvm::PointerType::getUnqual(PrefixStructTy));
3357f4a2713aSLionel Sambuc llvm::Value *CalleeSigPtr =
3358f4a2713aSLionel Sambuc Builder.CreateConstGEP2_32(CalleePrefixStruct, 0, 0);
3359f4a2713aSLionel Sambuc llvm::Value *CalleeSig = Builder.CreateLoad(CalleeSigPtr);
3360f4a2713aSLionel Sambuc llvm::Value *CalleeSigMatch = Builder.CreateICmpEQ(CalleeSig, PrefixSig);
3361f4a2713aSLionel Sambuc
3362f4a2713aSLionel Sambuc llvm::BasicBlock *Cont = createBasicBlock("cont");
3363f4a2713aSLionel Sambuc llvm::BasicBlock *TypeCheck = createBasicBlock("typecheck");
3364f4a2713aSLionel Sambuc Builder.CreateCondBr(CalleeSigMatch, TypeCheck, Cont);
3365f4a2713aSLionel Sambuc
3366f4a2713aSLionel Sambuc EmitBlock(TypeCheck);
3367f4a2713aSLionel Sambuc llvm::Value *CalleeRTTIPtr =
3368f4a2713aSLionel Sambuc Builder.CreateConstGEP2_32(CalleePrefixStruct, 0, 1);
3369f4a2713aSLionel Sambuc llvm::Value *CalleeRTTI = Builder.CreateLoad(CalleeRTTIPtr);
3370f4a2713aSLionel Sambuc llvm::Value *CalleeRTTIMatch =
3371f4a2713aSLionel Sambuc Builder.CreateICmpEQ(CalleeRTTI, FTRTTIConst);
3372f4a2713aSLionel Sambuc llvm::Constant *StaticData[] = {
3373*0a6a1f1dSLionel Sambuc EmitCheckSourceLocation(E->getLocStart()),
3374f4a2713aSLionel Sambuc EmitCheckTypeDescriptor(CalleeType)
3375f4a2713aSLionel Sambuc };
3376*0a6a1f1dSLionel Sambuc EmitCheck(std::make_pair(CalleeRTTIMatch, SanitizerKind::Function),
3377*0a6a1f1dSLionel Sambuc "function_type_mismatch", StaticData, Callee);
3378f4a2713aSLionel Sambuc
3379f4a2713aSLionel Sambuc Builder.CreateBr(Cont);
3380f4a2713aSLionel Sambuc EmitBlock(Cont);
3381f4a2713aSLionel Sambuc }
3382f4a2713aSLionel Sambuc }
3383f4a2713aSLionel Sambuc
3384f4a2713aSLionel Sambuc CallArgList Args;
3385*0a6a1f1dSLionel Sambuc if (Chain)
3386*0a6a1f1dSLionel Sambuc Args.add(RValue::get(Builder.CreateBitCast(Chain, CGM.VoidPtrTy)),
3387*0a6a1f1dSLionel Sambuc CGM.getContext().VoidPtrTy);
3388*0a6a1f1dSLionel Sambuc EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), E->arg_begin(),
3389*0a6a1f1dSLionel Sambuc E->arg_end(), E->getDirectCallee(), /*ParamsToSkip*/ 0,
3390f4a2713aSLionel Sambuc ForceColumnInfo);
3391f4a2713aSLionel Sambuc
3392*0a6a1f1dSLionel Sambuc const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeFreeFunctionCall(
3393*0a6a1f1dSLionel Sambuc Args, FnType, /*isChainCall=*/Chain);
3394f4a2713aSLionel Sambuc
3395f4a2713aSLionel Sambuc // C99 6.5.2.2p6:
3396f4a2713aSLionel Sambuc // If the expression that denotes the called function has a type
3397f4a2713aSLionel Sambuc // that does not include a prototype, [the default argument
3398f4a2713aSLionel Sambuc // promotions are performed]. If the number of arguments does not
3399f4a2713aSLionel Sambuc // equal the number of parameters, the behavior is undefined. If
3400f4a2713aSLionel Sambuc // the function is defined with a type that includes a prototype,
3401f4a2713aSLionel Sambuc // and either the prototype ends with an ellipsis (, ...) or the
3402f4a2713aSLionel Sambuc // types of the arguments after promotion are not compatible with
3403f4a2713aSLionel Sambuc // the types of the parameters, the behavior is undefined. If the
3404f4a2713aSLionel Sambuc // function is defined with a type that does not include a
3405f4a2713aSLionel Sambuc // prototype, and the types of the arguments after promotion are
3406f4a2713aSLionel Sambuc // not compatible with those of the parameters after promotion,
3407f4a2713aSLionel Sambuc // the behavior is undefined [except in some trivial cases].
3408f4a2713aSLionel Sambuc // That is, in the general case, we should assume that a call
3409f4a2713aSLionel Sambuc // through an unprototyped function type works like a *non-variadic*
3410f4a2713aSLionel Sambuc // call. The way we make this work is to cast to the exact type
3411f4a2713aSLionel Sambuc // of the promoted arguments.
3412*0a6a1f1dSLionel Sambuc //
3413*0a6a1f1dSLionel Sambuc // Chain calls use this same code path to add the invisible chain parameter
3414*0a6a1f1dSLionel Sambuc // to the function type.
3415*0a6a1f1dSLionel Sambuc if (isa<FunctionNoProtoType>(FnType) || Chain) {
3416f4a2713aSLionel Sambuc llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo);
3417f4a2713aSLionel Sambuc CalleeTy = CalleeTy->getPointerTo();
3418f4a2713aSLionel Sambuc Callee = Builder.CreateBitCast(Callee, CalleeTy, "callee.knr.cast");
3419f4a2713aSLionel Sambuc }
3420f4a2713aSLionel Sambuc
3421f4a2713aSLionel Sambuc return EmitCall(FnInfo, Callee, ReturnValue, Args, TargetDecl);
3422f4a2713aSLionel Sambuc }
3423f4a2713aSLionel Sambuc
3424f4a2713aSLionel Sambuc LValue CodeGenFunction::
EmitPointerToDataMemberBinaryExpr(const BinaryOperator * E)3425f4a2713aSLionel Sambuc EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
3426f4a2713aSLionel Sambuc llvm::Value *BaseV;
3427f4a2713aSLionel Sambuc if (E->getOpcode() == BO_PtrMemI)
3428f4a2713aSLionel Sambuc BaseV = EmitScalarExpr(E->getLHS());
3429f4a2713aSLionel Sambuc else
3430f4a2713aSLionel Sambuc BaseV = EmitLValue(E->getLHS()).getAddress();
3431f4a2713aSLionel Sambuc
3432f4a2713aSLionel Sambuc llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
3433f4a2713aSLionel Sambuc
3434f4a2713aSLionel Sambuc const MemberPointerType *MPT
3435f4a2713aSLionel Sambuc = E->getRHS()->getType()->getAs<MemberPointerType>();
3436f4a2713aSLionel Sambuc
3437*0a6a1f1dSLionel Sambuc llvm::Value *AddV = CGM.getCXXABI().EmitMemberDataPointerAddress(
3438*0a6a1f1dSLionel Sambuc *this, E, BaseV, OffsetV, MPT);
3439f4a2713aSLionel Sambuc
3440f4a2713aSLionel Sambuc return MakeAddrLValue(AddV, MPT->getPointeeType());
3441f4a2713aSLionel Sambuc }
3442f4a2713aSLionel Sambuc
3443f4a2713aSLionel Sambuc /// Given the address of a temporary variable, produce an r-value of
3444f4a2713aSLionel Sambuc /// its type.
convertTempToRValue(llvm::Value * addr,QualType type,SourceLocation loc)3445f4a2713aSLionel Sambuc RValue CodeGenFunction::convertTempToRValue(llvm::Value *addr,
3446f4a2713aSLionel Sambuc QualType type,
3447f4a2713aSLionel Sambuc SourceLocation loc) {
3448f4a2713aSLionel Sambuc LValue lvalue = MakeNaturalAlignAddrLValue(addr, type);
3449f4a2713aSLionel Sambuc switch (getEvaluationKind(type)) {
3450f4a2713aSLionel Sambuc case TEK_Complex:
3451f4a2713aSLionel Sambuc return RValue::getComplex(EmitLoadOfComplex(lvalue, loc));
3452f4a2713aSLionel Sambuc case TEK_Aggregate:
3453f4a2713aSLionel Sambuc return lvalue.asAggregateRValue();
3454f4a2713aSLionel Sambuc case TEK_Scalar:
3455f4a2713aSLionel Sambuc return RValue::get(EmitLoadOfScalar(lvalue, loc));
3456f4a2713aSLionel Sambuc }
3457f4a2713aSLionel Sambuc llvm_unreachable("bad evaluation kind");
3458f4a2713aSLionel Sambuc }
3459f4a2713aSLionel Sambuc
SetFPAccuracy(llvm::Value * Val,float Accuracy)3460f4a2713aSLionel Sambuc void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) {
3461f4a2713aSLionel Sambuc assert(Val->getType()->isFPOrFPVectorTy());
3462f4a2713aSLionel Sambuc if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val))
3463f4a2713aSLionel Sambuc return;
3464f4a2713aSLionel Sambuc
3465f4a2713aSLionel Sambuc llvm::MDBuilder MDHelper(getLLVMContext());
3466f4a2713aSLionel Sambuc llvm::MDNode *Node = MDHelper.createFPMath(Accuracy);
3467f4a2713aSLionel Sambuc
3468f4a2713aSLionel Sambuc cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node);
3469f4a2713aSLionel Sambuc }
3470f4a2713aSLionel Sambuc
3471f4a2713aSLionel Sambuc namespace {
3472f4a2713aSLionel Sambuc struct LValueOrRValue {
3473f4a2713aSLionel Sambuc LValue LV;
3474f4a2713aSLionel Sambuc RValue RV;
3475f4a2713aSLionel Sambuc };
3476f4a2713aSLionel Sambuc }
3477f4a2713aSLionel Sambuc
emitPseudoObjectExpr(CodeGenFunction & CGF,const PseudoObjectExpr * E,bool forLValue,AggValueSlot slot)3478f4a2713aSLionel Sambuc static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF,
3479f4a2713aSLionel Sambuc const PseudoObjectExpr *E,
3480f4a2713aSLionel Sambuc bool forLValue,
3481f4a2713aSLionel Sambuc AggValueSlot slot) {
3482f4a2713aSLionel Sambuc SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
3483f4a2713aSLionel Sambuc
3484f4a2713aSLionel Sambuc // Find the result expression, if any.
3485f4a2713aSLionel Sambuc const Expr *resultExpr = E->getResultExpr();
3486f4a2713aSLionel Sambuc LValueOrRValue result;
3487f4a2713aSLionel Sambuc
3488f4a2713aSLionel Sambuc for (PseudoObjectExpr::const_semantics_iterator
3489f4a2713aSLionel Sambuc i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
3490f4a2713aSLionel Sambuc const Expr *semantic = *i;
3491f4a2713aSLionel Sambuc
3492f4a2713aSLionel Sambuc // If this semantic expression is an opaque value, bind it
3493f4a2713aSLionel Sambuc // to the result of its source expression.
3494*0a6a1f1dSLionel Sambuc if (const auto *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
3495f4a2713aSLionel Sambuc
3496f4a2713aSLionel Sambuc // If this is the result expression, we may need to evaluate
3497f4a2713aSLionel Sambuc // directly into the slot.
3498f4a2713aSLionel Sambuc typedef CodeGenFunction::OpaqueValueMappingData OVMA;
3499f4a2713aSLionel Sambuc OVMA opaqueData;
3500f4a2713aSLionel Sambuc if (ov == resultExpr && ov->isRValue() && !forLValue &&
3501f4a2713aSLionel Sambuc CodeGenFunction::hasAggregateEvaluationKind(ov->getType())) {
3502f4a2713aSLionel Sambuc CGF.EmitAggExpr(ov->getSourceExpr(), slot);
3503f4a2713aSLionel Sambuc
3504f4a2713aSLionel Sambuc LValue LV = CGF.MakeAddrLValue(slot.getAddr(), ov->getType());
3505f4a2713aSLionel Sambuc opaqueData = OVMA::bind(CGF, ov, LV);
3506f4a2713aSLionel Sambuc result.RV = slot.asRValue();
3507f4a2713aSLionel Sambuc
3508f4a2713aSLionel Sambuc // Otherwise, emit as normal.
3509f4a2713aSLionel Sambuc } else {
3510f4a2713aSLionel Sambuc opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
3511f4a2713aSLionel Sambuc
3512f4a2713aSLionel Sambuc // If this is the result, also evaluate the result now.
3513f4a2713aSLionel Sambuc if (ov == resultExpr) {
3514f4a2713aSLionel Sambuc if (forLValue)
3515f4a2713aSLionel Sambuc result.LV = CGF.EmitLValue(ov);
3516f4a2713aSLionel Sambuc else
3517f4a2713aSLionel Sambuc result.RV = CGF.EmitAnyExpr(ov, slot);
3518f4a2713aSLionel Sambuc }
3519f4a2713aSLionel Sambuc }
3520f4a2713aSLionel Sambuc
3521f4a2713aSLionel Sambuc opaques.push_back(opaqueData);
3522f4a2713aSLionel Sambuc
3523f4a2713aSLionel Sambuc // Otherwise, if the expression is the result, evaluate it
3524f4a2713aSLionel Sambuc // and remember the result.
3525f4a2713aSLionel Sambuc } else if (semantic == resultExpr) {
3526f4a2713aSLionel Sambuc if (forLValue)
3527f4a2713aSLionel Sambuc result.LV = CGF.EmitLValue(semantic);
3528f4a2713aSLionel Sambuc else
3529f4a2713aSLionel Sambuc result.RV = CGF.EmitAnyExpr(semantic, slot);
3530f4a2713aSLionel Sambuc
3531f4a2713aSLionel Sambuc // Otherwise, evaluate the expression in an ignored context.
3532f4a2713aSLionel Sambuc } else {
3533f4a2713aSLionel Sambuc CGF.EmitIgnoredExpr(semantic);
3534f4a2713aSLionel Sambuc }
3535f4a2713aSLionel Sambuc }
3536f4a2713aSLionel Sambuc
3537f4a2713aSLionel Sambuc // Unbind all the opaques now.
3538f4a2713aSLionel Sambuc for (unsigned i = 0, e = opaques.size(); i != e; ++i)
3539f4a2713aSLionel Sambuc opaques[i].unbind(CGF);
3540f4a2713aSLionel Sambuc
3541f4a2713aSLionel Sambuc return result;
3542f4a2713aSLionel Sambuc }
3543f4a2713aSLionel Sambuc
EmitPseudoObjectRValue(const PseudoObjectExpr * E,AggValueSlot slot)3544f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitPseudoObjectRValue(const PseudoObjectExpr *E,
3545f4a2713aSLionel Sambuc AggValueSlot slot) {
3546f4a2713aSLionel Sambuc return emitPseudoObjectExpr(*this, E, false, slot).RV;
3547f4a2713aSLionel Sambuc }
3548f4a2713aSLionel Sambuc
EmitPseudoObjectLValue(const PseudoObjectExpr * E)3549f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitPseudoObjectLValue(const PseudoObjectExpr *E) {
3550f4a2713aSLionel Sambuc return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV;
3551f4a2713aSLionel Sambuc }
3552