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