clang 23.0.0git
CGBuiltin.cpp
Go to the documentation of this file.
1//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code to emit Builtin calls as LLVM code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CGBuiltin.h"
14#include "ABIInfo.h"
15#include "CGCUDARuntime.h"
16#include "CGCXXABI.h"
17#include "CGDebugInfo.h"
18#include "CGObjCRuntime.h"
19#include "CGOpenCLRuntime.h"
20#include "CGRecordLayout.h"
21#include "CGValue.h"
22#include "CodeGenFunction.h"
23#include "CodeGenModule.h"
24#include "ConstantEmitter.h"
25#include "PatternInit.h"
26#include "TargetInfo.h"
27#include "clang/AST/OSLog.h"
31#include "llvm/ADT/APFloat.h"
32#include "llvm/IR/InlineAsm.h"
33#include "llvm/IR/Instruction.h"
34#include "llvm/IR/Intrinsics.h"
35#include "llvm/IR/IntrinsicsX86.h"
36#include "llvm/IR/MatrixBuilder.h"
37#include "llvm/Support/ConvertUTF.h"
38#include "llvm/Support/ScopedPrinter.h"
39#include <algorithm>
40#include <optional>
41#include <utility>
42
43using namespace clang;
44using namespace CodeGen;
45using namespace llvm;
46
47/// Some builtins do not have library implementation on some targets and
48/// are instead emitted as LLVM IRs by some target builtin emitters.
49/// FIXME: Remove this when library support is added
50static bool shouldEmitBuiltinAsIR(unsigned BuiltinID,
51 const Builtin::Context &BI,
52 const CodeGenFunction &CGF) {
53 if (!CGF.CGM.getLangOpts().MathErrno &&
57 switch (BuiltinID) {
58 default:
59 return false;
60 case Builtin::BIlogbf:
61 case Builtin::BI__builtin_logbf:
62 case Builtin::BIlogb:
63 case Builtin::BI__builtin_logb:
64 case Builtin::BIscalbnf:
65 case Builtin::BI__builtin_scalbnf:
66 case Builtin::BIscalbn:
67 case Builtin::BI__builtin_scalbn:
68 return true;
69 }
70 }
71 return false;
72}
73
75 unsigned BuiltinID, const CallExpr *E,
76 ReturnValueSlot ReturnValue,
77 llvm::Triple::ArchType Arch) {
78 // When compiling in HipStdPar mode we have to be conservative in rejecting
79 // target specific features in the FE, and defer the possible error to the
80 // AcceleratorCodeSelection pass, wherein iff an unsupported target builtin is
81 // referenced by an accelerator executable function, we emit an error.
82 // Returning nullptr here leads to the builtin being handled in
83 // EmitStdParUnsupportedBuiltin.
84 if (CGF->getLangOpts().HIPStdPar && CGF->getLangOpts().CUDAIsDevice &&
85 Arch != CGF->getTarget().getTriple().getArch())
86 return nullptr;
87
88 switch (Arch) {
89 case llvm::Triple::arm:
90 case llvm::Triple::armeb:
91 case llvm::Triple::thumb:
92 case llvm::Triple::thumbeb:
93 return CGF->EmitARMBuiltinExpr(BuiltinID, E, ReturnValue, Arch);
94 case llvm::Triple::aarch64:
95 case llvm::Triple::aarch64_32:
96 case llvm::Triple::aarch64_be:
97 return CGF->EmitAArch64BuiltinExpr(BuiltinID, E, Arch);
98 case llvm::Triple::bpfeb:
99 case llvm::Triple::bpfel:
100 return CGF->EmitBPFBuiltinExpr(BuiltinID, E);
101 case llvm::Triple::dxil:
102 return CGF->EmitDirectXBuiltinExpr(BuiltinID, E);
103 case llvm::Triple::x86:
104 case llvm::Triple::x86_64:
105 return CGF->EmitX86BuiltinExpr(BuiltinID, E);
106 case llvm::Triple::ppc:
107 case llvm::Triple::ppcle:
108 case llvm::Triple::ppc64:
109 case llvm::Triple::ppc64le:
110 return CGF->EmitPPCBuiltinExpr(BuiltinID, E);
111 case llvm::Triple::r600:
112 case llvm::Triple::amdgcn:
113 return CGF->EmitAMDGPUBuiltinExpr(BuiltinID, E);
114 case llvm::Triple::systemz:
115 return CGF->EmitSystemZBuiltinExpr(BuiltinID, E);
116 case llvm::Triple::nvptx:
117 case llvm::Triple::nvptx64:
118 return CGF->EmitNVPTXBuiltinExpr(BuiltinID, E);
119 case llvm::Triple::wasm32:
120 case llvm::Triple::wasm64:
121 return CGF->EmitWebAssemblyBuiltinExpr(BuiltinID, E);
122 case llvm::Triple::hexagon:
123 return CGF->EmitHexagonBuiltinExpr(BuiltinID, E);
124 case llvm::Triple::riscv32:
125 case llvm::Triple::riscv64:
126 case llvm::Triple::riscv32be:
127 case llvm::Triple::riscv64be:
128 return CGF->EmitRISCVBuiltinExpr(BuiltinID, E, ReturnValue);
129 case llvm::Triple::spirv32:
130 case llvm::Triple::spirv64:
131 if (CGF->getTarget().getTriple().getOS() == llvm::Triple::OSType::AMDHSA)
132 return CGF->EmitAMDGPUBuiltinExpr(BuiltinID, E);
133 [[fallthrough]];
134 case llvm::Triple::spirv:
135 return CGF->EmitSPIRVBuiltinExpr(BuiltinID, E);
136 default:
137 return nullptr;
138 }
139}
140
142 const CallExpr *E,
144 if (getContext().BuiltinInfo.isAuxBuiltinID(BuiltinID)) {
145 assert(getContext().getAuxTargetInfo() && "Missing aux target info");
147 this, getContext().BuiltinInfo.getAuxBuiltinID(BuiltinID), E,
148 ReturnValue, getContext().getAuxTargetInfo()->getTriple().getArch());
149 }
150
151 return EmitTargetArchBuiltinExpr(this, BuiltinID, E, ReturnValue,
152 getTarget().getTriple().getArch());
153}
154
155static void initializeAlloca(CodeGenFunction &CGF, AllocaInst *AI, Value *Size,
156 Align AlignmentInBytes) {
157 ConstantInt *Byte;
158 switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
160 // Nothing to initialize.
161 return;
163 Byte = CGF.Builder.getInt8(0x00);
164 break;
166 llvm::Type *Int8 = llvm::IntegerType::getInt8Ty(CGF.CGM.getLLVMContext());
167 Byte = llvm::dyn_cast<llvm::ConstantInt>(
168 initializationPatternFor(CGF.CGM, Int8));
169 break;
170 }
171 }
172 if (CGF.CGM.stopAutoInit())
173 return;
174 auto *I = CGF.Builder.CreateMemSet(AI, Byte, Size, AlignmentInBytes);
175 I->addAnnotationMetadata("auto-init");
176}
177
178/// getBuiltinLibFunction - Given a builtin id for a function like
179/// "__builtin_fabsf", return a Function* for "fabsf".
181 unsigned BuiltinID) {
182 assert(Context.BuiltinInfo.isLibFunction(BuiltinID));
183
184 // Get the name, skip over the __builtin_ prefix (if necessary). We may have
185 // to build this up so provide a small stack buffer to handle the vast
186 // majority of names.
188 GlobalDecl D(FD);
189
190 // TODO: This list should be expanded or refactored after all GCC-compatible
191 // std libcall builtins are implemented.
192 static const SmallDenseMap<unsigned, StringRef, 64> F128Builtins{
193 {Builtin::BI__builtin___fprintf_chk, "__fprintf_chkieee128"},
194 {Builtin::BI__builtin___printf_chk, "__printf_chkieee128"},
195 {Builtin::BI__builtin___snprintf_chk, "__snprintf_chkieee128"},
196 {Builtin::BI__builtin___sprintf_chk, "__sprintf_chkieee128"},
197 {Builtin::BI__builtin___vfprintf_chk, "__vfprintf_chkieee128"},
198 {Builtin::BI__builtin___vprintf_chk, "__vprintf_chkieee128"},
199 {Builtin::BI__builtin___vsnprintf_chk, "__vsnprintf_chkieee128"},
200 {Builtin::BI__builtin___vsprintf_chk, "__vsprintf_chkieee128"},
201 {Builtin::BI__builtin_fprintf, "__fprintfieee128"},
202 {Builtin::BI__builtin_printf, "__printfieee128"},
203 {Builtin::BI__builtin_snprintf, "__snprintfieee128"},
204 {Builtin::BI__builtin_sprintf, "__sprintfieee128"},
205 {Builtin::BI__builtin_vfprintf, "__vfprintfieee128"},
206 {Builtin::BI__builtin_vprintf, "__vprintfieee128"},
207 {Builtin::BI__builtin_vsnprintf, "__vsnprintfieee128"},
208 {Builtin::BI__builtin_vsprintf, "__vsprintfieee128"},
209 {Builtin::BI__builtin_fscanf, "__fscanfieee128"},
210 {Builtin::BI__builtin_scanf, "__scanfieee128"},
211 {Builtin::BI__builtin_sscanf, "__sscanfieee128"},
212 {Builtin::BI__builtin_vfscanf, "__vfscanfieee128"},
213 {Builtin::BI__builtin_vscanf, "__vscanfieee128"},
214 {Builtin::BI__builtin_vsscanf, "__vsscanfieee128"},
215 {Builtin::BI__builtin_nexttowardf128, "__nexttowardieee128"},
216 };
217
218 // The AIX library functions frexpl, ldexpl, and modfl are for 128-bit
219 // IBM 'long double' (i.e. __ibm128). Map to the 'double' versions
220 // if it is 64-bit 'long double' mode.
221 static const SmallDenseMap<unsigned, StringRef, 4> AIXLongDouble64Builtins{
222 {Builtin::BI__builtin_frexpl, "frexp"},
223 {Builtin::BI__builtin_ldexpl, "ldexp"},
224 {Builtin::BI__builtin_modfl, "modf"},
225 };
226
227 // If the builtin has been declared explicitly with an assembler label,
228 // use the mangled name. This differs from the plain label on platforms
229 // that prefix labels.
230 if (FD->hasAttr<AsmLabelAttr>())
231 Name = getMangledName(D);
232 else {
233 // TODO: This mutation should also be applied to other targets other than
234 // PPC, after backend supports IEEE 128-bit style libcalls.
235 if (getTriple().isPPC64() &&
236 &getTarget().getLongDoubleFormat() == &llvm::APFloat::IEEEquad() &&
237 F128Builtins.contains(BuiltinID))
238 Name = F128Builtins.lookup(BuiltinID);
239 else if (getTriple().isOSAIX() &&
240 &getTarget().getLongDoubleFormat() ==
241 &llvm::APFloat::IEEEdouble() &&
242 AIXLongDouble64Builtins.contains(BuiltinID))
243 Name = AIXLongDouble64Builtins.lookup(BuiltinID);
244 else
245 Name = Context.BuiltinInfo.getName(BuiltinID).substr(10);
246 }
247
248 llvm::FunctionType *Ty =
249 cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType()));
250
251 return GetOrCreateLLVMFunction(Name, Ty, D, /*ForVTable=*/false);
252}
253
254/// Emit the conversions required to turn the given value into an
255/// integer of the given size.
256Value *EmitToInt(CodeGenFunction &CGF, llvm::Value *V,
257 QualType T, llvm::IntegerType *IntType) {
258 V = CGF.EmitToMemory(V, T);
259
260 if (V->getType()->isPointerTy())
261 return CGF.Builder.CreatePtrToInt(V, IntType);
262
263 assert(V->getType() == IntType);
264 return V;
265}
266
267Value *EmitFromInt(CodeGenFunction &CGF, llvm::Value *V,
268 QualType T, llvm::Type *ResultType) {
269 V = CGF.EmitFromMemory(V, T);
270
271 if (ResultType->isPointerTy())
272 return CGF.Builder.CreateIntToPtr(V, ResultType);
273
274 assert(V->getType() == ResultType);
275 return V;
276}
277
279 ASTContext &Ctx = CGF.getContext();
280 Address Ptr = CGF.EmitPointerWithAlignment(E->getArg(0));
281 const llvm::DataLayout &DL = CGF.CGM.getDataLayout();
282 unsigned Bytes = Ptr.getElementType()->isPointerTy()
284 : DL.getTypeStoreSize(Ptr.getElementType());
285 unsigned Align = Ptr.getAlignment().getQuantity();
286 if (Align % Bytes != 0) {
287 DiagnosticsEngine &Diags = CGF.CGM.getDiags();
288 Diags.Report(E->getBeginLoc(), diag::warn_sync_op_misaligned);
289 // Force address to be at least naturally-aligned.
290 return Ptr.withAlignment(CharUnits::fromQuantity(Bytes));
291 }
292 return Ptr;
293}
294
295/// Utility to insert an atomic instruction based on Intrinsic::ID
296/// and the expression node.
298 CodeGenFunction &CGF, llvm::AtomicRMWInst::BinOp Kind, const CallExpr *E,
299 AtomicOrdering Ordering) {
300
301 QualType T = E->getType();
302 assert(E->getArg(0)->getType()->isPointerType());
303 assert(CGF.getContext().hasSameUnqualifiedType(T,
304 E->getArg(0)->getType()->getPointeeType()));
305 assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
306
307 Address DestAddr = CheckAtomicAlignment(CGF, E);
308
309 llvm::IntegerType *IntType = llvm::IntegerType::get(
310 CGF.getLLVMContext(), CGF.getContext().getTypeSize(T));
311
312 llvm::Value *Val = CGF.EmitScalarExpr(E->getArg(1));
313 llvm::Type *ValueType = Val->getType();
314 Val = EmitToInt(CGF, Val, T, IntType);
315
316 llvm::Value *Result =
317 CGF.Builder.CreateAtomicRMW(Kind, DestAddr, Val, Ordering);
318 // Consider atomics to be volatile in MS kernel mode.
319 if (CGF.CGM.getLangOpts().Kernel)
320 cast<llvm::AtomicRMWInst>(Result)->setVolatile(true);
321 return EmitFromInt(CGF, Result, T, ValueType);
322}
323
325 Value *Val = CGF.EmitScalarExpr(E->getArg(0));
327
328 Val = CGF.EmitToMemory(Val, E->getArg(0)->getType());
329 LValue LV = CGF.MakeAddrLValue(Addr, E->getArg(0)->getType());
330 LV.setNontemporal(true);
331 CGF.EmitStoreOfScalar(Val, LV, false);
332 return nullptr;
333}
334
337
338 LValue LV = CGF.MakeAddrLValue(Addr, E->getType());
339 LV.setNontemporal(true);
340 return CGF.EmitLoadOfScalar(LV, E->getExprLoc());
341}
342
344 llvm::AtomicRMWInst::BinOp Kind,
345 const CallExpr *E) {
346 return RValue::get(MakeBinaryAtomicValue(CGF, Kind, E));
347}
348
349/// Utility to insert an atomic instruction based Intrinsic::ID and
350/// the expression node, where the return value is the result of the
351/// operation.
353 llvm::AtomicRMWInst::BinOp Kind,
354 const CallExpr *E,
355 Instruction::BinaryOps Op,
356 bool Invert = false) {
357 QualType T = E->getType();
358 assert(E->getArg(0)->getType()->isPointerType());
359 assert(CGF.getContext().hasSameUnqualifiedType(T,
360 E->getArg(0)->getType()->getPointeeType()));
361 assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
362
363 Address DestAddr = CheckAtomicAlignment(CGF, E);
364
365 llvm::IntegerType *IntType = llvm::IntegerType::get(
366 CGF.getLLVMContext(), CGF.getContext().getTypeSize(T));
367
368 llvm::Value *Val = CGF.EmitScalarExpr(E->getArg(1));
369 llvm::Type *ValueType = Val->getType();
370 Val = EmitToInt(CGF, Val, T, IntType);
371
372 llvm::Value *Result = CGF.Builder.CreateAtomicRMW(
373 Kind, DestAddr, Val, llvm::AtomicOrdering::SequentiallyConsistent);
374 Result = CGF.Builder.CreateBinOp(Op, Result, Val);
375 if (Invert)
376 Result =
377 CGF.Builder.CreateBinOp(llvm::Instruction::Xor, Result,
378 llvm::ConstantInt::getAllOnesValue(IntType));
379 Result = EmitFromInt(CGF, Result, T, ValueType);
380 return RValue::get(Result);
381}
382
383/// Utility to insert an atomic cmpxchg instruction.
384///
385/// @param CGF The current codegen function.
386/// @param E Builtin call expression to convert to cmpxchg.
387/// arg0 - address to operate on
388/// arg1 - value to compare with
389/// arg2 - new value
390/// @param ReturnBool Specifies whether to return success flag of
391/// cmpxchg result or the old value.
392///
393/// @returns result of cmpxchg, according to ReturnBool
394///
395/// Note: In order to lower Microsoft's _InterlockedCompareExchange* intrinsics
396/// invoke the function EmitAtomicCmpXchgForMSIntrin.
398 bool ReturnBool,
399 llvm::AtomicOrdering SuccessOrdering,
400 llvm::AtomicOrdering FailureOrdering) {
401 QualType T = ReturnBool ? E->getArg(1)->getType() : E->getType();
402 Address DestAddr = CheckAtomicAlignment(CGF, E);
403
404 llvm::IntegerType *IntType = llvm::IntegerType::get(
405 CGF.getLLVMContext(), CGF.getContext().getTypeSize(T));
406
407 Value *Cmp = CGF.EmitScalarExpr(E->getArg(1));
408 llvm::Type *ValueType = Cmp->getType();
409 Cmp = EmitToInt(CGF, Cmp, T, IntType);
410 Value *New = EmitToInt(CGF, CGF.EmitScalarExpr(E->getArg(2)), T, IntType);
411
413 DestAddr, Cmp, New, SuccessOrdering, FailureOrdering);
414 if (ReturnBool)
415 // Extract boolean success flag and zext it to int.
416 return CGF.Builder.CreateZExt(CGF.Builder.CreateExtractValue(Pair, 1),
417 CGF.ConvertType(E->getType()));
418 else
419 // Extract old value and emit it using the same type as compare value.
420 return EmitFromInt(CGF, CGF.Builder.CreateExtractValue(Pair, 0), T,
421 ValueType);
422}
423
424/// This function should be invoked to emit atomic cmpxchg for Microsoft's
425/// _InterlockedCompareExchange* intrinsics which have the following signature:
426/// T _InterlockedCompareExchange(T volatile *Destination,
427/// T Exchange,
428/// T Comparand);
429///
430/// Whereas the llvm 'cmpxchg' instruction has the following syntax:
431/// cmpxchg *Destination, Comparand, Exchange.
432/// So we need to swap Comparand and Exchange when invoking
433/// CreateAtomicCmpXchg. That is the reason we could not use the above utility
434/// function MakeAtomicCmpXchgValue since it expects the arguments to be
435/// already swapped.
436
437static
439 AtomicOrdering SuccessOrdering = AtomicOrdering::SequentiallyConsistent) {
440 assert(E->getArg(0)->getType()->isPointerType());
442 E->getType(), E->getArg(0)->getType()->getPointeeType()));
443 assert(CGF.getContext().hasSameUnqualifiedType(E->getType(),
444 E->getArg(1)->getType()));
445 assert(CGF.getContext().hasSameUnqualifiedType(E->getType(),
446 E->getArg(2)->getType()));
447
448 Address DestAddr = CheckAtomicAlignment(CGF, E);
449
450 auto *Exchange = CGF.EmitScalarExpr(E->getArg(1));
451 auto *RTy = Exchange->getType();
452
453 auto *Comparand = CGF.EmitScalarExpr(E->getArg(2));
454
455 if (RTy->isPointerTy()) {
456 Exchange = CGF.Builder.CreatePtrToInt(Exchange, CGF.IntPtrTy);
457 Comparand = CGF.Builder.CreatePtrToInt(Comparand, CGF.IntPtrTy);
458 }
459
460 // For Release ordering, the failure ordering should be Monotonic.
461 auto FailureOrdering = SuccessOrdering == AtomicOrdering::Release ?
462 AtomicOrdering::Monotonic :
463 SuccessOrdering;
464
465 // The atomic instruction is marked volatile for consistency with MSVC. This
466 // blocks the few atomics optimizations that LLVM has. If we want to optimize
467 // _Interlocked* operations in the future, we will have to remove the volatile
468 // marker.
469 auto *CmpXchg = CGF.Builder.CreateAtomicCmpXchg(
470 DestAddr, Comparand, Exchange, SuccessOrdering, FailureOrdering);
471 CmpXchg->setVolatile(true);
472
473 auto *Result = CGF.Builder.CreateExtractValue(CmpXchg, 0);
474 if (RTy->isPointerTy()) {
475 Result = CGF.Builder.CreateIntToPtr(Result, RTy);
476 }
477
478 return Result;
479}
480
481// 64-bit Microsoft platforms support 128 bit cmpxchg operations. They are
482// prototyped like this:
483//
484// unsigned char _InterlockedCompareExchange128...(
485// __int64 volatile * _Destination,
486// __int64 _ExchangeHigh,
487// __int64 _ExchangeLow,
488// __int64 * _ComparandResult);
489//
490// Note that Destination is assumed to be at least 16-byte aligned, despite
491// being typed int64.
492
494 const CallExpr *E,
495 AtomicOrdering SuccessOrdering) {
496 assert(E->getNumArgs() == 4);
497 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
498 llvm::Value *ExchangeHigh = CGF.EmitScalarExpr(E->getArg(1));
499 llvm::Value *ExchangeLow = CGF.EmitScalarExpr(E->getArg(2));
500 Address ComparandAddr = CGF.EmitPointerWithAlignment(E->getArg(3));
501
502 assert(DestPtr->getType()->isPointerTy());
503 assert(!ExchangeHigh->getType()->isPointerTy());
504 assert(!ExchangeLow->getType()->isPointerTy());
505
506 // For Release ordering, the failure ordering should be Monotonic.
507 auto FailureOrdering = SuccessOrdering == AtomicOrdering::Release
508 ? AtomicOrdering::Monotonic
509 : SuccessOrdering;
510
511 // Convert to i128 pointers and values. Alignment is also overridden for
512 // destination pointer.
513 llvm::Type *Int128Ty = llvm::IntegerType::get(CGF.getLLVMContext(), 128);
514 Address DestAddr(DestPtr, Int128Ty,
516 ComparandAddr = ComparandAddr.withElementType(Int128Ty);
517
518 // (((i128)hi) << 64) | ((i128)lo)
519 ExchangeHigh = CGF.Builder.CreateZExt(ExchangeHigh, Int128Ty);
520 ExchangeLow = CGF.Builder.CreateZExt(ExchangeLow, Int128Ty);
521 ExchangeHigh =
522 CGF.Builder.CreateShl(ExchangeHigh, llvm::ConstantInt::get(Int128Ty, 64));
523 llvm::Value *Exchange = CGF.Builder.CreateOr(ExchangeHigh, ExchangeLow);
524
525 // Load the comparand for the instruction.
526 llvm::Value *Comparand = CGF.Builder.CreateLoad(ComparandAddr);
527
528 auto *CXI = CGF.Builder.CreateAtomicCmpXchg(DestAddr, Comparand, Exchange,
529 SuccessOrdering, FailureOrdering);
530
531 // The atomic instruction is marked volatile for consistency with MSVC. This
532 // blocks the few atomics optimizations that LLVM has. If we want to optimize
533 // _Interlocked* operations in the future, we will have to remove the volatile
534 // marker.
535 CXI->setVolatile(true);
536
537 // Store the result as an outparameter.
538 CGF.Builder.CreateStore(CGF.Builder.CreateExtractValue(CXI, 0),
539 ComparandAddr);
540
541 // Get the success boolean and zero extend it to i8.
542 Value *Success = CGF.Builder.CreateExtractValue(CXI, 1);
543 return CGF.Builder.CreateZExt(Success, CGF.Int8Ty);
544}
545
547 AtomicOrdering Ordering = AtomicOrdering::SequentiallyConsistent) {
548 assert(E->getArg(0)->getType()->isPointerType());
549
550 auto *IntTy = CGF.ConvertType(E->getType());
551 Address DestAddr = CheckAtomicAlignment(CGF, E);
552 auto *Result = CGF.Builder.CreateAtomicRMW(
553 AtomicRMWInst::Add, DestAddr, ConstantInt::get(IntTy, 1), Ordering);
554 return CGF.Builder.CreateAdd(Result, ConstantInt::get(IntTy, 1));
555}
556
558 CodeGenFunction &CGF, const CallExpr *E,
559 AtomicOrdering Ordering = AtomicOrdering::SequentiallyConsistent) {
560 assert(E->getArg(0)->getType()->isPointerType());
561
562 auto *IntTy = CGF.ConvertType(E->getType());
563 Address DestAddr = CheckAtomicAlignment(CGF, E);
564 auto *Result = CGF.Builder.CreateAtomicRMW(
565 AtomicRMWInst::Sub, DestAddr, ConstantInt::get(IntTy, 1), Ordering);
566 return CGF.Builder.CreateSub(Result, ConstantInt::get(IntTy, 1));
567}
568
569// Build a plain volatile load.
571 Value *Ptr = CGF.EmitScalarExpr(E->getArg(0));
572 QualType ElTy = E->getArg(0)->getType()->getPointeeType();
573 CharUnits LoadSize = CGF.getContext().getTypeSizeInChars(ElTy);
574 llvm::Type *ITy =
575 llvm::IntegerType::get(CGF.getLLVMContext(), LoadSize.getQuantity() * 8);
576 llvm::LoadInst *Load = CGF.Builder.CreateAlignedLoad(ITy, Ptr, LoadSize);
577 Load->setAtomic(llvm::AtomicOrdering::Monotonic);
578 Load->setVolatile(true);
579 return Load;
580}
581
582// Build a plain volatile store.
584 Value *Ptr = CGF.EmitScalarExpr(E->getArg(0));
585 Value *Value = CGF.EmitScalarExpr(E->getArg(1));
586 QualType ElTy = E->getArg(0)->getType()->getPointeeType();
587 CharUnits StoreSize = CGF.getContext().getTypeSizeInChars(ElTy);
588 llvm::StoreInst *Store =
589 CGF.Builder.CreateAlignedStore(Value, Ptr, StoreSize);
590 Store->setAtomic(llvm::AtomicOrdering::Monotonic);
591 Store->setVolatile(true);
592 return Store;
593}
594
595// Emit a simple mangled intrinsic that has 1 argument and a return type
596// matching the argument type. Depending on mode, this may be a constrained
597// floating-point intrinsic.
599 const CallExpr *E, unsigned IntrinsicID,
600 unsigned ConstrainedIntrinsicID) {
601 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
602
603 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
604 if (CGF.Builder.getIsFPConstrained()) {
605 Function *F = CGF.CGM.getIntrinsic(ConstrainedIntrinsicID, Src0->getType());
606 return CGF.Builder.CreateConstrainedFPCall(F, { Src0 });
607 } else {
608 Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
609 return CGF.Builder.CreateCall(F, Src0);
610 }
611}
612
613// Emit an intrinsic that has 2 operands of the same type as its result.
614// Depending on mode, this may be a constrained floating-point intrinsic.
616 const CallExpr *E, unsigned IntrinsicID,
617 unsigned ConstrainedIntrinsicID) {
618 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
619 llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
620
621 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
622 if (CGF.Builder.getIsFPConstrained()) {
623 Function *F = CGF.CGM.getIntrinsic(ConstrainedIntrinsicID, Src0->getType());
624 return CGF.Builder.CreateConstrainedFPCall(F, { Src0, Src1 });
625 } else {
626 Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
627 return CGF.Builder.CreateCall(F, { Src0, Src1 });
628 }
629}
630
631// Has second type mangled argument.
632static Value *
634 Intrinsic::ID IntrinsicID,
635 Intrinsic::ID ConstrainedIntrinsicID) {
636 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
637 llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
638
639 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
640 if (CGF.Builder.getIsFPConstrained()) {
641 Function *F = CGF.CGM.getIntrinsic(ConstrainedIntrinsicID,
642 {Src0->getType(), Src1->getType()});
643 return CGF.Builder.CreateConstrainedFPCall(F, {Src0, Src1});
644 }
645
646 Function *F =
647 CGF.CGM.getIntrinsic(IntrinsicID, {Src0->getType(), Src1->getType()});
648 return CGF.Builder.CreateCall(F, {Src0, Src1});
649}
650
651// Emit an intrinsic that has 3 operands of the same type as its result.
652// Depending on mode, this may be a constrained floating-point intrinsic.
654 const CallExpr *E, unsigned IntrinsicID,
655 unsigned ConstrainedIntrinsicID) {
656 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
657 llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
658 llvm::Value *Src2 = CGF.EmitScalarExpr(E->getArg(2));
659
660 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
661 if (CGF.Builder.getIsFPConstrained()) {
662 Function *F = CGF.CGM.getIntrinsic(ConstrainedIntrinsicID, Src0->getType());
663 return CGF.Builder.CreateConstrainedFPCall(F, { Src0, Src1, Src2 });
664 } else {
665 Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
666 return CGF.Builder.CreateCall(F, { Src0, Src1, Src2 });
667 }
668}
669
670// Emit an intrinsic that has overloaded integer result and fp operand.
671static Value *
673 unsigned IntrinsicID,
674 unsigned ConstrainedIntrinsicID) {
675 llvm::Type *ResultType = CGF.ConvertType(E->getType());
676 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
677
678 if (CGF.Builder.getIsFPConstrained()) {
679 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
680 Function *F = CGF.CGM.getIntrinsic(ConstrainedIntrinsicID,
681 {ResultType, Src0->getType()});
682 return CGF.Builder.CreateConstrainedFPCall(F, {Src0});
683 } else {
684 Function *F =
685 CGF.CGM.getIntrinsic(IntrinsicID, {ResultType, Src0->getType()});
686 return CGF.Builder.CreateCall(F, Src0);
687 }
688}
689
691 Intrinsic::ID IntrinsicID) {
692 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
693 llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
694
695 QualType IntPtrTy = E->getArg(1)->getType()->getPointeeType();
696 llvm::Type *IntTy = CGF.ConvertType(IntPtrTy);
697 llvm::Function *F =
698 CGF.CGM.getIntrinsic(IntrinsicID, {Src0->getType(), IntTy});
699 llvm::Value *Call = CGF.Builder.CreateCall(F, Src0);
700
701 llvm::Value *Exp = CGF.Builder.CreateExtractValue(Call, 1);
702 LValue LV = CGF.MakeNaturalAlignAddrLValue(Src1, IntPtrTy);
703 CGF.EmitStoreOfScalar(Exp, LV);
704
705 return CGF.Builder.CreateExtractValue(Call, 0);
706}
707
708static void emitSincosBuiltin(CodeGenFunction &CGF, const CallExpr *E,
709 Intrinsic::ID IntrinsicID) {
710 llvm::Value *Val = CGF.EmitScalarExpr(E->getArg(0));
711 llvm::Value *Dest0 = CGF.EmitScalarExpr(E->getArg(1));
712 llvm::Value *Dest1 = CGF.EmitScalarExpr(E->getArg(2));
713
714 llvm::Function *F = CGF.CGM.getIntrinsic(IntrinsicID, {Val->getType()});
715 llvm::Value *Call = CGF.Builder.CreateCall(F, Val);
716
717 llvm::Value *SinResult = CGF.Builder.CreateExtractValue(Call, 0);
718 llvm::Value *CosResult = CGF.Builder.CreateExtractValue(Call, 1);
719
720 QualType DestPtrType = E->getArg(1)->getType()->getPointeeType();
721 LValue SinLV = CGF.MakeNaturalAlignAddrLValue(Dest0, DestPtrType);
722 LValue CosLV = CGF.MakeNaturalAlignAddrLValue(Dest1, DestPtrType);
723
724 llvm::StoreInst *StoreSin =
725 CGF.Builder.CreateStore(SinResult, SinLV.getAddress());
726 llvm::StoreInst *StoreCos =
727 CGF.Builder.CreateStore(CosResult, CosLV.getAddress());
728
729 // Mark the two stores as non-aliasing with each other. The order of stores
730 // emitted by this builtin is arbitrary, enforcing a particular order will
731 // prevent optimizations later on.
732 llvm::MDBuilder MDHelper(CGF.getLLVMContext());
733 MDNode *Domain = MDHelper.createAnonymousAliasScopeDomain();
734 MDNode *AliasScope = MDHelper.createAnonymousAliasScope(Domain);
735 MDNode *AliasScopeList = MDNode::get(Call->getContext(), AliasScope);
736 StoreSin->setMetadata(LLVMContext::MD_alias_scope, AliasScopeList);
737 StoreCos->setMetadata(LLVMContext::MD_noalias, AliasScopeList);
738}
739
740static llvm::Value *emitModfBuiltin(CodeGenFunction &CGF, const CallExpr *E,
741 Intrinsic::ID IntrinsicID) {
742 llvm::Value *Val = CGF.EmitScalarExpr(E->getArg(0));
743 llvm::Value *IntPartDest = CGF.EmitScalarExpr(E->getArg(1));
744
745 llvm::Value *Call =
746 CGF.Builder.CreateIntrinsic(IntrinsicID, {Val->getType()}, Val);
747
748 llvm::Value *FractionalResult = CGF.Builder.CreateExtractValue(Call, 0);
749 llvm::Value *IntegralResult = CGF.Builder.CreateExtractValue(Call, 1);
750
751 QualType DestPtrType = E->getArg(1)->getType()->getPointeeType();
752 LValue IntegralLV = CGF.MakeNaturalAlignAddrLValue(IntPartDest, DestPtrType);
753 CGF.EmitStoreOfScalar(IntegralResult, IntegralLV);
754
755 return FractionalResult;
756}
757
758/// EmitFAbs - Emit a call to @llvm.fabs().
760 llvm::Value *Call = CGF.Builder.CreateFAbs(V);
761 if (auto *CallI = dyn_cast<llvm::CallInst>(Call))
762 CallI->setDoesNotAccessMemory();
763 return Call;
764}
765
766/// Emit the computation of the sign bit for a floating point value. Returns
767/// the i1 sign bit value.
769 LLVMContext &C = CGF.CGM.getLLVMContext();
770
771 llvm::Type *Ty = V->getType();
772 int Width = Ty->getPrimitiveSizeInBits();
773 llvm::Type *IntTy = llvm::IntegerType::get(C, Width);
774 V = CGF.Builder.CreateBitCast(V, IntTy);
775 if (Ty->isPPC_FP128Ty()) {
776 // We want the sign bit of the higher-order double. The bitcast we just
777 // did works as if the double-double was stored to memory and then
778 // read as an i128. The "store" will put the higher-order double in the
779 // lower address in both little- and big-Endian modes, but the "load"
780 // will treat those bits as a different part of the i128: the low bits in
781 // little-Endian, the high bits in big-Endian. Therefore, on big-Endian
782 // we need to shift the high bits down to the low before truncating.
783 Width >>= 1;
784 if (CGF.getTarget().isBigEndian()) {
785 Value *ShiftCst = llvm::ConstantInt::get(IntTy, Width);
786 V = CGF.Builder.CreateLShr(V, ShiftCst);
787 }
788 // We are truncating value in order to extract the higher-order
789 // double, which we will be using to extract the sign from.
790 IntTy = llvm::IntegerType::get(C, Width);
791 V = CGF.Builder.CreateTrunc(V, IntTy);
792 }
793 Value *Zero = llvm::Constant::getNullValue(IntTy);
794 return CGF.Builder.CreateICmpSLT(V, Zero);
795}
796
797/// Checks no arguments or results are passed indirectly in the ABI (i.e. via a
798/// hidden pointer). This is used to check annotating FP libcalls (that could
799/// set `errno`) with "int" TBAA metadata is safe. If any floating-point
800/// arguments are passed indirectly, setup for the call could be incorrectly
801/// optimized out.
803 auto IsIndirect = [&](ABIArgInfo const &info) {
804 return info.isIndirect() || info.isIndirectAliased() || info.isInAlloca();
805 };
806 return !IsIndirect(FnInfo.getReturnInfo()) &&
807 llvm::none_of(FnInfo.arguments(),
808 [&](CGFunctionInfoArgInfo const &ArgInfo) {
809 return IsIndirect(ArgInfo.info);
810 });
811}
812
814 const CallExpr *E, llvm::Constant *calleeValue) {
815 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
816 CGCallee callee = CGCallee::forDirect(calleeValue, GlobalDecl(FD));
817 llvm::CallBase *callOrInvoke = nullptr;
818 CGFunctionInfo const *FnInfo = nullptr;
819 RValue Call =
820 CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot(),
821 /*Chain=*/nullptr, &callOrInvoke, &FnInfo);
822
823 if (unsigned BuiltinID = FD->getBuiltinID()) {
824 // Check whether a FP math builtin function, such as BI__builtin_expf
825 ASTContext &Context = CGF.getContext();
826 bool ConstWithoutErrnoAndExceptions =
828 // Restrict to target with errno, for example, MacOS doesn't set errno.
829 // TODO: Support builtin function with complex type returned, eg: cacosh
830 if (ConstWithoutErrnoAndExceptions && CGF.CGM.getLangOpts().MathErrno &&
831 !CGF.Builder.getIsFPConstrained() && Call.isScalar() &&
833 // Emit "int" TBAA metadata on FP math libcalls.
834 clang::QualType IntTy = Context.IntTy;
835 TBAAAccessInfo TBAAInfo = CGF.CGM.getTBAAAccessInfo(IntTy);
836 CGF.CGM.DecorateInstructionWithTBAA(callOrInvoke, TBAAInfo);
837 }
838 }
839 return Call;
840}
841
842/// Emit a call to llvm.{sadd,uadd,ssub,usub,smul,umul}.with.overflow.*
843/// depending on IntrinsicID.
844///
845/// \arg CGF The current codegen function.
846/// \arg IntrinsicID The ID for the Intrinsic we wish to generate.
847/// \arg X The first argument to the llvm.*.with.overflow.*.
848/// \arg Y The second argument to the llvm.*.with.overflow.*.
849/// \arg Carry The carry returned by the llvm.*.with.overflow.*.
850/// \returns The result (i.e. sum/product) returned by the intrinsic.
852 const Intrinsic::ID IntrinsicID,
853 llvm::Value *X, llvm::Value *Y,
854 llvm::Value *&Carry) {
855 // Make sure we have integers of the same width.
856 assert(X->getType() == Y->getType() &&
857 "Arguments must be the same type. (Did you forget to make sure both "
858 "arguments have the same integer width?)");
859
860 Function *Callee = CGF.CGM.getIntrinsic(IntrinsicID, X->getType());
861 llvm::Value *Tmp = CGF.Builder.CreateCall(Callee, {X, Y});
862 Carry = CGF.Builder.CreateExtractValue(Tmp, 1);
863 return CGF.Builder.CreateExtractValue(Tmp, 0);
864}
865
866namespace {
867 struct WidthAndSignedness {
868 unsigned Width;
869 bool Signed;
870 };
871}
872
873static WidthAndSignedness
875 const clang::QualType Type) {
876 assert(Type->isIntegerType() && "Given type is not an integer.");
877 unsigned Width = context.getIntWidth(Type);
879 return {Width, Signed};
880}
881
882// Given one or more integer types, this function produces an integer type that
883// encompasses them: any value in one of the given types could be expressed in
884// the encompassing type.
885static struct WidthAndSignedness
886EncompassingIntegerType(ArrayRef<struct WidthAndSignedness> Types) {
887 assert(Types.size() > 0 && "Empty list of types.");
888
889 // If any of the given types is signed, we must return a signed type.
890 bool Signed = false;
891 for (const auto &Type : Types) {
892 Signed |= Type.Signed;
893 }
894
895 // The encompassing type must have a width greater than or equal to the width
896 // of the specified types. Additionally, if the encompassing type is signed,
897 // its width must be strictly greater than the width of any unsigned types
898 // given.
899 unsigned Width = 0;
900 for (const auto &Type : Types) {
901 unsigned MinWidth = Type.Width + (Signed && !Type.Signed);
902 if (Width < MinWidth) {
903 Width = MinWidth;
904 }
905 }
906
907 return {Width, Signed};
908}
909
910Value *CodeGenFunction::EmitVAStartEnd(Value *ArgValue, bool IsStart) {
911 Intrinsic::ID inst = IsStart ? Intrinsic::vastart : Intrinsic::vaend;
912 return Builder.CreateCall(CGM.getIntrinsic(inst, {ArgValue->getType()}),
913 ArgValue);
914}
915
916/// Checks if using the result of __builtin_object_size(p, @p From) in place of
917/// __builtin_object_size(p, @p To) is correct
918static bool areBOSTypesCompatible(int From, int To) {
919 // Note: Our __builtin_object_size implementation currently treats Type=0 and
920 // Type=2 identically. Encoding this implementation detail here may make
921 // improving __builtin_object_size difficult in the future, so it's omitted.
922 return From == To || (From == 0 && To == 1) || (From == 3 && To == 2);
923}
924
925static llvm::Value *
926getDefaultBuiltinObjectSizeResult(unsigned Type, llvm::IntegerType *ResType) {
927 return ConstantInt::get(ResType, (Type & 2) ? 0 : -1, /*isSigned=*/true);
928}
929
930llvm::Value *
931CodeGenFunction::evaluateOrEmitBuiltinObjectSize(const Expr *E, unsigned Type,
932 llvm::IntegerType *ResType,
933 llvm::Value *EmittedE,
934 bool IsDynamic) {
935 if (std::optional<uint64_t> ObjectSize =
937 return ConstantInt::get(ResType, *ObjectSize, /*isSigned=*/true);
938 return emitBuiltinObjectSize(E, Type, ResType, EmittedE, IsDynamic);
939}
940
941namespace {
942
943/// StructFieldAccess is a simple visitor class to grab the first MemberExpr
944/// from an Expr. It records any ArraySubscriptExpr we meet along the way.
945class StructFieldAccess
946 : public ConstStmtVisitor<StructFieldAccess, const Expr *> {
947 bool AddrOfSeen = false;
948
949public:
950 const Expr *ArrayIndex = nullptr;
951 QualType ArrayElementTy;
952
953 const Expr *VisitMemberExpr(const MemberExpr *E) {
954 if (AddrOfSeen && E->getType()->isArrayType())
955 // Avoid forms like '&ptr->array'.
956 return nullptr;
957 return E;
958 }
959
960 const Expr *VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
961 if (ArrayIndex)
962 // We don't support multiple subscripts.
963 return nullptr;
964
965 AddrOfSeen = false; // '&ptr->array[idx]' is okay.
966 ArrayIndex = E->getIdx();
967 ArrayElementTy = E->getBase()->getType();
968 return Visit(E->getBase());
969 }
970 const Expr *VisitCastExpr(const CastExpr *E) {
971 if (E->getCastKind() == CK_LValueToRValue)
972 return E;
973 return Visit(E->getSubExpr());
974 }
975 const Expr *VisitParenExpr(const ParenExpr *E) {
976 return Visit(E->getSubExpr());
977 }
978 const Expr *VisitUnaryAddrOf(const clang::UnaryOperator *E) {
979 AddrOfSeen = true;
980 return Visit(E->getSubExpr());
981 }
982 const Expr *VisitUnaryDeref(const clang::UnaryOperator *E) {
983 AddrOfSeen = false;
984 return Visit(E->getSubExpr());
985 }
986 const Expr *VisitBinaryOperator(const clang::BinaryOperator *Op) {
987 return Op->isCommaOp() ? Visit(Op->getRHS()) : nullptr;
988 }
989};
990
991} // end anonymous namespace
992
993/// Find a struct's flexible array member. It may be embedded inside multiple
994/// sub-structs, but must still be the last field.
996 ASTContext &Ctx,
997 const RecordDecl *RD) {
998 const LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
999 CGF.getLangOpts().getStrictFlexArraysLevel();
1000
1001 if (RD->isImplicit())
1002 return nullptr;
1003
1004 for (const FieldDecl *FD : RD->fields()) {
1006 Ctx, FD, FD->getType(), StrictFlexArraysLevel,
1007 /*IgnoreTemplateOrMacroSubstitution=*/true))
1008 return FD;
1009
1010 if (const auto *RD = FD->getType()->getAsRecordDecl())
1011 if (const FieldDecl *FD = FindFlexibleArrayMemberField(CGF, Ctx, RD))
1012 return FD;
1013 }
1014
1015 return nullptr;
1016}
1017
1018/// Calculate the offset of a struct field. It may be embedded inside multiple
1019/// sub-structs.
1020static bool GetFieldOffset(ASTContext &Ctx, const RecordDecl *RD,
1021 const FieldDecl *FD, int64_t &Offset) {
1022 if (RD->isImplicit())
1023 return false;
1024
1025 // Keep track of the field number ourselves, because the other methods
1026 // (CGRecordLayout::getLLVMFieldNo) aren't always equivalent to how the AST
1027 // is laid out.
1028 uint32_t FieldNo = 0;
1029 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
1030
1031 for (const FieldDecl *Field : RD->fields()) {
1032 if (Field == FD) {
1033 Offset += Layout.getFieldOffset(FieldNo);
1034 return true;
1035 }
1036
1037 if (const auto *RD = Field->getType()->getAsRecordDecl()) {
1038 if (GetFieldOffset(Ctx, RD, FD, Offset)) {
1039 Offset += Layout.getFieldOffset(FieldNo);
1040 return true;
1041 }
1042 }
1043
1044 if (!RD->isUnion())
1045 ++FieldNo;
1046 }
1047
1048 return false;
1049}
1050
1051static std::optional<int64_t>
1052GetFieldOffset(ASTContext &Ctx, const RecordDecl *RD, const FieldDecl *FD) {
1053 int64_t Offset = 0;
1054
1055 if (GetFieldOffset(Ctx, RD, FD, Offset))
1056 return std::optional<int64_t>(Offset);
1057
1058 return std::nullopt;
1059}
1060
1061llvm::Value *CodeGenFunction::emitCountedBySize(const Expr *E,
1062 llvm::Value *EmittedE,
1063 unsigned Type,
1064 llvm::IntegerType *ResType) {
1065 // Note: If the whole struct is specificed in the __bdos (i.e. Visitor
1066 // returns a DeclRefExpr). The calculation of the whole size of the structure
1067 // with a flexible array member can be done in two ways:
1068 //
1069 // 1) sizeof(struct S) + count * sizeof(typeof(fam))
1070 // 2) offsetof(struct S, fam) + count * sizeof(typeof(fam))
1071 //
1072 // The first will add additional padding after the end of the array
1073 // allocation while the second method is more precise, but not quite expected
1074 // from programmers. See
1075 // https://lore.kernel.org/lkml/ZvV6X5FPBBW7CO1f@archlinux/ for a discussion
1076 // of the topic.
1077 //
1078 // GCC isn't (currently) able to calculate __bdos on a pointer to the whole
1079 // structure. Therefore, because of the above issue, we choose to match what
1080 // GCC does for consistency's sake.
1081
1082 StructFieldAccess Visitor;
1083 E = Visitor.Visit(E);
1084 if (!E)
1085 return nullptr;
1086
1087 const Expr *Idx = Visitor.ArrayIndex;
1088 if (Idx) {
1089 if (Idx->HasSideEffects(getContext()))
1090 // We can't have side-effects.
1091 return getDefaultBuiltinObjectSizeResult(Type, ResType);
1092
1093 if (const auto *IL = dyn_cast<IntegerLiteral>(Idx)) {
1094 int64_t Val = IL->getValue().getSExtValue();
1095 if (Val < 0)
1096 return getDefaultBuiltinObjectSizeResult(Type, ResType);
1097
1098 // The index is 0, so we don't need to take it into account.
1099 if (Val == 0)
1100 Idx = nullptr;
1101 }
1102 }
1103
1104 // __counted_by on either a flexible array member or a pointer into a struct
1105 // with a flexible array member.
1106 if (const auto *ME = dyn_cast<MemberExpr>(E))
1107 return emitCountedByMemberSize(ME, Idx, EmittedE, Visitor.ArrayElementTy,
1108 Type, ResType);
1109
1110 // __counted_by on a pointer in a struct.
1111 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
1112 ICE && ICE->getCastKind() == CK_LValueToRValue)
1113 return emitCountedByPointerSize(ICE, Idx, EmittedE, Visitor.ArrayElementTy,
1114 Type, ResType);
1115
1116 return nullptr;
1117}
1118
1120 llvm::Value *Res,
1121 llvm::Value *Index,
1122 llvm::IntegerType *ResType,
1123 bool IsSigned) {
1124 // cmp = (array_size >= 0)
1125 Value *Cmp = CGF.Builder.CreateIsNotNeg(Res);
1126 if (Index)
1127 // cmp = (cmp && index >= 0)
1128 Cmp = CGF.Builder.CreateAnd(CGF.Builder.CreateIsNotNeg(Index), Cmp);
1129
1130 // return cmp ? result : 0
1131 return CGF.Builder.CreateSelect(Cmp, Res,
1132 ConstantInt::get(ResType, 0, IsSigned));
1133}
1134
1135static std::pair<llvm::Value *, llvm::Value *>
1137 const FieldDecl *ArrayFD, const FieldDecl *CountFD,
1138 const Expr *Idx, llvm::IntegerType *ResType,
1139 bool IsSigned) {
1140 // count = ptr->count;
1141 Value *Count = CGF.EmitLoadOfCountedByField(ME, ArrayFD, CountFD);
1142 if (!Count)
1143 return std::make_pair<Value *>(nullptr, nullptr);
1144 Count = CGF.Builder.CreateIntCast(Count, ResType, IsSigned, "count");
1145
1146 // index = ptr->index;
1147 Value *Index = nullptr;
1148 if (Idx) {
1149 bool IdxSigned = Idx->getType()->isSignedIntegerType();
1150 Index = CGF.EmitScalarExpr(Idx);
1151 Index = CGF.Builder.CreateIntCast(Index, ResType, IdxSigned, "index");
1152 }
1153
1154 return std::make_pair(Count, Index);
1155}
1156
1157llvm::Value *CodeGenFunction::emitCountedByPointerSize(
1158 const ImplicitCastExpr *E, const Expr *Idx, llvm::Value *EmittedE,
1159 QualType CastedArrayElementTy, unsigned Type, llvm::IntegerType *ResType) {
1160 assert(E->getCastKind() == CK_LValueToRValue &&
1161 "must be an LValue to RValue cast");
1162
1163 const MemberExpr *ME =
1164 dyn_cast<MemberExpr>(E->getSubExpr()->IgnoreParenNoopCasts(getContext()));
1165 if (!ME)
1166 return nullptr;
1167
1168 const auto *ArrayBaseFD = dyn_cast<FieldDecl>(ME->getMemberDecl());
1169 if (!ArrayBaseFD || !ArrayBaseFD->getType()->isPointerType() ||
1170 !ArrayBaseFD->getType()->isCountAttributedType())
1171 return nullptr;
1172
1173 // Get the 'count' FieldDecl.
1174 const FieldDecl *CountFD = ArrayBaseFD->findCountedByField();
1175 if (!CountFD)
1176 // Can't find the field referenced by the "counted_by" attribute.
1177 return nullptr;
1178
1179 // Calculate the array's object size using these formulae. (Note: if the
1180 // calculation is negative, we return 0.):
1181 //
1182 // struct p;
1183 // struct s {
1184 // /* ... */
1185 // struct p **array __attribute__((counted_by(count)));
1186 // int count;
1187 // };
1188 //
1189 // 1) 'ptr->array':
1190 //
1191 // count = ptr->count;
1192 //
1193 // array_element_size = sizeof (*ptr->array);
1194 // array_size = count * array_element_size;
1195 //
1196 // result = array_size;
1197 //
1198 // cmp = (result >= 0)
1199 // return cmp ? result : 0;
1200 //
1201 // 2) '&((cast) ptr->array)[idx]':
1202 //
1203 // count = ptr->count;
1204 // index = idx;
1205 //
1206 // array_element_size = sizeof (*ptr->array);
1207 // array_size = count * array_element_size;
1208 //
1209 // casted_array_element_size = sizeof (*((cast) ptr->array));
1210 //
1211 // index_size = index * casted_array_element_size;
1212 // result = array_size - index_size;
1213 //
1214 // cmp = (result >= 0)
1215 // if (index)
1216 // cmp = (cmp && index > 0)
1217 // return cmp ? result : 0;
1218
1219 auto GetElementBaseSize = [&](QualType ElementTy) {
1220 CharUnits ElementSize =
1221 getContext().getTypeSizeInChars(ElementTy->getPointeeType());
1222
1223 if (ElementSize.isZero()) {
1224 // This might be a __sized_by (or __counted_by) on a
1225 // 'void *', which counts bytes, not elements.
1226 [[maybe_unused]] auto *CAT = ElementTy->getAs<CountAttributedType>();
1227 assert(CAT && "must have an CountAttributedType");
1228
1229 ElementSize = CharUnits::One();
1230 }
1231
1232 return std::optional<CharUnits>(ElementSize);
1233 };
1234
1235 // Get the sizes of the original array element and the casted array element,
1236 // if different.
1237 std::optional<CharUnits> ArrayElementBaseSize =
1238 GetElementBaseSize(ArrayBaseFD->getType());
1239 if (!ArrayElementBaseSize)
1240 return nullptr;
1241
1242 std::optional<CharUnits> CastedArrayElementBaseSize = ArrayElementBaseSize;
1243 if (!CastedArrayElementTy.isNull() && CastedArrayElementTy->isPointerType()) {
1244 CastedArrayElementBaseSize = GetElementBaseSize(CastedArrayElementTy);
1245 if (!CastedArrayElementBaseSize)
1246 return nullptr;
1247 }
1248
1249 bool IsSigned = CountFD->getType()->isSignedIntegerType();
1250
1251 // count = ptr->count;
1252 // index = ptr->index;
1253 Value *Count, *Index;
1254 std::tie(Count, Index) = GetCountFieldAndIndex(
1255 *this, ME, ArrayBaseFD, CountFD, Idx, ResType, IsSigned);
1256 if (!Count)
1257 return nullptr;
1258
1259 // array_element_size = sizeof (*ptr->array)
1260 auto *ArrayElementSize = llvm::ConstantInt::get(
1261 ResType, ArrayElementBaseSize->getQuantity(), IsSigned);
1262
1263 // casted_array_element_size = sizeof (*((cast) ptr->array));
1264 auto *CastedArrayElementSize = llvm::ConstantInt::get(
1265 ResType, CastedArrayElementBaseSize->getQuantity(), IsSigned);
1266
1267 // array_size = count * array_element_size;
1268 Value *ArraySize = Builder.CreateMul(Count, ArrayElementSize, "array_size",
1269 !IsSigned, IsSigned);
1270
1271 // Option (1) 'ptr->array'
1272 // result = array_size
1273 Value *Result = ArraySize;
1274
1275 if (Idx) { // Option (2) '&((cast) ptr->array)[idx]'
1276 // index_size = index * casted_array_element_size;
1277 Value *IndexSize = Builder.CreateMul(Index, CastedArrayElementSize,
1278 "index_size", !IsSigned, IsSigned);
1279
1280 // result = result - index_size;
1281 Result =
1282 Builder.CreateSub(Result, IndexSize, "result", !IsSigned, IsSigned);
1283 }
1284
1285 return EmitPositiveResultOrZero(*this, Result, Index, ResType, IsSigned);
1286}
1287
1288llvm::Value *CodeGenFunction::emitCountedByMemberSize(
1289 const MemberExpr *ME, const Expr *Idx, llvm::Value *EmittedE,
1290 QualType CastedArrayElementTy, unsigned Type, llvm::IntegerType *ResType) {
1291 const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
1292 if (!FD)
1293 return nullptr;
1294
1295 // Find the flexible array member and check that it has the __counted_by
1296 // attribute.
1297 ASTContext &Ctx = getContext();
1298 const RecordDecl *RD = FD->getDeclContext()->getOuterLexicalRecordContext();
1299 const FieldDecl *FlexibleArrayMemberFD = nullptr;
1300
1302 Ctx, FD, FD->getType(), getLangOpts().getStrictFlexArraysLevel(),
1303 /*IgnoreTemplateOrMacroSubstitution=*/true))
1304 FlexibleArrayMemberFD = FD;
1305 else
1306 FlexibleArrayMemberFD = FindFlexibleArrayMemberField(*this, Ctx, RD);
1307
1308 if (!FlexibleArrayMemberFD ||
1309 !FlexibleArrayMemberFD->getType()->isCountAttributedType())
1310 return nullptr;
1311
1312 // Get the 'count' FieldDecl.
1313 const FieldDecl *CountFD = FlexibleArrayMemberFD->findCountedByField();
1314 if (!CountFD)
1315 // Can't find the field referenced by the "counted_by" attribute.
1316 return nullptr;
1317
1318 // Calculate the flexible array member's object size using these formulae.
1319 // (Note: if the calculation is negative, we return 0.):
1320 //
1321 // struct p;
1322 // struct s {
1323 // /* ... */
1324 // int count;
1325 // struct p *array[] __attribute__((counted_by(count)));
1326 // };
1327 //
1328 // 1) 'ptr->array':
1329 //
1330 // count = ptr->count;
1331 //
1332 // flexible_array_member_element_size = sizeof (*ptr->array);
1333 // flexible_array_member_size =
1334 // count * flexible_array_member_element_size;
1335 //
1336 // result = flexible_array_member_size;
1337 //
1338 // cmp = (result >= 0)
1339 // return cmp ? result : 0;
1340 //
1341 // 2) '&((cast) ptr->array)[idx]':
1342 //
1343 // count = ptr->count;
1344 // index = idx;
1345 //
1346 // flexible_array_member_element_size = sizeof (*ptr->array);
1347 // flexible_array_member_size =
1348 // count * flexible_array_member_element_size;
1349 //
1350 // casted_flexible_array_member_element_size =
1351 // sizeof (*((cast) ptr->array));
1352 // index_size = index * casted_flexible_array_member_element_size;
1353 //
1354 // result = flexible_array_member_size - index_size;
1355 //
1356 // cmp = (result >= 0)
1357 // if (index != 0)
1358 // cmp = (cmp && index >= 0)
1359 // return cmp ? result : 0;
1360 //
1361 // 3) '&ptr->field':
1362 //
1363 // count = ptr->count;
1364 // sizeof_struct = sizeof (struct s);
1365 //
1366 // flexible_array_member_element_size = sizeof (*ptr->array);
1367 // flexible_array_member_size =
1368 // count * flexible_array_member_element_size;
1369 //
1370 // field_offset = offsetof (struct s, field);
1371 // offset_diff = sizeof_struct - field_offset;
1372 //
1373 // result = offset_diff + flexible_array_member_size;
1374 //
1375 // cmp = (result >= 0)
1376 // return cmp ? result : 0;
1377 //
1378 // 4) '&((cast) ptr->field_array)[idx]':
1379 //
1380 // count = ptr->count;
1381 // index = idx;
1382 // sizeof_struct = sizeof (struct s);
1383 //
1384 // flexible_array_member_element_size = sizeof (*ptr->array);
1385 // flexible_array_member_size =
1386 // count * flexible_array_member_element_size;
1387 //
1388 // casted_field_element_size = sizeof (*((cast) ptr->field_array));
1389 // field_offset = offsetof (struct s, field)
1390 // field_offset += index * casted_field_element_size;
1391 //
1392 // offset_diff = sizeof_struct - field_offset;
1393 //
1394 // result = offset_diff + flexible_array_member_size;
1395 //
1396 // cmp = (result >= 0)
1397 // if (index != 0)
1398 // cmp = (cmp && index >= 0)
1399 // return cmp ? result : 0;
1400
1401 bool IsSigned = CountFD->getType()->isSignedIntegerType();
1402
1403 QualType FlexibleArrayMemberTy = FlexibleArrayMemberFD->getType();
1404
1405 // Explicit cast because otherwise the CharWidth will promote an i32's into
1406 // u64's leading to overflows.
1407 int64_t CharWidth = static_cast<int64_t>(CGM.getContext().getCharWidth());
1408
1409 // field_offset = offsetof (struct s, field);
1410 Value *FieldOffset = nullptr;
1411 if (FlexibleArrayMemberFD != FD) {
1412 std::optional<int64_t> Offset = GetFieldOffset(Ctx, RD, FD);
1413 if (!Offset)
1414 return nullptr;
1415 FieldOffset =
1416 llvm::ConstantInt::get(ResType, *Offset / CharWidth, IsSigned);
1417 }
1418
1419 // count = ptr->count;
1420 // index = ptr->index;
1421 Value *Count, *Index;
1422 std::tie(Count, Index) = GetCountFieldAndIndex(
1423 *this, ME, FlexibleArrayMemberFD, CountFD, Idx, ResType, IsSigned);
1424 if (!Count)
1425 return nullptr;
1426
1427 // flexible_array_member_element_size = sizeof (*ptr->array);
1428 const ArrayType *ArrayTy = Ctx.getAsArrayType(FlexibleArrayMemberTy);
1429 CharUnits BaseSize = Ctx.getTypeSizeInChars(ArrayTy->getElementType());
1430 auto *FlexibleArrayMemberElementSize =
1431 llvm::ConstantInt::get(ResType, BaseSize.getQuantity(), IsSigned);
1432
1433 // flexible_array_member_size = count * flexible_array_member_element_size;
1434 Value *FlexibleArrayMemberSize =
1435 Builder.CreateMul(Count, FlexibleArrayMemberElementSize,
1436 "flexible_array_member_size", !IsSigned, IsSigned);
1437
1438 Value *Result = nullptr;
1439 if (FlexibleArrayMemberFD == FD) {
1440 if (Idx) { // Option (2) '&((cast) ptr->array)[idx]'
1441 // casted_flexible_array_member_element_size =
1442 // sizeof (*((cast) ptr->array));
1443 llvm::ConstantInt *CastedFlexibleArrayMemberElementSize =
1444 FlexibleArrayMemberElementSize;
1445 if (!CastedArrayElementTy.isNull() &&
1446 CastedArrayElementTy->isPointerType()) {
1447 CharUnits BaseSize =
1448 Ctx.getTypeSizeInChars(CastedArrayElementTy->getPointeeType());
1449 CastedFlexibleArrayMemberElementSize =
1450 llvm::ConstantInt::get(ResType, BaseSize.getQuantity(), IsSigned);
1451 }
1452
1453 // index_size = index * casted_flexible_array_member_element_size;
1454 Value *IndexSize =
1455 Builder.CreateMul(Index, CastedFlexibleArrayMemberElementSize,
1456 "index_size", !IsSigned, IsSigned);
1457
1458 // result = flexible_array_member_size - index_size;
1459 Result = Builder.CreateSub(FlexibleArrayMemberSize, IndexSize, "result",
1460 !IsSigned, IsSigned);
1461 } else { // Option (1) 'ptr->array'
1462 // result = flexible_array_member_size;
1463 Result = FlexibleArrayMemberSize;
1464 }
1465 } else {
1466 // sizeof_struct = sizeof (struct s);
1467 llvm::StructType *StructTy = getTypes().getCGRecordLayout(RD).getLLVMType();
1468 const llvm::DataLayout &Layout = CGM.getDataLayout();
1469 TypeSize Size = Layout.getTypeSizeInBits(StructTy);
1470 Value *SizeofStruct =
1471 llvm::ConstantInt::get(ResType, Size.getKnownMinValue() / CharWidth);
1472
1473 if (Idx) { // Option (4) '&((cast) ptr->field_array)[idx]'
1474 // casted_field_element_size = sizeof (*((cast) ptr->field_array));
1475 CharUnits BaseSize;
1476 if (!CastedArrayElementTy.isNull() &&
1477 CastedArrayElementTy->isPointerType()) {
1478 BaseSize =
1479 Ctx.getTypeSizeInChars(CastedArrayElementTy->getPointeeType());
1480 } else {
1481 const ArrayType *ArrayTy = Ctx.getAsArrayType(FD->getType());
1482 BaseSize = Ctx.getTypeSizeInChars(ArrayTy->getElementType());
1483 }
1484
1485 llvm::ConstantInt *CastedFieldElementSize =
1486 llvm::ConstantInt::get(ResType, BaseSize.getQuantity(), IsSigned);
1487
1488 // field_offset += index * casted_field_element_size;
1489 Value *Mul = Builder.CreateMul(Index, CastedFieldElementSize,
1490 "field_offset", !IsSigned, IsSigned);
1491 FieldOffset = Builder.CreateAdd(FieldOffset, Mul);
1492 }
1493 // Option (3) '&ptr->field', and Option (4) continuation.
1494 // offset_diff = flexible_array_member_offset - field_offset;
1495 Value *OffsetDiff = Builder.CreateSub(SizeofStruct, FieldOffset,
1496 "offset_diff", !IsSigned, IsSigned);
1497
1498 // result = offset_diff + flexible_array_member_size;
1499 Result = Builder.CreateAdd(FlexibleArrayMemberSize, OffsetDiff, "result");
1500 }
1501
1502 return EmitPositiveResultOrZero(*this, Result, Index, ResType, IsSigned);
1503}
1504
1505/// Returns a Value corresponding to the size of the given expression.
1506/// This Value may be either of the following:
1507/// - A llvm::Argument (if E is a param with the pass_object_size attribute on
1508/// it)
1509/// - A call to the @llvm.objectsize intrinsic
1510///
1511/// EmittedE is the result of emitting `E` as a scalar expr. If it's non-null
1512/// and we wouldn't otherwise try to reference a pass_object_size parameter,
1513/// we'll call @llvm.objectsize on EmittedE, rather than emitting E.
1514llvm::Value *
1515CodeGenFunction::emitBuiltinObjectSize(const Expr *E, unsigned Type,
1516 llvm::IntegerType *ResType,
1517 llvm::Value *EmittedE, bool IsDynamic) {
1518 // We need to reference an argument if the pointer is a parameter with the
1519 // pass_object_size attribute.
1520 if (auto *D = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) {
1521 auto *Param = dyn_cast<ParmVarDecl>(D->getDecl());
1522 auto *PS = D->getDecl()->getAttr<PassObjectSizeAttr>();
1523 if (Param != nullptr && PS != nullptr &&
1524 areBOSTypesCompatible(PS->getType(), Type)) {
1525 auto Iter = SizeArguments.find(Param);
1526 assert(Iter != SizeArguments.end());
1527
1528 const ImplicitParamDecl *D = Iter->second;
1529 auto DIter = LocalDeclMap.find(D);
1530 assert(DIter != LocalDeclMap.end());
1531
1532 return EmitLoadOfScalar(DIter->second, /*Volatile=*/false,
1533 getContext().getSizeType(), E->getBeginLoc());
1534 }
1535 }
1536
1537 // LLVM can't handle Type=3 appropriately, and __builtin_object_size shouldn't
1538 // evaluate E for side-effects. In either case, we shouldn't lower to
1539 // @llvm.objectsize.
1540 if (Type == 3 || (!EmittedE && E->HasSideEffects(getContext())))
1541 return getDefaultBuiltinObjectSizeResult(Type, ResType);
1542
1543 Value *Ptr = EmittedE ? EmittedE : EmitScalarExpr(E);
1544 assert(Ptr->getType()->isPointerTy() &&
1545 "Non-pointer passed to __builtin_object_size?");
1546
1547 if (IsDynamic)
1548 // Emit special code for a flexible array member with the "counted_by"
1549 // attribute.
1550 if (Value *V = emitCountedBySize(E, Ptr, Type, ResType))
1551 return V;
1552
1553 Function *F =
1554 CGM.getIntrinsic(Intrinsic::objectsize, {ResType, Ptr->getType()});
1555
1556 // LLVM only supports 0 and 2, make sure that we pass along that as a boolean.
1557 Value *Min = Builder.getInt1((Type & 2) != 0);
1558 // For GCC compatibility, __builtin_object_size treat NULL as unknown size.
1559 Value *NullIsUnknown = Builder.getTrue();
1560 Value *Dynamic = Builder.getInt1(IsDynamic);
1561 return Builder.CreateCall(F, {Ptr, Min, NullIsUnknown, Dynamic});
1562}
1563
1564namespace {
1565/// A struct to generically describe a bit test intrinsic.
1566struct BitTest {
1567 enum ActionKind : uint8_t { TestOnly, Complement, Reset, Set };
1568 enum InterlockingKind : uint8_t {
1569 Unlocked,
1570 Sequential,
1571 Acquire,
1572 Release,
1573 NoFence
1574 };
1575
1576 ActionKind Action;
1577 InterlockingKind Interlocking;
1578 bool Is64Bit;
1579
1580 static BitTest decodeBitTestBuiltin(unsigned BuiltinID);
1581};
1582
1583} // namespace
1584
1585BitTest BitTest::decodeBitTestBuiltin(unsigned BuiltinID) {
1586 switch (BuiltinID) {
1587 // Main portable variants.
1588 case Builtin::BI_bittest:
1589 return {TestOnly, Unlocked, false};
1590 case Builtin::BI_bittestandcomplement:
1591 return {Complement, Unlocked, false};
1592 case Builtin::BI_bittestandreset:
1593 return {Reset, Unlocked, false};
1594 case Builtin::BI_bittestandset:
1595 return {Set, Unlocked, false};
1596 case Builtin::BI_interlockedbittestandreset:
1597 return {Reset, Sequential, false};
1598 case Builtin::BI_interlockedbittestandset:
1599 return {Set, Sequential, false};
1600
1601 // 64-bit variants.
1602 case Builtin::BI_bittest64:
1603 return {TestOnly, Unlocked, true};
1604 case Builtin::BI_bittestandcomplement64:
1605 return {Complement, Unlocked, true};
1606 case Builtin::BI_bittestandreset64:
1607 return {Reset, Unlocked, true};
1608 case Builtin::BI_bittestandset64:
1609 return {Set, Unlocked, true};
1610 case Builtin::BI_interlockedbittestandreset64:
1611 return {Reset, Sequential, true};
1612 case Builtin::BI_interlockedbittestandset64:
1613 return {Set, Sequential, true};
1614
1615 // ARM/AArch64-specific ordering variants.
1616 case Builtin::BI_interlockedbittestandset_acq:
1617 return {Set, Acquire, false};
1618 case Builtin::BI_interlockedbittestandset_rel:
1619 return {Set, Release, false};
1620 case Builtin::BI_interlockedbittestandset_nf:
1621 return {Set, NoFence, false};
1622 case Builtin::BI_interlockedbittestandreset_acq:
1623 return {Reset, Acquire, false};
1624 case Builtin::BI_interlockedbittestandreset_rel:
1625 return {Reset, Release, false};
1626 case Builtin::BI_interlockedbittestandreset_nf:
1627 return {Reset, NoFence, false};
1628 case Builtin::BI_interlockedbittestandreset64_acq:
1629 return {Reset, Acquire, false};
1630 case Builtin::BI_interlockedbittestandreset64_rel:
1631 return {Reset, Release, false};
1632 case Builtin::BI_interlockedbittestandreset64_nf:
1633 return {Reset, NoFence, false};
1634 case Builtin::BI_interlockedbittestandset64_acq:
1635 return {Set, Acquire, false};
1636 case Builtin::BI_interlockedbittestandset64_rel:
1637 return {Set, Release, false};
1638 case Builtin::BI_interlockedbittestandset64_nf:
1639 return {Set, NoFence, false};
1640 }
1641 llvm_unreachable("expected only bittest intrinsics");
1642}
1643
1644static char bitActionToX86BTCode(BitTest::ActionKind A) {
1645 switch (A) {
1646 case BitTest::TestOnly: return '\0';
1647 case BitTest::Complement: return 'c';
1648 case BitTest::Reset: return 'r';
1649 case BitTest::Set: return 's';
1650 }
1651 llvm_unreachable("invalid action");
1652}
1653
1655 BitTest BT,
1656 const CallExpr *E, Value *BitBase,
1657 Value *BitPos) {
1658 char Action = bitActionToX86BTCode(BT.Action);
1659 char SizeSuffix = BT.Is64Bit ? 'q' : 'l';
1660
1661 // Build the assembly.
1663 raw_svector_ostream AsmOS(Asm);
1664 if (BT.Interlocking != BitTest::Unlocked)
1665 AsmOS << "lock ";
1666 AsmOS << "bt";
1667 if (Action)
1668 AsmOS << Action;
1669 AsmOS << SizeSuffix << " $2, ($1)";
1670
1671 // Build the constraints. FIXME: We should support immediates when possible.
1672 std::string Constraints = "={@ccc},r,r,~{cc},~{memory}";
1673 std::string_view MachineClobbers = CGF.getTarget().getClobbers();
1674 if (!MachineClobbers.empty()) {
1675 Constraints += ',';
1676 Constraints += MachineClobbers;
1677 }
1678 llvm::IntegerType *IntType = llvm::IntegerType::get(
1679 CGF.getLLVMContext(),
1680 CGF.getContext().getTypeSize(E->getArg(1)->getType()));
1681 llvm::FunctionType *FTy =
1682 llvm::FunctionType::get(CGF.Int8Ty, {CGF.DefaultPtrTy, IntType}, false);
1683
1684 llvm::InlineAsm *IA =
1685 llvm::InlineAsm::get(FTy, Asm, Constraints, /*hasSideEffects=*/true);
1686 return CGF.Builder.CreateCall(IA, {BitBase, BitPos});
1687}
1688
1689static llvm::AtomicOrdering
1690getBitTestAtomicOrdering(BitTest::InterlockingKind I) {
1691 switch (I) {
1692 case BitTest::Unlocked: return llvm::AtomicOrdering::NotAtomic;
1693 case BitTest::Sequential: return llvm::AtomicOrdering::SequentiallyConsistent;
1694 case BitTest::Acquire: return llvm::AtomicOrdering::Acquire;
1695 case BitTest::Release: return llvm::AtomicOrdering::Release;
1696 case BitTest::NoFence: return llvm::AtomicOrdering::Monotonic;
1697 }
1698 llvm_unreachable("invalid interlocking");
1699}
1700
1701static llvm::Value *EmitBitCountExpr(CodeGenFunction &CGF, const Expr *E) {
1702 llvm::Value *ArgValue = CGF.EmitScalarExpr(E);
1703 llvm::Type *ArgType = ArgValue->getType();
1704
1705 // Boolean vectors can be casted directly to its bitfield representation. We
1706 // intentionally do not round up to the next power of two size and let LLVM
1707 // handle the trailing bits.
1708 if (auto *VT = dyn_cast<llvm::FixedVectorType>(ArgType);
1709 VT && VT->getElementType()->isIntegerTy(1)) {
1710 llvm::Type *StorageType =
1711 llvm::Type::getIntNTy(CGF.getLLVMContext(), VT->getNumElements());
1712 ArgValue = CGF.Builder.CreateBitCast(ArgValue, StorageType);
1713 }
1714
1715 return ArgValue;
1716}
1717
1718/// Emit a _bittest* intrinsic. These intrinsics take a pointer to an array of
1719/// bits and a bit position and read and optionally modify the bit at that
1720/// position. The position index can be arbitrarily large, i.e. it can be larger
1721/// than 31 or 63, so we need an indexed load in the general case.
1722static llvm::Value *EmitBitTestIntrinsic(CodeGenFunction &CGF,
1723 unsigned BuiltinID,
1724 const CallExpr *E) {
1725 Value *BitBase = CGF.EmitScalarExpr(E->getArg(0));
1726 Value *BitPos = CGF.EmitScalarExpr(E->getArg(1));
1727
1728 BitTest BT = BitTest::decodeBitTestBuiltin(BuiltinID);
1729
1730 // X86 has special BT, BTC, BTR, and BTS instructions that handle the array
1731 // indexing operation internally. Use them if possible.
1732 if (CGF.getTarget().getTriple().isX86())
1733 return EmitX86BitTestIntrinsic(CGF, BT, E, BitBase, BitPos);
1734
1735 // Otherwise, use generic code to load one byte and test the bit. Use all but
1736 // the bottom three bits as the array index, and the bottom three bits to form
1737 // a mask.
1738 // Bit = BitBaseI8[BitPos >> 3] & (1 << (BitPos & 0x7)) != 0;
1739 Value *ByteIndex = CGF.Builder.CreateAShr(
1740 BitPos, llvm::ConstantInt::get(BitPos->getType(), 3), "bittest.byteidx");
1741 Address ByteAddr(CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, BitBase, ByteIndex,
1742 "bittest.byteaddr"),
1743 CGF.Int8Ty, CharUnits::One());
1744 Value *PosLow =
1745 CGF.Builder.CreateAnd(CGF.Builder.CreateTrunc(BitPos, CGF.Int8Ty),
1746 llvm::ConstantInt::get(CGF.Int8Ty, 0x7));
1747
1748 // The updating instructions will need a mask.
1749 Value *Mask = nullptr;
1750 if (BT.Action != BitTest::TestOnly) {
1751 Mask = CGF.Builder.CreateShl(llvm::ConstantInt::get(CGF.Int8Ty, 1), PosLow,
1752 "bittest.mask");
1753 }
1754
1755 // Check the action and ordering of the interlocked intrinsics.
1756 llvm::AtomicOrdering Ordering = getBitTestAtomicOrdering(BT.Interlocking);
1757
1758 Value *OldByte = nullptr;
1759 if (Ordering != llvm::AtomicOrdering::NotAtomic) {
1760 // Emit a combined atomicrmw load/store operation for the interlocked
1761 // intrinsics.
1762 llvm::AtomicRMWInst::BinOp RMWOp = llvm::AtomicRMWInst::Or;
1763 if (BT.Action == BitTest::Reset) {
1764 Mask = CGF.Builder.CreateNot(Mask);
1765 RMWOp = llvm::AtomicRMWInst::And;
1766 }
1767 OldByte = CGF.Builder.CreateAtomicRMW(RMWOp, ByteAddr, Mask, Ordering);
1768 } else {
1769 // Emit a plain load for the non-interlocked intrinsics.
1770 OldByte = CGF.Builder.CreateLoad(ByteAddr, "bittest.byte");
1771 Value *NewByte = nullptr;
1772 switch (BT.Action) {
1773 case BitTest::TestOnly:
1774 // Don't store anything.
1775 break;
1776 case BitTest::Complement:
1777 NewByte = CGF.Builder.CreateXor(OldByte, Mask);
1778 break;
1779 case BitTest::Reset:
1780 NewByte = CGF.Builder.CreateAnd(OldByte, CGF.Builder.CreateNot(Mask));
1781 break;
1782 case BitTest::Set:
1783 NewByte = CGF.Builder.CreateOr(OldByte, Mask);
1784 break;
1785 }
1786 if (NewByte)
1787 CGF.Builder.CreateStore(NewByte, ByteAddr);
1788 }
1789
1790 // However we loaded the old byte, either by plain load or atomicrmw, shift
1791 // the bit into the low position and mask it to 0 or 1.
1792 Value *ShiftedByte = CGF.Builder.CreateLShr(OldByte, PosLow, "bittest.shr");
1793 return CGF.Builder.CreateAnd(
1794 ShiftedByte, llvm::ConstantInt::get(CGF.Int8Ty, 1), "bittest.res");
1795}
1796
1797namespace {
1798enum class MSVCSetJmpKind {
1799 _setjmpex,
1800 _setjmp3,
1801 _setjmp
1802};
1803}
1804
1805/// MSVC handles setjmp a bit differently on different platforms. On every
1806/// architecture except 32-bit x86, the frame address is passed. On x86, extra
1807/// parameters can be passed as variadic arguments, but we always pass none.
1808static RValue EmitMSVCRTSetJmp(CodeGenFunction &CGF, MSVCSetJmpKind SJKind,
1809 const CallExpr *E) {
1810 llvm::Value *Arg1 = nullptr;
1811 llvm::Type *Arg1Ty = nullptr;
1812 StringRef Name;
1813 bool IsVarArg = false;
1814 if (SJKind == MSVCSetJmpKind::_setjmp3) {
1815 Name = "_setjmp3";
1816 Arg1Ty = CGF.Int32Ty;
1817 Arg1 = llvm::ConstantInt::get(CGF.IntTy, 0);
1818 IsVarArg = true;
1819 } else {
1820 Name = SJKind == MSVCSetJmpKind::_setjmp ? "_setjmp" : "_setjmpex";
1821 Arg1Ty = CGF.Int8PtrTy;
1822 if (CGF.getTarget().getTriple().getArch() == llvm::Triple::aarch64) {
1823 Arg1 = CGF.Builder.CreateCall(
1824 CGF.CGM.getIntrinsic(Intrinsic::sponentry, CGF.AllocaInt8PtrTy));
1825 } else
1826 Arg1 = CGF.Builder.CreateCall(
1827 CGF.CGM.getIntrinsic(Intrinsic::frameaddress, CGF.AllocaInt8PtrTy),
1828 llvm::ConstantInt::get(CGF.Int32Ty, 0));
1829 }
1830
1831 // Mark the call site and declaration with ReturnsTwice.
1832 llvm::Type *ArgTypes[2] = {CGF.Int8PtrTy, Arg1Ty};
1833 llvm::AttributeList ReturnsTwiceAttr = llvm::AttributeList::get(
1834 CGF.getLLVMContext(), llvm::AttributeList::FunctionIndex,
1835 llvm::Attribute::ReturnsTwice);
1836 llvm::FunctionCallee SetJmpFn = CGF.CGM.CreateRuntimeFunction(
1837 llvm::FunctionType::get(CGF.IntTy, ArgTypes, IsVarArg), Name,
1838 ReturnsTwiceAttr, /*Local=*/true);
1839
1840 llvm::Value *Buf = CGF.Builder.CreateBitOrPointerCast(
1841 CGF.EmitScalarExpr(E->getArg(0)), CGF.Int8PtrTy);
1842 llvm::Value *Args[] = {Buf, Arg1};
1843 llvm::CallBase *CB = CGF.EmitRuntimeCallOrInvoke(SetJmpFn, Args);
1844 CB->setAttributes(ReturnsTwiceAttr);
1845 return RValue::get(CB);
1846}
1847
1848// Emit an MSVC intrinsic. Assumes that arguments have *not* been evaluated.
1850 const CallExpr *E) {
1851 switch (BuiltinID) {
1854 Address IndexAddress(EmitPointerWithAlignment(E->getArg(0)));
1855 Value *ArgValue = EmitScalarExpr(E->getArg(1));
1856
1857 llvm::Type *ArgType = ArgValue->getType();
1858 llvm::Type *IndexType = IndexAddress.getElementType();
1859 llvm::Type *ResultType = ConvertType(E->getType());
1860
1861 Value *ArgZero = llvm::Constant::getNullValue(ArgType);
1862 Value *ResZero = llvm::Constant::getNullValue(ResultType);
1863 Value *ResOne = llvm::ConstantInt::get(ResultType, 1);
1864
1865 BasicBlock *Begin = Builder.GetInsertBlock();
1866 BasicBlock *End = createBasicBlock("bitscan_end", this->CurFn);
1867 Builder.SetInsertPoint(End);
1868 PHINode *Result = Builder.CreatePHI(ResultType, 2, "bitscan_result");
1869
1870 Builder.SetInsertPoint(Begin);
1871 Value *IsZero = Builder.CreateICmpEQ(ArgValue, ArgZero);
1872 BasicBlock *NotZero = createBasicBlock("bitscan_not_zero", this->CurFn);
1873 Builder.CreateCondBr(IsZero, End, NotZero);
1874 Result->addIncoming(ResZero, Begin);
1875
1876 Builder.SetInsertPoint(NotZero);
1877
1878 if (BuiltinID == MSVCIntrin::_BitScanForward) {
1879 Function *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
1880 Value *ZeroCount = Builder.CreateCall(F, {ArgValue, Builder.getTrue()});
1881 ZeroCount = Builder.CreateIntCast(ZeroCount, IndexType, false);
1882 Builder.CreateStore(ZeroCount, IndexAddress, false);
1883 } else {
1884 unsigned ArgWidth = cast<llvm::IntegerType>(ArgType)->getBitWidth();
1885 Value *ArgTypeLastIndex = llvm::ConstantInt::get(IndexType, ArgWidth - 1);
1886
1887 Function *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
1888 Value *ZeroCount = Builder.CreateCall(F, {ArgValue, Builder.getTrue()});
1889 ZeroCount = Builder.CreateIntCast(ZeroCount, IndexType, false);
1890 Value *Index = Builder.CreateNSWSub(ArgTypeLastIndex, ZeroCount);
1891 Builder.CreateStore(Index, IndexAddress, false);
1892 }
1893 Builder.CreateBr(End);
1894 Result->addIncoming(ResOne, NotZero);
1895
1896 Builder.SetInsertPoint(End);
1897 return Result;
1898 }
1900 return MakeBinaryAtomicValue(*this, AtomicRMWInst::And, E);
1902 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xchg, E);
1904 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Add, E);
1906 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Sub, E);
1908 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Or, E);
1910 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xor, E);
1912 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Add, E,
1913 AtomicOrdering::Acquire);
1915 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Add, E,
1916 AtomicOrdering::Release);
1918 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Add, E,
1919 AtomicOrdering::Monotonic);
1921 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xchg, E,
1922 AtomicOrdering::Acquire);
1924 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xchg, E,
1925 AtomicOrdering::Release);
1927 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xchg, E,
1928 AtomicOrdering::Monotonic);
1930 return EmitAtomicCmpXchgForMSIntrin(*this, E);
1932 return EmitAtomicCmpXchgForMSIntrin(*this, E, AtomicOrdering::Acquire);
1934 return EmitAtomicCmpXchgForMSIntrin(*this, E, AtomicOrdering::Release);
1936 return EmitAtomicCmpXchgForMSIntrin(*this, E, AtomicOrdering::Monotonic);
1939 *this, E, AtomicOrdering::SequentiallyConsistent);
1941 return EmitAtomicCmpXchg128ForMSIntrin(*this, E, AtomicOrdering::Acquire);
1943 return EmitAtomicCmpXchg128ForMSIntrin(*this, E, AtomicOrdering::Release);
1945 return EmitAtomicCmpXchg128ForMSIntrin(*this, E, AtomicOrdering::Monotonic);
1947 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Or, E,
1948 AtomicOrdering::Acquire);
1950 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Or, E,
1951 AtomicOrdering::Release);
1953 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Or, E,
1954 AtomicOrdering::Monotonic);
1956 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xor, E,
1957 AtomicOrdering::Acquire);
1959 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xor, E,
1960 AtomicOrdering::Release);
1962 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xor, E,
1963 AtomicOrdering::Monotonic);
1965 return MakeBinaryAtomicValue(*this, AtomicRMWInst::And, E,
1966 AtomicOrdering::Acquire);
1968 return MakeBinaryAtomicValue(*this, AtomicRMWInst::And, E,
1969 AtomicOrdering::Release);
1971 return MakeBinaryAtomicValue(*this, AtomicRMWInst::And, E,
1972 AtomicOrdering::Monotonic);
1974 return EmitAtomicIncrementValue(*this, E, AtomicOrdering::Acquire);
1976 return EmitAtomicIncrementValue(*this, E, AtomicOrdering::Release);
1978 return EmitAtomicIncrementValue(*this, E, AtomicOrdering::Monotonic);
1980 return EmitAtomicDecrementValue(*this, E, AtomicOrdering::Acquire);
1982 return EmitAtomicDecrementValue(*this, E, AtomicOrdering::Release);
1984 return EmitAtomicDecrementValue(*this, E, AtomicOrdering::Monotonic);
1985
1987 return EmitAtomicDecrementValue(*this, E);
1989 return EmitAtomicIncrementValue(*this, E);
1990
1992 // Request immediate process termination from the kernel. The instruction
1993 // sequences to do this are documented on MSDN:
1994 // https://msdn.microsoft.com/en-us/library/dn774154.aspx
1995 llvm::Triple::ArchType ISA = getTarget().getTriple().getArch();
1996 StringRef Asm, Constraints;
1997 switch (ISA) {
1998 default:
1999 ErrorUnsupported(E, "__fastfail call for this architecture");
2000 break;
2001 case llvm::Triple::x86:
2002 case llvm::Triple::x86_64:
2003 Asm = "int $$0x29";
2004 Constraints = "{cx}";
2005 break;
2006 case llvm::Triple::thumb:
2007 Asm = "udf #251";
2008 Constraints = "{r0}";
2009 break;
2010 case llvm::Triple::aarch64:
2011 Asm = "brk #0xF003";
2012 Constraints = "{w0}";
2013 }
2014 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, {Int32Ty}, false);
2015 llvm::InlineAsm *IA =
2016 llvm::InlineAsm::get(FTy, Asm, Constraints, /*hasSideEffects=*/true);
2017 llvm::AttributeList NoReturnAttr = llvm::AttributeList::get(
2018 getLLVMContext(), llvm::AttributeList::FunctionIndex,
2019 llvm::Attribute::NoReturn);
2020 llvm::CallInst *CI = Builder.CreateCall(IA, EmitScalarExpr(E->getArg(0)));
2021 CI->setAttributes(NoReturnAttr);
2022 return CI;
2023 }
2024 }
2025 llvm_unreachable("Incorrect MSVC intrinsic!");
2026}
2027
2028namespace {
2029// ARC cleanup for __builtin_os_log_format
2030struct CallObjCArcUse final : EHScopeStack::Cleanup {
2031 CallObjCArcUse(llvm::Value *object) : object(object) {}
2032 llvm::Value *object;
2033
2034 void Emit(CodeGenFunction &CGF, Flags flags) override {
2035 CGF.EmitARCIntrinsicUse(object);
2036 }
2037};
2038}
2039
2041 BuiltinCheckKind Kind) {
2042 assert((Kind == BCK_CLZPassedZero || Kind == BCK_CTZPassedZero) &&
2043 "Unsupported builtin check kind");
2044
2045 Value *ArgValue = EmitBitCountExpr(*this, E);
2046 if (!SanOpts.has(SanitizerKind::Builtin))
2047 return ArgValue;
2048
2049 auto CheckOrdinal = SanitizerKind::SO_Builtin;
2050 auto CheckHandler = SanitizerHandler::InvalidBuiltin;
2051 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
2052 Value *Cond = Builder.CreateICmpNE(
2053 ArgValue, llvm::Constant::getNullValue(ArgValue->getType()));
2054 EmitCheck(std::make_pair(Cond, CheckOrdinal), CheckHandler,
2056 llvm::ConstantInt::get(Builder.getInt8Ty(), Kind)},
2057 {});
2058 return ArgValue;
2059}
2060
2062 Value *ArgValue = EvaluateExprAsBool(E);
2063 if (!SanOpts.has(SanitizerKind::Builtin))
2064 return ArgValue;
2065
2066 auto CheckOrdinal = SanitizerKind::SO_Builtin;
2067 auto CheckHandler = SanitizerHandler::InvalidBuiltin;
2068 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
2069 EmitCheck(
2070 std::make_pair(ArgValue, CheckOrdinal), CheckHandler,
2072 llvm::ConstantInt::get(Builder.getInt8Ty(), BCK_AssumePassedFalse)},
2073 {});
2074 return ArgValue;
2075}
2076
2077static Value *EmitAbs(CodeGenFunction &CGF, Value *ArgValue, bool HasNSW) {
2078 return CGF.Builder.CreateBinaryIntrinsic(
2079 Intrinsic::abs, ArgValue,
2080 ConstantInt::get(CGF.Builder.getInt1Ty(), HasNSW));
2081}
2082
2084 bool SanitizeOverflow) {
2085 Value *ArgValue = CGF.EmitScalarExpr(E->getArg(0));
2086
2087 // Try to eliminate overflow check.
2088 if (const auto *VCI = dyn_cast<llvm::ConstantInt>(ArgValue)) {
2089 if (!VCI->isMinSignedValue())
2090 return EmitAbs(CGF, ArgValue, true);
2091 }
2092
2094 SanitizerHandler CheckHandler;
2095 if (SanitizeOverflow) {
2096 Ordinals.push_back(SanitizerKind::SO_SignedIntegerOverflow);
2097 CheckHandler = SanitizerHandler::NegateOverflow;
2098 } else
2099 CheckHandler = SanitizerHandler::SubOverflow;
2100
2101 SanitizerDebugLocation SanScope(&CGF, Ordinals, CheckHandler);
2102
2103 Constant *Zero = Constant::getNullValue(ArgValue->getType());
2104 Value *ResultAndOverflow = CGF.Builder.CreateBinaryIntrinsic(
2105 Intrinsic::ssub_with_overflow, Zero, ArgValue);
2106 Value *Result = CGF.Builder.CreateExtractValue(ResultAndOverflow, 0);
2107 Value *NotOverflow = CGF.Builder.CreateNot(
2108 CGF.Builder.CreateExtractValue(ResultAndOverflow, 1));
2109
2110 // TODO: support -ftrapv-handler.
2111 if (SanitizeOverflow) {
2112 CGF.EmitCheck({{NotOverflow, SanitizerKind::SO_SignedIntegerOverflow}},
2113 CheckHandler,
2116 {ArgValue});
2117 } else
2118 CGF.EmitTrapCheck(NotOverflow, CheckHandler);
2119
2120 Value *CmpResult = CGF.Builder.CreateICmpSLT(ArgValue, Zero, "abscond");
2121 return CGF.Builder.CreateSelect(CmpResult, Result, ArgValue, "abs");
2122}
2123
2124/// Get the argument type for arguments to os_log_helper.
2126 QualType UnsignedTy = C.getIntTypeForBitwidth(Size * 8, /*Signed=*/false);
2127 return C.getCanonicalType(UnsignedTy);
2128}
2129
2132 CharUnits BufferAlignment) {
2133 ASTContext &Ctx = getContext();
2134
2136 {
2137 raw_svector_ostream OS(Name);
2138 OS << "__os_log_helper";
2139 OS << "_" << BufferAlignment.getQuantity();
2140 OS << "_" << int(Layout.getSummaryByte());
2141 OS << "_" << int(Layout.getNumArgsByte());
2142 for (const auto &Item : Layout.Items)
2143 OS << "_" << int(Item.getSizeByte()) << "_"
2144 << int(Item.getDescriptorByte());
2145 }
2146
2147 if (llvm::Function *F = CGM.getModule().getFunction(Name))
2148 return F;
2149
2151 FunctionArgList Args;
2152 Args.push_back(ImplicitParamDecl::Create(
2153 Ctx, nullptr, SourceLocation(), &Ctx.Idents.get("buffer"), Ctx.VoidPtrTy,
2155 ArgTys.emplace_back(Ctx.VoidPtrTy);
2156
2157 for (unsigned int I = 0, E = Layout.Items.size(); I < E; ++I) {
2158 char Size = Layout.Items[I].getSizeByte();
2159 if (!Size)
2160 continue;
2161
2162 QualType ArgTy = getOSLogArgType(Ctx, Size);
2163 Args.push_back(ImplicitParamDecl::Create(
2164 Ctx, nullptr, SourceLocation(),
2165 &Ctx.Idents.get(std::string("arg") + llvm::to_string(I)), ArgTy,
2167 ArgTys.emplace_back(ArgTy);
2168 }
2169
2170 QualType ReturnTy = Ctx.VoidTy;
2171
2172 // The helper function has linkonce_odr linkage to enable the linker to merge
2173 // identical functions. To ensure the merging always happens, 'noinline' is
2174 // attached to the function when compiling with -Oz.
2175 const CGFunctionInfo &FI =
2176 CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, Args);
2177 llvm::FunctionType *FuncTy = CGM.getTypes().GetFunctionType(FI);
2178 llvm::Function *Fn = llvm::Function::Create(
2179 FuncTy, llvm::GlobalValue::LinkOnceODRLinkage, Name, &CGM.getModule());
2180 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
2181 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Fn, /*IsThunk=*/false);
2182 CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Fn);
2183 Fn->setDoesNotThrow();
2184
2185 // Attach 'noinline' at -Oz.
2186 if (CGM.getCodeGenOpts().OptimizeSize == 2)
2187 Fn->addFnAttr(llvm::Attribute::NoInline);
2188
2189 auto NL = ApplyDebugLocation::CreateEmpty(*this);
2190 StartFunction(GlobalDecl(), ReturnTy, Fn, FI, Args);
2191
2192 // Create a scope with an artificial location for the body of this function.
2193 auto AL = ApplyDebugLocation::CreateArtificial(*this);
2194
2195 CharUnits Offset;
2197 Builder.CreateLoad(GetAddrOfLocalVar(Args[0]), "buf"), Ctx.VoidTy,
2198 BufferAlignment);
2199 Builder.CreateStore(Builder.getInt8(Layout.getSummaryByte()),
2200 Builder.CreateConstByteGEP(BufAddr, Offset++, "summary"));
2201 Builder.CreateStore(Builder.getInt8(Layout.getNumArgsByte()),
2202 Builder.CreateConstByteGEP(BufAddr, Offset++, "numArgs"));
2203
2204 unsigned I = 1;
2205 for (const auto &Item : Layout.Items) {
2206 Builder.CreateStore(
2207 Builder.getInt8(Item.getDescriptorByte()),
2208 Builder.CreateConstByteGEP(BufAddr, Offset++, "argDescriptor"));
2209 Builder.CreateStore(
2210 Builder.getInt8(Item.getSizeByte()),
2211 Builder.CreateConstByteGEP(BufAddr, Offset++, "argSize"));
2212
2213 CharUnits Size = Item.size();
2214 if (!Size.getQuantity())
2215 continue;
2216
2217 Address Arg = GetAddrOfLocalVar(Args[I]);
2218 Address Addr = Builder.CreateConstByteGEP(BufAddr, Offset, "argData");
2219 Addr = Addr.withElementType(Arg.getElementType());
2220 Builder.CreateStore(Builder.CreateLoad(Arg), Addr);
2221 Offset += Size;
2222 ++I;
2223 }
2224
2226
2227 return Fn;
2228}
2229
2231 assert(E.getNumArgs() >= 2 &&
2232 "__builtin_os_log_format takes at least 2 arguments");
2233 ASTContext &Ctx = getContext();
2236 Address BufAddr = EmitPointerWithAlignment(E.getArg(0));
2237
2238 // Ignore argument 1, the format string. It is not currently used.
2239 CallArgList Args;
2240 Args.add(RValue::get(BufAddr.emitRawPointer(*this)), Ctx.VoidPtrTy);
2241
2242 for (const auto &Item : Layout.Items) {
2243 int Size = Item.getSizeByte();
2244 if (!Size)
2245 continue;
2246
2247 llvm::Value *ArgVal;
2248
2249 if (Item.getKind() == analyze_os_log::OSLogBufferItem::MaskKind) {
2250 uint64_t Val = 0;
2251 for (unsigned I = 0, E = Item.getMaskType().size(); I < E; ++I)
2252 Val |= ((uint64_t)Item.getMaskType()[I]) << I * 8;
2253 ArgVal = llvm::Constant::getIntegerValue(Int64Ty, llvm::APInt(64, Val));
2254 } else if (const Expr *TheExpr = Item.getExpr()) {
2255 ArgVal = EmitScalarExpr(TheExpr, /*Ignore*/ false);
2256
2257 // If a temporary object that requires destruction after the full
2258 // expression is passed, push a lifetime-extended cleanup to extend its
2259 // lifetime to the end of the enclosing block scope.
2260 auto LifetimeExtendObject = [&](const Expr *E) {
2261 E = E->IgnoreParenCasts();
2262 // Extend lifetimes of objects returned by function calls and message
2263 // sends.
2264
2265 // FIXME: We should do this in other cases in which temporaries are
2266 // created including arguments of non-ARC types (e.g., C++
2267 // temporaries).
2269 return true;
2270 return false;
2271 };
2272
2273 if (TheExpr->getType()->isObjCRetainableType() &&
2274 getLangOpts().ObjCAutoRefCount && LifetimeExtendObject(TheExpr)) {
2275 assert(getEvaluationKind(TheExpr->getType()) == TEK_Scalar &&
2276 "Only scalar can be a ObjC retainable type");
2277 if (!isa<Constant>(ArgVal)) {
2278 CleanupKind Cleanup = getARCCleanupKind();
2279 QualType Ty = TheExpr->getType();
2281 RawAddress Addr = CreateMemTemp(Ty, "os.log.arg", &Alloca);
2282 ArgVal = EmitARCRetain(Ty, ArgVal);
2283 Builder.CreateStore(ArgVal, Addr);
2284 pushLifetimeExtendedDestroy(Cleanup, Alloca, Ty,
2286 Cleanup & EHCleanup);
2287
2288 // Push a clang.arc.use call to ensure ARC optimizer knows that the
2289 // argument has to be alive.
2290 if (CGM.getCodeGenOpts().OptimizationLevel != 0)
2292 }
2293 }
2294 } else {
2295 ArgVal = Builder.getInt32(Item.getConstValue().getQuantity());
2296 }
2297
2298 unsigned ArgValSize =
2299 CGM.getDataLayout().getTypeSizeInBits(ArgVal->getType());
2300 llvm::IntegerType *IntTy = llvm::Type::getIntNTy(getLLVMContext(),
2301 ArgValSize);
2302 ArgVal = Builder.CreateBitOrPointerCast(ArgVal, IntTy);
2303 CanQualType ArgTy = getOSLogArgType(Ctx, Size);
2304 // If ArgVal has type x86_fp80, zero-extend ArgVal.
2305 ArgVal = Builder.CreateZExtOrBitCast(ArgVal, ConvertType(ArgTy));
2306 Args.add(RValue::get(ArgVal), ArgTy);
2307 }
2308
2309 const CGFunctionInfo &FI =
2310 CGM.getTypes().arrangeBuiltinFunctionCall(Ctx.VoidTy, Args);
2311 llvm::Function *F = CodeGenFunction(CGM).generateBuiltinOSLogHelperFunction(
2312 Layout, BufAddr.getAlignment());
2314 return RValue::get(BufAddr, *this);
2315}
2316
2318 unsigned BuiltinID, WidthAndSignedness Op1Info, WidthAndSignedness Op2Info,
2319 WidthAndSignedness ResultInfo) {
2320 return BuiltinID == Builtin::BI__builtin_mul_overflow &&
2321 Op1Info.Width == Op2Info.Width && Op2Info.Width == ResultInfo.Width &&
2322 !Op1Info.Signed && !Op2Info.Signed && ResultInfo.Signed;
2323}
2324
2326 CodeGenFunction &CGF, const clang::Expr *Op1, WidthAndSignedness Op1Info,
2327 const clang::Expr *Op2, WidthAndSignedness Op2Info,
2328 const clang::Expr *ResultArg, QualType ResultQTy,
2329 WidthAndSignedness ResultInfo) {
2331 Builtin::BI__builtin_mul_overflow, Op1Info, Op2Info, ResultInfo) &&
2332 "Cannot specialize this multiply");
2333
2334 llvm::Value *V1 = CGF.EmitScalarExpr(Op1);
2335 llvm::Value *V2 = CGF.EmitScalarExpr(Op2);
2336
2337 llvm::Value *HasOverflow;
2338 llvm::Value *Result = EmitOverflowIntrinsic(
2339 CGF, Intrinsic::umul_with_overflow, V1, V2, HasOverflow);
2340
2341 // The intrinsic call will detect overflow when the value is > UINT_MAX,
2342 // however, since the original builtin had a signed result, we need to report
2343 // an overflow when the result is greater than INT_MAX.
2344 auto IntMax = llvm::APInt::getSignedMaxValue(ResultInfo.Width);
2345 llvm::Value *IntMaxValue = llvm::ConstantInt::get(Result->getType(), IntMax);
2346
2347 llvm::Value *IntMaxOverflow = CGF.Builder.CreateICmpUGT(Result, IntMaxValue);
2348 HasOverflow = CGF.Builder.CreateOr(HasOverflow, IntMaxOverflow);
2349
2350 bool isVolatile =
2351 ResultArg->getType()->getPointeeType().isVolatileQualified();
2352 Address ResultPtr = CGF.EmitPointerWithAlignment(ResultArg);
2353 CGF.Builder.CreateStore(CGF.EmitToMemory(Result, ResultQTy), ResultPtr,
2354 isVolatile);
2355 return RValue::get(HasOverflow);
2356}
2357
2358/// Determine if a binop is a checked mixed-sign multiply we can specialize.
2359static bool isSpecialMixedSignMultiply(unsigned BuiltinID,
2360 WidthAndSignedness Op1Info,
2361 WidthAndSignedness Op2Info,
2362 WidthAndSignedness ResultInfo) {
2363 return BuiltinID == Builtin::BI__builtin_mul_overflow &&
2364 std::max(Op1Info.Width, Op2Info.Width) >= ResultInfo.Width &&
2365 Op1Info.Signed != Op2Info.Signed;
2366}
2367
2368/// Emit a checked mixed-sign multiply. This is a cheaper specialization of
2369/// the generic checked-binop irgen.
2370static RValue
2372 WidthAndSignedness Op1Info, const clang::Expr *Op2,
2373 WidthAndSignedness Op2Info,
2374 const clang::Expr *ResultArg, QualType ResultQTy,
2375 WidthAndSignedness ResultInfo) {
2376 assert(isSpecialMixedSignMultiply(Builtin::BI__builtin_mul_overflow, Op1Info,
2377 Op2Info, ResultInfo) &&
2378 "Not a mixed-sign multipliction we can specialize");
2379
2380 // Emit the signed and unsigned operands.
2381 const clang::Expr *SignedOp = Op1Info.Signed ? Op1 : Op2;
2382 const clang::Expr *UnsignedOp = Op1Info.Signed ? Op2 : Op1;
2383 llvm::Value *Signed = CGF.EmitScalarExpr(SignedOp);
2384 llvm::Value *Unsigned = CGF.EmitScalarExpr(UnsignedOp);
2385 unsigned SignedOpWidth = Op1Info.Signed ? Op1Info.Width : Op2Info.Width;
2386 unsigned UnsignedOpWidth = Op1Info.Signed ? Op2Info.Width : Op1Info.Width;
2387
2388 // One of the operands may be smaller than the other. If so, [s|z]ext it.
2389 if (SignedOpWidth < UnsignedOpWidth)
2390 Signed = CGF.Builder.CreateSExt(Signed, Unsigned->getType(), "op.sext");
2391 if (UnsignedOpWidth < SignedOpWidth)
2392 Unsigned = CGF.Builder.CreateZExt(Unsigned, Signed->getType(), "op.zext");
2393
2394 llvm::Type *OpTy = Signed->getType();
2395 llvm::Value *Zero = llvm::Constant::getNullValue(OpTy);
2396 Address ResultPtr = CGF.EmitPointerWithAlignment(ResultArg);
2397 llvm::Type *ResTy = CGF.getTypes().ConvertType(ResultQTy);
2398 unsigned OpWidth = std::max(Op1Info.Width, Op2Info.Width);
2399
2400 // Take the absolute value of the signed operand.
2401 llvm::Value *IsNegative = CGF.Builder.CreateICmpSLT(Signed, Zero);
2402 llvm::Value *AbsOfNegative = CGF.Builder.CreateSub(Zero, Signed);
2403 llvm::Value *AbsSigned =
2404 CGF.Builder.CreateSelect(IsNegative, AbsOfNegative, Signed);
2405
2406 // Perform a checked unsigned multiplication.
2407 llvm::Value *UnsignedOverflow;
2408 llvm::Value *UnsignedResult =
2409 EmitOverflowIntrinsic(CGF, Intrinsic::umul_with_overflow, AbsSigned,
2410 Unsigned, UnsignedOverflow);
2411
2412 llvm::Value *Overflow, *Result;
2413 if (ResultInfo.Signed) {
2414 // Signed overflow occurs if the result is greater than INT_MAX or lesser
2415 // than INT_MIN, i.e when |Result| > (INT_MAX + IsNegative).
2416 auto IntMax =
2417 llvm::APInt::getSignedMaxValue(ResultInfo.Width).zext(OpWidth);
2418 llvm::Value *MaxResult =
2419 CGF.Builder.CreateAdd(llvm::ConstantInt::get(OpTy, IntMax),
2420 CGF.Builder.CreateZExt(IsNegative, OpTy));
2421 llvm::Value *SignedOverflow =
2422 CGF.Builder.CreateICmpUGT(UnsignedResult, MaxResult);
2423 Overflow = CGF.Builder.CreateOr(UnsignedOverflow, SignedOverflow);
2424
2425 // Prepare the signed result (possibly by negating it).
2426 llvm::Value *NegativeResult = CGF.Builder.CreateNeg(UnsignedResult);
2427 llvm::Value *SignedResult =
2428 CGF.Builder.CreateSelect(IsNegative, NegativeResult, UnsignedResult);
2429 Result = CGF.Builder.CreateTrunc(SignedResult, ResTy);
2430 } else {
2431 // Unsigned overflow occurs if the result is < 0 or greater than UINT_MAX.
2432 llvm::Value *Underflow = CGF.Builder.CreateAnd(
2433 IsNegative, CGF.Builder.CreateIsNotNull(UnsignedResult));
2434 Overflow = CGF.Builder.CreateOr(UnsignedOverflow, Underflow);
2435 if (ResultInfo.Width < OpWidth) {
2436 auto IntMax =
2437 llvm::APInt::getMaxValue(ResultInfo.Width).zext(OpWidth);
2438 llvm::Value *TruncOverflow = CGF.Builder.CreateICmpUGT(
2439 UnsignedResult, llvm::ConstantInt::get(OpTy, IntMax));
2440 Overflow = CGF.Builder.CreateOr(Overflow, TruncOverflow);
2441 }
2442
2443 // Negate the product if it would be negative in infinite precision.
2444 Result = CGF.Builder.CreateSelect(
2445 IsNegative, CGF.Builder.CreateNeg(UnsignedResult), UnsignedResult);
2446
2447 Result = CGF.Builder.CreateTrunc(Result, ResTy);
2448 }
2449 assert(Overflow && Result && "Missing overflow or result");
2450
2451 bool isVolatile =
2452 ResultArg->getType()->getPointeeType().isVolatileQualified();
2453 CGF.Builder.CreateStore(CGF.EmitToMemory(Result, ResultQTy), ResultPtr,
2454 isVolatile);
2455 return RValue::get(Overflow);
2456}
2457
2458/// Determine if the specified type requires laundering by checking if it is a
2459/// dynamic class type or contains a subobject which is a dynamic class type.
2461 if (!CGM.getCodeGenOpts().StrictVTablePointers)
2462 return false;
2463 return Ty.requiresBuiltinLaunder(CGM.getContext());
2464}
2465
2466RValue CodeGenFunction::emitRotate(const CallExpr *E, bool IsRotateRight) {
2467 llvm::Value *Src = EmitScalarExpr(E->getArg(0));
2468 llvm::Value *ShiftAmt = EmitScalarExpr(E->getArg(1));
2469
2470 // The builtin's shift arg may have a different type than the source arg and
2471 // result, but the LLVM intrinsic uses the same type for all values.
2472 llvm::Type *Ty = Src->getType();
2473 llvm::Type *ShiftTy = ShiftAmt->getType();
2474
2475 unsigned BitWidth = Ty->getIntegerBitWidth();
2476
2477 // Normalize shift amount to [0, BitWidth) range to match runtime behavior.
2478 // This matches the algorithm in ExprConstant.cpp for constant evaluation.
2479 if (BitWidth == 1) {
2480 // Rotating a 1-bit value is always a no-op
2481 ShiftAmt = ConstantInt::get(ShiftTy, 0);
2482 } else if (BitWidth == 2) {
2483 // For 2-bit values: rotation amount is 0 or 1 based on
2484 // whether the amount is even or odd. We can't use srem here because
2485 // the divisor (2) would be misinterpreted as -2 in 2-bit signed arithmetic.
2486 llvm::Value *One = ConstantInt::get(ShiftTy, 1);
2487 ShiftAmt = Builder.CreateAnd(ShiftAmt, One);
2488 } else {
2489 unsigned ShiftAmtBitWidth = ShiftTy->getIntegerBitWidth();
2490 bool ShiftAmtIsSigned = E->getArg(1)->getType()->isSignedIntegerType();
2491
2492 // Choose the wider type for the divisor to avoid truncation
2493 llvm::Type *DivisorTy = ShiftAmtBitWidth > BitWidth ? ShiftTy : Ty;
2494 llvm::Value *Divisor = ConstantInt::get(DivisorTy, BitWidth);
2495
2496 // Extend ShiftAmt to match Divisor width if needed
2497 if (ShiftAmtBitWidth < DivisorTy->getIntegerBitWidth()) {
2498 ShiftAmt = Builder.CreateIntCast(ShiftAmt, DivisorTy, ShiftAmtIsSigned);
2499 }
2500
2501 // Normalize to [0, BitWidth)
2502 llvm::Value *RemResult;
2503 if (ShiftAmtIsSigned) {
2504 RemResult = Builder.CreateSRem(ShiftAmt, Divisor);
2505 // Signed remainder can be negative, convert to positive equivalent
2506 llvm::Value *Zero = ConstantInt::get(DivisorTy, 0);
2507 llvm::Value *IsNegative = Builder.CreateICmpSLT(RemResult, Zero);
2508 llvm::Value *PositiveShift = Builder.CreateAdd(RemResult, Divisor);
2509 ShiftAmt = Builder.CreateSelect(IsNegative, PositiveShift, RemResult);
2510 } else {
2511 ShiftAmt = Builder.CreateURem(ShiftAmt, Divisor);
2512 }
2513 }
2514
2515 // Convert to the source type if needed
2516 if (ShiftAmt->getType() != Ty) {
2517 ShiftAmt = Builder.CreateIntCast(ShiftAmt, Ty, false);
2518 }
2519
2520 // Rotate is a special case of LLVM funnel shift - 1st 2 args are the same.
2521 unsigned IID = IsRotateRight ? Intrinsic::fshr : Intrinsic::fshl;
2522 Function *F = CGM.getIntrinsic(IID, Ty);
2523 return RValue::get(Builder.CreateCall(F, {Src, Src, ShiftAmt}));
2524}
2525
2526// Map math builtins for long-double to f128 version.
2527static unsigned mutateLongDoubleBuiltin(unsigned BuiltinID) {
2528 switch (BuiltinID) {
2529#define MUTATE_LDBL(func) \
2530 case Builtin::BI__builtin_##func##l: \
2531 return Builtin::BI__builtin_##func##f128;
2562 MUTATE_LDBL(nans)
2563 MUTATE_LDBL(inf)
2582 MUTATE_LDBL(huge_val)
2592#undef MUTATE_LDBL
2593 default:
2594 return BuiltinID;
2595 }
2596}
2597
2598static Value *tryUseTestFPKind(CodeGenFunction &CGF, unsigned BuiltinID,
2599 Value *V) {
2600 if (CGF.Builder.getIsFPConstrained() &&
2601 CGF.Builder.getDefaultConstrainedExcept() != fp::ebIgnore) {
2602 if (Value *Result =
2603 CGF.getTargetHooks().testFPKind(V, BuiltinID, CGF.Builder, CGF.CGM))
2604 return Result;
2605 }
2606 return nullptr;
2607}
2608
2610 const FunctionDecl *FD) {
2611 auto Name = FD->getNameAsString() + "__hipstdpar_unsupported";
2612 auto FnTy = CGF->CGM.getTypes().GetFunctionType(FD);
2613 auto UBF = CGF->CGM.getModule().getOrInsertFunction(Name, FnTy);
2614
2616 for (auto &&FormalTy : FnTy->params())
2617 Args.push_back(llvm::PoisonValue::get(FormalTy));
2618
2619 return RValue::get(CGF->Builder.CreateCall(UBF, Args));
2620}
2621
2622// stdc_{leading,trailing}_{zeros,ones} and stdc_count_ones: counts bits using
2623// ctlz, cttz, or ctpop (IsPop). InvertArg flips the input to count the
2624// opposite bit value.
2626 Intrinsic::ID IntID,
2627 bool InvertArg, bool IsPop) {
2628 Value *ArgValue = EmitScalarExpr(E->getArg(0));
2629 llvm::Type *ArgType = ArgValue->getType();
2630 llvm::Type *ResultType = ConvertType(E->getType());
2631 Value *ActualArg = InvertArg ? Builder.CreateNot(ArgValue) : ArgValue;
2632 Function *F = CGM.getIntrinsic(IntID, ArgType);
2633 Value *Result = IsPop
2634 ? Builder.CreateCall(F, ActualArg)
2635 : Builder.CreateCall(F, {ActualArg, Builder.getFalse()});
2636 if (Result->getType() != ResultType)
2637 Result = Builder.CreateIntCast(Result, ResultType, false);
2638 return RValue::get(Result);
2639}
2640
2641// stdc_count_zeros (BitWidth - ctpop) and stdc_bit_width (BitWidth - ctlz).
2642// IsPop selects ctpop; otherwise ctlz is used.
2644 Intrinsic::ID IntID, bool IsPop) {
2645 Value *ArgValue = EmitScalarExpr(E->getArg(0));
2646 llvm::Type *ArgType = ArgValue->getType();
2647 llvm::Type *ResultType = ConvertType(E->getType());
2648 unsigned BitWidth = ArgType->getIntegerBitWidth();
2649 Function *F = CGM.getIntrinsic(IntID, ArgType);
2650 Value *Cnt = IsPop ? Builder.CreateCall(F, ArgValue)
2651 : Builder.CreateCall(F, {ArgValue, Builder.getFalse()});
2652 Value *Result = Builder.CreateSub(ConstantInt::get(ArgType, BitWidth), Cnt);
2653 if (Result->getType() != ResultType)
2654 Result = Builder.CreateIntCast(Result, ResultType, false);
2655 return RValue::get(Result);
2656}
2657
2658// stdc_first_{leading,trailing}_{zero,one}: returns the 1-based position of
2659// the first matching bit, or 0 if no such bit exists. InvertArg flips the
2660// input to search for zeros instead of ones.
2662 bool InvertArg) {
2663 Value *ArgValue = EmitScalarExpr(E->getArg(0));
2664 llvm::Type *ArgType = ArgValue->getType();
2665 llvm::Type *ResultType = ConvertType(E->getType());
2666 Value *Zero = ConstantInt::get(ArgType, 0);
2667 Value *One = ConstantInt::get(ArgType, 1);
2668 Value *ActualArg = InvertArg ? Builder.CreateNot(ArgValue) : ArgValue;
2669 Function *F = CGM.getIntrinsic(IntID, ArgType);
2670 Value *Cnt = Builder.CreateCall(F, {ActualArg, Builder.getFalse()});
2671 Value *Tmp = Builder.CreateAdd(Cnt, One);
2672 Value *IsZero = Builder.CreateICmpEQ(ActualArg, Zero);
2673 Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp);
2674 if (Result->getType() != ResultType)
2675 Result = Builder.CreateIntCast(Result, ResultType, false);
2676 return RValue::get(Result);
2677}
2678
2679namespace {
2680
2681// PaddingClearer is a utility class that clears padding bits in a
2682// c/c++ type. It traverses the type recursively, collecting occupied
2683// bit intervals, and then computes the padding intervals.
2684// In the end, it clears the padding bits by writing zeros
2685// to the padding intervals bytes-by-bytes. If a byte only contains
2686// some padding bits, it writes zeros to only those bits. This is
2687// the case for bit-fields.
2688struct PaddingClearer {
2689 PaddingClearer(CodeGenFunction &F)
2690 : CGF(F), CharWidth(CGF.getContext().getCharWidth()) {}
2691
2692 void run(Address Src, QualType Ty) {
2693 OccuppiedIntervals.clear();
2694 Stack.clear();
2695
2696 Stack.push_back(Data{0, Ty, true});
2697 while (!Stack.empty()) {
2698 auto Current = Stack.back();
2699 Stack.pop_back();
2700 Visit(Current);
2701 }
2702
2703 MergeOccuppiedIntervals();
2704 auto PaddingIntervals =
2705 GetPaddingIntervals(CGF.getContext().getTypeSize(Ty));
2706 for (const auto &Interval : PaddingIntervals) {
2707 ClearPadding(Src, Interval);
2708 }
2709 }
2710
2711private:
2712 struct BitInterval {
2713 // [First, Last)
2715 uint64_t Last;
2716 };
2717
2718 struct Data {
2719 uint64_t StartBitOffset;
2720 QualType Ty;
2721 bool VisitVirtualBase;
2722 };
2723
2724 // Return the number of non padding bits of a scalar type.
2725 //
2726 // The property that we specifically care about here is whether the scalar
2727 // type has padding bits, i.e. are there bits in the type which are not
2728 // specified by the ABI.
2729 //
2730 // We currently don't care about this anywhere else in clang: layout cares
2731 // about the ABI size, calling convention code cares about specific types, but
2732 // nothing cares about padding specifically. And it's not something we can
2733 // easily query from LLVM due to the type system mismatches.
2734 // DL.getTypeSizeInBits(convertTypeForLoadStore(T)) is probably close, but the
2735 // DataLayout methods aren't really designed for this usage.
2736 //
2737 // Therefore, it is better to explicitly list all the scalar types containing
2738 // padding bits that we know of, namely, _BitInt(N) and x87 long double.
2739 uint64_t getScalarOccupiedSizeInBits(QualType Ty) const {
2740 if (const auto *BIT = Ty->getAs<BitIntType>())
2741 return BIT->getNumBits();
2742
2743 if (const auto *BT = Ty->getAs<BuiltinType>()) {
2744 if (BT->getKind() == BuiltinType::LongDouble &&
2745 &CGF.getTarget().getLongDoubleFormat() ==
2746 &APFloat::x87DoubleExtended())
2747 return APFloat::getSizeInBits(CGF.getTarget().getLongDoubleFormat());
2748 }
2749
2750 return CGF.getContext().getTypeSize(Ty);
2751 }
2752
2753 void Visit(const Data &D) {
2754 if (auto *AT = dyn_cast<ConstantArrayType>(D.Ty)) {
2755 VisitArray(AT, D.StartBitOffset);
2756 return;
2757 }
2758
2759 if (auto *Record = D.Ty->getAsRecordDecl()) {
2760 VisitStruct(Record, D.StartBitOffset, D.VisitVirtualBase);
2761 return;
2762 }
2763
2764 if (D.Ty->isAtomicType()) {
2765 auto Unwrapped = D;
2766 Unwrapped.Ty = D.Ty.getAtomicUnqualifiedType();
2767 Stack.push_back(Unwrapped);
2768 return;
2769 }
2770
2771 if (const auto *Complex = D.Ty->getAs<ComplexType>()) {
2772 VisitComplex(Complex, D.StartBitOffset);
2773 return;
2774 }
2775
2776 if (const auto *VT = D.Ty->getAs<clang::VectorType>()) {
2777 VisitVector(VT, D.StartBitOffset);
2778 return;
2779 }
2780
2781 uint64_t SizeBit = getScalarOccupiedSizeInBits(D.Ty);
2782 OccuppiedIntervals.push_back(
2783 BitInterval{D.StartBitOffset, D.StartBitOffset + SizeBit});
2784 }
2785
2786 void VisitArray(const ConstantArrayType *AT, uint64_t StartBitOffset) {
2787 for (uint64_t ArrIndex = 0; ArrIndex < AT->getSize().getLimitedValue();
2788 ++ArrIndex) {
2789
2790 QualType ElementQualType = AT->getElementType();
2791 auto ElementSize = CGF.getContext().getTypeSizeInChars(ElementQualType);
2792 auto ElementAlign = CGF.getContext().getTypeAlignInChars(ElementQualType);
2793 auto Offset = ElementSize.alignTo(ElementAlign);
2794
2795 Stack.push_back(
2796 Data{StartBitOffset + ArrIndex * Offset.getQuantity() * CharWidth,
2797 ElementQualType, /*VisitVirtualBase*/ true});
2798 }
2799 }
2800
2801 void VisitStruct(const RecordDecl *R, uint64_t StartBitOffset,
2802 bool VisitVirtualBase) {
2803 const auto &DL = CGF.CGM.getModule().getDataLayout();
2804 const ASTRecordLayout &ASTLayout = CGF.getContext().getASTRecordLayout(R);
2805
2806 auto *CXXRecord = dyn_cast<CXXRecordDecl>(R);
2807
2808 if (CXXRecord) {
2809 if (ASTLayout.hasOwnVFPtr()) {
2810 OccuppiedIntervals.push_back(BitInterval{
2811 StartBitOffset, StartBitOffset + DL.getPointerSizeInBits()});
2812 }
2813
2814 const auto VisitBase = [&ASTLayout, StartBitOffset, this](
2815 const CXXBaseSpecifier &Base, auto GetOffset) {
2816 auto *BaseRecord = Base.getType()->getAsCXXRecordDecl();
2817 if (!BaseRecord) {
2818 return;
2819 }
2820 auto BaseOffset =
2821 std::invoke(GetOffset, ASTLayout, BaseRecord).getQuantity();
2822
2823 Stack.push_back(Data{StartBitOffset + BaseOffset * CharWidth,
2824 Base.getType(), /*VisitVirtualBase*/ false});
2825 };
2826
2827 for (auto Base : CXXRecord->bases()) {
2828 if (!Base.isVirtual()) {
2829 VisitBase(Base, &ASTRecordLayout::getBaseClassOffset);
2830 }
2831 }
2832
2833 if (VisitVirtualBase) {
2834 for (auto VBase : CXXRecord->vbases()) {
2835 VisitBase(VBase, &ASTRecordLayout::getVBaseClassOffset);
2836 }
2837 }
2838 }
2839
2840 for (auto *Field : R->fields()) {
2841 auto FieldOffset = ASTLayout.getFieldOffset(Field->getFieldIndex());
2842 if (Field->isBitField()) {
2843 OccuppiedIntervals.push_back(BitInterval{
2844 StartBitOffset + FieldOffset,
2845 StartBitOffset + FieldOffset + Field->getBitWidthValue()});
2846 } else {
2847 Stack.push_back(Data{StartBitOffset + FieldOffset, Field->getType(),
2848 /*VisitVirtualBase*/ true});
2849 }
2850 }
2851 }
2852
2853 void VisitComplex(const ComplexType *CT, uint64_t StartBitOffset) {
2854 QualType ElementQualType = CT->getElementType();
2855 auto ElementSize = CGF.getContext().getTypeSizeInChars(ElementQualType);
2856 auto ElementAlign = CGF.getContext().getTypeAlignInChars(ElementQualType);
2857 auto ImgOffset = ElementSize.alignTo(ElementAlign);
2858
2859 Stack.push_back(
2860 Data{StartBitOffset, ElementQualType, /*VisitVirtualBase*/ true});
2861 Stack.push_back(Data{StartBitOffset + ImgOffset.getQuantity() * CharWidth,
2862 ElementQualType, /*VisitVirtualBase*/ true});
2863 }
2864
2865 void VisitVector(const clang::VectorType *VT, uint64_t StartBitOffset) {
2866 ASTContext &Ctx = CGF.getContext();
2867 uint64_t SizeBit = [&]() -> uint64_t {
2868 if (VT->isPackedVectorBoolType(Ctx))
2869 return VT->getNumElements();
2870 return getScalarOccupiedSizeInBits(VT->getElementType()) *
2871 VT->getNumElements();
2872 }();
2873 OccuppiedIntervals.push_back(
2874 BitInterval{StartBitOffset, StartBitOffset + SizeBit});
2875 }
2876
2877 void MergeOccuppiedIntervals() {
2878 std::sort(OccuppiedIntervals.begin(), OccuppiedIntervals.end(),
2879 [](const BitInterval &lhs, const BitInterval &rhs) {
2880 return std::tie(lhs.First, lhs.Last) <
2881 std::tie(rhs.First, rhs.Last);
2882 });
2883
2884 llvm::SmallVector<BitInterval> Merged;
2885 Merged.reserve(OccuppiedIntervals.size());
2886
2887 for (const BitInterval &NextInterval : OccuppiedIntervals) {
2888 if (Merged.empty()) {
2889 Merged.push_back(NextInterval);
2890 continue;
2891 }
2892 auto &LastInterval = Merged.back();
2893
2894 if (NextInterval.First > LastInterval.Last) {
2895 Merged.push_back(NextInterval);
2896 } else {
2897 LastInterval.Last = std::max(LastInterval.Last, NextInterval.Last);
2898 }
2899 }
2900
2901 OccuppiedIntervals = Merged;
2902 }
2903
2904 llvm::SmallVector<BitInterval>
2905 GetPaddingIntervals(uint64_t SizeInBits) const {
2906 llvm::SmallVector<BitInterval> Results;
2907 if (OccuppiedIntervals.size() == 1 &&
2908 OccuppiedIntervals.front().First == 0 &&
2909 OccuppiedIntervals.front().Last == SizeInBits) {
2910 return Results;
2911 }
2912 Results.reserve(OccuppiedIntervals.size() + 1);
2913 uint64_t CurrentPos = 0;
2914 for (const BitInterval &OccupiedInterval : OccuppiedIntervals) {
2915 if (OccupiedInterval.First > CurrentPos) {
2916 Results.push_back(BitInterval{CurrentPos, OccupiedInterval.First});
2917 }
2918 CurrentPos = OccupiedInterval.Last;
2919 }
2920 if (SizeInBits > CurrentPos) {
2921 Results.push_back(BitInterval{CurrentPos, SizeInBits});
2922 }
2923 return Results;
2924 }
2925
2926 void ClearPadding(Address Src, const BitInterval &PaddingInterval) {
2927 auto *I8Ptr =
2928 CGF.Builder.CreateBitCast(Src.getBasePointer(), CGF.Int8PtrTy);
2929 auto *Zero = ConstantInt::get(CGF.Int8Ty, 0);
2930
2931 // Calculate byte indices and bit positions
2932 auto StartByte = PaddingInterval.First / CharWidth;
2933 auto StartBit = PaddingInterval.First % CharWidth;
2934 auto EndByte = PaddingInterval.Last / CharWidth;
2935 auto EndBit = PaddingInterval.Last % CharWidth;
2936
2937 if (StartByte == EndByte) {
2938 // Interval is within a single byte
2939 auto *Index = ConstantInt::get(CGF.IntTy, StartByte);
2940 auto *Element = CGF.Builder.CreateGEP(CGF.Int8Ty, I8Ptr, Index);
2941 Address ElementAddr(Element, CGF.Int8Ty,
2943 CharUnits::fromQuantity(StartByte)));
2944
2945 auto *Value = CGF.Builder.CreateLoad(ElementAddr);
2946
2947 // Create mask to clear bits within the byte
2948 // We want to clear bits from StartBit to EndBit-1
2949 uint8_t bitsToClear = ((1 << EndBit) - 1) & ~((1 << StartBit) - 1);
2950 uint8_t bitsToKeep = ~bitsToClear;
2951 auto *MaskValue = ConstantInt::get(CGF.Int8Ty, bitsToKeep);
2952 auto *NewValue = CGF.Builder.CreateAnd(Value, MaskValue);
2953
2954 CGF.Builder.CreateStore(NewValue, ElementAddr);
2955 } else {
2956 // Handle the start byte
2957 if (StartBit != 0) {
2958 auto *Index = ConstantInt::get(CGF.IntTy, StartByte);
2959 auto *Element = CGF.Builder.CreateGEP(CGF.Int8Ty, I8Ptr, Index);
2960 Address ElementAddr(Element, CGF.Int8Ty,
2962 CharUnits::fromQuantity(StartByte)));
2963
2964 auto *Value = CGF.Builder.CreateLoad(ElementAddr);
2965
2966 uint8_t bitsToClear = ((1 << (CharWidth - StartBit)) - 1) << StartBit;
2967 uint8_t bitsToKeep = ~bitsToClear;
2968 auto *MaskValue = ConstantInt::get(CGF.Int8Ty, bitsToKeep);
2969 auto *NewValue = CGF.Builder.CreateAnd(Value, MaskValue);
2970
2971 CGF.Builder.CreateStore(NewValue, ElementAddr);
2972 ++StartByte;
2973 }
2974
2975 // Handle full bytes in the middle
2976 for (auto Offset = StartByte; Offset < EndByte; ++Offset) {
2977 auto *Index = ConstantInt::get(CGF.IntTy, Offset);
2978 auto *Element = CGF.Builder.CreateGEP(CGF.Int8Ty, I8Ptr, Index);
2979 Address ElementAddr(Element, CGF.Int8Ty,
2981 CharUnits::fromQuantity(Offset)));
2982
2983 CGF.Builder.CreateStore(Zero, ElementAddr);
2984 }
2985
2986 // Handle the end byte
2987 if (EndBit != 0) {
2988 auto *Index = ConstantInt::get(CGF.IntTy, EndByte);
2989 auto *Element = CGF.Builder.CreateGEP(CGF.Int8Ty, I8Ptr, Index);
2990 Address ElementAddr(Element, CGF.Int8Ty,
2992 CharUnits::fromQuantity(EndByte)));
2993
2994 auto *Value = CGF.Builder.CreateLoad(ElementAddr);
2995
2996 uint8_t bitsToClear = (1 << EndBit) - 1;
2997 uint8_t bitsToKeep = ~bitsToClear;
2998 auto *MaskValue = ConstantInt::get(CGF.Int8Ty, bitsToKeep);
2999 auto *NewValue = CGF.Builder.CreateAnd(Value, MaskValue);
3000
3001 CGF.Builder.CreateStore(NewValue, ElementAddr);
3002 }
3003 }
3004 }
3005
3006 CodeGenFunction &CGF;
3007 const uint64_t CharWidth;
3008 llvm::SmallVector<Data> Stack;
3009 llvm::SmallVector<BitInterval> OccuppiedIntervals;
3010};
3011
3012} // namespace
3013
3015 const CallExpr *E,
3017 assert(!getContext().BuiltinInfo.isImmediate(BuiltinID) &&
3018 "Should not codegen for consteval builtins");
3019
3020 const FunctionDecl *FD = GD.getDecl()->getAsFunction();
3021 // See if we can constant fold this builtin. If so, don't emit it at all.
3022 // TODO: Extend this handling to all builtin calls that we can constant-fold.
3024 if (E->isPRValue() && E->EvaluateAsRValue(Result, CGM.getContext()) &&
3025 !Result.hasSideEffects()) {
3026 if (Result.Val.isInt())
3027 return RValue::get(llvm::ConstantInt::get(getLLVMContext(),
3028 Result.Val.getInt()));
3029 if (Result.Val.isFloat())
3030 return RValue::get(llvm::ConstantFP::get(getLLVMContext(),
3031 Result.Val.getFloat()));
3032 }
3033
3034 // If current long-double semantics is IEEE 128-bit, replace math builtins
3035 // of long-double with f128 equivalent.
3036 // TODO: This mutation should also be applied to other targets other than PPC,
3037 // after backend supports IEEE 128-bit style libcalls.
3038 if (getTarget().getTriple().isPPC64() &&
3039 &getTarget().getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
3040 BuiltinID = mutateLongDoubleBuiltin(BuiltinID);
3041
3042 // If the builtin has been declared explicitly with an assembler label,
3043 // disable the specialized emitting below. Ideally we should communicate the
3044 // rename in IR, or at least avoid generating the intrinsic calls that are
3045 // likely to get lowered to the renamed library functions.
3046 const unsigned BuiltinIDIfNoAsmLabel =
3047 FD->hasAttr<AsmLabelAttr>() ? 0 : BuiltinID;
3048
3049 std::optional<bool> ErrnoOverriden;
3050 // ErrnoOverriden is true if math-errno is overriden via the
3051 // '#pragma float_control(precise, on)'. This pragma disables fast-math,
3052 // which implies math-errno.
3053 if (E->hasStoredFPFeatures()) {
3055 if (OP.hasMathErrnoOverride())
3056 ErrnoOverriden = OP.getMathErrnoOverride();
3057 }
3058 // True if 'attribute__((optnone))' is used. This attribute overrides
3059 // fast-math which implies math-errno.
3060 bool OptNone = CurFuncDecl && CurFuncDecl->hasAttr<OptimizeNoneAttr>();
3061
3062 bool IsOptimizationEnabled = CGM.getCodeGenOpts().OptimizationLevel != 0;
3063
3064 bool GenerateFPMathIntrinsics =
3066 BuiltinID, CGM.getTriple(), ErrnoOverriden, getLangOpts().MathErrno,
3067 OptNone, IsOptimizationEnabled);
3068
3069 if (GenerateFPMathIntrinsics) {
3070 switch (BuiltinIDIfNoAsmLabel) {
3071 case Builtin::BIacos:
3072 case Builtin::BIacosf:
3073 case Builtin::BIacosl:
3074 case Builtin::BI__builtin_acos:
3075 case Builtin::BI__builtin_acosf:
3076 case Builtin::BI__builtin_acosf16:
3077 case Builtin::BI__builtin_acosl:
3078 case Builtin::BI__builtin_acosf128:
3079 case Builtin::BI__builtin_elementwise_acos:
3081 *this, E, Intrinsic::acos, Intrinsic::experimental_constrained_acos));
3082
3083 case Builtin::BIasin:
3084 case Builtin::BIasinf:
3085 case Builtin::BIasinl:
3086 case Builtin::BI__builtin_asin:
3087 case Builtin::BI__builtin_asinf:
3088 case Builtin::BI__builtin_asinf16:
3089 case Builtin::BI__builtin_asinl:
3090 case Builtin::BI__builtin_asinf128:
3091 case Builtin::BI__builtin_elementwise_asin:
3093 *this, E, Intrinsic::asin, Intrinsic::experimental_constrained_asin));
3094
3095 case Builtin::BIatan:
3096 case Builtin::BIatanf:
3097 case Builtin::BIatanl:
3098 case Builtin::BI__builtin_atan:
3099 case Builtin::BI__builtin_atanf:
3100 case Builtin::BI__builtin_atanf16:
3101 case Builtin::BI__builtin_atanl:
3102 case Builtin::BI__builtin_atanf128:
3103 case Builtin::BI__builtin_elementwise_atan:
3105 *this, E, Intrinsic::atan, Intrinsic::experimental_constrained_atan));
3106
3107 case Builtin::BIatan2:
3108 case Builtin::BIatan2f:
3109 case Builtin::BIatan2l:
3110 case Builtin::BI__builtin_atan2:
3111 case Builtin::BI__builtin_atan2f:
3112 case Builtin::BI__builtin_atan2f16:
3113 case Builtin::BI__builtin_atan2l:
3114 case Builtin::BI__builtin_atan2f128:
3115 case Builtin::BI__builtin_elementwise_atan2:
3117 *this, E, Intrinsic::atan2,
3118 Intrinsic::experimental_constrained_atan2));
3119
3120 case Builtin::BIceil:
3121 case Builtin::BIceilf:
3122 case Builtin::BIceill:
3123 case Builtin::BI__builtin_ceil:
3124 case Builtin::BI__builtin_ceilf:
3125 case Builtin::BI__builtin_ceilf16:
3126 case Builtin::BI__builtin_ceill:
3127 case Builtin::BI__builtin_ceilf128:
3128 case Builtin::BI__builtin_elementwise_ceil:
3130 Intrinsic::ceil,
3131 Intrinsic::experimental_constrained_ceil));
3132
3133 case Builtin::BIcopysign:
3134 case Builtin::BIcopysignf:
3135 case Builtin::BIcopysignl:
3136 case Builtin::BI__builtin_copysign:
3137 case Builtin::BI__builtin_copysignf:
3138 case Builtin::BI__builtin_copysignf16:
3139 case Builtin::BI__builtin_copysignl:
3140 case Builtin::BI__builtin_copysignf128:
3141 return RValue::get(
3142 emitBuiltinWithOneOverloadedType<2>(*this, E, Intrinsic::copysign));
3143
3144 case Builtin::BIcos:
3145 case Builtin::BIcosf:
3146 case Builtin::BIcosl:
3147 case Builtin::BI__builtin_cos:
3148 case Builtin::BI__builtin_cosf:
3149 case Builtin::BI__builtin_cosf16:
3150 case Builtin::BI__builtin_cosl:
3151 case Builtin::BI__builtin_cosf128:
3152 case Builtin::BI__builtin_elementwise_cos:
3154 Intrinsic::cos,
3155 Intrinsic::experimental_constrained_cos));
3156
3157 case Builtin::BIcosh:
3158 case Builtin::BIcoshf:
3159 case Builtin::BIcoshl:
3160 case Builtin::BI__builtin_cosh:
3161 case Builtin::BI__builtin_coshf:
3162 case Builtin::BI__builtin_coshf16:
3163 case Builtin::BI__builtin_coshl:
3164 case Builtin::BI__builtin_coshf128:
3165 case Builtin::BI__builtin_elementwise_cosh:
3167 *this, E, Intrinsic::cosh, Intrinsic::experimental_constrained_cosh));
3168
3169 case Builtin::BIexp:
3170 case Builtin::BIexpf:
3171 case Builtin::BIexpl:
3172 case Builtin::BI__builtin_exp:
3173 case Builtin::BI__builtin_expf:
3174 case Builtin::BI__builtin_expf16:
3175 case Builtin::BI__builtin_expl:
3176 case Builtin::BI__builtin_expf128:
3177 case Builtin::BI__builtin_elementwise_exp:
3179 Intrinsic::exp,
3180 Intrinsic::experimental_constrained_exp));
3181
3182 case Builtin::BIexp2:
3183 case Builtin::BIexp2f:
3184 case Builtin::BIexp2l:
3185 case Builtin::BI__builtin_exp2:
3186 case Builtin::BI__builtin_exp2f:
3187 case Builtin::BI__builtin_exp2f16:
3188 case Builtin::BI__builtin_exp2l:
3189 case Builtin::BI__builtin_exp2f128:
3190 case Builtin::BI__builtin_elementwise_exp2:
3192 Intrinsic::exp2,
3193 Intrinsic::experimental_constrained_exp2));
3194 case Builtin::BI__builtin_exp10:
3195 case Builtin::BI__builtin_exp10f:
3196 case Builtin::BI__builtin_exp10f16:
3197 case Builtin::BI__builtin_exp10l:
3198 case Builtin::BI__builtin_exp10f128:
3199 case Builtin::BI__builtin_elementwise_exp10: {
3200 // TODO: strictfp support
3201 if (Builder.getIsFPConstrained())
3202 break;
3203 return RValue::get(
3204 emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::exp10));
3205 }
3206 case Builtin::BIfabs:
3207 case Builtin::BIfabsf:
3208 case Builtin::BIfabsl:
3209 case Builtin::BI__builtin_fabs:
3210 case Builtin::BI__builtin_fabsf:
3211 case Builtin::BI__builtin_fabsf16:
3212 case Builtin::BI__builtin_fabsl:
3213 case Builtin::BI__builtin_fabsf128:
3214 return RValue::get(
3215 emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::fabs));
3216
3217 case Builtin::BIfloor:
3218 case Builtin::BIfloorf:
3219 case Builtin::BIfloorl:
3220 case Builtin::BI__builtin_floor:
3221 case Builtin::BI__builtin_floorf:
3222 case Builtin::BI__builtin_floorf16:
3223 case Builtin::BI__builtin_floorl:
3224 case Builtin::BI__builtin_floorf128:
3225 case Builtin::BI__builtin_elementwise_floor:
3227 Intrinsic::floor,
3228 Intrinsic::experimental_constrained_floor));
3229
3230 case Builtin::BIfma:
3231 case Builtin::BIfmaf:
3232 case Builtin::BIfmal:
3233 case Builtin::BI__builtin_fma:
3234 case Builtin::BI__builtin_fmaf:
3235 case Builtin::BI__builtin_fmaf16:
3236 case Builtin::BI__builtin_fmal:
3237 case Builtin::BI__builtin_fmaf128:
3238 case Builtin::BI__builtin_elementwise_fma:
3240 Intrinsic::fma,
3241 Intrinsic::experimental_constrained_fma));
3242
3243 case Builtin::BIfmax:
3244 case Builtin::BIfmaxf:
3245 case Builtin::BIfmaxl:
3246 case Builtin::BI__builtin_fmax:
3247 case Builtin::BI__builtin_fmaxf:
3248 case Builtin::BI__builtin_fmaxf16:
3249 case Builtin::BI__builtin_fmaxl:
3250 case Builtin::BI__builtin_fmaxf128: {
3251 IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
3252 Builder.getFastMathFlags().setNoSignedZeros();
3254 *this, E, Intrinsic::maxnum,
3255 Intrinsic::experimental_constrained_maxnum));
3256 }
3257
3258 case Builtin::BIfmin:
3259 case Builtin::BIfminf:
3260 case Builtin::BIfminl:
3261 case Builtin::BI__builtin_fmin:
3262 case Builtin::BI__builtin_fminf:
3263 case Builtin::BI__builtin_fminf16:
3264 case Builtin::BI__builtin_fminl:
3265 case Builtin::BI__builtin_fminf128: {
3266 IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
3267 Builder.getFastMathFlags().setNoSignedZeros();
3269 *this, E, Intrinsic::minnum,
3270 Intrinsic::experimental_constrained_minnum));
3271 }
3272
3273 case Builtin::BIfmaximum_num:
3274 case Builtin::BIfmaximum_numf:
3275 case Builtin::BIfmaximum_numl:
3276 case Builtin::BI__builtin_fmaximum_num:
3277 case Builtin::BI__builtin_fmaximum_numf:
3278 case Builtin::BI__builtin_fmaximum_numf16:
3279 case Builtin::BI__builtin_fmaximum_numl:
3280 case Builtin::BI__builtin_fmaximum_numf128:
3281 return RValue::get(
3282 emitBuiltinWithOneOverloadedType<2>(*this, E, Intrinsic::maximumnum));
3283
3284 case Builtin::BIfminimum_num:
3285 case Builtin::BIfminimum_numf:
3286 case Builtin::BIfminimum_numl:
3287 case Builtin::BI__builtin_fminimum_num:
3288 case Builtin::BI__builtin_fminimum_numf:
3289 case Builtin::BI__builtin_fminimum_numf16:
3290 case Builtin::BI__builtin_fminimum_numl:
3291 case Builtin::BI__builtin_fminimum_numf128:
3292 return RValue::get(
3293 emitBuiltinWithOneOverloadedType<2>(*this, E, Intrinsic::minimumnum));
3294
3295 // fmod() is a special-case. It maps to the frem instruction rather than an
3296 // LLVM intrinsic.
3297 case Builtin::BIfmod:
3298 case Builtin::BIfmodf:
3299 case Builtin::BIfmodl:
3300 case Builtin::BI__builtin_fmod:
3301 case Builtin::BI__builtin_fmodf:
3302 case Builtin::BI__builtin_fmodf16:
3303 case Builtin::BI__builtin_fmodl:
3304 case Builtin::BI__builtin_fmodf128:
3305 case Builtin::BI__builtin_elementwise_fmod: {
3306 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
3307 Value *Arg1 = EmitScalarExpr(E->getArg(0));
3308 Value *Arg2 = EmitScalarExpr(E->getArg(1));
3309 if (Builder.getIsFPConstrained()) {
3310 Function *F = CGM.getIntrinsic(Intrinsic::experimental_constrained_frem,
3311 Arg1->getType());
3312 return RValue::get(Builder.CreateConstrainedFPCall(F, {Arg1, Arg2}));
3313 } else {
3314 return RValue::get(Builder.CreateFRem(Arg1, Arg2, "fmod"));
3315 }
3316 }
3317
3318 case Builtin::BIlog:
3319 case Builtin::BIlogf:
3320 case Builtin::BIlogl:
3321 case Builtin::BI__builtin_log:
3322 case Builtin::BI__builtin_logf:
3323 case Builtin::BI__builtin_logf16:
3324 case Builtin::BI__builtin_logl:
3325 case Builtin::BI__builtin_logf128:
3326 case Builtin::BI__builtin_elementwise_log:
3328 Intrinsic::log,
3329 Intrinsic::experimental_constrained_log));
3330
3331 case Builtin::BIlog10:
3332 case Builtin::BIlog10f:
3333 case Builtin::BIlog10l:
3334 case Builtin::BI__builtin_log10:
3335 case Builtin::BI__builtin_log10f:
3336 case Builtin::BI__builtin_log10f16:
3337 case Builtin::BI__builtin_log10l:
3338 case Builtin::BI__builtin_log10f128:
3339 case Builtin::BI__builtin_elementwise_log10:
3341 Intrinsic::log10,
3342 Intrinsic::experimental_constrained_log10));
3343
3344 case Builtin::BIlog2:
3345 case Builtin::BIlog2f:
3346 case Builtin::BIlog2l:
3347 case Builtin::BI__builtin_log2:
3348 case Builtin::BI__builtin_log2f:
3349 case Builtin::BI__builtin_log2f16:
3350 case Builtin::BI__builtin_log2l:
3351 case Builtin::BI__builtin_log2f128:
3352 case Builtin::BI__builtin_elementwise_log2:
3354 Intrinsic::log2,
3355 Intrinsic::experimental_constrained_log2));
3356
3357 case Builtin::BInearbyint:
3358 case Builtin::BInearbyintf:
3359 case Builtin::BInearbyintl:
3360 case Builtin::BI__builtin_nearbyint:
3361 case Builtin::BI__builtin_nearbyintf:
3362 case Builtin::BI__builtin_nearbyintl:
3363 case Builtin::BI__builtin_nearbyintf128:
3364 case Builtin::BI__builtin_elementwise_nearbyint:
3366 Intrinsic::nearbyint,
3367 Intrinsic::experimental_constrained_nearbyint));
3368
3369 case Builtin::BIpow:
3370 case Builtin::BIpowf:
3371 case Builtin::BIpowl:
3372 case Builtin::BI__builtin_pow:
3373 case Builtin::BI__builtin_powf:
3374 case Builtin::BI__builtin_powf16:
3375 case Builtin::BI__builtin_powl:
3376 case Builtin::BI__builtin_powf128:
3377 case Builtin::BI__builtin_elementwise_pow:
3379 Intrinsic::pow,
3380 Intrinsic::experimental_constrained_pow));
3381
3382 case Builtin::BIrint:
3383 case Builtin::BIrintf:
3384 case Builtin::BIrintl:
3385 case Builtin::BI__builtin_rint:
3386 case Builtin::BI__builtin_rintf:
3387 case Builtin::BI__builtin_rintf16:
3388 case Builtin::BI__builtin_rintl:
3389 case Builtin::BI__builtin_rintf128:
3390 case Builtin::BI__builtin_elementwise_rint:
3392 Intrinsic::rint,
3393 Intrinsic::experimental_constrained_rint));
3394
3395 case Builtin::BIround:
3396 case Builtin::BIroundf:
3397 case Builtin::BIroundl:
3398 case Builtin::BI__builtin_round:
3399 case Builtin::BI__builtin_roundf:
3400 case Builtin::BI__builtin_roundf16:
3401 case Builtin::BI__builtin_roundl:
3402 case Builtin::BI__builtin_roundf128:
3403 case Builtin::BI__builtin_elementwise_round:
3405 Intrinsic::round,
3406 Intrinsic::experimental_constrained_round));
3407
3408 case Builtin::BIroundeven:
3409 case Builtin::BIroundevenf:
3410 case Builtin::BIroundevenl:
3411 case Builtin::BI__builtin_roundeven:
3412 case Builtin::BI__builtin_roundevenf:
3413 case Builtin::BI__builtin_roundevenf16:
3414 case Builtin::BI__builtin_roundevenl:
3415 case Builtin::BI__builtin_roundevenf128:
3416 case Builtin::BI__builtin_elementwise_roundeven:
3418 Intrinsic::roundeven,
3419 Intrinsic::experimental_constrained_roundeven));
3420
3421 case Builtin::BIsin:
3422 case Builtin::BIsinf:
3423 case Builtin::BIsinl:
3424 case Builtin::BI__builtin_sin:
3425 case Builtin::BI__builtin_sinf:
3426 case Builtin::BI__builtin_sinf16:
3427 case Builtin::BI__builtin_sinl:
3428 case Builtin::BI__builtin_sinf128:
3429 case Builtin::BI__builtin_elementwise_sin:
3431 Intrinsic::sin,
3432 Intrinsic::experimental_constrained_sin));
3433
3434 case Builtin::BIsinh:
3435 case Builtin::BIsinhf:
3436 case Builtin::BIsinhl:
3437 case Builtin::BI__builtin_sinh:
3438 case Builtin::BI__builtin_sinhf:
3439 case Builtin::BI__builtin_sinhf16:
3440 case Builtin::BI__builtin_sinhl:
3441 case Builtin::BI__builtin_sinhf128:
3442 case Builtin::BI__builtin_elementwise_sinh:
3444 *this, E, Intrinsic::sinh, Intrinsic::experimental_constrained_sinh));
3445
3446 case Builtin::BI__builtin_sincospi:
3447 case Builtin::BI__builtin_sincospif:
3448 case Builtin::BI__builtin_sincospil:
3449 if (Builder.getIsFPConstrained())
3450 break; // TODO: Emit constrained sincospi intrinsic once one exists.
3451 emitSincosBuiltin(*this, E, Intrinsic::sincospi);
3452 return RValue::get(nullptr);
3453
3454 case Builtin::BIsincos:
3455 case Builtin::BIsincosf:
3456 case Builtin::BIsincosl:
3457 case Builtin::BI__builtin_sincos:
3458 case Builtin::BI__builtin_sincosf:
3459 case Builtin::BI__builtin_sincosf16:
3460 case Builtin::BI__builtin_sincosl:
3461 case Builtin::BI__builtin_sincosf128:
3462 if (Builder.getIsFPConstrained())
3463 break; // TODO: Emit constrained sincos intrinsic once one exists.
3464 emitSincosBuiltin(*this, E, Intrinsic::sincos);
3465 return RValue::get(nullptr);
3466
3467 case Builtin::BIsqrt:
3468 case Builtin::BIsqrtf:
3469 case Builtin::BIsqrtl:
3470 case Builtin::BI__builtin_sqrt:
3471 case Builtin::BI__builtin_sqrtf:
3472 case Builtin::BI__builtin_sqrtf16:
3473 case Builtin::BI__builtin_sqrtl:
3474 case Builtin::BI__builtin_sqrtf128:
3475 case Builtin::BI__builtin_elementwise_sqrt: {
3477 *this, E, Intrinsic::sqrt, Intrinsic::experimental_constrained_sqrt);
3479 return RValue::get(Call);
3480 }
3481
3482 case Builtin::BItan:
3483 case Builtin::BItanf:
3484 case Builtin::BItanl:
3485 case Builtin::BI__builtin_tan:
3486 case Builtin::BI__builtin_tanf:
3487 case Builtin::BI__builtin_tanf16:
3488 case Builtin::BI__builtin_tanl:
3489 case Builtin::BI__builtin_tanf128:
3490 case Builtin::BI__builtin_elementwise_tan:
3492 *this, E, Intrinsic::tan, Intrinsic::experimental_constrained_tan));
3493
3494 case Builtin::BItanh:
3495 case Builtin::BItanhf:
3496 case Builtin::BItanhl:
3497 case Builtin::BI__builtin_tanh:
3498 case Builtin::BI__builtin_tanhf:
3499 case Builtin::BI__builtin_tanhf16:
3500 case Builtin::BI__builtin_tanhl:
3501 case Builtin::BI__builtin_tanhf128:
3502 case Builtin::BI__builtin_elementwise_tanh:
3504 *this, E, Intrinsic::tanh, Intrinsic::experimental_constrained_tanh));
3505
3506 case Builtin::BItrunc:
3507 case Builtin::BItruncf:
3508 case Builtin::BItruncl:
3509 case Builtin::BI__builtin_trunc:
3510 case Builtin::BI__builtin_truncf:
3511 case Builtin::BI__builtin_truncf16:
3512 case Builtin::BI__builtin_truncl:
3513 case Builtin::BI__builtin_truncf128:
3514 case Builtin::BI__builtin_elementwise_trunc:
3516 Intrinsic::trunc,
3517 Intrinsic::experimental_constrained_trunc));
3518
3519 case Builtin::BIlround:
3520 case Builtin::BIlroundf:
3521 case Builtin::BIlroundl:
3522 case Builtin::BI__builtin_lround:
3523 case Builtin::BI__builtin_lroundf:
3524 case Builtin::BI__builtin_lroundl:
3525 case Builtin::BI__builtin_lroundf128:
3527 *this, E, Intrinsic::lround,
3528 Intrinsic::experimental_constrained_lround));
3529
3530 case Builtin::BIllround:
3531 case Builtin::BIllroundf:
3532 case Builtin::BIllroundl:
3533 case Builtin::BI__builtin_llround:
3534 case Builtin::BI__builtin_llroundf:
3535 case Builtin::BI__builtin_llroundl:
3536 case Builtin::BI__builtin_llroundf128:
3538 *this, E, Intrinsic::llround,
3539 Intrinsic::experimental_constrained_llround));
3540
3541 case Builtin::BIlrint:
3542 case Builtin::BIlrintf:
3543 case Builtin::BIlrintl:
3544 case Builtin::BI__builtin_lrint:
3545 case Builtin::BI__builtin_lrintf:
3546 case Builtin::BI__builtin_lrintl:
3547 case Builtin::BI__builtin_lrintf128:
3549 *this, E, Intrinsic::lrint,
3550 Intrinsic::experimental_constrained_lrint));
3551
3552 case Builtin::BIllrint:
3553 case Builtin::BIllrintf:
3554 case Builtin::BIllrintl:
3555 case Builtin::BI__builtin_llrint:
3556 case Builtin::BI__builtin_llrintf:
3557 case Builtin::BI__builtin_llrintl:
3558 case Builtin::BI__builtin_llrintf128:
3560 *this, E, Intrinsic::llrint,
3561 Intrinsic::experimental_constrained_llrint));
3562 case Builtin::BI__builtin_ldexp:
3563 case Builtin::BI__builtin_ldexpf:
3564 case Builtin::BI__builtin_ldexpl:
3565 case Builtin::BI__builtin_ldexpf16:
3566 case Builtin::BI__builtin_ldexpf128:
3567 case Builtin::BI__builtin_elementwise_ldexp:
3569 *this, E, Intrinsic::ldexp,
3570 Intrinsic::experimental_constrained_ldexp));
3571 default:
3572 break;
3573 }
3574 }
3575
3576 // Check NonnullAttribute/NullabilityArg and Alignment.
3577 auto EmitArgCheck = [&](TypeCheckKind Kind, Address A, const Expr *Arg,
3578 unsigned ParmNum) {
3579 Value *Val = A.emitRawPointer(*this);
3580 EmitNonNullArgCheck(RValue::get(Val), Arg->getType(), Arg->getExprLoc(), FD,
3581 ParmNum);
3582
3583 if (SanOpts.has(SanitizerKind::Alignment)) {
3584 SanitizerSet SkippedChecks;
3585 SkippedChecks.set(SanitizerKind::All);
3586 SkippedChecks.clear(SanitizerKind::Alignment);
3587 SourceLocation Loc = Arg->getExprLoc();
3588 // Strip an implicit cast.
3589 if (auto *CE = dyn_cast<ImplicitCastExpr>(Arg))
3590 if (CE->getCastKind() == CK_BitCast)
3591 Arg = CE->getSubExpr();
3592 EmitTypeCheck(Kind, Loc, Val, Arg->getType(), A.getAlignment(),
3593 SkippedChecks);
3594 }
3595 };
3596
3597 switch (BuiltinIDIfNoAsmLabel) {
3598 default: break;
3599 case Builtin::BI__builtin___CFStringMakeConstantString:
3600 case Builtin::BI__builtin___NSStringMakeConstantString:
3601 return RValue::get(ConstantEmitter(*this).emitAbstract(E, E->getType()));
3602 case Builtin::BI__builtin_stdarg_start:
3603 case Builtin::BI__builtin_va_start:
3604 case Builtin::BI__va_start:
3605 case Builtin::BI__builtin_c23_va_start:
3606 case Builtin::BI__builtin_va_end:
3607 EmitVAStartEnd(BuiltinID == Builtin::BI__va_start
3608 ? EmitScalarExpr(E->getArg(0))
3609 : EmitVAListRef(E->getArg(0)).emitRawPointer(*this),
3610 BuiltinID != Builtin::BI__builtin_va_end);
3611 return RValue::get(nullptr);
3612 case Builtin::BI__builtin_va_copy: {
3613 Value *DstPtr = EmitVAListRef(E->getArg(0)).emitRawPointer(*this);
3614 Value *SrcPtr = EmitVAListRef(E->getArg(1)).emitRawPointer(*this);
3615 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::vacopy, {DstPtr->getType()}),
3616 {DstPtr, SrcPtr});
3617 return RValue::get(nullptr);
3618 }
3619 case Builtin::BIabs:
3620 case Builtin::BIlabs:
3621 case Builtin::BIllabs:
3622 case Builtin::BI__builtin_abs:
3623 case Builtin::BI__builtin_labs:
3624 case Builtin::BI__builtin_llabs: {
3625 bool SanitizeOverflow = SanOpts.has(SanitizerKind::SignedIntegerOverflow);
3626
3627 Value *Result;
3628 switch (getLangOpts().getSignedOverflowBehavior()) {
3630 Result = EmitAbs(*this, EmitScalarExpr(E->getArg(0)), false);
3631 break;
3633 if (!SanitizeOverflow) {
3634 Result = EmitAbs(*this, EmitScalarExpr(E->getArg(0)), true);
3635 break;
3636 }
3637 [[fallthrough]];
3639 // TODO: Somehow handle the corner case when the address of abs is taken.
3640 Result = EmitOverflowCheckedAbs(*this, E, SanitizeOverflow);
3641 break;
3642 }
3643 return RValue::get(Result);
3644 }
3645 case Builtin::BI__builtin_complex: {
3646 Value *Real = EmitScalarExpr(E->getArg(0));
3647 Value *Imag = EmitScalarExpr(E->getArg(1));
3648 return RValue::getComplex({Real, Imag});
3649 }
3650 case Builtin::BI__builtin_conj:
3651 case Builtin::BI__builtin_conjf:
3652 case Builtin::BI__builtin_conjl:
3653 case Builtin::BIconj:
3654 case Builtin::BIconjf:
3655 case Builtin::BIconjl: {
3656 ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
3657 Value *Real = ComplexVal.first;
3658 Value *Imag = ComplexVal.second;
3659 Imag = Builder.CreateFNeg(Imag, "neg");
3660 return RValue::getComplex(std::make_pair(Real, Imag));
3661 }
3662 case Builtin::BI__builtin_creal:
3663 case Builtin::BI__builtin_crealf:
3664 case Builtin::BI__builtin_creall:
3665 case Builtin::BIcreal:
3666 case Builtin::BIcrealf:
3667 case Builtin::BIcreall: {
3668 ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
3669 return RValue::get(ComplexVal.first);
3670 }
3671
3672 case Builtin::BI__builtin_preserve_access_index: {
3673 // Only enabled preserved access index region when debuginfo
3674 // is available as debuginfo is needed to preserve user-level
3675 // access pattern.
3676 if (!getDebugInfo()) {
3677 CGM.Error(E->getExprLoc(), "using builtin_preserve_access_index() without -g");
3678 return RValue::get(EmitScalarExpr(E->getArg(0)));
3679 }
3680
3681 // Nested builtin_preserve_access_index() not supported
3683 CGM.Error(E->getExprLoc(), "nested builtin_preserve_access_index() not supported");
3684 return RValue::get(EmitScalarExpr(E->getArg(0)));
3685 }
3686
3687 IsInPreservedAIRegion = true;
3688 Value *Res = EmitScalarExpr(E->getArg(0));
3689 IsInPreservedAIRegion = false;
3690 return RValue::get(Res);
3691 }
3692
3693 case Builtin::BI__builtin_cimag:
3694 case Builtin::BI__builtin_cimagf:
3695 case Builtin::BI__builtin_cimagl:
3696 case Builtin::BIcimag:
3697 case Builtin::BIcimagf:
3698 case Builtin::BIcimagl: {
3699 ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
3700 return RValue::get(ComplexVal.second);
3701 }
3702
3703 case Builtin::BI__builtin_clrsb:
3704 case Builtin::BI__builtin_clrsbl:
3705 case Builtin::BI__builtin_clrsbll: {
3706 // clrsb(x) -> clz(x < 0 ? ~x : x) - 1 or
3707 Value *ArgValue = EmitScalarExpr(E->getArg(0));
3708
3709 llvm::Type *ArgType = ArgValue->getType();
3710 Function *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
3711
3712 llvm::Type *ResultType = ConvertType(E->getType());
3713 Value *Zero = llvm::Constant::getNullValue(ArgType);
3714 Value *IsNeg = Builder.CreateICmpSLT(ArgValue, Zero, "isneg");
3715 Value *Inverse = Builder.CreateNot(ArgValue, "not");
3716 Value *Tmp = Builder.CreateSelect(IsNeg, Inverse, ArgValue);
3717 Value *Ctlz = Builder.CreateCall(F, {Tmp, Builder.getFalse()});
3718 Value *Result =
3719 Builder.CreateNUWSub(Ctlz, llvm::ConstantInt::get(ArgType, 1));
3720 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
3721 "cast");
3722 return RValue::get(Result);
3723 }
3724 case Builtin::BI__builtin_ctzs:
3725 case Builtin::BI__builtin_ctz:
3726 case Builtin::BI__builtin_ctzl:
3727 case Builtin::BI__builtin_ctzll:
3728 case Builtin::BI__builtin_ctzg:
3729 case Builtin::BI__builtin_elementwise_ctzg: {
3730 bool HasFallback =
3731 (BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_ctzg ||
3732 BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_elementwise_ctzg) &&
3733 E->getNumArgs() > 1;
3734
3735 Value *ArgValue =
3736 HasFallback ? EmitBitCountExpr(*this, E->getArg(0))
3738
3739 llvm::Type *ArgType = ArgValue->getType();
3740 Function *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
3741
3742 llvm::Type *ResultType = ConvertType(E->getType());
3743 // The elementwise builtins always exhibit zero-is-undef behaviour
3744 Value *ZeroUndef = Builder.getInt1(
3745 HasFallback || getTarget().isCLZForZeroUndef() ||
3746 BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_elementwise_ctzg);
3747 Value *Result = Builder.CreateCall(F, {ArgValue, ZeroUndef});
3748 if (Result->getType() != ResultType)
3749 Result =
3750 Builder.CreateIntCast(Result, ResultType, /*isSigned*/ false, "cast");
3751 if (!HasFallback)
3752 return RValue::get(Result);
3753
3754 Value *Zero = Constant::getNullValue(ArgType);
3755 Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
3756 Value *FallbackValue = EmitScalarExpr(E->getArg(1));
3757 Value *ResultOrFallback =
3758 Builder.CreateSelect(IsZero, FallbackValue, Result, "ctzg");
3759 return RValue::get(ResultOrFallback);
3760 }
3761 case Builtin::BI__builtin_clzs:
3762 case Builtin::BI__builtin_clz:
3763 case Builtin::BI__builtin_clzl:
3764 case Builtin::BI__builtin_clzll:
3765 case Builtin::BI__builtin_clzg:
3766 case Builtin::BI__builtin_elementwise_clzg: {
3767 bool HasFallback =
3768 (BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_clzg ||
3769 BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_elementwise_clzg) &&
3770 E->getNumArgs() > 1;
3771
3772 Value *ArgValue =
3773 HasFallback ? EmitBitCountExpr(*this, E->getArg(0))
3775
3776 llvm::Type *ArgType = ArgValue->getType();
3777 Function *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
3778
3779 llvm::Type *ResultType = ConvertType(E->getType());
3780 // The elementwise builtins always exhibit zero-is-undef behaviour
3781 Value *ZeroUndef = Builder.getInt1(
3782 HasFallback || getTarget().isCLZForZeroUndef() ||
3783 BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_elementwise_clzg);
3784 Value *Result = Builder.CreateCall(F, {ArgValue, ZeroUndef});
3785 if (Result->getType() != ResultType)
3786 Result =
3787 Builder.CreateIntCast(Result, ResultType, /*isSigned*/ false, "cast");
3788 if (!HasFallback)
3789 return RValue::get(Result);
3790
3791 Value *Zero = Constant::getNullValue(ArgType);
3792 Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
3793 Value *FallbackValue = EmitScalarExpr(E->getArg(1));
3794 Value *ResultOrFallback =
3795 Builder.CreateSelect(IsZero, FallbackValue, Result, "clzg");
3796 return RValue::get(ResultOrFallback);
3797 }
3798 case Builtin::BI__builtin_ffs:
3799 case Builtin::BI__builtin_ffsl:
3800 case Builtin::BI__builtin_ffsll: {
3801 // ffs(x) -> x ? cttz(x) + 1 : 0
3802 Value *ArgValue = EmitScalarExpr(E->getArg(0));
3803
3804 llvm::Type *ArgType = ArgValue->getType();
3805 Function *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
3806
3807 llvm::Type *ResultType = ConvertType(E->getType());
3808 Value *Tmp =
3809 Builder.CreateAdd(Builder.CreateCall(F, {ArgValue, Builder.getTrue()}),
3810 llvm::ConstantInt::get(ArgType, 1));
3811 Value *Zero = llvm::Constant::getNullValue(ArgType);
3812 Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
3813 Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs");
3814 if (Result->getType() != ResultType)
3815 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
3816 "cast");
3817 return RValue::get(Result);
3818 }
3819 case Builtin::BI__builtin_parity:
3820 case Builtin::BI__builtin_parityl:
3821 case Builtin::BI__builtin_parityll: {
3822 // parity(x) -> ctpop(x) & 1
3823 Value *ArgValue = EmitScalarExpr(E->getArg(0));
3824
3825 llvm::Type *ArgType = ArgValue->getType();
3826 Function *F = CGM.getIntrinsic(Intrinsic::ctpop, ArgType);
3827
3828 llvm::Type *ResultType = ConvertType(E->getType());
3829 Value *Tmp = Builder.CreateCall(F, ArgValue);
3830 Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1));
3831 if (Result->getType() != ResultType)
3832 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
3833 "cast");
3834 return RValue::get(Result);
3835 }
3836 case Builtin::BI__lzcnt16:
3837 case Builtin::BI__lzcnt:
3838 case Builtin::BI__lzcnt64: {
3839 Value *ArgValue = EmitScalarExpr(E->getArg(0));
3840
3841 llvm::Type *ArgType = ArgValue->getType();
3842 Function *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
3843
3844 llvm::Type *ResultType = ConvertType(E->getType());
3845 Value *Result = Builder.CreateCall(F, {ArgValue, Builder.getFalse()});
3846 if (Result->getType() != ResultType)
3847 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
3848 "cast");
3849 return RValue::get(Result);
3850 }
3851 case Builtin::BI__popcnt16:
3852 case Builtin::BI__popcnt:
3853 case Builtin::BI__popcnt64:
3854 case Builtin::BI__builtin_popcount:
3855 case Builtin::BI__builtin_popcountl:
3856 case Builtin::BI__builtin_popcountll:
3857 case Builtin::BI__builtin_popcountg: {
3858 Value *ArgValue = EmitBitCountExpr(*this, E->getArg(0));
3859
3860 llvm::Type *ArgType = ArgValue->getType();
3861 Function *F = CGM.getIntrinsic(Intrinsic::ctpop, ArgType);
3862
3863 llvm::Type *ResultType = ConvertType(E->getType());
3864 Value *Result = Builder.CreateCall(F, ArgValue);
3865 if (Result->getType() != ResultType)
3866 Result =
3867 Builder.CreateIntCast(Result, ResultType, /*isSigned*/ false, "cast");
3868 return RValue::get(Result);
3869 }
3870 case Builtin::BI__builtin_unpredictable: {
3871 // Always return the argument of __builtin_unpredictable. LLVM does not
3872 // handle this builtin. Metadata for this builtin should be added directly
3873 // to instructions such as branches or switches that use it.
3874 return RValue::get(EmitScalarExpr(E->getArg(0)));
3875 }
3876 case Builtin::BI__builtin_expect: {
3877 Value *ArgValue = EmitScalarExpr(E->getArg(0));
3878 llvm::Type *ArgType = ArgValue->getType();
3879
3880 Value *ExpectedValue = EmitScalarExpr(E->getArg(1));
3881 // Don't generate llvm.expect on -O0 as the backend won't use it for
3882 // anything.
3883 // Note, we still IRGen ExpectedValue because it could have side-effects.
3884 if (CGM.getCodeGenOpts().OptimizationLevel == 0)
3885 return RValue::get(ArgValue);
3886
3887 Function *FnExpect = CGM.getIntrinsic(Intrinsic::expect, ArgType);
3888 Value *Result =
3889 Builder.CreateCall(FnExpect, {ArgValue, ExpectedValue}, "expval");
3890 return RValue::get(Result);
3891 }
3892 case Builtin::BI__builtin_expect_with_probability: {
3893 Value *ArgValue = EmitScalarExpr(E->getArg(0));
3894 llvm::Type *ArgType = ArgValue->getType();
3895
3896 Value *ExpectedValue = EmitScalarExpr(E->getArg(1));
3897 llvm::APFloat Probability(0.0);
3898 const Expr *ProbArg = E->getArg(2);
3899 bool EvalSucceed = ProbArg->EvaluateAsFloat(Probability, CGM.getContext());
3900 assert(EvalSucceed && "probability should be able to evaluate as float");
3901 (void)EvalSucceed;
3902 bool LoseInfo = false;
3903 Probability.convert(llvm::APFloat::IEEEdouble(),
3904 llvm::RoundingMode::Dynamic, &LoseInfo);
3905 llvm::Type *Ty = ConvertType(ProbArg->getType());
3906 Constant *Confidence = ConstantFP::get(Ty, Probability);
3907 // Don't generate llvm.expect.with.probability on -O0 as the backend
3908 // won't use it for anything.
3909 // Note, we still IRGen ExpectedValue because it could have side-effects.
3910 if (CGM.getCodeGenOpts().OptimizationLevel == 0)
3911 return RValue::get(ArgValue);
3912
3913 Function *FnExpect =
3914 CGM.getIntrinsic(Intrinsic::expect_with_probability, ArgType);
3915 Value *Result = Builder.CreateCall(
3916 FnExpect, {ArgValue, ExpectedValue, Confidence}, "expval");
3917 return RValue::get(Result);
3918 }
3919 case Builtin::BI__builtin_assume_aligned: {
3920 const Expr *Ptr = E->getArg(0);
3921 Value *PtrValue = EmitScalarExpr(Ptr);
3922 Value *OffsetValue =
3923 (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : nullptr;
3924
3925 Value *AlignmentValue = EmitScalarExpr(E->getArg(1));
3926 ConstantInt *AlignmentCI = cast<ConstantInt>(AlignmentValue);
3927 if (AlignmentCI->getValue().ugt(llvm::Value::MaximumAlignment))
3928 AlignmentCI = ConstantInt::get(AlignmentCI->getIntegerType(),
3929 llvm::Value::MaximumAlignment);
3930
3931 emitAlignmentAssumption(PtrValue, Ptr,
3932 /*The expr loc is sufficient.*/ SourceLocation(),
3933 AlignmentCI, OffsetValue);
3934 return RValue::get(PtrValue);
3935 }
3936 case Builtin::BI__builtin_assume_dereferenceable: {
3937 const Expr *Ptr = E->getArg(0);
3938 const Expr *Size = E->getArg(1);
3939 Value *PtrValue = EmitScalarExpr(Ptr);
3940 Value *SizeValue = EmitScalarExpr(Size);
3941 if (SizeValue->getType() != IntPtrTy)
3942 SizeValue =
3943 Builder.CreateIntCast(SizeValue, IntPtrTy, false, "casted.size");
3944 Builder.CreateDereferenceableAssumption(PtrValue, SizeValue);
3945 return RValue::get(nullptr);
3946 }
3947 case Builtin::BI__assume:
3948 case Builtin::BI__builtin_assume: {
3949 if (E->getArg(0)->HasSideEffects(getContext()))
3950 return RValue::get(nullptr);
3951
3952 Value *ArgValue = EmitCheckedArgForAssume(E->getArg(0));
3953 Function *FnAssume = CGM.getIntrinsic(Intrinsic::assume);
3954 Builder.CreateCall(FnAssume, ArgValue);
3955 return RValue::get(nullptr);
3956 }
3957 case Builtin::BI__builtin_assume_separate_storage: {
3958 const Expr *Arg0 = E->getArg(0);
3959 const Expr *Arg1 = E->getArg(1);
3960
3961 Value *Value0 = EmitScalarExpr(Arg0);
3962 Value *Value1 = EmitScalarExpr(Arg1);
3963
3964 Value *Values[] = {Value0, Value1};
3965 OperandBundleDefT<Value *> OBD("separate_storage", Values);
3966 Builder.CreateAssumption({OBD});
3967 return RValue::get(nullptr);
3968 }
3969 case Builtin::BI__builtin_allow_runtime_check: {
3970 StringRef Kind =
3971 cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())->getString();
3972 LLVMContext &Ctx = CGM.getLLVMContext();
3973 llvm::Value *Allow = Builder.CreateCall(
3974 CGM.getIntrinsic(Intrinsic::allow_runtime_check),
3975 llvm::MetadataAsValue::get(Ctx, llvm::MDString::get(Ctx, Kind)));
3976 return RValue::get(Allow);
3977 }
3978 case Builtin::BI__builtin_allow_sanitize_check: {
3979 Intrinsic::ID IntrID = Intrinsic::not_intrinsic;
3980 StringRef Name =
3981 cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())->getString();
3982
3983 // We deliberately allow the use of kernel- and non-kernel names
3984 // interchangably, even when one or the other is enabled. This is consistent
3985 // with the no_sanitize-attribute, which allows either kernel- or non-kernel
3986 // name to disable instrumentation (see CodeGenFunction::StartFunction).
3987 if (getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
3988 SanitizerKind::KernelAddress) &&
3989 (Name == "address" || Name == "kernel-address")) {
3990 IntrID = Intrinsic::allow_sanitize_address;
3991 } else if (getLangOpts().Sanitize.has(SanitizerKind::Thread) &&
3992 Name == "thread") {
3993 IntrID = Intrinsic::allow_sanitize_thread;
3994 } else if (getLangOpts().Sanitize.hasOneOf(SanitizerKind::Memory |
3995 SanitizerKind::KernelMemory) &&
3996 (Name == "memory" || Name == "kernel-memory")) {
3997 IntrID = Intrinsic::allow_sanitize_memory;
3998 } else if (getLangOpts().Sanitize.hasOneOf(
3999 SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress) &&
4000 (Name == "hwaddress" || Name == "kernel-hwaddress")) {
4001 IntrID = Intrinsic::allow_sanitize_hwaddress;
4002 }
4003
4004 if (IntrID != Intrinsic::not_intrinsic) {
4005 llvm::Value *Allow = Builder.CreateCall(CGM.getIntrinsic(IntrID));
4006 return RValue::get(Allow);
4007 }
4008 // If the checked sanitizer is not enabled, we can safely lower to false
4009 // right away. This is also more efficient, since the LowerAllowCheckPass
4010 // must not always be enabled if none of the above sanitizers are enabled.
4011 return RValue::get(Builder.getFalse());
4012 }
4013 case Builtin::BI__arithmetic_fence: {
4014 // Create the builtin call if FastMath is selected, and the target
4015 // supports the builtin, otherwise just return the argument.
4016 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4017 llvm::FastMathFlags FMF = Builder.getFastMathFlags();
4018 bool isArithmeticFenceEnabled =
4019 FMF.allowReassoc() &&
4021 QualType ArgType = E->getArg(0)->getType();
4022 if (ArgType->isComplexType()) {
4023 if (isArithmeticFenceEnabled) {
4024 QualType ElementType = ArgType->castAs<ComplexType>()->getElementType();
4025 ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
4026 Value *Real = Builder.CreateArithmeticFence(ComplexVal.first,
4027 ConvertType(ElementType));
4028 Value *Imag = Builder.CreateArithmeticFence(ComplexVal.second,
4029 ConvertType(ElementType));
4030 return RValue::getComplex(std::make_pair(Real, Imag));
4031 }
4032 ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
4033 Value *Real = ComplexVal.first;
4034 Value *Imag = ComplexVal.second;
4035 return RValue::getComplex(std::make_pair(Real, Imag));
4036 }
4037 Value *ArgValue = EmitScalarExpr(E->getArg(0));
4038 if (isArithmeticFenceEnabled)
4039 return RValue::get(
4040 Builder.CreateArithmeticFence(ArgValue, ConvertType(ArgType)));
4041 return RValue::get(ArgValue);
4042 }
4043 case Builtin::BI__builtin_bswapg: {
4044 Value *ArgValue = EmitScalarExpr(E->getArg(0));
4045 llvm::IntegerType *IntTy = cast<llvm::IntegerType>(ArgValue->getType());
4046 assert(IntTy && "LLVM's __builtin_bswapg only supports integer variants");
4047 if (IntTy->getBitWidth() == 1 || IntTy->getBitWidth() == 8)
4048 return RValue::get(ArgValue);
4049 assert(((IntTy->getBitWidth() % 16 == 0 && IntTy->getBitWidth() != 0)) &&
4050 "LLVM's __builtin_bswapg only supports integer variants that has a "
4051 "multiple of 16 bits as well as a single byte");
4052 return RValue::get(
4053 emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::bswap));
4054 }
4055 case Builtin::BI__builtin_bswap16:
4056 case Builtin::BI__builtin_bswap32:
4057 case Builtin::BI__builtin_bswap64:
4058 case Builtin::BI_byteswap_ushort:
4059 case Builtin::BI_byteswap_ulong:
4060 case Builtin::BI_byteswap_uint64: {
4061 return RValue::get(
4062 emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::bswap));
4063 }
4064 case Builtin::BI__builtin_bitreverseg: {
4065 Value *ArgValue = EmitScalarExpr(E->getArg(0));
4066 llvm::IntegerType *IntTy = cast<llvm::IntegerType>(ArgValue->getType());
4067 assert(IntTy &&
4068 "LLVM's __builtin_bitreverseg only support integer variants");
4069 if (IntTy->getBitWidth() == 1)
4070 return RValue::get(ArgValue);
4071 return RValue::get(
4072 emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::bitreverse));
4073 }
4074 case Builtin::BI__builtin_bitreverse8:
4075 case Builtin::BI__builtin_bitreverse16:
4076 case Builtin::BI__builtin_bitreverse32:
4077 case Builtin::BI__builtin_bitreverse64: {
4078 return RValue::get(
4079 emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::bitreverse));
4080 }
4081 case Builtin::BI__builtin_rotateleft8:
4082 case Builtin::BI__builtin_rotateleft16:
4083 case Builtin::BI__builtin_rotateleft32:
4084 case Builtin::BI__builtin_rotateleft64:
4085 case Builtin::BI__builtin_stdc_rotate_left:
4086 case Builtin::BIstdc_rotate_left_uc:
4087 case Builtin::BIstdc_rotate_left_us:
4088 case Builtin::BIstdc_rotate_left_ui:
4089 case Builtin::BIstdc_rotate_left_ul:
4090 case Builtin::BIstdc_rotate_left_ull:
4091 case Builtin::BI_rotl8: // Microsoft variants of rotate left
4092 case Builtin::BI_rotl16:
4093 case Builtin::BI_rotl:
4094 case Builtin::BI_lrotl:
4095 case Builtin::BI_rotl64:
4096 return emitRotate(E, false);
4097
4098 case Builtin::BI__builtin_rotateright8:
4099 case Builtin::BI__builtin_rotateright16:
4100 case Builtin::BI__builtin_rotateright32:
4101 case Builtin::BI__builtin_rotateright64:
4102 case Builtin::BI__builtin_stdc_rotate_right:
4103 case Builtin::BIstdc_rotate_right_uc:
4104 case Builtin::BIstdc_rotate_right_us:
4105 case Builtin::BIstdc_rotate_right_ui:
4106 case Builtin::BIstdc_rotate_right_ul:
4107 case Builtin::BIstdc_rotate_right_ull:
4108 case Builtin::BI_rotr8: // Microsoft variants of rotate right
4109 case Builtin::BI_rotr16:
4110 case Builtin::BI_rotr:
4111 case Builtin::BI_lrotr:
4112 case Builtin::BI_rotr64:
4113 return emitRotate(E, true);
4114
4115 case Builtin::BIstdc_leading_zeros_uc:
4116 case Builtin::BIstdc_leading_zeros_us:
4117 case Builtin::BIstdc_leading_zeros_ui:
4118 case Builtin::BIstdc_leading_zeros_ul:
4119 case Builtin::BIstdc_leading_zeros_ull:
4120 case Builtin::BI__builtin_stdc_leading_zeros:
4121 return emitStdcCountIntrinsic(E, Intrinsic::ctlz, /*InvertArg=*/false);
4122 case Builtin::BIstdc_leading_ones_uc:
4123 case Builtin::BIstdc_leading_ones_us:
4124 case Builtin::BIstdc_leading_ones_ui:
4125 case Builtin::BIstdc_leading_ones_ul:
4126 case Builtin::BIstdc_leading_ones_ull:
4127 case Builtin::BI__builtin_stdc_leading_ones:
4128 return emitStdcCountIntrinsic(E, Intrinsic::ctlz, /*InvertArg=*/true);
4129 case Builtin::BIstdc_trailing_zeros_uc:
4130 case Builtin::BIstdc_trailing_zeros_us:
4131 case Builtin::BIstdc_trailing_zeros_ui:
4132 case Builtin::BIstdc_trailing_zeros_ul:
4133 case Builtin::BIstdc_trailing_zeros_ull:
4134 case Builtin::BI__builtin_stdc_trailing_zeros:
4135 return emitStdcCountIntrinsic(E, Intrinsic::cttz, /*InvertArg=*/false);
4136 case Builtin::BIstdc_trailing_ones_uc:
4137 case Builtin::BIstdc_trailing_ones_us:
4138 case Builtin::BIstdc_trailing_ones_ui:
4139 case Builtin::BIstdc_trailing_ones_ul:
4140 case Builtin::BIstdc_trailing_ones_ull:
4141 case Builtin::BI__builtin_stdc_trailing_ones:
4142 return emitStdcCountIntrinsic(E, Intrinsic::cttz, /*InvertArg=*/true);
4143 case Builtin::BIstdc_first_leading_zero_uc:
4144 case Builtin::BIstdc_first_leading_zero_us:
4145 case Builtin::BIstdc_first_leading_zero_ui:
4146 case Builtin::BIstdc_first_leading_zero_ul:
4147 case Builtin::BIstdc_first_leading_zero_ull:
4148 case Builtin::BI__builtin_stdc_first_leading_zero:
4149 return emitStdcFirstBit(E, Intrinsic::ctlz, /*InvertArg=*/true);
4150 case Builtin::BIstdc_first_leading_one_uc:
4151 case Builtin::BIstdc_first_leading_one_us:
4152 case Builtin::BIstdc_first_leading_one_ui:
4153 case Builtin::BIstdc_first_leading_one_ul:
4154 case Builtin::BIstdc_first_leading_one_ull:
4155 case Builtin::BI__builtin_stdc_first_leading_one:
4156 return emitStdcFirstBit(E, Intrinsic::ctlz, /*InvertArg=*/false);
4157 case Builtin::BIstdc_first_trailing_zero_uc:
4158 case Builtin::BIstdc_first_trailing_zero_us:
4159 case Builtin::BIstdc_first_trailing_zero_ui:
4160 case Builtin::BIstdc_first_trailing_zero_ul:
4161 case Builtin::BIstdc_first_trailing_zero_ull:
4162 case Builtin::BI__builtin_stdc_first_trailing_zero:
4163 return emitStdcFirstBit(E, Intrinsic::cttz, /*InvertArg=*/true);
4164 case Builtin::BIstdc_first_trailing_one_uc:
4165 case Builtin::BIstdc_first_trailing_one_us:
4166 case Builtin::BIstdc_first_trailing_one_ui:
4167 case Builtin::BIstdc_first_trailing_one_ul:
4168 case Builtin::BIstdc_first_trailing_one_ull:
4169 case Builtin::BI__builtin_stdc_first_trailing_one:
4170 return emitStdcFirstBit(E, Intrinsic::cttz, /*InvertArg=*/false);
4171 case Builtin::BIstdc_count_zeros_uc:
4172 case Builtin::BIstdc_count_zeros_us:
4173 case Builtin::BIstdc_count_zeros_ui:
4174 case Builtin::BIstdc_count_zeros_ul:
4175 case Builtin::BIstdc_count_zeros_ull:
4176 case Builtin::BI__builtin_stdc_count_zeros:
4177 return emitStdcBitWidthMinus(E, Intrinsic::ctpop, /*IsPop=*/true);
4178 case Builtin::BIstdc_count_ones_uc:
4179 case Builtin::BIstdc_count_ones_us:
4180 case Builtin::BIstdc_count_ones_ui:
4181 case Builtin::BIstdc_count_ones_ul:
4182 case Builtin::BIstdc_count_ones_ull:
4183 case Builtin::BI__builtin_stdc_count_ones:
4184 return emitStdcCountIntrinsic(E, Intrinsic::ctpop, /*InvertArg=*/false,
4185 /*IsPop=*/true);
4186 case Builtin::BIstdc_has_single_bit_uc:
4187 case Builtin::BIstdc_has_single_bit_us:
4188 case Builtin::BIstdc_has_single_bit_ui:
4189 case Builtin::BIstdc_has_single_bit_ul:
4190 case Builtin::BIstdc_has_single_bit_ull:
4191 case Builtin::BI__builtin_stdc_has_single_bit: {
4192 Value *ArgValue = EmitScalarExpr(E->getArg(0));
4193 llvm::Type *ArgType = ArgValue->getType();
4194 Value *One = ConstantInt::get(ArgType, 1);
4195 Function *F = CGM.getIntrinsic(Intrinsic::ctpop, ArgType);
4196 Value *PopCnt = Builder.CreateCall(F, ArgValue);
4197 return RValue::get(Builder.CreateICmpEQ(PopCnt, One));
4198 }
4199 case Builtin::BIstdc_bit_width_uc:
4200 case Builtin::BIstdc_bit_width_us:
4201 case Builtin::BIstdc_bit_width_ui:
4202 case Builtin::BIstdc_bit_width_ul:
4203 case Builtin::BIstdc_bit_width_ull:
4204 case Builtin::BI__builtin_stdc_bit_width:
4205 return emitStdcBitWidthMinus(E, Intrinsic::ctlz, /*IsPop=*/false);
4206 case Builtin::BIstdc_bit_floor_uc:
4207 case Builtin::BIstdc_bit_floor_us:
4208 case Builtin::BIstdc_bit_floor_ui:
4209 case Builtin::BIstdc_bit_floor_ul:
4210 case Builtin::BIstdc_bit_floor_ull:
4211 case Builtin::BI__builtin_stdc_bit_floor: {
4212 Value *ArgValue = EmitScalarExpr(E->getArg(0));
4213 llvm::Type *ArgType = ArgValue->getType();
4214 unsigned BitWidth = ArgType->getIntegerBitWidth();
4215 Value *Zero = ConstantInt::get(ArgType, 0);
4216 Value *One = ConstantInt::get(ArgType, 1);
4217 Function *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
4218 Value *LZ = Builder.CreateCall(F, {ArgValue, Builder.getTrue()});
4219 Value *ShiftAmt =
4220 Builder.CreateSub(ConstantInt::get(ArgType, BitWidth - 1), LZ);
4221 Value *Shifted = Builder.CreateShl(One, ShiftAmt);
4222 Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero);
4223 Value *Result = Builder.CreateSelect(IsZero, Zero, Shifted);
4224 return RValue::get(Result);
4225 }
4226 case Builtin::BIstdc_bit_ceil_uc:
4227 case Builtin::BIstdc_bit_ceil_us:
4228 case Builtin::BIstdc_bit_ceil_ui:
4229 case Builtin::BIstdc_bit_ceil_ul:
4230 case Builtin::BIstdc_bit_ceil_ull:
4231 case Builtin::BI__builtin_stdc_bit_ceil: {
4232 Value *ArgValue = EmitScalarExpr(E->getArg(0));
4233 llvm::Type *ArgType = ArgValue->getType();
4234 unsigned BitWidth = ArgType->getIntegerBitWidth();
4235 Value *One = ConstantInt::get(ArgType, 1);
4236 Value *Two = ConstantInt::get(ArgType, 2);
4237
4238 Value *IsLEOne = Builder.CreateICmpULE(ArgValue, One, "isleone");
4239
4240 BasicBlock *EntryBB = Builder.GetInsertBlock();
4241 BasicBlock *CalcBB = createBasicBlock("bitceil.calc", CurFn);
4242 BasicBlock *MergeBB = createBasicBlock("bitceil.merge", CurFn);
4243
4244 Builder.CreateCondBr(IsLEOne, MergeBB, CalcBB);
4245
4246 Builder.SetInsertPoint(CalcBB);
4247 Function *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
4248 Value *ArgMinusOne = Builder.CreateSub(ArgValue, One);
4249 Value *LZ = Builder.CreateCall(F, {ArgMinusOne, Builder.getFalse()});
4250 // 2<<(BitWidth-1-LZ) to get the next power of two. The shift
4251 // amount is always in [0, BitWidth-1], so when LZ==0 (argument has its MSB
4252 // set), the result wraps to 0
4253 Value *ShiftAmt =
4254 Builder.CreateSub(ConstantInt::get(ArgType, BitWidth - 1), LZ);
4255 Value *Tmp = Builder.CreateShl(Two, ShiftAmt);
4256 Builder.CreateBr(MergeBB);
4257
4258 Builder.SetInsertPoint(MergeBB);
4259 PHINode *Phi = Builder.CreatePHI(ArgType, 2);
4260 Phi->addIncoming(One, EntryBB);
4261 Phi->addIncoming(Tmp, CalcBB);
4262 return RValue::get(Phi);
4263 }
4264
4265 case Builtin::BI__builtin_constant_p: {
4266 llvm::Type *ResultType = ConvertType(E->getType());
4267
4268 const Expr *Arg = E->getArg(0);
4269 QualType ArgType = Arg->getType();
4270 // FIXME: The allowance for Obj-C pointers and block pointers is historical
4271 // and likely a mistake.
4272 if (!ArgType->isIntegralOrEnumerationType() && !ArgType->isFloatingType() &&
4273 !ArgType->isObjCObjectPointerType() && !ArgType->isBlockPointerType())
4274 // Per the GCC documentation, only numeric constants are recognized after
4275 // inlining.
4276 return RValue::get(ConstantInt::get(ResultType, 0));
4277
4278 if (Arg->HasSideEffects(getContext()))
4279 // The argument is unevaluated, so be conservative if it might have
4280 // side-effects.
4281 return RValue::get(ConstantInt::get(ResultType, 0));
4282
4283 Value *ArgValue = EmitScalarExpr(Arg);
4284 if (ArgType->isObjCObjectPointerType()) {
4285 // Convert Objective-C objects to id because we cannot distinguish between
4286 // LLVM types for Obj-C classes as they are opaque.
4287 ArgType = CGM.getContext().getObjCIdType();
4288 ArgValue = Builder.CreateBitCast(ArgValue, ConvertType(ArgType));
4289 }
4290 Function *F =
4291 CGM.getIntrinsic(Intrinsic::is_constant, ConvertType(ArgType));
4292 Value *Result = Builder.CreateCall(F, ArgValue);
4293 if (Result->getType() != ResultType)
4294 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/false);
4295 return RValue::get(Result);
4296 }
4297 case Builtin::BI__builtin_dynamic_object_size:
4298 case Builtin::BI__builtin_object_size: {
4299 unsigned Type =
4300 E->getArg(1)->EvaluateKnownConstInt(getContext()).getZExtValue();
4301 auto *ResType = cast<llvm::IntegerType>(ConvertType(E->getType()));
4302
4303 // We pass this builtin onto the optimizer so that it can figure out the
4304 // object size in more complex cases.
4305 bool IsDynamic = BuiltinID == Builtin::BI__builtin_dynamic_object_size;
4306 return RValue::get(emitBuiltinObjectSize(E->getArg(0), Type, ResType,
4307 /*EmittedE=*/nullptr, IsDynamic));
4308 }
4309 case Builtin::BI__builtin_counted_by_ref: {
4310 // Default to returning '(void *) 0'.
4311 llvm::Value *Result = llvm::ConstantPointerNull::get(
4312 llvm::PointerType::getUnqual(getLLVMContext()));
4313
4314 const Expr *Arg = E->getArg(0)->IgnoreParenImpCasts();
4315
4316 if (auto *UO = dyn_cast<UnaryOperator>(Arg);
4317 UO && UO->getOpcode() == UO_AddrOf) {
4318 Arg = UO->getSubExpr()->IgnoreParenImpCasts();
4319
4320 if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Arg))
4321 Arg = ASE->getBase()->IgnoreParenImpCasts();
4322 }
4323
4324 if (const MemberExpr *ME = dyn_cast_if_present<MemberExpr>(Arg)) {
4325 if (auto *CATy =
4327 CATy && CATy->getKind() == CountAttributedType::CountedBy) {
4328 const auto *MemberDecl = cast<FieldDecl>(ME->getMemberDecl());
4329 if (const FieldDecl *CountFD = MemberDecl->findCountedByField())
4330 Result = GetCountedByFieldExprGEP(Arg, MemberDecl, CountFD);
4331 else
4332 llvm::report_fatal_error("Cannot find the counted_by 'count' field");
4333 }
4334 }
4335
4336 return RValue::get(Result);
4337 }
4338 case Builtin::BI__builtin_prefetch: {
4339 Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
4340 // FIXME: Technically these constants should of type 'int', yes?
4341 RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
4342 llvm::ConstantInt::get(Int32Ty, 0);
4343 Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
4344 llvm::ConstantInt::get(Int32Ty, 3);
4345 Value *Data = llvm::ConstantInt::get(Int32Ty, 1);
4346 Function *F = CGM.getIntrinsic(Intrinsic::prefetch, Address->getType());
4347 Builder.CreateCall(F, {Address, RW, Locality, Data});
4348 return RValue::get(nullptr);
4349 }
4350 case Builtin::BI__builtin_readcyclecounter: {
4351 Function *F = CGM.getIntrinsic(Intrinsic::readcyclecounter);
4352 return RValue::get(Builder.CreateCall(F));
4353 }
4354 case Builtin::BI__builtin_readsteadycounter: {
4355 Function *F = CGM.getIntrinsic(Intrinsic::readsteadycounter);
4356 return RValue::get(Builder.CreateCall(F));
4357 }
4358 case Builtin::BI__builtin___clear_cache: {
4359 Value *Begin = EmitScalarExpr(E->getArg(0));
4360 Value *End = EmitScalarExpr(E->getArg(1));
4361 Function *F = CGM.getIntrinsic(Intrinsic::clear_cache, {CGM.DefaultPtrTy});
4362 return RValue::get(Builder.CreateCall(F, {Begin, End}));
4363 }
4364 case Builtin::BI__builtin_trap:
4365 EmitTrapCall(Intrinsic::trap);
4366 return RValue::get(nullptr);
4367 case Builtin::BI__builtin_verbose_trap: {
4368 llvm::DILocation *TrapLocation = Builder.getCurrentDebugLocation();
4369 if (getDebugInfo()) {
4370 TrapLocation = getDebugInfo()->CreateTrapFailureMessageFor(
4371 TrapLocation, *E->getArg(0)->tryEvaluateString(getContext()),
4373 }
4374 ApplyDebugLocation ApplyTrapDI(*this, TrapLocation);
4375 // Currently no attempt is made to prevent traps from being merged.
4376 EmitTrapCall(Intrinsic::trap);
4377 return RValue::get(nullptr);
4378 }
4379 case Builtin::BI__debugbreak:
4380 EmitTrapCall(Intrinsic::debugtrap);
4381 return RValue::get(nullptr);
4382 case Builtin::BI__builtin_unreachable: {
4384
4385 // We do need to preserve an insertion point.
4386 EmitBlock(createBasicBlock("unreachable.cont"));
4387
4388 return RValue::get(nullptr);
4389 }
4390
4391 case Builtin::BI__builtin_powi:
4392 case Builtin::BI__builtin_powif:
4393 case Builtin::BI__builtin_powil: {
4394 llvm::Value *Src0 = EmitScalarExpr(E->getArg(0));
4395 llvm::Value *Src1 = EmitScalarExpr(E->getArg(1));
4396
4397 if (Builder.getIsFPConstrained()) {
4398 // FIXME: llvm.powi has 2 mangling types,
4399 // llvm.experimental.constrained.powi has one.
4400 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4401 Function *F = CGM.getIntrinsic(Intrinsic::experimental_constrained_powi,
4402 Src0->getType());
4403 return RValue::get(Builder.CreateConstrainedFPCall(F, { Src0, Src1 }));
4404 }
4405
4406 Function *F = CGM.getIntrinsic(Intrinsic::powi,
4407 { Src0->getType(), Src1->getType() });
4408 return RValue::get(Builder.CreateCall(F, { Src0, Src1 }));
4409 }
4410 case Builtin::BI__builtin_frexpl: {
4411 // Linux PPC will not be adding additional PPCDoubleDouble support.
4412 // WIP to switch default to IEEE long double. Will emit libcall for
4413 // frexpl instead of legalizing this type in the BE.
4414 if (&getTarget().getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble())
4415 break;
4416 [[fallthrough]];
4417 }
4418 case Builtin::BI__builtin_frexp:
4419 case Builtin::BI__builtin_frexpf:
4420 case Builtin::BI__builtin_frexpf128:
4421 case Builtin::BI__builtin_frexpf16:
4422 return RValue::get(emitFrexpBuiltin(*this, E, Intrinsic::frexp));
4423 case Builtin::BImodf:
4424 case Builtin::BImodff:
4425 case Builtin::BImodfl:
4426 case Builtin::BI__builtin_modf:
4427 case Builtin::BI__builtin_modff:
4428 case Builtin::BI__builtin_modfl:
4429 if (Builder.getIsFPConstrained())
4430 break; // TODO: Emit constrained modf intrinsic once one exists.
4431 return RValue::get(emitModfBuiltin(*this, E, Intrinsic::modf));
4432 case Builtin::BI__builtin_isgreater:
4433 case Builtin::BI__builtin_isgreaterequal:
4434 case Builtin::BI__builtin_isless:
4435 case Builtin::BI__builtin_islessequal:
4436 case Builtin::BI__builtin_islessgreater:
4437 case Builtin::BI__builtin_isunordered: {
4438 // Ordered comparisons: we know the arguments to these are matching scalar
4439 // floating point values.
4440 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4441 Value *LHS = EmitScalarExpr(E->getArg(0));
4442 Value *RHS = EmitScalarExpr(E->getArg(1));
4443
4444 switch (BuiltinID) {
4445 default: llvm_unreachable("Unknown ordered comparison");
4446 case Builtin::BI__builtin_isgreater:
4447 LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
4448 break;
4449 case Builtin::BI__builtin_isgreaterequal:
4450 LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
4451 break;
4452 case Builtin::BI__builtin_isless:
4453 LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
4454 break;
4455 case Builtin::BI__builtin_islessequal:
4456 LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
4457 break;
4458 case Builtin::BI__builtin_islessgreater:
4459 LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
4460 break;
4461 case Builtin::BI__builtin_isunordered:
4462 LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
4463 break;
4464 }
4465 // ZExt bool to int type.
4466 return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType())));
4467 }
4468
4469 case Builtin::BI__builtin_isnan: {
4470 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4471 Value *V = EmitScalarExpr(E->getArg(0));
4472 if (Value *Result = tryUseTestFPKind(*this, BuiltinID, V))
4473 return RValue::get(Result);
4474 return RValue::get(
4475 Builder.CreateZExt(Builder.createIsFPClass(V, FPClassTest::fcNan),
4476 ConvertType(E->getType())));
4477 }
4478
4479 case Builtin::BI__builtin_issignaling: {
4480 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4481 Value *V = EmitScalarExpr(E->getArg(0));
4482 return RValue::get(
4483 Builder.CreateZExt(Builder.createIsFPClass(V, FPClassTest::fcSNan),
4484 ConvertType(E->getType())));
4485 }
4486
4487 case Builtin::BI__builtin_isinf: {
4488 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4489 Value *V = EmitScalarExpr(E->getArg(0));
4490 if (Value *Result = tryUseTestFPKind(*this, BuiltinID, V))
4491 return RValue::get(Result);
4492 return RValue::get(
4493 Builder.CreateZExt(Builder.createIsFPClass(V, FPClassTest::fcInf),
4494 ConvertType(E->getType())));
4495 }
4496
4497 case Builtin::BIfinite:
4498 case Builtin::BI__finite:
4499 case Builtin::BIfinitef:
4500 case Builtin::BI__finitef:
4501 case Builtin::BIfinitel:
4502 case Builtin::BI__finitel:
4503 case Builtin::BI__builtin_isfinite: {
4504 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4505 Value *V = EmitScalarExpr(E->getArg(0));
4506 if (Value *Result = tryUseTestFPKind(*this, BuiltinID, V))
4507 return RValue::get(Result);
4508 return RValue::get(
4509 Builder.CreateZExt(Builder.createIsFPClass(V, FPClassTest::fcFinite),
4510 ConvertType(E->getType())));
4511 }
4512
4513 case Builtin::BI__builtin_isnormal: {
4514 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4515 Value *V = EmitScalarExpr(E->getArg(0));
4516 return RValue::get(
4517 Builder.CreateZExt(Builder.createIsFPClass(V, FPClassTest::fcNormal),
4518 ConvertType(E->getType())));
4519 }
4520
4521 case Builtin::BI__builtin_issubnormal: {
4522 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4523 Value *V = EmitScalarExpr(E->getArg(0));
4524 return RValue::get(
4525 Builder.CreateZExt(Builder.createIsFPClass(V, FPClassTest::fcSubnormal),
4526 ConvertType(E->getType())));
4527 }
4528
4529 case Builtin::BI__builtin_iszero: {
4530 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4531 Value *V = EmitScalarExpr(E->getArg(0));
4532 return RValue::get(
4533 Builder.CreateZExt(Builder.createIsFPClass(V, FPClassTest::fcZero),
4534 ConvertType(E->getType())));
4535 }
4536
4537 case Builtin::BI__builtin_isfpclass: {
4539 if (!E->getArg(1)->EvaluateAsInt(Result, CGM.getContext()))
4540 break;
4541 uint64_t Test = Result.Val.getInt().getLimitedValue();
4542 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4543 Value *V = EmitScalarExpr(E->getArg(0));
4544 return RValue::get(Builder.CreateZExt(Builder.createIsFPClass(V, Test),
4545 ConvertType(E->getType())));
4546 }
4547
4548 case Builtin::BI__builtin_nondeterministic_value: {
4549 llvm::Type *Ty = ConvertType(E->getArg(0)->getType());
4550
4551 Value *Result = PoisonValue::get(Ty);
4552 Result = Builder.CreateFreeze(Result);
4553
4554 return RValue::get(Result);
4555 }
4556
4557 case Builtin::BI__builtin_elementwise_abs: {
4558 Value *Result;
4559 QualType QT = E->getArg(0)->getType();
4560
4561 if (auto *VecTy = QT->getAs<VectorType>())
4562 QT = VecTy->getElementType();
4563 if (QT->isIntegerType())
4564 Result = Builder.CreateBinaryIntrinsic(
4565 Intrinsic::abs, EmitScalarExpr(E->getArg(0)), Builder.getFalse(),
4566 nullptr, "elt.abs");
4567 else
4568 Result = emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::fabs,
4569 "elt.abs");
4570
4571 return RValue::get(Result);
4572 }
4573 case Builtin::BI__builtin_elementwise_bitreverse:
4575 *this, E, Intrinsic::bitreverse, "elt.bitreverse"));
4576 case Builtin::BI__builtin_elementwise_popcount:
4578 *this, E, Intrinsic::ctpop, "elt.ctpop"));
4579 case Builtin::BI__builtin_elementwise_canonicalize:
4581 *this, E, Intrinsic::canonicalize, "elt.canonicalize"));
4582 case Builtin::BI__builtin_elementwise_copysign:
4583 return RValue::get(
4584 emitBuiltinWithOneOverloadedType<2>(*this, E, Intrinsic::copysign));
4585 case Builtin::BI__builtin_elementwise_fshl:
4586 return RValue::get(
4587 emitBuiltinWithOneOverloadedType<3>(*this, E, Intrinsic::fshl));
4588 case Builtin::BI__builtin_elementwise_fshr:
4589 return RValue::get(
4590 emitBuiltinWithOneOverloadedType<3>(*this, E, Intrinsic::fshr));
4591 case Builtin::BI__builtin_elementwise_clmul:
4592 return RValue::get(
4593 emitBuiltinWithOneOverloadedType<2>(*this, E, Intrinsic::clmul));
4594
4595 case Builtin::BI__builtin_elementwise_add_sat:
4596 case Builtin::BI__builtin_elementwise_sub_sat: {
4597 Value *Op0 = EmitScalarExpr(E->getArg(0));
4598 Value *Op1 = EmitScalarExpr(E->getArg(1));
4599 Value *Result;
4600 assert(Op0->getType()->isIntOrIntVectorTy() && "integer type expected");
4601 QualType Ty = E->getArg(0)->getType();
4602 if (auto *VecTy = Ty->getAs<VectorType>())
4603 Ty = VecTy->getElementType();
4604 bool IsSigned = Ty->isSignedIntegerType();
4605 unsigned Opc;
4606 if (BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_elementwise_add_sat)
4607 Opc = IsSigned ? Intrinsic::sadd_sat : Intrinsic::uadd_sat;
4608 else
4609 Opc = IsSigned ? Intrinsic::ssub_sat : Intrinsic::usub_sat;
4610 Result = Builder.CreateBinaryIntrinsic(Opc, Op0, Op1, nullptr, "elt.sat");
4611 return RValue::get(Result);
4612 }
4613
4614 case Builtin::BI__builtin_elementwise_max: {
4615 Value *Op0 = EmitScalarExpr(E->getArg(0));
4616 Value *Op1 = EmitScalarExpr(E->getArg(1));
4617 Value *Result;
4618 if (Op0->getType()->isIntOrIntVectorTy()) {
4619 QualType Ty = E->getArg(0)->getType();
4620 if (auto *VecTy = Ty->getAs<VectorType>())
4621 Ty = VecTy->getElementType();
4622 Result = Builder.CreateBinaryIntrinsic(
4623 Ty->isSignedIntegerType() ? Intrinsic::smax : Intrinsic::umax, Op0,
4624 Op1, nullptr, "elt.max");
4625 } else
4626 Result = Builder.CreateMaxNum(Op0, Op1, /*FMFSource=*/nullptr, "elt.max");
4627 return RValue::get(Result);
4628 }
4629 case Builtin::BI__builtin_elementwise_min: {
4630 Value *Op0 = EmitScalarExpr(E->getArg(0));
4631 Value *Op1 = EmitScalarExpr(E->getArg(1));
4632 Value *Result;
4633 if (Op0->getType()->isIntOrIntVectorTy()) {
4634 QualType Ty = E->getArg(0)->getType();
4635 if (auto *VecTy = Ty->getAs<VectorType>())
4636 Ty = VecTy->getElementType();
4637 Result = Builder.CreateBinaryIntrinsic(
4638 Ty->isSignedIntegerType() ? Intrinsic::smin : Intrinsic::umin, Op0,
4639 Op1, nullptr, "elt.min");
4640 } else
4641 Result = Builder.CreateMinNum(Op0, Op1, /*FMFSource=*/nullptr, "elt.min");
4642 return RValue::get(Result);
4643 }
4644
4645 case Builtin::BI__builtin_elementwise_maxnum: {
4646 Value *Op0 = EmitScalarExpr(E->getArg(0));
4647 Value *Op1 = EmitScalarExpr(E->getArg(1));
4648 Value *Result = Builder.CreateBinaryIntrinsic(llvm::Intrinsic::maxnum, Op0,
4649 Op1, nullptr, "elt.maxnum");
4650 return RValue::get(Result);
4651 }
4652
4653 case Builtin::BI__builtin_elementwise_minnum: {
4654 Value *Op0 = EmitScalarExpr(E->getArg(0));
4655 Value *Op1 = EmitScalarExpr(E->getArg(1));
4656 Value *Result = Builder.CreateBinaryIntrinsic(llvm::Intrinsic::minnum, Op0,
4657 Op1, nullptr, "elt.minnum");
4658 return RValue::get(Result);
4659 }
4660
4661 case Builtin::BI__builtin_elementwise_maximum: {
4662 Value *Op0 = EmitScalarExpr(E->getArg(0));
4663 Value *Op1 = EmitScalarExpr(E->getArg(1));
4664 Value *Result = Builder.CreateBinaryIntrinsic(Intrinsic::maximum, Op0, Op1,
4665 nullptr, "elt.maximum");
4666 return RValue::get(Result);
4667 }
4668
4669 case Builtin::BI__builtin_elementwise_minimum: {
4670 Value *Op0 = EmitScalarExpr(E->getArg(0));
4671 Value *Op1 = EmitScalarExpr(E->getArg(1));
4672 Value *Result = Builder.CreateBinaryIntrinsic(Intrinsic::minimum, Op0, Op1,
4673 nullptr, "elt.minimum");
4674 return RValue::get(Result);
4675 }
4676
4677 case Builtin::BI__builtin_elementwise_maximumnum: {
4678 Value *Op0 = EmitScalarExpr(E->getArg(0));
4679 Value *Op1 = EmitScalarExpr(E->getArg(1));
4680 Value *Result = Builder.CreateBinaryIntrinsic(
4681 Intrinsic::maximumnum, Op0, Op1, nullptr, "elt.maximumnum");
4682 return RValue::get(Result);
4683 }
4684
4685 case Builtin::BI__builtin_elementwise_minimumnum: {
4686 Value *Op0 = EmitScalarExpr(E->getArg(0));
4687 Value *Op1 = EmitScalarExpr(E->getArg(1));
4688 Value *Result = Builder.CreateBinaryIntrinsic(
4689 Intrinsic::minimumnum, Op0, Op1, nullptr, "elt.minimumnum");
4690 return RValue::get(Result);
4691 }
4692
4693 case Builtin::BI__builtin_reduce_max: {
4694 auto GetIntrinsicID = [this](QualType QT) {
4695 if (auto *VecTy = QT->getAs<VectorType>())
4696 QT = VecTy->getElementType();
4697 else if (QT->isSizelessVectorType())
4698 QT = QT->getSizelessVectorEltType(CGM.getContext());
4699
4700 if (QT->isSignedIntegerType())
4701 return Intrinsic::vector_reduce_smax;
4702 if (QT->isUnsignedIntegerType())
4703 return Intrinsic::vector_reduce_umax;
4704 assert(QT->isFloatingType() && "must have a float here");
4705 return Intrinsic::vector_reduce_fmax;
4706 };
4708 *this, E, GetIntrinsicID(E->getArg(0)->getType()), "rdx.min"));
4709 }
4710
4711 case Builtin::BI__builtin_reduce_min: {
4712 auto GetIntrinsicID = [this](QualType QT) {
4713 if (auto *VecTy = QT->getAs<VectorType>())
4714 QT = VecTy->getElementType();
4715 else if (QT->isSizelessVectorType())
4716 QT = QT->getSizelessVectorEltType(CGM.getContext());
4717
4718 if (QT->isSignedIntegerType())
4719 return Intrinsic::vector_reduce_smin;
4720 if (QT->isUnsignedIntegerType())
4721 return Intrinsic::vector_reduce_umin;
4722 assert(QT->isFloatingType() && "must have a float here");
4723 return Intrinsic::vector_reduce_fmin;
4724 };
4725
4727 *this, E, GetIntrinsicID(E->getArg(0)->getType()), "rdx.min"));
4728 }
4729
4730 case Builtin::BI__builtin_reduce_add:
4732 *this, E, Intrinsic::vector_reduce_add, "rdx.add"));
4733 case Builtin::BI__builtin_reduce_mul:
4735 *this, E, Intrinsic::vector_reduce_mul, "rdx.mul"));
4736 case Builtin::BI__builtin_reduce_xor:
4738 *this, E, Intrinsic::vector_reduce_xor, "rdx.xor"));
4739 case Builtin::BI__builtin_reduce_or:
4741 *this, E, Intrinsic::vector_reduce_or, "rdx.or"));
4742 case Builtin::BI__builtin_reduce_and:
4744 *this, E, Intrinsic::vector_reduce_and, "rdx.and"));
4745 case Builtin::BI__builtin_reduce_maximum:
4747 *this, E, Intrinsic::vector_reduce_fmaximum, "rdx.maximum"));
4748 case Builtin::BI__builtin_reduce_minimum:
4750 *this, E, Intrinsic::vector_reduce_fminimum, "rdx.minimum"));
4751 case Builtin::BI__builtin_reduce_assoc_fadd:
4752 case Builtin::BI__builtin_reduce_in_order_fadd: {
4753 llvm::Value *Vector = EmitScalarExpr(E->getArg(0));
4754 llvm::Type *ScalarTy = Vector->getType()->getScalarType();
4755 llvm::Value *StartValue = nullptr;
4756 if (E->getNumArgs() == 2)
4757 StartValue = Builder.CreateFPCast(EmitScalarExpr(E->getArg(1)), ScalarTy);
4758 llvm::Value *Args[] = {/*start_value=*/StartValue
4759 ? StartValue
4760 : llvm::ConstantFP::get(ScalarTy, -0.0F),
4761 /*vector=*/Vector};
4762 llvm::Function *F =
4763 CGM.getIntrinsic(Intrinsic::vector_reduce_fadd, Vector->getType());
4764 llvm::CallBase *Reduce = Builder.CreateCall(F, Args, "rdx.addf");
4765 if (BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_reduce_assoc_fadd) {
4766 // `__builtin_reduce_assoc_fadd` is an associative reduction which
4767 // requires the reassoc FMF flag.
4768 llvm::FastMathFlags FMF;
4769 FMF.setAllowReassoc();
4770 cast<llvm::CallBase>(Reduce)->setFastMathFlags(FMF);
4771 }
4772 return RValue::get(Reduce);
4773 }
4774
4775 case Builtin::BI__builtin_matrix_transpose: {
4776 auto *MatrixTy = E->getArg(0)->getType()->castAs<ConstantMatrixType>();
4777 Value *MatValue = EmitScalarExpr(E->getArg(0));
4778 MatrixBuilder MB(Builder);
4779 Value *Result = MB.CreateMatrixTranspose(MatValue, MatrixTy->getNumRows(),
4780 MatrixTy->getNumColumns());
4781 return RValue::get(Result);
4782 }
4783
4784 case Builtin::BI__builtin_matrix_column_major_load: {
4785 MatrixBuilder MB(Builder);
4786 // Emit everything that isn't dependent on the first parameter type
4787 Value *Stride = EmitScalarExpr(E->getArg(3));
4788 const auto *ResultTy = E->getType()->getAs<ConstantMatrixType>();
4789 auto *PtrTy = E->getArg(0)->getType()->getAs<PointerType>();
4790 assert(PtrTy && "arg0 must be of pointer type");
4791 bool IsVolatile = PtrTy->getPointeeType().isVolatileQualified();
4792
4795 E->getArg(0)->getType(), E->getArg(0)->getExprLoc(), FD,
4796 0);
4797 Value *Result = MB.CreateColumnMajorLoad(
4798 Src.getElementType(), Src.emitRawPointer(*this),
4799 Align(Src.getAlignment().getQuantity()), Stride, IsVolatile,
4800 ResultTy->getNumRows(), ResultTy->getNumColumns(), "matrix");
4801 return RValue::get(Result);
4802 }
4803
4804 case Builtin::BI__builtin_matrix_column_major_store: {
4805 MatrixBuilder MB(Builder);
4806 Value *Matrix = EmitScalarExpr(E->getArg(0));
4808 Value *Stride = EmitScalarExpr(E->getArg(2));
4809
4810 const auto *MatrixTy = E->getArg(0)->getType()->getAs<ConstantMatrixType>();
4811 auto *PtrTy = E->getArg(1)->getType()->getAs<PointerType>();
4812 assert(PtrTy && "arg1 must be of pointer type");
4813 bool IsVolatile = PtrTy->getPointeeType().isVolatileQualified();
4814
4816 E->getArg(1)->getType(), E->getArg(1)->getExprLoc(), FD,
4817 0);
4818 Value *Result = MB.CreateColumnMajorStore(
4819 Matrix, Dst.emitRawPointer(*this),
4820 Align(Dst.getAlignment().getQuantity()), Stride, IsVolatile,
4821 MatrixTy->getNumRows(), MatrixTy->getNumColumns());
4823 return RValue::get(Result);
4824 }
4825
4826 case Builtin::BI__builtin_masked_load:
4827 case Builtin::BI__builtin_masked_expand_load: {
4828 llvm::Value *Mask = EmitScalarExpr(E->getArg(0));
4829 llvm::Value *Ptr = EmitScalarExpr(E->getArg(1));
4830
4831 llvm::Type *RetTy = CGM.getTypes().ConvertType(E->getType());
4832 llvm::Value *PassThru = llvm::PoisonValue::get(RetTy);
4833 if (E->getNumArgs() > 2)
4834 PassThru = EmitScalarExpr(E->getArg(2));
4835
4836 CharUnits Align = CGM.getNaturalTypeAlignment(
4837 E->getType()->getAs<VectorType>()->getElementType(), nullptr);
4838
4839 llvm::Value *Result;
4840 if (BuiltinID == Builtin::BI__builtin_masked_load) {
4841 Result = Builder.CreateMaskedLoad(RetTy, Ptr, Align.getAsAlign(), Mask,
4842 PassThru, "masked_load");
4843 } else {
4844 Function *F = CGM.getIntrinsic(Intrinsic::masked_expandload, {RetTy});
4845 Result =
4846 Builder.CreateCall(F, {Ptr, Mask, PassThru}, "masked_expand_load");
4847 }
4848 return RValue::get(Result);
4849 };
4850 case Builtin::BI__builtin_masked_gather: {
4851 llvm::Value *Mask = EmitScalarExpr(E->getArg(0));
4852 llvm::Value *Idx = EmitScalarExpr(E->getArg(1));
4853 llvm::Value *Ptr = EmitScalarExpr(E->getArg(2));
4854
4855 llvm::Type *RetTy = CGM.getTypes().ConvertType(E->getType());
4856 CharUnits Align = CGM.getNaturalTypeAlignment(
4857 E->getType()->getAs<VectorType>()->getElementType(), nullptr);
4858
4859 llvm::Value *PassThru = llvm::PoisonValue::get(RetTy);
4860 if (E->getNumArgs() > 3)
4861 PassThru = EmitScalarExpr(E->getArg(3));
4862
4863 llvm::Type *ElemTy = CGM.getTypes().ConvertType(
4865 llvm::Value *PtrVec = Builder.CreateGEP(ElemTy, Ptr, Idx);
4866
4867 llvm::Value *Result = Builder.CreateMaskedGather(
4868 RetTy, PtrVec, Align.getAsAlign(), Mask, PassThru, "masked_gather");
4869 return RValue::get(Result);
4870 }
4871 case Builtin::BI__builtin_masked_store:
4872 case Builtin::BI__builtin_masked_compress_store: {
4873 llvm::Value *Mask = EmitScalarExpr(E->getArg(0));
4874 llvm::Value *Val = EmitScalarExpr(E->getArg(1));
4875 llvm::Value *Ptr = EmitScalarExpr(E->getArg(2));
4876
4877 QualType ValTy = E->getArg(1)->getType();
4878 llvm::Type *ValLLTy = CGM.getTypes().ConvertType(ValTy);
4879
4880 CharUnits Align = CGM.getNaturalTypeAlignment(
4882 nullptr);
4883
4884 if (BuiltinID == Builtin::BI__builtin_masked_store) {
4885 Builder.CreateMaskedStore(Val, Ptr, Align.getAsAlign(), Mask);
4886 } else {
4887 llvm::Function *F =
4888 CGM.getIntrinsic(llvm::Intrinsic::masked_compressstore, {ValLLTy});
4889 Builder.CreateCall(F, {Val, Ptr, Mask});
4890 }
4891 return RValue::get(nullptr);
4892 }
4893 case Builtin::BI__builtin_masked_scatter: {
4894 llvm::Value *Mask = EmitScalarExpr(E->getArg(0));
4895 llvm::Value *Idx = EmitScalarExpr(E->getArg(1));
4896 llvm::Value *Val = EmitScalarExpr(E->getArg(2));
4897 llvm::Value *Ptr = EmitScalarExpr(E->getArg(3));
4898
4899 CharUnits Align = CGM.getNaturalTypeAlignment(
4901 nullptr);
4902
4903 llvm::Type *ElemTy = CGM.getTypes().ConvertType(
4904 E->getArg(1)->getType()->getAs<VectorType>()->getElementType());
4905 llvm::Value *PtrVec = Builder.CreateGEP(ElemTy, Ptr, Idx);
4906
4907 Builder.CreateMaskedScatter(Val, PtrVec, Align.getAsAlign(), Mask);
4908 return RValue();
4909 }
4910 case Builtin::BI__builtin_isinf_sign: {
4911 // isinf_sign(x) -> fabs(x) == infinity ? (signbit(x) ? -1 : 1) : 0
4912 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4913 // FIXME: for strictfp/IEEE-754 we need to not trap on SNaN here.
4914 Value *Arg = EmitScalarExpr(E->getArg(0));
4915 Value *AbsArg = EmitFAbs(*this, Arg);
4916 Value *IsInf = Builder.CreateFCmpOEQ(
4917 AbsArg, ConstantFP::getInfinity(Arg->getType()), "isinf");
4918 Value *IsNeg = EmitSignBit(*this, Arg);
4919
4920 llvm::Type *IntTy = ConvertType(E->getType());
4921 Value *Zero = Constant::getNullValue(IntTy);
4922 Value *One = ConstantInt::get(IntTy, 1);
4923 Value *NegativeOne = ConstantInt::getAllOnesValue(IntTy);
4924 Value *SignResult = Builder.CreateSelect(IsNeg, NegativeOne, One);
4925 Value *Result = Builder.CreateSelect(IsInf, SignResult, Zero);
4926 return RValue::get(Result);
4927 }
4928
4929 case Builtin::BI__builtin_flt_rounds: {
4930 Function *F = CGM.getIntrinsic(Intrinsic::get_rounding);
4931
4932 llvm::Type *ResultType = ConvertType(E->getType());
4933 Value *Result = Builder.CreateCall(F);
4934 if (Result->getType() != ResultType)
4935 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
4936 "cast");
4937 return RValue::get(Result);
4938 }
4939
4940 case Builtin::BI__builtin_set_flt_rounds: {
4941 Function *F = CGM.getIntrinsic(Intrinsic::set_rounding);
4942
4943 Value *V = EmitScalarExpr(E->getArg(0));
4944 Builder.CreateCall(F, V);
4945 return RValue::get(nullptr);
4946 }
4947
4948 case Builtin::BI__builtin_fpclassify: {
4949 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
4950 // FIXME: for strictfp/IEEE-754 we need to not trap on SNaN here.
4951 Value *V = EmitScalarExpr(E->getArg(5));
4952 llvm::Type *Ty = ConvertType(E->getArg(5)->getType());
4953
4954 // Create Result
4955 BasicBlock *Begin = Builder.GetInsertBlock();
4956 BasicBlock *End = createBasicBlock("fpclassify_end", this->CurFn);
4957 Builder.SetInsertPoint(End);
4958 PHINode *Result =
4959 Builder.CreatePHI(ConvertType(E->getArg(0)->getType()), 4,
4960 "fpclassify_result");
4961
4962 // if (V==0) return FP_ZERO
4963 Builder.SetInsertPoint(Begin);
4964 Value *IsZero = Builder.CreateFCmpOEQ(V, Constant::getNullValue(Ty),
4965 "iszero");
4966 Value *ZeroLiteral = EmitScalarExpr(E->getArg(4));
4967 BasicBlock *NotZero = createBasicBlock("fpclassify_not_zero", this->CurFn);
4968 Builder.CreateCondBr(IsZero, End, NotZero);
4969 Result->addIncoming(ZeroLiteral, Begin);
4970
4971 // if (V != V) return FP_NAN
4972 Builder.SetInsertPoint(NotZero);
4973 Value *IsNan = Builder.CreateFCmpUNO(V, V, "cmp");
4974 Value *NanLiteral = EmitScalarExpr(E->getArg(0));
4975 BasicBlock *NotNan = createBasicBlock("fpclassify_not_nan", this->CurFn);
4976 Builder.CreateCondBr(IsNan, End, NotNan);
4977 Result->addIncoming(NanLiteral, NotZero);
4978
4979 // if (fabs(V) == infinity) return FP_INFINITY
4980 Builder.SetInsertPoint(NotNan);
4981 Value *VAbs = EmitFAbs(*this, V);
4982 Value *IsInf =
4983 Builder.CreateFCmpOEQ(VAbs, ConstantFP::getInfinity(V->getType()),
4984 "isinf");
4985 Value *InfLiteral = EmitScalarExpr(E->getArg(1));
4986 BasicBlock *NotInf = createBasicBlock("fpclassify_not_inf", this->CurFn);
4987 Builder.CreateCondBr(IsInf, End, NotInf);
4988 Result->addIncoming(InfLiteral, NotNan);
4989
4990 // if (fabs(V) >= MIN_NORMAL) return FP_NORMAL else FP_SUBNORMAL
4991 Builder.SetInsertPoint(NotInf);
4992 APFloat Smallest = APFloat::getSmallestNormalized(
4993 getContext().getFloatTypeSemantics(E->getArg(5)->getType()));
4994 Value *IsNormal =
4995 Builder.CreateFCmpUGE(VAbs, ConstantFP::get(V->getContext(), Smallest),
4996 "isnormal");
4997 Value *NormalResult =
4998 Builder.CreateSelect(IsNormal, EmitScalarExpr(E->getArg(2)),
4999 EmitScalarExpr(E->getArg(3)));
5000 Builder.CreateBr(End);
5001 Result->addIncoming(NormalResult, NotInf);
5002
5003 // return Result
5004 Builder.SetInsertPoint(End);
5005 return RValue::get(Result);
5006 }
5007
5008 // An alloca will always return a pointer to the alloca (stack) address
5009 // space. This address space need not be the same as the AST / Language
5010 // default (e.g. in C / C++ auto vars are in the generic address space). At
5011 // the AST level this is handled within CreateTempAlloca et al., but for the
5012 // builtin / dynamic alloca we have to handle it here. We use an explicit cast
5013 // instead of passing an AS to CreateAlloca so as to not inhibit optimisation.
5014 case Builtin::BIalloca:
5015 case Builtin::BI_alloca:
5016 case Builtin::BI__builtin_alloca_uninitialized:
5017 case Builtin::BI__builtin_alloca: {
5018 Value *Size = EmitScalarExpr(E->getArg(0));
5019 const TargetInfo &TI = getContext().getTargetInfo();
5020 // The alignment of the alloca should correspond to __BIGGEST_ALIGNMENT__.
5021 const Align SuitableAlignmentInBytes =
5022 CGM.getContext()
5023 .toCharUnitsFromBits(TI.getSuitableAlign())
5024 .getAsAlign();
5025 AllocaInst *AI = Builder.CreateAlloca(Builder.getInt8Ty(), Size);
5026 AI->setAlignment(SuitableAlignmentInBytes);
5027 if (BuiltinID != Builtin::BI__builtin_alloca_uninitialized)
5028 initializeAlloca(*this, AI, Size, SuitableAlignmentInBytes);
5029 if (AI->getAddressSpace() !=
5030 CGM.getContext().getTargetAddressSpace(
5032 llvm::Type *Ty = CGM.getTypes().ConvertType(E->getType());
5033 return RValue::get(performAddrSpaceCast(AI, Ty));
5034 }
5035 return RValue::get(AI);
5036 }
5037
5038 case Builtin::BI__builtin_alloca_with_align_uninitialized:
5039 case Builtin::BI__builtin_alloca_with_align: {
5040 Value *Size = EmitScalarExpr(E->getArg(0));
5041 Value *AlignmentInBitsValue = EmitScalarExpr(E->getArg(1));
5042 auto *AlignmentInBitsCI = cast<ConstantInt>(AlignmentInBitsValue);
5043 unsigned AlignmentInBits = AlignmentInBitsCI->getZExtValue();
5044 const Align AlignmentInBytes =
5045 CGM.getContext().toCharUnitsFromBits(AlignmentInBits).getAsAlign();
5046 AllocaInst *AI = Builder.CreateAlloca(Builder.getInt8Ty(), Size);
5047 AI->setAlignment(AlignmentInBytes);
5048 if (BuiltinID != Builtin::BI__builtin_alloca_with_align_uninitialized)
5049 initializeAlloca(*this, AI, Size, AlignmentInBytes);
5050 if (AI->getAddressSpace() !=
5051 CGM.getContext().getTargetAddressSpace(
5053 llvm::Type *Ty = CGM.getTypes().ConvertType(E->getType());
5054 return RValue::get(performAddrSpaceCast(AI, Ty));
5055 }
5056 return RValue::get(AI);
5057 }
5058
5059 case Builtin::BI__builtin_infer_alloc_token: {
5060 llvm::MDNode *MDN = buildAllocToken(E);
5061 llvm::Value *MDV = MetadataAsValue::get(getLLVMContext(), MDN);
5062 llvm::Function *F =
5063 CGM.getIntrinsic(llvm::Intrinsic::alloc_token_id, {IntPtrTy});
5064 llvm::CallBase *TokenID = Builder.CreateCall(F, MDV);
5065 return RValue::get(TokenID);
5066 }
5067
5068 case Builtin::BIbzero:
5069 case Builtin::BI__builtin_bzero: {
5071 Value *SizeVal = EmitScalarExpr(E->getArg(1));
5072 EmitNonNullArgCheck(Dest, E->getArg(0)->getType(),
5073 E->getArg(0)->getExprLoc(), FD, 0);
5074 auto *I = Builder.CreateMemSet(Dest, Builder.getInt8(0), SizeVal, false);
5075 addInstToNewSourceAtom(I, nullptr);
5076 return RValue::get(nullptr);
5077 }
5078
5079 case Builtin::BIbcopy:
5080 case Builtin::BI__builtin_bcopy: {
5083 Value *SizeVal = EmitScalarExpr(E->getArg(2));
5085 E->getArg(0)->getType(), E->getArg(0)->getExprLoc(), FD,
5086 0);
5088 E->getArg(1)->getType(), E->getArg(1)->getExprLoc(), FD,
5089 0);
5090 auto *I = Builder.CreateMemMove(Dest, Src, SizeVal, false);
5091 addInstToNewSourceAtom(I, nullptr);
5092 return RValue::get(nullptr);
5093 }
5094
5095 case Builtin::BImemcpy:
5096 case Builtin::BI__builtin_memcpy:
5097 case Builtin::BImempcpy:
5098 case Builtin::BI__builtin_mempcpy: {
5101 Value *SizeVal = EmitScalarExpr(E->getArg(2));
5102 EmitArgCheck(TCK_Store, Dest, E->getArg(0), 0);
5103 EmitArgCheck(TCK_Load, Src, E->getArg(1), 1);
5104 auto *I = Builder.CreateMemCpy(Dest, Src, SizeVal, false);
5105 addInstToNewSourceAtom(I, nullptr);
5106 if (BuiltinID == Builtin::BImempcpy ||
5107 BuiltinID == Builtin::BI__builtin_mempcpy)
5108 return RValue::get(Builder.CreateInBoundsGEP(
5109 Dest.getElementType(), Dest.emitRawPointer(*this), SizeVal));
5110 else
5111 return RValue::get(Dest, *this);
5112 }
5113
5114 case Builtin::BI__builtin_memcpy_inline: {
5117 uint64_t Size =
5118 E->getArg(2)->EvaluateKnownConstInt(getContext()).getZExtValue();
5119 EmitArgCheck(TCK_Store, Dest, E->getArg(0), 0);
5120 EmitArgCheck(TCK_Load, Src, E->getArg(1), 1);
5121 auto *I = Builder.CreateMemCpyInline(Dest, Src, Size);
5122 addInstToNewSourceAtom(I, nullptr);
5123 return RValue::get(nullptr);
5124 }
5125
5126 case Builtin::BI__builtin_char_memchr:
5127 BuiltinID = Builtin::BI__builtin_memchr;
5128 break;
5129
5130 case Builtin::BI__builtin___memcpy_chk: {
5131 // fold __builtin_memcpy_chk(x, y, cst1, cst2) to memcpy iff cst1<=cst2.
5132 Expr::EvalResult SizeResult, DstSizeResult;
5133 if (!E->getArg(2)->EvaluateAsInt(SizeResult, CGM.getContext()) ||
5134 !E->getArg(3)->EvaluateAsInt(DstSizeResult, CGM.getContext()))
5135 break;
5136 llvm::APSInt Size = SizeResult.Val.getInt();
5137 llvm::APSInt DstSize = DstSizeResult.Val.getInt();
5138 if (Size.ugt(DstSize))
5139 break;
5142 Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
5143 auto *I = Builder.CreateMemCpy(Dest, Src, SizeVal, false);
5144 addInstToNewSourceAtom(I, nullptr);
5145 return RValue::get(Dest, *this);
5146 }
5147
5148 case Builtin::BI__builtin_objc_memmove_collectable: {
5149 Address DestAddr = EmitPointerWithAlignment(E->getArg(0));
5150 Address SrcAddr = EmitPointerWithAlignment(E->getArg(1));
5151 Value *SizeVal = EmitScalarExpr(E->getArg(2));
5152 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this,
5153 DestAddr, SrcAddr, SizeVal);
5154 return RValue::get(DestAddr, *this);
5155 }
5156
5157 case Builtin::BI__builtin___memmove_chk: {
5158 // fold __builtin_memmove_chk(x, y, cst1, cst2) to memmove iff cst1<=cst2.
5159 Expr::EvalResult SizeResult, DstSizeResult;
5160 if (!E->getArg(2)->EvaluateAsInt(SizeResult, CGM.getContext()) ||
5161 !E->getArg(3)->EvaluateAsInt(DstSizeResult, CGM.getContext()))
5162 break;
5163 llvm::APSInt Size = SizeResult.Val.getInt();
5164 llvm::APSInt DstSize = DstSizeResult.Val.getInt();
5165 if (Size.ugt(DstSize))
5166 break;
5169 Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
5170 auto *I = Builder.CreateMemMove(Dest, Src, SizeVal, false);
5171 addInstToNewSourceAtom(I, nullptr);
5172 return RValue::get(Dest, *this);
5173 }
5174
5175 case Builtin::BI__builtin_trivially_relocate:
5176 case Builtin::BImemmove:
5177 case Builtin::BI__builtin_memmove: {
5180 Value *SizeVal = EmitScalarExpr(E->getArg(2));
5181 if (BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_trivially_relocate)
5182 SizeVal = Builder.CreateMul(
5183 SizeVal,
5184 ConstantInt::get(
5185 SizeVal->getType(),
5186 getContext()
5187 .getTypeSizeInChars(E->getArg(0)->getType()->getPointeeType())
5188 .getQuantity()));
5189 EmitArgCheck(TCK_Store, Dest, E->getArg(0), 0);
5190 EmitArgCheck(TCK_Load, Src, E->getArg(1), 1);
5191 auto *I = Builder.CreateMemMove(Dest, Src, SizeVal, false);
5192 addInstToNewSourceAtom(I, nullptr);
5193 return RValue::get(Dest, *this);
5194 }
5195 case Builtin::BImemset:
5196 case Builtin::BI__builtin_memset: {
5198 Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
5199 Builder.getInt8Ty());
5200 Value *SizeVal = EmitScalarExpr(E->getArg(2));
5201 EmitNonNullArgCheck(Dest, E->getArg(0)->getType(),
5202 E->getArg(0)->getExprLoc(), FD, 0);
5203 auto *I = Builder.CreateMemSet(Dest, ByteVal, SizeVal, false);
5204 addInstToNewSourceAtom(I, ByteVal);
5205 return RValue::get(Dest, *this);
5206 }
5207 case Builtin::BI__builtin_memset_inline: {
5209 Value *ByteVal =
5210 Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), Builder.getInt8Ty());
5211 uint64_t Size =
5212 E->getArg(2)->EvaluateKnownConstInt(getContext()).getZExtValue();
5214 E->getArg(0)->getType(), E->getArg(0)->getExprLoc(), FD,
5215 0);
5216 auto *I = Builder.CreateMemSetInline(Dest, ByteVal, Size);
5217 addInstToNewSourceAtom(I, nullptr);
5218 return RValue::get(nullptr);
5219 }
5220 case Builtin::BI__builtin___memset_chk: {
5221 // fold __builtin_memset_chk(x, y, cst1, cst2) to memset iff cst1<=cst2.
5222 Expr::EvalResult SizeResult, DstSizeResult;
5223 if (!E->getArg(2)->EvaluateAsInt(SizeResult, CGM.getContext()) ||
5224 !E->getArg(3)->EvaluateAsInt(DstSizeResult, CGM.getContext()))
5225 break;
5226 llvm::APSInt Size = SizeResult.Val.getInt();
5227 llvm::APSInt DstSize = DstSizeResult.Val.getInt();
5228 if (Size.ugt(DstSize))
5229 break;
5231 Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
5232 Builder.getInt8Ty());
5233 Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
5234 auto *I = Builder.CreateMemSet(Dest, ByteVal, SizeVal, false);
5235 addInstToNewSourceAtom(I, nullptr);
5236 return RValue::get(Dest, *this);
5237 }
5238 case Builtin::BI__builtin_wmemchr: {
5239 // The MSVC runtime library does not provide a definition of wmemchr, so we
5240 // need an inline implementation.
5241 if (!getTarget().getTriple().isOSMSVCRT())
5242 break;
5243
5244 llvm::Type *WCharTy = ConvertType(getContext().WCharTy);
5245 Value *Str = EmitScalarExpr(E->getArg(0));
5246 Value *Chr = EmitScalarExpr(E->getArg(1));
5247 Value *Size = EmitScalarExpr(E->getArg(2));
5248
5249 BasicBlock *Entry = Builder.GetInsertBlock();
5250 BasicBlock *CmpEq = createBasicBlock("wmemchr.eq");
5251 BasicBlock *Next = createBasicBlock("wmemchr.next");
5252 BasicBlock *Exit = createBasicBlock("wmemchr.exit");
5253 Value *SizeEq0 = Builder.CreateICmpEQ(Size, ConstantInt::get(SizeTy, 0));
5254 Builder.CreateCondBr(SizeEq0, Exit, CmpEq);
5255
5256 EmitBlock(CmpEq);
5257 PHINode *StrPhi = Builder.CreatePHI(Str->getType(), 2);
5258 StrPhi->addIncoming(Str, Entry);
5259 PHINode *SizePhi = Builder.CreatePHI(SizeTy, 2);
5260 SizePhi->addIncoming(Size, Entry);
5261 CharUnits WCharAlign =
5263 Value *StrCh = Builder.CreateAlignedLoad(WCharTy, StrPhi, WCharAlign);
5264 Value *FoundChr = Builder.CreateConstInBoundsGEP1_32(WCharTy, StrPhi, 0);
5265 Value *StrEqChr = Builder.CreateICmpEQ(StrCh, Chr);
5266 Builder.CreateCondBr(StrEqChr, Exit, Next);
5267
5268 EmitBlock(Next);
5269 Value *NextStr = Builder.CreateConstInBoundsGEP1_32(WCharTy, StrPhi, 1);
5270 Value *NextSize = Builder.CreateSub(SizePhi, ConstantInt::get(SizeTy, 1));
5271 Value *NextSizeEq0 =
5272 Builder.CreateICmpEQ(NextSize, ConstantInt::get(SizeTy, 0));
5273 Builder.CreateCondBr(NextSizeEq0, Exit, CmpEq);
5274 StrPhi->addIncoming(NextStr, Next);
5275 SizePhi->addIncoming(NextSize, Next);
5276
5277 EmitBlock(Exit);
5278 PHINode *Ret = Builder.CreatePHI(Str->getType(), 3);
5279 Ret->addIncoming(llvm::Constant::getNullValue(Str->getType()), Entry);
5280 Ret->addIncoming(llvm::Constant::getNullValue(Str->getType()), Next);
5281 Ret->addIncoming(FoundChr, CmpEq);
5282 return RValue::get(Ret);
5283 }
5284 case Builtin::BI__builtin_wmemcmp: {
5285 // The MSVC runtime library does not provide a definition of wmemcmp, so we
5286 // need an inline implementation.
5287 if (!getTarget().getTriple().isOSMSVCRT())
5288 break;
5289
5290 llvm::Type *WCharTy = ConvertType(getContext().WCharTy);
5291
5292 Value *Dst = EmitScalarExpr(E->getArg(0));
5293 Value *Src = EmitScalarExpr(E->getArg(1));
5294 Value *Size = EmitScalarExpr(E->getArg(2));
5295
5296 BasicBlock *Entry = Builder.GetInsertBlock();
5297 BasicBlock *CmpGT = createBasicBlock("wmemcmp.gt");
5298 BasicBlock *CmpLT = createBasicBlock("wmemcmp.lt");
5299 BasicBlock *Next = createBasicBlock("wmemcmp.next");
5300 BasicBlock *Exit = createBasicBlock("wmemcmp.exit");
5301 Value *SizeEq0 = Builder.CreateICmpEQ(Size, ConstantInt::get(SizeTy, 0));
5302 Builder.CreateCondBr(SizeEq0, Exit, CmpGT);
5303
5304 EmitBlock(CmpGT);
5305 PHINode *DstPhi = Builder.CreatePHI(Dst->getType(), 2);
5306 DstPhi->addIncoming(Dst, Entry);
5307 PHINode *SrcPhi = Builder.CreatePHI(Src->getType(), 2);
5308 SrcPhi->addIncoming(Src, Entry);
5309 PHINode *SizePhi = Builder.CreatePHI(SizeTy, 2);
5310 SizePhi->addIncoming(Size, Entry);
5311 CharUnits WCharAlign =
5313 Value *DstCh = Builder.CreateAlignedLoad(WCharTy, DstPhi, WCharAlign);
5314 Value *SrcCh = Builder.CreateAlignedLoad(WCharTy, SrcPhi, WCharAlign);
5315 Value *DstGtSrc = Builder.CreateICmpUGT(DstCh, SrcCh);
5316 Builder.CreateCondBr(DstGtSrc, Exit, CmpLT);
5317
5318 EmitBlock(CmpLT);
5319 Value *DstLtSrc = Builder.CreateICmpULT(DstCh, SrcCh);
5320 Builder.CreateCondBr(DstLtSrc, Exit, Next);
5321
5322 EmitBlock(Next);
5323 Value *NextDst = Builder.CreateConstInBoundsGEP1_32(WCharTy, DstPhi, 1);
5324 Value *NextSrc = Builder.CreateConstInBoundsGEP1_32(WCharTy, SrcPhi, 1);
5325 Value *NextSize = Builder.CreateSub(SizePhi, ConstantInt::get(SizeTy, 1));
5326 Value *NextSizeEq0 =
5327 Builder.CreateICmpEQ(NextSize, ConstantInt::get(SizeTy, 0));
5328 Builder.CreateCondBr(NextSizeEq0, Exit, CmpGT);
5329 DstPhi->addIncoming(NextDst, Next);
5330 SrcPhi->addIncoming(NextSrc, Next);
5331 SizePhi->addIncoming(NextSize, Next);
5332
5333 EmitBlock(Exit);
5334 PHINode *Ret = Builder.CreatePHI(IntTy, 4);
5335 Ret->addIncoming(ConstantInt::get(IntTy, 0), Entry);
5336 Ret->addIncoming(ConstantInt::get(IntTy, 1), CmpGT);
5337 Ret->addIncoming(ConstantInt::getAllOnesValue(IntTy), CmpLT);
5338 Ret->addIncoming(ConstantInt::get(IntTy, 0), Next);
5339 return RValue::get(Ret);
5340 }
5341 case Builtin::BI__builtin_dwarf_cfa: {
5342 // The offset in bytes from the first argument to the CFA.
5343 //
5344 // Why on earth is this in the frontend? Is there any reason at
5345 // all that the backend can't reasonably determine this while
5346 // lowering llvm.eh.dwarf.cfa()?
5347 //
5348 // TODO: If there's a satisfactory reason, add a target hook for
5349 // this instead of hard-coding 0, which is correct for most targets.
5350 int32_t Offset = 0;
5351
5352 Function *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa);
5353 return RValue::get(Builder.CreateCall(F,
5354 llvm::ConstantInt::get(Int32Ty, Offset)));
5355 }
5356 case Builtin::BI__builtin_return_address: {
5357 Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
5358 getContext().UnsignedIntTy);
5359 Function *F =
5360 CGM.getIntrinsic(Intrinsic::returnaddress, {CGM.ProgramPtrTy});
5361 return RValue::get(Builder.CreateCall(F, Depth));
5362 }
5363 case Builtin::BI_ReturnAddress: {
5364 Function *F =
5365 CGM.getIntrinsic(Intrinsic::returnaddress, {CGM.ProgramPtrTy});
5366 return RValue::get(Builder.CreateCall(F, Builder.getInt32(0)));
5367 }
5368 case Builtin::BI__builtin_frame_address: {
5369 Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
5370 getContext().UnsignedIntTy);
5371 Function *F = CGM.getIntrinsic(Intrinsic::frameaddress, AllocaInt8PtrTy);
5372 return RValue::get(Builder.CreateCall(F, Depth));
5373 }
5374 case Builtin::BI__builtin_stack_address: {
5375 return RValue::get(Builder.CreateCall(
5376 CGM.getIntrinsic(Intrinsic::stackaddress, AllocaInt8PtrTy)));
5377 }
5378 case Builtin::BI__builtin_extract_return_addr: {
5381 return RValue::get(Result);
5382 }
5383 case Builtin::BI__builtin_frob_return_addr: {
5386 return RValue::get(Result);
5387 }
5388 case Builtin::BI__builtin_dwarf_sp_column: {
5389 llvm::IntegerType *Ty
5392 if (Column == -1) {
5393 CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column");
5394 return RValue::get(llvm::UndefValue::get(Ty));
5395 }
5396 return RValue::get(llvm::ConstantInt::get(Ty, Column, true));
5397 }
5398 case Builtin::BI__builtin_init_dwarf_reg_size_table: {
5400 if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address))
5401 CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table");
5402 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
5403 }
5404 case Builtin::BI__builtin_eh_return: {
5405 Value *Int = EmitScalarExpr(E->getArg(0));
5406 Value *Ptr = EmitScalarExpr(E->getArg(1));
5407
5408 llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType());
5409 assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) &&
5410 "LLVM's __builtin_eh_return only supports 32- and 64-bit variants");
5411 Function *F =
5412 CGM.getIntrinsic(IntTy->getBitWidth() == 32 ? Intrinsic::eh_return_i32
5413 : Intrinsic::eh_return_i64);
5414 Builder.CreateCall(F, {Int, Ptr});
5415 Builder.CreateUnreachable();
5416
5417 // We do need to preserve an insertion point.
5418 EmitBlock(createBasicBlock("builtin_eh_return.cont"));
5419
5420 return RValue::get(nullptr);
5421 }
5422 case Builtin::BI__builtin_unwind_init: {
5423 Function *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init);
5424 Builder.CreateCall(F);
5425 return RValue::get(nullptr);
5426 }
5427 case Builtin::BI__builtin_extend_pointer: {
5428 // Extends a pointer to the size of an _Unwind_Word, which is
5429 // uint64_t on all platforms. Generally this gets poked into a
5430 // register and eventually used as an address, so if the
5431 // addressing registers are wider than pointers and the platform
5432 // doesn't implicitly ignore high-order bits when doing
5433 // addressing, we need to make sure we zext / sext based on
5434 // the platform's expectations.
5435 //
5436 // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html
5437
5438 // Cast the pointer to intptr_t.
5439 Value *Ptr = EmitScalarExpr(E->getArg(0));
5440 Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast");
5441
5442 // If that's 64 bits, we're done.
5443 if (IntPtrTy->getBitWidth() == 64)
5444 return RValue::get(Result);
5445
5446 // Otherwise, ask the codegen data what to do.
5447 if (getTargetHooks().extendPointerWithSExt())
5448 return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext"));
5449 else
5450 return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext"));
5451 }
5452 case Builtin::BI__builtin_setjmp: {
5453 // Buffer is a void**.
5455
5456 if (getTarget().getTriple().getArch() == llvm::Triple::systemz) {
5457 // On this target, the back end fills in the context buffer completely.
5458 // It doesn't really matter if the frontend stores to the buffer before
5459 // calling setjmp, the back-end is going to overwrite them anyway.
5460 Function *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp);
5461 return RValue::get(Builder.CreateCall(F, Buf.emitRawPointer(*this)));
5462 }
5463
5464 // Store the frame pointer to the setjmp buffer.
5465 Value *FrameAddr = Builder.CreateCall(
5466 CGM.getIntrinsic(Intrinsic::frameaddress, AllocaInt8PtrTy),
5467 ConstantInt::get(Int32Ty, 0));
5468 Builder.CreateStore(FrameAddr, Buf);
5469
5470 // Store the stack pointer to the setjmp buffer.
5471 Value *StackAddr = Builder.CreateStackSave();
5472 assert(Buf.emitRawPointer(*this)->getType() == StackAddr->getType());
5473
5474 Address StackSaveSlot = Builder.CreateConstInBoundsGEP(Buf, 2);
5475 Builder.CreateStore(StackAddr, StackSaveSlot);
5476
5477 // Call LLVM's EH setjmp, which is lightweight.
5478 Function *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp);
5479 return RValue::get(Builder.CreateCall(F, Buf.emitRawPointer(*this)));
5480 }
5481 case Builtin::BI__builtin_longjmp: {
5482 Value *Buf = EmitScalarExpr(E->getArg(0));
5483
5484 // Call LLVM's EH longjmp, which is lightweight.
5485 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp), Buf);
5486
5487 // longjmp doesn't return; mark this as unreachable.
5488 Builder.CreateUnreachable();
5489
5490 // We do need to preserve an insertion point.
5491 EmitBlock(createBasicBlock("longjmp.cont"));
5492
5493 return RValue::get(nullptr);
5494 }
5495 case Builtin::BI__builtin_launder: {
5496 const Expr *Arg = E->getArg(0);
5497 QualType ArgTy = Arg->getType()->getPointeeType();
5498 Value *Ptr = EmitScalarExpr(Arg);
5499 if (TypeRequiresBuiltinLaunder(CGM, ArgTy))
5500 Ptr = Builder.CreateLaunderInvariantGroup(Ptr);
5501
5502 return RValue::get(Ptr);
5503 }
5504 case Builtin::BI__builtin_clear_padding: {
5506 auto PointeeTy = E->getArg(0)->getType()->getPointeeType();
5507 PaddingClearer clearer{*this};
5508 clearer.run(Src, PointeeTy);
5509 return RValue::get(nullptr);
5510 }
5511 case Builtin::BI__sync_fetch_and_add:
5512 case Builtin::BI__sync_fetch_and_sub:
5513 case Builtin::BI__sync_fetch_and_or:
5514 case Builtin::BI__sync_fetch_and_and:
5515 case Builtin::BI__sync_fetch_and_xor:
5516 case Builtin::BI__sync_fetch_and_nand:
5517 case Builtin::BI__sync_add_and_fetch:
5518 case Builtin::BI__sync_sub_and_fetch:
5519 case Builtin::BI__sync_and_and_fetch:
5520 case Builtin::BI__sync_or_and_fetch:
5521 case Builtin::BI__sync_xor_and_fetch:
5522 case Builtin::BI__sync_nand_and_fetch:
5523 case Builtin::BI__sync_val_compare_and_swap:
5524 case Builtin::BI__sync_bool_compare_and_swap:
5525 case Builtin::BI__sync_lock_test_and_set:
5526 case Builtin::BI__sync_lock_release:
5527 case Builtin::BI__sync_swap:
5528 llvm_unreachable("Shouldn't make it through sema");
5529 case Builtin::BI__sync_fetch_and_add_1:
5530 case Builtin::BI__sync_fetch_and_add_2:
5531 case Builtin::BI__sync_fetch_and_add_4:
5532 case Builtin::BI__sync_fetch_and_add_8:
5533 case Builtin::BI__sync_fetch_and_add_16:
5534 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Add, E);
5535 case Builtin::BI__sync_fetch_and_sub_1:
5536 case Builtin::BI__sync_fetch_and_sub_2:
5537 case Builtin::BI__sync_fetch_and_sub_4:
5538 case Builtin::BI__sync_fetch_and_sub_8:
5539 case Builtin::BI__sync_fetch_and_sub_16:
5540 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Sub, E);
5541 case Builtin::BI__sync_fetch_and_or_1:
5542 case Builtin::BI__sync_fetch_and_or_2:
5543 case Builtin::BI__sync_fetch_and_or_4:
5544 case Builtin::BI__sync_fetch_and_or_8:
5545 case Builtin::BI__sync_fetch_and_or_16:
5546 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Or, E);
5547 case Builtin::BI__sync_fetch_and_and_1:
5548 case Builtin::BI__sync_fetch_and_and_2:
5549 case Builtin::BI__sync_fetch_and_and_4:
5550 case Builtin::BI__sync_fetch_and_and_8:
5551 case Builtin::BI__sync_fetch_and_and_16:
5552 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::And, E);
5553 case Builtin::BI__sync_fetch_and_xor_1:
5554 case Builtin::BI__sync_fetch_and_xor_2:
5555 case Builtin::BI__sync_fetch_and_xor_4:
5556 case Builtin::BI__sync_fetch_and_xor_8:
5557 case Builtin::BI__sync_fetch_and_xor_16:
5558 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xor, E);
5559 case Builtin::BI__sync_fetch_and_nand_1:
5560 case Builtin::BI__sync_fetch_and_nand_2:
5561 case Builtin::BI__sync_fetch_and_nand_4:
5562 case Builtin::BI__sync_fetch_and_nand_8:
5563 case Builtin::BI__sync_fetch_and_nand_16:
5564 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Nand, E);
5565
5566 // Clang extensions: not overloaded yet.
5567 case Builtin::BI__sync_fetch_and_min:
5568 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Min, E);
5569 case Builtin::BI__sync_fetch_and_max:
5570 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Max, E);
5571 case Builtin::BI__sync_fetch_and_umin:
5572 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::UMin, E);
5573 case Builtin::BI__sync_fetch_and_umax:
5574 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::UMax, E);
5575
5576 case Builtin::BI__sync_add_and_fetch_1:
5577 case Builtin::BI__sync_add_and_fetch_2:
5578 case Builtin::BI__sync_add_and_fetch_4:
5579 case Builtin::BI__sync_add_and_fetch_8:
5580 case Builtin::BI__sync_add_and_fetch_16:
5581 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Add, E,
5582 llvm::Instruction::Add);
5583 case Builtin::BI__sync_sub_and_fetch_1:
5584 case Builtin::BI__sync_sub_and_fetch_2:
5585 case Builtin::BI__sync_sub_and_fetch_4:
5586 case Builtin::BI__sync_sub_and_fetch_8:
5587 case Builtin::BI__sync_sub_and_fetch_16:
5588 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Sub, E,
5589 llvm::Instruction::Sub);
5590 case Builtin::BI__sync_and_and_fetch_1:
5591 case Builtin::BI__sync_and_and_fetch_2:
5592 case Builtin::BI__sync_and_and_fetch_4:
5593 case Builtin::BI__sync_and_and_fetch_8:
5594 case Builtin::BI__sync_and_and_fetch_16:
5595 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::And, E,
5596 llvm::Instruction::And);
5597 case Builtin::BI__sync_or_and_fetch_1:
5598 case Builtin::BI__sync_or_and_fetch_2:
5599 case Builtin::BI__sync_or_and_fetch_4:
5600 case Builtin::BI__sync_or_and_fetch_8:
5601 case Builtin::BI__sync_or_and_fetch_16:
5602 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Or, E,
5603 llvm::Instruction::Or);
5604 case Builtin::BI__sync_xor_and_fetch_1:
5605 case Builtin::BI__sync_xor_and_fetch_2:
5606 case Builtin::BI__sync_xor_and_fetch_4:
5607 case Builtin::BI__sync_xor_and_fetch_8:
5608 case Builtin::BI__sync_xor_and_fetch_16:
5609 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Xor, E,
5610 llvm::Instruction::Xor);
5611 case Builtin::BI__sync_nand_and_fetch_1:
5612 case Builtin::BI__sync_nand_and_fetch_2:
5613 case Builtin::BI__sync_nand_and_fetch_4:
5614 case Builtin::BI__sync_nand_and_fetch_8:
5615 case Builtin::BI__sync_nand_and_fetch_16:
5616 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Nand, E,
5617 llvm::Instruction::And, true);
5618
5619 case Builtin::BI__sync_val_compare_and_swap_1:
5620 case Builtin::BI__sync_val_compare_and_swap_2:
5621 case Builtin::BI__sync_val_compare_and_swap_4:
5622 case Builtin::BI__sync_val_compare_and_swap_8:
5623 case Builtin::BI__sync_val_compare_and_swap_16:
5625 *this, E, false, AtomicOrdering::SequentiallyConsistent,
5626 AtomicOrdering::SequentiallyConsistent));
5627
5628 case Builtin::BI__sync_bool_compare_and_swap_1:
5629 case Builtin::BI__sync_bool_compare_and_swap_2:
5630 case Builtin::BI__sync_bool_compare_and_swap_4:
5631 case Builtin::BI__sync_bool_compare_and_swap_8:
5632 case Builtin::BI__sync_bool_compare_and_swap_16:
5634 *this, E, true, AtomicOrdering::SequentiallyConsistent,
5635 AtomicOrdering::SequentiallyConsistent));
5636
5637 case Builtin::BI__sync_swap_1:
5638 case Builtin::BI__sync_swap_2:
5639 case Builtin::BI__sync_swap_4:
5640 case Builtin::BI__sync_swap_8:
5641 case Builtin::BI__sync_swap_16:
5642 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E);
5643
5644 case Builtin::BI__sync_lock_test_and_set_1:
5645 case Builtin::BI__sync_lock_test_and_set_2:
5646 case Builtin::BI__sync_lock_test_and_set_4:
5647 case Builtin::BI__sync_lock_test_and_set_8:
5648 case Builtin::BI__sync_lock_test_and_set_16:
5649 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E);
5650
5651 case Builtin::BI__sync_lock_release_1:
5652 case Builtin::BI__sync_lock_release_2:
5653 case Builtin::BI__sync_lock_release_4:
5654 case Builtin::BI__sync_lock_release_8:
5655 case Builtin::BI__sync_lock_release_16: {
5656 Address Ptr = CheckAtomicAlignment(*this, E);
5657 QualType ElTy = E->getArg(0)->getType()->getPointeeType();
5658
5659 llvm::Type *ITy = llvm::IntegerType::get(getLLVMContext(),
5660 getContext().getTypeSize(ElTy));
5661 llvm::StoreInst *Store =
5662 Builder.CreateStore(llvm::Constant::getNullValue(ITy), Ptr);
5663 Store->setAtomic(llvm::AtomicOrdering::Release);
5664 return RValue::get(nullptr);
5665 }
5666
5667 case Builtin::BI__sync_synchronize: {
5668 // We assume this is supposed to correspond to a C++0x-style
5669 // sequentially-consistent fence (i.e. this is only usable for
5670 // synchronization, not device I/O or anything like that). This intrinsic
5671 // is really badly designed in the sense that in theory, there isn't
5672 // any way to safely use it... but in practice, it mostly works
5673 // to use it with non-atomic loads and stores to get acquire/release
5674 // semantics.
5675 Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent);
5676 return RValue::get(nullptr);
5677 }
5678
5679 case Builtin::BI__builtin_nontemporal_load:
5680 return RValue::get(EmitNontemporalLoad(*this, E));
5681 case Builtin::BI__builtin_nontemporal_store:
5682 return RValue::get(EmitNontemporalStore(*this, E));
5683 case Builtin::BI__c11_atomic_is_lock_free:
5684 case Builtin::BI__atomic_is_lock_free: {
5685 // Call "bool __atomic_is_lock_free(size_t size, void *ptr)". For the
5686 // __c11 builtin, ptr is 0 (indicating a properly-aligned object), since
5687 // _Atomic(T) is always properly-aligned.
5688 const char *LibCallName = "__atomic_is_lock_free";
5689 CallArgList Args;
5690 Args.add(RValue::get(EmitScalarExpr(E->getArg(0))),
5691 getContext().getSizeType());
5692 if (BuiltinID == Builtin::BI__atomic_is_lock_free)
5693 Args.add(RValue::get(EmitScalarExpr(E->getArg(1))),
5695 else
5696 Args.add(RValue::get(llvm::Constant::getNullValue(VoidPtrTy)),
5698 const CGFunctionInfo &FuncInfo =
5699 CGM.getTypes().arrangeBuiltinFunctionCall(E->getType(), Args);
5700 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo);
5701 llvm::FunctionCallee Func = CGM.CreateRuntimeFunction(FTy, LibCallName);
5702 return EmitCall(FuncInfo, CGCallee::forDirect(Func),
5703 ReturnValueSlot(), Args);
5704 }
5705
5706 case Builtin::BI__atomic_thread_fence:
5707 case Builtin::BI__atomic_signal_fence:
5708 case Builtin::BI__c11_atomic_thread_fence:
5709 case Builtin::BI__c11_atomic_signal_fence: {
5710 llvm::SyncScope::ID SSID;
5711 if (BuiltinID == Builtin::BI__atomic_signal_fence ||
5712 BuiltinID == Builtin::BI__c11_atomic_signal_fence)
5713 SSID = llvm::SyncScope::SingleThread;
5714 else
5715 SSID = llvm::SyncScope::System;
5716 Value *Order = EmitScalarExpr(E->getArg(0));
5717 if (isa<llvm::ConstantInt>(Order)) {
5718 int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
5719 switch (ord) {
5720 case 0: // memory_order_relaxed
5721 default: // invalid order
5722 break;
5723 case 1: // memory_order_consume
5724 case 2: // memory_order_acquire
5725 Builder.CreateFence(llvm::AtomicOrdering::Acquire, SSID);
5726 break;
5727 case 3: // memory_order_release
5728 Builder.CreateFence(llvm::AtomicOrdering::Release, SSID);
5729 break;
5730 case 4: // memory_order_acq_rel
5731 Builder.CreateFence(llvm::AtomicOrdering::AcquireRelease, SSID);
5732 break;
5733 case 5: // memory_order_seq_cst
5734 Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent, SSID);
5735 break;
5736 }
5737 return RValue::get(nullptr);
5738 }
5739
5740 llvm::BasicBlock *AcquireBB, *ReleaseBB, *AcqRelBB, *SeqCstBB;
5741 AcquireBB = createBasicBlock("acquire", CurFn);
5742 ReleaseBB = createBasicBlock("release", CurFn);
5743 AcqRelBB = createBasicBlock("acqrel", CurFn);
5744 SeqCstBB = createBasicBlock("seqcst", CurFn);
5745 llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
5746
5747 Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
5748 llvm::SwitchInst *SI = Builder.CreateSwitch(Order, ContBB);
5749
5750 Builder.SetInsertPoint(AcquireBB);
5751 Builder.CreateFence(llvm::AtomicOrdering::Acquire, SSID);
5752 Builder.CreateBr(ContBB);
5753 SI->addCase(Builder.getInt32(1), AcquireBB);
5754 SI->addCase(Builder.getInt32(2), AcquireBB);
5755
5756 Builder.SetInsertPoint(ReleaseBB);
5757 Builder.CreateFence(llvm::AtomicOrdering::Release, SSID);
5758 Builder.CreateBr(ContBB);
5759 SI->addCase(Builder.getInt32(3), ReleaseBB);
5760
5761 Builder.SetInsertPoint(AcqRelBB);
5762 Builder.CreateFence(llvm::AtomicOrdering::AcquireRelease, SSID);
5763 Builder.CreateBr(ContBB);
5764 SI->addCase(Builder.getInt32(4), AcqRelBB);
5765
5766 Builder.SetInsertPoint(SeqCstBB);
5767 Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent, SSID);
5768 Builder.CreateBr(ContBB);
5769 SI->addCase(Builder.getInt32(5), SeqCstBB);
5770
5771 Builder.SetInsertPoint(ContBB);
5772 return RValue::get(nullptr);
5773 }
5774 case Builtin::BI__scoped_atomic_thread_fence: {
5776
5777 Value *Order = EmitScalarExpr(E->getArg(0));
5778 Value *Scope = EmitScalarExpr(E->getArg(1));
5779 auto Ord = dyn_cast<llvm::ConstantInt>(Order);
5780 auto Scp = dyn_cast<llvm::ConstantInt>(Scope);
5781 if (Ord && Scp) {
5782 SyncScope SS = ScopeModel->isValid(Scp->getZExtValue())
5783 ? ScopeModel->map(Scp->getZExtValue())
5784 : ScopeModel->map(ScopeModel->getFallBackValue());
5785 switch (Ord->getZExtValue()) {
5786 case 0: // memory_order_relaxed
5787 default: // invalid order
5788 break;
5789 case 1: // memory_order_consume
5790 case 2: // memory_order_acquire
5791 Builder.CreateFence(
5792 llvm::AtomicOrdering::Acquire,
5793 getTargetHooks().getLLVMSyncScopeID(getLangOpts(), SS,
5794 llvm::AtomicOrdering::Acquire,
5795 getLLVMContext()));
5796 break;
5797 case 3: // memory_order_release
5798 Builder.CreateFence(
5799 llvm::AtomicOrdering::Release,
5800 getTargetHooks().getLLVMSyncScopeID(getLangOpts(), SS,
5801 llvm::AtomicOrdering::Release,
5802 getLLVMContext()));
5803 break;
5804 case 4: // memory_order_acq_rel
5805 Builder.CreateFence(llvm::AtomicOrdering::AcquireRelease,
5806 getTargetHooks().getLLVMSyncScopeID(
5807 getLangOpts(), SS,
5808 llvm::AtomicOrdering::AcquireRelease,
5809 getLLVMContext()));
5810 break;
5811 case 5: // memory_order_seq_cst
5812 Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent,
5813 getTargetHooks().getLLVMSyncScopeID(
5814 getLangOpts(), SS,
5815 llvm::AtomicOrdering::SequentiallyConsistent,
5816 getLLVMContext()));
5817 break;
5818 }
5819 return RValue::get(nullptr);
5820 }
5821
5822 llvm::BasicBlock *ContBB = createBasicBlock("atomic.scope.continue", CurFn);
5823
5825 OrderBBs;
5826 if (Ord) {
5827 switch (Ord->getZExtValue()) {
5828 case 0: // memory_order_relaxed
5829 default: // invalid order
5830 ContBB->eraseFromParent();
5831 return RValue::get(nullptr);
5832 case 1: // memory_order_consume
5833 case 2: // memory_order_acquire
5834 OrderBBs.emplace_back(Builder.GetInsertBlock(),
5835 llvm::AtomicOrdering::Acquire);
5836 break;
5837 case 3: // memory_order_release
5838 OrderBBs.emplace_back(Builder.GetInsertBlock(),
5839 llvm::AtomicOrdering::Release);
5840 break;
5841 case 4: // memory_order_acq_rel
5842 OrderBBs.emplace_back(Builder.GetInsertBlock(),
5843 llvm::AtomicOrdering::AcquireRelease);
5844 break;
5845 case 5: // memory_order_seq_cst
5846 OrderBBs.emplace_back(Builder.GetInsertBlock(),
5847 llvm::AtomicOrdering::SequentiallyConsistent);
5848 break;
5849 }
5850 } else {
5851 llvm::BasicBlock *AcquireBB = createBasicBlock("acquire", CurFn);
5852 llvm::BasicBlock *ReleaseBB = createBasicBlock("release", CurFn);
5853 llvm::BasicBlock *AcqRelBB = createBasicBlock("acqrel", CurFn);
5854 llvm::BasicBlock *SeqCstBB = createBasicBlock("seqcst", CurFn);
5855
5856 Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
5857 llvm::SwitchInst *SI = Builder.CreateSwitch(Order, ContBB);
5858 SI->addCase(Builder.getInt32(1), AcquireBB);
5859 SI->addCase(Builder.getInt32(2), AcquireBB);
5860 SI->addCase(Builder.getInt32(3), ReleaseBB);
5861 SI->addCase(Builder.getInt32(4), AcqRelBB);
5862 SI->addCase(Builder.getInt32(5), SeqCstBB);
5863
5864 OrderBBs.emplace_back(AcquireBB, llvm::AtomicOrdering::Acquire);
5865 OrderBBs.emplace_back(ReleaseBB, llvm::AtomicOrdering::Release);
5866 OrderBBs.emplace_back(AcqRelBB, llvm::AtomicOrdering::AcquireRelease);
5867 OrderBBs.emplace_back(SeqCstBB,
5868 llvm::AtomicOrdering::SequentiallyConsistent);
5869 }
5870
5871 for (auto &[OrderBB, Ordering] : OrderBBs) {
5872 Builder.SetInsertPoint(OrderBB);
5873 if (Scp) {
5874 SyncScope SS = ScopeModel->isValid(Scp->getZExtValue())
5875 ? ScopeModel->map(Scp->getZExtValue())
5876 : ScopeModel->map(ScopeModel->getFallBackValue());
5877 Builder.CreateFence(Ordering,
5878 getTargetHooks().getLLVMSyncScopeID(
5879 getLangOpts(), SS, Ordering, getLLVMContext()));
5880 Builder.CreateBr(ContBB);
5881 } else {
5882 llvm::DenseMap<unsigned, llvm::BasicBlock *> BBs;
5883 for (unsigned Scp : ScopeModel->getRuntimeValues())
5884 BBs[Scp] = createBasicBlock(getAsString(ScopeModel->map(Scp)), CurFn);
5885
5886 auto *SC = Builder.CreateIntCast(Scope, Builder.getInt32Ty(), false);
5887 llvm::SwitchInst *SI = Builder.CreateSwitch(SC, ContBB);
5888 for (unsigned Scp : ScopeModel->getRuntimeValues()) {
5889 auto *B = BBs[Scp];
5890 SI->addCase(Builder.getInt32(Scp), B);
5891
5892 Builder.SetInsertPoint(B);
5893 Builder.CreateFence(Ordering, getTargetHooks().getLLVMSyncScopeID(
5894 getLangOpts(), ScopeModel->map(Scp),
5895 Ordering, getLLVMContext()));
5896 Builder.CreateBr(ContBB);
5897 }
5898 }
5899 }
5900
5901 Builder.SetInsertPoint(ContBB);
5902 return RValue::get(nullptr);
5903 }
5904
5905 case Builtin::BI__builtin_signbit:
5906 case Builtin::BI__builtin_signbitf:
5907 case Builtin::BI__builtin_signbitl: {
5908 return RValue::get(
5909 Builder.CreateZExt(EmitSignBit(*this, EmitScalarExpr(E->getArg(0))),
5910 ConvertType(E->getType())));
5911 }
5912 case Builtin::BI__warn_memset_zero_len:
5913 return RValue::getIgnored();
5914 case Builtin::BI__annotation: {
5915 // Re-encode each wide string to UTF8 and make an MDString.
5917 for (const Expr *Arg : E->arguments()) {
5918 const auto *Str = cast<StringLiteral>(Arg->IgnoreParenCasts());
5919 assert(Str->getCharByteWidth() == 2 || Str->getCharByteWidth() == 4);
5920 StringRef WideBytes = Str->getBytes();
5921 std::string StrUtf8;
5922 bool Converted =
5923 (Str->getCharByteWidth() == 2)
5924 ? convertUTF16ToUTF8String(
5925 ArrayRef(WideBytes.data(), WideBytes.size()), StrUtf8)
5926 : convertUTF32ToUTF8String(
5927 ArrayRef(WideBytes.data(), WideBytes.size()), StrUtf8);
5928 if (!Converted) {
5929 CGM.ErrorUnsupported(E, "non-Unicode __annotation argument");
5930 continue;
5931 }
5932 Strings.push_back(llvm::MDString::get(getLLVMContext(), StrUtf8));
5933 }
5934
5935 // Build and MDTuple of MDStrings and emit the intrinsic call.
5936 llvm::Function *F = CGM.getIntrinsic(Intrinsic::codeview_annotation, {});
5937 MDTuple *StrTuple = MDTuple::get(getLLVMContext(), Strings);
5938 Builder.CreateCall(F, MetadataAsValue::get(getLLVMContext(), StrTuple));
5939 return RValue::getIgnored();
5940 }
5941 case Builtin::BI__builtin_annotation: {
5942 llvm::Value *AnnVal = EmitScalarExpr(E->getArg(0));
5943 llvm::Function *F = CGM.getIntrinsic(
5944 Intrinsic::annotation, {AnnVal->getType(), CGM.ConstGlobalsPtrTy});
5945
5946 // Get the annotation string, go through casts. Sema requires this to be a
5947 // non-wide string literal, potentially casted, so the cast<> is safe.
5948 const Expr *AnnotationStrExpr = E->getArg(1)->IgnoreParenCasts();
5949 StringRef Str = cast<StringLiteral>(AnnotationStrExpr)->getString();
5950 return RValue::get(
5951 EmitAnnotationCall(F, AnnVal, Str, E->getExprLoc(), nullptr));
5952 }
5953 case Builtin::BI__builtin_addcb:
5954 case Builtin::BI__builtin_addcs:
5955 case Builtin::BI__builtin_addc:
5956 case Builtin::BI__builtin_addcl:
5957 case Builtin::BI__builtin_addcll:
5958 case Builtin::BI__builtin_subcb:
5959 case Builtin::BI__builtin_subcs:
5960 case Builtin::BI__builtin_subc:
5961 case Builtin::BI__builtin_subcl:
5962 case Builtin::BI__builtin_subcll: {
5963
5964 // We translate all of these builtins from expressions of the form:
5965 // int x = ..., y = ..., carryin = ..., carryout, result;
5966 // result = __builtin_addc(x, y, carryin, &carryout);
5967 //
5968 // to LLVM IR of the form:
5969 //
5970 // %tmp1 = call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)
5971 // %tmpsum1 = extractvalue {i32, i1} %tmp1, 0
5972 // %carry1 = extractvalue {i32, i1} %tmp1, 1
5973 // %tmp2 = call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %tmpsum1,
5974 // i32 %carryin)
5975 // %result = extractvalue {i32, i1} %tmp2, 0
5976 // %carry2 = extractvalue {i32, i1} %tmp2, 1
5977 // %tmp3 = or i1 %carry1, %carry2
5978 // %tmp4 = zext i1 %tmp3 to i32
5979 // store i32 %tmp4, i32* %carryout
5980
5981 // Scalarize our inputs.
5982 llvm::Value *X = EmitScalarExpr(E->getArg(0));
5983 llvm::Value *Y = EmitScalarExpr(E->getArg(1));
5984 llvm::Value *Carryin = EmitScalarExpr(E->getArg(2));
5985 Address CarryOutPtr = EmitPointerWithAlignment(E->getArg(3));
5986
5987 // Decide if we are lowering to a uadd.with.overflow or usub.with.overflow.
5988 Intrinsic::ID IntrinsicId;
5989 switch (BuiltinID) {
5990 default: llvm_unreachable("Unknown multiprecision builtin id.");
5991 case Builtin::BI__builtin_addcb:
5992 case Builtin::BI__builtin_addcs:
5993 case Builtin::BI__builtin_addc:
5994 case Builtin::BI__builtin_addcl:
5995 case Builtin::BI__builtin_addcll:
5996 IntrinsicId = Intrinsic::uadd_with_overflow;
5997 break;
5998 case Builtin::BI__builtin_subcb:
5999 case Builtin::BI__builtin_subcs:
6000 case Builtin::BI__builtin_subc:
6001 case Builtin::BI__builtin_subcl:
6002 case Builtin::BI__builtin_subcll:
6003 IntrinsicId = Intrinsic::usub_with_overflow;
6004 break;
6005 }
6006
6007 // Construct our resulting LLVM IR expression.
6008 llvm::Value *Carry1;
6009 llvm::Value *Sum1 = EmitOverflowIntrinsic(*this, IntrinsicId,
6010 X, Y, Carry1);
6011 llvm::Value *Carry2;
6012 llvm::Value *Sum2 = EmitOverflowIntrinsic(*this, IntrinsicId,
6013 Sum1, Carryin, Carry2);
6014 llvm::Value *CarryOut = Builder.CreateZExt(Builder.CreateOr(Carry1, Carry2),
6015 X->getType());
6016 Builder.CreateStore(CarryOut, CarryOutPtr);
6017 return RValue::get(Sum2);
6018 }
6019
6020 case Builtin::BI__builtin_add_overflow:
6021 case Builtin::BI__builtin_sub_overflow:
6022 case Builtin::BI__builtin_mul_overflow: {
6023 const clang::Expr *LeftArg = E->getArg(0);
6024 const clang::Expr *RightArg = E->getArg(1);
6025 const clang::Expr *ResultArg = E->getArg(2);
6026
6027 clang::QualType ResultQTy =
6028 ResultArg->getType()->castAs<PointerType>()->getPointeeType();
6029
6030 WidthAndSignedness LeftInfo =
6031 getIntegerWidthAndSignedness(CGM.getContext(), LeftArg->getType());
6032 WidthAndSignedness RightInfo =
6033 getIntegerWidthAndSignedness(CGM.getContext(), RightArg->getType());
6034 WidthAndSignedness ResultInfo =
6035 getIntegerWidthAndSignedness(CGM.getContext(), ResultQTy);
6036
6037 // Handle mixed-sign multiplication as a special case, because adding
6038 // runtime or backend support for our generic irgen would be too expensive.
6039 if (isSpecialMixedSignMultiply(BuiltinID, LeftInfo, RightInfo, ResultInfo))
6040 return EmitCheckedMixedSignMultiply(*this, LeftArg, LeftInfo, RightArg,
6041 RightInfo, ResultArg, ResultQTy,
6042 ResultInfo);
6043
6044 if (isSpecialUnsignedMultiplySignedResult(BuiltinID, LeftInfo, RightInfo,
6045 ResultInfo))
6047 *this, LeftArg, LeftInfo, RightArg, RightInfo, ResultArg, ResultQTy,
6048 ResultInfo);
6049
6050 WidthAndSignedness EncompassingInfo =
6051 EncompassingIntegerType({LeftInfo, RightInfo, ResultInfo});
6052
6053 llvm::Type *EncompassingLLVMTy =
6054 llvm::IntegerType::get(CGM.getLLVMContext(), EncompassingInfo.Width);
6055
6056 llvm::Type *ResultLLVMTy = CGM.getTypes().ConvertType(ResultQTy);
6057
6058 Intrinsic::ID IntrinsicId;
6059 switch (BuiltinID) {
6060 default:
6061 llvm_unreachable("Unknown overflow builtin id.");
6062 case Builtin::BI__builtin_add_overflow:
6063 IntrinsicId = EncompassingInfo.Signed ? Intrinsic::sadd_with_overflow
6064 : Intrinsic::uadd_with_overflow;
6065 break;
6066 case Builtin::BI__builtin_sub_overflow:
6067 IntrinsicId = EncompassingInfo.Signed ? Intrinsic::ssub_with_overflow
6068 : Intrinsic::usub_with_overflow;
6069 break;
6070 case Builtin::BI__builtin_mul_overflow:
6071 IntrinsicId = EncompassingInfo.Signed ? Intrinsic::smul_with_overflow
6072 : Intrinsic::umul_with_overflow;
6073 break;
6074 }
6075
6076 llvm::Value *Left = EmitScalarExpr(LeftArg);
6077 llvm::Value *Right = EmitScalarExpr(RightArg);
6078 Address ResultPtr = EmitPointerWithAlignment(ResultArg);
6079
6080 // Extend each operand to the encompassing type.
6081 Left = Builder.CreateIntCast(Left, EncompassingLLVMTy, LeftInfo.Signed);
6082 Right = Builder.CreateIntCast(Right, EncompassingLLVMTy, RightInfo.Signed);
6083
6084 // Perform the operation on the extended values.
6085 llvm::Value *Overflow, *Result;
6086 Result = EmitOverflowIntrinsic(*this, IntrinsicId, Left, Right, Overflow);
6087
6088 if (EncompassingInfo.Width > ResultInfo.Width) {
6089 // The encompassing type is wider than the result type, so we need to
6090 // truncate it.
6091 llvm::Value *ResultTrunc = Builder.CreateTrunc(Result, ResultLLVMTy);
6092
6093 // To see if the truncation caused an overflow, we will extend
6094 // the result and then compare it to the original result.
6095 llvm::Value *ResultTruncExt = Builder.CreateIntCast(
6096 ResultTrunc, EncompassingLLVMTy, ResultInfo.Signed);
6097 llvm::Value *TruncationOverflow =
6098 Builder.CreateICmpNE(Result, ResultTruncExt);
6099
6100 Overflow = Builder.CreateOr(Overflow, TruncationOverflow);
6101 Result = ResultTrunc;
6102 }
6103
6104 // Finally, store the result using the pointer.
6105 bool isVolatile =
6106 ResultArg->getType()->getPointeeType().isVolatileQualified();
6107 Builder.CreateStore(EmitToMemory(Result, ResultQTy), ResultPtr, isVolatile);
6108
6109 return RValue::get(Overflow);
6110 }
6111
6112 case Builtin::BI__builtin_uadd_overflow:
6113 case Builtin::BI__builtin_uaddl_overflow:
6114 case Builtin::BI__builtin_uaddll_overflow:
6115 case Builtin::BI__builtin_usub_overflow:
6116 case Builtin::BI__builtin_usubl_overflow:
6117 case Builtin::BI__builtin_usubll_overflow:
6118 case Builtin::BI__builtin_umul_overflow:
6119 case Builtin::BI__builtin_umull_overflow:
6120 case Builtin::BI__builtin_umulll_overflow:
6121 case Builtin::BI__builtin_sadd_overflow:
6122 case Builtin::BI__builtin_saddl_overflow:
6123 case Builtin::BI__builtin_saddll_overflow:
6124 case Builtin::BI__builtin_ssub_overflow:
6125 case Builtin::BI__builtin_ssubl_overflow:
6126 case Builtin::BI__builtin_ssubll_overflow:
6127 case Builtin::BI__builtin_smul_overflow:
6128 case Builtin::BI__builtin_smull_overflow:
6129 case Builtin::BI__builtin_smulll_overflow: {
6130
6131 // We translate all of these builtins directly to the relevant llvm IR node.
6132
6133 // Scalarize our inputs.
6134 llvm::Value *X = EmitScalarExpr(E->getArg(0));
6135 llvm::Value *Y = EmitScalarExpr(E->getArg(1));
6136 Address SumOutPtr = EmitPointerWithAlignment(E->getArg(2));
6137
6138 // Decide which of the overflow intrinsics we are lowering to:
6139 Intrinsic::ID IntrinsicId;
6140 switch (BuiltinID) {
6141 default: llvm_unreachable("Unknown overflow builtin id.");
6142 case Builtin::BI__builtin_uadd_overflow:
6143 case Builtin::BI__builtin_uaddl_overflow:
6144 case Builtin::BI__builtin_uaddll_overflow:
6145 IntrinsicId = Intrinsic::uadd_with_overflow;
6146 break;
6147 case Builtin::BI__builtin_usub_overflow:
6148 case Builtin::BI__builtin_usubl_overflow:
6149 case Builtin::BI__builtin_usubll_overflow:
6150 IntrinsicId = Intrinsic::usub_with_overflow;
6151 break;
6152 case Builtin::BI__builtin_umul_overflow:
6153 case Builtin::BI__builtin_umull_overflow:
6154 case Builtin::BI__builtin_umulll_overflow:
6155 IntrinsicId = Intrinsic::umul_with_overflow;
6156 break;
6157 case Builtin::BI__builtin_sadd_overflow:
6158 case Builtin::BI__builtin_saddl_overflow:
6159 case Builtin::BI__builtin_saddll_overflow:
6160 IntrinsicId = Intrinsic::sadd_with_overflow;
6161 break;
6162 case Builtin::BI__builtin_ssub_overflow:
6163 case Builtin::BI__builtin_ssubl_overflow:
6164 case Builtin::BI__builtin_ssubll_overflow:
6165 IntrinsicId = Intrinsic::ssub_with_overflow;
6166 break;
6167 case Builtin::BI__builtin_smul_overflow:
6168 case Builtin::BI__builtin_smull_overflow:
6169 case Builtin::BI__builtin_smulll_overflow:
6170 IntrinsicId = Intrinsic::smul_with_overflow;
6171 break;
6172 }
6173
6174
6175 llvm::Value *Carry;
6176 llvm::Value *Sum = EmitOverflowIntrinsic(*this, IntrinsicId, X, Y, Carry);
6177 Builder.CreateStore(Sum, SumOutPtr);
6178
6179 return RValue::get(Carry);
6180 }
6181 case Builtin::BIaddressof:
6182 case Builtin::BI__addressof:
6183 case Builtin::BI__builtin_addressof:
6184 return RValue::get(EmitLValue(E->getArg(0)).getPointer(*this));
6185 case Builtin::BI__builtin_function_start:
6186 return RValue::get(CGM.GetFunctionStart(
6187 E->getArg(0)->getAsBuiltinConstantDeclRef(CGM.getContext())));
6188 case Builtin::BI__builtin_operator_new:
6190 E->getCallee()->getType()->castAs<FunctionProtoType>(), E, false);
6191 case Builtin::BI__builtin_operator_delete:
6193 E->getCallee()->getType()->castAs<FunctionProtoType>(), E, true);
6194 return RValue::get(nullptr);
6195
6196 case Builtin::BI__builtin_is_aligned:
6197 return EmitBuiltinIsAligned(E);
6198 case Builtin::BI__builtin_align_up:
6199 return EmitBuiltinAlignTo(E, true);
6200 case Builtin::BI__builtin_align_down:
6201 return EmitBuiltinAlignTo(E, false);
6202
6203 case Builtin::BI__noop:
6204 // __noop always evaluates to an integer literal zero.
6205 return RValue::get(ConstantInt::get(IntTy, 0));
6206 case Builtin::BI__builtin_call_with_static_chain: {
6207 const CallExpr *Call = cast<CallExpr>(E->getArg(0));
6208 const Expr *Chain = E->getArg(1);
6209 return EmitCall(Call->getCallee()->getType(),
6210 EmitCallee(Call->getCallee()), Call, ReturnValue,
6211 EmitScalarExpr(Chain));
6212 }
6213 case Builtin::BI_InterlockedExchange8:
6214 case Builtin::BI_InterlockedExchange16:
6215 case Builtin::BI_InterlockedExchange:
6216 case Builtin::BI_InterlockedExchangePointer:
6217 return RValue::get(
6219 case Builtin::BI_InterlockedCompareExchangePointer:
6220 return RValue::get(
6222 case Builtin::BI_InterlockedCompareExchangePointer_nf:
6223 return RValue::get(
6225 case Builtin::BI_InterlockedCompareExchange8:
6226 case Builtin::BI_InterlockedCompareExchange16:
6227 case Builtin::BI_InterlockedCompareExchange:
6228 case Builtin::BI_InterlockedCompareExchange64:
6229 return RValue::get(EmitAtomicCmpXchgForMSIntrin(*this, E));
6230 case Builtin::BI_InterlockedIncrement16:
6231 case Builtin::BI_InterlockedIncrement:
6232 return RValue::get(
6234 case Builtin::BI_InterlockedDecrement16:
6235 case Builtin::BI_InterlockedDecrement:
6236 return RValue::get(
6238 case Builtin::BI_InterlockedAnd8:
6239 case Builtin::BI_InterlockedAnd16:
6240 case Builtin::BI_InterlockedAnd:
6242 case Builtin::BI_InterlockedExchangeAdd8:
6243 case Builtin::BI_InterlockedExchangeAdd16:
6244 case Builtin::BI_InterlockedExchangeAdd:
6245 return RValue::get(
6247 case Builtin::BI_InterlockedExchangeSub8:
6248 case Builtin::BI_InterlockedExchangeSub16:
6249 case Builtin::BI_InterlockedExchangeSub:
6250 return RValue::get(
6252 case Builtin::BI_InterlockedOr8:
6253 case Builtin::BI_InterlockedOr16:
6254 case Builtin::BI_InterlockedOr:
6256 case Builtin::BI_InterlockedXor8:
6257 case Builtin::BI_InterlockedXor16:
6258 case Builtin::BI_InterlockedXor:
6260
6261 case Builtin::BI_bittest64:
6262 case Builtin::BI_bittest:
6263 case Builtin::BI_bittestandcomplement64:
6264 case Builtin::BI_bittestandcomplement:
6265 case Builtin::BI_bittestandreset64:
6266 case Builtin::BI_bittestandreset:
6267 case Builtin::BI_bittestandset64:
6268 case Builtin::BI_bittestandset:
6269 case Builtin::BI_interlockedbittestandreset:
6270 case Builtin::BI_interlockedbittestandreset64:
6271 case Builtin::BI_interlockedbittestandreset64_acq:
6272 case Builtin::BI_interlockedbittestandreset64_rel:
6273 case Builtin::BI_interlockedbittestandreset64_nf:
6274 case Builtin::BI_interlockedbittestandset64:
6275 case Builtin::BI_interlockedbittestandset64_acq:
6276 case Builtin::BI_interlockedbittestandset64_rel:
6277 case Builtin::BI_interlockedbittestandset64_nf:
6278 case Builtin::BI_interlockedbittestandset:
6279 case Builtin::BI_interlockedbittestandset_acq:
6280 case Builtin::BI_interlockedbittestandset_rel:
6281 case Builtin::BI_interlockedbittestandset_nf:
6282 case Builtin::BI_interlockedbittestandreset_acq:
6283 case Builtin::BI_interlockedbittestandreset_rel:
6284 case Builtin::BI_interlockedbittestandreset_nf:
6285 return RValue::get(EmitBitTestIntrinsic(*this, BuiltinID, E));
6286
6287 // These builtins exist to emit regular volatile loads and stores not
6288 // affected by the -fms-volatile setting.
6289 case Builtin::BI__iso_volatile_load8:
6290 case Builtin::BI__iso_volatile_load16:
6291 case Builtin::BI__iso_volatile_load32:
6292 case Builtin::BI__iso_volatile_load64:
6293 return RValue::get(EmitISOVolatileLoad(*this, E));
6294 case Builtin::BI__iso_volatile_store8:
6295 case Builtin::BI__iso_volatile_store16:
6296 case Builtin::BI__iso_volatile_store32:
6297 case Builtin::BI__iso_volatile_store64:
6298 return RValue::get(EmitISOVolatileStore(*this, E));
6299
6300 case Builtin::BI__builtin_ptrauth_sign_constant:
6301 return RValue::get(ConstantEmitter(*this).emitAbstract(E, E->getType()));
6302
6303 case Builtin::BI__builtin_ptrauth_auth:
6304 case Builtin::BI__builtin_ptrauth_auth_and_resign:
6305 case Builtin::BI__builtin_ptrauth_auth_load_relative_and_sign:
6306 case Builtin::BI__builtin_ptrauth_blend_discriminator:
6307 case Builtin::BI__builtin_ptrauth_sign_generic_data:
6308 case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
6309 case Builtin::BI__builtin_ptrauth_strip: {
6310 // Emit the arguments.
6312 for (auto argExpr : E->arguments())
6313 Args.push_back(EmitScalarExpr(argExpr));
6314
6315 // Cast the value to intptr_t, saving its original type.
6316 llvm::Type *OrigValueType = Args[0]->getType();
6317 if (OrigValueType->isPointerTy())
6318 Args[0] = Builder.CreatePtrToInt(Args[0], IntPtrTy);
6319
6320 switch (BuiltinID) {
6321 case Builtin::BI__builtin_ptrauth_auth_and_resign:
6322 case Builtin::BI__builtin_ptrauth_auth_load_relative_and_sign:
6323 if (Args[4]->getType()->isPointerTy())
6324 Args[4] = Builder.CreatePtrToInt(Args[4], IntPtrTy);
6325 [[fallthrough]];
6326
6327 case Builtin::BI__builtin_ptrauth_auth:
6328 case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
6329 if (Args[2]->getType()->isPointerTy())
6330 Args[2] = Builder.CreatePtrToInt(Args[2], IntPtrTy);
6331 break;
6332
6333 case Builtin::BI__builtin_ptrauth_sign_generic_data:
6334 if (Args[1]->getType()->isPointerTy())
6335 Args[1] = Builder.CreatePtrToInt(Args[1], IntPtrTy);
6336 break;
6337
6338 case Builtin::BI__builtin_ptrauth_blend_discriminator:
6339 case Builtin::BI__builtin_ptrauth_strip:
6340 break;
6341 }
6342
6343 // Call the intrinsic.
6344 auto IntrinsicID = [&]() -> unsigned {
6345 switch (BuiltinID) {
6346 case Builtin::BI__builtin_ptrauth_auth:
6347 return Intrinsic::ptrauth_auth;
6348 case Builtin::BI__builtin_ptrauth_auth_and_resign:
6349 return Intrinsic::ptrauth_resign;
6350 case Builtin::BI__builtin_ptrauth_auth_load_relative_and_sign:
6351 return Intrinsic::ptrauth_resign_load_relative;
6352 case Builtin::BI__builtin_ptrauth_blend_discriminator:
6353 return Intrinsic::ptrauth_blend;
6354 case Builtin::BI__builtin_ptrauth_sign_generic_data:
6355 return Intrinsic::ptrauth_sign_generic;
6356 case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
6357 return Intrinsic::ptrauth_sign;
6358 case Builtin::BI__builtin_ptrauth_strip:
6359 return Intrinsic::ptrauth_strip;
6360 }
6361 llvm_unreachable("bad ptrauth intrinsic");
6362 }();
6363 auto Intrinsic = CGM.getIntrinsic(IntrinsicID);
6364 llvm::Value *Result = EmitRuntimeCall(Intrinsic, Args);
6365
6366 if (BuiltinID != Builtin::BI__builtin_ptrauth_sign_generic_data &&
6367 BuiltinID != Builtin::BI__builtin_ptrauth_blend_discriminator &&
6368 OrigValueType->isPointerTy()) {
6369 Result = Builder.CreateIntToPtr(Result, OrigValueType);
6370 }
6371 return RValue::get(Result);
6372 }
6373
6374 case Builtin::BI__builtin_get_vtable_pointer: {
6375 const Expr *Target = E->getArg(0);
6376 QualType TargetType = Target->getType();
6377 const CXXRecordDecl *Decl = TargetType->getPointeeCXXRecordDecl();
6378 assert(Decl);
6379 auto ThisAddress = EmitPointerWithAlignment(Target);
6380 assert(ThisAddress.isValid());
6381 llvm::Value *VTablePointer =
6383 return RValue::get(VTablePointer);
6384 }
6385
6386 case Builtin::BI__exception_code:
6387 case Builtin::BI_exception_code:
6389 case Builtin::BI__exception_info:
6390 case Builtin::BI_exception_info:
6392 case Builtin::BI__abnormal_termination:
6393 case Builtin::BI_abnormal_termination:
6395 case Builtin::BI_setjmpex:
6396 if (getTarget().getTriple().isOSMSVCRT() && E->getNumArgs() == 1 &&
6397 E->getArg(0)->getType()->isPointerType())
6398 return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmpex, E);
6399 break;
6400 case Builtin::BI_setjmp:
6401 if (getTarget().getTriple().isOSMSVCRT() && E->getNumArgs() == 1 &&
6402 E->getArg(0)->getType()->isPointerType()) {
6403 if (getTarget().getTriple().getArch() == llvm::Triple::x86)
6404 return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmp3, E);
6405 else if (getTarget().getTriple().getArch() == llvm::Triple::aarch64)
6406 return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmpex, E);
6407 return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmp, E);
6408 }
6409 break;
6410
6411 // C++ std:: builtins.
6412 case Builtin::BImove:
6413 case Builtin::BImove_if_noexcept:
6414 case Builtin::BIforward:
6415 case Builtin::BIforward_like:
6416 case Builtin::BIas_const:
6417 return RValue::get(EmitLValue(E->getArg(0)).getPointer(*this));
6418 case Builtin::BI__GetExceptionInfo: {
6419 if (llvm::GlobalVariable *GV =
6420 CGM.getCXXABI().getThrowInfo(FD->getParamDecl(0)->getType()))
6421 return RValue::get(GV);
6422 break;
6423 }
6424
6425 case Builtin::BI__fastfail:
6427
6428 case Builtin::BI__builtin_coro_id:
6429 return EmitCoroutineIntrinsic(E, Intrinsic::coro_id);
6430 case Builtin::BI__builtin_coro_promise:
6431 return EmitCoroutineIntrinsic(E, Intrinsic::coro_promise);
6432 case Builtin::BI__builtin_coro_resume:
6433 EmitCoroutineIntrinsic(E, Intrinsic::coro_resume);
6434 return RValue::get(nullptr);
6435 case Builtin::BI__builtin_coro_frame:
6436 return EmitCoroutineIntrinsic(E, Intrinsic::coro_frame);
6437 case Builtin::BI__builtin_coro_noop:
6438 return EmitCoroutineIntrinsic(E, Intrinsic::coro_noop);
6439 case Builtin::BI__builtin_coro_free:
6440 return EmitCoroutineIntrinsic(E, Intrinsic::coro_free);
6441 case Builtin::BI__builtin_coro_destroy:
6442 EmitCoroutineIntrinsic(E, Intrinsic::coro_destroy);
6443 return RValue::get(nullptr);
6444 case Builtin::BI__builtin_coro_done:
6445 return EmitCoroutineIntrinsic(E, Intrinsic::coro_done);
6446 case Builtin::BI__builtin_coro_alloc:
6447 return EmitCoroutineIntrinsic(E, Intrinsic::coro_alloc);
6448 case Builtin::BI__builtin_coro_begin:
6449 return EmitCoroutineIntrinsic(E, Intrinsic::coro_begin);
6450 case Builtin::BI__builtin_coro_end:
6451 return EmitCoroutineIntrinsic(E, Intrinsic::coro_end);
6452 case Builtin::BI__builtin_coro_suspend:
6453 return EmitCoroutineIntrinsic(E, Intrinsic::coro_suspend);
6454 case Builtin::BI__builtin_coro_size:
6455 return EmitCoroutineIntrinsic(E, Intrinsic::coro_size);
6456 case Builtin::BI__builtin_coro_align:
6457 return EmitCoroutineIntrinsic(E, Intrinsic::coro_align);
6458
6459 // OpenCL v2.0 s6.13.16.2, Built-in pipe read and write functions
6460 case Builtin::BIread_pipe:
6461 case Builtin::BIwrite_pipe: {
6462 Value *Arg0 = EmitScalarExpr(E->getArg(0)),
6463 *Arg1 = EmitScalarExpr(E->getArg(1));
6464 CGOpenCLRuntime OpenCLRT(CGM);
6465 Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
6466 Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
6467
6468 // Type of the generic packet parameter.
6469 unsigned GenericAS =
6471 llvm::Type *I8PTy = llvm::PointerType::get(getLLVMContext(), GenericAS);
6472
6473 // Testing which overloaded version we should generate the call for.
6474 if (2U == E->getNumArgs()) {
6475 const char *Name = (BuiltinID == Builtin::BIread_pipe) ? "__read_pipe_2"
6476 : "__write_pipe_2";
6477 // Creating a generic function type to be able to call with any builtin or
6478 // user defined type.
6479 llvm::Type *ArgTys[] = {Arg0->getType(), I8PTy, Int32Ty, Int32Ty};
6480 llvm::FunctionType *FTy = llvm::FunctionType::get(Int32Ty, ArgTys, false);
6481 Value *ACast = Builder.CreateAddrSpaceCast(Arg1, I8PTy);
6482 return RValue::get(
6483 EmitRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name),
6484 {Arg0, ACast, PacketSize, PacketAlign}));
6485 } else {
6486 assert(4 == E->getNumArgs() &&
6487 "Illegal number of parameters to pipe function");
6488 const char *Name = (BuiltinID == Builtin::BIread_pipe) ? "__read_pipe_4"
6489 : "__write_pipe_4";
6490
6491 llvm::Type *ArgTys[] = {Arg0->getType(), Arg1->getType(), Int32Ty, I8PTy,
6492 Int32Ty, Int32Ty};
6493 Value *Arg2 = EmitScalarExpr(E->getArg(2)),
6494 *Arg3 = EmitScalarExpr(E->getArg(3));
6495 llvm::FunctionType *FTy = llvm::FunctionType::get(Int32Ty, ArgTys, false);
6496 Value *ACast = Builder.CreateAddrSpaceCast(Arg3, I8PTy);
6497 // We know the third argument is an integer type, but we may need to cast
6498 // it to i32.
6499 if (Arg2->getType() != Int32Ty)
6500 Arg2 = Builder.CreateZExtOrTrunc(Arg2, Int32Ty);
6501 return RValue::get(
6502 EmitRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name),
6503 {Arg0, Arg1, Arg2, ACast, PacketSize, PacketAlign}));
6504 }
6505 }
6506 // OpenCL v2.0 s6.13.16 ,s9.17.3.5 - Built-in pipe reserve read and write
6507 // functions
6508 case Builtin::BIreserve_read_pipe:
6509 case Builtin::BIreserve_write_pipe:
6510 case Builtin::BIwork_group_reserve_read_pipe:
6511 case Builtin::BIwork_group_reserve_write_pipe:
6512 case Builtin::BIsub_group_reserve_read_pipe:
6513 case Builtin::BIsub_group_reserve_write_pipe: {
6514 // Composing the mangled name for the function.
6515 const char *Name;
6516 if (BuiltinID == Builtin::BIreserve_read_pipe)
6517 Name = "__reserve_read_pipe";
6518 else if (BuiltinID == Builtin::BIreserve_write_pipe)
6519 Name = "__reserve_write_pipe";
6520 else if (BuiltinID == Builtin::BIwork_group_reserve_read_pipe)
6521 Name = "__work_group_reserve_read_pipe";
6522 else if (BuiltinID == Builtin::BIwork_group_reserve_write_pipe)
6523 Name = "__work_group_reserve_write_pipe";
6524 else if (BuiltinID == Builtin::BIsub_group_reserve_read_pipe)
6525 Name = "__sub_group_reserve_read_pipe";
6526 else
6527 Name = "__sub_group_reserve_write_pipe";
6528
6529 Value *Arg0 = EmitScalarExpr(E->getArg(0)),
6530 *Arg1 = EmitScalarExpr(E->getArg(1));
6531 llvm::Type *ReservedIDTy = ConvertType(getContext().OCLReserveIDTy);
6532 CGOpenCLRuntime OpenCLRT(CGM);
6533 Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
6534 Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
6535
6536 // Building the generic function prototype.
6537 llvm::Type *ArgTys[] = {Arg0->getType(), Int32Ty, Int32Ty, Int32Ty};
6538 llvm::FunctionType *FTy =
6539 llvm::FunctionType::get(ReservedIDTy, ArgTys, false);
6540 // We know the second argument is an integer type, but we may need to cast
6541 // it to i32.
6542 if (Arg1->getType() != Int32Ty)
6543 Arg1 = Builder.CreateZExtOrTrunc(Arg1, Int32Ty);
6544 return RValue::get(EmitRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name),
6545 {Arg0, Arg1, PacketSize, PacketAlign}));
6546 }
6547 // OpenCL v2.0 s6.13.16, s9.17.3.5 - Built-in pipe commit read and write
6548 // functions
6549 case Builtin::BIcommit_read_pipe:
6550 case Builtin::BIcommit_write_pipe:
6551 case Builtin::BIwork_group_commit_read_pipe:
6552 case Builtin::BIwork_group_commit_write_pipe:
6553 case Builtin::BIsub_group_commit_read_pipe:
6554 case Builtin::BIsub_group_commit_write_pipe: {
6555 const char *Name;
6556 if (BuiltinID == Builtin::BIcommit_read_pipe)
6557 Name = "__commit_read_pipe";
6558 else if (BuiltinID == Builtin::BIcommit_write_pipe)
6559 Name = "__commit_write_pipe";
6560 else if (BuiltinID == Builtin::BIwork_group_commit_read_pipe)
6561 Name = "__work_group_commit_read_pipe";
6562 else if (BuiltinID == Builtin::BIwork_group_commit_write_pipe)
6563 Name = "__work_group_commit_write_pipe";
6564 else if (BuiltinID == Builtin::BIsub_group_commit_read_pipe)
6565 Name = "__sub_group_commit_read_pipe";
6566 else
6567 Name = "__sub_group_commit_write_pipe";
6568
6569 Value *Arg0 = EmitScalarExpr(E->getArg(0)),
6570 *Arg1 = EmitScalarExpr(E->getArg(1));
6571 CGOpenCLRuntime OpenCLRT(CGM);
6572 Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
6573 Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
6574
6575 // Building the generic function prototype.
6576 llvm::Type *ArgTys[] = {Arg0->getType(), Arg1->getType(), Int32Ty, Int32Ty};
6577 llvm::FunctionType *FTy = llvm::FunctionType::get(
6578 llvm::Type::getVoidTy(getLLVMContext()), ArgTys, false);
6579
6580 return RValue::get(EmitRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name),
6581 {Arg0, Arg1, PacketSize, PacketAlign}));
6582 }
6583 // OpenCL v2.0 s6.13.16.4 Built-in pipe query functions
6584 case Builtin::BIget_pipe_num_packets:
6585 case Builtin::BIget_pipe_max_packets: {
6586 const char *BaseName;
6587 const auto *PipeTy = E->getArg(0)->getType()->castAs<PipeType>();
6588 if (BuiltinID == Builtin::BIget_pipe_num_packets)
6589 BaseName = "__get_pipe_num_packets";
6590 else
6591 BaseName = "__get_pipe_max_packets";
6592 std::string Name = std::string(BaseName) +
6593 std::string(PipeTy->isReadOnly() ? "_ro" : "_wo");
6594
6595 // Building the generic function prototype.
6596 Value *Arg0 = EmitScalarExpr(E->getArg(0));
6597 CGOpenCLRuntime OpenCLRT(CGM);
6598 Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
6599 Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
6600 llvm::Type *ArgTys[] = {Arg0->getType(), Int32Ty, Int32Ty};
6601 llvm::FunctionType *FTy = llvm::FunctionType::get(Int32Ty, ArgTys, false);
6602
6603 return RValue::get(EmitRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name),
6604 {Arg0, PacketSize, PacketAlign}));
6605 }
6606
6607 // OpenCL v2.0 s6.13.9 - Address space qualifier functions.
6608 case Builtin::BIto_global:
6609 case Builtin::BIto_local:
6610 case Builtin::BIto_private: {
6611 auto Arg0 = EmitScalarExpr(E->getArg(0));
6612 auto NewArgT = llvm::PointerType::get(
6614 CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
6615 auto NewRetT = llvm::PointerType::get(
6617 CGM.getContext().getTargetAddressSpace(
6619 auto FTy = llvm::FunctionType::get(NewRetT, {NewArgT}, false);
6620 llvm::Value *NewArg;
6621 if (Arg0->getType()->getPointerAddressSpace() !=
6622 NewArgT->getPointerAddressSpace())
6623 NewArg = Builder.CreateAddrSpaceCast(Arg0, NewArgT);
6624 else
6625 NewArg = Builder.CreateBitOrPointerCast(Arg0, NewArgT);
6626 auto NewName = std::string("__") + E->getDirectCallee()->getName().str();
6627 auto NewCall =
6628 EmitRuntimeCall(CGM.CreateRuntimeFunction(FTy, NewName), {NewArg});
6629 return RValue::get(Builder.CreateBitOrPointerCast(NewCall,
6630 ConvertType(E->getType())));
6631 }
6632
6633 // OpenCL v2.0, s6.13.17 - Enqueue kernel function.
6634 // Table 6.13.17.1 specifies four overload forms of enqueue_kernel.
6635 // The code below expands the builtin call to a call to one of the following
6636 // functions that an OpenCL runtime library will have to provide:
6637 // __enqueue_kernel_basic
6638 // __enqueue_kernel_varargs
6639 // __enqueue_kernel_basic_events
6640 // __enqueue_kernel_events_varargs
6641 case Builtin::BIenqueue_kernel: {
6642 StringRef Name; // Generated function call name
6643 unsigned NumArgs = E->getNumArgs();
6644
6645 llvm::Type *QueueTy = ConvertType(getContext().OCLQueueTy);
6646 llvm::Type *GenericVoidPtrTy = Builder.getPtrTy(
6647 getContext().getTargetAddressSpace(LangAS::opencl_generic));
6648
6649 llvm::Value *Queue = EmitScalarExpr(E->getArg(0));
6650 llvm::Value *Flags = EmitScalarExpr(E->getArg(1));
6651 LValue NDRangeL = EmitAggExprToLValue(E->getArg(2));
6652 llvm::Value *Range = NDRangeL.getAddress().emitRawPointer(*this);
6653
6654 // FIXME: Look through the addrspacecast which may exist to the stack
6655 // temporary as a hack.
6656 //
6657 // This is hardcoding the assumed ABI of the target function. This assumes
6658 // direct passing for every argument except NDRange, which is assumed to be
6659 // byval or byref indirect passed.
6660 //
6661 // This should be fixed to query a signature from CGOpenCLRuntime, and go
6662 // through EmitCallArgs to get the correct target ABI.
6663 Range = Range->stripPointerCasts();
6664
6665 llvm::Type *RangePtrTy = Range->getType();
6666
6667 if (NumArgs == 4) {
6668 // The most basic form of the call with parameters:
6669 // queue_t, kernel_enqueue_flags_t, ndrange_t, block(void)
6670 Name = "__enqueue_kernel_basic";
6671 llvm::Type *ArgTys[] = {QueueTy, Int32Ty, RangePtrTy, GenericVoidPtrTy,
6672 GenericVoidPtrTy};
6673 llvm::FunctionType *FTy = llvm::FunctionType::get(Int32Ty, ArgTys, false);
6674
6675 auto Info =
6676 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(3));
6677 llvm::Value *Kernel =
6678 Builder.CreatePointerCast(Info.KernelHandle, GenericVoidPtrTy);
6679 llvm::Value *Block =
6680 Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
6681
6682 auto RTCall = EmitRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name),
6683 {Queue, Flags, Range, Kernel, Block});
6684 return RValue::get(RTCall);
6685 }
6686 assert(NumArgs >= 5 && "Invalid enqueue_kernel signature");
6687
6688 // Create a temporary array to hold the sizes of local pointer arguments
6689 // for the block. \p First is the position of the first size argument.
6690 auto CreateArrayForSizeVar =
6691 [=](unsigned First) -> std::pair<llvm::Value *, llvm::Value *> {
6692 llvm::APInt ArraySize(32, NumArgs - First);
6694 getContext().getSizeType(), ArraySize, nullptr,
6696 /*IndexTypeQuals=*/0);
6697 auto Tmp = CreateMemTemp(SizeArrayTy, "block_sizes");
6698 llvm::Value *TmpPtr = Tmp.getPointer();
6699 // The EmitLifetime* pair expect a naked Alloca as their last argument,
6700 // however for cases where the default AS is not the Alloca AS, Tmp is
6701 // actually the Alloca ascasted to the default AS, hence the
6702 // stripPointerCasts()
6703 llvm::Value *Alloca = TmpPtr->stripPointerCasts();
6704 llvm::Value *ElemPtr;
6705 EmitLifetimeStart(Alloca);
6706 // Each of the following arguments specifies the size of the corresponding
6707 // argument passed to the enqueued block.
6708 auto *Zero = llvm::ConstantInt::get(IntTy, 0);
6709 for (unsigned I = First; I < NumArgs; ++I) {
6710 auto *Index = llvm::ConstantInt::get(IntTy, I - First);
6711 auto *GEP =
6712 Builder.CreateGEP(Tmp.getElementType(), Alloca, {Zero, Index});
6713 if (I == First)
6714 ElemPtr = GEP;
6715 auto *V =
6716 Builder.CreateZExtOrTrunc(EmitScalarExpr(E->getArg(I)), SizeTy);
6717 Builder.CreateAlignedStore(
6718 V, GEP, CGM.getDataLayout().getPrefTypeAlign(SizeTy));
6719 }
6720 // Return the Alloca itself rather than a potential ascast as this is only
6721 // used by the paired EmitLifetimeEnd.
6722 return {ElemPtr, Alloca};
6723 };
6724
6725 // Could have events and/or varargs.
6726 if (E->getArg(3)->getType()->isBlockPointerType()) {
6727 // No events passed, but has variadic arguments.
6728 Name = "__enqueue_kernel_varargs";
6729 auto Info =
6730 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(3));
6731 llvm::Value *Kernel =
6732 Builder.CreatePointerCast(Info.KernelHandle, GenericVoidPtrTy);
6733 auto *Block = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
6734 auto [ElemPtr, TmpPtr] = CreateArrayForSizeVar(4);
6735
6736 // Create a vector of the arguments, as well as a constant value to
6737 // express to the runtime the number of variadic arguments.
6738 llvm::Value *const Args[] = {Queue, Flags,
6739 Range, Kernel,
6740 Block, ConstantInt::get(IntTy, NumArgs - 4),
6741 ElemPtr};
6742 llvm::Type *const ArgTys[] = {
6743 QueueTy, IntTy, RangePtrTy, GenericVoidPtrTy,
6744 GenericVoidPtrTy, IntTy, ElemPtr->getType()};
6745
6746 llvm::FunctionType *FTy = llvm::FunctionType::get(Int32Ty, ArgTys, false);
6747 auto Call = RValue::get(
6748 EmitRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name), Args));
6749 EmitLifetimeEnd(TmpPtr);
6750 return Call;
6751 }
6752 // Any calls now have event arguments passed.
6753 if (NumArgs >= 7) {
6754 llvm::PointerType *PtrTy = llvm::PointerType::get(
6755 CGM.getLLVMContext(),
6756 CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
6757
6758 llvm::Value *NumEvents =
6759 Builder.CreateZExtOrTrunc(EmitScalarExpr(E->getArg(3)), Int32Ty);
6760
6761 // Since SemaOpenCLBuiltinEnqueueKernel allows fifth and sixth arguments
6762 // to be a null pointer constant (including `0` literal), we can take it
6763 // into account and emit null pointer directly.
6764 llvm::Value *EventWaitList = nullptr;
6765 if (E->getArg(4)->isNullPointerConstant(
6767 EventWaitList = llvm::ConstantPointerNull::get(PtrTy);
6768 } else {
6769 EventWaitList =
6770 E->getArg(4)->getType()->isArrayType()
6772 : EmitScalarExpr(E->getArg(4));
6773 // Convert to generic address space.
6774 EventWaitList = Builder.CreatePointerCast(EventWaitList, PtrTy);
6775 }
6776 llvm::Value *EventRet = nullptr;
6777 if (E->getArg(5)->isNullPointerConstant(
6779 EventRet = llvm::ConstantPointerNull::get(PtrTy);
6780 } else {
6781 EventRet =
6782 Builder.CreatePointerCast(EmitScalarExpr(E->getArg(5)), PtrTy);
6783 }
6784
6785 auto Info =
6786 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(6));
6787 llvm::Value *Kernel =
6788 Builder.CreatePointerCast(Info.KernelHandle, GenericVoidPtrTy);
6789 llvm::Value *Block =
6790 Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
6791
6792 std::vector<llvm::Type *> ArgTys = {
6793 QueueTy, Int32Ty, RangePtrTy, Int32Ty,
6794 PtrTy, PtrTy, GenericVoidPtrTy, GenericVoidPtrTy};
6795
6796 std::vector<llvm::Value *> Args = {Queue, Flags, Range,
6797 NumEvents, EventWaitList, EventRet,
6798 Kernel, Block};
6799
6800 if (NumArgs == 7) {
6801 // Has events but no variadics.
6802 Name = "__enqueue_kernel_basic_events";
6803 llvm::FunctionType *FTy =
6804 llvm::FunctionType::get(Int32Ty, ArgTys, false);
6805 return RValue::get(
6806 EmitRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name), Args));
6807 }
6808 // Has event info and variadics
6809 // Pass the number of variadics to the runtime function too.
6810 Args.push_back(ConstantInt::get(Int32Ty, NumArgs - 7));
6811 ArgTys.push_back(Int32Ty);
6812 Name = "__enqueue_kernel_events_varargs";
6813
6814 auto [ElemPtr, TmpPtr] = CreateArrayForSizeVar(7);
6815 Args.push_back(ElemPtr);
6816 ArgTys.push_back(ElemPtr->getType());
6817
6818 llvm::FunctionType *FTy = llvm::FunctionType::get(Int32Ty, ArgTys, false);
6819 auto Call = RValue::get(
6820 EmitRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name), Args));
6821 EmitLifetimeEnd(TmpPtr);
6822 return Call;
6823 }
6824 llvm_unreachable("Unexpected enqueue_kernel signature");
6825 }
6826 // OpenCL v2.0 s6.13.17.6 - Kernel query functions need bitcast of block
6827 // parameter.
6828 case Builtin::BIget_kernel_work_group_size: {
6829 llvm::Type *GenericVoidPtrTy = Builder.getPtrTy(
6830 getContext().getTargetAddressSpace(LangAS::opencl_generic));
6831 auto Info =
6832 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(0));
6833 Value *Kernel =
6834 Builder.CreatePointerCast(Info.KernelHandle, GenericVoidPtrTy);
6835 Value *Arg = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
6837 CGM.CreateRuntimeFunction(
6838 llvm::FunctionType::get(IntTy, {GenericVoidPtrTy, GenericVoidPtrTy},
6839 false),
6840 "__get_kernel_work_group_size_impl"),
6841 {Kernel, Arg}));
6842 }
6843 case Builtin::BIget_kernel_preferred_work_group_size_multiple: {
6844 llvm::Type *GenericVoidPtrTy = Builder.getPtrTy(
6845 getContext().getTargetAddressSpace(LangAS::opencl_generic));
6846 auto Info =
6847 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(0));
6848 Value *Kernel =
6849 Builder.CreatePointerCast(Info.KernelHandle, GenericVoidPtrTy);
6850 Value *Arg = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
6852 CGM.CreateRuntimeFunction(
6853 llvm::FunctionType::get(IntTy, {GenericVoidPtrTy, GenericVoidPtrTy},
6854 false),
6855 "__get_kernel_preferred_work_group_size_multiple_impl"),
6856 {Kernel, Arg}));
6857 }
6858 case Builtin::BIget_kernel_max_sub_group_size_for_ndrange:
6859 case Builtin::BIget_kernel_sub_group_count_for_ndrange: {
6860 llvm::Type *GenericVoidPtrTy = Builder.getPtrTy(
6861 getContext().getTargetAddressSpace(LangAS::opencl_generic));
6862 LValue NDRangeL = EmitAggExprToLValue(E->getArg(0));
6863 llvm::Value *NDRange = NDRangeL.getAddress().emitRawPointer(*this);
6864 auto Info =
6865 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(1));
6866 Value *Kernel =
6867 Builder.CreatePointerCast(Info.KernelHandle, GenericVoidPtrTy);
6868 Value *Block = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
6869 const char *Name =
6870 BuiltinID == Builtin::BIget_kernel_max_sub_group_size_for_ndrange
6871 ? "__get_kernel_max_sub_group_size_for_ndrange_impl"
6872 : "__get_kernel_sub_group_count_for_ndrange_impl";
6874 CGM.CreateRuntimeFunction(
6875 llvm::FunctionType::get(
6876 IntTy, {NDRange->getType(), GenericVoidPtrTy, GenericVoidPtrTy},
6877 false),
6878 Name),
6879 {NDRange, Kernel, Block}));
6880 }
6881 case Builtin::BI__builtin_store_half:
6882 case Builtin::BI__builtin_store_halff: {
6883 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
6884 Value *Val = EmitScalarExpr(E->getArg(0));
6886 Value *HalfVal = Builder.CreateFPTrunc(Val, Builder.getHalfTy());
6887 Builder.CreateStore(HalfVal, Address);
6888 return RValue::get(nullptr);
6889 }
6890 case Builtin::BI__builtin_load_half: {
6892 Value *HalfVal = Builder.CreateLoad(Address);
6893 return RValue::get(Builder.CreateFPExt(HalfVal, Builder.getDoubleTy()));
6894 }
6895 case Builtin::BI__builtin_load_halff: {
6897 Value *HalfVal = Builder.CreateLoad(Address);
6898 return RValue::get(Builder.CreateFPExt(HalfVal, Builder.getFloatTy()));
6899 }
6900 case Builtin::BI__builtin_printf:
6901 case Builtin::BIprintf:
6902 if (getTarget().getTriple().isNVPTX() ||
6903 getTarget().getTriple().isAMDGCN() ||
6904 (getTarget().getTriple().isSPIRV() &&
6905 getTarget().getTriple().getVendor() == Triple::VendorType::AMD)) {
6906 if (getTarget().getTriple().isNVPTX())
6908 if ((getTarget().getTriple().isAMDGCN() ||
6909 getTarget().getTriple().isSPIRV()) &&
6910 getLangOpts().HIP)
6912 }
6913
6914 break;
6915 case Builtin::BI__builtin_canonicalize:
6916 case Builtin::BI__builtin_canonicalizef:
6917 case Builtin::BI__builtin_canonicalizef16:
6918 case Builtin::BI__builtin_canonicalizel:
6919 return RValue::get(
6920 emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::canonicalize));
6921
6922 case Builtin::BI__builtin_thread_pointer: {
6923 if (!getContext().getTargetInfo().isTLSSupported())
6924 CGM.ErrorUnsupported(E, "__builtin_thread_pointer");
6925
6926 return RValue::get(Builder.CreateIntrinsic(llvm::Intrinsic::thread_pointer,
6927 {GlobalsInt8PtrTy}, {}));
6928 }
6929 case Builtin::BI__builtin_os_log_format:
6930 return emitBuiltinOSLogFormat(*E);
6931
6932 case Builtin::BI__xray_customevent: {
6934 return RValue::getIgnored();
6935
6936 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
6938 return RValue::getIgnored();
6939
6940 if (const auto *XRayAttr = CurFuncDecl->getAttr<XRayInstrumentAttr>())
6941 if (XRayAttr->neverXRayInstrument() && !AlwaysEmitXRayCustomEvents())
6942 return RValue::getIgnored();
6943
6944 Function *F = CGM.getIntrinsic(Intrinsic::xray_customevent);
6945 auto FTy = F->getFunctionType();
6946 auto Arg0 = E->getArg(0);
6947 auto Arg0Val = EmitScalarExpr(Arg0);
6948 auto Arg0Ty = Arg0->getType();
6949 auto PTy0 = FTy->getParamType(0);
6950 if (PTy0 != Arg0Val->getType()) {
6951 if (Arg0Ty->isArrayType())
6952 Arg0Val = EmitArrayToPointerDecay(Arg0).emitRawPointer(*this);
6953 else
6954 Arg0Val = Builder.CreatePointerCast(Arg0Val, PTy0);
6955 }
6956 auto Arg1 = EmitScalarExpr(E->getArg(1));
6957 auto PTy1 = FTy->getParamType(1);
6958 if (PTy1 != Arg1->getType())
6959 Arg1 = Builder.CreateTruncOrBitCast(Arg1, PTy1);
6960 return RValue::get(Builder.CreateCall(F, {Arg0Val, Arg1}));
6961 }
6962
6963 case Builtin::BI__xray_typedevent: {
6964 // TODO: There should be a way to always emit events even if the current
6965 // function is not instrumented. Losing events in a stream can cripple
6966 // a trace.
6968 return RValue::getIgnored();
6969
6970 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
6972 return RValue::getIgnored();
6973
6974 if (const auto *XRayAttr = CurFuncDecl->getAttr<XRayInstrumentAttr>())
6975 if (XRayAttr->neverXRayInstrument() && !AlwaysEmitXRayTypedEvents())
6976 return RValue::getIgnored();
6977
6978 Function *F = CGM.getIntrinsic(Intrinsic::xray_typedevent);
6979 auto FTy = F->getFunctionType();
6980 auto Arg0 = EmitScalarExpr(E->getArg(0));
6981 auto PTy0 = FTy->getParamType(0);
6982 if (PTy0 != Arg0->getType())
6983 Arg0 = Builder.CreateTruncOrBitCast(Arg0, PTy0);
6984 auto Arg1 = E->getArg(1);
6985 auto Arg1Val = EmitScalarExpr(Arg1);
6986 auto Arg1Ty = Arg1->getType();
6987 auto PTy1 = FTy->getParamType(1);
6988 if (PTy1 != Arg1Val->getType()) {
6989 if (Arg1Ty->isArrayType())
6990 Arg1Val = EmitArrayToPointerDecay(Arg1).emitRawPointer(*this);
6991 else
6992 Arg1Val = Builder.CreatePointerCast(Arg1Val, PTy1);
6993 }
6994 auto Arg2 = EmitScalarExpr(E->getArg(2));
6995 auto PTy2 = FTy->getParamType(2);
6996 if (PTy2 != Arg2->getType())
6997 Arg2 = Builder.CreateTruncOrBitCast(Arg2, PTy2);
6998 return RValue::get(Builder.CreateCall(F, {Arg0, Arg1Val, Arg2}));
6999 }
7000
7001 case Builtin::BI__builtin_ms_va_start:
7002 case Builtin::BI__builtin_ms_va_end:
7003 return RValue::get(
7005 BuiltinID == Builtin::BI__builtin_ms_va_start));
7006
7007 case Builtin::BI__builtin_ms_va_copy: {
7008 // Lower this manually. We can't reliably determine whether or not any
7009 // given va_copy() is for a Win64 va_list from the calling convention
7010 // alone, because it's legal to do this from a System V ABI function.
7011 // With opaque pointer types, we won't have enough information in LLVM
7012 // IR to determine this from the argument types, either. Best to do it
7013 // now, while we have enough information.
7014 Address DestAddr = EmitMSVAListRef(E->getArg(0));
7015 Address SrcAddr = EmitMSVAListRef(E->getArg(1));
7016
7017 DestAddr = DestAddr.withElementType(Int8PtrTy);
7018 SrcAddr = SrcAddr.withElementType(Int8PtrTy);
7019
7020 Value *ArgPtr = Builder.CreateLoad(SrcAddr, "ap.val");
7021 return RValue::get(Builder.CreateStore(ArgPtr, DestAddr));
7022 }
7023
7024 case Builtin::BI__builtin_get_device_side_mangled_name: {
7025 auto Name = CGM.getCUDARuntime().getDeviceSideName(
7026 cast<DeclRefExpr>(E->getArg(0)->IgnoreImpCasts())->getDecl());
7027 auto Str = CGM.GetAddrOfConstantCString(Name, "");
7028 return RValue::get(Str.getPointer());
7029 }
7030 }
7031
7032 // If this is an alias for a lib function (e.g. __builtin_sin), emit
7033 // the call using the normal call path, but using the unmangled
7034 // version of the function name.
7035 const auto &BI = getContext().BuiltinInfo;
7036 if (!shouldEmitBuiltinAsIR(BuiltinID, BI, *this) &&
7037 BI.isLibFunction(BuiltinID))
7038 return emitLibraryCall(*this, FD, E,
7039 CGM.getBuiltinLibFunction(FD, BuiltinID));
7040
7041 // If this is a predefined lib function (e.g. malloc), emit the call
7042 // using exactly the normal call path.
7043 if (BI.isPredefinedLibFunction(BuiltinID))
7044 return emitLibraryCall(*this, FD, E, CGM.getRawFunctionPointer(FD));
7045
7046 // Check that a call to a target specific builtin has the correct target
7047 // features.
7048 // This is down here to avoid non-target specific builtins, however, if
7049 // generic builtins start to require generic target features then we
7050 // can move this up to the beginning of the function.
7051 checkTargetFeatures(E, FD);
7052
7053 if (unsigned VectorWidth = getContext().BuiltinInfo.getRequiredVectorWidth(BuiltinID))
7054 LargestVectorWidth = std::max(LargestVectorWidth, VectorWidth);
7055
7056 // See if we have a target specific intrinsic.
7057 std::string Name = getContext().BuiltinInfo.getName(BuiltinID);
7058 Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic;
7059 StringRef Prefix =
7060 llvm::Triple::getArchTypePrefix(getTarget().getTriple().getArch());
7061 if (!Prefix.empty()) {
7062 IntrinsicID = Intrinsic::getIntrinsicForClangBuiltin(Prefix.data(), Name);
7063 if (IntrinsicID == Intrinsic::not_intrinsic && Prefix == "spv" &&
7064 getTarget().getTriple().getOS() == llvm::Triple::OSType::AMDHSA)
7065 IntrinsicID = Intrinsic::getIntrinsicForClangBuiltin("amdgcn", Name);
7066 // NOTE we don't need to perform a compatibility flag check here since the
7067 // intrinsics are declared in Builtins*.def via LANGBUILTIN which filter the
7068 // MS builtins via ALL_MS_LANGUAGES and are filtered earlier.
7069 if (IntrinsicID == Intrinsic::not_intrinsic)
7070 IntrinsicID = Intrinsic::getIntrinsicForMSBuiltin(Prefix.data(), Name);
7071 }
7072
7073 if (IntrinsicID != Intrinsic::not_intrinsic) {
7075
7076 // Find out if any arguments are required to be integer constant
7077 // expressions.
7078 unsigned ICEArguments = 0;
7080 getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments);
7081 assert(Error == ASTContext::GE_None && "Should not codegen an error");
7082
7083 Function *F = CGM.getIntrinsic(IntrinsicID);
7084 llvm::FunctionType *FTy = F->getFunctionType();
7085
7086 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
7087 Value *ArgValue = EmitScalarOrConstFoldImmArg(ICEArguments, i, E);
7088 // If the intrinsic arg type is different from the builtin arg type
7089 // we need to do a bit cast.
7090 llvm::Type *PTy = FTy->getParamType(i);
7091 if (PTy != ArgValue->getType()) {
7092 // XXX - vector of pointers?
7093 if (auto *PtrTy = dyn_cast<llvm::PointerType>(PTy)) {
7094 if (PtrTy->getAddressSpace() !=
7095 ArgValue->getType()->getPointerAddressSpace()) {
7096 ArgValue = Builder.CreateAddrSpaceCast(
7097 ArgValue, llvm::PointerType::get(getLLVMContext(),
7098 PtrTy->getAddressSpace()));
7099 }
7100 }
7101
7102 // Cast vector type (e.g., v256i32) to x86_amx, this only happen
7103 // in amx intrinsics.
7104 if (PTy->isX86_AMXTy())
7105 ArgValue = Builder.CreateIntrinsic(Intrinsic::x86_cast_vector_to_tile,
7106 {ArgValue->getType()}, {ArgValue});
7107 else
7108 ArgValue = Builder.CreateBitCast(ArgValue, PTy);
7109 }
7110
7111 Args.push_back(ArgValue);
7112 }
7113
7114 Value *V = Builder.CreateCall(F, Args);
7115 QualType BuiltinRetType = E->getType();
7116
7117 llvm::Type *RetTy = VoidTy;
7118 if (!BuiltinRetType->isVoidType())
7119 RetTy = ConvertType(BuiltinRetType);
7120
7121 if (RetTy != V->getType()) {
7122 // XXX - vector of pointers?
7123 if (auto *PtrTy = dyn_cast<llvm::PointerType>(RetTy)) {
7124 if (PtrTy->getAddressSpace() != V->getType()->getPointerAddressSpace()) {
7125 V = Builder.CreateAddrSpaceCast(
7126 V, llvm::PointerType::get(getLLVMContext(),
7127 PtrTy->getAddressSpace()));
7128 }
7129 }
7130
7131 // Cast x86_amx to vector type (e.g., v256i32), this only happen
7132 // in amx intrinsics.
7133 if (V->getType()->isX86_AMXTy())
7134 V = Builder.CreateIntrinsic(Intrinsic::x86_cast_tile_to_vector, {RetTy},
7135 {V});
7136 else
7137 V = Builder.CreateBitCast(V, RetTy);
7138 }
7139
7140 if (RetTy->isVoidTy())
7141 return RValue::get(nullptr);
7142
7143 return RValue::get(V);
7144 }
7145
7146 // Some target-specific builtins can have aggregate return values, e.g.
7147 // __builtin_arm_mve_vld2q_u32. So if the result is an aggregate, force
7148 // ReturnValue to be non-null, so that the target-specific emission code can
7149 // always just emit into it.
7151 if (EvalKind == TEK_Aggregate && ReturnValue.isNull()) {
7152 Address DestPtr = CreateMemTemp(E->getType(), "agg.tmp");
7153 ReturnValue = ReturnValueSlot(DestPtr, false);
7154 }
7155
7156 // Now see if we can emit a target-specific builtin.
7157 if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E, ReturnValue)) {
7158 switch (EvalKind) {
7159 case TEK_Scalar:
7160 if (V->getType()->isVoidTy())
7161 return RValue::get(nullptr);
7162 return RValue::get(V);
7163 case TEK_Aggregate:
7164 return RValue::getAggregate(ReturnValue.getAddress(),
7165 ReturnValue.isVolatile());
7166 case TEK_Complex:
7167 llvm_unreachable("No current target builtin returns complex");
7168 }
7169 llvm_unreachable("Bad evaluation kind in EmitBuiltinExpr");
7170 }
7171
7172 // EmitHLSLBuiltinExpr will check getLangOpts().HLSL
7173 if (Value *V = EmitHLSLBuiltinExpr(BuiltinID, E, ReturnValue)) {
7174 switch (EvalKind) {
7175 case TEK_Scalar:
7176 if (V->getType()->isVoidTy())
7177 return RValue::get(nullptr);
7178 return RValue::get(V);
7179 case TEK_Aggregate:
7180 return RValue::getAggregate(ReturnValue.getAddress(),
7181 ReturnValue.isVolatile());
7182 case TEK_Complex:
7183 llvm_unreachable("No current hlsl builtin returns complex");
7184 }
7185 llvm_unreachable("Bad evaluation kind in EmitBuiltinExpr");
7186 }
7187
7188 if (getLangOpts().HIPStdPar && getLangOpts().CUDAIsDevice)
7189 return EmitHipStdParUnsupportedBuiltin(this, FD);
7190
7191 ErrorUnsupported(E, "builtin function");
7192
7193 // Unknown builtin, for now just dump it out and return undef.
7194 return GetUndefRValue(E->getType());
7195}
7196
7197namespace {
7198struct BuiltinAlignArgs {
7199 llvm::Value *Src = nullptr;
7200 llvm::Type *SrcType = nullptr;
7201 llvm::Value *Alignment = nullptr;
7202 llvm::Value *Mask = nullptr;
7203 llvm::IntegerType *IntType = nullptr;
7204
7205 BuiltinAlignArgs(const CallExpr *E, CodeGenFunction &CGF) {
7206 QualType AstType = E->getArg(0)->getType();
7207 if (AstType->isArrayType())
7208 Src = CGF.EmitArrayToPointerDecay(E->getArg(0)).emitRawPointer(CGF);
7209 else
7210 Src = CGF.EmitScalarExpr(E->getArg(0));
7211 SrcType = Src->getType();
7212 if (SrcType->isPointerTy()) {
7213 IntType = IntegerType::get(
7214 CGF.getLLVMContext(),
7215 CGF.CGM.getDataLayout().getIndexTypeSizeInBits(SrcType));
7216 } else {
7217 assert(SrcType->isIntegerTy());
7218 IntType = cast<llvm::IntegerType>(SrcType);
7219 }
7220 Alignment = CGF.EmitScalarExpr(E->getArg(1));
7221 Alignment = CGF.Builder.CreateZExtOrTrunc(Alignment, IntType, "alignment");
7222 auto *One = llvm::ConstantInt::get(IntType, 1);
7223 Mask = CGF.Builder.CreateSub(Alignment, One, "mask");
7224 }
7225};
7226} // namespace
7227
7228/// Generate (x & (y-1)) == 0.
7230 BuiltinAlignArgs Args(E, *this);
7231 llvm::Value *SrcAddress = Args.Src;
7232 if (Args.SrcType->isPointerTy())
7233 SrcAddress =
7234 Builder.CreateBitOrPointerCast(Args.Src, Args.IntType, "src_addr");
7235 return RValue::get(Builder.CreateICmpEQ(
7236 Builder.CreateAnd(SrcAddress, Args.Mask, "set_bits"),
7237 llvm::Constant::getNullValue(Args.IntType), "is_aligned"));
7238}
7239
7240/// Generate (x & ~(y-1)) to align down or ((x+(y-1)) & ~(y-1)) to align up.
7241/// Note: For pointer types we can avoid ptrtoint/inttoptr pairs by using the
7242/// llvm.ptrmask intrinsic (with a GEP before in the align_up case).
7244 BuiltinAlignArgs Args(E, *this);
7245 llvm::Value *SrcForMask = Args.Src;
7246 if (AlignUp) {
7247 // When aligning up we have to first add the mask to ensure we go over the
7248 // next alignment value and then align down to the next valid multiple.
7249 // By adding the mask, we ensure that align_up on an already aligned
7250 // value will not change the value.
7251 if (Args.Src->getType()->isPointerTy()) {
7252 if (getLangOpts().PointerOverflowDefined)
7253 SrcForMask =
7254 Builder.CreateGEP(Int8Ty, SrcForMask, Args.Mask, "over_boundary");
7255 else
7256 SrcForMask = EmitCheckedInBoundsGEP(Int8Ty, SrcForMask, Args.Mask,
7257 /*SignedIndices=*/true,
7258 /*isSubtraction=*/false,
7259 E->getExprLoc(), "over_boundary");
7260 } else {
7261 SrcForMask = Builder.CreateAdd(SrcForMask, Args.Mask, "over_boundary");
7262 }
7263 }
7264 // Invert the mask to only clear the lower bits.
7265 llvm::Value *InvertedMask = Builder.CreateNot(Args.Mask, "inverted_mask");
7266 llvm::Value *Result = nullptr;
7267 if (Args.Src->getType()->isPointerTy()) {
7268 Result = Builder.CreateIntrinsic(
7269 Intrinsic::ptrmask, {Args.SrcType, Args.IntType},
7270 {SrcForMask, InvertedMask}, nullptr, "aligned_result");
7271 } else {
7272 Result = Builder.CreateAnd(SrcForMask, InvertedMask, "aligned_result");
7273 }
7274 assert(Result->getType() == Args.SrcType);
7275 return RValue::get(Result);
7276}
#define V(N, I)
static char bitActionToX86BTCode(BitTest::ActionKind A)
static Value * EmitAtomicCmpXchg128ForMSIntrin(CodeGenFunction &CGF, const CallExpr *E, AtomicOrdering SuccessOrdering)
static void emitSincosBuiltin(CodeGenFunction &CGF, const CallExpr *E, Intrinsic::ID IntrinsicID)
static CanQualType getOSLogArgType(ASTContext &C, int Size)
Get the argument type for arguments to os_log_helper.
static Value * EmitOverflowCheckedAbs(CodeGenFunction &CGF, const CallExpr *E, bool SanitizeOverflow)
static llvm::Value * EmitBitCountExpr(CodeGenFunction &CGF, const Expr *E)
static Value * tryUseTestFPKind(CodeGenFunction &CGF, unsigned BuiltinID, Value *V)
static bool areBOSTypesCompatible(int From, int To)
Checks if using the result of __builtin_object_size(p, From) in place of __builtin_object_size(p,...
static std::pair< llvm::Value *, llvm::Value * > GetCountFieldAndIndex(CodeGenFunction &CGF, const MemberExpr *ME, const FieldDecl *ArrayFD, const FieldDecl *CountFD, const Expr *Idx, llvm::IntegerType *ResType, bool IsSigned)
Value * EmitFromInt(CodeGenFunction &CGF, llvm::Value *V, QualType T, llvm::Type *ResultType)
Value * MakeAtomicCmpXchgValue(CodeGenFunction &CGF, const CallExpr *E, bool ReturnBool, llvm::AtomicOrdering SuccessOrdering, llvm::AtomicOrdering FailureOrdering)
Utility to insert an atomic cmpxchg instruction.
static Value * EmitAtomicIncrementValue(CodeGenFunction &CGF, const CallExpr *E, AtomicOrdering Ordering=AtomicOrdering::SequentiallyConsistent)
static RValue EmitMSVCRTSetJmp(CodeGenFunction &CGF, MSVCSetJmpKind SJKind, const CallExpr *E)
MSVC handles setjmp a bit differently on different platforms.
#define MUTATE_LDBL(func)
static Value * emitMaybeConstrainedFPToIntRoundBuiltin(CodeGenFunction &CGF, const CallExpr *E, unsigned IntrinsicID, unsigned ConstrainedIntrinsicID)
static bool TypeRequiresBuiltinLaunder(CodeGenModule &CGM, QualType Ty)
Determine if the specified type requires laundering by checking if it is a dynamic class type or cont...
static Value * EmitISOVolatileLoad(CodeGenFunction &CGF, const CallExpr *E)
static Value * EmitTargetArchBuiltinExpr(CodeGenFunction *CGF, unsigned BuiltinID, const CallExpr *E, ReturnValueSlot ReturnValue, llvm::Triple::ArchType Arch)
Definition CGBuiltin.cpp:74
static RValue EmitBinaryAtomicPost(CodeGenFunction &CGF, llvm::AtomicRMWInst::BinOp Kind, const CallExpr *E, Instruction::BinaryOps Op, bool Invert=false)
Utility to insert an atomic instruction based Intrinsic::ID and the expression node,...
static bool HasNoIndirectArgumentsOrResults(CGFunctionInfo const &FnInfo)
Checks no arguments or results are passed indirectly in the ABI (i.e.
Value * EmitToInt(CodeGenFunction &CGF, llvm::Value *V, QualType T, llvm::IntegerType *IntType)
Emit the conversions required to turn the given value into an integer of the given size.
static llvm::Value * EmitBitTestIntrinsic(CodeGenFunction &CGF, unsigned BuiltinID, const CallExpr *E)
Emit a _bittest* intrinsic.
static Value * EmitSignBit(CodeGenFunction &CGF, Value *V)
Emit the computation of the sign bit for a floating point value.
static Value * EmitFAbs(CodeGenFunction &CGF, Value *V)
EmitFAbs - Emit a call to @llvm.fabs().
static llvm::Value * EmitPositiveResultOrZero(CodeGenFunction &CGF, llvm::Value *Res, llvm::Value *Index, llvm::IntegerType *ResType, bool IsSigned)
static bool shouldEmitBuiltinAsIR(unsigned BuiltinID, const Builtin::Context &BI, const CodeGenFunction &CGF)
Some builtins do not have library implementation on some targets and are instead emitted as LLVM IRs ...
Definition CGBuiltin.cpp:50
static bool isSpecialUnsignedMultiplySignedResult(unsigned BuiltinID, WidthAndSignedness Op1Info, WidthAndSignedness Op2Info, WidthAndSignedness ResultInfo)
static llvm::Value * getDefaultBuiltinObjectSizeResult(unsigned Type, llvm::IntegerType *ResType)
static Value * emitTernaryMaybeConstrainedFPBuiltin(CodeGenFunction &CGF, const CallExpr *E, unsigned IntrinsicID, unsigned ConstrainedIntrinsicID)
static RValue EmitCheckedMixedSignMultiply(CodeGenFunction &CGF, const clang::Expr *Op1, WidthAndSignedness Op1Info, const clang::Expr *Op2, WidthAndSignedness Op2Info, const clang::Expr *ResultArg, QualType ResultQTy, WidthAndSignedness ResultInfo)
Emit a checked mixed-sign multiply.
static unsigned mutateLongDoubleBuiltin(unsigned BuiltinID)
static RValue EmitBinaryAtomic(CodeGenFunction &CGF, llvm::AtomicRMWInst::BinOp Kind, const CallExpr *E)
static void initializeAlloca(CodeGenFunction &CGF, AllocaInst *AI, Value *Size, Align AlignmentInBytes)
static Value * EmitAtomicCmpXchgForMSIntrin(CodeGenFunction &CGF, const CallExpr *E, AtomicOrdering SuccessOrdering=AtomicOrdering::SequentiallyConsistent)
This function should be invoked to emit atomic cmpxchg for Microsoft's _InterlockedCompareExchange* i...
static bool isSpecialMixedSignMultiply(unsigned BuiltinID, WidthAndSignedness Op1Info, WidthAndSignedness Op2Info, WidthAndSignedness ResultInfo)
Determine if a binop is a checked mixed-sign multiply we can specialize.
static Value * emitFrexpBuiltin(CodeGenFunction &CGF, const CallExpr *E, Intrinsic::ID IntrinsicID)
static llvm::Value * emitModfBuiltin(CodeGenFunction &CGF, const CallExpr *E, Intrinsic::ID IntrinsicID)
static Value * EmitNontemporalStore(CodeGenFunction &CGF, const CallExpr *E)
static const FieldDecl * FindFlexibleArrayMemberField(CodeGenFunction &CGF, ASTContext &Ctx, const RecordDecl *RD)
Find a struct's flexible array member.
static Value * EmitISOVolatileStore(CodeGenFunction &CGF, const CallExpr *E)
static RValue EmitHipStdParUnsupportedBuiltin(CodeGenFunction *CGF, const FunctionDecl *FD)
static llvm::Value * EmitX86BitTestIntrinsic(CodeGenFunction &CGF, BitTest BT, const CallExpr *E, Value *BitBase, Value *BitPos)
static RValue EmitCheckedUnsignedMultiplySignedResult(CodeGenFunction &CGF, const clang::Expr *Op1, WidthAndSignedness Op1Info, const clang::Expr *Op2, WidthAndSignedness Op2Info, const clang::Expr *ResultArg, QualType ResultQTy, WidthAndSignedness ResultInfo)
Address CheckAtomicAlignment(CodeGenFunction &CGF, const CallExpr *E)
static Value * EmitNontemporalLoad(CodeGenFunction &CGF, const CallExpr *E)
static llvm::AtomicOrdering getBitTestAtomicOrdering(BitTest::InterlockingKind I)
static bool GetFieldOffset(ASTContext &Ctx, const RecordDecl *RD, const FieldDecl *FD, int64_t &Offset)
Calculate the offset of a struct field.
Value * MakeBinaryAtomicValue(CodeGenFunction &CGF, llvm::AtomicRMWInst::BinOp Kind, const CallExpr *E, AtomicOrdering Ordering)
Utility to insert an atomic instruction based on Intrinsic::ID and the expression node.
llvm::Value * EmitOverflowIntrinsic(CodeGenFunction &CGF, const Intrinsic::ID IntrinsicID, llvm::Value *X, llvm::Value *Y, llvm::Value *&Carry)
Emit a call to llvm.
static Value * EmitAbs(CodeGenFunction &CGF, Value *ArgValue, bool HasNSW)
static Value * EmitAtomicDecrementValue(CodeGenFunction &CGF, const CallExpr *E, AtomicOrdering Ordering=AtomicOrdering::SequentiallyConsistent)
llvm::Value * emitBuiltinWithOneOverloadedType(clang::CodeGen::CodeGenFunction &CGF, const clang::CallExpr *E, unsigned IntrinsicID, llvm::StringRef Name="")
Definition CGBuiltin.h:63
static mlir::Value emitBinaryExpMaybeConstrainedFPBuiltin(CIRGenFunction &cgf, const CallExpr *e, llvm::StringRef intrinsicName, llvm::StringRef constrainedIntrinsicName)
static mlir::Value emitBinaryMaybeConstrainedFPBuiltin(CIRGenFunction &cgf, const CallExpr &e)
static RValue emitUnaryMaybeConstrainedFPBuiltin(CIRGenFunction &cgf, const CallExpr &e)
static bool shouldEmitBuiltinAsIR(unsigned builtinID, const Builtin::Context &bi, const CIRGenFunction &cgf)
static RValue emitLibraryCall(CIRGenFunction &cgf, const FunctionDecl *fd, const CallExpr *e, mlir::Operation *calleeValue)
static WidthAndSignedness getIntegerWidthAndSignedness(const clang::ASTContext &astContext, const clang::QualType type)
static struct WidthAndSignedness EncompassingIntegerType(ArrayRef< struct WidthAndSignedness > types)
TokenType getType() const
Returns the token's type, e.g.
FormatToken * Next
The next token in the unwrapped line.
Result
Implement __builtin_bit_cast and related operations.
#define X(type, name)
Definition Value.h:97
static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target)
llvm::MachO::Record Record
Definition MachO.h:31
static StringRef getTriple(const Command &Job)
SanitizerHandler
static QualType getPointeeType(const MemRegion *R)
__DEVICE__ float modf(float __x, float *__iptr)
__DEVICE__ double nan(const char *)
APSInt & getInt()
Definition APValue.h:508
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
unsigned getIntWidth(QualType T) const
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
CanQualType VoidPtrTy
IdentifierTable & Idents
Definition ASTContext.h:804
Builtin::Context & BuiltinInfo
Definition ASTContext.h:806
QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, const Expr *SizeExpr, ArraySizeModifier ASM, unsigned IndexTypeQuals) const
Return the unique reference to the type for a constant array of the specified element type.
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CanQualType VoidTy
QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error, unsigned *IntegerConstantArgs=nullptr) const
Return the type for the specified builtin.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:923
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
unsigned getTargetAddressSpace(LangAS AS) const
static bool hasSameUnqualifiedType(QualType T1, QualType T2)
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
@ GE_None
No error.
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
bool hasOwnVFPtr() const
hasOwnVFPtr - Does this class provide its own virtual-function table pointer, rather than inheriting ...
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
QualType getElementType() const
Definition TypeBase.h:3796
static std::unique_ptr< AtomicScopeModel > create(AtomicScopeModelKind K)
Create an atomic scope model by AtomicScopeModelKind.
Definition SyncScope.h:298
static bool isCommaOp(Opcode Opc)
Definition Expr.h:4144
Expr * getRHS() const
Definition Expr.h:4093
Holds information about both target-independent and target-specific builtins, allowing easy queries b...
Definition Builtins.h:236
bool shouldGenerateFPMathIntrinsic(unsigned BuiltinID, llvm::Triple Trip, std::optional< bool > ErrnoOverwritten, bool MathErrnoEnabled, bool HasOptNoneAttr, bool IsOptimizationEnabled) const
Determine whether we can generate LLVM intrinsics for the given builtin ID, based on whether it has s...
Definition Builtins.cpp:228
bool isConstWithoutErrnoAndExceptions(unsigned ID) const
Return true if this function has no side effects and doesn't read memory, except for possibly errno o...
Definition Builtins.h:413
std::string getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
Definition Builtins.cpp:80
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2946
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3150
bool hasStoredFPFeatures() const
Definition Expr.h:3105
SourceLocation getBeginLoc() const
Definition Expr.h:3280
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3129
Expr * getCallee()
Definition Expr.h:3093
FPOptionsOverride getFPFeatures() const
Definition Expr.h:3245
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition Expr.h:3137
arg_range arguments()
Definition Expr.h:3198
CastKind getCastKind() const
Definition Expr.h:3723
Expr * getSubExpr()
Definition Expr.h:3729
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
CharUnits alignmentAtOffset(CharUnits offset) const
Given that this is a non-zero alignment value, what is the alignment at the given offset?
Definition CharUnits.h:207
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition CharUnits.h:122
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition CharUnits.h:189
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition CharUnits.h:58
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
CharUnits alignTo(const CharUnits &Align) const
alignTo - Returns the next integer (mod 2**64) that is greater than or equal to this quantity and is ...
Definition CharUnits.h:201
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
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
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
Address withAlignment(CharUnits NewAlignment) const
Return address with different alignment, but same pointer and element type.
Definition Address.h:269
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition Address.h:204
A scoped helper to set the current debug location to the specified location or preferred location of ...
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF)
Apply TemporaryLocation if it is valid.
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF)
Set the IRBuilder to not attach debug locations.
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition CGBuilder.h:146
llvm::StoreInst * CreateAlignedStore(llvm::Value *Val, llvm::Value *Addr, CharUnits Align, bool IsVolatile=false)
Definition CGBuilder.h:153
llvm::AtomicRMWInst * CreateAtomicRMW(llvm::AtomicRMWInst::BinOp Op, Address Addr, llvm::Value *Val, llvm::AtomicOrdering Ordering, llvm::SyncScope::ID SSID=llvm::SyncScope::System)
Definition CGBuilder.h:190
llvm::CallInst * CreateMemSet(Address Dest, llvm::Value *Value, llvm::Value *Size, bool IsVolatile=false)
Definition CGBuilder.h:430
llvm::AtomicCmpXchgInst * CreateAtomicCmpXchg(Address Addr, llvm::Value *Cmp, llvm::Value *New, llvm::AtomicOrdering SuccessOrdering, llvm::AtomicOrdering FailureOrdering, llvm::SyncScope::ID SSID=llvm::SyncScope::System)
Definition CGBuilder.h:179
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition CGBuilder.h:118
llvm::LoadInst * CreateAlignedLoad(llvm::Type *Ty, llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition CGBuilder.h:138
Address CreateInBoundsGEP(Address Addr, ArrayRef< llvm::Value * > IdxList, llvm::Type *ElementType, CharUnits Align, const Twine &Name="")
Definition CGBuilder.h:356
All available information about a concrete callee.
Definition CGCall.h:64
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition CGCall.h:138
llvm::DILocation * CreateTrapFailureMessageFor(llvm::DebugLoc TrapLocation, StringRef Category, StringRef FailureMsg)
Create a debug location from TrapLocation that adds an artificial inline frame where the frame name i...
CGFunctionInfo - Class to encapsulate the information about a function definition.
MutableArrayRef< ArgInfo > arguments()
llvm::Value * getPipeElemAlign(const Expr *PipeArg)
llvm::Value * getPipeElemSize(const Expr *PipeArg)
llvm::StructType * getLLVMType() const
Return the "complete object" LLVM type associated with this record.
CallArgList - Type for representing both the value and type of arguments in a call.
Definition CGCall.h:275
void add(RValue rvalue, QualType type)
Definition CGCall.h:303
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
RValue EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E)
llvm::Value * GetVTablePtr(Address This, llvm::Type *VTableTy, const CXXRecordDecl *VTableClass, VTableAuthMode AuthMode=VTableAuthMode::Authenticate)
GetVTablePtr - Return the Value of the vtable pointer member pointed to by This.
Definition CGClass.cpp:2812
RValue EmitNVPTXDevicePrintfCallExpr(const CallExpr *E)
RValue EmitCoroutineIntrinsic(const CallExpr *E, unsigned int IID)
llvm::Value * performAddrSpaceCast(llvm::Value *Src, llvm::Type *DestTy)
llvm::Value * EmitScalarOrConstFoldImmArg(unsigned ICEArguments, unsigned Idx, const CallExpr *E)
SanitizerSet SanOpts
Sanitizers enabled for this function.
void checkTargetFeatures(const CallExpr *E, const FunctionDecl *TargetDecl)
llvm::Value * GetCountedByFieldExprGEP(const Expr *Base, const FieldDecl *FD, const FieldDecl *CountDecl)
Definition CGExpr.cpp:1199
llvm::Type * ConvertType(QualType T)
void addInstToNewSourceAtom(llvm::Instruction *KeyInstruction, llvm::Value *Backup)
Add KeyInstruction and an optional Backup instruction to a new atom group (See ApplyAtomGroup for mor...
BuiltinCheckKind
Specifies which type of sanitizer check to apply when handling a particular builtin.
llvm::Value * EmitSystemZBuiltinExpr(unsigned BuiltinID, const CallExpr *E)
Definition SystemZ.cpp:39
llvm::CallBase * EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, ArrayRef< llvm::Value * > args, const Twine &name="")
Emits a call or invoke instruction to the given runtime function.
Definition CGCall.cpp:5292
llvm::Value * EmitSEHAbnormalTermination()
RValue emitStdcFirstBit(const CallExpr *E, llvm::Intrinsic::ID IntID, bool InvertArg)
llvm::Value * EmitARCRetain(QualType type, llvm::Value *value)
Produce the code to do a retain.
Definition CGObjC.cpp:2360
CleanupKind getARCCleanupKind()
Retrieves the default cleanup kind for an ARC cleanup.
llvm::Value * EmitVAStartEnd(llvm::Value *ArgValue, bool IsStart)
Emits a call to an LLVM variable-argument intrinsic, either llvm.va_start or llvm....
RValue emitStdcBitWidthMinus(const CallExpr *E, llvm::Intrinsic::ID IntID, bool IsPop)
llvm::Value * EmitAMDGPUBuiltinExpr(unsigned BuiltinID, const CallExpr *E)
Definition AMDGPU.cpp:518
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:4045
void SetSqrtFPAccuracy(llvm::Value *Val)
Set the minimum required accuracy of the given sqrt operation based on CodeGenOpts.
Definition CGExpr.cpp:7225
RValue emitBuiltinOSLogFormat(const CallExpr &E)
Emit IR for __builtin_os_log_format.
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
llvm::Function * generateBuiltinOSLogHelperFunction(const analyze_os_log::OSLogBufferLayout &Layout, CharUnits BufferAlignment)
const LangOptions & getLangOpts() const
LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
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.
TypeCheckKind
Situations in which we might emit a check for the suitability of a pointer or glvalue.
@ TCK_Store
Checking the destination of a store. Must be suitably sized and aligned.
@ TCK_Load
Checking the operand of a load. Must be suitably sized and aligned.
llvm::Value * EmitRISCVBuiltinExpr(unsigned BuiltinID, const CallExpr *E, ReturnValueSlot ReturnValue)
Definition RISCV.cpp:1079
llvm::Value * EmitCheckedArgForBuiltin(const Expr *E, BuiltinCheckKind Kind)
Emits an argument for a call to a builtin.
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:3935
void EmitNonNullArgCheck(RValue RV, QualType ArgType, SourceLocation ArgLoc, AbstractCallee AC, unsigned ParmNum)
Create a check for a function parameter that may potentially be declared as non-null.
Definition CGCall.cpp:4764
const TargetInfo & getTarget() const
RValue emitRotate(const CallExpr *E, bool IsRotateRight)
llvm::Value * EmitAnnotationCall(llvm::Function *AnnotationFn, llvm::Value *AnnotatedVal, StringRef AnnotationStr, SourceLocation Location, const AnnotateAttr *Attr)
Emit an annotation call (intrinsic).
llvm::Value * EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E, ReturnValueSlot ReturnValue, llvm::Triple::ArchType Arch)
Definition ARM.cpp:2164
CGCallee EmitCallee(const Expr *E)
Definition CGExpr.cpp:6581
void pushCleanupAfterFullExpr(CleanupKind Kind, As... A)
Queue a cleanup to be pushed after finishing the current full-expression, potentially with an active ...
llvm::Value * EmitBPFBuiltinExpr(unsigned BuiltinID, const CallExpr *E)
Definition ARM.cpp:7206
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.
LValue EmitAggExprToLValue(const Expr *E)
EmitAggExprToLValue - Emit the computation of the specified expression of aggregate type into a tempo...
llvm::Value * EvaluateExprAsBool(const Expr *E)
EvaluateExprAsBool - Perform the usual unary conversions on the specified expression and compare the ...
Definition CGExpr.cpp:238
llvm::Value * EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E)
Definition PPC.cpp:205
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:4193
bool AlwaysEmitXRayTypedEvents() const
AlwaysEmitXRayTypedEvents - Return true if clang must unconditionally emit XRay typed event handling ...
llvm::Value * getTypeSize(QualType Ty)
Returns calculated size of the specified type.
bool EmitLifetimeStart(llvm::Value *Addr)
Emit a lifetime.begin marker if some criteria are satisfied.
Definition CGDecl.cpp:1357
llvm::MDNode * buildAllocToken(QualType AllocType)
Build metadata used by the AllocToken instrumentation.
Definition CGExpr.cpp:1322
llvm::Value * EmitToMemory(llvm::Value *Value, QualType Ty)
EmitToMemory - Change a scalar value from its value representation to its in-memory representation.
Definition CGExpr.cpp:2243
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type,...
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:5448
const TargetCodeGenInfo & getTargetHooks() const
RValue EmitBuiltinAlignTo(const CallExpr *E, bool AlignUp)
Emit IR for __builtin_align_up/__builtin_align_down.
void EmitLifetimeEnd(llvm::Value *Addr)
Definition CGDecl.cpp:1369
llvm::Value * EmitWebAssemblyBuiltinExpr(unsigned BuiltinID, const CallExpr *E)
bool IsInPreservedAIRegion
True if CodeGen currently emits code inside presereved access index region.
llvm::Value * EmitDirectXBuiltinExpr(unsigned BuiltinID, const CallExpr *E)
Definition DirectX.cpp:22
llvm::Value * EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E, llvm::Triple::ArchType Arch)
Definition ARM.cpp:4495
llvm::Value * EmitMSVCBuiltinExpr(MSVCIntrin BuiltinID, const CallExpr *E)
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
Address EmitArrayToPointerDecay(const Expr *Array, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
Definition CGExpr.cpp:4625
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
Definition CGDecl.cpp:2352
llvm::Value * EmitSPIRVBuiltinExpr(unsigned BuiltinID, const CallExpr *E)
Definition SPIR.cpp:22
RValue EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, const CallExpr *E, ReturnValueSlot ReturnValue)
Address EmitVAListRef(const Expr *E)
RValue GetUndefRValue(QualType Ty)
GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
Definition CGExpr.cpp:1614
RValue EmitBuiltinIsAligned(const CallExpr *E)
Emit IR for __builtin_is_aligned.
RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type, const CallExpr *TheCallExpr, bool IsDelete)
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
llvm::Value * EmitHexagonBuiltinExpr(unsigned BuiltinID, const CallExpr *E)
Definition Hexagon.cpp:77
CodeGenTypes & getTypes() const
RValue emitStdcCountIntrinsic(const CallExpr *E, llvm::Intrinsic::ID IntID, bool InvertArg, bool IsPop=false)
llvm::Value * EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E)
Definition X86.cpp:793
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, LValue LV, QualType Type, SanitizerSet SkippedChecks=SanitizerSet(), llvm::Value *ArraySize=nullptr)
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:1597
RawAddress CreateMemTemp(QualType T, const Twine &Name="tmp", RawAddress *Alloca=nullptr)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen and cas...
Definition CGExpr.cpp:194
llvm::Value * EmitCheckedInBoundsGEP(llvm::Type *ElemTy, llvm::Value *Ptr, ArrayRef< llvm::Value * > IdxList, bool SignedIndices, bool IsSubtraction, SourceLocation Loc, const Twine &Name="")
Same as IRBuilder::CreateInBoundsGEP, but additionally emits a check to detect undefined behavior whe...
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,...
llvm::CallInst * EmitTrapCall(llvm::Intrinsic::ID IntrID)
Emit a call to trap or debugtrap and attach function attribute "trap-func-name" if specified.
Definition CGExpr.cpp:4610
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void EmitTrapCheck(llvm::Value *Checked, SanitizerHandler CheckHandlerID, bool NoMerge=false, const TrapReason *TR=nullptr)
Create a basic block that will call the trap intrinsic, and emit a conditional branch to it,...
Definition CGExpr.cpp:4532
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
llvm::Value * EmitFromMemory(llvm::Value *Value, QualType Ty)
EmitFromMemory - Change a scalar value from its memory representation to its value representation.
Definition CGExpr.cpp:2277
llvm::Value * EmitCheckedArgForAssume(const Expr *E)
Emits an argument for a call to a __builtin_assume.
llvm::Value * EmitLoadOfCountedByField(const Expr *Base, const FieldDecl *FD, const FieldDecl *CountDecl)
Build an expression accessing the "counted_by" field.
Definition CGExpr.cpp:1253
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
llvm::Value * EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E)
Definition NVPTX.cpp:446
void EmitUnreachable(SourceLocation Loc)
Emit a reached-unreachable diagnostic if Loc is valid and runtime checking is enabled.
Definition CGExpr.cpp:4520
void ErrorUnsupported(const Stmt *S, const char *Type)
ErrorUnsupported - Print out an error that codegen doesn't support the specified stmt yet.
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
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:1713
bool ShouldXRayInstrumentFunction() const
ShouldXRayInstrument - Return true if the current function should be instrumented with XRay nop sleds...
llvm::LLVMContext & getLLVMContext()
llvm::Value * EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E, ReturnValueSlot ReturnValue)
EmitTargetBuiltinExpr - Emit the given builtin call.
void emitAlignmentAssumption(llvm::Value *PtrValue, QualType Ty, SourceLocation Loc, SourceLocation AssumptionLoc, llvm::Value *Alignment, llvm::Value *OffsetValue=nullptr)
llvm::Value * EmitHLSLBuiltinExpr(unsigned BuiltinID, const CallExpr *E, ReturnValueSlot ReturnValue)
void EmitARCIntrinsicUse(ArrayRef< llvm::Value * > values)
Given a number of pointers, inform the optimizer that they're being intrinsically used up until this ...
Definition CGObjC.cpp:2199
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource Source=AlignmentSource::Type, bool isInit=false, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition CGStmt.cpp:643
This class organizes the cross-function state that is used while generating LLVM code.
llvm::Module & getModule() const
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
llvm::Constant * getBuiltinLibFunction(const FunctionDecl *FD, unsigned BuiltinID)
Given a builtin id for a function like "__builtin_fabsf", return a Function* for "fabsf".
DiagnosticsEngine & getDiags() const
const LangOptions & getLangOpts() const
const TargetInfo & getTarget() const
const llvm::DataLayout & getDataLayout() const
const llvm::Triple & getTriple() const
void DecorateInstructionWithTBAA(llvm::Instruction *Inst, TBAAAccessInfo TBAAInfo)
DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
TBAAAccessInfo getTBAAAccessInfo(QualType AccessType)
getTBAAAccessInfo - Get TBAA information that describes an access to an object of the given type.
ASTContext & getContext() const
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
StringRef getMangledName(GlobalDecl GD)
llvm::LLVMContext & getLLVMContext()
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys={})
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition CGCall.cpp:1873
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
llvm::Constant * emitAbstract(const Expr *E, QualType T)
Emit the result of the given expression as an abstract constant, asserting that it succeeded.
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition CGCall.h:376
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
static RValue getIgnored()
Definition CGValue.h:94
static RValue get(llvm::Value *V)
Definition CGValue.h:99
static RValue getAggregate(Address addr, bool isVolatile=false)
Convert an Address to an RValue.
Definition CGValue.h:126
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
Definition CGValue.h:109
An abstract representation of an aligned address.
Definition Address.h:42
static RawAddress invalid()
Definition Address.h:61
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition CGCall.h:382
virtual bool supportsLibCall() const
supportsLibCall - Query to whether or not target supports all lib calls.
Definition TargetInfo.h:79
virtual llvm::Value * encodeReturnAddress(CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const
Performs the code-generation required to convert the address of an instruction into a return address ...
Definition TargetInfo.h:177
virtual llvm::Value * decodeReturnAddress(CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const
Performs the code-generation required to convert a return address as stored by the system into the ac...
Definition TargetInfo.h:167
virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const
Determines the DWARF register number for the stack pointer, for exception-handling purposes.
Definition TargetInfo.h:149
virtual llvm::Value * testFPKind(llvm::Value *V, unsigned BuiltinID, CGBuilderTy &Builder, CodeGenModule &CGM) const
Performs a target specific test of a floating point value for things like IsNaN, Infinity,...
Definition TargetInfo.h:186
Complex values, per C99 6.2.5p11.
Definition TypeBase.h:3337
QualType getElementType() const
Definition TypeBase.h:3347
llvm::APInt getSize() const
Return the constant array size as an APInt.
Definition TypeBase.h:3878
Represents a concrete matrix type with constant number of rows and columns.
Definition TypeBase.h:4449
Represents a sugar type with __counted_by or __sized_by annotations, including their _or_null variant...
Definition TypeBase.h:3498
DynamicCountPointerKind getKind() const
Definition TypeBase.h:3528
static bool isFlexibleArrayMemberLike(const ASTContext &Context, const Decl *D, QualType Ty, LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel, bool IgnoreTemplateOrMacroSubstitution)
Whether it resembles a flexible array member.
Definition DeclBase.cpp:460
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition DeclBase.h:601
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition DeclBase.cpp:273
bool hasAttr() const
Definition DeclBase.h:585
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
This represents one expression.
Definition Expr.h:112
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,...
Expr * IgnoreParenNoopCasts(const ASTContext &Ctx) LLVM_READONLY
Skip past any parentheses and casts which do not change the value (including ptr->int casts of the sa...
Definition Expr.cpp:3124
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition Expr.cpp:3102
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition Expr.cpp:3097
bool EvaluateAsFloat(llvm::APFloat &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsFloat - Return true if this is a constant which we can fold and convert to a floating point...
bool isPRValue() const
Definition Expr.h:285
@ NPC_ValueDependentIsNotNull
Specifies that a value-dependent expression should be considered to never be a null pointer constant.
Definition Expr.h:838
bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsRValue - Return true if this is a constant which we can fold to an rvalue using any crazy t...
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
Definition Expr.cpp:3695
std::optional< std::string > tryEvaluateString(ASTContext &Ctx) const
If the current Expr can be evaluated to a pointer to a null-terminated constant string,...
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3077
NullPointerConstantKind isNullPointerConstant(ASTContext &Ctx, NullPointerConstantValueDependence NPC) const
isNullPointerConstant - C99 6.3.2.3p3 - Test if this reduces down to a Null pointer constant.
Definition Expr.cpp:4075
std::optional< uint64_t > tryEvaluateObjectSize(const ASTContext &Ctx, unsigned Type) const
If the current Expr is a pointer, this will try to statically determine the number of bytes available...
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:282
QualType getType() const
Definition Expr.h:144
const ValueDecl * getAsBuiltinConstantDeclRef(const ASTContext &Context) const
If this expression is an unambiguous reference to a single declaration, in the style of __builtin_fun...
Definition Expr.cpp:231
Represents difference between two FPOptions values.
LangOptions::FPExceptionModeKind getExceptionMode() const
Represents a member of a struct/union/class.
Definition Decl.h:3166
const FieldDecl * findCountedByField() const
Find the FieldDecl specified in a FAM's "counted_by" attribute.
Definition Decl.cpp:4876
Represents a function declaration or definition.
Definition Decl.h:2018
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2789
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition Decl.cpp:3737
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5369
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
const Decl * getDecl() const
Definition GlobalDecl.h:106
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
static ImplicitParamDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, ImplicitParamKind ParamKind)
Create implicit parameter.
Definition Decl.cpp:5623
@ FPE_Ignore
Assume that floating-point exceptions are masked.
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3367
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3450
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
Definition Decl.h:317
const Expr * getSubExpr() const
Definition Expr.h:2202
PipeType - OpenCL20.
Definition TypeBase.h:8263
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3390
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8529
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8571
bool requiresBuiltinLaunder(const ASTContext &Context) const
Returns true if this type requires laundering by checking if it is a dynamic class type,...
Definition Type.cpp:5591
Represents a struct/union/class.
Definition Decl.h:4331
field_range fields() const
Definition Decl.h:4534
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
Encodes a location in the source.
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
bool isUnion() const
Definition Decl.h:3934
Exposes information about the current target.
Definition TargetInfo.h:227
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
bool isBigEndian() const
virtual bool checkArithmeticFenceSupported() const
Controls if __arithmetic_fence is supported in the targeted backend.
unsigned getSuitableAlign() const
Return the alignment that is the largest alignment ever used for any scalar/SIMD data type on the tar...
Definition TargetInfo.h:748
virtual std::string_view getClobbers() const =0
Returns a string of target-specific clobbers, in LLVM format.
The base class of the type hierarchy.
Definition TypeBase.h:1875
bool isBlockPointerType() const
Definition TypeBase.h:8702
bool isVoidType() const
Definition TypeBase.h:9048
bool isPackedVectorBoolType(const ASTContext &ctx) const
Definition Type.cpp:455
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
Definition Type.cpp:2266
bool isArrayType() const
Definition TypeBase.h:8781
bool isCountAttributedType() const
Definition Type.cpp:778
bool isPointerType() const
Definition TypeBase.h:8682
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9092
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9342
const CXXRecordDecl * getPointeeCXXRecordDecl() const
If this is a pointer or reference to a RecordType, return the CXXRecordDecl that the type refers to.
Definition Type.cpp:1958
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9275
Expr * getSubExpr() const
Definition Expr.h:2288
QualType getType() const
Definition Decl.h:723
QualType getType() const
Definition Value.cpp:237
Represents a GCC generic vector type.
Definition TypeBase.h:4237
unsigned getNumElements() const
Definition TypeBase.h:4252
QualType getElementType() const
Definition TypeBase.h:4251
SmallVector< OSLogBufferItem, 4 > Items
Definition OSLog.h:113
unsigned char getNumArgsByte() const
Definition OSLog.h:148
unsigned char getSummaryByte() const
Definition OSLog.h:139
Defines the clang::TargetInfo interface.
@ 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
llvm::Constant * initializationPatternFor(CodeGenModule &, llvm::Type *)
TypeEvaluationKind
The kind of evaluation to perform on values of a particular type.
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
constexpr XRayInstrMask Typed
Definition XRayInstr.h:42
constexpr XRayInstrMask Custom
Definition XRayInstr.h:41
bool computeOSLogBufferLayout(clang::ASTContext &Ctx, const clang::CallExpr *E, OSLogBufferLayout &layout)
Definition OSLog.cpp:192
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
bool Mul(InterpState &S, CodePtr OpPC)
Definition Interp.h:471
The JSON file list parser is used to communicate input to InstallAPI.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ Success
Annotation was successful.
Definition Parser.h:65
@ Vector
'vector' clause, allowed on 'loop', Combined, and 'routine' directives.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
Expr * Cond
};
@ Asm
Assembly: we accept this only so that we can preprocess it.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
SyncScope
Defines sync scope values used internally by clang.
Definition SyncScope.h:42
llvm::StringRef getAsString(SyncScope S)
Definition SyncScope.h:62
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Other
Other implicit parameter.
Definition Decl.h:1763
unsigned long uint64_t
long int64_t
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
EvalResult is a struct with detailed info about an evaluated expression.
Definition Expr.h:648
APValue Val
Val - This is the value the expression can be folded to.
Definition Expr.h:650
void clear(SanitizerMask K=SanitizerKind::All)
Disable the sanitizers specified in K.
Definition Sanitizers.h:195
void set(SanitizerMask K, bool Value)
Enable or disable a certain (single) sanitizer.
Definition Sanitizers.h:187
#define sinh(__x)
Definition tgmath.h:373
#define asin(__x)
Definition tgmath.h:112
#define scalbln(__x, __y)
Definition tgmath.h:1182
#define sqrt(__x)
Definition tgmath.h:520
#define acos(__x)
Definition tgmath.h:83
#define fmin(__x, __y)
Definition tgmath.h:780
#define exp(__x)
Definition tgmath.h:431
#define ilogb(__x)
Definition tgmath.h:851
#define copysign(__x, __y)
Definition tgmath.h:618
#define erf(__x)
Definition tgmath.h:636
#define atanh(__x)
Definition tgmath.h:228
#define remquo(__x, __y, __z)
Definition tgmath.h:1111
#define nextafter(__x, __y)
Definition tgmath.h:1055
#define frexp(__x, __y)
Definition tgmath.h:816
#define asinh(__x)
Definition tgmath.h:199
#define erfc(__x)
Definition tgmath.h:653
#define atan2(__x, __y)
Definition tgmath.h:566
#define nexttoward(__x, __y)
Definition tgmath.h:1073
#define hypot(__x, __y)
Definition tgmath.h:833
#define exp2(__x)
Definition tgmath.h:670
#define sin(__x)
Definition tgmath.h:286
#define cbrt(__x)
Definition tgmath.h:584
#define log2(__x)
Definition tgmath.h:970
#define llround(__x)
Definition tgmath.h:919
#define cosh(__x)
Definition tgmath.h:344
#define trunc(__x)
Definition tgmath.h:1216
#define fmax(__x, __y)
Definition tgmath.h:762
#define ldexp(__x, __y)
Definition tgmath.h:868
#define acosh(__x)
Definition tgmath.h:170
#define tgamma(__x)
Definition tgmath.h:1199
#define scalbn(__x, __y)
Definition tgmath.h:1165
#define round(__x)
Definition tgmath.h:1148
#define fmod(__x, __y)
Definition tgmath.h:798
#define llrint(__x)
Definition tgmath.h:902
#define tan(__x)
Definition tgmath.h:315
#define cos(__x)
Definition tgmath.h:257
#define log10(__x)
Definition tgmath.h:936
#define fabs(__x)
Definition tgmath.h:549
#define pow(__x, __y)
Definition tgmath.h:490
#define log1p(__x)
Definition tgmath.h:953
#define rint(__x)
Definition tgmath.h:1131
#define expm1(__x)
Definition tgmath.h:687
#define remainder(__x, __y)
Definition tgmath.h:1090
#define fdim(__x, __y)
Definition tgmath.h:704
#define lgamma(__x)
Definition tgmath.h:885
#define tanh(__x)
Definition tgmath.h:402
#define lrint(__x)
Definition tgmath.h:1004
#define atan(__x)
Definition tgmath.h:141
#define floor(__x)
Definition tgmath.h:722
#define ceil(__x)
Definition tgmath.h:601
#define log(__x)
Definition tgmath.h:460
#define logb(__x)
Definition tgmath.h:987
#define nearbyint(__x)
Definition tgmath.h:1038
#define lround(__x)
Definition tgmath.h:1021
#define fma(__x, __y, __z)
Definition tgmath.h:742