Bug Summary

File:clang/lib/CodeGen/CGExpr.cpp
Warning:line 1936, column 37
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name CGExpr.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/clang/lib/CodeGen -I /build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/clang/lib/CodeGen -I /build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/clang/include -I tools/clang/include -I include -I /build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-01-08-143526-16334-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/clang/lib/CodeGen/CGExpr.cpp

/build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/clang/lib/CodeGen/CGExpr.cpp

1//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code to emit Expr nodes as LLVM code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CGCUDARuntime.h"
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 "ConstantEmitter.h"
24#include "TargetInfo.h"
25#include "clang/AST/ASTContext.h"
26#include "clang/AST/Attr.h"
27#include "clang/AST/DeclObjC.h"
28#include "clang/AST/NSAPI.h"
29#include "clang/Basic/Builtins.h"
30#include "clang/Basic/CodeGenOptions.h"
31#include "clang/Basic/SourceManager.h"
32#include "llvm/ADT/Hashing.h"
33#include "llvm/ADT/StringExtras.h"
34#include "llvm/IR/DataLayout.h"
35#include "llvm/IR/Intrinsics.h"
36#include "llvm/IR/LLVMContext.h"
37#include "llvm/IR/MDBuilder.h"
38#include "llvm/IR/MatrixBuilder.h"
39#include "llvm/Support/ConvertUTF.h"
40#include "llvm/Support/MathExtras.h"
41#include "llvm/Support/Path.h"
42#include "llvm/Support/SaveAndRestore.h"
43#include "llvm/Transforms/Utils/SanitizerStats.h"
44
45#include <string>
46
47using namespace clang;
48using namespace CodeGen;
49
50//===--------------------------------------------------------------------===//
51// Miscellaneous Helper Methods
52//===--------------------------------------------------------------------===//
53
54llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) {
55 unsigned addressSpace =
56 cast<llvm::PointerType>(value->getType())->getAddressSpace();
57
58 llvm::PointerType *destType = Int8PtrTy;
59 if (addressSpace)
60 destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace);
61
62 if (value->getType() == destType) return value;
63 return Builder.CreateBitCast(value, destType);
64}
65
66/// CreateTempAlloca - This creates a alloca and inserts it into the entry
67/// block.
68Address CodeGenFunction::CreateTempAllocaWithoutCast(llvm::Type *Ty,
69 CharUnits Align,
70 const Twine &Name,
71 llvm::Value *ArraySize) {
72 auto Alloca = CreateTempAlloca(Ty, Name, ArraySize);
73 Alloca->setAlignment(Align.getAsAlign());
74 return Address(Alloca, Ty, Align);
75}
76
77/// CreateTempAlloca - This creates a alloca and inserts it into the entry
78/// block. The alloca is casted to default address space if necessary.
79Address CodeGenFunction::CreateTempAlloca(llvm::Type *Ty, CharUnits Align,
80 const Twine &Name,
81 llvm::Value *ArraySize,
82 Address *AllocaAddr) {
83 auto Alloca = CreateTempAllocaWithoutCast(Ty, Align, Name, ArraySize);
84 if (AllocaAddr)
85 *AllocaAddr = Alloca;
86 llvm::Value *V = Alloca.getPointer();
87 // Alloca always returns a pointer in alloca address space, which may
88 // be different from the type defined by the language. For example,
89 // in C++ the auto variables are in the default address space. Therefore
90 // cast alloca to the default address space when necessary.
91 if (getASTAllocaAddressSpace() != LangAS::Default) {
92 auto DestAddrSpace = getContext().getTargetAddressSpace(LangAS::Default);
93 llvm::IRBuilderBase::InsertPointGuard IPG(Builder);
94 // When ArraySize is nullptr, alloca is inserted at AllocaInsertPt,
95 // otherwise alloca is inserted at the current insertion point of the
96 // builder.
97 if (!ArraySize)
98 Builder.SetInsertPoint(getPostAllocaInsertPoint());
99 V = getTargetHooks().performAddrSpaceCast(
100 *this, V, getASTAllocaAddressSpace(), LangAS::Default,
101 Ty->getPointerTo(DestAddrSpace), /*non-null*/ true);
102 }
103
104 return Address(V, Ty, Align);
105}
106
107/// CreateTempAlloca - This creates an alloca and inserts it into the entry
108/// block if \p ArraySize is nullptr, otherwise inserts it at the current
109/// insertion point of the builder.
110llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
111 const Twine &Name,
112 llvm::Value *ArraySize) {
113 if (ArraySize)
114 return Builder.CreateAlloca(Ty, ArraySize, Name);
115 return new llvm::AllocaInst(Ty, CGM.getDataLayout().getAllocaAddrSpace(),
116 ArraySize, Name, AllocaInsertPt);
117}
118
119/// CreateDefaultAlignTempAlloca - This creates an alloca with the
120/// default alignment of the corresponding LLVM type, which is *not*
121/// guaranteed to be related in any way to the expected alignment of
122/// an AST type that might have been lowered to Ty.
123Address CodeGenFunction::CreateDefaultAlignTempAlloca(llvm::Type *Ty,
124 const Twine &Name) {
125 CharUnits Align =
126 CharUnits::fromQuantity(CGM.getDataLayout().getPrefTypeAlignment(Ty));
127 return CreateTempAlloca(Ty, Align, Name);
128}
129
130Address CodeGenFunction::CreateIRTemp(QualType Ty, const Twine &Name) {
131 CharUnits Align = getContext().getTypeAlignInChars(Ty);
132 return CreateTempAlloca(ConvertType(Ty), Align, Name);
133}
134
135Address CodeGenFunction::CreateMemTemp(QualType Ty, const Twine &Name,
136 Address *Alloca) {
137 // FIXME: Should we prefer the preferred type alignment here?
138 return CreateMemTemp(Ty, getContext().getTypeAlignInChars(Ty), Name, Alloca);
139}
140
141Address CodeGenFunction::CreateMemTemp(QualType Ty, CharUnits Align,
142 const Twine &Name, Address *Alloca) {
143 Address Result = CreateTempAlloca(ConvertTypeForMem(Ty), Align, Name,
144 /*ArraySize=*/nullptr, Alloca);
145
146 if (Ty->isConstantMatrixType()) {
147 auto *ArrayTy = cast<llvm::ArrayType>(Result.getElementType());
148 auto *VectorTy = llvm::FixedVectorType::get(ArrayTy->getElementType(),
149 ArrayTy->getNumElements());
150
151 Result = Address(
152 Builder.CreateBitCast(Result.getPointer(), VectorTy->getPointerTo()),
153 Result.getAlignment());
154 }
155 return Result;
156}
157
158Address CodeGenFunction::CreateMemTempWithoutCast(QualType Ty, CharUnits Align,
159 const Twine &Name) {
160 return CreateTempAllocaWithoutCast(ConvertTypeForMem(Ty), Align, Name);
161}
162
163Address CodeGenFunction::CreateMemTempWithoutCast(QualType Ty,
164 const Twine &Name) {
165 return CreateMemTempWithoutCast(Ty, getContext().getTypeAlignInChars(Ty),
166 Name);
167}
168
169/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
170/// expression and compare the result against zero, returning an Int1Ty value.
171llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
172 PGO.setCurrentStmt(E);
173 if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
174 llvm::Value *MemPtr = EmitScalarExpr(E);
175 return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT);
176 }
177
178 QualType BoolTy = getContext().BoolTy;
179 SourceLocation Loc = E->getExprLoc();
180 CGFPOptionsRAII FPOptsRAII(*this, E);
181 if (!E->getType()->isAnyComplexType())
182 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy, Loc);
183
184 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(), BoolTy,
185 Loc);
186}
187
188/// EmitIgnoredExpr - Emit code to compute the specified expression,
189/// ignoring the result.
190void CodeGenFunction::EmitIgnoredExpr(const Expr *E) {
191 if (E->isPRValue())
192 return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true);
193
194 // Just emit it as an l-value and drop the result.
195 EmitLValue(E);
196}
197
198/// EmitAnyExpr - Emit code to compute the specified expression which
199/// can have any type. The result is returned as an RValue struct.
200/// If this is an aggregate expression, AggSlot indicates where the
201/// result should be returned.
202RValue CodeGenFunction::EmitAnyExpr(const Expr *E,
203 AggValueSlot aggSlot,
204 bool ignoreResult) {
205 switch (getEvaluationKind(E->getType())) {
206 case TEK_Scalar:
207 return RValue::get(EmitScalarExpr(E, ignoreResult));
208 case TEK_Complex:
209 return RValue::getComplex(EmitComplexExpr(E, ignoreResult, ignoreResult));
210 case TEK_Aggregate:
211 if (!ignoreResult && aggSlot.isIgnored())
212 aggSlot = CreateAggTemp(E->getType(), "agg-temp");
213 EmitAggExpr(E, aggSlot);
214 return aggSlot.asRValue();
215 }
216 llvm_unreachable("bad evaluation kind")::llvm::llvm_unreachable_internal("bad evaluation kind", "clang/lib/CodeGen/CGExpr.cpp"
, 216)
;
217}
218
219/// EmitAnyExprToTemp - Similar to EmitAnyExpr(), however, the result will
220/// always be accessible even if no aggregate location is provided.
221RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) {
222 AggValueSlot AggSlot = AggValueSlot::ignored();
223
224 if (hasAggregateEvaluationKind(E->getType()))
225 AggSlot = CreateAggTemp(E->getType(), "agg.tmp");
226 return EmitAnyExpr(E, AggSlot);
227}
228
229/// EmitAnyExprToMem - Evaluate an expression into a given memory
230/// location.
231void CodeGenFunction::EmitAnyExprToMem(const Expr *E,
232 Address Location,
233 Qualifiers Quals,
234 bool IsInit) {
235 // FIXME: This function should take an LValue as an argument.
236 switch (getEvaluationKind(E->getType())) {
237 case TEK_Complex:
238 EmitComplexExprIntoLValue(E, MakeAddrLValue(Location, E->getType()),
239 /*isInit*/ false);
240 return;
241
242 case TEK_Aggregate: {
243 EmitAggExpr(E, AggValueSlot::forAddr(Location, Quals,
244 AggValueSlot::IsDestructed_t(IsInit),
245 AggValueSlot::DoesNotNeedGCBarriers,
246 AggValueSlot::IsAliased_t(!IsInit),
247 AggValueSlot::MayOverlap));
248 return;
249 }
250
251 case TEK_Scalar: {
252 RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
253 LValue LV = MakeAddrLValue(Location, E->getType());
254 EmitStoreThroughLValue(RV, LV);
255 return;
256 }
257 }
258 llvm_unreachable("bad evaluation kind")::llvm::llvm_unreachable_internal("bad evaluation kind", "clang/lib/CodeGen/CGExpr.cpp"
, 258)
;
259}
260
261static void
262pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
263 const Expr *E, Address ReferenceTemporary) {
264 // Objective-C++ ARC:
265 // If we are binding a reference to a temporary that has ownership, we
266 // need to perform retain/release operations on the temporary.
267 //
268 // FIXME: This should be looking at E, not M.
269 if (auto Lifetime = M->getType().getObjCLifetime()) {
270 switch (Lifetime) {
271 case Qualifiers::OCL_None:
272 case Qualifiers::OCL_ExplicitNone:
273 // Carry on to normal cleanup handling.
274 break;
275
276 case Qualifiers::OCL_Autoreleasing:
277 // Nothing to do; cleaned up by an autorelease pool.
278 return;
279
280 case Qualifiers::OCL_Strong:
281 case Qualifiers::OCL_Weak:
282 switch (StorageDuration Duration = M->getStorageDuration()) {
283 case SD_Static:
284 // Note: we intentionally do not register a cleanup to release
285 // the object on program termination.
286 return;
287
288 case SD_Thread:
289 // FIXME: We should probably register a cleanup in this case.
290 return;
291
292 case SD_Automatic:
293 case SD_FullExpression:
294 CodeGenFunction::Destroyer *Destroy;
295 CleanupKind CleanupKind;
296 if (Lifetime == Qualifiers::OCL_Strong) {
297 const ValueDecl *VD = M->getExtendingDecl();
298 bool Precise =
299 VD && isa<VarDecl>(VD) && VD->hasAttr<ObjCPreciseLifetimeAttr>();
300 CleanupKind = CGF.getARCCleanupKind();
301 Destroy = Precise ? &CodeGenFunction::destroyARCStrongPrecise
302 : &CodeGenFunction::destroyARCStrongImprecise;
303 } else {
304 // __weak objects always get EH cleanups; otherwise, exceptions
305 // could cause really nasty crashes instead of mere leaks.
306 CleanupKind = NormalAndEHCleanup;
307 Destroy = &CodeGenFunction::destroyARCWeak;
308 }
309 if (Duration == SD_FullExpression)
310 CGF.pushDestroy(CleanupKind, ReferenceTemporary,
311 M->getType(), *Destroy,
312 CleanupKind & EHCleanup);
313 else
314 CGF.pushLifetimeExtendedDestroy(CleanupKind, ReferenceTemporary,
315 M->getType(),
316 *Destroy, CleanupKind & EHCleanup);
317 return;
318
319 case SD_Dynamic:
320 llvm_unreachable("temporary cannot have dynamic storage duration")::llvm::llvm_unreachable_internal("temporary cannot have dynamic storage duration"
, "clang/lib/CodeGen/CGExpr.cpp", 320)
;
321 }
322 llvm_unreachable("unknown storage duration")::llvm::llvm_unreachable_internal("unknown storage duration",
"clang/lib/CodeGen/CGExpr.cpp", 322)
;
323 }
324 }
325
326 CXXDestructorDecl *ReferenceTemporaryDtor = nullptr;
327 if (const RecordType *RT =
328 E->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) {
329 // Get the destructor for the reference temporary.
330 auto *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
331 if (!ClassDecl->hasTrivialDestructor())
332 ReferenceTemporaryDtor = ClassDecl->getDestructor();
333 }
334
335 if (!ReferenceTemporaryDtor)
336 return;
337
338 // Call the destructor for the temporary.
339 switch (M->getStorageDuration()) {
340 case SD_Static:
341 case SD_Thread: {
342 llvm::FunctionCallee CleanupFn;
343 llvm::Constant *CleanupArg;
344 if (E->getType()->isArrayType()) {
345 CleanupFn = CodeGenFunction(CGF.CGM).generateDestroyHelper(
346 ReferenceTemporary, E->getType(),
347 CodeGenFunction::destroyCXXObject, CGF.getLangOpts().Exceptions,
348 dyn_cast_or_null<VarDecl>(M->getExtendingDecl()));
349 CleanupArg = llvm::Constant::getNullValue(CGF.Int8PtrTy);
350 } else {
351 CleanupFn = CGF.CGM.getAddrAndTypeOfCXXStructor(
352 GlobalDecl(ReferenceTemporaryDtor, Dtor_Complete));
353 CleanupArg = cast<llvm::Constant>(ReferenceTemporary.getPointer());
354 }
355 CGF.CGM.getCXXABI().registerGlobalDtor(
356 CGF, *cast<VarDecl>(M->getExtendingDecl()), CleanupFn, CleanupArg);
357 break;
358 }
359
360 case SD_FullExpression:
361 CGF.pushDestroy(NormalAndEHCleanup, ReferenceTemporary, E->getType(),
362 CodeGenFunction::destroyCXXObject,
363 CGF.getLangOpts().Exceptions);
364 break;
365
366 case SD_Automatic:
367 CGF.pushLifetimeExtendedDestroy(NormalAndEHCleanup,
368 ReferenceTemporary, E->getType(),
369 CodeGenFunction::destroyCXXObject,
370 CGF.getLangOpts().Exceptions);
371 break;
372
373 case SD_Dynamic:
374 llvm_unreachable("temporary cannot have dynamic storage duration")::llvm::llvm_unreachable_internal("temporary cannot have dynamic storage duration"
, "clang/lib/CodeGen/CGExpr.cpp", 374)
;
375 }
376}
377
378static Address createReferenceTemporary(CodeGenFunction &CGF,
379 const MaterializeTemporaryExpr *M,
380 const Expr *Inner,
381 Address *Alloca = nullptr) {
382 auto &TCG = CGF.getTargetHooks();
383 switch (M->getStorageDuration()) {
384 case SD_FullExpression:
385 case SD_Automatic: {
386 // If we have a constant temporary array or record try to promote it into a
387 // constant global under the same rules a normal constant would've been
388 // promoted. This is easier on the optimizer and generally emits fewer
389 // instructions.
390 QualType Ty = Inner->getType();
391 if (CGF.CGM.getCodeGenOpts().MergeAllConstants &&
392 (Ty->isArrayType() || Ty->isRecordType()) &&
393 CGF.CGM.isTypeConstant(Ty, true))
394 if (auto Init = ConstantEmitter(CGF).tryEmitAbstract(Inner, Ty)) {
395 auto AS = CGF.CGM.GetGlobalConstantAddressSpace();
396 auto *GV = new llvm::GlobalVariable(
397 CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true,
398 llvm::GlobalValue::PrivateLinkage, Init, ".ref.tmp", nullptr,
399 llvm::GlobalValue::NotThreadLocal,
400 CGF.getContext().getTargetAddressSpace(AS));
401 CharUnits alignment = CGF.getContext().getTypeAlignInChars(Ty);
402 GV->setAlignment(alignment.getAsAlign());
403 llvm::Constant *C = GV;
404 if (AS != LangAS::Default)
405 C = TCG.performAddrSpaceCast(
406 CGF.CGM, GV, AS, LangAS::Default,
407 GV->getValueType()->getPointerTo(
408 CGF.getContext().getTargetAddressSpace(LangAS::Default)));
409 // FIXME: Should we put the new global into a COMDAT?
410 return Address(C, alignment);
411 }
412 return CGF.CreateMemTemp(Ty, "ref.tmp", Alloca);
413 }
414 case SD_Thread:
415 case SD_Static:
416 return CGF.CGM.GetAddrOfGlobalTemporary(M, Inner);
417
418 case SD_Dynamic:
419 llvm_unreachable("temporary can't have dynamic storage duration")::llvm::llvm_unreachable_internal("temporary can't have dynamic storage duration"
, "clang/lib/CodeGen/CGExpr.cpp", 419)
;
420 }
421 llvm_unreachable("unknown storage duration")::llvm::llvm_unreachable_internal("unknown storage duration",
"clang/lib/CodeGen/CGExpr.cpp", 421)
;
422}
423
424/// Helper method to check if the underlying ABI is AAPCS
425static bool isAAPCS(const TargetInfo &TargetInfo) {
426 return TargetInfo.getABI().startswith("aapcs");
427}
428
429LValue CodeGenFunction::
430EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) {
431 const Expr *E = M->getSubExpr();
432
433 assert((!M->getExtendingDecl() || !isa<VarDecl>(M->getExtendingDecl()) ||(static_cast <bool> ((!M->getExtendingDecl() || !isa
<VarDecl>(M->getExtendingDecl()) || !cast<VarDecl
>(M->getExtendingDecl())->isARCPseudoStrong()) &&
"Reference should never be pseudo-strong!") ? void (0) : __assert_fail
("(!M->getExtendingDecl() || !isa<VarDecl>(M->getExtendingDecl()) || !cast<VarDecl>(M->getExtendingDecl())->isARCPseudoStrong()) && \"Reference should never be pseudo-strong!\""
, "clang/lib/CodeGen/CGExpr.cpp", 435, __extension__ __PRETTY_FUNCTION__
))
434 !cast<VarDecl>(M->getExtendingDecl())->isARCPseudoStrong()) &&(static_cast <bool> ((!M->getExtendingDecl() || !isa
<VarDecl>(M->getExtendingDecl()) || !cast<VarDecl
>(M->getExtendingDecl())->isARCPseudoStrong()) &&
"Reference should never be pseudo-strong!") ? void (0) : __assert_fail
("(!M->getExtendingDecl() || !isa<VarDecl>(M->getExtendingDecl()) || !cast<VarDecl>(M->getExtendingDecl())->isARCPseudoStrong()) && \"Reference should never be pseudo-strong!\""
, "clang/lib/CodeGen/CGExpr.cpp", 435, __extension__ __PRETTY_FUNCTION__
))
435 "Reference should never be pseudo-strong!")(static_cast <bool> ((!M->getExtendingDecl() || !isa
<VarDecl>(M->getExtendingDecl()) || !cast<VarDecl
>(M->getExtendingDecl())->isARCPseudoStrong()) &&
"Reference should never be pseudo-strong!") ? void (0) : __assert_fail
("(!M->getExtendingDecl() || !isa<VarDecl>(M->getExtendingDecl()) || !cast<VarDecl>(M->getExtendingDecl())->isARCPseudoStrong()) && \"Reference should never be pseudo-strong!\""
, "clang/lib/CodeGen/CGExpr.cpp", 435, __extension__ __PRETTY_FUNCTION__
))
;
436
437 // FIXME: ideally this would use EmitAnyExprToMem, however, we cannot do so
438 // as that will cause the lifetime adjustment to be lost for ARC
439 auto ownership = M->getType().getObjCLifetime();
440 if (ownership != Qualifiers::OCL_None &&
441 ownership != Qualifiers::OCL_ExplicitNone) {
442 Address Object = createReferenceTemporary(*this, M, E);
443 if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object.getPointer())) {
444 Object = Address(llvm::ConstantExpr::getBitCast(Var,
445 ConvertTypeForMem(E->getType())
446 ->getPointerTo(Object.getAddressSpace())),
447 Object.getAlignment());
448
449 // createReferenceTemporary will promote the temporary to a global with a
450 // constant initializer if it can. It can only do this to a value of
451 // ARC-manageable type if the value is global and therefore "immune" to
452 // ref-counting operations. Therefore we have no need to emit either a
453 // dynamic initialization or a cleanup and we can just return the address
454 // of the temporary.
455 if (Var->hasInitializer())
456 return MakeAddrLValue(Object, M->getType(), AlignmentSource::Decl);
457
458 Var->setInitializer(CGM.EmitNullConstant(E->getType()));
459 }
460 LValue RefTempDst = MakeAddrLValue(Object, M->getType(),
461 AlignmentSource::Decl);
462
463 switch (getEvaluationKind(E->getType())) {
464 default: llvm_unreachable("expected scalar or aggregate expression")::llvm::llvm_unreachable_internal("expected scalar or aggregate expression"
, "clang/lib/CodeGen/CGExpr.cpp", 464)
;
465 case TEK_Scalar:
466 EmitScalarInit(E, M->getExtendingDecl(), RefTempDst, false);
467 break;
468 case TEK_Aggregate: {
469 EmitAggExpr(E, AggValueSlot::forAddr(Object,
470 E->getType().getQualifiers(),
471 AggValueSlot::IsDestructed,
472 AggValueSlot::DoesNotNeedGCBarriers,
473 AggValueSlot::IsNotAliased,
474 AggValueSlot::DoesNotOverlap));
475 break;
476 }
477 }
478
479 pushTemporaryCleanup(*this, M, E, Object);
480 return RefTempDst;
481 }
482
483 SmallVector<const Expr *, 2> CommaLHSs;
484 SmallVector<SubobjectAdjustment, 2> Adjustments;
485 E = E->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
486
487 for (const auto &Ignored : CommaLHSs)
488 EmitIgnoredExpr(Ignored);
489
490 if (const auto *opaque = dyn_cast<OpaqueValueExpr>(E)) {
491 if (opaque->getType()->isRecordType()) {
492 assert(Adjustments.empty())(static_cast <bool> (Adjustments.empty()) ? void (0) : __assert_fail
("Adjustments.empty()", "clang/lib/CodeGen/CGExpr.cpp", 492,
__extension__ __PRETTY_FUNCTION__))
;
493 return EmitOpaqueValueLValue(opaque);
494 }
495 }
496
497 // Create and initialize the reference temporary.
498 Address Alloca = Address::invalid();
499 Address Object = createReferenceTemporary(*this, M, E, &Alloca);
500 if (auto *Var = dyn_cast<llvm::GlobalVariable>(
501 Object.getPointer()->stripPointerCasts())) {
502 Object = Address(llvm::ConstantExpr::getBitCast(
503 cast<llvm::Constant>(Object.getPointer()),
504 ConvertTypeForMem(E->getType())->getPointerTo()),
505 Object.getAlignment());
506 // If the temporary is a global and has a constant initializer or is a
507 // constant temporary that we promoted to a global, we may have already
508 // initialized it.
509 if (!Var->hasInitializer()) {
510 Var->setInitializer(CGM.EmitNullConstant(E->getType()));
511 EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
512 }
513 } else {
514 switch (M->getStorageDuration()) {
515 case SD_Automatic:
516 if (auto *Size = EmitLifetimeStart(
517 CGM.getDataLayout().getTypeAllocSize(Alloca.getElementType()),
518 Alloca.getPointer())) {
519 pushCleanupAfterFullExpr<CallLifetimeEnd>(NormalEHLifetimeMarker,
520 Alloca, Size);
521 }
522 break;
523
524 case SD_FullExpression: {
525 if (!ShouldEmitLifetimeMarkers)
526 break;
527
528 // Avoid creating a conditional cleanup just to hold an llvm.lifetime.end
529 // marker. Instead, start the lifetime of a conditional temporary earlier
530 // so that it's unconditional. Don't do this with sanitizers which need
531 // more precise lifetime marks.
532 ConditionalEvaluation *OldConditional = nullptr;
533 CGBuilderTy::InsertPoint OldIP;
534 if (isInConditionalBranch() && !E->getType().isDestructedType() &&
535 !SanOpts.has(SanitizerKind::HWAddress) &&
536 !SanOpts.has(SanitizerKind::Memory) &&
537 !CGM.getCodeGenOpts().SanitizeAddressUseAfterScope) {
538 OldConditional = OutermostConditional;
539 OutermostConditional = nullptr;
540
541 OldIP = Builder.saveIP();
542 llvm::BasicBlock *Block = OldConditional->getStartingBlock();
543 Builder.restoreIP(CGBuilderTy::InsertPoint(
544 Block, llvm::BasicBlock::iterator(Block->back())));
545 }
546
547 if (auto *Size = EmitLifetimeStart(
548 CGM.getDataLayout().getTypeAllocSize(Alloca.getElementType()),
549 Alloca.getPointer())) {
550 pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, Alloca,
551 Size);
552 }
553
554 if (OldConditional) {
555 OutermostConditional = OldConditional;
556 Builder.restoreIP(OldIP);
557 }
558 break;
559 }
560
561 default:
562 break;
563 }
564 EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
565 }
566 pushTemporaryCleanup(*this, M, E, Object);
567
568 // Perform derived-to-base casts and/or field accesses, to get from the
569 // temporary object we created (and, potentially, for which we extended
570 // the lifetime) to the subobject we're binding the reference to.
571 for (SubobjectAdjustment &Adjustment : llvm::reverse(Adjustments)) {
572 switch (Adjustment.Kind) {
573 case SubobjectAdjustment::DerivedToBaseAdjustment:
574 Object =
575 GetAddressOfBaseClass(Object, Adjustment.DerivedToBase.DerivedClass,
576 Adjustment.DerivedToBase.BasePath->path_begin(),
577 Adjustment.DerivedToBase.BasePath->path_end(),
578 /*NullCheckValue=*/ false, E->getExprLoc());
579 break;
580
581 case SubobjectAdjustment::FieldAdjustment: {
582 LValue LV = MakeAddrLValue(Object, E->getType(), AlignmentSource::Decl);
583 LV = EmitLValueForField(LV, Adjustment.Field);
584 assert(LV.isSimple() &&(static_cast <bool> (LV.isSimple() && "materialized temporary field is not a simple lvalue"
) ? void (0) : __assert_fail ("LV.isSimple() && \"materialized temporary field is not a simple lvalue\""
, "clang/lib/CodeGen/CGExpr.cpp", 585, __extension__ __PRETTY_FUNCTION__
))
585 "materialized temporary field is not a simple lvalue")(static_cast <bool> (LV.isSimple() && "materialized temporary field is not a simple lvalue"
) ? void (0) : __assert_fail ("LV.isSimple() && \"materialized temporary field is not a simple lvalue\""
, "clang/lib/CodeGen/CGExpr.cpp", 585, __extension__ __PRETTY_FUNCTION__
))
;
586 Object = LV.getAddress(*this);
587 break;
588 }
589
590 case SubobjectAdjustment::MemberPointerAdjustment: {
591 llvm::Value *Ptr = EmitScalarExpr(Adjustment.Ptr.RHS);
592 Object = EmitCXXMemberDataPointerAddress(E, Object, Ptr,
593 Adjustment.Ptr.MPT);
594 break;
595 }
596 }
597 }
598
599 return MakeAddrLValue(Object, M->getType(), AlignmentSource::Decl);
600}
601
602RValue
603CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E) {
604 // Emit the expression as an lvalue.
605 LValue LV = EmitLValue(E);
606 assert(LV.isSimple())(static_cast <bool> (LV.isSimple()) ? void (0) : __assert_fail
("LV.isSimple()", "clang/lib/CodeGen/CGExpr.cpp", 606, __extension__
__PRETTY_FUNCTION__))
;
607 llvm::Value *Value = LV.getPointer(*this);
608
609 if (sanitizePerformTypeCheck() && !E->getType()->isFunctionType()) {
610 // C++11 [dcl.ref]p5 (as amended by core issue 453):
611 // If a glvalue to which a reference is directly bound designates neither
612 // an existing object or function of an appropriate type nor a region of
613 // storage of suitable size and alignment to contain an object of the
614 // reference's type, the behavior is undefined.
615 QualType Ty = E->getType();
616 EmitTypeCheck(TCK_ReferenceBinding, E->getExprLoc(), Value, Ty);
617 }
618
619 return RValue::get(Value);
620}
621
622
623/// getAccessedFieldNo - Given an encoded value and a result number, return the
624/// input field number being accessed.
625unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
626 const llvm::Constant *Elts) {
627 return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx))
628 ->getZExtValue();
629}
630
631/// Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h.
632static llvm::Value *emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low,
633 llvm::Value *High) {
634 llvm::Value *KMul = Builder.getInt64(0x9ddfea08eb382d69ULL);
635 llvm::Value *K47 = Builder.getInt64(47);
636 llvm::Value *A0 = Builder.CreateMul(Builder.CreateXor(Low, High), KMul);
637 llvm::Value *A1 = Builder.CreateXor(Builder.CreateLShr(A0, K47), A0);
638 llvm::Value *B0 = Builder.CreateMul(Builder.CreateXor(High, A1), KMul);
639 llvm::Value *B1 = Builder.CreateXor(Builder.CreateLShr(B0, K47), B0);
640 return Builder.CreateMul(B1, KMul);
641}
642
643bool CodeGenFunction::isNullPointerAllowed(TypeCheckKind TCK) {
644 return TCK == TCK_DowncastPointer || TCK == TCK_Upcast ||
645 TCK == TCK_UpcastToVirtualBase || TCK == TCK_DynamicOperation;
646}
647
648bool CodeGenFunction::isVptrCheckRequired(TypeCheckKind TCK, QualType Ty) {
649 CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
650 return (RD && RD->hasDefinition() && RD->isDynamicClass()) &&
651 (TCK == TCK_MemberAccess || TCK == TCK_MemberCall ||
652 TCK == TCK_DowncastPointer || TCK == TCK_DowncastReference ||
653 TCK == TCK_UpcastToVirtualBase || TCK == TCK_DynamicOperation);
654}
655
656bool CodeGenFunction::sanitizePerformTypeCheck() const {
657 return SanOpts.has(SanitizerKind::Null) ||
658 SanOpts.has(SanitizerKind::Alignment) ||
659 SanOpts.has(SanitizerKind::ObjectSize) ||
660 SanOpts.has(SanitizerKind::Vptr);
661}
662
663void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
664 llvm::Value *Ptr, QualType Ty,
665 CharUnits Alignment,
666 SanitizerSet SkippedChecks,
667 llvm::Value *ArraySize) {
668 if (!sanitizePerformTypeCheck())
669 return;
670
671 // Don't check pointers outside the default address space. The null check
672 // isn't correct, the object-size check isn't supported by LLVM, and we can't
673 // communicate the addresses to the runtime handler for the vptr check.
674 if (Ptr->getType()->getPointerAddressSpace())
675 return;
676
677 // Don't check pointers to volatile data. The behavior here is implementation-
678 // defined.
679 if (Ty.isVolatileQualified())
680 return;
681
682 SanitizerScope SanScope(this);
683
684 SmallVector<std::pair<llvm::Value *, SanitizerMask>, 3> Checks;
685 llvm::BasicBlock *Done = nullptr;
686
687 // Quickly determine whether we have a pointer to an alloca. It's possible
688 // to skip null checks, and some alignment checks, for these pointers. This
689 // can reduce compile-time significantly.
690 auto PtrToAlloca = dyn_cast<llvm::AllocaInst>(Ptr->stripPointerCasts());
691
692 llvm::Value *True = llvm::ConstantInt::getTrue(getLLVMContext());
693 llvm::Value *IsNonNull = nullptr;
694 bool IsGuaranteedNonNull =
695 SkippedChecks.has(SanitizerKind::Null) || PtrToAlloca;
696 bool AllowNullPointers = isNullPointerAllowed(TCK);
697 if ((SanOpts.has(SanitizerKind::Null) || AllowNullPointers) &&
698 !IsGuaranteedNonNull) {
699 // The glvalue must not be an empty glvalue.
700 IsNonNull = Builder.CreateIsNotNull(Ptr);
701
702 // The IR builder can constant-fold the null check if the pointer points to
703 // a constant.
704 IsGuaranteedNonNull = IsNonNull == True;
705
706 // Skip the null check if the pointer is known to be non-null.
707 if (!IsGuaranteedNonNull) {
708 if (AllowNullPointers) {
709 // When performing pointer casts, it's OK if the value is null.
710 // Skip the remaining checks in that case.
711 Done = createBasicBlock("null");
712 llvm::BasicBlock *Rest = createBasicBlock("not.null");
713 Builder.CreateCondBr(IsNonNull, Rest, Done);
714 EmitBlock(Rest);
715 } else {
716 Checks.push_back(std::make_pair(IsNonNull, SanitizerKind::Null));
717 }
718 }
719 }
720
721 if (SanOpts.has(SanitizerKind::ObjectSize) &&
722 !SkippedChecks.has(SanitizerKind::ObjectSize) &&
723 !Ty->isIncompleteType()) {
724 uint64_t TySize = CGM.getMinimumObjectSize(Ty).getQuantity();
725 llvm::Value *Size = llvm::ConstantInt::get(IntPtrTy, TySize);
726 if (ArraySize)
727 Size = Builder.CreateMul(Size, ArraySize);
728
729 // Degenerate case: new X[0] does not need an objectsize check.
730 llvm::Constant *ConstantSize = dyn_cast<llvm::Constant>(Size);
731 if (!ConstantSize || !ConstantSize->isNullValue()) {
732 // The glvalue must refer to a large enough storage region.
733 // FIXME: If Address Sanitizer is enabled, insert dynamic instrumentation
734 // to check this.
735 // FIXME: Get object address space
736 llvm::Type *Tys[2] = { IntPtrTy, Int8PtrTy };
737 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, Tys);
738 llvm::Value *Min = Builder.getFalse();
739 llvm::Value *NullIsUnknown = Builder.getFalse();
740 llvm::Value *Dynamic = Builder.getFalse();
741 llvm::Value *CastAddr = Builder.CreateBitCast(Ptr, Int8PtrTy);
742 llvm::Value *LargeEnough = Builder.CreateICmpUGE(
743 Builder.CreateCall(F, {CastAddr, Min, NullIsUnknown, Dynamic}), Size);
744 Checks.push_back(std::make_pair(LargeEnough, SanitizerKind::ObjectSize));
745 }
746 }
747
748 uint64_t AlignVal = 0;
749 llvm::Value *PtrAsInt = nullptr;
750
751 if (SanOpts.has(SanitizerKind::Alignment) &&
752 !SkippedChecks.has(SanitizerKind::Alignment)) {
753 AlignVal = Alignment.getQuantity();
754 if (!Ty->isIncompleteType() && !AlignVal)
755 AlignVal = CGM.getNaturalTypeAlignment(Ty, nullptr, nullptr,
756 /*ForPointeeType=*/true)
757 .getQuantity();
758
759 // The glvalue must be suitably aligned.
760 if (AlignVal > 1 &&
761 (!PtrToAlloca || PtrToAlloca->getAlignment() < AlignVal)) {
762 PtrAsInt = Builder.CreatePtrToInt(Ptr, IntPtrTy);
763 llvm::Value *Align = Builder.CreateAnd(
764 PtrAsInt, llvm::ConstantInt::get(IntPtrTy, AlignVal - 1));
765 llvm::Value *Aligned =
766 Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
767 if (Aligned != True)
768 Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment));
769 }
770 }
771
772 if (Checks.size() > 0) {
773 // Make sure we're not losing information. Alignment needs to be a power of
774 // 2
775 assert(!AlignVal || (uint64_t)1 << llvm::Log2_64(AlignVal) == AlignVal)(static_cast <bool> (!AlignVal || (uint64_t)1 << llvm
::Log2_64(AlignVal) == AlignVal) ? void (0) : __assert_fail (
"!AlignVal || (uint64_t)1 << llvm::Log2_64(AlignVal) == AlignVal"
, "clang/lib/CodeGen/CGExpr.cpp", 775, __extension__ __PRETTY_FUNCTION__
))
;
776 llvm::Constant *StaticData[] = {
777 EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(Ty),
778 llvm::ConstantInt::get(Int8Ty, AlignVal ? llvm::Log2_64(AlignVal) : 1),
779 llvm::ConstantInt::get(Int8Ty, TCK)};
780 EmitCheck(Checks, SanitizerHandler::TypeMismatch, StaticData,
781 PtrAsInt ? PtrAsInt : Ptr);
782 }
783
784 // If possible, check that the vptr indicates that there is a subobject of
785 // type Ty at offset zero within this object.
786 //
787 // C++11 [basic.life]p5,6:
788 // [For storage which does not refer to an object within its lifetime]
789 // The program has undefined behavior if:
790 // -- the [pointer or glvalue] is used to access a non-static data member
791 // or call a non-static member function
792 if (SanOpts.has(SanitizerKind::Vptr) &&
793 !SkippedChecks.has(SanitizerKind::Vptr) && isVptrCheckRequired(TCK, Ty)) {
794 // Ensure that the pointer is non-null before loading it. If there is no
795 // compile-time guarantee, reuse the run-time null check or emit a new one.
796 if (!IsGuaranteedNonNull) {
797 if (!IsNonNull)
798 IsNonNull = Builder.CreateIsNotNull(Ptr);
799 if (!Done)
800 Done = createBasicBlock("vptr.null");
801 llvm::BasicBlock *VptrNotNull = createBasicBlock("vptr.not.null");
802 Builder.CreateCondBr(IsNonNull, VptrNotNull, Done);
803 EmitBlock(VptrNotNull);
804 }
805
806 // Compute a hash of the mangled name of the type.
807 //
808 // FIXME: This is not guaranteed to be deterministic! Move to a
809 // fingerprinting mechanism once LLVM provides one. For the time
810 // being the implementation happens to be deterministic.
811 SmallString<64> MangledName;
812 llvm::raw_svector_ostream Out(MangledName);
813 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty.getUnqualifiedType(),
814 Out);
815
816 // Contained in NoSanitizeList based on the mangled type.
817 if (!CGM.getContext().getNoSanitizeList().containsType(SanitizerKind::Vptr,
818 Out.str())) {
819 llvm::hash_code TypeHash = hash_value(Out.str());
820
821 // Load the vptr, and compute hash_16_bytes(TypeHash, vptr).
822 llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash);
823 llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0);
824 Address VPtrAddr(Builder.CreateBitCast(Ptr, VPtrTy), getPointerAlign());
825 llvm::Value *VPtrVal = Builder.CreateLoad(VPtrAddr);
826 llvm::Value *High = Builder.CreateZExt(VPtrVal, Int64Ty);
827
828 llvm::Value *Hash = emitHash16Bytes(Builder, Low, High);
829 Hash = Builder.CreateTrunc(Hash, IntPtrTy);
830
831 // Look the hash up in our cache.
832 const int CacheSize = 128;
833 llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize);
834 llvm::Value *Cache = CGM.CreateRuntimeVariable(HashTable,
835 "__ubsan_vptr_type_cache");
836 llvm::Value *Slot = Builder.CreateAnd(Hash,
837 llvm::ConstantInt::get(IntPtrTy,
838 CacheSize-1));
839 llvm::Value *Indices[] = { Builder.getInt32(0), Slot };
840 llvm::Value *CacheVal = Builder.CreateAlignedLoad(
841 IntPtrTy, Builder.CreateInBoundsGEP(HashTable, Cache, Indices),
842 getPointerAlign());
843
844 // If the hash isn't in the cache, call a runtime handler to perform the
845 // hard work of checking whether the vptr is for an object of the right
846 // type. This will either fill in the cache and return, or produce a
847 // diagnostic.
848 llvm::Value *EqualHash = Builder.CreateICmpEQ(CacheVal, Hash);
849 llvm::Constant *StaticData[] = {
850 EmitCheckSourceLocation(Loc),
851 EmitCheckTypeDescriptor(Ty),
852 CGM.GetAddrOfRTTIDescriptor(Ty.getUnqualifiedType()),
853 llvm::ConstantInt::get(Int8Ty, TCK)
854 };
855 llvm::Value *DynamicData[] = { Ptr, Hash };
856 EmitCheck(std::make_pair(EqualHash, SanitizerKind::Vptr),
857 SanitizerHandler::DynamicTypeCacheMiss, StaticData,
858 DynamicData);
859 }
860 }
861
862 if (Done) {
863 Builder.CreateBr(Done);
864 EmitBlock(Done);
865 }
866}
867
868/// Determine whether this expression refers to a flexible array member in a
869/// struct. We disable array bounds checks for such members.
870static bool isFlexibleArrayMemberExpr(const Expr *E) {
871 // For compatibility with existing code, we treat arrays of length 0 or
872 // 1 as flexible array members.
873 // FIXME: This is inconsistent with the warning code in SemaChecking. Unify
874 // the two mechanisms.
875 const ArrayType *AT = E->getType()->castAsArrayTypeUnsafe();
876 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) {
877 // FIXME: Sema doesn't treat [1] as a flexible array member if the bound
878 // was produced by macro expansion.
879 if (CAT->getSize().ugt(1))
880 return false;
881 } else if (!isa<IncompleteArrayType>(AT))
882 return false;
883
884 E = E->IgnoreParens();
885
886 // A flexible array member must be the last member in the class.
887 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
888 // FIXME: If the base type of the member expr is not FD->getParent(),
889 // this should not be treated as a flexible array member access.
890 if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
891 // FIXME: Sema doesn't treat a T[1] union member as a flexible array
892 // member, only a T[0] or T[] member gets that treatment.
893 if (FD->getParent()->isUnion())
894 return true;
895 RecordDecl::field_iterator FI(
896 DeclContext::decl_iterator(const_cast<FieldDecl *>(FD)));
897 return ++FI == FD->getParent()->field_end();
898 }
899 } else if (const auto *IRE = dyn_cast<ObjCIvarRefExpr>(E)) {
900 return IRE->getDecl()->getNextIvar() == nullptr;
901 }
902
903 return false;
904}
905
906llvm::Value *CodeGenFunction::LoadPassedObjectSize(const Expr *E,
907 QualType EltTy) {
908 ASTContext &C = getContext();
909 uint64_t EltSize = C.getTypeSizeInChars(EltTy).getQuantity();
910 if (!EltSize)
911 return nullptr;
912
913 auto *ArrayDeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts());
914 if (!ArrayDeclRef)
915 return nullptr;
916
917 auto *ParamDecl = dyn_cast<ParmVarDecl>(ArrayDeclRef->getDecl());
918 if (!ParamDecl)
919 return nullptr;
920
921 auto *POSAttr = ParamDecl->getAttr<PassObjectSizeAttr>();
922 if (!POSAttr)
923 return nullptr;
924
925 // Don't load the size if it's a lower bound.
926 int POSType = POSAttr->getType();
927 if (POSType != 0 && POSType != 1)
928 return nullptr;
929
930 // Find the implicit size parameter.
931 auto PassedSizeIt = SizeArguments.find(ParamDecl);
932 if (PassedSizeIt == SizeArguments.end())
933 return nullptr;
934
935 const ImplicitParamDecl *PassedSizeDecl = PassedSizeIt->second;
936 assert(LocalDeclMap.count(PassedSizeDecl) && "Passed size not loadable")(static_cast <bool> (LocalDeclMap.count(PassedSizeDecl)
&& "Passed size not loadable") ? void (0) : __assert_fail
("LocalDeclMap.count(PassedSizeDecl) && \"Passed size not loadable\""
, "clang/lib/CodeGen/CGExpr.cpp", 936, __extension__ __PRETTY_FUNCTION__
))
;
937 Address AddrOfSize = LocalDeclMap.find(PassedSizeDecl)->second;
938 llvm::Value *SizeInBytes = EmitLoadOfScalar(AddrOfSize, /*Volatile=*/false,
939 C.getSizeType(), E->getExprLoc());
940 llvm::Value *SizeOfElement =
941 llvm::ConstantInt::get(SizeInBytes->getType(), EltSize);
942 return Builder.CreateUDiv(SizeInBytes, SizeOfElement);
943}
944
945/// If Base is known to point to the start of an array, return the length of
946/// that array. Return 0 if the length cannot be determined.
947static llvm::Value *getArrayIndexingBound(
948 CodeGenFunction &CGF, const Expr *Base, QualType &IndexedType) {
949 // For the vector indexing extension, the bound is the number of elements.
950 if (const VectorType *VT = Base->getType()->getAs<VectorType>()) {
951 IndexedType = Base->getType();
952 return CGF.Builder.getInt32(VT->getNumElements());
953 }
954
955 Base = Base->IgnoreParens();
956
957 if (const auto *CE = dyn_cast<CastExpr>(Base)) {
958 if (CE->getCastKind() == CK_ArrayToPointerDecay &&
959 !isFlexibleArrayMemberExpr(CE->getSubExpr())) {
960 IndexedType = CE->getSubExpr()->getType();
961 const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe();
962 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
963 return CGF.Builder.getInt(CAT->getSize());
964 else if (const auto *VAT = dyn_cast<VariableArrayType>(AT))
965 return CGF.getVLASize(VAT).NumElts;
966 // Ignore pass_object_size here. It's not applicable on decayed pointers.
967 }
968 }
969
970 QualType EltTy{Base->getType()->getPointeeOrArrayElementType(), 0};
971 if (llvm::Value *POS = CGF.LoadPassedObjectSize(Base, EltTy)) {
972 IndexedType = Base->getType();
973 return POS;
974 }
975
976 return nullptr;
977}
978
979void CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base,
980 llvm::Value *Index, QualType IndexType,
981 bool Accessed) {
982 assert(SanOpts.has(SanitizerKind::ArrayBounds) &&(static_cast <bool> (SanOpts.has(SanitizerKind::ArrayBounds
) && "should not be called unless adding bounds checks"
) ? void (0) : __assert_fail ("SanOpts.has(SanitizerKind::ArrayBounds) && \"should not be called unless adding bounds checks\""
, "clang/lib/CodeGen/CGExpr.cpp", 983, __extension__ __PRETTY_FUNCTION__
))
983 "should not be called unless adding bounds checks")(static_cast <bool> (SanOpts.has(SanitizerKind::ArrayBounds
) && "should not be called unless adding bounds checks"
) ? void (0) : __assert_fail ("SanOpts.has(SanitizerKind::ArrayBounds) && \"should not be called unless adding bounds checks\""
, "clang/lib/CodeGen/CGExpr.cpp", 983, __extension__ __PRETTY_FUNCTION__
))
;
984 SanitizerScope SanScope(this);
985
986 QualType IndexedType;
987 llvm::Value *Bound = getArrayIndexingBound(*this, Base, IndexedType);
988 if (!Bound)
989 return;
990
991 bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType();
992 llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned);
993 llvm::Value *BoundVal = Builder.CreateIntCast(Bound, SizeTy, false);
994
995 llvm::Constant *StaticData[] = {
996 EmitCheckSourceLocation(E->getExprLoc()),
997 EmitCheckTypeDescriptor(IndexedType),
998 EmitCheckTypeDescriptor(IndexType)
999 };
1000 llvm::Value *Check = Accessed ? Builder.CreateICmpULT(IndexVal, BoundVal)
1001 : Builder.CreateICmpULE(IndexVal, BoundVal);
1002 EmitCheck(std::make_pair(Check, SanitizerKind::ArrayBounds),
1003 SanitizerHandler::OutOfBounds, StaticData, Index);
1004}
1005
1006
1007CodeGenFunction::ComplexPairTy CodeGenFunction::
1008EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
1009 bool isInc, bool isPre) {
1010 ComplexPairTy InVal = EmitLoadOfComplex(LV, E->getExprLoc());
1011
1012 llvm::Value *NextVal;
1013 if (isa<llvm::IntegerType>(InVal.first->getType())) {
1014 uint64_t AmountVal = isInc ? 1 : -1;
1015 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
1016
1017 // Add the inc/dec to the real part.
1018 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
1019 } else {
1020 QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType();
1021 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
1022 if (!isInc)
1023 FVal.changeSign();
1024 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
1025
1026 // Add the inc/dec to the real part.
1027 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
1028 }
1029
1030 ComplexPairTy IncVal(NextVal, InVal.second);
1031
1032 // Store the updated result through the lvalue.
1033 EmitStoreOfComplex(IncVal, LV, /*init*/ false);
1034 if (getLangOpts().OpenMP)
1035 CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(*this,
1036 E->getSubExpr());
1037
1038 // If this is a postinc, return the value read from memory, otherwise use the
1039 // updated value.
1040 return isPre ? IncVal : InVal;
1041}
1042
1043void CodeGenModule::EmitExplicitCastExprType(const ExplicitCastExpr *E,
1044 CodeGenFunction *CGF) {
1045 // Bind VLAs in the cast type.
1046 if (CGF && E->getType()->isVariablyModifiedType())
1047 CGF->EmitVariablyModifiedType(E->getType());
1048
1049 if (CGDebugInfo *DI = getModuleDebugInfo())
1050 DI->EmitExplicitCastType(E->getType());
1051}
1052
1053//===----------------------------------------------------------------------===//
1054// LValue Expression Emission
1055//===----------------------------------------------------------------------===//
1056
1057/// EmitPointerWithAlignment - Given an expression of pointer type, try to
1058/// derive a more accurate bound on the alignment of the pointer.
1059Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E,
1060 LValueBaseInfo *BaseInfo,
1061 TBAAAccessInfo *TBAAInfo) {
1062 // We allow this with ObjC object pointers because of fragile ABIs.
1063 assert(E->getType()->isPointerType() ||(static_cast <bool> (E->getType()->isPointerType(
) || E->getType()->isObjCObjectPointerType()) ? void (0
) : __assert_fail ("E->getType()->isPointerType() || E->getType()->isObjCObjectPointerType()"
, "clang/lib/CodeGen/CGExpr.cpp", 1064, __extension__ __PRETTY_FUNCTION__
))
1064 E->getType()->isObjCObjectPointerType())(static_cast <bool> (E->getType()->isPointerType(
) || E->getType()->isObjCObjectPointerType()) ? void (0
) : __assert_fail ("E->getType()->isPointerType() || E->getType()->isObjCObjectPointerType()"
, "clang/lib/CodeGen/CGExpr.cpp", 1064, __extension__ __PRETTY_FUNCTION__
))
;
1065 E = E->IgnoreParens();
1066
1067 // Casts:
1068 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
1069 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(CE))
1070 CGM.EmitExplicitCastExprType(ECE, this);
1071
1072 switch (CE->getCastKind()) {
1073 // Non-converting casts (but not C's implicit conversion from void*).
1074 case CK_BitCast:
1075 case CK_NoOp:
1076 case CK_AddressSpaceConversion:
1077 if (auto PtrTy = CE->getSubExpr()->getType()->getAs<PointerType>()) {
1078 if (PtrTy->getPointeeType()->isVoidType())
1079 break;
1080
1081 LValueBaseInfo InnerBaseInfo;
1082 TBAAAccessInfo InnerTBAAInfo;
1083 Address Addr = EmitPointerWithAlignment(CE->getSubExpr(),
1084 &InnerBaseInfo,
1085 &InnerTBAAInfo);
1086 if (BaseInfo) *BaseInfo = InnerBaseInfo;
1087 if (TBAAInfo) *TBAAInfo = InnerTBAAInfo;
1088
1089 if (isa<ExplicitCastExpr>(CE)) {
1090 LValueBaseInfo TargetTypeBaseInfo;
1091 TBAAAccessInfo TargetTypeTBAAInfo;
1092 CharUnits Align = CGM.getNaturalPointeeTypeAlignment(
1093 E->getType(), &TargetTypeBaseInfo, &TargetTypeTBAAInfo);
1094 if (TBAAInfo)
1095 *TBAAInfo = CGM.mergeTBAAInfoForCast(*TBAAInfo,
1096 TargetTypeTBAAInfo);
1097 // If the source l-value is opaque, honor the alignment of the
1098 // casted-to type.
1099 if (InnerBaseInfo.getAlignmentSource() != AlignmentSource::Decl) {
1100 if (BaseInfo)
1101 BaseInfo->mergeForCast(TargetTypeBaseInfo);
1102 Addr = Address(Addr.getPointer(), Addr.getElementType(), Align);
1103 }
1104 }
1105
1106 if (SanOpts.has(SanitizerKind::CFIUnrelatedCast) &&
1107 CE->getCastKind() == CK_BitCast) {
1108 if (auto PT = E->getType()->getAs<PointerType>())
1109 EmitVTablePtrCheckForCast(PT->getPointeeType(), Addr.getPointer(),
1110 /*MayBeNull=*/true,
1111 CodeGenFunction::CFITCK_UnrelatedCast,
1112 CE->getBeginLoc());
1113 }
1114
1115 if (CE->getCastKind() == CK_AddressSpaceConversion)
1116 return Builder.CreateAddrSpaceCast(Addr, ConvertType(E->getType()));
1117
1118 llvm::Type *ElemTy = ConvertTypeForMem(E->getType()->getPointeeType());
1119 return Builder.CreateElementBitCast(Addr, ElemTy);
1120 }
1121 break;
1122
1123 // Array-to-pointer decay.
1124 case CK_ArrayToPointerDecay:
1125 return EmitArrayToPointerDecay(CE->getSubExpr(), BaseInfo, TBAAInfo);
1126
1127 // Derived-to-base conversions.
1128 case CK_UncheckedDerivedToBase:
1129 case CK_DerivedToBase: {
1130 // TODO: Support accesses to members of base classes in TBAA. For now, we
1131 // conservatively pretend that the complete object is of the base class
1132 // type.
1133 if (TBAAInfo)
1134 *TBAAInfo = CGM.getTBAAAccessInfo(E->getType());
1135 Address Addr = EmitPointerWithAlignment(CE->getSubExpr(), BaseInfo);
1136 auto Derived = CE->getSubExpr()->getType()->getPointeeCXXRecordDecl();
1137 return GetAddressOfBaseClass(Addr, Derived,
1138 CE->path_begin(), CE->path_end(),
1139 ShouldNullCheckClassCastValue(CE),
1140 CE->getExprLoc());
1141 }
1142
1143 // TODO: Is there any reason to treat base-to-derived conversions
1144 // specially?
1145 default:
1146 break;
1147 }
1148 }
1149
1150 // Unary &.
1151 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1152 if (UO->getOpcode() == UO_AddrOf) {
1153 LValue LV = EmitLValue(UO->getSubExpr());
1154 if (BaseInfo) *BaseInfo = LV.getBaseInfo();
1155 if (TBAAInfo) *TBAAInfo = LV.getTBAAInfo();
1156 return LV.getAddress(*this);
1157 }
1158 }
1159
1160 // TODO: conditional operators, comma.
1161
1162 // Otherwise, use the alignment of the type.
1163 CharUnits Align =
1164 CGM.getNaturalPointeeTypeAlignment(E->getType(), BaseInfo, TBAAInfo);
1165 llvm::Type *ElemTy = ConvertTypeForMem(E->getType()->getPointeeType());
1166 return Address(EmitScalarExpr(E), ElemTy, Align);
1167}
1168
1169llvm::Value *CodeGenFunction::EmitNonNullRValueCheck(RValue RV, QualType T) {
1170 llvm::Value *V = RV.getScalarVal();
1171 if (auto MPT = T->getAs<MemberPointerType>())
1172 return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, V, MPT);
1173 return Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
1174}
1175
1176RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
1177 if (Ty->isVoidType())
1178 return RValue::get(nullptr);
1179
1180 switch (getEvaluationKind(Ty)) {
1181 case TEK_Complex: {
1182 llvm::Type *EltTy =
1183 ConvertType(Ty->castAs<ComplexType>()->getElementType());
1184 llvm::Value *U = llvm::UndefValue::get(EltTy);
1185 return RValue::getComplex(std::make_pair(U, U));
1186 }
1187
1188 // If this is a use of an undefined aggregate type, the aggregate must have an
1189 // identifiable address. Just because the contents of the value are undefined
1190 // doesn't mean that the address can't be taken and compared.
1191 case TEK_Aggregate: {
1192 Address DestPtr = CreateMemTemp(Ty, "undef.agg.tmp");
1193 return RValue::getAggregate(DestPtr);
1194 }
1195
1196 case TEK_Scalar:
1197 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
1198 }
1199 llvm_unreachable("bad evaluation kind")::llvm::llvm_unreachable_internal("bad evaluation kind", "clang/lib/CodeGen/CGExpr.cpp"
, 1199)
;
1200}
1201
1202RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
1203 const char *Name) {
1204 ErrorUnsupported(E, Name);
1205 return GetUndefRValue(E->getType());
1206}
1207
1208LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
1209 const char *Name) {
1210 ErrorUnsupported(E, Name);
1211 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
1212 return MakeAddrLValue(Address(llvm::UndefValue::get(Ty), CharUnits::One()),
1213 E->getType());
1214}
1215
1216bool CodeGenFunction::IsWrappedCXXThis(const Expr *Obj) {
1217 const Expr *Base = Obj;
1218 while (!isa<CXXThisExpr>(Base)) {
1219 // The result of a dynamic_cast can be null.
1220 if (isa<CXXDynamicCastExpr>(Base))
1221 return false;
1222
1223 if (const auto *CE = dyn_cast<CastExpr>(Base)) {
1224 Base = CE->getSubExpr();
1225 } else if (const auto *PE = dyn_cast<ParenExpr>(Base)) {
1226 Base = PE->getSubExpr();
1227 } else if (const auto *UO = dyn_cast<UnaryOperator>(Base)) {
1228 if (UO->getOpcode() == UO_Extension)
1229 Base = UO->getSubExpr();
1230 else
1231 return false;
1232 } else {
1233 return false;
1234 }
1235 }
1236 return true;
1237}
1238
1239LValue CodeGenFunction::EmitCheckedLValue(const Expr *E, TypeCheckKind TCK) {
1240 LValue LV;
1241 if (SanOpts.has(SanitizerKind::ArrayBounds) && isa<ArraySubscriptExpr>(E))
1242 LV = EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E), /*Accessed*/true);
1243 else
1244 LV = EmitLValue(E);
1245 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple()) {
1246 SanitizerSet SkippedChecks;
1247 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
1248 bool IsBaseCXXThis = IsWrappedCXXThis(ME->getBase());
1249 if (IsBaseCXXThis)
1250 SkippedChecks.set(SanitizerKind::Alignment, true);
1251 if (IsBaseCXXThis || isa<DeclRefExpr>(ME->getBase()))
1252 SkippedChecks.set(SanitizerKind::Null, true);
1253 }
1254 EmitTypeCheck(TCK, E->getExprLoc(), LV.getPointer(*this), E->getType(),
1255 LV.getAlignment(), SkippedChecks);
1256 }
1257 return LV;
1258}
1259
1260/// EmitLValue - Emit code to compute a designator that specifies the location
1261/// of the expression.
1262///
1263/// This can return one of two things: a simple address or a bitfield reference.
1264/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
1265/// an LLVM pointer type.
1266///
1267/// If this returns a bitfield reference, nothing about the pointee type of the
1268/// LLVM value is known: For example, it may not be a pointer to an integer.
1269///
1270/// If this returns a normal address, and if the lvalue's C type is fixed size,
1271/// this method guarantees that the returned pointer type will point to an LLVM
1272/// type of the same size of the lvalue's type. If the lvalue has a variable
1273/// length type, this is not possible.
1274///
1275LValue CodeGenFunction::EmitLValue(const Expr *E) {
1276 ApplyDebugLocation DL(*this, E);
1277 switch (E->getStmtClass()) {
1278 default: return EmitUnsupportedLValue(E, "l-value expression");
1279
1280 case Expr::ObjCPropertyRefExprClass:
1281 llvm_unreachable("cannot emit a property reference directly")::llvm::llvm_unreachable_internal("cannot emit a property reference directly"
, "clang/lib/CodeGen/CGExpr.cpp", 1281)
;
1282
1283 case Expr::ObjCSelectorExprClass:
1284 return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
1285 case Expr::ObjCIsaExprClass:
1286 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
1287 case Expr::BinaryOperatorClass:
1288 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
1289 case Expr::CompoundAssignOperatorClass: {
1290 QualType Ty = E->getType();
1291 if (const AtomicType *AT = Ty->getAs<AtomicType>())
1292 Ty = AT->getValueType();
1293 if (!Ty->isAnyComplexType())
1294 return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
1295 return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
1296 }
1297 case Expr::CallExprClass:
1298 case Expr::CXXMemberCallExprClass:
1299 case Expr::CXXOperatorCallExprClass:
1300 case Expr::UserDefinedLiteralClass:
1301 return EmitCallExprLValue(cast<CallExpr>(E));
1302 case Expr::CXXRewrittenBinaryOperatorClass:
1303 return EmitLValue(cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm());
1304 case Expr::VAArgExprClass:
1305 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
1306 case Expr::DeclRefExprClass:
1307 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
1308 case Expr::ConstantExprClass: {
1309 const ConstantExpr *CE = cast<ConstantExpr>(E);
1310 if (llvm::Value *Result = ConstantEmitter(*this).tryEmitConstantExpr(CE)) {
1311 QualType RetType = cast<CallExpr>(CE->getSubExpr()->IgnoreImplicit())
1312 ->getCallReturnType(getContext())
1313 ->getPointeeType();
1314 return MakeNaturalAlignAddrLValue(Result, RetType);
1315 }
1316 return EmitLValue(cast<ConstantExpr>(E)->getSubExpr());
1317 }
1318 case Expr::ParenExprClass:
1319 return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
1320 case Expr::GenericSelectionExprClass:
1321 return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr());
1322 case Expr::PredefinedExprClass:
1323 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
1324 case Expr::StringLiteralClass:
1325 return EmitStringLiteralLValue(cast<StringLiteral>(E));
1326 case Expr::ObjCEncodeExprClass:
1327 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
1328 case Expr::PseudoObjectExprClass:
1329 return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E));
1330 case Expr::InitListExprClass:
1331 return EmitInitListLValue(cast<InitListExpr>(E));
1332 case Expr::CXXTemporaryObjectExprClass:
1333 case Expr::CXXConstructExprClass:
1334 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
1335 case Expr::CXXBindTemporaryExprClass:
1336 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
1337 case Expr::CXXUuidofExprClass:
1338 return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E));
1339 case Expr::LambdaExprClass:
1340 return EmitAggExprToLValue(E);
1341
1342 case Expr::ExprWithCleanupsClass: {
1343 const auto *cleanups = cast<ExprWithCleanups>(E);
1344 RunCleanupsScope Scope(*this);
1345 LValue LV = EmitLValue(cleanups->getSubExpr());
1346 if (LV.isSimple()) {
1347 // Defend against branches out of gnu statement expressions surrounded by
1348 // cleanups.
1349 Address Addr = LV.getAddress(*this);
1350 llvm::Value *V = Addr.getPointer();
1351 Scope.ForceCleanup({&V});
1352 return LValue::MakeAddr(Addr.withPointer(V), LV.getType(), getContext(),
1353 LV.getBaseInfo(), LV.getTBAAInfo());
1354 }
1355 // FIXME: Is it possible to create an ExprWithCleanups that produces a
1356 // bitfield lvalue or some other non-simple lvalue?
1357 return LV;
1358 }
1359
1360 case Expr::CXXDefaultArgExprClass: {
1361 auto *DAE = cast<CXXDefaultArgExpr>(E);
1362 CXXDefaultArgExprScope Scope(*this, DAE);
1363 return EmitLValue(DAE->getExpr());
1364 }
1365 case Expr::CXXDefaultInitExprClass: {
1366 auto *DIE = cast<CXXDefaultInitExpr>(E);
1367 CXXDefaultInitExprScope Scope(*this, DIE);
1368 return EmitLValue(DIE->getExpr());
1369 }
1370 case Expr::CXXTypeidExprClass:
1371 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
1372
1373 case Expr::ObjCMessageExprClass:
1374 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
1375 case Expr::ObjCIvarRefExprClass:
1376 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
1377 case Expr::StmtExprClass:
1378 return EmitStmtExprLValue(cast<StmtExpr>(E));
1379 case Expr::UnaryOperatorClass:
1380 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
1381 case Expr::ArraySubscriptExprClass:
1382 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
1383 case Expr::MatrixSubscriptExprClass:
1384 return EmitMatrixSubscriptExpr(cast<MatrixSubscriptExpr>(E));
1385 case Expr::OMPArraySectionExprClass:
1386 return EmitOMPArraySectionExpr(cast<OMPArraySectionExpr>(E));
1387 case Expr::ExtVectorElementExprClass:
1388 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
1389 case Expr::MemberExprClass:
1390 return EmitMemberExpr(cast<MemberExpr>(E));
1391 case Expr::CompoundLiteralExprClass:
1392 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
1393 case Expr::ConditionalOperatorClass:
1394 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
1395 case Expr::BinaryConditionalOperatorClass:
1396 return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E));
1397 case Expr::ChooseExprClass:
1398 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr());
1399 case Expr::OpaqueValueExprClass:
1400 return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E));
1401 case Expr::SubstNonTypeTemplateParmExprClass:
1402 return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
1403 case Expr::ImplicitCastExprClass:
1404 case Expr::CStyleCastExprClass:
1405 case Expr::CXXFunctionalCastExprClass:
1406 case Expr::CXXStaticCastExprClass:
1407 case Expr::CXXDynamicCastExprClass:
1408 case Expr::CXXReinterpretCastExprClass:
1409 case Expr::CXXConstCastExprClass:
1410 case Expr::CXXAddrspaceCastExprClass:
1411 case Expr::ObjCBridgedCastExprClass:
1412 return EmitCastLValue(cast<CastExpr>(E));
1413
1414 case Expr::MaterializeTemporaryExprClass:
1415 return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E));
1416
1417 case Expr::CoawaitExprClass:
1418 return EmitCoawaitLValue(cast<CoawaitExpr>(E));
1419 case Expr::CoyieldExprClass:
1420 return EmitCoyieldLValue(cast<CoyieldExpr>(E));
1421 }
1422}
1423
1424/// Given an object of the given canonical type, can we safely copy a
1425/// value out of it based on its initializer?
1426static bool isConstantEmittableObjectType(QualType type) {
1427 assert(type.isCanonical())(static_cast <bool> (type.isCanonical()) ? void (0) : __assert_fail
("type.isCanonical()", "clang/lib/CodeGen/CGExpr.cpp", 1427,
__extension__ __PRETTY_FUNCTION__))
;
1428 assert(!type->isReferenceType())(static_cast <bool> (!type->isReferenceType()) ? void
(0) : __assert_fail ("!type->isReferenceType()", "clang/lib/CodeGen/CGExpr.cpp"
, 1428, __extension__ __PRETTY_FUNCTION__))
;
1429
1430 // Must be const-qualified but non-volatile.
1431 Qualifiers qs = type.getLocalQualifiers();
1432 if (!qs.hasConst() || qs.hasVolatile()) return false;
1433
1434 // Otherwise, all object types satisfy this except C++ classes with
1435 // mutable subobjects or non-trivial copy/destroy behavior.
1436 if (const auto *RT = dyn_cast<RecordType>(type))
1437 if (const auto *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
1438 if (RD->hasMutableFields() || !RD->isTrivial())
1439 return false;
1440
1441 return true;
1442}
1443
1444/// Can we constant-emit a load of a reference to a variable of the
1445/// given type? This is different from predicates like
1446/// Decl::mightBeUsableInConstantExpressions because we do want it to apply
1447/// in situations that don't necessarily satisfy the language's rules
1448/// for this (e.g. C++'s ODR-use rules). For example, we want to able
1449/// to do this with const float variables even if those variables
1450/// aren't marked 'constexpr'.
1451enum ConstantEmissionKind {
1452 CEK_None,
1453 CEK_AsReferenceOnly,
1454 CEK_AsValueOrReference,
1455 CEK_AsValueOnly
1456};
1457static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type) {
1458 type = type.getCanonicalType();
1459 if (const auto *ref = dyn_cast<ReferenceType>(type)) {
1460 if (isConstantEmittableObjectType(ref->getPointeeType()))
1461 return CEK_AsValueOrReference;
1462 return CEK_AsReferenceOnly;
1463 }
1464 if (isConstantEmittableObjectType(type))
1465 return CEK_AsValueOnly;
1466 return CEK_None;
1467}
1468
1469/// Try to emit a reference to the given value without producing it as
1470/// an l-value. This is just an optimization, but it avoids us needing
1471/// to emit global copies of variables if they're named without triggering
1472/// a formal use in a context where we can't emit a direct reference to them,
1473/// for instance if a block or lambda or a member of a local class uses a
1474/// const int variable or constexpr variable from an enclosing function.
1475CodeGenFunction::ConstantEmission
1476CodeGenFunction::tryEmitAsConstant(DeclRefExpr *refExpr) {
1477 ValueDecl *value = refExpr->getDecl();
1478
1479 // The value needs to be an enum constant or a constant variable.
1480 ConstantEmissionKind CEK;
1481 if (isa<ParmVarDecl>(value)) {
1482 CEK = CEK_None;
1483 } else if (auto *var = dyn_cast<VarDecl>(value)) {
1484 CEK = checkVarTypeForConstantEmission(var->getType());
1485 } else if (isa<EnumConstantDecl>(value)) {
1486 CEK = CEK_AsValueOnly;
1487 } else {
1488 CEK = CEK_None;
1489 }
1490 if (CEK == CEK_None) return ConstantEmission();
1491
1492 Expr::EvalResult result;
1493 bool resultIsReference;
1494 QualType resultType;
1495
1496 // It's best to evaluate all the way as an r-value if that's permitted.
1497 if (CEK != CEK_AsReferenceOnly &&
1498 refExpr->EvaluateAsRValue(result, getContext())) {
1499 resultIsReference = false;
1500 resultType = refExpr->getType();
1501
1502 // Otherwise, try to evaluate as an l-value.
1503 } else if (CEK != CEK_AsValueOnly &&
1504 refExpr->EvaluateAsLValue(result, getContext())) {
1505 resultIsReference = true;
1506 resultType = value->getType();
1507
1508 // Failure.
1509 } else {
1510 return ConstantEmission();
1511 }
1512
1513 // In any case, if the initializer has side-effects, abandon ship.
1514 if (result.HasSideEffects)
1515 return ConstantEmission();
1516
1517 // In CUDA/HIP device compilation, a lambda may capture a reference variable
1518 // referencing a global host variable by copy. In this case the lambda should
1519 // make a copy of the value of the global host variable. The DRE of the
1520 // captured reference variable cannot be emitted as load from the host
1521 // global variable as compile time constant, since the host variable is not
1522 // accessible on device. The DRE of the captured reference variable has to be
1523 // loaded from captures.
1524 if (CGM.getLangOpts().CUDAIsDevice && result.Val.isLValue() &&
1525 refExpr->refersToEnclosingVariableOrCapture()) {
1526 auto *MD = dyn_cast_or_null<CXXMethodDecl>(CurCodeDecl);
1527 if (MD && MD->getParent()->isLambda() &&
1528 MD->getOverloadedOperator() == OO_Call) {
1529 const APValue::LValueBase &base = result.Val.getLValueBase();
1530 if (const ValueDecl *D = base.dyn_cast<const ValueDecl *>()) {
1531 if (const VarDecl *VD = dyn_cast<const VarDecl>(D)) {
1532 if (!VD->hasAttr<CUDADeviceAttr>()) {
1533 return ConstantEmission();
1534 }
1535 }
1536 }
1537 }
1538 }
1539
1540 // Emit as a constant.
1541 auto C = ConstantEmitter(*this).emitAbstract(refExpr->getLocation(),
1542 result.Val, resultType);
1543
1544 // Make sure we emit a debug reference to the global variable.
1545 // This should probably fire even for
1546 if (isa<VarDecl>(value)) {
1547 if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value)))
1548 EmitDeclRefExprDbgValue(refExpr, result.Val);
1549 } else {
1550 assert(isa<EnumConstantDecl>(value))(static_cast <bool> (isa<EnumConstantDecl>(value)
) ? void (0) : __assert_fail ("isa<EnumConstantDecl>(value)"
, "clang/lib/CodeGen/CGExpr.cpp", 1550, __extension__ __PRETTY_FUNCTION__
))
;
1551 EmitDeclRefExprDbgValue(refExpr, result.Val);
1552 }
1553
1554 // If we emitted a reference constant, we need to dereference that.
1555 if (resultIsReference)
1556 return ConstantEmission::forReference(C);
1557
1558 return ConstantEmission::forValue(C);
1559}
1560
1561static DeclRefExpr *tryToConvertMemberExprToDeclRefExpr(CodeGenFunction &CGF,
1562 const MemberExpr *ME) {
1563 if (auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) {
1564 // Try to emit static variable member expressions as DREs.
1565 return DeclRefExpr::Create(
1566 CGF.getContext(), NestedNameSpecifierLoc(), SourceLocation(), VD,
1567 /*RefersToEnclosingVariableOrCapture=*/false, ME->getExprLoc(),
1568 ME->getType(), ME->getValueKind(), nullptr, nullptr, ME->isNonOdrUse());
1569 }
1570 return nullptr;
1571}
1572
1573CodeGenFunction::ConstantEmission
1574CodeGenFunction::tryEmitAsConstant(const MemberExpr *ME) {
1575 if (DeclRefExpr *DRE = tryToConvertMemberExprToDeclRefExpr(*this, ME))
1576 return tryEmitAsConstant(DRE);
1577 return ConstantEmission();
1578}
1579
1580llvm::Value *CodeGenFunction::emitScalarConstant(
1581 const CodeGenFunction::ConstantEmission &Constant, Expr *E) {
1582 assert(Constant && "not a constant")(static_cast <bool> (Constant && "not a constant"
) ? void (0) : __assert_fail ("Constant && \"not a constant\""
, "clang/lib/CodeGen/CGExpr.cpp", 1582, __extension__ __PRETTY_FUNCTION__
))
;
1
Assuming the condition is true
2
'?' condition is true
1583 if (Constant.isReference())
3
Assuming the condition is true
4
Taking true branch
1584 return EmitLoadOfLValue(Constant.getReferenceLValue(*this, E),
5
Calling 'CodeGenFunction::EmitLoadOfLValue'
1585 E->getExprLoc())
1586 .getScalarVal();
1587 return Constant.getValue();
1588}
1589
1590llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue,
1591 SourceLocation Loc) {
1592 return EmitLoadOfScalar(lvalue.getAddress(*this), lvalue.isVolatile(),
1593 lvalue.getType(), Loc, lvalue.getBaseInfo(),
1594 lvalue.getTBAAInfo(), lvalue.isNontemporal());
1595}
1596
1597static bool hasBooleanRepresentation(QualType Ty) {
1598 if (Ty->isBooleanType())
1599 return true;
1600
1601 if (const EnumType *ET = Ty->getAs<EnumType>())
1602 return ET->getDecl()->getIntegerType()->isBooleanType();
1603
1604 if (const AtomicType *AT = Ty->getAs<AtomicType>())
1605 return hasBooleanRepresentation(AT->getValueType());
1606
1607 return false;
1608}
1609
1610static bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
1611 llvm::APInt &Min, llvm::APInt &End,
1612 bool StrictEnums, bool IsBool) {
1613 const EnumType *ET = Ty->getAs<EnumType>();
1614 bool IsRegularCPlusPlusEnum = CGF.getLangOpts().CPlusPlus && StrictEnums &&
1615 ET && !ET->getDecl()->isFixed();
1616 if (!IsBool && !IsRegularCPlusPlusEnum)
1617 return false;
1618
1619 if (IsBool) {
1620 Min = llvm::APInt(CGF.getContext().getTypeSize(Ty), 0);
1621 End = llvm::APInt(CGF.getContext().getTypeSize(Ty), 2);
1622 } else {
1623 const EnumDecl *ED = ET->getDecl();
1624 llvm::Type *LTy = CGF.ConvertTypeForMem(ED->getIntegerType());
1625 unsigned Bitwidth = LTy->getScalarSizeInBits();
1626 unsigned NumNegativeBits = ED->getNumNegativeBits();
1627 unsigned NumPositiveBits = ED->getNumPositiveBits();
1628
1629 if (NumNegativeBits) {
1630 unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1);
1631 assert(NumBits <= Bitwidth)(static_cast <bool> (NumBits <= Bitwidth) ? void (0)
: __assert_fail ("NumBits <= Bitwidth", "clang/lib/CodeGen/CGExpr.cpp"
, 1631, __extension__ __PRETTY_FUNCTION__))
;
1632 End = llvm::APInt(Bitwidth, 1) << (NumBits - 1);
1633 Min = -End;
1634 } else {
1635 assert(NumPositiveBits <= Bitwidth)(static_cast <bool> (NumPositiveBits <= Bitwidth) ? void
(0) : __assert_fail ("NumPositiveBits <= Bitwidth", "clang/lib/CodeGen/CGExpr.cpp"
, 1635, __extension__ __PRETTY_FUNCTION__))
;
1636 End = llvm::APInt(Bitwidth, 1) << NumPositiveBits;
1637 Min = llvm::APInt::getZero(Bitwidth);
1638 }
1639 }
1640 return true;
1641}
1642
1643llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) {
1644 llvm::APInt Min, End;
1645 if (!getRangeForType(*this, Ty, Min, End, CGM.getCodeGenOpts().StrictEnums,
1646 hasBooleanRepresentation(Ty)))
1647 return nullptr;
1648
1649 llvm::MDBuilder MDHelper(getLLVMContext());
1650 return MDHelper.createRange(Min, End);
1651}
1652
1653bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
1654 SourceLocation Loc) {
1655 bool HasBoolCheck = SanOpts.has(SanitizerKind::Bool);
1656 bool HasEnumCheck = SanOpts.has(SanitizerKind::Enum);
1657 if (!HasBoolCheck && !HasEnumCheck)
1658 return false;
1659
1660 bool IsBool = hasBooleanRepresentation(Ty) ||
1661 NSAPI(CGM.getContext()).isObjCBOOLType(Ty);
1662 bool NeedsBoolCheck = HasBoolCheck && IsBool;
1663 bool NeedsEnumCheck = HasEnumCheck && Ty->getAs<EnumType>();
1664 if (!NeedsBoolCheck && !NeedsEnumCheck)
1665 return false;
1666
1667 // Single-bit booleans don't need to be checked. Special-case this to avoid
1668 // a bit width mismatch when handling bitfield values. This is handled by
1669 // EmitFromMemory for the non-bitfield case.
1670 if (IsBool &&
1671 cast<llvm::IntegerType>(Value->getType())->getBitWidth() == 1)
1672 return false;
1673
1674 llvm::APInt Min, End;
1675 if (!getRangeForType(*this, Ty, Min, End, /*StrictEnums=*/true, IsBool))
1676 return true;
1677
1678 auto &Ctx = getLLVMContext();
1679 SanitizerScope SanScope(this);
1680 llvm::Value *Check;
1681 --End;
1682 if (!Min) {
1683 Check = Builder.CreateICmpULE(Value, llvm::ConstantInt::get(Ctx, End));
1684 } else {
1685 llvm::Value *Upper =
1686 Builder.CreateICmpSLE(Value, llvm::ConstantInt::get(Ctx, End));
1687 llvm::Value *Lower =
1688 Builder.CreateICmpSGE(Value, llvm::ConstantInt::get(Ctx, Min));
1689 Check = Builder.CreateAnd(Upper, Lower);
1690 }
1691 llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation(Loc),
1692 EmitCheckTypeDescriptor(Ty)};
1693 SanitizerMask Kind =
1694 NeedsEnumCheck ? SanitizerKind::Enum : SanitizerKind::Bool;
1695 EmitCheck(std::make_pair(Check, Kind), SanitizerHandler::LoadInvalidValue,
1696 StaticArgs, EmitCheckValue(Value));
1697 return true;
1698}
1699
1700llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile,
1701 QualType Ty,
1702 SourceLocation Loc,
1703 LValueBaseInfo BaseInfo,
1704 TBAAAccessInfo TBAAInfo,
1705 bool isNontemporal) {
1706 if (!CGM.getCodeGenOpts().PreserveVec3Type) {
1707 // For better performance, handle vector loads differently.
1708 if (Ty->isVectorType()) {
1709 const llvm::Type *EltTy = Addr.getElementType();
1710
1711 const auto *VTy = cast<llvm::FixedVectorType>(EltTy);
1712
1713 // Handle vectors of size 3 like size 4 for better performance.
1714 if (VTy->getNumElements() == 3) {
1715
1716 // Bitcast to vec4 type.
1717 auto *vec4Ty = llvm::FixedVectorType::get(VTy->getElementType(), 4);
1718 Address Cast = Builder.CreateElementBitCast(Addr, vec4Ty, "castToVec4");
1719 // Now load value.
1720 llvm::Value *V = Builder.CreateLoad(Cast, Volatile, "loadVec4");
1721
1722 // Shuffle vector to get vec3.
1723 V = Builder.CreateShuffleVector(V, ArrayRef<int>{0, 1, 2},
1724 "extractVec");
1725 return EmitFromMemory(V, Ty);
1726 }
1727 }
1728 }
1729
1730 // Atomic operations have to be done on integral types.
1731 LValue AtomicLValue =
1732 LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo);
1733 if (Ty->isAtomicType() || LValueIsSuitableForInlineAtomic(AtomicLValue)) {
1734 return EmitAtomicLoad(AtomicLValue, Loc).getScalarVal();
1735 }
1736
1737 llvm::LoadInst *Load = Builder.CreateLoad(Addr, Volatile);
1738 if (isNontemporal) {
1739 llvm::MDNode *Node = llvm::MDNode::get(
1740 Load->getContext(), llvm::ConstantAsMetadata::get(Builder.getInt32(1)));
1741 Load->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node);
1742 }
1743
1744 CGM.DecorateInstructionWithTBAA(Load, TBAAInfo);
1745
1746 if (EmitScalarRangeCheck(Load, Ty, Loc)) {
1747 // In order to prevent the optimizer from throwing away the check, don't
1748 // attach range metadata to the load.
1749 } else if (CGM.getCodeGenOpts().OptimizationLevel > 0)
1750 if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty))
1751 Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo);
1752
1753 return EmitFromMemory(Load, Ty);
1754}
1755
1756llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
1757 // Bool has a different representation in memory than in registers.
1758 if (hasBooleanRepresentation(Ty)) {
1759 // This should really always be an i1, but sometimes it's already
1760 // an i8, and it's awkward to track those cases down.
1761 if (Value->getType()->isIntegerTy(1))
1762 return Builder.CreateZExt(Value, ConvertTypeForMem(Ty), "frombool");
1763 assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&(static_cast <bool> (Value->getType()->isIntegerTy
(getContext().getTypeSize(Ty)) && "wrong value rep of bool"
) ? void (0) : __assert_fail ("Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && \"wrong value rep of bool\""
, "clang/lib/CodeGen/CGExpr.cpp", 1764, __extension__ __PRETTY_FUNCTION__
))
1764 "wrong value rep of bool")(static_cast <bool> (Value->getType()->isIntegerTy
(getContext().getTypeSize(Ty)) && "wrong value rep of bool"
) ? void (0) : __assert_fail ("Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && \"wrong value rep of bool\""
, "clang/lib/CodeGen/CGExpr.cpp", 1764, __extension__ __PRETTY_FUNCTION__
))
;
1765 }
1766
1767 return Value;
1768}
1769
1770llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
1771 // Bool has a different representation in memory than in registers.
1772 if (hasBooleanRepresentation(Ty)) {
1773 assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&(static_cast <bool> (Value->getType()->isIntegerTy
(getContext().getTypeSize(Ty)) && "wrong value rep of bool"
) ? void (0) : __assert_fail ("Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && \"wrong value rep of bool\""
, "clang/lib/CodeGen/CGExpr.cpp", 1774, __extension__ __PRETTY_FUNCTION__
))
1774 "wrong value rep of bool")(static_cast <bool> (Value->getType()->isIntegerTy
(getContext().getTypeSize(Ty)) && "wrong value rep of bool"
) ? void (0) : __assert_fail ("Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && \"wrong value rep of bool\""
, "clang/lib/CodeGen/CGExpr.cpp", 1774, __extension__ __PRETTY_FUNCTION__
))
;
1775 return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool");
1776 }
1777
1778 return Value;
1779}
1780
1781// Convert the pointer of \p Addr to a pointer to a vector (the value type of
1782// MatrixType), if it points to a array (the memory type of MatrixType).
1783static Address MaybeConvertMatrixAddress(Address Addr, CodeGenFunction &CGF,
1784 bool IsVector = true) {
1785 auto *ArrayTy = dyn_cast<llvm::ArrayType>(Addr.getElementType());
1786 if (ArrayTy && IsVector) {
1787 auto *VectorTy = llvm::FixedVectorType::get(ArrayTy->getElementType(),
1788 ArrayTy->getNumElements());
1789
1790 return Address(CGF.Builder.CreateElementBitCast(Addr, VectorTy));
1791 }
1792 auto *VectorTy = dyn_cast<llvm::VectorType>(Addr.getElementType());
1793 if (VectorTy && !IsVector) {
1794 auto *ArrayTy = llvm::ArrayType::get(
1795 VectorTy->getElementType(),
1796 cast<llvm::FixedVectorType>(VectorTy)->getNumElements());
1797
1798 return Address(CGF.Builder.CreateElementBitCast(Addr, ArrayTy));
1799 }
1800
1801 return Addr;
1802}
1803
1804// Emit a store of a matrix LValue. This may require casting the original
1805// pointer to memory address (ArrayType) to a pointer to the value type
1806// (VectorType).
1807static void EmitStoreOfMatrixScalar(llvm::Value *value, LValue lvalue,
1808 bool isInit, CodeGenFunction &CGF) {
1809 Address Addr = MaybeConvertMatrixAddress(lvalue.getAddress(CGF), CGF,
1810 value->getType()->isVectorTy());
1811 CGF.EmitStoreOfScalar(value, Addr, lvalue.isVolatile(), lvalue.getType(),
1812 lvalue.getBaseInfo(), lvalue.getTBAAInfo(), isInit,
1813 lvalue.isNontemporal());
1814}
1815
1816void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr,
1817 bool Volatile, QualType Ty,
1818 LValueBaseInfo BaseInfo,
1819 TBAAAccessInfo TBAAInfo,
1820 bool isInit, bool isNontemporal) {
1821 if (!CGM.getCodeGenOpts().PreserveVec3Type) {
1822 // Handle vectors differently to get better performance.
1823 if (Ty->isVectorType()) {
1824 llvm::Type *SrcTy = Value->getType();
1825 auto *VecTy = dyn_cast<llvm::VectorType>(SrcTy);
1826 // Handle vec3 special.
1827 if (VecTy && cast<llvm::FixedVectorType>(VecTy)->getNumElements() == 3) {
1828 // Our source is a vec3, do a shuffle vector to make it a vec4.
1829 Value = Builder.CreateShuffleVector(Value, ArrayRef<int>{0, 1, 2, -1},
1830 "extractVec");
1831 SrcTy = llvm::FixedVectorType::get(VecTy->getElementType(), 4);
1832 }
1833 if (Addr.getElementType() != SrcTy) {
1834 Addr = Builder.CreateElementBitCast(Addr, SrcTy, "storetmp");
1835 }
1836 }
1837 }
1838
1839 Value = EmitToMemory(Value, Ty);
1840
1841 LValue AtomicLValue =
1842 LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo);
1843 if (Ty->isAtomicType() ||
1844 (!isInit && LValueIsSuitableForInlineAtomic(AtomicLValue))) {
1845 EmitAtomicStore(RValue::get(Value), AtomicLValue, isInit);
1846 return;
1847 }
1848
1849 llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
1850 if (isNontemporal) {
1851 llvm::MDNode *Node =
1852 llvm::MDNode::get(Store->getContext(),
1853 llvm::ConstantAsMetadata::get(Builder.getInt32(1)));
1854 Store->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node);
1855 }
1856
1857 CGM.DecorateInstructionWithTBAA(Store, TBAAInfo);
1858}
1859
1860void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue,
1861 bool isInit) {
1862 if (lvalue.getType()->isConstantMatrixType()) {
1863 EmitStoreOfMatrixScalar(value, lvalue, isInit, *this);
1864 return;
1865 }
1866
1867 EmitStoreOfScalar(value, lvalue.getAddress(*this), lvalue.isVolatile(),
1868 lvalue.getType(), lvalue.getBaseInfo(),
1869 lvalue.getTBAAInfo(), isInit, lvalue.isNontemporal());
1870}
1871
1872// Emit a load of a LValue of matrix type. This may require casting the pointer
1873// to memory address (ArrayType) to a pointer to the value type (VectorType).
1874static RValue EmitLoadOfMatrixLValue(LValue LV, SourceLocation Loc,
1875 CodeGenFunction &CGF) {
1876 assert(LV.getType()->isConstantMatrixType())(static_cast <bool> (LV.getType()->isConstantMatrixType
()) ? void (0) : __assert_fail ("LV.getType()->isConstantMatrixType()"
, "clang/lib/CodeGen/CGExpr.cpp", 1876, __extension__ __PRETTY_FUNCTION__
))
;
1877 Address Addr = MaybeConvertMatrixAddress(LV.getAddress(CGF), CGF);
1878 LV.setAddress(Addr);
1879 return RValue::get(CGF.EmitLoadOfScalar(LV, Loc));
1880}
1881
1882/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
1883/// method emits the address of the lvalue, then loads the result as an rvalue,
1884/// returning the rvalue.
1885RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, SourceLocation Loc) {
1886 if (LV.isObjCWeak()) {
6
Calling 'LValue::isObjCWeak'
9
Returning from 'LValue::isObjCWeak'
10
Taking false branch
1887 // load of a __weak object.
1888 Address AddrWeakObj = LV.getAddress(*this);
1889 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
1890 AddrWeakObj));
1891 }
1892 if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
11
Assuming the condition is false
12
Taking false branch
1893 // In MRC mode, we do a load+autorelease.
1894 if (!getLangOpts().ObjCAutoRefCount) {
1895 return RValue::get(EmitARCLoadWeak(LV.getAddress(*this)));
1896 }
1897
1898 // In ARC mode, we load retained and then consume the value.
1899 llvm::Value *Object = EmitARCLoadWeakRetained(LV.getAddress(*this));
1900 Object = EmitObjCConsumeObject(LV.getType(), Object);
1901 return RValue::get(Object);
1902 }
1903
1904 if (LV.isSimple()) {
13
Calling 'LValue::isSimple'
16
Returning from 'LValue::isSimple'
17
Taking false branch
1905 assert(!LV.getType()->isFunctionType())(static_cast <bool> (!LV.getType()->isFunctionType()
) ? void (0) : __assert_fail ("!LV.getType()->isFunctionType()"
, "clang/lib/CodeGen/CGExpr.cpp", 1905, __extension__ __PRETTY_FUNCTION__
))
;
1906
1907 if (LV.getType()->isConstantMatrixType())
1908 return EmitLoadOfMatrixLValue(LV, Loc, *this);
1909
1910 // Everything needs a load.
1911 return RValue::get(EmitLoadOfScalar(LV, Loc));
1912 }
1913
1914 if (LV.isVectorElt()) {
18
Calling 'LValue::isVectorElt'
21
Returning from 'LValue::isVectorElt'
22
Taking false branch
1915 llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddress(),
1916 LV.isVolatileQualified());
1917 return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(),
1918 "vecext"));
1919 }
1920
1921 // If this is a reference to a subset of the elements of a vector, either
1922 // shuffle the input or extract/insert them as appropriate.
1923 if (LV.isExtVectorElt()) {
23
Calling 'LValue::isExtVectorElt'
26
Returning from 'LValue::isExtVectorElt'
27
Taking false branch
1924 return EmitLoadOfExtVectorElementLValue(LV);
1925 }
1926
1927 // Global Register variables always invoke intrinsics
1928 if (LV.isGlobalReg())
28
Calling 'LValue::isGlobalReg'
31
Returning from 'LValue::isGlobalReg'
32
Taking false branch
1929 return EmitLoadOfGlobalRegLValue(LV);
1930
1931 if (LV.isMatrixElt()) {
33
Calling 'LValue::isMatrixElt'
36
Returning from 'LValue::isMatrixElt'
37
Taking true branch
1932 llvm::Value *Idx = LV.getMatrixIdx();
1933 if (CGM.getCodeGenOpts().OptimizationLevel > 0) {
38
Assuming field 'OptimizationLevel' is > 0
39
Taking true branch
1934 const auto *const MatTy = LV.getType()->getAs<ConstantMatrixType>();
40
Assuming the object is not a 'ConstantMatrixType'
41
'MatTy' initialized to a null pointer value
1935 llvm::MatrixBuilder<CGBuilderTy> MB(Builder);
1936 MB.CreateIndexAssumption(Idx, MatTy->getNumElementsFlattened());
42
Called C++ object pointer is null
1937 }
1938 llvm::LoadInst *Load =
1939 Builder.CreateLoad(LV.getMatrixAddress(), LV.isVolatileQualified());
1940 return RValue::get(Builder.CreateExtractElement(Load, Idx, "matrixext"));
1941 }
1942
1943 assert(LV.isBitField() && "Unknown LValue type!")(static_cast <bool> (LV.isBitField() && "Unknown LValue type!"
) ? void (0) : __assert_fail ("LV.isBitField() && \"Unknown LValue type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 1943, __extension__ __PRETTY_FUNCTION__
))
;
1944 return EmitLoadOfBitfieldLValue(LV, Loc);
1945}
1946
1947RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
1948 SourceLocation Loc) {
1949 const CGBitFieldInfo &Info = LV.getBitFieldInfo();
1950
1951 // Get the output type.
1952 llvm::Type *ResLTy = ConvertType(LV.getType());
1953
1954 Address Ptr = LV.getBitFieldAddress();
1955 llvm::Value *Val =
1956 Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "bf.load");
1957
1958 bool UseVolatile = LV.isVolatileQualified() &&
1959 Info.VolatileStorageSize != 0 && isAAPCS(CGM.getTarget());
1960 const unsigned Offset = UseVolatile ? Info.VolatileOffset : Info.Offset;
1961 const unsigned StorageSize =
1962 UseVolatile ? Info.VolatileStorageSize : Info.StorageSize;
1963 if (Info.IsSigned) {
1964 assert(static_cast<unsigned>(Offset + Info.Size) <= StorageSize)(static_cast <bool> (static_cast<unsigned>(Offset
+ Info.Size) <= StorageSize) ? void (0) : __assert_fail (
"static_cast<unsigned>(Offset + Info.Size) <= StorageSize"
, "clang/lib/CodeGen/CGExpr.cpp", 1964, __extension__ __PRETTY_FUNCTION__
))
;
1965 unsigned HighBits = StorageSize - Offset - Info.Size;
1966 if (HighBits)
1967 Val = Builder.CreateShl(Val, HighBits, "bf.shl");
1968 if (Offset + HighBits)
1969 Val = Builder.CreateAShr(Val, Offset + HighBits, "bf.ashr");
1970 } else {
1971 if (Offset)
1972 Val = Builder.CreateLShr(Val, Offset, "bf.lshr");
1973 if (static_cast<unsigned>(Offset) + Info.Size < StorageSize)
1974 Val = Builder.CreateAnd(
1975 Val, llvm::APInt::getLowBitsSet(StorageSize, Info.Size), "bf.clear");
1976 }
1977 Val = Builder.CreateIntCast(Val, ResLTy, Info.IsSigned, "bf.cast");
1978 EmitScalarRangeCheck(Val, LV.getType(), Loc);
1979 return RValue::get(Val);
1980}
1981
1982// If this is a reference to a subset of the elements of a vector, create an
1983// appropriate shufflevector.
1984RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) {
1985 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddress(),
1986 LV.isVolatileQualified());
1987
1988 const llvm::Constant *Elts = LV.getExtVectorElts();
1989
1990 // If the result of the expression is a non-vector type, we must be extracting
1991 // a single element. Just codegen as an extractelement.
1992 const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
1993 if (!ExprVT) {
1994 unsigned InIdx = getAccessedFieldNo(0, Elts);
1995 llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
1996 return RValue::get(Builder.CreateExtractElement(Vec, Elt));
1997 }
1998
1999 // Always use shuffle vector to try to retain the original program structure
2000 unsigned NumResultElts = ExprVT->getNumElements();
2001
2002 SmallVector<int, 4> Mask;
2003 for (unsigned i = 0; i != NumResultElts; ++i)
2004 Mask.push_back(getAccessedFieldNo(i, Elts));
2005
2006 Vec = Builder.CreateShuffleVector(Vec, Mask);
2007 return RValue::get(Vec);
2008}
2009
2010/// Generates lvalue for partial ext_vector access.
2011Address CodeGenFunction::EmitExtVectorElementLValue(LValue LV) {
2012 Address VectorAddress = LV.getExtVectorAddress();
2013 QualType EQT = LV.getType()->castAs<VectorType>()->getElementType();
2014 llvm::Type *VectorElementTy = CGM.getTypes().ConvertType(EQT);
2015
2016 Address CastToPointerElement =
2017 Builder.CreateElementBitCast(VectorAddress, VectorElementTy,
2018 "conv.ptr.element");
2019
2020 const llvm::Constant *Elts = LV.getExtVectorElts();
2021 unsigned ix = getAccessedFieldNo(0, Elts);
2022
2023 Address VectorBasePtrPlusIx =
2024 Builder.CreateConstInBoundsGEP(CastToPointerElement, ix,
2025 "vector.elt");
2026
2027 return VectorBasePtrPlusIx;
2028}
2029
2030/// Load of global gamed gegisters are always calls to intrinsics.
2031RValue CodeGenFunction::EmitLoadOfGlobalRegLValue(LValue LV) {
2032 assert((LV.getType()->isIntegerType() || LV.getType()->isPointerType()) &&(static_cast <bool> ((LV.getType()->isIntegerType() ||
LV.getType()->isPointerType()) && "Bad type for register variable"
) ? void (0) : __assert_fail ("(LV.getType()->isIntegerType() || LV.getType()->isPointerType()) && \"Bad type for register variable\""
, "clang/lib/CodeGen/CGExpr.cpp", 2033, __extension__ __PRETTY_FUNCTION__
))
2033 "Bad type for register variable")(static_cast <bool> ((LV.getType()->isIntegerType() ||
LV.getType()->isPointerType()) && "Bad type for register variable"
) ? void (0) : __assert_fail ("(LV.getType()->isIntegerType() || LV.getType()->isPointerType()) && \"Bad type for register variable\""
, "clang/lib/CodeGen/CGExpr.cpp", 2033, __extension__ __PRETTY_FUNCTION__
))
;
2034 llvm::MDNode *RegName = cast<llvm::MDNode>(
2035 cast<llvm::MetadataAsValue>(LV.getGlobalReg())->getMetadata());
2036
2037 // We accept integer and pointer types only
2038 llvm::Type *OrigTy = CGM.getTypes().ConvertType(LV.getType());
2039 llvm::Type *Ty = OrigTy;
2040 if (OrigTy->isPointerTy())
2041 Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
2042 llvm::Type *Types[] = { Ty };
2043
2044 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::read_register, Types);
2045 llvm::Value *Call = Builder.CreateCall(
2046 F, llvm::MetadataAsValue::get(Ty->getContext(), RegName));
2047 if (OrigTy->isPointerTy())
2048 Call = Builder.CreateIntToPtr(Call, OrigTy);
2049 return RValue::get(Call);
2050}
2051
2052/// EmitStoreThroughLValue - Store the specified rvalue into the specified
2053/// lvalue, where both are guaranteed to the have the same type, and that type
2054/// is 'Ty'.
2055void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
2056 bool isInit) {
2057 if (!Dst.isSimple()) {
2058 if (Dst.isVectorElt()) {
2059 // Read/modify/write the vector, inserting the new element.
2060 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddress(),
2061 Dst.isVolatileQualified());
2062 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
2063 Dst.getVectorIdx(), "vecins");
2064 Builder.CreateStore(Vec, Dst.getVectorAddress(),
2065 Dst.isVolatileQualified());
2066 return;
2067 }
2068
2069 // If this is an update of extended vector elements, insert them as
2070 // appropriate.
2071 if (Dst.isExtVectorElt())
2072 return EmitStoreThroughExtVectorComponentLValue(Src, Dst);
2073
2074 if (Dst.isGlobalReg())
2075 return EmitStoreThroughGlobalRegLValue(Src, Dst);
2076
2077 if (Dst.isMatrixElt()) {
2078 llvm::Value *Idx = Dst.getMatrixIdx();
2079 if (CGM.getCodeGenOpts().OptimizationLevel > 0) {
2080 const auto *const MatTy = Dst.getType()->getAs<ConstantMatrixType>();
2081 llvm::MatrixBuilder<CGBuilderTy> MB(Builder);
2082 MB.CreateIndexAssumption(Idx, MatTy->getNumElementsFlattened());
2083 }
2084 llvm::Instruction *Load = Builder.CreateLoad(Dst.getMatrixAddress());
2085 llvm::Value *Vec =
2086 Builder.CreateInsertElement(Load, Src.getScalarVal(), Idx, "matins");
2087 Builder.CreateStore(Vec, Dst.getMatrixAddress(),
2088 Dst.isVolatileQualified());
2089 return;
2090 }
2091
2092 assert(Dst.isBitField() && "Unknown LValue type")(static_cast <bool> (Dst.isBitField() && "Unknown LValue type"
) ? void (0) : __assert_fail ("Dst.isBitField() && \"Unknown LValue type\""
, "clang/lib/CodeGen/CGExpr.cpp", 2092, __extension__ __PRETTY_FUNCTION__
))
;
2093 return EmitStoreThroughBitfieldLValue(Src, Dst);
2094 }
2095
2096 // There's special magic for assigning into an ARC-qualified l-value.
2097 if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) {
2098 switch (Lifetime) {
2099 case Qualifiers::OCL_None:
2100 llvm_unreachable("present but none")::llvm::llvm_unreachable_internal("present but none", "clang/lib/CodeGen/CGExpr.cpp"
, 2100)
;
2101
2102 case Qualifiers::OCL_ExplicitNone:
2103 // nothing special
2104 break;
2105
2106 case Qualifiers::OCL_Strong:
2107 if (isInit) {
2108 Src = RValue::get(EmitARCRetain(Dst.getType(), Src.getScalarVal()));
2109 break;
2110 }
2111 EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true);
2112 return;
2113
2114 case Qualifiers::OCL_Weak:
2115 if (isInit)
2116 // Initialize and then skip the primitive store.
2117 EmitARCInitWeak(Dst.getAddress(*this), Src.getScalarVal());
2118 else
2119 EmitARCStoreWeak(Dst.getAddress(*this), Src.getScalarVal(),
2120 /*ignore*/ true);
2121 return;
2122
2123 case Qualifiers::OCL_Autoreleasing:
2124 Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(),
2125 Src.getScalarVal()));
2126 // fall into the normal path
2127 break;
2128 }
2129 }
2130
2131 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
2132 // load of a __weak object.
2133 Address LvalueDst = Dst.getAddress(*this);
2134 llvm::Value *src = Src.getScalarVal();
2135 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
2136 return;
2137 }
2138
2139 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
2140 // load of a __strong object.
2141 Address LvalueDst = Dst.getAddress(*this);
2142 llvm::Value *src = Src.getScalarVal();
2143 if (Dst.isObjCIvar()) {
2144 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL")(static_cast <bool> (Dst.getBaseIvarExp() && "BaseIvarExp is NULL"
) ? void (0) : __assert_fail ("Dst.getBaseIvarExp() && \"BaseIvarExp is NULL\""
, "clang/lib/CodeGen/CGExpr.cpp", 2144, __extension__ __PRETTY_FUNCTION__
))
;
2145 llvm::Type *ResultType = IntPtrTy;
2146 Address dst = EmitPointerWithAlignment(Dst.getBaseIvarExp());
2147 llvm::Value *RHS = dst.getPointer();
2148 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
2149 llvm::Value *LHS =
2150 Builder.CreatePtrToInt(LvalueDst.getPointer(), ResultType,
2151 "sub.ptr.lhs.cast");
2152 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
2153 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
2154 BytesBetween);
2155 } else if (Dst.isGlobalObjCRef()) {
2156 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst,
2157 Dst.isThreadLocalRef());
2158 }
2159 else
2160 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
2161 return;
2162 }
2163
2164 assert(Src.isScalar() && "Can't emit an agg store with this method")(static_cast <bool> (Src.isScalar() && "Can't emit an agg store with this method"
) ? void (0) : __assert_fail ("Src.isScalar() && \"Can't emit an agg store with this method\""
, "clang/lib/CodeGen/CGExpr.cpp", 2164, __extension__ __PRETTY_FUNCTION__
))
;
2165 EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit);
2166}
2167
2168void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
2169 llvm::Value **Result) {
2170 const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
2171 llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType());
2172 Address Ptr = Dst.getBitFieldAddress();
2173
2174 // Get the source value, truncated to the width of the bit-field.
2175 llvm::Value *SrcVal = Src.getScalarVal();
2176
2177 // Cast the source to the storage type and shift it into place.
2178 SrcVal = Builder.CreateIntCast(SrcVal, Ptr.getElementType(),
2179 /*isSigned=*/false);
2180 llvm::Value *MaskedVal = SrcVal;
2181
2182 const bool UseVolatile =
2183 CGM.getCodeGenOpts().AAPCSBitfieldWidth && Dst.isVolatileQualified() &&
2184 Info.VolatileStorageSize != 0 && isAAPCS(CGM.getTarget());
2185 const unsigned StorageSize =
2186 UseVolatile ? Info.VolatileStorageSize : Info.StorageSize;
2187 const unsigned Offset = UseVolatile ? Info.VolatileOffset : Info.Offset;
2188 // See if there are other bits in the bitfield's storage we'll need to load
2189 // and mask together with source before storing.
2190 if (StorageSize != Info.Size) {
2191 assert(StorageSize > Info.Size && "Invalid bitfield size.")(static_cast <bool> (StorageSize > Info.Size &&
"Invalid bitfield size.") ? void (0) : __assert_fail ("StorageSize > Info.Size && \"Invalid bitfield size.\""
, "clang/lib/CodeGen/CGExpr.cpp", 2191, __extension__ __PRETTY_FUNCTION__
))
;
2192 llvm::Value *Val =
2193 Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), "bf.load");
2194
2195 // Mask the source value as needed.
2196 if (!hasBooleanRepresentation(Dst.getType()))
2197 SrcVal = Builder.CreateAnd(
2198 SrcVal, llvm::APInt::getLowBitsSet(StorageSize, Info.Size),
2199 "bf.value");
2200 MaskedVal = SrcVal;
2201 if (Offset)
2202 SrcVal = Builder.CreateShl(SrcVal, Offset, "bf.shl");
2203
2204 // Mask out the original value.
2205 Val = Builder.CreateAnd(
2206 Val, ~llvm::APInt::getBitsSet(StorageSize, Offset, Offset + Info.Size),
2207 "bf.clear");
2208
2209 // Or together the unchanged values and the source value.
2210 SrcVal = Builder.CreateOr(Val, SrcVal, "bf.set");
2211 } else {
2212 assert(Offset == 0)(static_cast <bool> (Offset == 0) ? void (0) : __assert_fail
("Offset == 0", "clang/lib/CodeGen/CGExpr.cpp", 2212, __extension__
__PRETTY_FUNCTION__))
;
2213 // According to the AACPS:
2214 // When a volatile bit-field is written, and its container does not overlap
2215 // with any non-bit-field member, its container must be read exactly once
2216 // and written exactly once using the access width appropriate to the type
2217 // of the container. The two accesses are not atomic.
2218 if (Dst.isVolatileQualified() && isAAPCS(CGM.getTarget()) &&
2219 CGM.getCodeGenOpts().ForceAAPCSBitfieldLoad)
2220 Builder.CreateLoad(Ptr, true, "bf.load");
2221 }
2222
2223 // Write the new value back out.
2224 Builder.CreateStore(SrcVal, Ptr, Dst.isVolatileQualified());
2225
2226 // Return the new value of the bit-field, if requested.
2227 if (Result) {
2228 llvm::Value *ResultVal = MaskedVal;
2229
2230 // Sign extend the value if needed.
2231 if (Info.IsSigned) {
2232 assert(Info.Size <= StorageSize)(static_cast <bool> (Info.Size <= StorageSize) ? void
(0) : __assert_fail ("Info.Size <= StorageSize", "clang/lib/CodeGen/CGExpr.cpp"
, 2232, __extension__ __PRETTY_FUNCTION__))
;
2233 unsigned HighBits = StorageSize - Info.Size;
2234 if (HighBits) {
2235 ResultVal = Builder.CreateShl(ResultVal, HighBits, "bf.result.shl");
2236 ResultVal = Builder.CreateAShr(ResultVal, HighBits, "bf.result.ashr");
2237 }
2238 }
2239
2240 ResultVal = Builder.CreateIntCast(ResultVal, ResLTy, Info.IsSigned,
2241 "bf.result.cast");
2242 *Result = EmitFromMemory(ResultVal, Dst.getType());
2243 }
2244}
2245
2246void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
2247 LValue Dst) {
2248 // This access turns into a read/modify/write of the vector. Load the input
2249 // value now.
2250 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddress(),
2251 Dst.isVolatileQualified());
2252 const llvm::Constant *Elts = Dst.getExtVectorElts();
2253
2254 llvm::Value *SrcVal = Src.getScalarVal();
2255
2256 if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) {
2257 unsigned NumSrcElts = VTy->getNumElements();
2258 unsigned NumDstElts =
2259 cast<llvm::FixedVectorType>(Vec->getType())->getNumElements();
2260 if (NumDstElts == NumSrcElts) {
2261 // Use shuffle vector is the src and destination are the same number of
2262 // elements and restore the vector mask since it is on the side it will be
2263 // stored.
2264 SmallVector<int, 4> Mask(NumDstElts);
2265 for (unsigned i = 0; i != NumSrcElts; ++i)
2266 Mask[getAccessedFieldNo(i, Elts)] = i;
2267
2268 Vec = Builder.CreateShuffleVector(SrcVal, Mask);
2269 } else if (NumDstElts > NumSrcElts) {
2270 // Extended the source vector to the same length and then shuffle it
2271 // into the destination.
2272 // FIXME: since we're shuffling with undef, can we just use the indices
2273 // into that? This could be simpler.
2274 SmallVector<int, 4> ExtMask;
2275 for (unsigned i = 0; i != NumSrcElts; ++i)
2276 ExtMask.push_back(i);
2277 ExtMask.resize(NumDstElts, -1);
2278 llvm::Value *ExtSrcVal = Builder.CreateShuffleVector(SrcVal, ExtMask);
2279 // build identity
2280 SmallVector<int, 4> Mask;
2281 for (unsigned i = 0; i != NumDstElts; ++i)
2282 Mask.push_back(i);
2283
2284 // When the vector size is odd and .odd or .hi is used, the last element
2285 // of the Elts constant array will be one past the size of the vector.
2286 // Ignore the last element here, if it is greater than the mask size.
2287 if (getAccessedFieldNo(NumSrcElts - 1, Elts) == Mask.size())
2288 NumSrcElts--;
2289
2290 // modify when what gets shuffled in
2291 for (unsigned i = 0; i != NumSrcElts; ++i)
2292 Mask[getAccessedFieldNo(i, Elts)] = i + NumDstElts;
2293 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, Mask);
2294 } else {
2295 // We should never shorten the vector
2296 llvm_unreachable("unexpected shorten vector length")::llvm::llvm_unreachable_internal("unexpected shorten vector length"
, "clang/lib/CodeGen/CGExpr.cpp", 2296)
;
2297 }
2298 } else {
2299 // If the Src is a scalar (not a vector) it must be updating one element.
2300 unsigned InIdx = getAccessedFieldNo(0, Elts);
2301 llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
2302 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt);
2303 }
2304
2305 Builder.CreateStore(Vec, Dst.getExtVectorAddress(),
2306 Dst.isVolatileQualified());
2307}
2308
2309/// Store of global named registers are always calls to intrinsics.
2310void CodeGenFunction::EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst) {
2311 assert((Dst.getType()->isIntegerType() || Dst.getType()->isPointerType()) &&(static_cast <bool> ((Dst.getType()->isIntegerType()
|| Dst.getType()->isPointerType()) && "Bad type for register variable"
) ? void (0) : __assert_fail ("(Dst.getType()->isIntegerType() || Dst.getType()->isPointerType()) && \"Bad type for register variable\""
, "clang/lib/CodeGen/CGExpr.cpp", 2312, __extension__ __PRETTY_FUNCTION__
))
2312 "Bad type for register variable")(static_cast <bool> ((Dst.getType()->isIntegerType()
|| Dst.getType()->isPointerType()) && "Bad type for register variable"
) ? void (0) : __assert_fail ("(Dst.getType()->isIntegerType() || Dst.getType()->isPointerType()) && \"Bad type for register variable\""
, "clang/lib/CodeGen/CGExpr.cpp", 2312, __extension__ __PRETTY_FUNCTION__
))
;
2313 llvm::MDNode *RegName = cast<llvm::MDNode>(
2314 cast<llvm::MetadataAsValue>(Dst.getGlobalReg())->getMetadata());
2315 assert(RegName && "Register LValue is not metadata")(static_cast <bool> (RegName && "Register LValue is not metadata"
) ? void (0) : __assert_fail ("RegName && \"Register LValue is not metadata\""
, "clang/lib/CodeGen/CGExpr.cpp", 2315, __extension__ __PRETTY_FUNCTION__
))
;
2316
2317 // We accept integer and pointer types only
2318 llvm::Type *OrigTy = CGM.getTypes().ConvertType(Dst.getType());
2319 llvm::Type *Ty = OrigTy;
2320 if (OrigTy->isPointerTy())
2321 Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
2322 llvm::Type *Types[] = { Ty };
2323
2324 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::write_register, Types);
2325 llvm::Value *Value = Src.getScalarVal();
2326 if (OrigTy->isPointerTy())
2327 Value = Builder.CreatePtrToInt(Value, Ty);
2328 Builder.CreateCall(
2329 F, {llvm::MetadataAsValue::get(Ty->getContext(), RegName), Value});
2330}
2331
2332// setObjCGCLValueClass - sets class of the lvalue for the purpose of
2333// generating write-barries API. It is currently a global, ivar,
2334// or neither.
2335static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
2336 LValue &LV,
2337 bool IsMemberAccess=false) {
2338 if (Ctx.getLangOpts().getGC() == LangOptions::NonGC)
2339 return;
2340
2341 if (isa<ObjCIvarRefExpr>(E)) {
2342 QualType ExpTy = E->getType();
2343 if (IsMemberAccess && ExpTy->isPointerType()) {
2344 // If ivar is a structure pointer, assigning to field of
2345 // this struct follows gcc's behavior and makes it a non-ivar
2346 // writer-barrier conservatively.
2347 ExpTy = ExpTy->castAs<PointerType>()->getPointeeType();
2348 if (ExpTy->isRecordType()) {
2349 LV.setObjCIvar(false);
2350 return;
2351 }
2352 }
2353 LV.setObjCIvar(true);
2354 auto *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr *>(E));
2355 LV.setBaseIvarExp(Exp->getBase());
2356 LV.setObjCArray(E->getType()->isArrayType());
2357 return;
2358 }
2359
2360 if (const auto *Exp = dyn_cast<DeclRefExpr>(E)) {
2361 if (const auto *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
2362 if (VD->hasGlobalStorage()) {
2363 LV.setGlobalObjCRef(true);
2364 LV.setThreadLocalRef(VD->getTLSKind() != VarDecl::TLS_None);
2365 }
2366 }
2367 LV.setObjCArray(E->getType()->isArrayType());
2368 return;
2369 }
2370
2371 if (const auto *Exp = dyn_cast<UnaryOperator>(E)) {
2372 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2373 return;
2374 }
2375
2376 if (const auto *Exp = dyn_cast<ParenExpr>(E)) {
2377 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2378 if (LV.isObjCIvar()) {
2379 // If cast is to a structure pointer, follow gcc's behavior and make it
2380 // a non-ivar write-barrier.
2381 QualType ExpTy = E->getType();
2382 if (ExpTy->isPointerType())
2383 ExpTy = ExpTy->castAs<PointerType>()->getPointeeType();
2384 if (ExpTy->isRecordType())
2385 LV.setObjCIvar(false);
2386 }
2387 return;
2388 }
2389
2390 if (const auto *Exp = dyn_cast<GenericSelectionExpr>(E)) {
2391 setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV);
2392 return;
2393 }
2394
2395 if (const auto *Exp = dyn_cast<ImplicitCastExpr>(E)) {
2396 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2397 return;
2398 }
2399
2400 if (const auto *Exp = dyn_cast<CStyleCastExpr>(E)) {
2401 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2402 return;
2403 }
2404
2405 if (const auto *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) {
2406 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2407 return;
2408 }
2409
2410 if (const auto *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
2411 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
2412 if (LV.isObjCIvar() && !LV.isObjCArray())
2413 // Using array syntax to assigning to what an ivar points to is not
2414 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
2415 LV.setObjCIvar(false);
2416 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
2417 // Using array syntax to assigning to what global points to is not
2418 // same as assigning to the global itself. {id *G;} G[i] = 0;
2419 LV.setGlobalObjCRef(false);
2420 return;
2421 }
2422
2423 if (const auto *Exp = dyn_cast<MemberExpr>(E)) {
2424 setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true);
2425 // We don't know if member is an 'ivar', but this flag is looked at
2426 // only in the context of LV.isObjCIvar().
2427 LV.setObjCArray(E->getType()->isArrayType());
2428 return;
2429 }
2430}
2431
2432static llvm::Value *
2433EmitBitCastOfLValueToProperType(CodeGenFunction &CGF,
2434 llvm::Value *V, llvm::Type *IRType,
2435 StringRef Name = StringRef()) {
2436 unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
2437 return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name);
2438}
2439
2440static LValue EmitThreadPrivateVarDeclLValue(
2441 CodeGenFunction &CGF, const VarDecl *VD, QualType T, Address Addr,
2442 llvm::Type *RealVarTy, SourceLocation Loc) {
2443 if (CGF.CGM.getLangOpts().OpenMPIRBuilder)
2444 Addr = CodeGenFunction::OMPBuilderCBHelpers::getAddrOfThreadPrivate(
2445 CGF, VD, Addr, Loc);
2446 else
2447 Addr =
2448 CGF.CGM.getOpenMPRuntime().getAddrOfThreadPrivate(CGF, VD, Addr, Loc);
2449
2450 Addr = CGF.Builder.CreateElementBitCast(Addr, RealVarTy);
2451 return CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl);
2452}
2453
2454static Address emitDeclTargetVarDeclLValue(CodeGenFunction &CGF,
2455 const VarDecl *VD, QualType T) {
2456 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
2457 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
2458 // Return an invalid address if variable is MT_To and unified
2459 // memory is not enabled. For all other cases: MT_Link and
2460 // MT_To with unified memory, return a valid address.
2461 if (!Res || (*Res == OMPDeclareTargetDeclAttr::MT_To &&
2462 !CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory()))
2463 return Address::invalid();
2464 assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||(static_cast <bool> (((*Res == OMPDeclareTargetDeclAttr
::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To &&
CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())
) && "Expected link clause OR to clause with unified memory enabled."
) ? void (0) : __assert_fail ("((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To && CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())) && \"Expected link clause OR to clause with unified memory enabled.\""
, "clang/lib/CodeGen/CGExpr.cpp", 2467, __extension__ __PRETTY_FUNCTION__
))
2465 (*Res == OMPDeclareTargetDeclAttr::MT_To &&(static_cast <bool> (((*Res == OMPDeclareTargetDeclAttr
::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To &&
CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())
) && "Expected link clause OR to clause with unified memory enabled."
) ? void (0) : __assert_fail ("((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To && CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())) && \"Expected link clause OR to clause with unified memory enabled.\""
, "clang/lib/CodeGen/CGExpr.cpp", 2467, __extension__ __PRETTY_FUNCTION__
))
2466 CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())) &&(static_cast <bool> (((*Res == OMPDeclareTargetDeclAttr
::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To &&
CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())
) && "Expected link clause OR to clause with unified memory enabled."
) ? void (0) : __assert_fail ("((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To && CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())) && \"Expected link clause OR to clause with unified memory enabled.\""
, "clang/lib/CodeGen/CGExpr.cpp", 2467, __extension__ __PRETTY_FUNCTION__
))
2467 "Expected link clause OR to clause with unified memory enabled.")(static_cast <bool> (((*Res == OMPDeclareTargetDeclAttr
::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To &&
CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())
) && "Expected link clause OR to clause with unified memory enabled."
) ? void (0) : __assert_fail ("((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To && CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())) && \"Expected link clause OR to clause with unified memory enabled.\""
, "clang/lib/CodeGen/CGExpr.cpp", 2467, __extension__ __PRETTY_FUNCTION__
))
;
2468 QualType PtrTy = CGF.getContext().getPointerType(VD->getType());
2469 Address Addr = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetVar(VD);
2470 return CGF.EmitLoadOfPointer(Addr, PtrTy->castAs<PointerType>());
2471}
2472
2473Address
2474CodeGenFunction::EmitLoadOfReference(LValue RefLVal,
2475 LValueBaseInfo *PointeeBaseInfo,
2476 TBAAAccessInfo *PointeeTBAAInfo) {
2477 llvm::LoadInst *Load =
2478 Builder.CreateLoad(RefLVal.getAddress(*this), RefLVal.isVolatile());
2479 CGM.DecorateInstructionWithTBAA(Load, RefLVal.getTBAAInfo());
2480
2481 QualType PointeeType = RefLVal.getType()->getPointeeType();
2482 CharUnits Align = CGM.getNaturalTypeAlignment(
2483 PointeeType, PointeeBaseInfo, PointeeTBAAInfo,
2484 /* forPointeeType= */ true);
2485 return Address(Load, ConvertTypeForMem(PointeeType), Align);
2486}
2487
2488LValue CodeGenFunction::EmitLoadOfReferenceLValue(LValue RefLVal) {
2489 LValueBaseInfo PointeeBaseInfo;
2490 TBAAAccessInfo PointeeTBAAInfo;
2491 Address PointeeAddr = EmitLoadOfReference(RefLVal, &PointeeBaseInfo,
2492 &PointeeTBAAInfo);
2493 return MakeAddrLValue(PointeeAddr, RefLVal.getType()->getPointeeType(),
2494 PointeeBaseInfo, PointeeTBAAInfo);
2495}
2496
2497Address CodeGenFunction::EmitLoadOfPointer(Address Ptr,
2498 const PointerType *PtrTy,
2499 LValueBaseInfo *BaseInfo,
2500 TBAAAccessInfo *TBAAInfo) {
2501 llvm::Value *Addr = Builder.CreateLoad(Ptr);
2502 return Address(Addr, CGM.getNaturalTypeAlignment(PtrTy->getPointeeType(),
2503 BaseInfo, TBAAInfo,
2504 /*forPointeeType=*/true));
2505}
2506
2507LValue CodeGenFunction::EmitLoadOfPointerLValue(Address PtrAddr,
2508 const PointerType *PtrTy) {
2509 LValueBaseInfo BaseInfo;
2510 TBAAAccessInfo TBAAInfo;
2511 Address Addr = EmitLoadOfPointer(PtrAddr, PtrTy, &BaseInfo, &TBAAInfo);
2512 return MakeAddrLValue(Addr, PtrTy->getPointeeType(), BaseInfo, TBAAInfo);
2513}
2514
2515static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
2516 const Expr *E, const VarDecl *VD) {
2517 QualType T = E->getType();
2518
2519 // If it's thread_local, emit a call to its wrapper function instead.
2520 if (VD->getTLSKind() == VarDecl::TLS_Dynamic &&
2521 CGF.CGM.getCXXABI().usesThreadWrapperFunction(VD))
2522 return CGF.CGM.getCXXABI().EmitThreadLocalVarDeclLValue(CGF, VD, T);
2523 // Check if the variable is marked as declare target with link clause in
2524 // device codegen.
2525 if (CGF.getLangOpts().OpenMPIsDevice) {
2526 Address Addr = emitDeclTargetVarDeclLValue(CGF, VD, T);
2527 if (Addr.isValid())
2528 return CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl);
2529 }
2530
2531 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
2532 llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType());
2533 V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy);
2534 CharUnits Alignment = CGF.getContext().getDeclAlign(VD);
2535 Address Addr(V, RealVarTy, Alignment);
2536 // Emit reference to the private copy of the variable if it is an OpenMP
2537 // threadprivate variable.
2538 if (CGF.getLangOpts().OpenMP && !CGF.getLangOpts().OpenMPSimd &&
2539 VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
2540 return EmitThreadPrivateVarDeclLValue(CGF, VD, T, Addr, RealVarTy,
2541 E->getExprLoc());
2542 }
2543 LValue LV = VD->getType()->isReferenceType() ?
2544 CGF.EmitLoadOfReferenceLValue(Addr, VD->getType(),
2545 AlignmentSource::Decl) :
2546 CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl);
2547 setObjCGCLValueClass(CGF.getContext(), E, LV);
2548 return LV;
2549}
2550
2551static llvm::Constant *EmitFunctionDeclPointer(CodeGenModule &CGM,
2552 GlobalDecl GD) {
2553 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2554 if (FD->hasAttr<WeakRefAttr>()) {
2555 ConstantAddress aliasee = CGM.GetWeakRefReference(FD);
2556 return aliasee.getPointer();
2557 }
2558
2559 llvm::Constant *V = CGM.GetAddrOfFunction(GD);
2560 if (!FD->hasPrototype()) {
2561 if (const FunctionProtoType *Proto =
2562 FD->getType()->getAs<FunctionProtoType>()) {
2563 // Ugly case: for a K&R-style definition, the type of the definition
2564 // isn't the same as the type of a use. Correct for this with a
2565 // bitcast.
2566 QualType NoProtoType =
2567 CGM.getContext().getFunctionNoProtoType(Proto->getReturnType());
2568 NoProtoType = CGM.getContext().getPointerType(NoProtoType);
2569 V = llvm::ConstantExpr::getBitCast(V,
2570 CGM.getTypes().ConvertType(NoProtoType));
2571 }
2572 }
2573 return V;
2574}
2575
2576static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, const Expr *E,
2577 GlobalDecl GD) {
2578 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2579 llvm::Value *V = EmitFunctionDeclPointer(CGF.CGM, GD);
2580 CharUnits Alignment = CGF.getContext().getDeclAlign(FD);
2581 return CGF.MakeAddrLValue(V, E->getType(), Alignment,
2582 AlignmentSource::Decl);
2583}
2584
2585static LValue EmitCapturedFieldLValue(CodeGenFunction &CGF, const FieldDecl *FD,
2586 llvm::Value *ThisValue) {
2587 QualType TagType = CGF.getContext().getTagDeclType(FD->getParent());
2588 LValue LV = CGF.MakeNaturalAlignAddrLValue(ThisValue, TagType);
2589 return CGF.EmitLValueForField(LV, FD);
2590}
2591
2592/// Named Registers are named metadata pointing to the register name
2593/// which will be read from/written to as an argument to the intrinsic
2594/// @llvm.read/write_register.
2595/// So far, only the name is being passed down, but other options such as
2596/// register type, allocation type or even optimization options could be
2597/// passed down via the metadata node.
2598static LValue EmitGlobalNamedRegister(const VarDecl *VD, CodeGenModule &CGM) {
2599 SmallString<64> Name("llvm.named.register.");
2600 AsmLabelAttr *Asm = VD->getAttr<AsmLabelAttr>();
2601 assert(Asm->getLabel().size() < 64-Name.size() &&(static_cast <bool> (Asm->getLabel().size() < 64-
Name.size() && "Register name too big") ? void (0) : __assert_fail
("Asm->getLabel().size() < 64-Name.size() && \"Register name too big\""
, "clang/lib/CodeGen/CGExpr.cpp", 2602, __extension__ __PRETTY_FUNCTION__
))
2602 "Register name too big")(static_cast <bool> (Asm->getLabel().size() < 64-
Name.size() && "Register name too big") ? void (0) : __assert_fail
("Asm->getLabel().size() < 64-Name.size() && \"Register name too big\""
, "clang/lib/CodeGen/CGExpr.cpp", 2602, __extension__ __PRETTY_FUNCTION__
))
;
2603 Name.append(Asm->getLabel());
2604 llvm::NamedMDNode *M =
2605 CGM.getModule().getOrInsertNamedMetadata(Name);
2606 if (M->getNumOperands() == 0) {
2607 llvm::MDString *Str = llvm::MDString::get(CGM.getLLVMContext(),
2608 Asm->getLabel());
2609 llvm::Metadata *Ops[] = {Str};
2610 M->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
2611 }
2612
2613 CharUnits Alignment = CGM.getContext().getDeclAlign(VD);
2614
2615 llvm::Value *Ptr =
2616 llvm::MetadataAsValue::get(CGM.getLLVMContext(), M->getOperand(0));
2617 return LValue::MakeGlobalReg(Ptr, Alignment, VD->getType());
2618}
2619
2620/// Determine whether we can emit a reference to \p VD from the current
2621/// context, despite not necessarily having seen an odr-use of the variable in
2622/// this context.
2623static bool canEmitSpuriousReferenceToVariable(CodeGenFunction &CGF,
2624 const DeclRefExpr *E,
2625 const VarDecl *VD,
2626 bool IsConstant) {
2627 // For a variable declared in an enclosing scope, do not emit a spurious
2628 // reference even if we have a capture, as that will emit an unwarranted
2629 // reference to our capture state, and will likely generate worse code than
2630 // emitting a local copy.
2631 if (E->refersToEnclosingVariableOrCapture())
2632 return false;
2633
2634 // For a local declaration declared in this function, we can always reference
2635 // it even if we don't have an odr-use.
2636 if (VD->hasLocalStorage()) {
2637 return VD->getDeclContext() ==
2638 dyn_cast_or_null<DeclContext>(CGF.CurCodeDecl);
2639 }
2640
2641 // For a global declaration, we can emit a reference to it if we know
2642 // for sure that we are able to emit a definition of it.
2643 VD = VD->getDefinition(CGF.getContext());
2644 if (!VD)
2645 return false;
2646
2647 // Don't emit a spurious reference if it might be to a variable that only
2648 // exists on a different device / target.
2649 // FIXME: This is unnecessarily broad. Check whether this would actually be a
2650 // cross-target reference.
2651 if (CGF.getLangOpts().OpenMP || CGF.getLangOpts().CUDA ||
2652 CGF.getLangOpts().OpenCL) {
2653 return false;
2654 }
2655
2656 // We can emit a spurious reference only if the linkage implies that we'll
2657 // be emitting a non-interposable symbol that will be retained until link
2658 // time.
2659 switch (CGF.CGM.getLLVMLinkageVarDefinition(VD, IsConstant)) {
2660 case llvm::GlobalValue::ExternalLinkage:
2661 case llvm::GlobalValue::LinkOnceODRLinkage:
2662 case llvm::GlobalValue::WeakODRLinkage:
2663 case llvm::GlobalValue::InternalLinkage:
2664 case llvm::GlobalValue::PrivateLinkage:
2665 return true;
2666 default:
2667 return false;
2668 }
2669}
2670
2671LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
2672 const NamedDecl *ND = E->getDecl();
2673 QualType T = E->getType();
2674
2675 assert(E->isNonOdrUse() != NOUR_Unevaluated &&(static_cast <bool> (E->isNonOdrUse() != NOUR_Unevaluated
&& "should not emit an unevaluated operand") ? void (
0) : __assert_fail ("E->isNonOdrUse() != NOUR_Unevaluated && \"should not emit an unevaluated operand\""
, "clang/lib/CodeGen/CGExpr.cpp", 2676, __extension__ __PRETTY_FUNCTION__
))
2676 "should not emit an unevaluated operand")(static_cast <bool> (E->isNonOdrUse() != NOUR_Unevaluated
&& "should not emit an unevaluated operand") ? void (
0) : __assert_fail ("E->isNonOdrUse() != NOUR_Unevaluated && \"should not emit an unevaluated operand\""
, "clang/lib/CodeGen/CGExpr.cpp", 2676, __extension__ __PRETTY_FUNCTION__
))
;
2677
2678 if (const auto *VD = dyn_cast<VarDecl>(ND)) {
2679 // Global Named registers access via intrinsics only
2680 if (VD->getStorageClass() == SC_Register &&
2681 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())
2682 return EmitGlobalNamedRegister(VD, CGM);
2683
2684 // If this DeclRefExpr does not constitute an odr-use of the variable,
2685 // we're not permitted to emit a reference to it in general, and it might
2686 // not be captured if capture would be necessary for a use. Emit the
2687 // constant value directly instead.
2688 if (E->isNonOdrUse() == NOUR_Constant &&
2689 (VD->getType()->isReferenceType() ||
2690 !canEmitSpuriousReferenceToVariable(*this, E, VD, true))) {
2691 VD->getAnyInitializer(VD);
2692 llvm::Constant *Val = ConstantEmitter(*this).emitAbstract(
2693 E->getLocation(), *VD->evaluateValue(), VD->getType());
2694 assert(Val && "failed to emit constant expression")(static_cast <bool> (Val && "failed to emit constant expression"
) ? void (0) : __assert_fail ("Val && \"failed to emit constant expression\""
, "clang/lib/CodeGen/CGExpr.cpp", 2694, __extension__ __PRETTY_FUNCTION__
))
;
2695
2696 Address Addr = Address::invalid();
2697 if (!VD->getType()->isReferenceType()) {
2698 // Spill the constant value to a global.
2699 Addr = CGM.createUnnamedGlobalFrom(*VD, Val,
2700 getContext().getDeclAlign(VD));
2701 llvm::Type *VarTy = getTypes().ConvertTypeForMem(VD->getType());
2702 auto *PTy = llvm::PointerType::get(
2703 VarTy, getContext().getTargetAddressSpace(VD->getType()));
2704 if (PTy != Addr.getType())
2705 Addr = Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, PTy);
2706 } else {
2707 // Should we be using the alignment of the constant pointer we emitted?
2708 CharUnits Alignment =
2709 CGM.getNaturalTypeAlignment(E->getType(),
2710 /* BaseInfo= */ nullptr,
2711 /* TBAAInfo= */ nullptr,
2712 /* forPointeeType= */ true);
2713 Addr = Address(Val, ConvertTypeForMem(E->getType()), Alignment);
2714 }
2715 return MakeAddrLValue(Addr, T, AlignmentSource::Decl);
2716 }
2717
2718 // FIXME: Handle other kinds of non-odr-use DeclRefExprs.
2719
2720 // Check for captured variables.
2721 if (E->refersToEnclosingVariableOrCapture()) {
2722 VD = VD->getCanonicalDecl();
2723 if (auto *FD = LambdaCaptureFields.lookup(VD))
2724 return EmitCapturedFieldLValue(*this, FD, CXXABIThisValue);
2725 if (CapturedStmtInfo) {
2726 auto I = LocalDeclMap.find(VD);
2727 if (I != LocalDeclMap.end()) {
2728 LValue CapLVal;
2729 if (VD->getType()->isReferenceType())
2730 CapLVal = EmitLoadOfReferenceLValue(I->second, VD->getType(),
2731 AlignmentSource::Decl);
2732 else
2733 CapLVal = MakeAddrLValue(I->second, T);
2734 // Mark lvalue as nontemporal if the variable is marked as nontemporal
2735 // in simd context.
2736 if (getLangOpts().OpenMP &&
2737 CGM.getOpenMPRuntime().isNontemporalDecl(VD))
2738 CapLVal.setNontemporal(/*Value=*/true);
2739 return CapLVal;
2740 }
2741 LValue CapLVal =
2742 EmitCapturedFieldLValue(*this, CapturedStmtInfo->lookup(VD),
2743 CapturedStmtInfo->getContextValue());
2744 CapLVal = MakeAddrLValue(
2745 Address(CapLVal.getPointer(*this), getContext().getDeclAlign(VD)),
2746 CapLVal.getType(), LValueBaseInfo(AlignmentSource::Decl),
2747 CapLVal.getTBAAInfo());
2748 // Mark lvalue as nontemporal if the variable is marked as nontemporal
2749 // in simd context.
2750 if (getLangOpts().OpenMP &&
2751 CGM.getOpenMPRuntime().isNontemporalDecl(VD))
2752 CapLVal.setNontemporal(/*Value=*/true);
2753 return CapLVal;
2754 }
2755
2756 assert(isa<BlockDecl>(CurCodeDecl))(static_cast <bool> (isa<BlockDecl>(CurCodeDecl))
? void (0) : __assert_fail ("isa<BlockDecl>(CurCodeDecl)"
, "clang/lib/CodeGen/CGExpr.cpp", 2756, __extension__ __PRETTY_FUNCTION__
))
;
2757 Address addr = GetAddrOfBlockDecl(VD);
2758 return MakeAddrLValue(addr, T, AlignmentSource::Decl);
2759 }
2760 }
2761
2762 // FIXME: We should be able to assert this for FunctionDecls as well!
2763 // FIXME: We should be able to assert this for all DeclRefExprs, not just
2764 // those with a valid source location.
2765 assert((ND->isUsed(false) || !isa<VarDecl>(ND) || E->isNonOdrUse() ||(static_cast <bool> ((ND->isUsed(false) || !isa<VarDecl
>(ND) || E->isNonOdrUse() || !E->getLocation().isValid
()) && "Should not use decl without marking it used!"
) ? void (0) : __assert_fail ("(ND->isUsed(false) || !isa<VarDecl>(ND) || E->isNonOdrUse() || !E->getLocation().isValid()) && \"Should not use decl without marking it used!\""
, "clang/lib/CodeGen/CGExpr.cpp", 2767, __extension__ __PRETTY_FUNCTION__
))
2766 !E->getLocation().isValid()) &&(static_cast <bool> ((ND->isUsed(false) || !isa<VarDecl
>(ND) || E->isNonOdrUse() || !E->getLocation().isValid
()) && "Should not use decl without marking it used!"
) ? void (0) : __assert_fail ("(ND->isUsed(false) || !isa<VarDecl>(ND) || E->isNonOdrUse() || !E->getLocation().isValid()) && \"Should not use decl without marking it used!\""
, "clang/lib/CodeGen/CGExpr.cpp", 2767, __extension__ __PRETTY_FUNCTION__
))
2767 "Should not use decl without marking it used!")(static_cast <bool> ((ND->isUsed(false) || !isa<VarDecl
>(ND) || E->isNonOdrUse() || !E->getLocation().isValid
()) && "Should not use decl without marking it used!"
) ? void (0) : __assert_fail ("(ND->isUsed(false) || !isa<VarDecl>(ND) || E->isNonOdrUse() || !E->getLocation().isValid()) && \"Should not use decl without marking it used!\""
, "clang/lib/CodeGen/CGExpr.cpp", 2767, __extension__ __PRETTY_FUNCTION__
))
;
2768
2769 if (ND->hasAttr<WeakRefAttr>()) {
2770 const auto *VD = cast<ValueDecl>(ND);
2771 ConstantAddress Aliasee = CGM.GetWeakRefReference(VD);
2772 return MakeAddrLValue(Aliasee, T, AlignmentSource::Decl);
2773 }
2774
2775 if (const auto *VD = dyn_cast<VarDecl>(ND)) {
2776 // Check if this is a global variable.
2777 if (VD->hasLinkage() || VD->isStaticDataMember())
2778 return EmitGlobalVarDeclLValue(*this, E, VD);
2779
2780 Address addr = Address::invalid();
2781
2782 // The variable should generally be present in the local decl map.
2783 auto iter = LocalDeclMap.find(VD);
2784 if (iter != LocalDeclMap.end()) {
2785 addr = iter->second;
2786
2787 // Otherwise, it might be static local we haven't emitted yet for
2788 // some reason; most likely, because it's in an outer function.
2789 } else if (VD->isStaticLocal()) {
2790 llvm::Constant *var = CGM.getOrCreateStaticVarDecl(
2791 *VD, CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false));
2792 addr = Address(
2793 var, ConvertTypeForMem(VD->getType()), getContext().getDeclAlign(VD));
2794
2795 // No other cases for now.
2796 } else {
2797 llvm_unreachable("DeclRefExpr for Decl not entered in LocalDeclMap?")::llvm::llvm_unreachable_internal("DeclRefExpr for Decl not entered in LocalDeclMap?"
, "clang/lib/CodeGen/CGExpr.cpp", 2797)
;
2798 }
2799
2800
2801 // Check for OpenMP threadprivate variables.
2802 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd &&
2803 VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
2804 return EmitThreadPrivateVarDeclLValue(
2805 *this, VD, T, addr, getTypes().ConvertTypeForMem(VD->getType()),
2806 E->getExprLoc());
2807 }
2808
2809 // Drill into block byref variables.
2810 bool isBlockByref = VD->isEscapingByref();
2811 if (isBlockByref) {
2812 addr = emitBlockByrefAddress(addr, VD);
2813 }
2814
2815 // Drill into reference types.
2816 LValue LV = VD->getType()->isReferenceType() ?
2817 EmitLoadOfReferenceLValue(addr, VD->getType(), AlignmentSource::Decl) :
2818 MakeAddrLValue(addr, T, AlignmentSource::Decl);
2819
2820 bool isLocalStorage = VD->hasLocalStorage();
2821
2822 bool NonGCable = isLocalStorage &&
2823 !VD->getType()->isReferenceType() &&
2824 !isBlockByref;
2825 if (NonGCable) {
2826 LV.getQuals().removeObjCGCAttr();
2827 LV.setNonGC(true);
2828 }
2829
2830 bool isImpreciseLifetime =
2831 (isLocalStorage && !VD->hasAttr<ObjCPreciseLifetimeAttr>());
2832 if (isImpreciseLifetime)
2833 LV.setARCPreciseLifetime(ARCImpreciseLifetime);
2834 setObjCGCLValueClass(getContext(), E, LV);
2835 return LV;
2836 }
2837
2838 if (const auto *FD = dyn_cast<FunctionDecl>(ND)) {
2839 LValue LV = EmitFunctionDeclLValue(*this, E, FD);
2840
2841 // Emit debuginfo for the function declaration if the target wants to.
2842 if (getContext().getTargetInfo().allowDebugInfoForExternalRef()) {
2843 if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) {
2844 auto *Fn =
2845 cast<llvm::Function>(LV.getPointer(*this)->stripPointerCasts());
2846 if (!Fn->getSubprogram())
2847 DI->EmitFunctionDecl(FD, FD->getLocation(), T, Fn);
2848 }
2849 }
2850
2851 return LV;
2852 }
2853
2854 // FIXME: While we're emitting a binding from an enclosing scope, all other
2855 // DeclRefExprs we see should be implicitly treated as if they also refer to
2856 // an enclosing scope.
2857 if (const auto *BD = dyn_cast<BindingDecl>(ND))
2858 return EmitLValue(BD->getBinding());
2859
2860 // We can form DeclRefExprs naming GUID declarations when reconstituting
2861 // non-type template parameters into expressions.
2862 if (const auto *GD = dyn_cast<MSGuidDecl>(ND))
2863 return MakeAddrLValue(CGM.GetAddrOfMSGuidDecl(GD), T,
2864 AlignmentSource::Decl);
2865
2866 if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND))
2867 return MakeAddrLValue(CGM.GetAddrOfTemplateParamObject(TPO), T,
2868 AlignmentSource::Decl);
2869
2870 llvm_unreachable("Unhandled DeclRefExpr")::llvm::llvm_unreachable_internal("Unhandled DeclRefExpr", "clang/lib/CodeGen/CGExpr.cpp"
, 2870)
;
2871}
2872
2873LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
2874 // __extension__ doesn't affect lvalue-ness.
2875 if (E->getOpcode() == UO_Extension)
2876 return EmitLValue(E->getSubExpr());
2877
2878 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
2879 switch (E->getOpcode()) {
2880 default: llvm_unreachable("Unknown unary operator lvalue!")::llvm::llvm_unreachable_internal("Unknown unary operator lvalue!"
, "clang/lib/CodeGen/CGExpr.cpp", 2880)
;
2881 case UO_Deref: {
2882 QualType T = E->getSubExpr()->getType()->getPointeeType();
2883 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type")(static_cast <bool> (!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type"
) ? void (0) : __assert_fail ("!T.isNull() && \"CodeGenFunction::EmitUnaryOpLValue: Illegal type\""
, "clang/lib/CodeGen/CGExpr.cpp", 2883, __extension__ __PRETTY_FUNCTION__
))
;
2884
2885 LValueBaseInfo BaseInfo;
2886 TBAAAccessInfo TBAAInfo;
2887 Address Addr = EmitPointerWithAlignment(E->getSubExpr(), &BaseInfo,
2888 &TBAAInfo);
2889 LValue LV = MakeAddrLValue(Addr, T, BaseInfo, TBAAInfo);
2890 LV.getQuals().setAddressSpace(ExprTy.getAddressSpace());
2891
2892 // We should not generate __weak write barrier on indirect reference
2893 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
2894 // But, we continue to generate __strong write barrier on indirect write
2895 // into a pointer to object.
2896 if (getLangOpts().ObjC &&
2897 getLangOpts().getGC() != LangOptions::NonGC &&
2898 LV.isObjCWeak())
2899 LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
2900 return LV;
2901 }
2902 case UO_Real:
2903 case UO_Imag: {
2904 LValue LV = EmitLValue(E->getSubExpr());
2905 assert(LV.isSimple() && "real/imag on non-ordinary l-value")(static_cast <bool> (LV.isSimple() && "real/imag on non-ordinary l-value"
) ? void (0) : __assert_fail ("LV.isSimple() && \"real/imag on non-ordinary l-value\""
, "clang/lib/CodeGen/CGExpr.cpp", 2905, __extension__ __PRETTY_FUNCTION__
))
;
2906
2907 // __real is valid on scalars. This is a faster way of testing that.
2908 // __imag can only produce an rvalue on scalars.
2909 if (E->getOpcode() == UO_Real &&
2910 !LV.getAddress(*this).getElementType()->isStructTy()) {
2911 assert(E->getSubExpr()->getType()->isArithmeticType())(static_cast <bool> (E->getSubExpr()->getType()->
isArithmeticType()) ? void (0) : __assert_fail ("E->getSubExpr()->getType()->isArithmeticType()"
, "clang/lib/CodeGen/CGExpr.cpp", 2911, __extension__ __PRETTY_FUNCTION__
))
;
2912 return LV;
2913 }
2914
2915 QualType T = ExprTy->castAs<ComplexType>()->getElementType();
2916
2917 Address Component =
2918 (E->getOpcode() == UO_Real
2919 ? emitAddrOfRealComponent(LV.getAddress(*this), LV.getType())
2920 : emitAddrOfImagComponent(LV.getAddress(*this), LV.getType()));
2921 LValue ElemLV = MakeAddrLValue(Component, T, LV.getBaseInfo(),
2922 CGM.getTBAAInfoForSubobject(LV, T));
2923 ElemLV.getQuals().addQualifiers(LV.getQuals());
2924 return ElemLV;
2925 }
2926 case UO_PreInc:
2927 case UO_PreDec: {
2928 LValue LV = EmitLValue(E->getSubExpr());
2929 bool isInc = E->getOpcode() == UO_PreInc;
2930
2931 if (E->getType()->isAnyComplexType())
2932 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
2933 else
2934 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
2935 return LV;
2936 }
2937 }
2938}
2939
2940LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
2941 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E),
2942 E->getType(), AlignmentSource::Decl);
2943}
2944
2945LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
2946 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E),
2947 E->getType(), AlignmentSource::Decl);
2948}
2949
2950LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
2951 auto SL = E->getFunctionName();
2952 assert(SL != nullptr && "No StringLiteral name in PredefinedExpr")(static_cast <bool> (SL != nullptr && "No StringLiteral name in PredefinedExpr"
) ? void (0) : __assert_fail ("SL != nullptr && \"No StringLiteral name in PredefinedExpr\""
, "clang/lib/CodeGen/CGExpr.cpp", 2952, __extension__ __PRETTY_FUNCTION__
))
;
2953 StringRef FnName = CurFn->getName();
2954 if (FnName.startswith("\01"))
2955 FnName = FnName.substr(1);
2956 StringRef NameItems[] = {
2957 PredefinedExpr::getIdentKindName(E->getIdentKind()), FnName};
2958 std::string GVName = llvm::join(NameItems, NameItems + 2, ".");
2959 if (auto *BD = dyn_cast_or_null<BlockDecl>(CurCodeDecl)) {
2960 std::string Name = std::string(SL->getString());
2961 if (!Name.empty()) {
2962 unsigned Discriminator =
2963 CGM.getCXXABI().getMangleContext().getBlockId(BD, true);
2964 if (Discriminator)
2965 Name += "_" + Twine(Discriminator + 1).str();
2966 auto C = CGM.GetAddrOfConstantCString(Name, GVName.c_str());
2967 return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl);
2968 } else {
2969 auto C =
2970 CGM.GetAddrOfConstantCString(std::string(FnName), GVName.c_str());
2971 return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl);
2972 }
2973 }
2974 auto C = CGM.GetAddrOfConstantStringFromLiteral(SL, GVName);
2975 return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl);
2976}
2977
2978/// Emit a type description suitable for use by a runtime sanitizer library. The
2979/// format of a type descriptor is
2980///
2981/// \code
2982/// { i16 TypeKind, i16 TypeInfo }
2983/// \endcode
2984///
2985/// followed by an array of i8 containing the type name. TypeKind is 0 for an
2986/// integer, 1 for a floating point value, and -1 for anything else.
2987llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
2988 // Only emit each type's descriptor once.
2989 if (llvm::Constant *C = CGM.getTypeDescriptorFromMap(T))
2990 return C;
2991
2992 uint16_t TypeKind = -1;
2993 uint16_t TypeInfo = 0;
2994
2995 if (T->isIntegerType()) {
2996 TypeKind = 0;
2997 TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) |
2998 (T->isSignedIntegerType() ? 1 : 0);
2999 } else if (T->isFloatingType()) {
3000 TypeKind = 1;
3001 TypeInfo = getContext().getTypeSize(T);
3002 }
3003
3004 // Format the type name as if for a diagnostic, including quotes and
3005 // optionally an 'aka'.
3006 SmallString<32> Buffer;
3007 CGM.getDiags().ConvertArgToString(DiagnosticsEngine::ak_qualtype,
3008 (intptr_t)T.getAsOpaquePtr(),
3009 StringRef(), StringRef(), None, Buffer,
3010 None);
3011
3012 llvm::Constant *Components[] = {
3013 Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo),
3014 llvm::ConstantDataArray::getString(getLLVMContext(), Buffer)
3015 };
3016 llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components);
3017
3018 auto *GV = new llvm::GlobalVariable(
3019 CGM.getModule(), Descriptor->getType(),
3020 /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, Descriptor);
3021 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3022 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(GV);
3023
3024 // Remember the descriptor for this type.
3025 CGM.setTypeDescriptorInMap(T, GV);
3026
3027 return GV;
3028}
3029
3030llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) {
3031 llvm::Type *TargetTy = IntPtrTy;
3032
3033 if (V->getType() == TargetTy)
3034 return V;
3035
3036 // Floating-point types which fit into intptr_t are bitcast to integers
3037 // and then passed directly (after zero-extension, if necessary).
3038 if (V->getType()->isFloatingPointTy()) {
3039 unsigned Bits = V->getType()->getPrimitiveSizeInBits().getFixedSize();
3040 if (Bits <= TargetTy->getIntegerBitWidth())
3041 V = Builder.CreateBitCast(V, llvm::Type::getIntNTy(getLLVMContext(),
3042 Bits));
3043 }
3044
3045 // Integers which fit in intptr_t are zero-extended and passed directly.
3046 if (V->getType()->isIntegerTy() &&
3047 V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth())
3048 return Builder.CreateZExt(V, TargetTy);
3049
3050 // Pointers are passed directly, everything else is passed by address.
3051 if (!V->getType()->isPointerTy()) {
3052 Address Ptr = CreateDefaultAlignTempAlloca(V->getType());
3053 Builder.CreateStore(V, Ptr);
3054 V = Ptr.getPointer();
3055 }
3056 return Builder.CreatePtrToInt(V, TargetTy);
3057}
3058
3059/// Emit a representation of a SourceLocation for passing to a handler
3060/// in a sanitizer runtime library. The format for this data is:
3061/// \code
3062/// struct SourceLocation {
3063/// const char *Filename;
3064/// int32_t Line, Column;
3065/// };
3066/// \endcode
3067/// For an invalid SourceLocation, the Filename pointer is null.
3068llvm::Constant *CodeGenFunction::EmitCheckSourceLocation(SourceLocation Loc) {
3069 llvm::Constant *Filename;
3070 int Line, Column;
3071
3072 PresumedLoc PLoc = getContext().getSourceManager().getPresumedLoc(Loc);
3073 if (PLoc.isValid()) {
3074 StringRef FilenameString = PLoc.getFilename();
3075
3076 int PathComponentsToStrip =
3077 CGM.getCodeGenOpts().EmitCheckPathComponentsToStrip;
3078 if (PathComponentsToStrip < 0) {
3079 assert(PathComponentsToStrip != INT_MIN)(static_cast <bool> (PathComponentsToStrip != (-2147483647
-1)) ? void (0) : __assert_fail ("PathComponentsToStrip != INT_MIN"
, "clang/lib/CodeGen/CGExpr.cpp", 3079, __extension__ __PRETTY_FUNCTION__
))
;
3080 int PathComponentsToKeep = -PathComponentsToStrip;
3081 auto I = llvm::sys::path::rbegin(FilenameString);
3082 auto E = llvm::sys::path::rend(FilenameString);
3083 while (I != E && --PathComponentsToKeep)
3084 ++I;
3085
3086 FilenameString = FilenameString.substr(I - E);
3087 } else if (PathComponentsToStrip > 0) {
3088 auto I = llvm::sys::path::begin(FilenameString);
3089 auto E = llvm::sys::path::end(FilenameString);
3090 while (I != E && PathComponentsToStrip--)
3091 ++I;
3092
3093 if (I != E)
3094 FilenameString =
3095 FilenameString.substr(I - llvm::sys::path::begin(FilenameString));
3096 else
3097 FilenameString = llvm::sys::path::filename(FilenameString);
3098 }
3099
3100 auto FilenameGV =
3101 CGM.GetAddrOfConstantCString(std::string(FilenameString), ".src");
3102 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(
3103 cast<llvm::GlobalVariable>(FilenameGV.getPointer()));
3104 Filename = FilenameGV.getPointer();
3105 Line = PLoc.getLine();
3106 Column = PLoc.getColumn();
3107 } else {
3108 Filename = llvm::Constant::getNullValue(Int8PtrTy);
3109 Line = Column = 0;
3110 }
3111
3112 llvm::Constant *Data[] = {Filename, Builder.getInt32(Line),
3113 Builder.getInt32(Column)};
3114
3115 return llvm::ConstantStruct::getAnon(Data);
3116}
3117
3118namespace {
3119/// Specify under what conditions this check can be recovered
3120enum class CheckRecoverableKind {
3121 /// Always terminate program execution if this check fails.
3122 Unrecoverable,
3123 /// Check supports recovering, runtime has both fatal (noreturn) and
3124 /// non-fatal handlers for this check.
3125 Recoverable,
3126 /// Runtime conditionally aborts, always need to support recovery.
3127 AlwaysRecoverable
3128};
3129}
3130
3131static CheckRecoverableKind getRecoverableKind(SanitizerMask Kind) {
3132 assert(Kind.countPopulation() == 1)(static_cast <bool> (Kind.countPopulation() == 1) ? void
(0) : __assert_fail ("Kind.countPopulation() == 1", "clang/lib/CodeGen/CGExpr.cpp"
, 3132, __extension__ __PRETTY_FUNCTION__))
;
3133 if (Kind == SanitizerKind::Function || Kind == SanitizerKind::Vptr)
3134 return CheckRecoverableKind::AlwaysRecoverable;
3135 else if (Kind == SanitizerKind::Return || Kind == SanitizerKind::Unreachable)
3136 return CheckRecoverableKind::Unrecoverable;
3137 else
3138 return CheckRecoverableKind::Recoverable;
3139}
3140
3141namespace {
3142struct SanitizerHandlerInfo {
3143 char const *const Name;
3144 unsigned Version;
3145};
3146}
3147
3148const SanitizerHandlerInfo SanitizerHandlers[] = {
3149#define SANITIZER_CHECK(Enum, Name, Version) {#Name, Version},
3150 LIST_SANITIZER_CHECKSSANITIZER_CHECK(AddOverflow, add_overflow, 0) SANITIZER_CHECK
(BuiltinUnreachable, builtin_unreachable, 0) SANITIZER_CHECK(
CFICheckFail, cfi_check_fail, 0) SANITIZER_CHECK(DivremOverflow
, divrem_overflow, 0) SANITIZER_CHECK(DynamicTypeCacheMiss, dynamic_type_cache_miss
, 0) SANITIZER_CHECK(FloatCastOverflow, float_cast_overflow, 0
) SANITIZER_CHECK(FunctionTypeMismatch, function_type_mismatch
, 1) SANITIZER_CHECK(ImplicitConversion, implicit_conversion,
0) SANITIZER_CHECK(InvalidBuiltin, invalid_builtin, 0) SANITIZER_CHECK
(InvalidObjCCast, invalid_objc_cast, 0) SANITIZER_CHECK(LoadInvalidValue
, load_invalid_value, 0) SANITIZER_CHECK(MissingReturn, missing_return
, 0) SANITIZER_CHECK(MulOverflow, mul_overflow, 0) SANITIZER_CHECK
(NegateOverflow, negate_overflow, 0) SANITIZER_CHECK(NullabilityArg
, nullability_arg, 0) SANITIZER_CHECK(NullabilityReturn, nullability_return
, 1) SANITIZER_CHECK(NonnullArg, nonnull_arg, 0) SANITIZER_CHECK
(NonnullReturn, nonnull_return, 1) SANITIZER_CHECK(OutOfBounds
, out_of_bounds, 0) SANITIZER_CHECK(PointerOverflow, pointer_overflow
, 0) SANITIZER_CHECK(ShiftOutOfBounds, shift_out_of_bounds, 0
) SANITIZER_CHECK(SubOverflow, sub_overflow, 0) SANITIZER_CHECK
(TypeMismatch, type_mismatch, 1) SANITIZER_CHECK(AlignmentAssumption
, alignment_assumption, 0) SANITIZER_CHECK(VLABoundNotPositive
, vla_bound_not_positive, 0)
3151#undef SANITIZER_CHECK
3152};
3153
3154static void emitCheckHandlerCall(CodeGenFunction &CGF,
3155 llvm::FunctionType *FnType,
3156 ArrayRef<llvm::Value *> FnArgs,
3157 SanitizerHandler CheckHandler,
3158 CheckRecoverableKind RecoverKind, bool IsFatal,
3159 llvm::BasicBlock *ContBB) {
3160 assert(IsFatal || RecoverKind != CheckRecoverableKind::Unrecoverable)(static_cast <bool> (IsFatal || RecoverKind != CheckRecoverableKind
::Unrecoverable) ? void (0) : __assert_fail ("IsFatal || RecoverKind != CheckRecoverableKind::Unrecoverable"
, "clang/lib/CodeGen/CGExpr.cpp", 3160, __extension__ __PRETTY_FUNCTION__
))
;
3161 Optional<ApplyDebugLocation> DL;
3162 if (!CGF.Builder.getCurrentDebugLocation()) {
3163 // Ensure that the call has at least an artificial debug location.
3164 DL.emplace(CGF, SourceLocation());
3165 }
3166 bool NeedsAbortSuffix =
3167 IsFatal && RecoverKind != CheckRecoverableKind::Unrecoverable;
3168 bool MinimalRuntime = CGF.CGM.getCodeGenOpts().SanitizeMinimalRuntime;
3169 const SanitizerHandlerInfo &CheckInfo = SanitizerHandlers[CheckHandler];
3170 const StringRef CheckName = CheckInfo.Name;
3171 std::string FnName = "__ubsan_handle_" + CheckName.str();
3172 if (CheckInfo.Version && !MinimalRuntime)
3173 FnName += "_v" + llvm::utostr(CheckInfo.Version);
3174 if (MinimalRuntime)
3175 FnName += "_minimal";
3176 if (NeedsAbortSuffix)
3177 FnName += "_abort";
3178 bool MayReturn =
3179 !IsFatal || RecoverKind == CheckRecoverableKind::AlwaysRecoverable;
3180
3181 llvm::AttrBuilder B;
3182 if (!MayReturn) {
3183 B.addAttribute(llvm::Attribute::NoReturn)
3184 .addAttribute(llvm::Attribute::NoUnwind);
3185 }
3186 B.addAttribute(llvm::Attribute::UWTable);
3187
3188 llvm::FunctionCallee Fn = CGF.CGM.CreateRuntimeFunction(
3189 FnType, FnName,
3190 llvm::AttributeList::get(CGF.getLLVMContext(),
3191 llvm::AttributeList::FunctionIndex, B),
3192 /*Local=*/true);
3193 llvm::CallInst *HandlerCall = CGF.EmitNounwindRuntimeCall(Fn, FnArgs);
3194 if (!MayReturn) {
3195 HandlerCall->setDoesNotReturn();
3196 CGF.Builder.CreateUnreachable();
3197 } else {
3198 CGF.Builder.CreateBr(ContBB);
3199 }
3200}
3201
3202void CodeGenFunction::EmitCheck(
3203 ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked,
3204 SanitizerHandler CheckHandler, ArrayRef<llvm::Constant *> StaticArgs,
3205 ArrayRef<llvm::Value *> DynamicArgs) {
3206 assert(IsSanitizerScope)(static_cast <bool> (IsSanitizerScope) ? void (0) : __assert_fail
("IsSanitizerScope", "clang/lib/CodeGen/CGExpr.cpp", 3206, __extension__
__PRETTY_FUNCTION__))
;
3207 assert(Checked.size() > 0)(static_cast <bool> (Checked.size() > 0) ? void (0) :
__assert_fail ("Checked.size() > 0", "clang/lib/CodeGen/CGExpr.cpp"
, 3207, __extension__ __PRETTY_FUNCTION__))
;
3208 assert(CheckHandler >= 0 &&(static_cast <bool> (CheckHandler >= 0 && size_t
(CheckHandler) < llvm::array_lengthof(SanitizerHandlers)) ?
void (0) : __assert_fail ("CheckHandler >= 0 && size_t(CheckHandler) < llvm::array_lengthof(SanitizerHandlers)"
, "clang/lib/CodeGen/CGExpr.cpp", 3209, __extension__ __PRETTY_FUNCTION__
))
3209 size_t(CheckHandler) < llvm::array_lengthof(SanitizerHandlers))(static_cast <bool> (CheckHandler >= 0 && size_t
(CheckHandler) < llvm::array_lengthof(SanitizerHandlers)) ?
void (0) : __assert_fail ("CheckHandler >= 0 && size_t(CheckHandler) < llvm::array_lengthof(SanitizerHandlers)"
, "clang/lib/CodeGen/CGExpr.cpp", 3209, __extension__ __PRETTY_FUNCTION__
))
;
3210 const StringRef CheckName = SanitizerHandlers[CheckHandler].Name;
3211
3212 llvm::Value *FatalCond = nullptr;
3213 llvm::Value *RecoverableCond = nullptr;
3214 llvm::Value *TrapCond = nullptr;
3215 for (int i = 0, n = Checked.size(); i < n; ++i) {
3216 llvm::Value *Check = Checked[i].first;
3217 // -fsanitize-trap= overrides -fsanitize-recover=.
3218 llvm::Value *&Cond =
3219 CGM.getCodeGenOpts().SanitizeTrap.has(Checked[i].second)
3220 ? TrapCond
3221 : CGM.getCodeGenOpts().SanitizeRecover.has(Checked[i].second)
3222 ? RecoverableCond
3223 : FatalCond;
3224 Cond = Cond ? Builder.CreateAnd(Cond, Check) : Check;
3225 }
3226
3227 if (TrapCond)
3228 EmitTrapCheck(TrapCond, CheckHandler);
3229 if (!FatalCond && !RecoverableCond)
3230 return;
3231
3232 llvm::Value *JointCond;
3233 if (FatalCond && RecoverableCond)
3234 JointCond = Builder.CreateAnd(FatalCond, RecoverableCond);
3235 else
3236 JointCond = FatalCond ? FatalCond : RecoverableCond;
3237 assert(JointCond)(static_cast <bool> (JointCond) ? void (0) : __assert_fail
("JointCond", "clang/lib/CodeGen/CGExpr.cpp", 3237, __extension__
__PRETTY_FUNCTION__))
;
3238
3239 CheckRecoverableKind RecoverKind = getRecoverableKind(Checked[0].second);
3240 assert(SanOpts.has(Checked[0].second))(static_cast <bool> (SanOpts.has(Checked[0].second)) ? void
(0) : __assert_fail ("SanOpts.has(Checked[0].second)", "clang/lib/CodeGen/CGExpr.cpp"
, 3240, __extension__ __PRETTY_FUNCTION__))
;
3241#ifndef NDEBUG
3242 for (int i = 1, n = Checked.size(); i < n; ++i) {
3243 assert(RecoverKind == getRecoverableKind(Checked[i].second) &&(static_cast <bool> (RecoverKind == getRecoverableKind(
Checked[i].second) && "All recoverable kinds in a single check must be same!"
) ? void (0) : __assert_fail ("RecoverKind == getRecoverableKind(Checked[i].second) && \"All recoverable kinds in a single check must be same!\""
, "clang/lib/CodeGen/CGExpr.cpp", 3244, __extension__ __PRETTY_FUNCTION__
))
3244 "All recoverable kinds in a single check must be same!")(static_cast <bool> (RecoverKind == getRecoverableKind(
Checked[i].second) && "All recoverable kinds in a single check must be same!"
) ? void (0) : __assert_fail ("RecoverKind == getRecoverableKind(Checked[i].second) && \"All recoverable kinds in a single check must be same!\""
, "clang/lib/CodeGen/CGExpr.cpp", 3244, __extension__ __PRETTY_FUNCTION__
))
;
3245 assert(SanOpts.has(Checked[i].second))(static_cast <bool> (SanOpts.has(Checked[i].second)) ? void
(0) : __assert_fail ("SanOpts.has(Checked[i].second)", "clang/lib/CodeGen/CGExpr.cpp"
, 3245, __extension__ __PRETTY_FUNCTION__))
;
3246 }
3247#endif
3248
3249 llvm::BasicBlock *Cont = createBasicBlock("cont");
3250 llvm::BasicBlock *Handlers = createBasicBlock("handler." + CheckName);
3251 llvm::Instruction *Branch = Builder.CreateCondBr(JointCond, Cont, Handlers);
3252 // Give hint that we very much don't expect to execute the handler
3253 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
3254 llvm::MDBuilder MDHelper(getLLVMContext());
3255 llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1);
3256 Branch->setMetadata(llvm::LLVMContext::MD_prof, Node);
3257 EmitBlock(Handlers);
3258
3259 // Handler functions take an i8* pointing to the (handler-specific) static
3260 // information block, followed by a sequence of intptr_t arguments
3261 // representing operand values.
3262 SmallVector<llvm::Value *, 4> Args;
3263 SmallVector<llvm::Type *, 4> ArgTypes;
3264 if (!CGM.getCodeGenOpts().SanitizeMinimalRuntime) {
3265 Args.reserve(DynamicArgs.size() + 1);
3266 ArgTypes.reserve(DynamicArgs.size() + 1);
3267
3268 // Emit handler arguments and create handler function type.
3269 if (!StaticArgs.empty()) {
3270 llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
3271 auto *InfoPtr =
3272 new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false,
3273 llvm::GlobalVariable::PrivateLinkage, Info);
3274 InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3275 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr);
3276 Args.push_back(Builder.CreateBitCast(InfoPtr, Int8PtrTy));
3277 ArgTypes.push_back(Int8PtrTy);
3278 }
3279
3280 for (size_t i = 0, n = DynamicArgs.size(); i != n; ++i) {
3281 Args.push_back(EmitCheckValue(DynamicArgs[i]));
3282 ArgTypes.push_back(IntPtrTy);
3283 }
3284 }
3285
3286 llvm::FunctionType *FnType =
3287 llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false);
3288
3289 if (!FatalCond || !RecoverableCond) {
3290 // Simple case: we need to generate a single handler call, either
3291 // fatal, or non-fatal.
3292 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind,
3293 (FatalCond != nullptr), Cont);
3294 } else {
3295 // Emit two handler calls: first one for set of unrecoverable checks,
3296 // another one for recoverable.
3297 llvm::BasicBlock *NonFatalHandlerBB =
3298 createBasicBlock("non_fatal." + CheckName);
3299 llvm::BasicBlock *FatalHandlerBB = createBasicBlock("fatal." + CheckName);
3300 Builder.CreateCondBr(FatalCond, NonFatalHandlerBB, FatalHandlerBB);
3301 EmitBlock(FatalHandlerBB);
3302 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, true,
3303 NonFatalHandlerBB);
3304 EmitBlock(NonFatalHandlerBB);
3305 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, false,
3306 Cont);
3307 }
3308
3309 EmitBlock(Cont);
3310}
3311
3312void CodeGenFunction::EmitCfiSlowPathCheck(
3313 SanitizerMask Kind, llvm::Value *Cond, llvm::ConstantInt *TypeId,
3314 llvm::Value *Ptr, ArrayRef<llvm::Constant *> StaticArgs) {
3315 llvm::BasicBlock *Cont = createBasicBlock("cfi.cont");
3316
3317 llvm::BasicBlock *CheckBB = createBasicBlock("cfi.slowpath");
3318 llvm::BranchInst *BI = Builder.CreateCondBr(Cond, Cont, CheckBB);
3319
3320 llvm::MDBuilder MDHelper(getLLVMContext());
3321 llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1);
3322 BI->setMetadata(llvm::LLVMContext::MD_prof, Node);
3323
3324 EmitBlock(CheckBB);
3325
3326 bool WithDiag = !CGM.getCodeGenOpts().SanitizeTrap.has(Kind);
3327
3328 llvm::CallInst *CheckCall;
3329 llvm::FunctionCallee SlowPathFn;
3330 if (WithDiag) {
3331 llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
3332 auto *InfoPtr =
3333 new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false,
3334 llvm::GlobalVariable::PrivateLinkage, Info);
3335 InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3336 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr);
3337
3338 SlowPathFn = CGM.getModule().getOrInsertFunction(
3339 "__cfi_slowpath_diag",
3340 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy},
3341 false));
3342 CheckCall = Builder.CreateCall(
3343 SlowPathFn, {TypeId, Ptr, Builder.CreateBitCast(InfoPtr, Int8PtrTy)});
3344 } else {
3345 SlowPathFn = CGM.getModule().getOrInsertFunction(
3346 "__cfi_slowpath",
3347 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy}, false));
3348 CheckCall = Builder.CreateCall(SlowPathFn, {TypeId, Ptr});
3349 }
3350
3351 CGM.setDSOLocal(
3352 cast<llvm::GlobalValue>(SlowPathFn.getCallee()->stripPointerCasts()));
3353 CheckCall->setDoesNotThrow();
3354
3355 EmitBlock(Cont);
3356}
3357
3358// Emit a stub for __cfi_check function so that the linker knows about this
3359// symbol in LTO mode.
3360void CodeGenFunction::EmitCfiCheckStub() {
3361 llvm::Module *M = &CGM.getModule();
3362 auto &Ctx = M->getContext();
3363 llvm::Function *F = llvm::Function::Create(
3364 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy}, false),
3365 llvm::GlobalValue::WeakAnyLinkage, "__cfi_check", M);
3366 CGM.setDSOLocal(F);
3367 llvm::BasicBlock *BB = llvm::BasicBlock::Create(Ctx, "entry", F);
3368 // FIXME: consider emitting an intrinsic call like
3369 // call void @llvm.cfi_check(i64 %0, i8* %1, i8* %2)
3370 // which can be lowered in CrossDSOCFI pass to the actual contents of
3371 // __cfi_check. This would allow inlining of __cfi_check calls.
3372 llvm::CallInst::Create(
3373 llvm::Intrinsic::getDeclaration(M, llvm::Intrinsic::trap), "", BB);
3374 llvm::ReturnInst::Create(Ctx, nullptr, BB);
3375}
3376
3377// This function is basically a switch over the CFI failure kind, which is
3378// extracted from CFICheckFailData (1st function argument). Each case is either
3379// llvm.trap or a call to one of the two runtime handlers, based on
3380// -fsanitize-trap and -fsanitize-recover settings. Default case (invalid
3381// failure kind) traps, but this should really never happen. CFICheckFailData
3382// can be nullptr if the calling module has -fsanitize-trap behavior for this
3383// check kind; in this case __cfi_check_fail traps as well.
3384void CodeGenFunction::EmitCfiCheckFail() {
3385 SanitizerScope SanScope(this);
3386 FunctionArgList Args;
3387 ImplicitParamDecl ArgData(getContext(), getContext().VoidPtrTy,
3388 ImplicitParamDecl::Other);
3389 ImplicitParamDecl ArgAddr(getContext(), getContext().VoidPtrTy,
3390 ImplicitParamDecl::Other);
3391 Args.push_back(&ArgData);
3392 Args.push_back(&ArgAddr);
3393
3394 const CGFunctionInfo &FI =
3395 CGM.getTypes().arrangeBuiltinFunctionDeclaration(getContext().VoidTy, Args);
3396
3397 llvm::Function *F = llvm::Function::Create(
3398 llvm::FunctionType::get(VoidTy, {VoidPtrTy, VoidPtrTy}, false),
3399 llvm::GlobalValue::WeakODRLinkage, "__cfi_check_fail", &CGM.getModule());
3400
3401 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, F, /*IsThunk=*/false);
3402 CGM.SetLLVMFunctionAttributesForDefinition(nullptr, F);
3403 F->setVisibility(llvm::GlobalValue::HiddenVisibility);
3404
3405 StartFunction(GlobalDecl(), CGM.getContext().VoidTy, F, FI, Args,
3406 SourceLocation());
3407
3408 // This function is not affected by NoSanitizeList. This function does
3409 // not have a source location, but "src:*" would still apply. Revert any
3410 // changes to SanOpts made in StartFunction.
3411 SanOpts = CGM.getLangOpts().Sanitize;
3412
3413 llvm::Value *Data =
3414 EmitLoadOfScalar(GetAddrOfLocalVar(&ArgData), /*Volatile=*/false,
3415 CGM.getContext().VoidPtrTy, ArgData.getLocation());
3416 llvm::Value *Addr =
3417 EmitLoadOfScalar(GetAddrOfLocalVar(&ArgAddr), /*Volatile=*/false,
3418 CGM.getContext().VoidPtrTy, ArgAddr.getLocation());
3419
3420 // Data == nullptr means the calling module has trap behaviour for this check.
3421 llvm::Value *DataIsNotNullPtr =
3422 Builder.CreateICmpNE(Data, llvm::ConstantPointerNull::get(Int8PtrTy));
3423 EmitTrapCheck(DataIsNotNullPtr, SanitizerHandler::CFICheckFail);
3424
3425 llvm::StructType *SourceLocationTy =
3426 llvm::StructType::get(VoidPtrTy, Int32Ty, Int32Ty);
3427 llvm::StructType *CfiCheckFailDataTy =
3428 llvm::StructType::get(Int8Ty, SourceLocationTy, VoidPtrTy);
3429
3430 llvm::Value *V = Builder.CreateConstGEP2_32(
3431 CfiCheckFailDataTy,
3432 Builder.CreatePointerCast(Data, CfiCheckFailDataTy->getPointerTo(0)), 0,
3433 0);
3434 Address CheckKindAddr(V, getIntAlign());
3435 llvm::Value *CheckKind = Builder.CreateLoad(CheckKindAddr);
3436
3437 llvm::Value *AllVtables = llvm::MetadataAsValue::get(
3438 CGM.getLLVMContext(),
3439 llvm::MDString::get(CGM.getLLVMContext(), "all-vtables"));
3440 llvm::Value *ValidVtable = Builder.CreateZExt(
3441 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test),
3442 {Addr, AllVtables}),
3443 IntPtrTy);
3444
3445 const std::pair<int, SanitizerMask> CheckKinds[] = {
3446 {CFITCK_VCall, SanitizerKind::CFIVCall},
3447 {CFITCK_NVCall, SanitizerKind::CFINVCall},
3448 {CFITCK_DerivedCast, SanitizerKind::CFIDerivedCast},
3449 {CFITCK_UnrelatedCast, SanitizerKind::CFIUnrelatedCast},
3450 {CFITCK_ICall, SanitizerKind::CFIICall}};
3451
3452 SmallVector<std::pair<llvm::Value *, SanitizerMask>, 5> Checks;
3453 for (auto CheckKindMaskPair : CheckKinds) {
3454 int Kind = CheckKindMaskPair.first;
3455 SanitizerMask Mask = CheckKindMaskPair.second;
3456 llvm::Value *Cond =
3457 Builder.CreateICmpNE(CheckKind, llvm::ConstantInt::get(Int8Ty, Kind));
3458 if (CGM.getLangOpts().Sanitize.has(Mask))
3459 EmitCheck(std::make_pair(Cond, Mask), SanitizerHandler::CFICheckFail, {},
3460 {Data, Addr, ValidVtable});
3461 else
3462 EmitTrapCheck(Cond, SanitizerHandler::CFICheckFail);
3463 }
3464
3465 FinishFunction();
3466 // The only reference to this function will be created during LTO link.
3467 // Make sure it survives until then.
3468 CGM.addUsedGlobal(F);
3469}
3470
3471void CodeGenFunction::EmitUnreachable(SourceLocation Loc) {
3472 if (SanOpts.has(SanitizerKind::Unreachable)) {
3473 SanitizerScope SanScope(this);
3474 EmitCheck(std::make_pair(static_cast<llvm::Value *>(Builder.getFalse()),
3475 SanitizerKind::Unreachable),
3476 SanitizerHandler::BuiltinUnreachable,
3477 EmitCheckSourceLocation(Loc), None);
3478 }
3479 Builder.CreateUnreachable();
3480}
3481
3482void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
3483 SanitizerHandler CheckHandlerID) {
3484 llvm::BasicBlock *Cont = createBasicBlock("cont");
3485
3486 // If we're optimizing, collapse all calls to trap down to just one per
3487 // check-type per function to save on code size.
3488 if (TrapBBs.size() <= CheckHandlerID)
3489 TrapBBs.resize(CheckHandlerID + 1);
3490 llvm::BasicBlock *&TrapBB = TrapBBs[CheckHandlerID];
3491
3492 if (!CGM.getCodeGenOpts().OptimizationLevel || !TrapBB) {
3493 TrapBB = createBasicBlock("trap");
3494 Builder.CreateCondBr(Checked, Cont, TrapBB);
3495 EmitBlock(TrapBB);
3496
3497 llvm::CallInst *TrapCall =
3498 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::ubsantrap),
3499 llvm::ConstantInt::get(CGM.Int8Ty, CheckHandlerID));
3500
3501 if (!CGM.getCodeGenOpts().TrapFuncName.empty()) {
3502 auto A = llvm::Attribute::get(getLLVMContext(), "trap-func-name",
3503 CGM.getCodeGenOpts().TrapFuncName);
3504 TrapCall->addFnAttr(A);
3505 }
3506 TrapCall->setDoesNotReturn();
3507 TrapCall->setDoesNotThrow();
3508 Builder.CreateUnreachable();
3509 } else {
3510 auto Call = TrapBB->begin();
3511 assert(isa<llvm::CallInst>(Call) && "Expected call in trap BB")(static_cast <bool> (isa<llvm::CallInst>(Call) &&
"Expected call in trap BB") ? void (0) : __assert_fail ("isa<llvm::CallInst>(Call) && \"Expected call in trap BB\""
, "clang/lib/CodeGen/CGExpr.cpp", 3511, __extension__ __PRETTY_FUNCTION__
))
;
3512
3513 Call->applyMergedLocation(Call->getDebugLoc(),
3514 Builder.getCurrentDebugLocation());
3515 Builder.CreateCondBr(Checked, Cont, TrapBB);
3516 }
3517
3518 EmitBlock(Cont);
3519}
3520
3521llvm::CallInst *CodeGenFunction::EmitTrapCall(llvm::Intrinsic::ID IntrID) {
3522 llvm::CallInst *TrapCall =
3523 Builder.CreateCall(CGM.getIntrinsic(IntrID));
3524
3525 if (!CGM.getCodeGenOpts().TrapFuncName.empty()) {
3526 auto A = llvm::Attribute::get(getLLVMContext(), "trap-func-name",
3527 CGM.getCodeGenOpts().TrapFuncName);
3528 TrapCall->addFnAttr(A);
3529 }
3530
3531 return TrapCall;
3532}
3533
3534Address CodeGenFunction::EmitArrayToPointerDecay(const Expr *E,
3535 LValueBaseInfo *BaseInfo,
3536 TBAAAccessInfo *TBAAInfo) {
3537 assert(E->getType()->isArrayType() &&(static_cast <bool> (E->getType()->isArrayType() &&
"Array to pointer decay must have array source type!") ? void
(0) : __assert_fail ("E->getType()->isArrayType() && \"Array to pointer decay must have array source type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 3538, __extension__ __PRETTY_FUNCTION__
))
3538 "Array to pointer decay must have array source type!")(static_cast <bool> (E->getType()->isArrayType() &&
"Array to pointer decay must have array source type!") ? void
(0) : __assert_fail ("E->getType()->isArrayType() && \"Array to pointer decay must have array source type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 3538, __extension__ __PRETTY_FUNCTION__
))
;
3539
3540 // Expressions of array type can't be bitfields or vector elements.
3541 LValue LV = EmitLValue(E);
3542 Address Addr = LV.getAddress(*this);
3543
3544 // If the array type was an incomplete type, we need to make sure
3545 // the decay ends up being the right type.
3546 llvm::Type *NewTy = ConvertType(E->getType());
3547 Addr = Builder.CreateElementBitCast(Addr, NewTy);
3548
3549 // Note that VLA pointers are always decayed, so we don't need to do
3550 // anything here.
3551 if (!E->getType()->isVariableArrayType()) {
3552 assert(isa<llvm::ArrayType>(Addr.getElementType()) &&(static_cast <bool> (isa<llvm::ArrayType>(Addr.getElementType
()) && "Expected pointer to array") ? void (0) : __assert_fail
("isa<llvm::ArrayType>(Addr.getElementType()) && \"Expected pointer to array\""
, "clang/lib/CodeGen/CGExpr.cpp", 3553, __extension__ __PRETTY_FUNCTION__
))
3553 "Expected pointer to array")(static_cast <bool> (isa<llvm::ArrayType>(Addr.getElementType
()) && "Expected pointer to array") ? void (0) : __assert_fail
("isa<llvm::ArrayType>(Addr.getElementType()) && \"Expected pointer to array\""
, "clang/lib/CodeGen/CGExpr.cpp", 3553, __extension__ __PRETTY_FUNCTION__
))
;
3554 Addr = Builder.CreateConstArrayGEP(Addr, 0, "arraydecay");
3555 }
3556
3557 // The result of this decay conversion points to an array element within the
3558 // base lvalue. However, since TBAA currently does not support representing
3559 // accesses to elements of member arrays, we conservatively represent accesses
3560 // to the pointee object as if it had no any base lvalue specified.
3561 // TODO: Support TBAA for member arrays.
3562 QualType EltType = E->getType()->castAsArrayTypeUnsafe()->getElementType();
3563 if (BaseInfo) *BaseInfo = LV.getBaseInfo();
3564 if (TBAAInfo) *TBAAInfo = CGM.getTBAAAccessInfo(EltType);
3565
3566 return Builder.CreateElementBitCast(Addr, ConvertTypeForMem(EltType));
3567}
3568
3569/// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
3570/// array to pointer, return the array subexpression.
3571static const Expr *isSimpleArrayDecayOperand(const Expr *E) {
3572 // If this isn't just an array->pointer decay, bail out.
3573 const auto *CE = dyn_cast<CastExpr>(E);
3574 if (!CE || CE->getCastKind() != CK_ArrayToPointerDecay)
3575 return nullptr;
3576
3577 // If this is a decay from variable width array, bail out.
3578 const Expr *SubExpr = CE->getSubExpr();
3579 if (SubExpr->getType()->isVariableArrayType())
3580 return nullptr;
3581
3582 return SubExpr;
3583}
3584
3585static llvm::Value *emitArraySubscriptGEP(CodeGenFunction &CGF,
3586 llvm::Type *elemType,
3587 llvm::Value *ptr,
3588 ArrayRef<llvm::Value*> indices,
3589 bool inbounds,
3590 bool signedIndices,
3591 SourceLocation loc,
3592 const llvm::Twine &name = "arrayidx") {
3593 if (inbounds) {
3594 return CGF.EmitCheckedInBoundsGEP(elemType, ptr, indices, signedIndices,
3595 CodeGenFunction::NotSubtraction, loc,
3596 name);
3597 } else {
3598 return CGF.Builder.CreateGEP(elemType, ptr, indices, name);
3599 }
3600}
3601
3602static CharUnits getArrayElementAlign(CharUnits arrayAlign,
3603 llvm::Value *idx,
3604 CharUnits eltSize) {
3605 // If we have a constant index, we can use the exact offset of the
3606 // element we're accessing.
3607 if (auto constantIdx = dyn_cast<llvm::ConstantInt>(idx)) {
3608 CharUnits offset = constantIdx->getZExtValue() * eltSize;
3609 return arrayAlign.alignmentAtOffset(offset);
3610
3611 // Otherwise, use the worst-case alignment for any element.
3612 } else {
3613 return arrayAlign.alignmentOfArrayElement(eltSize);
3614 }
3615}
3616
3617static QualType getFixedSizeElementType(const ASTContext &ctx,
3618 const VariableArrayType *vla) {
3619 QualType eltType;
3620 do {
3621 eltType = vla->getElementType();
3622 } while ((vla = ctx.getAsVariableArrayType(eltType)));
3623 return eltType;
3624}
3625
3626/// Given an array base, check whether its member access belongs to a record
3627/// with preserve_access_index attribute or not.
3628static bool IsPreserveAIArrayBase(CodeGenFunction &CGF, const Expr *ArrayBase) {
3629 if (!ArrayBase || !CGF.getDebugInfo())
3630 return false;
3631
3632 // Only support base as either a MemberExpr or DeclRefExpr.
3633 // DeclRefExpr to cover cases like:
3634 // struct s { int a; int b[10]; };
3635 // struct s *p;
3636 // p[1].a
3637 // p[1] will generate a DeclRefExpr and p[1].a is a MemberExpr.
3638 // p->b[5] is a MemberExpr example.
3639 const Expr *E = ArrayBase->IgnoreImpCasts();
3640 if (const auto *ME = dyn_cast<MemberExpr>(E))
3641 return ME->getMemberDecl()->hasAttr<BPFPreserveAccessIndexAttr>();
3642
3643 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
3644 const auto *VarDef = dyn_cast<VarDecl>(DRE->getDecl());
3645 if (!VarDef)
3646 return false;
3647
3648 const auto *PtrT = VarDef->getType()->getAs<PointerType>();
3649 if (!PtrT)
3650 return false;
3651
3652 const auto *PointeeT = PtrT->getPointeeType()
3653 ->getUnqualifiedDesugaredType();
3654 if (const auto *RecT = dyn_cast<RecordType>(PointeeT))
3655 return RecT->getDecl()->hasAttr<BPFPreserveAccessIndexAttr>();
3656 return false;
3657 }
3658
3659 return false;
3660}
3661
3662static Address emitArraySubscriptGEP(CodeGenFunction &CGF, Address addr,
3663 ArrayRef<llvm::Value *> indices,
3664 QualType eltType, bool inbounds,
3665 bool signedIndices, SourceLocation loc,
3666 QualType *arrayType = nullptr,
3667 const Expr *Base = nullptr,
3668 const llvm::Twine &name = "arrayidx") {
3669 // All the indices except that last must be zero.
3670#ifndef NDEBUG
3671 for (auto idx : indices.drop_back())
3672 assert(isa<llvm::ConstantInt>(idx) &&(static_cast <bool> (isa<llvm::ConstantInt>(idx) &&
cast<llvm::ConstantInt>(idx)->isZero()) ? void (0) :
__assert_fail ("isa<llvm::ConstantInt>(idx) && cast<llvm::ConstantInt>(idx)->isZero()"
, "clang/lib/CodeGen/CGExpr.cpp", 3673, __extension__ __PRETTY_FUNCTION__
))
3673 cast<llvm::ConstantInt>(idx)->isZero())(static_cast <bool> (isa<llvm::ConstantInt>(idx) &&
cast<llvm::ConstantInt>(idx)->isZero()) ? void (0) :
__assert_fail ("isa<llvm::ConstantInt>(idx) && cast<llvm::ConstantInt>(idx)->isZero()"
, "clang/lib/CodeGen/CGExpr.cpp", 3673, __extension__ __PRETTY_FUNCTION__
))
;
3674#endif
3675
3676 // Determine the element size of the statically-sized base. This is
3677 // the thing that the indices are expressed in terms of.
3678 if (auto vla = CGF.getContext().getAsVariableArrayType(eltType)) {
3679 eltType = getFixedSizeElementType(CGF.getContext(), vla);
3680 }
3681
3682 // We can use that to compute the best alignment of the element.
3683 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(eltType);
3684 CharUnits eltAlign =
3685 getArrayElementAlign(addr.getAlignment(), indices.back(), eltSize);
3686
3687 llvm::Value *eltPtr;
3688 auto LastIndex = dyn_cast<llvm::ConstantInt>(indices.back());
3689 if (!LastIndex ||
3690 (!CGF.IsInPreservedAIRegion && !IsPreserveAIArrayBase(CGF, Base))) {
3691 eltPtr = emitArraySubscriptGEP(
3692 CGF, addr.getElementType(), addr.getPointer(), indices, inbounds,
3693 signedIndices, loc, name);
3694 } else {
3695 // Remember the original array subscript for bpf target
3696 unsigned idx = LastIndex->getZExtValue();
3697 llvm::DIType *DbgInfo = nullptr;
3698 if (arrayType)
3699 DbgInfo = CGF.getDebugInfo()->getOrCreateStandaloneType(*arrayType, loc);
3700 eltPtr = CGF.Builder.CreatePreserveArrayAccessIndex(addr.getElementType(),
3701 addr.getPointer(),
3702 indices.size() - 1,
3703 idx, DbgInfo);
3704 }
3705
3706 return Address(eltPtr, CGF.ConvertTypeForMem(eltType), eltAlign);
3707}
3708
3709LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
3710 bool Accessed) {
3711 // The index must always be an integer, which is not an aggregate. Emit it
3712 // in lexical order (this complexity is, sadly, required by C++17).
3713 llvm::Value *IdxPre =
3714 (E->getLHS() == E->getIdx()) ? EmitScalarExpr(E->getIdx()) : nullptr;
3715 bool SignedIndices = false;
3716 auto EmitIdxAfterBase = [&, IdxPre](bool Promote) -> llvm::Value * {
3717 auto *Idx = IdxPre;
3718 if (E->getLHS() != E->getIdx()) {
3719 assert(E->getRHS() == E->getIdx() && "index was neither LHS nor RHS")(static_cast <bool> (E->getRHS() == E->getIdx() &&
"index was neither LHS nor RHS") ? void (0) : __assert_fail (
"E->getRHS() == E->getIdx() && \"index was neither LHS nor RHS\""
, "clang/lib/CodeGen/CGExpr.cpp", 3719, __extension__ __PRETTY_FUNCTION__
))
;
3720 Idx = EmitScalarExpr(E->getIdx());
3721 }
3722
3723 QualType IdxTy = E->getIdx()->getType();
3724 bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType();
3725 SignedIndices |= IdxSigned;
3726
3727 if (SanOpts.has(SanitizerKind::ArrayBounds))
3728 EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, Accessed);
3729
3730 // Extend or truncate the index type to 32 or 64-bits.
3731 if (Promote && Idx->getType() != IntPtrTy)
3732 Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
3733
3734 return Idx;
3735 };
3736 IdxPre = nullptr;
3737
3738 // If the base is a vector type, then we are forming a vector element lvalue
3739 // with this subscript.
3740 if (E->getBase()->getType()->isVectorType() &&
3741 !isa<ExtVectorElementExpr>(E->getBase())) {
3742 // Emit the vector as an lvalue to get its address.
3743 LValue LHS = EmitLValue(E->getBase());
3744 auto *Idx = EmitIdxAfterBase(/*Promote*/false);
3745 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!")(static_cast <bool> (LHS.isSimple() && "Can only subscript lvalue vectors here!"
) ? void (0) : __assert_fail ("LHS.isSimple() && \"Can only subscript lvalue vectors here!\""
, "clang/lib/CodeGen/CGExpr.cpp", 3745, __extension__ __PRETTY_FUNCTION__
))
;
3746 return LValue::MakeVectorElt(LHS.getAddress(*this), Idx,
3747 E->getBase()->getType(), LHS.getBaseInfo(),
3748 TBAAAccessInfo());
3749 }
3750
3751 // All the other cases basically behave like simple offsetting.
3752
3753 // Handle the extvector case we ignored above.
3754 if (isa<ExtVectorElementExpr>(E->getBase())) {
3755 LValue LV = EmitLValue(E->getBase());
3756 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
3757 Address Addr = EmitExtVectorElementLValue(LV);
3758
3759 QualType EltType = LV.getType()->castAs<VectorType>()->getElementType();
3760 Addr = emitArraySubscriptGEP(*this, Addr, Idx, EltType, /*inbounds*/ true,
3761 SignedIndices, E->getExprLoc());
3762 return MakeAddrLValue(Addr, EltType, LV.getBaseInfo(),
3763 CGM.getTBAAInfoForSubobject(LV, EltType));
3764 }
3765
3766 LValueBaseInfo EltBaseInfo;
3767 TBAAAccessInfo EltTBAAInfo;
3768 Address Addr = Address::invalid();
3769 if (const VariableArrayType *vla =
3770 getContext().getAsVariableArrayType(E->getType())) {
3771 // The base must be a pointer, which is not an aggregate. Emit
3772 // it. It needs to be emitted first in case it's what captures
3773 // the VLA bounds.
3774 Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo);
3775 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
3776
3777 // The element count here is the total number of non-VLA elements.
3778 llvm::Value *numElements = getVLASize(vla).NumElts;
3779
3780 // Effectively, the multiply by the VLA size is part of the GEP.
3781 // GEP indexes are signed, and scaling an index isn't permitted to
3782 // signed-overflow, so we use the same semantics for our explicit
3783 // multiply. We suppress this if overflow is not undefined behavior.
3784 if (getLangOpts().isSignedOverflowDefined()) {
3785 Idx = Builder.CreateMul(Idx, numElements);
3786 } else {
3787 Idx = Builder.CreateNSWMul(Idx, numElements);
3788 }
3789
3790 Addr = emitArraySubscriptGEP(*this, Addr, Idx, vla->getElementType(),
3791 !getLangOpts().isSignedOverflowDefined(),
3792 SignedIndices, E->getExprLoc());
3793
3794 } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
3795 // Indexing over an interface, as in "NSString *P; P[4];"
3796
3797 // Emit the base pointer.
3798 Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo);
3799 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
3800
3801 CharUnits InterfaceSize = getContext().getTypeSizeInChars(OIT);
3802 llvm::Value *InterfaceSizeVal =
3803 llvm::ConstantInt::get(Idx->getType(), InterfaceSize.getQuantity());
3804
3805 llvm::Value *ScaledIdx = Builder.CreateMul(Idx, InterfaceSizeVal);
3806
3807 // We don't necessarily build correct LLVM struct types for ObjC
3808 // interfaces, so we can't rely on GEP to do this scaling
3809 // correctly, so we need to cast to i8*. FIXME: is this actually
3810 // true? A lot of other things in the fragile ABI would break...
3811 llvm::Type *OrigBaseTy = Addr.getType();
3812 Addr = Builder.CreateElementBitCast(Addr, Int8Ty);
3813
3814 // Do the GEP.
3815 CharUnits EltAlign =
3816 getArrayElementAlign(Addr.getAlignment(), Idx, InterfaceSize);
3817 llvm::Value *EltPtr =
3818 emitArraySubscriptGEP(*this, Addr.getElementType(), Addr.getPointer(),
3819 ScaledIdx, false, SignedIndices, E->getExprLoc());
3820 Addr = Address(EltPtr, EltAlign);
3821
3822 // Cast back.
3823 Addr = Builder.CreateBitCast(Addr, OrigBaseTy);
3824 } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
3825 // If this is A[i] where A is an array, the frontend will have decayed the
3826 // base to be a ArrayToPointerDecay implicit cast. While correct, it is
3827 // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
3828 // "gep x, i" here. Emit one "gep A, 0, i".
3829 assert(Array->getType()->isArrayType() &&(static_cast <bool> (Array->getType()->isArrayType
() && "Array to pointer decay must have array source type!"
) ? void (0) : __assert_fail ("Array->getType()->isArrayType() && \"Array to pointer decay must have array source type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 3830, __extension__ __PRETTY_FUNCTION__
))
3830 "Array to pointer decay must have array source type!")(static_cast <bool> (Array->getType()->isArrayType
() && "Array to pointer decay must have array source type!"
) ? void (0) : __assert_fail ("Array->getType()->isArrayType() && \"Array to pointer decay must have array source type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 3830, __extension__ __PRETTY_FUNCTION__
))
;
3831 LValue ArrayLV;
3832 // For simple multidimensional array indexing, set the 'accessed' flag for
3833 // better bounds-checking of the base expression.
3834 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array))
3835 ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true);
3836 else
3837 ArrayLV = EmitLValue(Array);
3838 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
3839
3840 // Propagate the alignment from the array itself to the result.
3841 QualType arrayType = Array->getType();
3842 Addr = emitArraySubscriptGEP(
3843 *this, ArrayLV.getAddress(*this), {CGM.getSize(CharUnits::Zero()), Idx},
3844 E->getType(), !getLangOpts().isSignedOverflowDefined(), SignedIndices,
3845 E->getExprLoc(), &arrayType, E->getBase());
3846 EltBaseInfo = ArrayLV.getBaseInfo();
3847 EltTBAAInfo = CGM.getTBAAInfoForSubobject(ArrayLV, E->getType());
3848 } else {
3849 // The base must be a pointer; emit it with an estimate of its alignment.
3850 Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo);
3851 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
3852 QualType ptrType = E->getBase()->getType();
3853 Addr = emitArraySubscriptGEP(*this, Addr, Idx, E->getType(),
3854 !getLangOpts().isSignedOverflowDefined(),
3855 SignedIndices, E->getExprLoc(), &ptrType,
3856 E->getBase());
3857 }
3858
3859 LValue LV = MakeAddrLValue(Addr, E->getType(), EltBaseInfo, EltTBAAInfo);
3860
3861 if (getLangOpts().ObjC &&
3862 getLangOpts().getGC() != LangOptions::NonGC) {
3863 LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
3864 setObjCGCLValueClass(getContext(), E, LV);
3865 }
3866 return LV;
3867}
3868
3869LValue CodeGenFunction::EmitMatrixSubscriptExpr(const MatrixSubscriptExpr *E) {
3870 assert((static_cast <bool> (!E->isIncomplete() && "incomplete matrix subscript expressions should be rejected during Sema"
) ? void (0) : __assert_fail ("!E->isIncomplete() && \"incomplete matrix subscript expressions should be rejected during Sema\""
, "clang/lib/CodeGen/CGExpr.cpp", 3872, __extension__ __PRETTY_FUNCTION__
))
3871 !E->isIncomplete() &&(static_cast <bool> (!E->isIncomplete() && "incomplete matrix subscript expressions should be rejected during Sema"
) ? void (0) : __assert_fail ("!E->isIncomplete() && \"incomplete matrix subscript expressions should be rejected during Sema\""
, "clang/lib/CodeGen/CGExpr.cpp", 3872, __extension__ __PRETTY_FUNCTION__
))
3872 "incomplete matrix subscript expressions should be rejected during Sema")(static_cast <bool> (!E->isIncomplete() && "incomplete matrix subscript expressions should be rejected during Sema"
) ? void (0) : __assert_fail ("!E->isIncomplete() && \"incomplete matrix subscript expressions should be rejected during Sema\""
, "clang/lib/CodeGen/CGExpr.cpp", 3872, __extension__ __PRETTY_FUNCTION__
))
;
3873 LValue Base = EmitLValue(E->getBase());
3874 llvm::Value *RowIdx = EmitScalarExpr(E->getRowIdx());
3875 llvm::Value *ColIdx = EmitScalarExpr(E->getColumnIdx());
3876 llvm::Value *NumRows = Builder.getIntN(
3877 RowIdx->getType()->getScalarSizeInBits(),
3878 E->getBase()->getType()->castAs<ConstantMatrixType>()->getNumRows());
3879 llvm::Value *FinalIdx =
3880 Builder.CreateAdd(Builder.CreateMul(ColIdx, NumRows), RowIdx);
3881 return LValue::MakeMatrixElt(
3882 MaybeConvertMatrixAddress(Base.getAddress(*this), *this), FinalIdx,
3883 E->getBase()->getType(), Base.getBaseInfo(), TBAAAccessInfo());
3884}
3885
3886static Address emitOMPArraySectionBase(CodeGenFunction &CGF, const Expr *Base,
3887 LValueBaseInfo &BaseInfo,
3888 TBAAAccessInfo &TBAAInfo,
3889 QualType BaseTy, QualType ElTy,
3890 bool IsLowerBound) {
3891 LValue BaseLVal;
3892 if (auto *ASE = dyn_cast<OMPArraySectionExpr>(Base->IgnoreParenImpCasts())) {
3893 BaseLVal = CGF.EmitOMPArraySectionExpr(ASE, IsLowerBound);
3894 if (BaseTy->isArrayType()) {
3895 Address Addr = BaseLVal.getAddress(CGF);
3896 BaseInfo = BaseLVal.getBaseInfo();
3897
3898 // If the array type was an incomplete type, we need to make sure
3899 // the decay ends up being the right type.
3900 llvm::Type *NewTy = CGF.ConvertType(BaseTy);
3901 Addr = CGF.Builder.CreateElementBitCast(Addr, NewTy);
3902
3903 // Note that VLA pointers are always decayed, so we don't need to do
3904 // anything here.
3905 if (!BaseTy->isVariableArrayType()) {
3906 assert(isa<llvm::ArrayType>(Addr.getElementType()) &&(static_cast <bool> (isa<llvm::ArrayType>(Addr.getElementType
()) && "Expected pointer to array") ? void (0) : __assert_fail
("isa<llvm::ArrayType>(Addr.getElementType()) && \"Expected pointer to array\""
, "clang/lib/CodeGen/CGExpr.cpp", 3907, __extension__ __PRETTY_FUNCTION__
))
3907 "Expected pointer to array")(static_cast <bool> (isa<llvm::ArrayType>(Addr.getElementType
()) && "Expected pointer to array") ? void (0) : __assert_fail
("isa<llvm::ArrayType>(Addr.getElementType()) && \"Expected pointer to array\""
, "clang/lib/CodeGen/CGExpr.cpp", 3907, __extension__ __PRETTY_FUNCTION__
))
;
3908 Addr = CGF.Builder.CreateConstArrayGEP(Addr, 0, "arraydecay");
3909 }
3910
3911 return CGF.Builder.CreateElementBitCast(Addr,
3912 CGF.ConvertTypeForMem(ElTy));
3913 }
3914 LValueBaseInfo TypeBaseInfo;
3915 TBAAAccessInfo TypeTBAAInfo;
3916 CharUnits Align =
3917 CGF.CGM.getNaturalTypeAlignment(ElTy, &TypeBaseInfo, &TypeTBAAInfo);
3918 BaseInfo.mergeForCast(TypeBaseInfo);
3919 TBAAInfo = CGF.CGM.mergeTBAAInfoForCast(TBAAInfo, TypeTBAAInfo);
3920 return Address(CGF.Builder.CreateLoad(BaseLVal.getAddress(CGF)), Align);
3921 }
3922 return CGF.EmitPointerWithAlignment(Base, &BaseInfo, &TBAAInfo);
3923}
3924
3925LValue CodeGenFunction::EmitOMPArraySectionExpr(const OMPArraySectionExpr *E,
3926 bool IsLowerBound) {
3927 QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType(E->getBase());
3928 QualType ResultExprTy;
3929 if (auto *AT = getContext().getAsArrayType(BaseTy))
3930 ResultExprTy = AT->getElementType();
3931 else
3932 ResultExprTy = BaseTy->getPointeeType();
3933 llvm::Value *Idx = nullptr;
3934 if (IsLowerBound || E->getColonLocFirst().isInvalid()) {
3935 // Requesting lower bound or upper bound, but without provided length and
3936 // without ':' symbol for the default length -> length = 1.
3937 // Idx = LowerBound ?: 0;
3938 if (auto *LowerBound = E->getLowerBound()) {
3939 Idx = Builder.CreateIntCast(
3940 EmitScalarExpr(LowerBound), IntPtrTy,
3941 LowerBound->getType()->hasSignedIntegerRepresentation());
3942 } else
3943 Idx = llvm::ConstantInt::getNullValue(IntPtrTy);
3944 } else {
3945 // Try to emit length or lower bound as constant. If this is possible, 1
3946 // is subtracted from constant length or lower bound. Otherwise, emit LLVM
3947 // IR (LB + Len) - 1.
3948 auto &C = CGM.getContext();
3949 auto *Length = E->getLength();
3950 llvm::APSInt ConstLength;
3951 if (Length) {
3952 // Idx = LowerBound + Length - 1;
3953 if (Optional<llvm::APSInt> CL = Length->getIntegerConstantExpr(C)) {
3954 ConstLength = CL->zextOrTrunc(PointerWidthInBits);
3955 Length = nullptr;
3956 }
3957 auto *LowerBound = E->getLowerBound();
3958 llvm::APSInt ConstLowerBound(PointerWidthInBits, /*isUnsigned=*/false);
3959 if (LowerBound) {
3960 if (Optional<llvm::APSInt> LB = LowerBound->getIntegerConstantExpr(C)) {
3961 ConstLowerBound = LB->zextOrTrunc(PointerWidthInBits);
3962 LowerBound = nullptr;
3963 }
3964 }
3965 if (!Length)
3966 --ConstLength;
3967 else if (!LowerBound)
3968 --ConstLowerBound;
3969
3970 if (Length || LowerBound) {
3971 auto *LowerBoundVal =
3972 LowerBound
3973 ? Builder.CreateIntCast(
3974 EmitScalarExpr(LowerBound), IntPtrTy,
3975 LowerBound->getType()->hasSignedIntegerRepresentation())
3976 : llvm::ConstantInt::get(IntPtrTy, ConstLowerBound);
3977 auto *LengthVal =
3978 Length
3979 ? Builder.CreateIntCast(
3980 EmitScalarExpr(Length), IntPtrTy,
3981 Length->getType()->hasSignedIntegerRepresentation())
3982 : llvm::ConstantInt::get(IntPtrTy, ConstLength);
3983 Idx = Builder.CreateAdd(LowerBoundVal, LengthVal, "lb_add_len",
3984 /*HasNUW=*/false,
3985 !getLangOpts().isSignedOverflowDefined());
3986 if (Length && LowerBound) {
3987 Idx = Builder.CreateSub(
3988 Idx, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "idx_sub_1",
3989 /*HasNUW=*/false, !getLangOpts().isSignedOverflowDefined());
3990 }
3991 } else
3992 Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength + ConstLowerBound);
3993 } else {
3994 // Idx = ArraySize - 1;
3995 QualType ArrayTy = BaseTy->isPointerType()
3996 ? E->getBase()->IgnoreParenImpCasts()->getType()
3997 : BaseTy;
3998 if (auto *VAT = C.getAsVariableArrayType(ArrayTy)) {
3999 Length = VAT->getSizeExpr();
4000 if (Optional<llvm::APSInt> L = Length->getIntegerConstantExpr(C)) {
4001 ConstLength = *L;
4002 Length = nullptr;
4003 }
4004 } else {
4005 auto *CAT = C.getAsConstantArrayType(ArrayTy);
4006 ConstLength = CAT->getSize();
4007 }
4008 if (Length) {
4009 auto *LengthVal = Builder.CreateIntCast(
4010 EmitScalarExpr(Length), IntPtrTy,
4011 Length->getType()->hasSignedIntegerRepresentation());
4012 Idx = Builder.CreateSub(
4013 LengthVal, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "len_sub_1",
4014 /*HasNUW=*/false, !getLangOpts().isSignedOverflowDefined());
4015 } else {
4016 ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits);
4017 --ConstLength;
4018 Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength);
4019 }
4020 }
4021 }
4022 assert(Idx)(static_cast <bool> (Idx) ? void (0) : __assert_fail ("Idx"
, "clang/lib/CodeGen/CGExpr.cpp", 4022, __extension__ __PRETTY_FUNCTION__
))
;
4023
4024 Address EltPtr = Address::invalid();
4025 LValueBaseInfo BaseInfo;
4026 TBAAAccessInfo TBAAInfo;
4027 if (auto *VLA = getContext().getAsVariableArrayType(ResultExprTy)) {
4028 // The base must be a pointer, which is not an aggregate. Emit
4029 // it. It needs to be emitted first in case it's what captures
4030 // the VLA bounds.
4031 Address Base =
4032 emitOMPArraySectionBase(*this, E->getBase(), BaseInfo, TBAAInfo,
4033 BaseTy, VLA->getElementType(), IsLowerBound);
4034 // The element count here is the total number of non-VLA elements.
4035 llvm::Value *NumElements = getVLASize(VLA).NumElts;
4036
4037 // Effectively, the multiply by the VLA size is part of the GEP.
4038 // GEP indexes are signed, and scaling an index isn't permitted to
4039 // signed-overflow, so we use the same semantics for our explicit
4040 // multiply. We suppress this if overflow is not undefined behavior.
4041 if (getLangOpts().isSignedOverflowDefined())
4042 Idx = Builder.CreateMul(Idx, NumElements);
4043 else
4044 Idx = Builder.CreateNSWMul(Idx, NumElements);
4045 EltPtr = emitArraySubscriptGEP(*this, Base, Idx, VLA->getElementType(),
4046 !getLangOpts().isSignedOverflowDefined(),
4047 /*signedIndices=*/false, E->getExprLoc());
4048 } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
4049 // If this is A[i] where A is an array, the frontend will have decayed the
4050 // base to be a ArrayToPointerDecay implicit cast. While correct, it is
4051 // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
4052 // "gep x, i" here. Emit one "gep A, 0, i".
4053 assert(Array->getType()->isArrayType() &&(static_cast <bool> (Array->getType()->isArrayType
() && "Array to pointer decay must have array source type!"
) ? void (0) : __assert_fail ("Array->getType()->isArrayType() && \"Array to pointer decay must have array source type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 4054, __extension__ __PRETTY_FUNCTION__
))
4054 "Array to pointer decay must have array source type!")(static_cast <bool> (Array->getType()->isArrayType
() && "Array to pointer decay must have array source type!"
) ? void (0) : __assert_fail ("Array->getType()->isArrayType() && \"Array to pointer decay must have array source type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 4054, __extension__ __PRETTY_FUNCTION__
))
;
4055 LValue ArrayLV;
4056 // For simple multidimensional array indexing, set the 'accessed' flag for
4057 // better bounds-checking of the base expression.
4058 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array))
4059 ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true);
4060 else
4061 ArrayLV = EmitLValue(Array);
4062
4063 // Propagate the alignment from the array itself to the result.
4064 EltPtr = emitArraySubscriptGEP(
4065 *this, ArrayLV.getAddress(*this), {CGM.getSize(CharUnits::Zero()), Idx},
4066 ResultExprTy, !getLangOpts().isSignedOverflowDefined(),
4067 /*signedIndices=*/false, E->getExprLoc());
4068 BaseInfo = ArrayLV.getBaseInfo();
4069 TBAAInfo = CGM.getTBAAInfoForSubobject(ArrayLV, ResultExprTy);
4070 } else {
4071 Address Base = emitOMPArraySectionBase(*this, E->getBase(), BaseInfo,
4072 TBAAInfo, BaseTy, ResultExprTy,
4073 IsLowerBound);
4074 EltPtr = emitArraySubscriptGEP(*this, Base, Idx, ResultExprTy,
4075 !getLangOpts().isSignedOverflowDefined(),
4076 /*signedIndices=*/false, E->getExprLoc());
4077 }
4078
4079 return MakeAddrLValue(EltPtr, ResultExprTy, BaseInfo, TBAAInfo);
4080}
4081
4082LValue CodeGenFunction::
4083EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
4084 // Emit the base vector as an l-value.
4085 LValue Base;
4086
4087 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
4088 if (E->isArrow()) {
4089 // If it is a pointer to a vector, emit the address and form an lvalue with
4090 // it.
4091 LValueBaseInfo BaseInfo;
4092 TBAAAccessInfo TBAAInfo;
4093 Address Ptr = EmitPointerWithAlignment(E->getBase(), &BaseInfo, &TBAAInfo);
4094 const auto *PT = E->getBase()->getType()->castAs<PointerType>();
4095 Base = MakeAddrLValue(Ptr, PT->getPointeeType(), BaseInfo, TBAAInfo);
4096 Base.getQuals().removeObjCGCAttr();
4097 } else if (E->getBase()->isGLValue()) {
4098 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
4099 // emit the base as an lvalue.
4100 assert(E->getBase()->getType()->isVectorType())(static_cast <bool> (E->getBase()->getType()->
isVectorType()) ? void (0) : __assert_fail ("E->getBase()->getType()->isVectorType()"
, "clang/lib/CodeGen/CGExpr.cpp", 4100, __extension__ __PRETTY_FUNCTION__
))
;
4101 Base = EmitLValue(E->getBase());
4102 } else {
4103 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
4104 assert(E->getBase()->getType()->isVectorType() &&(static_cast <bool> (E->getBase()->getType()->
isVectorType() && "Result must be a vector") ? void (
0) : __assert_fail ("E->getBase()->getType()->isVectorType() && \"Result must be a vector\""
, "clang/lib/CodeGen/CGExpr.cpp", 4105, __extension__ __PRETTY_FUNCTION__
))
4105 "Result must be a vector")(static_cast <bool> (E->getBase()->getType()->
isVectorType() && "Result must be a vector") ? void (
0) : __assert_fail ("E->getBase()->getType()->isVectorType() && \"Result must be a vector\""
, "clang/lib/CodeGen/CGExpr.cpp", 4105, __extension__ __PRETTY_FUNCTION__
))
;
4106 llvm::Value *Vec = EmitScalarExpr(E->getBase());
4107
4108 // Store the vector to memory (because LValue wants an address).
4109 Address VecMem = CreateMemTemp(E->getBase()->getType());
4110 Builder.CreateStore(Vec, VecMem);
4111 Base = MakeAddrLValue(VecMem, E->getBase()->getType(),
4112 AlignmentSource::Decl);
4113 }
4114
4115 QualType type =
4116 E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers());
4117
4118 // Encode the element access list into a vector of unsigned indices.
4119 SmallVector<uint32_t, 4> Indices;
4120 E->getEncodedElementAccess(Indices);
4121
4122 if (Base.isSimple()) {
4123 llvm::Constant *CV =
4124 llvm::ConstantDataVector::get(getLLVMContext(), Indices);
4125 return LValue::MakeExtVectorElt(Base.getAddress(*this), CV, type,
4126 Base.getBaseInfo(), TBAAAccessInfo());
4127 }
4128 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!")(static_cast <bool> (Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!"
) ? void (0) : __assert_fail ("Base.isExtVectorElt() && \"Can only subscript lvalue vec elts here!\""
, "clang/lib/CodeGen/CGExpr.cpp", 4128, __extension__ __PRETTY_FUNCTION__
))
;
4129
4130 llvm::Constant *BaseElts = Base.getExtVectorElts();
4131 SmallVector<llvm::Constant *, 4> CElts;
4132
4133 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
4134 CElts.push_back(BaseElts->getAggregateElement(Indices[i]));
4135 llvm::Constant *CV = llvm::ConstantVector::get(CElts);
4136 return LValue::MakeExtVectorElt(Base.getExtVectorAddress(), CV, type,
4137 Base.getBaseInfo(), TBAAAccessInfo());
4138}
4139
4140LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
4141 if (DeclRefExpr *DRE = tryToConvertMemberExprToDeclRefExpr(*this, E)) {
4142 EmitIgnoredExpr(E->getBase());
4143 return EmitDeclRefLValue(DRE);
4144 }
4145
4146 Expr *BaseExpr = E->getBase();
4147 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
4148 LValue BaseLV;
4149 if (E->isArrow()) {
4150 LValueBaseInfo BaseInfo;
4151 TBAAAccessInfo TBAAInfo;
4152 Address Addr = EmitPointerWithAlignment(BaseExpr, &BaseInfo, &TBAAInfo);
4153 QualType PtrTy = BaseExpr->getType()->getPointeeType();
4154 SanitizerSet SkippedChecks;
4155 bool IsBaseCXXThis = IsWrappedCXXThis(BaseExpr);
4156 if (IsBaseCXXThis)
4157 SkippedChecks.set(SanitizerKind::Alignment, true);
4158 if (IsBaseCXXThis || isa<DeclRefExpr>(BaseExpr))
4159 SkippedChecks.set(SanitizerKind::Null, true);
4160 EmitTypeCheck(TCK_MemberAccess, E->getExprLoc(), Addr.getPointer(), PtrTy,
4161 /*Alignment=*/CharUnits::Zero(), SkippedChecks);
4162 BaseLV = MakeAddrLValue(Addr, PtrTy, BaseInfo, TBAAInfo);
4163 } else
4164 BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess);
4165
4166 NamedDecl *ND = E->getMemberDecl();
4167 if (auto *Field = dyn_cast<FieldDecl>(ND)) {
4168 LValue LV = EmitLValueForField(BaseLV, Field);
4169 setObjCGCLValueClass(getContext(), E, LV);
4170 if (getLangOpts().OpenMP) {
4171 // If the member was explicitly marked as nontemporal, mark it as
4172 // nontemporal. If the base lvalue is marked as nontemporal, mark access
4173 // to children as nontemporal too.
4174 if ((IsWrappedCXXThis(BaseExpr) &&
4175 CGM.getOpenMPRuntime().isNontemporalDecl(Field)) ||
4176 BaseLV.isNontemporal())
4177 LV.setNontemporal(/*Value=*/true);
4178 }
4179 return LV;
4180 }
4181
4182 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
4183 return EmitFunctionDeclLValue(*this, E, FD);
4184
4185 llvm_unreachable("Unhandled member declaration!")::llvm::llvm_unreachable_internal("Unhandled member declaration!"
, "clang/lib/CodeGen/CGExpr.cpp", 4185)
;
4186}
4187
4188/// Given that we are currently emitting a lambda, emit an l-value for
4189/// one of its members.
4190LValue CodeGenFunction::EmitLValueForLambdaField(const FieldDecl *Field) {
4191 if (CurCodeDecl) {
4192 assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent()->isLambda())(static_cast <bool> (cast<CXXMethodDecl>(CurCodeDecl
)->getParent()->isLambda()) ? void (0) : __assert_fail (
"cast<CXXMethodDecl>(CurCodeDecl)->getParent()->isLambda()"
, "clang/lib/CodeGen/CGExpr.cpp", 4192, __extension__ __PRETTY_FUNCTION__
))
;
4193 assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent() == Field->getParent())(static_cast <bool> (cast<CXXMethodDecl>(CurCodeDecl
)->getParent() == Field->getParent()) ? void (0) : __assert_fail
("cast<CXXMethodDecl>(CurCodeDecl)->getParent() == Field->getParent()"
, "clang/lib/CodeGen/CGExpr.cpp", 4193, __extension__ __PRETTY_FUNCTION__
))
;
4194 }
4195 QualType LambdaTagType =
4196 getContext().getTagDeclType(Field->getParent());
4197 LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue, LambdaTagType);
4198 return EmitLValueForField(LambdaLV, Field);
4199}
4200
4201/// Get the field index in the debug info. The debug info structure/union
4202/// will ignore the unnamed bitfields.
4203unsigned CodeGenFunction::getDebugInfoFIndex(const RecordDecl *Rec,
4204 unsigned FieldIndex) {
4205 unsigned I = 0, Skipped = 0;
4206
4207 for (auto F : Rec->getDefinition()->fields()) {
4208 if (I == FieldIndex)
4209 break;
4210 if (F->isUnnamedBitfield())
4211 Skipped++;
4212 I++;
4213 }
4214
4215 return FieldIndex - Skipped;
4216}
4217
4218/// Get the address of a zero-sized field within a record. The resulting
4219/// address doesn't necessarily have the right type.
4220static Address emitAddrOfZeroSizeField(CodeGenFunction &CGF, Address Base,
4221 const FieldDecl *Field) {
4222 CharUnits Offset = CGF.getContext().toCharUnitsFromBits(
4223 CGF.getContext().getFieldOffset(Field));
4224 if (Offset.isZero())
4225 return Base;
4226 Base = CGF.Builder.CreateElementBitCast(Base, CGF.Int8Ty);
4227 return CGF.Builder.CreateConstInBoundsByteGEP(Base, Offset);
4228}
4229
4230/// Drill down to the storage of a field without walking into
4231/// reference types.
4232///
4233/// The resulting address doesn't necessarily have the right type.
4234static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base,
4235 const FieldDecl *field) {
4236 if (field->isZeroSize(CGF.getContext()))
4237 return emitAddrOfZeroSizeField(CGF, base, field);
4238
4239 const RecordDecl *rec = field->getParent();
4240
4241 unsigned idx =
4242 CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
4243
4244 return CGF.Builder.CreateStructGEP(base, idx, field->getName());
4245}
4246
4247static Address emitPreserveStructAccess(CodeGenFunction &CGF, LValue base,
4248 Address addr, const FieldDecl *field) {
4249 const RecordDecl *rec = field->getParent();
4250 llvm::DIType *DbgInfo = CGF.getDebugInfo()->getOrCreateStandaloneType(
4251 base.getType(), rec->getLocation());
4252
4253 unsigned idx =
4254 CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
4255
4256 return CGF.Builder.CreatePreserveStructAccessIndex(
4257 addr, idx, CGF.getDebugInfoFIndex(rec, field->getFieldIndex()), DbgInfo);
4258}
4259
4260static bool hasAnyVptr(const QualType Type, const ASTContext &Context) {
4261 const auto *RD = Type.getTypePtr()->getAsCXXRecordDecl();
4262 if (!RD)
4263 return false;
4264
4265 if (RD->isDynamicClass())
4266 return true;
4267
4268 for (const auto &Base : RD->bases())
4269 if (hasAnyVptr(Base.getType(), Context))
4270 return true;
4271
4272 for (const FieldDecl *Field : RD->fields())
4273 if (hasAnyVptr(Field->getType(), Context))
4274 return true;
4275
4276 return false;
4277}
4278
4279LValue CodeGenFunction::EmitLValueForField(LValue base,
4280 const FieldDecl *field) {
4281 LValueBaseInfo BaseInfo = base.getBaseInfo();
4282
4283 if (field->isBitField()) {
4284 const CGRecordLayout &RL =
4285 CGM.getTypes().getCGRecordLayout(field->getParent());
4286 const CGBitFieldInfo &Info = RL.getBitFieldInfo(field);
4287 const bool UseVolatile = isAAPCS(CGM.getTarget()) &&
4288 CGM.getCodeGenOpts().AAPCSBitfieldWidth &&
4289 Info.VolatileStorageSize != 0 &&
4290 field->getType()
4291 .withCVRQualifiers(base.getVRQualifiers())
4292 .isVolatileQualified();
4293 Address Addr = base.getAddress(*this);
4294 unsigned Idx = RL.getLLVMFieldNo(field);
4295 const RecordDecl *rec = field->getParent();
4296 if (!UseVolatile) {
4297 if (!IsInPreservedAIRegion &&
4298 (!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>())) {
4299 if (Idx != 0)
4300 // For structs, we GEP to the field that the record layout suggests.
4301 Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
4302 } else {
4303 llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateRecordType(
4304 getContext().getRecordType(rec), rec->getLocation());
4305 Addr = Builder.CreatePreserveStructAccessIndex(
4306 Addr, Idx, getDebugInfoFIndex(rec, field->getFieldIndex()),
4307 DbgInfo);
4308 }
4309 }
4310 const unsigned SS =
4311 UseVolatile ? Info.VolatileStorageSize : Info.StorageSize;
4312 // Get the access type.
4313 llvm::Type *FieldIntTy = llvm::Type::getIntNTy(getLLVMContext(), SS);
4314 if (Addr.getElementType() != FieldIntTy)
4315 Addr = Builder.CreateElementBitCast(Addr, FieldIntTy);
4316 if (UseVolatile) {
4317 const unsigned VolatileOffset = Info.VolatileStorageOffset.getQuantity();
4318 if (VolatileOffset)
4319 Addr = Builder.CreateConstInBoundsGEP(Addr, VolatileOffset);
4320 }
4321
4322 QualType fieldType =
4323 field->getType().withCVRQualifiers(base.getVRQualifiers());
4324 // TODO: Support TBAA for bit fields.
4325 LValueBaseInfo FieldBaseInfo(BaseInfo.getAlignmentSource());
4326 return LValue::MakeBitfield(Addr, Info, fieldType, FieldBaseInfo,
4327 TBAAAccessInfo());
4328 }
4329
4330 // Fields of may-alias structures are may-alias themselves.
4331 // FIXME: this should get propagated down through anonymous structs
4332 // and unions.
4333 QualType FieldType = field->getType();
4334 const RecordDecl *rec = field->getParent();
4335 AlignmentSource BaseAlignSource = BaseInfo.getAlignmentSource();
4336 LValueBaseInfo FieldBaseInfo(getFieldAlignmentSource(BaseAlignSource));
4337 TBAAAccessInfo FieldTBAAInfo;
4338 if (base.getTBAAInfo().isMayAlias() ||
4339 rec->hasAttr<MayAliasAttr>() || FieldType->isVectorType()) {
4340 FieldTBAAInfo = TBAAAccessInfo::getMayAliasInfo();
4341 } else if (rec->isUnion()) {
4342 // TODO: Support TBAA for unions.
4343 FieldTBAAInfo = TBAAAccessInfo::getMayAliasInfo();
4344 } else {
4345 // If no base type been assigned for the base access, then try to generate
4346 // one for this base lvalue.
4347 FieldTBAAInfo = base.getTBAAInfo();
4348 if (!FieldTBAAInfo.BaseType) {
4349 FieldTBAAInfo.BaseType = CGM.getTBAABaseTypeInfo(base.getType());
4350 assert(!FieldTBAAInfo.Offset &&(static_cast <bool> (!FieldTBAAInfo.Offset && "Nonzero offset for an access with no base type!"
) ? void (0) : __assert_fail ("!FieldTBAAInfo.Offset && \"Nonzero offset for an access with no base type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 4351, __extension__ __PRETTY_FUNCTION__
))
4351 "Nonzero offset for an access with no base type!")(static_cast <bool> (!FieldTBAAInfo.Offset && "Nonzero offset for an access with no base type!"
) ? void (0) : __assert_fail ("!FieldTBAAInfo.Offset && \"Nonzero offset for an access with no base type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 4351, __extension__ __PRETTY_FUNCTION__
))
;
4352 }
4353
4354 // Adjust offset to be relative to the base type.
4355 const ASTRecordLayout &Layout =
4356 getContext().getASTRecordLayout(field->getParent());
4357 unsigned CharWidth = getContext().getCharWidth();
4358 if (FieldTBAAInfo.BaseType)
4359 FieldTBAAInfo.Offset +=
4360 Layout.getFieldOffset(field->getFieldIndex()) / CharWidth;
4361
4362 // Update the final access type and size.
4363 FieldTBAAInfo.AccessType = CGM.getTBAATypeInfo(FieldType);
4364 FieldTBAAInfo.Size =
4365 getContext().getTypeSizeInChars(FieldType).getQuantity();
4366 }
4367
4368 Address addr = base.getAddress(*this);
4369 if (auto *ClassDef = dyn_cast<CXXRecordDecl>(rec)) {
4370 if (CGM.getCodeGenOpts().StrictVTablePointers &&
4371 ClassDef->isDynamicClass()) {
4372 // Getting to any field of dynamic object requires stripping dynamic
4373 // information provided by invariant.group. This is because accessing
4374 // fields may leak the real address of dynamic object, which could result
4375 // in miscompilation when leaked pointer would be compared.
4376 auto *stripped = Builder.CreateStripInvariantGroup(addr.getPointer());
4377 addr = Address(stripped, addr.getAlignment());
4378 }
4379 }
4380
4381 unsigned RecordCVR = base.getVRQualifiers();
4382 if (rec->isUnion()) {
4383 // For unions, there is no pointer adjustment.
4384 if (CGM.getCodeGenOpts().StrictVTablePointers &&
4385 hasAnyVptr(FieldType, getContext()))
4386 // Because unions can easily skip invariant.barriers, we need to add
4387 // a barrier every time CXXRecord field with vptr is referenced.
4388 addr = Builder.CreateLaunderInvariantGroup(addr);
4389
4390 if (IsInPreservedAIRegion ||
4391 (getDebugInfo() && rec->hasAttr<BPFPreserveAccessIndexAttr>())) {
4392 // Remember the original union field index
4393 llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateStandaloneType(base.getType(),
4394 rec->getLocation());
4395 addr = Address(
4396 Builder.CreatePreserveUnionAccessIndex(
4397 addr.getPointer(), getDebugInfoFIndex(rec, field->getFieldIndex()), DbgInfo),
4398 addr.getAlignment());
4399 }
4400
4401 if (FieldType->isReferenceType())
4402 addr = Builder.CreateElementBitCast(
4403 addr, CGM.getTypes().ConvertTypeForMem(FieldType), field->getName());
4404 } else {
4405 if (!IsInPreservedAIRegion &&
4406 (!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>()))
4407 // For structs, we GEP to the field that the record layout suggests.
4408 addr = emitAddrOfFieldStorage(*this, addr, field);
4409 else
4410 // Remember the original struct field index
4411 addr = emitPreserveStructAccess(*this, base, addr, field);
4412 }
4413
4414 // If this is a reference field, load the reference right now.
4415 if (FieldType->isReferenceType()) {
4416 LValue RefLVal =
4417 MakeAddrLValue(addr, FieldType, FieldBaseInfo, FieldTBAAInfo);
4418 if (RecordCVR & Qualifiers::Volatile)
4419 RefLVal.getQuals().addVolatile();
4420 addr = EmitLoadOfReference(RefLVal, &FieldBaseInfo, &FieldTBAAInfo);
4421
4422 // Qualifiers on the struct don't apply to the referencee.
4423 RecordCVR = 0;
4424 FieldType = FieldType->getPointeeType();
4425 }
4426
4427 // Make sure that the address is pointing to the right type. This is critical
4428 // for both unions and structs. A union needs a bitcast, a struct element
4429 // will need a bitcast if the LLVM type laid out doesn't match the desired
4430 // type.
4431 addr = Builder.CreateElementBitCast(
4432 addr, CGM.getTypes().ConvertTypeForMem(FieldType), field->getName());
4433
4434 if (field->hasAttr<AnnotateAttr>())
4435 addr = EmitFieldAnnotations(field, addr);
4436
4437 LValue LV = MakeAddrLValue(addr, FieldType, FieldBaseInfo, FieldTBAAInfo);
4438 LV.getQuals().addCVRQualifiers(RecordCVR);
4439
4440 // __weak attribute on a field is ignored.
4441 if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak)
4442 LV.getQuals().removeObjCGCAttr();
4443
4444 return LV;
4445}
4446
4447LValue
4448CodeGenFunction::EmitLValueForFieldInitialization(LValue Base,
4449 const FieldDecl *Field) {
4450 QualType FieldType = Field->getType();
4451
4452 if (!FieldType->isReferenceType())
4453 return EmitLValueForField(Base, Field);
4454
4455 Address V = emitAddrOfFieldStorage(*this, Base.getAddress(*this), Field);
4456
4457 // Make sure that the address is pointing to the right type.
4458 llvm::Type *llvmType = ConvertTypeForMem(FieldType);
4459 V = Builder.CreateElementBitCast(V, llvmType, Field->getName());
4460
4461 // TODO: Generate TBAA information that describes this access as a structure
4462 // member access and not just an access to an object of the field's type. This
4463 // should be similar to what we do in EmitLValueForField().
4464 LValueBaseInfo BaseInfo = Base.getBaseInfo();
4465 AlignmentSource FieldAlignSource = BaseInfo.getAlignmentSource();
4466 LValueBaseInfo FieldBaseInfo(getFieldAlignmentSource(FieldAlignSource));
4467 return MakeAddrLValue(V, FieldType, FieldBaseInfo,
4468 CGM.getTBAAInfoForSubobject(Base, FieldType));
4469}
4470
4471LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
4472 if (E->isFileScope()) {
4473 ConstantAddress GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E);
4474 return MakeAddrLValue(GlobalPtr, E->getType(), AlignmentSource::Decl);
4475 }
4476 if (E->getType()->isVariablyModifiedType())
4477 // make sure to emit the VLA size.
4478 EmitVariablyModifiedType(E->getType());
4479
4480 Address DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
4481 const Expr *InitExpr = E->getInitializer();
4482 LValue Result = MakeAddrLValue(DeclPtr, E->getType(), AlignmentSource::Decl);
4483
4484 EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(),
4485 /*Init*/ true);
4486
4487 // Block-scope compound literals are destroyed at the end of the enclosing
4488 // scope in C.
4489 if (!getLangOpts().CPlusPlus)
4490 if (QualType::DestructionKind DtorKind = E->getType().isDestructedType())
4491 pushLifetimeExtendedDestroy(getCleanupKind(DtorKind), DeclPtr,
4492 E->getType(), getDestroyer(DtorKind),
4493 DtorKind & EHCleanup);
4494
4495 return Result;
4496}
4497
4498LValue CodeGenFunction::EmitInitListLValue(const InitListExpr *E) {
4499 if (!E->isGLValue())
4500 // Initializing an aggregate temporary in C++11: T{...}.
4501 return EmitAggExprToLValue(E);
4502
4503 // An lvalue initializer list must be initializing a reference.
4504 assert(E->isTransparent() && "non-transparent glvalue init list")(static_cast <bool> (E->isTransparent() && "non-transparent glvalue init list"
) ? void (0) : __assert_fail ("E->isTransparent() && \"non-transparent glvalue init list\""
, "clang/lib/CodeGen/CGExpr.cpp", 4504, __extension__ __PRETTY_FUNCTION__
))
;
4505 return EmitLValue(E->getInit(0));
4506}
4507
4508/// Emit the operand of a glvalue conditional operator. This is either a glvalue
4509/// or a (possibly-parenthesized) throw-expression. If this is a throw, no
4510/// LValue is returned and the current block has been terminated.
4511static Optional<LValue> EmitLValueOrThrowExpression(CodeGenFunction &CGF,
4512 const Expr *Operand) {
4513 if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Operand->IgnoreParens())) {
4514 CGF.EmitCXXThrowExpr(ThrowExpr, /*KeepInsertionPoint*/false);
4515 return None;
4516 }
4517
4518 return CGF.EmitLValue(Operand);
4519}
4520
4521LValue CodeGenFunction::
4522EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) {
4523 if (!expr->isGLValue()) {
4524 // ?: here should be an aggregate.
4525 assert(hasAggregateEvaluationKind(expr->getType()) &&(static_cast <bool> (hasAggregateEvaluationKind(expr->
getType()) && "Unexpected conditional operator!") ? void
(0) : __assert_fail ("hasAggregateEvaluationKind(expr->getType()) && \"Unexpected conditional operator!\""
, "clang/lib/CodeGen/CGExpr.cpp", 4526, __extension__ __PRETTY_FUNCTION__
))
4526 "Unexpected conditional operator!")(static_cast <bool> (hasAggregateEvaluationKind(expr->
getType()) && "Unexpected conditional operator!") ? void
(0) : __assert_fail ("hasAggregateEvaluationKind(expr->getType()) && \"Unexpected conditional operator!\""
, "clang/lib/CodeGen/CGExpr.cpp", 4526, __extension__ __PRETTY_FUNCTION__
))
;
4527 return EmitAggExprToLValue(expr);
4528 }
4529
4530 OpaqueValueMapping binding(*this, expr);
4531
4532 const Expr *condExpr = expr->getCond();
4533 bool CondExprBool;
4534 if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
4535 const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr();
4536 if (!CondExprBool) std::swap(live, dead);
4537
4538 if (!ContainsLabel(dead)) {
4539 // If the true case is live, we need to track its region.
4540 if (CondExprBool)
4541 incrementProfileCounter(expr);
4542 // If a throw expression we emit it and return an undefined lvalue
4543 // because it can't be used.
4544 if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(live->IgnoreParens())) {
4545 EmitCXXThrowExpr(ThrowExpr);
4546 llvm::Type *ElemTy = ConvertType(dead->getType());
4547 llvm::Type *Ty = llvm::PointerType::getUnqual(ElemTy);
4548 return MakeAddrLValue(
4549 Address(llvm::UndefValue::get(Ty), ElemTy, CharUnits::One()),
4550 dead->getType());
4551 }
4552 return EmitLValue(live);
4553 }
4554 }
4555
4556 llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true");
4557 llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false");
4558 llvm::BasicBlock *contBlock = createBasicBlock("cond.end");
4559
4560 ConditionalEvaluation eval(*this);
4561 EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock, getProfileCount(expr));
4562
4563 // Any temporaries created here are conditional.
4564 EmitBlock(lhsBlock);
4565 incrementProfileCounter(expr);
4566 eval.begin(*this);
4567 Optional<LValue> lhs =
4568 EmitLValueOrThrowExpression(*this, expr->getTrueExpr());
4569 eval.end(*this);
4570
4571 if (lhs && !lhs->isSimple())
4572 return EmitUnsupportedLValue(expr, "conditional operator");
4573
4574 lhsBlock = Builder.GetInsertBlock();
4575 if (lhs)
4576 Builder.CreateBr(contBlock);
4577
4578 // Any temporaries created here are conditional.
4579 EmitBlock(rhsBlock);
4580 eval.begin(*this);
4581 Optional<LValue> rhs =
4582 EmitLValueOrThrowExpression(*this, expr->getFalseExpr());
4583 eval.end(*this);
4584 if (rhs && !rhs->isSimple())
4585 return EmitUnsupportedLValue(expr, "conditional operator");
4586 rhsBlock = Builder.GetInsertBlock();
4587
4588 EmitBlock(contBlock);
4589
4590 if (lhs && rhs) {
4591 Address lhsAddr = lhs->getAddress(*this);
4592 Address rhsAddr = rhs->getAddress(*this);
4593 llvm::PHINode *phi = Builder.CreatePHI(lhsAddr.getType(), 2, "cond-lvalue");
4594 phi->addIncoming(lhsAddr.getPointer(), lhsBlock);
4595 phi->addIncoming(rhsAddr.getPointer(), rhsBlock);
4596 Address result(phi, lhsAddr.getElementType(),
4597 std::min(lhsAddr.getAlignment(), rhsAddr.getAlignment()));
4598 AlignmentSource alignSource =
4599 std::max(lhs->getBaseInfo().getAlignmentSource(),
4600 rhs->getBaseInfo().getAlignmentSource());
4601 TBAAAccessInfo TBAAInfo = CGM.mergeTBAAInfoForConditionalOperator(
4602 lhs->getTBAAInfo(), rhs->getTBAAInfo());
4603 return MakeAddrLValue(result, expr->getType(), LValueBaseInfo(alignSource),
4604 TBAAInfo);
4605 } else {
4606 assert((lhs || rhs) &&(static_cast <bool> ((lhs || rhs) && "both operands of glvalue conditional are throw-expressions?"
) ? void (0) : __assert_fail ("(lhs || rhs) && \"both operands of glvalue conditional are throw-expressions?\""
, "clang/lib/CodeGen/CGExpr.cpp", 4607, __extension__ __PRETTY_FUNCTION__
))
4607 "both operands of glvalue conditional are throw-expressions?")(static_cast <bool> ((lhs || rhs) && "both operands of glvalue conditional are throw-expressions?"
) ? void (0) : __assert_fail ("(lhs || rhs) && \"both operands of glvalue conditional are throw-expressions?\""
, "clang/lib/CodeGen/CGExpr.cpp", 4607, __extension__ __PRETTY_FUNCTION__
))
;
4608 return lhs ? *lhs : *rhs;
4609 }
4610}
4611
4612/// EmitCastLValue - Casts are never lvalues unless that cast is to a reference
4613/// type. If the cast is to a reference, we can have the usual lvalue result,
4614/// otherwise if a cast is needed by the code generator in an lvalue context,
4615/// then it must mean that we need the address of an aggregate in order to
4616/// access one of its members. This can happen for all the reasons that casts
4617/// are permitted with aggregate result, including noop aggregate casts, and
4618/// cast from scalar to union.
4619LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
4620 switch (E->getCastKind()) {
4621 case CK_ToVoid:
4622 case CK_BitCast:
4623 case CK_LValueToRValueBitCast:
4624 case CK_ArrayToPointerDecay:
4625 case CK_FunctionToPointerDecay:
4626 case CK_NullToMemberPointer:
4627 case CK_NullToPointer:
4628 case CK_IntegralToPointer:
4629 case CK_PointerToIntegral:
4630 case CK_PointerToBoolean:
4631 case CK_VectorSplat:
4632 case CK_IntegralCast:
4633 case CK_BooleanToSignedIntegral:
4634 case CK_IntegralToBoolean:
4635 case CK_IntegralToFloating:
4636 case CK_FloatingToIntegral:
4637 case CK_FloatingToBoolean:
4638 case CK_FloatingCast:
4639 case CK_FloatingRealToComplex:
4640 case CK_FloatingComplexToReal:
4641 case CK_FloatingComplexToBoolean:
4642 case CK_FloatingComplexCast:
4643 case CK_FloatingComplexToIntegralComplex:
4644 case CK_IntegralRealToComplex:
4645 case CK_IntegralComplexToReal:
4646 case CK_IntegralComplexToBoolean:
4647 case CK_IntegralComplexCast:
4648 case CK_IntegralComplexToFloatingComplex:
4649 case CK_DerivedToBaseMemberPointer:
4650 case CK_BaseToDerivedMemberPointer:
4651 case CK_MemberPointerToBoolean:
4652 case CK_ReinterpretMemberPointer:
4653 case CK_AnyPointerToBlockPointerCast:
4654 case CK_ARCProduceObject:
4655 case CK_ARCConsumeObject:
4656 case CK_ARCReclaimReturnedObject:
4657 case CK_ARCExtendBlockObject:
4658 case CK_CopyAndAutoreleaseBlockObject:
4659 case CK_IntToOCLSampler:
4660 case CK_FloatingToFixedPoint:
4661 case CK_FixedPointToFloating:
4662 case CK_FixedPointCast:
4663 case CK_FixedPointToBoolean:
4664 case CK_FixedPointToIntegral:
4665 case CK_IntegralToFixedPoint:
4666 case CK_MatrixCast:
4667 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
4668
4669 case CK_Dependent:
4670 llvm_unreachable("dependent cast kind in IR gen!")::llvm::llvm_unreachable_internal("dependent cast kind in IR gen!"
, "clang/lib/CodeGen/CGExpr.cpp", 4670)
;
4671
4672 case CK_BuiltinFnToFnPtr:
4673 llvm_unreachable("builtin functions are handled elsewhere")::llvm::llvm_unreachable_internal("builtin functions are handled elsewhere"
, "clang/lib/CodeGen/CGExpr.cpp", 4673)
;
4674
4675 // These are never l-values; just use the aggregate emission code.
4676 case CK_NonAtomicToAtomic:
4677 case CK_AtomicToNonAtomic:
4678 return EmitAggExprToLValue(E);
4679
4680 case CK_Dynamic: {
4681 LValue LV = EmitLValue(E->getSubExpr());
4682 Address V = LV.getAddress(*this);
4683 const auto *DCE = cast<CXXDynamicCastExpr>(E);
4684 return MakeNaturalAlignAddrLValue(EmitDynamicCast(V, DCE), E->getType());
4685 }
4686
4687 case CK_ConstructorConversion:
4688 case CK_UserDefinedConversion:
4689 case CK_CPointerToObjCPointerCast:
4690 case CK_BlockPointerToObjCPointerCast:
4691 case CK_LValueToRValue:
4692 return EmitLValue(E->getSubExpr());
4693
4694 case CK_NoOp: {
4695 // CK_NoOp can model a qualification conversion, which can remove an array
4696 // bound and change the IR type.
4697 // FIXME: Once pointee types are removed from IR, remove this.
4698 LValue LV = EmitLValue(E->getSubExpr());
4699 if (LV.isSimple()) {
4700 Address V = LV.getAddress(*this);
4701 if (V.isValid()) {
4702 llvm::Type *T =
4703 ConvertTypeForMem(E->getType())
4704 ->getPointerTo(
4705 cast<llvm::PointerType>(V.getType())->getAddressSpace());
4706 if (V.getType() != T)
4707 LV.setAddress(Builder.CreateBitCast(V, T));
4708 }
4709 }
4710 return LV;
4711 }
4712
4713 case CK_UncheckedDerivedToBase:
4714 case CK_DerivedToBase: {
4715 const auto *DerivedClassTy =
4716 E->getSubExpr()->getType()->castAs<RecordType>();
4717 auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl());
4718
4719 LValue LV = EmitLValue(E->getSubExpr());
4720 Address This = LV.getAddress(*this);
4721
4722 // Perform the derived-to-base conversion
4723 Address Base = GetAddressOfBaseClass(
4724 This, DerivedClassDecl, E->path_begin(), E->path_end(),
4725 /*NullCheckValue=*/false, E->getExprLoc());
4726
4727 // TODO: Support accesses to members of base classes in TBAA. For now, we
4728 // conservatively pretend that the complete object is of the base class
4729 // type.
4730 return MakeAddrLValue(Base, E->getType(), LV.getBaseInfo(),
4731 CGM.getTBAAInfoForSubobject(LV, E->getType()));
4732 }
4733 case CK_ToUnion:
4734 return EmitAggExprToLValue(E);
4735 case CK_BaseToDerived: {
4736 const auto *DerivedClassTy = E->getType()->castAs<RecordType>();
4737 auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl());
4738
4739 LValue LV = EmitLValue(E->getSubExpr());
4740
4741 // Perform the base-to-derived conversion
4742 Address Derived = GetAddressOfDerivedClass(
4743 LV.getAddress(*this), DerivedClassDecl, E->path_begin(), E->path_end(),
4744 /*NullCheckValue=*/false);
4745
4746 // C++11 [expr.static.cast]p2: Behavior is undefined if a downcast is
4747 // performed and the object is not of the derived type.
4748 if (sanitizePerformTypeCheck())
4749 EmitTypeCheck(TCK_DowncastReference, E->getExprLoc(),
4750 Derived.getPointer(), E->getType());
4751
4752 if (SanOpts.has(SanitizerKind::CFIDerivedCast))
4753 EmitVTablePtrCheckForCast(E->getType(), Derived.getPointer(),
4754 /*MayBeNull=*/false, CFITCK_DerivedCast,
4755 E->getBeginLoc());
4756
4757 return MakeAddrLValue(Derived, E->getType(), LV.getBaseInfo(),
4758 CGM.getTBAAInfoForSubobject(LV, E->getType()));
4759 }
4760 case CK_LValueBitCast: {
4761 // This must be a reinterpret_cast (or c-style equivalent).
4762 const auto *CE = cast<ExplicitCastExpr>(E);
4763
4764 CGM.EmitExplicitCastExprType(CE, this);
4765 LValue LV = EmitLValue(E->getSubExpr());
4766 Address V = Builder.CreateBitCast(LV.getAddress(*this),
4767 ConvertType(CE->getTypeAsWritten()));
4768
4769 if (SanOpts.has(SanitizerKind::CFIUnrelatedCast))
4770 EmitVTablePtrCheckForCast(E->getType(), V.getPointer(),
4771 /*MayBeNull=*/false, CFITCK_UnrelatedCast,
4772 E->getBeginLoc());
4773
4774 return MakeAddrLValue(V, E->getType(), LV.getBaseInfo(),
4775 CGM.getTBAAInfoForSubobject(LV, E->getType()));
4776 }
4777 case CK_AddressSpaceConversion: {
4778 LValue LV = EmitLValue(E->getSubExpr());
4779 QualType DestTy = getContext().getPointerType(E->getType());
4780 llvm::Value *V = getTargetHooks().performAddrSpaceCast(
4781 *this, LV.getPointer(*this),
4782 E->getSubExpr()->getType().getAddressSpace(),
4783 E->getType().getAddressSpace(), ConvertType(DestTy));
4784 return MakeAddrLValue(Address(V, LV.getAddress(*this).getAlignment()),
4785 E->getType(), LV.getBaseInfo(), LV.getTBAAInfo());
4786 }
4787 case CK_ObjCObjectLValueCast: {
4788 LValue LV = EmitLValue(E->getSubExpr());
4789 Address V = Builder.CreateElementBitCast(LV.getAddress(*this),
4790 ConvertType(E->getType()));
4791 return MakeAddrLValue(V, E->getType(), LV.getBaseInfo(),
4792 CGM.getTBAAInfoForSubobject(LV, E->getType()));
4793 }
4794 case CK_ZeroToOCLOpaqueType:
4795 llvm_unreachable("NULL to OpenCL opaque type lvalue cast is not valid")::llvm::llvm_unreachable_internal("NULL to OpenCL opaque type lvalue cast is not valid"
, "clang/lib/CodeGen/CGExpr.cpp", 4795)
;
4796 }
4797
4798 llvm_unreachable("Unhandled lvalue cast kind?")::llvm::llvm_unreachable_internal("Unhandled lvalue cast kind?"
, "clang/lib/CodeGen/CGExpr.cpp", 4798)
;
4799}
4800
4801LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) {
4802 assert(OpaqueValueMappingData::shouldBindAsLValue(e))(static_cast <bool> (OpaqueValueMappingData::shouldBindAsLValue
(e)) ? void (0) : __assert_fail ("OpaqueValueMappingData::shouldBindAsLValue(e)"
, "clang/lib/CodeGen/CGExpr.cpp", 4802, __extension__ __PRETTY_FUNCTION__
))
;
4803 return getOrCreateOpaqueLValueMapping(e);
4804}
4805
4806LValue
4807CodeGenFunction::getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e) {
4808 assert(OpaqueValueMapping::shouldBindAsLValue(e))(static_cast <bool> (OpaqueValueMapping::shouldBindAsLValue
(e)) ? void (0) : __assert_fail ("OpaqueValueMapping::shouldBindAsLValue(e)"
, "clang/lib/CodeGen/CGExpr.cpp", 4808, __extension__ __PRETTY_FUNCTION__
))
;
4809
4810 llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
4811 it = OpaqueLValues.find(e);
4812
4813 if (it != OpaqueLValues.end())
4814 return it->second;
4815
4816 assert(e->isUnique() && "LValue for a nonunique OVE hasn't been emitted")(static_cast <bool> (e->isUnique() && "LValue for a nonunique OVE hasn't been emitted"
) ? void (0) : __assert_fail ("e->isUnique() && \"LValue for a nonunique OVE hasn't been emitted\""
, "clang/lib/CodeGen/CGExpr.cpp", 4816, __extension__ __PRETTY_FUNCTION__
))
;
4817 return EmitLValue(e->getSourceExpr());
4818}
4819
4820RValue
4821CodeGenFunction::getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e) {
4822 assert(!OpaqueValueMapping::shouldBindAsLValue(e))(static_cast <bool> (!OpaqueValueMapping::shouldBindAsLValue
(e)) ? void (0) : __assert_fail ("!OpaqueValueMapping::shouldBindAsLValue(e)"
, "clang/lib/CodeGen/CGExpr.cpp", 4822, __extension__ __PRETTY_FUNCTION__
))
;
4823
4824 llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
4825 it = OpaqueRValues.find(e);
4826
4827 if (it != OpaqueRValues.end())
4828 return it->second;
4829
4830 assert(e->isUnique() && "RValue for a nonunique OVE hasn't been emitted")(static_cast <bool> (e->isUnique() && "RValue for a nonunique OVE hasn't been emitted"
) ? void (0) : __assert_fail ("e->isUnique() && \"RValue for a nonunique OVE hasn't been emitted\""
, "clang/lib/CodeGen/CGExpr.cpp", 4830, __extension__ __PRETTY_FUNCTION__
))
;
4831 return EmitAnyExpr(e->getSourceExpr());
4832}
4833
4834RValue CodeGenFunction::EmitRValueForField(LValue LV,
4835 const FieldDecl *FD,
4836 SourceLocation Loc) {
4837 QualType FT = FD->getType();
4838 LValue FieldLV = EmitLValueForField(LV, FD);
4839 switch (getEvaluationKind(FT)) {
4840 case TEK_Complex:
4841 return RValue::getComplex(EmitLoadOfComplex(FieldLV, Loc));
4842 case TEK_Aggregate:
4843 return FieldLV.asAggregateRValue(*this);
4844 case TEK_Scalar:
4845 // This routine is used to load fields one-by-one to perform a copy, so
4846 // don't load reference fields.
4847 if (FD->getType()->isReferenceType())
4848 return RValue::get(FieldLV.getPointer(*this));
4849 // Call EmitLoadOfScalar except when the lvalue is a bitfield to emit a
4850 // primitive load.
4851 if (FieldLV.isBitField())
4852 return EmitLoadOfLValue(FieldLV, Loc);
4853 return RValue::get(EmitLoadOfScalar(FieldLV, Loc));
4854 }
4855 llvm_unreachable("bad evaluation kind")::llvm::llvm_unreachable_internal("bad evaluation kind", "clang/lib/CodeGen/CGExpr.cpp"
, 4855)
;
4856}
4857
4858//===--------------------------------------------------------------------===//
4859// Expression Emission
4860//===--------------------------------------------------------------------===//
4861
4862RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
4863 ReturnValueSlot ReturnValue) {
4864 // Builtins never have block type.
4865 if (E->getCallee()->getType()->isBlockPointerType())
4866 return EmitBlockCallExpr(E, ReturnValue);
4867
4868 if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E))
4869 return EmitCXXMemberCallExpr(CE, ReturnValue);
4870
4871 if (const auto *CE = dyn_cast<CUDAKernelCallExpr>(E))
4872 return EmitCUDAKernelCallExpr(CE, ReturnValue);
4873
4874 if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E))
4875 if (const CXXMethodDecl *MD =
4876 dyn_cast_or_null<CXXMethodDecl>(CE->getCalleeDecl()))
4877 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
4878
4879 CGCallee callee = EmitCallee(E->getCallee());
4880
4881 if (callee.isBuiltin()) {
4882 return EmitBuiltinExpr(callee.getBuiltinDecl(), callee.getBuiltinID(),
4883 E, ReturnValue);
4884 }
4885
4886 if (callee.isPseudoDestructor()) {
4887 return EmitCXXPseudoDestructorExpr(callee.getPseudoDestructorExpr());
4888 }
4889
4890 return EmitCall(E->getCallee()->getType(), callee, E, ReturnValue);
4891}
4892
4893/// Emit a CallExpr without considering whether it might be a subclass.
4894RValue CodeGenFunction::EmitSimpleCallExpr(const CallExpr *E,
4895 ReturnValueSlot ReturnValue) {
4896 CGCallee Callee = EmitCallee(E->getCallee());
4897 return EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue);
4898}
4899
4900static CGCallee EmitDirectCallee(CodeGenFunction &CGF, GlobalDecl GD) {
4901 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
4902
4903 if (auto builtinID = FD->getBuiltinID()) {
4904 std::string FDInlineName = (FD->getName() + ".inline").str();
4905 // When directing calling an inline builtin, call it through it's mangled
4906 // name to make it clear it's not the actual builtin.
4907 if (FD->isInlineBuiltinDeclaration() &&
4908 CGF.CurFn->getName() != FDInlineName) {
4909 llvm::Constant *CalleePtr = EmitFunctionDeclPointer(CGF.CGM, GD);
4910 llvm::Function *Fn = llvm::cast<llvm::Function>(CalleePtr);
4911 llvm::Module *M = Fn->getParent();
4912 llvm::Function *Clone = M->getFunction(FDInlineName);
4913 if (!Clone) {
4914 Clone = llvm::Function::Create(Fn->getFunctionType(),
4915 llvm::GlobalValue::InternalLinkage,
4916 Fn->getAddressSpace(), FDInlineName, M);
4917 Clone->addFnAttr(llvm::Attribute::AlwaysInline);
4918 }
4919 return CGCallee::forDirect(Clone, GD);
4920 }
4921
4922 // Replaceable builtins provide their own implementation of a builtin. If we
4923 // are in an inline builtin implementation, avoid trivial infinite
4924 // recursion.
4925 else
4926 return CGCallee::forBuiltin(builtinID, FD);
4927 }
4928
4929 llvm::Constant *CalleePtr = EmitFunctionDeclPointer(CGF.CGM, GD);
4930 if (CGF.CGM.getLangOpts().CUDA && !CGF.CGM.getLangOpts().CUDAIsDevice &&
4931 FD->hasAttr<CUDAGlobalAttr>())
4932 CalleePtr = CGF.CGM.getCUDARuntime().getKernelStub(
4933 cast<llvm::GlobalValue>(CalleePtr->stripPointerCasts()));
4934
4935 return CGCallee::forDirect(CalleePtr, GD);
4936}
4937
4938CGCallee CodeGenFunction::EmitCallee(const Expr *E) {
4939 E = E->IgnoreParens();
4940
4941 // Look through function-to-pointer decay.
4942 if (auto ICE = dyn_cast<ImplicitCastExpr>(E)) {
4943 if (ICE->getCastKind() == CK_FunctionToPointerDecay ||
4944 ICE->getCastKind() == CK_BuiltinFnToFnPtr) {
4945 return EmitCallee(ICE->getSubExpr());
4946 }
4947
4948 // Resolve direct calls.
4949 } else if (auto DRE = dyn_cast<DeclRefExpr>(E)) {
4950 if (auto FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
4951 return EmitDirectCallee(*this, FD);
4952 }
4953 } else if (auto ME = dyn_cast<MemberExpr>(E)) {
4954 if (auto FD = dyn_cast<FunctionDecl>(ME->getMemberDecl())) {
4955 EmitIgnoredExpr(ME->getBase());
4956 return EmitDirectCallee(*this, FD);
4957 }
4958
4959 // Look through template substitutions.
4960 } else if (auto NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
4961 return EmitCallee(NTTP->getReplacement());
4962
4963 // Treat pseudo-destructor calls differently.
4964 } else if (auto PDE = dyn_cast<CXXPseudoDestructorExpr>(E)) {
4965 return CGCallee::forPseudoDestructor(PDE);
4966 }
4967
4968 // Otherwise, we have an indirect reference.
4969 llvm::Value *calleePtr;
4970 QualType functionType;
4971 if (auto ptrType = E->getType()->getAs<PointerType>()) {
4972 calleePtr = EmitScalarExpr(E);
4973 functionType = ptrType->getPointeeType();
4974 } else {
4975 functionType = E->getType();
4976 calleePtr = EmitLValue(E).getPointer(*this);
4977 }
4978 assert(functionType->isFunctionType())(static_cast <bool> (functionType->isFunctionType())
? void (0) : __assert_fail ("functionType->isFunctionType()"
, "clang/lib/CodeGen/CGExpr.cpp", 4978, __extension__ __PRETTY_FUNCTION__
))
;
4979
4980 GlobalDecl GD;
4981 if (const auto *VD =
4982 dyn_cast_or_null<VarDecl>(E->getReferencedDeclOfCallee()))
4983 GD = GlobalDecl(VD);
4984
4985 CGCalleeInfo calleeInfo(functionType->getAs<FunctionProtoType>(), GD);
4986 CGCallee callee(calleeInfo, calleePtr);
4987 return callee;
4988}
4989
4990LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
4991 // Comma expressions just emit their LHS then their RHS as an l-value.
4992 if (E->getOpcode() == BO_Comma) {
4993 EmitIgnoredExpr(E->getLHS());
4994 EnsureInsertPoint();
4995 return EmitLValue(E->getRHS());
4996 }
4997
4998 if (E->getOpcode() == BO_PtrMemD ||
4999 E->getOpcode() == BO_PtrMemI)
5000 return EmitPointerToDataMemberBinaryExpr(E);
5001
5002 assert(E->getOpcode() == BO_Assign && "unexpected binary l-value")(static_cast <bool> (E->getOpcode() == BO_Assign &&
"unexpected binary l-value") ? void (0) : __assert_fail ("E->getOpcode() == BO_Assign && \"unexpected binary l-value\""
, "clang/lib/CodeGen/CGExpr.cpp", 5002, __extension__ __PRETTY_FUNCTION__
))
;
5003
5004 // Note that in all of these cases, __block variables need the RHS
5005 // evaluated first just in case the variable gets moved by the RHS.
5006
5007 switch (getEvaluationKind(E->getType())) {
5008 case TEK_Scalar: {
5009 switch (E->getLHS()->getType().getObjCLifetime()) {
5010 case Qualifiers::OCL_Strong:
5011 return EmitARCStoreStrong(E, /*ignored*/ false).first;
5012
5013 case Qualifiers::OCL_Autoreleasing:
5014 return EmitARCStoreAutoreleasing(E).first;
5015
5016 // No reason to do any of these differently.
5017 case Qualifiers::OCL_None:
5018 case Qualifiers::OCL_ExplicitNone:
5019 case Qualifiers::OCL_Weak:
5020 break;
5021 }
5022
5023 RValue RV = EmitAnyExpr(E->getRHS());
5024 LValue LV = EmitCheckedLValue(E->getLHS(), TCK_Store);
5025 if (RV.isScalar())
5026 EmitNullabilityCheck(LV, RV.getScalarVal(), E->getExprLoc());
5027 EmitStoreThroughLValue(RV, LV);
5028 if (getLangOpts().OpenMP)
5029 CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(*this,
5030 E->getLHS());
5031 return LV;
5032 }
5033
5034 case TEK_Complex:
5035 return EmitComplexAssignmentLValue(E);
5036
5037 case TEK_Aggregate:
5038 return EmitAggExprToLValue(E);
5039 }
5040 llvm_unreachable("bad evaluation kind")::llvm::llvm_unreachable_internal("bad evaluation kind", "clang/lib/CodeGen/CGExpr.cpp"
, 5040)
;
5041}
5042
5043LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
5044 RValue RV = EmitCallExpr(E);
5045
5046 if (!RV.isScalar())
5047 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(),
5048 AlignmentSource::Decl);
5049
5050 assert(E->getCallReturnType(getContext())->isReferenceType() &&(static_cast <bool> (E->getCallReturnType(getContext
())->isReferenceType() && "Can't have a scalar return unless the return type is a "
"reference type!") ? void (0) : __assert_fail ("E->getCallReturnType(getContext())->isReferenceType() && \"Can't have a scalar return unless the return type is a \" \"reference type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 5052, __extension__ __PRETTY_FUNCTION__
))
5051 "Can't have a scalar return unless the return type is a "(static_cast <bool> (E->getCallReturnType(getContext
())->isReferenceType() && "Can't have a scalar return unless the return type is a "
"reference type!") ? void (0) : __assert_fail ("E->getCallReturnType(getContext())->isReferenceType() && \"Can't have a scalar return unless the return type is a \" \"reference type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 5052, __extension__ __PRETTY_FUNCTION__
))
5052 "reference type!")(static_cast <bool> (E->getCallReturnType(getContext
())->isReferenceType() && "Can't have a scalar return unless the return type is a "
"reference type!") ? void (0) : __assert_fail ("E->getCallReturnType(getContext())->isReferenceType() && \"Can't have a scalar return unless the return type is a \" \"reference type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 5052, __extension__ __PRETTY_FUNCTION__
))
;
5053
5054 return MakeNaturalAlignPointeeAddrLValue(RV.getScalarVal(), E->getType());
5055}
5056
5057LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
5058 // FIXME: This shouldn't require another copy.
5059 return EmitAggExprToLValue(E);
5060}
5061
5062LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
5063 assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor()(static_cast <bool> (E->getType()->getAsCXXRecordDecl
()->hasTrivialDestructor() && "binding l-value to type which needs a temporary"
) ? void (0) : __assert_fail ("E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor() && \"binding l-value to type which needs a temporary\""
, "clang/lib/CodeGen/CGExpr.cpp", 5064, __extension__ __PRETTY_FUNCTION__
))
5064 && "binding l-value to type which needs a temporary")(static_cast <bool> (E->getType()->getAsCXXRecordDecl
()->hasTrivialDestructor() && "binding l-value to type which needs a temporary"
) ? void (0) : __assert_fail ("E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor() && \"binding l-value to type which needs a temporary\""
, "clang/lib/CodeGen/CGExpr.cpp", 5064, __extension__ __PRETTY_FUNCTION__
))
;
5065 AggValueSlot Slot = CreateAggTemp(E->getType());
5066 EmitCXXConstructExpr(E, Slot);
5067 return MakeAddrLValue(Slot.getAddress(), E->getType(), AlignmentSource::Decl);
5068}
5069
5070LValue
5071CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
5072 return MakeNaturalAlignAddrLValue(EmitCXXTypeidExpr(E), E->getType());
5073}
5074
5075Address CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) {
5076 return Builder.CreateElementBitCast(CGM.GetAddrOfMSGuidDecl(E->getGuidDecl()),
5077 ConvertType(E->getType()));
5078}
5079
5080LValue CodeGenFunction::EmitCXXUuidofLValue(const CXXUuidofExpr *E) {
5081 return MakeAddrLValue(EmitCXXUuidofExpr(E), E->getType(),
5082 AlignmentSource::Decl);
5083}
5084
5085LValue
5086CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
5087 AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
5088 Slot.setExternallyDestructed();
5089 EmitAggExpr(E->getSubExpr(), Slot);
5090 EmitCXXTemporary(E->getTemporary(), E->getType(), Slot.getAddress());
5091 return MakeAddrLValue(Slot.getAddress(), E->getType(), AlignmentSource::Decl);
5092}
5093
5094LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
5095 RValue RV = EmitObjCMessageExpr(E);
5096
5097 if (!RV.isScalar())
5098 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(),
5099 AlignmentSource::Decl);
5100
5101 assert(E->getMethodDecl()->getReturnType()->isReferenceType() &&(static_cast <bool> (E->getMethodDecl()->getReturnType
()->isReferenceType() && "Can't have a scalar return unless the return type is a "
"reference type!") ? void (0) : __assert_fail ("E->getMethodDecl()->getReturnType()->isReferenceType() && \"Can't have a scalar return unless the return type is a \" \"reference type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 5103, __extension__ __PRETTY_FUNCTION__
))
5102 "Can't have a scalar return unless the return type is a "(static_cast <bool> (E->getMethodDecl()->getReturnType
()->isReferenceType() && "Can't have a scalar return unless the return type is a "
"reference type!") ? void (0) : __assert_fail ("E->getMethodDecl()->getReturnType()->isReferenceType() && \"Can't have a scalar return unless the return type is a \" \"reference type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 5103, __extension__ __PRETTY_FUNCTION__
))
5103 "reference type!")(static_cast <bool> (E->getMethodDecl()->getReturnType
()->isReferenceType() && "Can't have a scalar return unless the return type is a "
"reference type!") ? void (0) : __assert_fail ("E->getMethodDecl()->getReturnType()->isReferenceType() && \"Can't have a scalar return unless the return type is a \" \"reference type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 5103, __extension__ __PRETTY_FUNCTION__
))
;
5104
5105 return MakeNaturalAlignPointeeAddrLValue(RV.getScalarVal(), E->getType());
5106}
5107
5108LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) {
5109 Address V =
5110 CGM.getObjCRuntime().GetAddrOfSelector(*this, E->getSelector());
5111 return MakeAddrLValue(V, E->getType(), AlignmentSource::Decl);
5112}
5113
5114llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
5115 const ObjCIvarDecl *Ivar) {
5116 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
5117}
5118
5119LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
5120 llvm::Value *BaseValue,
5121 const ObjCIvarDecl *Ivar,
5122 unsigned CVRQualifiers) {
5123 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
5124 Ivar, CVRQualifiers);
5125}
5126
5127LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
5128 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
5129 llvm::Value *BaseValue = nullptr;
5130 const Expr *BaseExpr = E->getBase();
5131 Qualifiers BaseQuals;
5132 QualType ObjectTy;
5133 if (E->isArrow()) {
5134 BaseValue = EmitScalarExpr(BaseExpr);
5135 ObjectTy = BaseExpr->getType()->getPointeeType();
5136 BaseQuals = ObjectTy.getQualifiers();
5137 } else {
5138 LValue BaseLV = EmitLValue(BaseExpr);
5139 BaseValue = BaseLV.getPointer(*this);
5140 ObjectTy = BaseExpr->getType();
5141 BaseQuals = ObjectTy.getQualifiers();
5142 }
5143
5144 LValue LV =
5145 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
5146 BaseQuals.getCVRQualifiers());
5147 setObjCGCLValueClass(getContext(), E, LV);
5148 return LV;
5149}
5150
5151LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
5152 // Can only get l-value for message expression returning aggregate type
5153 RValue RV = EmitAnyExprToTemp(E);
5154 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(),
5155 AlignmentSource::Decl);
5156}
5157
5158RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee,
5159 const CallExpr *E, ReturnValueSlot ReturnValue,
5160 llvm::Value *Chain) {
5161 // Get the actual function type. The callee type will always be a pointer to
5162 // function type or a block pointer type.
5163 assert(CalleeType->isFunctionPointerType() &&(static_cast <bool> (CalleeType->isFunctionPointerType
() && "Call must have function pointer type!") ? void
(0) : __assert_fail ("CalleeType->isFunctionPointerType() && \"Call must have function pointer type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 5164, __extension__ __PRETTY_FUNCTION__
))
5164 "Call must have function pointer type!")(static_cast <bool> (CalleeType->isFunctionPointerType
() && "Call must have function pointer type!") ? void
(0) : __assert_fail ("CalleeType->isFunctionPointerType() && \"Call must have function pointer type!\""
, "clang/lib/CodeGen/CGExpr.cpp", 5164, __extension__ __PRETTY_FUNCTION__
))
;
5165
5166 const Decl *TargetDecl =
5167 OrigCallee.getAbstractInfo().getCalleeDecl().getDecl();
5168
5169 CalleeType = getContext().getCanonicalType(CalleeType);
5170
5171 auto PointeeType = cast<PointerType>(CalleeType)->getPointeeType();
5172
5173 CGCallee Callee = OrigCallee;
5174
5175 if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function) &&
5176 (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
5177 if (llvm::Constant *PrefixSig =
5178 CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) {
5179 SanitizerScope SanScope(this);
5180 // Remove any (C++17) exception specifications, to allow calling e.g. a
5181 // noexcept function through a non-noexcept pointer.
5182 auto ProtoTy =
5183 getContext().getFunctionTypeWithExceptionSpec(PointeeType, EST_None);
5184 llvm::Constant *FTRTTIConst =
5185 CGM.GetAddrOfRTTIDescriptor(ProtoTy, /*ForEH=*/true);
5186 llvm::Type *PrefixSigType = PrefixSig->getType();
5187 llvm::StructType *PrefixStructTy = llvm::StructType::get(
5188 CGM.getLLVMContext(), {PrefixSigType, Int32Ty}, /*isPacked=*/true);
5189
5190 llvm::Value *CalleePtr = Callee.getFunctionPointer();
5191
5192 llvm::Value *CalleePrefixStruct = Builder.CreateBitCast(
5193 CalleePtr, llvm::PointerType::getUnqual(PrefixStructTy));
5194 llvm::Value *CalleeSigPtr =
5195 Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, 0, 0);
5196 llvm::Value *CalleeSig =
5197 Builder.CreateAlignedLoad(PrefixSigType, CalleeSigPtr, getIntAlign());
5198 llvm::Value *CalleeSigMatch = Builder.CreateICmpEQ(CalleeSig, PrefixSig);
5199
5200 llvm::BasicBlock *Cont = createBasicBlock("cont");
5201 llvm::BasicBlock *TypeCheck = createBasicBlock("typecheck");
5202 Builder.CreateCondBr(CalleeSigMatch, TypeCheck, Cont);
5203
5204 EmitBlock(TypeCheck);
5205 llvm::Value *CalleeRTTIPtr =
5206 Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, 0, 1);
5207 llvm::Value *CalleeRTTIEncoded =
5208 Builder.CreateAlignedLoad(Int32Ty, CalleeRTTIPtr, getPointerAlign());
5209 llvm::Value *CalleeRTTI =
5210 DecodeAddrUsedInPrologue(CalleePtr, CalleeRTTIEncoded);
5211 llvm::Value *CalleeRTTIMatch =
5212 Builder.CreateICmpEQ(CalleeRTTI, FTRTTIConst);
5213 llvm::Constant *StaticData[] = {EmitCheckSourceLocation(E->getBeginLoc()),
5214 EmitCheckTypeDescriptor(CalleeType)};
5215 EmitCheck(std::make_pair(CalleeRTTIMatch, SanitizerKind::Function),
5216 SanitizerHandler::FunctionTypeMismatch, StaticData,
5217 {CalleePtr, CalleeRTTI, FTRTTIConst});
5218
5219 Builder.CreateBr(Cont);
5220 EmitBlock(Cont);
5221 }
5222 }
5223
5224 const auto *FnType = cast<FunctionType>(PointeeType);
5225
5226 // If we are checking indirect calls and this call is indirect, check that the
5227 // function pointer is a member of the bit set for the function type.
5228 if (SanOpts.has(SanitizerKind::CFIICall) &&
5229 (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
5230 SanitizerScope SanScope(this);
5231 EmitSanitizerStatReport(llvm::SanStat_CFI_ICall);
5232
5233 llvm::Metadata *MD;
5234 if (CGM.getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
5235 MD = CGM.CreateMetadataIdentifierGeneralized(QualType(FnType, 0));
5236 else
5237 MD = CGM.CreateMetadataIdentifierForType(QualType(FnType, 0));
5238
5239 llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD);
5240
5241 llvm::Value *CalleePtr = Callee.getFunctionPointer();
5242 llvm::Value *CastedCallee = Builder.CreateBitCast(CalleePtr, Int8PtrTy);
5243 llvm::Value *TypeTest = Builder.CreateCall(
5244 CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedCallee, TypeId});
5245
5246 auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD);
5247 llvm::Constant *StaticData[] = {
5248 llvm::ConstantInt::get(Int8Ty, CFITCK_ICall),
5249 EmitCheckSourceLocation(E->getBeginLoc()),
5250 EmitCheckTypeDescriptor(QualType(FnType, 0)),
5251 };
5252 if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) {
5253 EmitCfiSlowPathCheck(SanitizerKind::CFIICall, TypeTest, CrossDsoTypeId,
5254 CastedCallee, StaticData);
5255 } else {
5256 EmitCheck(std::make_pair(TypeTest, SanitizerKind::CFIICall),
5257 SanitizerHandler::CFICheckFail, StaticData,
5258 {CastedCallee, llvm::UndefValue::get(IntPtrTy)});
5259 }
5260 }
5261
5262 CallArgList Args;
5263 if (Chain)
5264 Args.add(RValue::get(Builder.CreateBitCast(Chain, CGM.VoidPtrTy)),
5265 CGM.getContext().VoidPtrTy);
5266
5267 // C++17 requires that we evaluate arguments to a call using assignment syntax
5268 // right-to-left, and that we evaluate arguments to certain other operators
5269 // left-to-right. Note that we allow this to override the order dictated by
5270 // the calling convention on the MS ABI, which means that parameter
5271 // destruction order is not necessarily reverse construction order.
5272 // FIXME: Revisit this based on C++ committee response to unimplementability.
5273 EvaluationOrder Order = EvaluationOrder::Default;
5274 if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
5275 if (OCE->isAssignmentOp())
5276 Order = EvaluationOrder::ForceRightToLeft;
5277 else {
5278 switch (OCE->getOperator()) {
5279 case OO_LessLess:
5280 case OO_GreaterGreater:
5281 case OO_AmpAmp:
5282 case OO_PipePipe:
5283 case OO_Comma:
5284 case OO_ArrowStar:
5285 Order = EvaluationOrder::ForceLeftToRight;
5286 break;
5287 default:
5288 break;
5289 }
5290 }
5291 }
5292
5293 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), E->arguments(),
5294 E->getDirectCallee(), /*ParamsToSkip*/ 0, Order);
5295
5296 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeFreeFunctionCall(
5297 Args, FnType, /*ChainCall=*/Chain);
5298
5299 // C99 6.5.2.2p6:
5300 // If the expression that denotes the called function has a type
5301 // that does not include a prototype, [the default argument
5302 // promotions are performed]. If the number of arguments does not
5303 // equal the number of parameters, the behavior is undefined. If
5304 // the function is defined with a type that includes a prototype,
5305 // and either the prototype ends with an ellipsis (, ...) or the
5306 // types of the arguments after promotion are not compatible with
5307 // the types of the parameters, the behavior is undefined. If the
5308 // function is defined with a type that does not include a
5309 // prototype, and the types of the arguments after promotion are
5310 // not compatible with those of the parameters after promotion,
5311 // the behavior is undefined [except in some trivial cases].
5312 // That is, in the general case, we should assume that a call
5313 // through an unprototyped function type works like a *non-variadic*
5314 // call. The way we make this work is to cast to the exact type
5315 // of the promoted arguments.
5316 //
5317 // Chain calls use this same code path to add the invisible chain parameter
5318 // to the function type.
5319 if (isa<FunctionNoProtoType>(FnType) || Chain) {
5320 llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo);
5321 int AS = Callee.getFunctionPointer()->getType()->getPointerAddressSpace();
5322 CalleeTy = CalleeTy->getPointerTo(AS);
5323
5324 llvm::Value *CalleePtr = Callee.getFunctionPointer();
5325 CalleePtr = Builder.CreateBitCast(CalleePtr, CalleeTy, "callee.knr.cast");
5326 Callee.setFunctionPointer(CalleePtr);
5327 }
5328
5329 // HIP function pointer contains kernel handle when it is used in triple
5330 // chevron. The kernel stub needs to be loaded from kernel handle and used
5331 // as callee.
5332 if (CGM.getLangOpts().HIP && !CGM.getLangOpts().CUDAIsDevice &&
5333 isa<CUDAKernelCallExpr>(E) &&
5334 (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
5335 llvm::Value *Handle = Callee.getFunctionPointer();
5336 auto *Cast =
5337 Builder.CreateBitCast(Handle, Handle->getType()->getPointerTo());
5338 auto *Stub = Builder.CreateLoad(Address(Cast, CGM.getPointerAlign()));
5339 Callee.setFunctionPointer(Stub);
5340 }
5341 llvm::CallBase *CallOrInvoke = nullptr;
5342 RValue Call = EmitCall(FnInfo, Callee, ReturnValue, Args, &CallOrInvoke,
5343 E == MustTailCall, E->getExprLoc());
5344
5345 // Generate function declaration DISuprogram in order to be used
5346 // in debug info about call sites.
5347 if (CGDebugInfo *DI = getDebugInfo()) {
5348 if (auto *CalleeDecl = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
5349 FunctionArgList Args;
5350 QualType ResTy = BuildFunctionArgList(CalleeDecl, Args);
5351 DI->EmitFuncDeclForCallSite(CallOrInvoke,
5352 DI->getFunctionType(CalleeDecl, ResTy, Args),
5353 CalleeDecl);
5354 }
5355 }
5356
5357 return Call;
5358}
5359
5360LValue CodeGenFunction::
5361EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
5362 Address BaseAddr = Address::invalid();
5363 if (E->getOpcode() == BO_PtrMemI) {
5364 BaseAddr = EmitPointerWithAlignment(E->getLHS());
5365 } else {
5366 BaseAddr = EmitLValue(E->getLHS()).getAddress(*this);
5367 }
5368
5369 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
5370 const auto *MPT = E->getRHS()->getType()->castAs<MemberPointerType>();
5371
5372 LValueBaseInfo BaseInfo;
5373 TBAAAccessInfo TBAAInfo;
5374 Address MemberAddr =
5375 EmitCXXMemberDataPointerAddress(E, BaseAddr, OffsetV, MPT, &BaseInfo,
5376 &TBAAInfo);
5377
5378 return MakeAddrLValue(MemberAddr, MPT->getPointeeType(), BaseInfo, TBAAInfo);
5379}
5380
5381/// Given the address of a temporary variable, produce an r-value of
5382/// its type.
5383RValue CodeGenFunction::convertTempToRValue(Address addr,
5384 QualType type,
5385 SourceLocation loc) {
5386 LValue lvalue = MakeAddrLValue(addr, type, AlignmentSource::Decl);
5387 switch (getEvaluationKind(type)) {
5388 case TEK_Complex:
5389 return RValue::getComplex(EmitLoadOfComplex(lvalue, loc));
5390 case TEK_Aggregate:
5391 return lvalue.asAggregateRValue(*this);
5392 case TEK_Scalar:
5393 return RValue::get(EmitLoadOfScalar(lvalue, loc));
5394 }
5395 llvm_unreachable("bad evaluation kind")::llvm::llvm_unreachable_internal("bad evaluation kind", "clang/lib/CodeGen/CGExpr.cpp"
, 5395)
;
5396}
5397
5398void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) {
5399 assert(Val->getType()->isFPOrFPVectorTy())(static_cast <bool> (Val->getType()->isFPOrFPVectorTy
()) ? void (0) : __assert_fail ("Val->getType()->isFPOrFPVectorTy()"
, "clang/lib/CodeGen/CGExpr.cpp", 5399, __extension__ __PRETTY_FUNCTION__
))
;
5400 if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val))
5401 return;
5402
5403 llvm::MDBuilder MDHelper(getLLVMContext());
5404 llvm::MDNode *Node = MDHelper.createFPMath(Accuracy);
5405
5406 cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node);
5407}
5408
5409namespace {
5410 struct LValueOrRValue {
5411 LValue LV;
5412 RValue RV;
5413 };
5414}
5415
5416static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF,
5417 const PseudoObjectExpr *E,
5418 bool forLValue,
5419 AggValueSlot slot) {
5420 SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
5421
5422 // Find the result expression, if any.
5423 const Expr *resultExpr = E->getResultExpr();
5424 LValueOrRValue result;
5425
5426 for (PseudoObjectExpr::const_semantics_iterator
5427 i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
5428 const Expr *semantic = *i;
5429
5430 // If this semantic expression is an opaque value, bind it
5431 // to the result of its source expression.
5432 if (const auto *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
5433 // Skip unique OVEs.
5434 if (ov->isUnique()) {
5435 assert(ov != resultExpr &&(static_cast <bool> (ov != resultExpr && "A unique OVE cannot be used as the result expression"
) ? void (0) : __assert_fail ("ov != resultExpr && \"A unique OVE cannot be used as the result expression\""
, "clang/lib/CodeGen/CGExpr.cpp", 5436, __extension__ __PRETTY_FUNCTION__
))
5436 "A unique OVE cannot be used as the result expression")(static_cast <bool> (ov != resultExpr && "A unique OVE cannot be used as the result expression"
) ? void (0) : __assert_fail ("ov != resultExpr && \"A unique OVE cannot be used as the result expression\""
, "clang/lib/CodeGen/CGExpr.cpp", 5436, __extension__ __PRETTY_FUNCTION__
))
;
5437 continue;
5438 }
5439
5440 // If this is the result expression, we may need to evaluate
5441 // directly into the slot.
5442 typedef CodeGenFunction::OpaqueValueMappingData OVMA;
5443 OVMA opaqueData;
5444 if (ov == resultExpr && ov->isPRValue() && !forLValue &&
5445 CodeGenFunction::hasAggregateEvaluationKind(ov->getType())) {
5446 CGF.EmitAggExpr(ov->getSourceExpr(), slot);
5447 LValue LV = CGF.MakeAddrLValue(slot.getAddress(), ov->getType(),
5448 AlignmentSource::Decl);
5449 opaqueData = OVMA::bind(CGF, ov, LV);
5450 result.RV = slot.asRValue();
5451
5452 // Otherwise, emit as normal.
5453 } else {
5454 opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
5455
5456 // If this is the result, also evaluate the result now.
5457 if (ov == resultExpr) {
5458 if (forLValue)
5459 result.LV = CGF.EmitLValue(ov);
5460 else
5461 result.RV = CGF.EmitAnyExpr(ov, slot);
5462 }
5463 }
5464
5465 opaques.push_back(opaqueData);
5466
5467 // Otherwise, if the expression is the result, evaluate it
5468 // and remember the result.
5469 } else if (semantic == resultExpr) {
5470 if (forLValue)
5471 result.LV = CGF.EmitLValue(semantic);
5472 else
5473 result.RV = CGF.EmitAnyExpr(semantic, slot);
5474
5475 // Otherwise, evaluate the expression in an ignored context.
5476 } else {
5477 CGF.EmitIgnoredExpr(semantic);
5478 }
5479 }
5480
5481 // Unbind all the opaques now.
5482 for (unsigned i = 0, e = opaques.size(); i != e; ++i)
5483 opaques[i].unbind(CGF);
5484
5485 return result;
5486}
5487
5488RValue CodeGenFunction::EmitPseudoObjectRValue(const PseudoObjectExpr *E,
5489 AggValueSlot slot) {
5490 return emitPseudoObjectExpr(*this, E, false, slot).RV;
5491}
5492
5493LValue CodeGenFunction::EmitPseudoObjectLValue(const PseudoObjectExpr *E) {
5494 return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV;
5495}

/build/llvm-toolchain-snapshot-14~++20220108111521+9345ab3a4550/clang/lib/CodeGen/CGValue.h

1//===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// These classes implement wrappers around llvm::Value in order to
10// fully represent the range of values for C L- and R- values.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
15#define LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
16
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/Type.h"
19#include "llvm/IR/Value.h"
20#include "llvm/IR/Type.h"
21#include "Address.h"
22#include "CodeGenTBAA.h"
23
24namespace llvm {
25 class Constant;
26 class MDNode;
27}
28
29namespace clang {
30namespace CodeGen {
31 class AggValueSlot;
32 class CodeGenFunction;
33 struct CGBitFieldInfo;
34
35/// RValue - This trivial value class is used to represent the result of an
36/// expression that is evaluated. It can be one of three things: either a
37/// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
38/// address of an aggregate value in memory.
39class RValue {
40 enum Flavor { Scalar, Complex, Aggregate };
41
42 // The shift to make to an aggregate's alignment to make it look
43 // like a pointer.
44 enum { AggAlignShift = 4 };
45
46 // Stores first value and flavor.
47 llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
48 // Stores second value and volatility.
49 llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
50 // Stores element type for aggregate values.
51 llvm::Type *ElementType;
52
53public:
54 bool isScalar() const { return V1.getInt() == Scalar; }
55 bool isComplex() const { return V1.getInt() == Complex; }
56 bool isAggregate() const { return V1.getInt() == Aggregate; }
57
58 bool isVolatileQualified() const { return V2.getInt(); }
59
60 /// getScalarVal() - Return the Value* of this scalar value.
61 llvm::Value *getScalarVal() const {
62 assert(isScalar() && "Not a scalar!")(static_cast <bool> (isScalar() && "Not a scalar!"
) ? void (0) : __assert_fail ("isScalar() && \"Not a scalar!\""
, "clang/lib/CodeGen/CGValue.h", 62, __extension__ __PRETTY_FUNCTION__
))
;
63 return V1.getPointer();
64 }
65
66 /// getComplexVal - Return the real/imag components of this complex value.
67 ///
68 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
69 return std::make_pair(V1.getPointer(), V2.getPointer());
70 }
71
72 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
73 Address getAggregateAddress() const {
74 assert(isAggregate() && "Not an aggregate!")(static_cast <bool> (isAggregate() && "Not an aggregate!"
) ? void (0) : __assert_fail ("isAggregate() && \"Not an aggregate!\""
, "clang/lib/CodeGen/CGValue.h", 74, __extension__ __PRETTY_FUNCTION__
))
;
75 auto align = reinterpret_cast<uintptr_t>(V2.getPointer()) >> AggAlignShift;
76 return Address(
77 V1.getPointer(), ElementType, CharUnits::fromQuantity(align));
78 }
79 llvm::Value *getAggregatePointer() const {
80 assert(isAggregate() && "Not an aggregate!")(static_cast <bool> (isAggregate() && "Not an aggregate!"
) ? void (0) : __assert_fail ("isAggregate() && \"Not an aggregate!\""
, "clang/lib/CodeGen/CGValue.h", 80, __extension__ __PRETTY_FUNCTION__
))
;
81 return V1.getPointer();
82 }
83
84 static RValue getIgnored() {
85 // FIXME: should we make this a more explicit state?
86 return get(nullptr);
87 }
88
89 static RValue get(llvm::Value *V) {
90 RValue ER;
91 ER.V1.setPointer(V);
92 ER.V1.setInt(Scalar);
93 ER.V2.setInt(false);
94 return ER;
95 }
96 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
97 RValue ER;
98 ER.V1.setPointer(V1);
99 ER.V2.setPointer(V2);
100 ER.V1.setInt(Complex);
101 ER.V2.setInt(false);
102 return ER;
103 }
104 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
105 return getComplex(C.first, C.second);
106 }
107 // FIXME: Aggregate rvalues need to retain information about whether they are
108 // volatile or not. Remove default to find all places that probably get this
109 // wrong.
110 static RValue getAggregate(Address addr, bool isVolatile = false) {
111 RValue ER;
112 ER.V1.setPointer(addr.getPointer());
113 ER.V1.setInt(Aggregate);
114 ER.ElementType = addr.getElementType();
115
116 auto align = static_cast<uintptr_t>(addr.getAlignment().getQuantity());
117 ER.V2.setPointer(reinterpret_cast<llvm::Value*>(align << AggAlignShift));
118 ER.V2.setInt(isVolatile);
119 return ER;
120 }
121};
122
123/// Does an ARC strong l-value have precise lifetime?
124enum ARCPreciseLifetime_t {
125 ARCImpreciseLifetime, ARCPreciseLifetime
126};
127
128/// The source of the alignment of an l-value; an expression of
129/// confidence in the alignment actually matching the estimate.
130enum class AlignmentSource {
131 /// The l-value was an access to a declared entity or something
132 /// equivalently strong, like the address of an array allocated by a
133 /// language runtime.
134 Decl,
135
136 /// The l-value was considered opaque, so the alignment was
137 /// determined from a type, but that type was an explicitly-aligned
138 /// typedef.
139 AttributedType,
140
141 /// The l-value was considered opaque, so the alignment was
142 /// determined from a type.
143 Type
144};
145
146/// Given that the base address has the given alignment source, what's
147/// our confidence in the alignment of the field?
148static inline AlignmentSource getFieldAlignmentSource(AlignmentSource Source) {
149 // For now, we don't distinguish fields of opaque pointers from
150 // top-level declarations, but maybe we should.
151 return AlignmentSource::Decl;
152}
153
154class LValueBaseInfo {
155 AlignmentSource AlignSource;
156
157public:
158 explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type)
159 : AlignSource(Source) {}
160 AlignmentSource getAlignmentSource() const { return AlignSource; }
161 void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; }
162
163 void mergeForCast(const LValueBaseInfo &Info) {
164 setAlignmentSource(Info.getAlignmentSource());
165 }
166};
167
168/// LValue - This represents an lvalue references. Because C/C++ allow
169/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
170/// bitrange.
171class LValue {
172 enum {
173 Simple, // This is a normal l-value, use getAddress().
174 VectorElt, // This is a vector element l-value (V[i]), use getVector*
175 BitField, // This is a bitfield l-value, use getBitfield*.
176 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
177 GlobalReg, // This is a register l-value, use getGlobalReg()
178 MatrixElt // This is a matrix element, use getVector*
179 } LVType;
180
181 llvm::Value *V;
182 llvm::Type *ElementType;
183
184 union {
185 // Index into a vector subscript: V[i]
186 llvm::Value *VectorIdx;
187
188 // ExtVector element subset: V.xyx
189 llvm::Constant *VectorElts;
190
191 // BitField start bit and size
192 const CGBitFieldInfo *BitFieldInfo;
193 };
194
195 QualType Type;
196
197 // 'const' is unused here
198 Qualifiers Quals;
199
200 // The alignment to use when accessing this lvalue. (For vector elements,
201 // this is the alignment of the whole vector.)
202 unsigned Alignment;
203
204 // objective-c's ivar
205 bool Ivar:1;
206
207 // objective-c's ivar is an array
208 bool ObjIsArray:1;
209
210 // LValue is non-gc'able for any reason, including being a parameter or local
211 // variable.
212 bool NonGC: 1;
213
214 // Lvalue is a global reference of an objective-c object
215 bool GlobalObjCRef : 1;
216
217 // Lvalue is a thread local reference
218 bool ThreadLocalRef : 1;
219
220 // Lvalue has ARC imprecise lifetime. We store this inverted to try
221 // to make the default bitfield pattern all-zeroes.
222 bool ImpreciseLifetime : 1;
223
224 // This flag shows if a nontemporal load/stores should be used when accessing
225 // this lvalue.
226 bool Nontemporal : 1;
227
228 LValueBaseInfo BaseInfo;
229 TBAAAccessInfo TBAAInfo;
230
231 Expr *BaseIvarExp;
232
233private:
234 void Initialize(QualType Type, Qualifiers Quals, CharUnits Alignment,
235 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
236 assert((!Alignment.isZero() || Type->isIncompleteType()) &&(static_cast <bool> ((!Alignment.isZero() || Type->isIncompleteType
()) && "initializing l-value with zero alignment!") ?
void (0) : __assert_fail ("(!Alignment.isZero() || Type->isIncompleteType()) && \"initializing l-value with zero alignment!\""
, "clang/lib/CodeGen/CGValue.h", 237, __extension__ __PRETTY_FUNCTION__
))
237 "initializing l-value with zero alignment!")(static_cast <bool> ((!Alignment.isZero() || Type->isIncompleteType
()) && "initializing l-value with zero alignment!") ?
void (0) : __assert_fail ("(!Alignment.isZero() || Type->isIncompleteType()) && \"initializing l-value with zero alignment!\""
, "clang/lib/CodeGen/CGValue.h", 237, __extension__ __PRETTY_FUNCTION__
))
;
238 if (isGlobalReg())
239 assert(ElementType == nullptr && "Global reg does not store elem type")(static_cast <bool> (ElementType == nullptr && "Global reg does not store elem type"
) ? void (0) : __assert_fail ("ElementType == nullptr && \"Global reg does not store elem type\""
, "clang/lib/CodeGen/CGValue.h", 239, __extension__ __PRETTY_FUNCTION__
))
;
240 else
241 assert(llvm::cast<llvm::PointerType>(V->getType())(static_cast <bool> (llvm::cast<llvm::PointerType>
(V->getType()) ->isOpaqueOrPointeeTypeMatches(ElementType
) && "Pointer element type mismatch") ? void (0) : __assert_fail
("llvm::cast<llvm::PointerType>(V->getType()) ->isOpaqueOrPointeeTypeMatches(ElementType) && \"Pointer element type mismatch\""
, "clang/lib/CodeGen/CGValue.h", 243, __extension__ __PRETTY_FUNCTION__
))
242 ->isOpaqueOrPointeeTypeMatches(ElementType) &&(static_cast <bool> (llvm::cast<llvm::PointerType>
(V->getType()) ->isOpaqueOrPointeeTypeMatches(ElementType
) && "Pointer element type mismatch") ? void (0) : __assert_fail
("llvm::cast<llvm::PointerType>(V->getType()) ->isOpaqueOrPointeeTypeMatches(ElementType) && \"Pointer element type mismatch\""
, "clang/lib/CodeGen/CGValue.h", 243, __extension__ __PRETTY_FUNCTION__
))
243 "Pointer element type mismatch")(static_cast <bool> (llvm::cast<llvm::PointerType>
(V->getType()) ->isOpaqueOrPointeeTypeMatches(ElementType
) && "Pointer element type mismatch") ? void (0) : __assert_fail
("llvm::cast<llvm::PointerType>(V->getType()) ->isOpaqueOrPointeeTypeMatches(ElementType) && \"Pointer element type mismatch\""
, "clang/lib/CodeGen/CGValue.h", 243, __extension__ __PRETTY_FUNCTION__
))
;
244
245 this->Type = Type;
246 this->Quals = Quals;
247 const unsigned MaxAlign = 1U << 31;
248 this->Alignment = Alignment.getQuantity() <= MaxAlign
249 ? Alignment.getQuantity()
250 : MaxAlign;
251 assert(this->Alignment == Alignment.getQuantity() &&(static_cast <bool> (this->Alignment == Alignment.getQuantity
() && "Alignment exceeds allowed max!") ? void (0) : __assert_fail
("this->Alignment == Alignment.getQuantity() && \"Alignment exceeds allowed max!\""
, "clang/lib/CodeGen/CGValue.h", 252, __extension__ __PRETTY_FUNCTION__
))
252 "Alignment exceeds allowed max!")(static_cast <bool> (this->Alignment == Alignment.getQuantity
() && "Alignment exceeds allowed max!") ? void (0) : __assert_fail
("this->Alignment == Alignment.getQuantity() && \"Alignment exceeds allowed max!\""
, "clang/lib/CodeGen/CGValue.h", 252, __extension__ __PRETTY_FUNCTION__
))
;
253 this->BaseInfo = BaseInfo;
254 this->TBAAInfo = TBAAInfo;
255
256 // Initialize Objective-C flags.
257 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
258 this->ImpreciseLifetime = false;
259 this->Nontemporal = false;
260 this->ThreadLocalRef = false;
261 this->BaseIvarExp = nullptr;
262 }
263
264public:
265 bool isSimple() const { return LVType == Simple; }
14
Assuming field 'LVType' is not equal to Simple
15
Returning zero, which participates in a condition later
266 bool isVectorElt() const { return LVType == VectorElt; }
19
Assuming field 'LVType' is not equal to VectorElt
20
Returning zero, which participates in a condition later
267 bool isBitField() const { return LVType == BitField; }
268 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
24
Assuming field 'LVType' is not equal to ExtVectorElt
25
Returning zero, which participates in a condition later
269 bool isGlobalReg() const { return LVType == GlobalReg; }
29
Assuming field 'LVType' is not equal to GlobalReg
30
Returning zero, which participates in a condition later
270 bool isMatrixElt() const { return LVType == MatrixElt; }
34
Assuming field 'LVType' is equal to MatrixElt
35
Returning the value 1, which participates in a condition later
271
272 bool isVolatileQualified() const { return Quals.hasVolatile(); }
273 bool isRestrictQualified() const { return Quals.hasRestrict(); }
274 unsigned getVRQualifiers() const {
275 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
276 }
277
278 QualType getType() const { return Type; }
279
280 Qualifiers::ObjCLifetime getObjCLifetime() const {
281 return Quals.getObjCLifetime();
282 }
283
284 bool isObjCIvar() const { return Ivar; }
285 void setObjCIvar(bool Value) { Ivar = Value; }
286
287 bool isObjCArray() const { return ObjIsArray; }
288 void setObjCArray(bool Value) { ObjIsArray = Value; }
289
290 bool isNonGC () const { return NonGC; }
291 void setNonGC(bool Value) { NonGC = Value; }
292
293 bool isGlobalObjCRef() const { return GlobalObjCRef; }
294 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
295
296 bool isThreadLocalRef() const { return ThreadLocalRef; }
297 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
298
299 ARCPreciseLifetime_t isARCPreciseLifetime() const {
300 return ARCPreciseLifetime_t(!ImpreciseLifetime);
301 }
302 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
303 ImpreciseLifetime = (value == ARCImpreciseLifetime);
304 }
305 bool isNontemporal() const { return Nontemporal; }
306 void setNontemporal(bool Value) { Nontemporal = Value; }
307
308 bool isObjCWeak() const {
309 return Quals.getObjCGCAttr() == Qualifiers::Weak;
7
Assuming the condition is false
8
Returning zero, which participates in a condition later
310 }
311 bool isObjCStrong() const {
312 return Quals.getObjCGCAttr() == Qualifiers::Strong;
313 }
314
315 bool isVolatile() const {
316 return Quals.hasVolatile();
317 }
318
319 Expr *getBaseIvarExp() const { return BaseIvarExp; }
320 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
321
322 TBAAAccessInfo getTBAAInfo() const { return TBAAInfo; }
323 void setTBAAInfo(TBAAAccessInfo Info) { TBAAInfo = Info; }
324
325 const Qualifiers &getQuals() const { return Quals; }
326 Qualifiers &getQuals() { return Quals; }
327
328 LangAS getAddressSpace() const { return Quals.getAddressSpace(); }
329
330 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
331 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
332
333 LValueBaseInfo getBaseInfo() const { return BaseInfo; }
334 void setBaseInfo(LValueBaseInfo Info) { BaseInfo = Info; }
335
336 // simple lvalue
337 llvm::Value *getPointer(CodeGenFunction &CGF) const {
338 assert(isSimple())(static_cast <bool> (isSimple()) ? void (0) : __assert_fail
("isSimple()", "clang/lib/CodeGen/CGValue.h", 338, __extension__
__PRETTY_FUNCTION__))
;
339 return V;
340 }
341 Address getAddress(CodeGenFunction &CGF) const {
342 return Address(getPointer(CGF), ElementType, getAlignment());
343 }
344 void setAddress(Address address) {
345 assert(isSimple())(static_cast <bool> (isSimple()) ? void (0) : __assert_fail
("isSimple()", "clang/lib/CodeGen/CGValue.h", 345, __extension__
__PRETTY_FUNCTION__))
;
346 V = address.getPointer();
347 ElementType = address.getElementType();
348 Alignment = address.getAlignment().getQuantity();
349 }
350
351 // vector elt lvalue
352 Address getVectorAddress() const {
353 return Address(getVectorPointer(), ElementType, getAlignment());
354 }
355 llvm::Value *getVectorPointer() const {
356 assert(isVectorElt())(static_cast <bool> (isVectorElt()) ? void (0) : __assert_fail
("isVectorElt()", "clang/lib/CodeGen/CGValue.h", 356, __extension__
__PRETTY_FUNCTION__))
;
357 return V;
358 }
359 llvm::Value *getVectorIdx() const {
360 assert(isVectorElt())(static_cast <bool> (isVectorElt()) ? void (0) : __assert_fail
("isVectorElt()", "clang/lib/CodeGen/CGValue.h", 360, __extension__
__PRETTY_FUNCTION__))
;
361 return VectorIdx;
362 }
363
364 Address getMatrixAddress() const {
365 return Address(getMatrixPointer(), ElementType, getAlignment());
366 }
367 llvm::Value *getMatrixPointer() const {
368 assert(isMatrixElt())(static_cast <bool> (isMatrixElt()) ? void (0) : __assert_fail
("isMatrixElt()", "clang/lib/CodeGen/CGValue.h", 368, __extension__
__PRETTY_FUNCTION__))
;
369 return V;
370 }
371 llvm::Value *getMatrixIdx() const {
372 assert(isMatrixElt())(static_cast <bool> (isMatrixElt()) ? void (0) : __assert_fail
("isMatrixElt()", "clang/lib/CodeGen/CGValue.h", 372, __extension__
__PRETTY_FUNCTION__))
;
373 return VectorIdx;
374 }
375
376 // extended vector elements.
377 Address getExtVectorAddress() const {
378 return Address(getExtVectorPointer(), ElementType, getAlignment());
379 }
380 llvm::Value *getExtVectorPointer() const {
381 assert(isExtVectorElt())(static_cast <bool> (isExtVectorElt()) ? void (0) : __assert_fail
("isExtVectorElt()", "clang/lib/CodeGen/CGValue.h", 381, __extension__
__PRETTY_FUNCTION__))
;
382 return V;
383 }
384 llvm::Constant *getExtVectorElts() const {
385 assert(isExtVectorElt())(static_cast <bool> (isExtVectorElt()) ? void (0) : __assert_fail
("isExtVectorElt()", "clang/lib/CodeGen/CGValue.h", 385, __extension__
__PRETTY_FUNCTION__))
;
386 return VectorElts;
387 }
388
389 // bitfield lvalue
390 Address getBitFieldAddress() const {
391 return Address(getBitFieldPointer(), ElementType, getAlignment());
392 }
393 llvm::Value *getBitFieldPointer() const { assert(isBitField())(static_cast <bool> (isBitField()) ? void (0) : __assert_fail
("isBitField()", "clang/lib/CodeGen/CGValue.h", 393, __extension__
__PRETTY_FUNCTION__))
; return V; }
394 const CGBitFieldInfo &getBitFieldInfo() const {
395 assert(isBitField())(static_cast <bool> (isBitField()) ? void (0) : __assert_fail
("isBitField()", "clang/lib/CodeGen/CGValue.h", 395, __extension__
__PRETTY_FUNCTION__))
;
396 return *BitFieldInfo;
397 }
398
399 // global register lvalue
400 llvm::Value *getGlobalReg() const { assert(isGlobalReg())(static_cast <bool> (isGlobalReg()) ? void (0) : __assert_fail
("isGlobalReg()", "clang/lib/CodeGen/CGValue.h", 400, __extension__
__PRETTY_FUNCTION__))
; return V; }
401
402 static LValue MakeAddr(Address address, QualType type, ASTContext &Context,
403 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
404 Qualifiers qs = type.getQualifiers();
405 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
406
407 LValue R;
408 R.LVType = Simple;
409 assert(address.getPointer()->getType()->isPointerTy())(static_cast <bool> (address.getPointer()->getType()
->isPointerTy()) ? void (0) : __assert_fail ("address.getPointer()->getType()->isPointerTy()"
, "clang/lib/CodeGen/CGValue.h", 409, __extension__ __PRETTY_FUNCTION__
))
;
410 R.V = address.getPointer();
411 R.ElementType = address.getElementType();
412 R.Initialize(type, qs, address.getAlignment(), BaseInfo, TBAAInfo);
413 return R;
414 }
415
416 static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx,
417 QualType type, LValueBaseInfo BaseInfo,
418 TBAAAccessInfo TBAAInfo) {
419 LValue R;
420 R.LVType = VectorElt;
421 R.V = vecAddress.getPointer();
422 R.ElementType = vecAddress.getElementType();
423 R.VectorIdx = Idx;
424 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
425 BaseInfo, TBAAInfo);
426 return R;
427 }
428
429 static LValue MakeExtVectorElt(Address vecAddress, llvm::Constant *Elts,
430 QualType type, LValueBaseInfo BaseInfo,
431 TBAAAccessInfo TBAAInfo) {
432 LValue R;
433 R.LVType = ExtVectorElt;
434 R.V = vecAddress.getPointer();
435 R.ElementType = vecAddress.getElementType();
436 R.VectorElts = Elts;
437 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
438 BaseInfo, TBAAInfo);
439 return R;
440 }
441
442 /// Create a new object to represent a bit-field access.
443 ///
444 /// \param Addr - The base address of the bit-field sequence this
445 /// bit-field refers to.
446 /// \param Info - The information describing how to perform the bit-field
447 /// access.
448 static LValue MakeBitfield(Address Addr, const CGBitFieldInfo &Info,
449 QualType type, LValueBaseInfo BaseInfo,
450 TBAAAccessInfo TBAAInfo) {
451 LValue R;
452 R.LVType = BitField;
453 R.V = Addr.getPointer();
454 R.ElementType = Addr.getElementType();
455 R.BitFieldInfo = &Info;
456 R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo,
457 TBAAInfo);
458 return R;
459 }
460
461 static LValue MakeGlobalReg(llvm::Value *V, CharUnits alignment,
462 QualType type) {
463 LValue R;
464 R.LVType = GlobalReg;
465 R.V = V;
466 R.ElementType = nullptr;
467 R.Initialize(type, type.getQualifiers(), alignment,
468 LValueBaseInfo(AlignmentSource::Decl), TBAAAccessInfo());
469 return R;
470 }
471
472 static LValue MakeMatrixElt(Address matAddress, llvm::Value *Idx,
473 QualType type, LValueBaseInfo BaseInfo,
474 TBAAAccessInfo TBAAInfo) {
475 LValue R;
476 R.LVType = MatrixElt;
477 R.V = matAddress.getPointer();
478 R.ElementType = matAddress.getElementType();
479 R.VectorIdx = Idx;
480 R.Initialize(type, type.getQualifiers(), matAddress.getAlignment(),
481 BaseInfo, TBAAInfo);
482 return R;
483 }
484
485 RValue asAggregateRValue(CodeGenFunction &CGF) const {
486 return RValue::getAggregate(getAddress(CGF), isVolatileQualified());
487 }
488};
489
490/// An aggregate value slot.
491class AggValueSlot {
492 /// The address.
493 Address Addr;
494
495 // Qualifiers
496 Qualifiers Quals;
497
498 /// DestructedFlag - This is set to true if some external code is
499 /// responsible for setting up a destructor for the slot. Otherwise
500 /// the code which constructs it should push the appropriate cleanup.
501 bool DestructedFlag : 1;
502
503 /// ObjCGCFlag - This is set to true if writing to the memory in the
504 /// slot might require calling an appropriate Objective-C GC
505 /// barrier. The exact interaction here is unnecessarily mysterious.
506 bool ObjCGCFlag : 1;
507
508 /// ZeroedFlag - This is set to true if the memory in the slot is
509 /// known to be zero before the assignment into it. This means that
510 /// zero fields don't need to be set.
511 bool ZeroedFlag : 1;
512
513 /// AliasedFlag - This is set to true if the slot might be aliased
514 /// and it's not undefined behavior to access it through such an
515 /// alias. Note that it's always undefined behavior to access a C++
516 /// object that's under construction through an alias derived from
517 /// outside the construction process.
518 ///
519 /// This flag controls whether calls that produce the aggregate
520 /// value may be evaluated directly into the slot, or whether they
521 /// must be evaluated into an unaliased temporary and then memcpy'ed
522 /// over. Since it's invalid in general to memcpy a non-POD C++
523 /// object, it's important that this flag never be set when
524 /// evaluating an expression which constructs such an object.
525 bool AliasedFlag : 1;
526
527 /// This is set to true if the tail padding of this slot might overlap
528 /// another object that may have already been initialized (and whose
529 /// value must be preserved by this initialization). If so, we may only
530 /// store up to the dsize of the type. Otherwise we can widen stores to
531 /// the size of the type.
532 bool OverlapFlag : 1;
533
534 /// If is set to true, sanitizer checks are already generated for this address
535 /// or not required. For instance, if this address represents an object
536 /// created in 'new' expression, sanitizer checks for memory is made as a part
537 /// of 'operator new' emission and object constructor should not generate
538 /// them.
539 bool SanitizerCheckedFlag : 1;
540
541 AggValueSlot(Address Addr, Qualifiers Quals, bool DestructedFlag,
542 bool ObjCGCFlag, bool ZeroedFlag, bool AliasedFlag,
543 bool OverlapFlag, bool SanitizerCheckedFlag)
544 : Addr(Addr), Quals(Quals), DestructedFlag(DestructedFlag),
545 ObjCGCFlag(ObjCGCFlag), ZeroedFlag(ZeroedFlag),
546 AliasedFlag(AliasedFlag), OverlapFlag(OverlapFlag),
547 SanitizerCheckedFlag(SanitizerCheckedFlag) {}
548
549public:
550 enum IsAliased_t { IsNotAliased, IsAliased };
551 enum IsDestructed_t { IsNotDestructed, IsDestructed };
552 enum IsZeroed_t { IsNotZeroed, IsZeroed };
553 enum Overlap_t { DoesNotOverlap, MayOverlap };
554 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
555 enum IsSanitizerChecked_t { IsNotSanitizerChecked, IsSanitizerChecked };
556
557 /// ignored - Returns an aggregate value slot indicating that the
558 /// aggregate value is being ignored.
559 static AggValueSlot ignored() {
560 return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed,
561 DoesNotNeedGCBarriers, IsNotAliased, DoesNotOverlap);
562 }
563
564 /// forAddr - Make a slot for an aggregate value.
565 ///
566 /// \param quals - The qualifiers that dictate how the slot should
567 /// be initialied. Only 'volatile' and the Objective-C lifetime
568 /// qualifiers matter.
569 ///
570 /// \param isDestructed - true if something else is responsible
571 /// for calling destructors on this object
572 /// \param needsGC - true if the slot is potentially located
573 /// somewhere that ObjC GC calls should be emitted for
574 static AggValueSlot forAddr(Address addr,
575 Qualifiers quals,
576 IsDestructed_t isDestructed,
577 NeedsGCBarriers_t needsGC,
578 IsAliased_t isAliased,
579 Overlap_t mayOverlap,
580 IsZeroed_t isZeroed = IsNotZeroed,
581 IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
582 return AggValueSlot(addr, quals, isDestructed, needsGC, isZeroed, isAliased,
583 mayOverlap, isChecked);
584 }
585
586 static AggValueSlot
587 forLValue(const LValue &LV, CodeGenFunction &CGF, IsDestructed_t isDestructed,
588 NeedsGCBarriers_t needsGC, IsAliased_t isAliased,
589 Overlap_t mayOverlap, IsZeroed_t isZeroed = IsNotZeroed,
590 IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
591 return forAddr(LV.getAddress(CGF), LV.getQuals(), isDestructed, needsGC,
592 isAliased, mayOverlap, isZeroed, isChecked);
593 }
594
595 IsDestructed_t isExternallyDestructed() const {
596 return IsDestructed_t(DestructedFlag);
597 }
598 void setExternallyDestructed(bool destructed = true) {
599 DestructedFlag = destructed;
600 }
601
602 Qualifiers getQualifiers() const { return Quals; }
603
604 bool isVolatile() const {
605 return Quals.hasVolatile();
606 }
607
608 void setVolatile(bool flag) {
609 if (flag)
610 Quals.addVolatile();
611 else
612 Quals.removeVolatile();
613 }
614
615 Qualifiers::ObjCLifetime getObjCLifetime() const {
616 return Quals.getObjCLifetime();
617 }
618
619 NeedsGCBarriers_t requiresGCollection() const {
620 return NeedsGCBarriers_t(ObjCGCFlag);
621 }
622
623 llvm::Value *getPointer() const {
624 return Addr.getPointer();
625 }
626
627 Address getAddress() const {
628 return Addr;
629 }
630
631 bool isIgnored() const {
632 return !Addr.isValid();
633 }
634
635 CharUnits getAlignment() const {
636 return Addr.getAlignment();
637 }
638
639 IsAliased_t isPotentiallyAliased() const {
640 return IsAliased_t(AliasedFlag);
641 }
642
643 Overlap_t mayOverlap() const {
644 return Overlap_t(OverlapFlag);
645 }
646
647 bool isSanitizerChecked() const {
648 return SanitizerCheckedFlag;
649 }
650
651 RValue asRValue() const {
652 if (isIgnored()) {
653 return RValue::getIgnored();
654 } else {
655 return RValue::getAggregate(getAddress(), isVolatile());
656 }
657 }
658
659 void setZeroed(bool V = true) { ZeroedFlag = V; }
660 IsZeroed_t isZeroed() const {
661 return IsZeroed_t(ZeroedFlag);
662 }
663
664 /// Get the preferred size to use when storing a value to this slot. This
665 /// is the type size unless that might overlap another object, in which
666 /// case it's the dsize.
667 CharUnits getPreferredSize(ASTContext &Ctx, QualType Type) const {
668 return mayOverlap() ? Ctx.getTypeInfoDataSizeInChars(Type).Width
669 : Ctx.getTypeSizeInChars(Type);
670 }
671};
672
673} // end namespace CodeGen
674} // end namespace clang
675
676#endif