1//===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- 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// This is the internal per-function state used for llvm translation.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
14#define LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
15
16#include "CGBuilder.h"
17#include "CGDebugInfo.h"
18#include "CGLoopInfo.h"
19#include "CGValue.h"
20#include "CodeGenModule.h"
21#include "CodeGenPGO.h"
22#include "EHScopeStack.h"
23#include "VarBypassDetector.h"
24#include "clang/AST/CharUnits.h"
25#include "clang/AST/CurrentSourceLocExprScope.h"
26#include "clang/AST/ExprCXX.h"
27#include "clang/AST/ExprObjC.h"
28#include "clang/AST/ExprOpenMP.h"
29#include "clang/AST/StmtOpenMP.h"
30#include "clang/AST/Type.h"
31#include "clang/Basic/ABI.h"
32#include "clang/Basic/CapturedStmt.h"
33#include "clang/Basic/CodeGenOptions.h"
34#include "clang/Basic/OpenMPKinds.h"
35#include "clang/Basic/TargetInfo.h"
36#include "llvm/ADT/ArrayRef.h"
37#include "llvm/ADT/DenseMap.h"
38#include "llvm/ADT/MapVector.h"
39#include "llvm/ADT/SmallVector.h"
40#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
41#include "llvm/IR/ValueHandle.h"
42#include "llvm/Support/Debug.h"
43#include "llvm/Transforms/Utils/SanitizerStats.h"
44
45namespace llvm {
46class BasicBlock;
47class LLVMContext;
48class MDNode;
49class Module;
50class SwitchInst;
51class Twine;
52class Value;
53class CanonicalLoopInfo;
54}
55
56namespace clang {
57class ASTContext;
58class BlockDecl;
59class CXXDestructorDecl;
60class CXXForRangeStmt;
61class CXXTryStmt;
62class Decl;
63class LabelDecl;
64class EnumConstantDecl;
65class FunctionDecl;
66class FunctionProtoType;
67class LabelStmt;
68class ObjCContainerDecl;
69class ObjCInterfaceDecl;
70class ObjCIvarDecl;
71class ObjCMethodDecl;
72class ObjCImplementationDecl;
73class ObjCPropertyImplDecl;
74class TargetInfo;
75class VarDecl;
76class ObjCForCollectionStmt;
77class ObjCAtTryStmt;
78class ObjCAtThrowStmt;
79class ObjCAtSynchronizedStmt;
80class ObjCAutoreleasePoolStmt;
81class OMPUseDevicePtrClause;
82class OMPUseDeviceAddrClause;
83class ReturnsNonNullAttr;
84class SVETypeFlags;
85class OMPExecutableDirective;
86
87namespace analyze_os_log {
88class OSLogBufferLayout;
89}
90
91namespace CodeGen {
92class CodeGenTypes;
93class CGCallee;
94class CGFunctionInfo;
95class CGRecordLayout;
96class CGBlockInfo;
97class CGCXXABI;
98class BlockByrefHelpers;
99class BlockByrefInfo;
100class BlockFlags;
101class BlockFieldFlags;
102class RegionCodeGenTy;
103class TargetCodeGenInfo;
104struct OMPTaskDataTy;
105struct CGCoroData;
106
107/// The kind of evaluation to perform on values of a particular
108/// type. Basically, is the code in CGExprScalar, CGExprComplex, or
109/// CGExprAgg?
110///
111/// TODO: should vectors maybe be split out into their own thing?
112enum TypeEvaluationKind {
113 TEK_Scalar,
114 TEK_Complex,
115 TEK_Aggregate
116};
117
118#define LIST_SANITIZER_CHECKS \
119 SANITIZER_CHECK(AddOverflow, add_overflow, 0) \
120 SANITIZER_CHECK(BuiltinUnreachable, builtin_unreachable, 0) \
121 SANITIZER_CHECK(CFICheckFail, cfi_check_fail, 0) \
122 SANITIZER_CHECK(DivremOverflow, divrem_overflow, 0) \
123 SANITIZER_CHECK(DynamicTypeCacheMiss, dynamic_type_cache_miss, 0) \
124 SANITIZER_CHECK(FloatCastOverflow, float_cast_overflow, 0) \
125 SANITIZER_CHECK(FunctionTypeMismatch, function_type_mismatch, 1) \
126 SANITIZER_CHECK(ImplicitConversion, implicit_conversion, 0) \
127 SANITIZER_CHECK(InvalidBuiltin, invalid_builtin, 0) \
128 SANITIZER_CHECK(InvalidObjCCast, invalid_objc_cast, 0) \
129 SANITIZER_CHECK(LoadInvalidValue, load_invalid_value, 0) \
130 SANITIZER_CHECK(MissingReturn, missing_return, 0) \
131 SANITIZER_CHECK(MulOverflow, mul_overflow, 0) \
132 SANITIZER_CHECK(NegateOverflow, negate_overflow, 0) \
133 SANITIZER_CHECK(NullabilityArg, nullability_arg, 0) \
134 SANITIZER_CHECK(NullabilityReturn, nullability_return, 1) \
135 SANITIZER_CHECK(NonnullArg, nonnull_arg, 0) \
136 SANITIZER_CHECK(NonnullReturn, nonnull_return, 1) \
137 SANITIZER_CHECK(OutOfBounds, out_of_bounds, 0) \
138 SANITIZER_CHECK(PointerOverflow, pointer_overflow, 0) \
139 SANITIZER_CHECK(ShiftOutOfBounds, shift_out_of_bounds, 0) \
140 SANITIZER_CHECK(SubOverflow, sub_overflow, 0) \
141 SANITIZER_CHECK(TypeMismatch, type_mismatch, 1) \
142 SANITIZER_CHECK(AlignmentAssumption, alignment_assumption, 0) \
143 SANITIZER_CHECK(VLABoundNotPositive, vla_bound_not_positive, 0)
144
145enum SanitizerHandler {
146#define SANITIZER_CHECK(Enum, Name, Version) Enum,
147 LIST_SANITIZER_CHECKS
148#undef SANITIZER_CHECK
149};
150
151/// Helper class with most of the code for saving a value for a
152/// conditional expression cleanup.
153struct DominatingLLVMValue {
154 typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
155
156 /// Answer whether the given value needs extra work to be saved.
157 static bool needsSaving(llvm::Value *value) {
158 // If it's not an instruction, we don't need to save.
159 if (!isa<llvm::Instruction>(value)) return false;
160
161 // If it's an instruction in the entry block, we don't need to save.
162 llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
163 return (block != &block->getParent()->getEntryBlock());
164 }
165
166 static saved_type save(CodeGenFunction &CGF, llvm::Value *value);
167 static llvm::Value *restore(CodeGenFunction &CGF, saved_type value);
168};
169
170/// A partial specialization of DominatingValue for llvm::Values that
171/// might be llvm::Instructions.
172template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
173 typedef T *type;
174 static type restore(CodeGenFunction &CGF, saved_type value) {
175 return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
176 }
177};
178
179/// A specialization of DominatingValue for Address.
180template <> struct DominatingValue<Address> {
181 typedef Address type;
182
183 struct saved_type {
184 DominatingLLVMValue::saved_type SavedValue;
185 CharUnits Alignment;
186 };
187
188 static bool needsSaving(type value) {
189 return DominatingLLVMValue::needsSaving(value.getPointer());
190 }
191 static saved_type save(CodeGenFunction &CGF, type value) {
192 return { DominatingLLVMValue::save(CGF, value.getPointer()),
193 value.getAlignment() };
194 }
195 static type restore(CodeGenFunction &CGF, saved_type value) {
196 return Address(DominatingLLVMValue::restore(CGF, value.SavedValue),
197 value.Alignment);
198 }
199};
200
201/// A specialization of DominatingValue for RValue.
202template <> struct DominatingValue<RValue> {
203 typedef RValue type;
204 class saved_type {
205 enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
206 AggregateAddress, ComplexAddress };
207
208 llvm::Value *Value;
209 unsigned K : 3;
210 unsigned Align : 29;
211 saved_type(llvm::Value *v, Kind k, unsigned a = 0)
212 : Value(v), K(k), Align(a) {}
213
214 public:
215 static bool needsSaving(RValue value);
216 static saved_type save(CodeGenFunction &CGF, RValue value);
217 RValue restore(CodeGenFunction &CGF);
218
219 // implementations in CGCleanup.cpp
220 };
221
222 static bool needsSaving(type value) {
223 return saved_type::needsSaving(value);
224 }
225 static saved_type save(CodeGenFunction &CGF, type value) {
226 return saved_type::save(CGF, value);
227 }
228 static type restore(CodeGenFunction &CGF, saved_type value) {
229 return value.restore(CGF);
230 }
231};
232
233/// CodeGenFunction - This class organizes the per-function state that is used
234/// while generating LLVM code.
235class CodeGenFunction : public CodeGenTypeCache {
236 CodeGenFunction(const CodeGenFunction &) = delete;
237 void operator=(const CodeGenFunction &) = delete;
238
239 friend class CGCXXABI;
240public:
241 /// A jump destination is an abstract label, branching to which may
242 /// require a jump out through normal cleanups.
243 struct JumpDest {
244 JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {}
245 JumpDest(llvm::BasicBlock *Block,
246 EHScopeStack::stable_iterator Depth,
247 unsigned Index)
248 : Block(Block), ScopeDepth(Depth), Index(Index) {}
249
250 bool isValid() const { return Block != nullptr; }
251 llvm::BasicBlock *getBlock() const { return Block; }
252 EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
253 unsigned getDestIndex() const { return Index; }
254
255 // This should be used cautiously.
256 void setScopeDepth(EHScopeStack::stable_iterator depth) {
257 ScopeDepth = depth;
258 }
259
260 private:
261 llvm::BasicBlock *Block;
262 EHScopeStack::stable_iterator ScopeDepth;
263 unsigned Index;
264 };
265
266 CodeGenModule &CGM; // Per-module state.
267 const TargetInfo &Target;
268
269 // For EH/SEH outlined funclets, this field points to parent's CGF
270 CodeGenFunction *ParentCGF = nullptr;
271
272 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
273 LoopInfoStack LoopStack;
274 CGBuilderTy Builder;
275
276 // Stores variables for which we can't generate correct lifetime markers
277 // because of jumps.
278 VarBypassDetector Bypasses;
279
280 /// List of recently emitted OMPCanonicalLoops.
281 ///
282 /// Since OMPCanonicalLoops are nested inside other statements (in particular
283 /// CapturedStmt generated by OMPExecutableDirective and non-perfectly nested
284 /// loops), we cannot directly call OMPEmitOMPCanonicalLoop and receive its
285 /// llvm::CanonicalLoopInfo. Instead, we call EmitStmt and any
286 /// OMPEmitOMPCanonicalLoop called by it will add its CanonicalLoopInfo to
287 /// this stack when done. Entering a new loop requires clearing this list; it
288 /// either means we start parsing a new loop nest (in which case the previous
289 /// loop nest goes out of scope) or a second loop in the same level in which
290 /// case it would be ambiguous into which of the two (or more) loops the loop
291 /// nest would extend.
292 SmallVector<llvm::CanonicalLoopInfo *, 4> OMPLoopNestStack;
293
294 // CodeGen lambda for loops and support for ordered clause
295 typedef llvm::function_ref<void(CodeGenFunction &, const OMPLoopDirective &,
296 JumpDest)>
297 CodeGenLoopTy;
298 typedef llvm::function_ref<void(CodeGenFunction &, SourceLocation,
299 const unsigned, const bool)>
300 CodeGenOrderedTy;
301
302 // Codegen lambda for loop bounds in worksharing loop constructs
303 typedef llvm::function_ref<std::pair<LValue, LValue>(
304 CodeGenFunction &, const OMPExecutableDirective &S)>
305 CodeGenLoopBoundsTy;
306
307 // Codegen lambda for loop bounds in dispatch-based loop implementation
308 typedef llvm::function_ref<std::pair<llvm::Value *, llvm::Value *>(
309 CodeGenFunction &, const OMPExecutableDirective &S, Address LB,
310 Address UB)>
311 CodeGenDispatchBoundsTy;
312
313 /// CGBuilder insert helper. This function is called after an
314 /// instruction is created using Builder.
315 void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
316 llvm::BasicBlock *BB,
317 llvm::BasicBlock::iterator InsertPt) const;
318
319 /// CurFuncDecl - Holds the Decl for the current outermost
320 /// non-closure context.
321 const Decl *CurFuncDecl;
322 /// CurCodeDecl - This is the inner-most code context, which includes blocks.
323 const Decl *CurCodeDecl;
324 const CGFunctionInfo *CurFnInfo;
325 QualType FnRetTy;
326 llvm::Function *CurFn = nullptr;
327
328 /// Save Parameter Decl for coroutine.
329 llvm::SmallVector<const ParmVarDecl *, 4> FnArgs;
330
331 // Holds coroutine data if the current function is a coroutine. We use a
332 // wrapper to manage its lifetime, so that we don't have to define CGCoroData
333 // in this header.
334 struct CGCoroInfo {
335 std::unique_ptr<CGCoroData> Data;
336 CGCoroInfo();
337 ~CGCoroInfo();
338 };
339 CGCoroInfo CurCoro;
340
341 bool isCoroutine() const {
342 return CurCoro.Data != nullptr;
343 }
344
345 /// CurGD - The GlobalDecl for the current function being compiled.
346 GlobalDecl CurGD;
347
348 /// PrologueCleanupDepth - The cleanup depth enclosing all the
349 /// cleanups associated with the parameters.
350 EHScopeStack::stable_iterator PrologueCleanupDepth;
351
352 /// ReturnBlock - Unified return block.
353 JumpDest ReturnBlock;
354
355 /// ReturnValue - The temporary alloca to hold the return
356 /// value. This is invalid iff the function has no return value.
357 Address ReturnValue = Address::invalid();
358
359 /// ReturnValuePointer - The temporary alloca to hold a pointer to sret.
360 /// This is invalid if sret is not in use.
361 Address ReturnValuePointer = Address::invalid();
362
363 /// If a return statement is being visited, this holds the return statment's
364 /// result expression.
365 const Expr *RetExpr = nullptr;
366
367 /// Return true if a label was seen in the current scope.
368 bool hasLabelBeenSeenInCurrentScope() const {
369 if (CurLexicalScope)
370 return CurLexicalScope->hasLabels();
371 return !LabelMap.empty();
372 }
373
374 /// AllocaInsertPoint - This is an instruction in the entry block before which
375 /// we prefer to insert allocas.
376 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
377
378 /// API for captured statement code generation.
379 class CGCapturedStmtInfo {
380 public:
381 explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default)
382 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {}
383 explicit CGCapturedStmtInfo(const CapturedStmt &S,
384 CapturedRegionKind K = CR_Default)
385 : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
386
387 RecordDecl::field_iterator Field =
388 S.getCapturedRecordDecl()->field_begin();
389 for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
390 E = S.capture_end();
391 I != E; ++I, ++Field) {
392 if (I->capturesThis())
393 CXXThisFieldDecl = *Field;
394 else if (I->capturesVariable())
395 CaptureFields[I->getCapturedVar()->getCanonicalDecl()] = *Field;
396 else if (I->capturesVariableByCopy())
397 CaptureFields[I->getCapturedVar()->getCanonicalDecl()] = *Field;
398 }
399 }
400
401 virtual ~CGCapturedStmtInfo();
402
403 CapturedRegionKind getKind() const { return Kind; }
404
405 virtual void setContextValue(llvm::Value *V) { ThisValue = V; }
406 // Retrieve the value of the context parameter.
407 virtual llvm::Value *getContextValue() const { return ThisValue; }
408
409 /// Lookup the captured field decl for a variable.
410 virtual const FieldDecl *lookup(const VarDecl *VD) const {
411 return CaptureFields.lookup(VD->getCanonicalDecl());
412 }
413
414 bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; }
415 virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
416
417 static bool classof(const CGCapturedStmtInfo *) {
418 return true;
419 }
420
421 /// Emit the captured statement body.
422 virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) {
423 CGF.incrementProfileCounter(S);
424 CGF.EmitStmt(S);
425 }
426
427 /// Get the name of the capture helper.
428 virtual StringRef getHelperName() const { return "__captured_stmt"; }
429
430 private:
431 /// The kind of captured statement being generated.
432 CapturedRegionKind Kind;
433
434 /// Keep the map between VarDecl and FieldDecl.
435 llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields;
436
437 /// The base address of the captured record, passed in as the first
438 /// argument of the parallel region function.
439 llvm::Value *ThisValue;
440
441 /// Captured 'this' type.
442 FieldDecl *CXXThisFieldDecl;
443 };
444 CGCapturedStmtInfo *CapturedStmtInfo = nullptr;
445
446 /// RAII for correct setting/restoring of CapturedStmtInfo.
447 class CGCapturedStmtRAII {
448 private:
449 CodeGenFunction &CGF;
450 CGCapturedStmtInfo *PrevCapturedStmtInfo;
451 public:
452 CGCapturedStmtRAII(CodeGenFunction &CGF,
453 CGCapturedStmtInfo *NewCapturedStmtInfo)
454 : CGF(CGF), PrevCapturedStmtInfo(CGF.CapturedStmtInfo) {
455 CGF.CapturedStmtInfo = NewCapturedStmtInfo;
456 }
457 ~CGCapturedStmtRAII() { CGF.CapturedStmtInfo = PrevCapturedStmtInfo; }
458 };
459
460 /// An abstract representation of regular/ObjC call/message targets.
461 class AbstractCallee {
462 /// The function declaration of the callee.
463 const Decl *CalleeDecl;
464
465 public:
466 AbstractCallee() : CalleeDecl(nullptr) {}
467 AbstractCallee(const FunctionDecl *FD) : CalleeDecl(FD) {}
468 AbstractCallee(const ObjCMethodDecl *OMD) : CalleeDecl(OMD) {}
469 bool hasFunctionDecl() const {
470 return dyn_cast_or_null<FunctionDecl>(CalleeDecl);
471 }
472 const Decl *getDecl() const { return CalleeDecl; }
473 unsigned getNumParams() const {
474 if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl))
475 return FD->getNumParams();
476 return cast<ObjCMethodDecl>(CalleeDecl)->param_size();
477 }
478 const ParmVarDecl *getParamDecl(unsigned I) const {
479 if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl))
480 return FD->getParamDecl(I);
481 return *(cast<ObjCMethodDecl>(CalleeDecl)->param_begin() + I);
482 }
483 };
484
485 /// Sanitizers enabled for this function.
486 SanitizerSet SanOpts;
487
488 /// True if CodeGen currently emits code implementing sanitizer checks.
489 bool IsSanitizerScope = false;
490
491 /// RAII object to set/unset CodeGenFunction::IsSanitizerScope.
492 class SanitizerScope {
493 CodeGenFunction *CGF;
494 public:
495 SanitizerScope(CodeGenFunction *CGF);
496 ~SanitizerScope();
497 };
498
499 /// In C++, whether we are code generating a thunk. This controls whether we
500 /// should emit cleanups.
501 bool CurFuncIsThunk = false;
502
503 /// In ARC, whether we should autorelease the return value.
504 bool AutoreleaseResult = false;
505
506 /// Whether we processed a Microsoft-style asm block during CodeGen. These can
507 /// potentially set the return value.
508 bool SawAsmBlock = false;
509
510 const NamedDecl *CurSEHParent = nullptr;
511
512 /// True if the current function is an outlined SEH helper. This can be a
513 /// finally block or filter expression.
514 bool IsOutlinedSEHHelper = false;
515
516 /// True if CodeGen currently emits code inside presereved access index
517 /// region.
518 bool IsInPreservedAIRegion = false;
519
520 /// True if the current statement has nomerge attribute.
521 bool InNoMergeAttributedStmt = false;
522
523 // The CallExpr within the current statement that the musttail attribute
524 // applies to. nullptr if there is no 'musttail' on the current statement.
525 const CallExpr *MustTailCall = nullptr;
526
527 /// Returns true if a function must make progress, which means the
528 /// mustprogress attribute can be added.
529 bool checkIfFunctionMustProgress() {
530 if (CGM.getCodeGenOpts().getFiniteLoops() ==
531 CodeGenOptions::FiniteLoopsKind::Never)
532 return false;
533
534 // C++11 and later guarantees that a thread eventually will do one of the
535 // following (6.9.2.3.1 in C++11):
536 // - terminate,
537 // - make a call to a library I/O function,
538 // - perform an access through a volatile glvalue, or
539 // - perform a synchronization operation or an atomic operation.
540 //
541 // Hence each function is 'mustprogress' in C++11 or later.
542 return getLangOpts().CPlusPlus11;
543 }
544
545 /// Returns true if a loop must make progress, which means the mustprogress
546 /// attribute can be added. \p HasConstantCond indicates whether the branch
547 /// condition is a known constant.
548 bool checkIfLoopMustProgress(bool HasConstantCond) {
549 if (CGM.getCodeGenOpts().getFiniteLoops() ==
550 CodeGenOptions::FiniteLoopsKind::Always)
551 return true;
552 if (CGM.getCodeGenOpts().getFiniteLoops() ==
553 CodeGenOptions::FiniteLoopsKind::Never)
554 return false;
555
556 // If the containing function must make progress, loops also must make
557 // progress (as in C++11 and later).
558 if (checkIfFunctionMustProgress())
559 return true;
560
561 // Now apply rules for plain C (see 6.8.5.6 in C11).
562 // Loops with constant conditions do not have to make progress in any C
563 // version.
564 if (HasConstantCond)
565 return false;
566
567 // Loops with non-constant conditions must make progress in C11 and later.
568 return getLangOpts().C11;
569 }
570
571 const CodeGen::CGBlockInfo *BlockInfo = nullptr;
572 llvm::Value *BlockPointer = nullptr;
573
574 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
575 FieldDecl *LambdaThisCaptureField = nullptr;
576
577 /// A mapping from NRVO variables to the flags used to indicate
578 /// when the NRVO has been applied to this variable.
579 llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
580
581 EHScopeStack EHStack;
582 llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
583 llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack;
584
585 llvm::Instruction *CurrentFuncletPad = nullptr;
586
587 class CallLifetimeEnd final : public EHScopeStack::Cleanup {
588 bool isRedundantBeforeReturn() override { return true; }
589
590 llvm::Value *Addr;
591 llvm::Value *Size;
592
593 public:
594 CallLifetimeEnd(Address addr, llvm::Value *size)
595 : Addr(addr.getPointer()), Size(size) {}
596
597 void Emit(CodeGenFunction &CGF, Flags flags) override {
598 CGF.EmitLifetimeEnd(Size, Addr);
599 }
600 };
601
602 /// Header for data within LifetimeExtendedCleanupStack.
603 struct LifetimeExtendedCleanupHeader {
604 /// The size of the following cleanup object.
605 unsigned Size;
606 /// The kind of cleanup to push: a value from the CleanupKind enumeration.
607 unsigned Kind : 31;
608 /// Whether this is a conditional cleanup.
609 unsigned IsConditional : 1;
610
611 size_t getSize() const { return Size; }
612 CleanupKind getKind() const { return (CleanupKind)Kind; }
613 bool isConditional() const { return IsConditional; }
614 };
615
616 /// i32s containing the indexes of the cleanup destinations.
617 Address NormalCleanupDest = Address::invalid();
618
619 unsigned NextCleanupDestIndex = 1;
620
621 /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
622 llvm::BasicBlock *EHResumeBlock = nullptr;
623
624 /// The exception slot. All landing pads write the current exception pointer
625 /// into this alloca.
626 llvm::Value *ExceptionSlot = nullptr;
627
628 /// The selector slot. Under the MandatoryCleanup model, all landing pads
629 /// write the current selector value into this alloca.
630 llvm::AllocaInst *EHSelectorSlot = nullptr;
631
632 /// A stack of exception code slots. Entering an __except block pushes a slot
633 /// on the stack and leaving pops one. The __exception_code() intrinsic loads
634 /// a value from the top of the stack.
635 SmallVector<Address, 1> SEHCodeSlotStack;
636
637 /// Value returned by __exception_info intrinsic.
638 llvm::Value *SEHInfo = nullptr;
639
640 /// Emits a landing pad for the current EH stack.
641 llvm::BasicBlock *EmitLandingPad();
642
643 llvm::BasicBlock *getInvokeDestImpl();
644
645 /// Parent loop-based directive for scan directive.
646 const OMPExecutableDirective *OMPParentLoopDirectiveForScan = nullptr;
647 llvm::BasicBlock *OMPBeforeScanBlock = nullptr;
648 llvm::BasicBlock *OMPAfterScanBlock = nullptr;
649 llvm::BasicBlock *OMPScanExitBlock = nullptr;
650 llvm::BasicBlock *OMPScanDispatch = nullptr;
651 bool OMPFirstScanLoop = false;
652
653 /// Manages parent directive for scan directives.
654 class ParentLoopDirectiveForScanRegion {
655 CodeGenFunction &CGF;
656 const OMPExecutableDirective *ParentLoopDirectiveForScan;
657
658 public:
659 ParentLoopDirectiveForScanRegion(
660 CodeGenFunction &CGF,
661 const OMPExecutableDirective &ParentLoopDirectiveForScan)
662 : CGF(CGF),
663 ParentLoopDirectiveForScan(CGF.OMPParentLoopDirectiveForScan) {
664 CGF.OMPParentLoopDirectiveForScan = &ParentLoopDirectiveForScan;
665 }
666 ~ParentLoopDirectiveForScanRegion() {
667 CGF.OMPParentLoopDirectiveForScan = ParentLoopDirectiveForScan;
668 }
669 };
670
671 template <class T>
672 typename DominatingValue<T>::saved_type saveValueInCond(T value) {
673 return DominatingValue<T>::save(*this, value);
674 }
675
676 class CGFPOptionsRAII {
677 public:
678 CGFPOptionsRAII(CodeGenFunction &CGF, FPOptions FPFeatures);
679 CGFPOptionsRAII(CodeGenFunction &CGF, const Expr *E);
680 ~CGFPOptionsRAII();
681
682 private:
683 void ConstructorHelper(FPOptions FPFeatures);
684 CodeGenFunction &CGF;
685 FPOptions OldFPFeatures;
686 llvm::fp::ExceptionBehavior OldExcept;
687 llvm::RoundingMode OldRounding;
688 Optional<CGBuilderTy::FastMathFlagGuard> FMFGuard;
689 };
690 FPOptions CurFPFeatures;
691
692public:
693 /// ObjCEHValueStack - Stack of Objective-C exception values, used for
694 /// rethrows.
695 SmallVector<llvm::Value*, 8> ObjCEHValueStack;
696
697 /// A class controlling the emission of a finally block.
698 class FinallyInfo {
699 /// Where the catchall's edge through the cleanup should go.
700 JumpDest RethrowDest;
701
702 /// A function to call to enter the catch.
703 llvm::FunctionCallee BeginCatchFn;
704
705 /// An i1 variable indicating whether or not the @finally is
706 /// running for an exception.
707 llvm::AllocaInst *ForEHVar;
708
709 /// An i8* variable into which the exception pointer to rethrow
710 /// has been saved.
711 llvm::AllocaInst *SavedExnVar;
712
713 public:
714 void enter(CodeGenFunction &CGF, const Stmt *Finally,
715 llvm::FunctionCallee beginCatchFn,
716 llvm::FunctionCallee endCatchFn, llvm::FunctionCallee rethrowFn);
717 void exit(CodeGenFunction &CGF);
718 };
719
720 /// Returns true inside SEH __try blocks.
721 bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); }
722
723 /// Returns true while emitting a cleanuppad.
724 bool isCleanupPadScope() const {
725 return CurrentFuncletPad && isa<llvm::CleanupPadInst>(CurrentFuncletPad);
726 }
727
728 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
729 /// current full-expression. Safe against the possibility that
730 /// we're currently inside a conditionally-evaluated expression.
731 template <class T, class... As>
732 void pushFullExprCleanup(CleanupKind kind, As... A) {
733 // If we're not in a conditional branch, or if none of the
734 // arguments requires saving, then use the unconditional cleanup.
735 if (!isInConditionalBranch())
736 return EHStack.pushCleanup<T>(kind, A...);
737
738 // Stash values in a tuple so we can guarantee the order of saves.
739 typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
740 SavedTuple Saved{saveValueInCond(A)...};
741
742 typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType;
743 EHStack.pushCleanupTuple<CleanupType>(kind, Saved);
744 initFullExprCleanup();
745 }
746
747 /// Queue a cleanup to be pushed after finishing the current full-expression,
748 /// potentially with an active flag.
749 template <class T, class... As>
750 void pushCleanupAfterFullExpr(CleanupKind Kind, As... A) {
751 if (!isInConditionalBranch())
752 return pushCleanupAfterFullExprWithActiveFlag<T>(Kind, Address::invalid(),
753 A...);
754
755 Address ActiveFlag = createCleanupActiveFlag();
756 assert(!DominatingValue<Address>::needsSaving(ActiveFlag) &&
757 "cleanup active flag should never need saving");
758
759 typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple;
760 SavedTuple Saved{saveValueInCond(A)...};
761
762 typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType;
763 pushCleanupAfterFullExprWithActiveFlag<CleanupType>(Kind, ActiveFlag, Saved);
764 }
765
766 template <class T, class... As>
767 void pushCleanupAfterFullExprWithActiveFlag(CleanupKind Kind,
768 Address ActiveFlag, As... A) {
769 LifetimeExtendedCleanupHeader Header = {sizeof(T), Kind,
770 ActiveFlag.isValid()};
771
772 size_t OldSize = LifetimeExtendedCleanupStack.size();
773 LifetimeExtendedCleanupStack.resize(
774 LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size +
775 (Header.IsConditional ? sizeof(ActiveFlag) : 0));
776
777 static_assert(sizeof(Header) % alignof(T) == 0,
778 "Cleanup will be allocated on misaligned address");
779 char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
780 new (Buffer) LifetimeExtendedCleanupHeader(Header);
781 new (Buffer + sizeof(Header)) T(A...);
782 if (Header.IsConditional)
783 new (Buffer + sizeof(Header) + sizeof(T)) Address(ActiveFlag);
784 }
785
786 /// Set up the last cleanup that was pushed as a conditional
787 /// full-expression cleanup.
788 void initFullExprCleanup() {
789 initFullExprCleanupWithFlag(createCleanupActiveFlag());
790 }
791
792 void initFullExprCleanupWithFlag(Address ActiveFlag);
793 Address createCleanupActiveFlag();
794
795 /// PushDestructorCleanup - Push a cleanup to call the
796 /// complete-object destructor of an object of the given type at the
797 /// given address. Does nothing if T is not a C++ class type with a
798 /// non-trivial destructor.
799 void PushDestructorCleanup(QualType T, Address Addr);
800
801 /// PushDestructorCleanup - Push a cleanup to call the
802 /// complete-object variant of the given destructor on the object at
803 /// the given address.
804 void PushDestructorCleanup(const CXXDestructorDecl *Dtor, QualType T,
805 Address Addr);
806
807 /// PopCleanupBlock - Will pop the cleanup entry on the stack and
808 /// process all branch fixups.
809 void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
810
811 /// DeactivateCleanupBlock - Deactivates the given cleanup block.
812 /// The block cannot be reactivated. Pops it if it's the top of the
813 /// stack.
814 ///
815 /// \param DominatingIP - An instruction which is known to
816 /// dominate the current IP (if set) and which lies along
817 /// all paths of execution between the current IP and the
818 /// the point at which the cleanup comes into scope.
819 void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
820 llvm::Instruction *DominatingIP);
821
822 /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
823 /// Cannot be used to resurrect a deactivated cleanup.
824 ///
825 /// \param DominatingIP - An instruction which is known to
826 /// dominate the current IP (if set) and which lies along
827 /// all paths of execution between the current IP and the
828 /// the point at which the cleanup comes into scope.
829 void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
830 llvm::Instruction *DominatingIP);
831
832 /// Enters a new scope for capturing cleanups, all of which
833 /// will be executed once the scope is exited.
834 class RunCleanupsScope {
835 EHScopeStack::stable_iterator CleanupStackDepth, OldCleanupScopeDepth;
836 size_t LifetimeExtendedCleanupStackSize;
837 bool OldDidCallStackSave;
838 protected:
839 bool PerformCleanup;
840 private:
841
842 RunCleanupsScope(const RunCleanupsScope &) = delete;
843 void operator=(const RunCleanupsScope &) = delete;
844
845 protected:
846 CodeGenFunction& CGF;
847
848 public:
849 /// Enter a new cleanup scope.
850 explicit RunCleanupsScope(CodeGenFunction &CGF)
851 : PerformCleanup(true), CGF(CGF)
852 {
853 CleanupStackDepth = CGF.EHStack.stable_begin();
854 LifetimeExtendedCleanupStackSize =
855 CGF.LifetimeExtendedCleanupStack.size();
856 OldDidCallStackSave = CGF.DidCallStackSave;
857 CGF.DidCallStackSave = false;
858 OldCleanupScopeDepth = CGF.CurrentCleanupScopeDepth;
859 CGF.CurrentCleanupScopeDepth = CleanupStackDepth;
860 }
861
862 /// Exit this cleanup scope, emitting any accumulated cleanups.
863 ~RunCleanupsScope() {
864 if (PerformCleanup)
865 ForceCleanup();
866 }
867
868 /// Determine whether this scope requires any cleanups.
869 bool requiresCleanups() const {
870 return CGF.EHStack.stable_begin() != CleanupStackDepth;
871 }
872
873 /// Force the emission of cleanups now, instead of waiting
874 /// until this object is destroyed.
875 /// \param ValuesToReload - A list of values that need to be available at
876 /// the insertion point after cleanup emission. If cleanup emission created
877 /// a shared cleanup block, these value pointers will be rewritten.
878 /// Otherwise, they not will be modified.
879 void ForceCleanup(std::initializer_list<llvm::Value**> ValuesToReload = {}) {
880 assert(PerformCleanup && "Already forced cleanup");
881 CGF.DidCallStackSave = OldDidCallStackSave;
882 CGF.PopCleanupBlocks(CleanupStackDepth, LifetimeExtendedCleanupStackSize,
883 ValuesToReload);
884 PerformCleanup = false;
885 CGF.CurrentCleanupScopeDepth = OldCleanupScopeDepth;
886 }
887 };
888
889 // Cleanup stack depth of the RunCleanupsScope that was pushed most recently.
890 EHScopeStack::stable_iterator CurrentCleanupScopeDepth =
891 EHScopeStack::stable_end();
892
893 class LexicalScope : public RunCleanupsScope {
894 SourceRange Range;
895 SmallVector<const LabelDecl*, 4> Labels;
896 LexicalScope *ParentScope;
897
898 LexicalScope(const LexicalScope &) = delete;
899 void operator=(const LexicalScope &) = delete;
900
901 public:
902 /// Enter a new cleanup scope.
903 explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
904 : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
905 CGF.CurLexicalScope = this;
906 if (CGDebugInfo *DI = CGF.getDebugInfo())
907 DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
908 }
909
910 void addLabel(const LabelDecl *label) {
911 assert(PerformCleanup && "adding label to dead scope?");
912 Labels.push_back(label);
913 }
914
915 /// Exit this cleanup scope, emitting any accumulated
916 /// cleanups.
917 ~LexicalScope() {
918 if (CGDebugInfo *DI = CGF.getDebugInfo())
919 DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
920
921 // If we should perform a cleanup, force them now. Note that
922 // this ends the cleanup scope before rescoping any labels.
923 if (PerformCleanup) {
924 ApplyDebugLocation DL(CGF, Range.getEnd());
925 ForceCleanup();
926 }
927 }
928
929 /// Force the emission of cleanups now, instead of waiting
930 /// until this object is destroyed.
931 void ForceCleanup() {
932 CGF.CurLexicalScope = ParentScope;
933 RunCleanupsScope::ForceCleanup();
934
935 if (!Labels.empty())
936 rescopeLabels();
937 }
938
939 bool hasLabels() const {
940 return !Labels.empty();
941 }
942
943 void rescopeLabels();
944 };
945
946 typedef llvm::DenseMap<const Decl *, Address> DeclMapTy;
947
948 /// The class used to assign some variables some temporarily addresses.
949 class OMPMapVars {
950 DeclMapTy SavedLocals;
951 DeclMapTy SavedTempAddresses;
952 OMPMapVars(const OMPMapVars &) = delete;
953 void operator=(const OMPMapVars &) = delete;
954
955 public:
956 explicit OMPMapVars() = default;
957 ~OMPMapVars() {
958 assert(SavedLocals.empty() && "Did not restored original addresses.");
959 };
960
961 /// Sets the address of the variable \p LocalVD to be \p TempAddr in
962 /// function \p CGF.
963 /// \return true if at least one variable was set already, false otherwise.
964 bool setVarAddr(CodeGenFunction &CGF, const VarDecl *LocalVD,
965 Address TempAddr) {
966 LocalVD = LocalVD->getCanonicalDecl();
967 // Only save it once.
968 if (SavedLocals.count(LocalVD)) return false;
969
970 // Copy the existing local entry to SavedLocals.
971 auto it = CGF.LocalDeclMap.find(LocalVD);
972 if (it != CGF.LocalDeclMap.end())
973 SavedLocals.try_emplace(LocalVD, it->second);
974 else
975 SavedLocals.try_emplace(LocalVD, Address::invalid());
976
977 // Generate the private entry.
978 QualType VarTy = LocalVD->getType();
979 if (VarTy->isReferenceType()) {
980 Address Temp = CGF.CreateMemTemp(VarTy);
981 CGF.Builder.CreateStore(TempAddr.getPointer(), Temp);
982 TempAddr = Temp;
983 }
984 SavedTempAddresses.try_emplace(LocalVD, TempAddr);
985
986 return true;
987 }
988
989 /// Applies new addresses to the list of the variables.
990 /// \return true if at least one variable is using new address, false
991 /// otherwise.
992 bool apply(CodeGenFunction &CGF) {
993 copyInto(SavedTempAddresses, CGF.LocalDeclMap);
994 SavedTempAddresses.clear();
995 return !SavedLocals.empty();
996 }
997
998 /// Restores original addresses of the variables.
999 void restore(CodeGenFunction &CGF) {
1000 if (!SavedLocals.empty()) {
1001 copyInto(SavedLocals, CGF.LocalDeclMap);
1002 SavedLocals.clear();
1003 }
1004 }
1005
1006 private:
1007 /// Copy all the entries in the source map over the corresponding
1008 /// entries in the destination, which must exist.
1009 static void copyInto(const DeclMapTy &Src, DeclMapTy &Dest) {
1010 for (auto &Pair : Src) {
1011 if (!Pair.second.isValid()) {
1012 Dest.erase(Pair.first);
1013 continue;
1014 }
1015
1016 auto I = Dest.find(Pair.first);
1017 if (I != Dest.end())
1018 I->second = Pair.second;
1019 else
1020 Dest.insert(Pair);
1021 }
1022 }
1023 };
1024
1025 /// The scope used to remap some variables as private in the OpenMP loop body
1026 /// (or other captured region emitted without outlining), and to restore old
1027 /// vars back on exit.
1028 class OMPPrivateScope : public RunCleanupsScope {
1029 OMPMapVars MappedVars;
1030 OMPPrivateScope(const OMPPrivateScope &) = delete;
1031 void operator=(const OMPPrivateScope &) = delete;
1032
1033 public:
1034 /// Enter a new OpenMP private scope.
1035 explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
1036
1037 /// Registers \p LocalVD variable as a private and apply \p PrivateGen
1038 /// function for it to generate corresponding private variable. \p
1039 /// PrivateGen returns an address of the generated private variable.
1040 /// \return true if the variable is registered as private, false if it has
1041 /// been privatized already.
1042 bool addPrivate(const VarDecl *LocalVD,
1043 const llvm::function_ref<Address()> PrivateGen) {
1044 assert(PerformCleanup && "adding private to dead scope");
1045 return MappedVars.setVarAddr(CGF, LocalVD, PrivateGen());
1046 }
1047
1048 /// Privatizes local variables previously registered as private.
1049 /// Registration is separate from the actual privatization to allow
1050 /// initializers use values of the original variables, not the private one.
1051 /// This is important, for example, if the private variable is a class
1052 /// variable initialized by a constructor that references other private
1053 /// variables. But at initialization original variables must be used, not
1054 /// private copies.
1055 /// \return true if at least one variable was privatized, false otherwise.
1056 bool Privatize() { return MappedVars.apply(CGF); }
1057
1058 void ForceCleanup() {
1059 RunCleanupsScope::ForceCleanup();
1060 MappedVars.restore(CGF);
1061 }
1062
1063 /// Exit scope - all the mapped variables are restored.
1064 ~OMPPrivateScope() {
1065 if (PerformCleanup)
1066 ForceCleanup();
1067 }
1068
1069 /// Checks if the global variable is captured in current function.
1070 bool isGlobalVarCaptured(const VarDecl *VD) const {
1071 VD = VD->getCanonicalDecl();
1072 return !VD->isLocalVarDeclOrParm() && CGF.LocalDeclMap.count(VD) > 0;
1073 }
1074 };
1075
1076 /// Save/restore original map of previously emitted local vars in case when we
1077 /// need to duplicate emission of the same code several times in the same
1078 /// function for OpenMP code.
1079 class OMPLocalDeclMapRAII {
1080 CodeGenFunction &CGF;
1081 DeclMapTy SavedMap;
1082
1083 public:
1084 OMPLocalDeclMapRAII(CodeGenFunction &CGF)
1085 : CGF(CGF), SavedMap(CGF.LocalDeclMap) {}
1086 ~OMPLocalDeclMapRAII() { SavedMap.swap(CGF.LocalDeclMap); }
1087 };
1088
1089 /// Takes the old cleanup stack size and emits the cleanup blocks
1090 /// that have been added.
1091 void
1092 PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
1093 std::initializer_list<llvm::Value **> ValuesToReload = {});
1094
1095 /// Takes the old cleanup stack size and emits the cleanup blocks
1096 /// that have been added, then adds all lifetime-extended cleanups from
1097 /// the given position to the stack.
1098 void
1099 PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
1100 size_t OldLifetimeExtendedStackSize,
1101 std::initializer_list<llvm::Value **> ValuesToReload = {});
1102
1103 void ResolveBranchFixups(llvm::BasicBlock *Target);
1104
1105 /// The given basic block lies in the current EH scope, but may be a
1106 /// target of a potentially scope-crossing jump; get a stable handle
1107 /// to which we can perform this jump later.
1108 JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
1109 return JumpDest(Target,
1110 EHStack.getInnermostNormalCleanup(),
1111 NextCleanupDestIndex++);
1112 }
1113
1114 /// The given basic block lies in the current EH scope, but may be a
1115 /// target of a potentially scope-crossing jump; get a stable handle
1116 /// to which we can perform this jump later.
1117 JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
1118 return getJumpDestInCurrentScope(createBasicBlock(Name));
1119 }
1120
1121 /// EmitBranchThroughCleanup - Emit a branch from the current insert
1122 /// block through the normal cleanup handling code (if any) and then
1123 /// on to \arg Dest.
1124 void EmitBranchThroughCleanup(JumpDest Dest);
1125
1126 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
1127 /// specified destination obviously has no cleanups to run. 'false' is always
1128 /// a conservatively correct answer for this method.
1129 bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
1130
1131 /// popCatchScope - Pops the catch scope at the top of the EHScope
1132 /// stack, emitting any required code (other than the catch handlers
1133 /// themselves).
1134 void popCatchScope();
1135
1136 llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
1137 llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
1138 llvm::BasicBlock *
1139 getFuncletEHDispatchBlock(EHScopeStack::stable_iterator scope);
1140
1141 /// An object to manage conditionally-evaluated expressions.
1142 class ConditionalEvaluation {
1143 llvm::BasicBlock *StartBB;
1144
1145 public:
1146 ConditionalEvaluation(CodeGenFunction &CGF)
1147 : StartBB(CGF.Builder.GetInsertBlock()) {}
1148
1149 void begin(CodeGenFunction &CGF) {
1150 assert(CGF.OutermostConditional != this);
1151 if (!CGF.OutermostConditional)
1152 CGF.OutermostConditional = this;
1153 }
1154
1155 void end(CodeGenFunction &CGF) {
1156 assert(CGF.OutermostConditional != nullptr);
1157 if (CGF.OutermostConditional == this)
1158 CGF.OutermostConditional = nullptr;
1159 }
1160
1161 /// Returns a block which will be executed prior to each
1162 /// evaluation of the conditional code.
1163 llvm::BasicBlock *getStartingBlock() const {
1164 return StartBB;
1165 }
1166 };
1167
1168 /// isInConditionalBranch - Return true if we're currently emitting
1169 /// one branch or the other of a conditional expression.
1170 bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
1171
1172 void setBeforeOutermostConditional(llvm::Value *value, Address addr) {
1173 assert(isInConditionalBranch());
1174 llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
1175 auto store = new llvm::StoreInst(value, addr.getPointer(), &block->back());
1176 store->setAlignment(addr.getAlignment().getAsAlign());
1177 }
1178
1179 /// An RAII object to record that we're evaluating a statement
1180 /// expression.
1181 class StmtExprEvaluation {
1182 CodeGenFunction &CGF;
1183
1184 /// We have to save the outermost conditional: cleanups in a
1185 /// statement expression aren't conditional just because the
1186 /// StmtExpr is.
1187 ConditionalEvaluation *SavedOutermostConditional;
1188
1189 public:
1190 StmtExprEvaluation(CodeGenFunction &CGF)
1191 : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
1192 CGF.OutermostConditional = nullptr;
1193 }
1194
1195 ~StmtExprEvaluation() {
1196 CGF.OutermostConditional = SavedOutermostConditional;
1197 CGF.EnsureInsertPoint();
1198 }
1199 };
1200
1201 /// An object which temporarily prevents a value from being
1202 /// destroyed by aggressive peephole optimizations that assume that
1203 /// all uses of a value have been realized in the IR.
1204 class PeepholeProtection {
1205 llvm::Instruction *Inst;
1206 friend class CodeGenFunction;
1207
1208 public:
1209 PeepholeProtection() : Inst(nullptr) {}
1210 };
1211
1212 /// A non-RAII class containing all the information about a bound
1213 /// opaque value. OpaqueValueMapping, below, is a RAII wrapper for
1214 /// this which makes individual mappings very simple; using this
1215 /// class directly is useful when you have a variable number of
1216 /// opaque values or don't want the RAII functionality for some
1217 /// reason.
1218 class OpaqueValueMappingData {
1219 const OpaqueValueExpr *OpaqueValue;
1220 bool BoundLValue;
1221 CodeGenFunction::PeepholeProtection Protection;
1222
1223 OpaqueValueMappingData(const OpaqueValueExpr *ov,
1224 bool boundLValue)
1225 : OpaqueValue(ov), BoundLValue(boundLValue) {}
1226 public:
1227 OpaqueValueMappingData() : OpaqueValue(nullptr) {}
1228
1229 static bool shouldBindAsLValue(const Expr *expr) {
1230 // gl-values should be bound as l-values for obvious reasons.
1231 // Records should be bound as l-values because IR generation
1232 // always keeps them in memory. Expressions of function type
1233 // act exactly like l-values but are formally required to be
1234 // r-values in C.
1235 return expr->isGLValue() ||
1236 expr->getType()->isFunctionType() ||
1237 hasAggregateEvaluationKind(expr->getType());
1238 }
1239
1240 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
1241 const OpaqueValueExpr *ov,
1242 const Expr *e) {
1243 if (shouldBindAsLValue(ov))
1244 return bind(CGF, ov, CGF.EmitLValue(e));
1245 return bind(CGF, ov, CGF.EmitAnyExpr(e));
1246 }
1247
1248 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
1249 const OpaqueValueExpr *ov,
1250 const LValue &lv) {
1251 assert(shouldBindAsLValue(ov));
1252 CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
1253 return OpaqueValueMappingData(ov, true);
1254 }
1255
1256 static OpaqueValueMappingData bind(CodeGenFunction &CGF,
1257 const OpaqueValueExpr *ov,
1258 const RValue &rv) {
1259 assert(!shouldBindAsLValue(ov));
1260 CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
1261
1262 OpaqueValueMappingData data(ov, false);
1263
1264 // Work around an extremely aggressive peephole optimization in
1265 // EmitScalarConversion which assumes that all other uses of a
1266 // value are extant.
1267 data.Protection = CGF.protectFromPeepholes(rv);
1268
1269 return data;
1270 }
1271
1272 bool isValid() const { return OpaqueValue != nullptr; }
1273 void clear() { OpaqueValue = nullptr; }
1274
1275 void unbind(CodeGenFunction &CGF) {
1276 assert(OpaqueValue && "no data to unbind!");
1277
1278 if (BoundLValue) {
1279 CGF.OpaqueLValues.erase(OpaqueValue);
1280 } else {
1281 CGF.OpaqueRValues.erase(OpaqueValue);
1282 CGF.unprotectFromPeepholes(Protection);
1283 }
1284 }
1285 };
1286
1287 /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
1288 class OpaqueValueMapping {
1289 CodeGenFunction &CGF;
1290 OpaqueValueMappingData Data;
1291
1292 public:
1293 static bool shouldBindAsLValue(const Expr *expr) {
1294 return OpaqueValueMappingData::shouldBindAsLValue(expr);
1295 }
1296
1297 /// Build the opaque value mapping for the given conditional
1298 /// operator if it's the GNU ?: extension. This is a common
1299 /// enough pattern that the convenience operator is really
1300 /// helpful.
1301 ///
1302 OpaqueValueMapping(CodeGenFunction &CGF,
1303 const AbstractConditionalOperator *op) : CGF(CGF) {
1304 if (isa<ConditionalOperator>(op))
1305 // Leave Data empty.
1306 return;
1307
1308 const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
1309 Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
1310 e->getCommon());
1311 }
1312
1313 /// Build the opaque value mapping for an OpaqueValueExpr whose source
1314 /// expression is set to the expression the OVE represents.
1315 OpaqueValueMapping(CodeGenFunction &CGF, const OpaqueValueExpr *OV)
1316 : CGF(CGF) {
1317 if (OV) {
1318 assert(OV->getSourceExpr() && "wrong form of OpaqueValueMapping used "
1319 "for OVE with no source expression");
1320 Data = OpaqueValueMappingData::bind(CGF, OV, OV->getSourceExpr());
1321 }
1322 }
1323
1324 OpaqueValueMapping(CodeGenFunction &CGF,
1325 const OpaqueValueExpr *opaqueValue,
1326 LValue lvalue)
1327 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
1328 }
1329
1330 OpaqueValueMapping(CodeGenFunction &CGF,
1331 const OpaqueValueExpr *opaqueValue,
1332 RValue rvalue)
1333 : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
1334 }
1335
1336 void pop() {
1337 Data.unbind(CGF);
1338 Data.clear();
1339 }
1340
1341 ~OpaqueValueMapping() {
1342 if (Data.isValid()) Data.unbind(CGF);
1343 }
1344 };
1345
1346private:
1347 CGDebugInfo *DebugInfo;
1348 /// Used to create unique names for artificial VLA size debug info variables.
1349 unsigned VLAExprCounter = 0;
1350 bool DisableDebugInfo = false;
1351
1352 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
1353 /// calling llvm.stacksave for multiple VLAs in the same scope.
1354 bool DidCallStackSave = false;
1355
1356 /// IndirectBranch - The first time an indirect goto is seen we create a block
1357 /// with an indirect branch. Every time we see the address of a label taken,
1358 /// we add the label to the indirect goto. Every subsequent indirect goto is
1359 /// codegen'd as a jump to the IndirectBranch's basic block.
1360 llvm::IndirectBrInst *IndirectBranch = nullptr;
1361
1362 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
1363 /// decls.
1364 DeclMapTy LocalDeclMap;
1365
1366 // Keep track of the cleanups for callee-destructed parameters pushed to the
1367 // cleanup stack so that they can be deactivated later.
1368 llvm::DenseMap<const ParmVarDecl *, EHScopeStack::stable_iterator>
1369 CalleeDestructedParamCleanups;
1370
1371 /// SizeArguments - If a ParmVarDecl had the pass_object_size attribute, this
1372 /// will contain a mapping from said ParmVarDecl to its implicit "object_size"
1373 /// parameter.
1374 llvm::SmallDenseMap<const ParmVarDecl *, const ImplicitParamDecl *, 2>
1375 SizeArguments;
1376
1377 /// Track escaped local variables with auto storage. Used during SEH
1378 /// outlining to produce a call to llvm.localescape.
1379 llvm::DenseMap<llvm::AllocaInst *, int> EscapedLocals;
1380
1381 /// LabelMap - This keeps track of the LLVM basic block for each C label.
1382 llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
1383
1384 // BreakContinueStack - This keeps track of where break and continue
1385 // statements should jump to.
1386 struct BreakContinue {
1387 BreakContinue(JumpDest Break, JumpDest Continue)
1388 : BreakBlock(Break), ContinueBlock(Continue) {}
1389
1390 JumpDest BreakBlock;
1391 JumpDest ContinueBlock;
1392 };
1393 SmallVector<BreakContinue, 8> BreakContinueStack;
1394
1395 /// Handles cancellation exit points in OpenMP-related constructs.
1396 class OpenMPCancelExitStack {
1397 /// Tracks cancellation exit point and join point for cancel-related exit
1398 /// and normal exit.
1399 struct CancelExit {
1400 CancelExit() = default;
1401 CancelExit(OpenMPDirectiveKind Kind, JumpDest ExitBlock,
1402 JumpDest ContBlock)
1403 : Kind(Kind), ExitBlock(ExitBlock), ContBlock(ContBlock) {}
1404 OpenMPDirectiveKind Kind = llvm::omp::OMPD_unknown;
1405 /// true if the exit block has been emitted already by the special
1406 /// emitExit() call, false if the default codegen is used.
1407 bool HasBeenEmitted = false;
1408 JumpDest ExitBlock;
1409 JumpDest ContBlock;
1410 };
1411
1412 SmallVector<CancelExit, 8> Stack;
1413
1414 public:
1415 OpenMPCancelExitStack() : Stack(1) {}
1416 ~OpenMPCancelExitStack() = default;
1417 /// Fetches the exit block for the current OpenMP construct.
1418 JumpDest getExitBlock() const { return Stack.back().ExitBlock; }
1419 /// Emits exit block with special codegen procedure specific for the related
1420 /// OpenMP construct + emits code for normal construct cleanup.
1421 void emitExit(CodeGenFunction &CGF, OpenMPDirectiveKind Kind,
1422 const llvm::function_ref<void(CodeGenFunction &)> CodeGen) {
1423 if (Stack.back().Kind == Kind && getExitBlock().isValid()) {
1424 assert(CGF.getOMPCancelDestination(Kind).isValid());
1425 assert(CGF.HaveInsertPoint());
1426 assert(!Stack.back().HasBeenEmitted);
1427 auto IP = CGF.Builder.saveAndClearIP();
1428 CGF.EmitBlock(Stack.back().ExitBlock.getBlock());
1429 CodeGen(CGF);
1430 CGF.EmitBranch(Stack.back().ContBlock.getBlock());
1431 CGF.Builder.restoreIP(IP);
1432 Stack.back().HasBeenEmitted = true;
1433 }
1434 CodeGen(CGF);
1435 }
1436 /// Enter the cancel supporting \a Kind construct.
1437 /// \param Kind OpenMP directive that supports cancel constructs.
1438 /// \param HasCancel true, if the construct has inner cancel directive,
1439 /// false otherwise.
1440 void enter(CodeGenFunction &CGF, OpenMPDirectiveKind Kind, bool HasCancel) {
1441 Stack.push_back({Kind,
1442 HasCancel ? CGF.getJumpDestInCurrentScope("cancel.exit")
1443 : JumpDest(),
1444 HasCancel ? CGF.getJumpDestInCurrentScope("cancel.cont")
1445 : JumpDest()});
1446 }
1447 /// Emits default exit point for the cancel construct (if the special one
1448 /// has not be used) + join point for cancel/normal exits.
1449 void exit(CodeGenFunction &CGF) {
1450 if (getExitBlock().isValid()) {
1451 assert(CGF.getOMPCancelDestination(Stack.back().Kind).isValid());
1452 bool HaveIP = CGF.HaveInsertPoint();
1453 if (!Stack.back().HasBeenEmitted) {
1454 if (HaveIP)
1455 CGF.EmitBranchThroughCleanup(Stack.back().ContBlock);
1456 CGF.EmitBlock(Stack.back().ExitBlock.getBlock());
1457 CGF.EmitBranchThroughCleanup(Stack.back().ContBlock);
1458 }
1459 CGF.EmitBlock(Stack.back().ContBlock.getBlock());
1460 if (!HaveIP) {
1461 CGF.Builder.CreateUnreachable();
1462 CGF.Builder.ClearInsertionPoint();
1463 }
1464 }
1465 Stack.pop_back();
1466 }
1467 };
1468 OpenMPCancelExitStack OMPCancelStack;
1469
1470 /// Lower the Likelihood knowledge about the \p Cond via llvm.expect intrin.
1471 llvm::Value *emitCondLikelihoodViaExpectIntrinsic(llvm::Value *Cond,
1472 Stmt::Likelihood LH);
1473
1474 CodeGenPGO PGO;
1475
1476 /// Calculate branch weights appropriate for PGO data
1477 llvm::MDNode *createProfileWeights(uint64_t TrueCount,
1478 uint64_t FalseCount) const;
1479 llvm::MDNode *createProfileWeights(ArrayRef<uint64_t> Weights) const;
1480 llvm::MDNode *createProfileWeightsForLoop(const Stmt *Cond,
1481 uint64_t LoopCount) const;
1482
1483public:
1484 /// Increment the profiler's counter for the given statement by \p StepV.
1485 /// If \p StepV is null, the default increment is 1.
1486 void incrementProfileCounter(const Stmt *S, llvm::Value *StepV = nullptr) {
1487 if (CGM.getCodeGenOpts().hasProfileClangInstr() &&
1488 !CurFn->hasFnAttribute(llvm::Attribute::NoProfile))
1489 PGO.emitCounterIncrement(Builder, S, StepV);
1490 PGO.setCurrentStmt(S);
1491 }
1492
1493 /// Get the profiler's count for the given statement.
1494 uint64_t getProfileCount(const Stmt *S) {
1495 Optional<uint64_t> Count = PGO.getStmtCount(S);
1496 if (!Count.hasValue())
1497 return 0;
1498 return *Count;
1499 }
1500
1501 /// Set the profiler's current count.
1502 void setCurrentProfileCount(uint64_t Count) {
1503 PGO.setCurrentRegionCount(Count);
1504 }
1505
1506 /// Get the profiler's current count. This is generally the count for the most
1507 /// recently incremented counter.
1508 uint64_t getCurrentProfileCount() {
1509 return PGO.getCurrentRegionCount();
1510 }
1511
1512private:
1513
1514 /// SwitchInsn - This is nearest current switch instruction. It is null if
1515 /// current context is not in a switch.
1516 llvm::SwitchInst *SwitchInsn = nullptr;
1517 /// The branch weights of SwitchInsn when doing instrumentation based PGO.
1518 SmallVector<uint64_t, 16> *SwitchWeights = nullptr;
1519
1520 /// The likelihood attributes of the SwitchCase.
1521 SmallVector<Stmt::Likelihood, 16> *SwitchLikelihood = nullptr;
1522
1523 /// CaseRangeBlock - This block holds if condition check for last case
1524 /// statement range in current switch instruction.
1525 llvm::BasicBlock *CaseRangeBlock = nullptr;
1526
1527 /// OpaqueLValues - Keeps track of the current set of opaque value
1528 /// expressions.
1529 llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
1530 llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
1531
1532 // VLASizeMap - This keeps track of the associated size for each VLA type.
1533 // We track this by the size expression rather than the type itself because
1534 // in certain situations, like a const qualifier applied to an VLA typedef,
1535 // multiple VLA types can share the same size expression.
1536 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
1537 // enter/leave scopes.
1538 llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
1539
1540 /// A block containing a single 'unreachable' instruction. Created
1541 /// lazily by getUnreachableBlock().
1542 llvm::BasicBlock *UnreachableBlock = nullptr;
1543
1544 /// Counts of the number return expressions in the function.
1545 unsigned NumReturnExprs = 0;
1546
1547 /// Count the number of simple (constant) return expressions in the function.
1548 unsigned NumSimpleReturnExprs = 0;
1549
1550 /// The last regular (non-return) debug location (breakpoint) in the function.
1551 SourceLocation LastStopPoint;
1552
1553public:
1554 /// Source location information about the default argument or member
1555 /// initializer expression we're evaluating, if any.
1556 CurrentSourceLocExprScope CurSourceLocExprScope;
1557 using SourceLocExprScopeGuard =
1558 CurrentSourceLocExprScope::SourceLocExprScopeGuard;
1559
1560 /// A scope within which we are constructing the fields of an object which
1561 /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
1562 /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
1563 class FieldConstructionScope {
1564 public:
1565 FieldConstructionScope(CodeGenFunction &CGF, Address This)
1566 : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
1567 CGF.CXXDefaultInitExprThis = This;
1568 }
1569 ~FieldConstructionScope() {
1570 CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
1571 }
1572
1573 private:
1574 CodeGenFunction &CGF;
1575 Address OldCXXDefaultInitExprThis;
1576 };
1577
1578 /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
1579 /// is overridden to be the object under construction.
1580 class CXXDefaultInitExprScope {
1581 public:
1582 CXXDefaultInitExprScope(CodeGenFunction &CGF, const CXXDefaultInitExpr *E)
1583 : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue),
1584 OldCXXThisAlignment(CGF.CXXThisAlignment),
1585 SourceLocScope(E, CGF.CurSourceLocExprScope) {
1586 CGF.CXXThisValue = CGF.CXXDefaultInitExprThis.getPointer();
1587 CGF.CXXThisAlignment = CGF.CXXDefaultInitExprThis.getAlignment();
1588 }
1589 ~CXXDefaultInitExprScope() {
1590 CGF.CXXThisValue = OldCXXThisValue;
1591 CGF.CXXThisAlignment = OldCXXThisAlignment;
1592 }
1593
1594 public:
1595 CodeGenFunction &CGF;
1596 llvm::Value *OldCXXThisValue;
1597 CharUnits OldCXXThisAlignment;
1598 SourceLocExprScopeGuard SourceLocScope;
1599 };
1600
1601 struct CXXDefaultArgExprScope : SourceLocExprScopeGuard {
1602 CXXDefaultArgExprScope(CodeGenFunction &CGF, const CXXDefaultArgExpr *E)
1603 : SourceLocExprScopeGuard(E, CGF.CurSourceLocExprScope) {}
1604 };
1605
1606 /// The scope of an ArrayInitLoopExpr. Within this scope, the value of the
1607 /// current loop index is overridden.
1608 class ArrayInitLoopExprScope {
1609 public:
1610 ArrayInitLoopExprScope(CodeGenFunction &CGF, llvm::Value *Index)
1611 : CGF(CGF), OldArrayInitIndex(CGF.ArrayInitIndex) {
1612 CGF.ArrayInitIndex = Index;
1613 }
1614 ~ArrayInitLoopExprScope() {
1615 CGF.ArrayInitIndex = OldArrayInitIndex;
1616 }
1617
1618 private:
1619 CodeGenFunction &CGF;
1620 llvm::Value *OldArrayInitIndex;
1621 };
1622
1623 class InlinedInheritingConstructorScope {
1624 public:
1625 InlinedInheritingConstructorScope(CodeGenFunction &CGF, GlobalDecl GD)
1626 : CGF(CGF), OldCurGD(CGF.CurGD), OldCurFuncDecl(CGF.CurFuncDecl),
1627 OldCurCodeDecl(CGF.CurCodeDecl),
1628 OldCXXABIThisDecl(CGF.CXXABIThisDecl),
1629 OldCXXABIThisValue(CGF.CXXABIThisValue),
1630 OldCXXThisValue(CGF.CXXThisValue),
1631 OldCXXABIThisAlignment(CGF.CXXABIThisAlignment),
1632 OldCXXThisAlignment(CGF.CXXThisAlignment),
1633 OldReturnValue(CGF.ReturnValue), OldFnRetTy(CGF.FnRetTy),
1634 OldCXXInheritedCtorInitExprArgs(
1635 std::move(CGF.CXXInheritedCtorInitExprArgs)) {
1636 CGF.CurGD = GD;
1637 CGF.CurFuncDecl = CGF.CurCodeDecl =
1638 cast<CXXConstructorDecl>(GD.getDecl());
1639 CGF.CXXABIThisDecl = nullptr;
1640 CGF.CXXABIThisValue = nullptr;
1641 CGF.CXXThisValue = nullptr;
1642 CGF.CXXABIThisAlignment = CharUnits();
1643 CGF.CXXThisAlignment = CharUnits();
1644 CGF.ReturnValue = Address::invalid();
1645 CGF.FnRetTy = QualType();
1646 CGF.CXXInheritedCtorInitExprArgs.clear();
1647 }
1648 ~InlinedInheritingConstructorScope() {
1649 CGF.CurGD = OldCurGD;
1650 CGF.CurFuncDecl = OldCurFuncDecl;
1651 CGF.CurCodeDecl = OldCurCodeDecl;
1652 CGF.CXXABIThisDecl = OldCXXABIThisDecl;
1653 CGF.CXXABIThisValue = OldCXXABIThisValue;
1654 CGF.CXXThisValue = OldCXXThisValue;
1655 CGF.CXXABIThisAlignment = OldCXXABIThisAlignment;
1656 CGF.CXXThisAlignment = OldCXXThisAlignment;
1657 CGF.ReturnValue = OldReturnValue;
1658 CGF.FnRetTy = OldFnRetTy;
1659 CGF.CXXInheritedCtorInitExprArgs =
1660 std::move(OldCXXInheritedCtorInitExprArgs);
1661 }
1662
1663 private:
1664 CodeGenFunction &CGF;
1665 GlobalDecl OldCurGD;
1666 const Decl *OldCurFuncDecl;
1667 const Decl *OldCurCodeDecl;
1668 ImplicitParamDecl *OldCXXABIThisDecl;
1669 llvm::Value *OldCXXABIThisValue;
1670 llvm::Value *OldCXXThisValue;
1671 CharUnits OldCXXABIThisAlignment;
1672 CharUnits OldCXXThisAlignment;
1673 Address OldReturnValue;
1674 QualType OldFnRetTy;
1675 CallArgList OldCXXInheritedCtorInitExprArgs;
1676 };
1677
1678 // Helper class for the OpenMP IR Builder. Allows reusability of code used for
1679 // region body, and finalization codegen callbacks. This will class will also
1680 // contain privatization functions used by the privatization call backs
1681 //
1682 // TODO: this is temporary class for things that are being moved out of
1683 // CGOpenMPRuntime, new versions of current CodeGenFunction methods, or
1684 // utility function for use with the OMPBuilder. Once that move to use the
1685 // OMPBuilder is done, everything here will either become part of CodeGenFunc.
1686 // directly, or a new helper class that will contain functions used by both
1687 // this and the OMPBuilder
1688
1689 struct OMPBuilderCBHelpers {
1690
1691 OMPBuilderCBHelpers() = delete;
1692 OMPBuilderCBHelpers(const OMPBuilderCBHelpers &) = delete;
1693 OMPBuilderCBHelpers &operator=(const OMPBuilderCBHelpers &) = delete;
1694
1695 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
1696
1697 /// Cleanup action for allocate support.
1698 class OMPAllocateCleanupTy final : public EHScopeStack::Cleanup {
1699
1700 private:
1701 llvm::CallInst *RTLFnCI;
1702
1703 public:
1704 OMPAllocateCleanupTy(llvm::CallInst *RLFnCI) : RTLFnCI(RLFnCI) {
1705 RLFnCI->removeFromParent();
1706 }
1707
1708 void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
1709 if (!CGF.HaveInsertPoint())
1710 return;
1711 CGF.Builder.Insert(RTLFnCI);
1712 }
1713 };
1714
1715 /// Returns address of the threadprivate variable for the current
1716 /// thread. This Also create any necessary OMP runtime calls.
1717 ///
1718 /// \param VD VarDecl for Threadprivate variable.
1719 /// \param VDAddr Address of the Vardecl
1720 /// \param Loc The location where the barrier directive was encountered
1721 static Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1722 const VarDecl *VD, Address VDAddr,
1723 SourceLocation Loc);
1724
1725 /// Gets the OpenMP-specific address of the local variable /p VD.
1726 static Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1727 const VarDecl *VD);
1728 /// Get the platform-specific name separator.
1729 /// \param Parts different parts of the final name that needs separation
1730 /// \param FirstSeparator First separator used between the initial two
1731 /// parts of the name.
1732 /// \param Separator separator used between all of the rest consecutinve
1733 /// parts of the name
1734 static std::string getNameWithSeparators(ArrayRef<StringRef> Parts,
1735 StringRef FirstSeparator = ".",
1736 StringRef Separator = ".");
1737 /// Emit the Finalization for an OMP region
1738 /// \param CGF The Codegen function this belongs to
1739 /// \param IP Insertion point for generating the finalization code.
1740 static void FinalizeOMPRegion(CodeGenFunction &CGF, InsertPointTy IP) {
1741 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
1742 assert(IP.getBlock()->end() != IP.getPoint() &&
1743 "OpenMP IR Builder should cause terminated block!");
1744
1745 llvm::BasicBlock *IPBB = IP.getBlock();
1746 llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor();
1747 assert(DestBB && "Finalization block should have one successor!");
1748
1749 // erase and replace with cleanup branch.
1750 IPBB->getTerminator()->eraseFromParent();
1751 CGF.Builder.SetInsertPoint(IPBB);
1752 CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB);
1753 CGF.EmitBranchThroughCleanup(Dest);
1754 }
1755
1756 /// Emit the body of an OMP region
1757 /// \param CGF The Codegen function this belongs to
1758 /// \param RegionBodyStmt The body statement for the OpenMP region being
1759 /// generated
1760 /// \param CodeGenIP Insertion point for generating the body code.
1761 /// \param FiniBB The finalization basic block
1762 static void EmitOMPRegionBody(CodeGenFunction &CGF,
1763 const Stmt *RegionBodyStmt,
1764 InsertPointTy CodeGenIP,
1765 llvm::BasicBlock &FiniBB) {
1766 llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
1767 if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator())
1768 CodeGenIPBBTI->eraseFromParent();
1769
1770 CGF.Builder.SetInsertPoint(CodeGenIPBB);
1771
1772 CGF.EmitStmt(RegionBodyStmt);
1773
1774 if (CGF.Builder.saveIP().isSet())
1775 CGF.Builder.CreateBr(&FiniBB);
1776 }
1777
1778 /// RAII for preserving necessary info during Outlined region body codegen.
1779 class OutlinedRegionBodyRAII {
1780
1781 llvm::AssertingVH<llvm::Instruction> OldAllocaIP;
1782 CodeGenFunction::JumpDest OldReturnBlock;
1783 CGBuilderTy::InsertPoint IP;
1784 CodeGenFunction &CGF;
1785
1786 public:
1787 OutlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP,
1788 llvm::BasicBlock &RetBB)
1789 : CGF(cgf) {
1790 assert(AllocaIP.isSet() &&
1791 "Must specify Insertion point for allocas of outlined function");
1792 OldAllocaIP = CGF.AllocaInsertPt;
1793 CGF.AllocaInsertPt = &*AllocaIP.getPoint();
1794 IP = CGF.Builder.saveIP();
1795
1796 OldReturnBlock = CGF.ReturnBlock;
1797 CGF.ReturnBlock = CGF.getJumpDestInCurrentScope(&RetBB);
1798 }
1799
1800 ~OutlinedRegionBodyRAII() {
1801 CGF.AllocaInsertPt = OldAllocaIP;
1802 CGF.ReturnBlock = OldReturnBlock;
1803 CGF.Builder.restoreIP(IP);
1804 }
1805 };
1806
1807 /// RAII for preserving necessary info during inlined region body codegen.
1808 class InlinedRegionBodyRAII {
1809
1810 llvm::AssertingVH<llvm::Instruction> OldAllocaIP;
1811 CodeGenFunction &CGF;
1812
1813 public:
1814 InlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP,
1815 llvm::BasicBlock &FiniBB)
1816 : CGF(cgf) {
1817 // Alloca insertion block should be in the entry block of the containing
1818 // function so it expects an empty AllocaIP in which case will reuse the
1819 // old alloca insertion point, or a new AllocaIP in the same block as
1820 // the old one
1821 assert((!AllocaIP.isSet() ||
1822 CGF.AllocaInsertPt->getParent() == AllocaIP.getBlock()) &&
1823 "Insertion point should be in the entry block of containing "
1824 "function!");
1825 OldAllocaIP = CGF.AllocaInsertPt;
1826 if (AllocaIP.isSet())
1827 CGF.AllocaInsertPt = &*AllocaIP.getPoint();
1828
1829 // TODO: Remove the call, after making sure the counter is not used by
1830 // the EHStack.
1831 // Since this is an inlined region, it should not modify the
1832 // ReturnBlock, and should reuse the one for the enclosing outlined
1833 // region. So, the JumpDest being return by the function is discarded
1834 (void)CGF.getJumpDestInCurrentScope(&FiniBB);
1835 }
1836
1837 ~InlinedRegionBodyRAII() { CGF.AllocaInsertPt = OldAllocaIP; }
1838 };
1839 };
1840
1841private:
1842 /// CXXThisDecl - When generating code for a C++ member function,
1843 /// this will hold the implicit 'this' declaration.
1844 ImplicitParamDecl *CXXABIThisDecl = nullptr;
1845 llvm::Value *CXXABIThisValue = nullptr;
1846 llvm::Value *CXXThisValue = nullptr;
1847 CharUnits CXXABIThisAlignment;
1848 CharUnits CXXThisAlignment;
1849
1850 /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
1851 /// this expression.
1852 Address CXXDefaultInitExprThis = Address::invalid();
1853
1854 /// The current array initialization index when evaluating an
1855 /// ArrayInitIndexExpr within an ArrayInitLoopExpr.
1856 llvm::Value *ArrayInitIndex = nullptr;
1857
1858 /// The values of function arguments to use when evaluating
1859 /// CXXInheritedCtorInitExprs within this context.
1860 CallArgList CXXInheritedCtorInitExprArgs;
1861
1862 /// CXXStructorImplicitParamDecl - When generating code for a constructor or
1863 /// destructor, this will hold the implicit argument (e.g. VTT).
1864 ImplicitParamDecl *CXXStructorImplicitParamDecl = nullptr;
1865 llvm::Value *CXXStructorImplicitParamValue = nullptr;
1866
1867 /// OutermostConditional - Points to the outermost active
1868 /// conditional control. This is used so that we know if a
1869 /// temporary should be destroyed conditionally.
1870 ConditionalEvaluation *OutermostConditional = nullptr;
1871
1872 /// The current lexical scope.
1873 LexicalScope *CurLexicalScope = nullptr;
1874
1875 /// The current source location that should be used for exception
1876 /// handling code.
1877 SourceLocation CurEHLocation;
1878
1879 /// BlockByrefInfos - For each __block variable, contains
1880 /// information about the layout of the variable.
1881 llvm::DenseMap<const ValueDecl *, BlockByrefInfo> BlockByrefInfos;
1882
1883 /// Used by -fsanitize=nullability-return to determine whether the return
1884 /// value can be checked.
1885 llvm::Value *RetValNullabilityPrecondition = nullptr;
1886
1887 /// Check if -fsanitize=nullability-return instrumentation is required for
1888 /// this function.
1889 bool requiresReturnValueNullabilityCheck() const {
1890 return RetValNullabilityPrecondition;
1891 }
1892
1893 /// Used to store precise source locations for return statements by the
1894 /// runtime return value checks.
1895 Address ReturnLocation = Address::invalid();
1896
1897 /// Check if the return value of this function requires sanitization.
1898 bool requiresReturnValueCheck() const;
1899
1900 llvm::BasicBlock *TerminateLandingPad = nullptr;
1901 llvm::BasicBlock *TerminateHandler = nullptr;
1902 llvm::SmallVector<llvm::BasicBlock *, 2> TrapBBs;
1903
1904 /// Terminate funclets keyed by parent funclet pad.
1905 llvm::MapVector<llvm::Value *, llvm::BasicBlock *> TerminateFunclets;
1906
1907 /// Largest vector width used in ths function. Will be used to create a
1908 /// function attribute.
1909 unsigned LargestVectorWidth = 0;
1910
1911 /// True if we need emit the life-time markers. This is initially set in
1912 /// the constructor, but could be overwritten to true if this is a coroutine.
1913 bool ShouldEmitLifetimeMarkers;
1914
1915 /// Add OpenCL kernel arg metadata and the kernel attribute metadata to
1916 /// the function metadata.
1917 void EmitOpenCLKernelMetadata(const FunctionDecl *FD,
1918 llvm::Function *Fn);
1919
1920public:
1921 CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
1922 ~CodeGenFunction();
1923
1924 CodeGenTypes &getTypes() const { return CGM.getTypes(); }
1925 ASTContext &getContext() const { return CGM.getContext(); }
1926 CGDebugInfo *getDebugInfo() {
1927 if (DisableDebugInfo)
1928 return nullptr;
1929 return DebugInfo;
1930 }
1931 void disableDebugInfo() { DisableDebugInfo = true; }
1932 void enableDebugInfo() { DisableDebugInfo = false; }
1933
1934 bool shouldUseFusedARCCalls() {
1935 return CGM.getCodeGenOpts().OptimizationLevel == 0;
1936 }
1937
1938 const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
1939
1940 /// Returns a pointer to the function's exception object and selector slot,
1941 /// which is assigned in every landing pad.
1942 Address getExceptionSlot();
1943 Address getEHSelectorSlot();
1944
1945 /// Returns the contents of the function's exception object and selector
1946 /// slots.
1947 llvm::Value *getExceptionFromSlot();
1948 llvm::Value *getSelectorFromSlot();
1949
1950 Address getNormalCleanupDestSlot();
1951
1952 llvm::BasicBlock *getUnreachableBlock() {
1953 if (!UnreachableBlock) {
1954 UnreachableBlock = createBasicBlock("unreachable");
1955 new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1956 }
1957 return UnreachableBlock;
1958 }
1959
1960 llvm::BasicBlock *getInvokeDest() {
1961 if (!EHStack.requiresLandingPad()) return nullptr;
1962 return getInvokeDestImpl();
1963 }
1964
1965 bool currentFunctionUsesSEHTry() const { return CurSEHParent != nullptr; }
1966
1967 const TargetInfo &getTarget() const { return Target; }
1968 llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
1969 const TargetCodeGenInfo &getTargetHooks() const {
1970 return CGM.getTargetCodeGenInfo();
1971 }
1972
1973 //===--------------------------------------------------------------------===//
1974 // Cleanups
1975 //===--------------------------------------------------------------------===//
1976
1977 typedef void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty);
1978
1979 void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1980 Address arrayEndPointer,
1981 QualType elementType,
1982 CharUnits elementAlignment,
1983 Destroyer *destroyer);
1984 void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1985 llvm::Value *arrayEnd,
1986 QualType elementType,
1987 CharUnits elementAlignment,
1988 Destroyer *destroyer);
1989
1990 void pushDestroy(QualType::DestructionKind dtorKind,
1991 Address addr, QualType type);
1992 void pushEHDestroy(QualType::DestructionKind dtorKind,
1993 Address addr, QualType type);
1994 void pushDestroy(CleanupKind kind, Address addr, QualType type,
1995 Destroyer *destroyer, bool useEHCleanupForArray);
1996 void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr,
1997 QualType type, Destroyer *destroyer,
1998 bool useEHCleanupForArray);
1999 void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
2000 llvm::Value *CompletePtr,
2001 QualType ElementType);
2002 void pushStackRestore(CleanupKind kind, Address SPMem);
2003 void emitDestroy(Address addr, QualType type, Destroyer *destroyer,
2004 bool useEHCleanupForArray);
2005 llvm::Function *generateDestroyHelper(Address addr, QualType type,
2006 Destroyer *destroyer,
2007 bool useEHCleanupForArray,
2008 const VarDecl *VD);
2009 void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
2010 QualType elementType, CharUnits elementAlign,
2011 Destroyer *destroyer,
2012 bool checkZeroLength, bool useEHCleanup);
2013
2014 Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
2015
2016 /// Determines whether an EH cleanup is required to destroy a type
2017 /// with the given destruction kind.
2018 bool needsEHCleanup(QualType::DestructionKind kind) {
2019 switch (kind) {
2020 case QualType::DK_none:
2021 return false;
2022 case QualType::DK_cxx_destructor:
2023 case QualType::DK_objc_weak_lifetime:
2024 case QualType::DK_nontrivial_c_struct:
2025 return getLangOpts().Exceptions;
2026 case QualType::DK_objc_strong_lifetime:
2027 return getLangOpts().Exceptions &&
2028 CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
2029 }
2030 llvm_unreachable("bad destruction kind");
2031 }
2032
2033 CleanupKind getCleanupKind(QualType::DestructionKind kind) {
2034 return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
2035 }
2036
2037 //===--------------------------------------------------------------------===//
2038 // Objective-C
2039 //===--------------------------------------------------------------------===//
2040
2041 void GenerateObjCMethod(const ObjCMethodDecl *OMD);
2042
2043 void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD);
2044
2045 /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
2046 void GenerateObjCGetter(ObjCImplementationDecl *IMP,
2047 const ObjCPropertyImplDecl *PID);
2048 void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
2049 const ObjCPropertyImplDecl *propImpl,
2050 const ObjCMethodDecl *GetterMothodDecl,
2051 llvm::Constant *AtomicHelperFn);
2052
2053 void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
2054 ObjCMethodDecl *MD, bool ctor);
2055
2056 /// GenerateObjCSetter - Synthesize an Objective-C property setter function
2057 /// for the given property.
2058 void GenerateObjCSetter(ObjCImplementationDecl *IMP,
2059 const ObjCPropertyImplDecl *PID);
2060 void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
2061 const ObjCPropertyImplDecl *propImpl,
2062 llvm::Constant *AtomicHelperFn);
2063
2064 //===--------------------------------------------------------------------===//
2065 // Block Bits
2066 //===--------------------------------------------------------------------===//
2067
2068 /// Emit block literal.
2069 /// \return an LLVM value which is a pointer to a struct which contains
2070 /// information about the block, including the block invoke function, the
2071 /// captured variables, etc.
2072 llvm::Value *EmitBlockLiteral(const BlockExpr *);
2073
2074 llvm::Function *GenerateBlockFunction(GlobalDecl GD,
2075 const CGBlockInfo &Info,
2076 const DeclMapTy &ldm,
2077 bool IsLambdaConversionToBlock,
2078 bool BuildGlobalBlock);
2079
2080 /// Check if \p T is a C++ class that has a destructor that can throw.
2081 static bool cxxDestructorCanThrow(QualType T);
2082
2083 llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
2084 llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
2085 llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
2086 const ObjCPropertyImplDecl *PID);
2087 llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
2088 const ObjCPropertyImplDecl *PID);
2089 llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
2090
2091 void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags,
2092 bool CanThrow);
2093
2094 class AutoVarEmission;
2095
2096 void emitByrefStructureInit(const AutoVarEmission &emission);
2097
2098 /// Enter a cleanup to destroy a __block variable. Note that this
2099 /// cleanup should be a no-op if the variable hasn't left the stack
2100 /// yet; if a cleanup is required for the variable itself, that needs
2101 /// to be done externally.
2102 ///
2103 /// \param Kind Cleanup kind.
2104 ///
2105 /// \param Addr When \p LoadBlockVarAddr is false, the address of the __block
2106 /// structure that will be passed to _Block_object_dispose. When
2107 /// \p LoadBlockVarAddr is true, the address of the field of the block
2108 /// structure that holds the address of the __block structure.
2109 ///
2110 /// \param Flags The flag that will be passed to _Block_object_dispose.
2111 ///
2112 /// \param LoadBlockVarAddr Indicates whether we need to emit a load from
2113 /// \p Addr to get the address of the __block structure.
2114 void enterByrefCleanup(CleanupKind Kind, Address Addr, BlockFieldFlags Flags,
2115 bool LoadBlockVarAddr, bool CanThrow);
2116
2117 void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum,
2118 llvm::Value *ptr);
2119
2120 Address LoadBlockStruct();
2121 Address GetAddrOfBlockDecl(const VarDecl *var);
2122
2123 /// BuildBlockByrefAddress - Computes the location of the
2124 /// data in a variable which is declared as __block.
2125 Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V,
2126 bool followForward = true);
2127 Address emitBlockByrefAddress(Address baseAddr,
2128 const BlockByrefInfo &info,
2129 bool followForward,
2130 const llvm::Twine &name);
2131
2132 const BlockByrefInfo &getBlockByrefInfo(const VarDecl *var);
2133
2134 QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args);
2135
2136 void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
2137 const CGFunctionInfo &FnInfo);
2138
2139 /// Annotate the function with an attribute that disables TSan checking at
2140 /// runtime.
2141 void markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn);
2142
2143 /// Emit code for the start of a function.
2144 /// \param Loc The location to be associated with the function.
2145 /// \param StartLoc The location of the function body.
2146 void StartFunction(GlobalDecl GD,
2147 QualType RetTy,
2148 llvm::Function *Fn,
2149 const CGFunctionInfo &FnInfo,
2150 const FunctionArgList &Args,
2151 SourceLocation Loc = SourceLocation(),
2152 SourceLocation StartLoc = SourceLocation());
2153
2154 static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor);
2155
2156 void EmitConstructorBody(FunctionArgList &Args);
2157 void EmitDestructorBody(FunctionArgList &Args);
2158 void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
2159 void EmitFunctionBody(const Stmt *Body);
2160 void EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S);
2161
2162 void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
2163 CallArgList &CallArgs);
2164 void EmitLambdaBlockInvokeBody();
2165 void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
2166 void EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD);
2167 void EmitLambdaVLACapture(const VariableArrayType *VAT, LValue LV) {
2168 EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV);
2169 }
2170 void EmitAsanPrologueOrEpilogue(bool Prologue);
2171
2172 /// Emit the unified return block, trying to avoid its emission when
2173 /// possible.
2174 /// \return The debug location of the user written return statement if the
2175 /// return block is is avoided.
2176 llvm::DebugLoc EmitReturnBlock();
2177
2178 /// FinishFunction - Complete IR generation of the current function. It is
2179 /// legal to call this function even if there is no current insertion point.
2180 void FinishFunction(SourceLocation EndLoc=SourceLocation());
2181
2182 void StartThunk(llvm::Function *Fn, GlobalDecl GD,
2183 const CGFunctionInfo &FnInfo, bool IsUnprototyped);
2184
2185 void EmitCallAndReturnForThunk(llvm::FunctionCallee Callee,
2186 const ThunkInfo *Thunk, bool IsUnprototyped);
2187
2188 void FinishThunk();
2189
2190 /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
2191 void EmitMustTailThunk(GlobalDecl GD, llvm::Value *AdjustedThisPtr,
2192 llvm::FunctionCallee Callee);
2193
2194 /// Generate a thunk for the given method.
2195 void generateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
2196 GlobalDecl GD, const ThunkInfo &Thunk,
2197 bool IsUnprototyped);
2198
2199 llvm::Function *GenerateVarArgsThunk(llvm::Function *Fn,
2200 const CGFunctionInfo &FnInfo,
2201 GlobalDecl GD, const ThunkInfo &Thunk);
2202
2203 void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
2204 FunctionArgList &Args);
2205
2206 void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init);
2207
2208 /// Struct with all information about dynamic [sub]class needed to set vptr.
2209 struct VPtr {
2210 BaseSubobject Base;
2211 const CXXRecordDecl *NearestVBase;
2212 CharUnits OffsetFromNearestVBase;
2213 const CXXRecordDecl *VTableClass;
2214 };
2215
2216 /// Initialize the vtable pointer of the given subobject.
2217 void InitializeVTablePointer(const VPtr &vptr);
2218
2219 typedef llvm::SmallVector<VPtr, 4> VPtrsVector;
2220
2221 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
2222 VPtrsVector getVTablePointers(const CXXRecordDecl *VTableClass);
2223
2224 void getVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase,
2225 CharUnits OffsetFromNearestVBase,
2226 bool BaseIsNonVirtualPrimaryBase,
2227 const CXXRecordDecl *VTableClass,
2228 VisitedVirtualBasesSetTy &VBases, VPtrsVector &vptrs);
2229
2230 void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
2231
2232 /// GetVTablePtr - Return the Value of the vtable pointer member pointed
2233 /// to by This.
2234 llvm::Value *GetVTablePtr(Address This, llvm::Type *VTableTy,
2235 const CXXRecordDecl *VTableClass);
2236
2237 enum CFITypeCheckKind {
2238 CFITCK_VCall,
2239 CFITCK_NVCall,
2240 CFITCK_DerivedCast,
2241 CFITCK_UnrelatedCast,
2242 CFITCK_ICall,
2243 CFITCK_NVMFCall,
2244 CFITCK_VMFCall,
2245 };
2246
2247 /// Derived is the presumed address of an object of type T after a
2248 /// cast. If T is a polymorphic class type, emit a check that the virtual
2249 /// table for Derived belongs to a class derived from T.
2250 void EmitVTablePtrCheckForCast(QualType T, llvm::Value *Derived,
2251 bool MayBeNull, CFITypeCheckKind TCK,
2252 SourceLocation Loc);
2253
2254 /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable.
2255 /// If vptr CFI is enabled, emit a check that VTable is valid.
2256 void EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, llvm::Value *VTable,
2257 CFITypeCheckKind TCK, SourceLocation Loc);
2258
2259 /// EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for
2260 /// RD using llvm.type.test.
2261 void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable,
2262 CFITypeCheckKind TCK, SourceLocation Loc);
2263
2264 /// If whole-program virtual table optimization is enabled, emit an assumption
2265 /// that VTable is a member of RD's type identifier. Or, if vptr CFI is
2266 /// enabled, emit a check that VTable is a member of RD's type identifier.
2267 void EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD,
2268 llvm::Value *VTable, SourceLocation Loc);
2269
2270 /// Returns whether we should perform a type checked load when loading a
2271 /// virtual function for virtual calls to members of RD. This is generally
2272 /// true when both vcall CFI and whole-program-vtables are enabled.
2273 bool ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD);
2274
2275 /// Emit a type checked load from the given vtable.
2276 llvm::Value *EmitVTableTypeCheckedLoad(const CXXRecordDecl *RD, llvm::Value *VTable,
2277 uint64_t VTableByteOffset);
2278
2279 /// EnterDtorCleanups - Enter the cleanups necessary to complete the
2280 /// given phase of destruction for a destructor. The end result
2281 /// should call destructors on members and base classes in reverse
2282 /// order of their construction.
2283 void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
2284
2285 /// ShouldInstrumentFunction - Return true if the current function should be
2286 /// instrumented with __cyg_profile_func_* calls
2287 bool ShouldInstrumentFunction();
2288
2289 /// ShouldXRayInstrument - Return true if the current function should be
2290 /// instrumented with XRay nop sleds.
2291 bool ShouldXRayInstrumentFunction() const;
2292
2293 /// AlwaysEmitXRayCustomEvents - Return true if we must unconditionally emit
2294 /// XRay custom event handling calls.
2295 bool AlwaysEmitXRayCustomEvents() const;
2296
2297 /// AlwaysEmitXRayTypedEvents - Return true if clang must unconditionally emit
2298 /// XRay typed event handling calls.
2299 bool AlwaysEmitXRayTypedEvents() const;
2300
2301 /// Encode an address into a form suitable for use in a function prologue.
2302 llvm::Constant *EncodeAddrForUseInPrologue(llvm::Function *F,
2303 llvm::Constant *Addr);
2304
2305 /// Decode an address used in a function prologue, encoded by \c
2306 /// EncodeAddrForUseInPrologue.
2307 llvm::Value *DecodeAddrUsedInPrologue(llvm::Value *F,
2308 llvm::Value *EncodedAddr);
2309
2310 /// EmitFunctionProlog - Emit the target specific LLVM code to load the
2311 /// arguments for the given function. This is also responsible for naming the
2312 /// LLVM function arguments.
2313 void EmitFunctionProlog(const CGFunctionInfo &FI,
2314 llvm::Function *Fn,
2315 const FunctionArgList &Args);
2316
2317 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
2318 /// given temporary.
2319 void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
2320 SourceLocation EndLoc);
2321
2322 /// Emit a test that checks if the return value \p RV is nonnull.
2323 void EmitReturnValueCheck(llvm::Value *RV);
2324
2325 /// EmitStartEHSpec - Emit the start of the exception spec.
2326 void EmitStartEHSpec(const Decl *D);
2327
2328 /// EmitEndEHSpec - Emit the end of the exception spec.
2329 void EmitEndEHSpec(const Decl *D);
2330
2331 /// getTerminateLandingPad - Return a landing pad that just calls terminate.
2332 llvm::BasicBlock *getTerminateLandingPad();
2333
2334 /// getTerminateLandingPad - Return a cleanup funclet that just calls
2335 /// terminate.
2336 llvm::BasicBlock *getTerminateFunclet();
2337
2338 /// getTerminateHandler - Return a handler (not a landing pad, just
2339 /// a catch handler) that just calls terminate. This is used when
2340 /// a terminate scope encloses a try.
2341 llvm::BasicBlock *getTerminateHandler();
2342
2343 llvm::Type *ConvertTypeForMem(QualType T);
2344 llvm::Type *ConvertType(QualType T);
2345 llvm::Type *ConvertType(const TypeDecl *T) {
2346 return ConvertType(getContext().getTypeDeclType(T));
2347 }
2348
2349 /// LoadObjCSelf - Load the value of self. This function is only valid while
2350 /// generating code for an Objective-C method.
2351 llvm::Value *LoadObjCSelf();
2352
2353 /// TypeOfSelfObject - Return type of object that this self represents.
2354 QualType TypeOfSelfObject();
2355
2356 /// getEvaluationKind - Return the TypeEvaluationKind of QualType \c T.
2357 static TypeEvaluationKind getEvaluationKind(QualType T);
2358
2359 static bool hasScalarEvaluationKind(QualType T) {
2360 return getEvaluationKind(T) == TEK_Scalar;
2361 }
2362
2363 static bool hasAggregateEvaluationKind(QualType T) {
2364 return getEvaluationKind(T) == TEK_Aggregate;
2365 }
2366
2367 /// createBasicBlock - Create an LLVM basic block.
2368 llvm::BasicBlock *createBasicBlock(const Twine &name = "",
2369 llvm::Function *parent = nullptr,
2370 llvm::BasicBlock *before = nullptr) {
2371 return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
2372 }
2373
2374 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
2375 /// label maps to.
2376 JumpDest getJumpDestForLabel(const LabelDecl *S);
2377
2378 /// SimplifyForwardingBlocks - If the given basic block is only a branch to
2379 /// another basic block, simplify it. This assumes that no other code could
2380 /// potentially reference the basic block.
2381 void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
2382
2383 /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
2384 /// adding a fall-through branch from the current insert block if
2385 /// necessary. It is legal to call this function even if there is no current
2386 /// insertion point.
2387 ///
2388 /// IsFinished - If true, indicates that the caller has finished emitting
2389 /// branches to the given block and does not expect to emit code into it. This
2390 /// means the block can be ignored if it is unreachable.
2391 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
2392
2393 /// EmitBlockAfterUses - Emit the given block somewhere hopefully
2394 /// near its uses, and leave the insertion point in it.
2395 void EmitBlockAfterUses(llvm::BasicBlock *BB);
2396
2397 /// EmitBranch - Emit a branch to the specified basic block from the current
2398 /// insert block, taking care to avoid creation of branches from dummy
2399 /// blocks. It is legal to call this function even if there is no current
2400 /// insertion point.
2401 ///
2402 /// This function clears the current insertion point. The caller should follow
2403 /// calls to this function with calls to Emit*Block prior to generation new
2404 /// code.
2405 void EmitBranch(llvm::BasicBlock *Block);
2406
2407 /// HaveInsertPoint - True if an insertion point is defined. If not, this
2408 /// indicates that the current code being emitted is unreachable.
2409 bool HaveInsertPoint() const {
2410 return Builder.GetInsertBlock() != nullptr;
2411 }
2412
2413 /// EnsureInsertPoint - Ensure that an insertion point is defined so that
2414 /// emitted IR has a place to go. Note that by definition, if this function
2415 /// creates a block then that block is unreachable; callers may do better to
2416 /// detect when no insertion point is defined and simply skip IR generation.
2417 void EnsureInsertPoint() {
2418 if (!HaveInsertPoint())
2419 EmitBlock(createBasicBlock());
2420 }
2421
2422 /// ErrorUnsupported - Print out an error that codegen doesn't support the
2423 /// specified stmt yet.
2424 void ErrorUnsupported(const Stmt *S, const char *Type);
2425
2426 //===--------------------------------------------------------------------===//
2427 // Helpers
2428 //===--------------------------------------------------------------------===//
2429
2430 LValue MakeAddrLValue(Address Addr, QualType T,
2431 AlignmentSource Source = AlignmentSource::Type) {
2432 return LValue::MakeAddr(Addr, T, getContext(), LValueBaseInfo(Source),
2433 CGM.getTBAAAccessInfo(T));
2434 }
2435
2436 LValue MakeAddrLValue(Address Addr, QualType T, LValueBaseInfo BaseInfo,
2437 TBAAAccessInfo TBAAInfo) {
2438 return LValue::MakeAddr(Addr, T, getContext(), BaseInfo, TBAAInfo);
2439 }
2440
2441 LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment,
2442 AlignmentSource Source = AlignmentSource::Type) {
2443 return LValue::MakeAddr(Address(V, Alignment), T, getContext(),
2444 LValueBaseInfo(Source), CGM.getTBAAAccessInfo(T));
2445 }
2446
2447 LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment,
2448 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
2449 return LValue::MakeAddr(Address(V, Alignment), T, getContext(),
2450 BaseInfo, TBAAInfo);
2451 }
2452
2453 LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T);
2454 LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
2455
2456 Address EmitLoadOfReference(LValue RefLVal,
2457 LValueBaseInfo *PointeeBaseInfo = nullptr,
2458 TBAAAccessInfo *PointeeTBAAInfo = nullptr);
2459 LValue EmitLoadOfReferenceLValue(LValue RefLVal);
2460 LValue EmitLoadOfReferenceLValue(Address RefAddr, QualType RefTy,
2461 AlignmentSource Source =
2462 AlignmentSource::Type) {
2463 LValue RefLVal = MakeAddrLValue(RefAddr, RefTy, LValueBaseInfo(Source),
2464 CGM.getTBAAAccessInfo(RefTy));
2465 return EmitLoadOfReferenceLValue(RefLVal);
2466 }
2467
2468 Address EmitLoadOfPointer(Address Ptr, const PointerType *PtrTy,
2469 LValueBaseInfo *BaseInfo = nullptr,
2470 TBAAAccessInfo *TBAAInfo = nullptr);
2471 LValue EmitLoadOfPointerLValue(Address Ptr, const PointerType *PtrTy);
2472
2473 /// CreateTempAlloca - This creates an alloca and inserts it into the entry
2474 /// block if \p ArraySize is nullptr, otherwise inserts it at the current
2475 /// insertion point of the builder. The caller is responsible for setting an
2476 /// appropriate alignment on
2477 /// the alloca.
2478 ///
2479 /// \p ArraySize is the number of array elements to be allocated if it
2480 /// is not nullptr.
2481 ///
2482 /// LangAS::Default is the address space of pointers to local variables and
2483 /// temporaries, as exposed in the source language. In certain
2484 /// configurations, this is not the same as the alloca address space, and a
2485 /// cast is needed to lift the pointer from the alloca AS into
2486 /// LangAS::Default. This can happen when the target uses a restricted
2487 /// address space for the stack but the source language requires
2488 /// LangAS::Default to be a generic address space. The latter condition is
2489 /// common for most programming languages; OpenCL is an exception in that
2490 /// LangAS::Default is the private address space, which naturally maps
2491 /// to the stack.
2492 ///
2493 /// Because the address of a temporary is often exposed to the program in
2494 /// various ways, this function will perform the cast. The original alloca