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