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