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