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