clang 24.0.0git
CodeGenFunction.cpp
Go to the documentation of this file.
1//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
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 coordinates the per-function state used while generating code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CodeGenFunction.h"
14#include "CGBlocks.h"
15#include "CGCUDARuntime.h"
16#include "CGCXXABI.h"
17#include "CGCleanup.h"
18#include "CGDebugInfo.h"
19#include "CGHLSLRuntime.h"
20#include "CGOpenMPRuntime.h"
21#include "CodeGenModule.h"
22#include "CodeGenPGO.h"
23#include "TargetInfo.h"
25#include "clang/AST/ASTLambda.h"
26#include "clang/AST/Attr.h"
27#include "clang/AST/Decl.h"
28#include "clang/AST/DeclCXX.h"
29#include "clang/AST/Expr.h"
31#include "clang/AST/StmtCXX.h"
32#include "clang/AST/StmtObjC.h"
40#include "llvm/ADT/ArrayRef.h"
41#include "llvm/ADT/ScopeExit.h"
42#include "llvm/ADT/StringExtras.h"
43#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
44#include "llvm/IR/DataLayout.h"
45#include "llvm/IR/Dominators.h"
46#include "llvm/IR/FPEnv.h"
47#include "llvm/IR/Instruction.h"
48#include "llvm/IR/IntrinsicInst.h"
49#include "llvm/IR/Intrinsics.h"
50#include "llvm/IR/IntrinsicsPowerPC.h"
51#include "llvm/IR/MDBuilder.h"
52#include "llvm/Support/CRC.h"
53#include "llvm/Support/SaveAndRestore.h"
54#include "llvm/Support/SipHash.h"
55#include "llvm/Support/xxhash.h"
56#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
57#include "llvm/Transforms/Utils/PromoteMemToReg.h"
58#include <optional>
59
60using namespace clang;
61using namespace CodeGen;
62
63CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
64 : CodeGenTypeCache(cgm), CGM(cgm), Target(cgm.getTarget()),
65 Builder(cgm, cgm.getModule().getContext(), CGBuilderInserterTy(this)),
67 DebugInfo(CGM.getModuleDebugInfo()),
68 PGO(std::make_unique<CodeGenPGO>(cgm)),
69 ShouldEmitLifetimeMarkers(CodeGenUtils::shouldEmitLifetimeMarkers(
70 CGM.getCodeGenOpts(), CGM.getLangOpts())) {
71 if (!suppressNewContext)
72 CGM.getCXXABI().getMangleContext().startNewFunction();
73 EHStack.setCGF(this);
74
76}
77
79 const auto *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl);
80 if (!FD)
81 FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl);
82 return FD;
83}
84
86 assert(LifetimeExtendedCleanupStack.empty() && "failed to emit a cleanup");
87 assert(DeferredDeactivationCleanupStack.empty() &&
88 "missed to deactivate a cleanup");
89
90 if (getLangOpts().OpenMP && CurFn)
91 CGM.getOpenMPRuntime().functionFinished(*this);
92
93 // If we have an OpenMPIRBuilder we want to finalize functions (incl.
94 // outlining etc) at some point. Doing it once the function codegen is done
95 // seems to be a reasonable spot. We do it here, as opposed to the deletion
96 // time of the CodeGenModule, because we have to ensure the IR has not yet
97 // been "emitted" to the outside, thus, modifications are still sensible.
98 if (CGM.getLangOpts().OpenMPIRBuilder && CurFn)
99 CGM.getOpenMPRuntime().getOMPBuilder().finalize(CurFn);
100}
101
102// Map the LangOption for exception behavior into
103// the corresponding enum in the IR.
104llvm::fp::ExceptionBehavior
106
107 switch (Kind) {
108 case LangOptions::FPE_Ignore: return llvm::fp::ebIgnore;
109 case LangOptions::FPE_MayTrap: return llvm::fp::ebMayTrap;
110 case LangOptions::FPE_Strict: return llvm::fp::ebStrict;
111 default:
112 llvm_unreachable("Unsupported FP Exception Behavior");
113 }
114}
115
117 llvm::FastMathFlags FMF;
118 FMF.setAllowReassoc(FPFeatures.getAllowFPReassociate());
119 FMF.setNoNaNs(FPFeatures.getNoHonorNaNs());
120 FMF.setNoInfs(FPFeatures.getNoHonorInfs());
121 FMF.setNoSignedZeros(FPFeatures.getNoSignedZero());
122 FMF.setAllowReciprocal(FPFeatures.getAllowReciprocal());
123 FMF.setApproxFunc(FPFeatures.getAllowApproxFunc());
124 FMF.setAllowContract(FPFeatures.allowFPContractAcrossStatement());
125 Builder.setFastMathFlags(FMF);
126}
127
129 const Expr *E)
130 : CGF(CGF) {
131 ConstructorHelper(E->getFPFeaturesInEffect(CGF.getLangOpts()));
132}
133
135 FPOptions FPFeatures)
136 : CGF(CGF) {
137 ConstructorHelper(FPFeatures);
138}
139
140void CodeGenFunction::CGFPOptionsRAII::ConstructorHelper(FPOptions FPFeatures) {
141 OldFPFeatures = CGF.CurFPFeatures;
142 CGF.CurFPFeatures = FPFeatures;
143
144 OldExcept = CGF.Builder.getDefaultConstrainedExcept();
145 OldRounding = CGF.Builder.getDefaultConstrainedRounding();
146
147 if (OldFPFeatures == FPFeatures)
148 return;
149
150 FMFGuard.emplace(CGF.Builder);
151
152 llvm::RoundingMode NewRoundingBehavior = FPFeatures.getRoundingMode();
153 CGF.Builder.setDefaultConstrainedRounding(NewRoundingBehavior);
154 auto NewExceptionBehavior =
156 CGF.Builder.setDefaultConstrainedExcept(NewExceptionBehavior);
157
158 CGF.SetFastMathFlags(FPFeatures);
159
160 assert((CGF.CurFuncDecl == nullptr || CGF.Builder.getIsFPConstrained() ||
161 isa<CXXConstructorDecl>(CGF.CurFuncDecl) ||
162 isa<CXXDestructorDecl>(CGF.CurFuncDecl) ||
163 (NewExceptionBehavior == llvm::fp::ebIgnore &&
164 NewRoundingBehavior == llvm::RoundingMode::NearestTiesToEven)) &&
165 "FPConstrained should be enabled on entire function");
166
167 auto mergeFnAttrValue = [&](StringRef Name, bool Value) {
168 auto OldValue =
169 CGF.CurFn->getFnAttribute(Name).getValueAsBool();
170 auto NewValue = OldValue & Value;
171 if (OldValue != NewValue)
172 CGF.CurFn->addFnAttr(Name, llvm::toStringRef(NewValue));
173 };
174 mergeFnAttrValue("no-signed-zeros-fp-math", FPFeatures.getNoSignedZero());
175}
176
178 CGF.CurFPFeatures = OldFPFeatures;
179 CGF.Builder.setDefaultConstrainedExcept(OldExcept);
180 CGF.Builder.setDefaultConstrainedRounding(OldRounding);
181}
182
183static LValue
184makeNaturalAlignAddrLValue(llvm::Value *V, QualType T, bool ForPointeeType,
185 bool MightBeSigned, CodeGenFunction &CGF,
186 KnownNonNull_t IsKnownNonNull = NotKnownNonNull) {
187 LValueBaseInfo BaseInfo;
188 TBAAAccessInfo TBAAInfo;
189 CharUnits Alignment =
190 CGF.CGM.getNaturalTypeAlignment(T, &BaseInfo, &TBAAInfo, ForPointeeType);
191 Address Addr =
192 MightBeSigned
193 ? CGF.makeNaturalAddressForPointer(V, T, Alignment, false, nullptr,
194 nullptr, IsKnownNonNull)
195 : Address(V, CGF.ConvertTypeForMem(T), Alignment, IsKnownNonNull);
196 return CGF.MakeAddrLValue(Addr, T, BaseInfo, TBAAInfo);
197}
198
199LValue
201 KnownNonNull_t IsKnownNonNull) {
202 return ::makeNaturalAlignAddrLValue(V, T, /*ForPointeeType*/ false,
203 /*MightBeSigned*/ true, *this,
204 IsKnownNonNull);
205}
206
207LValue
209 return ::makeNaturalAlignAddrLValue(V, T, /*ForPointeeType*/ true,
210 /*MightBeSigned*/ true, *this);
211}
212
214 QualType T) {
215 return ::makeNaturalAlignAddrLValue(V, T, /*ForPointeeType*/ false,
216 /*MightBeSigned*/ false, *this);
217}
218
220 QualType T) {
221 return ::makeNaturalAlignAddrLValue(V, T, /*ForPointeeType*/ true,
222 /*MightBeSigned*/ false, *this);
223}
224
226 return CGM.getTypes().ConvertTypeForMem(T);
227}
228
230 return CGM.getTypes().ConvertType(T);
231}
232
234 llvm::Type *LLVMTy) {
235 return CGM.getTypes().convertTypeForLoadStore(ASTTy, LLVMTy);
236}
237
239 type = type.getCanonicalType();
240 while (true) {
241 switch (type->getTypeClass()) {
242#define TYPE(name, parent)
243#define ABSTRACT_TYPE(name, parent)
244#define NON_CANONICAL_TYPE(name, parent) case Type::name:
245#define DEPENDENT_TYPE(name, parent) case Type::name:
246#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name:
247#include "clang/AST/TypeNodes.inc"
248 llvm_unreachable("non-canonical or dependent type in IR-generation");
249
250 case Type::Auto:
251 case Type::DeducedTemplateSpecialization:
252 llvm_unreachable("undeduced type in IR-generation");
253
254 // Various scalar types.
255 case Type::Builtin:
256 case Type::Pointer:
257 case Type::BlockPointer:
258 case Type::LValueReference:
259 case Type::RValueReference:
260 case Type::MemberPointer:
261 case Type::Vector:
262 case Type::ExtVector:
263 case Type::ConstantMatrix:
264 case Type::FunctionProto:
265 case Type::FunctionNoProto:
266 case Type::Enum:
267 case Type::ObjCObjectPointer:
268 case Type::Pipe:
269 case Type::BitInt:
270 case Type::HLSLAttributedResource:
271 case Type::HLSLInlineSpirv:
272 case Type::OverflowBehavior:
273 return TEK_Scalar;
274
275 // Complexes.
276 case Type::Complex:
277 return TEK_Complex;
278
279 // Arrays, records, and Objective-C objects.
280 case Type::ConstantArray:
281 case Type::IncompleteArray:
282 case Type::VariableArray:
283 case Type::Record:
284 case Type::ObjCObject:
285 case Type::ObjCInterface:
286 case Type::ArrayParameter:
287 return TEK_Aggregate;
288
289 // We operate on atomic values according to their underlying type.
290 case Type::Atomic:
291 type = cast<AtomicType>(type)->getValueType();
292 continue;
293 }
294 llvm_unreachable("unknown type kind!");
295 }
296}
297
299 // For cleanliness, we try to avoid emitting the return block for
300 // simple cases.
301 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
302
303 if (CurBB) {
304 assert(!CurBB->hasTerminator() && "Unexpected terminated block.");
305
306 // We have a valid insert point, reuse it if it is empty or there are no
307 // explicit jumps to the return block.
308 if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) {
309 ReturnBlock.getBlock()->replaceAllUsesWith(CurBB);
310 delete ReturnBlock.getBlock();
312 } else
313 EmitBlock(ReturnBlock.getBlock());
314 return llvm::DebugLoc();
315 }
316
317 // Otherwise, if the return block is the target of a single direct
318 // branch then we can just put the code in that block instead. This
319 // cleans up functions which started with a unified return block.
320 if (ReturnBlock.getBlock()->hasOneUse()) {
321 auto *BI =
322 dyn_cast<llvm::UncondBrInst>(*ReturnBlock.getBlock()->user_begin());
323 if (BI && BI->getSuccessor(0) == ReturnBlock.getBlock()) {
324 // Record/return the DebugLoc of the simple 'return' expression to be used
325 // later by the actual 'ret' instruction.
326 llvm::DebugLoc Loc = BI->getDebugLoc();
327 Builder.SetInsertPoint(BI->getParent());
328 BI->eraseFromParent();
329 delete ReturnBlock.getBlock();
331 return Loc;
332 }
333 }
334
335 // FIXME: We are at an unreachable point, there is no reason to emit the block
336 // unless it has uses. However, we still need a place to put the debug
337 // region.end for now.
338
339 EmitBlock(ReturnBlock.getBlock());
340 return llvm::DebugLoc();
341}
342
343static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
344 if (!BB) return;
345 if (!BB->use_empty()) {
346 CGF.CurFn->insert(CGF.CurFn->end(), BB);
347 return;
348 }
349 delete BB;
350}
351
353 assert(BreakContinueStack.empty() &&
354 "mismatched push/pop in break/continue stack!");
355 assert(LifetimeExtendedCleanupStack.empty() &&
356 "mismatched push/pop of cleanups in EHStack!");
357 assert(DeferredDeactivationCleanupStack.empty() &&
358 "mismatched activate/deactivate of cleanups!");
359
360 if (CGM.shouldEmitConvergenceTokens()) {
361 ConvergenceTokenStack.pop_back();
362 assert(ConvergenceTokenStack.empty() &&
363 "mismatched push/pop in convergence stack!");
364 }
365
366 bool OnlySimpleReturnStmts = NumSimpleReturnExprs > 0
367 && NumSimpleReturnExprs == NumReturnExprs
368 && ReturnBlock.getBlock()->use_empty();
369 // Usually the return expression is evaluated before the cleanup
370 // code. If the function contains only a simple return statement,
371 // such as a constant, the location before the cleanup code becomes
372 // the last useful breakpoint in the function, because the simple
373 // return expression will be evaluated after the cleanup code. To be
374 // safe, set the debug location for cleanup code to the location of
375 // the return statement. Otherwise the cleanup code should be at the
376 // end of the function's lexical scope.
377 //
378 // If there are multiple branches to the return block, the branch
379 // instructions will get the location of the return statements and
380 // all will be fine.
381 if (CGDebugInfo *DI = getDebugInfo()) {
382 if (OnlySimpleReturnStmts)
383 DI->EmitLocation(Builder, LastStopPoint);
384 else
385 DI->EmitLocation(Builder, EndLoc);
386 }
387
388 // Pop any cleanups that might have been associated with the
389 // parameters. Do this in whatever block we're currently in; it's
390 // important to do this before we enter the return block or return
391 // edges will be *really* confused.
392 bool HasCleanups = EHStack.stable_begin() != PrologueCleanupDepth;
393 bool HasOnlyNoopCleanups =
394 HasCleanups && EHStack.containsOnlyNoopCleanups(PrologueCleanupDepth);
395 bool EmitRetDbgLoc = !HasCleanups || HasOnlyNoopCleanups;
396
397 std::optional<ApplyDebugLocation> OAL;
398 if (HasCleanups) {
399 // Make sure the line table doesn't jump back into the body for
400 // the ret after it's been at EndLoc.
401 if (CGDebugInfo *DI = getDebugInfo()) {
402 if (OnlySimpleReturnStmts)
403 DI->EmitLocation(Builder, EndLoc);
404 else
405 // We may not have a valid end location. Try to apply it anyway, and
406 // fall back to an artificial location if needed.
408 }
409
411 }
412
413 // Emit function epilog (to return).
414 llvm::DebugLoc Loc = EmitReturnBlock();
415
417 if (CGM.getCodeGenOpts().InstrumentFunctions)
418 CurFn->addFnAttr("instrument-function-exit", "__cyg_profile_func_exit");
419 if (CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining)
420 CurFn->addFnAttr("instrument-function-exit-inlined",
421 "__cyg_profile_func_exit");
422 }
423
424 // Emit debug descriptor for function end.
425 if (CGDebugInfo *DI = getDebugInfo())
426 DI->EmitFunctionEnd(Builder, CurFn);
427
428 // Reset the debug location to that of the simple 'return' expression, if any
429 // rather than that of the end of the function's scope '}'.
430 uint64_t RetKeyInstructionsAtomGroup = Loc ? Loc->getAtomGroup() : 0;
431 ApplyDebugLocation AL(*this, Loc);
432 EmitFunctionEpilog(*CurFnInfo, EmitRetDbgLoc, EndLoc,
433 RetKeyInstructionsAtomGroup);
435
436 assert(EHStack.empty() &&
437 "did not remove all scopes from cleanup stack!");
438
439 // If someone did an indirect goto, emit the indirect goto block at the end of
440 // the function.
441 if (IndirectBranch) {
442 EmitBlock(IndirectBranch->getParent());
443 Builder.ClearInsertionPoint();
444 }
445
446 // If some of our locals escaped, insert a call to llvm.localescape in the
447 // entry block.
448 if (!EscapedLocals.empty()) {
449 // Invert the map from local to index into a simple vector. There should be
450 // no holes.
452 EscapeArgs.resize(EscapedLocals.size());
453 for (auto &Pair : EscapedLocals)
454 EscapeArgs[Pair.second] = Pair.first;
455 llvm::Function *FrameEscapeFn = llvm::Intrinsic::getOrInsertDeclaration(
456 &CGM.getModule(), llvm::Intrinsic::localescape);
457 CGBuilderTy(CGM, AllocaInsertPt).CreateCall(FrameEscapeFn, EscapeArgs);
458 }
459
460 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
461 llvm::Instruction *Ptr = AllocaInsertPt;
462 AllocaInsertPt = nullptr;
463 Ptr->eraseFromParent();
464
465 // PostAllocaInsertPt, if created, was lazily created when it was required,
466 // remove it now since it was just created for our own convenience.
467 if (PostAllocaInsertPt) {
468 llvm::Instruction *PostPtr = PostAllocaInsertPt;
469 PostAllocaInsertPt = nullptr;
470 PostPtr->eraseFromParent();
471 }
472
473 // If someone took the address of a label but never did an indirect goto, we
474 // made a zero entry PHI node, which is illegal, zap it now.
475 if (IndirectBranch) {
476 llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
477 if (PN->getNumIncomingValues() == 0) {
478 PN->replaceAllUsesWith(llvm::PoisonValue::get(PN->getType()));
479 PN->eraseFromParent();
480 }
481 }
482
484 EmitIfUsed(*this, TerminateLandingPad);
485 EmitIfUsed(*this, TerminateHandler);
486 EmitIfUsed(*this, UnreachableBlock);
487
488 for (const auto &FuncletAndParent : TerminateFunclets)
489 EmitIfUsed(*this, FuncletAndParent.second);
490
491 if (CGM.getCodeGenOpts().EmitDeclMetadata)
492 EmitDeclMetadata();
493
494 for (const auto &R : DeferredReplacements) {
495 if (llvm::Value *Old = R.first) {
496 Old->replaceAllUsesWith(R.second);
497 cast<llvm::Instruction>(Old)->eraseFromParent();
498 }
499 }
500 DeferredReplacements.clear();
501
502 // Eliminate CleanupDestSlot alloca by replacing it with SSA values and
503 // PHIs if the current function is a coroutine. We don't do it for all
504 // functions as it may result in slight increase in numbers of instructions
505 // if compiled with no optimizations. We do it for coroutine as the lifetime
506 // of CleanupDestSlot alloca make correct coroutine frame building very
507 // difficult.
508 if (NormalCleanupDest.isValid() && isCoroutine()) {
509 llvm::DominatorTree DT(*CurFn);
510 llvm::PromoteMemToReg(
511 cast<llvm::AllocaInst>(NormalCleanupDest.getPointer()), DT);
513 }
514
515 // Scan function arguments for vector width.
516 for (llvm::Argument &A : CurFn->args())
517 if (auto *VT = dyn_cast<llvm::VectorType>(A.getType()))
518 LargestVectorWidth =
519 std::max((uint64_t)LargestVectorWidth,
520 VT->getPrimitiveSizeInBits().getKnownMinValue());
521
522 // Update vector width based on return type.
523 if (auto *VT = dyn_cast<llvm::VectorType>(CurFn->getReturnType()))
524 LargestVectorWidth =
525 std::max((uint64_t)LargestVectorWidth,
526 VT->getPrimitiveSizeInBits().getKnownMinValue());
527
528 if (CurFnInfo->getMaxVectorWidth() > LargestVectorWidth)
529 LargestVectorWidth = CurFnInfo->getMaxVectorWidth();
530
531 // Add the min-legal-vector-width attribute. This contains the max width from:
532 // 1. min-vector-width attribute used in the source program.
533 // 2. Any builtins used that have a vector width specified.
534 // 3. Values passed in and out of inline assembly.
535 // 4. Width of vector arguments and return types for this function.
536 // 5. Width of vector arguments and return types for functions called by this
537 // function.
538 if (getContext().getTargetInfo().getTriple().isX86())
539 CurFn->addFnAttr("min-legal-vector-width",
540 llvm::utostr(LargestVectorWidth));
541
542 // If we generated an unreachable return block, delete it now.
543 if (ReturnBlock.isValid() && ReturnBlock.getBlock()->use_empty()) {
544 Builder.ClearInsertionPoint();
545 ReturnBlock.getBlock()->eraseFromParent();
546 }
547 if (ReturnValue.isValid()) {
548 auto *RetAlloca =
549 dyn_cast<llvm::AllocaInst>(ReturnValue.emitRawPointer(*this));
550 if (RetAlloca && RetAlloca->use_empty()) {
551 RetAlloca->eraseFromParent();
553 }
554 }
555}
556
557/// ShouldInstrumentFunction - Return true if the current function should be
558/// instrumented with __cyg_profile_func_* calls
560 if (!CGM.getCodeGenOpts().InstrumentFunctions &&
561 !CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining &&
562 !CGM.getCodeGenOpts().InstrumentFunctionEntryBare)
563 return false;
564 if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
565 return false;
566 return true;
567}
568
570 if (!CurFuncDecl)
571 return false;
572 return CurFuncDecl->hasAttr<DisableSanitizerInstrumentationAttr>();
573}
574
575/// ShouldXRayInstrument - Return true if the current function should be
576/// instrumented with XRay nop sleds.
578 return CGM.getCodeGenOpts().XRayInstrumentFunctions;
579}
580
581/// AlwaysEmitXRayCustomEvents - Return true if we should emit IR for calls to
582/// the __xray_customevent(...) builtin calls, when doing XRay instrumentation.
584 return CGM.getCodeGenOpts().XRayInstrumentFunctions &&
585 (CGM.getCodeGenOpts().XRayAlwaysEmitCustomEvents ||
586 CGM.getCodeGenOpts().XRayInstrumentationBundle.Mask ==
588}
589
591 return CGM.getCodeGenOpts().XRayInstrumentFunctions &&
592 (CGM.getCodeGenOpts().XRayAlwaysEmitTypedEvents ||
593 CGM.getCodeGenOpts().XRayInstrumentationBundle.Mask ==
595}
596
597llvm::ConstantInt *
599 // Remove any (C++17) exception specifications, to allow calling e.g. a
600 // noexcept function through a non-noexcept pointer.
601 if (!Ty->isFunctionNoProtoType())
603 std::string Mangled;
604 llvm::raw_string_ostream Out(Mangled);
605 CGM.getCXXABI().getMangleContext().mangleCanonicalTypeName(Ty, Out, false);
606 return llvm::ConstantInt::get(
607 CGM.Int32Ty, static_cast<uint32_t>(llvm::xxh3_64bits(Mangled)));
608}
609
610void CodeGenFunction::EmitKernelMetadata(const FunctionDecl *FD,
611 llvm::Function *Fn) {
612 if (!FD->hasAttr<DeviceKernelAttr>() && !FD->hasAttr<CUDAGlobalAttr>())
613 return;
614
615 llvm::LLVMContext &Context = getLLVMContext();
616
617 CGM.GenKernelArgMetadata(Fn, FD, this);
618
619 if (!(getLangOpts().OpenCL ||
620 (getLangOpts().CUDA &&
621 getContext().getTargetInfo().getTriple().isSPIRV())))
622 return;
623
624 if (const VecTypeHintAttr *A = FD->getAttr<VecTypeHintAttr>()) {
625 QualType HintQTy = A->getTypeHint();
626 const ExtVectorType *HintEltQTy = HintQTy->getAs<ExtVectorType>();
627 bool IsSignedInteger =
628 HintQTy->isSignedIntegerType() ||
629 (HintEltQTy && HintEltQTy->getElementType()->isSignedIntegerType());
630 llvm::Metadata *AttrMDArgs[] = {
631 llvm::ConstantAsMetadata::get(llvm::PoisonValue::get(
632 CGM.getTypes().ConvertType(A->getTypeHint()))),
633 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
634 llvm::IntegerType::get(Context, 32),
635 llvm::APInt(32, (uint64_t)(IsSignedInteger ? 1 : 0))))};
636 Fn->setMetadata("vec_type_hint", llvm::MDNode::get(Context, AttrMDArgs));
637 }
638
639 if (const WorkGroupSizeHintAttr *A = FD->getAttr<WorkGroupSizeHintAttr>()) {
640 auto Eval = [&](Expr *E) {
641 return E->EvaluateKnownConstInt(FD->getASTContext()).getExtValue();
642 };
643 llvm::Metadata *AttrMDArgs[] = {
644 llvm::ConstantAsMetadata::get(Builder.getInt32(Eval(A->getXDim()))),
645 llvm::ConstantAsMetadata::get(Builder.getInt32(Eval(A->getYDim()))),
646 llvm::ConstantAsMetadata::get(Builder.getInt32(Eval(A->getZDim())))};
647 Fn->setMetadata("work_group_size_hint", llvm::MDNode::get(Context, AttrMDArgs));
648 }
649
650 if (const ReqdWorkGroupSizeAttr *A = FD->getAttr<ReqdWorkGroupSizeAttr>()) {
651 auto Eval = [&](Expr *E) {
652 return E->EvaluateKnownConstInt(FD->getASTContext()).getExtValue();
653 };
654 llvm::Metadata *AttrMDArgs[] = {
655 llvm::ConstantAsMetadata::get(Builder.getInt32(Eval(A->getXDim()))),
656 llvm::ConstantAsMetadata::get(Builder.getInt32(Eval(A->getYDim()))),
657 llvm::ConstantAsMetadata::get(Builder.getInt32(Eval(A->getZDim())))};
658 Fn->setMetadata("reqd_work_group_size", llvm::MDNode::get(Context, AttrMDArgs));
659 }
660
661 if (const OpenCLIntelReqdSubGroupSizeAttr *A =
662 FD->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
663 llvm::Metadata *AttrMDArgs[] = {
664 llvm::ConstantAsMetadata::get(Builder.getInt32(A->getSubGroupSize()))};
665 Fn->setMetadata("intel_reqd_sub_group_size",
666 llvm::MDNode::get(Context, AttrMDArgs));
667 }
668}
669
670/// Determine whether the function F ends with a return stmt.
671static bool endsWithReturn(const Decl* F) {
672 const Stmt *Body = nullptr;
673 if (auto *FD = dyn_cast_or_null<FunctionDecl>(F))
674 Body = FD->getBody();
675 else if (auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(F))
676 Body = OMD->getBody();
677
678 if (auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
679 auto LastStmt = CS->body_rbegin();
680 if (LastStmt != CS->body_rend())
681 return isa<ReturnStmt>(*LastStmt);
682 }
683 return false;
684}
685
687 if (SanOpts.has(SanitizerKind::Thread)) {
688 Fn->addFnAttr("sanitize_thread_no_checking_at_run_time");
689 Fn->removeFnAttr(llvm::Attribute::SanitizeThread);
690 }
691 if (SanOpts.has(SanitizerKind::Concurrency)) {
692 Fn->addFnAttr("sanitize_concurrency_no_checking_at_run_time");
693 Fn->removeFnAttr(llvm::Attribute::SanitizeConcurrency);
694 }
695}
696
697/// Check if the return value of this function requires sanitization.
698bool CodeGenFunction::requiresReturnValueCheck() const {
699 return requiresReturnValueNullabilityCheck() ||
700 (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute) && CurCodeDecl &&
701 CurCodeDecl->getAttr<ReturnsNonNullAttr>()) ||
702 (SanOpts.has(SanitizerKind::Alignment) && CurCodeDecl &&
704 CurCodeDecl->getAttr<AllocAlignAttr>()) ||
705 CurCodeDecl->getAttr<AssumeAlignedAttr>()));
706}
707
708static bool matchesStlAllocatorFn(const Decl *D, const ASTContext &Ctx) {
709 auto *MD = dyn_cast_or_null<CXXMethodDecl>(D);
710 if (!MD || !MD->getDeclName().getAsIdentifierInfo() ||
711 !MD->getDeclName().getAsIdentifierInfo()->isStr("allocate") ||
712 (MD->getNumParams() != 1 && MD->getNumParams() != 2))
713 return false;
714
715 if (!Ctx.hasSameType(MD->parameters()[0]->getType(), Ctx.getSizeType()))
716 return false;
717
718 if (MD->getNumParams() == 2) {
719 auto *PT = MD->parameters()[1]->getType()->getAs<PointerType>();
720 if (!PT || !PT->isVoidPointerType() ||
721 !PT->getPointeeType().isConstQualified())
722 return false;
723 }
724
725 return true;
726}
727
728bool CodeGenFunction::isInAllocaArgument(CGCXXABI &ABI, QualType Ty) {
729 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
730 return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory;
731}
732
733bool CodeGenFunction::hasInAllocaArg(const CXXMethodDecl *MD) {
734 return getTarget().getTriple().getArch() == llvm::Triple::x86 &&
736 llvm::any_of(MD->parameters(), [&](ParmVarDecl *P) {
737 return isInAllocaArgument(CGM.getCXXABI(), P->getType());
738 });
739}
740
741/// Return the UBSan prologue signature for \p FD if one is available.
742static llvm::Constant *getPrologueSignature(CodeGenModule &CGM,
743 const FunctionDecl *FD) {
744 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
745 if (!MD->isStatic())
746 return nullptr;
748}
749
751 llvm::Function *Fn,
752 const CGFunctionInfo &FnInfo,
753 const FunctionArgList &Args,
754 SourceLocation Loc,
755 SourceLocation StartLoc) {
756 assert(!CurFn &&
757 "Do not use a CodeGenFunction object for more than one function");
758
759 const Decl *D = GD.getDecl();
760
761 DidCallStackSave = false;
762 CurCodeDecl = D;
763 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
764 if (FD && FD->usesSEHTry())
765 CurSEHParent = GD;
766 if (const Stmt *Body = D ? D->getBody() : nullptr) {
767 // Coroutines always emit lifetime markers.
768 if (isa<CoroutineBodyStmt>(Body))
769 ShouldEmitLifetimeMarkers = true;
770
771 // Detect jumps that invalidate lifetime markers or bypass auto-var-init.
772 bool NeedsBypassDetection =
773 ShouldEmitLifetimeMarkers ||
774 (CGM.getLangOpts().getTrivialAutoVarInit() !=
776 if (NeedsBypassDetection)
777 Bypasses.Init(CGM, Body);
778 }
779 CurFuncDecl = (D ? D->getNonClosureContext() : nullptr);
780 FnRetTy = RetTy;
781 CurFn = Fn;
782 CurFnInfo = &FnInfo;
783 assert(CurFn->isDeclaration() && "Function already has body?");
784
785 // If this function is ignored for any of the enabled sanitizers,
786 // disable the sanitizer for the function.
787 do {
788#define SANITIZER(NAME, ID) \
789 if (SanOpts.empty()) \
790 break; \
791 if (SanOpts.has(SanitizerKind::ID)) \
792 if (CGM.isInNoSanitizeList(SanitizerKind::ID, Fn, Loc)) \
793 SanOpts.set(SanitizerKind::ID, false);
794
795#include "clang/Basic/Sanitizers.def"
796#undef SANITIZER
797 } while (false);
798
799 if (D) {
800 const bool SanitizeBounds = SanOpts.hasOneOf(SanitizerKind::Bounds);
801 SanitizerMask no_sanitize_mask;
802 bool NoSanitizeCoverage = false;
803
804 for (auto *Attr : D->specific_attrs<NoSanitizeAttr>()) {
805 no_sanitize_mask |= Attr->getMask();
806 // SanitizeCoverage is not handled by SanOpts.
807 if (Attr->hasCoverage())
808 NoSanitizeCoverage = true;
809 }
810
811 // Apply the no_sanitize* attributes to SanOpts.
812 SanOpts.Mask &= ~no_sanitize_mask;
813 if (no_sanitize_mask & SanitizerKind::Address)
814 SanOpts.set(SanitizerKind::KernelAddress, false);
815 if (no_sanitize_mask & SanitizerKind::KernelAddress)
816 SanOpts.set(SanitizerKind::Address, false);
817 if (no_sanitize_mask & SanitizerKind::HWAddress)
818 SanOpts.set(SanitizerKind::KernelHWAddress, false);
819 if (no_sanitize_mask & SanitizerKind::KernelHWAddress)
820 SanOpts.set(SanitizerKind::HWAddress, false);
821
822 if (SanitizeBounds && !SanOpts.hasOneOf(SanitizerKind::Bounds))
823 Fn->addFnAttr(llvm::Attribute::NoSanitizeBounds);
824
825 if (NoSanitizeCoverage && CGM.getCodeGenOpts().hasSanitizeCoverage())
826 Fn->addFnAttr(llvm::Attribute::NoSanitizeCoverage);
827
828 // Some passes need the non-negated no_sanitize attribute. Pass them on.
829 if (CGM.getCodeGenOpts().hasSanitizeBinaryMetadata()) {
830 if (no_sanitize_mask & SanitizerKind::Thread)
831 Fn->addFnAttr("no_sanitize_thread");
832 }
833 }
834
836 CurFn->addFnAttr(llvm::Attribute::DisableSanitizerInstrumentation);
837 } else {
838 // Apply sanitizer attributes to the function.
839 if (SanOpts.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress))
840 Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
841 if (SanOpts.hasOneOf(SanitizerKind::HWAddress |
842 SanitizerKind::KernelHWAddress))
843 Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
844 if (SanOpts.has(SanitizerKind::MemtagStack))
845 Fn->addFnAttr(llvm::Attribute::SanitizeMemTag);
846 if (SanOpts.has(SanitizerKind::Thread))
847 Fn->addFnAttr(llvm::Attribute::SanitizeThread);
848 if (SanOpts.has(SanitizerKind::Concurrency))
849 Fn->addFnAttr(llvm::Attribute::SanitizeConcurrency);
850 if (SanOpts.has(SanitizerKind::Type))
851 Fn->addFnAttr(llvm::Attribute::SanitizeType);
852 if (SanOpts.has(SanitizerKind::NumericalStability))
853 Fn->addFnAttr(llvm::Attribute::SanitizeNumericalStability);
854 if (SanOpts.hasOneOf(SanitizerKind::Memory | SanitizerKind::KernelMemory))
855 Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
856 if (SanOpts.has(SanitizerKind::AllocToken))
857 Fn->addFnAttr(llvm::Attribute::SanitizeAllocToken);
858 }
859 if (SanOpts.has(SanitizerKind::SafeStack))
860 Fn->addFnAttr(llvm::Attribute::SafeStack);
861 if (SanOpts.has(SanitizerKind::ShadowCallStack))
862 Fn->addFnAttr(llvm::Attribute::ShadowCallStack);
863
864 if (SanOpts.has(SanitizerKind::Realtime))
865 if (FD && FD->getASTContext().hasAnyFunctionEffects())
866 for (const FunctionEffectWithCondition &Fe : FD->getFunctionEffects()) {
867 if (Fe.Effect.kind() == FunctionEffect::Kind::NonBlocking)
868 Fn->addFnAttr(llvm::Attribute::SanitizeRealtime);
869 else if (Fe.Effect.kind() == FunctionEffect::Kind::Blocking)
870 Fn->addFnAttr(llvm::Attribute::SanitizeRealtimeBlocking);
871 }
872
873 // Apply fuzzing attribute to the function.
874 if (SanOpts.hasOneOf(SanitizerKind::Fuzzer | SanitizerKind::FuzzerNoLink))
875 Fn->addFnAttr(llvm::Attribute::OptForFuzzing);
876
877 // Ignore TSan/CSan memory accesses from within ObjC/ObjC++ dealloc,
878 // initialize, .cxx_destruct, __destroy_helper_block_ and all of their callees
879 // at run time.
880 if (SanOpts.hasOneOf(SanitizerKind::Thread | SanitizerKind::Concurrency)) {
881 if (const auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
882 const IdentifierInfo *II = OMD->getSelector().getIdentifierInfoForSlot(0);
883 if (OMD->getMethodFamily() == OMF_dealloc ||
884 OMD->getMethodFamily() == OMF_initialize ||
885 (OMD->getSelector().isUnarySelector() && II->isStr(".cxx_destruct"))) {
887 }
888 }
889 }
890
891 // Ignore unrelated casts in STL allocate() since the allocator must cast
892 // from void* to T* before object initialization completes. Don't match on the
893 // namespace because not all allocators are in std::
894 if (D && SanOpts.has(SanitizerKind::CFIUnrelatedCast)) {
896 SanOpts.Mask &= ~SanitizerKind::CFIUnrelatedCast;
897 }
898
899 // Ignore null checks in coroutine functions since the coroutines passes
900 // are not aware of how to move the extra UBSan instructions across the split
901 // coroutine boundaries.
902 if (D && SanOpts.has(SanitizerKind::Null))
903 if (FD && FD->getBody() &&
904 FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass)
906
907 // Apply xray attributes to the function (as a string, for now)
908 bool AlwaysXRayAttr = false;
909 if (const auto *XRayAttr = D ? D->getAttr<XRayInstrumentAttr>() : nullptr) {
910 if (CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
912 CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
914 if (XRayAttr->alwaysXRayInstrument() && ShouldXRayInstrumentFunction()) {
915 Fn->addFnAttr("function-instrument", "xray-always");
916 AlwaysXRayAttr = true;
917 }
918 if (XRayAttr->neverXRayInstrument())
919 Fn->addFnAttr("function-instrument", "xray-never");
920 if (const auto *LogArgs = D->getAttr<XRayLogArgsAttr>())
922 Fn->addFnAttr("xray-log-args",
923 llvm::utostr(LogArgs->getArgumentCount()));
924 }
925 } else {
926 if (ShouldXRayInstrumentFunction() && !CGM.imbueXRayAttrs(Fn, Loc))
927 Fn->addFnAttr(
928 "xray-instruction-threshold",
929 llvm::itostr(CGM.getCodeGenOpts().XRayInstructionThreshold));
930 }
931
933 if (CGM.getCodeGenOpts().XRayIgnoreLoops)
934 Fn->addFnAttr("xray-ignore-loops");
935
936 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
938 Fn->addFnAttr("xray-skip-exit");
939
940 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
942 Fn->addFnAttr("xray-skip-entry");
943
944 auto FuncGroups = CGM.getCodeGenOpts().XRayTotalFunctionGroups;
945 if (FuncGroups > 1) {
946 auto FuncName = llvm::ArrayRef<uint8_t>(CurFn->getName().bytes_begin(),
947 CurFn->getName().bytes_end());
948 auto Group = crc32(FuncName) % FuncGroups;
949 if (Group != CGM.getCodeGenOpts().XRaySelectedFunctionGroup &&
950 !AlwaysXRayAttr)
951 Fn->addFnAttr("function-instrument", "xray-never");
952 }
953 }
954
955 if (CGM.getCodeGenOpts().getProfileInstr() !=
956 llvm::driver::ProfileInstrKind::ProfileNone) {
957 switch (CGM.isFunctionBlockedFromProfileInstr(Fn, Loc)) {
959 Fn->addFnAttr(llvm::Attribute::SkipProfile);
960 break;
962 Fn->addFnAttr(llvm::Attribute::NoProfile);
963 break;
965 break;
966 }
967 }
968
969 unsigned Count, Offset;
970 StringRef Section;
971 if (const auto *Attr =
972 D ? D->getAttr<PatchableFunctionEntryAttr>() : nullptr) {
973 Count = Attr->getCount();
974 Offset = Attr->getOffset();
975 Section = Attr->getSection();
976 } else {
977 Count = CGM.getCodeGenOpts().PatchableFunctionEntryCount;
978 Offset = CGM.getCodeGenOpts().PatchableFunctionEntryOffset;
979 }
980 if (Section.empty())
981 Section = CGM.getCodeGenOpts().PatchableFunctionEntrySection;
982 if (Count && Offset <= Count) {
983 Fn->addFnAttr("patchable-function-entry", std::to_string(Count - Offset));
984 if (Offset)
985 Fn->addFnAttr("patchable-function-prefix", std::to_string(Offset));
986 if (!Section.empty())
987 Fn->addFnAttr("patchable-function-entry-section", Section);
988 }
989 // Instruct that functions for COFF/CodeView targets should start with a
990 // patchable instruction, but only on x86/x64. Don't forward this to ARM/ARM64
991 // backends as they don't need it -- instructions on these architectures are
992 // always atomically patchable at runtime.
993 if (CGM.getCodeGenOpts().HotPatch &&
994 getContext().getTargetInfo().getTriple().isX86() &&
995 getContext().getTargetInfo().getTriple().getEnvironment() !=
996 llvm::Triple::CODE16)
997 Fn->addFnAttr("patchable-function", "prologue-short-redirect");
998
999 // Add no-jump-tables value.
1000 if (CGM.getCodeGenOpts().NoUseJumpTables)
1001 Fn->addFnAttr("no-jump-tables", "true");
1002
1003 // Add no-inline-line-tables value.
1004 if (CGM.getCodeGenOpts().NoInlineLineTables)
1005 Fn->addFnAttr("no-inline-line-tables");
1006
1007 // Add profile-sample-accurate value.
1008 if (CGM.getCodeGenOpts().ProfileSampleAccurate)
1009 Fn->addFnAttr("profile-sample-accurate");
1010
1011 if (!CGM.getCodeGenOpts().SampleProfileFile.empty())
1012 Fn->addFnAttr("use-sample-profile");
1013
1014 if (D && D->hasAttr<CFICanonicalJumpTableAttr>())
1015 Fn->addFnAttr("cfi-canonical-jump-table");
1016
1017 if (D && D->hasAttr<NoProfileFunctionAttr>())
1018 Fn->addFnAttr(llvm::Attribute::NoProfile);
1019
1020 if (D && D->hasAttr<HybridPatchableAttr>())
1021 Fn->addFnAttr(llvm::Attribute::HybridPatchable);
1022
1023 if (D) {
1024 // Function attributes take precedence over command line flags.
1025 if (auto *A = D->getAttr<FunctionReturnThunksAttr>()) {
1026 switch (A->getThunkType()) {
1027 case FunctionReturnThunksAttr::Kind::Keep:
1028 break;
1029 case FunctionReturnThunksAttr::Kind::Extern:
1030 Fn->addFnAttr(llvm::Attribute::FnRetThunkExtern);
1031 break;
1032 }
1033 } else if (CGM.getCodeGenOpts().FunctionReturnThunks)
1034 Fn->addFnAttr(llvm::Attribute::FnRetThunkExtern);
1035 }
1036
1037 if (FD && (getLangOpts().OpenCL ||
1038 (getLangOpts().CUDA &&
1039 getContext().getTargetInfo().getTriple().isSPIRV()) ||
1040 ((getLangOpts().HIP || getLangOpts().OffloadViaLLVM) &&
1041 getLangOpts().CUDAIsDevice))) {
1042 // Add metadata for a kernel function.
1043 EmitKernelMetadata(FD, Fn);
1044 }
1045
1046 if (FD && FD->hasAttr<ClspvLibclcBuiltinAttr>()) {
1047 Fn->setMetadata("clspv_libclc_builtin",
1048 llvm::MDNode::get(getLLVMContext(), {}));
1049 }
1050
1051 // If we are checking function types, emit a function type signature as
1052 // prologue data. Kernel functions have strict alignment requirements and
1053 // cannot be call indirectly so we do not instrument them.
1054 if (FD && SanOpts.has(SanitizerKind::Function) &&
1056 llvm::isCallableCC(Fn->getCallingConv())) {
1057 if (llvm::Constant *PrologueSig = getPrologueSignature(CGM, FD)) {
1058 llvm::LLVMContext &Ctx = Fn->getContext();
1059 llvm::MDBuilder MDB(Ctx);
1060 Fn->setMetadata(
1061 llvm::LLVMContext::MD_func_sanitize,
1062 MDB.createRTTIPointerPrologue(
1063 PrologueSig, getUBSanFunctionTypeHash(FD->getType())));
1064 }
1065 }
1066
1067 // If we're checking nullability, we need to know whether we can check the
1068 // return value. Initialize the flag to 'true' and refine it in EmitParmDecl.
1069 if (SanOpts.has(SanitizerKind::NullabilityReturn)) {
1070 auto Nullability = FnRetTy->getNullability();
1071 if (Nullability && *Nullability == NullabilityKind::NonNull &&
1072 !FnRetTy->isRecordType()) {
1073 if (!(SanOpts.has(SanitizerKind::ReturnsNonnullAttribute) &&
1074 CurCodeDecl && CurCodeDecl->getAttr<ReturnsNonNullAttr>()))
1075 RetValNullabilityPrecondition =
1076 llvm::ConstantInt::getTrue(getLLVMContext());
1077 }
1078 }
1079
1080 // Annotate C++ special member functions so that CopyProfPass can instrument
1081 // them, provided the object is at least as large as the size threshold.
1082 if (CGM.getCodeGenOpts().CopyProf) {
1083 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D)) {
1084 StringRef Attr;
1085 if (const auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
1086 if (!CD->isMoveConstructor())
1087 Attr =
1088 CD->isCopyConstructor() ? "copyprof-copy-ctor" : "copyprof-ctor";
1089 } else if (isa<CXXDestructorDecl>(MD)) {
1090 Attr = "copyprof-dtor";
1091 } else if (MD->isCopyAssignmentOperator()) {
1092 Attr = "copyprof-copy-assign-op";
1093 }
1094 if (!Attr.empty()) {
1095 // Finally, add the object size in bytes to the annotation.
1096 // A special member function always has an implicit object parameter, so
1097 // its type is guaranteed to be complete here.
1100 if (ObjSize.getQuantity() >=
1101 CGM.getCodeGenOpts().CopyProfStaticSizeThreshold)
1102 Fn->addFnAttr(Attr, llvm::utostr(ObjSize.getQuantity()));
1103 }
1104 }
1105 }
1106
1107 // If we're in C++ mode and the function name is "main", it is guaranteed
1108 // to be norecurse by the standard (3.6.1.3 "The function main shall not be
1109 // used within a program").
1110 //
1111 // OpenCL C 2.0 v2.2-11 s6.9.i:
1112 // Recursion is not supported.
1113 //
1114 // HLSL
1115 // Recursion is not supported.
1116 //
1117 // SYCL v1.2.1 s3.10:
1118 // kernels cannot include RTTI information, exception classes,
1119 // recursive code, virtual functions or make use of C++ libraries that
1120 // are not compiled for the device.
1121 if (FD &&
1122 ((getLangOpts().CPlusPlus && FD->isMain()) || getLangOpts().OpenCL ||
1123 getLangOpts().HLSL || getLangOpts().SYCLIsDevice ||
1124 (getLangOpts().CUDA && FD->hasAttr<CUDAGlobalAttr>())))
1125 Fn->addFnAttr(llvm::Attribute::NoRecurse);
1126
1127 llvm::RoundingMode RM = getLangOpts().getDefaultRoundingMode();
1128 llvm::fp::ExceptionBehavior FPExceptionBehavior =
1129 ToConstrainedExceptMD(getLangOpts().getDefaultExceptionMode());
1130 Builder.setDefaultConstrainedRounding(RM);
1131 Builder.setDefaultConstrainedExcept(FPExceptionBehavior);
1132 if ((FD && (FD->UsesFPIntrin() || FD->hasAttr<StrictFPAttr>())) ||
1133 (!FD && (FPExceptionBehavior != llvm::fp::ebIgnore ||
1134 RM != llvm::RoundingMode::NearestTiesToEven))) {
1135 Builder.setIsFPConstrained(true);
1136 Fn->addFnAttr(llvm::Attribute::StrictFP);
1137 }
1138
1139 // If a custom alignment is used, force realigning to this alignment on
1140 // any main function which certainly will need it.
1141 if (FD && ((FD->isMain() || FD->isMSVCRTEntryPoint()) &&
1142 CGM.getCodeGenOpts().StackAlignment))
1143 Fn->addFnAttr("stackrealign");
1144
1145 // "main" doesn't need to zero out call-used registers.
1146 if (FD && FD->isMain())
1147 Fn->removeFnAttr("zero-call-used-regs");
1148
1149 // Add vscale_range attribute if appropriate.
1150 auto IsArmStreaming = TargetInfo::ArmStreamingKind::NotStreaming;
1151 if (FD) {
1152 if (const auto *T = FD->getType()->getAs<FunctionProtoType>())
1153 if (T->getAArch64SMEAttributes() &
1156
1157 if (IsArmStreamingFunction(FD, true))
1159 }
1160 const auto *FeatureMap =
1161 FD ? &getContext().getFunctionFeatureMap(FD) : nullptr;
1162 if (auto VScaleRange = getContext().getTargetInfo().getVScaleRange(
1163 getLangOpts(), IsArmStreaming, FeatureMap))
1164 CurFn->addFnAttr(llvm::Attribute::getWithVScaleRangeArgs(
1165 getLLVMContext(), VScaleRange->first, VScaleRange->second));
1166
1167 llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
1168
1169 // Create a marker to make it easy to insert allocas into the entryblock
1170 // later. Don't create this with the builder, because we don't want it
1171 // folded.
1172 llvm::Value *Poison = llvm::PoisonValue::get(Int32Ty);
1173 AllocaInsertPt = new llvm::BitCastInst(Poison, Int32Ty, "allocapt", EntryBB);
1174
1176
1177 Builder.SetInsertPoint(EntryBB);
1178
1179 // If we're checking the return value, allocate space for a pointer to a
1180 // precise source location of the checked return statement.
1181 if (requiresReturnValueCheck()) {
1182 ReturnLocation = CreateDefaultAlignTempAlloca(Int8PtrTy, "return.sloc.ptr");
1183 Builder.CreateStore(llvm::ConstantPointerNull::get(Int8PtrTy),
1184 ReturnLocation);
1185 }
1186
1187 // Emit subprogram debug descriptor.
1188 if (CGDebugInfo *DI = getDebugInfo()) {
1189 // Reconstruct the type from the argument list so that implicit parameters,
1190 // such as 'this' and 'vtt', show up in the debug info. Preserve the calling
1191 // convention.
1192 DI->emitFunctionStart(GD, Loc, StartLoc,
1193 DI->getFunctionType(FD, RetTy, Args), CurFn,
1195 }
1196
1198 if (CGM.getCodeGenOpts().InstrumentFunctions)
1199 CurFn->addFnAttr("instrument-function-entry", "__cyg_profile_func_enter");
1200 if (CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining)
1201 CurFn->addFnAttr("instrument-function-entry-inlined",
1202 "__cyg_profile_func_enter");
1203 if (CGM.getCodeGenOpts().InstrumentFunctionEntryBare)
1204 CurFn->addFnAttr("instrument-function-entry-inlined",
1205 "__cyg_profile_func_enter_bare");
1206 }
1207
1208 // Since emitting the mcount call here impacts optimizations such as function
1209 // inlining, we just add an attribute to insert a mcount call in backend.
1210 // The attribute "counting-function" is set to mcount function name which is
1211 // architecture dependent.
1212 if (CGM.getCodeGenOpts().InstrumentForProfiling) {
1213 // Calls to fentry/mcount should not be generated if function has
1214 // the no_instrument_function attribute.
1215 if (!CurFuncDecl || !CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) {
1216 if (CGM.getCodeGenOpts().CallFEntry)
1217 Fn->addFnAttr("fentry-call", "true");
1218 else {
1219 Fn->addFnAttr("instrument-function-entry-inlined",
1220 getTarget().getMCountName());
1221 }
1222 if (CGM.getCodeGenOpts().MNopMCount) {
1223 if (!CGM.getCodeGenOpts().CallFEntry)
1224 CGM.getDiags().Report(diag::err_opt_not_valid_without_opt)
1225 << "-mnop-mcount" << "-mfentry";
1226 Fn->addFnAttr("mnop-mcount");
1227 }
1228
1229 if (CGM.getCodeGenOpts().RecordMCount) {
1230 if (!CGM.getCodeGenOpts().CallFEntry)
1231 CGM.getDiags().Report(diag::err_opt_not_valid_without_opt)
1232 << "-mrecord-mcount" << "-mfentry";
1233 Fn->addFnAttr("mrecord-mcount");
1234 }
1235 }
1236 }
1237
1238 if (CGM.getCodeGenOpts().PackedStack) {
1239 if (getContext().getTargetInfo().getTriple().getArch() !=
1240 llvm::Triple::systemz)
1241 CGM.getDiags().Report(diag::err_opt_not_valid_on_target)
1242 << "-mpacked-stack";
1243 Fn->addFnAttr("packed-stack");
1244 }
1245
1246 if (!CGM.getCodeGenOpts().ZOSPPA1Name)
1247 Fn->addFnAttr("zos-ppa1-name", "");
1248 else if (getContext().getTargetInfo().getTriple().isOSzOS() && FD &&
1249 FD->hasAttr<AsmLabelAttr>()) {
1250 std::string SrcName = CGM.getMangledNameIgnoringAsmLabel(GD, FD);
1251 Fn->addFnAttr("zos-ppa1-name", SrcName);
1252 }
1253
1254 if (CGM.getCodeGenOpts().WarnStackSize != UINT_MAX &&
1255 !CGM.getDiags().isIgnored(diag::warn_fe_backend_frame_larger_than, Loc))
1256 Fn->addFnAttr("warn-stack-size",
1257 std::to_string(CGM.getCodeGenOpts().WarnStackSize));
1258
1259 if (RetTy->isVoidType()) {
1260 // Void type; nothing to return.
1262
1263 // Count the implicit return.
1264 if (!endsWithReturn(D))
1265 ++NumReturnExprs;
1266 } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect) {
1267 // Indirect return; emit returned value directly into sret slot.
1268 // This reduces code size, and affects correctness in C++.
1269 auto AI = CurFn->arg_begin();
1270 if (CurFnInfo->getReturnInfo().isSRetAfterThis())
1271 ++AI;
1273 &*AI, RetTy, CurFnInfo->getReturnInfo().getIndirectAlign(), false,
1274 nullptr, nullptr, KnownNonNull);
1275 if (!CurFnInfo->getReturnInfo().getIndirectByVal()) {
1277 CreateDefaultAlignTempAlloca(ReturnValue.getType(), "result.ptr");
1278 Builder.CreateStore(ReturnValue.emitRawPointer(*this),
1280 }
1281 } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::InAlloca &&
1282 !hasScalarEvaluationKind(CurFnInfo->getReturnType())) {
1283 // Load the sret pointer from the argument struct and return into that.
1284 unsigned Idx = CurFnInfo->getReturnInfo().getInAllocaFieldIndex();
1285 llvm::Function::arg_iterator EI = CurFn->arg_end();
1286 --EI;
1287 llvm::Value *Addr = Builder.CreateStructGEP(
1288 CurFnInfo->getArgStruct(), &*EI, Idx);
1289 llvm::Type *Ty =
1290 cast<llvm::GetElementPtrInst>(Addr)->getResultElementType();
1292 Addr = Builder.CreateAlignedLoad(Ty, Addr, getPointerAlign(), "agg.result");
1294 CGM.getNaturalTypeAlignment(RetTy), KnownNonNull);
1295 } else {
1296 ReturnValue = CreateIRTempWithoutCast(RetTy, "retval");
1297
1298 // Tell the epilog emitter to autorelease the result. We do this
1299 // now so that various specialized functions can suppress it
1300 // during their IR-generation.
1301 if (getLangOpts().ObjCAutoRefCount &&
1302 !CurFnInfo->isReturnsRetained() &&
1303 RetTy->isObjCRetainableType())
1304 AutoreleaseResult = true;
1305 }
1306
1308
1309 PrologueCleanupDepth = EHStack.stable_begin();
1310
1311 // Emit OpenMP specific initialization of the device functions.
1312 if (getLangOpts().OpenMP && CurCodeDecl)
1313 CGM.getOpenMPRuntime().emitFunctionProlog(*this, CurCodeDecl);
1314
1315 if (FD && getLangOpts().HLSL) {
1316 // Handle emitting HLSL entry functions.
1317 if (FD->hasAttr<HLSLShaderAttr>()) {
1318 CGM.getHLSLRuntime().emitEntryFunction(FD, Fn);
1319 }
1320 }
1321
1323
1324 if (const CXXMethodDecl *MD = dyn_cast_if_present<CXXMethodDecl>(D);
1325 MD && !MD->isStatic()) {
1326 bool IsInLambda =
1327 MD->getParent()->isLambda() && MD->getOverloadedOperator() == OO_Call;
1329 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
1330 if (IsInLambda) {
1331 // We're in a lambda; figure out the captures.
1335 // If the lambda captures the object referred to by '*this' - either by
1336 // value or by reference, make sure CXXThisValue points to the correct
1337 // object.
1338
1339 // Get the lvalue for the field (which is a copy of the enclosing object
1340 // or contains the address of the enclosing object).
1342 if (!LambdaThisCaptureField->getType()->isPointerType()) {
1343 // If the enclosing object was captured by value, just use its
1344 // address. Sign this pointer.
1345 CXXThisValue = ThisFieldLValue.getPointer(*this);
1346 } else {
1347 // Load the lvalue pointed to by the field, since '*this' was captured
1348 // by reference.
1349 CXXThisValue =
1350 EmitLoadOfLValue(ThisFieldLValue, SourceLocation()).getScalarVal();
1351 }
1352 }
1353 for (auto *FD : MD->getParent()->fields()) {
1354 if (FD->hasCapturedVLAType()) {
1355 auto *ExprArg = EmitLoadOfLValue(EmitLValueForLambdaField(FD),
1357 auto VAT = FD->getCapturedVLAType();
1358 VLASizeMap[VAT->getSizeExpr()] = ExprArg;
1359 }
1360 }
1361 } else if (MD->isImplicitObjectMemberFunction()) {
1362 // Not in a lambda; just use 'this' from the method.
1363 // FIXME: Should we generate a new load for each use of 'this'? The
1364 // fast register allocator would be happier...
1365 CXXThisValue = CXXABIThisValue;
1366 }
1367
1368 // Check the 'this' pointer once per function, if it's available.
1369 if (CXXABIThisValue) {
1370 SanitizerSet SkippedChecks;
1371 SkippedChecks.set(SanitizerKind::ObjectSize, true);
1372 QualType ThisTy = MD->getThisType();
1373
1374 // If this is the call operator of a lambda with no captures, it
1375 // may have a static invoker function, which may call this operator with
1376 // a null 'this' pointer.
1378 SkippedChecks.set(SanitizerKind::Null, true);
1379
1382 Loc, CXXABIThisValue, ThisTy, CXXABIThisAlignment, SkippedChecks);
1383 }
1384 }
1385
1386 // If any of the arguments have a variably modified type, make sure to
1387 // emit the type size, but only if the function is not naked. Naked functions
1388 // have no prolog to run this evaluation.
1389 if (!FD || !FD->hasAttr<NakedAttr>()) {
1390 for (const VarDecl *VD : Args) {
1391 // Dig out the type as written from ParmVarDecls; it's unclear whether
1392 // the standard (C99 6.9.1p10) requires this, but we're following the
1393 // precedent set by gcc.
1394 QualType Ty;
1395 if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD))
1396 Ty = PVD->getOriginalType();
1397 else
1398 Ty = VD->getType();
1399
1400 if (Ty->isVariablyModifiedType())
1402 }
1403 }
1404 // Emit a location at the end of the prologue.
1405 if (CGDebugInfo *DI = getDebugInfo())
1406 DI->EmitLocation(Builder, StartLoc);
1407 // TODO: Do we need to handle this in two places like we do with
1408 // target-features/target-cpu?
1409 if (CurFuncDecl)
1410 if (const auto *VecWidth = CurFuncDecl->getAttr<MinVectorWidthAttr>())
1411 LargestVectorWidth = VecWidth->getVectorWidth();
1412
1413 if (CGM.shouldEmitConvergenceTokens())
1414 ConvergenceTokenStack.push_back(getOrEmitConvergenceEntryToken(CurFn));
1415}
1416
1420 if (const CompoundStmt *S = dyn_cast<CompoundStmt>(Body))
1422 else
1423 EmitStmt(Body);
1424}
1425
1426/// When instrumenting to collect profile data, the counts for some blocks
1427/// such as switch cases need to not include the fall-through counts, so
1428/// emit a branch around the instrumentation code. When not instrumenting,
1429/// this just calls EmitBlock().
1431 const Stmt *S) {
1432 llvm::BasicBlock *SkipCountBB = nullptr;
1433 if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr()) {
1434 // When instrumenting for profiling, the fallthrough to certain
1435 // statements needs to skip over the instrumentation code so that we
1436 // get an accurate count.
1437 SkipCountBB = createBasicBlock("skipcount");
1438 EmitBranch(SkipCountBB);
1439 }
1440 EmitBlock(BB);
1441 uint64_t CurrentCount = getCurrentProfileCount();
1444 if (SkipCountBB)
1445 EmitBlock(SkipCountBB);
1446}
1447
1448/// Tries to mark the given function nounwind based on the
1449/// non-existence of any throwing calls within it. We believe this is
1450/// lightweight enough to do at -O0.
1451static void TryMarkNoThrow(llvm::Function *F) {
1452 // LLVM treats 'nounwind' on a function as part of the type, so we
1453 // can't do this on functions that can be overwritten.
1454 if (F->isInterposable()) return;
1455
1456 for (llvm::BasicBlock &BB : *F)
1457 for (llvm::Instruction &I : BB)
1458 if (I.mayThrow())
1459 return;
1460
1461 F->setDoesNotThrow();
1462}
1463
1465 FunctionArgList &Args) {
1466 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
1467 QualType ResTy = FD->getReturnType();
1468
1469 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
1470 if (MD && MD->isImplicitObjectMemberFunction()) {
1471 if (CGM.getCXXABI().HasThisReturn(GD))
1472 ResTy = MD->getThisType();
1473 else if (CGM.getCXXABI().hasMostDerivedReturn(GD))
1474 ResTy = CGM.getContext().VoidPtrTy;
1475 CGM.getCXXABI().buildThisParam(*this, Args);
1476 }
1477
1478 // The base version of an inheriting constructor whose constructed base is a
1479 // virtual base is not passed any arguments (because it doesn't actually call
1480 // the inherited constructor).
1481 bool PassedParams = true;
1482 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
1483 if (auto Inherited = CD->getInheritedConstructor())
1484 PassedParams =
1485 getTypes().inheritingCtorHasParams(Inherited, GD.getCtorType());
1486
1487 if (PassedParams) {
1488 for (auto *Param : FD->parameters()) {
1489 Args.push_back(Param);
1490 if (!Param->hasAttr<PassObjectSizeAttr>())
1491 continue;
1492
1494 getContext(), Param->getDeclContext(), Param->getLocation(),
1495 /*Id=*/nullptr, getContext().getSizeType(), ImplicitParamKind::Other);
1496 SizeArguments[Param] = Implicit;
1497 Args.push_back(Implicit);
1498 }
1499 }
1500
1501 if (MD && (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)))
1502 CGM.getCXXABI().addImplicitStructorParams(*this, ResTy, Args);
1503
1504 return ResTy;
1505}
1506
1507void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1508 const CGFunctionInfo &FnInfo) {
1509 assert(Fn && "generating code for null Function");
1510 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
1511 CurGD = GD;
1512
1513 FunctionArgList Args;
1514 QualType ResTy = BuildFunctionArgList(GD, Args);
1515
1516 CGM.getTargetCodeGenInfo().checkFunctionABI(CGM, FD);
1517
1518 if (FD->isInlineBuiltinDeclaration()) {
1519 // When generating code for a builtin with an inline declaration, use a
1520 // mangled name to hold the actual body, while keeping an external
1521 // definition in case the function pointer is referenced somewhere.
1522 std::string FDInlineName = (Fn->getName() + ".inline").str();
1523 llvm::Module *M = Fn->getParent();
1524 llvm::Function *Clone = M->getFunction(FDInlineName);
1525 if (!Clone) {
1526 Clone = llvm::Function::Create(Fn->getFunctionType(),
1527 llvm::GlobalValue::InternalLinkage,
1528 Fn->getAddressSpace(), FDInlineName, M);
1529 Clone->setCallingConv(Fn->getCallingConv());
1530 Clone->setAttributes(Fn->getAttributes());
1531 Clone->addFnAttr(llvm::Attribute::AlwaysInline);
1532 }
1533 Fn->setLinkage(llvm::GlobalValue::ExternalLinkage);
1534 Fn = Clone;
1535 } else {
1536 // Detect the unusual situation where an inline version is shadowed by a
1537 // non-inline version. In that case we should pick the external one
1538 // everywhere. That's GCC behavior too. Unfortunately, I cannot find a way
1539 // to detect that situation before we reach codegen, so do some late
1540 // replacement.
1541 for (const FunctionDecl *PD = FD->getPreviousDecl(); PD;
1542 PD = PD->getPreviousDecl()) {
1543 if (LLVM_UNLIKELY(PD->isInlineBuiltinDeclaration())) {
1544 std::string FDInlineName = (Fn->getName() + ".inline").str();
1545 llvm::Module *M = Fn->getParent();
1546 if (llvm::Function *Clone = M->getFunction(FDInlineName)) {
1547 Clone->replaceAllUsesWith(Fn);
1548 Clone->eraseFromParent();
1549 }
1550 break;
1551 }
1552 }
1553 }
1554
1555 // Check if we should generate debug info for this function.
1556 if (FD->hasAttr<NoDebugAttr>()) {
1557 // Clear non-distinct debug info that was possibly attached to the function
1558 // due to an earlier declaration without the nodebug attribute
1559 Fn->setSubprogram(nullptr);
1560 // Disable debug info indefinitely for this function
1561 DebugInfo = nullptr;
1562 }
1563 // Finalize function debug info on exit.
1564 llvm::scope_exit Cleanup([this] {
1565 if (CGDebugInfo *DI = getDebugInfo())
1566 DI->completeFunction();
1567 });
1568
1569 // The function might not have a body if we're generating thunks for a
1570 // function declaration.
1571 SourceRange BodyRange;
1572 if (Stmt *Body = FD->getBody())
1573 BodyRange = Body->getSourceRange();
1574 else
1575 BodyRange = FD->getLocation();
1576 CurEHLocation = BodyRange.getEnd();
1577
1578 // Use the location of the start of the function to determine where
1579 // the function definition is located. By default use the location
1580 // of the declaration as the location for the subprogram. A function
1581 // may lack a declaration in the source code if it is created by code
1582 // gen. (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
1583 SourceLocation Loc = FD->getLocation();
1584
1585 // If this is a function specialization then use the pattern body
1586 // as the location for the function.
1587 if (const FunctionDecl *SpecDecl = FD->getTemplateInstantiationPattern())
1588 if (SpecDecl->hasBody(SpecDecl))
1589 Loc = SpecDecl->getLocation();
1590
1591 Stmt *Body = FD->getBody();
1592
1593 // Emit the standard function prologue.
1594 StartFunction(GD, ResTy, Fn, FnInfo, Args, Loc, BodyRange.getBegin());
1595
1596 // Save parameters for coroutine function.
1597 if (Body && isa_and_nonnull<CoroutineBodyStmt>(Body))
1598 llvm::append_range(FnArgs, FD->parameters());
1599
1600 // Ensure that the function adheres to the forward progress guarantee, which
1601 // is required by certain optimizations.
1602 // In C++11 and up, the attribute will be removed if the body contains a
1603 // trivial empty loop.
1605 CurFn->addFnAttr(llvm::Attribute::MustProgress);
1606
1607 // Generate the body of the function.
1608 PGO->assignRegionCounters(GD, CurFn);
1609 if (isa<CXXDestructorDecl>(FD))
1610 EmitDestructorBody(Args);
1611 else if (isa<CXXConstructorDecl>(FD))
1612 EmitConstructorBody(Args);
1613 else if (getLangOpts().CUDA &&
1614 !getLangOpts().CUDAIsDevice &&
1615 FD->hasAttr<CUDAGlobalAttr>())
1616 CGM.getCUDARuntime().emitDeviceStub(*this, Args);
1617 else if (isa<CXXMethodDecl>(FD) &&
1618 cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) {
1619 // The lambda static invoker function is special, because it forwards or
1620 // clones the body of the function call operator (but is actually static).
1622 } else if (isa<CXXMethodDecl>(FD) &&
1624 !FnInfo.isDelegateCall() &&
1625 cast<CXXMethodDecl>(FD)->getParent()->getLambdaStaticInvoker() &&
1626 hasInAllocaArg(cast<CXXMethodDecl>(FD))) {
1627 // If emitting a lambda with static invoker on X86 Windows, change
1628 // the call operator body.
1629 // Make sure that this is a call operator with an inalloca arg and check
1630 // for delegate call to make sure this is the original call op and not the
1631 // new forwarding function for the static invoker.
1633 } else if (FD->isDefaulted() && isa<CXXMethodDecl>(FD) &&
1634 (cast<CXXMethodDecl>(FD)->isCopyAssignmentOperator() ||
1635 cast<CXXMethodDecl>(FD)->isMoveAssignmentOperator())) {
1636 // Implicit copy-assignment gets the same special treatment as implicit
1637 // copy-constructors.
1639 } else if (DeviceKernelAttr::isOpenCLSpelling(
1640 FD->getAttr<DeviceKernelAttr>()) &&
1642 CallArgList CallArgs;
1643 for (unsigned i = 0; i < Args.size(); ++i) {
1644 Address ArgAddr = GetAddrOfLocalVar(Args[i]);
1645 QualType ArgQualType = Args[i]->getType();
1646 RValue ArgRValue = convertTempToRValue(ArgAddr, ArgQualType, Loc);
1647 CallArgs.add(ArgRValue, ArgQualType);
1648 }
1650 const FunctionType *FT = cast<FunctionType>(FD->getType());
1651 CGM.getTargetCodeGenInfo().setOCLKernelStubCallingConvention(FT);
1652 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeFreeFunctionCall(
1653 CallArgs, FT, /*ChainCall=*/false, getCurrentFunctionDecl());
1654 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FnInfo);
1655 llvm::Constant *GDStubFunctionPointer =
1656 CGM.getRawFunctionPointer(GDStub, FTy);
1657 CGCallee GDStubCallee = CGCallee::forDirect(GDStubFunctionPointer, GDStub);
1658 EmitCall(FnInfo, GDStubCallee, ReturnValueSlot(), CallArgs, nullptr, false,
1659 Loc);
1660 } else if (Body) {
1661 EmitFunctionBody(Body);
1662 } else
1663 llvm_unreachable("no definition for emitted function");
1664
1665 // C++11 [stmt.return]p2:
1666 // Flowing off the end of a function [...] results in undefined behavior in
1667 // a value-returning function.
1668 // C11 6.9.1p12:
1669 // If the '}' that terminates a function is reached, and the value of the
1670 // function call is used by the caller, the behavior is undefined.
1672 !FD->getReturnType()->isVoidType() && Builder.GetInsertBlock()) {
1673 bool ShouldEmitUnreachable =
1674 CGM.getCodeGenOpts().StrictReturn ||
1675 !CGM.MayDropFunctionReturn(FD->getASTContext(), FD->getReturnType());
1676 if (SanOpts.has(SanitizerKind::Return)) {
1677 auto CheckOrdinal = SanitizerKind::SO_Return;
1678 auto CheckHandler = SanitizerHandler::MissingReturn;
1679 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
1680 llvm::Value *IsFalse = Builder.getFalse();
1681 EmitCheck(std::make_pair(IsFalse, CheckOrdinal), CheckHandler,
1683 } else if (ShouldEmitUnreachable) {
1684 if (CGM.getCodeGenOpts().OptimizationLevel == 0)
1685 EmitTrapCall(llvm::Intrinsic::trap);
1686 }
1687 if (SanOpts.has(SanitizerKind::Return) || ShouldEmitUnreachable) {
1688 Builder.CreateUnreachable();
1689 Builder.ClearInsertionPoint();
1690 }
1691 }
1692
1693 // Emit the standard function epilogue.
1694 FinishFunction(BodyRange.getEnd());
1695
1696 PGO->verifyCounterMap();
1697
1698 if (CurCodeDecl->hasAttr<PersonalityAttr>()) {
1699 StringRef Identifier =
1700 CurCodeDecl->getAttr<PersonalityAttr>()->getRoutine()->getName();
1701 llvm::FunctionCallee PersonalityRoutine =
1702 CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true),
1703 Identifier, {}, /*local=*/true);
1704 Fn->setPersonalityFn(cast<llvm::Constant>(PersonalityRoutine.getCallee()));
1705 }
1706
1707 // If we haven't marked the function nothrow through other means, do
1708 // a quick pass now to see if we can.
1709 if (!CurFn->doesNotThrow())
1711}
1712
1713/// ContainsLabel - Return true if the statement contains a label in it. If
1714/// this statement is not executed normally, it not containing a label means
1715/// that we can just remove the code.
1716bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
1717 // Null statement, not a label!
1718 if (!S) return false;
1719
1720 // If this is a label, we have to emit the code, consider something like:
1721 // if (0) { ... foo: bar(); } goto foo;
1722 //
1723 // TODO: If anyone cared, we could track __label__'s, since we know that you
1724 // can't jump to one from outside their declared region.
1725 if (isa<LabelStmt>(S))
1726 return true;
1727
1728 // If this is a case/default statement, and we haven't seen a switch, we have
1729 // to emit the code.
1730 if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
1731 return true;
1732
1733 // If this is a switch statement, we want to ignore cases below it.
1734 if (isa<SwitchStmt>(S))
1735 IgnoreCaseStmts = true;
1736
1737 // Scan subexpressions for verboten labels.
1738 for (const Stmt *SubStmt : S->children())
1739 if (ContainsLabel(SubStmt, IgnoreCaseStmts))
1740 return true;
1741
1742 return false;
1743}
1744
1745/// containsBreak - Return true if the statement contains a break out of it.
1746/// If the statement (recursively) contains a switch or loop with a break
1747/// inside of it, this is fine.
1749 // Null statement, not a label!
1750 if (!S) return false;
1751
1752 // If this is a switch or loop that defines its own break scope, then we can
1753 // include it and anything inside of it.
1754 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) ||
1755 isa<ForStmt>(S))
1756 return false;
1757
1758 if (isa<BreakStmt>(S))
1759 return true;
1760
1761 // Scan subexpressions for verboten breaks.
1762 for (const Stmt *SubStmt : S->children())
1763 if (containsBreak(SubStmt))
1764 return true;
1765
1766 return false;
1767}
1768
1770 if (!S) return false;
1771
1772 // Some statement kinds add a scope and thus never add a decl to the current
1773 // scope. Note, this list is longer than the list of statements that might
1774 // have an unscoped decl nested within them, but this way is conservatively
1775 // correct even if more statement kinds are added.
1776 if (isa<IfStmt>(S) || isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
1780 return false;
1781
1782 if (isa<DeclStmt>(S))
1783 return true;
1784
1785 for (const Stmt *SubStmt : S->children())
1786 if (mightAddDeclToScope(SubStmt))
1787 return true;
1788
1789 return false;
1790}
1791
1792/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
1793/// to a constant, or if it does but contains a label, return false. If it
1794/// constant folds return true and set the boolean result in Result.
1796 bool &ResultBool,
1797 bool AllowLabels) {
1798 // If MC/DC is enabled, disable folding so that we can instrument all
1799 // conditions to yield complete test vectors. We still keep track of
1800 // folded conditions during region mapping and visualization.
1801 if (!AllowLabels && CGM.getCodeGenOpts().hasProfileClangInstr() &&
1802 CGM.getCodeGenOpts().MCDCCoverage)
1803 return false;
1804
1805 llvm::APSInt ResultInt;
1806 if (!ConstantFoldsToSimpleInteger(Cond, ResultInt, AllowLabels))
1807 return false;
1808
1809 ResultBool = ResultInt.getBoolValue();
1810 return true;
1811}
1812
1813/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
1814/// to a constant, or if it does but contains a label, return false. If it
1815/// constant folds return true and set the folded value.
1817 llvm::APSInt &ResultInt,
1818 bool AllowLabels) {
1819 // FIXME: Rename and handle conversion of other evaluatable things
1820 // to bool.
1822 if (!Cond->EvaluateAsInt(Result, getContext()))
1823 return false; // Not foldable, not integer or not fully evaluatable.
1824
1825 llvm::APSInt Int = Result.Val.getInt();
1826 if (!AllowLabels && CodeGenFunction::ContainsLabel(Cond))
1827 return false; // Contains a label.
1828
1829 PGO->markStmtMaybeUsed(Cond);
1830 ResultInt = std::move(Int);
1831 return true;
1832}
1833
1834/// Strip parentheses and simplistic logical-NOT operators.
1836 while (true) {
1837 const Expr *SC = IgnoreExprNodes(
1840 if (C == SC)
1841 return SC;
1842 C = SC;
1843 }
1844}
1845
1846/// Determine whether the given condition is an instrumentable condition
1847/// (i.e. no "&&" or "||").
1849 const BinaryOperator *BOp = dyn_cast<BinaryOperator>(stripCond(C));
1850 return (!BOp || !BOp->isLogicalOp());
1851}
1852
1853/// EmitBranchToCounterBlock - Emit a conditional branch to a new block that
1854/// increments a profile counter based on the semantics of the given logical
1855/// operator opcode. This is used to instrument branch condition coverage for
1856/// logical operators.
1858 const Expr *Cond, BinaryOperator::Opcode LOp, llvm::BasicBlock *TrueBlock,
1859 llvm::BasicBlock *FalseBlock, uint64_t TrueCount /* = 0 */,
1860 Stmt::Likelihood LH /* =None */, const Expr *CntrIdx /* = nullptr */) {
1861 // If not instrumenting, just emit a branch.
1862 bool InstrumentRegions = CGM.getCodeGenOpts().hasProfileClangInstr();
1863 if (!InstrumentRegions || !isInstrumentedCondition(Cond))
1864 return EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount, LH);
1865
1866 const Stmt *CntrStmt = (CntrIdx ? CntrIdx : Cond);
1867
1868 llvm::BasicBlock *ThenBlock = nullptr;
1869 llvm::BasicBlock *ElseBlock = nullptr;
1870 llvm::BasicBlock *NextBlock = nullptr;
1871
1872 // Create the block we'll use to increment the appropriate counter.
1873 llvm::BasicBlock *CounterIncrBlock = createBasicBlock("lop.rhscnt");
1874
1875 llvm::BasicBlock *SkipIncrBlock =
1876 (hasSkipCounter(CntrStmt) ? createBasicBlock("lop.rhsskip") : nullptr);
1877 llvm::BasicBlock *SkipNextBlock = nullptr;
1878
1879 // Set block pointers according to Logical-AND (BO_LAnd) semantics. This
1880 // means we need to evaluate the condition and increment the counter on TRUE:
1881 //
1882 // if (Cond)
1883 // goto CounterIncrBlock;
1884 // else
1885 // goto FalseBlock;
1886 //
1887 // CounterIncrBlock:
1888 // Counter++;
1889 // goto TrueBlock;
1890
1891 if (LOp == BO_LAnd) {
1892 SkipNextBlock = FalseBlock;
1893 ThenBlock = CounterIncrBlock;
1894 ElseBlock = (SkipIncrBlock ? SkipIncrBlock : SkipNextBlock);
1895 NextBlock = TrueBlock;
1896 }
1897
1898 // Set block pointers according to Logical-OR (BO_LOr) semantics. This means
1899 // we need to evaluate the condition and increment the counter on FALSE:
1900 //
1901 // if (Cond)
1902 // goto TrueBlock;
1903 // else
1904 // goto CounterIncrBlock;
1905 //
1906 // CounterIncrBlock:
1907 // Counter++;
1908 // goto FalseBlock;
1909
1910 else if (LOp == BO_LOr) {
1911 SkipNextBlock = TrueBlock;
1912 ThenBlock = (SkipIncrBlock ? SkipIncrBlock : SkipNextBlock);
1913 ElseBlock = CounterIncrBlock;
1914 NextBlock = FalseBlock;
1915 } else {
1916 llvm_unreachable("Expected Opcode must be that of a Logical Operator");
1917 }
1918
1919 // Emit Branch based on condition.
1920 EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, TrueCount, LH);
1921
1922 if (SkipIncrBlock) {
1923 EmitBlock(SkipIncrBlock);
1925 EmitBranch(SkipNextBlock);
1926 }
1927
1928 // Emit the block containing the counter increment(s).
1929 EmitBlock(CounterIncrBlock);
1930
1931 // Increment corresponding counter; if index not provided, use Cond as index.
1933
1934 // Go to the next block.
1935 EmitBranch(NextBlock);
1936}
1937
1938/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
1939/// statement) to the specified blocks. Based on the condition, this might try
1940/// to simplify the codegen of the conditional based on the branch.
1941/// \param LH The value of the likelihood attribute on the True branch.
1942/// \param ConditionalOp Used by MC/DC code coverage to track the result of the
1943/// ConditionalOperator (ternary) through a recursive call for the operator's
1944/// LHS and RHS nodes.
1946 const Expr *Cond, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock,
1947 uint64_t TrueCount, Stmt::Likelihood LH, const Expr *ConditionalOp,
1948 const VarDecl *ConditionalDecl) {
1949 Cond = Cond->IgnoreParens();
1950
1951 if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
1952 bool HasSkip = hasSkipCounter(CondBOp);
1953
1954 // Handle X && Y in a condition.
1955 if (CondBOp->getOpcode() == BO_LAnd) {
1956 // If we have "1 && X", simplify the code. "0 && X" would have constant
1957 // folded if the case was simple enough.
1958 bool ConstantBool = false;
1959 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
1960 ConstantBool) {
1961 // br(1 && X) -> br(X).
1962 incrementProfileCounter(CondBOp);
1963 EmitBranchToCounterBlock(CondBOp->getRHS(), BO_LAnd, TrueBlock,
1964 FalseBlock, TrueCount, LH);
1965 return;
1966 }
1967
1968 // If we have "X && 1", simplify the code to use an uncond branch.
1969 // "X && 0" would have been constant folded to 0.
1970 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
1971 ConstantBool) {
1972 // br(X && 1) -> br(X).
1973 EmitBranchToCounterBlock(CondBOp->getLHS(), BO_LAnd, TrueBlock,
1974 FalseBlock, TrueCount, LH, CondBOp);
1975 return;
1976 }
1977
1978 // Emit the LHS as a conditional. If the LHS conditional is false, we
1979 // want to jump to the FalseBlock.
1980 llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
1981 llvm::BasicBlock *LHSFalse =
1982 (HasSkip ? createBasicBlock("land.lhsskip") : FalseBlock);
1983 // The counter tells us how often we evaluate RHS, and all of TrueCount
1984 // can be propagated to that branch.
1985 uint64_t RHSCount = getProfileCount(CondBOp->getRHS());
1986
1987 ConditionalEvaluation eval(*this);
1988 {
1989 ApplyDebugLocation DL(*this, Cond);
1990 // Propagate the likelihood attribute like __builtin_expect
1991 // __builtin_expect(X && Y, 1) -> X and Y are likely
1992 // __builtin_expect(X && Y, 0) -> only Y is unlikely
1993 EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, LHSFalse, RHSCount,
1994 LH == Stmt::LH_Unlikely ? Stmt::LH_None : LH);
1995 if (HasSkip) {
1996 EmitBlock(LHSFalse);
1998 EmitBranch(FalseBlock);
1999 }
2000 EmitBlock(LHSTrue);
2001 }
2002
2004 setCurrentProfileCount(getProfileCount(CondBOp->getRHS()));
2005
2006 // Any temporaries created here are conditional.
2007 eval.begin(*this);
2008 EmitBranchToCounterBlock(CondBOp->getRHS(), BO_LAnd, TrueBlock,
2009 FalseBlock, TrueCount, LH);
2010 eval.end(*this);
2011 return;
2012 }
2013
2014 if (CondBOp->getOpcode() == BO_LOr) {
2015 // If we have "0 || X", simplify the code. "1 || X" would have constant
2016 // folded if the case was simple enough.
2017 bool ConstantBool = false;
2018 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
2019 !ConstantBool) {
2020 // br(0 || X) -> br(X).
2021 incrementProfileCounter(CondBOp);
2022 EmitBranchToCounterBlock(CondBOp->getRHS(), BO_LOr, TrueBlock,
2023 FalseBlock, TrueCount, LH);
2024 return;
2025 }
2026
2027 // If we have "X || 0", simplify the code to use an uncond branch.
2028 // "X || 1" would have been constant folded to 1.
2029 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
2030 !ConstantBool) {
2031 // br(X || 0) -> br(X).
2032 EmitBranchToCounterBlock(CondBOp->getLHS(), BO_LOr, TrueBlock,
2033 FalseBlock, TrueCount, LH, CondBOp);
2034 return;
2035 }
2036 // Emit the LHS as a conditional. If the LHS conditional is true, we
2037 // want to jump to the TrueBlock.
2038 llvm::BasicBlock *LHSTrue =
2039 (HasSkip ? createBasicBlock("lor.lhsskip") : TrueBlock);
2040 llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
2041 // We have the count for entry to the RHS and for the whole expression
2042 // being true, so we can divy up True count between the short circuit and
2043 // the RHS.
2044 uint64_t LHSCount =
2045 getCurrentProfileCount() - getProfileCount(CondBOp->getRHS());
2046 uint64_t RHSCount = TrueCount - LHSCount;
2047
2048 ConditionalEvaluation eval(*this);
2049 {
2050 // Propagate the likelihood attribute like __builtin_expect
2051 // __builtin_expect(X || Y, 1) -> only Y is likely
2052 // __builtin_expect(X || Y, 0) -> both X and Y are unlikely
2053 ApplyDebugLocation DL(*this, Cond);
2054 EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, LHSFalse, LHSCount,
2055 LH == Stmt::LH_Likely ? Stmt::LH_None : LH);
2056 if (HasSkip) {
2057 EmitBlock(LHSTrue);
2059 EmitBranch(TrueBlock);
2060 }
2061 EmitBlock(LHSFalse);
2062 }
2063
2065 setCurrentProfileCount(getProfileCount(CondBOp->getRHS()));
2066
2067 // Any temporaries created here are conditional.
2068 eval.begin(*this);
2069 EmitBranchToCounterBlock(CondBOp->getRHS(), BO_LOr, TrueBlock, FalseBlock,
2070 RHSCount, LH);
2071
2072 eval.end(*this);
2073 return;
2074 }
2075 }
2076
2077 if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
2078 // br(!x, t, f) -> br(x, f, t)
2079 // Avoid doing this optimization when instrumenting a condition for MC/DC.
2080 // LNot is taken as part of the condition for simplicity, and changing its
2081 // sense negatively impacts test vector tracking.
2082 bool MCDCCondition = CGM.getCodeGenOpts().hasProfileClangInstr() &&
2083 CGM.getCodeGenOpts().MCDCCoverage &&
2085 if (CondUOp->getOpcode() == UO_LNot && !MCDCCondition) {
2086 // Negate the count.
2087 uint64_t FalseCount = getCurrentProfileCount() - TrueCount;
2088 // The values of the enum are chosen to make this negation possible.
2089 LH = static_cast<Stmt::Likelihood>(-LH);
2090 // Negate the condition and swap the destination blocks.
2091 return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock,
2092 FalseCount, LH);
2093 }
2094 }
2095
2096 if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
2097 // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
2098 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
2099 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
2100
2101 // The ConditionalOperator itself has no likelihood information for its
2102 // true and false branches. This matches the behavior of __builtin_expect.
2103 ConditionalEvaluation cond(*this);
2104 EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock,
2106
2107 // When computing PGO branch weights, we only know the overall count for
2108 // the true block. This code is essentially doing tail duplication of the
2109 // naive code-gen, introducing new edges for which counts are not
2110 // available. Divide the counts proportionally between the LHS and RHS of
2111 // the conditional operator.
2112 uint64_t LHSScaledTrueCount = 0;
2113 if (TrueCount) {
2114 double LHSRatio =
2115 getProfileCount(CondOp) / (double)getCurrentProfileCount();
2116 LHSScaledTrueCount = TrueCount * LHSRatio;
2117 }
2118
2119 cond.begin(*this);
2120 EmitBlock(LHSBlock);
2122 {
2123 ApplyDebugLocation DL(*this, Cond);
2124 EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock,
2125 LHSScaledTrueCount, LH, CondOp);
2126 }
2127 cond.end(*this);
2128
2129 cond.begin(*this);
2130 EmitBlock(RHSBlock);
2132 EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock,
2133 TrueCount - LHSScaledTrueCount, LH, CondOp);
2134 cond.end(*this);
2135
2136 return;
2137 }
2138
2139 if (const CXXThrowExpr *Throw = dyn_cast<CXXThrowExpr>(Cond)) {
2140 // Conditional operator handling can give us a throw expression as a
2141 // condition for a case like:
2142 // br(c ? throw x : y, t, f) -> br(c, br(throw x, t, f), br(y, t, f)
2143 // Fold this to:
2144 // br(c, throw x, br(y, t, f))
2145 EmitCXXThrowExpr(Throw, /*KeepInsertionPoint*/false);
2146 return;
2147 }
2148
2149 // Emit the code with the fully general case.
2150 llvm::Value *CondV;
2151 {
2152 ApplyDebugLocation DL(*this, Cond);
2153 CondV = EvaluateExprAsBool(Cond);
2154 }
2155
2156 MaybeEmitDeferredVarDeclInit(ConditionalDecl);
2157
2158 // If not at the top of the logical operator nest, update MCDC temp with the
2159 // boolean result of the evaluated condition.
2160 {
2161 const Expr *MCDCBaseExpr = Cond;
2162 // When a nested ConditionalOperator (ternary) is encountered in a boolean
2163 // expression, MC/DC tracks the result of the ternary, and this is tied to
2164 // the ConditionalOperator expression and not the ternary's LHS or RHS. If
2165 // this is the case, the ConditionalOperator expression is passed through
2166 // the ConditionalOp parameter and then used as the MCDC base expression.
2167 if (ConditionalOp)
2168 MCDCBaseExpr = ConditionalOp;
2169
2170 if (isMCDCBranchExpr(stripCond(MCDCBaseExpr)) &&
2172 maybeUpdateMCDCCondBitmap(MCDCBaseExpr, CondV);
2173 }
2174
2175 llvm::MDNode *Weights = nullptr;
2176 llvm::MDNode *Unpredictable = nullptr;
2177
2178 // If the branch has a condition wrapped by __builtin_unpredictable,
2179 // create metadata that specifies that the branch is unpredictable.
2180 // Don't bother if not optimizing because that metadata would not be used.
2181 auto *Call = dyn_cast<CallExpr>(Cond->IgnoreImpCasts());
2182 if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) {
2183 auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl());
2184 if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) {
2185 llvm::MDBuilder MDHelper(getLLVMContext());
2186 Unpredictable = MDHelper.createUnpredictable();
2187 }
2188 }
2189
2190 // If there is a Likelihood knowledge for the cond, lower it.
2191 // Note that if not optimizing this won't emit anything.
2192 llvm::Value *NewCondV = emitCondLikelihoodViaExpectIntrinsic(CondV, LH);
2193 if (CondV != NewCondV)
2194 CondV = NewCondV;
2195 else {
2196 // Otherwise, lower profile counts. Note that we do this even at -O0.
2197 uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount);
2198 Weights = createProfileWeights(TrueCount, CurrentCount - TrueCount);
2199 }
2200
2201 llvm::Instruction *BrInst = Builder.CreateCondBr(CondV, TrueBlock, FalseBlock,
2202 Weights, Unpredictable);
2203 addInstToNewSourceAtom(BrInst, CondV);
2204
2205 switch (HLSLControlFlowAttr) {
2206 case HLSLControlFlowHintAttr::Microsoft_branch:
2207 case HLSLControlFlowHintAttr::Microsoft_flatten: {
2208 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
2209
2210 llvm::ConstantInt *BranchHintConstant =
2212 HLSLControlFlowHintAttr::Spelling::Microsoft_branch
2213 ? llvm::ConstantInt::get(CGM.Int32Ty, 1)
2214 : llvm::ConstantInt::get(CGM.Int32Ty, 2);
2215
2217 {MDHelper.createString("hlsl.controlflow.hint"),
2218 MDHelper.createConstant(BranchHintConstant)});
2219 BrInst->setMetadata("hlsl.controlflow.hint",
2220 llvm::MDNode::get(CGM.getLLVMContext(), Vals));
2221 break;
2222 }
2223 // This is required to avoid warnings during compilation
2224 case HLSLControlFlowHintAttr::SpellingNotCalculated:
2225 break;
2226 }
2227}
2228
2229llvm::Value *CodeGenFunction::EmitScalarOrConstFoldImmArg(unsigned ICEArguments,
2230 unsigned Idx,
2231 const CallExpr *E) {
2232 llvm::Value *Arg = nullptr;
2233 if ((ICEArguments & (1 << Idx)) == 0) {
2234 Arg = EmitScalarExpr(E->getArg(Idx));
2235 } else {
2236 // If this is required to be a constant, constant fold it so that we
2237 // know that the generated intrinsic gets a ConstantInt.
2238 std::optional<llvm::APSInt> Result =
2240 assert(Result && "Expected argument to be a constant");
2241 Arg = llvm::ConstantInt::get(getLLVMContext(), *Result);
2242 }
2243 return Arg;
2244}
2245
2246/// ErrorUnsupported - Print out an error that codegen doesn't support the
2247/// specified stmt yet.
2248void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type) {
2249 CGM.ErrorUnsupported(S, Type);
2250}
2251
2252/// emitNonZeroVLAInit - Emit the "zero" initialization of a
2253/// variable-length array whose elements have a non-zero bit-pattern.
2254///
2255/// \param baseType the inner-most element type of the array
2256/// \param src - a char* pointing to the bit-pattern for a single
2257/// base element of the array
2258/// \param sizeInChars - the total size of the VLA, in chars
2260 Address dest, Address src,
2261 llvm::Value *sizeInChars) {
2262 CGBuilderTy &Builder = CGF.Builder;
2263
2264 CharUnits baseSize = CGF.getContext().getTypeSizeInChars(baseType);
2265 llvm::Value *baseSizeInChars
2266 = llvm::ConstantInt::get(CGF.IntPtrTy, baseSize.getQuantity());
2267
2268 Address begin = dest.withElementType(CGF.Int8Ty);
2269 llvm::Value *end = Builder.CreateInBoundsGEP(begin.getElementType(),
2270 begin.emitRawPointer(CGF),
2271 sizeInChars, "vla.end");
2272
2273 llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock();
2274 llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop");
2275 llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont");
2276
2277 // Make a loop over the VLA. C99 guarantees that the VLA element
2278 // count must be nonzero.
2279 CGF.EmitBlock(loopBB);
2280
2281 llvm::PHINode *cur = Builder.CreatePHI(begin.getType(), 2, "vla.cur");
2282 cur->addIncoming(begin.emitRawPointer(CGF), originBB);
2283
2284 CharUnits curAlign =
2285 dest.getAlignment().alignmentOfArrayElement(baseSize);
2286
2287 // memcpy the individual element bit-pattern.
2288 Builder.CreateMemCpy(Address(cur, CGF.Int8Ty, curAlign), src, baseSizeInChars,
2289 /*volatile*/ false);
2290
2291 // Go to the next element.
2292 llvm::Value *next =
2293 Builder.CreateInBoundsGEP(CGF.Int8Ty, cur, baseSizeInChars, "vla.next");
2294
2295 // Leave if that's the end of the VLA.
2296 llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone");
2297 Builder.CreateCondBr(done, contBB, loopBB);
2298 cur->addIncoming(next, loopBB);
2299
2300 CGF.EmitBlock(contBB);
2301}
2302
2304 const PFPField &Field) {
2305 return EmitAddressOfPFPField(
2306 RecordPtr,
2307 Builder.CreateConstInBoundsByteGEP(RecordPtr.withElementType(Int8Ty),
2308 Field.Offset),
2309 Field.Field);
2310}
2311
2313 Address PtrPtr,
2314 const FieldDecl *Field) {
2315 llvm::Value *Disc;
2316 if (CGM.getContext().arePFPFieldsTriviallyCopyable(Field->getParent())) {
2317 uint64_t FieldSignature =
2318 llvm::getPointerAuthStableSipHash(CGM.getPFPFieldName(Field));
2319 Disc = llvm::ConstantInt::get(CGM.Int64Ty, FieldSignature);
2320 } else
2321 Disc = Builder.CreatePtrToInt(RecordPtr.getBasePointer(), CGM.Int64Ty);
2322
2323 llvm::GlobalValue *DS = CGM.getPFPDeactivationSymbol(Field);
2324 llvm::OperandBundleDef DSBundle("deactivation-symbol", DS);
2325 llvm::Value *Args[] = {PtrPtr.getBasePointer(), Disc, Builder.getTrue()};
2326 return Address(
2327 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::protected_field_ptr,
2328 PtrPtr.getType()),
2329 Args, DSBundle),
2330 VoidPtrTy, PtrPtr.getAlignment());
2331}
2332
2333void
2335 // Ignore empty classes in C++.
2336 if (getLangOpts().CPlusPlus)
2337 if (const auto *RD = Ty->getAsCXXRecordDecl(); RD && RD->isEmpty())
2338 return;
2339
2340 if (DestPtr.getElementType() != Int8Ty)
2341 DestPtr = DestPtr.withElementType(Int8Ty);
2342
2343 // Get size and alignment info for this aggregate.
2345
2346 llvm::Value *SizeVal;
2347 const VariableArrayType *vla;
2348
2349 // Don't bother emitting a zero-byte memset.
2350 if (size.isZero()) {
2351 // But note that getTypeInfo returns 0 for a VLA.
2352 if (const VariableArrayType *vlaType =
2353 dyn_cast_or_null<VariableArrayType>(
2354 getContext().getAsArrayType(Ty))) {
2355 auto VlaSize = getVLASize(vlaType);
2356 SizeVal = VlaSize.NumElts;
2357 CharUnits eltSize = getContext().getTypeSizeInChars(VlaSize.Type);
2358 if (!eltSize.isOne())
2359 SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize));
2360 vla = vlaType;
2361 } else {
2362 return;
2363 }
2364 } else {
2365 SizeVal = CGM.getSize(size);
2366 vla = nullptr;
2367 }
2368
2369 // If the type contains a pointer to data member we can't memset it to zero.
2370 // Instead, create a null constant and copy it to the destination.
2371 // TODO: there are other patterns besides zero that we can usefully memset,
2372 // like -1, which happens to be the pattern used by member-pointers.
2373 if (!CGM.getTypes().isZeroInitializable(Ty)) {
2374 // For a VLA, emit a single element, then splat that over the VLA.
2375 if (vla) Ty = getContext().getBaseElementType(vla);
2376
2377 llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
2378
2379 llvm::GlobalVariable *NullVariable =
2380 new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
2381 /*isConstant=*/true,
2382 llvm::GlobalVariable::PrivateLinkage,
2383 NullConstant, Twine());
2384 CharUnits NullAlign = DestPtr.getAlignment();
2385 NullVariable->setAlignment(NullAlign.getAsAlign());
2386 Address SrcPtr(NullVariable, Builder.getInt8Ty(), NullAlign);
2387
2388 if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal);
2389
2390 // Get and call the appropriate llvm.memcpy overload.
2391 Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, false);
2392 } else {
2393 // Otherwise, just memset the whole thing to zero. This is legal
2394 // because in LLVM, all default initializers (other than the ones we just
2395 // handled above, and the case handled below) are guaranteed to have a bit
2396 // pattern of all zeros.
2397 Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal, false);
2398 }
2399
2400 // With the pointer field protection feature, null pointers do not have a bit
2401 // pattern of zero in memory, so we must initialize them separately.
2402 for (auto &Field : getContext().findPFPFields(Ty)) {
2403 auto addr = EmitAddressOfPFPField(DestPtr, Field);
2404 Builder.CreateStore(llvm::ConstantPointerNull::get(VoidPtrTy), addr);
2405 }
2406}
2407
2408llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) {
2409 // Make sure that there is a block for the indirect goto.
2410 if (!IndirectBranch)
2412
2413 llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock();
2414
2415 // Make sure the indirect branch includes all of the address-taken blocks.
2416 IndirectBranch->addDestination(BB);
2417 return llvm::BlockAddress::get(CurFn->getType(), BB);
2418}
2419
2421 // If we already made the indirect branch for indirect goto, return its block.
2422 if (IndirectBranch) return IndirectBranch->getParent();
2423
2424 CGBuilderTy TmpBuilder(CGM, createBasicBlock("indirectgoto"));
2425
2426 // Create the PHI node that indirect gotos will add entries to.
2427 llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0,
2428 "indirect.goto.dest");
2429
2430 // Create the indirect branch instruction.
2431 IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
2432 return IndirectBranch->getParent();
2433}
2434
2435/// Computes the length of an array in elements, as well as the base
2436/// element type and a properly-typed first element pointer.
2437llvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType,
2438 QualType &baseType,
2439 Address &addr) {
2440 const ArrayType *arrayType = origArrayType;
2441
2442 // If it's a VLA, we have to load the stored size. Note that
2443 // this is the size of the VLA in bytes, not its size in elements.
2444 llvm::Value *numVLAElements = nullptr;
2447
2448 // Walk into all VLAs. This doesn't require changes to addr,
2449 // which has type T* where T is the first non-VLA element type.
2450 do {
2451 QualType elementType = arrayType->getElementType();
2452 arrayType = getContext().getAsArrayType(elementType);
2453
2454 // If we only have VLA components, 'addr' requires no adjustment.
2455 if (!arrayType) {
2456 baseType = elementType;
2457 return numVLAElements;
2458 }
2460
2461 // We get out here only if we find a constant array type
2462 // inside the VLA.
2463 }
2464
2465 // We have some number of constant-length arrays, so addr should
2466 // have LLVM type [M x [N x [...]]]*. Build a GEP that walks
2467 // down to the first element of addr.
2469
2470 // GEP down to the array type.
2471 llvm::ConstantInt *zero = Builder.getInt32(0);
2472 gepIndices.push_back(zero);
2473
2474 uint64_t countFromCLAs = 1;
2475 QualType eltType;
2476
2477 llvm::ArrayType *llvmArrayType =
2478 dyn_cast<llvm::ArrayType>(addr.getElementType());
2479 while (llvmArrayType) {
2481 assert(cast<ConstantArrayType>(arrayType)->getZExtSize() ==
2482 llvmArrayType->getNumElements());
2483
2484 gepIndices.push_back(zero);
2485 countFromCLAs *= llvmArrayType->getNumElements();
2486 eltType = arrayType->getElementType();
2487
2488 llvmArrayType =
2489 dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType());
2490 arrayType = getContext().getAsArrayType(arrayType->getElementType());
2491 assert((!llvmArrayType || arrayType) &&
2492 "LLVM and Clang types are out-of-synch");
2493 }
2494
2495 if (arrayType) {
2496 // From this point onwards, the Clang array type has been emitted
2497 // as some other type (probably a packed struct). Compute the array
2498 // size, and just emit the 'begin' expression as a bitcast.
2499 while (arrayType) {
2500 countFromCLAs *= cast<ConstantArrayType>(arrayType)->getZExtSize();
2501 eltType = arrayType->getElementType();
2502 arrayType = getContext().getAsArrayType(eltType);
2503 }
2504
2505 llvm::Type *baseType = ConvertType(eltType);
2506 addr = addr.withElementType(baseType);
2507 } else {
2508 // Create the actual GEP.
2509 addr = Address(Builder.CreateInBoundsGEP(addr.getElementType(),
2510 addr.emitRawPointer(*this),
2511 gepIndices, "array.begin"),
2512 ConvertTypeForMem(eltType), addr.getAlignment());
2513 }
2514
2515 baseType = eltType;
2516
2517 llvm::Value *numElements
2518 = llvm::ConstantInt::get(SizeTy, countFromCLAs);
2519
2520 // If we had any VLA dimensions, factor them in.
2521 if (numVLAElements)
2522 numElements = Builder.CreateNUWMul(numVLAElements, numElements);
2523
2524 return numElements;
2525}
2526
2529 assert(vla && "type was not a variable array type!");
2530 return getVLASize(vla);
2531}
2532
2535 // The number of elements so far; always size_t.
2536 llvm::Value *numElements = nullptr;
2537
2538 QualType elementType;
2539 do {
2540 elementType = type->getElementType();
2541 llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()];
2542 assert(vlaSize && "no size for VLA!");
2543 assert(vlaSize->getType() == SizeTy);
2544
2545 if (!numElements) {
2546 numElements = vlaSize;
2547 } else {
2548 // It's undefined behavior if this wraps around, so mark it that way.
2549 // FIXME: Teach -fsanitize=undefined to trap this.
2550 numElements = Builder.CreateNUWMul(numElements, vlaSize);
2551 }
2552 } while ((type = getContext().getAsVariableArrayType(elementType)));
2553
2554 return { numElements, elementType };
2555}
2556
2560 assert(vla && "type was not a variable array type!");
2561 return getVLAElements1D(vla);
2562}
2563
2566 llvm::Value *VlaSize = VLASizeMap[Vla->getSizeExpr()];
2567 assert(VlaSize && "no size for VLA!");
2568 assert(VlaSize->getType() == SizeTy);
2569 return { VlaSize, Vla->getElementType() };
2570}
2571
2573 assert(type->isVariablyModifiedType() &&
2574 "Must pass variably modified type to EmitVLASizes!");
2575
2577
2578 // We're going to walk down into the type and look for VLA
2579 // expressions.
2580 do {
2581 assert(type->isVariablyModifiedType());
2582
2583 const Type *ty = type.getTypePtr();
2584 switch (ty->getTypeClass()) {
2585
2586#define TYPE(Class, Base)
2587#define ABSTRACT_TYPE(Class, Base)
2588#define NON_CANONICAL_TYPE(Class, Base)
2589#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2590#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)
2591#include "clang/AST/TypeNodes.inc"
2592 llvm_unreachable("unexpected dependent type!");
2593
2594 // These types are never variably-modified.
2595 case Type::Builtin:
2596 case Type::Complex:
2597 case Type::Vector:
2598 case Type::ExtVector:
2599 case Type::ConstantMatrix:
2600 case Type::Record:
2601 case Type::Enum:
2602 case Type::Using:
2603 case Type::TemplateSpecialization:
2604 case Type::ObjCTypeParam:
2605 case Type::ObjCObject:
2606 case Type::ObjCInterface:
2607 case Type::ObjCObjectPointer:
2608 case Type::BitInt:
2609 case Type::HLSLInlineSpirv:
2610 case Type::PredefinedSugar:
2611 llvm_unreachable("type class is never variably-modified!");
2612
2613 case Type::Adjusted:
2614 type = cast<AdjustedType>(ty)->getAdjustedType();
2615 break;
2616
2617 case Type::Decayed:
2618 type = cast<DecayedType>(ty)->getPointeeType();
2619 break;
2620
2621 case Type::Pointer:
2622 type = cast<PointerType>(ty)->getPointeeType();
2623 break;
2624
2625 case Type::BlockPointer:
2626 type = cast<BlockPointerType>(ty)->getPointeeType();
2627 break;
2628
2629 case Type::LValueReference:
2630 case Type::RValueReference:
2631 type = cast<ReferenceType>(ty)->getPointeeType();
2632 break;
2633
2634 case Type::MemberPointer:
2635 type = cast<MemberPointerType>(ty)->getPointeeType();
2636 break;
2637
2638 case Type::ArrayParameter:
2639 case Type::ConstantArray:
2640 case Type::IncompleteArray:
2641 // Losing element qualification here is fine.
2642 type = cast<ArrayType>(ty)->getElementType();
2643 break;
2644
2645 case Type::VariableArray: {
2646 // Losing element qualification here is fine.
2648
2649 // Unknown size indication requires no size computation.
2650 // Otherwise, evaluate and record it.
2651 if (const Expr *sizeExpr = vat->getSizeExpr()) {
2652 // It's possible that we might have emitted this already,
2653 // e.g. with a typedef and a pointer to it.
2654 llvm::Value *&entry = VLASizeMap[sizeExpr];
2655 if (!entry) {
2656 llvm::Value *size = EmitScalarExpr(sizeExpr);
2657
2658 // C11 6.7.6.2p5:
2659 // If the size is an expression that is not an integer constant
2660 // expression [...] each time it is evaluated it shall have a value
2661 // greater than zero.
2662 if (SanOpts.has(SanitizerKind::VLABound)) {
2663 auto CheckOrdinal = SanitizerKind::SO_VLABound;
2664 auto CheckHandler = SanitizerHandler::VLABoundNotPositive;
2665 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
2666 llvm::Value *Zero = llvm::Constant::getNullValue(size->getType());
2667 clang::QualType SEType = sizeExpr->getType();
2668 llvm::Value *CheckCondition =
2669 SEType->isSignedIntegerType()
2670 ? Builder.CreateICmpSGT(size, Zero)
2671 : Builder.CreateICmpUGT(size, Zero);
2672 llvm::Constant *StaticArgs[] = {
2673 EmitCheckSourceLocation(sizeExpr->getBeginLoc()),
2674 EmitCheckTypeDescriptor(SEType)};
2675 EmitCheck(std::make_pair(CheckCondition, CheckOrdinal),
2676 CheckHandler, StaticArgs, size);
2677 }
2678
2679 // Always zexting here would be wrong if it weren't
2680 // undefined behavior to have a negative bound.
2681 // FIXME: What about when size's type is larger than size_t?
2682 entry = Builder.CreateIntCast(size, SizeTy, /*signed*/ false);
2683 }
2684 }
2685 type = vat->getElementType();
2686 break;
2687 }
2688
2689 case Type::FunctionProto:
2690 case Type::FunctionNoProto:
2691 type = cast<FunctionType>(ty)->getReturnType();
2692 break;
2693
2694 case Type::Paren:
2695 case Type::TypeOf:
2696 case Type::UnaryTransform:
2697 case Type::Attributed:
2698 case Type::BTFTagAttributed:
2699 case Type::OverflowBehavior:
2700 case Type::HLSLAttributedResource:
2701 case Type::SubstTemplateTypeParm:
2702 case Type::MacroQualified:
2703 case Type::CountAttributed:
2704 case Type::LateParsedAttr:
2705 // Keep walking after single level desugaring.
2706 type = type.getSingleStepDesugaredType(getContext());
2707 break;
2708
2709 case Type::Typedef:
2710 case Type::Decltype:
2711 case Type::Auto:
2712 case Type::DeducedTemplateSpecialization:
2713 case Type::PackIndexing:
2714 // Stop walking: nothing to do.
2715 return;
2716
2717 case Type::TypeOfExpr:
2718 // Stop walking: emit typeof expression.
2719 EmitIgnoredExpr(cast<TypeOfExprType>(ty)->getUnderlyingExpr());
2720 return;
2721
2722 case Type::Atomic:
2723 type = cast<AtomicType>(ty)->getValueType();
2724 break;
2725
2726 case Type::Pipe:
2727 type = cast<PipeType>(ty)->getElementType();
2728 break;
2729 }
2730 } while (type->isVariablyModifiedType());
2731}
2732
2734 if (getContext().getBuiltinVaListType()->isArrayType())
2735 return EmitPointerWithAlignment(E);
2736 return EmitLValue(E).getAddress();
2737}
2738
2742
2746
2748 const APValue &Init) {
2749 assert(Init.hasValue() && "Invalid DeclRefExpr initializer!");
2750 if (CGDebugInfo *Dbg = getDebugInfo())
2751 if (CGM.getCodeGenOpts().hasReducedDebugInfo())
2752 Dbg->EmitGlobalVariable(E->getDecl(), Init);
2753}
2754
2757 // At the moment, the only aggressive peephole we do in IR gen
2758 // is trunc(zext) folding, but if we add more, we can easily
2759 // extend this protection.
2760
2761 if (!rvalue.isScalar()) return PeepholeProtection();
2762 llvm::Value *value = rvalue.getScalarVal();
2763 if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection();
2764
2765 // Just make an extra bitcast.
2766 assert(HaveInsertPoint());
2767 llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "",
2768 Builder.GetInsertBlock());
2769
2770 PeepholeProtection protection;
2771 protection.Inst = inst;
2772 return protection;
2773}
2774
2776 if (!protection.Inst) return;
2777
2778 // In theory, we could try to duplicate the peepholes now, but whatever.
2779 protection.Inst->eraseFromParent();
2780}
2781
2783 QualType Ty, SourceLocation Loc,
2784 SourceLocation AssumptionLoc,
2785 llvm::Value *Alignment,
2786 llvm::Value *OffsetValue) {
2787 if (Alignment->getType() != IntPtrTy)
2788 Alignment =
2789 Builder.CreateIntCast(Alignment, IntPtrTy, false, "casted.align");
2790 if (OffsetValue && OffsetValue->getType() != IntPtrTy)
2791 OffsetValue =
2792 Builder.CreateIntCast(OffsetValue, IntPtrTy, true, "casted.offset");
2793 if (SanOpts.has(SanitizerKind::Alignment))
2794 emitAlignmentAssumptionCheck(PtrValue, Ty, Loc, AssumptionLoc, Alignment,
2795 OffsetValue);
2796 Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment,
2797 OffsetValue);
2798}
2799
2801 const Expr *E,
2802 SourceLocation AssumptionLoc,
2803 llvm::Value *Alignment,
2804 llvm::Value *OffsetValue) {
2805 QualType Ty = E->getType();
2806 SourceLocation Loc = E->getExprLoc();
2807
2808 emitAlignmentAssumption(PtrValue, Ty, Loc, AssumptionLoc, Alignment,
2809 OffsetValue);
2810}
2811
2812llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Function *AnnotationFn,
2813 llvm::Value *AnnotatedVal,
2814 StringRef AnnotationStr,
2815 SourceLocation Location,
2816 const AnnotateAttr *Attr) {
2818 AnnotatedVal,
2819 CGM.EmitAnnotationString(AnnotationStr),
2820 CGM.EmitAnnotationUnit(Location),
2821 CGM.EmitAnnotationLineNo(Location),
2822 };
2823 if (Attr)
2824 Args.push_back(CGM.EmitAnnotationArgs(Attr));
2825 return Builder.CreateCall(AnnotationFn, Args);
2826}
2827
2828void CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) {
2829 assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
2830 for (const auto *I : D->specific_attrs<AnnotateAttr>())
2831 EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation,
2832 {V->getType(), CGM.ConstGlobalsPtrTy}),
2833 V, I->getAnnotation(), D->getLocation(), I);
2834}
2835
2837 Address Addr) {
2838 assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
2839 llvm::Value *V = Addr.emitRawPointer(*this);
2840 llvm::Type *VTy = V->getType();
2841 auto *PTy = dyn_cast<llvm::PointerType>(VTy);
2842 unsigned AS = PTy ? PTy->getAddressSpace() : 0;
2843 llvm::PointerType *IntrinTy =
2844 llvm::PointerType::get(CGM.getLLVMContext(), AS);
2845 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
2846 {IntrinTy, CGM.ConstGlobalsPtrTy});
2847
2848 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2849 // FIXME Always emit the cast inst so we can differentiate between
2850 // annotation on the first field of a struct and annotation on the struct
2851 // itself.
2852 if (VTy != IntrinTy)
2853 V = Builder.CreateBitCast(V, IntrinTy);
2854 V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation(), I);
2855 V = Builder.CreateBitCast(V, VTy);
2856 }
2857
2858 return Address(V, Addr.getElementType(), Addr.getAlignment());
2859}
2860
2862
2864 : CGF(CGF) {
2865 assert(!CGF->IsSanitizerScope);
2866 CGF->IsSanitizerScope = true;
2867}
2868
2870 CGF->IsSanitizerScope = false;
2871}
2872
2873void CodeGenFunction::InsertHelper(llvm::Instruction *I,
2874 const llvm::Twine &Name,
2875 llvm::BasicBlock::iterator InsertPt) const {
2876 LoopStack.InsertHelper(I);
2877 if (IsSanitizerScope)
2878 I->setNoSanitizeMetadata();
2879}
2880
2882 llvm::Instruction *I, const llvm::Twine &Name,
2883 llvm::BasicBlock::iterator InsertPt) const {
2884 llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
2885 if (CGF)
2886 CGF->InsertHelper(I, Name, InsertPt);
2887}
2888
2889// Emits an error if we don't have a valid set of target features for the
2890// called function.
2892 const FunctionDecl *TargetDecl) {
2893 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl);
2894 CodeGenUtils::checkTargetFeatures(CGM.getContext(), CGM.getDiags(),
2895 getLangOpts(), E, FD, TargetDecl);
2896}
2897
2898// Emits an error if we don't have a valid set of target features for the
2899// called function.
2901 const FunctionDecl *TargetDecl) {
2902 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl);
2903 CodeGenUtils::checkTargetFeatures(CGM.getContext(), CGM.getDiags(),
2904 getLangOpts(), Loc, FD, TargetDecl);
2905}
2906
2907void CodeGenFunction::EmitSanitizerStatReport(llvm::SanitizerStatKind SSK) {
2908 if (!CGM.getCodeGenOpts().SanitizeStats)
2909 return;
2910
2911 llvm::IRBuilder<> IRB(Builder.GetInsertPoint());
2912 IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
2913 CGM.getSanStats().create(IRB, SSK);
2914}
2915
2917 const CGCallee &Callee, SmallVectorImpl<llvm::OperandBundleDef> &Bundles) {
2918 const CGCalleeInfo &CI = Callee.getAbstractInfo();
2920 if (!FP)
2921 return;
2922
2923 StringRef Salt;
2924 if (const auto &Info = FP->getExtraAttributeInfo())
2925 Salt = Info.CFISalt;
2926
2927 Bundles.emplace_back("kcfi", CGM.CreateKCFITypeId(FP->desugar(), Salt));
2928}
2929
2930llvm::Value *
2931CodeGenFunction::FormAArch64ResolverCondition(const FMVResolverOption &RO) {
2932 return RO.Features.empty() ? nullptr : EmitAArch64CpuSupports(RO.Features);
2933}
2934
2935llvm::Value *
2936CodeGenFunction::FormX86ResolverCondition(const FMVResolverOption &RO) {
2937 llvm::Value *Condition = nullptr;
2938
2939 if (RO.Architecture) {
2940 StringRef Arch = *RO.Architecture;
2941 // If arch= specifies an x86-64 micro-architecture level, test the feature
2942 // with __builtin_cpu_supports, otherwise use __builtin_cpu_is.
2943 if (Arch.starts_with("x86-64"))
2944 Condition = EmitX86CpuSupports({Arch});
2945 else
2946 Condition = EmitX86CpuIs(Arch);
2947 }
2948
2949 if (!RO.Features.empty()) {
2950 llvm::Value *FeatureCond = EmitX86CpuSupports(RO.Features);
2951 Condition =
2952 Condition ? Builder.CreateAnd(Condition, FeatureCond) : FeatureCond;
2953 }
2954 return Condition;
2955}
2956
2958 llvm::Function *Resolver,
2959 CGBuilderTy &Builder,
2960 llvm::Function *FuncToReturn,
2961 bool SupportsIFunc) {
2962 if (SupportsIFunc) {
2963 Builder.CreateRet(FuncToReturn);
2964 return;
2965 }
2966
2968 llvm::make_pointer_range(Resolver->args()));
2969
2970 llvm::CallInst *Result = Builder.CreateCall(FuncToReturn, Args);
2971 Result->setTailCallKind(llvm::CallInst::TCK_MustTail);
2972
2973 if (Resolver->getReturnType()->isVoidTy())
2974 Builder.CreateRetVoid();
2975 else
2976 Builder.CreateRet(Result);
2977}
2978
2980 llvm::Function *Resolver, ArrayRef<FMVResolverOption> Options) {
2981 llvm::SaveAndRestore<llvm::Function *> savedCurFn(CurFn, Resolver);
2982 llvm::Triple::ArchType ArchType =
2983 getContext().getTargetInfo().getTriple().getArch();
2984
2985 switch (ArchType) {
2986 case llvm::Triple::x86:
2987 case llvm::Triple::x86_64:
2988 EmitX86MultiVersionResolver(Resolver, Options);
2989 return;
2990 case llvm::Triple::aarch64:
2991 EmitAArch64MultiVersionResolver(Resolver, Options);
2992 return;
2993 case llvm::Triple::riscv32:
2994 case llvm::Triple::riscv64:
2995 case llvm::Triple::riscv32be:
2996 case llvm::Triple::riscv64be:
2997 EmitRISCVMultiVersionResolver(Resolver, Options);
2998 return;
2999 case llvm::Triple::ppc:
3000 case llvm::Triple::ppc64:
3001 if (getContext().getTargetInfo().getTriple().isOSAIX()) {
3002 EmitPPCAIXMultiVersionResolver(Resolver, Options);
3003 return;
3004 }
3005 [[fallthrough]];
3006 default:
3007 assert(false &&
3008 "Only implemented for x86, AArch64, RISC-V, and PowerPC AIX");
3009 }
3010}
3011
3012/**
3013 * define internal ptr @foo.resolver() {
3014 * entry:
3015 * %is_version_1 = __builtin_cpu_supports(version_1)
3016 * br i1 %1, label %if.version_1, label %if.else_2
3017 *
3018 * if.version_1:
3019 * ret ptr @foo.version_1
3020 *
3021 * if.else_2:
3022 * %is_version_2 = __builtin_cpu_supports(version_2)
3023 * ...
3024 * if.else: ; preds = %entry
3025 * ret ptr @foo.default
3026 * }
3027 */
3029 llvm::Function *Resolver, ArrayRef<FMVResolverOption> Options) {
3030
3031 // entry:
3032 llvm::BasicBlock *CurBlock = createBasicBlock("entry", Resolver);
3033
3034 for (const FMVResolverOption &RO : Options) {
3035 Builder.SetInsertPoint(CurBlock);
3036 // The 'default' or 'generic' case.
3037 if (!RO.Architecture && RO.Features.empty()) {
3038 // if.else:
3039 // ret ptr @foo.default
3040 assert(&RO == Options.end() - 1 &&
3041 "Default or Generic case must be last");
3042 Builder.CreateRet(RO.Function);
3043 return;
3044 }
3045 // if.else_n:
3046 // %is_version_n = __builtin_cpu_supports(version_n)
3047 // br i1 %is_version_n, label %if.version_n, label %if.else_n+1
3048 //
3049 // if.version_n:
3050 // ret ptr @foo_version_n
3051 assert(RO.Features.size() == 1 &&
3052 "for now one feature requirement per version");
3053
3054 StringRef FeatureStr = RO.Features[0];
3055 StringRef BuiltinCpuSupportsArg;
3056 bool IsNegated = false;
3057
3058 if (FeatureStr.starts_with("cpu=")) {
3059 // CPU specification - map to ISA level
3060 StringRef CPU = FeatureStr.split("=").second.trim();
3061 BuiltinCpuSupportsArg = llvm::StringSwitch<StringRef>(CPU)
3062#define PPC_AIX_CLONES_CPU(CPU_NAME, AIX_BUILTIN_CPU_SUPPORTS_NAME, _) \
3063 .Case(CPU_NAME, AIX_BUILTIN_CPU_SUPPORTS_NAME)
3064#include "llvm/TargetParser/PPCTargetParser.def"
3065 .Default("error");
3066 } else {
3067 // Feature strings arrive here already normalized:
3068 // - Positive features: just the name (e.g., "altivec")
3069 // - Negated features: "no-" prefix (e.g., "no-altivec")
3070 if (FeatureStr.starts_with("no-")) {
3071 IsNegated = true;
3072 FeatureStr = FeatureStr.drop_front(3);
3073 }
3074
3075 // Map feature names to __builtin_cpu_supports() strings
3076 BuiltinCpuSupportsArg =
3077 llvm::StringSwitch<StringRef>(FeatureStr)
3078#define PPC_AIX_CLONES_FEATURE(FEATURE_NAME, AIX_BUILTIN_CPU_SUPPORTS_NAME, _, \
3079 __) \
3080 .Case(FEATURE_NAME, AIX_BUILTIN_CPU_SUPPORTS_NAME)
3081#include "llvm/TargetParser/PPCTargetParser.def"
3082 // Features without runtime checks return empty string
3083 .Default("");
3084
3085 // All features in target_clones must have runtime detection
3086 assert(!BuiltinCpuSupportsArg.empty() &&
3087 "Feature without runtime detection should have been rejected in "
3088 "Sema");
3089 }
3090
3091 assert(getContext().getTargetInfo().validateCpuSupports(
3092 BuiltinCpuSupportsArg));
3093
3094 llvm::Value *Condition =
3095 EmitPPCBuiltinCpu(Builtin::BI__builtin_cpu_supports,
3096 Builder.getInt1Ty(), BuiltinCpuSupportsArg);
3097
3098 // Negate the condition if this is a negated feature
3099 if (IsNegated) {
3100 Condition = Builder.CreateNot(Condition, "neg");
3101 }
3102
3103 llvm::BasicBlock *ThenBlock = createBasicBlock("if.version", Resolver);
3104 CurBlock = createBasicBlock("if.else", Resolver);
3105 Builder.CreateCondBr(Condition, ThenBlock, CurBlock);
3106
3107 Builder.SetInsertPoint(ThenBlock);
3108 Builder.CreateRet(RO.Function);
3109 }
3110
3111 llvm_unreachable("Default case missing");
3112}
3113
3115 llvm::Function *Resolver, ArrayRef<FMVResolverOption> Options) {
3116
3117 if (getContext().getTargetInfo().getTriple().getOS() !=
3118 llvm::Triple::OSType::Linux) {
3119 CGM.getDiags().Report(diag::err_os_unsupport_riscv_fmv);
3120 return;
3121 }
3122
3123 llvm::BasicBlock *CurBlock = createBasicBlock("resolver_entry", Resolver);
3124 Builder.SetInsertPoint(CurBlock);
3126
3127 bool SupportsIFunc = getContext().getTargetInfo().supportsIFunc();
3128 bool HasDefault = false;
3129 unsigned DefaultIndex = 0;
3130
3131 // Check the each candidate function.
3132 for (unsigned Index = 0; Index < Options.size(); Index++) {
3133
3134 if (Options[Index].Features.empty()) {
3135 HasDefault = true;
3136 DefaultIndex = Index;
3137 continue;
3138 }
3139
3140 Builder.SetInsertPoint(CurBlock);
3141
3142 // FeaturesCondition: The bitmask of the required extension has been
3143 // enabled by the runtime object.
3144 // (__riscv_feature_bits.features[i] & REQUIRED_BITMASK) ==
3145 // REQUIRED_BITMASK
3146 //
3147 // When condition is met, return this version of the function.
3148 // Otherwise, try the next version.
3149 //
3150 // if (FeaturesConditionVersion1)
3151 // return Version1;
3152 // else if (FeaturesConditionVersion2)
3153 // return Version2;
3154 // else if (FeaturesConditionVersion3)
3155 // return Version3;
3156 // ...
3157 // else
3158 // return DefaultVersion;
3159
3160 // TODO: Add a condition to check the length before accessing elements.
3161 // Without checking the length first, we may access an incorrect memory
3162 // address when using different versions.
3163 llvm::SmallVector<StringRef, 8> CurrTargetAttrFeats;
3164 llvm::SmallVector<std::string, 8> TargetAttrFeats;
3165
3166 for (StringRef Feat : Options[Index].Features) {
3167 std::vector<std::string> FeatStr =
3169
3170 assert(FeatStr.size() == 1 && "Feature string not delimited");
3171
3172 std::string &CurrFeat = FeatStr.front();
3173 if (CurrFeat[0] == '+')
3174 TargetAttrFeats.push_back(CurrFeat.substr(1));
3175 }
3176
3177 if (TargetAttrFeats.empty())
3178 continue;
3179
3180 for (std::string &Feat : TargetAttrFeats)
3181 CurrTargetAttrFeats.push_back(Feat);
3182
3183 Builder.SetInsertPoint(CurBlock);
3184 llvm::Value *FeatsCondition = EmitRISCVCpuSupports(CurrTargetAttrFeats);
3185
3186 llvm::BasicBlock *RetBlock = createBasicBlock("resolver_return", Resolver);
3187 CGBuilderTy RetBuilder(CGM, RetBlock);
3188 CreateMultiVersionResolverReturn(CGM, Resolver, RetBuilder,
3189 Options[Index].Function, SupportsIFunc);
3190 llvm::BasicBlock *ElseBlock = createBasicBlock("resolver_else", Resolver);
3191
3192 Builder.SetInsertPoint(CurBlock);
3193 Builder.CreateCondBr(FeatsCondition, RetBlock, ElseBlock);
3194
3195 CurBlock = ElseBlock;
3196 }
3197
3198 // Finally, emit the default one.
3199 if (HasDefault) {
3200 Builder.SetInsertPoint(CurBlock);
3202 CGM, Resolver, Builder, Options[DefaultIndex].Function, SupportsIFunc);
3203 return;
3204 }
3205
3206 // If no generic/default, emit an unreachable.
3207 Builder.SetInsertPoint(CurBlock);
3209}
3210
3212 llvm::Function *Resolver, ArrayRef<FMVResolverOption> Options) {
3213 assert(!Options.empty() && "No multiversion resolver options found");
3214 assert(Options.back().Features.size() == 0 && "Default case must be last");
3215 bool SupportsIFunc = getContext().getTargetInfo().supportsIFunc();
3216 assert(SupportsIFunc &&
3217 "Multiversion resolver requires target IFUNC support");
3218 bool AArch64CpuInitialized = false;
3219 llvm::BasicBlock *CurBlock = createBasicBlock("resolver_entry", Resolver);
3220
3221 for (const FMVResolverOption &RO : Options) {
3222 // Skip unreachable versions.
3223 if (RO.Function == nullptr)
3224 continue;
3225
3226 Builder.SetInsertPoint(CurBlock);
3227 llvm::Value *Condition = FormAArch64ResolverCondition(RO);
3228
3229 // The 'default' or 'all features enabled' case.
3230 if (!Condition) {
3231 CreateMultiVersionResolverReturn(CGM, Resolver, Builder, RO.Function,
3232 SupportsIFunc);
3233 return;
3234 }
3235
3236 if (!AArch64CpuInitialized) {
3237 Builder.SetInsertPoint(CurBlock->begin());
3238 EmitAArch64CpuInit();
3239 AArch64CpuInitialized = true;
3240 Builder.SetInsertPoint(CurBlock);
3241 }
3242
3243 llvm::BasicBlock *RetBlock = createBasicBlock("resolver_return", Resolver);
3244 CGBuilderTy RetBuilder(CGM, RetBlock);
3245 CreateMultiVersionResolverReturn(CGM, Resolver, RetBuilder, RO.Function,
3246 SupportsIFunc);
3247 CurBlock = createBasicBlock("resolver_else", Resolver);
3248 Builder.CreateCondBr(Condition, RetBlock, CurBlock);
3249 }
3250
3251 // If no default, emit an unreachable.
3252 Builder.SetInsertPoint(CurBlock);
3254}
3255
3257 llvm::Function *Resolver, ArrayRef<FMVResolverOption> Options) {
3258
3259 bool SupportsIFunc = getContext().getTargetInfo().supportsIFunc();
3260
3261 // Main function's basic block.
3262 llvm::BasicBlock *CurBlock = createBasicBlock("resolver_entry", Resolver);
3263 Builder.SetInsertPoint(CurBlock);
3264 EmitX86CpuInit();
3265
3266 for (const FMVResolverOption &RO : Options) {
3267 Builder.SetInsertPoint(CurBlock);
3268 llvm::Value *Condition = FormX86ResolverCondition(RO);
3269
3270 // The 'default' or 'generic' case.
3271 if (!Condition) {
3272 assert(&RO == Options.end() - 1 &&
3273 "Default or Generic case must be last");
3274 CreateMultiVersionResolverReturn(CGM, Resolver, Builder, RO.Function,
3275 SupportsIFunc);
3276 return;
3277 }
3278
3279 llvm::BasicBlock *RetBlock = createBasicBlock("resolver_return", Resolver);
3280 CGBuilderTy RetBuilder(CGM, RetBlock);
3281 CreateMultiVersionResolverReturn(CGM, Resolver, RetBuilder, RO.Function,
3282 SupportsIFunc);
3283 CurBlock = createBasicBlock("resolver_else", Resolver);
3284 Builder.CreateCondBr(Condition, RetBlock, CurBlock);
3285 }
3286
3287 // If no generic/default, emit an unreachable.
3288 Builder.SetInsertPoint(CurBlock);
3290}
3291
3292// Loc / DynamicLoc - where the diagnostic will point, where in the source code
3293// this alignment has failed.
3294// AssumptionLoc - if present (will be present if sufficiently different from
3295// Loc), the diagnostic will additionally point a "Note:" to this location.
3296// It should be the location where the __attribute__((assume_aligned))
3297// was written e.g.
3298static void emitAlignmentAssumptionCheck(CodeGenFunction &CGF, llvm::Value *Ptr,
3299 QualType Ty, SourceLocation Loc,
3300 llvm::Value *DynamicLoc,
3301 SourceLocation AssumptionLoc,
3302 llvm::Value *Alignment,
3303 llvm::Value *OffsetValue) {
3304 if (!CGF.SanOpts.has(SanitizerKind::Alignment))
3305 return;
3306
3307 CGBuilderTy &Builder = CGF.Builder;
3308 llvm::IntegerType *IntPtrTy = CGF.IntPtrTy;
3309 if (Alignment->getType() != IntPtrTy)
3310 Alignment =
3311 Builder.CreateIntCast(Alignment, IntPtrTy, false, "casted.align");
3312 if (OffsetValue && OffsetValue->getType() != IntPtrTy)
3313 OffsetValue =
3314 Builder.CreateIntCast(OffsetValue, IntPtrTy, true, "casted.offset");
3315
3316 llvm::Value *PtrIntValue = Builder.CreatePtrToInt(Ptr, IntPtrTy, "ptrint");
3317
3318 if (OffsetValue) {
3319 bool IsOffsetZero = false;
3320 if (const auto *CI = dyn_cast<llvm::ConstantInt>(OffsetValue))
3321 IsOffsetZero = CI->isZero();
3322
3323 if (!IsOffsetZero)
3324 PtrIntValue = Builder.CreateSub(PtrIntValue, OffsetValue, "offsetptr");
3325 }
3326
3327 llvm::Value *Zero = llvm::ConstantInt::get(IntPtrTy, 0);
3328 llvm::Value *Mask =
3329 Builder.CreateSub(Alignment, llvm::ConstantInt::get(IntPtrTy, 1));
3330 llvm::Value *MaskedPtr = Builder.CreateAnd(PtrIntValue, Mask, "maskedptr");
3331 llvm::Value *TheCheck = Builder.CreateICmpEQ(MaskedPtr, Zero, "maskcond");
3332
3333 // Don't check pointers to volatile data. The behavior here is implementation-
3334 // defined.
3336 return;
3337
3338 auto CheckOrdinal = SanitizerKind::SO_Alignment;
3339 auto CheckHandler = !DynamicLoc ? SanitizerHandler::AlignmentAssumption
3340 : SanitizerHandler::ReturnAlignmentAssumption;
3341 SanitizerDebugLocation SanScope(&CGF, {CheckOrdinal}, CheckHandler);
3342
3343 if (!OffsetValue)
3344 OffsetValue = Builder.getInt1(false); // no offset.
3345
3346 if (!DynamicLoc) {
3347 llvm::Constant *StaticData[] = {CGF.EmitCheckSourceLocation(Loc),
3348 CGF.EmitCheckSourceLocation(AssumptionLoc),
3349 CGF.EmitCheckTypeDescriptor(Ty)};
3350 llvm::Value *DynamicData[] = {Ptr, Alignment, OffsetValue};
3351 CGF.EmitCheck({std::make_pair(TheCheck, CheckOrdinal)}, CheckHandler,
3352 StaticData, DynamicData);
3353 } else {
3354 llvm::Constant *StaticData[] = {CGF.EmitCheckSourceLocation(AssumptionLoc),
3355 CGF.EmitCheckTypeDescriptor(Ty)};
3356 llvm::Value *DynamicData[] = {DynamicLoc, Ptr, Alignment, OffsetValue};
3357 CGF.EmitCheck({std::make_pair(TheCheck, CheckOrdinal)}, CheckHandler,
3358 StaticData, DynamicData);
3359 }
3360}
3361
3363 QualType Ty,
3364 SourceLocation Loc,
3365 SourceLocation AssumptionLoc,
3366 llvm::Value *Alignment,
3367 llvm::Value *OffsetValue) {
3368 ::emitAlignmentAssumptionCheck(*this, Ptr, Ty, Loc, nullptr, AssumptionLoc,
3369 Alignment, OffsetValue);
3370}
3371
3373 llvm::Value *Ptr, QualType Ty, SourceLocation AssumptionLoc,
3374 llvm::Value *Alignment, llvm::Value *OffsetValue) {
3375 llvm::Value *SLocPtr = Builder.CreateLoad(ReturnLocation, "return.sloc.load");
3376 ::emitAlignmentAssumptionCheck(*this, Ptr, Ty, SourceLocation(), SLocPtr,
3377 AssumptionLoc, Alignment, OffsetValue);
3378}
3379
3381 if (CGDebugInfo *DI = getDebugInfo())
3382 return DI->SourceLocToDebugLoc(Location);
3383
3384 return llvm::DebugLoc();
3385}
3386
3387llvm::Value *
3388CodeGenFunction::emitCondLikelihoodViaExpectIntrinsic(llvm::Value *Cond,
3389 Stmt::Likelihood LH) {
3390 switch (LH) {
3391 case Stmt::LH_None:
3392 return Cond;
3393 case Stmt::LH_Likely:
3394 case Stmt::LH_Unlikely:
3395 // Don't generate llvm.expect on -O0 as the backend won't use it for
3396 // anything.
3397 if (CGM.getCodeGenOpts().OptimizationLevel == 0)
3398 return Cond;
3399 llvm::Type *CondTy = Cond->getType();
3400 assert(CondTy->isIntegerTy(1) && "expecting condition to be a boolean");
3401 llvm::Function *FnExpect =
3402 CGM.getIntrinsic(llvm::Intrinsic::expect, CondTy);
3403 llvm::Value *ExpectedValueOfCond =
3404 llvm::ConstantInt::getBool(CondTy, LH == Stmt::LH_Likely);
3405 return Builder.CreateCall(FnExpect, {Cond, ExpectedValueOfCond},
3406 Cond->getName() + ".expval");
3407 }
3408 llvm_unreachable("Unknown Likelihood");
3409}
3410
3411llvm::Value *CodeGenFunction::emitBoolVecConversion(llvm::Value *SrcVec,
3412 unsigned NumElementsDst,
3413 const llvm::Twine &Name) {
3414 auto *SrcTy = cast<llvm::FixedVectorType>(SrcVec->getType());
3415 unsigned NumElementsSrc = SrcTy->getNumElements();
3416 if (NumElementsSrc == NumElementsDst)
3417 return SrcVec;
3418
3419 std::vector<int> ShuffleMask(NumElementsDst, -1);
3420 for (unsigned MaskIdx = 0;
3421 MaskIdx < std::min<>(NumElementsDst, NumElementsSrc); ++MaskIdx)
3422 ShuffleMask[MaskIdx] = MaskIdx;
3423
3424 return Builder.CreateShuffleVector(SrcVec, ShuffleMask, Name);
3425}
3426
3428 const CGPointerAuthInfo &PointerAuth,
3430 if (!PointerAuth.isSigned())
3431 return;
3432
3433 auto *Key = Builder.getInt32(PointerAuth.getKey());
3434
3435 llvm::Value *Discriminator = PointerAuth.getDiscriminator();
3436 if (!Discriminator)
3437 Discriminator = Builder.getSize(0);
3438
3439 llvm::Value *Args[] = {Key, Discriminator};
3440 Bundles.emplace_back("ptrauth", Args);
3441}
3442
3444 const CGPointerAuthInfo &PointerAuth,
3445 llvm::Value *Pointer,
3446 unsigned IntrinsicID) {
3447 if (!PointerAuth)
3448 return Pointer;
3449
3450 auto Key = CGF.Builder.getInt32(PointerAuth.getKey());
3451
3452 llvm::Value *Discriminator = PointerAuth.getDiscriminator();
3453 if (!Discriminator) {
3454 Discriminator = CGF.Builder.getSize(0);
3455 }
3456
3457 // Convert the pointer to intptr_t before signing it.
3458 auto OrigType = Pointer->getType();
3459 Pointer = CGF.Builder.CreatePtrToInt(Pointer, CGF.IntPtrTy);
3460
3461 // call i64 @llvm.ptrauth.sign.i64(i64 %pointer, i32 %key, i64 %discriminator)
3462 auto Intrinsic = CGF.CGM.getIntrinsic(IntrinsicID);
3463 Pointer = CGF.EmitRuntimeCall(Intrinsic, {Pointer, Key, Discriminator});
3464
3465 // Convert back to the original type.
3466 Pointer = CGF.Builder.CreateIntToPtr(Pointer, OrigType);
3467 return Pointer;
3468}
3469
3470llvm::Value *
3472 llvm::Value *Pointer) {
3473 if (!PointerAuth.shouldSign())
3474 return Pointer;
3475 return EmitPointerAuthCommon(*this, PointerAuth, Pointer,
3476 llvm::Intrinsic::ptrauth_sign);
3477}
3478
3479static llvm::Value *EmitStrip(CodeGenFunction &CGF,
3480 const CGPointerAuthInfo &PointerAuth,
3481 llvm::Value *Pointer) {
3482 auto StripIntrinsic = CGF.CGM.getIntrinsic(llvm::Intrinsic::ptrauth_strip);
3483
3484 auto Key = CGF.Builder.getInt32(PointerAuth.getKey());
3485 // Convert the pointer to intptr_t before signing it.
3486 auto OrigType = Pointer->getType();
3488 StripIntrinsic, {CGF.Builder.CreatePtrToInt(Pointer, CGF.IntPtrTy), Key});
3489 return CGF.Builder.CreateIntToPtr(Pointer, OrigType);
3490}
3491
3492llvm::Value *
3494 llvm::Value *Pointer) {
3495 if (PointerAuth.shouldStrip()) {
3496 return EmitStrip(*this, PointerAuth, Pointer);
3497 }
3498 if (!PointerAuth.shouldAuth()) {
3499 return Pointer;
3500 }
3501
3502 return EmitPointerAuthCommon(*this, PointerAuth, Pointer,
3503 llvm::Intrinsic::ptrauth_auth);
3504}
3505
3507 llvm::Instruction *KeyInstruction, llvm::Value *Backup) {
3508 if (CGDebugInfo *DI = getDebugInfo())
3509 DI->addInstToCurrentSourceAtom(KeyInstruction, Backup);
3510}
3511
3513 llvm::Instruction *KeyInstruction, llvm::Value *Backup, uint64_t Atom) {
3514 if (CGDebugInfo *DI = getDebugInfo())
3515 DI->addInstToSpecificSourceAtom(KeyInstruction, Backup, Atom);
3516}
3517
3518void CodeGenFunction::addInstToNewSourceAtom(llvm::Instruction *KeyInstruction,
3519 llvm::Value *Backup) {
3520 if (CGDebugInfo *DI = getDebugInfo()) {
3522 DI->addInstToCurrentSourceAtom(KeyInstruction, Backup);
3523 }
3524}
3525
3527 QualType Ty) {
3528 for (auto &Field : getContext().findPFPFields(Ty)) {
3529 if (getContext().arePFPFieldsTriviallyCopyable(Field.Field->getParent()))
3530 continue;
3531 auto DestFieldPtr = EmitAddressOfPFPField(DestPtr, Field);
3532 auto SrcFieldPtr = EmitAddressOfPFPField(SrcPtr, Field);
3533 Builder.CreateStore(Builder.CreateLoad(SrcFieldPtr), DestFieldPtr);
3534 }
3535}
static void findPFPFields(const ASTContext &Ctx, QualType Ty, CharUnits Offset, std::vector< PFPField > &Fields, bool IncludeVBases)
Defines the clang::ASTContext interface.
#define V(N, I)
This file provides some common utility functions for processing Lambda related AST Constructs.
Defines enum values for all the target-independent builtin functions.
static llvm::Value * EmitPointerAuthCommon(CodeGenFunction &CGF, const CGPointerAuthInfo &PointerAuth, llvm::Value *Pointer, unsigned IntrinsicID)
static void CreateMultiVersionResolverReturn(CodeGenModule &CGM, llvm::Function *Resolver, CGBuilderTy &Builder, llvm::Function *FuncToReturn, bool SupportsIFunc)
static llvm::Value * EmitStrip(CodeGenFunction &CGF, const CGPointerAuthInfo &PointerAuth, llvm::Value *Pointer)
static void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType, Address dest, Address src, llvm::Value *sizeInChars)
emitNonZeroVLAInit - Emit the "zero" initialization of a variable-length array whose elements have a ...
static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB)
static LValue makeNaturalAlignAddrLValue(llvm::Value *V, QualType T, bool ForPointeeType, bool MightBeSigned, CodeGenFunction &CGF, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
static void TryMarkNoThrow(llvm::Function *F)
Tries to mark the given function nounwind based on the non-existence of any throwing calls within it.
static llvm::Constant * getPrologueSignature(CodeGenModule &CGM, const FunctionDecl *FD)
Return the UBSan prologue signature for FD if one is available.
static bool endsWithReturn(const Decl *F)
Determine whether the function F ends with a return stmt.
static bool matchesStlAllocatorFn(const Decl *D, const ASTContext &Ctx)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Result
Implement __builtin_bit_cast and related operations.
static StringRef getTriple(const Command &Job)
Defines the Objective-C statement AST node classes.
Enumerates target-specific builtins in their own namespaces within namespace clang.
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:158
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
const llvm::StringMap< bool > & getFunctionFeatureMap(const FunctionDecl *FD) const
QualType getFunctionTypeWithExceptionSpec(QualType Orig, const FunctionProtoType::ExceptionSpecInfo &ESI) const
Get a function type and produce the equivalent function type with the specified exception specificati...
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
bool hasAnyFunctionEffects() const
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
static bool hasSameType(QualType T1, QualType T2)
Determine whether the given types T1 and T2 are equivalent.
const VariableArrayType * getAsVariableArrayType(QualType T) const
QualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:974
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3820
QualType getElementType() const
Definition TypeBase.h:3832
Attr - This represents one attribute.
Definition Attr.h:47
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4107
static bool isLogicalOp(Opcode Opc)
Definition Expr.h:4240
BinaryOperatorKind Opcode
Definition Expr.h:4112
Represents a C++ constructor within a class.
Definition DeclCXX.h:2645
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2153
bool isImplicitObjectMemberFunction() const
[C++2b][dcl.fct]/p7 An implicit object member function is a non-static member function without an exp...
Definition DeclCXX.cpp:2726
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2296
QualType getThisType() const
Return the type of the this pointer.
Definition DeclCXX.cpp:2859
QualType getFunctionObjectParameterType() const
Definition DeclCXX.h:2320
bool isStatic() const
Definition DeclCXX.cpp:2417
bool isCopyAssignmentOperator() const
Determine whether this is a copy-assignment operator, regardless of whether it was declared implicitl...
Definition DeclCXX.cpp:2730
bool isLambda() const
Determine whether this class describes a lambda function object.
Definition DeclCXX.h:1028
void getCaptureFields(llvm::DenseMap< const ValueDecl *, FieldDecl * > &Captures, FieldDecl *&ThisCapture) const
For a closure type, retrieve the mapping from captured variables and this to the non-static data memb...
Definition DeclCXX.cpp:1792
bool isCapturelessLambda() const
Definition DeclCXX.h:1074
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition DeclCXX.h:1196
A C++ throw-expression (C++ [except.throw]).
Definition ExprCXX.h:1213
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:3012
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3216
This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
bool isZero() const
Test whether the quantity equals zero.
Definition CharUnits.h:101
llvm::Align getAsAlign() const
Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit bytes.
Definition CharUnits.h:159
QuantityType getQuantity() const
Get the raw integer representation of this quantity.
Definition CharUnits.h:155
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition CharUnits.h:184
bool isOne() const
Test whether the quantity equals one.
Definition CharUnits.h:104
@ InAlloca
InAlloca - Pass the argument directly using the LLVM inalloca attribute.
@ Indirect
Indirect - Pass the argument indirectly via a hidden pointer with the specified alignment (0 indicate...
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition Address.h:128
llvm::Value * getBasePointer() const
Definition Address.h:198
static Address invalid()
Definition Address.h:176
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
Definition Address.h:253
CharUnits getAlignment() const
Definition Address.h:194
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition Address.h:209
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
Definition Address.h:276
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition Address.h:204
A scoped helper to set the current source atom group for CGDebugInfo::addInstToCurrentSourceAtom.
A scoped helper to set the current debug location to the specified location or preferred location of ...
static ApplyDebugLocation CreateDefaultArtificial(CodeGenFunction &CGF, SourceLocation TemporaryLocation)
Apply TemporaryLocation if it is valid.
void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock::iterator InsertPt) const
This forwards to CodeGenFunction::InsertHelper.
llvm::ConstantInt * getSize(CharUnits N)
Definition CGBuilder.h:119
@ RAA_DirectInMemory
Pass it on the stack using its defined layout.
Definition CGCXXABI.h:158
virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const =0
Returns how an argument of the given record type should be passed.
Abstract information about a function or function prototype.
Definition CGCall.h:43
const FunctionProtoType * getCalleeFunctionProtoType() const
Definition CGCall.h:59
All available information about a concrete callee.
Definition CGCall.h:66
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition CGCall.h:140
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition CGDebugInfo.h:60
CGFunctionInfo - Class to encapsulate the information about a function definition.
llvm::Value * getDiscriminator() const
CallArgList - Type for representing both the value and type of arguments in a call.
Definition CGCall.h:277
void add(RValue rvalue, QualType type)
Definition CGCall.h:305
CGFPOptionsRAII(CodeGenFunction &CGF, FPOptions FPFeatures)
An object to manage conditionally-evaluated expressions.
An object which temporarily prevents a value from being destroyed by aggressive peephole optimization...
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void EmitRISCVMultiVersionResolver(llvm::Function *Resolver, ArrayRef< FMVResolverOption > Options)
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, uint64_t TrueCount, Stmt::Likelihood LH=Stmt::LH_None, const Expr *ConditionalOp=nullptr, const VarDecl *ConditionalDecl=nullptr)
EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g.
void setCurrentProfileCount(uint64_t Count)
Set the profiler's current count.
llvm::CallInst * EmitTrapCall(llvm::Intrinsic::ID IntrID, bool EnsureInsertPoint=true)
Emit a call to trap or debugtrap.
Definition CGExpr.cpp:4733
llvm::Value * emitBoolVecConversion(llvm::Value *SrcVec, unsigned NumElementsDst, const llvm::Twine &Name="")
void EmitAArch64MultiVersionResolver(llvm::Function *Resolver, ArrayRef< FMVResolverOption > Options)
JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target)
The given basic block lies in the current EH scope, but may be a target of a potentially scope-crossi...
llvm::Value * EmitScalarOrConstFoldImmArg(unsigned ICEArguments, unsigned Idx, const CallExpr *E)
SanitizerSet SanOpts
Sanitizers enabled for this function.
void EmitNullInitialization(Address DestPtr, QualType Ty)
EmitNullInitialization - Generate code to set a value of the given type to null, If the type contains...
void checkTargetFeatures(const CallExpr *E, const FunctionDecl *TargetDecl)
static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts=false)
ContainsLabel - Return true if the statement contains a label in it.
void EmitPPCAIXMultiVersionResolver(llvm::Function *Resolver, ArrayRef< FMVResolverOption > Options)
define internal ptr @foo.resolver() { entry: is_version_1 = __builtin_cpu_supports(version_1) br i1 %...
bool ShouldSkipSanitizerInstrumentation()
ShouldSkipSanitizerInstrumentation - Return true if the current function should not be instrumented w...
llvm::Value * EmitPPCBuiltinCpu(unsigned BuiltinID, llvm::Type *ReturnType, StringRef CPUStr)
Definition PPC.cpp:73
llvm::BlockAddress * GetAddrOfLabel(const LabelDecl *L)
llvm::Value * EmitRISCVCpuSupports(const CallExpr *E)
Definition RISCV.cpp:976
llvm::Value * EmitRISCVCpuInit()
Definition RISCV.cpp:966
static bool hasScalarEvaluationKind(QualType T)
llvm::Type * ConvertType(QualType T)
void GenerateCode(GlobalDecl GD, llvm::Function *Fn, const CGFunctionInfo &FnInfo)
void EmitSanitizerStatReport(llvm::SanitizerStatKind SSK)
void addInstToNewSourceAtom(llvm::Instruction *KeyInstruction, llvm::Value *Backup)
Add KeyInstruction and an optional Backup instruction to a new atom group (See ApplyAtomGroup for mor...
PeepholeProtection protectFromPeepholes(RValue rvalue)
protectFromPeepholes - Protect a value that we're intending to store to the side, but which will prob...
void EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD)
Definition CGClass.cpp:3111
bool CurFuncIsThunk
In C++, whether we are code generating a thunk.
LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T)
Given a value of type T* that may not be to a complete object, construct an l-value with the natural ...
JumpDest getJumpDestForLabel(const LabelDecl *S)
getBasicBlockForLabel - Return the LLVM basicblock that the specified label maps to.
Definition CGStmt.cpp:712
void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint=true)
SmallVector< llvm::ConvergenceControlInst *, 4 > ConvergenceTokenStack
Stack to track the controlled convergence tokens.
void unprotectFromPeepholes(PeepholeProtection protection)
RValue convertTempToRValue(Address addr, QualType type, SourceLocation Loc)
Given the address of a temporary variable, produce an r-value of its type.
Definition CGExpr.cpp:7375
llvm::Constant * EmitCheckSourceLocation(SourceLocation Loc)
Emit a description of a source location in a format suitable for passing to a runtime sanitizer handl...
Definition CGExpr.cpp:4170
llvm::SmallVector< DeferredDeactivateCleanup > DeferredDeactivationCleanupStack
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
void addInstToCurrentSourceAtom(llvm::Instruction *KeyInstruction, llvm::Value *Backup)
See CGDebugInfo::addInstToCurrentSourceAtom.
const LangOptions & getLangOpts() const
void addInstToSpecificSourceAtom(llvm::Instruction *KeyInstruction, llvm::Value *Backup, uint64_t Atom)
See CGDebugInfo::addInstToSpecificSourceAtom.
LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
void EmitVarAnnotations(const VarDecl *D, llvm::Value *V)
Emit local annotations for the local variable V, declared by D.
llvm::BasicBlock * EHResumeBlock
EHResumeBlock - Unified block containing a call to llvm.eh.resume.
Address EmitFieldAnnotations(const FieldDecl *D, Address V)
Emit field annotations for the given field & value.
void EmitConstructorBody(FunctionArgList &Args)
EmitConstructorBody - Emits the body of the current constructor.
Definition CGClass.cpp:781
void EmitKCFIOperandBundle(const CGCallee &Callee, SmallVectorImpl< llvm::OperandBundleDef > &Bundles)
void EmitDeclRefExprDbgValue(const DeclRefExpr *E, const APValue &Init)
Address makeNaturalAddressForPointer(llvm::Value *Ptr, QualType T, CharUnits Alignment=CharUnits::Zero(), bool ForPointeeType=false, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
Construct an address with the natural alignment of T.
LValue MakeNaturalAlignPointeeRawAddrLValue(llvm::Value *V, QualType T)
Same as MakeNaturalAlignPointeeAddrLValue except that the pointer is known to be unsigned.
@ TCK_ConstructorCall
Checking the 'this' pointer for a constructor call.
@ TCK_MemberCall
Checking the 'this' pointer for a call to a non-static member function.
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
bool hasSkipCounter(const Stmt *S) const
llvm::AssertingVH< llvm::Instruction > AllocaInsertPt
AllocaInsertPoint - This is an instruction in the entry block before which we prefer to insert alloca...
void EmitFunctionBody(const Stmt *Body)
JumpDest ReturnBlock
ReturnBlock - Unified return block.
llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Location)
Converts Location to a DebugLoc, if debug information is enabled.
llvm::Constant * EmitCheckTypeDescriptor(QualType T)
Emit a description of a type in a format suitable for passing to a runtime sanitizer handler.
Definition CGExpr.cpp:4051
llvm::DebugLoc EmitReturnBlock()
Emit the unified return block, trying to avoid its emission when possible.
RawAddress CreateDefaultAlignTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
CreateDefaultAlignedTempAlloca - This creates an alloca with the default ABI alignment of the given L...
Definition CGExpr.cpp:187
const TargetInfo & getTarget() const
llvm::Value * EmitAnnotationCall(llvm::Function *AnnotationFn, llvm::Value *AnnotatedVal, StringRef AnnotationStr, SourceLocation Location, const AnnotateAttr *Attr)
Emit an annotation call (intrinsic).
Address EmitCompoundStmtWithoutScope(const CompoundStmt &S, bool GetLast=false, AggValueSlot AVS=AggValueSlot::ignored())
Definition CGStmt.cpp:587
void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize, std::initializer_list< llvm::Value ** > ValuesToReload={})
Takes the old cleanup stack size and emits the cleanup blocks that have been added.
void maybeCreateMCDCCondBitmap()
Allocate a temp value on the stack that MCDC can use to track condition results.
void emitReturnAlignmentAssumptionCheck(llvm::Value *Ptr, QualType Ty, SourceLocation AssumptionLoc, llvm::Value *Alignment, llvm::Value *OffsetValue=nullptr)
void EmitIgnoredExpr(const Expr *E)
EmitIgnoredExpr - Emit an expression in a context which ignores the result.
Definition CGExpr.cpp:263
RValue EmitLoadOfLValue(LValue V, SourceLocation Loc)
EmitLoadOfLValue - Given an expression that represents a value lvalue, this method emits the address ...
Definition CGExpr.cpp:2549
static bool isInstrumentedCondition(const Expr *C)
isInstrumentedCondition - Determine whether the given condition is an instrumentable condition (i....
VlaSizePair getVLAElements1D(const VariableArrayType *vla)
Return the number of elements for a single dimension for the given array type.
RawAddress CreateIRTempWithoutCast(QualType T, const Twine &Name="tmp")
CreateIRTempWithoutCast - Create a temporary IR object of the given type, with appropriate alignment.
Definition CGExpr.cpp:194
bool AlwaysEmitXRayCustomEvents() const
AlwaysEmitXRayCustomEvents - Return true if we must unconditionally emit XRay custom event handling c...
void StartFunction(GlobalDecl GD, QualType RetTy, llvm::Function *Fn, const CGFunctionInfo &FnInfo, const FunctionArgList &Args, SourceLocation Loc=SourceLocation(), SourceLocation StartLoc=SourceLocation())
Emit code for the start of a function.
llvm::Value * EmitPointerAuthSign(const CGPointerAuthInfo &Info, llvm::Value *Pointer)
void markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn)
Annotate the function with an attribute that disables TSan checking at runtime.
llvm::Value * EvaluateExprAsBool(const Expr *E)
EvaluateExprAsBool - Perform the usual unary conversions on the specified expression and compare the ...
Definition CGExpr.cpp:244
void EmitPointerAuthOperandBundle(const CGPointerAuthInfo &Info, SmallVectorImpl< llvm::OperandBundleDef > &Bundles)
void EmitCheck(ArrayRef< std::pair< llvm::Value *, SanitizerKind::SanitizerOrdinal > > Checked, SanitizerHandler Check, ArrayRef< llvm::Constant * > StaticArgs, ArrayRef< llvm::Value * > DynamicArgs, const TrapReason *TR=nullptr)
Create a basic block that will either trap or call a handler function in the UBSan runtime with the p...
Definition CGExpr.cpp:4318
void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock::iterator InsertPt) const
CGBuilder insert helper.
llvm::Value * emitArrayLength(const ArrayType *arrayType, QualType &baseType, Address &addr)
emitArrayLength - Compute the length of an array, even if it's a VLA, and drill down to the base elem...
bool HaveInsertPoint() const
HaveInsertPoint - True if an insertion point is defined.
bool AlwaysEmitXRayTypedEvents() const
AlwaysEmitXRayTypedEvents - Return true if clang must unconditionally emit XRay typed event handling ...
void EmitStartEHSpec(const Decl *D)
EmitStartEHSpec - Emit the start of the exception spec.
void EmitDestructorBody(FunctionArgList &Args)
EmitDestructorBody - Emits the body of the current destructor.
Definition CGClass.cpp:1419
void EmitX86MultiVersionResolver(llvm::Function *Resolver, ArrayRef< FMVResolverOption > Options)
bool ShouldInstrumentFunction()
ShouldInstrumentFunction - Return true if the current function should be instrumented with __cyg_prof...
void maybeUpdateMCDCCondBitmap(const Expr *E, llvm::Value *Val)
Update the MCDC temp value with the condition's evaluated result.
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **CallOrInvoke, bool IsMustTail, SourceLocation Loc, bool IsVirtualFunctionPointerThunk=false)
EmitCall - Generate a call of the given function, expecting the given result type,...
Definition CGCall.cpp:5744
llvm::ConstantInt * getUBSanFunctionTypeHash(QualType T) const
Return a type hash constant for a function instrumented by -fsanitize=function.
void EmitBranchToCounterBlock(const Expr *Cond, BinaryOperator::Opcode LOp, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, uint64_t TrueCount=0, Stmt::Likelihood LH=Stmt::LH_None, const Expr *CntrIdx=nullptr)
EmitBranchToCounterBlock - Emit a conditional branch to a new block that increments a profile counter...
void emitAlignmentAssumptionCheck(llvm::Value *Ptr, QualType Ty, SourceLocation Loc, SourceLocation AssumptionLoc, llvm::Value *Alignment, llvm::Value *OffsetValue=nullptr)
VlaSizePair getVLASize(const VariableArrayType *vla)
Returns an LLVM value that corresponds to the size, in non-variably-sized elements,...
bool isMCDCBranchExpr(const Expr *E) const
void EmitMultiVersionResolver(llvm::Function *Resolver, ArrayRef< FMVResolverOption > Options)
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
static const Expr * stripCond(const Expr *C)
Ignore parentheses and logical-NOT to track conditions consistently.
void EmitFunctionProlog(const CGFunctionInfo &FI, llvm::Function *Fn, const FunctionArgList &Args)
EmitFunctionProlog - Emit the target specific LLVM code to load the arguments for the given function.
Definition CGCall.cpp:3499
Address EmitAddressOfPFPField(Address RecordPtr, const PFPField &Field)
void SetFastMathFlags(FPOptions FPFeatures)
Set the codegen fast-math flags.
llvm::SmallVector< char, 256 > LifetimeExtendedCleanupStack
Address EmitVAListRef(const Expr *E)
void EmitLambdaInAllocaCallOpBody(const CXXMethodDecl *MD)
Definition CGClass.cpp:3168
Address ReturnValuePointer
ReturnValuePointer - The temporary alloca to hold a pointer to sret.
static bool mightAddDeclToScope(const Stmt *S)
Determine if the given statement might introduce a declaration into the current scope,...
void EmitStmt(const Stmt *S, ArrayRef< const Attr * > Attrs={})
EmitStmt - Emit the code for the statement.
Definition CGStmt.cpp:59
llvm::DenseMap< const ValueDecl *, FieldDecl * > LambdaCaptureFields
bool AutoreleaseResult
In ARC, whether we should autorelease the return value.
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
uint64_t getCurrentProfileCount()
Get the profiler's current count.
llvm::Type * ConvertTypeForMem(QualType T)
void EmitEndEHSpec(const Decl *D)
EmitEndEHSpec - Emit the end of the exception spec.
LValue EmitLValueForLambdaField(const FieldDecl *Field)
Definition CGExpr.cpp:5872
void emitPFPPostCopyUpdates(Address DestPtr, Address SrcPtr, QualType Ty)
Copy all PFP fields from SrcPtr to DestPtr while updating signatures, assuming that DestPtr was alrea...
Address EmitZOSVAListRef(const Expr *E)
Emit a "reference" to a __builtin_zos_va_list; this is always the address of the expression,...
CodeGenTypes & getTypes() const
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
bool IsSanitizerScope
True if CodeGen currently emits code implementing sanitizer checks.
static bool containsBreak(const Stmt *S)
containsBreak - Return true if the statement contains a break out of it.
void emitImplicitAssignmentOperatorBody(FunctionArgList &Args)
Definition CGClass.cpp:1538
HLSLControlFlowHintAttr::Spelling HLSLControlFlowAttr
HLSL Branch attribute.
void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, LValue LV, QualType Type, SanitizerSet SkippedChecks=SanitizerSet(), llvm::Value *ArraySize=nullptr)
llvm::SmallVector< const ParmVarDecl *, 4 > FnArgs
Save Parameter Decl for coroutine.
void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc, SourceLocation EndLoc, uint64_t RetKeyInstructionsSourceAtom)
EmitFunctionEpilog - Emit the target specific LLVM code to return the given temporary.
Definition CGCall.cpp:4396
Address EmitPointerWithAlignment(const Expr *Addr, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitPointerWithAlignment - Given an expression with a pointer type, emit the value and compute our be...
Definition CGExpr.cpp:1661
void EmitBranch(llvm::BasicBlock *Block)
EmitBranch - Emit a branch to the specified basic block from the current insert block,...
Definition CGStmt.cpp:678
RawAddress NormalCleanupDest
i32s containing the indexes of the cleanup destinations.
llvm::Type * convertTypeForLoadStore(QualType ASTTy, llvm::Type *LLVMTy=nullptr)
llvm::BasicBlock * GetIndirectGotoBlock()
EHScopeStack::stable_iterator PrologueCleanupDepth
PrologueCleanupDepth - The cleanup depth enclosing all the cleanups associated with the parameters.
Address EmitMSVAListRef(const Expr *E)
Emit a "reference" to a __builtin_ms_va_list; this is always the value of the expression,...
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
const FunctionDecl * getCurrentFunctionDecl() const
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void EmitTrapCallAndMakeUnreachable()
Emit a call to '@llvm.trap()' and clear the current insert point.
Definition CGExpr.cpp:4756
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
const CGFunctionInfo * CurFnInfo
uint64_t getProfileCount(const Stmt *S)
Get the profiler's count for the given statement.
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result, bool AllowLabels=false)
ConstantFoldsToSimpleInteger - If the specified expression does not fold to a constant,...
void ErrorUnsupported(const Stmt *S, const char *Type)
ErrorUnsupported - Print out an error that codegen doesn't support the specified stmt yet.
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
LValue EmitLValue(const Expr *E, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitLValue - Emit code to compute a designator that specifies the location of the expression.
Definition CGExpr.cpp:1777
bool ShouldXRayInstrumentFunction() const
ShouldXRayInstrument - Return true if the current function should be instrumented with XRay nop sleds...
void EnsureInsertPoint()
EnsureInsertPoint - Ensure that an insertion point is defined so that emitted IR has a place to go.
llvm::LLVMContext & getLLVMContext()
bool SawAsmBlock
Whether we processed a Microsoft-style asm block during CodeGen.
bool checkIfFunctionMustProgress()
Returns true if a function must make progress, which means the mustprogress attribute can be added.
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV=nullptr)
Increment the profiler's counter for the given statement by StepV.
void emitAlignmentAssumption(llvm::Value *PtrValue, QualType Ty, SourceLocation Loc, SourceLocation AssumptionLoc, llvm::Value *Alignment, llvm::Value *OffsetValue=nullptr)
bool isMCDCDecisionExpr(const Expr *E) const
void EmitVariablyModifiedType(QualType Ty)
EmitVLASize - Capture all the sizes for the VLA expressions in the given variably-modified type and s...
void MaybeEmitDeferredVarDeclInit(const VarDecl *var)
Definition CGDecl.cpp:2159
void EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S)
When instrumenting to collect profile data, the counts for some blocks such as switch cases need to n...
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition CGStmt.cpp:658
LValue MakeNaturalAlignRawAddrLValue(llvm::Value *V, QualType T)
QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args)
llvm::Value * EmitPointerAuthAuth(const CGPointerAuthInfo &Info, llvm::Value *Pointer)
This class organizes the cross-function state that is used while generating LLVM code.
CharUnits getNaturalTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, bool forPointeeType=false)
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
void GenKernelArgMetadata(llvm::Function *FN, const FunctionDecl *FD=nullptr, CodeGenFunction *CGF=nullptr)
OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument information in the program executab...
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys={})
Per-function PGO state.
Definition CodeGenPGO.h:29
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
bool inheritingCtorHasParams(const InheritedConstructor &Inherited, CXXCtorType Type)
Determine if a C++ inheriting constructor should have parameters matching those of its inherited cons...
Definition CGCall.cpp:411
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition CGCall.h:378
LValue - This represents an lvalue references.
Definition CGValue.h:183
llvm::Value * getPointer(CodeGenFunction &CGF) const
Address getAddress() const
Definition CGValue.h:373
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition CGValue.h:42
bool isScalar() const
Definition CGValue.h:64
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition CGValue.h:72
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition CGCall.h:384
virtual llvm::Constant * getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const
Return a constant used by UBSan as a signature to identify functions possessing type information,...
Definition TargetInfo.h:274
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition Stmt.h:1751
ConditionalOperator - The ?
Definition Expr.h:4460
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1315
ValueDecl * getDecl()
Definition Expr.h:1383
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
T * getAttr() const
Definition DeclBase.h:582
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:550
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition DeclBase.h:1105
Decl * getNonClosureContext()
Find the innermost non-closure ancestor of this declaration, walking up through blocks,...
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
Definition DeclBase.h:568
SourceLocation getLocation() const
Definition DeclBase.h:448
bool hasAttr() const
Definition DeclBase.h:586
This represents one expression.
Definition Expr.h:114
bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer,...
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Returns the set of floating point options that apply to this expression.
Definition Expr.cpp:4031
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3125
std::optional< llvm::APSInt > getIntegerConstantExpr(const ASTContext &Ctx, bool AllowRelaxedEval=false) const
isIntegerConstantExpr - Return the value if this expression is a valid integer constant expression.
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3109
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:283
QualType getType() const
Definition Expr.h:146
ExtVectorType - Extended vector type.
Definition TypeBase.h:4497
LangOptions::FPExceptionModeKind getExceptionMode() const
bool allowFPContractAcrossStatement() const
RoundingMode getRoundingMode() const
Represents a member of a struct/union/class.
Definition Decl.h:3341
Represents a function declaration or definition.
Definition Decl.h:2082
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3285
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition Decl.cpp:3836
bool UsesFPIntrin() const
Determine whether the function was declared in source context that requires constrained FP intrinsics...
Definition Decl.h:3086
bool usesSEHTry() const
Indicates the function uses __try.
Definition Decl.h:2687
QualType getReturnType() const
Definition Decl.h:3022
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2947
FunctionDecl * getTemplateInstantiationPattern(bool ForDefinition=true) const
Retrieve the function declaration from which this function could be instantiated, if it is an instant...
Definition Decl.cpp:4340
FunctionEffectsRef getFunctionEffects() const
Definition Decl.h:3315
bool isMSVCRTEntryPoint() const
Determines whether this function is a MSVCRT user defined entry point.
Definition Decl.cpp:3449
bool isInlineBuiltinDeclaration() const
Determine if this function provides an inline implementation of a builtin.
Definition Decl.cpp:3600
bool hasImplicitReturnZero() const
Whether falling off this function implicitly returns null/zero.
Definition Decl.h:2597
bool isMain() const
Determines whether this function is "main", which is the entry point into an executable program.
Definition Decl.cpp:3442
bool isDefaulted() const
Whether this function is defaulted.
Definition Decl.h:2554
OverloadedOperatorKind getOverloadedOperator() const
getOverloadedOperator - Which C++ overloaded operator this function represents, if any.
Definition Decl.cpp:4206
FunctionDecl * getPreviousDecl()
Return the previous declaration of this declaration or NULL if this is the first declaration.
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5548
QualType desugar() const
Definition TypeBase.h:6129
FunctionTypeExtraAttributeInfo getExtraAttributeInfo() const
Return the extra attribute information.
Definition TypeBase.h:6037
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4744
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:60
CXXCtorType getCtorType() const
Definition GlobalDecl.h:117
KernelReferenceKind getKernelReferenceKind() const
Definition GlobalDecl.h:142
const Decl * getDecl() const
Definition GlobalDecl.h:115
One of these records is kept for each identifier that is lexed.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
static ImplicitParamDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, ImplicitParamKind ParamKind)
Create implicit parameter.
Definition Decl.cpp:5710
Represents the declaration of a label.
Definition Decl.h:526
FPExceptionModeKind
Possible floating point exception behavior.
@ FPE_Strict
Strictly preserve the floating-point exception semantics.
@ FPE_MayTrap
Transformations do not cause new exceptions but may hide some.
@ FPE_Ignore
Assume that floating-point exceptions are masked.
RoundingMode getDefaultRoundingMode() const
Represents a parameter to a function.
Definition Decl.h:1832
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3403
@ Forbid
Profiling is forbidden using the noprofile attribute.
Definition ProfileList.h:37
@ Skip
Profiling is skipped using the skipprofile attribute.
Definition ProfileList.h:35
@ Allow
Profiling is allowed.
Definition ProfileList.h:33
A (possibly-)qualified type.
Definition TypeBase.h:938
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8662
field_range fields() const
Definition Decl.h:4709
Encodes a location in the source.
A trivial tuple used to represent a source range.
Stmt - This represents one statement.
Definition Stmt.h:85
child_range children()
Definition Stmt.cpp:304
StmtClass getStmtClass() const
Definition Stmt.h:1504
Likelihood
The likelihood of a branch being taken.
Definition Stmt.h:1447
@ LH_Unlikely
Branch has the [[unlikely]] attribute.
Definition Stmt.h:1448
@ LH_None
No attribute set or branches of the IfStmt have the same attribute.
Definition Stmt.h:1449
@ LH_Likely
Branch has the [[likely]] attribute.
Definition Stmt.h:1451
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
bool supportsIFunc() const
Identify whether this target supports IFuncs.
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
virtual ParsedTargetAttr parseTargetAttr(StringRef Str) const
bool isVoidType() const
Definition TypeBase.h:9200
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
Definition Type.cpp:2394
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:885
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition TypeBase.h:2881
TypeClass getTypeClass() const
Definition TypeBase.h:2449
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9431
bool isObjCRetainableType() const
Definition Type.cpp:5611
bool isFunctionNoProtoType() const
Definition TypeBase.h:2664
bool isCFIUncheckedCalleeFunctionType() const
Definition TypeBase.h:8861
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2313
QualType getType() const
Definition Decl.h:725
Represents a variable declaration or definition.
Definition Decl.h:934
Represents a C array with a specified size that is not an integer-constant-expression.
Definition TypeBase.h:4064
Expr * getSizeExpr() const
Definition TypeBase.h:4078
QualType getElementType() const
Definition TypeBase.h:4412
Defines the clang::TargetInfo interface.
#define UINT_MAX
Definition limits.h:64
void checkTargetFeatures(ASTContext &Ctx, DiagnosticsEngine &Diags, const LangOptions &LangOpts, const CallExpr *E, const FunctionDecl *Caller, const FunctionDecl *TargetDecl)
Check that a call to a target-specific builtin has the required target features enabled in the caller...
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
Definition CGValue.h:155
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:146
TypeEvaluationKind
The kind of evaluation to perform on values of a particular type.
CGBuilderInserter CGBuilderInserterTy
Definition CGBuilder.h:47
constexpr XRayInstrMask Typed
Definition XRayInstr.h:42
constexpr XRayInstrMask FunctionExit
Definition XRayInstr.h:40
constexpr XRayInstrMask FunctionEntry
Definition XRayInstr.h:39
constexpr XRayInstrMask Custom
Definition XRayInstr.h:41
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const AstTypeMatcher< ArrayType > arrayType
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ CPlusPlus
Expr * IgnoreBuiltinExpectSingleStep(Expr *E)
Definition IgnoreExpr.h:135
@ NonNull
Values of this type can never be null.
Definition Specifiers.h:352
Expr * IgnoreExprNodes(Expr *E, FnTys &&... Fns)
Given an expression E and functions Fn_1,...,Fn_n : Expr * -> Expr *, Recursively apply each of the f...
Definition IgnoreExpr.h:24
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition ASTLambda.h:28
@ Result
The result type of a method or function.
Definition TypeBase.h:906
const FunctionProtoType * T
Expr * IgnoreImplicitCastsSingleStep(Expr *E)
Definition IgnoreExpr.h:38
Expr * IgnoreUOpLNotSingleStep(Expr *E)
Definition IgnoreExpr.h:127
Expr * IgnoreParensSingleStep(Expr *E)
Definition IgnoreExpr.h:157
llvm::fp::ExceptionBehavior ToConstrainedExceptMD(LangOptions::FPExceptionModeKind Kind)
U cast(CodeGen::Address addr)
Definition Address.h:327
bool IsArmStreamingFunction(const FunctionDecl *FD, bool IncludeLocallyStreaming)
Returns whether the given FunctionDecl has an __arm[_locally]_streaming attribute.
Definition Decl.cpp:6214
@ Other
Other implicit parameter.
Definition Decl.h:1787
@ EST_None
no exception specification
@ Implicit
An implicit conversion.
Definition Sema.h:434
A jump destination is an abstract label, branching to which may require a jump out through normal cle...
This structure provides a set of types that are commonly used during IR emission.
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
EvalResult is a struct with detailed info about an evaluated expression.
Definition Expr.h:667
A FunctionEffect plus a potential boolean expression determining whether the effect is declared (e....
Definition TypeBase.h:5285
std::vector< std::string > Features
Definition TargetInfo.h:60
void set(SanitizerMask K, bool Value)
Enable or disable a certain (single) sanitizer.
Definition Sanitizers.h:187
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition Sanitizers.h:174