Bug Summary

File:clang/lib/CodeGen/CGExpr.cpp
Warning:line 2080, column 39
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 -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~++20211016100712+8e1d532707fd/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D CLANG_ROUND_TRIP_CC1_ARGS=ON -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~++20211016100712+8e1d532707fd/clang/lib/CodeGen -I /build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include -I tools/clang/include -I include -I /build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/include -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 -O2 -Wno-unused-command-line-argument -Wno-unknown-warning-option -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~++20211016100712+8e1d532707fd/build-llvm -ferror-limit 19 -fvisibility-inlines-hidden -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-2021-10-17-004846-21170-1 -x c++ /build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/CodeGen/CGExpr.cpp

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

/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/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
51public:
52 bool isScalar() const { return V1.getInt() == Scalar; }
53 bool isComplex() const { return V1.getInt() == Complex; }
54 bool isAggregate() const { return V1.getInt() == Aggregate; }
55
56 bool isVolatileQualified() const { return V2.getInt(); }
57
58 /// getScalarVal() - Return the Value* of this scalar value.
59 llvm::Value *getScalarVal() const {
60 assert(isScalar() && "Not a scalar!")(static_cast <bool> (isScalar() && "Not a scalar!"
) ? void (0) : __assert_fail ("isScalar() && \"Not a scalar!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/CodeGen/CGValue.h"
, 60, __extension__ __PRETTY_FUNCTION__))
;
61 return V1.getPointer();
62 }
63
64 /// getComplexVal - Return the real/imag components of this complex value.
65 ///
66 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
67 return std::make_pair(V1.getPointer(), V2.getPointer());
68 }
69
70 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
71 Address getAggregateAddress() const {
72 assert(isAggregate() && "Not an aggregate!")(static_cast <bool> (isAggregate() && "Not an aggregate!"
) ? void (0) : __assert_fail ("isAggregate() && \"Not an aggregate!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/CodeGen/CGValue.h"
, 72, __extension__ __PRETTY_FUNCTION__))
;
73 auto align = reinterpret_cast<uintptr_t>(V2.getPointer()) >> AggAlignShift;
74 return Address(V1.getPointer(), CharUnits::fromQuantity(align));
75 }
76 llvm::Value *getAggregatePointer() const {
77 assert(isAggregate() && "Not an aggregate!")(static_cast <bool> (isAggregate() && "Not an aggregate!"
) ? void (0) : __assert_fail ("isAggregate() && \"Not an aggregate!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/CodeGen/CGValue.h"
, 77, __extension__ __PRETTY_FUNCTION__))
;
78 return V1.getPointer();
79 }
80
81 static RValue getIgnored() {
82 // FIXME: should we make this a more explicit state?
83 return get(nullptr);
84 }
85
86 static RValue get(llvm::Value *V) {
87 RValue ER;
88 ER.V1.setPointer(V);
89 ER.V1.setInt(Scalar);
90 ER.V2.setInt(false);
91 return ER;
92 }
93 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
94 RValue ER;
95 ER.V1.setPointer(V1);
96 ER.V2.setPointer(V2);
97 ER.V1.setInt(Complex);
98 ER.V2.setInt(false);
99 return ER;
100 }
101 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
102 return getComplex(C.first, C.second);
103 }
104 // FIXME: Aggregate rvalues need to retain information about whether they are
105 // volatile or not. Remove default to find all places that probably get this
106 // wrong.
107 static RValue getAggregate(Address addr, bool isVolatile = false) {
108 RValue ER;
109 ER.V1.setPointer(addr.getPointer());
110 ER.V1.setInt(Aggregate);
111
112 auto align = static_cast<uintptr_t>(addr.getAlignment().getQuantity());
113 ER.V2.setPointer(reinterpret_cast<llvm::Value*>(align << AggAlignShift));
114 ER.V2.setInt(isVolatile);
115 return ER;
116 }
117};
118
119/// Does an ARC strong l-value have precise lifetime?
120enum ARCPreciseLifetime_t {
121 ARCImpreciseLifetime, ARCPreciseLifetime
122};
123
124/// The source of the alignment of an l-value; an expression of
125/// confidence in the alignment actually matching the estimate.
126enum class AlignmentSource {
127 /// The l-value was an access to a declared entity or something
128 /// equivalently strong, like the address of an array allocated by a
129 /// language runtime.
130 Decl,
131
132 /// The l-value was considered opaque, so the alignment was
133 /// determined from a type, but that type was an explicitly-aligned
134 /// typedef.
135 AttributedType,
136
137 /// The l-value was considered opaque, so the alignment was
138 /// determined from a type.
139 Type
140};
141
142/// Given that the base address has the given alignment source, what's
143/// our confidence in the alignment of the field?
144static inline AlignmentSource getFieldAlignmentSource(AlignmentSource Source) {
145 // For now, we don't distinguish fields of opaque pointers from
146 // top-level declarations, but maybe we should.
147 return AlignmentSource::Decl;
148}
149
150class LValueBaseInfo {
151 AlignmentSource AlignSource;
152
153public:
154 explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type)
155 : AlignSource(Source) {}
156 AlignmentSource getAlignmentSource() const { return AlignSource; }
157 void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; }
158
159 void mergeForCast(const LValueBaseInfo &Info) {
160 setAlignmentSource(Info.getAlignmentSource());
161 }
162};
163
164/// LValue - This represents an lvalue references. Because C/C++ allow
165/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
166/// bitrange.
167class LValue {
168 enum {
169 Simple, // This is a normal l-value, use getAddress().
170 VectorElt, // This is a vector element l-value (V[i]), use getVector*
171 BitField, // This is a bitfield l-value, use getBitfield*.
172 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
173 GlobalReg, // This is a register l-value, use getGlobalReg()
174 MatrixElt // This is a matrix element, use getVector*
175 } LVType;
176
177 llvm::Value *V;
178
179 union {
180 // Index into a vector subscript: V[i]
181 llvm::Value *VectorIdx;
182
183 // ExtVector element subset: V.xyx
184 llvm::Constant *VectorElts;
185
186 // BitField start bit and size
187 const CGBitFieldInfo *BitFieldInfo;
188 };
189
190 QualType Type;
191
192 // 'const' is unused here
193 Qualifiers Quals;
194
195 // The alignment to use when accessing this lvalue. (For vector elements,
196 // this is the alignment of the whole vector.)
197 unsigned Alignment;
198
199 // objective-c's ivar
200 bool Ivar:1;
201
202 // objective-c's ivar is an array
203 bool ObjIsArray:1;
204
205 // LValue is non-gc'able for any reason, including being a parameter or local
206 // variable.
207 bool NonGC: 1;
208
209 // Lvalue is a global reference of an objective-c object
210 bool GlobalObjCRef : 1;
211
212 // Lvalue is a thread local reference
213 bool ThreadLocalRef : 1;
214
215 // Lvalue has ARC imprecise lifetime. We store this inverted to try
216 // to make the default bitfield pattern all-zeroes.
217 bool ImpreciseLifetime : 1;
218
219 // This flag shows if a nontemporal load/stores should be used when accessing
220 // this lvalue.
221 bool Nontemporal : 1;
222
223 LValueBaseInfo BaseInfo;
224 TBAAAccessInfo TBAAInfo;
225
226 Expr *BaseIvarExp;