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}
703
704static bool matchesStlAllocatorFn(const Decl *D, const ASTContext &Ctx) {
705 auto *MD = dyn_cast_or_null<CXXMethodDecl>(D);
706 if (!MD || !MD->getDeclName().getAsIdentifierInfo() ||
707 !MD->getDeclName().getAsIdentifierInfo()->isStr("allocate") ||
708 (MD->getNumParams() != 1 && MD->getNumParams() != 2))
709 return false;
710
711 if (!Ctx.hasSameType(MD->parameters()[0]->getType(), Ctx.getSizeType()))
712 return false;
713
714 if (MD->getNumParams() == 2) {
715 auto *PT = MD->parameters()[1]->getType()->getAs<PointerType>();
716 if (!PT || !PT->isVoidPointerType() ||
717 !PT->getPointeeType().isConstQualified())
718 return false;
719 }
720
721 return true;
722}
723
724bool CodeGenFunction::isInAllocaArgument(CGCXXABI &ABI, QualType Ty) {
725 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
726 return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory;
727}
728
729bool CodeGenFunction::hasInAllocaArg(const CXXMethodDecl *MD) {
730 return getTarget().getTriple().getArch() == llvm::Triple::x86 &&
732 llvm::any_of(MD->parameters(), [&](ParmVarDecl *P) {
733 return isInAllocaArgument(CGM.getCXXABI(), P->getType());
734 });
735}
736
737/// Return the UBSan prologue signature for \p FD if one is available.
738static llvm::Constant *getPrologueSignature(CodeGenModule &CGM,
739 const FunctionDecl *FD) {
740 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
741 if (!MD->isStatic())
742 return nullptr;
744}
745
747 llvm::Function *Fn,
748 const CGFunctionInfo &FnInfo,
749 const FunctionArgList &Args,
750 SourceLocation Loc,
751 SourceLocation StartLoc) {
752 assert(!CurFn &&
753 "Do not use a CodeGenFunction object for more than one function");
754
755 const Decl *D = GD.getDecl();
756
757 DidCallStackSave = false;
758 CurCodeDecl = D;
759 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
760 if (FD && FD->usesSEHTry())
761 CurSEHParent = GD;
762 CurFuncDecl = (D ? D->getNonClosureContext() : nullptr);
763 FnRetTy = RetTy;
764 CurFn = Fn;
765 CurFnInfo = &FnInfo;
766 assert(CurFn->isDeclaration() && "Function already has body?");
767
768 // If this function is ignored for any of the enabled sanitizers,
769 // disable the sanitizer for the function.
770 do {
771#define SANITIZER(NAME, ID) \
772 if (SanOpts.empty()) \
773 break; \
774 if (SanOpts.has(SanitizerKind::ID)) \
775 if (CGM.isInNoSanitizeList(SanitizerKind::ID, Fn, Loc)) \
776 SanOpts.set(SanitizerKind::ID, false);
777
778#include "clang/Basic/Sanitizers.def"
779#undef SANITIZER
780 } while (false);
781
782 if (D) {
783 const bool SanitizeBounds = SanOpts.hasOneOf(SanitizerKind::Bounds);
784 SanitizerMask no_sanitize_mask;
785 bool NoSanitizeCoverage = false;
786
787 for (auto *Attr : D->specific_attrs<NoSanitizeAttr>()) {
788 no_sanitize_mask |= Attr->getMask();
789 // SanitizeCoverage is not handled by SanOpts.
790 if (Attr->hasCoverage())
791 NoSanitizeCoverage = true;
792 }
793
794 // Apply the no_sanitize* attributes to SanOpts.
795 SanOpts.Mask &= ~no_sanitize_mask;
796 if (no_sanitize_mask & SanitizerKind::Address)
797 SanOpts.set(SanitizerKind::KernelAddress, false);
798 if (no_sanitize_mask & SanitizerKind::KernelAddress)
799 SanOpts.set(SanitizerKind::Address, false);
800 if (no_sanitize_mask & SanitizerKind::HWAddress)
801 SanOpts.set(SanitizerKind::KernelHWAddress, false);
802 if (no_sanitize_mask & SanitizerKind::KernelHWAddress)
803 SanOpts.set(SanitizerKind::HWAddress, false);
804
805 if (SanitizeBounds && !SanOpts.hasOneOf(SanitizerKind::Bounds))
806 Fn->addFnAttr(llvm::Attribute::NoSanitizeBounds);
807
808 if (NoSanitizeCoverage && CGM.getCodeGenOpts().hasSanitizeCoverage())
809 Fn->addFnAttr(llvm::Attribute::NoSanitizeCoverage);
810
811 // Some passes need the non-negated no_sanitize attribute. Pass them on.
812 if (CGM.getCodeGenOpts().hasSanitizeBinaryMetadata()) {
813 if (no_sanitize_mask & SanitizerKind::Thread)
814 Fn->addFnAttr("no_sanitize_thread");
815 }
816 }
817
819 CurFn->addFnAttr(llvm::Attribute::DisableSanitizerInstrumentation);
820 } else {
821 // Apply sanitizer attributes to the function.
822 if (SanOpts.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress))
823 Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
824 if (SanOpts.hasOneOf(SanitizerKind::HWAddress |
825 SanitizerKind::KernelHWAddress))
826 Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
827 if (SanOpts.has(SanitizerKind::MemtagStack))
828 Fn->addFnAttr(llvm::Attribute::SanitizeMemTag);
829 if (SanOpts.has(SanitizerKind::Thread))
830 Fn->addFnAttr(llvm::Attribute::SanitizeThread);
831 if (SanOpts.has(SanitizerKind::Concurrency))
832 Fn->addFnAttr(llvm::Attribute::SanitizeConcurrency);
833 if (SanOpts.has(SanitizerKind::Type))
834 Fn->addFnAttr(llvm::Attribute::SanitizeType);
835 if (SanOpts.has(SanitizerKind::NumericalStability))
836 Fn->addFnAttr(llvm::Attribute::SanitizeNumericalStability);
837 if (SanOpts.hasOneOf(SanitizerKind::Memory | SanitizerKind::KernelMemory))
838 Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
839 if (SanOpts.has(SanitizerKind::AllocToken))
840 Fn->addFnAttr(llvm::Attribute::SanitizeAllocToken);
841 }
842 if (SanOpts.has(SanitizerKind::SafeStack))
843 Fn->addFnAttr(llvm::Attribute::SafeStack);
844 if (SanOpts.has(SanitizerKind::ShadowCallStack))
845 Fn->addFnAttr(llvm::Attribute::ShadowCallStack);
846
847 if (SanOpts.has(SanitizerKind::Realtime))
848 if (FD && FD->getASTContext().hasAnyFunctionEffects())
849 for (const FunctionEffectWithCondition &Fe : FD->getFunctionEffects()) {
850 if (Fe.Effect.kind() == FunctionEffect::Kind::NonBlocking)
851 Fn->addFnAttr(llvm::Attribute::SanitizeRealtime);
852 else if (Fe.Effect.kind() == FunctionEffect::Kind::Blocking)
853 Fn->addFnAttr(llvm::Attribute::SanitizeRealtimeBlocking);
854 }
855
856 // Apply fuzzing attribute to the function.
857 if (SanOpts.hasOneOf(SanitizerKind::Fuzzer | SanitizerKind::FuzzerNoLink))
858 Fn->addFnAttr(llvm::Attribute::OptForFuzzing);
859
860 // Ignore TSan/CSan memory accesses from within ObjC/ObjC++ dealloc,
861 // initialize, .cxx_destruct, __destroy_helper_block_ and all of their callees
862 // at run time.
863 if (SanOpts.hasOneOf(SanitizerKind::Thread | SanitizerKind::Concurrency)) {
864 if (const auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
865 const IdentifierInfo *II = OMD->getSelector().getIdentifierInfoForSlot(0);
866 if (OMD->getMethodFamily() == OMF_dealloc ||
867 OMD->getMethodFamily() == OMF_initialize ||
868 (OMD->getSelector().isUnarySelector() && II->isStr(".cxx_destruct"))) {
870 }
871 }
872 }
873
874 // Ignore unrelated casts in STL allocate() since the allocator must cast
875 // from void* to T* before object initialization completes. Don't match on the
876 // namespace because not all allocators are in std::
877 if (D && SanOpts.has(SanitizerKind::CFIUnrelatedCast)) {
879 SanOpts.Mask &= ~SanitizerKind::CFIUnrelatedCast;
880 }
881
882 // Ignore null checks in coroutine functions since the coroutines passes
883 // are not aware of how to move the extra UBSan instructions across the split
884 // coroutine boundaries.
885 if (D && SanOpts.has(SanitizerKind::Null))
886 if (FD && FD->getBody() &&
887 FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass)
889
890 // Apply xray attributes to the function (as a string, for now)
891 bool AlwaysXRayAttr = false;
892 if (const auto *XRayAttr = D ? D->getAttr<XRayInstrumentAttr>() : nullptr) {
893 if (CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
895 CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
897 if (XRayAttr->alwaysXRayInstrument() && ShouldXRayInstrumentFunction()) {
898 Fn->addFnAttr("function-instrument", "xray-always");
899 AlwaysXRayAttr = true;
900 }
901 if (XRayAttr->neverXRayInstrument())
902 Fn->addFnAttr("function-instrument", "xray-never");
903 if (const auto *LogArgs = D->getAttr<XRayLogArgsAttr>())
905 Fn->addFnAttr("xray-log-args",
906 llvm::utostr(LogArgs->getArgumentCount()));
907 }
908 } else {
909 if (ShouldXRayInstrumentFunction() && !CGM.imbueXRayAttrs(Fn, Loc))
910 Fn->addFnAttr(
911 "xray-instruction-threshold",
912 llvm::itostr(CGM.getCodeGenOpts().XRayInstructionThreshold));
913 }
914
916 if (CGM.getCodeGenOpts().XRayIgnoreLoops)
917 Fn->addFnAttr("xray-ignore-loops");
918
919 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
921 Fn->addFnAttr("xray-skip-exit");
922
923 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
925 Fn->addFnAttr("xray-skip-entry");
926
927 auto FuncGroups = CGM.getCodeGenOpts().XRayTotalFunctionGroups;
928 if (FuncGroups > 1) {
929 auto FuncName = llvm::ArrayRef<uint8_t>(CurFn->getName().bytes_begin(),
930 CurFn->getName().bytes_end());
931 auto Group = crc32(FuncName) % FuncGroups;
932 if (Group != CGM.getCodeGenOpts().XRaySelectedFunctionGroup &&
933 !AlwaysXRayAttr)
934 Fn->addFnAttr("function-instrument", "xray-never");
935 }
936 }
937
938 if (CGM.getCodeGenOpts().getProfileInstr() !=
939 llvm::driver::ProfileInstrKind::ProfileNone) {
940 switch (CGM.isFunctionBlockedFromProfileInstr(Fn, Loc)) {
942 Fn->addFnAttr(llvm::Attribute::SkipProfile);
943 break;
945 Fn->addFnAttr(llvm::Attribute::NoProfile);
946 break;
948 break;
949 }
950 }
951
952 unsigned Count, Offset;
953 StringRef Section;
954 if (const auto *Attr =
955 D ? D->getAttr<PatchableFunctionEntryAttr>() : nullptr) {
956 Count = Attr->getCount();
957 Offset = Attr->getOffset();
958 Section = Attr->getSection();
959 } else {
960 Count = CGM.getCodeGenOpts().PatchableFunctionEntryCount;
961 Offset = CGM.getCodeGenOpts().PatchableFunctionEntryOffset;
962 }
963 if (Section.empty())
964 Section = CGM.getCodeGenOpts().PatchableFunctionEntrySection;
965 if (Count && Offset <= Count) {
966 Fn->addFnAttr("patchable-function-entry", std::to_string(Count - Offset));
967 if (Offset)
968 Fn->addFnAttr("patchable-function-prefix", std::to_string(Offset));
969 if (!Section.empty())
970 Fn->addFnAttr("patchable-function-entry-section", Section);
971 }
972 // Instruct that functions for COFF/CodeView targets should start with a
973 // patchable instruction, but only on x86/x64. Don't forward this to ARM/ARM64
974 // backends as they don't need it -- instructions on these architectures are
975 // always atomically patchable at runtime.
976 if (CGM.getCodeGenOpts().HotPatch &&
977 getContext().getTargetInfo().getTriple().isX86() &&
978 getContext().getTargetInfo().getTriple().getEnvironment() !=
979 llvm::Triple::CODE16)
980 Fn->addFnAttr("patchable-function", "prologue-short-redirect");
981
982 // Add no-jump-tables value.
983 if (CGM.getCodeGenOpts().NoUseJumpTables)
984 Fn->addFnAttr("no-jump-tables", "true");
985
986 // Add no-inline-line-tables value.
987 if (CGM.getCodeGenOpts().NoInlineLineTables)
988 Fn->addFnAttr("no-inline-line-tables");
989
990 // Add profile-sample-accurate value.
991 if (CGM.getCodeGenOpts().ProfileSampleAccurate)
992 Fn->addFnAttr("profile-sample-accurate");
993
994 if (!CGM.getCodeGenOpts().SampleProfileFile.empty())
995 Fn->addFnAttr("use-sample-profile");
996
997 if (D && D->hasAttr<CFICanonicalJumpTableAttr>())
998 Fn->addFnAttr("cfi-canonical-jump-table");
999
1000 if (D && D->hasAttr<NoProfileFunctionAttr>())
1001 Fn->addFnAttr(llvm::Attribute::NoProfile);
1002
1003 if (D && D->hasAttr<HybridPatchableAttr>())
1004 Fn->addFnAttr(llvm::Attribute::HybridPatchable);
1005
1006 if (D) {
1007 // Function attributes take precedence over command line flags.
1008 if (auto *A = D->getAttr<FunctionReturnThunksAttr>()) {
1009 switch (A->getThunkType()) {
1010 case FunctionReturnThunksAttr::Kind::Keep:
1011 break;
1012 case FunctionReturnThunksAttr::Kind::Extern:
1013 Fn->addFnAttr(llvm::Attribute::FnRetThunkExtern);
1014 break;
1015 }
1016 } else if (CGM.getCodeGenOpts().FunctionReturnThunks)
1017 Fn->addFnAttr(llvm::Attribute::FnRetThunkExtern);
1018 }
1019
1020 if (FD && (getLangOpts().OpenCL ||
1021 (getLangOpts().CUDA &&
1022 getContext().getTargetInfo().getTriple().isSPIRV()) ||
1023 ((getLangOpts().HIP || getLangOpts().OffloadViaLLVM) &&
1024 getLangOpts().CUDAIsDevice))) {
1025 // Add metadata for a kernel function.
1026 EmitKernelMetadata(FD, Fn);
1027 }
1028
1029 if (FD && FD->hasAttr<ClspvLibclcBuiltinAttr>()) {
1030 Fn->setMetadata("clspv_libclc_builtin",
1031 llvm::MDNode::get(getLLVMContext(), {}));
1032 }
1033
1034 // If we are checking function types, emit a function type signature as
1035 // prologue data. Kernel functions have strict alignment requirements and
1036 // cannot be call indirectly so we do not instrument them.
1037 if (FD && SanOpts.has(SanitizerKind::Function) &&
1039 llvm::isCallableCC(Fn->getCallingConv())) {
1040 if (llvm::Constant *PrologueSig = getPrologueSignature(CGM, FD)) {
1041 llvm::LLVMContext &Ctx = Fn->getContext();
1042 llvm::MDBuilder MDB(Ctx);
1043 Fn->setMetadata(
1044 llvm::LLVMContext::MD_func_sanitize,
1045 MDB.createRTTIPointerPrologue(
1046 PrologueSig, getUBSanFunctionTypeHash(FD->getType())));
1047 }
1048 }
1049
1050 // If we're checking nullability, we need to know whether we can check the
1051 // return value. Initialize the flag to 'true' and refine it in EmitParmDecl.
1052 if (SanOpts.has(SanitizerKind::NullabilityReturn)) {
1053 auto Nullability = FnRetTy->getNullability();
1054 if (Nullability && *Nullability == NullabilityKind::NonNull &&
1055 !FnRetTy->isRecordType()) {
1056 if (!(SanOpts.has(SanitizerKind::ReturnsNonnullAttribute) &&
1057 CurCodeDecl && CurCodeDecl->getAttr<ReturnsNonNullAttr>()))
1058 RetValNullabilityPrecondition =
1059 llvm::ConstantInt::getTrue(getLLVMContext());
1060 }
1061 }
1062
1063 // Annotate C++ special member functions so that CopyProfPass can instrument
1064 // them, provided the object is at least as large as the size threshold.
1065 if (CGM.getCodeGenOpts().CopyProf) {
1066 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D)) {
1067 StringRef Attr;
1068 if (const auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
1069 if (!CD->isMoveConstructor())
1070 Attr =
1071 CD->isCopyConstructor() ? "copyprof-copy-ctor" : "copyprof-ctor";
1072 } else if (isa<CXXDestructorDecl>(MD)) {
1073 Attr = "copyprof-dtor";
1074 } else if (MD->isCopyAssignmentOperator()) {
1075 Attr = "copyprof-copy-assign-op";
1076 }
1077 if (!Attr.empty()) {
1078 // Finally, add the object size in bytes to the annotation.
1079 // A special member function always has an implicit object parameter, so
1080 // its type is guaranteed to be complete here.
1083 if (ObjSize.getQuantity() >=
1084 CGM.getCodeGenOpts().CopyProfStaticSizeThreshold)
1085 Fn->addFnAttr(Attr, llvm::utostr(ObjSize.getQuantity()));
1086 }
1087 }
1088 }
1089
1090 // If we're in C++ mode and the function name is "main", it is guaranteed
1091 // to be norecurse by the standard (3.6.1.3 "The function main shall not be
1092 // used within a program").
1093 //
1094 // OpenCL C 2.0 v2.2-11 s6.9.i:
1095 // Recursion is not supported.
1096 //
1097 // HLSL
1098 // Recursion is not supported.
1099 //
1100 // SYCL v1.2.1 s3.10:
1101 // kernels cannot include RTTI information, exception classes,
1102 // recursive code, virtual functions or make use of C++ libraries that
1103 // are not compiled for the device.
1104 if (FD &&
1105 ((getLangOpts().CPlusPlus && FD->isMain()) || getLangOpts().OpenCL ||
1106 getLangOpts().HLSL || getLangOpts().SYCLIsDevice ||
1107 (getLangOpts().CUDA && FD->hasAttr<CUDAGlobalAttr>())))
1108 Fn->addFnAttr(llvm::Attribute::NoRecurse);
1109
1110 llvm::RoundingMode RM = getLangOpts().getDefaultRoundingMode();
1111 llvm::fp::ExceptionBehavior FPExceptionBehavior =
1112 ToConstrainedExceptMD(getLangOpts().getDefaultExceptionMode());
1113 Builder.setDefaultConstrainedRounding(RM);
1114 Builder.setDefaultConstrainedExcept(FPExceptionBehavior);
1115 if ((FD && (FD->UsesFPIntrin() || FD->hasAttr<StrictFPAttr>())) ||
1116 (!FD && (FPExceptionBehavior != llvm::fp::ebIgnore ||
1117 RM != llvm::RoundingMode::NearestTiesToEven))) {
1118 Builder.setIsFPConstrained(true);
1119 Fn->addFnAttr(llvm::Attribute::StrictFP);
1120 }
1121
1122 // If a custom alignment is used, force realigning to this alignment on
1123 // any main function which certainly will need it.
1124 if (FD && ((FD->isMain() || FD->isMSVCRTEntryPoint()) &&
1125 CGM.getCodeGenOpts().StackAlignment))
1126 Fn->addFnAttr("stackrealign");
1127
1128 // "main" doesn't need to zero out call-used registers.
1129 if (FD && FD->isMain())
1130 Fn->removeFnAttr("zero-call-used-regs");
1131
1132 // Add vscale_range attribute if appropriate.
1133 llvm::StringMap<bool> FeatureMap;
1134 auto IsArmStreaming = TargetInfo::ArmStreamingKind::NotStreaming;
1135 if (FD) {
1136 getContext().getFunctionFeatureMap(FeatureMap, FD);
1137 if (const auto *T = FD->getType()->getAs<FunctionProtoType>())
1138 if (T->getAArch64SMEAttributes() &
1141
1142 if (IsArmStreamingFunction(FD, true))
1144 }
1145 std::optional<std::pair<unsigned, unsigned>> VScaleRange =
1146 getContext().getTargetInfo().getVScaleRange(getLangOpts(), IsArmStreaming,
1147 &FeatureMap);
1148 if (VScaleRange) {
1149 CurFn->addFnAttr(llvm::Attribute::getWithVScaleRangeArgs(
1150 getLLVMContext(), VScaleRange->first, VScaleRange->second));
1151 }
1152
1153 llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
1154
1155 // Create a marker to make it easy to insert allocas into the entryblock
1156 // later. Don't create this with the builder, because we don't want it
1157 // folded.
1158 llvm::Value *Poison = llvm::PoisonValue::get(Int32Ty);
1159 AllocaInsertPt = new llvm::BitCastInst(Poison, Int32Ty, "allocapt", EntryBB);
1160
1162
1163 Builder.SetInsertPoint(EntryBB);
1164
1165 // If we're checking the return value, allocate space for a pointer to a
1166 // precise source location of the checked return statement.
1167 if (requiresReturnValueCheck()) {
1168 ReturnLocation = CreateDefaultAlignTempAlloca(Int8PtrTy, "return.sloc.ptr");
1169 Builder.CreateStore(llvm::ConstantPointerNull::get(Int8PtrTy),
1170 ReturnLocation);
1171 }
1172
1173 // Emit subprogram debug descriptor.
1174 if (CGDebugInfo *DI = getDebugInfo()) {
1175 // Reconstruct the type from the argument list so that implicit parameters,
1176 // such as 'this' and 'vtt', show up in the debug info. Preserve the calling
1177 // convention.
1178 DI->emitFunctionStart(GD, Loc, StartLoc,
1179 DI->getFunctionType(FD, RetTy, Args), CurFn,
1181 }
1182
1184 if (CGM.getCodeGenOpts().InstrumentFunctions)
1185 CurFn->addFnAttr("instrument-function-entry", "__cyg_profile_func_enter");
1186 if (CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining)
1187 CurFn->addFnAttr("instrument-function-entry-inlined",
1188 "__cyg_profile_func_enter");
1189 if (CGM.getCodeGenOpts().InstrumentFunctionEntryBare)
1190 CurFn->addFnAttr("instrument-function-entry-inlined",
1191 "__cyg_profile_func_enter_bare");
1192 }
1193
1194 // Since emitting the mcount call here impacts optimizations such as function
1195 // inlining, we just add an attribute to insert a mcount call in backend.
1196 // The attribute "counting-function" is set to mcount function name which is
1197 // architecture dependent.
1198 if (CGM.getCodeGenOpts().InstrumentForProfiling) {
1199 // Calls to fentry/mcount should not be generated if function has
1200 // the no_instrument_function attribute.
1201 if (!CurFuncDecl || !CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) {
1202 if (CGM.getCodeGenOpts().CallFEntry)
1203 Fn->addFnAttr("fentry-call", "true");
1204 else {
1205 Fn->addFnAttr("instrument-function-entry-inlined",
1206 getTarget().getMCountName());
1207 }
1208 if (CGM.getCodeGenOpts().MNopMCount) {
1209 if (!CGM.getCodeGenOpts().CallFEntry)
1210 CGM.getDiags().Report(diag::err_opt_not_valid_without_opt)
1211 << "-mnop-mcount" << "-mfentry";
1212 Fn->addFnAttr("mnop-mcount");
1213 }
1214
1215 if (CGM.getCodeGenOpts().RecordMCount) {
1216 if (!CGM.getCodeGenOpts().CallFEntry)
1217 CGM.getDiags().Report(diag::err_opt_not_valid_without_opt)
1218 << "-mrecord-mcount" << "-mfentry";
1219 Fn->addFnAttr("mrecord-mcount");
1220 }
1221 }
1222 }
1223
1224 if (CGM.getCodeGenOpts().PackedStack) {
1225 if (getContext().getTargetInfo().getTriple().getArch() !=
1226 llvm::Triple::systemz)
1227 CGM.getDiags().Report(diag::err_opt_not_valid_on_target)
1228 << "-mpacked-stack";
1229 Fn->addFnAttr("packed-stack");
1230 }
1231
1232 if (!CGM.getCodeGenOpts().ZOSPPA1Name)
1233 Fn->addFnAttr("zos-ppa1-name", "");
1234 else if (getContext().getTargetInfo().getTriple().isOSzOS() && FD &&
1235 FD->hasAttr<AsmLabelAttr>()) {
1236 std::string SrcName = CGM.getMangledNameIgnoringAsmLabel(GD, FD);
1237 Fn->addFnAttr("zos-ppa1-name", SrcName);
1238 }
1239
1240 if (CGM.getCodeGenOpts().WarnStackSize != UINT_MAX &&
1241 !CGM.getDiags().isIgnored(diag::warn_fe_backend_frame_larger_than, Loc))
1242 Fn->addFnAttr("warn-stack-size",
1243 std::to_string(CGM.getCodeGenOpts().WarnStackSize));
1244
1245 if (RetTy->isVoidType()) {
1246 // Void type; nothing to return.
1248
1249 // Count the implicit return.
1250 if (!endsWithReturn(D))
1251 ++NumReturnExprs;
1252 } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect) {
1253 // Indirect return; emit returned value directly into sret slot.
1254 // This reduces code size, and affects correctness in C++.
1255 auto AI = CurFn->arg_begin();
1256 if (CurFnInfo->getReturnInfo().isSRetAfterThis())
1257 ++AI;
1259 &*AI, RetTy, CurFnInfo->getReturnInfo().getIndirectAlign(), false,
1260 nullptr, nullptr, KnownNonNull);
1261 if (!CurFnInfo->getReturnInfo().getIndirectByVal()) {
1263 CreateDefaultAlignTempAlloca(ReturnValue.getType(), "result.ptr");
1264 Builder.CreateStore(ReturnValue.emitRawPointer(*this),
1266 }
1267 } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::InAlloca &&
1268 !hasScalarEvaluationKind(CurFnInfo->getReturnType())) {
1269 // Load the sret pointer from the argument struct and return into that.
1270 unsigned Idx = CurFnInfo->getReturnInfo().getInAllocaFieldIndex();
1271 llvm::Function::arg_iterator EI = CurFn->arg_end();
1272 --EI;
1273 llvm::Value *Addr = Builder.CreateStructGEP(
1274 CurFnInfo->getArgStruct(), &*EI, Idx);
1275 llvm::Type *Ty =
1276 cast<llvm::GetElementPtrInst>(Addr)->getResultElementType();
1278 Addr = Builder.CreateAlignedLoad(Ty, Addr, getPointerAlign(), "agg.result");
1280 CGM.getNaturalTypeAlignment(RetTy), KnownNonNull);
1281 } else {
1282 ReturnValue = CreateIRTempWithoutCast(RetTy, "retval");
1283
1284 // Tell the epilog emitter to autorelease the result. We do this
1285 // now so that various specialized functions can suppress it
1286 // during their IR-generation.
1287 if (getLangOpts().ObjCAutoRefCount &&
1288 !CurFnInfo->isReturnsRetained() &&
1289 RetTy->isObjCRetainableType())
1290 AutoreleaseResult = true;
1291 }
1292
1294
1295 PrologueCleanupDepth = EHStack.stable_begin();
1296
1297 // Emit OpenMP specific initialization of the device functions.
1298 if (getLangOpts().OpenMP && CurCodeDecl)
1299 CGM.getOpenMPRuntime().emitFunctionProlog(*this, CurCodeDecl);
1300
1301 if (FD && getLangOpts().HLSL) {
1302 // Handle emitting HLSL entry functions.
1303 if (FD->hasAttr<HLSLShaderAttr>()) {
1304 CGM.getHLSLRuntime().emitEntryFunction(FD, Fn);
1305 }
1306 }
1307
1309
1310 if (const CXXMethodDecl *MD = dyn_cast_if_present<CXXMethodDecl>(D);
1311 MD && !MD->isStatic()) {
1312 bool IsInLambda =
1313 MD->getParent()->isLambda() && MD->getOverloadedOperator() == OO_Call;
1315 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
1316 if (IsInLambda) {
1317 // We're in a lambda; figure out the captures.
1321 // If the lambda captures the object referred to by '*this' - either by
1322 // value or by reference, make sure CXXThisValue points to the correct
1323 // object.
1324
1325 // Get the lvalue for the field (which is a copy of the enclosing object
1326 // or contains the address of the enclosing object).
1328 if (!LambdaThisCaptureField->getType()->isPointerType()) {
1329 // If the enclosing object was captured by value, just use its
1330 // address. Sign this pointer.
1331 CXXThisValue = ThisFieldLValue.getPointer(*this);
1332 } else {
1333 // Load the lvalue pointed to by the field, since '*this' was captured
1334 // by reference.
1335 CXXThisValue =
1336 EmitLoadOfLValue(ThisFieldLValue, SourceLocation()).getScalarVal();
1337 }
1338 }
1339 for (auto *FD : MD->getParent()->fields()) {
1340 if (FD->hasCapturedVLAType()) {
1341 auto *ExprArg = EmitLoadOfLValue(EmitLValueForLambdaField(FD),
1343 auto VAT = FD->getCapturedVLAType();
1344 VLASizeMap[VAT->getSizeExpr()] = ExprArg;
1345 }
1346 }
1347 } else if (MD->isImplicitObjectMemberFunction()) {
1348 // Not in a lambda; just use 'this' from the method.
1349 // FIXME: Should we generate a new load for each use of 'this'? The
1350 // fast register allocator would be happier...
1351 CXXThisValue = CXXABIThisValue;
1352 }
1353
1354 // Check the 'this' pointer once per function, if it's available.
1355 if (CXXABIThisValue) {
1356 SanitizerSet SkippedChecks;
1357 SkippedChecks.set(SanitizerKind::ObjectSize, true);
1358 QualType ThisTy = MD->getThisType();
1359
1360 // If this is the call operator of a lambda with no captures, it
1361 // may have a static invoker function, which may call this operator with
1362 // a null 'this' pointer.
1364 SkippedChecks.set(SanitizerKind::Null, true);
1365
1368 Loc, CXXABIThisValue, ThisTy, CXXABIThisAlignment, SkippedChecks);
1369 }
1370 }
1371
1372 // If any of the arguments have a variably modified type, make sure to
1373 // emit the type size, but only if the function is not naked. Naked functions
1374 // have no prolog to run this evaluation.
1375 if (!FD || !FD->hasAttr<NakedAttr>()) {
1376 for (const VarDecl *VD : Args) {
1377 // Dig out the type as written from ParmVarDecls; it's unclear whether
1378 // the standard (C99 6.9.1p10) requires this, but we're following the
1379 // precedent set by gcc.
1380 QualType Ty;
1381 if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD))
1382 Ty = PVD->getOriginalType();
1383 else
1384 Ty = VD->getType();
1385
1386 if (Ty->isVariablyModifiedType())
1388 }
1389 }
1390 // Emit a location at the end of the prologue.
1391 if (CGDebugInfo *DI = getDebugInfo())
1392 DI->EmitLocation(Builder, StartLoc);
1393 // TODO: Do we need to handle this in two places like we do with
1394 // target-features/target-cpu?
1395 if (CurFuncDecl)
1396 if (const auto *VecWidth = CurFuncDecl->getAttr<MinVectorWidthAttr>())
1397 LargestVectorWidth = VecWidth->getVectorWidth();
1398
1399 if (CGM.shouldEmitConvergenceTokens())
1400 ConvergenceTokenStack.push_back(getOrEmitConvergenceEntryToken(CurFn));
1401}
1402
1406 if (const CompoundStmt *S = dyn_cast<CompoundStmt>(Body))
1408 else
1409 EmitStmt(Body);
1410}
1411
1412/// When instrumenting to collect profile data, the counts for some blocks
1413/// such as switch cases need to not include the fall-through counts, so
1414/// emit a branch around the instrumentation code. When not instrumenting,
1415/// this just calls EmitBlock().
1417 const Stmt *S) {
1418 llvm::BasicBlock *SkipCountBB = nullptr;
1419 if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr()) {
1420 // When instrumenting for profiling, the fallthrough to certain
1421 // statements needs to skip over the instrumentation code so that we
1422 // get an accurate count.
1423 SkipCountBB = createBasicBlock("skipcount");
1424 EmitBranch(SkipCountBB);
1425 }
1426 EmitBlock(BB);
1427 uint64_t CurrentCount = getCurrentProfileCount();
1430 if (SkipCountBB)
1431 EmitBlock(SkipCountBB);
1432}
1433
1434/// Tries to mark the given function nounwind based on the
1435/// non-existence of any throwing calls within it. We believe this is
1436/// lightweight enough to do at -O0.
1437static void TryMarkNoThrow(llvm::Function *F) {
1438 // LLVM treats 'nounwind' on a function as part of the type, so we
1439 // can't do this on functions that can be overwritten.
1440 if (F->isInterposable()) return;
1441
1442 for (llvm::BasicBlock &BB : *F)
1443 for (llvm::Instruction &I : BB)
1444 if (I.mayThrow())
1445 return;
1446
1447 F->setDoesNotThrow();
1448}
1449
1451 FunctionArgList &Args) {
1452 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
1453 QualType ResTy = FD->getReturnType();
1454
1455 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
1456 if (MD && MD->isImplicitObjectMemberFunction()) {
1457 if (CGM.getCXXABI().HasThisReturn(GD))
1458 ResTy = MD->getThisType();
1459 else if (CGM.getCXXABI().hasMostDerivedReturn(GD))
1460 ResTy = CGM.getContext().VoidPtrTy;
1461 CGM.getCXXABI().buildThisParam(*this, Args);
1462 }
1463
1464 // The base version of an inheriting constructor whose constructed base is a
1465 // virtual base is not passed any arguments (because it doesn't actually call
1466 // the inherited constructor).
1467 bool PassedParams = true;
1468 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
1469 if (auto Inherited = CD->getInheritedConstructor())
1470 PassedParams =
1471 getTypes().inheritingCtorHasParams(Inherited, GD.getCtorType());
1472
1473 if (PassedParams) {
1474 for (auto *Param : FD->parameters()) {
1475 Args.push_back(Param);
1476 if (!Param->hasAttr<PassObjectSizeAttr>())
1477 continue;
1478
1480 getContext(), Param->getDeclContext(), Param->getLocation(),
1481 /*Id=*/nullptr, getContext().getSizeType(), ImplicitParamKind::Other);
1482 SizeArguments[Param] = Implicit;
1483 Args.push_back(Implicit);
1484 }
1485 }
1486
1487 if (MD && (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)))
1488 CGM.getCXXABI().addImplicitStructorParams(*this, ResTy, Args);
1489
1490 return ResTy;
1491}
1492
1493void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1494 const CGFunctionInfo &FnInfo) {
1495 assert(Fn && "generating code for null Function");
1496 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
1497 CurGD = GD;
1498
1499 FunctionArgList Args;
1500 QualType ResTy = BuildFunctionArgList(GD, Args);
1501
1502 CGM.getTargetCodeGenInfo().checkFunctionABI(CGM, FD);
1503
1504 if (FD->isInlineBuiltinDeclaration()) {
1505 // When generating code for a builtin with an inline declaration, use a
1506 // mangled name to hold the actual body, while keeping an external
1507 // definition in case the function pointer is referenced somewhere.
1508 std::string FDInlineName = (Fn->getName() + ".inline").str();
1509 llvm::Module *M = Fn->getParent();
1510 llvm::Function *Clone = M->getFunction(FDInlineName);
1511 if (!Clone) {
1512 Clone = llvm::Function::Create(Fn->getFunctionType(),
1513 llvm::GlobalValue::InternalLinkage,
1514 Fn->getAddressSpace(), FDInlineName, M);
1515 Clone->setCallingConv(Fn->getCallingConv());
1516 Clone->setAttributes(Fn->getAttributes());
1517 Clone->addFnAttr(llvm::Attribute::AlwaysInline);
1518 }
1519 Fn->setLinkage(llvm::GlobalValue::ExternalLinkage);
1520 Fn = Clone;
1521 } else {
1522 // Detect the unusual situation where an inline version is shadowed by a
1523 // non-inline version. In that case we should pick the external one
1524 // everywhere. That's GCC behavior too. Unfortunately, I cannot find a way
1525 // to detect that situation before we reach codegen, so do some late
1526 // replacement.
1527 for (const FunctionDecl *PD = FD->getPreviousDecl(); PD;
1528 PD = PD->getPreviousDecl()) {
1529 if (LLVM_UNLIKELY(PD->isInlineBuiltinDeclaration())) {
1530 std::string FDInlineName = (Fn->getName() + ".inline").str();
1531 llvm::Module *M = Fn->getParent();
1532 if (llvm::Function *Clone = M->getFunction(FDInlineName)) {
1533 Clone->replaceAllUsesWith(Fn);
1534 Clone->eraseFromParent();
1535 }
1536 break;
1537 }
1538 }
1539 }
1540
1541 // Check if we should generate debug info for this function.
1542 if (FD->hasAttr<NoDebugAttr>()) {
1543 // Clear non-distinct debug info that was possibly attached to the function
1544 // due to an earlier declaration without the nodebug attribute
1545 Fn->setSubprogram(nullptr);
1546 // Disable debug info indefinitely for this function
1547 DebugInfo = nullptr;
1548 }
1549 // Finalize function debug info on exit.
1550 llvm::scope_exit Cleanup([this] {
1551 if (CGDebugInfo *DI = getDebugInfo())
1552 DI->completeFunction();
1553 });
1554
1555 // The function might not have a body if we're generating thunks for a
1556 // function declaration.
1557 SourceRange BodyRange;
1558 if (Stmt *Body = FD->getBody())
1559 BodyRange = Body->getSourceRange();
1560 else
1561 BodyRange = FD->getLocation();
1562 CurEHLocation = BodyRange.getEnd();
1563
1564 // Use the location of the start of the function to determine where
1565 // the function definition is located. By default use the location
1566 // of the declaration as the location for the subprogram. A function
1567 // may lack a declaration in the source code if it is created by code
1568 // gen. (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
1569 SourceLocation Loc = FD->getLocation();
1570
1571 // If this is a function specialization then use the pattern body
1572 // as the location for the function.
1573 if (const FunctionDecl *SpecDecl = FD->getTemplateInstantiationPattern())
1574 if (SpecDecl->hasBody(SpecDecl))
1575 Loc = SpecDecl->getLocation();
1576
1577 Stmt *Body = FD->getBody();
1578
1579 if (Body) {
1580 // Coroutines always emit lifetime markers.
1581 if (isa<CoroutineBodyStmt>(Body))
1582 ShouldEmitLifetimeMarkers = true;
1583
1584 // Detect jumps that invalidate lifetime markers or bypass auto-var-init.
1585 bool NeedsBypassDetection =
1586 ShouldEmitLifetimeMarkers ||
1587 (CGM.getLangOpts().getTrivialAutoVarInit() !=
1589 if (NeedsBypassDetection)
1590 Bypasses.Init(CGM, Body);
1591 }
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 llvm::Value *TheCheck = nullptr;
2794 if (SanOpts.has(SanitizerKind::Alignment)) {
2795 llvm::Value *PtrIntValue =
2796 Builder.CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
2797
2798 if (OffsetValue) {
2799 bool IsOffsetZero = false;
2800 if (const auto *CI = dyn_cast<llvm::ConstantInt>(OffsetValue))
2801 IsOffsetZero = CI->isZero();
2802
2803 if (!IsOffsetZero)
2804 PtrIntValue = Builder.CreateSub(PtrIntValue, OffsetValue, "offsetptr");
2805 }
2806
2807 llvm::Value *Zero = llvm::ConstantInt::get(IntPtrTy, 0);
2808 llvm::Value *Mask =
2809 Builder.CreateSub(Alignment, llvm::ConstantInt::get(IntPtrTy, 1));
2810 llvm::Value *MaskedPtr = Builder.CreateAnd(PtrIntValue, Mask, "maskedptr");
2811 TheCheck = Builder.CreateICmpEQ(MaskedPtr, Zero, "maskcond");
2812 }
2813 llvm::Instruction *Assumption = Builder.CreateAlignmentAssumption(
2814 CGM.getDataLayout(), PtrValue, Alignment, OffsetValue);
2815
2816 if (!SanOpts.has(SanitizerKind::Alignment))
2817 return;
2818 emitAlignmentAssumptionCheck(PtrValue, Ty, Loc, AssumptionLoc, Alignment,
2819 OffsetValue, TheCheck, Assumption);
2820}
2821
2823 const Expr *E,
2824 SourceLocation AssumptionLoc,
2825 llvm::Value *Alignment,
2826 llvm::Value *OffsetValue) {
2827 QualType Ty = E->getType();
2828 SourceLocation Loc = E->getExprLoc();
2829
2830 emitAlignmentAssumption(PtrValue, Ty, Loc, AssumptionLoc, Alignment,
2831 OffsetValue);
2832}
2833
2834llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Function *AnnotationFn,
2835 llvm::Value *AnnotatedVal,
2836 StringRef AnnotationStr,
2837 SourceLocation Location,
2838 const AnnotateAttr *Attr) {
2840 AnnotatedVal,
2841 CGM.EmitAnnotationString(AnnotationStr),
2842 CGM.EmitAnnotationUnit(Location),
2843 CGM.EmitAnnotationLineNo(Location),
2844 };
2845 if (Attr)
2846 Args.push_back(CGM.EmitAnnotationArgs(Attr));
2847 return Builder.CreateCall(AnnotationFn, Args);
2848}
2849
2850void CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) {
2851 assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
2852 for (const auto *I : D->specific_attrs<AnnotateAttr>())
2853 EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation,
2854 {V->getType(), CGM.ConstGlobalsPtrTy}),
2855 V, I->getAnnotation(), D->getLocation(), I);
2856}
2857
2859 Address Addr) {
2860 assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
2861 llvm::Value *V = Addr.emitRawPointer(*this);
2862 llvm::Type *VTy = V->getType();
2863 auto *PTy = dyn_cast<llvm::PointerType>(VTy);
2864 unsigned AS = PTy ? PTy->getAddressSpace() : 0;
2865 llvm::PointerType *IntrinTy =
2866 llvm::PointerType::get(CGM.getLLVMContext(), AS);
2867 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
2868 {IntrinTy, CGM.ConstGlobalsPtrTy});
2869
2870 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2871 // FIXME Always emit the cast inst so we can differentiate between
2872 // annotation on the first field of a struct and annotation on the struct
2873 // itself.
2874 if (VTy != IntrinTy)
2875 V = Builder.CreateBitCast(V, IntrinTy);
2876 V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation(), I);
2877 V = Builder.CreateBitCast(V, VTy);
2878 }
2879
2880 return Address(V, Addr.getElementType(), Addr.getAlignment());
2881}
2882
2884
2886 : CGF(CGF) {
2887 assert(!CGF->IsSanitizerScope);
2888 CGF->IsSanitizerScope = true;
2889}
2890
2892 CGF->IsSanitizerScope = false;
2893}
2894
2895void CodeGenFunction::InsertHelper(llvm::Instruction *I,
2896 const llvm::Twine &Name,
2897 llvm::BasicBlock::iterator InsertPt) const {
2898 LoopStack.InsertHelper(I);
2899 if (IsSanitizerScope)
2900 I->setNoSanitizeMetadata();
2901}
2902
2904 llvm::Instruction *I, const llvm::Twine &Name,
2905 llvm::BasicBlock::iterator InsertPt) const {
2906 llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
2907 if (CGF)
2908 CGF->InsertHelper(I, Name, InsertPt);
2909}
2910
2911// Emits an error if we don't have a valid set of target features for the
2912// called function.
2914 const FunctionDecl *TargetDecl) {
2915 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl);
2916 CodeGenUtils::checkTargetFeatures(CGM.getContext(), CGM.getDiags(),
2917 getLangOpts(), E, FD, TargetDecl);
2918}
2919
2920// Emits an error if we don't have a valid set of target features for the
2921// called function.
2923 const FunctionDecl *TargetDecl) {
2924 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl);
2925 CodeGenUtils::checkTargetFeatures(CGM.getContext(), CGM.getDiags(),
2926 getLangOpts(), Loc, FD, TargetDecl);
2927}
2928
2929void CodeGenFunction::EmitSanitizerStatReport(llvm::SanitizerStatKind SSK) {
2930 if (!CGM.getCodeGenOpts().SanitizeStats)
2931 return;
2932
2933 llvm::IRBuilder<> IRB(Builder.GetInsertPoint());
2934 IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
2935 CGM.getSanStats().create(IRB, SSK);
2936}
2937
2939 const CGCallee &Callee, SmallVectorImpl<llvm::OperandBundleDef> &Bundles) {
2940 const CGCalleeInfo &CI = Callee.getAbstractInfo();
2942 if (!FP)
2943 return;
2944
2945 StringRef Salt;
2946 if (const auto &Info = FP->getExtraAttributeInfo())
2947 Salt = Info.CFISalt;
2948
2949 Bundles.emplace_back("kcfi", CGM.CreateKCFITypeId(FP->desugar(), Salt));
2950}
2951
2952llvm::Value *
2953CodeGenFunction::FormAArch64ResolverCondition(const FMVResolverOption &RO) {
2954 return RO.Features.empty() ? nullptr : EmitAArch64CpuSupports(RO.Features);
2955}
2956
2957llvm::Value *
2958CodeGenFunction::FormX86ResolverCondition(const FMVResolverOption &RO) {
2959 llvm::Value *Condition = nullptr;
2960
2961 if (RO.Architecture) {
2962 StringRef Arch = *RO.Architecture;
2963 // If arch= specifies an x86-64 micro-architecture level, test the feature
2964 // with __builtin_cpu_supports, otherwise use __builtin_cpu_is.
2965 if (Arch.starts_with("x86-64"))
2966 Condition = EmitX86CpuSupports({Arch});
2967 else
2968 Condition = EmitX86CpuIs(Arch);
2969 }
2970
2971 if (!RO.Features.empty()) {
2972 llvm::Value *FeatureCond = EmitX86CpuSupports(RO.Features);
2973 Condition =
2974 Condition ? Builder.CreateAnd(Condition, FeatureCond) : FeatureCond;
2975 }
2976 return Condition;
2977}
2978
2980 llvm::Function *Resolver,
2981 CGBuilderTy &Builder,
2982 llvm::Function *FuncToReturn,
2983 bool SupportsIFunc) {
2984 if (SupportsIFunc) {
2985 Builder.CreateRet(FuncToReturn);
2986 return;
2987 }
2988
2990 llvm::make_pointer_range(Resolver->args()));
2991
2992 llvm::CallInst *Result = Builder.CreateCall(FuncToReturn, Args);
2993 Result->setTailCallKind(llvm::CallInst::TCK_MustTail);
2994
2995 if (Resolver->getReturnType()->isVoidTy())
2996 Builder.CreateRetVoid();
2997 else
2998 Builder.CreateRet(Result);
2999}
3000
3002 llvm::Function *Resolver, ArrayRef<FMVResolverOption> Options) {
3003 llvm::SaveAndRestore<llvm::Function *> savedCurFn(CurFn, Resolver);
3004 llvm::Triple::ArchType ArchType =
3005 getContext().getTargetInfo().getTriple().getArch();
3006
3007 switch (ArchType) {
3008 case llvm::Triple::x86:
3009 case llvm::Triple::x86_64:
3010 EmitX86MultiVersionResolver(Resolver, Options);
3011 return;
3012 case llvm::Triple::aarch64:
3013 EmitAArch64MultiVersionResolver(Resolver, Options);
3014 return;
3015 case llvm::Triple::riscv32:
3016 case llvm::Triple::riscv64:
3017 case llvm::Triple::riscv32be:
3018 case llvm::Triple::riscv64be:
3019 EmitRISCVMultiVersionResolver(Resolver, Options);
3020 return;
3021 case llvm::Triple::ppc:
3022 case llvm::Triple::ppc64:
3023 if (getContext().getTargetInfo().getTriple().isOSAIX()) {
3024 EmitPPCAIXMultiVersionResolver(Resolver, Options);
3025 return;
3026 }
3027 [[fallthrough]];
3028 default:
3029 assert(false &&
3030 "Only implemented for x86, AArch64, RISC-V, and PowerPC AIX");
3031 }
3032}
3033
3034/**
3035 * define internal ptr @foo.resolver() {
3036 * entry:
3037 * %is_version_1 = __builtin_cpu_supports(version_1)
3038 * br i1 %1, label %if.version_1, label %if.else_2
3039 *
3040 * if.version_1:
3041 * ret ptr @foo.version_1
3042 *
3043 * if.else_2:
3044 * %is_version_2 = __builtin_cpu_supports(version_2)
3045 * ...
3046 * if.else: ; preds = %entry
3047 * ret ptr @foo.default
3048 * }
3049 */
3051 llvm::Function *Resolver, ArrayRef<FMVResolverOption> Options) {
3052
3053 // entry:
3054 llvm::BasicBlock *CurBlock = createBasicBlock("entry", Resolver);
3055
3056 for (const FMVResolverOption &RO : Options) {
3057 Builder.SetInsertPoint(CurBlock);
3058 // The 'default' or 'generic' case.
3059 if (!RO.Architecture && RO.Features.empty()) {
3060 // if.else:
3061 // ret ptr @foo.default
3062 assert(&RO == Options.end() - 1 &&
3063 "Default or Generic case must be last");
3064 Builder.CreateRet(RO.Function);
3065 return;
3066 }
3067 // if.else_n:
3068 // %is_version_n = __builtin_cpu_supports(version_n)
3069 // br i1 %is_version_n, label %if.version_n, label %if.else_n+1
3070 //
3071 // if.version_n:
3072 // ret ptr @foo_version_n
3073 assert(RO.Features.size() == 1 &&
3074 "for now one feature requirement per version");
3075
3076 StringRef FeatureStr = RO.Features[0];
3077 StringRef BuiltinCpuSupportsArg;
3078 bool IsNegated = false;
3079
3080 if (FeatureStr.starts_with("cpu=")) {
3081 // CPU specification - map to ISA level
3082 StringRef CPU = FeatureStr.split("=").second.trim();
3083 BuiltinCpuSupportsArg = llvm::StringSwitch<StringRef>(CPU)
3084#define PPC_AIX_CLONES_CPU(CPU_NAME, AIX_BUILTIN_CPU_SUPPORTS_NAME, _) \
3085 .Case(CPU_NAME, AIX_BUILTIN_CPU_SUPPORTS_NAME)
3086#include "llvm/TargetParser/PPCTargetParser.def"
3087 .Default("error");
3088 } else {
3089 // Feature strings arrive here already normalized:
3090 // - Positive features: just the name (e.g., "altivec")
3091 // - Negated features: "no-" prefix (e.g., "no-altivec")
3092 if (FeatureStr.starts_with("no-")) {
3093 IsNegated = true;
3094 FeatureStr = FeatureStr.drop_front(3);
3095 }
3096
3097 // Map feature names to __builtin_cpu_supports() strings
3098 BuiltinCpuSupportsArg =
3099 llvm::StringSwitch<StringRef>(FeatureStr)
3100#define PPC_AIX_CLONES_FEATURE(FEATURE_NAME, AIX_BUILTIN_CPU_SUPPORTS_NAME, _, \
3101 __) \
3102 .Case(FEATURE_NAME, AIX_BUILTIN_CPU_SUPPORTS_NAME)
3103#include "llvm/TargetParser/PPCTargetParser.def"
3104 // Features without runtime checks return empty string
3105 .Default("");
3106
3107 // All features in target_clones must have runtime detection
3108 assert(!BuiltinCpuSupportsArg.empty() &&
3109 "Feature without runtime detection should have been rejected in "
3110 "Sema");
3111 }
3112
3113 assert(getContext().getTargetInfo().validateCpuSupports(
3114 BuiltinCpuSupportsArg));
3115
3116 llvm::Value *Condition =
3117 EmitPPCBuiltinCpu(Builtin::BI__builtin_cpu_supports,
3118 Builder.getInt1Ty(), BuiltinCpuSupportsArg);
3119
3120 // Negate the condition if this is a negated feature
3121 if (IsNegated) {
3122 Condition = Builder.CreateNot(Condition, "neg");
3123 }
3124
3125 llvm::BasicBlock *ThenBlock = createBasicBlock("if.version", Resolver);
3126 CurBlock = createBasicBlock("if.else", Resolver);
3127 Builder.CreateCondBr(Condition, ThenBlock, CurBlock);
3128
3129 Builder.SetInsertPoint(ThenBlock);
3130 Builder.CreateRet(RO.Function);
3131 }
3132
3133 llvm_unreachable("Default case missing");
3134}
3135
3137 llvm::Function *Resolver, ArrayRef<FMVResolverOption> Options) {
3138
3139 if (getContext().getTargetInfo().getTriple().getOS() !=
3140 llvm::Triple::OSType::Linux) {
3141 CGM.getDiags().Report(diag::err_os_unsupport_riscv_fmv);
3142 return;
3143 }
3144
3145 llvm::BasicBlock *CurBlock = createBasicBlock("resolver_entry", Resolver);
3146 Builder.SetInsertPoint(CurBlock);
3148
3149 bool SupportsIFunc = getContext().getTargetInfo().supportsIFunc();
3150 bool HasDefault = false;
3151 unsigned DefaultIndex = 0;
3152
3153 // Check the each candidate function.
3154 for (unsigned Index = 0; Index < Options.size(); Index++) {
3155
3156 if (Options[Index].Features.empty()) {
3157 HasDefault = true;
3158 DefaultIndex = Index;
3159 continue;
3160 }
3161
3162 Builder.SetInsertPoint(CurBlock);
3163
3164 // FeaturesCondition: The bitmask of the required extension has been
3165 // enabled by the runtime object.
3166 // (__riscv_feature_bits.features[i] & REQUIRED_BITMASK) ==
3167 // REQUIRED_BITMASK
3168 //
3169 // When condition is met, return this version of the function.
3170 // Otherwise, try the next version.
3171 //
3172 // if (FeaturesConditionVersion1)
3173 // return Version1;
3174 // else if (FeaturesConditionVersion2)
3175 // return Version2;
3176 // else if (FeaturesConditionVersion3)
3177 // return Version3;
3178 // ...
3179 // else
3180 // return DefaultVersion;
3181
3182 // TODO: Add a condition to check the length before accessing elements.
3183 // Without checking the length first, we may access an incorrect memory
3184 // address when using different versions.
3185 llvm::SmallVector<StringRef, 8> CurrTargetAttrFeats;
3186 llvm::SmallVector<std::string, 8> TargetAttrFeats;
3187
3188 for (StringRef Feat : Options[Index].Features) {
3189 std::vector<std::string> FeatStr =
3191
3192 assert(FeatStr.size() == 1 && "Feature string not delimited");
3193
3194 std::string &CurrFeat = FeatStr.front();
3195 if (CurrFeat[0] == '+')
3196 TargetAttrFeats.push_back(CurrFeat.substr(1));
3197 }
3198
3199 if (TargetAttrFeats.empty())
3200 continue;
3201
3202 for (std::string &Feat : TargetAttrFeats)
3203 CurrTargetAttrFeats.push_back(Feat);
3204
3205 Builder.SetInsertPoint(CurBlock);
3206 llvm::Value *FeatsCondition = EmitRISCVCpuSupports(CurrTargetAttrFeats);
3207
3208 llvm::BasicBlock *RetBlock = createBasicBlock("resolver_return", Resolver);
3209 CGBuilderTy RetBuilder(CGM, RetBlock);
3210 CreateMultiVersionResolverReturn(CGM, Resolver, RetBuilder,
3211 Options[Index].Function, SupportsIFunc);
3212 llvm::BasicBlock *ElseBlock = createBasicBlock("resolver_else", Resolver);
3213
3214 Builder.SetInsertPoint(CurBlock);
3215 Builder.CreateCondBr(FeatsCondition, RetBlock, ElseBlock);
3216
3217 CurBlock = ElseBlock;
3218 }
3219
3220 // Finally, emit the default one.
3221 if (HasDefault) {
3222 Builder.SetInsertPoint(CurBlock);
3224 CGM, Resolver, Builder, Options[DefaultIndex].Function, SupportsIFunc);
3225 return;
3226 }
3227
3228 // If no generic/default, emit an unreachable.
3229 Builder.SetInsertPoint(CurBlock);
3231}
3232
3234 llvm::Function *Resolver, ArrayRef<FMVResolverOption> Options) {
3235 assert(!Options.empty() && "No multiversion resolver options found");
3236 assert(Options.back().Features.size() == 0 && "Default case must be last");
3237 bool SupportsIFunc = getContext().getTargetInfo().supportsIFunc();
3238 assert(SupportsIFunc &&
3239 "Multiversion resolver requires target IFUNC support");
3240 bool AArch64CpuInitialized = false;
3241 llvm::BasicBlock *CurBlock = createBasicBlock("resolver_entry", Resolver);
3242
3243 for (const FMVResolverOption &RO : Options) {
3244 // Skip unreachable versions.
3245 if (RO.Function == nullptr)
3246 continue;
3247
3248 Builder.SetInsertPoint(CurBlock);
3249 llvm::Value *Condition = FormAArch64ResolverCondition(RO);
3250
3251 // The 'default' or 'all features enabled' case.
3252 if (!Condition) {
3253 CreateMultiVersionResolverReturn(CGM, Resolver, Builder, RO.Function,
3254 SupportsIFunc);
3255 return;
3256 }
3257
3258 if (!AArch64CpuInitialized) {
3259 Builder.SetInsertPoint(CurBlock->begin());
3260 EmitAArch64CpuInit();
3261 AArch64CpuInitialized = true;
3262 Builder.SetInsertPoint(CurBlock);
3263 }
3264
3265 llvm::BasicBlock *RetBlock = createBasicBlock("resolver_return", Resolver);
3266 CGBuilderTy RetBuilder(CGM, RetBlock);
3267 CreateMultiVersionResolverReturn(CGM, Resolver, RetBuilder, RO.Function,
3268 SupportsIFunc);
3269 CurBlock = createBasicBlock("resolver_else", Resolver);
3270 Builder.CreateCondBr(Condition, RetBlock, CurBlock);
3271 }
3272
3273 // If no default, emit an unreachable.
3274 Builder.SetInsertPoint(CurBlock);
3276}
3277
3279 llvm::Function *Resolver, ArrayRef<FMVResolverOption> Options) {
3280
3281 bool SupportsIFunc = getContext().getTargetInfo().supportsIFunc();
3282
3283 // Main function's basic block.
3284 llvm::BasicBlock *CurBlock = createBasicBlock("resolver_entry", Resolver);
3285 Builder.SetInsertPoint(CurBlock);
3286 EmitX86CpuInit();
3287
3288 for (const FMVResolverOption &RO : Options) {
3289 Builder.SetInsertPoint(CurBlock);
3290 llvm::Value *Condition = FormX86ResolverCondition(RO);
3291
3292 // The 'default' or 'generic' case.
3293 if (!Condition) {
3294 assert(&RO == Options.end() - 1 &&
3295 "Default or Generic case must be last");
3296 CreateMultiVersionResolverReturn(CGM, Resolver, Builder, RO.Function,
3297 SupportsIFunc);
3298 return;
3299 }
3300
3301 llvm::BasicBlock *RetBlock = createBasicBlock("resolver_return", Resolver);
3302 CGBuilderTy RetBuilder(CGM, RetBlock);
3303 CreateMultiVersionResolverReturn(CGM, Resolver, RetBuilder, RO.Function,
3304 SupportsIFunc);
3305 CurBlock = createBasicBlock("resolver_else", Resolver);
3306 Builder.CreateCondBr(Condition, RetBlock, CurBlock);
3307 }
3308
3309 // If no generic/default, emit an unreachable.
3310 Builder.SetInsertPoint(CurBlock);
3312}
3313
3314// Loc - where the diagnostic will point, where in the source code this
3315// alignment has failed.
3316// SecondaryLoc - if present (will be present if sufficiently different from
3317// Loc), the diagnostic will additionally point a "Note:" to this location.
3318// It should be the location where the __attribute__((assume_aligned))
3319// was written e.g.
3321 llvm::Value *Ptr, QualType Ty, SourceLocation Loc,
3322 SourceLocation SecondaryLoc, llvm::Value *Alignment,
3323 llvm::Value *OffsetValue, llvm::Value *TheCheck,
3324 llvm::Instruction *Assumption) {
3325 assert(isa_and_nonnull<llvm::CallInst>(Assumption) &&
3326 cast<llvm::CallInst>(Assumption)->getCalledOperand() ==
3327 llvm::Intrinsic::getOrInsertDeclaration(Builder.getModule(),
3328 llvm::Intrinsic::assume) &&
3329 "Assumption should be a call to llvm.assume().");
3330 assert(&(Builder.GetInsertBlock()->back()) == Assumption &&
3331 "Assumption should be the last instruction of the basic block, "
3332 "since the basic block is still being generated.");
3333
3334 if (!SanOpts.has(SanitizerKind::Alignment))
3335 return;
3336
3337 // Don't check pointers to volatile data. The behavior here is implementation-
3338 // defined.
3340 return;
3341
3342 // We need to temorairly remove the assumption so we can insert the
3343 // sanitizer check before it, else the check will be dropped by optimizations.
3344 Assumption->removeFromParent();
3345
3346 {
3347 auto CheckOrdinal = SanitizerKind::SO_Alignment;
3348 auto CheckHandler = SanitizerHandler::AlignmentAssumption;
3349 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
3350
3351 if (!OffsetValue)
3352 OffsetValue = Builder.getInt1(false); // no offset.
3353
3354 llvm::Constant *StaticData[] = {EmitCheckSourceLocation(Loc),
3355 EmitCheckSourceLocation(SecondaryLoc),
3357 llvm::Value *DynamicData[] = {Ptr, Alignment, OffsetValue};
3358 EmitCheck({std::make_pair(TheCheck, CheckOrdinal)}, CheckHandler,
3359 StaticData, DynamicData);
3360 }
3361
3362 // We are now in the (new, empty) "cont" basic block.
3363 // Reintroduce the assumption.
3364 Builder.Insert(Assumption);
3365 // FIXME: Assumption still has it's original basic block as it's Parent.
3366}
3367
3369 if (CGDebugInfo *DI = getDebugInfo())
3370 return DI->SourceLocToDebugLoc(Location);
3371
3372 return llvm::DebugLoc();
3373}
3374
3375llvm::Value *
3376CodeGenFunction::emitCondLikelihoodViaExpectIntrinsic(llvm::Value *Cond,
3377 Stmt::Likelihood LH) {
3378 switch (LH) {
3379 case Stmt::LH_None:
3380 return Cond;
3381 case Stmt::LH_Likely:
3382 case Stmt::LH_Unlikely:
3383 // Don't generate llvm.expect on -O0 as the backend won't use it for
3384 // anything.
3385 if (CGM.getCodeGenOpts().OptimizationLevel == 0)
3386 return Cond;
3387 llvm::Type *CondTy = Cond->getType();
3388 assert(CondTy->isIntegerTy(1) && "expecting condition to be a boolean");
3389 llvm::Function *FnExpect =
3390 CGM.getIntrinsic(llvm::Intrinsic::expect, CondTy);
3391 llvm::Value *ExpectedValueOfCond =
3392 llvm::ConstantInt::getBool(CondTy, LH == Stmt::LH_Likely);
3393 return Builder.CreateCall(FnExpect, {Cond, ExpectedValueOfCond},
3394 Cond->getName() + ".expval");
3395 }
3396 llvm_unreachable("Unknown Likelihood");
3397}
3398
3399llvm::Value *CodeGenFunction::emitBoolVecConversion(llvm::Value *SrcVec,
3400 unsigned NumElementsDst,
3401 const llvm::Twine &Name) {
3402 auto *SrcTy = cast<llvm::FixedVectorType>(SrcVec->getType());
3403 unsigned NumElementsSrc = SrcTy->getNumElements();
3404 if (NumElementsSrc == NumElementsDst)
3405 return SrcVec;
3406
3407 std::vector<int> ShuffleMask(NumElementsDst, -1);
3408 for (unsigned MaskIdx = 0;
3409 MaskIdx < std::min<>(NumElementsDst, NumElementsSrc); ++MaskIdx)
3410 ShuffleMask[MaskIdx] = MaskIdx;
3411
3412 return Builder.CreateShuffleVector(SrcVec, ShuffleMask, Name);
3413}
3414
3416 const CGPointerAuthInfo &PointerAuth,
3418 if (!PointerAuth.isSigned())
3419 return;
3420
3421 auto *Key = Builder.getInt32(PointerAuth.getKey());
3422
3423 llvm::Value *Discriminator = PointerAuth.getDiscriminator();
3424 if (!Discriminator)
3425 Discriminator = Builder.getSize(0);
3426
3427 llvm::Value *Args[] = {Key, Discriminator};
3428 Bundles.emplace_back("ptrauth", Args);
3429}
3430
3432 const CGPointerAuthInfo &PointerAuth,
3433 llvm::Value *Pointer,
3434 unsigned IntrinsicID) {
3435 if (!PointerAuth)
3436 return Pointer;
3437
3438 auto Key = CGF.Builder.getInt32(PointerAuth.getKey());
3439
3440 llvm::Value *Discriminator = PointerAuth.getDiscriminator();
3441 if (!Discriminator) {
3442 Discriminator = CGF.Builder.getSize(0);
3443 }
3444
3445 // Convert the pointer to intptr_t before signing it.
3446 auto OrigType = Pointer->getType();
3447 Pointer = CGF.Builder.CreatePtrToInt(Pointer, CGF.IntPtrTy);
3448
3449 // call i64 @llvm.ptrauth.sign.i64(i64 %pointer, i32 %key, i64 %discriminator)
3450 auto Intrinsic = CGF.CGM.getIntrinsic(IntrinsicID);
3451 Pointer = CGF.EmitRuntimeCall(Intrinsic, {Pointer, Key, Discriminator});
3452
3453 // Convert back to the original type.
3454 Pointer = CGF.Builder.CreateIntToPtr(Pointer, OrigType);
3455 return Pointer;
3456}
3457
3458llvm::Value *
3460 llvm::Value *Pointer) {
3461 if (!PointerAuth.shouldSign())
3462 return Pointer;
3463 return EmitPointerAuthCommon(*this, PointerAuth, Pointer,
3464 llvm::Intrinsic::ptrauth_sign);
3465}
3466
3467static llvm::Value *EmitStrip(CodeGenFunction &CGF,
3468 const CGPointerAuthInfo &PointerAuth,
3469 llvm::Value *Pointer) {
3470 auto StripIntrinsic = CGF.CGM.getIntrinsic(llvm::Intrinsic::ptrauth_strip);
3471
3472 auto Key = CGF.Builder.getInt32(PointerAuth.getKey());
3473 // Convert the pointer to intptr_t before signing it.
3474 auto OrigType = Pointer->getType();
3476 StripIntrinsic, {CGF.Builder.CreatePtrToInt(Pointer, CGF.IntPtrTy), Key});
3477 return CGF.Builder.CreateIntToPtr(Pointer, OrigType);
3478}
3479
3480llvm::Value *
3482 llvm::Value *Pointer) {
3483 if (PointerAuth.shouldStrip()) {
3484 return EmitStrip(*this, PointerAuth, Pointer);
3485 }
3486 if (!PointerAuth.shouldAuth()) {
3487 return Pointer;
3488 }
3489
3490 return EmitPointerAuthCommon(*this, PointerAuth, Pointer,
3491 llvm::Intrinsic::ptrauth_auth);
3492}
3493
3495 llvm::Instruction *KeyInstruction, llvm::Value *Backup) {
3496 if (CGDebugInfo *DI = getDebugInfo())
3497 DI->addInstToCurrentSourceAtom(KeyInstruction, Backup);
3498}
3499
3501 llvm::Instruction *KeyInstruction, llvm::Value *Backup, uint64_t Atom) {
3502 if (CGDebugInfo *DI = getDebugInfo())
3503 DI->addInstToSpecificSourceAtom(KeyInstruction, Backup, Atom);
3504}
3505
3506void CodeGenFunction::addInstToNewSourceAtom(llvm::Instruction *KeyInstruction,
3507 llvm::Value *Backup) {
3508 if (CGDebugInfo *DI = getDebugInfo()) {
3510 DI->addInstToCurrentSourceAtom(KeyInstruction, Backup);
3511 }
3512}
3513
3515 QualType Ty) {
3516 for (auto &Field : getContext().findPFPFields(Ty)) {
3517 if (getContext().arePFPFieldsTriviallyCopyable(Field.Field->getParent()))
3518 continue;
3519 auto DestFieldPtr = EmitAddressOfPFPField(DestPtr, Field);
3520 auto SrcFieldPtr = EmitAddressOfPFPField(SrcPtr, Field);
3521 Builder.CreateStore(Builder.CreateLoad(SrcFieldPtr), DestFieldPtr);
3522 }
3523}
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
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:971
void getFunctionFeatureMap(llvm::StringMap< bool > &FeatureMap, const FunctionDecl *) const
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:59
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:4732
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:3104
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:7374
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:4169
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 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:4317
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.
void emitAlignmentAssumptionCheck(llvm::Value *Ptr, QualType Ty, SourceLocation Loc, SourceLocation AssumptionLoc, llvm::Value *Alignment, llvm::Value *OffsetValue, llvm::Value *TheCheck, llvm::Instruction *Assumption)
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:5703
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...
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:3503
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:3161
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:5871
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:4400
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:4755
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:2153
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
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:3284
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition Decl.cpp:3835
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:4339
FunctionEffectsRef getFunctionEffects() const
Definition Decl.h:3315
bool isMSVCRTEntryPoint() const
Determines whether this function is a MSVCRT user defined entry point.
Definition Decl.cpp:3448
bool isInlineBuiltinDeclaration() const
Determine if this function provides an inline implementation of a builtin.
Definition Decl.cpp:3599
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:3441
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:4205
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:5738
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:4711
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.
virtual std::optional< std::pair< unsigned, unsigned > > getVScaleRange(const LangOptions &LangOpts, ArmStreamingKind Mode, llvm::StringMap< bool > *FeatureMap=nullptr) const
Returns target-specific min and max values VScale_Range.
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:2392
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:5609
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:6242
@ Other
Other implicit parameter.
Definition Decl.h:1787
@ EST_None
no exception specification
@ Implicit
An implicit conversion.
Definition Sema.h:433
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