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