Bug Summary

File:tools/clang/lib/CodeGen/CGBuiltin.cpp
Warning:line 1203, column 5
Value stored to 'Off' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CGBuiltin.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-8/lib/clang/8.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/clang/lib/CodeGen -I /build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen -I /build/llvm-toolchain-snapshot-8~svn345461/tools/clang/include -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn345461/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/8.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/clang/lib/CodeGen -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-10-27-211344-32123-1 -x c++ /build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp -faddrsig
1//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Builtin calls as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGCXXABI.h"
15#include "CGObjCRuntime.h"
16#include "CGOpenCLRuntime.h"
17#include "CGRecordLayout.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "ConstantEmitter.h"
21#include "TargetInfo.h"
22#include "clang/AST/ASTContext.h"
23#include "clang/AST/Decl.h"
24#include "clang/Analysis/Analyses/OSLog.h"
25#include "clang/Basic/TargetBuiltins.h"
26#include "clang/Basic/TargetInfo.h"
27#include "clang/CodeGen/CGFunctionInfo.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/IR/CallSite.h"
30#include "llvm/IR/DataLayout.h"
31#include "llvm/IR/InlineAsm.h"
32#include "llvm/IR/Intrinsics.h"
33#include "llvm/IR/MDBuilder.h"
34#include "llvm/Support/ConvertUTF.h"
35#include "llvm/Support/ScopedPrinter.h"
36#include "llvm/Support/TargetParser.h"
37#include <sstream>
38
39using namespace clang;
40using namespace CodeGen;
41using namespace llvm;
42
43static
44int64_t clamp(int64_t Value, int64_t Low, int64_t High) {
45 return std::min(High, std::max(Low, Value));
46}
47
48/// getBuiltinLibFunction - Given a builtin id for a function like
49/// "__builtin_fabsf", return a Function* for "fabsf".
50llvm::Constant *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
51 unsigned BuiltinID) {
52 assert(Context.BuiltinInfo.isLibFunction(BuiltinID))((Context.BuiltinInfo.isLibFunction(BuiltinID)) ? static_cast
<void> (0) : __assert_fail ("Context.BuiltinInfo.isLibFunction(BuiltinID)"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 52, __PRETTY_FUNCTION__))
;
53
54 // Get the name, skip over the __builtin_ prefix (if necessary).
55 StringRef Name;
56 GlobalDecl D(FD);
57
58 // If the builtin has been declared explicitly with an assembler label,
59 // use the mangled name. This differs from the plain label on platforms
60 // that prefix labels.
61 if (FD->hasAttr<AsmLabelAttr>())
62 Name = getMangledName(D);
63 else
64 Name = Context.BuiltinInfo.getName(BuiltinID) + 10;
65
66 llvm::FunctionType *Ty =
67 cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType()));
68
69 return GetOrCreateLLVMFunction(Name, Ty, D, /*ForVTable=*/false);
70}
71
72/// Emit the conversions required to turn the given value into an
73/// integer of the given size.
74static Value *EmitToInt(CodeGenFunction &CGF, llvm::Value *V,
75 QualType T, llvm::IntegerType *IntType) {
76 V = CGF.EmitToMemory(V, T);
77
78 if (V->getType()->isPointerTy())
79 return CGF.Builder.CreatePtrToInt(V, IntType);
80
81 assert(V->getType() == IntType)((V->getType() == IntType) ? static_cast<void> (0) :
__assert_fail ("V->getType() == IntType", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 81, __PRETTY_FUNCTION__))
;
82 return V;
83}
84
85static Value *EmitFromInt(CodeGenFunction &CGF, llvm::Value *V,
86 QualType T, llvm::Type *ResultType) {
87 V = CGF.EmitFromMemory(V, T);
88
89 if (ResultType->isPointerTy())
90 return CGF.Builder.CreateIntToPtr(V, ResultType);
91
92 assert(V->getType() == ResultType)((V->getType() == ResultType) ? static_cast<void> (0
) : __assert_fail ("V->getType() == ResultType", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 92, __PRETTY_FUNCTION__))
;
93 return V;
94}
95
96/// Utility to insert an atomic instruction based on Instrinsic::ID
97/// and the expression node.
98static Value *MakeBinaryAtomicValue(CodeGenFunction &CGF,
99 llvm::AtomicRMWInst::BinOp Kind,
100 const CallExpr *E) {
101 QualType T = E->getType();
102 assert(E->getArg(0)->getType()->isPointerType())((E->getArg(0)->getType()->isPointerType()) ? static_cast
<void> (0) : __assert_fail ("E->getArg(0)->getType()->isPointerType()"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 102, __PRETTY_FUNCTION__))
;
103 assert(CGF.getContext().hasSameUnqualifiedType(T,((CGF.getContext().hasSameUnqualifiedType(T, E->getArg(0)->
getType()->getPointeeType())) ? static_cast<void> (0
) : __assert_fail ("CGF.getContext().hasSameUnqualifiedType(T, E->getArg(0)->getType()->getPointeeType())"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 104, __PRETTY_FUNCTION__))
104 E->getArg(0)->getType()->getPointeeType()))((CGF.getContext().hasSameUnqualifiedType(T, E->getArg(0)->
getType()->getPointeeType())) ? static_cast<void> (0
) : __assert_fail ("CGF.getContext().hasSameUnqualifiedType(T, E->getArg(0)->getType()->getPointeeType())"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 104, __PRETTY_FUNCTION__))
;
105 assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()))((CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->
getType())) ? static_cast<void> (0) : __assert_fail ("CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType())"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 105, __PRETTY_FUNCTION__))
;
106
107 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
108 unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
109
110 llvm::IntegerType *IntType =
111 llvm::IntegerType::get(CGF.getLLVMContext(),
112 CGF.getContext().getTypeSize(T));
113 llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
114
115 llvm::Value *Args[2];
116 Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
117 Args[1] = CGF.EmitScalarExpr(E->getArg(1));
118 llvm::Type *ValueType = Args[1]->getType();
119 Args[1] = EmitToInt(CGF, Args[1], T, IntType);
120
121 llvm::Value *Result = CGF.Builder.CreateAtomicRMW(
122 Kind, Args[0], Args[1], llvm::AtomicOrdering::SequentiallyConsistent);
123 return EmitFromInt(CGF, Result, T, ValueType);
124}
125
126static Value *EmitNontemporalStore(CodeGenFunction &CGF, const CallExpr *E) {
127 Value *Val = CGF.EmitScalarExpr(E->getArg(0));
128 Value *Address = CGF.EmitScalarExpr(E->getArg(1));
129
130 // Convert the type of the pointer to a pointer to the stored type.
131 Val = CGF.EmitToMemory(Val, E->getArg(0)->getType());
132 Value *BC = CGF.Builder.CreateBitCast(
133 Address, llvm::PointerType::getUnqual(Val->getType()), "cast");
134 LValue LV = CGF.MakeNaturalAlignAddrLValue(BC, E->getArg(0)->getType());
135 LV.setNontemporal(true);
136 CGF.EmitStoreOfScalar(Val, LV, false);
137 return nullptr;
138}
139
140static Value *EmitNontemporalLoad(CodeGenFunction &CGF, const CallExpr *E) {
141 Value *Address = CGF.EmitScalarExpr(E->getArg(0));
142
143 LValue LV = CGF.MakeNaturalAlignAddrLValue(Address, E->getType());
144 LV.setNontemporal(true);
145 return CGF.EmitLoadOfScalar(LV, E->getExprLoc());
146}
147
148static RValue EmitBinaryAtomic(CodeGenFunction &CGF,
149 llvm::AtomicRMWInst::BinOp Kind,
150 const CallExpr *E) {
151 return RValue::get(MakeBinaryAtomicValue(CGF, Kind, E));
152}
153
154/// Utility to insert an atomic instruction based Instrinsic::ID and
155/// the expression node, where the return value is the result of the
156/// operation.
157static RValue EmitBinaryAtomicPost(CodeGenFunction &CGF,
158 llvm::AtomicRMWInst::BinOp Kind,
159 const CallExpr *E,
160 Instruction::BinaryOps Op,
161 bool Invert = false) {
162 QualType T = E->getType();
163 assert(E->getArg(0)->getType()->isPointerType())((E->getArg(0)->getType()->isPointerType()) ? static_cast
<void> (0) : __assert_fail ("E->getArg(0)->getType()->isPointerType()"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 163, __PRETTY_FUNCTION__))
;
164 assert(CGF.getContext().hasSameUnqualifiedType(T,((CGF.getContext().hasSameUnqualifiedType(T, E->getArg(0)->
getType()->getPointeeType())) ? static_cast<void> (0
) : __assert_fail ("CGF.getContext().hasSameUnqualifiedType(T, E->getArg(0)->getType()->getPointeeType())"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 165, __PRETTY_FUNCTION__))
165 E->getArg(0)->getType()->getPointeeType()))((CGF.getContext().hasSameUnqualifiedType(T, E->getArg(0)->
getType()->getPointeeType())) ? static_cast<void> (0
) : __assert_fail ("CGF.getContext().hasSameUnqualifiedType(T, E->getArg(0)->getType()->getPointeeType())"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 165, __PRETTY_FUNCTION__))
;
166 assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()))((CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->
getType())) ? static_cast<void> (0) : __assert_fail ("CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType())"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 166, __PRETTY_FUNCTION__))
;
167
168 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
169 unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
170
171 llvm::IntegerType *IntType =
172 llvm::IntegerType::get(CGF.getLLVMContext(),
173 CGF.getContext().getTypeSize(T));
174 llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
175
176 llvm::Value *Args[2];
177 Args[1] = CGF.EmitScalarExpr(E->getArg(1));
178 llvm::Type *ValueType = Args[1]->getType();
179 Args[1] = EmitToInt(CGF, Args[1], T, IntType);
180 Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
181
182 llvm::Value *Result = CGF.Builder.CreateAtomicRMW(
183 Kind, Args[0], Args[1], llvm::AtomicOrdering::SequentiallyConsistent);
184 Result = CGF.Builder.CreateBinOp(Op, Result, Args[1]);
185 if (Invert)
186 Result = CGF.Builder.CreateBinOp(llvm::Instruction::Xor, Result,
187 llvm::ConstantInt::get(IntType, -1));
188 Result = EmitFromInt(CGF, Result, T, ValueType);
189 return RValue::get(Result);
190}
191
192/// Utility to insert an atomic cmpxchg instruction.
193///
194/// @param CGF The current codegen function.
195/// @param E Builtin call expression to convert to cmpxchg.
196/// arg0 - address to operate on
197/// arg1 - value to compare with
198/// arg2 - new value
199/// @param ReturnBool Specifies whether to return success flag of
200/// cmpxchg result or the old value.
201///
202/// @returns result of cmpxchg, according to ReturnBool
203static Value *MakeAtomicCmpXchgValue(CodeGenFunction &CGF, const CallExpr *E,
204 bool ReturnBool) {
205 QualType T = ReturnBool ? E->getArg(1)->getType() : E->getType();
206 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
207 unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
208
209 llvm::IntegerType *IntType = llvm::IntegerType::get(
210 CGF.getLLVMContext(), CGF.getContext().getTypeSize(T));
211 llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
212
213 Value *Args[3];
214 Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
215 Args[1] = CGF.EmitScalarExpr(E->getArg(1));
216 llvm::Type *ValueType = Args[1]->getType();
217 Args[1] = EmitToInt(CGF, Args[1], T, IntType);
218 Args[2] = EmitToInt(CGF, CGF.EmitScalarExpr(E->getArg(2)), T, IntType);
219
220 Value *Pair = CGF.Builder.CreateAtomicCmpXchg(
221 Args[0], Args[1], Args[2], llvm::AtomicOrdering::SequentiallyConsistent,
222 llvm::AtomicOrdering::SequentiallyConsistent);
223 if (ReturnBool)
224 // Extract boolean success flag and zext it to int.
225 return CGF.Builder.CreateZExt(CGF.Builder.CreateExtractValue(Pair, 1),
226 CGF.ConvertType(E->getType()));
227 else
228 // Extract old value and emit it using the same type as compare value.
229 return EmitFromInt(CGF, CGF.Builder.CreateExtractValue(Pair, 0), T,
230 ValueType);
231}
232
233// Emit a simple mangled intrinsic that has 1 argument and a return type
234// matching the argument type.
235static Value *emitUnaryBuiltin(CodeGenFunction &CGF,
236 const CallExpr *E,
237 unsigned IntrinsicID) {
238 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
239
240 Value *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
241 return CGF.Builder.CreateCall(F, Src0);
242}
243
244// Emit an intrinsic that has 2 operands of the same type as its result.
245static Value *emitBinaryBuiltin(CodeGenFunction &CGF,
246 const CallExpr *E,
247 unsigned IntrinsicID) {
248 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
249 llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
250
251 Value *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
252 return CGF.Builder.CreateCall(F, { Src0, Src1 });
253}
254
255// Emit an intrinsic that has 3 operands of the same type as its result.
256static Value *emitTernaryBuiltin(CodeGenFunction &CGF,
257 const CallExpr *E,
258 unsigned IntrinsicID) {
259 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
260 llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
261 llvm::Value *Src2 = CGF.EmitScalarExpr(E->getArg(2));
262
263 Value *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
264 return CGF.Builder.CreateCall(F, { Src0, Src1, Src2 });
265}
266
267// Emit an intrinsic that has 1 float or double operand, and 1 integer.
268static Value *emitFPIntBuiltin(CodeGenFunction &CGF,
269 const CallExpr *E,
270 unsigned IntrinsicID) {
271 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
272 llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
273
274 Value *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
275 return CGF.Builder.CreateCall(F, {Src0, Src1});
276}
277
278/// EmitFAbs - Emit a call to @llvm.fabs().
279static Value *EmitFAbs(CodeGenFunction &CGF, Value *V) {
280 Value *F = CGF.CGM.getIntrinsic(Intrinsic::fabs, V->getType());
281 llvm::CallInst *Call = CGF.Builder.CreateCall(F, V);
282 Call->setDoesNotAccessMemory();
283 return Call;
284}
285
286/// Emit the computation of the sign bit for a floating point value. Returns
287/// the i1 sign bit value.
288static Value *EmitSignBit(CodeGenFunction &CGF, Value *V) {
289 LLVMContext &C = CGF.CGM.getLLVMContext();
290
291 llvm::Type *Ty = V->getType();
292 int Width = Ty->getPrimitiveSizeInBits();
293 llvm::Type *IntTy = llvm::IntegerType::get(C, Width);
294 V = CGF.Builder.CreateBitCast(V, IntTy);
295 if (Ty->isPPC_FP128Ty()) {
296 // We want the sign bit of the higher-order double. The bitcast we just
297 // did works as if the double-double was stored to memory and then
298 // read as an i128. The "store" will put the higher-order double in the
299 // lower address in both little- and big-Endian modes, but the "load"
300 // will treat those bits as a different part of the i128: the low bits in
301 // little-Endian, the high bits in big-Endian. Therefore, on big-Endian
302 // we need to shift the high bits down to the low before truncating.
303 Width >>= 1;
304 if (CGF.getTarget().isBigEndian()) {
305 Value *ShiftCst = llvm::ConstantInt::get(IntTy, Width);
306 V = CGF.Builder.CreateLShr(V, ShiftCst);
307 }
308 // We are truncating value in order to extract the higher-order
309 // double, which we will be using to extract the sign from.
310 IntTy = llvm::IntegerType::get(C, Width);
311 V = CGF.Builder.CreateTrunc(V, IntTy);
312 }
313 Value *Zero = llvm::Constant::getNullValue(IntTy);
314 return CGF.Builder.CreateICmpSLT(V, Zero);
315}
316
317static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
318 const CallExpr *E, llvm::Constant *calleeValue) {
319 CGCallee callee = CGCallee::forDirect(calleeValue, FD);
320 return CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
321}
322
323/// Emit a call to llvm.{sadd,uadd,ssub,usub,smul,umul}.with.overflow.*
324/// depending on IntrinsicID.
325///
326/// \arg CGF The current codegen function.
327/// \arg IntrinsicID The ID for the Intrinsic we wish to generate.
328/// \arg X The first argument to the llvm.*.with.overflow.*.
329/// \arg Y The second argument to the llvm.*.with.overflow.*.
330/// \arg Carry The carry returned by the llvm.*.with.overflow.*.
331/// \returns The result (i.e. sum/product) returned by the intrinsic.
332static llvm::Value *EmitOverflowIntrinsic(CodeGenFunction &CGF,
333 const llvm::Intrinsic::ID IntrinsicID,
334 llvm::Value *X, llvm::Value *Y,
335 llvm::Value *&Carry) {
336 // Make sure we have integers of the same width.
337 assert(X->getType() == Y->getType() &&((X->getType() == Y->getType() && "Arguments must be the same type. (Did you forget to make sure both "
"arguments have the same integer width?)") ? static_cast<
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?)\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 339, __PRETTY_FUNCTION__))
338 "Arguments must be the same type. (Did you forget to make sure both "((X->getType() == Y->getType() && "Arguments must be the same type. (Did you forget to make sure both "
"arguments have the same integer width?)") ? static_cast<
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?)\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 339, __PRETTY_FUNCTION__))
339 "arguments have the same integer width?)")((X->getType() == Y->getType() && "Arguments must be the same type. (Did you forget to make sure both "
"arguments have the same integer width?)") ? static_cast<
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?)\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 339, __PRETTY_FUNCTION__))
;
340
341 llvm::Value *Callee = CGF.CGM.getIntrinsic(IntrinsicID, X->getType());
342 llvm::Value *Tmp = CGF.Builder.CreateCall(Callee, {X, Y});
343 Carry = CGF.Builder.CreateExtractValue(Tmp, 1);
344 return CGF.Builder.CreateExtractValue(Tmp, 0);
345}
346
347static Value *emitRangedBuiltin(CodeGenFunction &CGF,
348 unsigned IntrinsicID,
349 int low, int high) {
350 llvm::MDBuilder MDHelper(CGF.getLLVMContext());
351 llvm::MDNode *RNode = MDHelper.createRange(APInt(32, low), APInt(32, high));
352 Value *F = CGF.CGM.getIntrinsic(IntrinsicID, {});
353 llvm::Instruction *Call = CGF.Builder.CreateCall(F);
354 Call->setMetadata(llvm::LLVMContext::MD_range, RNode);
355 return Call;
356}
357
358namespace {
359 struct WidthAndSignedness {
360 unsigned Width;
361 bool Signed;
362 };
363}
364
365static WidthAndSignedness
366getIntegerWidthAndSignedness(const clang::ASTContext &context,
367 const clang::QualType Type) {
368 assert(Type->isIntegerType() && "Given type is not an integer.")((Type->isIntegerType() && "Given type is not an integer."
) ? static_cast<void> (0) : __assert_fail ("Type->isIntegerType() && \"Given type is not an integer.\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 368, __PRETTY_FUNCTION__))
;
369 unsigned Width = Type->isBooleanType() ? 1 : context.getTypeInfo(Type).Width;
370 bool Signed = Type->isSignedIntegerType();
371 return {Width, Signed};
372}
373
374// Given one or more integer types, this function produces an integer type that
375// encompasses them: any value in one of the given types could be expressed in
376// the encompassing type.
377static struct WidthAndSignedness
378EncompassingIntegerType(ArrayRef<struct WidthAndSignedness> Types) {
379 assert(Types.size() > 0 && "Empty list of types.")((Types.size() > 0 && "Empty list of types.") ? static_cast
<void> (0) : __assert_fail ("Types.size() > 0 && \"Empty list of types.\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 379, __PRETTY_FUNCTION__))
;
380
381 // If any of the given types is signed, we must return a signed type.
382 bool Signed = false;
383 for (const auto &Type : Types) {
384 Signed |= Type.Signed;
385 }
386
387 // The encompassing type must have a width greater than or equal to the width
388 // of the specified types. Additionally, if the encompassing type is signed,
389 // its width must be strictly greater than the width of any unsigned types
390 // given.
391 unsigned Width = 0;
392 for (const auto &Type : Types) {
393 unsigned MinWidth = Type.Width + (Signed && !Type.Signed);
394 if (Width < MinWidth) {
395 Width = MinWidth;
396 }
397 }
398
399 return {Width, Signed};
400}
401
402Value *CodeGenFunction::EmitVAStartEnd(Value *ArgValue, bool IsStart) {
403 llvm::Type *DestType = Int8PtrTy;
404 if (ArgValue->getType() != DestType)
405 ArgValue =
406 Builder.CreateBitCast(ArgValue, DestType, ArgValue->getName().data());
407
408 Intrinsic::ID inst = IsStart ? Intrinsic::vastart : Intrinsic::vaend;
409 return Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue);
410}
411
412/// Checks if using the result of __builtin_object_size(p, @p From) in place of
413/// __builtin_object_size(p, @p To) is correct
414static bool areBOSTypesCompatible(int From, int To) {
415 // Note: Our __builtin_object_size implementation currently treats Type=0 and
416 // Type=2 identically. Encoding this implementation detail here may make
417 // improving __builtin_object_size difficult in the future, so it's omitted.
418 return From == To || (From == 0 && To == 1) || (From == 3 && To == 2);
419}
420
421static llvm::Value *
422getDefaultBuiltinObjectSizeResult(unsigned Type, llvm::IntegerType *ResType) {
423 return ConstantInt::get(ResType, (Type & 2) ? 0 : -1, /*isSigned=*/true);
424}
425
426llvm::Value *
427CodeGenFunction::evaluateOrEmitBuiltinObjectSize(const Expr *E, unsigned Type,
428 llvm::IntegerType *ResType,
429 llvm::Value *EmittedE) {
430 uint64_t ObjectSize;
431 if (!E->tryEvaluateObjectSize(ObjectSize, getContext(), Type))
432 return emitBuiltinObjectSize(E, Type, ResType, EmittedE);
433 return ConstantInt::get(ResType, ObjectSize, /*isSigned=*/true);
434}
435
436/// Returns a Value corresponding to the size of the given expression.
437/// This Value may be either of the following:
438/// - A llvm::Argument (if E is a param with the pass_object_size attribute on
439/// it)
440/// - A call to the @llvm.objectsize intrinsic
441///
442/// EmittedE is the result of emitting `E` as a scalar expr. If it's non-null
443/// and we wouldn't otherwise try to reference a pass_object_size parameter,
444/// we'll call @llvm.objectsize on EmittedE, rather than emitting E.
445llvm::Value *
446CodeGenFunction::emitBuiltinObjectSize(const Expr *E, unsigned Type,
447 llvm::IntegerType *ResType,
448 llvm::Value *EmittedE) {
449 // We need to reference an argument if the pointer is a parameter with the
450 // pass_object_size attribute.
451 if (auto *D = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) {
452 auto *Param = dyn_cast<ParmVarDecl>(D->getDecl());
453 auto *PS = D->getDecl()->getAttr<PassObjectSizeAttr>();
454 if (Param != nullptr && PS != nullptr &&
455 areBOSTypesCompatible(PS->getType(), Type)) {
456 auto Iter = SizeArguments.find(Param);
457 assert(Iter != SizeArguments.end())((Iter != SizeArguments.end()) ? static_cast<void> (0) :
__assert_fail ("Iter != SizeArguments.end()", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 457, __PRETTY_FUNCTION__))
;
458
459 const ImplicitParamDecl *D = Iter->second;
460 auto DIter = LocalDeclMap.find(D);
461 assert(DIter != LocalDeclMap.end())((DIter != LocalDeclMap.end()) ? static_cast<void> (0) :
__assert_fail ("DIter != LocalDeclMap.end()", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 461, __PRETTY_FUNCTION__))
;
462
463 return EmitLoadOfScalar(DIter->second, /*volatile=*/false,
464 getContext().getSizeType(), E->getBeginLoc());
465 }
466 }
467
468 // LLVM can't handle Type=3 appropriately, and __builtin_object_size shouldn't
469 // evaluate E for side-effects. In either case, we shouldn't lower to
470 // @llvm.objectsize.
471 if (Type == 3 || (!EmittedE && E->HasSideEffects(getContext())))
472 return getDefaultBuiltinObjectSizeResult(Type, ResType);
473
474 Value *Ptr = EmittedE ? EmittedE : EmitScalarExpr(E);
475 assert(Ptr->getType()->isPointerTy() &&((Ptr->getType()->isPointerTy() && "Non-pointer passed to __builtin_object_size?"
) ? static_cast<void> (0) : __assert_fail ("Ptr->getType()->isPointerTy() && \"Non-pointer passed to __builtin_object_size?\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 476, __PRETTY_FUNCTION__))
476 "Non-pointer passed to __builtin_object_size?")((Ptr->getType()->isPointerTy() && "Non-pointer passed to __builtin_object_size?"
) ? static_cast<void> (0) : __assert_fail ("Ptr->getType()->isPointerTy() && \"Non-pointer passed to __builtin_object_size?\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 476, __PRETTY_FUNCTION__))
;
477
478 Value *F = CGM.getIntrinsic(Intrinsic::objectsize, {ResType, Ptr->getType()});
479
480 // LLVM only supports 0 and 2, make sure that we pass along that as a boolean.
481 Value *Min = Builder.getInt1((Type & 2) != 0);
482 // For GCC compatibility, __builtin_object_size treat NULL as unknown size.
483 Value *NullIsUnknown = Builder.getTrue();
484 return Builder.CreateCall(F, {Ptr, Min, NullIsUnknown});
485}
486
487namespace {
488/// A struct to generically desribe a bit test intrinsic.
489struct BitTest {
490 enum ActionKind : uint8_t { TestOnly, Complement, Reset, Set };
491 enum InterlockingKind : uint8_t {
492 Unlocked,
493 Sequential,
494 Acquire,
495 Release,
496 NoFence
497 };
498
499 ActionKind Action;
500 InterlockingKind Interlocking;
501 bool Is64Bit;
502
503 static BitTest decodeBitTestBuiltin(unsigned BuiltinID);
504};
505} // namespace
506
507BitTest BitTest::decodeBitTestBuiltin(unsigned BuiltinID) {
508 switch (BuiltinID) {
509 // Main portable variants.
510 case Builtin::BI_bittest:
511 return {TestOnly, Unlocked, false};
512 case Builtin::BI_bittestandcomplement:
513 return {Complement, Unlocked, false};
514 case Builtin::BI_bittestandreset:
515 return {Reset, Unlocked, false};
516 case Builtin::BI_bittestandset:
517 return {Set, Unlocked, false};
518 case Builtin::BI_interlockedbittestandreset:
519 return {Reset, Sequential, false};
520 case Builtin::BI_interlockedbittestandset:
521 return {Set, Sequential, false};
522
523 // X86-specific 64-bit variants.
524 case Builtin::BI_bittest64:
525 return {TestOnly, Unlocked, true};
526 case Builtin::BI_bittestandcomplement64:
527 return {Complement, Unlocked, true};
528 case Builtin::BI_bittestandreset64:
529 return {Reset, Unlocked, true};
530 case Builtin::BI_bittestandset64:
531 return {Set, Unlocked, true};
532 case Builtin::BI_interlockedbittestandreset64:
533 return {Reset, Sequential, true};
534 case Builtin::BI_interlockedbittestandset64:
535 return {Set, Sequential, true};
536
537 // ARM/AArch64-specific ordering variants.
538 case Builtin::BI_interlockedbittestandset_acq:
539 return {Set, Acquire, false};
540 case Builtin::BI_interlockedbittestandset_rel:
541 return {Set, Release, false};
542 case Builtin::BI_interlockedbittestandset_nf:
543 return {Set, NoFence, false};
544 case Builtin::BI_interlockedbittestandreset_acq:
545 return {Reset, Acquire, false};
546 case Builtin::BI_interlockedbittestandreset_rel:
547 return {Reset, Release, false};
548 case Builtin::BI_interlockedbittestandreset_nf:
549 return {Reset, NoFence, false};
550 }
551 llvm_unreachable("expected only bittest intrinsics")::llvm::llvm_unreachable_internal("expected only bittest intrinsics"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 551)
;
552}
553
554static char bitActionToX86BTCode(BitTest::ActionKind A) {
555 switch (A) {
556 case BitTest::TestOnly: return '\0';
557 case BitTest::Complement: return 'c';
558 case BitTest::Reset: return 'r';
559 case BitTest::Set: return 's';
560 }
561 llvm_unreachable("invalid action")::llvm::llvm_unreachable_internal("invalid action", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 561)
;
562}
563
564static llvm::Value *EmitX86BitTestIntrinsic(CodeGenFunction &CGF,
565 BitTest BT,
566 const CallExpr *E, Value *BitBase,
567 Value *BitPos) {
568 char Action = bitActionToX86BTCode(BT.Action);
569 char SizeSuffix = BT.Is64Bit ? 'q' : 'l';
570
571 // Build the assembly.
572 SmallString<64> Asm;
573 raw_svector_ostream AsmOS(Asm);
574 if (BT.Interlocking != BitTest::Unlocked)
575 AsmOS << "lock ";
576 AsmOS << "bt";
577 if (Action)
578 AsmOS << Action;
579 AsmOS << SizeSuffix << " $2, ($1)\n\tsetc ${0:b}";
580
581 // Build the constraints. FIXME: We should support immediates when possible.
582 std::string Constraints = "=r,r,r,~{cc},~{flags},~{fpsr}";
583 llvm::IntegerType *IntType = llvm::IntegerType::get(
584 CGF.getLLVMContext(),
585 CGF.getContext().getTypeSize(E->getArg(1)->getType()));
586 llvm::Type *IntPtrType = IntType->getPointerTo();
587 llvm::FunctionType *FTy =
588 llvm::FunctionType::get(CGF.Int8Ty, {IntPtrType, IntType}, false);
589
590 llvm::InlineAsm *IA =
591 llvm::InlineAsm::get(FTy, Asm, Constraints, /*SideEffects=*/true);
592 return CGF.Builder.CreateCall(IA, {BitBase, BitPos});
593}
594
595static llvm::AtomicOrdering
596getBitTestAtomicOrdering(BitTest::InterlockingKind I) {
597 switch (I) {
598 case BitTest::Unlocked: return llvm::AtomicOrdering::NotAtomic;
599 case BitTest::Sequential: return llvm::AtomicOrdering::SequentiallyConsistent;
600 case BitTest::Acquire: return llvm::AtomicOrdering::Acquire;
601 case BitTest::Release: return llvm::AtomicOrdering::Release;
602 case BitTest::NoFence: return llvm::AtomicOrdering::Monotonic;
603 }
604 llvm_unreachable("invalid interlocking")::llvm::llvm_unreachable_internal("invalid interlocking", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 604)
;
605}
606
607/// Emit a _bittest* intrinsic. These intrinsics take a pointer to an array of
608/// bits and a bit position and read and optionally modify the bit at that
609/// position. The position index can be arbitrarily large, i.e. it can be larger
610/// than 31 or 63, so we need an indexed load in the general case.
611static llvm::Value *EmitBitTestIntrinsic(CodeGenFunction &CGF,
612 unsigned BuiltinID,
613 const CallExpr *E) {
614 Value *BitBase = CGF.EmitScalarExpr(E->getArg(0));
615 Value *BitPos = CGF.EmitScalarExpr(E->getArg(1));
616
617 BitTest BT = BitTest::decodeBitTestBuiltin(BuiltinID);
618
619 // X86 has special BT, BTC, BTR, and BTS instructions that handle the array
620 // indexing operation internally. Use them if possible.
621 llvm::Triple::ArchType Arch = CGF.getTarget().getTriple().getArch();
622 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64)
623 return EmitX86BitTestIntrinsic(CGF, BT, E, BitBase, BitPos);
624
625 // Otherwise, use generic code to load one byte and test the bit. Use all but
626 // the bottom three bits as the array index, and the bottom three bits to form
627 // a mask.
628 // Bit = BitBaseI8[BitPos >> 3] & (1 << (BitPos & 0x7)) != 0;
629 Value *ByteIndex = CGF.Builder.CreateAShr(
630 BitPos, llvm::ConstantInt::get(BitPos->getType(), 3), "bittest.byteidx");
631 Value *BitBaseI8 = CGF.Builder.CreatePointerCast(BitBase, CGF.Int8PtrTy);
632 Address ByteAddr(CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, BitBaseI8,
633 ByteIndex, "bittest.byteaddr"),
634 CharUnits::One());
635 Value *PosLow =
636 CGF.Builder.CreateAnd(CGF.Builder.CreateTrunc(BitPos, CGF.Int8Ty),
637 llvm::ConstantInt::get(CGF.Int8Ty, 0x7));
638
639 // The updating instructions will need a mask.
640 Value *Mask = nullptr;
641 if (BT.Action != BitTest::TestOnly) {
642 Mask = CGF.Builder.CreateShl(llvm::ConstantInt::get(CGF.Int8Ty, 1), PosLow,
643 "bittest.mask");
644 }
645
646 // Check the action and ordering of the interlocked intrinsics.
647 llvm::AtomicOrdering Ordering = getBitTestAtomicOrdering(BT.Interlocking);
648
649 Value *OldByte = nullptr;
650 if (Ordering != llvm::AtomicOrdering::NotAtomic) {
651 // Emit a combined atomicrmw load/store operation for the interlocked
652 // intrinsics.
653 llvm::AtomicRMWInst::BinOp RMWOp = llvm::AtomicRMWInst::Or;
654 if (BT.Action == BitTest::Reset) {
655 Mask = CGF.Builder.CreateNot(Mask);
656 RMWOp = llvm::AtomicRMWInst::And;
657 }
658 OldByte = CGF.Builder.CreateAtomicRMW(RMWOp, ByteAddr.getPointer(), Mask,
659 Ordering);
660 } else {
661 // Emit a plain load for the non-interlocked intrinsics.
662 OldByte = CGF.Builder.CreateLoad(ByteAddr, "bittest.byte");
663 Value *NewByte = nullptr;
664 switch (BT.Action) {
665 case BitTest::TestOnly:
666 // Don't store anything.
667 break;
668 case BitTest::Complement:
669 NewByte = CGF.Builder.CreateXor(OldByte, Mask);
670 break;
671 case BitTest::Reset:
672 NewByte = CGF.Builder.CreateAnd(OldByte, CGF.Builder.CreateNot(Mask));
673 break;
674 case BitTest::Set:
675 NewByte = CGF.Builder.CreateOr(OldByte, Mask);
676 break;
677 }
678 if (NewByte)
679 CGF.Builder.CreateStore(NewByte, ByteAddr);
680 }
681
682 // However we loaded the old byte, either by plain load or atomicrmw, shift
683 // the bit into the low position and mask it to 0 or 1.
684 Value *ShiftedByte = CGF.Builder.CreateLShr(OldByte, PosLow, "bittest.shr");
685 return CGF.Builder.CreateAnd(
686 ShiftedByte, llvm::ConstantInt::get(CGF.Int8Ty, 1), "bittest.res");
687}
688
689namespace {
690enum class MSVCSetJmpKind {
691 _setjmpex,
692 _setjmp3,
693 _setjmp
694};
695}
696
697/// MSVC handles setjmp a bit differently on different platforms. On every
698/// architecture except 32-bit x86, the frame address is passed. On x86, extra
699/// parameters can be passed as variadic arguments, but we always pass none.
700static RValue EmitMSVCRTSetJmp(CodeGenFunction &CGF, MSVCSetJmpKind SJKind,
701 const CallExpr *E) {
702 llvm::Value *Arg1 = nullptr;
703 llvm::Type *Arg1Ty = nullptr;
704 StringRef Name;
705 bool IsVarArg = false;
706 if (SJKind == MSVCSetJmpKind::_setjmp3) {
707 Name = "_setjmp3";
708 Arg1Ty = CGF.Int32Ty;
709 Arg1 = llvm::ConstantInt::get(CGF.IntTy, 0);
710 IsVarArg = true;
711 } else {
712 Name = SJKind == MSVCSetJmpKind::_setjmp ? "_setjmp" : "_setjmpex";
713 Arg1Ty = CGF.Int8PtrTy;
714 Arg1 = CGF.Builder.CreateCall(CGF.CGM.getIntrinsic(Intrinsic::frameaddress),
715 llvm::ConstantInt::get(CGF.Int32Ty, 0));
716 }
717
718 // Mark the call site and declaration with ReturnsTwice.
719 llvm::Type *ArgTypes[2] = {CGF.Int8PtrTy, Arg1Ty};
720 llvm::AttributeList ReturnsTwiceAttr = llvm::AttributeList::get(
721 CGF.getLLVMContext(), llvm::AttributeList::FunctionIndex,
722 llvm::Attribute::ReturnsTwice);
723 llvm::Constant *SetJmpFn = CGF.CGM.CreateRuntimeFunction(
724 llvm::FunctionType::get(CGF.IntTy, ArgTypes, IsVarArg), Name,
725 ReturnsTwiceAttr, /*Local=*/true);
726
727 llvm::Value *Buf = CGF.Builder.CreateBitOrPointerCast(
728 CGF.EmitScalarExpr(E->getArg(0)), CGF.Int8PtrTy);
729 llvm::Value *Args[] = {Buf, Arg1};
730 llvm::CallSite CS = CGF.EmitRuntimeCallOrInvoke(SetJmpFn, Args);
731 CS.setAttributes(ReturnsTwiceAttr);
732 return RValue::get(CS.getInstruction());
733}
734
735// Many of MSVC builtins are on x64, ARM and AArch64; to avoid repeating code,
736// we handle them here.
737enum class CodeGenFunction::MSVCIntrin {
738 _BitScanForward,
739 _BitScanReverse,
740 _InterlockedAnd,
741 _InterlockedDecrement,
742 _InterlockedExchange,
743 _InterlockedExchangeAdd,
744 _InterlockedExchangeSub,
745 _InterlockedIncrement,
746 _InterlockedOr,
747 _InterlockedXor,
748 __fastfail,
749};
750
751Value *CodeGenFunction::EmitMSVCBuiltinExpr(MSVCIntrin BuiltinID,
752 const CallExpr *E) {
753 switch (BuiltinID) {
754 case MSVCIntrin::_BitScanForward:
755 case MSVCIntrin::_BitScanReverse: {
756 Value *ArgValue = EmitScalarExpr(E->getArg(1));
757
758 llvm::Type *ArgType = ArgValue->getType();
759 llvm::Type *IndexType =
760 EmitScalarExpr(E->getArg(0))->getType()->getPointerElementType();
761 llvm::Type *ResultType = ConvertType(E->getType());
762
763 Value *ArgZero = llvm::Constant::getNullValue(ArgType);
764 Value *ResZero = llvm::Constant::getNullValue(ResultType);
765 Value *ResOne = llvm::ConstantInt::get(ResultType, 1);
766
767 BasicBlock *Begin = Builder.GetInsertBlock();
768 BasicBlock *End = createBasicBlock("bitscan_end", this->CurFn);
769 Builder.SetInsertPoint(End);
770 PHINode *Result = Builder.CreatePHI(ResultType, 2, "bitscan_result");
771
772 Builder.SetInsertPoint(Begin);
773 Value *IsZero = Builder.CreateICmpEQ(ArgValue, ArgZero);
774 BasicBlock *NotZero = createBasicBlock("bitscan_not_zero", this->CurFn);
775 Builder.CreateCondBr(IsZero, End, NotZero);
776 Result->addIncoming(ResZero, Begin);
777
778 Builder.SetInsertPoint(NotZero);
779 Address IndexAddress = EmitPointerWithAlignment(E->getArg(0));
780
781 if (BuiltinID == MSVCIntrin::_BitScanForward) {
782 Value *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
783 Value *ZeroCount = Builder.CreateCall(F, {ArgValue, Builder.getTrue()});
784 ZeroCount = Builder.CreateIntCast(ZeroCount, IndexType, false);
785 Builder.CreateStore(ZeroCount, IndexAddress, false);
786 } else {
787 unsigned ArgWidth = cast<llvm::IntegerType>(ArgType)->getBitWidth();
788 Value *ArgTypeLastIndex = llvm::ConstantInt::get(IndexType, ArgWidth - 1);
789
790 Value *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
791 Value *ZeroCount = Builder.CreateCall(F, {ArgValue, Builder.getTrue()});
792 ZeroCount = Builder.CreateIntCast(ZeroCount, IndexType, false);
793 Value *Index = Builder.CreateNSWSub(ArgTypeLastIndex, ZeroCount);
794 Builder.CreateStore(Index, IndexAddress, false);
795 }
796 Builder.CreateBr(End);
797 Result->addIncoming(ResOne, NotZero);
798
799 Builder.SetInsertPoint(End);
800 return Result;
801 }
802 case MSVCIntrin::_InterlockedAnd:
803 return MakeBinaryAtomicValue(*this, AtomicRMWInst::And, E);
804 case MSVCIntrin::_InterlockedExchange:
805 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xchg, E);
806 case MSVCIntrin::_InterlockedExchangeAdd:
807 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Add, E);
808 case MSVCIntrin::_InterlockedExchangeSub:
809 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Sub, E);
810 case MSVCIntrin::_InterlockedOr:
811 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Or, E);
812 case MSVCIntrin::_InterlockedXor:
813 return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xor, E);
814
815 case MSVCIntrin::_InterlockedDecrement: {
816 llvm::Type *IntTy = ConvertType(E->getType());
817 AtomicRMWInst *RMWI = Builder.CreateAtomicRMW(
818 AtomicRMWInst::Sub,
819 EmitScalarExpr(E->getArg(0)),
820 ConstantInt::get(IntTy, 1),
821 llvm::AtomicOrdering::SequentiallyConsistent);
822 return Builder.CreateSub(RMWI, ConstantInt::get(IntTy, 1));
823 }
824 case MSVCIntrin::_InterlockedIncrement: {
825 llvm::Type *IntTy = ConvertType(E->getType());
826 AtomicRMWInst *RMWI = Builder.CreateAtomicRMW(
827 AtomicRMWInst::Add,
828 EmitScalarExpr(E->getArg(0)),
829 ConstantInt::get(IntTy, 1),
830 llvm::AtomicOrdering::SequentiallyConsistent);
831 return Builder.CreateAdd(RMWI, ConstantInt::get(IntTy, 1));
832 }
833
834 case MSVCIntrin::__fastfail: {
835 // Request immediate process termination from the kernel. The instruction
836 // sequences to do this are documented on MSDN:
837 // https://msdn.microsoft.com/en-us/library/dn774154.aspx
838 llvm::Triple::ArchType ISA = getTarget().getTriple().getArch();
839 StringRef Asm, Constraints;
840 switch (ISA) {
841 default:
842 ErrorUnsupported(E, "__fastfail call for this architecture");
843 break;
844 case llvm::Triple::x86:
845 case llvm::Triple::x86_64:
846 Asm = "int $$0x29";
847 Constraints = "{cx}";
848 break;
849 case llvm::Triple::thumb:
850 Asm = "udf #251";
851 Constraints = "{r0}";
852 break;
853 }
854 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, {Int32Ty}, false);
855 llvm::InlineAsm *IA =
856 llvm::InlineAsm::get(FTy, Asm, Constraints, /*SideEffects=*/true);
857 llvm::AttributeList NoReturnAttr = llvm::AttributeList::get(
858 getLLVMContext(), llvm::AttributeList::FunctionIndex,
859 llvm::Attribute::NoReturn);
860 CallSite CS = Builder.CreateCall(IA, EmitScalarExpr(E->getArg(0)));
861 CS.setAttributes(NoReturnAttr);
862 return CS.getInstruction();
863 }
864 }
865 llvm_unreachable("Incorrect MSVC intrinsic!")::llvm::llvm_unreachable_internal("Incorrect MSVC intrinsic!"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 865)
;
866}
867
868namespace {
869// ARC cleanup for __builtin_os_log_format
870struct CallObjCArcUse final : EHScopeStack::Cleanup {
871 CallObjCArcUse(llvm::Value *object) : object(object) {}
872 llvm::Value *object;
873
874 void Emit(CodeGenFunction &CGF, Flags flags) override {
875 CGF.EmitARCIntrinsicUse(object);
876 }
877};
878}
879
880Value *CodeGenFunction::EmitCheckedArgForBuiltin(const Expr *E,
881 BuiltinCheckKind Kind) {
882 assert((Kind == BCK_CLZPassedZero || Kind == BCK_CTZPassedZero)(((Kind == BCK_CLZPassedZero || Kind == BCK_CTZPassedZero) &&
"Unsupported builtin check kind") ? static_cast<void> (
0) : __assert_fail ("(Kind == BCK_CLZPassedZero || Kind == BCK_CTZPassedZero) && \"Unsupported builtin check kind\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 883, __PRETTY_FUNCTION__))
883 && "Unsupported builtin check kind")(((Kind == BCK_CLZPassedZero || Kind == BCK_CTZPassedZero) &&
"Unsupported builtin check kind") ? static_cast<void> (
0) : __assert_fail ("(Kind == BCK_CLZPassedZero || Kind == BCK_CTZPassedZero) && \"Unsupported builtin check kind\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 883, __PRETTY_FUNCTION__))
;
884
885 Value *ArgValue = EmitScalarExpr(E);
886 if (!SanOpts.has(SanitizerKind::Builtin) || !getTarget().isCLZForZeroUndef())
887 return ArgValue;
888
889 SanitizerScope SanScope(this);
890 Value *Cond = Builder.CreateICmpNE(
891 ArgValue, llvm::Constant::getNullValue(ArgValue->getType()));
892 EmitCheck(std::make_pair(Cond, SanitizerKind::Builtin),
893 SanitizerHandler::InvalidBuiltin,
894 {EmitCheckSourceLocation(E->getExprLoc()),
895 llvm::ConstantInt::get(Builder.getInt8Ty(), Kind)},
896 None);
897 return ArgValue;
898}
899
900/// Get the argument type for arguments to os_log_helper.
901static CanQualType getOSLogArgType(ASTContext &C, int Size) {
902 QualType UnsignedTy = C.getIntTypeForBitwidth(Size * 8, /*Signed=*/false);
903 return C.getCanonicalType(UnsignedTy);
904}
905
906llvm::Function *CodeGenFunction::generateBuiltinOSLogHelperFunction(
907 const analyze_os_log::OSLogBufferLayout &Layout,
908 CharUnits BufferAlignment) {
909 ASTContext &Ctx = getContext();
910
911 llvm::SmallString<64> Name;
912 {
913 raw_svector_ostream OS(Name);
914 OS << "__os_log_helper";
915 OS << "_" << BufferAlignment.getQuantity();
916 OS << "_" << int(Layout.getSummaryByte());
917 OS << "_" << int(Layout.getNumArgsByte());
918 for (const auto &Item : Layout.Items)
919 OS << "_" << int(Item.getSizeByte()) << "_"
920 << int(Item.getDescriptorByte());
921 }
922
923 if (llvm::Function *F = CGM.getModule().getFunction(Name))
924 return F;
925
926 llvm::SmallVector<ImplicitParamDecl, 4> Params;
927 Params.emplace_back(Ctx, nullptr, SourceLocation(), &Ctx.Idents.get("buffer"),
928 Ctx.VoidPtrTy, ImplicitParamDecl::Other);
929
930 for (unsigned int I = 0, E = Layout.Items.size(); I < E; ++I) {
931 char Size = Layout.Items[I].getSizeByte();
932 if (!Size)
933 continue;
934
935 Params.emplace_back(
936 Ctx, nullptr, SourceLocation(),
937 &Ctx.Idents.get(std::string("arg") + llvm::to_string(I)),
938 getOSLogArgType(Ctx, Size), ImplicitParamDecl::Other);
939 }
940
941 FunctionArgList Args;
942 for (auto &P : Params)
943 Args.push_back(&P);
944
945 // The helper function has linkonce_odr linkage to enable the linker to merge
946 // identical functions. To ensure the merging always happens, 'noinline' is
947 // attached to the function when compiling with -Oz.
948 const CGFunctionInfo &FI =
949 CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args);
950 llvm::FunctionType *FuncTy = CGM.getTypes().GetFunctionType(FI);
951 llvm::Function *Fn = llvm::Function::Create(
952 FuncTy, llvm::GlobalValue::LinkOnceODRLinkage, Name, &CGM.getModule());
953 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
954 CGM.SetLLVMFunctionAttributes(nullptr, FI, Fn);
955 CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Fn);
956
957 // Attach 'noinline' at -Oz.
958 if (CGM.getCodeGenOpts().OptimizeSize == 2)
959 Fn->addFnAttr(llvm::Attribute::NoInline);
960
961 auto NL = ApplyDebugLocation::CreateEmpty(*this);
962 IdentifierInfo *II = &Ctx.Idents.get(Name);
963 FunctionDecl *FD = FunctionDecl::Create(
964 Ctx, Ctx.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II,
965 Ctx.VoidTy, nullptr, SC_PrivateExtern, false, false);
966
967 StartFunction(FD, Ctx.VoidTy, Fn, FI, Args);
968
969 // Create a scope with an artificial location for the body of this function.
970 auto AL = ApplyDebugLocation::CreateArtificial(*this);
971
972 CharUnits Offset;
973 Address BufAddr(Builder.CreateLoad(GetAddrOfLocalVar(&Params[0]), "buf"),
974 BufferAlignment);
975 Builder.CreateStore(Builder.getInt8(Layout.getSummaryByte()),
976 Builder.CreateConstByteGEP(BufAddr, Offset++, "summary"));
977 Builder.CreateStore(Builder.getInt8(Layout.getNumArgsByte()),
978 Builder.CreateConstByteGEP(BufAddr, Offset++, "numArgs"));
979
980 unsigned I = 1;
981 for (const auto &Item : Layout.Items) {
982 Builder.CreateStore(
983 Builder.getInt8(Item.getDescriptorByte()),
984 Builder.CreateConstByteGEP(BufAddr, Offset++, "argDescriptor"));
985 Builder.CreateStore(
986 Builder.getInt8(Item.getSizeByte()),
987 Builder.CreateConstByteGEP(BufAddr, Offset++, "argSize"));
988
989 CharUnits Size = Item.size();
990 if (!Size.getQuantity())
991 continue;
992
993 Address Arg = GetAddrOfLocalVar(&Params[I]);
994 Address Addr = Builder.CreateConstByteGEP(BufAddr, Offset, "argData");
995 Addr = Builder.CreateBitCast(Addr, Arg.getPointer()->getType(),
996 "argDataCast");
997 Builder.CreateStore(Builder.CreateLoad(Arg), Addr);
998 Offset += Size;
999 ++I;
1000 }
1001
1002 FinishFunction();
1003
1004 return Fn;
1005}
1006
1007RValue CodeGenFunction::emitBuiltinOSLogFormat(const CallExpr &E) {
1008 assert(E.getNumArgs() >= 2 &&((E.getNumArgs() >= 2 && "__builtin_os_log_format takes at least 2 arguments"
) ? static_cast<void> (0) : __assert_fail ("E.getNumArgs() >= 2 && \"__builtin_os_log_format takes at least 2 arguments\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 1009, __PRETTY_FUNCTION__))
1009 "__builtin_os_log_format takes at least 2 arguments")((E.getNumArgs() >= 2 && "__builtin_os_log_format takes at least 2 arguments"
) ? static_cast<void> (0) : __assert_fail ("E.getNumArgs() >= 2 && \"__builtin_os_log_format takes at least 2 arguments\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 1009, __PRETTY_FUNCTION__))
;
1010 ASTContext &Ctx = getContext();
1011 analyze_os_log::OSLogBufferLayout Layout;
1012 analyze_os_log::computeOSLogBufferLayout(Ctx, &E, Layout);
1013 Address BufAddr = EmitPointerWithAlignment(E.getArg(0));
1014 llvm::SmallVector<llvm::Value *, 4> RetainableOperands;
1015
1016 // Ignore argument 1, the format string. It is not currently used.
1017 CallArgList Args;
1018 Args.add(RValue::get(BufAddr.getPointer()), Ctx.VoidPtrTy);
1019
1020 for (const auto &Item : Layout.Items) {
1021 int Size = Item.getSizeByte();
1022 if (!Size)
1023 continue;
1024
1025 llvm::Value *ArgVal;
1026
1027 if (const Expr *TheExpr = Item.getExpr()) {
1028 ArgVal = EmitScalarExpr(TheExpr, /*Ignore*/ false);
1029
1030 // Check if this is a retainable type.
1031 if (TheExpr->getType()->isObjCRetainableType()) {
1032 assert(getEvaluationKind(TheExpr->getType()) == TEK_Scalar &&((getEvaluationKind(TheExpr->getType()) == TEK_Scalar &&
"Only scalar can be a ObjC retainable type") ? static_cast<
void> (0) : __assert_fail ("getEvaluationKind(TheExpr->getType()) == TEK_Scalar && \"Only scalar can be a ObjC retainable type\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 1033, __PRETTY_FUNCTION__))
1033 "Only scalar can be a ObjC retainable type")((getEvaluationKind(TheExpr->getType()) == TEK_Scalar &&
"Only scalar can be a ObjC retainable type") ? static_cast<
void> (0) : __assert_fail ("getEvaluationKind(TheExpr->getType()) == TEK_Scalar && \"Only scalar can be a ObjC retainable type\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 1033, __PRETTY_FUNCTION__))
;
1034 // Check if the object is constant, if not, save it in
1035 // RetainableOperands.
1036 if (!isa<Constant>(ArgVal))
1037 RetainableOperands.push_back(ArgVal);
1038 }
1039 } else {
1040 ArgVal = Builder.getInt32(Item.getConstValue().getQuantity());
1041 }
1042
1043 unsigned ArgValSize =
1044 CGM.getDataLayout().getTypeSizeInBits(ArgVal->getType());
1045 llvm::IntegerType *IntTy = llvm::Type::getIntNTy(getLLVMContext(),
1046 ArgValSize);
1047 ArgVal = Builder.CreateBitOrPointerCast(ArgVal, IntTy);
1048 CanQualType ArgTy = getOSLogArgType(Ctx, Size);
1049 // If ArgVal has type x86_fp80, zero-extend ArgVal.
1050 ArgVal = Builder.CreateZExtOrBitCast(ArgVal, ConvertType(ArgTy));
1051 Args.add(RValue::get(ArgVal), ArgTy);
1052 }
1053
1054 const CGFunctionInfo &FI =
1055 CGM.getTypes().arrangeBuiltinFunctionCall(Ctx.VoidTy, Args);
1056 llvm::Function *F = CodeGenFunction(CGM).generateBuiltinOSLogHelperFunction(
1057 Layout, BufAddr.getAlignment());
1058 EmitCall(FI, CGCallee::forDirect(F), ReturnValueSlot(), Args);
1059
1060 // Push a clang.arc.use cleanup for each object in RetainableOperands. The
1061 // cleanup will cause the use to appear after the final log call, keeping
1062 // the object valid while it’s held in the log buffer. Note that if there’s
1063 // a release cleanup on the object, it will already be active; since
1064 // cleanups are emitted in reverse order, the use will occur before the
1065 // object is released.
1066 if (!RetainableOperands.empty() && getLangOpts().ObjCAutoRefCount &&
1067 CGM.getCodeGenOpts().OptimizationLevel != 0)
1068 for (llvm::Value *Object : RetainableOperands)
1069 pushFullExprCleanup<CallObjCArcUse>(getARCCleanupKind(), Object);
1070
1071 return RValue::get(BufAddr.getPointer());
1072}
1073
1074/// Determine if a binop is a checked mixed-sign multiply we can specialize.
1075static bool isSpecialMixedSignMultiply(unsigned BuiltinID,
1076 WidthAndSignedness Op1Info,
1077 WidthAndSignedness Op2Info,
1078 WidthAndSignedness ResultInfo) {
1079 return BuiltinID == Builtin::BI__builtin_mul_overflow &&
1080 Op1Info.Width == Op2Info.Width && Op1Info.Width >= ResultInfo.Width &&
1081 Op1Info.Signed != Op2Info.Signed;
1082}
1083
1084/// Emit a checked mixed-sign multiply. This is a cheaper specialization of
1085/// the generic checked-binop irgen.
1086static RValue
1087EmitCheckedMixedSignMultiply(CodeGenFunction &CGF, const clang::Expr *Op1,
1088 WidthAndSignedness Op1Info, const clang::Expr *Op2,
1089 WidthAndSignedness Op2Info,
1090 const clang::Expr *ResultArg, QualType ResultQTy,
1091 WidthAndSignedness ResultInfo) {
1092 assert(isSpecialMixedSignMultiply(Builtin::BI__builtin_mul_overflow, Op1Info,((isSpecialMixedSignMultiply(Builtin::BI__builtin_mul_overflow
, Op1Info, Op2Info, ResultInfo) && "Not a mixed-sign multipliction we can specialize"
) ? static_cast<void> (0) : __assert_fail ("isSpecialMixedSignMultiply(Builtin::BI__builtin_mul_overflow, Op1Info, Op2Info, ResultInfo) && \"Not a mixed-sign multipliction we can specialize\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 1094, __PRETTY_FUNCTION__))
1093 Op2Info, ResultInfo) &&((isSpecialMixedSignMultiply(Builtin::BI__builtin_mul_overflow
, Op1Info, Op2Info, ResultInfo) && "Not a mixed-sign multipliction we can specialize"
) ? static_cast<void> (0) : __assert_fail ("isSpecialMixedSignMultiply(Builtin::BI__builtin_mul_overflow, Op1Info, Op2Info, ResultInfo) && \"Not a mixed-sign multipliction we can specialize\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 1094, __PRETTY_FUNCTION__))
1094 "Not a mixed-sign multipliction we can specialize")((isSpecialMixedSignMultiply(Builtin::BI__builtin_mul_overflow
, Op1Info, Op2Info, ResultInfo) && "Not a mixed-sign multipliction we can specialize"
) ? static_cast<void> (0) : __assert_fail ("isSpecialMixedSignMultiply(Builtin::BI__builtin_mul_overflow, Op1Info, Op2Info, ResultInfo) && \"Not a mixed-sign multipliction we can specialize\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 1094, __PRETTY_FUNCTION__))
;
1095
1096 // Emit the signed and unsigned operands.
1097 const clang::Expr *SignedOp = Op1Info.Signed ? Op1 : Op2;
1098 const clang::Expr *UnsignedOp = Op1Info.Signed ? Op2 : Op1;
1099 llvm::Value *Signed = CGF.EmitScalarExpr(SignedOp);
1100 llvm::Value *Unsigned = CGF.EmitScalarExpr(UnsignedOp);
1101
1102 llvm::Type *OpTy = Signed->getType();
1103 llvm::Value *Zero = llvm::Constant::getNullValue(OpTy);
1104 Address ResultPtr = CGF.EmitPointerWithAlignment(ResultArg);
1105 llvm::Type *ResTy = ResultPtr.getElementType();
1106
1107 // Take the absolute value of the signed operand.
1108 llvm::Value *IsNegative = CGF.Builder.CreateICmpSLT(Signed, Zero);
1109 llvm::Value *AbsOfNegative = CGF.Builder.CreateSub(Zero, Signed);
1110 llvm::Value *AbsSigned =
1111 CGF.Builder.CreateSelect(IsNegative, AbsOfNegative, Signed);
1112
1113 // Perform a checked unsigned multiplication.
1114 llvm::Value *UnsignedOverflow;
1115 llvm::Value *UnsignedResult =
1116 EmitOverflowIntrinsic(CGF, llvm::Intrinsic::umul_with_overflow, AbsSigned,
1117 Unsigned, UnsignedOverflow);
1118
1119 llvm::Value *Overflow, *Result;
1120 if (ResultInfo.Signed) {
1121 // Signed overflow occurs if the result is greater than INT_MAX or lesser
1122 // than INT_MIN, i.e when |Result| > (INT_MAX + IsNegative).
1123 auto IntMax = llvm::APInt::getSignedMaxValue(ResultInfo.Width)
1124 .zextOrSelf(Op1Info.Width);
1125 llvm::Value *MaxResult =
1126 CGF.Builder.CreateAdd(llvm::ConstantInt::get(OpTy, IntMax),
1127 CGF.Builder.CreateZExt(IsNegative, OpTy));
1128 llvm::Value *SignedOverflow =
1129 CGF.Builder.CreateICmpUGT(UnsignedResult, MaxResult);
1130 Overflow = CGF.Builder.CreateOr(UnsignedOverflow, SignedOverflow);
1131
1132 // Prepare the signed result (possibly by negating it).
1133 llvm::Value *NegativeResult = CGF.Builder.CreateNeg(UnsignedResult);
1134 llvm::Value *SignedResult =
1135 CGF.Builder.CreateSelect(IsNegative, NegativeResult, UnsignedResult);
1136 Result = CGF.Builder.CreateTrunc(SignedResult, ResTy);
1137 } else {
1138 // Unsigned overflow occurs if the result is < 0 or greater than UINT_MAX.
1139 llvm::Value *Underflow = CGF.Builder.CreateAnd(
1140 IsNegative, CGF.Builder.CreateIsNotNull(UnsignedResult));
1141 Overflow = CGF.Builder.CreateOr(UnsignedOverflow, Underflow);
1142 if (ResultInfo.Width < Op1Info.Width) {
1143 auto IntMax =
1144 llvm::APInt::getMaxValue(ResultInfo.Width).zext(Op1Info.Width);
1145 llvm::Value *TruncOverflow = CGF.Builder.CreateICmpUGT(
1146 UnsignedResult, llvm::ConstantInt::get(OpTy, IntMax));
1147 Overflow = CGF.Builder.CreateOr(Overflow, TruncOverflow);
1148 }
1149
1150 // Negate the product if it would be negative in infinite precision.
1151 Result = CGF.Builder.CreateSelect(
1152 IsNegative, CGF.Builder.CreateNeg(UnsignedResult), UnsignedResult);
1153
1154 Result = CGF.Builder.CreateTrunc(Result, ResTy);
1155 }
1156 assert(Overflow && Result && "Missing overflow or result")((Overflow && Result && "Missing overflow or result"
) ? static_cast<void> (0) : __assert_fail ("Overflow && Result && \"Missing overflow or result\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 1156, __PRETTY_FUNCTION__))
;
1157
1158 bool isVolatile =
1159 ResultArg->getType()->getPointeeType().isVolatileQualified();
1160 CGF.Builder.CreateStore(CGF.EmitToMemory(Result, ResultQTy), ResultPtr,
1161 isVolatile);
1162 return RValue::get(Overflow);
1163}
1164
1165static llvm::Value *dumpRecord(CodeGenFunction &CGF, QualType RType,
1166 Value *&RecordPtr, CharUnits Align, Value *Func,
1167 int Lvl) {
1168 const auto *RT = RType->getAs<RecordType>();
1169 ASTContext &Context = CGF.getContext();
1170 RecordDecl *RD = RT->getDecl()->getDefinition();
1171 ASTContext &Ctx = RD->getASTContext();
1172 const ASTRecordLayout &RL = Ctx.getASTRecordLayout(RD);
1173 std::string Pad = std::string(Lvl * 4, ' ');
1174
1175 Value *GString =
1176 CGF.Builder.CreateGlobalStringPtr(RType.getAsString() + " {\n");
1177 Value *Res = CGF.Builder.CreateCall(Func, {GString});
1178
1179 static llvm::DenseMap<QualType, const char *> Types;
1180 if (Types.empty()) {
1181 Types[Context.CharTy] = "%c";
1182 Types[Context.BoolTy] = "%d";
1183 Types[Context.SignedCharTy] = "%hhd";
1184 Types[Context.UnsignedCharTy] = "%hhu";
1185 Types[Context.IntTy] = "%d";
1186 Types[Context.UnsignedIntTy] = "%u";
1187 Types[Context.LongTy] = "%ld";
1188 Types[Context.UnsignedLongTy] = "%lu";
1189 Types[Context.LongLongTy] = "%lld";
1190 Types[Context.UnsignedLongLongTy] = "%llu";
1191 Types[Context.ShortTy] = "%hd";
1192 Types[Context.UnsignedShortTy] = "%hu";
1193 Types[Context.VoidPtrTy] = "%p";
1194 Types[Context.FloatTy] = "%f";
1195 Types[Context.DoubleTy] = "%f";
1196 Types[Context.LongDoubleTy] = "%Lf";
1197 Types[Context.getPointerType(Context.CharTy)] = "%s";
1198 Types[Context.getPointerType(Context.getConstType(Context.CharTy))] = "%s";
1199 }
1200
1201 for (const auto *FD : RD->fields()) {
1202 uint64_t Off = RL.getFieldOffset(FD->getFieldIndex());
1203 Off = Ctx.toCharUnitsFromBits(Off).getQuantity();
Value stored to 'Off' is never read
1204
1205 Value *FieldPtr = RecordPtr;
1206 if (RD->isUnion())
1207 FieldPtr = CGF.Builder.CreatePointerCast(
1208 FieldPtr, CGF.ConvertType(Context.getPointerType(FD->getType())));
1209 else
1210 FieldPtr = CGF.Builder.CreateStructGEP(CGF.ConvertType(RType), FieldPtr,
1211 FD->getFieldIndex());
1212
1213 GString = CGF.Builder.CreateGlobalStringPtr(
1214 llvm::Twine(Pad)
1215 .concat(FD->getType().getAsString())
1216 .concat(llvm::Twine(' '))
1217 .concat(FD->getNameAsString())
1218 .concat(" : ")
1219 .str());
1220 Value *TmpRes = CGF.Builder.CreateCall(Func, {GString});
1221 Res = CGF.Builder.CreateAdd(Res, TmpRes);
1222
1223 QualType CanonicalType =
1224 FD->getType().getUnqualifiedType().getCanonicalType();
1225
1226 // We check whether we are in a recursive type
1227 if (CanonicalType->isRecordType()) {
1228 Value *TmpRes =
1229 dumpRecord(CGF, CanonicalType, FieldPtr, Align, Func, Lvl + 1);
1230 Res = CGF.Builder.CreateAdd(TmpRes, Res);
1231 continue;
1232 }
1233
1234 // We try to determine the best format to print the current field
1235 llvm::Twine Format = Types.find(CanonicalType) == Types.end()
1236 ? Types[Context.VoidPtrTy]
1237 : Types[CanonicalType];
1238
1239 Address FieldAddress = Address(FieldPtr, Align);
1240 FieldPtr = CGF.Builder.CreateLoad(FieldAddress);
1241
1242 // FIXME Need to handle bitfield here
1243 GString = CGF.Builder.CreateGlobalStringPtr(
1244 Format.concat(llvm::Twine('\n')).str());
1245 TmpRes = CGF.Builder.CreateCall(Func, {GString, FieldPtr});
1246 Res = CGF.Builder.CreateAdd(Res, TmpRes);
1247 }
1248
1249 GString = CGF.Builder.CreateGlobalStringPtr(Pad + "}\n");
1250 Value *TmpRes = CGF.Builder.CreateCall(Func, {GString});
1251 Res = CGF.Builder.CreateAdd(Res, TmpRes);
1252 return Res;
1253}
1254
1255RValue CodeGenFunction::emitRotate(const CallExpr *E, bool IsRotateRight) {
1256 llvm::Value *Src = EmitScalarExpr(E->getArg(0));
1257 llvm::Value *ShiftAmt = EmitScalarExpr(E->getArg(1));
1258
1259 // The builtin's shift arg may have a different type than the source arg and
1260 // result, but the LLVM intrinsic uses the same type for all values.
1261 llvm::Type *Ty = Src->getType();
1262 ShiftAmt = Builder.CreateIntCast(ShiftAmt, Ty, false);
1263
1264 // Rotate is a special case of LLVM funnel shift - 1st 2 args are the same.
1265 unsigned IID = IsRotateRight ? Intrinsic::fshr : Intrinsic::fshl;
1266 Value *F = CGM.getIntrinsic(IID, Ty);
1267 return RValue::get(Builder.CreateCall(F, { Src, Src, ShiftAmt }));
1268}
1269
1270RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
1271 unsigned BuiltinID, const CallExpr *E,
1272 ReturnValueSlot ReturnValue) {
1273 // See if we can constant fold this builtin. If so, don't emit it at all.
1274 Expr::EvalResult Result;
1275 if (E->EvaluateAsRValue(Result, CGM.getContext()) &&
1276 !Result.hasSideEffects()) {
1277 if (Result.Val.isInt())
1278 return RValue::get(llvm::ConstantInt::get(getLLVMContext(),
1279 Result.Val.getInt()));
1280 if (Result.Val.isFloat())
1281 return RValue::get(llvm::ConstantFP::get(getLLVMContext(),
1282 Result.Val.getFloat()));
1283 }
1284
1285 // There are LLVM math intrinsics/instructions corresponding to math library
1286 // functions except the LLVM op will never set errno while the math library
1287 // might. Also, math builtins have the same semantics as their math library
1288 // twins. Thus, we can transform math library and builtin calls to their
1289 // LLVM counterparts if the call is marked 'const' (known to never set errno).
1290 if (FD->hasAttr<ConstAttr>()) {
1291 switch (BuiltinID) {
1292 case Builtin::BIceil:
1293 case Builtin::BIceilf:
1294 case Builtin::BIceill:
1295 case Builtin::BI__builtin_ceil:
1296 case Builtin::BI__builtin_ceilf:
1297 case Builtin::BI__builtin_ceill:
1298 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::ceil));
1299
1300 case Builtin::BIcopysign:
1301 case Builtin::BIcopysignf:
1302 case Builtin::BIcopysignl:
1303 case Builtin::BI__builtin_copysign:
1304 case Builtin::BI__builtin_copysignf:
1305 case Builtin::BI__builtin_copysignl:
1306 case Builtin::BI__builtin_copysignf128:
1307 return RValue::get(emitBinaryBuiltin(*this, E, Intrinsic::copysign));
1308
1309 case Builtin::BIcos:
1310 case Builtin::BIcosf:
1311 case Builtin::BIcosl:
1312 case Builtin::BI__builtin_cos:
1313 case Builtin::BI__builtin_cosf:
1314 case Builtin::BI__builtin_cosl:
1315 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::cos));
1316
1317 case Builtin::BIexp:
1318 case Builtin::BIexpf:
1319 case Builtin::BIexpl:
1320 case Builtin::BI__builtin_exp:
1321 case Builtin::BI__builtin_expf:
1322 case Builtin::BI__builtin_expl:
1323 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::exp));
1324
1325 case Builtin::BIexp2:
1326 case Builtin::BIexp2f:
1327 case Builtin::BIexp2l:
1328 case Builtin::BI__builtin_exp2:
1329 case Builtin::BI__builtin_exp2f:
1330 case Builtin::BI__builtin_exp2l:
1331 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::exp2));
1332
1333 case Builtin::BIfabs:
1334 case Builtin::BIfabsf:
1335 case Builtin::BIfabsl:
1336 case Builtin::BI__builtin_fabs:
1337 case Builtin::BI__builtin_fabsf:
1338 case Builtin::BI__builtin_fabsl:
1339 case Builtin::BI__builtin_fabsf128:
1340 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::fabs));
1341
1342 case Builtin::BIfloor:
1343 case Builtin::BIfloorf:
1344 case Builtin::BIfloorl:
1345 case Builtin::BI__builtin_floor:
1346 case Builtin::BI__builtin_floorf:
1347 case Builtin::BI__builtin_floorl:
1348 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::floor));
1349
1350 case Builtin::BIfma:
1351 case Builtin::BIfmaf:
1352 case Builtin::BIfmal:
1353 case Builtin::BI__builtin_fma:
1354 case Builtin::BI__builtin_fmaf:
1355 case Builtin::BI__builtin_fmal:
1356 return RValue::get(emitTernaryBuiltin(*this, E, Intrinsic::fma));
1357
1358 case Builtin::BIfmax:
1359 case Builtin::BIfmaxf:
1360 case Builtin::BIfmaxl:
1361 case Builtin::BI__builtin_fmax:
1362 case Builtin::BI__builtin_fmaxf:
1363 case Builtin::BI__builtin_fmaxl:
1364 return RValue::get(emitBinaryBuiltin(*this, E, Intrinsic::maxnum));
1365
1366 case Builtin::BIfmin:
1367 case Builtin::BIfminf:
1368 case Builtin::BIfminl:
1369 case Builtin::BI__builtin_fmin:
1370 case Builtin::BI__builtin_fminf:
1371 case Builtin::BI__builtin_fminl:
1372 return RValue::get(emitBinaryBuiltin(*this, E, Intrinsic::minnum));
1373
1374 // fmod() is a special-case. It maps to the frem instruction rather than an
1375 // LLVM intrinsic.
1376 case Builtin::BIfmod:
1377 case Builtin::BIfmodf:
1378 case Builtin::BIfmodl:
1379 case Builtin::BI__builtin_fmod:
1380 case Builtin::BI__builtin_fmodf:
1381 case Builtin::BI__builtin_fmodl: {
1382 Value *Arg1 = EmitScalarExpr(E->getArg(0));
1383 Value *Arg2 = EmitScalarExpr(E->getArg(1));
1384 return RValue::get(Builder.CreateFRem(Arg1, Arg2, "fmod"));
1385 }
1386
1387 case Builtin::BIlog:
1388 case Builtin::BIlogf:
1389 case Builtin::BIlogl:
1390 case Builtin::BI__builtin_log:
1391 case Builtin::BI__builtin_logf:
1392 case Builtin::BI__builtin_logl:
1393 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::log));
1394
1395 case Builtin::BIlog10:
1396 case Builtin::BIlog10f:
1397 case Builtin::BIlog10l:
1398 case Builtin::BI__builtin_log10:
1399 case Builtin::BI__builtin_log10f:
1400 case Builtin::BI__builtin_log10l:
1401 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::log10));
1402
1403 case Builtin::BIlog2:
1404 case Builtin::BIlog2f:
1405 case Builtin::BIlog2l:
1406 case Builtin::BI__builtin_log2:
1407 case Builtin::BI__builtin_log2f:
1408 case Builtin::BI__builtin_log2l:
1409 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::log2));
1410
1411 case Builtin::BInearbyint:
1412 case Builtin::BInearbyintf:
1413 case Builtin::BInearbyintl:
1414 case Builtin::BI__builtin_nearbyint:
1415 case Builtin::BI__builtin_nearbyintf:
1416 case Builtin::BI__builtin_nearbyintl:
1417 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::nearbyint));
1418
1419 case Builtin::BIpow:
1420 case Builtin::BIpowf:
1421 case Builtin::BIpowl:
1422 case Builtin::BI__builtin_pow:
1423 case Builtin::BI__builtin_powf:
1424 case Builtin::BI__builtin_powl:
1425 return RValue::get(emitBinaryBuiltin(*this, E, Intrinsic::pow));
1426
1427 case Builtin::BIrint:
1428 case Builtin::BIrintf:
1429 case Builtin::BIrintl:
1430 case Builtin::BI__builtin_rint:
1431 case Builtin::BI__builtin_rintf:
1432 case Builtin::BI__builtin_rintl:
1433 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::rint));
1434
1435 case Builtin::BIround:
1436 case Builtin::BIroundf:
1437 case Builtin::BIroundl:
1438 case Builtin::BI__builtin_round:
1439 case Builtin::BI__builtin_roundf:
1440 case Builtin::BI__builtin_roundl:
1441 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::round));
1442
1443 case Builtin::BIsin:
1444 case Builtin::BIsinf:
1445 case Builtin::BIsinl:
1446 case Builtin::BI__builtin_sin:
1447 case Builtin::BI__builtin_sinf:
1448 case Builtin::BI__builtin_sinl:
1449 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::sin));
1450
1451 case Builtin::BIsqrt:
1452 case Builtin::BIsqrtf:
1453 case Builtin::BIsqrtl:
1454 case Builtin::BI__builtin_sqrt:
1455 case Builtin::BI__builtin_sqrtf:
1456 case Builtin::BI__builtin_sqrtl:
1457 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::sqrt));
1458
1459 case Builtin::BItrunc:
1460 case Builtin::BItruncf:
1461 case Builtin::BItruncl:
1462 case Builtin::BI__builtin_trunc:
1463 case Builtin::BI__builtin_truncf:
1464 case Builtin::BI__builtin_truncl:
1465 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::trunc));
1466
1467 default:
1468 break;
1469 }
1470 }
1471
1472 switch (BuiltinID) {
1473 default: break;
1474 case Builtin::BI__builtin___CFStringMakeConstantString:
1475 case Builtin::BI__builtin___NSStringMakeConstantString:
1476 return RValue::get(ConstantEmitter(*this).emitAbstract(E, E->getType()));
1477 case Builtin::BI__builtin_stdarg_start:
1478 case Builtin::BI__builtin_va_start:
1479 case Builtin::BI__va_start:
1480 case Builtin::BI__builtin_va_end:
1481 return RValue::get(
1482 EmitVAStartEnd(BuiltinID == Builtin::BI__va_start
1483 ? EmitScalarExpr(E->getArg(0))
1484 : EmitVAListRef(E->getArg(0)).getPointer(),
1485 BuiltinID != Builtin::BI__builtin_va_end));
1486 case Builtin::BI__builtin_va_copy: {
1487 Value *DstPtr = EmitVAListRef(E->getArg(0)).getPointer();
1488 Value *SrcPtr = EmitVAListRef(E->getArg(1)).getPointer();
1489
1490 llvm::Type *Type = Int8PtrTy;
1491
1492 DstPtr = Builder.CreateBitCast(DstPtr, Type);
1493 SrcPtr = Builder.CreateBitCast(SrcPtr, Type);
1494 return RValue::get(Builder.CreateCall(CGM.getIntrinsic(Intrinsic::vacopy),
1495 {DstPtr, SrcPtr}));
1496 }
1497 case Builtin::BI__builtin_abs:
1498 case Builtin::BI__builtin_labs:
1499 case Builtin::BI__builtin_llabs: {
1500 // X < 0 ? -X : X
1501 // The negation has 'nsw' because abs of INT_MIN is undefined.
1502 Value *ArgValue = EmitScalarExpr(E->getArg(0));
1503 Value *NegOp = Builder.CreateNSWNeg(ArgValue, "neg");
1504 Constant *Zero = llvm::Constant::getNullValue(ArgValue->getType());
1505 Value *CmpResult = Builder.CreateICmpSLT(ArgValue, Zero, "abscond");
1506 Value *Result = Builder.CreateSelect(CmpResult, NegOp, ArgValue, "abs");
1507 return RValue::get(Result);
1508 }
1509 case Builtin::BI__builtin_conj:
1510 case Builtin::BI__builtin_conjf:
1511 case Builtin::BI__builtin_conjl: {
1512 ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
1513 Value *Real = ComplexVal.first;
1514 Value *Imag = ComplexVal.second;
1515 Value *Zero =
1516 Imag->getType()->isFPOrFPVectorTy()
1517 ? llvm::ConstantFP::getZeroValueForNegation(Imag->getType())
1518 : llvm::Constant::getNullValue(Imag->getType());
1519
1520 Imag = Builder.CreateFSub(Zero, Imag, "sub");
1521 return RValue::getComplex(std::make_pair(Real, Imag));
1522 }
1523 case Builtin::BI__builtin_creal:
1524 case Builtin::BI__builtin_crealf:
1525 case Builtin::BI__builtin_creall:
1526 case Builtin::BIcreal:
1527 case Builtin::BIcrealf:
1528 case Builtin::BIcreall: {
1529 ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
1530 return RValue::get(ComplexVal.first);
1531 }
1532
1533 case Builtin::BI__builtin_dump_struct: {
1534 Value *Func = EmitScalarExpr(E->getArg(1)->IgnoreImpCasts());
1535 CharUnits Arg0Align = EmitPointerWithAlignment(E->getArg(0)).getAlignment();
1536
1537 const Expr *Arg0 = E->getArg(0)->IgnoreImpCasts();
1538 QualType Arg0Type = Arg0->getType()->getPointeeType();
1539
1540 Value *RecordPtr = EmitScalarExpr(Arg0);
1541 Value *Res = dumpRecord(*this, Arg0Type, RecordPtr, Arg0Align, Func, 0);
1542 return RValue::get(Res);
1543 }
1544
1545 case Builtin::BI__builtin_cimag:
1546 case Builtin::BI__builtin_cimagf:
1547 case Builtin::BI__builtin_cimagl:
1548 case Builtin::BIcimag:
1549 case Builtin::BIcimagf:
1550 case Builtin::BIcimagl: {
1551 ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
1552 return RValue::get(ComplexVal.second);
1553 }
1554
1555 case Builtin::BI__builtin_clrsb:
1556 case Builtin::BI__builtin_clrsbl:
1557 case Builtin::BI__builtin_clrsbll: {
1558 // clrsb(x) -> clz(x < 0 ? ~x : x) - 1 or
1559 Value *ArgValue = EmitScalarExpr(E->getArg(0));
1560
1561 llvm::Type *ArgType = ArgValue->getType();
1562 Value *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
1563
1564 llvm::Type *ResultType = ConvertType(E->getType());
1565 Value *Zero = llvm::Constant::getNullValue(ArgType);
1566 Value *IsNeg = Builder.CreateICmpSLT(ArgValue, Zero, "isneg");
1567 Value *Inverse = Builder.CreateNot(ArgValue, "not");
1568 Value *Tmp = Builder.CreateSelect(IsNeg, Inverse, ArgValue);
1569 Value *Ctlz = Builder.CreateCall(F, {Tmp, Builder.getFalse()});
1570 Value *Result = Builder.CreateSub(Ctlz, llvm::ConstantInt::get(ArgType, 1));
1571 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1572 "cast");
1573 return RValue::get(Result);
1574 }
1575 case Builtin::BI__builtin_ctzs:
1576 case Builtin::BI__builtin_ctz:
1577 case Builtin::BI__builtin_ctzl:
1578 case Builtin::BI__builtin_ctzll: {
1579 Value *ArgValue = EmitCheckedArgForBuiltin(E->getArg(0), BCK_CTZPassedZero);
1580
1581 llvm::Type *ArgType = ArgValue->getType();
1582 Value *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
1583
1584 llvm::Type *ResultType = ConvertType(E->getType());
1585 Value *ZeroUndef = Builder.getInt1(getTarget().isCLZForZeroUndef());
1586 Value *Result = Builder.CreateCall(F, {ArgValue, ZeroUndef});
1587 if (Result->getType() != ResultType)
1588 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1589 "cast");
1590 return RValue::get(Result);
1591 }
1592 case Builtin::BI__builtin_clzs:
1593 case Builtin::BI__builtin_clz:
1594 case Builtin::BI__builtin_clzl:
1595 case Builtin::BI__builtin_clzll: {
1596 Value *ArgValue = EmitCheckedArgForBuiltin(E->getArg(0), BCK_CLZPassedZero);
1597
1598 llvm::Type *ArgType = ArgValue->getType();
1599 Value *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
1600
1601 llvm::Type *ResultType = ConvertType(E->getType());
1602 Value *ZeroUndef = Builder.getInt1(getTarget().isCLZForZeroUndef());
1603 Value *Result = Builder.CreateCall(F, {ArgValue, ZeroUndef});
1604 if (Result->getType() != ResultType)
1605 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1606 "cast");
1607 return RValue::get(Result);
1608 }
1609 case Builtin::BI__builtin_ffs:
1610 case Builtin::BI__builtin_ffsl:
1611 case Builtin::BI__builtin_ffsll: {
1612 // ffs(x) -> x ? cttz(x) + 1 : 0
1613 Value *ArgValue = EmitScalarExpr(E->getArg(0));
1614
1615 llvm::Type *ArgType = ArgValue->getType();
1616 Value *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
1617
1618 llvm::Type *ResultType = ConvertType(E->getType());
1619 Value *Tmp =
1620 Builder.CreateAdd(Builder.CreateCall(F, {ArgValue, Builder.getTrue()}),
1621 llvm::ConstantInt::get(ArgType, 1));
1622 Value *Zero = llvm::Constant::getNullValue(ArgType);
1623 Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
1624 Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs");
1625 if (Result->getType() != ResultType)
1626 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1627 "cast");
1628 return RValue::get(Result);
1629 }
1630 case Builtin::BI__builtin_parity:
1631 case Builtin::BI__builtin_parityl:
1632 case Builtin::BI__builtin_parityll: {
1633 // parity(x) -> ctpop(x) & 1
1634 Value *ArgValue = EmitScalarExpr(E->getArg(0));
1635
1636 llvm::Type *ArgType = ArgValue->getType();
1637 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, ArgType);
1638
1639 llvm::Type *ResultType = ConvertType(E->getType());
1640 Value *Tmp = Builder.CreateCall(F, ArgValue);
1641 Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1));
1642 if (Result->getType() != ResultType)
1643 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1644 "cast");
1645 return RValue::get(Result);
1646 }
1647 case Builtin::BI__popcnt16:
1648 case Builtin::BI__popcnt:
1649 case Builtin::BI__popcnt64:
1650 case Builtin::BI__builtin_popcount:
1651 case Builtin::BI__builtin_popcountl:
1652 case Builtin::BI__builtin_popcountll: {
1653 Value *ArgValue = EmitScalarExpr(E->getArg(0));
1654
1655 llvm::Type *ArgType = ArgValue->getType();
1656 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, ArgType);
1657
1658 llvm::Type *ResultType = ConvertType(E->getType());
1659 Value *Result = Builder.CreateCall(F, ArgValue);
1660 if (Result->getType() != ResultType)
1661 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
1662 "cast");
1663 return RValue::get(Result);
1664 }
1665 case Builtin::BI_rotr8:
1666 case Builtin::BI_rotr16:
1667 case Builtin::BI_rotr:
1668 case Builtin::BI_lrotr:
1669 case Builtin::BI_rotr64: {
1670 Value *Val = EmitScalarExpr(E->getArg(0));
1671 Value *Shift = EmitScalarExpr(E->getArg(1));
1672
1673 llvm::Type *ArgType = Val->getType();
1674 Shift = Builder.CreateIntCast(Shift, ArgType, false);
1675 unsigned ArgWidth = ArgType->getIntegerBitWidth();
1676 Value *Mask = llvm::ConstantInt::get(ArgType, ArgWidth - 1);
1677
1678 Value *RightShiftAmt = Builder.CreateAnd(Shift, Mask);
1679 Value *RightShifted = Builder.CreateLShr(Val, RightShiftAmt);
1680 Value *LeftShiftAmt = Builder.CreateAnd(Builder.CreateNeg(Shift), Mask);
1681 Value *LeftShifted = Builder.CreateShl(Val, LeftShiftAmt);
1682 Value *Result = Builder.CreateOr(LeftShifted, RightShifted);
1683 return RValue::get(Result);
1684 }
1685 case Builtin::BI_rotl8:
1686 case Builtin::BI_rotl16:
1687 case Builtin::BI_rotl:
1688 case Builtin::BI_lrotl:
1689 case Builtin::BI_rotl64: {
1690 Value *Val = EmitScalarExpr(E->getArg(0));
1691 Value *Shift = EmitScalarExpr(E->getArg(1));
1692
1693 llvm::Type *ArgType = Val->getType();
1694 Shift = Builder.CreateIntCast(Shift, ArgType, false);
1695 unsigned ArgWidth = ArgType->getIntegerBitWidth();
1696 Value *Mask = llvm::ConstantInt::get(ArgType, ArgWidth - 1);
1697
1698 Value *LeftShiftAmt = Builder.CreateAnd(Shift, Mask);
1699 Value *LeftShifted = Builder.CreateShl(Val, LeftShiftAmt);
1700 Value *RightShiftAmt = Builder.CreateAnd(Builder.CreateNeg(Shift), Mask);
1701 Value *RightShifted = Builder.CreateLShr(Val, RightShiftAmt);
1702 Value *Result = Builder.CreateOr(LeftShifted, RightShifted);
1703 return RValue::get(Result);
1704 }
1705 case Builtin::BI__builtin_unpredictable: {
1706 // Always return the argument of __builtin_unpredictable. LLVM does not
1707 // handle this builtin. Metadata for this builtin should be added directly
1708 // to instructions such as branches or switches that use it.
1709 return RValue::get(EmitScalarExpr(E->getArg(0)));
1710 }
1711 case Builtin::BI__builtin_expect: {
1712 Value *ArgValue = EmitScalarExpr(E->getArg(0));
1713 llvm::Type *ArgType = ArgValue->getType();
1714
1715 Value *ExpectedValue = EmitScalarExpr(E->getArg(1));
1716 // Don't generate llvm.expect on -O0 as the backend won't use it for
1717 // anything.
1718 // Note, we still IRGen ExpectedValue because it could have side-effects.
1719 if (CGM.getCodeGenOpts().OptimizationLevel == 0)
1720 return RValue::get(ArgValue);
1721
1722 Value *FnExpect = CGM.getIntrinsic(Intrinsic::expect, ArgType);
1723 Value *Result =
1724 Builder.CreateCall(FnExpect, {ArgValue, ExpectedValue}, "expval");
1725 return RValue::get(Result);
1726 }
1727 case Builtin::BI__builtin_assume_aligned: {
1728 Value *PtrValue = EmitScalarExpr(E->getArg(0));
1729 Value *OffsetValue =
1730 (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : nullptr;
1731
1732 Value *AlignmentValue = EmitScalarExpr(E->getArg(1));
1733 ConstantInt *AlignmentCI = cast<ConstantInt>(AlignmentValue);
1734 unsigned Alignment = (unsigned) AlignmentCI->getZExtValue();
1735
1736 EmitAlignmentAssumption(PtrValue, Alignment, OffsetValue);
1737 return RValue::get(PtrValue);
1738 }
1739 case Builtin::BI__assume:
1740 case Builtin::BI__builtin_assume: {
1741 if (E->getArg(0)->HasSideEffects(getContext()))
1742 return RValue::get(nullptr);
1743
1744 Value *ArgValue = EmitScalarExpr(E->getArg(0));
1745 Value *FnAssume = CGM.getIntrinsic(Intrinsic::assume);
1746 return RValue::get(Builder.CreateCall(FnAssume, ArgValue));
1747 }
1748 case Builtin::BI__builtin_bswap16:
1749 case Builtin::BI__builtin_bswap32:
1750 case Builtin::BI__builtin_bswap64: {
1751 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::bswap));
1752 }
1753 case Builtin::BI__builtin_bitreverse8:
1754 case Builtin::BI__builtin_bitreverse16:
1755 case Builtin::BI__builtin_bitreverse32:
1756 case Builtin::BI__builtin_bitreverse64: {
1757 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::bitreverse));
1758 }
1759 case Builtin::BI__builtin_rotateleft8:
1760 case Builtin::BI__builtin_rotateleft16:
1761 case Builtin::BI__builtin_rotateleft32:
1762 case Builtin::BI__builtin_rotateleft64:
1763 return emitRotate(E, false);
1764
1765 case Builtin::BI__builtin_rotateright8:
1766 case Builtin::BI__builtin_rotateright16:
1767 case Builtin::BI__builtin_rotateright32:
1768 case Builtin::BI__builtin_rotateright64:
1769 return emitRotate(E, true);
1770
1771 case Builtin::BI__builtin_object_size: {
1772 unsigned Type =
1773 E->getArg(1)->EvaluateKnownConstInt(getContext()).getZExtValue();
1774 auto *ResType = cast<llvm::IntegerType>(ConvertType(E->getType()));
1775
1776 // We pass this builtin onto the optimizer so that it can figure out the
1777 // object size in more complex cases.
1778 return RValue::get(emitBuiltinObjectSize(E->getArg(0), Type, ResType,
1779 /*EmittedE=*/nullptr));
1780 }
1781 case Builtin::BI__builtin_prefetch: {
1782 Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
1783 // FIXME: Technically these constants should of type 'int', yes?
1784 RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
1785 llvm::ConstantInt::get(Int32Ty, 0);
1786 Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
1787 llvm::ConstantInt::get(Int32Ty, 3);
1788 Value *Data = llvm::ConstantInt::get(Int32Ty, 1);
1789 Value *F = CGM.getIntrinsic(Intrinsic::prefetch);
1790 return RValue::get(Builder.CreateCall(F, {Address, RW, Locality, Data}));
1791 }
1792 case Builtin::BI__builtin_readcyclecounter: {
1793 Value *F = CGM.getIntrinsic(Intrinsic::readcyclecounter);
1794 return RValue::get(Builder.CreateCall(F));
1795 }
1796 case Builtin::BI__builtin___clear_cache: {
1797 Value *Begin = EmitScalarExpr(E->getArg(0));
1798 Value *End = EmitScalarExpr(E->getArg(1));
1799 Value *F = CGM.getIntrinsic(Intrinsic::clear_cache);
1800 return RValue::get(Builder.CreateCall(F, {Begin, End}));
1801 }
1802 case Builtin::BI__builtin_trap:
1803 return RValue::get(EmitTrapCall(Intrinsic::trap));
1804 case Builtin::BI__debugbreak:
1805 return RValue::get(EmitTrapCall(Intrinsic::debugtrap));
1806 case Builtin::BI__builtin_unreachable: {
1807 EmitUnreachable(E->getExprLoc());
1808
1809 // We do need to preserve an insertion point.
1810 EmitBlock(createBasicBlock("unreachable.cont"));
1811
1812 return RValue::get(nullptr);
1813 }
1814
1815 case Builtin::BI__builtin_powi:
1816 case Builtin::BI__builtin_powif:
1817 case Builtin::BI__builtin_powil: {
1818 Value *Base = EmitScalarExpr(E->getArg(0));
1819 Value *Exponent = EmitScalarExpr(E->getArg(1));
1820 llvm::Type *ArgType = Base->getType();
1821 Value *F = CGM.getIntrinsic(Intrinsic::powi, ArgType);
1822 return RValue::get(Builder.CreateCall(F, {Base, Exponent}));
1823 }
1824
1825 case Builtin::BI__builtin_isgreater:
1826 case Builtin::BI__builtin_isgreaterequal:
1827 case Builtin::BI__builtin_isless:
1828 case Builtin::BI__builtin_islessequal:
1829 case Builtin::BI__builtin_islessgreater:
1830 case Builtin::BI__builtin_isunordered: {
1831 // Ordered comparisons: we know the arguments to these are matching scalar
1832 // floating point values.
1833 Value *LHS = EmitScalarExpr(E->getArg(0));
1834 Value *RHS = EmitScalarExpr(E->getArg(1));
1835
1836 switch (BuiltinID) {
1837 default: llvm_unreachable("Unknown ordered comparison")::llvm::llvm_unreachable_internal("Unknown ordered comparison"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 1837)
;
1838 case Builtin::BI__builtin_isgreater:
1839 LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
1840 break;
1841 case Builtin::BI__builtin_isgreaterequal:
1842 LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
1843 break;
1844 case Builtin::BI__builtin_isless:
1845 LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
1846 break;
1847 case Builtin::BI__builtin_islessequal:
1848 LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
1849 break;
1850 case Builtin::BI__builtin_islessgreater:
1851 LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
1852 break;
1853 case Builtin::BI__builtin_isunordered:
1854 LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
1855 break;
1856 }
1857 // ZExt bool to int type.
1858 return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType())));
1859 }
1860 case Builtin::BI__builtin_isnan: {
1861 Value *V = EmitScalarExpr(E->getArg(0));
1862 V = Builder.CreateFCmpUNO(V, V, "cmp");
1863 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
1864 }
1865
1866 case Builtin::BIfinite:
1867 case Builtin::BI__finite:
1868 case Builtin::BIfinitef:
1869 case Builtin::BI__finitef:
1870 case Builtin::BIfinitel:
1871 case Builtin::BI__finitel:
1872 case Builtin::BI__builtin_isinf:
1873 case Builtin::BI__builtin_isfinite: {
1874 // isinf(x) --> fabs(x) == infinity
1875 // isfinite(x) --> fabs(x) != infinity
1876 // x != NaN via the ordered compare in either case.
1877 Value *V = EmitScalarExpr(E->getArg(0));
1878 Value *Fabs = EmitFAbs(*this, V);
1879 Constant *Infinity = ConstantFP::getInfinity(V->getType());
1880 CmpInst::Predicate Pred = (BuiltinID == Builtin::BI__builtin_isinf)
1881 ? CmpInst::FCMP_OEQ
1882 : CmpInst::FCMP_ONE;
1883 Value *FCmp = Builder.CreateFCmp(Pred, Fabs, Infinity, "cmpinf");
1884 return RValue::get(Builder.CreateZExt(FCmp, ConvertType(E->getType())));
1885 }
1886
1887 case Builtin::BI__builtin_isinf_sign: {
1888 // isinf_sign(x) -> fabs(x) == infinity ? (signbit(x) ? -1 : 1) : 0
1889 Value *Arg = EmitScalarExpr(E->getArg(0));
1890 Value *AbsArg = EmitFAbs(*this, Arg);
1891 Value *IsInf = Builder.CreateFCmpOEQ(
1892 AbsArg, ConstantFP::getInfinity(Arg->getType()), "isinf");
1893 Value *IsNeg = EmitSignBit(*this, Arg);
1894
1895 llvm::Type *IntTy = ConvertType(E->getType());
1896 Value *Zero = Constant::getNullValue(IntTy);
1897 Value *One = ConstantInt::get(IntTy, 1);
1898 Value *NegativeOne = ConstantInt::get(IntTy, -1);
1899 Value *SignResult = Builder.CreateSelect(IsNeg, NegativeOne, One);
1900 Value *Result = Builder.CreateSelect(IsInf, SignResult, Zero);
1901 return RValue::get(Result);
1902 }
1903
1904 case Builtin::BI__builtin_isnormal: {
1905 // isnormal(x) --> x == x && fabsf(x) < infinity && fabsf(x) >= float_min
1906 Value *V = EmitScalarExpr(E->getArg(0));
1907 Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq");
1908
1909 Value *Abs = EmitFAbs(*this, V);
1910 Value *IsLessThanInf =
1911 Builder.CreateFCmpULT(Abs, ConstantFP::getInfinity(V->getType()),"isinf");
1912 APFloat Smallest = APFloat::getSmallestNormalized(
1913 getContext().getFloatTypeSemantics(E->getArg(0)->getType()));
1914 Value *IsNormal =
1915 Builder.CreateFCmpUGE(Abs, ConstantFP::get(V->getContext(), Smallest),
1916 "isnormal");
1917 V = Builder.CreateAnd(Eq, IsLessThanInf, "and");
1918 V = Builder.CreateAnd(V, IsNormal, "and");
1919 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
1920 }
1921
1922 case Builtin::BI__builtin_fpclassify: {
1923 Value *V = EmitScalarExpr(E->getArg(5));
1924 llvm::Type *Ty = ConvertType(E->getArg(5)->getType());
1925
1926 // Create Result
1927 BasicBlock *Begin = Builder.GetInsertBlock();
1928 BasicBlock *End = createBasicBlock("fpclassify_end", this->CurFn);
1929 Builder.SetInsertPoint(End);
1930 PHINode *Result =
1931 Builder.CreatePHI(ConvertType(E->getArg(0)->getType()), 4,
1932 "fpclassify_result");
1933
1934 // if (V==0) return FP_ZERO
1935 Builder.SetInsertPoint(Begin);
1936 Value *IsZero = Builder.CreateFCmpOEQ(V, Constant::getNullValue(Ty),
1937 "iszero");
1938 Value *ZeroLiteral = EmitScalarExpr(E->getArg(4));
1939 BasicBlock *NotZero = createBasicBlock("fpclassify_not_zero", this->CurFn);
1940 Builder.CreateCondBr(IsZero, End, NotZero);
1941 Result->addIncoming(ZeroLiteral, Begin);
1942
1943 // if (V != V) return FP_NAN
1944 Builder.SetInsertPoint(NotZero);
1945 Value *IsNan = Builder.CreateFCmpUNO(V, V, "cmp");
1946 Value *NanLiteral = EmitScalarExpr(E->getArg(0));
1947 BasicBlock *NotNan = createBasicBlock("fpclassify_not_nan", this->CurFn);
1948 Builder.CreateCondBr(IsNan, End, NotNan);
1949 Result->addIncoming(NanLiteral, NotZero);
1950
1951 // if (fabs(V) == infinity) return FP_INFINITY
1952 Builder.SetInsertPoint(NotNan);
1953 Value *VAbs = EmitFAbs(*this, V);
1954 Value *IsInf =
1955 Builder.CreateFCmpOEQ(VAbs, ConstantFP::getInfinity(V->getType()),
1956 "isinf");
1957 Value *InfLiteral = EmitScalarExpr(E->getArg(1));
1958 BasicBlock *NotInf = createBasicBlock("fpclassify_not_inf", this->CurFn);
1959 Builder.CreateCondBr(IsInf, End, NotInf);
1960 Result->addIncoming(InfLiteral, NotNan);
1961
1962 // if (fabs(V) >= MIN_NORMAL) return FP_NORMAL else FP_SUBNORMAL
1963 Builder.SetInsertPoint(NotInf);
1964 APFloat Smallest = APFloat::getSmallestNormalized(
1965 getContext().getFloatTypeSemantics(E->getArg(5)->getType()));
1966 Value *IsNormal =
1967 Builder.CreateFCmpUGE(VAbs, ConstantFP::get(V->getContext(), Smallest),
1968 "isnormal");
1969 Value *NormalResult =
1970 Builder.CreateSelect(IsNormal, EmitScalarExpr(E->getArg(2)),
1971 EmitScalarExpr(E->getArg(3)));
1972 Builder.CreateBr(End);
1973 Result->addIncoming(NormalResult, NotInf);
1974
1975 // return Result
1976 Builder.SetInsertPoint(End);
1977 return RValue::get(Result);
1978 }
1979
1980 case Builtin::BIalloca:
1981 case Builtin::BI_alloca:
1982 case Builtin::BI__builtin_alloca: {
1983 Value *Size = EmitScalarExpr(E->getArg(0));
1984 const TargetInfo &TI = getContext().getTargetInfo();
1985 // The alignment of the alloca should correspond to __BIGGEST_ALIGNMENT__.
1986 unsigned SuitableAlignmentInBytes =
1987 CGM.getContext()
1988 .toCharUnitsFromBits(TI.getSuitableAlign())
1989 .getQuantity();
1990 AllocaInst *AI = Builder.CreateAlloca(Builder.getInt8Ty(), Size);
1991 AI->setAlignment(SuitableAlignmentInBytes);
1992 return RValue::get(AI);
1993 }
1994
1995 case Builtin::BI__builtin_alloca_with_align: {
1996 Value *Size = EmitScalarExpr(E->getArg(0));
1997 Value *AlignmentInBitsValue = EmitScalarExpr(E->getArg(1));
1998 auto *AlignmentInBitsCI = cast<ConstantInt>(AlignmentInBitsValue);
1999 unsigned AlignmentInBits = AlignmentInBitsCI->getZExtValue();
2000 unsigned AlignmentInBytes =
2001 CGM.getContext().toCharUnitsFromBits(AlignmentInBits).getQuantity();
2002 AllocaInst *AI = Builder.CreateAlloca(Builder.getInt8Ty(), Size);
2003 AI->setAlignment(AlignmentInBytes);
2004 return RValue::get(AI);
2005 }
2006
2007 case Builtin::BIbzero:
2008 case Builtin::BI__builtin_bzero: {
2009 Address Dest = EmitPointerWithAlignment(E->getArg(0));
2010 Value *SizeVal = EmitScalarExpr(E->getArg(1));
2011 EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
2012 E->getArg(0)->getExprLoc(), FD, 0);
2013 Builder.CreateMemSet(Dest, Builder.getInt8(0), SizeVal, false);
2014 return RValue::get(nullptr);
2015 }
2016 case Builtin::BImemcpy:
2017 case Builtin::BI__builtin_memcpy: {
2018 Address Dest = EmitPointerWithAlignment(E->getArg(0));
2019 Address Src = EmitPointerWithAlignment(E->getArg(1));
2020 Value *SizeVal = EmitScalarExpr(E->getArg(2));
2021 EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
2022 E->getArg(0)->getExprLoc(), FD, 0);
2023 EmitNonNullArgCheck(RValue::get(Src.getPointer()), E->getArg(1)->getType(),
2024 E->getArg(1)->getExprLoc(), FD, 1);
2025 Builder.CreateMemCpy(Dest, Src, SizeVal, false);
2026 return RValue::get(Dest.getPointer());
2027 }
2028
2029 case Builtin::BI__builtin_char_memchr:
2030 BuiltinID = Builtin::BI__builtin_memchr;
2031 break;
2032
2033 case Builtin::BI__builtin___memcpy_chk: {
2034 // fold __builtin_memcpy_chk(x, y, cst1, cst2) to memcpy iff cst1<=cst2.
2035 llvm::APSInt Size, DstSize;
2036 if (!E->getArg(2)->EvaluateAsInt(Size, CGM.getContext()) ||
2037 !E->getArg(3)->EvaluateAsInt(DstSize, CGM.getContext()))
2038 break;
2039 if (Size.ugt(DstSize))
2040 break;
2041 Address Dest = EmitPointerWithAlignment(E->getArg(0));
2042 Address Src = EmitPointerWithAlignment(E->getArg(1));
2043 Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
2044 Builder.CreateMemCpy(Dest, Src, SizeVal, false);
2045 return RValue::get(Dest.getPointer());
2046 }
2047
2048 case Builtin::BI__builtin_objc_memmove_collectable: {
2049 Address DestAddr = EmitPointerWithAlignment(E->getArg(0));
2050 Address SrcAddr = EmitPointerWithAlignment(E->getArg(1));
2051 Value *SizeVal = EmitScalarExpr(E->getArg(2));
2052 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this,
2053 DestAddr, SrcAddr, SizeVal);
2054 return RValue::get(DestAddr.getPointer());
2055 }
2056
2057 case Builtin::BI__builtin___memmove_chk: {
2058 // fold __builtin_memmove_chk(x, y, cst1, cst2) to memmove iff cst1<=cst2.
2059 llvm::APSInt Size, DstSize;
2060 if (!E->getArg(2)->EvaluateAsInt(Size, CGM.getContext()) ||
2061 !E->getArg(3)->EvaluateAsInt(DstSize, CGM.getContext()))
2062 break;
2063 if (Size.ugt(DstSize))
2064 break;
2065 Address Dest = EmitPointerWithAlignment(E->getArg(0));
2066 Address Src = EmitPointerWithAlignment(E->getArg(1));
2067 Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
2068 Builder.CreateMemMove(Dest, Src, SizeVal, false);
2069 return RValue::get(Dest.getPointer());
2070 }
2071
2072 case Builtin::BImemmove:
2073 case Builtin::BI__builtin_memmove: {
2074 Address Dest = EmitPointerWithAlignment(E->getArg(0));
2075 Address Src = EmitPointerWithAlignment(E->getArg(1));
2076 Value *SizeVal = EmitScalarExpr(E->getArg(2));
2077 EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
2078 E->getArg(0)->getExprLoc(), FD, 0);
2079 EmitNonNullArgCheck(RValue::get(Src.getPointer()), E->getArg(1)->getType(),
2080 E->getArg(1)->getExprLoc(), FD, 1);
2081 Builder.CreateMemMove(Dest, Src, SizeVal, false);
2082 return RValue::get(Dest.getPointer());
2083 }
2084 case Builtin::BImemset:
2085 case Builtin::BI__builtin_memset: {
2086 Address Dest = EmitPointerWithAlignment(E->getArg(0));
2087 Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
2088 Builder.getInt8Ty());
2089 Value *SizeVal = EmitScalarExpr(E->getArg(2));
2090 EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
2091 E->getArg(0)->getExprLoc(), FD, 0);
2092 Builder.CreateMemSet(Dest, ByteVal, SizeVal, false);
2093 return RValue::get(Dest.getPointer());
2094 }
2095 case Builtin::BI__builtin___memset_chk: {
2096 // fold __builtin_memset_chk(x, y, cst1, cst2) to memset iff cst1<=cst2.
2097 llvm::APSInt Size, DstSize;
2098 if (!E->getArg(2)->EvaluateAsInt(Size, CGM.getContext()) ||
2099 !E->getArg(3)->EvaluateAsInt(DstSize, CGM.getContext()))
2100 break;
2101 if (Size.ugt(DstSize))
2102 break;
2103 Address Dest = EmitPointerWithAlignment(E->getArg(0));
2104 Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
2105 Builder.getInt8Ty());
2106 Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
2107 Builder.CreateMemSet(Dest, ByteVal, SizeVal, false);
2108 return RValue::get(Dest.getPointer());
2109 }
2110 case Builtin::BI__builtin_wmemcmp: {
2111 // The MSVC runtime library does not provide a definition of wmemcmp, so we
2112 // need an inline implementation.
2113 if (!getTarget().getTriple().isOSMSVCRT())
2114 break;
2115
2116 llvm::Type *WCharTy = ConvertType(getContext().WCharTy);
2117
2118 Value *Dst = EmitScalarExpr(E->getArg(0));
2119 Value *Src = EmitScalarExpr(E->getArg(1));
2120 Value *Size = EmitScalarExpr(E->getArg(2));
2121
2122 BasicBlock *Entry = Builder.GetInsertBlock();
2123 BasicBlock *CmpGT = createBasicBlock("wmemcmp.gt");
2124 BasicBlock *CmpLT = createBasicBlock("wmemcmp.lt");
2125 BasicBlock *Next = createBasicBlock("wmemcmp.next");
2126 BasicBlock *Exit = createBasicBlock("wmemcmp.exit");
2127 Value *SizeEq0 = Builder.CreateICmpEQ(Size, ConstantInt::get(SizeTy, 0));
2128 Builder.CreateCondBr(SizeEq0, Exit, CmpGT);
2129
2130 EmitBlock(CmpGT);
2131 PHINode *DstPhi = Builder.CreatePHI(Dst->getType(), 2);
2132 DstPhi->addIncoming(Dst, Entry);
2133 PHINode *SrcPhi = Builder.CreatePHI(Src->getType(), 2);
2134 SrcPhi->addIncoming(Src, Entry);
2135 PHINode *SizePhi = Builder.CreatePHI(SizeTy, 2);
2136 SizePhi->addIncoming(Size, Entry);
2137 CharUnits WCharAlign =
2138 getContext().getTypeAlignInChars(getContext().WCharTy);
2139 Value *DstCh = Builder.CreateAlignedLoad(WCharTy, DstPhi, WCharAlign);
2140 Value *SrcCh = Builder.CreateAlignedLoad(WCharTy, SrcPhi, WCharAlign);
2141 Value *DstGtSrc = Builder.CreateICmpUGT(DstCh, SrcCh);
2142 Builder.CreateCondBr(DstGtSrc, Exit, CmpLT);
2143
2144 EmitBlock(CmpLT);
2145 Value *DstLtSrc = Builder.CreateICmpULT(DstCh, SrcCh);
2146 Builder.CreateCondBr(DstLtSrc, Exit, Next);
2147
2148 EmitBlock(Next);
2149 Value *NextDst = Builder.CreateConstInBoundsGEP1_32(WCharTy, DstPhi, 1);
2150 Value *NextSrc = Builder.CreateConstInBoundsGEP1_32(WCharTy, SrcPhi, 1);
2151 Value *NextSize = Builder.CreateSub(SizePhi, ConstantInt::get(SizeTy, 1));
2152 Value *NextSizeEq0 =
2153 Builder.CreateICmpEQ(NextSize, ConstantInt::get(SizeTy, 0));
2154 Builder.CreateCondBr(NextSizeEq0, Exit, CmpGT);
2155 DstPhi->addIncoming(NextDst, Next);
2156 SrcPhi->addIncoming(NextSrc, Next);
2157 SizePhi->addIncoming(NextSize, Next);
2158
2159 EmitBlock(Exit);
2160 PHINode *Ret = Builder.CreatePHI(IntTy, 4);
2161 Ret->addIncoming(ConstantInt::get(IntTy, 0), Entry);
2162 Ret->addIncoming(ConstantInt::get(IntTy, 1), CmpGT);
2163 Ret->addIncoming(ConstantInt::get(IntTy, -1), CmpLT);
2164 Ret->addIncoming(ConstantInt::get(IntTy, 0), Next);
2165 return RValue::get(Ret);
2166 }
2167 case Builtin::BI__builtin_dwarf_cfa: {
2168 // The offset in bytes from the first argument to the CFA.
2169 //
2170 // Why on earth is this in the frontend? Is there any reason at
2171 // all that the backend can't reasonably determine this while
2172 // lowering llvm.eh.dwarf.cfa()?
2173 //
2174 // TODO: If there's a satisfactory reason, add a target hook for
2175 // this instead of hard-coding 0, which is correct for most targets.
2176 int32_t Offset = 0;
2177
2178 Value *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa);
2179 return RValue::get(Builder.CreateCall(F,
2180 llvm::ConstantInt::get(Int32Ty, Offset)));
2181 }
2182 case Builtin::BI__builtin_return_address: {
2183 Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
2184 getContext().UnsignedIntTy);
2185 Value *F = CGM.getIntrinsic(Intrinsic::returnaddress);
2186 return RValue::get(Builder.CreateCall(F, Depth));
2187 }
2188 case Builtin::BI_ReturnAddress: {
2189 Value *F = CGM.getIntrinsic(Intrinsic::returnaddress);
2190 return RValue::get(Builder.CreateCall(F, Builder.getInt32(0)));
2191 }
2192 case Builtin::BI__builtin_frame_address: {
2193 Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
2194 getContext().UnsignedIntTy);
2195 Value *F = CGM.getIntrinsic(Intrinsic::frameaddress);
2196 return RValue::get(Builder.CreateCall(F, Depth));
2197 }
2198 case Builtin::BI__builtin_extract_return_addr: {
2199 Value *Address = EmitScalarExpr(E->getArg(0));
2200 Value *Result = getTargetHooks().decodeReturnAddress(*this, Address);
2201 return RValue::get(Result);
2202 }
2203 case Builtin::BI__builtin_frob_return_addr: {
2204 Value *Address = EmitScalarExpr(E->getArg(0));
2205 Value *Result = getTargetHooks().encodeReturnAddress(*this, Address);
2206 return RValue::get(Result);
2207 }
2208 case Builtin::BI__builtin_dwarf_sp_column: {
2209 llvm::IntegerType *Ty
2210 = cast<llvm::IntegerType>(ConvertType(E->getType()));
2211 int Column = getTargetHooks().getDwarfEHStackPointer(CGM);
2212 if (Column == -1) {
2213 CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column");
2214 return RValue::get(llvm::UndefValue::get(Ty));
2215 }
2216 return RValue::get(llvm::ConstantInt::get(Ty, Column, true));
2217 }
2218 case Builtin::BI__builtin_init_dwarf_reg_size_table: {
2219 Value *Address = EmitScalarExpr(E->getArg(0));
2220 if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address))
2221 CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table");
2222 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
2223 }
2224 case Builtin::BI__builtin_eh_return: {
2225 Value *Int = EmitScalarExpr(E->getArg(0));
2226 Value *Ptr = EmitScalarExpr(E->getArg(1));
2227
2228 llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType());
2229 assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) &&(((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() ==
64) && "LLVM's __builtin_eh_return only supports 32- and 64-bit variants"
) ? static_cast<void> (0) : __assert_fail ("(IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) && \"LLVM's __builtin_eh_return only supports 32- and 64-bit variants\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 2230, __PRETTY_FUNCTION__))
2230 "LLVM's __builtin_eh_return only supports 32- and 64-bit variants")(((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() ==
64) && "LLVM's __builtin_eh_return only supports 32- and 64-bit variants"
) ? static_cast<void> (0) : __assert_fail ("(IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) && \"LLVM's __builtin_eh_return only supports 32- and 64-bit variants\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 2230, __PRETTY_FUNCTION__))
;
2231 Value *F = CGM.getIntrinsic(IntTy->getBitWidth() == 32
2232 ? Intrinsic::eh_return_i32
2233 : Intrinsic::eh_return_i64);
2234 Builder.CreateCall(F, {Int, Ptr});
2235 Builder.CreateUnreachable();
2236
2237 // We do need to preserve an insertion point.
2238 EmitBlock(createBasicBlock("builtin_eh_return.cont"));
2239
2240 return RValue::get(nullptr);
2241 }
2242 case Builtin::BI__builtin_unwind_init: {
2243 Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init);
2244 return RValue::get(Builder.CreateCall(F));
2245 }
2246 case Builtin::BI__builtin_extend_pointer: {
2247 // Extends a pointer to the size of an _Unwind_Word, which is
2248 // uint64_t on all platforms. Generally this gets poked into a
2249 // register and eventually used as an address, so if the
2250 // addressing registers are wider than pointers and the platform
2251 // doesn't implicitly ignore high-order bits when doing
2252 // addressing, we need to make sure we zext / sext based on
2253 // the platform's expectations.
2254 //
2255 // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html
2256
2257 // Cast the pointer to intptr_t.
2258 Value *Ptr = EmitScalarExpr(E->getArg(0));
2259 Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast");
2260
2261 // If that's 64 bits, we're done.
2262 if (IntPtrTy->getBitWidth() == 64)
2263 return RValue::get(Result);
2264
2265 // Otherwise, ask the codegen data what to do.
2266 if (getTargetHooks().extendPointerWithSExt())
2267 return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext"));
2268 else
2269 return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext"));
2270 }
2271 case Builtin::BI__builtin_setjmp: {
2272 // Buffer is a void**.
2273 Address Buf = EmitPointerWithAlignment(E->getArg(0));
2274
2275 // Store the frame pointer to the setjmp buffer.
2276 Value *FrameAddr =
2277 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::frameaddress),
2278 ConstantInt::get(Int32Ty, 0));
2279 Builder.CreateStore(FrameAddr, Buf);
2280
2281 // Store the stack pointer to the setjmp buffer.
2282 Value *StackAddr =
2283 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave));
2284 Address StackSaveSlot =
2285 Builder.CreateConstInBoundsGEP(Buf, 2, getPointerSize());
2286 Builder.CreateStore(StackAddr, StackSaveSlot);
2287
2288 // Call LLVM's EH setjmp, which is lightweight.
2289 Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp);
2290 Buf = Builder.CreateBitCast(Buf, Int8PtrTy);
2291 return RValue::get(Builder.CreateCall(F, Buf.getPointer()));
2292 }
2293 case Builtin::BI__builtin_longjmp: {
2294 Value *Buf = EmitScalarExpr(E->getArg(0));
2295 Buf = Builder.CreateBitCast(Buf, Int8PtrTy);
2296
2297 // Call LLVM's EH longjmp, which is lightweight.
2298 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp), Buf);
2299
2300 // longjmp doesn't return; mark this as unreachable.
2301 Builder.CreateUnreachable();
2302
2303 // We do need to preserve an insertion point.
2304 EmitBlock(createBasicBlock("longjmp.cont"));
2305
2306 return RValue::get(nullptr);
2307 }
2308 case Builtin::BI__sync_fetch_and_add:
2309 case Builtin::BI__sync_fetch_and_sub:
2310 case Builtin::BI__sync_fetch_and_or:
2311 case Builtin::BI__sync_fetch_and_and:
2312 case Builtin::BI__sync_fetch_and_xor:
2313 case Builtin::BI__sync_fetch_and_nand:
2314 case Builtin::BI__sync_add_and_fetch:
2315 case Builtin::BI__sync_sub_and_fetch:
2316 case Builtin::BI__sync_and_and_fetch:
2317 case Builtin::BI__sync_or_and_fetch:
2318 case Builtin::BI__sync_xor_and_fetch:
2319 case Builtin::BI__sync_nand_and_fetch:
2320 case Builtin::BI__sync_val_compare_and_swap:
2321 case Builtin::BI__sync_bool_compare_and_swap:
2322 case Builtin::BI__sync_lock_test_and_set:
2323 case Builtin::BI__sync_lock_release:
2324 case Builtin::BI__sync_swap:
2325 llvm_unreachable("Shouldn't make it through sema")::llvm::llvm_unreachable_internal("Shouldn't make it through sema"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 2325)
;
2326 case Builtin::BI__sync_fetch_and_add_1:
2327 case Builtin::BI__sync_fetch_and_add_2:
2328 case Builtin::BI__sync_fetch_and_add_4:
2329 case Builtin::BI__sync_fetch_and_add_8:
2330 case Builtin::BI__sync_fetch_and_add_16:
2331 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Add, E);
2332 case Builtin::BI__sync_fetch_and_sub_1:
2333 case Builtin::BI__sync_fetch_and_sub_2:
2334 case Builtin::BI__sync_fetch_and_sub_4:
2335 case Builtin::BI__sync_fetch_and_sub_8:
2336 case Builtin::BI__sync_fetch_and_sub_16:
2337 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Sub, E);
2338 case Builtin::BI__sync_fetch_and_or_1:
2339 case Builtin::BI__sync_fetch_and_or_2:
2340 case Builtin::BI__sync_fetch_and_or_4:
2341 case Builtin::BI__sync_fetch_and_or_8:
2342 case Builtin::BI__sync_fetch_and_or_16:
2343 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Or, E);
2344 case Builtin::BI__sync_fetch_and_and_1:
2345 case Builtin::BI__sync_fetch_and_and_2:
2346 case Builtin::BI__sync_fetch_and_and_4:
2347 case Builtin::BI__sync_fetch_and_and_8:
2348 case Builtin::BI__sync_fetch_and_and_16:
2349 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::And, E);
2350 case Builtin::BI__sync_fetch_and_xor_1:
2351 case Builtin::BI__sync_fetch_and_xor_2:
2352 case Builtin::BI__sync_fetch_and_xor_4:
2353 case Builtin::BI__sync_fetch_and_xor_8:
2354 case Builtin::BI__sync_fetch_and_xor_16:
2355 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xor, E);
2356 case Builtin::BI__sync_fetch_and_nand_1:
2357 case Builtin::BI__sync_fetch_and_nand_2:
2358 case Builtin::BI__sync_fetch_and_nand_4:
2359 case Builtin::BI__sync_fetch_and_nand_8:
2360 case Builtin::BI__sync_fetch_and_nand_16:
2361 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Nand, E);
2362
2363 // Clang extensions: not overloaded yet.
2364 case Builtin::BI__sync_fetch_and_min:
2365 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Min, E);
2366 case Builtin::BI__sync_fetch_and_max:
2367 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Max, E);
2368 case Builtin::BI__sync_fetch_and_umin:
2369 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::UMin, E);
2370 case Builtin::BI__sync_fetch_and_umax:
2371 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::UMax, E);
2372
2373 case Builtin::BI__sync_add_and_fetch_1:
2374 case Builtin::BI__sync_add_and_fetch_2:
2375 case Builtin::BI__sync_add_and_fetch_4:
2376 case Builtin::BI__sync_add_and_fetch_8:
2377 case Builtin::BI__sync_add_and_fetch_16:
2378 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Add, E,
2379 llvm::Instruction::Add);
2380 case Builtin::BI__sync_sub_and_fetch_1:
2381 case Builtin::BI__sync_sub_and_fetch_2:
2382 case Builtin::BI__sync_sub_and_fetch_4:
2383 case Builtin::BI__sync_sub_and_fetch_8:
2384 case Builtin::BI__sync_sub_and_fetch_16:
2385 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Sub, E,
2386 llvm::Instruction::Sub);
2387 case Builtin::BI__sync_and_and_fetch_1:
2388 case Builtin::BI__sync_and_and_fetch_2:
2389 case Builtin::BI__sync_and_and_fetch_4:
2390 case Builtin::BI__sync_and_and_fetch_8:
2391 case Builtin::BI__sync_and_and_fetch_16:
2392 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::And, E,
2393 llvm::Instruction::And);
2394 case Builtin::BI__sync_or_and_fetch_1:
2395 case Builtin::BI__sync_or_and_fetch_2:
2396 case Builtin::BI__sync_or_and_fetch_4:
2397 case Builtin::BI__sync_or_and_fetch_8:
2398 case Builtin::BI__sync_or_and_fetch_16:
2399 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Or, E,
2400 llvm::Instruction::Or);
2401 case Builtin::BI__sync_xor_and_fetch_1:
2402 case Builtin::BI__sync_xor_and_fetch_2:
2403 case Builtin::BI__sync_xor_and_fetch_4:
2404 case Builtin::BI__sync_xor_and_fetch_8:
2405 case Builtin::BI__sync_xor_and_fetch_16:
2406 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Xor, E,
2407 llvm::Instruction::Xor);
2408 case Builtin::BI__sync_nand_and_fetch_1:
2409 case Builtin::BI__sync_nand_and_fetch_2:
2410 case Builtin::BI__sync_nand_and_fetch_4:
2411 case Builtin::BI__sync_nand_and_fetch_8:
2412 case Builtin::BI__sync_nand_and_fetch_16:
2413 return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Nand, E,
2414 llvm::Instruction::And, true);
2415
2416 case Builtin::BI__sync_val_compare_and_swap_1:
2417 case Builtin::BI__sync_val_compare_and_swap_2:
2418 case Builtin::BI__sync_val_compare_and_swap_4:
2419 case Builtin::BI__sync_val_compare_and_swap_8:
2420 case Builtin::BI__sync_val_compare_and_swap_16:
2421 return RValue::get(MakeAtomicCmpXchgValue(*this, E, false));
2422
2423 case Builtin::BI__sync_bool_compare_and_swap_1:
2424 case Builtin::BI__sync_bool_compare_and_swap_2:
2425 case Builtin::BI__sync_bool_compare_and_swap_4:
2426 case Builtin::BI__sync_bool_compare_and_swap_8:
2427 case Builtin::BI__sync_bool_compare_and_swap_16:
2428 return RValue::get(MakeAtomicCmpXchgValue(*this, E, true));
2429
2430 case Builtin::BI__sync_swap_1:
2431 case Builtin::BI__sync_swap_2:
2432 case Builtin::BI__sync_swap_4:
2433 case Builtin::BI__sync_swap_8:
2434 case Builtin::BI__sync_swap_16:
2435 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E);
2436
2437 case Builtin::BI__sync_lock_test_and_set_1:
2438 case Builtin::BI__sync_lock_test_and_set_2:
2439 case Builtin::BI__sync_lock_test_and_set_4:
2440 case Builtin::BI__sync_lock_test_and_set_8:
2441 case Builtin::BI__sync_lock_test_and_set_16:
2442 return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E);
2443
2444 case Builtin::BI__sync_lock_release_1:
2445 case Builtin::BI__sync_lock_release_2:
2446 case Builtin::BI__sync_lock_release_4:
2447 case Builtin::BI__sync_lock_release_8:
2448 case Builtin::BI__sync_lock_release_16: {
2449 Value *Ptr = EmitScalarExpr(E->getArg(0));
2450 QualType ElTy = E->getArg(0)->getType()->getPointeeType();
2451 CharUnits StoreSize = getContext().getTypeSizeInChars(ElTy);
2452 llvm::Type *ITy = llvm::IntegerType::get(getLLVMContext(),
2453 StoreSize.getQuantity() * 8);
2454 Ptr = Builder.CreateBitCast(Ptr, ITy->getPointerTo());
2455 llvm::StoreInst *Store =
2456 Builder.CreateAlignedStore(llvm::Constant::getNullValue(ITy), Ptr,
2457 StoreSize);
2458 Store->setAtomic(llvm::AtomicOrdering::Release);
2459 return RValue::get(nullptr);
2460 }
2461
2462 case Builtin::BI__sync_synchronize: {
2463 // We assume this is supposed to correspond to a C++0x-style
2464 // sequentially-consistent fence (i.e. this is only usable for
2465 // synchronization, not device I/O or anything like that). This intrinsic
2466 // is really badly designed in the sense that in theory, there isn't
2467 // any way to safely use it... but in practice, it mostly works
2468 // to use it with non-atomic loads and stores to get acquire/release
2469 // semantics.
2470 Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent);
2471 return RValue::get(nullptr);
2472 }
2473
2474 case Builtin::BI__builtin_nontemporal_load:
2475 return RValue::get(EmitNontemporalLoad(*this, E));
2476 case Builtin::BI__builtin_nontemporal_store:
2477 return RValue::get(EmitNontemporalStore(*this, E));
2478 case Builtin::BI__c11_atomic_is_lock_free:
2479 case Builtin::BI__atomic_is_lock_free: {
2480 // Call "bool __atomic_is_lock_free(size_t size, void *ptr)". For the
2481 // __c11 builtin, ptr is 0 (indicating a properly-aligned object), since
2482 // _Atomic(T) is always properly-aligned.
2483 const char *LibCallName = "__atomic_is_lock_free";
2484 CallArgList Args;
2485 Args.add(RValue::get(EmitScalarExpr(E->getArg(0))),
2486 getContext().getSizeType());
2487 if (BuiltinID == Builtin::BI__atomic_is_lock_free)
2488 Args.add(RValue::get(EmitScalarExpr(E->getArg(1))),
2489 getContext().VoidPtrTy);
2490 else
2491 Args.add(RValue::get(llvm::Constant::getNullValue(VoidPtrTy)),
2492 getContext().VoidPtrTy);
2493 const CGFunctionInfo &FuncInfo =
2494 CGM.getTypes().arrangeBuiltinFunctionCall(E->getType(), Args);
2495 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo);
2496 llvm::Constant *Func = CGM.CreateRuntimeFunction(FTy, LibCallName);
2497 return EmitCall(FuncInfo, CGCallee::forDirect(Func),
2498 ReturnValueSlot(), Args);
2499 }
2500
2501 case Builtin::BI__atomic_test_and_set: {
2502 // Look at the argument type to determine whether this is a volatile
2503 // operation. The parameter type is always volatile.
2504 QualType PtrTy = E->getArg(0)->IgnoreImpCasts()->getType();
2505 bool Volatile =
2506 PtrTy->castAs<PointerType>()->getPointeeType().isVolatileQualified();
2507
2508 Value *Ptr = EmitScalarExpr(E->getArg(0));
2509 unsigned AddrSpace = Ptr->getType()->getPointerAddressSpace();
2510 Ptr = Builder.CreateBitCast(Ptr, Int8Ty->getPointerTo(AddrSpace));
2511 Value *NewVal = Builder.getInt8(1);
2512 Value *Order = EmitScalarExpr(E->getArg(1));
2513 if (isa<llvm::ConstantInt>(Order)) {
2514 int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
2515 AtomicRMWInst *Result = nullptr;
2516 switch (ord) {
2517 case 0: // memory_order_relaxed
2518 default: // invalid order
2519 Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
2520 llvm::AtomicOrdering::Monotonic);
2521 break;
2522 case 1: // memory_order_consume
2523 case 2: // memory_order_acquire
2524 Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
2525 llvm::AtomicOrdering::Acquire);
2526 break;
2527 case 3: // memory_order_release
2528 Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
2529 llvm::AtomicOrdering::Release);
2530 break;
2531 case 4: // memory_order_acq_rel
2532
2533 Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
2534 llvm::AtomicOrdering::AcquireRelease);
2535 break;
2536 case 5: // memory_order_seq_cst
2537 Result = Builder.CreateAtomicRMW(
2538 llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
2539 llvm::AtomicOrdering::SequentiallyConsistent);
2540 break;
2541 }
2542 Result->setVolatile(Volatile);
2543 return RValue::get(Builder.CreateIsNotNull(Result, "tobool"));
2544 }
2545
2546 llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
2547
2548 llvm::BasicBlock *BBs[5] = {
2549 createBasicBlock("monotonic", CurFn),
2550 createBasicBlock("acquire", CurFn),
2551 createBasicBlock("release", CurFn),
2552 createBasicBlock("acqrel", CurFn),
2553 createBasicBlock("seqcst", CurFn)
2554 };
2555 llvm::AtomicOrdering Orders[5] = {
2556 llvm::AtomicOrdering::Monotonic, llvm::AtomicOrdering::Acquire,
2557 llvm::AtomicOrdering::Release, llvm::AtomicOrdering::AcquireRelease,
2558 llvm::AtomicOrdering::SequentiallyConsistent};
2559
2560 Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
2561 llvm::SwitchInst *SI = Builder.CreateSwitch(Order, BBs[0]);
2562
2563 Builder.SetInsertPoint(ContBB);
2564 PHINode *Result = Builder.CreatePHI(Int8Ty, 5, "was_set");
2565
2566 for (unsigned i = 0; i < 5; ++i) {
2567 Builder.SetInsertPoint(BBs[i]);
2568 AtomicRMWInst *RMW = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg,
2569 Ptr, NewVal, Orders[i]);
2570 RMW->setVolatile(Volatile);
2571 Result->addIncoming(RMW, BBs[i]);
2572 Builder.CreateBr(ContBB);
2573 }
2574
2575 SI->addCase(Builder.getInt32(0), BBs[0]);
2576 SI->addCase(Builder.getInt32(1), BBs[1]);
2577 SI->addCase(Builder.getInt32(2), BBs[1]);
2578 SI->addCase(Builder.getInt32(3), BBs[2]);
2579 SI->addCase(Builder.getInt32(4), BBs[3]);
2580 SI->addCase(Builder.getInt32(5), BBs[4]);
2581
2582 Builder.SetInsertPoint(ContBB);
2583 return RValue::get(Builder.CreateIsNotNull(Result, "tobool"));
2584 }
2585
2586 case Builtin::BI__atomic_clear: {
2587 QualType PtrTy = E->getArg(0)->IgnoreImpCasts()->getType();
2588 bool Volatile =
2589 PtrTy->castAs<PointerType>()->getPointeeType().isVolatileQualified();
2590
2591 Address Ptr = EmitPointerWithAlignment(E->getArg(0));
2592 unsigned AddrSpace = Ptr.getPointer()->getType()->getPointerAddressSpace();
2593 Ptr = Builder.CreateBitCast(Ptr, Int8Ty->getPointerTo(AddrSpace));
2594 Value *NewVal = Builder.getInt8(0);
2595 Value *Order = EmitScalarExpr(E->getArg(1));
2596 if (isa<llvm::ConstantInt>(Order)) {
2597 int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
2598 StoreInst *Store = Builder.CreateStore(NewVal, Ptr, Volatile);
2599 switch (ord) {
2600 case 0: // memory_order_relaxed
2601 default: // invalid order
2602 Store->setOrdering(llvm::AtomicOrdering::Monotonic);
2603 break;
2604 case 3: // memory_order_release
2605 Store->setOrdering(llvm::AtomicOrdering::Release);
2606 break;
2607 case 5: // memory_order_seq_cst
2608 Store->setOrdering(llvm::AtomicOrdering::SequentiallyConsistent);
2609 break;
2610 }
2611 return RValue::get(nullptr);
2612 }
2613
2614 llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
2615
2616 llvm::BasicBlock *BBs[3] = {
2617 createBasicBlock("monotonic", CurFn),
2618 createBasicBlock("release", CurFn),
2619 createBasicBlock("seqcst", CurFn)
2620 };
2621 llvm::AtomicOrdering Orders[3] = {
2622 llvm::AtomicOrdering::Monotonic, llvm::AtomicOrdering::Release,
2623 llvm::AtomicOrdering::SequentiallyConsistent};
2624
2625 Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
2626 llvm::SwitchInst *SI = Builder.CreateSwitch(Order, BBs[0]);
2627
2628 for (unsigned i = 0; i < 3; ++i) {
2629 Builder.SetInsertPoint(BBs[i]);
2630 StoreInst *Store = Builder.CreateStore(NewVal, Ptr, Volatile);
2631 Store->setOrdering(Orders[i]);
2632 Builder.CreateBr(ContBB);
2633 }
2634
2635 SI->addCase(Builder.getInt32(0), BBs[0]);
2636 SI->addCase(Builder.getInt32(3), BBs[1]);
2637 SI->addCase(Builder.getInt32(5), BBs[2]);
2638
2639 Builder.SetInsertPoint(ContBB);
2640 return RValue::get(nullptr);
2641 }
2642
2643 case Builtin::BI__atomic_thread_fence:
2644 case Builtin::BI__atomic_signal_fence:
2645 case Builtin::BI__c11_atomic_thread_fence:
2646 case Builtin::BI__c11_atomic_signal_fence: {
2647 llvm::SyncScope::ID SSID;
2648 if (BuiltinID == Builtin::BI__atomic_signal_fence ||
2649 BuiltinID == Builtin::BI__c11_atomic_signal_fence)
2650 SSID = llvm::SyncScope::SingleThread;
2651 else
2652 SSID = llvm::SyncScope::System;
2653 Value *Order = EmitScalarExpr(E->getArg(0));
2654 if (isa<llvm::ConstantInt>(Order)) {
2655 int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
2656 switch (ord) {
2657 case 0: // memory_order_relaxed
2658 default: // invalid order
2659 break;
2660 case 1: // memory_order_consume
2661 case 2: // memory_order_acquire
2662 Builder.CreateFence(llvm::AtomicOrdering::Acquire, SSID);
2663 break;
2664 case 3: // memory_order_release
2665 Builder.CreateFence(llvm::AtomicOrdering::Release, SSID);
2666 break;
2667 case 4: // memory_order_acq_rel
2668 Builder.CreateFence(llvm::AtomicOrdering::AcquireRelease, SSID);
2669 break;
2670 case 5: // memory_order_seq_cst
2671 Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent, SSID);
2672 break;
2673 }
2674 return RValue::get(nullptr);
2675 }
2676
2677 llvm::BasicBlock *AcquireBB, *ReleaseBB, *AcqRelBB, *SeqCstBB;
2678 AcquireBB = createBasicBlock("acquire", CurFn);
2679 ReleaseBB = createBasicBlock("release", CurFn);
2680 AcqRelBB = createBasicBlock("acqrel", CurFn);
2681 SeqCstBB = createBasicBlock("seqcst", CurFn);
2682 llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
2683
2684 Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
2685 llvm::SwitchInst *SI = Builder.CreateSwitch(Order, ContBB);
2686
2687 Builder.SetInsertPoint(AcquireBB);
2688 Builder.CreateFence(llvm::AtomicOrdering::Acquire, SSID);
2689 Builder.CreateBr(ContBB);
2690 SI->addCase(Builder.getInt32(1), AcquireBB);
2691 SI->addCase(Builder.getInt32(2), AcquireBB);
2692
2693 Builder.SetInsertPoint(ReleaseBB);
2694 Builder.CreateFence(llvm::AtomicOrdering::Release, SSID);
2695 Builder.CreateBr(ContBB);
2696 SI->addCase(Builder.getInt32(3), ReleaseBB);
2697
2698 Builder.SetInsertPoint(AcqRelBB);
2699 Builder.CreateFence(llvm::AtomicOrdering::AcquireRelease, SSID);
2700 Builder.CreateBr(ContBB);
2701 SI->addCase(Builder.getInt32(4), AcqRelBB);
2702
2703 Builder.SetInsertPoint(SeqCstBB);
2704 Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent, SSID);
2705 Builder.CreateBr(ContBB);
2706 SI->addCase(Builder.getInt32(5), SeqCstBB);
2707
2708 Builder.SetInsertPoint(ContBB);
2709 return RValue::get(nullptr);
2710 }
2711
2712 case Builtin::BI__builtin_signbit:
2713 case Builtin::BI__builtin_signbitf:
2714 case Builtin::BI__builtin_signbitl: {
2715 return RValue::get(
2716 Builder.CreateZExt(EmitSignBit(*this, EmitScalarExpr(E->getArg(0))),
2717 ConvertType(E->getType())));
2718 }
2719 case Builtin::BI__annotation: {
2720 // Re-encode each wide string to UTF8 and make an MDString.
2721 SmallVector<Metadata *, 1> Strings;
2722 for (const Expr *Arg : E->arguments()) {
2723 const auto *Str = cast<StringLiteral>(Arg->IgnoreParenCasts());
2724 assert(Str->getCharByteWidth() == 2)((Str->getCharByteWidth() == 2) ? static_cast<void> (
0) : __assert_fail ("Str->getCharByteWidth() == 2", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 2724, __PRETTY_FUNCTION__))
;
2725 StringRef WideBytes = Str->getBytes();
2726 std::string StrUtf8;
2727 if (!convertUTF16ToUTF8String(
2728 makeArrayRef(WideBytes.data(), WideBytes.size()), StrUtf8)) {
2729 CGM.ErrorUnsupported(E, "non-UTF16 __annotation argument");
2730 continue;
2731 }
2732 Strings.push_back(llvm::MDString::get(getLLVMContext(), StrUtf8));
2733 }
2734
2735 // Build and MDTuple of MDStrings and emit the intrinsic call.
2736 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::codeview_annotation, {});
2737 MDTuple *StrTuple = MDTuple::get(getLLVMContext(), Strings);
2738 Builder.CreateCall(F, MetadataAsValue::get(getLLVMContext(), StrTuple));
2739 return RValue::getIgnored();
2740 }
2741 case Builtin::BI__builtin_annotation: {
2742 llvm::Value *AnnVal = EmitScalarExpr(E->getArg(0));
2743 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::annotation,
2744 AnnVal->getType());
2745
2746 // Get the annotation string, go through casts. Sema requires this to be a
2747 // non-wide string literal, potentially casted, so the cast<> is safe.
2748 const Expr *AnnotationStrExpr = E->getArg(1)->IgnoreParenCasts();
2749 StringRef Str = cast<StringLiteral>(AnnotationStrExpr)->getString();
2750 return RValue::get(EmitAnnotationCall(F, AnnVal, Str, E->getExprLoc()));
2751 }
2752 case Builtin::BI__builtin_addcb:
2753 case Builtin::BI__builtin_addcs:
2754 case Builtin::BI__builtin_addc:
2755 case Builtin::BI__builtin_addcl:
2756 case Builtin::BI__builtin_addcll:
2757 case Builtin::BI__builtin_subcb:
2758 case Builtin::BI__builtin_subcs:
2759 case Builtin::BI__builtin_subc:
2760 case Builtin::BI__builtin_subcl:
2761 case Builtin::BI__builtin_subcll: {
2762
2763 // We translate all of these builtins from expressions of the form:
2764 // int x = ..., y = ..., carryin = ..., carryout, result;
2765 // result = __builtin_addc(x, y, carryin, &carryout);
2766 //
2767 // to LLVM IR of the form:
2768 //
2769 // %tmp1 = call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)
2770 // %tmpsum1 = extractvalue {i32, i1} %tmp1, 0
2771 // %carry1 = extractvalue {i32, i1} %tmp1, 1
2772 // %tmp2 = call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %tmpsum1,
2773 // i32 %carryin)
2774 // %result = extractvalue {i32, i1} %tmp2, 0
2775 // %carry2 = extractvalue {i32, i1} %tmp2, 1
2776 // %tmp3 = or i1 %carry1, %carry2
2777 // %tmp4 = zext i1 %tmp3 to i32
2778 // store i32 %tmp4, i32* %carryout
2779
2780 // Scalarize our inputs.
2781 llvm::Value *X = EmitScalarExpr(E->getArg(0));
2782 llvm::Value *Y = EmitScalarExpr(E->getArg(1));
2783 llvm::Value *Carryin = EmitScalarExpr(E->getArg(2));
2784 Address CarryOutPtr = EmitPointerWithAlignment(E->getArg(3));
2785
2786 // Decide if we are lowering to a uadd.with.overflow or usub.with.overflow.
2787 llvm::Intrinsic::ID IntrinsicId;
2788 switch (BuiltinID) {
2789 default: llvm_unreachable("Unknown multiprecision builtin id.")::llvm::llvm_unreachable_internal("Unknown multiprecision builtin id."
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 2789)
;
2790 case Builtin::BI__builtin_addcb:
2791 case Builtin::BI__builtin_addcs:
2792 case Builtin::BI__builtin_addc:
2793 case Builtin::BI__builtin_addcl:
2794 case Builtin::BI__builtin_addcll:
2795 IntrinsicId = llvm::Intrinsic::uadd_with_overflow;
2796 break;
2797 case Builtin::BI__builtin_subcb:
2798 case Builtin::BI__builtin_subcs:
2799 case Builtin::BI__builtin_subc:
2800 case Builtin::BI__builtin_subcl:
2801 case Builtin::BI__builtin_subcll:
2802 IntrinsicId = llvm::Intrinsic::usub_with_overflow;
2803 break;
2804 }
2805
2806 // Construct our resulting LLVM IR expression.
2807 llvm::Value *Carry1;
2808 llvm::Value *Sum1 = EmitOverflowIntrinsic(*this, IntrinsicId,
2809 X, Y, Carry1);
2810 llvm::Value *Carry2;
2811 llvm::Value *Sum2 = EmitOverflowIntrinsic(*this, IntrinsicId,
2812 Sum1, Carryin, Carry2);
2813 llvm::Value *CarryOut = Builder.CreateZExt(Builder.CreateOr(Carry1, Carry2),
2814 X->getType());
2815 Builder.CreateStore(CarryOut, CarryOutPtr);
2816 return RValue::get(Sum2);
2817 }
2818
2819 case Builtin::BI__builtin_add_overflow:
2820 case Builtin::BI__builtin_sub_overflow:
2821 case Builtin::BI__builtin_mul_overflow: {
2822 const clang::Expr *LeftArg = E->getArg(0);
2823 const clang::Expr *RightArg = E->getArg(1);
2824 const clang::Expr *ResultArg = E->getArg(2);
2825
2826 clang::QualType ResultQTy =
2827 ResultArg->getType()->castAs<PointerType>()->getPointeeType();
2828
2829 WidthAndSignedness LeftInfo =
2830 getIntegerWidthAndSignedness(CGM.getContext(), LeftArg->getType());
2831 WidthAndSignedness RightInfo =
2832 getIntegerWidthAndSignedness(CGM.getContext(), RightArg->getType());
2833 WidthAndSignedness ResultInfo =
2834 getIntegerWidthAndSignedness(CGM.getContext(), ResultQTy);
2835
2836 // Handle mixed-sign multiplication as a special case, because adding
2837 // runtime or backend support for our generic irgen would be too expensive.
2838 if (isSpecialMixedSignMultiply(BuiltinID, LeftInfo, RightInfo, ResultInfo))
2839 return EmitCheckedMixedSignMultiply(*this, LeftArg, LeftInfo, RightArg,
2840 RightInfo, ResultArg, ResultQTy,
2841 ResultInfo);
2842
2843 WidthAndSignedness EncompassingInfo =
2844 EncompassingIntegerType({LeftInfo, RightInfo, ResultInfo});
2845
2846 llvm::Type *EncompassingLLVMTy =
2847 llvm::IntegerType::get(CGM.getLLVMContext(), EncompassingInfo.Width);
2848
2849 llvm::Type *ResultLLVMTy = CGM.getTypes().ConvertType(ResultQTy);
2850
2851 llvm::Intrinsic::ID IntrinsicId;
2852 switch (BuiltinID) {
2853 default:
2854 llvm_unreachable("Unknown overflow builtin id.")::llvm::llvm_unreachable_internal("Unknown overflow builtin id."
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 2854)
;
2855 case Builtin::BI__builtin_add_overflow:
2856 IntrinsicId = EncompassingInfo.Signed
2857 ? llvm::Intrinsic::sadd_with_overflow
2858 : llvm::Intrinsic::uadd_with_overflow;
2859 break;
2860 case Builtin::BI__builtin_sub_overflow:
2861 IntrinsicId = EncompassingInfo.Signed
2862 ? llvm::Intrinsic::ssub_with_overflow
2863 : llvm::Intrinsic::usub_with_overflow;
2864 break;
2865 case Builtin::BI__builtin_mul_overflow:
2866 IntrinsicId = EncompassingInfo.Signed
2867 ? llvm::Intrinsic::smul_with_overflow
2868 : llvm::Intrinsic::umul_with_overflow;
2869 break;
2870 }
2871
2872 llvm::Value *Left = EmitScalarExpr(LeftArg);
2873 llvm::Value *Right = EmitScalarExpr(RightArg);
2874 Address ResultPtr = EmitPointerWithAlignment(ResultArg);
2875
2876 // Extend each operand to the encompassing type.
2877 Left = Builder.CreateIntCast(Left, EncompassingLLVMTy, LeftInfo.Signed);
2878 Right = Builder.CreateIntCast(Right, EncompassingLLVMTy, RightInfo.Signed);
2879
2880 // Perform the operation on the extended values.
2881 llvm::Value *Overflow, *Result;
2882 Result = EmitOverflowIntrinsic(*this, IntrinsicId, Left, Right, Overflow);
2883
2884 if (EncompassingInfo.Width > ResultInfo.Width) {
2885 // The encompassing type is wider than the result type, so we need to
2886 // truncate it.
2887 llvm::Value *ResultTrunc = Builder.CreateTrunc(Result, ResultLLVMTy);
2888
2889 // To see if the truncation caused an overflow, we will extend
2890 // the result and then compare it to the original result.
2891 llvm::Value *ResultTruncExt = Builder.CreateIntCast(
2892 ResultTrunc, EncompassingLLVMTy, ResultInfo.Signed);
2893 llvm::Value *TruncationOverflow =
2894 Builder.CreateICmpNE(Result, ResultTruncExt);
2895
2896 Overflow = Builder.CreateOr(Overflow, TruncationOverflow);
2897 Result = ResultTrunc;
2898 }
2899
2900 // Finally, store the result using the pointer.
2901 bool isVolatile =
2902 ResultArg->getType()->getPointeeType().isVolatileQualified();
2903 Builder.CreateStore(EmitToMemory(Result, ResultQTy), ResultPtr, isVolatile);
2904
2905 return RValue::get(Overflow);
2906 }
2907
2908 case Builtin::BI__builtin_uadd_overflow:
2909 case Builtin::BI__builtin_uaddl_overflow:
2910 case Builtin::BI__builtin_uaddll_overflow:
2911 case Builtin::BI__builtin_usub_overflow:
2912 case Builtin::BI__builtin_usubl_overflow:
2913 case Builtin::BI__builtin_usubll_overflow:
2914 case Builtin::BI__builtin_umul_overflow:
2915 case Builtin::BI__builtin_umull_overflow:
2916 case Builtin::BI__builtin_umulll_overflow:
2917 case Builtin::BI__builtin_sadd_overflow:
2918 case Builtin::BI__builtin_saddl_overflow:
2919 case Builtin::BI__builtin_saddll_overflow:
2920 case Builtin::BI__builtin_ssub_overflow:
2921 case Builtin::BI__builtin_ssubl_overflow:
2922 case Builtin::BI__builtin_ssubll_overflow:
2923 case Builtin::BI__builtin_smul_overflow:
2924 case Builtin::BI__builtin_smull_overflow:
2925 case Builtin::BI__builtin_smulll_overflow: {
2926
2927 // We translate all of these builtins directly to the relevant llvm IR node.
2928
2929 // Scalarize our inputs.
2930 llvm::Value *X = EmitScalarExpr(E->getArg(0));
2931 llvm::Value *Y = EmitScalarExpr(E->getArg(1));
2932 Address SumOutPtr = EmitPointerWithAlignment(E->getArg(2));
2933
2934 // Decide which of the overflow intrinsics we are lowering to:
2935 llvm::Intrinsic::ID IntrinsicId;
2936 switch (BuiltinID) {
2937 default: llvm_unreachable("Unknown overflow builtin id.")::llvm::llvm_unreachable_internal("Unknown overflow builtin id."
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 2937)
;
2938 case Builtin::BI__builtin_uadd_overflow:
2939 case Builtin::BI__builtin_uaddl_overflow:
2940 case Builtin::BI__builtin_uaddll_overflow:
2941 IntrinsicId = llvm::Intrinsic::uadd_with_overflow;
2942 break;
2943 case Builtin::BI__builtin_usub_overflow:
2944 case Builtin::BI__builtin_usubl_overflow:
2945 case Builtin::BI__builtin_usubll_overflow:
2946 IntrinsicId = llvm::Intrinsic::usub_with_overflow;
2947 break;
2948 case Builtin::BI__builtin_umul_overflow:
2949 case Builtin::BI__builtin_umull_overflow:
2950 case Builtin::BI__builtin_umulll_overflow:
2951 IntrinsicId = llvm::Intrinsic::umul_with_overflow;
2952 break;
2953 case Builtin::BI__builtin_sadd_overflow:
2954 case Builtin::BI__builtin_saddl_overflow:
2955 case Builtin::BI__builtin_saddll_overflow:
2956 IntrinsicId = llvm::Intrinsic::sadd_with_overflow;
2957 break;
2958 case Builtin::BI__builtin_ssub_overflow:
2959 case Builtin::BI__builtin_ssubl_overflow:
2960 case Builtin::BI__builtin_ssubll_overflow:
2961 IntrinsicId = llvm::Intrinsic::ssub_with_overflow;
2962 break;
2963 case Builtin::BI__builtin_smul_overflow:
2964 case Builtin::BI__builtin_smull_overflow:
2965 case Builtin::BI__builtin_smulll_overflow:
2966 IntrinsicId = llvm::Intrinsic::smul_with_overflow;
2967 break;
2968 }
2969
2970
2971 llvm::Value *Carry;
2972 llvm::Value *Sum = EmitOverflowIntrinsic(*this, IntrinsicId, X, Y, Carry);
2973 Builder.CreateStore(Sum, SumOutPtr);
2974
2975 return RValue::get(Carry);
2976 }
2977 case Builtin::BI__builtin_addressof:
2978 return RValue::get(EmitLValue(E->getArg(0)).getPointer());
2979 case Builtin::BI__builtin_operator_new:
2980 return EmitBuiltinNewDeleteCall(
2981 E->getCallee()->getType()->castAs<FunctionProtoType>(), E, false);
2982 case Builtin::BI__builtin_operator_delete:
2983 return EmitBuiltinNewDeleteCall(
2984 E->getCallee()->getType()->castAs<FunctionProtoType>(), E, true);
2985
2986 case Builtin::BI__noop:
2987 // __noop always evaluates to an integer literal zero.
2988 return RValue::get(ConstantInt::get(IntTy, 0));
2989 case Builtin::BI__builtin_call_with_static_chain: {
2990 const CallExpr *Call = cast<CallExpr>(E->getArg(0));
2991 const Expr *Chain = E->getArg(1);
2992 return EmitCall(Call->getCallee()->getType(),
2993 EmitCallee(Call->getCallee()), Call, ReturnValue,
2994 EmitScalarExpr(Chain));
2995 }
2996 case Builtin::BI_InterlockedExchange8:
2997 case Builtin::BI_InterlockedExchange16:
2998 case Builtin::BI_InterlockedExchange:
2999 case Builtin::BI_InterlockedExchangePointer:
3000 return RValue::get(
3001 EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedExchange, E));
3002 case Builtin::BI_InterlockedCompareExchangePointer:
3003 case Builtin::BI_InterlockedCompareExchangePointer_nf: {
3004 llvm::Type *RTy;
3005 llvm::IntegerType *IntType =
3006 IntegerType::get(getLLVMContext(),
3007 getContext().getTypeSize(E->getType()));
3008 llvm::Type *IntPtrType = IntType->getPointerTo();
3009
3010 llvm::Value *Destination =
3011 Builder.CreateBitCast(EmitScalarExpr(E->getArg(0)), IntPtrType);
3012
3013 llvm::Value *Exchange = EmitScalarExpr(E->getArg(1));
3014 RTy = Exchange->getType();
3015 Exchange = Builder.CreatePtrToInt(Exchange, IntType);
3016
3017 llvm::Value *Comparand =
3018 Builder.CreatePtrToInt(EmitScalarExpr(E->getArg(2)), IntType);
3019
3020 auto Ordering =
3021 BuiltinID == Builtin::BI_InterlockedCompareExchangePointer_nf ?
3022 AtomicOrdering::Monotonic : AtomicOrdering::SequentiallyConsistent;
3023
3024 auto Result = Builder.CreateAtomicCmpXchg(Destination, Comparand, Exchange,
3025 Ordering, Ordering);
3026 Result->setVolatile(true);
3027
3028 return RValue::get(Builder.CreateIntToPtr(Builder.CreateExtractValue(Result,
3029 0),
3030 RTy));
3031 }
3032 case Builtin::BI_InterlockedCompareExchange8:
3033 case Builtin::BI_InterlockedCompareExchange16:
3034 case Builtin::BI_InterlockedCompareExchange:
3035 case Builtin::BI_InterlockedCompareExchange64: {
3036 AtomicCmpXchgInst *CXI = Builder.CreateAtomicCmpXchg(
3037 EmitScalarExpr(E->getArg(0)),
3038 EmitScalarExpr(E->getArg(2)),
3039 EmitScalarExpr(E->getArg(1)),
3040 AtomicOrdering::SequentiallyConsistent,
3041 AtomicOrdering::SequentiallyConsistent);
3042 CXI->setVolatile(true);
3043 return RValue::get(Builder.CreateExtractValue(CXI, 0));
3044 }
3045 case Builtin::BI_InterlockedIncrement16:
3046 case Builtin::BI_InterlockedIncrement:
3047 return RValue::get(
3048 EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedIncrement, E));
3049 case Builtin::BI_InterlockedDecrement16:
3050 case Builtin::BI_InterlockedDecrement:
3051 return RValue::get(
3052 EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedDecrement, E));
3053 case Builtin::BI_InterlockedAnd8:
3054 case Builtin::BI_InterlockedAnd16:
3055 case Builtin::BI_InterlockedAnd:
3056 return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedAnd, E));
3057 case Builtin::BI_InterlockedExchangeAdd8:
3058 case Builtin::BI_InterlockedExchangeAdd16:
3059 case Builtin::BI_InterlockedExchangeAdd:
3060 return RValue::get(
3061 EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedExchangeAdd, E));
3062 case Builtin::BI_InterlockedExchangeSub8:
3063 case Builtin::BI_InterlockedExchangeSub16:
3064 case Builtin::BI_InterlockedExchangeSub:
3065 return RValue::get(
3066 EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedExchangeSub, E));
3067 case Builtin::BI_InterlockedOr8:
3068 case Builtin::BI_InterlockedOr16:
3069 case Builtin::BI_InterlockedOr:
3070 return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedOr, E));
3071 case Builtin::BI_InterlockedXor8:
3072 case Builtin::BI_InterlockedXor16:
3073 case Builtin::BI_InterlockedXor:
3074 return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedXor, E));
3075
3076 case Builtin::BI_bittest64:
3077 case Builtin::BI_bittest:
3078 case Builtin::BI_bittestandcomplement64:
3079 case Builtin::BI_bittestandcomplement:
3080 case Builtin::BI_bittestandreset64:
3081 case Builtin::BI_bittestandreset:
3082 case Builtin::BI_bittestandset64:
3083 case Builtin::BI_bittestandset:
3084 case Builtin::BI_interlockedbittestandreset:
3085 case Builtin::BI_interlockedbittestandreset64:
3086 case Builtin::BI_interlockedbittestandset64:
3087 case Builtin::BI_interlockedbittestandset:
3088 case Builtin::BI_interlockedbittestandset_acq:
3089 case Builtin::BI_interlockedbittestandset_rel:
3090 case Builtin::BI_interlockedbittestandset_nf:
3091 case Builtin::BI_interlockedbittestandreset_acq:
3092 case Builtin::BI_interlockedbittestandreset_rel:
3093 case Builtin::BI_interlockedbittestandreset_nf:
3094 return RValue::get(EmitBitTestIntrinsic(*this, BuiltinID, E));
3095
3096 case Builtin::BI__exception_code:
3097 case Builtin::BI_exception_code:
3098 return RValue::get(EmitSEHExceptionCode());
3099 case Builtin::BI__exception_info:
3100 case Builtin::BI_exception_info:
3101 return RValue::get(EmitSEHExceptionInfo());
3102 case Builtin::BI__abnormal_termination:
3103 case Builtin::BI_abnormal_termination:
3104 return RValue::get(EmitSEHAbnormalTermination());
3105 case Builtin::BI_setjmpex:
3106 if (getTarget().getTriple().isOSMSVCRT())
3107 return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmpex, E);
3108 break;
3109 case Builtin::BI_setjmp:
3110 if (getTarget().getTriple().isOSMSVCRT()) {
3111 if (getTarget().getTriple().getArch() == llvm::Triple::x86)
3112 return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmp3, E);
3113 else if (getTarget().getTriple().getArch() == llvm::Triple::aarch64)
3114 return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmpex, E);
3115 return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmp, E);
3116 }
3117 break;
3118
3119 case Builtin::BI__GetExceptionInfo: {
3120 if (llvm::GlobalVariable *GV =
3121 CGM.getCXXABI().getThrowInfo(FD->getParamDecl(0)->getType()))
3122 return RValue::get(llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy));
3123 break;
3124 }
3125
3126 case Builtin::BI__fastfail:
3127 return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::__fastfail, E));
3128
3129 case Builtin::BI__builtin_coro_size: {
3130 auto & Context = getContext();
3131 auto SizeTy = Context.getSizeType();
3132 auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
3133 Value *F = CGM.getIntrinsic(Intrinsic::coro_size, T);
3134 return RValue::get(Builder.CreateCall(F));
3135 }
3136
3137 case Builtin::BI__builtin_coro_id:
3138 return EmitCoroutineIntrinsic(E, Intrinsic::coro_id);
3139 case Builtin::BI__builtin_coro_promise:
3140 return EmitCoroutineIntrinsic(E, Intrinsic::coro_promise);
3141 case Builtin::BI__builtin_coro_resume:
3142 return EmitCoroutineIntrinsic(E, Intrinsic::coro_resume);
3143 case Builtin::BI__builtin_coro_frame:
3144 return EmitCoroutineIntrinsic(E, Intrinsic::coro_frame);
3145 case Builtin::BI__builtin_coro_noop:
3146 return EmitCoroutineIntrinsic(E, Intrinsic::coro_noop);
3147 case Builtin::BI__builtin_coro_free:
3148 return EmitCoroutineIntrinsic(E, Intrinsic::coro_free);
3149 case Builtin::BI__builtin_coro_destroy:
3150 return EmitCoroutineIntrinsic(E, Intrinsic::coro_destroy);
3151 case Builtin::BI__builtin_coro_done:
3152 return EmitCoroutineIntrinsic(E, Intrinsic::coro_done);
3153 case Builtin::BI__builtin_coro_alloc:
3154 return EmitCoroutineIntrinsic(E, Intrinsic::coro_alloc);
3155 case Builtin::BI__builtin_coro_begin:
3156 return EmitCoroutineIntrinsic(E, Intrinsic::coro_begin);
3157 case Builtin::BI__builtin_coro_end:
3158 return EmitCoroutineIntrinsic(E, Intrinsic::coro_end);
3159 case Builtin::BI__builtin_coro_suspend:
3160 return EmitCoroutineIntrinsic(E, Intrinsic::coro_suspend);
3161 case Builtin::BI__builtin_coro_param:
3162 return EmitCoroutineIntrinsic(E, Intrinsic::coro_param);
3163
3164 // OpenCL v2.0 s6.13.16.2, Built-in pipe read and write functions
3165 case Builtin::BIread_pipe:
3166 case Builtin::BIwrite_pipe: {
3167 Value *Arg0 = EmitScalarExpr(E->getArg(0)),
3168 *Arg1 = EmitScalarExpr(E->getArg(1));
3169 CGOpenCLRuntime OpenCLRT(CGM);
3170 Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
3171 Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
3172
3173 // Type of the generic packet parameter.
3174 unsigned GenericAS =
3175 getContext().getTargetAddressSpace(LangAS::opencl_generic);
3176 llvm::Type *I8PTy = llvm::PointerType::get(
3177 llvm::Type::getInt8Ty(getLLVMContext()), GenericAS);
3178
3179 // Testing which overloaded version we should generate the call for.
3180 if (2U == E->getNumArgs()) {
3181 const char *Name = (BuiltinID == Builtin::BIread_pipe) ? "__read_pipe_2"
3182 : "__write_pipe_2";
3183 // Creating a generic function type to be able to call with any builtin or
3184 // user defined type.
3185 llvm::Type *ArgTys[] = {Arg0->getType(), I8PTy, Int32Ty, Int32Ty};
3186 llvm::FunctionType *FTy = llvm::FunctionType::get(
3187 Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3188 Value *BCast = Builder.CreatePointerCast(Arg1, I8PTy);
3189 return RValue::get(
3190 Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3191 {Arg0, BCast, PacketSize, PacketAlign}));
3192 } else {
3193 assert(4 == E->getNumArgs() &&((4 == E->getNumArgs() && "Illegal number of parameters to pipe function"
) ? static_cast<void> (0) : __assert_fail ("4 == E->getNumArgs() && \"Illegal number of parameters to pipe function\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3194, __PRETTY_FUNCTION__))
3194 "Illegal number of parameters to pipe function")((4 == E->getNumArgs() && "Illegal number of parameters to pipe function"
) ? static_cast<void> (0) : __assert_fail ("4 == E->getNumArgs() && \"Illegal number of parameters to pipe function\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3194, __PRETTY_FUNCTION__))
;
3195 const char *Name = (BuiltinID == Builtin::BIread_pipe) ? "__read_pipe_4"
3196 : "__write_pipe_4";
3197
3198 llvm::Type *ArgTys[] = {Arg0->getType(), Arg1->getType(), Int32Ty, I8PTy,
3199 Int32Ty, Int32Ty};
3200 Value *Arg2 = EmitScalarExpr(E->getArg(2)),
3201 *Arg3 = EmitScalarExpr(E->getArg(3));
3202 llvm::FunctionType *FTy = llvm::FunctionType::get(
3203 Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3204 Value *BCast = Builder.CreatePointerCast(Arg3, I8PTy);
3205 // We know the third argument is an integer type, but we may need to cast
3206 // it to i32.
3207 if (Arg2->getType() != Int32Ty)
3208 Arg2 = Builder.CreateZExtOrTrunc(Arg2, Int32Ty);
3209 return RValue::get(Builder.CreateCall(
3210 CGM.CreateRuntimeFunction(FTy, Name),
3211 {Arg0, Arg1, Arg2, BCast, PacketSize, PacketAlign}));
3212 }
3213 }
3214 // OpenCL v2.0 s6.13.16 ,s9.17.3.5 - Built-in pipe reserve read and write
3215 // functions
3216 case Builtin::BIreserve_read_pipe:
3217 case Builtin::BIreserve_write_pipe:
3218 case Builtin::BIwork_group_reserve_read_pipe:
3219 case Builtin::BIwork_group_reserve_write_pipe:
3220 case Builtin::BIsub_group_reserve_read_pipe:
3221 case Builtin::BIsub_group_reserve_write_pipe: {
3222 // Composing the mangled name for the function.
3223 const char *Name;
3224 if (BuiltinID == Builtin::BIreserve_read_pipe)
3225 Name = "__reserve_read_pipe";
3226 else if (BuiltinID == Builtin::BIreserve_write_pipe)
3227 Name = "__reserve_write_pipe";
3228 else if (BuiltinID == Builtin::BIwork_group_reserve_read_pipe)
3229 Name = "__work_group_reserve_read_pipe";
3230 else if (BuiltinID == Builtin::BIwork_group_reserve_write_pipe)
3231 Name = "__work_group_reserve_write_pipe";
3232 else if (BuiltinID == Builtin::BIsub_group_reserve_read_pipe)
3233 Name = "__sub_group_reserve_read_pipe";
3234 else
3235 Name = "__sub_group_reserve_write_pipe";
3236
3237 Value *Arg0 = EmitScalarExpr(E->getArg(0)),
3238 *Arg1 = EmitScalarExpr(E->getArg(1));
3239 llvm::Type *ReservedIDTy = ConvertType(getContext().OCLReserveIDTy);
3240 CGOpenCLRuntime OpenCLRT(CGM);
3241 Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
3242 Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
3243
3244 // Building the generic function prototype.
3245 llvm::Type *ArgTys[] = {Arg0->getType(), Int32Ty, Int32Ty, Int32Ty};
3246 llvm::FunctionType *FTy = llvm::FunctionType::get(
3247 ReservedIDTy, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3248 // We know the second argument is an integer type, but we may need to cast
3249 // it to i32.
3250 if (Arg1->getType() != Int32Ty)
3251 Arg1 = Builder.CreateZExtOrTrunc(Arg1, Int32Ty);
3252 return RValue::get(
3253 Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3254 {Arg0, Arg1, PacketSize, PacketAlign}));
3255 }
3256 // OpenCL v2.0 s6.13.16, s9.17.3.5 - Built-in pipe commit read and write
3257 // functions
3258 case Builtin::BIcommit_read_pipe:
3259 case Builtin::BIcommit_write_pipe:
3260 case Builtin::BIwork_group_commit_read_pipe:
3261 case Builtin::BIwork_group_commit_write_pipe:
3262 case Builtin::BIsub_group_commit_read_pipe:
3263 case Builtin::BIsub_group_commit_write_pipe: {
3264 const char *Name;
3265 if (BuiltinID == Builtin::BIcommit_read_pipe)
3266 Name = "__commit_read_pipe";
3267 else if (BuiltinID == Builtin::BIcommit_write_pipe)
3268 Name = "__commit_write_pipe";
3269 else if (BuiltinID == Builtin::BIwork_group_commit_read_pipe)
3270 Name = "__work_group_commit_read_pipe";
3271 else if (BuiltinID == Builtin::BIwork_group_commit_write_pipe)
3272 Name = "__work_group_commit_write_pipe";
3273 else if (BuiltinID == Builtin::BIsub_group_commit_read_pipe)
3274 Name = "__sub_group_commit_read_pipe";
3275 else
3276 Name = "__sub_group_commit_write_pipe";
3277
3278 Value *Arg0 = EmitScalarExpr(E->getArg(0)),
3279 *Arg1 = EmitScalarExpr(E->getArg(1));
3280 CGOpenCLRuntime OpenCLRT(CGM);
3281 Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
3282 Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
3283
3284 // Building the generic function prototype.
3285 llvm::Type *ArgTys[] = {Arg0->getType(), Arg1->getType(), Int32Ty, Int32Ty};
3286 llvm::FunctionType *FTy =
3287 llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()),
3288 llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3289
3290 return RValue::get(
3291 Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3292 {Arg0, Arg1, PacketSize, PacketAlign}));
3293 }
3294 // OpenCL v2.0 s6.13.16.4 Built-in pipe query functions
3295 case Builtin::BIget_pipe_num_packets:
3296 case Builtin::BIget_pipe_max_packets: {
3297 const char *BaseName;
3298 const PipeType *PipeTy = E->getArg(0)->getType()->getAs<PipeType>();
3299 if (BuiltinID == Builtin::BIget_pipe_num_packets)
3300 BaseName = "__get_pipe_num_packets";
3301 else
3302 BaseName = "__get_pipe_max_packets";
3303 auto Name = std::string(BaseName) +
3304 std::string(PipeTy->isReadOnly() ? "_ro" : "_wo");
3305
3306 // Building the generic function prototype.
3307 Value *Arg0 = EmitScalarExpr(E->getArg(0));
3308 CGOpenCLRuntime OpenCLRT(CGM);
3309 Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
3310 Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
3311 llvm::Type *ArgTys[] = {Arg0->getType(), Int32Ty, Int32Ty};
3312 llvm::FunctionType *FTy = llvm::FunctionType::get(
3313 Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3314
3315 return RValue::get(Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3316 {Arg0, PacketSize, PacketAlign}));
3317 }
3318
3319 // OpenCL v2.0 s6.13.9 - Address space qualifier functions.
3320 case Builtin::BIto_global:
3321 case Builtin::BIto_local:
3322 case Builtin::BIto_private: {
3323 auto Arg0 = EmitScalarExpr(E->getArg(0));
3324 auto NewArgT = llvm::PointerType::get(Int8Ty,
3325 CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
3326 auto NewRetT = llvm::PointerType::get(Int8Ty,
3327 CGM.getContext().getTargetAddressSpace(
3328 E->getType()->getPointeeType().getAddressSpace()));
3329 auto FTy = llvm::FunctionType::get(NewRetT, {NewArgT}, false);
3330 llvm::Value *NewArg;
3331 if (Arg0->getType()->getPointerAddressSpace() !=
3332 NewArgT->getPointerAddressSpace())
3333 NewArg = Builder.CreateAddrSpaceCast(Arg0, NewArgT);
3334 else
3335 NewArg = Builder.CreateBitOrPointerCast(Arg0, NewArgT);
3336 auto NewName = std::string("__") + E->getDirectCallee()->getName().str();
3337 auto NewCall =
3338 Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, NewName), {NewArg});
3339 return RValue::get(Builder.CreateBitOrPointerCast(NewCall,
3340 ConvertType(E->getType())));
3341 }
3342
3343 // OpenCL v2.0, s6.13.17 - Enqueue kernel function.
3344 // It contains four different overload formats specified in Table 6.13.17.1.
3345 case Builtin::BIenqueue_kernel: {
3346 StringRef Name; // Generated function call name
3347 unsigned NumArgs = E->getNumArgs();
3348
3349 llvm::Type *QueueTy = ConvertType(getContext().OCLQueueTy);
3350 llvm::Type *GenericVoidPtrTy = Builder.getInt8PtrTy(
3351 getContext().getTargetAddressSpace(LangAS::opencl_generic));
3352
3353 llvm::Value *Queue = EmitScalarExpr(E->getArg(0));
3354 llvm::Value *Flags = EmitScalarExpr(E->getArg(1));
3355 LValue NDRangeL = EmitAggExprToLValue(E->getArg(2));
3356 llvm::Value *Range = NDRangeL.getAddress().getPointer();
3357 llvm::Type *RangeTy = NDRangeL.getAddress().getType();
3358
3359 if (NumArgs == 4) {
3360 // The most basic form of the call with parameters:
3361 // queue_t, kernel_enqueue_flags_t, ndrange_t, block(void)
3362 Name = "__enqueue_kernel_basic";
3363 llvm::Type *ArgTys[] = {QueueTy, Int32Ty, RangeTy, GenericVoidPtrTy,
3364 GenericVoidPtrTy};
3365 llvm::FunctionType *FTy = llvm::FunctionType::get(
3366 Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3367
3368 auto Info =
3369 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(3));
3370 llvm::Value *Kernel =
3371 Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3372 llvm::Value *Block =
3373 Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3374
3375 AttrBuilder B;
3376 B.addAttribute(Attribute::ByVal);
3377 llvm::AttributeList ByValAttrSet =
3378 llvm::AttributeList::get(CGM.getModule().getContext(), 3U, B);
3379
3380 auto RTCall =
3381 Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name, ByValAttrSet),
3382 {Queue, Flags, Range, Kernel, Block});
3383 RTCall->setAttributes(ByValAttrSet);
3384 return RValue::get(RTCall);
3385 }
3386 assert(NumArgs >= 5 && "Invalid enqueue_kernel signature")((NumArgs >= 5 && "Invalid enqueue_kernel signature"
) ? static_cast<void> (0) : __assert_fail ("NumArgs >= 5 && \"Invalid enqueue_kernel signature\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3386, __PRETTY_FUNCTION__))
;
3387
3388 // Create a temporary array to hold the sizes of local pointer arguments
3389 // for the block. \p First is the position of the first size argument.
3390 auto CreateArrayForSizeVar = [=](unsigned First)
3391 -> std::tuple<llvm::Value *, llvm::Value *, llvm::Value *> {
3392 llvm::APInt ArraySize(32, NumArgs - First);
3393 QualType SizeArrayTy = getContext().getConstantArrayType(
3394 getContext().getSizeType(), ArraySize, ArrayType::Normal,
3395 /*IndexTypeQuals=*/0);
3396 auto Tmp = CreateMemTemp(SizeArrayTy, "block_sizes");
3397 llvm::Value *TmpPtr = Tmp.getPointer();
3398 llvm::Value *TmpSize = EmitLifetimeStart(
3399 CGM.getDataLayout().getTypeAllocSize(Tmp.getElementType()), TmpPtr);
3400 llvm::Value *ElemPtr;
3401 // Each of the following arguments specifies the size of the corresponding
3402 // argument passed to the enqueued block.
3403 auto *Zero = llvm::ConstantInt::get(IntTy, 0);
3404 for (unsigned I = First; I < NumArgs; ++I) {
3405 auto *Index = llvm::ConstantInt::get(IntTy, I - First);
3406 auto *GEP = Builder.CreateGEP(TmpPtr, {Zero, Index});
3407 if (I == First)
3408 ElemPtr = GEP;
3409 auto *V =
3410 Builder.CreateZExtOrTrunc(EmitScalarExpr(E->getArg(I)), SizeTy);
3411 Builder.CreateAlignedStore(
3412 V, GEP, CGM.getDataLayout().getPrefTypeAlignment(SizeTy));
3413 }
3414 return std::tie(ElemPtr, TmpSize, TmpPtr);
3415 };
3416
3417 // Could have events and/or varargs.
3418 if (E->getArg(3)->getType()->isBlockPointerType()) {
3419 // No events passed, but has variadic arguments.
3420 Name = "__enqueue_kernel_varargs";
3421 auto Info =
3422 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(3));
3423 llvm::Value *Kernel =
3424 Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3425 auto *Block = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3426 llvm::Value *ElemPtr, *TmpSize, *TmpPtr;
3427 std::tie(ElemPtr, TmpSize, TmpPtr) = CreateArrayForSizeVar(4);
3428
3429 // Create a vector of the arguments, as well as a constant value to
3430 // express to the runtime the number of variadic arguments.
3431 std::vector<llvm::Value *> Args = {
3432 Queue, Flags, Range,
3433 Kernel, Block, ConstantInt::get(IntTy, NumArgs - 4),
3434 ElemPtr};
3435 std::vector<llvm::Type *> ArgTys = {
3436 QueueTy, IntTy, RangeTy, GenericVoidPtrTy,
3437 GenericVoidPtrTy, IntTy, ElemPtr->getType()};
3438
3439 llvm::FunctionType *FTy = llvm::FunctionType::get(
3440 Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3441 auto Call =
3442 RValue::get(Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3443 llvm::ArrayRef<llvm::Value *>(Args)));
3444 if (TmpSize)
3445 EmitLifetimeEnd(TmpSize, TmpPtr);
3446 return Call;
3447 }
3448 // Any calls now have event arguments passed.
3449 if (NumArgs >= 7) {
3450 llvm::Type *EventTy = ConvertType(getContext().OCLClkEventTy);
3451 llvm::Type *EventPtrTy = EventTy->getPointerTo(
3452 CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
3453
3454 llvm::Value *NumEvents =
3455 Builder.CreateZExtOrTrunc(EmitScalarExpr(E->getArg(3)), Int32Ty);
3456 llvm::Value *EventList =
3457 E->getArg(4)->getType()->isArrayType()
3458 ? EmitArrayToPointerDecay(E->getArg(4)).getPointer()
3459 : EmitScalarExpr(E->getArg(4));
3460 llvm::Value *ClkEvent = EmitScalarExpr(E->getArg(5));
3461 // Convert to generic address space.
3462 EventList = Builder.CreatePointerCast(EventList, EventPtrTy);
3463 ClkEvent = Builder.CreatePointerCast(ClkEvent, EventPtrTy);
3464 auto Info =
3465 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(6));
3466 llvm::Value *Kernel =
3467 Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3468 llvm::Value *Block =
3469 Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3470
3471 std::vector<llvm::Type *> ArgTys = {
3472 QueueTy, Int32Ty, RangeTy, Int32Ty,
3473 EventPtrTy, EventPtrTy, GenericVoidPtrTy, GenericVoidPtrTy};
3474
3475 std::vector<llvm::Value *> Args = {Queue, Flags, Range, NumEvents,
3476 EventList, ClkEvent, Kernel, Block};
3477
3478 if (NumArgs == 7) {
3479 // Has events but no variadics.
3480 Name = "__enqueue_kernel_basic_events";
3481 llvm::FunctionType *FTy = llvm::FunctionType::get(
3482 Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3483 return RValue::get(
3484 Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3485 llvm::ArrayRef<llvm::Value *>(Args)));
3486 }
3487 // Has event info and variadics
3488 // Pass the number of variadics to the runtime function too.
3489 Args.push_back(ConstantInt::get(Int32Ty, NumArgs - 7));
3490 ArgTys.push_back(Int32Ty);
3491 Name = "__enqueue_kernel_events_varargs";
3492
3493 llvm::Value *ElemPtr, *TmpSize, *TmpPtr;
3494 std::tie(ElemPtr, TmpSize, TmpPtr) = CreateArrayForSizeVar(7);
3495 Args.push_back(ElemPtr);
3496 ArgTys.push_back(ElemPtr->getType());
3497
3498 llvm::FunctionType *FTy = llvm::FunctionType::get(
3499 Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3500 auto Call =
3501 RValue::get(Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3502 llvm::ArrayRef<llvm::Value *>(Args)));
3503 if (TmpSize)
3504 EmitLifetimeEnd(TmpSize, TmpPtr);
3505 return Call;
3506 }
3507 LLVM_FALLTHROUGH[[clang::fallthrough]];
3508 }
3509 // OpenCL v2.0 s6.13.17.6 - Kernel query functions need bitcast of block
3510 // parameter.
3511 case Builtin::BIget_kernel_work_group_size: {
3512 llvm::Type *GenericVoidPtrTy = Builder.getInt8PtrTy(
3513 getContext().getTargetAddressSpace(LangAS::opencl_generic));
3514 auto Info =
3515 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(0));
3516 Value *Kernel = Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3517 Value *Arg = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3518 return RValue::get(Builder.CreateCall(
3519 CGM.CreateRuntimeFunction(
3520 llvm::FunctionType::get(IntTy, {GenericVoidPtrTy, GenericVoidPtrTy},
3521 false),
3522 "__get_kernel_work_group_size_impl"),
3523 {Kernel, Arg}));
3524 }
3525 case Builtin::BIget_kernel_preferred_work_group_size_multiple: {
3526 llvm::Type *GenericVoidPtrTy = Builder.getInt8PtrTy(
3527 getContext().getTargetAddressSpace(LangAS::opencl_generic));
3528 auto Info =
3529 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(0));
3530 Value *Kernel = Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3531 Value *Arg = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3532 return RValue::get(Builder.CreateCall(
3533 CGM.CreateRuntimeFunction(
3534 llvm::FunctionType::get(IntTy, {GenericVoidPtrTy, GenericVoidPtrTy},
3535 false),
3536 "__get_kernel_preferred_work_group_size_multiple_impl"),
3537 {Kernel, Arg}));
3538 }
3539 case Builtin::BIget_kernel_max_sub_group_size_for_ndrange:
3540 case Builtin::BIget_kernel_sub_group_count_for_ndrange: {
3541 llvm::Type *GenericVoidPtrTy = Builder.getInt8PtrTy(
3542 getContext().getTargetAddressSpace(LangAS::opencl_generic));
3543 LValue NDRangeL = EmitAggExprToLValue(E->getArg(0));
3544 llvm::Value *NDRange = NDRangeL.getAddress().getPointer();
3545 auto Info =
3546 CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(1));
3547 Value *Kernel = Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
3548 Value *Block = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
3549 const char *Name =
3550 BuiltinID == Builtin::BIget_kernel_max_sub_group_size_for_ndrange
3551 ? "__get_kernel_max_sub_group_size_for_ndrange_impl"
3552 : "__get_kernel_sub_group_count_for_ndrange_impl";
3553 return RValue::get(Builder.CreateCall(
3554 CGM.CreateRuntimeFunction(
3555 llvm::FunctionType::get(
3556 IntTy, {NDRange->getType(), GenericVoidPtrTy, GenericVoidPtrTy},
3557 false),
3558 Name),
3559 {NDRange, Kernel, Block}));
3560 }
3561
3562 case Builtin::BI__builtin_store_half:
3563 case Builtin::BI__builtin_store_halff: {
3564 Value *Val = EmitScalarExpr(E->getArg(0));
3565 Address Address = EmitPointerWithAlignment(E->getArg(1));
3566 Value *HalfVal = Builder.CreateFPTrunc(Val, Builder.getHalfTy());
3567 return RValue::get(Builder.CreateStore(HalfVal, Address));
3568 }
3569 case Builtin::BI__builtin_load_half: {
3570 Address Address = EmitPointerWithAlignment(E->getArg(0));
3571 Value *HalfVal = Builder.CreateLoad(Address);
3572 return RValue::get(Builder.CreateFPExt(HalfVal, Builder.getDoubleTy()));
3573 }
3574 case Builtin::BI__builtin_load_halff: {
3575 Address Address = EmitPointerWithAlignment(E->getArg(0));
3576 Value *HalfVal = Builder.CreateLoad(Address);
3577 return RValue::get(Builder.CreateFPExt(HalfVal, Builder.getFloatTy()));
3578 }
3579 case Builtin::BIprintf:
3580 if (getTarget().getTriple().isNVPTX())
3581 return EmitNVPTXDevicePrintfCallExpr(E, ReturnValue);
3582 break;
3583 case Builtin::BI__builtin_canonicalize:
3584 case Builtin::BI__builtin_canonicalizef:
3585 case Builtin::BI__builtin_canonicalizel:
3586 return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::canonicalize));
3587
3588 case Builtin::BI__builtin_thread_pointer: {
3589 if (!getContext().getTargetInfo().isTLSSupported())
3590 CGM.ErrorUnsupported(E, "__builtin_thread_pointer");
3591 // Fall through - it's already mapped to the intrinsic by GCCBuiltin.
3592 break;
3593 }
3594 case Builtin::BI__builtin_os_log_format:
3595 return emitBuiltinOSLogFormat(*E);
3596
3597 case Builtin::BI__builtin_os_log_format_buffer_size: {
3598 analyze_os_log::OSLogBufferLayout Layout;
3599 analyze_os_log::computeOSLogBufferLayout(CGM.getContext(), E, Layout);
3600 return RValue::get(ConstantInt::get(ConvertType(E->getType()),
3601 Layout.size().getQuantity()));
3602 }
3603
3604 case Builtin::BI__xray_customevent: {
3605 if (!ShouldXRayInstrumentFunction())
3606 return RValue::getIgnored();
3607
3608 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
3609 XRayInstrKind::Custom))
3610 return RValue::getIgnored();
3611
3612 if (const auto *XRayAttr = CurFuncDecl->getAttr<XRayInstrumentAttr>())
3613 if (XRayAttr->neverXRayInstrument() && !AlwaysEmitXRayCustomEvents())
3614 return RValue::getIgnored();
3615
3616 Function *F = CGM.getIntrinsic(Intrinsic::xray_customevent);
3617 auto FTy = F->getFunctionType();
3618 auto Arg0 = E->getArg(0);
3619 auto Arg0Val = EmitScalarExpr(Arg0);
3620 auto Arg0Ty = Arg0->getType();
3621 auto PTy0 = FTy->getParamType(0);
3622 if (PTy0 != Arg0Val->getType()) {
3623 if (Arg0Ty->isArrayType())
3624 Arg0Val = EmitArrayToPointerDecay(Arg0).getPointer();
3625 else
3626 Arg0Val = Builder.CreatePointerCast(Arg0Val, PTy0);
3627 }
3628 auto Arg1 = EmitScalarExpr(E->getArg(1));
3629 auto PTy1 = FTy->getParamType(1);
3630 if (PTy1 != Arg1->getType())
3631 Arg1 = Builder.CreateTruncOrBitCast(Arg1, PTy1);
3632 return RValue::get(Builder.CreateCall(F, {Arg0Val, Arg1}));
3633 }
3634
3635 case Builtin::BI__xray_typedevent: {
3636 // TODO: There should be a way to always emit events even if the current
3637 // function is not instrumented. Losing events in a stream can cripple
3638 // a trace.
3639 if (!ShouldXRayInstrumentFunction())
3640 return RValue::getIgnored();
3641
3642 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has(
3643 XRayInstrKind::Typed))
3644 return RValue::getIgnored();
3645
3646 if (const auto *XRayAttr = CurFuncDecl->getAttr<XRayInstrumentAttr>())
3647 if (XRayAttr->neverXRayInstrument() && !AlwaysEmitXRayTypedEvents())
3648 return RValue::getIgnored();
3649
3650 Function *F = CGM.getIntrinsic(Intrinsic::xray_typedevent);
3651 auto FTy = F->getFunctionType();
3652 auto Arg0 = EmitScalarExpr(E->getArg(0));
3653 auto PTy0 = FTy->getParamType(0);
3654 if (PTy0 != Arg0->getType())
3655 Arg0 = Builder.CreateTruncOrBitCast(Arg0, PTy0);
3656 auto Arg1 = E->getArg(1);
3657 auto Arg1Val = EmitScalarExpr(Arg1);
3658 auto Arg1Ty = Arg1->getType();
3659 auto PTy1 = FTy->getParamType(1);
3660 if (PTy1 != Arg1Val->getType()) {
3661 if (Arg1Ty->isArrayType())
3662 Arg1Val = EmitArrayToPointerDecay(Arg1).getPointer();
3663 else
3664 Arg1Val = Builder.CreatePointerCast(Arg1Val, PTy1);
3665 }
3666 auto Arg2 = EmitScalarExpr(E->getArg(2));
3667 auto PTy2 = FTy->getParamType(2);
3668 if (PTy2 != Arg2->getType())
3669 Arg2 = Builder.CreateTruncOrBitCast(Arg2, PTy2);
3670 return RValue::get(Builder.CreateCall(F, {Arg0, Arg1Val, Arg2}));
3671 }
3672
3673 case Builtin::BI__builtin_ms_va_start:
3674 case Builtin::BI__builtin_ms_va_end:
3675 return RValue::get(
3676 EmitVAStartEnd(EmitMSVAListRef(E->getArg(0)).getPointer(),
3677 BuiltinID == Builtin::BI__builtin_ms_va_start));
3678
3679 case Builtin::BI__builtin_ms_va_copy: {
3680 // Lower this manually. We can't reliably determine whether or not any
3681 // given va_copy() is for a Win64 va_list from the calling convention
3682 // alone, because it's legal to do this from a System V ABI function.
3683 // With opaque pointer types, we won't have enough information in LLVM
3684 // IR to determine this from the argument types, either. Best to do it
3685 // now, while we have enough information.
3686 Address DestAddr = EmitMSVAListRef(E->getArg(0));
3687 Address SrcAddr = EmitMSVAListRef(E->getArg(1));
3688
3689 llvm::Type *BPP = Int8PtrPtrTy;
3690
3691 DestAddr = Address(Builder.CreateBitCast(DestAddr.getPointer(), BPP, "cp"),
3692 DestAddr.getAlignment());
3693 SrcAddr = Address(Builder.CreateBitCast(SrcAddr.getPointer(), BPP, "ap"),
3694 SrcAddr.getAlignment());
3695
3696 Value *ArgPtr = Builder.CreateLoad(SrcAddr, "ap.val");
3697 return RValue::get(Builder.CreateStore(ArgPtr, DestAddr));
3698 }
3699 }
3700
3701 // If this is an alias for a lib function (e.g. __builtin_sin), emit
3702 // the call using the normal call path, but using the unmangled
3703 // version of the function name.
3704 if (getContext().BuiltinInfo.isLibFunction(BuiltinID))
3705 return emitLibraryCall(*this, FD, E,
3706 CGM.getBuiltinLibFunction(FD, BuiltinID));
3707
3708 // If this is a predefined lib function (e.g. malloc), emit the call
3709 // using exactly the normal call path.
3710 if (getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
3711 return emitLibraryCall(*this, FD, E,
3712 cast<llvm::Constant>(EmitScalarExpr(E->getCallee())));
3713
3714 // Check that a call to a target specific builtin has the correct target
3715 // features.
3716 // This is down here to avoid non-target specific builtins, however, if
3717 // generic builtins start to require generic target features then we
3718 // can move this up to the beginning of the function.
3719 checkTargetFeatures(E, FD);
3720
3721 if (unsigned VectorWidth = getContext().BuiltinInfo.getRequiredVectorWidth(BuiltinID))
3722 LargestVectorWidth = std::max(LargestVectorWidth, VectorWidth);
3723
3724 // See if we have a target specific intrinsic.
3725 const char *Name = getContext().BuiltinInfo.getName(BuiltinID);
3726 Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic;
3727 StringRef Prefix =
3728 llvm::Triple::getArchTypePrefix(getTarget().getTriple().getArch());
3729 if (!Prefix.empty()) {
3730 IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix.data(), Name);
3731 // NOTE we don't need to perform a compatibility flag check here since the
3732 // intrinsics are declared in Builtins*.def via LANGBUILTIN which filter the
3733 // MS builtins via ALL_MS_LANGUAGES and are filtered earlier.
3734 if (IntrinsicID == Intrinsic::not_intrinsic)
3735 IntrinsicID = Intrinsic::getIntrinsicForMSBuiltin(Prefix.data(), Name);
3736 }
3737
3738 if (IntrinsicID != Intrinsic::not_intrinsic) {
3739 SmallVector<Value*, 16> Args;
3740
3741 // Find out if any arguments are required to be integer constant
3742 // expressions.
3743 unsigned ICEArguments = 0;
3744 ASTContext::GetBuiltinTypeError Error;
3745 getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments);
3746 assert(Error == ASTContext::GE_None && "Should not codegen an error")((Error == ASTContext::GE_None && "Should not codegen an error"
) ? static_cast<void> (0) : __assert_fail ("Error == ASTContext::GE_None && \"Should not codegen an error\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3746, __PRETTY_FUNCTION__))
;
3747
3748 Function *F = CGM.getIntrinsic(IntrinsicID);
3749 llvm::FunctionType *FTy = F->getFunctionType();
3750
3751 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
3752 Value *ArgValue;
3753 // If this is a normal argument, just emit it as a scalar.
3754 if ((ICEArguments & (1 << i)) == 0) {
3755 ArgValue = EmitScalarExpr(E->getArg(i));
3756 } else {
3757 // If this is required to be a constant, constant fold it so that we
3758 // know that the generated intrinsic gets a ConstantInt.
3759 llvm::APSInt Result;
3760 bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result,getContext());
3761 assert(IsConst && "Constant arg isn't actually constant?")((IsConst && "Constant arg isn't actually constant?")
? static_cast<void> (0) : __assert_fail ("IsConst && \"Constant arg isn't actually constant?\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3761, __PRETTY_FUNCTION__))
;
3762 (void)IsConst;
3763 ArgValue = llvm::ConstantInt::get(getLLVMContext(), Result);
3764 }
3765
3766 // If the intrinsic arg type is different from the builtin arg type
3767 // we need to do a bit cast.
3768 llvm::Type *PTy = FTy->getParamType(i);
3769 if (PTy != ArgValue->getType()) {
3770 // XXX - vector of pointers?
3771 if (auto *PtrTy = dyn_cast<llvm::PointerType>(PTy)) {
3772 if (PtrTy->getAddressSpace() !=
3773 ArgValue->getType()->getPointerAddressSpace()) {
3774 ArgValue = Builder.CreateAddrSpaceCast(
3775 ArgValue,
3776 ArgValue->getType()->getPointerTo(PtrTy->getAddressSpace()));
3777 }
3778 }
3779
3780 assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&((PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
"Must be able to losslessly bit cast to param") ? static_cast
<void> (0) : __assert_fail ("PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && \"Must be able to losslessly bit cast to param\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3781, __PRETTY_FUNCTION__))
3781 "Must be able to losslessly bit cast to param")((PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
"Must be able to losslessly bit cast to param") ? static_cast
<void> (0) : __assert_fail ("PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && \"Must be able to losslessly bit cast to param\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3781, __PRETTY_FUNCTION__))
;
3782 ArgValue = Builder.CreateBitCast(ArgValue, PTy);
3783 }
3784
3785 Args.push_back(ArgValue);
3786 }
3787
3788 Value *V = Builder.CreateCall(F, Args);
3789 QualType BuiltinRetType = E->getType();
3790
3791 llvm::Type *RetTy = VoidTy;
3792 if (!BuiltinRetType->isVoidType())
3793 RetTy = ConvertType(BuiltinRetType);
3794
3795 if (RetTy != V->getType()) {
3796 // XXX - vector of pointers?
3797 if (auto *PtrTy = dyn_cast<llvm::PointerType>(RetTy)) {
3798 if (PtrTy->getAddressSpace() != V->getType()->getPointerAddressSpace()) {
3799 V = Builder.CreateAddrSpaceCast(
3800 V, V->getType()->getPointerTo(PtrTy->getAddressSpace()));
3801 }
3802 }
3803
3804 assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&((V->getType()->canLosslesslyBitCastTo(RetTy) &&
"Must be able to losslessly bit cast result type") ? static_cast
<void> (0) : __assert_fail ("V->getType()->canLosslesslyBitCastTo(RetTy) && \"Must be able to losslessly bit cast result type\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3805, __PRETTY_FUNCTION__))
3805 "Must be able to losslessly bit cast result type")((V->getType()->canLosslesslyBitCastTo(RetTy) &&
"Must be able to losslessly bit cast result type") ? static_cast
<void> (0) : __assert_fail ("V->getType()->canLosslesslyBitCastTo(RetTy) && \"Must be able to losslessly bit cast result type\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3805, __PRETTY_FUNCTION__))
;
3806 V = Builder.CreateBitCast(V, RetTy);
3807 }
3808
3809 return RValue::get(V);
3810 }
3811
3812 // See if we have a target specific builtin that needs to be lowered.
3813 if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E))
3814 return RValue::get(V);
3815
3816 ErrorUnsupported(E, "builtin function");
3817
3818 // Unknown builtin, for now just dump it out and return undef.
3819 return GetUndefRValue(E->getType());
3820}
3821
3822static Value *EmitTargetArchBuiltinExpr(CodeGenFunction *CGF,
3823 unsigned BuiltinID, const CallExpr *E,
3824 llvm::Triple::ArchType Arch) {
3825 switch (Arch) {
3826 case llvm::Triple::arm:
3827 case llvm::Triple::armeb:
3828 case llvm::Triple::thumb:
3829 case llvm::Triple::thumbeb:
3830 return CGF->EmitARMBuiltinExpr(BuiltinID, E, Arch);
3831 case llvm::Triple::aarch64:
3832 case llvm::Triple::aarch64_be:
3833 return CGF->EmitAArch64BuiltinExpr(BuiltinID, E, Arch);
3834 case llvm::Triple::x86:
3835 case llvm::Triple::x86_64:
3836 return CGF->EmitX86BuiltinExpr(BuiltinID, E);
3837 case llvm::Triple::ppc:
3838 case llvm::Triple::ppc64:
3839 case llvm::Triple::ppc64le:
3840 return CGF->EmitPPCBuiltinExpr(BuiltinID, E);
3841 case llvm::Triple::r600:
3842 case llvm::Triple::amdgcn:
3843 return CGF->EmitAMDGPUBuiltinExpr(BuiltinID, E);
3844 case llvm::Triple::systemz:
3845 return CGF->EmitSystemZBuiltinExpr(BuiltinID, E);
3846 case llvm::Triple::nvptx:
3847 case llvm::Triple::nvptx64:
3848 return CGF->EmitNVPTXBuiltinExpr(BuiltinID, E);
3849 case llvm::Triple::wasm32:
3850 case llvm::Triple::wasm64:
3851 return CGF->EmitWebAssemblyBuiltinExpr(BuiltinID, E);
3852 case llvm::Triple::hexagon:
3853 return CGF->EmitHexagonBuiltinExpr(BuiltinID, E);
3854 default:
3855 return nullptr;
3856 }
3857}
3858
3859Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID,
3860 const CallExpr *E) {
3861 if (getContext().BuiltinInfo.isAuxBuiltinID(BuiltinID)) {
3862 assert(getContext().getAuxTargetInfo() && "Missing aux target info")((getContext().getAuxTargetInfo() && "Missing aux target info"
) ? static_cast<void> (0) : __assert_fail ("getContext().getAuxTargetInfo() && \"Missing aux target info\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3862, __PRETTY_FUNCTION__))
;
3863 return EmitTargetArchBuiltinExpr(
3864 this, getContext().BuiltinInfo.getAuxBuiltinID(BuiltinID), E,
3865 getContext().getAuxTargetInfo()->getTriple().getArch());
3866 }
3867
3868 return EmitTargetArchBuiltinExpr(this, BuiltinID, E,
3869 getTarget().getTriple().getArch());
3870}
3871
3872static llvm::VectorType *GetNeonType(CodeGenFunction *CGF,
3873 NeonTypeFlags TypeFlags,
3874 bool HasLegalHalfType=true,
3875 bool V1Ty=false) {
3876 int IsQuad = TypeFlags.isQuad();
3877 switch (TypeFlags.getEltType()) {
3878 case NeonTypeFlags::Int8:
3879 case NeonTypeFlags::Poly8:
3880 return llvm::VectorType::get(CGF->Int8Ty, V1Ty ? 1 : (8 << IsQuad));
3881 case NeonTypeFlags::Int16:
3882 case NeonTypeFlags::Poly16:
3883 return llvm::VectorType::get(CGF->Int16Ty, V1Ty ? 1 : (4 << IsQuad));
3884 case NeonTypeFlags::Float16:
3885 if (HasLegalHalfType)
3886 return llvm::VectorType::get(CGF->HalfTy, V1Ty ? 1 : (4 << IsQuad));
3887 else
3888 return llvm::VectorType::get(CGF->Int16Ty, V1Ty ? 1 : (4 << IsQuad));
3889 case NeonTypeFlags::Int32:
3890 return llvm::VectorType::get(CGF->Int32Ty, V1Ty ? 1 : (2 << IsQuad));
3891 case NeonTypeFlags::Int64:
3892 case NeonTypeFlags::Poly64:
3893 return llvm::VectorType::get(CGF->Int64Ty, V1Ty ? 1 : (1 << IsQuad));
3894 case NeonTypeFlags::Poly128:
3895 // FIXME: i128 and f128 doesn't get fully support in Clang and llvm.
3896 // There is a lot of i128 and f128 API missing.
3897 // so we use v16i8 to represent poly128 and get pattern matched.
3898 return llvm::VectorType::get(CGF->Int8Ty, 16);
3899 case NeonTypeFlags::Float32:
3900 return llvm::VectorType::get(CGF->FloatTy, V1Ty ? 1 : (2 << IsQuad));
3901 case NeonTypeFlags::Float64:
3902 return llvm::VectorType::get(CGF->DoubleTy, V1Ty ? 1 : (1 << IsQuad));
3903 }
3904 llvm_unreachable("Unknown vector element type!")::llvm::llvm_unreachable_internal("Unknown vector element type!"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3904)
;
3905}
3906
3907static llvm::VectorType *GetFloatNeonType(CodeGenFunction *CGF,
3908 NeonTypeFlags IntTypeFlags) {
3909 int IsQuad = IntTypeFlags.isQuad();
3910 switch (IntTypeFlags.getEltType()) {
3911 case NeonTypeFlags::Int16:
3912 return llvm::VectorType::get(CGF->HalfTy, (4 << IsQuad));
3913 case NeonTypeFlags::Int32:
3914 return llvm::VectorType::get(CGF->FloatTy, (2 << IsQuad));
3915 case NeonTypeFlags::Int64:
3916 return llvm::VectorType::get(CGF->DoubleTy, (1 << IsQuad));
3917 default:
3918 llvm_unreachable("Type can't be converted to floating-point!")::llvm::llvm_unreachable_internal("Type can't be converted to floating-point!"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 3918)
;
3919 }
3920}
3921
3922Value *CodeGenFunction::EmitNeonSplat(Value *V, Constant *C) {
3923 unsigned nElts = V->getType()->getVectorNumElements();
3924 Value* SV = llvm::ConstantVector::getSplat(nElts, C);
3925 return Builder.CreateShuffleVector(V, V, SV, "lane");
3926}
3927
3928Value *CodeGenFunction::EmitNeonCall(Function *F, SmallVectorImpl<Value*> &Ops,
3929 const char *name,
3930 unsigned shift, bool rightshift) {
3931 unsigned j = 0;
3932 for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end();
3933 ai != ae; ++ai, ++j)
3934 if (shift > 0 && shift == j)
3935 Ops[j] = EmitNeonShiftVector(Ops[j], ai->getType(), rightshift);
3936 else
3937 Ops[j] = Builder.CreateBitCast(Ops[j], ai->getType(), name);
3938
3939 return Builder.CreateCall(F, Ops, name);
3940}
3941
3942Value *CodeGenFunction::EmitNeonShiftVector(Value *V, llvm::Type *Ty,
3943 bool neg) {
3944 int SV = cast<ConstantInt>(V)->getSExtValue();
3945 return ConstantInt::get(Ty, neg ? -SV : SV);
3946}
3947
3948// Right-shift a vector by a constant.
3949Value *CodeGenFunction::EmitNeonRShiftImm(Value *Vec, Value *Shift,
3950 llvm::Type *Ty, bool usgn,
3951 const char *name) {
3952 llvm::VectorType *VTy = cast<llvm::VectorType>(Ty);
3953
3954 int ShiftAmt = cast<ConstantInt>(Shift)->getSExtValue();
3955 int EltSize = VTy->getScalarSizeInBits();
3956
3957 Vec = Builder.CreateBitCast(Vec, Ty);
3958
3959 // lshr/ashr are undefined when the shift amount is equal to the vector
3960 // element size.
3961 if (ShiftAmt == EltSize) {
3962 if (usgn) {
3963 // Right-shifting an unsigned value by its size yields 0.
3964 return llvm::ConstantAggregateZero::get(VTy);
3965 } else {
3966 // Right-shifting a signed value by its size is equivalent
3967 // to a shift of size-1.
3968 --ShiftAmt;
3969 Shift = ConstantInt::get(VTy->getElementType(), ShiftAmt);
3970 }
3971 }
3972
3973 Shift = EmitNeonShiftVector(Shift, Ty, false);
3974 if (usgn)
3975 return Builder.CreateLShr(Vec, Shift, name);
3976 else
3977 return Builder.CreateAShr(Vec, Shift, name);
3978}
3979
3980enum {
3981 AddRetType = (1 << 0),
3982 Add1ArgType = (1 << 1),
3983 Add2ArgTypes = (1 << 2),
3984
3985 VectorizeRetType = (1 << 3),
3986 VectorizeArgTypes = (1 << 4),
3987
3988 InventFloatType = (1 << 5),
3989 UnsignedAlts = (1 << 6),
3990
3991 Use64BitVectors = (1 << 7),
3992 Use128BitVectors = (1 << 8),
3993
3994 Vectorize1ArgType = Add1ArgType | VectorizeArgTypes,
3995 VectorRet = AddRetType | VectorizeRetType,
3996 VectorRetGetArgs01 =
3997 AddRetType | Add2ArgTypes | VectorizeRetType | VectorizeArgTypes,
3998 FpCmpzModifiers =
3999 AddRetType | VectorizeRetType | Add1ArgType | InventFloatType
4000};
4001
4002namespace {
4003struct NeonIntrinsicInfo {
4004 const char *NameHint;
4005 unsigned BuiltinID;
4006 unsigned LLVMIntrinsic;
4007 unsigned AltLLVMIntrinsic;
4008 unsigned TypeModifier;
4009
4010 bool operator<(unsigned RHSBuiltinID) const {
4011 return BuiltinID < RHSBuiltinID;
4012 }
4013 bool operator<(const NeonIntrinsicInfo &TE) const {
4014 return BuiltinID < TE.BuiltinID;
4015 }
4016};
4017} // end anonymous namespace
4018
4019#define NEONMAP0(NameBase) \
4020 { #NameBase, NEON::BI__builtin_neon_ ## NameBase, 0, 0, 0 }
4021
4022#define NEONMAP1(NameBase, LLVMIntrinsic, TypeModifier) \
4023 { #NameBase, NEON:: BI__builtin_neon_ ## NameBase, \
4024 Intrinsic::LLVMIntrinsic, 0, TypeModifier }
4025
4026#define NEONMAP2(NameBase, LLVMIntrinsic, AltLLVMIntrinsic, TypeModifier) \
4027 { #NameBase, NEON:: BI__builtin_neon_ ## NameBase, \
4028 Intrinsic::LLVMIntrinsic, Intrinsic::AltLLVMIntrinsic, \
4029 TypeModifier }
4030
4031static const NeonIntrinsicInfo ARMSIMDIntrinsicMap [] = {
4032 NEONMAP2(vabd_v, arm_neon_vabdu, arm_neon_vabds, Add1ArgType | UnsignedAlts),
4033 NEONMAP2(vabdq_v, arm_neon_vabdu, arm_neon_vabds, Add1ArgType | UnsignedAlts),
4034 NEONMAP1(vabs_v, arm_neon_vabs, 0),
4035 NEONMAP1(vabsq_v, arm_neon_vabs, 0),
4036 NEONMAP0(vaddhn_v),
4037 NEONMAP1(vaesdq_v, arm_neon_aesd, 0),
4038 NEONMAP1(vaeseq_v, arm_neon_aese, 0),
4039 NEONMAP1(vaesimcq_v, arm_neon_aesimc, 0),
4040 NEONMAP1(vaesmcq_v, arm_neon_aesmc, 0),
4041 NEONMAP1(vbsl_v, arm_neon_vbsl, AddRetType),
4042 NEONMAP1(vbslq_v, arm_neon_vbsl, AddRetType),
4043 NEONMAP1(vcage_v, arm_neon_vacge, 0),
4044 NEONMAP1(vcageq_v, arm_neon_vacge, 0),
4045 NEONMAP1(vcagt_v, arm_neon_vacgt, 0),
4046 NEONMAP1(vcagtq_v, arm_neon_vacgt, 0),
4047 NEONMAP1(vcale_v, arm_neon_vacge, 0),
4048 NEONMAP1(vcaleq_v, arm_neon_vacge, 0),
4049 NEONMAP1(vcalt_v, arm_neon_vacgt, 0),
4050 NEONMAP1(vcaltq_v, arm_neon_vacgt, 0),
4051 NEONMAP0(vceqz_v),
4052 NEONMAP0(vceqzq_v),
4053 NEONMAP0(vcgez_v),
4054 NEONMAP0(vcgezq_v),
4055 NEONMAP0(vcgtz_v),
4056 NEONMAP0(vcgtzq_v),
4057 NEONMAP0(vclez_v),
4058 NEONMAP0(vclezq_v),
4059 NEONMAP1(vcls_v, arm_neon_vcls, Add1ArgType),
4060 NEONMAP1(vclsq_v, arm_neon_vcls, Add1ArgType),
4061 NEONMAP0(vcltz_v),
4062 NEONMAP0(vcltzq_v),
4063 NEONMAP1(vclz_v, ctlz, Add1ArgType),
4064 NEONMAP1(vclzq_v, ctlz, Add1ArgType),
4065 NEONMAP1(vcnt_v, ctpop, Add1ArgType),
4066 NEONMAP1(vcntq_v, ctpop, Add1ArgType),
4067 NEONMAP1(vcvt_f16_f32, arm_neon_vcvtfp2hf, 0),
4068 NEONMAP0(vcvt_f16_v),
4069 NEONMAP1(vcvt_f32_f16, arm_neon_vcvthf2fp, 0),
4070 NEONMAP0(vcvt_f32_v),
4071 NEONMAP2(vcvt_n_f16_v, arm_neon_vcvtfxu2fp, arm_neon_vcvtfxs2fp, 0),
4072 NEONMAP2(vcvt_n_f32_v, arm_neon_vcvtfxu2fp, arm_neon_vcvtfxs2fp, 0),
4073 NEONMAP1(vcvt_n_s16_v, arm_neon_vcvtfp2fxs, 0),
4074 NEONMAP1(vcvt_n_s32_v, arm_neon_vcvtfp2fxs, 0),
4075 NEONMAP1(vcvt_n_s64_v, arm_neon_vcvtfp2fxs, 0),
4076 NEONMAP1(vcvt_n_u16_v, arm_neon_vcvtfp2fxu, 0),
4077 NEONMAP1(vcvt_n_u32_v, arm_neon_vcvtfp2fxu, 0),
4078 NEONMAP1(vcvt_n_u64_v, arm_neon_vcvtfp2fxu, 0),
4079 NEONMAP0(vcvt_s16_v),
4080 NEONMAP0(vcvt_s32_v),
4081 NEONMAP0(vcvt_s64_v),
4082 NEONMAP0(vcvt_u16_v),
4083 NEONMAP0(vcvt_u32_v),
4084 NEONMAP0(vcvt_u64_v),
4085 NEONMAP1(vcvta_s16_v, arm_neon_vcvtas, 0),
4086 NEONMAP1(vcvta_s32_v, arm_neon_vcvtas, 0),
4087 NEONMAP1(vcvta_s64_v, arm_neon_vcvtas, 0),
4088 NEONMAP1(vcvta_u16_v, arm_neon_vcvtau, 0),
4089 NEONMAP1(vcvta_u32_v, arm_neon_vcvtau, 0),
4090 NEONMAP1(vcvta_u64_v, arm_neon_vcvtau, 0),
4091 NEONMAP1(vcvtaq_s16_v, arm_neon_vcvtas, 0),
4092 NEONMAP1(vcvtaq_s32_v, arm_neon_vcvtas, 0),
4093 NEONMAP1(vcvtaq_s64_v, arm_neon_vcvtas, 0),
4094 NEONMAP1(vcvtaq_u16_v, arm_neon_vcvtau, 0),
4095 NEONMAP1(vcvtaq_u32_v, arm_neon_vcvtau, 0),
4096 NEONMAP1(vcvtaq_u64_v, arm_neon_vcvtau, 0),
4097 NEONMAP1(vcvtm_s16_v, arm_neon_vcvtms, 0),
4098 NEONMAP1(vcvtm_s32_v, arm_neon_vcvtms, 0),
4099 NEONMAP1(vcvtm_s64_v, arm_neon_vcvtms, 0),
4100 NEONMAP1(vcvtm_u16_v, arm_neon_vcvtmu, 0),
4101 NEONMAP1(vcvtm_u32_v, arm_neon_vcvtmu, 0),
4102 NEONMAP1(vcvtm_u64_v, arm_neon_vcvtmu, 0),
4103 NEONMAP1(vcvtmq_s16_v, arm_neon_vcvtms, 0),
4104 NEONMAP1(vcvtmq_s32_v, arm_neon_vcvtms, 0),
4105 NEONMAP1(vcvtmq_s64_v, arm_neon_vcvtms, 0),
4106 NEONMAP1(vcvtmq_u16_v, arm_neon_vcvtmu, 0),
4107 NEONMAP1(vcvtmq_u32_v, arm_neon_vcvtmu, 0),
4108 NEONMAP1(vcvtmq_u64_v, arm_neon_vcvtmu, 0),
4109 NEONMAP1(vcvtn_s16_v, arm_neon_vcvtns, 0),
4110 NEONMAP1(vcvtn_s32_v, arm_neon_vcvtns, 0),
4111 NEONMAP1(vcvtn_s64_v, arm_neon_vcvtns, 0),
4112 NEONMAP1(vcvtn_u16_v, arm_neon_vcvtnu, 0),
4113 NEONMAP1(vcvtn_u32_v, arm_neon_vcvtnu, 0),
4114 NEONMAP1(vcvtn_u64_v, arm_neon_vcvtnu, 0),
4115 NEONMAP1(vcvtnq_s16_v, arm_neon_vcvtns, 0),
4116 NEONMAP1(vcvtnq_s32_v, arm_neon_vcvtns, 0),
4117 NEONMAP1(vcvtnq_s64_v, arm_neon_vcvtns, 0),
4118 NEONMAP1(vcvtnq_u16_v, arm_neon_vcvtnu, 0),
4119 NEONMAP1(vcvtnq_u32_v, arm_neon_vcvtnu, 0),
4120 NEONMAP1(vcvtnq_u64_v, arm_neon_vcvtnu, 0),
4121 NEONMAP1(vcvtp_s16_v, arm_neon_vcvtps, 0),
4122 NEONMAP1(vcvtp_s32_v, arm_neon_vcvtps, 0),
4123 NEONMAP1(vcvtp_s64_v, arm_neon_vcvtps, 0),
4124 NEONMAP1(vcvtp_u16_v, arm_neon_vcvtpu, 0),
4125 NEONMAP1(vcvtp_u32_v, arm_neon_vcvtpu, 0),
4126 NEONMAP1(vcvtp_u64_v, arm_neon_vcvtpu, 0),
4127 NEONMAP1(vcvtpq_s16_v, arm_neon_vcvtps, 0),
4128 NEONMAP1(vcvtpq_s32_v, arm_neon_vcvtps, 0),
4129 NEONMAP1(vcvtpq_s64_v, arm_neon_vcvtps, 0),
4130 NEONMAP1(vcvtpq_u16_v, arm_neon_vcvtpu, 0),
4131 NEONMAP1(vcvtpq_u32_v, arm_neon_vcvtpu, 0),
4132 NEONMAP1(vcvtpq_u64_v, arm_neon_vcvtpu, 0),
4133 NEONMAP0(vcvtq_f16_v),
4134 NEONMAP0(vcvtq_f32_v),
4135 NEONMAP2(vcvtq_n_f16_v, arm_neon_vcvtfxu2fp, arm_neon_vcvtfxs2fp, 0),
4136 NEONMAP2(vcvtq_n_f32_v, arm_neon_vcvtfxu2fp, arm_neon_vcvtfxs2fp, 0),
4137 NEONMAP1(vcvtq_n_s16_v, arm_neon_vcvtfp2fxs, 0),
4138 NEONMAP1(vcvtq_n_s32_v, arm_neon_vcvtfp2fxs, 0),
4139 NEONMAP1(vcvtq_n_s64_v, arm_neon_vcvtfp2fxs, 0),
4140 NEONMAP1(vcvtq_n_u16_v, arm_neon_vcvtfp2fxu, 0),
4141 NEONMAP1(vcvtq_n_u32_v, arm_neon_vcvtfp2fxu, 0),
4142 NEONMAP1(vcvtq_n_u64_v, arm_neon_vcvtfp2fxu, 0),
4143 NEONMAP0(vcvtq_s16_v),
4144 NEONMAP0(vcvtq_s32_v),
4145 NEONMAP0(vcvtq_s64_v),
4146 NEONMAP0(vcvtq_u16_v),
4147 NEONMAP0(vcvtq_u32_v),
4148 NEONMAP0(vcvtq_u64_v),
4149 NEONMAP2(vdot_v, arm_neon_udot, arm_neon_sdot, 0),
4150 NEONMAP2(vdotq_v, arm_neon_udot, arm_neon_sdot, 0),
4151 NEONMAP0(vext_v),
4152 NEONMAP0(vextq_v),
4153 NEONMAP0(vfma_v),
4154 NEONMAP0(vfmaq_v),
4155 NEONMAP2(vhadd_v, arm_neon_vhaddu, arm_neon_vhadds, Add1ArgType | UnsignedAlts),
4156 NEONMAP2(vhaddq_v, arm_neon_vhaddu, arm_neon_vhadds, Add1ArgType | UnsignedAlts),
4157 NEONMAP2(vhsub_v, arm_neon_vhsubu, arm_neon_vhsubs, Add1ArgType | UnsignedAlts),
4158 NEONMAP2(vhsubq_v, arm_neon_vhsubu, arm_neon_vhsubs, Add1ArgType | UnsignedAlts),
4159 NEONMAP0(vld1_dup_v),
4160 NEONMAP1(vld1_v, arm_neon_vld1, 0),
4161 NEONMAP1(vld1_x2_v, arm_neon_vld1x2, 0),
4162 NEONMAP1(vld1_x3_v, arm_neon_vld1x3, 0),
4163 NEONMAP1(vld1_x4_v, arm_neon_vld1x4, 0),
4164 NEONMAP0(vld1q_dup_v),
4165 NEONMAP1(vld1q_v, arm_neon_vld1, 0),
4166 NEONMAP1(vld1q_x2_v, arm_neon_vld1x2, 0),
4167 NEONMAP1(vld1q_x3_v, arm_neon_vld1x3, 0),
4168 NEONMAP1(vld1q_x4_v, arm_neon_vld1x4, 0),
4169 NEONMAP1(vld2_dup_v, arm_neon_vld2dup, 0),
4170 NEONMAP1(vld2_lane_v, arm_neon_vld2lane, 0),
4171 NEONMAP1(vld2_v, arm_neon_vld2, 0),
4172 NEONMAP1(vld2q_dup_v, arm_neon_vld2dup, 0),
4173 NEONMAP1(vld2q_lane_v, arm_neon_vld2lane, 0),
4174 NEONMAP1(vld2q_v, arm_neon_vld2, 0),
4175 NEONMAP1(vld3_dup_v, arm_neon_vld3dup, 0),
4176 NEONMAP1(vld3_lane_v, arm_neon_vld3lane, 0),
4177 NEONMAP1(vld3_v, arm_neon_vld3, 0),
4178 NEONMAP1(vld3q_dup_v, arm_neon_vld3dup, 0),
4179 NEONMAP1(vld3q_lane_v, arm_neon_vld3lane, 0),
4180 NEONMAP1(vld3q_v, arm_neon_vld3, 0),
4181 NEONMAP1(vld4_dup_v, arm_neon_vld4dup, 0),
4182 NEONMAP1(vld4_lane_v, arm_neon_vld4lane, 0),
4183 NEONMAP1(vld4_v, arm_neon_vld4, 0),
4184 NEONMAP1(vld4q_dup_v, arm_neon_vld4dup, 0),
4185 NEONMAP1(vld4q_lane_v, arm_neon_vld4lane, 0),
4186 NEONMAP1(vld4q_v, arm_neon_vld4, 0),
4187 NEONMAP2(vmax_v, arm_neon_vmaxu, arm_neon_vmaxs, Add1ArgType | UnsignedAlts),
4188 NEONMAP1(vmaxnm_v, arm_neon_vmaxnm, Add1ArgType),
4189 NEONMAP1(vmaxnmq_v, arm_neon_vmaxnm, Add1ArgType),
4190 NEONMAP2(vmaxq_v, arm_neon_vmaxu, arm_neon_vmaxs, Add1ArgType | UnsignedAlts),
4191 NEONMAP2(vmin_v, arm_neon_vminu, arm_neon_vmins, Add1ArgType | UnsignedAlts),
4192 NEONMAP1(vminnm_v, arm_neon_vminnm, Add1ArgType),
4193 NEONMAP1(vminnmq_v, arm_neon_vminnm, Add1ArgType),
4194 NEONMAP2(vminq_v, arm_neon_vminu, arm_neon_vmins, Add1ArgType | UnsignedAlts),
4195 NEONMAP0(vmovl_v),
4196 NEONMAP0(vmovn_v),
4197 NEONMAP1(vmul_v, arm_neon_vmulp, Add1ArgType),
4198 NEONMAP0(vmull_v),
4199 NEONMAP1(vmulq_v, arm_neon_vmulp, Add1ArgType),
4200 NEONMAP2(vpadal_v, arm_neon_vpadalu, arm_neon_vpadals, UnsignedAlts),
4201 NEONMAP2(vpadalq_v, arm_neon_vpadalu, arm_neon_vpadals, UnsignedAlts),
4202 NEONMAP1(vpadd_v, arm_neon_vpadd, Add1ArgType),
4203 NEONMAP2(vpaddl_v, arm_neon_vpaddlu, arm_neon_vpaddls, UnsignedAlts),
4204 NEONMAP2(vpaddlq_v, arm_neon_vpaddlu, arm_neon_vpaddls, UnsignedAlts),
4205 NEONMAP1(vpaddq_v, arm_neon_vpadd, Add1ArgType),
4206 NEONMAP2(vpmax_v, arm_neon_vpmaxu, arm_neon_vpmaxs, Add1ArgType | UnsignedAlts),
4207 NEONMAP2(vpmin_v, arm_neon_vpminu, arm_neon_vpmins, Add1ArgType | UnsignedAlts),
4208 NEONMAP1(vqabs_v, arm_neon_vqabs, Add1ArgType),
4209 NEONMAP1(vqabsq_v, arm_neon_vqabs, Add1ArgType),
4210 NEONMAP2(vqadd_v, arm_neon_vqaddu, arm_neon_vqadds, Add1ArgType | UnsignedAlts),
4211 NEONMAP2(vqaddq_v, arm_neon_vqaddu, arm_neon_vqadds, Add1ArgType | UnsignedAlts),
4212 NEONMAP2(vqdmlal_v, arm_neon_vqdmull, arm_neon_vqadds, 0),
4213 NEONMAP2(vqdmlsl_v, arm_neon_vqdmull, arm_neon_vqsubs, 0),
4214 NEONMAP1(vqdmulh_v, arm_neon_vqdmulh, Add1ArgType),
4215 NEONMAP1(vqdmulhq_v, arm_neon_vqdmulh, Add1ArgType),
4216 NEONMAP1(vqdmull_v, arm_neon_vqdmull, Add1ArgType),
4217 NEONMAP2(vqmovn_v, arm_neon_vqmovnu, arm_neon_vqmovns, Add1ArgType | UnsignedAlts),
4218 NEONMAP1(vqmovun_v, arm_neon_vqmovnsu, Add1ArgType),
4219 NEONMAP1(vqneg_v, arm_neon_vqneg, Add1ArgType),
4220 NEONMAP1(vqnegq_v, arm_neon_vqneg, Add1ArgType),
4221 NEONMAP1(vqrdmulh_v, arm_neon_vqrdmulh, Add1ArgType),
4222 NEONMAP1(vqrdmulhq_v, arm_neon_vqrdmulh, Add1ArgType),
4223 NEONMAP2(vqrshl_v, arm_neon_vqrshiftu, arm_neon_vqrshifts, Add1ArgType | UnsignedAlts),
4224 NEONMAP2(vqrshlq_v, arm_neon_vqrshiftu, arm_neon_vqrshifts, Add1ArgType | UnsignedAlts),
4225 NEONMAP2(vqshl_n_v, arm_neon_vqshiftu, arm_neon_vqshifts, UnsignedAlts),
4226 NEONMAP2(vqshl_v, arm_neon_vqshiftu, arm_neon_vqshifts, Add1ArgType | UnsignedAlts),
4227 NEONMAP2(vqshlq_n_v, arm_neon_vqshiftu, arm_neon_vqshifts, UnsignedAlts),
4228 NEONMAP2(vqshlq_v, arm_neon_vqshiftu, arm_neon_vqshifts, Add1ArgType | UnsignedAlts),
4229 NEONMAP1(vqshlu_n_v, arm_neon_vqshiftsu, 0),
4230 NEONMAP1(vqshluq_n_v, arm_neon_vqshiftsu, 0),
4231 NEONMAP2(vqsub_v, arm_neon_vqsubu, arm_neon_vqsubs, Add1ArgType | UnsignedAlts),
4232 NEONMAP2(vqsubq_v, arm_neon_vqsubu, arm_neon_vqsubs, Add1ArgType | UnsignedAlts),
4233 NEONMAP1(vraddhn_v, arm_neon_vraddhn, Add1ArgType),
4234 NEONMAP2(vrecpe_v, arm_neon_vrecpe, arm_neon_vrecpe, 0),
4235 NEONMAP2(vrecpeq_v, arm_neon_vrecpe, arm_neon_vrecpe, 0),
4236 NEONMAP1(vrecps_v, arm_neon_vrecps, Add1ArgType),
4237 NEONMAP1(vrecpsq_v, arm_neon_vrecps, Add1ArgType),
4238 NEONMAP2(vrhadd_v, arm_neon_vrhaddu, arm_neon_vrhadds, Add1ArgType | UnsignedAlts),
4239 NEONMAP2(vrhaddq_v, arm_neon_vrhaddu, arm_neon_vrhadds, Add1ArgType | UnsignedAlts),
4240 NEONMAP1(vrnd_v, arm_neon_vrintz, Add1ArgType),
4241 NEONMAP1(vrnda_v, arm_neon_vrinta, Add1ArgType),
4242 NEONMAP1(vrndaq_v, arm_neon_vrinta, Add1ArgType),
4243 NEONMAP0(vrndi_v),
4244 NEONMAP0(vrndiq_v),
4245 NEONMAP1(vrndm_v, arm_neon_vrintm, Add1ArgType),
4246 NEONMAP1(vrndmq_v, arm_neon_vrintm, Add1ArgType),
4247 NEONMAP1(vrndn_v, arm_neon_vrintn, Add1ArgType),
4248 NEONMAP1(vrndnq_v, arm_neon_vrintn, Add1ArgType),
4249 NEONMAP1(vrndp_v, arm_neon_vrintp, Add1ArgType),
4250 NEONMAP1(vrndpq_v, arm_neon_vrintp, Add1ArgType),
4251 NEONMAP1(vrndq_v, arm_neon_vrintz, Add1ArgType),
4252 NEONMAP1(vrndx_v, arm_neon_vrintx, Add1ArgType),
4253 NEONMAP1(vrndxq_v, arm_neon_vrintx, Add1ArgType),
4254 NEONMAP2(vrshl_v, arm_neon_vrshiftu, arm_neon_vrshifts, Add1ArgType | UnsignedAlts),
4255 NEONMAP2(vrshlq_v, arm_neon_vrshiftu, arm_neon_vrshifts, Add1ArgType | UnsignedAlts),
4256 NEONMAP2(vrshr_n_v, arm_neon_vrshiftu, arm_neon_vrshifts, UnsignedAlts),
4257 NEONMAP2(vrshrq_n_v, arm_neon_vrshiftu, arm_neon_vrshifts, UnsignedAlts),
4258 NEONMAP2(vrsqrte_v, arm_neon_vrsqrte, arm_neon_vrsqrte, 0),
4259 NEONMAP2(vrsqrteq_v, arm_neon_vrsqrte, arm_neon_vrsqrte, 0),
4260 NEONMAP1(vrsqrts_v, arm_neon_vrsqrts, Add1ArgType),
4261 NEONMAP1(vrsqrtsq_v, arm_neon_vrsqrts, Add1ArgType),
4262 NEONMAP1(vrsubhn_v, arm_neon_vrsubhn, Add1ArgType),
4263 NEONMAP1(vsha1su0q_v, arm_neon_sha1su0, 0),
4264 NEONMAP1(vsha1su1q_v, arm_neon_sha1su1, 0),
4265 NEONMAP1(vsha256h2q_v, arm_neon_sha256h2, 0),
4266 NEONMAP1(vsha256hq_v, arm_neon_sha256h, 0),
4267 NEONMAP1(vsha256su0q_v, arm_neon_sha256su0, 0),
4268 NEONMAP1(vsha256su1q_v, arm_neon_sha256su1, 0),
4269 NEONMAP0(vshl_n_v),
4270 NEONMAP2(vshl_v, arm_neon_vshiftu, arm_neon_vshifts, Add1ArgType | UnsignedAlts),
4271 NEONMAP0(vshll_n_v),
4272 NEONMAP0(vshlq_n_v),
4273 NEONMAP2(vshlq_v, arm_neon_vshiftu, arm_neon_vshifts, Add1ArgType | UnsignedAlts),
4274 NEONMAP0(vshr_n_v),
4275 NEONMAP0(vshrn_n_v),
4276 NEONMAP0(vshrq_n_v),
4277 NEONMAP1(vst1_v, arm_neon_vst1, 0),
4278 NEONMAP1(vst1_x2_v, arm_neon_vst1x2, 0),
4279 NEONMAP1(vst1_x3_v, arm_neon_vst1x3, 0),
4280 NEONMAP1(vst1_x4_v, arm_neon_vst1x4, 0),
4281 NEONMAP1(vst1q_v, arm_neon_vst1, 0),
4282 NEONMAP1(vst1q_x2_v, arm_neon_vst1x2, 0),
4283 NEONMAP1(vst1q_x3_v, arm_neon_vst1x3, 0),
4284 NEONMAP1(vst1q_x4_v, arm_neon_vst1x4, 0),
4285 NEONMAP1(vst2_lane_v, arm_neon_vst2lane, 0),
4286 NEONMAP1(vst2_v, arm_neon_vst2, 0),
4287 NEONMAP1(vst2q_lane_v, arm_neon_vst2lane, 0),
4288 NEONMAP1(vst2q_v, arm_neon_vst2, 0),
4289 NEONMAP1(vst3_lane_v, arm_neon_vst3lane, 0),
4290 NEONMAP1(vst3_v, arm_neon_vst3, 0),
4291 NEONMAP1(vst3q_lane_v, arm_neon_vst3lane, 0),
4292 NEONMAP1(vst3q_v, arm_neon_vst3, 0),
4293 NEONMAP1(vst4_lane_v, arm_neon_vst4lane, 0),
4294 NEONMAP1(vst4_v, arm_neon_vst4, 0),
4295 NEONMAP1(vst4q_lane_v, arm_neon_vst4lane, 0),
4296 NEONMAP1(vst4q_v, arm_neon_vst4, 0),
4297 NEONMAP0(vsubhn_v),
4298 NEONMAP0(vtrn_v),
4299 NEONMAP0(vtrnq_v),
4300 NEONMAP0(vtst_v),
4301 NEONMAP0(vtstq_v),
4302 NEONMAP0(vuzp_v),
4303 NEONMAP0(vuzpq_v),
4304 NEONMAP0(vzip_v),
4305 NEONMAP0(vzipq_v)
4306};
4307
4308static const NeonIntrinsicInfo AArch64SIMDIntrinsicMap[] = {
4309 NEONMAP1(vabs_v, aarch64_neon_abs, 0),
4310 NEONMAP1(vabsq_v, aarch64_neon_abs, 0),
4311 NEONMAP0(vaddhn_v),
4312 NEONMAP1(vaesdq_v, aarch64_crypto_aesd, 0),
4313 NEONMAP1(vaeseq_v, aarch64_crypto_aese, 0),
4314 NEONMAP1(vaesimcq_v, aarch64_crypto_aesimc, 0),
4315 NEONMAP1(vaesmcq_v, aarch64_crypto_aesmc, 0),
4316 NEONMAP1(vcage_v, aarch64_neon_facge, 0),
4317 NEONMAP1(vcageq_v, aarch64_neon_facge, 0),
4318 NEONMAP1(vcagt_v, aarch64_neon_facgt, 0),
4319 NEONMAP1(vcagtq_v, aarch64_neon_facgt, 0),
4320 NEONMAP1(vcale_v, aarch64_neon_facge, 0),
4321 NEONMAP1(vcaleq_v, aarch64_neon_facge, 0),
4322 NEONMAP1(vcalt_v, aarch64_neon_facgt, 0),
4323 NEONMAP1(vcaltq_v, aarch64_neon_facgt, 0),
4324 NEONMAP0(vceqz_v),
4325 NEONMAP0(vceqzq_v),
4326 NEONMAP0(vcgez_v),
4327 NEONMAP0(vcgezq_v),
4328 NEONMAP0(vcgtz_v),
4329 NEONMAP0(vcgtzq_v),
4330 NEONMAP0(vclez_v),
4331 NEONMAP0(vclezq_v),
4332 NEONMAP1(vcls_v, aarch64_neon_cls, Add1ArgType),
4333 NEONMAP1(vclsq_v, aarch64_neon_cls, Add1ArgType),
4334 NEONMAP0(vcltz_v),
4335 NEONMAP0(vcltzq_v),
4336 NEONMAP1(vclz_v, ctlz, Add1ArgType),
4337 NEONMAP1(vclzq_v, ctlz, Add1ArgType),
4338 NEONMAP1(vcnt_v, ctpop, Add1ArgType),
4339 NEONMAP1(vcntq_v, ctpop, Add1ArgType),
4340 NEONMAP1(vcvt_f16_f32, aarch64_neon_vcvtfp2hf, 0),
4341 NEONMAP0(vcvt_f16_v),
4342 NEONMAP1(vcvt_f32_f16, aarch64_neon_vcvthf2fp, 0),
4343 NEONMAP0(vcvt_f32_v),
4344 NEONMAP2(vcvt_n_f16_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4345 NEONMAP2(vcvt_n_f32_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4346 NEONMAP2(vcvt_n_f64_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4347 NEONMAP1(vcvt_n_s16_v, aarch64_neon_vcvtfp2fxs, 0),
4348 NEONMAP1(vcvt_n_s32_v, aarch64_neon_vcvtfp2fxs, 0),
4349 NEONMAP1(vcvt_n_s64_v, aarch64_neon_vcvtfp2fxs, 0),
4350 NEONMAP1(vcvt_n_u16_v, aarch64_neon_vcvtfp2fxu, 0),
4351 NEONMAP1(vcvt_n_u32_v, aarch64_neon_vcvtfp2fxu, 0),
4352 NEONMAP1(vcvt_n_u64_v, aarch64_neon_vcvtfp2fxu, 0),
4353 NEONMAP0(vcvtq_f16_v),
4354 NEONMAP0(vcvtq_f32_v),
4355 NEONMAP2(vcvtq_n_f16_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4356 NEONMAP2(vcvtq_n_f32_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4357 NEONMAP2(vcvtq_n_f64_v, aarch64_neon_vcvtfxu2fp, aarch64_neon_vcvtfxs2fp, 0),
4358 NEONMAP1(vcvtq_n_s16_v, aarch64_neon_vcvtfp2fxs, 0),
4359 NEONMAP1(vcvtq_n_s32_v, aarch64_neon_vcvtfp2fxs, 0),
4360 NEONMAP1(vcvtq_n_s64_v, aarch64_neon_vcvtfp2fxs, 0),
4361 NEONMAP1(vcvtq_n_u16_v, aarch64_neon_vcvtfp2fxu, 0),
4362 NEONMAP1(vcvtq_n_u32_v, aarch64_neon_vcvtfp2fxu, 0),
4363 NEONMAP1(vcvtq_n_u64_v, aarch64_neon_vcvtfp2fxu, 0),
4364 NEONMAP1(vcvtx_f32_v, aarch64_neon_fcvtxn, AddRetType | Add1ArgType),
4365 NEONMAP2(vdot_v, aarch64_neon_udot, aarch64_neon_sdot, 0),
4366 NEONMAP2(vdotq_v, aarch64_neon_udot, aarch64_neon_sdot, 0),
4367 NEONMAP0(vext_v),
4368 NEONMAP0(vextq_v),
4369 NEONMAP0(vfma_v),
4370 NEONMAP0(vfmaq_v),
4371 NEONMAP1(vfmlal_high_v, aarch64_neon_fmlal2, 0),
4372 NEONMAP1(vfmlal_low_v, aarch64_neon_fmlal, 0),
4373 NEONMAP1(vfmlalq_high_v, aarch64_neon_fmlal2, 0),
4374 NEONMAP1(vfmlalq_low_v, aarch64_neon_fmlal, 0),
4375 NEONMAP1(vfmlsl_high_v, aarch64_neon_fmlsl2, 0),
4376 NEONMAP1(vfmlsl_low_v, aarch64_neon_fmlsl, 0),
4377 NEONMAP1(vfmlslq_high_v, aarch64_neon_fmlsl2, 0),
4378 NEONMAP1(vfmlslq_low_v, aarch64_neon_fmlsl, 0),
4379 NEONMAP2(vhadd_v, aarch64_neon_uhadd, aarch64_neon_shadd, Add1ArgType | UnsignedAlts),
4380 NEONMAP2(vhaddq_v, aarch64_neon_uhadd, aarch64_neon_shadd, Add1ArgType | UnsignedAlts),
4381 NEONMAP2(vhsub_v, aarch64_neon_uhsub, aarch64_neon_shsub, Add1ArgType | UnsignedAlts),
4382 NEONMAP2(vhsubq_v, aarch64_neon_uhsub, aarch64_neon_shsub, Add1ArgType | UnsignedAlts),
4383 NEONMAP1(vld1_x2_v, aarch64_neon_ld1x2, 0),
4384 NEONMAP1(vld1_x3_v, aarch64_neon_ld1x3, 0),
4385 NEONMAP1(vld1_x4_v, aarch64_neon_ld1x4, 0),
4386 NEONMAP1(vld1q_x2_v, aarch64_neon_ld1x2, 0),
4387 NEONMAP1(vld1q_x3_v, aarch64_neon_ld1x3, 0),
4388 NEONMAP1(vld1q_x4_v, aarch64_neon_ld1x4, 0),
4389 NEONMAP0(vmovl_v),
4390 NEONMAP0(vmovn_v),
4391 NEONMAP1(vmul_v, aarch64_neon_pmul, Add1ArgType),
4392 NEONMAP1(vmulq_v, aarch64_neon_pmul, Add1ArgType),
4393 NEONMAP1(vpadd_v, aarch64_neon_addp, Add1ArgType),
4394 NEONMAP2(vpaddl_v, aarch64_neon_uaddlp, aarch64_neon_saddlp, UnsignedAlts),
4395 NEONMAP2(vpaddlq_v, aarch64_neon_uaddlp, aarch64_neon_saddlp, UnsignedAlts),
4396 NEONMAP1(vpaddq_v, aarch64_neon_addp, Add1ArgType),
4397 NEONMAP1(vqabs_v, aarch64_neon_sqabs, Add1ArgType),
4398 NEONMAP1(vqabsq_v, aarch64_neon_sqabs, Add1ArgType),
4399 NEONMAP2(vqadd_v, aarch64_neon_uqadd, aarch64_neon_sqadd, Add1ArgType | UnsignedAlts),
4400 NEONMAP2(vqaddq_v, aarch64_neon_uqadd, aarch64_neon_sqadd, Add1ArgType | UnsignedAlts),
4401 NEONMAP2(vqdmlal_v, aarch64_neon_sqdmull, aarch64_neon_sqadd, 0),
4402 NEONMAP2(vqdmlsl_v, aarch64_neon_sqdmull, aarch64_neon_sqsub, 0),
4403 NEONMAP1(vqdmulh_v, aarch64_neon_sqdmulh, Add1ArgType),
4404 NEONMAP1(vqdmulhq_v, aarch64_neon_sqdmulh, Add1ArgType),
4405 NEONMAP1(vqdmull_v, aarch64_neon_sqdmull, Add1ArgType),
4406 NEONMAP2(vqmovn_v, aarch64_neon_uqxtn, aarch64_neon_sqxtn, Add1ArgType | UnsignedAlts),
4407 NEONMAP1(vqmovun_v, aarch64_neon_sqxtun, Add1ArgType),
4408 NEONMAP1(vqneg_v, aarch64_neon_sqneg, Add1ArgType),
4409 NEONMAP1(vqnegq_v, aarch64_neon_sqneg, Add1ArgType),
4410 NEONMAP1(vqrdmulh_v, aarch64_neon_sqrdmulh, Add1ArgType),
4411 NEONMAP1(vqrdmulhq_v, aarch64_neon_sqrdmulh, Add1ArgType),
4412 NEONMAP2(vqrshl_v, aarch64_neon_uqrshl, aarch64_neon_sqrshl, Add1ArgType | UnsignedAlts),
4413 NEONMAP2(vqrshlq_v, aarch64_neon_uqrshl, aarch64_neon_sqrshl, Add1ArgType | UnsignedAlts),
4414 NEONMAP2(vqshl_n_v, aarch64_neon_uqshl, aarch64_neon_sqshl, UnsignedAlts),
4415 NEONMAP2(vqshl_v, aarch64_neon_uqshl, aarch64_neon_sqshl, Add1ArgType | UnsignedAlts),
4416 NEONMAP2(vqshlq_n_v, aarch64_neon_uqshl, aarch64_neon_sqshl,UnsignedAlts),
4417 NEONMAP2(vqshlq_v, aarch64_neon_uqshl, aarch64_neon_sqshl, Add1ArgType | UnsignedAlts),
4418 NEONMAP1(vqshlu_n_v, aarch64_neon_sqshlu, 0),
4419 NEONMAP1(vqshluq_n_v, aarch64_neon_sqshlu, 0),
4420 NEONMAP2(vqsub_v, aarch64_neon_uqsub, aarch64_neon_sqsub, Add1ArgType | UnsignedAlts),
4421 NEONMAP2(vqsubq_v, aarch64_neon_uqsub, aarch64_neon_sqsub, Add1ArgType | UnsignedAlts),
4422 NEONMAP1(vraddhn_v, aarch64_neon_raddhn, Add1ArgType),
4423 NEONMAP2(vrecpe_v, aarch64_neon_frecpe, aarch64_neon_urecpe, 0),
4424 NEONMAP2(vrecpeq_v, aarch64_neon_frecpe, aarch64_neon_urecpe, 0),
4425 NEONMAP1(vrecps_v, aarch64_neon_frecps, Add1ArgType),
4426 NEONMAP1(vrecpsq_v, aarch64_neon_frecps, Add1ArgType),
4427 NEONMAP2(vrhadd_v, aarch64_neon_urhadd, aarch64_neon_srhadd, Add1ArgType | UnsignedAlts),
4428 NEONMAP2(vrhaddq_v, aarch64_neon_urhadd, aarch64_neon_srhadd, Add1ArgType | UnsignedAlts),
4429 NEONMAP0(vrndi_v),
4430 NEONMAP0(vrndiq_v),
4431 NEONMAP2(vrshl_v, aarch64_neon_urshl, aarch64_neon_srshl, Add1ArgType | UnsignedAlts),
4432 NEONMAP2(vrshlq_v, aarch64_neon_urshl, aarch64_neon_srshl, Add1ArgType | UnsignedAlts),
4433 NEONMAP2(vrshr_n_v, aarch64_neon_urshl, aarch64_neon_srshl, UnsignedAlts),
4434 NEONMAP2(vrshrq_n_v, aarch64_neon_urshl, aarch64_neon_srshl, UnsignedAlts),
4435 NEONMAP2(vrsqrte_v, aarch64_neon_frsqrte, aarch64_neon_ursqrte, 0),
4436 NEONMAP2(vrsqrteq_v, aarch64_neon_frsqrte, aarch64_neon_ursqrte, 0),
4437 NEONMAP1(vrsqrts_v, aarch64_neon_frsqrts, Add1ArgType),
4438 NEONMAP1(vrsqrtsq_v, aarch64_neon_frsqrts, Add1ArgType),
4439 NEONMAP1(vrsubhn_v, aarch64_neon_rsubhn, Add1ArgType),
4440 NEONMAP1(vsha1su0q_v, aarch64_crypto_sha1su0, 0),
4441 NEONMAP1(vsha1su1q_v, aarch64_crypto_sha1su1, 0),
4442 NEONMAP1(vsha256h2q_v, aarch64_crypto_sha256h2, 0),
4443 NEONMAP1(vsha256hq_v, aarch64_crypto_sha256h, 0),
4444 NEONMAP1(vsha256su0q_v, aarch64_crypto_sha256su0, 0),
4445 NEONMAP1(vsha256su1q_v, aarch64_crypto_sha256su1, 0),
4446 NEONMAP0(vshl_n_v),
4447 NEONMAP2(vshl_v, aarch64_neon_ushl, aarch64_neon_sshl, Add1ArgType | UnsignedAlts),
4448 NEONMAP0(vshll_n_v),
4449 NEONMAP0(vshlq_n_v),
4450 NEONMAP2(vshlq_v, aarch64_neon_ushl, aarch64_neon_sshl, Add1ArgType | UnsignedAlts),
4451 NEONMAP0(vshr_n_v),
4452 NEONMAP0(vshrn_n_v),
4453 NEONMAP0(vshrq_n_v),
4454 NEONMAP1(vst1_x2_v, aarch64_neon_st1x2, 0),
4455 NEONMAP1(vst1_x3_v, aarch64_neon_st1x3, 0),
4456 NEONMAP1(vst1_x4_v, aarch64_neon_st1x4, 0),
4457 NEONMAP1(vst1q_x2_v, aarch64_neon_st1x2, 0),
4458 NEONMAP1(vst1q_x3_v, aarch64_neon_st1x3, 0),
4459 NEONMAP1(vst1q_x4_v, aarch64_neon_st1x4, 0),
4460 NEONMAP0(vsubhn_v),
4461 NEONMAP0(vtst_v),
4462 NEONMAP0(vtstq_v),
4463};
4464
4465static const NeonIntrinsicInfo AArch64SISDIntrinsicMap[] = {
4466 NEONMAP1(vabdd_f64, aarch64_sisd_fabd, Add1ArgType),
4467 NEONMAP1(vabds_f32, aarch64_sisd_fabd, Add1ArgType),
4468 NEONMAP1(vabsd_s64, aarch64_neon_abs, Add1ArgType),
4469 NEONMAP1(vaddlv_s32, aarch64_neon_saddlv, AddRetType | Add1ArgType),
4470 NEONMAP1(vaddlv_u32, aarch64_neon_uaddlv, AddRetType | Add1ArgType),
4471 NEONMAP1(vaddlvq_s32, aarch64_neon_saddlv, AddRetType | Add1ArgType),
4472 NEONMAP1(vaddlvq_u32, aarch64_neon_uaddlv, AddRetType | Add1ArgType),
4473 NEONMAP1(vaddv_f32, aarch64_neon_faddv, AddRetType | Add1ArgType),
4474 NEONMAP1(vaddv_s32, aarch64_neon_saddv, AddRetType | Add1ArgType),
4475 NEONMAP1(vaddv_u32, aarch64_neon_uaddv, AddRetType | Add1ArgType),
4476 NEONMAP1(vaddvq_f32, aarch64_neon_faddv, AddRetType | Add1ArgType),
4477 NEONMAP1(vaddvq_f64, aarch64_neon_faddv, AddRetType | Add1ArgType),
4478 NEONMAP1(vaddvq_s32, aarch64_neon_saddv, AddRetType | Add1ArgType),
4479 NEONMAP1(vaddvq_s64, aarch64_neon_saddv, AddRetType | Add1ArgType),
4480 NEONMAP1(vaddvq_u32, aarch64_neon_uaddv, AddRetType | Add1ArgType),
4481 NEONMAP1(vaddvq_u64, aarch64_neon_uaddv, AddRetType | Add1ArgType),
4482 NEONMAP1(vcaged_f64, aarch64_neon_facge, AddRetType | Add1ArgType),
4483 NEONMAP1(vcages_f32, aarch64_neon_facge, AddRetType | Add1ArgType),
4484 NEONMAP1(vcagtd_f64, aarch64_neon_facgt, AddRetType | Add1ArgType),
4485 NEONMAP1(vcagts_f32, aarch64_neon_facgt, AddRetType | Add1ArgType),
4486 NEONMAP1(vcaled_f64, aarch64_neon_facge, AddRetType | Add1ArgType),
4487 NEONMAP1(vcales_f32, aarch64_neon_facge, AddRetType | Add1ArgType),
4488 NEONMAP1(vcaltd_f64, aarch64_neon_facgt, AddRetType | Add1ArgType),
4489 NEONMAP1(vcalts_f32, aarch64_neon_facgt, AddRetType | Add1ArgType),
4490 NEONMAP1(vcvtad_s64_f64, aarch64_neon_fcvtas, AddRetType | Add1ArgType),
4491 NEONMAP1(vcvtad_u64_f64, aarch64_neon_fcvtau, AddRetType | Add1ArgType),
4492 NEONMAP1(vcvtas_s32_f32, aarch64_neon_fcvtas, AddRetType | Add1ArgType),
4493 NEONMAP1(vcvtas_u32_f32, aarch64_neon_fcvtau, AddRetType | Add1ArgType),
4494 NEONMAP1(vcvtd_n_f64_s64, aarch64_neon_vcvtfxs2fp, AddRetType | Add1ArgType),
4495 NEONMAP1(vcvtd_n_f64_u64, aarch64_neon_vcvtfxu2fp, AddRetType | Add1ArgType),
4496 NEONMAP1(vcvtd_n_s64_f64, aarch64_neon_vcvtfp2fxs, AddRetType | Add1ArgType),
4497 NEONMAP1(vcvtd_n_u64_f64, aarch64_neon_vcvtfp2fxu, AddRetType | Add1ArgType),
4498 NEONMAP1(vcvtmd_s64_f64, aarch64_neon_fcvtms, AddRetType | Add1ArgType),
4499 NEONMAP1(vcvtmd_u64_f64, aarch64_neon_fcvtmu, AddRetType | Add1ArgType),
4500 NEONMAP1(vcvtms_s32_f32, aarch64_neon_fcvtms, AddRetType | Add1ArgType),
4501 NEONMAP1(vcvtms_u32_f32, aarch64_neon_fcvtmu, AddRetType | Add1ArgType),
4502 NEONMAP1(vcvtnd_s64_f64, aarch64_neon_fcvtns, AddRetType | Add1ArgType),
4503 NEONMAP1(vcvtnd_u64_f64, aarch64_neon_fcvtnu, AddRetType | Add1ArgType),
4504 NEONMAP1(vcvtns_s32_f32, aarch64_neon_fcvtns, AddRetType | Add1ArgType),
4505 NEONMAP1(vcvtns_u32_f32, aarch64_neon_fcvtnu, AddRetType | Add1ArgType),
4506 NEONMAP1(vcvtpd_s64_f64, aarch64_neon_fcvtps, AddRetType | Add1ArgType),
4507 NEONMAP1(vcvtpd_u64_f64, aarch64_neon_fcvtpu, AddRetType | Add1ArgType),
4508 NEONMAP1(vcvtps_s32_f32, aarch64_neon_fcvtps, AddRetType | Add1ArgType),
4509 NEONMAP1(vcvtps_u32_f32, aarch64_neon_fcvtpu, AddRetType | Add1ArgType),
4510 NEONMAP1(vcvts_n_f32_s32, aarch64_neon_vcvtfxs2fp, AddRetType | Add1ArgType),
4511 NEONMAP1(vcvts_n_f32_u32, aarch64_neon_vcvtfxu2fp, AddRetType | Add1ArgType),
4512 NEONMAP1(vcvts_n_s32_f32, aarch64_neon_vcvtfp2fxs, AddRetType | Add1ArgType),
4513 NEONMAP1(vcvts_n_u32_f32, aarch64_neon_vcvtfp2fxu, AddRetType | Add1ArgType),
4514 NEONMAP1(vcvtxd_f32_f64, aarch64_sisd_fcvtxn, 0),
4515 NEONMAP1(vmaxnmv_f32, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
4516 NEONMAP1(vmaxnmvq_f32, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
4517 NEONMAP1(vmaxnmvq_f64, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
4518 NEONMAP1(vmaxv_f32, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
4519 NEONMAP1(vmaxv_s32, aarch64_neon_smaxv, AddRetType | Add1ArgType),
4520 NEONMAP1(vmaxv_u32, aarch64_neon_umaxv, AddRetType | Add1ArgType),
4521 NEONMAP1(vmaxvq_f32, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
4522 NEONMAP1(vmaxvq_f64, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
4523 NEONMAP1(vmaxvq_s32, aarch64_neon_smaxv, AddRetType | Add1ArgType),
4524 NEONMAP1(vmaxvq_u32, aarch64_neon_umaxv, AddRetType | Add1ArgType),
4525 NEONMAP1(vminnmv_f32, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
4526 NEONMAP1(vminnmvq_f32, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
4527 NEONMAP1(vminnmvq_f64, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
4528 NEONMAP1(vminv_f32, aarch64_neon_fminv, AddRetType | Add1ArgType),
4529 NEONMAP1(vminv_s32, aarch64_neon_sminv, AddRetType | Add1ArgType),
4530 NEONMAP1(vminv_u32, aarch64_neon_uminv, AddRetType | Add1ArgType),
4531 NEONMAP1(vminvq_f32, aarch64_neon_fminv, AddRetType | Add1ArgType),
4532 NEONMAP1(vminvq_f64, aarch64_neon_fminv, AddRetType | Add1ArgType),
4533 NEONMAP1(vminvq_s32, aarch64_neon_sminv, AddRetType | Add1ArgType),
4534 NEONMAP1(vminvq_u32, aarch64_neon_uminv, AddRetType | Add1ArgType),
4535 NEONMAP1(vmull_p64, aarch64_neon_pmull64, 0),
4536 NEONMAP1(vmulxd_f64, aarch64_neon_fmulx, Add1ArgType),
4537 NEONMAP1(vmulxs_f32, aarch64_neon_fmulx, Add1ArgType),
4538 NEONMAP1(vpaddd_s64, aarch64_neon_uaddv, AddRetType | Add1ArgType),
4539 NEONMAP1(vpaddd_u64, aarch64_neon_uaddv, AddRetType | Add1ArgType),
4540 NEONMAP1(vpmaxnmqd_f64, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
4541 NEONMAP1(vpmaxnms_f32, aarch64_neon_fmaxnmv, AddRetType | Add1ArgType),
4542 NEONMAP1(vpmaxqd_f64, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
4543 NEONMAP1(vpmaxs_f32, aarch64_neon_fmaxv, AddRetType | Add1ArgType),
4544 NEONMAP1(vpminnmqd_f64, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
4545 NEONMAP1(vpminnms_f32, aarch64_neon_fminnmv, AddRetType | Add1ArgType),
4546 NEONMAP1(vpminqd_f64, aarch64_neon_fminv, AddRetType | Add1ArgType),
4547 NEONMAP1(vpmins_f32, aarch64_neon_fminv, AddRetType | Add1ArgType),
4548 NEONMAP1(vqabsb_s8, aarch64_neon_sqabs, Vectorize1ArgType | Use64BitVectors),
4549 NEONMAP1(vqabsd_s64, aarch64_neon_sqabs, Add1ArgType),
4550 NEONMAP1(vqabsh_s16, aarch64_neon_sqabs, Vectorize1ArgType | Use64BitVectors),
4551 NEONMAP1(vqabss_s32, aarch64_neon_sqabs, Add1ArgType),
4552 NEONMAP1(vqaddb_s8, aarch64_neon_sqadd, Vectorize1ArgType | Use64BitVectors),
4553 NEONMAP1(vqaddb_u8, aarch64_neon_uqadd, Vectorize1ArgType | Use64BitVectors),
4554 NEONMAP1(vqaddd_s64, aarch64_neon_sqadd, Add1ArgType),
4555 NEONMAP1(vqaddd_u64, aarch64_neon_uqadd, Add1ArgType),
4556 NEONMAP1(vqaddh_s16, aarch64_neon_sqadd, Vectorize1ArgType | Use64BitVectors),
4557 NEONMAP1(vqaddh_u16, aarch64_neon_uqadd, Vectorize1ArgType | Use64BitVectors),
4558 NEONMAP1(vqadds_s32, aarch64_neon_sqadd, Add1ArgType),
4559 NEONMAP1(vqadds_u32, aarch64_neon_uqadd, Add1ArgType),
4560 NEONMAP1(vqdmulhh_s16, aarch64_neon_sqdmulh, Vectorize1ArgType | Use64BitVectors),
4561 NEONMAP1(vqdmulhs_s32, aarch64_neon_sqdmulh, Add1ArgType),
4562 NEONMAP1(vqdmullh_s16, aarch64_neon_sqdmull, VectorRet | Use128BitVectors),
4563 NEONMAP1(vqdmulls_s32, aarch64_neon_sqdmulls_scalar, 0),
4564 NEONMAP1(vqmovnd_s64, aarch64_neon_scalar_sqxtn, AddRetType | Add1ArgType),
4565 NEONMAP1(vqmovnd_u64, aarch64_neon_scalar_uqxtn, AddRetType | Add1ArgType),
4566 NEONMAP1(vqmovnh_s16, aarch64_neon_sqxtn, VectorRet | Use64BitVectors),
4567 NEONMAP1(vqmovnh_u16, aarch64_neon_uqxtn, VectorRet | Use64BitVectors),
4568 NEONMAP1(vqmovns_s32, aarch64_neon_sqxtn, VectorRet | Use64BitVectors),
4569 NEONMAP1(vqmovns_u32, aarch64_neon_uqxtn, VectorRet | Use64BitVectors),
4570 NEONMAP1(vqmovund_s64, aarch64_neon_scalar_sqxtun, AddRetType | Add1ArgType),
4571 NEONMAP1(vqmovunh_s16, aarch64_neon_sqxtun, VectorRet | Use64BitVectors),
4572 NEONMAP1(vqmovuns_s32, aarch64_neon_sqxtun, VectorRet | Use64BitVectors),
4573 NEONMAP1(vqnegb_s8, aarch64_neon_sqneg, Vectorize1ArgType | Use64BitVectors),
4574 NEONMAP1(vqnegd_s64, aarch64_neon_sqneg, Add1ArgType),
4575 NEONMAP1(vqnegh_s16, aarch64_neon_sqneg, Vectorize1ArgType | Use64BitVectors),
4576 NEONMAP1(vqnegs_s32, aarch64_neon_sqneg, Add1ArgType),
4577 NEONMAP1(vqrdmulhh_s16, aarch64_neon_sqrdmulh, Vectorize1ArgType | Use64BitVectors),
4578 NEONMAP1(vqrdmulhs_s32, aarch64_neon_sqrdmulh, Add1ArgType),
4579 NEONMAP1(vqrshlb_s8, aarch64_neon_sqrshl, Vectorize1ArgType | Use64BitVectors),
4580 NEONMAP1(vqrshlb_u8, aarch64_neon_uqrshl, Vectorize1ArgType | Use64BitVectors),
4581 NEONMAP1(vqrshld_s64, aarch64_neon_sqrshl, Add1ArgType),
4582 NEONMAP1(vqrshld_u64, aarch64_neon_uqrshl, Add1ArgType),
4583 NEONMAP1(vqrshlh_s16, aarch64_neon_sqrshl, Vectorize1ArgType | Use64BitVectors),
4584 NEONMAP1(vqrshlh_u16, aarch64_neon_uqrshl, Vectorize1ArgType | Use64BitVectors),
4585 NEONMAP1(vqrshls_s32, aarch64_neon_sqrshl, Add1ArgType),
4586 NEONMAP1(vqrshls_u32, aarch64_neon_uqrshl, Add1ArgType),
4587 NEONMAP1(vqrshrnd_n_s64, aarch64_neon_sqrshrn, AddRetType),
4588 NEONMAP1(vqrshrnd_n_u64, aarch64_neon_uqrshrn, AddRetType),
4589 NEONMAP1(vqrshrnh_n_s16, aarch64_neon_sqrshrn, VectorRet | Use64BitVectors),
4590 NEONMAP1(vqrshrnh_n_u16, aarch64_neon_uqrshrn, VectorRet | Use64BitVectors),
4591 NEONMAP1(vqrshrns_n_s32, aarch64_neon_sqrshrn, VectorRet | Use64BitVectors),
4592 NEONMAP1(vqrshrns_n_u32, aarch64_neon_uqrshrn, VectorRet | Use64BitVectors),
4593 NEONMAP1(vqrshrund_n_s64, aarch64_neon_sqrshrun, AddRetType),
4594 NEONMAP1(vqrshrunh_n_s16, aarch64_neon_sqrshrun, VectorRet | Use64BitVectors),
4595 NEONMAP1(vqrshruns_n_s32, aarch64_neon_sqrshrun, VectorRet | Use64BitVectors),
4596 NEONMAP1(vqshlb_n_s8, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
4597 NEONMAP1(vqshlb_n_u8, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
4598 NEONMAP1(vqshlb_s8, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
4599 NEONMAP1(vqshlb_u8, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
4600 NEONMAP1(vqshld_s64, aarch64_neon_sqshl, Add1ArgType),
4601 NEONMAP1(vqshld_u64, aarch64_neon_uqshl, Add1ArgType),
4602 NEONMAP1(vqshlh_n_s16, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
4603 NEONMAP1(vqshlh_n_u16, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
4604 NEONMAP1(vqshlh_s16, aarch64_neon_sqshl, Vectorize1ArgType | Use64BitVectors),
4605 NEONMAP1(vqshlh_u16, aarch64_neon_uqshl, Vectorize1ArgType | Use64BitVectors),
4606 NEONMAP1(vqshls_n_s32, aarch64_neon_sqshl, Add1ArgType),
4607 NEONMAP1(vqshls_n_u32, aarch64_neon_uqshl, Add1ArgType),
4608 NEONMAP1(vqshls_s32, aarch64_neon_sqshl, Add1ArgType),
4609 NEONMAP1(vqshls_u32, aarch64_neon_uqshl, Add1ArgType),
4610 NEONMAP1(vqshlub_n_s8, aarch64_neon_sqshlu, Vectorize1ArgType | Use64BitVectors),
4611 NEONMAP1(vqshluh_n_s16, aarch64_neon_sqshlu, Vectorize1ArgType | Use64BitVectors),
4612 NEONMAP1(vqshlus_n_s32, aarch64_neon_sqshlu, Add1ArgType),
4613 NEONMAP1(vqshrnd_n_s64, aarch64_neon_sqshrn, AddRetType),
4614 NEONMAP1(vqshrnd_n_u64, aarch64_neon_uqshrn, AddRetType),
4615 NEONMAP1(vqshrnh_n_s16, aarch64_neon_sqshrn, VectorRet | Use64BitVectors),
4616 NEONMAP1(vqshrnh_n_u16, aarch64_neon_uqshrn, VectorRet | Use64BitVectors),
4617 NEONMAP1(vqshrns_n_s32, aarch64_neon_sqshrn, VectorRet | Use64BitVectors),
4618 NEONMAP1(vqshrns_n_u32, aarch64_neon_uqshrn, VectorRet | Use64BitVectors),
4619 NEONMAP1(vqshrund_n_s64, aarch64_neon_sqshrun, AddRetType),
4620 NEONMAP1(vqshrunh_n_s16, aarch64_neon_sqshrun, VectorRet | Use64BitVectors),
4621 NEONMAP1(vqshruns_n_s32, aarch64_neon_sqshrun, VectorRet | Use64BitVectors),
4622 NEONMAP1(vqsubb_s8, aarch64_neon_sqsub, Vectorize1ArgType | Use64BitVectors),
4623 NEONMAP1(vqsubb_u8, aarch64_neon_uqsub, Vectorize1ArgType | Use64BitVectors),
4624 NEONMAP1(vqsubd_s64, aarch64_neon_sqsub, Add1ArgType),
4625 NEONMAP1(vqsubd_u64, aarch64_neon_uqsub, Add1ArgType),
4626 NEONMAP1(vqsubh_s16, aarch64_neon_sqsub, Vectorize1ArgType | Use64BitVectors),
4627 NEONMAP1(vqsubh_u16, aarch64_neon_uqsub, Vectorize1ArgType | Use64BitVectors),
4628 NEONMAP1(vqsubs_s32, aarch64_neon_sqsub, Add1ArgType),
4629 NEONMAP1(vqsubs_u32, aarch64_neon_uqsub, Add1ArgType),
4630 NEONMAP1(vrecped_f64, aarch64_neon_frecpe, Add1ArgType),
4631 NEONMAP1(vrecpes_f32, aarch64_neon_frecpe, Add1ArgType),
4632 NEONMAP1(vrecpxd_f64, aarch64_neon_frecpx, Add1ArgType),
4633 NEONMAP1(vrecpxs_f32, aarch64_neon_frecpx, Add1ArgType),
4634 NEONMAP1(vrshld_s64, aarch64_neon_srshl, Add1ArgType),
4635 NEONMAP1(vrshld_u64, aarch64_neon_urshl, Add1ArgType),
4636 NEONMAP1(vrsqrted_f64, aarch64_neon_frsqrte, Add1ArgType),
4637 NEONMAP1(vrsqrtes_f32, aarch64_neon_frsqrte, Add1ArgType),
4638 NEONMAP1(vrsqrtsd_f64, aarch64_neon_frsqrts, Add1ArgType),
4639 NEONMAP1(vrsqrtss_f32, aarch64_neon_frsqrts, Add1ArgType),
4640 NEONMAP1(vsha1cq_u32, aarch64_crypto_sha1c, 0),
4641 NEONMAP1(vsha1h_u32, aarch64_crypto_sha1h, 0),
4642 NEONMAP1(vsha1mq_u32, aarch64_crypto_sha1m, 0),
4643 NEONMAP1(vsha1pq_u32, aarch64_crypto_sha1p, 0),
4644 NEONMAP1(vshld_s64, aarch64_neon_sshl, Add1ArgType),
4645 NEONMAP1(vshld_u64, aarch64_neon_ushl, Add1ArgType),
4646 NEONMAP1(vslid_n_s64, aarch64_neon_vsli, Vectorize1ArgType),
4647 NEONMAP1(vslid_n_u64, aarch64_neon_vsli, Vectorize1ArgType),
4648 NEONMAP1(vsqaddb_u8, aarch64_neon_usqadd, Vectorize1ArgType | Use64BitVectors),
4649 NEONMAP1(vsqaddd_u64, aarch64_neon_usqadd, Add1ArgType),
4650 NEONMAP1(vsqaddh_u16, aarch64_neon_usqadd, Vectorize1ArgType | Use64BitVectors),
4651 NEONMAP1(vsqadds_u32, aarch64_neon_usqadd, Add1ArgType),
4652 NEONMAP1(vsrid_n_s64, aarch64_neon_vsri, Vectorize1ArgType),
4653 NEONMAP1(vsrid_n_u64, aarch64_neon_vsri, Vectorize1ArgType),
4654 NEONMAP1(vuqaddb_s8, aarch64_neon_suqadd, Vectorize1ArgType | Use64BitVectors),
4655 NEONMAP1(vuqaddd_s64, aarch64_neon_suqadd, Add1ArgType),
4656 NEONMAP1(vuqaddh_s16, aarch64_neon_suqadd, Vectorize1ArgType | Use64BitVectors),
4657 NEONMAP1(vuqadds_s32, aarch64_neon_suqadd, Add1ArgType),
4658 // FP16 scalar intrinisics go here.
4659 NEONMAP1(vabdh_f16, aarch64_sisd_fabd, Add1ArgType),
4660 NEONMAP1(vcvtah_s32_f16, aarch64_neon_fcvtas, AddRetType | Add1ArgType),
4661 NEONMAP1(vcvtah_s64_f16, aarch64_neon_fcvtas, AddRetType | Add1ArgType),
4662 NEONMAP1(vcvtah_u32_f16, aarch64_neon_fcvtau, AddRetType | Add1ArgType),
4663 NEONMAP1(vcvtah_u64_f16, aarch64_neon_fcvtau, AddRetType | Add1ArgType),
4664 NEONMAP1(vcvth_n_f16_s32, aarch64_neon_vcvtfxs2fp, AddRetType | Add1ArgType),
4665 NEONMAP1(vcvth_n_f16_s64, aarch64_neon_vcvtfxs2fp, AddRetType | Add1ArgType),
4666 NEONMAP1(vcvth_n_f16_u32, aarch64_neon_vcvtfxu2fp, AddRetType | Add1ArgType),
4667 NEONMAP1(vcvth_n_f16_u64, aarch64_neon_vcvtfxu2fp, AddRetType | Add1ArgType),
4668 NEONMAP1(vcvth_n_s32_f16, aarch64_neon_vcvtfp2fxs, AddRetType | Add1ArgType),
4669 NEONMAP1(vcvth_n_s64_f16, aarch64_neon_vcvtfp2fxs, AddRetType | Add1ArgType),
4670 NEONMAP1(vcvth_n_u32_f16, aarch64_neon_vcvtfp2fxu, AddRetType | Add1ArgType),
4671 NEONMAP1(vcvth_n_u64_f16, aarch64_neon_vcvtfp2fxu, AddRetType | Add1ArgType),
4672 NEONMAP1(vcvtmh_s32_f16, aarch64_neon_fcvtms, AddRetType | Add1ArgType),
4673 NEONMAP1(vcvtmh_s64_f16, aarch64_neon_fcvtms, AddRetType | Add1ArgType),
4674 NEONMAP1(vcvtmh_u32_f16, aarch64_neon_fcvtmu, AddRetType | Add1ArgType),
4675 NEONMAP1(vcvtmh_u64_f16, aarch64_neon_fcvtmu, AddRetType | Add1ArgType),
4676 NEONMAP1(vcvtnh_s32_f16, aarch64_neon_fcvtns, AddRetType | Add1ArgType),
4677 NEONMAP1(vcvtnh_s64_f16, aarch64_neon_fcvtns, AddRetType | Add1ArgType),
4678 NEONMAP1(vcvtnh_u32_f16, aarch64_neon_fcvtnu, AddRetType | Add1ArgType),
4679 NEONMAP1(vcvtnh_u64_f16, aarch64_neon_fcvtnu, AddRetType | Add1ArgType),
4680 NEONMAP1(vcvtph_s32_f16, aarch64_neon_fcvtps, AddRetType | Add1ArgType),
4681 NEONMAP1(vcvtph_s64_f16, aarch64_neon_fcvtps, AddRetType | Add1ArgType),
4682 NEONMAP1(vcvtph_u32_f16, aarch64_neon_fcvtpu, AddRetType | Add1ArgType),
4683 NEONMAP1(vcvtph_u64_f16, aarch64_neon_fcvtpu, AddRetType | Add1ArgType),
4684 NEONMAP1(vmulxh_f16, aarch64_neon_fmulx, Add1ArgType),
4685 NEONMAP1(vrecpeh_f16, aarch64_neon_frecpe, Add1ArgType),
4686 NEONMAP1(vrecpxh_f16, aarch64_neon_frecpx, Add1ArgType),
4687 NEONMAP1(vrsqrteh_f16, aarch64_neon_frsqrte, Add1ArgType),
4688 NEONMAP1(vrsqrtsh_f16, aarch64_neon_frsqrts, Add1ArgType),
4689};
4690
4691#undef NEONMAP0
4692#undef NEONMAP1
4693#undef NEONMAP2
4694
4695static bool NEONSIMDIntrinsicsProvenSorted = false;
4696
4697static bool AArch64SIMDIntrinsicsProvenSorted = false;
4698static bool AArch64SISDIntrinsicsProvenSorted = false;
4699
4700
4701static const NeonIntrinsicInfo *
4702findNeonIntrinsicInMap(ArrayRef<NeonIntrinsicInfo> IntrinsicMap,
4703 unsigned BuiltinID, bool &MapProvenSorted) {
4704
4705#ifndef NDEBUG
4706 if (!MapProvenSorted) {
4707 assert(std::is_sorted(std::begin(IntrinsicMap), std::end(IntrinsicMap)))((std::is_sorted(std::begin(IntrinsicMap), std::end(IntrinsicMap
))) ? static_cast<void> (0) : __assert_fail ("std::is_sorted(std::begin(IntrinsicMap), std::end(IntrinsicMap))"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 4707, __PRETTY_FUNCTION__))
;
4708 MapProvenSorted = true;
4709 }
4710#endif
4711
4712 const NeonIntrinsicInfo *Builtin =
4713 std::lower_bound(IntrinsicMap.begin(), IntrinsicMap.end(), BuiltinID);
4714
4715 if (Builtin != IntrinsicMap.end() && Builtin->BuiltinID == BuiltinID)
4716 return Builtin;
4717
4718 return nullptr;
4719}
4720
4721Function *CodeGenFunction::LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
4722 unsigned Modifier,
4723 llvm::Type *ArgType,
4724 const CallExpr *E) {
4725 int VectorSize = 0;
4726 if (Modifier & Use64BitVectors)
4727 VectorSize = 64;
4728 else if (Modifier & Use128BitVectors)
4729 VectorSize = 128;
4730
4731 // Return type.
4732 SmallVector<llvm::Type *, 3> Tys;
4733 if (Modifier & AddRetType) {
4734 llvm::Type *Ty = ConvertType(E->getCallReturnType(getContext()));
4735 if (Modifier & VectorizeRetType)
4736 Ty = llvm::VectorType::get(
4737 Ty, VectorSize ? VectorSize / Ty->getPrimitiveSizeInBits() : 1);
4738
4739 Tys.push_back(Ty);
4740 }
4741
4742 // Arguments.
4743 if (Modifier & VectorizeArgTypes) {
4744 int Elts = VectorSize ? VectorSize / ArgType->getPrimitiveSizeInBits() : 1;
4745 ArgType = llvm::VectorType::get(ArgType, Elts);
4746 }
4747
4748 if (Modifier & (Add1ArgType | Add2ArgTypes))
4749 Tys.push_back(ArgType);
4750
4751 if (Modifier & Add2ArgTypes)
4752 Tys.push_back(ArgType);
4753
4754 if (Modifier & InventFloatType)
4755 Tys.push_back(FloatTy);
4756
4757 return CGM.getIntrinsic(IntrinsicID, Tys);
4758}
4759
4760static Value *EmitCommonNeonSISDBuiltinExpr(CodeGenFunction &CGF,
4761 const NeonIntrinsicInfo &SISDInfo,
4762 SmallVectorImpl<Value *> &Ops,
4763 const CallExpr *E) {
4764 unsigned BuiltinID = SISDInfo.BuiltinID;
4765 unsigned int Int = SISDInfo.LLVMIntrinsic;
4766 unsigned Modifier = SISDInfo.TypeModifier;
4767 const char *s = SISDInfo.NameHint;
4768
4769 switch (BuiltinID) {
4770 case NEON::BI__builtin_neon_vcled_s64:
4771 case NEON::BI__builtin_neon_vcled_u64:
4772 case NEON::BI__builtin_neon_vcles_f32:
4773 case NEON::BI__builtin_neon_vcled_f64:
4774 case NEON::BI__builtin_neon_vcltd_s64:
4775 case NEON::BI__builtin_neon_vcltd_u64:
4776 case NEON::BI__builtin_neon_vclts_f32:
4777 case NEON::BI__builtin_neon_vcltd_f64:
4778 case NEON::BI__builtin_neon_vcales_f32:
4779 case NEON::BI__builtin_neon_vcaled_f64:
4780 case NEON::BI__builtin_neon_vcalts_f32:
4781 case NEON::BI__builtin_neon_vcaltd_f64:
4782 // Only one direction of comparisons actually exist, cmle is actually a cmge
4783 // with swapped operands. The table gives us the right intrinsic but we
4784 // still need to do the swap.
4785 std::swap(Ops[0], Ops[1]);
4786 break;
4787 }
4788
4789 assert(Int && "Generic code assumes a valid intrinsic")((Int && "Generic code assumes a valid intrinsic") ? static_cast
<void> (0) : __assert_fail ("Int && \"Generic code assumes a valid intrinsic\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 4789, __PRETTY_FUNCTION__))
;
4790
4791 // Determine the type(s) of this overloaded AArch64 intrinsic.
4792 const Expr *Arg = E->getArg(0);
4793 llvm::Type *ArgTy = CGF.ConvertType(Arg->getType());
4794 Function *F = CGF.LookupNeonLLVMIntrinsic(Int, Modifier, ArgTy, E);
4795
4796 int j = 0;
4797 ConstantInt *C0 = ConstantInt::get(CGF.SizeTy, 0);
4798 for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end();
4799 ai != ae; ++ai, ++j) {
4800 llvm::Type *ArgTy = ai->getType();
4801 if (Ops[j]->getType()->getPrimitiveSizeInBits() ==
4802 ArgTy->getPrimitiveSizeInBits())
4803 continue;
4804
4805 assert(ArgTy->isVectorTy() && !Ops[j]->getType()->isVectorTy())((ArgTy->isVectorTy() && !Ops[j]->getType()->
isVectorTy()) ? static_cast<void> (0) : __assert_fail (
"ArgTy->isVectorTy() && !Ops[j]->getType()->isVectorTy()"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 4805, __PRETTY_FUNCTION__))
;
4806 // The constant argument to an _n_ intrinsic always has Int32Ty, so truncate
4807 // it before inserting.
4808 Ops[j] =
4809 CGF.Builder.CreateTruncOrBitCast(Ops[j], ArgTy->getVectorElementType());
4810 Ops[j] =
4811 CGF.Builder.CreateInsertElement(UndefValue::get(ArgTy), Ops[j], C0);
4812 }
4813
4814 Value *Result = CGF.EmitNeonCall(F, Ops, s);
4815 llvm::Type *ResultType = CGF.ConvertType(E->getType());
4816 if (ResultType->getPrimitiveSizeInBits() <
4817 Result->getType()->getPrimitiveSizeInBits())
4818 return CGF.Builder.CreateExtractElement(Result, C0);
4819
4820 return CGF.Builder.CreateBitCast(Result, ResultType, s);
4821}
4822
4823Value *CodeGenFunction::EmitCommonNeonBuiltinExpr(
4824 unsigned BuiltinID, unsigned LLVMIntrinsic, unsigned AltLLVMIntrinsic,
4825 const char *NameHint, unsigned Modifier, const CallExpr *E,
4826 SmallVectorImpl<llvm::Value *> &Ops, Address PtrOp0, Address PtrOp1,
4827 llvm::Triple::ArchType Arch) {
4828 // Get the last argument, which specifies the vector type.
4829 llvm::APSInt NeonTypeConst;
4830 const Expr *Arg = E->getArg(E->getNumArgs() - 1);
4831 if (!Arg->isIntegerConstantExpr(NeonTypeConst, getContext()))
4832 return nullptr;
4833
4834 // Determine the type of this overloaded NEON intrinsic.
4835 NeonTypeFlags Type(NeonTypeConst.getZExtValue());
4836 bool Usgn = Type.isUnsigned();
4837 bool Quad = Type.isQuad();
4838 const bool HasLegalHalfType = getTarget().hasLegalHalfType();
4839
4840 llvm::VectorType *VTy = GetNeonType(this, Type, HasLegalHalfType);
4841 llvm::Type *Ty = VTy;
4842 if (!Ty)
4843 return nullptr;
4844
4845 auto getAlignmentValue32 = [&](Address addr) -> Value* {
4846 return Builder.getInt32(addr.getAlignment().getQuantity());
4847 };
4848
4849 unsigned Int = LLVMIntrinsic;
4850 if ((Modifier & UnsignedAlts) && !Usgn)
4851 Int = AltLLVMIntrinsic;
4852
4853 switch (BuiltinID) {
4854 default: break;
4855 case NEON::BI__builtin_neon_vabs_v:
4856 case NEON::BI__builtin_neon_vabsq_v:
4857 if (VTy->getElementType()->isFloatingPointTy())
4858 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::fabs, Ty), Ops, "vabs");
4859 return EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Ty), Ops, "vabs");
4860 case NEON::BI__builtin_neon_vaddhn_v: {
4861 llvm::VectorType *SrcTy =
4862 llvm::VectorType::getExtendedElementVectorType(VTy);
4863
4864 // %sum = add <4 x i32> %lhs, %rhs
4865 Ops[0] = Builder.CreateBitCast(Ops[0], SrcTy);
4866 Ops[1] = Builder.CreateBitCast(Ops[1], SrcTy);
4867 Ops[0] = Builder.CreateAdd(Ops[0], Ops[1], "vaddhn");
4868
4869 // %high = lshr <4 x i32> %sum, <i32 16, i32 16, i32 16, i32 16>
4870 Constant *ShiftAmt =
4871 ConstantInt::get(SrcTy, SrcTy->getScalarSizeInBits() / 2);
4872 Ops[0] = Builder.CreateLShr(Ops[0], ShiftAmt, "vaddhn");
4873
4874 // %res = trunc <4 x i32> %high to <4 x i16>
4875 return Builder.CreateTrunc(Ops[0], VTy, "vaddhn");
4876 }
4877 case NEON::BI__builtin_neon_vcale_v:
4878 case NEON::BI__builtin_neon_vcaleq_v:
4879 case NEON::BI__builtin_neon_vcalt_v:
4880 case NEON::BI__builtin_neon_vcaltq_v:
4881 std::swap(Ops[0], Ops[1]);
4882 LLVM_FALLTHROUGH[[clang::fallthrough]];
4883 case NEON::BI__builtin_neon_vcage_v:
4884 case NEON::BI__builtin_neon_vcageq_v:
4885 case NEON::BI__builtin_neon_vcagt_v:
4886 case NEON::BI__builtin_neon_vcagtq_v: {
4887 llvm::Type *Ty;
4888 switch (VTy->getScalarSizeInBits()) {
4889 default: llvm_unreachable("unexpected type")::llvm::llvm_unreachable_internal("unexpected type", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 4889)
;
4890 case 32:
4891 Ty = FloatTy;
4892 break;
4893 case 64:
4894 Ty = DoubleTy;
4895 break;
4896 case 16:
4897 Ty = HalfTy;
4898 break;
4899 }
4900 llvm::Type *VecFlt = llvm::VectorType::get(Ty, VTy->getNumElements());
4901 llvm::Type *Tys[] = { VTy, VecFlt };
4902 Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
4903 return EmitNeonCall(F, Ops, NameHint);
4904 }
4905 case NEON::BI__builtin_neon_vceqz_v:
4906 case NEON::BI__builtin_neon_vceqzq_v:
4907 return EmitAArch64CompareBuiltinExpr(Ops[0], Ty, ICmpInst::FCMP_OEQ,
4908 ICmpInst::ICMP_EQ, "vceqz");
4909 case NEON::BI__builtin_neon_vcgez_v:
4910 case NEON::BI__builtin_neon_vcgezq_v:
4911 return EmitAArch64CompareBuiltinExpr(Ops[0], Ty, ICmpInst::FCMP_OGE,
4912 ICmpInst::ICMP_SGE, "vcgez");
4913 case NEON::BI__builtin_neon_vclez_v:
4914 case NEON::BI__builtin_neon_vclezq_v:
4915 return EmitAArch64CompareBuiltinExpr(Ops[0], Ty, ICmpInst::FCMP_OLE,
4916 ICmpInst::ICMP_SLE, "vclez");
4917 case NEON::BI__builtin_neon_vcgtz_v:
4918 case NEON::BI__builtin_neon_vcgtzq_v:
4919 return EmitAArch64CompareBuiltinExpr(Ops[0], Ty, ICmpInst::FCMP_OGT,
4920 ICmpInst::ICMP_SGT, "vcgtz");
4921 case NEON::BI__builtin_neon_vcltz_v:
4922 case NEON::BI__builtin_neon_vcltzq_v:
4923 return EmitAArch64CompareBuiltinExpr(Ops[0], Ty, ICmpInst::FCMP_OLT,
4924 ICmpInst::ICMP_SLT, "vcltz");
4925 case NEON::BI__builtin_neon_vclz_v:
4926 case NEON::BI__builtin_neon_vclzq_v:
4927 // We generate target-independent intrinsic, which needs a second argument
4928 // for whether or not clz of zero is undefined; on ARM it isn't.
4929 Ops.push_back(Builder.getInt1(getTarget().isCLZForZeroUndef()));
4930 break;
4931 case NEON::BI__builtin_neon_vcvt_f32_v:
4932 case NEON::BI__builtin_neon_vcvtq_f32_v:
4933 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
4934 Ty = GetNeonType(this, NeonTypeFlags(NeonTypeFlags::Float32, false, Quad),
4935 HasLegalHalfType);
4936 return Usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt")
4937 : Builder.CreateSIToFP(Ops[0], Ty, "vcvt");
4938 case NEON::BI__builtin_neon_vcvt_f16_v:
4939 case NEON::BI__builtin_neon_vcvtq_f16_v:
4940 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
4941 Ty = GetNeonType(this, NeonTypeFlags(NeonTypeFlags::Float16, false, Quad),
4942 HasLegalHalfType);
4943 return Usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt")
4944 : Builder.CreateSIToFP(Ops[0], Ty, "vcvt");
4945 case NEON::BI__builtin_neon_vcvt_n_f16_v:
4946 case NEON::BI__builtin_neon_vcvt_n_f32_v:
4947 case NEON::BI__builtin_neon_vcvt_n_f64_v:
4948 case NEON::BI__builtin_neon_vcvtq_n_f16_v:
4949 case NEON::BI__builtin_neon_vcvtq_n_f32_v:
4950 case NEON::BI__builtin_neon_vcvtq_n_f64_v: {
4951 llvm::Type *Tys[2] = { GetFloatNeonType(this, Type), Ty };
4952 Int = Usgn ? LLVMIntrinsic : AltLLVMIntrinsic;
4953 Function *F = CGM.getIntrinsic(Int, Tys);
4954 return EmitNeonCall(F, Ops, "vcvt_n");
4955 }
4956 case NEON::BI__builtin_neon_vcvt_n_s16_v:
4957 case NEON::BI__builtin_neon_vcvt_n_s32_v:
4958 case NEON::BI__builtin_neon_vcvt_n_u16_v:
4959 case NEON::BI__builtin_neon_vcvt_n_u32_v:
4960 case NEON::BI__builtin_neon_vcvt_n_s64_v:
4961 case NEON::BI__builtin_neon_vcvt_n_u64_v:
4962 case NEON::BI__builtin_neon_vcvtq_n_s16_v:
4963 case NEON::BI__builtin_neon_vcvtq_n_s32_v:
4964 case NEON::BI__builtin_neon_vcvtq_n_u16_v:
4965 case NEON::BI__builtin_neon_vcvtq_n_u32_v:
4966 case NEON::BI__builtin_neon_vcvtq_n_s64_v:
4967 case NEON::BI__builtin_neon_vcvtq_n_u64_v: {
4968 llvm::Type *Tys[2] = { Ty, GetFloatNeonType(this, Type) };
4969 Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
4970 return EmitNeonCall(F, Ops, "vcvt_n");
4971 }
4972 case NEON::BI__builtin_neon_vcvt_s32_v:
4973 case NEON::BI__builtin_neon_vcvt_u32_v:
4974 case NEON::BI__builtin_neon_vcvt_s64_v:
4975 case NEON::BI__builtin_neon_vcvt_u64_v:
4976 case NEON::BI__builtin_neon_vcvt_s16_v:
4977 case NEON::BI__builtin_neon_vcvt_u16_v:
4978 case NEON::BI__builtin_neon_vcvtq_s32_v:
4979 case NEON::BI__builtin_neon_vcvtq_u32_v:
4980 case NEON::BI__builtin_neon_vcvtq_s64_v:
4981 case NEON::BI__builtin_neon_vcvtq_u64_v:
4982 case NEON::BI__builtin_neon_vcvtq_s16_v:
4983 case NEON::BI__builtin_neon_vcvtq_u16_v: {
4984 Ops[0] = Builder.CreateBitCast(Ops[0], GetFloatNeonType(this, Type));
4985 return Usgn ? Builder.CreateFPToUI(Ops[0], Ty, "vcvt")
4986 : Builder.CreateFPToSI(Ops[0], Ty, "vcvt");
4987 }
4988 case NEON::BI__builtin_neon_vcvta_s16_v:
4989 case NEON::BI__builtin_neon_vcvta_s32_v:
4990 case NEON::BI__builtin_neon_vcvta_s64_v:
4991 case NEON::BI__builtin_neon_vcvta_u16_v:
4992 case NEON::BI__builtin_neon_vcvta_u32_v:
4993 case NEON::BI__builtin_neon_vcvta_u64_v:
4994 case NEON::BI__builtin_neon_vcvtaq_s16_v:
4995 case NEON::BI__builtin_neon_vcvtaq_s32_v:
4996 case NEON::BI__builtin_neon_vcvtaq_s64_v:
4997 case NEON::BI__builtin_neon_vcvtaq_u16_v:
4998 case NEON::BI__builtin_neon_vcvtaq_u32_v:
4999 case NEON::BI__builtin_neon_vcvtaq_u64_v:
5000 case NEON::BI__builtin_neon_vcvtn_s16_v:
5001 case NEON::BI__builtin_neon_vcvtn_s32_v:
5002 case NEON::BI__builtin_neon_vcvtn_s64_v:
5003 case NEON::BI__builtin_neon_vcvtn_u16_v:
5004 case NEON::BI__builtin_neon_vcvtn_u32_v:
5005 case NEON::BI__builtin_neon_vcvtn_u64_v:
5006 case NEON::BI__builtin_neon_vcvtnq_s16_v:
5007 case NEON::BI__builtin_neon_vcvtnq_s32_v:
5008 case NEON::BI__builtin_neon_vcvtnq_s64_v:
5009 case NEON::BI__builtin_neon_vcvtnq_u16_v:
5010 case NEON::BI__builtin_neon_vcvtnq_u32_v:
5011 case NEON::BI__builtin_neon_vcvtnq_u64_v:
5012 case NEON::BI__builtin_neon_vcvtp_s16_v:
5013 case NEON::BI__builtin_neon_vcvtp_s32_v:
5014 case NEON::BI__builtin_neon_vcvtp_s64_v:
5015 case NEON::BI__builtin_neon_vcvtp_u16_v:
5016 case NEON::BI__builtin_neon_vcvtp_u32_v:
5017 case NEON::BI__builtin_neon_vcvtp_u64_v:
5018 case NEON::BI__builtin_neon_vcvtpq_s16_v:
5019 case NEON::BI__builtin_neon_vcvtpq_s32_v:
5020 case NEON::BI__builtin_neon_vcvtpq_s64_v:
5021 case NEON::BI__builtin_neon_vcvtpq_u16_v:
5022 case NEON::BI__builtin_neon_vcvtpq_u32_v:
5023 case NEON::BI__builtin_neon_vcvtpq_u64_v:
5024 case NEON::BI__builtin_neon_vcvtm_s16_v:
5025 case NEON::BI__builtin_neon_vcvtm_s32_v:
5026 case NEON::BI__builtin_neon_vcvtm_s64_v:
5027 case NEON::BI__builtin_neon_vcvtm_u16_v:
5028 case NEON::BI__builtin_neon_vcvtm_u32_v:
5029 case NEON::BI__builtin_neon_vcvtm_u64_v:
5030 case NEON::BI__builtin_neon_vcvtmq_s16_v:
5031 case NEON::BI__builtin_neon_vcvtmq_s32_v:
5032 case NEON::BI__builtin_neon_vcvtmq_s64_v:
5033 case NEON::BI__builtin_neon_vcvtmq_u16_v:
5034 case NEON::BI__builtin_neon_vcvtmq_u32_v:
5035 case NEON::BI__builtin_neon_vcvtmq_u64_v: {
5036 llvm::Type *Tys[2] = { Ty, GetFloatNeonType(this, Type) };
5037 return EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Tys), Ops, NameHint);
5038 }
5039 case NEON::BI__builtin_neon_vext_v:
5040 case NEON::BI__builtin_neon_vextq_v: {
5041 int CV = cast<ConstantInt>(Ops[2])->getSExtValue();
5042 SmallVector<uint32_t, 16> Indices;
5043 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
5044 Indices.push_back(i+CV);
5045
5046 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
5047 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
5048 return Builder.CreateShuffleVector(Ops[0], Ops[1], Indices, "vext");
5049 }
5050 case NEON::BI__builtin_neon_vfma_v:
5051 case NEON::BI__builtin_neon_vfmaq_v: {
5052 Value *F = CGM.getIntrinsic(Intrinsic::fma, Ty);
5053 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
5054 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
5055 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
5056
5057 // NEON intrinsic puts accumulator first, unlike the LLVM fma.
5058 return Builder.CreateCall(F, {Ops[1], Ops[2], Ops[0]});
5059 }
5060 case NEON::BI__builtin_neon_vld1_v:
5061 case NEON::BI__builtin_neon_vld1q_v: {
5062 llvm::Type *Tys[] = {Ty, Int8PtrTy};
5063 Ops.push_back(getAlignmentValue32(PtrOp0));
5064 return EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Tys), Ops, "vld1");
5065 }
5066 case NEON::BI__builtin_neon_vld1_x2_v:
5067 case NEON::BI__builtin_neon_vld1q_x2_v:
5068 case NEON::BI__builtin_neon_vld1_x3_v:
5069 case NEON::BI__builtin_neon_vld1q_x3_v:
5070 case NEON::BI__builtin_neon_vld1_x4_v:
5071 case NEON::BI__builtin_neon_vld1q_x4_v: {
5072 llvm::Type *PTy = llvm::PointerType::getUnqual(VTy->getVectorElementType());
5073 Ops[1] = Builder.CreateBitCast(Ops[1], PTy);
5074 llvm::Type *Tys[2] = { VTy, PTy };
5075 Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
5076 Ops[1] = Builder.CreateCall(F, Ops[1], "vld1xN");
5077 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
5078 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
5079 return Builder.CreateDefaultAlignedStore(Ops[1], Ops[0]);
5080 }
5081 case NEON::BI__builtin_neon_vld2_v:
5082 case NEON::BI__builtin_neon_vld2q_v:
5083 case NEON::BI__builtin_neon_vld3_v:
5084 case NEON::BI__builtin_neon_vld3q_v:
5085 case NEON::BI__builtin_neon_vld4_v:
5086 case NEON::BI__builtin_neon_vld4q_v:
5087 case NEON::BI__builtin_neon_vld2_dup_v:
5088 case NEON::BI__builtin_neon_vld2q_dup_v:
5089 case NEON::BI__builtin_neon_vld3_dup_v:
5090 case NEON::BI__builtin_neon_vld3q_dup_v:
5091 case NEON::BI__builtin_neon_vld4_dup_v:
5092 case NEON::BI__builtin_neon_vld4q_dup_v: {
5093 llvm::Type *Tys[] = {Ty, Int8PtrTy};
5094 Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
5095 Value *Align = getAlignmentValue32(PtrOp1);
5096 Ops[1] = Builder.CreateCall(F, {Ops[1], Align}, NameHint);
5097 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
5098 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
5099 return Builder.CreateDefaultAlignedStore(Ops[1], Ops[0]);
5100 }
5101 case NEON::BI__builtin_neon_vld1_dup_v:
5102 case NEON::BI__builtin_neon_vld1q_dup_v: {
5103 Value *V = UndefValue::get(Ty);
5104 Ty = llvm::PointerType::getUnqual(VTy->getElementType());
5105 PtrOp0 = Builder.CreateBitCast(PtrOp0, Ty);
5106 LoadInst *Ld = Builder.CreateLoad(PtrOp0);
5107 llvm::Constant *CI = ConstantInt::get(SizeTy, 0);
5108 Ops[0] = Builder.CreateInsertElement(V, Ld, CI);
5109 return EmitNeonSplat(Ops[0], CI);
5110 }
5111 case NEON::BI__builtin_neon_vld2_lane_v:
5112 case NEON::BI__builtin_neon_vld2q_lane_v:
5113 case NEON::BI__builtin_neon_vld3_lane_v:
5114 case NEON::BI__builtin_neon_vld3q_lane_v:
5115 case NEON::BI__builtin_neon_vld4_lane_v:
5116 case NEON::BI__builtin_neon_vld4q_lane_v: {
5117 llvm::Type *Tys[] = {Ty, Int8PtrTy};
5118 Function *F = CGM.getIntrinsic(LLVMIntrinsic, Tys);
5119 for (unsigned I = 2; I < Ops.size() - 1; ++I)
5120 Ops[I] = Builder.CreateBitCast(Ops[I], Ty);
5121 Ops.push_back(getAlignmentValue32(PtrOp1));
5122 Ops[1] = Builder.CreateCall(F, makeArrayRef(Ops).slice(1), NameHint);
5123 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
5124 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
5125 return Builder.CreateDefaultAlignedStore(Ops[1], Ops[0]);
5126 }
5127 case NEON::BI__builtin_neon_vmovl_v: {
5128 llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
5129 Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
5130 if (Usgn)
5131 return Builder.CreateZExt(Ops[0], Ty, "vmovl");
5132 return Builder.CreateSExt(Ops[0], Ty, "vmovl");
5133 }
5134 case NEON::BI__builtin_neon_vmovn_v: {
5135 llvm::Type *QTy = llvm::VectorType::getExtendedElementVectorType(VTy);
5136 Ops[0] = Builder.CreateBitCast(Ops[0], QTy);
5137 return Builder.CreateTrunc(Ops[0], Ty, "vmovn");
5138 }
5139 case NEON::BI__builtin_neon_vmull_v:
5140 // FIXME: the integer vmull operations could be emitted in terms of pure
5141 // LLVM IR (2 exts followed by a mul). Unfortunately LLVM has a habit of
5142 // hoisting the exts outside loops. Until global ISel comes along that can
5143 // see through such movement this leads to bad CodeGen. So we need an
5144 // intrinsic for now.
5145 Int = Usgn ? Intrinsic::arm_neon_vmullu : Intrinsic::arm_neon_vmulls;
5146 Int = Type.isPoly() ? (unsigned)Intrinsic::arm_neon_vmullp : Int;
5147 return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vmull");
5148 case NEON::BI__builtin_neon_vpadal_v:
5149 case NEON::BI__builtin_neon_vpadalq_v: {
5150 // The source operand type has twice as many elements of half the size.
5151 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
5152 llvm::Type *EltTy =
5153 llvm::IntegerType::get(getLLVMContext(), EltBits / 2);
5154 llvm::Type *NarrowTy =
5155 llvm::VectorType::get(EltTy, VTy->getNumElements() * 2);
5156 llvm::Type *Tys[2] = { Ty, NarrowTy };
5157 return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, NameHint);
5158 }
5159 case NEON::BI__builtin_neon_vpaddl_v:
5160 case NEON::BI__builtin_neon_vpaddlq_v: {
5161 // The source operand type has twice as many elements of half the size.
5162 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
5163 llvm::Type *EltTy = llvm::IntegerType::get(getLLVMContext(), EltBits / 2);
5164 llvm::Type *NarrowTy =
5165 llvm::VectorType::get(EltTy, VTy->getNumElements() * 2);
5166 llvm::Type *Tys[2] = { Ty, NarrowTy };
5167 return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, "vpaddl");
5168 }
5169 case NEON::BI__builtin_neon_vqdmlal_v:
5170 case NEON::BI__builtin_neon_vqdmlsl_v: {
5171 SmallVector<Value *, 2> MulOps(Ops.begin() + 1, Ops.end());
5172 Ops[1] =
5173 EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Ty), MulOps, "vqdmlal");
5174 Ops.resize(2);
5175 return EmitNeonCall(CGM.getIntrinsic(AltLLVMIntrinsic, Ty), Ops, NameHint);
5176 }
5177 case NEON::BI__builtin_neon_vqshl_n_v:
5178 case NEON::BI__builtin_neon_vqshlq_n_v:
5179 return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vqshl_n",
5180 1, false);
5181 case NEON::BI__builtin_neon_vqshlu_n_v:
5182 case NEON::BI__builtin_neon_vqshluq_n_v:
5183 return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vqshlu_n",
5184 1, false);
5185 case NEON::BI__builtin_neon_vrecpe_v:
5186 case NEON::BI__builtin_neon_vrecpeq_v:
5187 case NEON::BI__builtin_neon_vrsqrte_v:
5188 case NEON::BI__builtin_neon_vrsqrteq_v:
5189 Int = Ty->isFPOrFPVectorTy() ? LLVMIntrinsic : AltLLVMIntrinsic;
5190 return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, NameHint);
5191 case NEON::BI__builtin_neon_vrndi_v:
5192 case NEON::BI__builtin_neon_vrndiq_v:
5193 Int = Intrinsic::nearbyint;
5194 return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, NameHint);
5195 case NEON::BI__builtin_neon_vrshr_n_v:
5196 case NEON::BI__builtin_neon_vrshrq_n_v:
5197 return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vrshr_n",
5198 1, true);
5199 case NEON::BI__builtin_neon_vshl_n_v:
5200 case NEON::BI__builtin_neon_vshlq_n_v:
5201 Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false);
5202 return Builder.CreateShl(Builder.CreateBitCast(Ops[0],Ty), Ops[1],
5203 "vshl_n");
5204 case NEON::BI__builtin_neon_vshll_n_v: {
5205 llvm::Type *SrcTy = llvm::VectorType::getTruncatedElementVectorType(VTy);
5206 Ops[0] = Builder.CreateBitCast(Ops[0], SrcTy);
5207 if (Usgn)
5208 Ops[0] = Builder.CreateZExt(Ops[0], VTy);
5209 else
5210 Ops[0] = Builder.CreateSExt(Ops[0], VTy);
5211 Ops[1] = EmitNeonShiftVector(Ops[1], VTy, false);
5212 return Builder.CreateShl(Ops[0], Ops[1], "vshll_n");
5213 }
5214 case NEON::BI__builtin_neon_vshrn_n_v: {
5215 llvm::Type *SrcTy = llvm::VectorType::getExtendedElementVectorType(VTy);
5216 Ops[0] = Builder.CreateBitCast(Ops[0], SrcTy);
5217 Ops[1] = EmitNeonShiftVector(Ops[1], SrcTy, false);
5218 if (Usgn)
5219 Ops[0] = Builder.CreateLShr(Ops[0], Ops[1]);
5220 else
5221 Ops[0] = Builder.CreateAShr(Ops[0], Ops[1]);
5222 return Builder.CreateTrunc(Ops[0], Ty, "vshrn_n");
5223 }
5224 case NEON::BI__builtin_neon_vshr_n_v:
5225 case NEON::BI__builtin_neon_vshrq_n_v:
5226 return EmitNeonRShiftImm(Ops[0], Ops[1], Ty, Usgn, "vshr_n");
5227 case NEON::BI__builtin_neon_vst1_v:
5228 case NEON::BI__builtin_neon_vst1q_v:
5229 case NEON::BI__builtin_neon_vst2_v:
5230 case NEON::BI__builtin_neon_vst2q_v:
5231 case NEON::BI__builtin_neon_vst3_v:
5232 case NEON::BI__builtin_neon_vst3q_v:
5233 case NEON::BI__builtin_neon_vst4_v:
5234 case NEON::BI__builtin_neon_vst4q_v:
5235 case NEON::BI__builtin_neon_vst2_lane_v:
5236 case NEON::BI__builtin_neon_vst2q_lane_v:
5237 case NEON::BI__builtin_neon_vst3_lane_v:
5238 case NEON::BI__builtin_neon_vst3q_lane_v:
5239 case NEON::BI__builtin_neon_vst4_lane_v:
5240 case NEON::BI__builtin_neon_vst4q_lane_v: {
5241 llvm::Type *Tys[] = {Int8PtrTy, Ty};
5242 Ops.push_back(getAlignmentValue32(PtrOp0));
5243 return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, "");
5244 }
5245 case NEON::BI__builtin_neon_vst1_x2_v:
5246 case NEON::BI__builtin_neon_vst1q_x2_v:
5247 case NEON::BI__builtin_neon_vst1_x3_v:
5248 case NEON::BI__builtin_neon_vst1q_x3_v:
5249 case NEON::BI__builtin_neon_vst1_x4_v:
5250 case NEON::BI__builtin_neon_vst1q_x4_v: {
5251 llvm::Type *PTy = llvm::PointerType::getUnqual(VTy->getVectorElementType());
5252 // TODO: Currently in AArch32 mode the pointer operand comes first, whereas
5253 // in AArch64 it comes last. We may want to stick to one or another.
5254 if (Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::aarch64_be) {
5255 llvm::Type *Tys[2] = { VTy, PTy };
5256 std::rotate(Ops.begin(), Ops.begin() + 1, Ops.end());
5257 return EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Tys), Ops, "");
5258 }
5259 llvm::Type *Tys[2] = { PTy, VTy };
5260 return EmitNeonCall(CGM.getIntrinsic(LLVMIntrinsic, Tys), Ops, "");
5261 }
5262 case NEON::BI__builtin_neon_vsubhn_v: {
5263 llvm::VectorType *SrcTy =
5264 llvm::VectorType::getExtendedElementVectorType(VTy);
5265
5266 // %sum = add <4 x i32> %lhs, %rhs
5267 Ops[0] = Builder.CreateBitCast(Ops[0], SrcTy);
5268 Ops[1] = Builder.CreateBitCast(Ops[1], SrcTy);
5269 Ops[0] = Builder.CreateSub(Ops[0], Ops[1], "vsubhn");
5270
5271 // %high = lshr <4 x i32> %sum, <i32 16, i32 16, i32 16, i32 16>
5272 Constant *ShiftAmt =
5273 ConstantInt::get(SrcTy, SrcTy->getScalarSizeInBits() / 2);
5274 Ops[0] = Builder.CreateLShr(Ops[0], ShiftAmt, "vsubhn");
5275
5276 // %res = trunc <4 x i32> %high to <4 x i16>
5277 return Builder.CreateTrunc(Ops[0], VTy, "vsubhn");
5278 }
5279 case NEON::BI__builtin_neon_vtrn_v:
5280 case NEON::BI__builtin_neon_vtrnq_v: {
5281 Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
5282 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
5283 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
5284 Value *SV = nullptr;
5285
5286 for (unsigned vi = 0; vi != 2; ++vi) {
5287 SmallVector<uint32_t, 16> Indices;
5288 for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) {
5289 Indices.push_back(i+vi);
5290 Indices.push_back(i+e+vi);
5291 }
5292 Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ty, Ops[0], vi);
5293 SV = Builder.CreateShuffleVector(Ops[1], Ops[2], Indices, "vtrn");
5294 SV = Builder.CreateDefaultAlignedStore(SV, Addr);
5295 }
5296 return SV;
5297 }
5298 case NEON::BI__builtin_neon_vtst_v:
5299 case NEON::BI__builtin_neon_vtstq_v: {
5300 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
5301 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
5302 Ops[0] = Builder.CreateAnd(Ops[0], Ops[1]);
5303 Ops[0] = Builder.CreateICmp(ICmpInst::ICMP_NE, Ops[0],
5304 ConstantAggregateZero::get(Ty));
5305 return Builder.CreateSExt(Ops[0], Ty, "vtst");
5306 }
5307 case NEON::BI__builtin_neon_vuzp_v:
5308 case NEON::BI__builtin_neon_vuzpq_v: {
5309 Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
5310 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
5311 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
5312 Value *SV = nullptr;
5313
5314 for (unsigned vi = 0; vi != 2; ++vi) {
5315 SmallVector<uint32_t, 16> Indices;
5316 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
5317 Indices.push_back(2*i+vi);
5318
5319 Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ty, Ops[0], vi);
5320 SV = Builder.CreateShuffleVector(Ops[1], Ops[2], Indices, "vuzp");
5321 SV = Builder.CreateDefaultAlignedStore(SV, Addr);
5322 }
5323 return SV;
5324 }
5325 case NEON::BI__builtin_neon_vzip_v:
5326 case NEON::BI__builtin_neon_vzipq_v: {
5327 Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
5328 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
5329 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
5330 Value *SV = nullptr;
5331
5332 for (unsigned vi = 0; vi != 2; ++vi) {
5333 SmallVector<uint32_t, 16> Indices;
5334 for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) {
5335 Indices.push_back((i + vi*e) >> 1);
5336 Indices.push_back(((i + vi*e) >> 1)+e);
5337 }
5338 Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ty, Ops[0], vi);
5339 SV = Builder.CreateShuffleVector(Ops[1], Ops[2], Indices, "vzip");
5340 SV = Builder.CreateDefaultAlignedStore(SV, Addr);
5341 }
5342 return SV;
5343 }
5344 case NEON::BI__builtin_neon_vdot_v:
5345 case NEON::BI__builtin_neon_vdotq_v: {
5346 llvm::Type *InputTy =
5347 llvm::VectorType::get(Int8Ty, Ty->getPrimitiveSizeInBits() / 8);
5348 llvm::Type *Tys[2] = { Ty, InputTy };
5349 Int = Usgn ? LLVMIntrinsic : AltLLVMIntrinsic;
5350 return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, "vdot");
5351 }
5352 case NEON::BI__builtin_neon_vfmlal_low_v:
5353 case NEON::BI__builtin_neon_vfmlalq_low_v: {
5354 llvm::Type *InputTy =
5355 llvm::VectorType::get(HalfTy, Ty->getPrimitiveSizeInBits() / 16);
5356 llvm::Type *Tys[2] = { Ty, InputTy };
5357 return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, "vfmlal_low");
5358 }
5359 case NEON::BI__builtin_neon_vfmlsl_low_v:
5360 case NEON::BI__builtin_neon_vfmlslq_low_v: {
5361 llvm::Type *InputTy =
5362 llvm::VectorType::get(HalfTy, Ty->getPrimitiveSizeInBits() / 16);
5363 llvm::Type *Tys[2] = { Ty, InputTy };
5364 return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, "vfmlsl_low");
5365 }
5366 case NEON::BI__builtin_neon_vfmlal_high_v:
5367 case NEON::BI__builtin_neon_vfmlalq_high_v: {
5368 llvm::Type *InputTy =
5369 llvm::VectorType::get(HalfTy, Ty->getPrimitiveSizeInBits() / 16);
5370 llvm::Type *Tys[2] = { Ty, InputTy };
5371 return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, "vfmlal_high");
5372 }
5373 case NEON::BI__builtin_neon_vfmlsl_high_v:
5374 case NEON::BI__builtin_neon_vfmlslq_high_v: {
5375 llvm::Type *InputTy =
5376 llvm::VectorType::get(HalfTy, Ty->getPrimitiveSizeInBits() / 16);
5377 llvm::Type *Tys[2] = { Ty, InputTy };
5378 return EmitNeonCall(CGM.getIntrinsic(Int, Tys), Ops, "vfmlsl_high");
5379 }
5380 }
5381
5382 assert(Int && "Expected valid intrinsic number")((Int && "Expected valid intrinsic number") ? static_cast
<void> (0) : __assert_fail ("Int && \"Expected valid intrinsic number\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 5382, __PRETTY_FUNCTION__))
;
5383
5384 // Determine the type(s) of this overloaded AArch64 intrinsic.
5385 Function *F = LookupNeonLLVMIntrinsic(Int, Modifier, Ty, E);
5386
5387 Value *Result = EmitNeonCall(F, Ops, NameHint);
5388 llvm::Type *ResultType = ConvertType(E->getType());
5389 // AArch64 intrinsic one-element vector type cast to
5390 // scalar type expected by the builtin
5391 return Builder.CreateBitCast(Result, ResultType, NameHint);
5392}
5393
5394Value *CodeGenFunction::EmitAArch64CompareBuiltinExpr(
5395 Value *Op, llvm::Type *Ty, const CmpInst::Predicate Fp,
5396 const CmpInst::Predicate Ip, const Twine &Name) {
5397 llvm::Type *OTy = Op->getType();
5398
5399 // FIXME: this is utterly horrific. We should not be looking at previous
5400 // codegen context to find out what needs doing. Unfortunately TableGen
5401 // currently gives us exactly the same calls for vceqz_f32 and vceqz_s32
5402 // (etc).
5403 if (BitCastInst *BI = dyn_cast<BitCastInst>(Op))
5404 OTy = BI->getOperand(0)->getType();
5405
5406 Op = Builder.CreateBitCast(Op, OTy);
5407 if (OTy->getScalarType()->isFloatingPointTy()) {
5408 Op = Builder.CreateFCmp(Fp, Op, Constant::getNullValue(OTy));
5409 } else {
5410 Op = Builder.CreateICmp(Ip, Op, Constant::getNullValue(OTy));
5411 }
5412 return Builder.CreateSExt(Op, Ty, Name);
5413}
5414
5415static Value *packTBLDVectorList(CodeGenFunction &CGF, ArrayRef<Value *> Ops,
5416 Value *ExtOp, Value *IndexOp,
5417 llvm::Type *ResTy, unsigned IntID,
5418 const char *Name) {
5419 SmallVector<Value *, 2> TblOps;
5420 if (ExtOp)
5421 TblOps.push_back(ExtOp);
5422
5423 // Build a vector containing sequential number like (0, 1, 2, ..., 15)
5424 SmallVector<uint32_t, 16> Indices;
5425 llvm::VectorType *TblTy = cast<llvm::VectorType>(Ops[0]->getType());
5426 for (unsigned i = 0, e = TblTy->getNumElements(); i != e; ++i) {
5427 Indices.push_back(2*i);
5428 Indices.push_back(2*i+1);
5429 }
5430
5431 int PairPos = 0, End = Ops.size() - 1;
5432 while (PairPos < End) {
5433 TblOps.push_back(CGF.Builder.CreateShuffleVector(Ops[PairPos],
5434 Ops[PairPos+1], Indices,
5435 Name));
5436 PairPos += 2;
5437 }
5438
5439 // If there's an odd number of 64-bit lookup table, fill the high 64-bit
5440 // of the 128-bit lookup table with zero.
5441 if (PairPos == End) {
5442 Value *ZeroTbl = ConstantAggregateZero::get(TblTy);
5443 TblOps.push_back(CGF.Builder.CreateShuffleVector(Ops[PairPos],
5444 ZeroTbl, Indices, Name));
5445 }
5446
5447 Function *TblF;
5448 TblOps.push_back(IndexOp);
5449 TblF = CGF.CGM.getIntrinsic(IntID, ResTy);
5450
5451 return CGF.EmitNeonCall(TblF, TblOps, Name);
5452}
5453
5454Value *CodeGenFunction::GetValueForARMHint(unsigned BuiltinID) {
5455 unsigned Value;
5456 switch (BuiltinID) {
5457 default:
5458 return nullptr;
5459 case ARM::BI__builtin_arm_nop:
5460 Value = 0;
5461 break;
5462 case ARM::BI__builtin_arm_yield:
5463 case ARM::BI__yield:
5464 Value = 1;
5465 break;
5466 case ARM::BI__builtin_arm_wfe:
5467 case ARM::BI__wfe:
5468 Value = 2;
5469 break;
5470 case ARM::BI__builtin_arm_wfi:
5471 case ARM::BI__wfi:
5472 Value = 3;
5473 break;
5474 case ARM::BI__builtin_arm_sev:
5475 case ARM::BI__sev:
5476 Value = 4;
5477 break;
5478 case ARM::BI__builtin_arm_sevl:
5479 case ARM::BI__sevl:
5480 Value = 5;
5481 break;
5482 }
5483
5484 return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::arm_hint),
5485 llvm::ConstantInt::get(Int32Ty, Value));
5486}
5487
5488// Generates the IR for the read/write special register builtin,
5489// ValueType is the type of the value that is to be written or read,
5490// RegisterType is the type of the register being written to or read from.
5491static Value *EmitSpecialRegisterBuiltin(CodeGenFunction &CGF,
5492 const CallExpr *E,
5493 llvm::Type *RegisterType,
5494 llvm::Type *ValueType,
5495 bool IsRead,
5496 StringRef SysReg = "") {
5497 // write and register intrinsics only support 32 and 64 bit operations.
5498 assert((RegisterType->isIntegerTy(32) || RegisterType->isIntegerTy(64))(((RegisterType->isIntegerTy(32) || RegisterType->isIntegerTy
(64)) && "Unsupported size for register.") ? static_cast
<void> (0) : __assert_fail ("(RegisterType->isIntegerTy(32) || RegisterType->isIntegerTy(64)) && \"Unsupported size for register.\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 5499, __PRETTY_FUNCTION__))
5499 && "Unsupported size for register.")(((RegisterType->isIntegerTy(32) || RegisterType->isIntegerTy
(64)) && "Unsupported size for register.") ? static_cast
<void> (0) : __assert_fail ("(RegisterType->isIntegerTy(32) || RegisterType->isIntegerTy(64)) && \"Unsupported size for register.\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 5499, __PRETTY_FUNCTION__))
;
5500
5501 CodeGen::CGBuilderTy &Builder = CGF.Builder;
5502 CodeGen::CodeGenModule &CGM = CGF.CGM;
5503 LLVMContext &Context = CGM.getLLVMContext();
5504
5505 if (SysReg.empty()) {
5506 const Expr *SysRegStrExpr = E->getArg(0)->IgnoreParenCasts();
5507 SysReg = cast<clang::StringLiteral>(SysRegStrExpr)->getString();
5508 }
5509
5510 llvm::Metadata *Ops[] = { llvm::MDString::get(Context, SysReg) };
5511 llvm::MDNode *RegName = llvm::MDNode::get(Context, Ops);
5512 llvm::Value *Metadata = llvm::MetadataAsValue::get(Context, RegName);
5513
5514 llvm::Type *Types[] = { RegisterType };
5515
5516 bool MixedTypes = RegisterType->isIntegerTy(64) && ValueType->isIntegerTy(32);
5517 assert(!(RegisterType->isIntegerTy(32) && ValueType->isIntegerTy(64))((!(RegisterType->isIntegerTy(32) && ValueType->
isIntegerTy(64)) && "Can't fit 64-bit value in 32-bit register"
) ? static_cast<void> (0) : __assert_fail ("!(RegisterType->isIntegerTy(32) && ValueType->isIntegerTy(64)) && \"Can't fit 64-bit value in 32-bit register\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 5518, __PRETTY_FUNCTION__))
5518 && "Can't fit 64-bit value in 32-bit register")((!(RegisterType->isIntegerTy(32) && ValueType->
isIntegerTy(64)) && "Can't fit 64-bit value in 32-bit register"
) ? static_cast<void> (0) : __assert_fail ("!(RegisterType->isIntegerTy(32) && ValueType->isIntegerTy(64)) && \"Can't fit 64-bit value in 32-bit register\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 5518, __PRETTY_FUNCTION__))
;
5519
5520 if (IsRead) {
5521 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::read_register, Types);
5522 llvm::Value *Call = Builder.CreateCall(F, Metadata);
5523
5524 if (MixedTypes)
5525 // Read into 64 bit register and then truncate result to 32 bit.
5526 return Builder.CreateTrunc(Call, ValueType);
5527
5528 if (ValueType->isPointerTy())
5529 // Have i32/i64 result (Call) but want to return a VoidPtrTy (i8*).
5530 return Builder.CreateIntToPtr(Call, ValueType);
5531
5532 return Call;
5533 }
5534
5535 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::write_register, Types);
5536 llvm::Value *ArgValue = CGF.EmitScalarExpr(E->getArg(1));
5537 if (MixedTypes) {
5538 // Extend 32 bit write value to 64 bit to pass to write.
5539 ArgValue = Builder.CreateZExt(ArgValue, RegisterType);
5540 return Builder.CreateCall(F, { Metadata, ArgValue });
5541 }
5542
5543 if (ValueType->isPointerTy()) {
5544 // Have VoidPtrTy ArgValue but want to return an i32/i64.
5545 ArgValue = Builder.CreatePtrToInt(ArgValue, RegisterType);
5546 return Builder.CreateCall(F, { Metadata, ArgValue });
5547 }
5548
5549 return Builder.CreateCall(F, { Metadata, ArgValue });
5550}
5551
5552/// Return true if BuiltinID is an overloaded Neon intrinsic with an extra
5553/// argument that specifies the vector type.
5554static bool HasExtraNeonArgument(unsigned BuiltinID) {
5555 switch (BuiltinID) {
5556 default: break;
5557 case NEON::BI__builtin_neon_vget_lane_i8:
5558 case NEON::BI__builtin_neon_vget_lane_i16:
5559 case NEON::BI__builtin_neon_vget_lane_i32:
5560 case NEON::BI__builtin_neon_vget_lane_i64:
5561 case NEON::BI__builtin_neon_vget_lane_f32:
5562 case NEON::BI__builtin_neon_vgetq_lane_i8:
5563 case NEON::BI__builtin_neon_vgetq_lane_i16:
5564 case NEON::BI__builtin_neon_vgetq_lane_i32:
5565 case NEON::BI__builtin_neon_vgetq_lane_i64:
5566 case NEON::BI__builtin_neon_vgetq_lane_f32:
5567 case NEON::BI__builtin_neon_vset_lane_i8:
5568 case NEON::BI__builtin_neon_vset_lane_i16:
5569 case NEON::BI__builtin_neon_vset_lane_i32:
5570 case NEON::BI__builtin_neon_vset_lane_i64:
5571 case NEON::BI__builtin_neon_vset_lane_f32:
5572 case NEON::BI__builtin_neon_vsetq_lane_i8:
5573 case NEON::BI__builtin_neon_vsetq_lane_i16:
5574 case NEON::BI__builtin_neon_vsetq_lane_i32:
5575 case NEON::BI__builtin_neon_vsetq_lane_i64:
5576 case NEON::BI__builtin_neon_vsetq_lane_f32:
5577 case NEON::BI__builtin_neon_vsha1h_u32:
5578 case NEON::BI__builtin_neon_vsha1cq_u32:
5579 case NEON::BI__builtin_neon_vsha1pq_u32:
5580 case NEON::BI__builtin_neon_vsha1mq_u32:
5581 case clang::ARM::BI_MoveToCoprocessor:
5582 case clang::ARM::BI_MoveToCoprocessor2:
5583 return false;
5584 }
5585 return true;
5586}
5587
5588Value *CodeGenFunction::EmitISOVolatileLoad(const CallExpr *E) {
5589 Value *Ptr = EmitScalarExpr(E->getArg(0));
5590 QualType ElTy = E->getArg(0)->getType()->getPointeeType();
5591 CharUnits LoadSize = getContext().getTypeSizeInChars(ElTy);
5592 llvm::Type *ITy = llvm::IntegerType::get(getLLVMContext(),
5593 LoadSize.getQuantity() * 8);
5594 Ptr = Builder.CreateBitCast(Ptr, ITy->getPointerTo());
5595 llvm::LoadInst *Load =
5596 Builder.CreateAlignedLoad(Ptr, LoadSize);
5597 Load->setVolatile(true);
5598 return Load;
5599}
5600
5601Value *CodeGenFunction::EmitISOVolatileStore(const CallExpr *E) {
5602 Value *Ptr = EmitScalarExpr(E->getArg(0));
5603 Value *Value = EmitScalarExpr(E->getArg(1));
5604 QualType ElTy = E->getArg(0)->getType()->getPointeeType();
5605 CharUnits StoreSize = getContext().getTypeSizeInChars(ElTy);
5606 llvm::Type *ITy = llvm::IntegerType::get(getLLVMContext(),
5607 StoreSize.getQuantity() * 8);
5608 Ptr = Builder.CreateBitCast(Ptr, ITy->getPointerTo());
5609 llvm::StoreInst *Store =
5610 Builder.CreateAlignedStore(Value, Ptr,
5611 StoreSize);
5612 Store->setVolatile(true);
5613 return Store;
5614}
5615
5616Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
5617 const CallExpr *E,
5618 llvm::Triple::ArchType Arch) {
5619 if (auto Hint = GetValueForARMHint(BuiltinID))
5620 return Hint;
5621
5622 if (BuiltinID == ARM::BI__emit) {
5623 bool IsThumb = getTarget().getTriple().getArch() == llvm::Triple::thumb;
5624 llvm::FunctionType *FTy =
5625 llvm::FunctionType::get(VoidTy, /*Variadic=*/false);
5626
5627 APSInt Value;
5628 if (!E->getArg(0)->EvaluateAsInt(Value, CGM.getContext()))
5629 llvm_unreachable("Sema will ensure that the parameter is constant")::llvm::llvm_unreachable_internal("Sema will ensure that the parameter is constant"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 5629)
;
5630
5631 uint64_t ZExtValue = Value.zextOrTrunc(IsThumb ? 16 : 32).getZExtValue();
5632
5633 llvm::InlineAsm *Emit =
5634 IsThumb ? InlineAsm::get(FTy, ".inst.n 0x" + utohexstr(ZExtValue), "",
5635 /*SideEffects=*/true)
5636 : InlineAsm::get(FTy, ".inst 0x" + utohexstr(ZExtValue), "",
5637 /*SideEffects=*/true);
5638
5639 return Builder.CreateCall(Emit);
5640 }
5641
5642 if (BuiltinID == ARM::BI__builtin_arm_dbg) {
5643 Value *Option = EmitScalarExpr(E->getArg(0));
5644 return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::arm_dbg), Option);
5645 }
5646
5647 if (BuiltinID == ARM::BI__builtin_arm_prefetch) {
5648 Value *Address = EmitScalarExpr(E->getArg(0));
5649 Value *RW = EmitScalarExpr(E->getArg(1));
5650 Value *IsData = EmitScalarExpr(E->getArg(2));
5651
5652 // Locality is not supported on ARM target
5653 Value *Locality = llvm::ConstantInt::get(Int32Ty, 3);
5654
5655 Value *F = CGM.getIntrinsic(Intrinsic::prefetch);
5656 return Builder.CreateCall(F, {Address, RW, Locality, IsData});
5657 }
5658
5659 if (BuiltinID == ARM::BI__builtin_arm_rbit) {
5660 llvm::Value *Arg = EmitScalarExpr(E->getArg(0));
5661 return Builder.CreateCall(
5662 CGM.getIntrinsic(Intrinsic::bitreverse, Arg->getType()), Arg, "rbit");
5663 }
5664
5665 if (BuiltinID == ARM::BI__clear_cache) {
5666 assert(E->getNumArgs() == 2 && "__clear_cache takes 2 arguments")((E->getNumArgs() == 2 && "__clear_cache takes 2 arguments"
) ? static_cast<void> (0) : __assert_fail ("E->getNumArgs() == 2 && \"__clear_cache takes 2 arguments\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 5666, __PRETTY_FUNCTION__))
;
5667 const FunctionDecl *FD = E->getDirectCallee();
5668 Value *Ops[2];
5669 for (unsigned i = 0; i < 2; i++)
5670 Ops[i] = EmitScalarExpr(E->getArg(i));
5671 llvm::Type *Ty = CGM.getTypes().ConvertType(FD->getType());
5672 llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
5673 StringRef Name = FD->getName();
5674 return EmitNounwindRuntimeCall(CGM.CreateRuntimeFunction(FTy, Name), Ops);
5675 }
5676
5677 if (BuiltinID == ARM::BI__builtin_arm_mcrr ||
5678 BuiltinID == ARM::BI__builtin_arm_mcrr2) {
5679 Function *F;
5680
5681 switch (BuiltinID) {
5682 default: llvm_unreachable("unexpected builtin")::llvm::llvm_unreachable_internal("unexpected builtin", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 5682)
;
5683 case ARM::BI__builtin_arm_mcrr:
5684 F = CGM.getIntrinsic(Intrinsic::arm_mcrr);
5685 break;
5686 case ARM::BI__builtin_arm_mcrr2:
5687 F = CGM.getIntrinsic(Intrinsic::arm_mcrr2);
5688 break;
5689 }
5690
5691 // MCRR{2} instruction has 5 operands but
5692 // the intrinsic has 4 because Rt and Rt2
5693 // are represented as a single unsigned 64
5694 // bit integer in the intrinsic definition
5695 // but internally it's represented as 2 32
5696 // bit integers.
5697
5698 Value *Coproc = EmitScalarExpr(E->getArg(0));
5699 Value *Opc1 = EmitScalarExpr(E->getArg(1));
5700 Value *RtAndRt2 = EmitScalarExpr(E->getArg(2));
5701 Value *CRm = EmitScalarExpr(E->getArg(3));
5702
5703 Value *C1 = llvm::ConstantInt::get(Int64Ty, 32);
5704 Value *Rt = Builder.CreateTruncOrBitCast(RtAndRt2, Int32Ty);
5705 Value *Rt2 = Builder.CreateLShr(RtAndRt2, C1);
5706 Rt2 = Builder.CreateTruncOrBitCast(Rt2, Int32Ty);
5707
5708 return Builder.CreateCall(F, {Coproc, Opc1, Rt, Rt2, CRm});
5709 }
5710
5711 if (BuiltinID == ARM::BI__builtin_arm_mrrc ||
5712 BuiltinID == ARM::BI__builtin_arm_mrrc2) {
5713 Function *F;
5714
5715 switch (BuiltinID) {
5716 default: llvm_unreachable("unexpected builtin")::llvm::llvm_unreachable_internal("unexpected builtin", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 5716)
;
5717 case ARM::BI__builtin_arm_mrrc:
5718 F = CGM.getIntrinsic(Intrinsic::arm_mrrc);
5719 break;
5720 case ARM::BI__builtin_arm_mrrc2:
5721 F = CGM.getIntrinsic(Intrinsic::arm_mrrc2);
5722 break;
5723 }
5724
5725 Value *Coproc = EmitScalarExpr(E->getArg(0));
5726 Value *Opc1 = EmitScalarExpr(E->getArg(1));
5727 Value *CRm = EmitScalarExpr(E->getArg(2));
5728 Value *RtAndRt2 = Builder.CreateCall(F, {Coproc, Opc1, CRm});
5729
5730 // Returns an unsigned 64 bit integer, represented
5731 // as two 32 bit integers.
5732
5733 Value *Rt = Builder.CreateExtractValue(RtAndRt2, 1);
5734 Value *Rt1 = Builder.CreateExtractValue(RtAndRt2, 0);
5735 Rt = Builder.CreateZExt(Rt, Int64Ty);
5736 Rt1 = Builder.CreateZExt(Rt1, Int64Ty);
5737
5738 Value *ShiftCast = llvm::ConstantInt::get(Int64Ty, 32);
5739 RtAndRt2 = Builder.CreateShl(Rt, ShiftCast, "shl", true);
5740 RtAndRt2 = Builder.CreateOr(RtAndRt2, Rt1);
5741
5742 return Builder.CreateBitCast(RtAndRt2, ConvertType(E->getType()));
5743 }
5744
5745 if (BuiltinID == ARM::BI__builtin_arm_ldrexd ||
5746 ((BuiltinID == ARM::BI__builtin_arm_ldrex ||
5747 BuiltinID == ARM::BI__builtin_arm_ldaex) &&
5748 getContext().getTypeSize(E->getType()) == 64) ||
5749 BuiltinID == ARM::BI__ldrexd) {
5750 Function *F;
5751
5752 switch (BuiltinID) {
5753 default: llvm_unreachable("unexpected builtin")::llvm::llvm_unreachable_internal("unexpected builtin", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 5753)
;
5754 case ARM::BI__builtin_arm_ldaex:
5755 F = CGM.getIntrinsic(Intrinsic::arm_ldaexd);
5756 break;
5757 case ARM::BI__builtin_arm_ldrexd:
5758 case ARM::BI__builtin_arm_ldrex:
5759 case ARM::BI__ldrexd:
5760 F = CGM.getIntrinsic(Intrinsic::arm_ldrexd);
5761 break;
5762 }
5763
5764 Value *LdPtr = EmitScalarExpr(E->getArg(0));
5765 Value *Val = Builder.CreateCall(F, Builder.CreateBitCast(LdPtr, Int8PtrTy),
5766 "ldrexd");
5767
5768 Value *Val0 = Builder.CreateExtractValue(Val, 1);
5769 Value *Val1 = Builder.CreateExtractValue(Val, 0);
5770 Val0 = Builder.CreateZExt(Val0, Int64Ty);
5771 Val1 = Builder.CreateZExt(Val1, Int64Ty);
5772
5773 Value *ShiftCst = llvm::ConstantInt::get(Int64Ty, 32);
5774 Val = Builder.CreateShl(Val0, ShiftCst, "shl", true /* nuw */);
5775 Val = Builder.CreateOr(Val, Val1);
5776 return Builder.CreateBitCast(Val, ConvertType(E->getType()));
5777 }
5778
5779 if (BuiltinID == ARM::BI__builtin_arm_ldrex ||
5780 BuiltinID == ARM::BI__builtin_arm_ldaex) {
5781 Value *LoadAddr = EmitScalarExpr(E->getArg(0));
5782
5783 QualType Ty = E->getType();
5784 llvm::Type *RealResTy = ConvertType(Ty);
5785 llvm::Type *PtrTy = llvm::IntegerType::get(
5786 getLLVMContext(), getContext().getTypeSize(Ty))->getPointerTo();
5787 LoadAddr = Builder.CreateBitCast(LoadAddr, PtrTy);
5788
5789 Function *F = CGM.getIntrinsic(BuiltinID == ARM::BI__builtin_arm_ldaex
5790 ? Intrinsic::arm_ldaex
5791 : Intrinsic::arm_ldrex,
5792 PtrTy);
5793 Value *Val = Builder.CreateCall(F, LoadAddr, "ldrex");
5794
5795 if (RealResTy->isPointerTy())
5796 return Builder.CreateIntToPtr(Val, RealResTy);
5797 else {
5798 llvm::Type *IntResTy = llvm::IntegerType::get(
5799 getLLVMContext(), CGM.getDataLayout().getTypeSizeInBits(RealResTy));
5800 Val = Builder.CreateTruncOrBitCast(Val, IntResTy);
5801 return Builder.CreateBitCast(Val, RealResTy);
5802 }
5803 }
5804
5805 if (BuiltinID == ARM::BI__builtin_arm_strexd ||
5806 ((BuiltinID == ARM::BI__builtin_arm_stlex ||
5807 BuiltinID == ARM::BI__builtin_arm_strex) &&
5808 getContext().getTypeSize(E->getArg(0)->getType()) == 64)) {
5809 Function *F = CGM.getIntrinsic(BuiltinID == ARM::BI__builtin_arm_stlex
5810 ? Intrinsic::arm_stlexd
5811 : Intrinsic::arm_strexd);
5812 llvm::Type *STy = llvm::StructType::get(Int32Ty, Int32Ty);
5813
5814 Address Tmp = CreateMemTemp(E->getArg(0)->getType());
5815 Value *Val = EmitScalarExpr(E->getArg(0));
5816 Builder.CreateStore(Val, Tmp);
5817
5818 Address LdPtr = Builder.CreateBitCast(Tmp,llvm::PointerType::getUnqual(STy));
5819 Val = Builder.CreateLoad(LdPtr);
5820
5821 Value *Arg0 = Builder.CreateExtractValue(Val, 0);
5822 Value *Arg1 = Builder.CreateExtractValue(Val, 1);
5823 Value *StPtr = Builder.CreateBitCast(EmitScalarExpr(E->getArg(1)), Int8PtrTy);
5824 return Builder.CreateCall(F, {Arg0, Arg1, StPtr}, "strexd");
5825 }
5826
5827 if (BuiltinID == ARM::BI__builtin_arm_strex ||
5828 BuiltinID == ARM::BI__builtin_arm_stlex) {
5829 Value *StoreVal = EmitScalarExpr(E->getArg(0));
5830 Value *StoreAddr = EmitScalarExpr(E->getArg(1));
5831
5832 QualType Ty = E->getArg(0)->getType();
5833 llvm::Type *StoreTy = llvm::IntegerType::get(getLLVMContext(),
5834 getContext().getTypeSize(Ty));
5835 StoreAddr = Builder.CreateBitCast(StoreAddr, StoreTy->getPointerTo());
5836
5837 if (StoreVal->getType()->isPointerTy())
5838 StoreVal = Builder.CreatePtrToInt(StoreVal, Int32Ty);
5839 else {
5840 llvm::Type *IntTy = llvm::IntegerType::get(
5841 getLLVMContext(),
5842 CGM.getDataLayout().getTypeSizeInBits(StoreVal->getType()));
5843 StoreVal = Builder.CreateBitCast(StoreVal, IntTy);
5844 StoreVal = Builder.CreateZExtOrBitCast(StoreVal, Int32Ty);
5845 }
5846
5847 Function *F = CGM.getIntrinsic(BuiltinID == ARM::BI__builtin_arm_stlex
5848 ? Intrinsic::arm_stlex
5849 : Intrinsic::arm_strex,
5850 StoreAddr->getType());
5851 return Builder.CreateCall(F, {StoreVal, StoreAddr}, "strex");
5852 }
5853
5854 switch (BuiltinID) {
5855 case ARM::BI__iso_volatile_load8:
5856 case ARM::BI__iso_volatile_load16:
5857 case ARM::BI__iso_volatile_load32:
5858 case ARM::BI__iso_volatile_load64:
5859 return EmitISOVolatileLoad(E);
5860 case ARM::BI__iso_volatile_store8:
5861 case ARM::BI__iso_volatile_store16:
5862 case ARM::BI__iso_volatile_store32:
5863 case ARM::BI__iso_volatile_store64:
5864 return EmitISOVolatileStore(E);
5865 }
5866
5867 if (BuiltinID == ARM::BI__builtin_arm_clrex) {
5868 Function *F = CGM.getIntrinsic(Intrinsic::arm_clrex);
5869 return Builder.CreateCall(F);
5870 }
5871
5872 // CRC32
5873 Intrinsic::ID CRCIntrinsicID = Intrinsic::not_intrinsic;
5874 switch (BuiltinID) {
5875 case ARM::BI__builtin_arm_crc32b:
5876 CRCIntrinsicID = Intrinsic::arm_crc32b; break;
5877 case ARM::BI__builtin_arm_crc32cb:
5878 CRCIntrinsicID = Intrinsic::arm_crc32cb; break;
5879 case ARM::BI__builtin_arm_crc32h:
5880 CRCIntrinsicID = Intrinsic::arm_crc32h; break;
5881 case ARM::BI__builtin_arm_crc32ch:
5882 CRCIntrinsicID = Intrinsic::arm_crc32ch; break;
5883 case ARM::BI__builtin_arm_crc32w:
5884 case ARM::BI__builtin_arm_crc32d:
5885 CRCIntrinsicID = Intrinsic::arm_crc32w; break;
5886 case ARM::BI__builtin_arm_crc32cw:
5887 case ARM::BI__builtin_arm_crc32cd:
5888 CRCIntrinsicID = Intrinsic::arm_crc32cw; break;
5889 }
5890
5891 if (CRCIntrinsicID != Intrinsic::not_intrinsic) {
5892 Value *Arg0 = EmitScalarExpr(E->getArg(0));
5893 Value *Arg1 = EmitScalarExpr(E->getArg(1));
5894
5895 // crc32{c,}d intrinsics are implemnted as two calls to crc32{c,}w
5896 // intrinsics, hence we need different codegen for these cases.
5897 if (BuiltinID == ARM::BI__builtin_arm_crc32d ||
5898 BuiltinID == ARM::BI__builtin_arm_crc32cd) {
5899 Value *C1 = llvm::ConstantInt::get(Int64Ty, 32);
5900 Value *Arg1a = Builder.CreateTruncOrBitCast(Arg1, Int32Ty);
5901 Value *Arg1b = Builder.CreateLShr(Arg1, C1);
5902 Arg1b = Builder.CreateTruncOrBitCast(Arg1b, Int32Ty);
5903
5904 Function *F = CGM.getIntrinsic(CRCIntrinsicID);
5905 Value *Res = Builder.CreateCall(F, {Arg0, Arg1a});
5906 return Builder.CreateCall(F, {Res, Arg1b});
5907 } else {
5908 Arg1 = Builder.CreateZExtOrBitCast(Arg1, Int32Ty);
5909
5910 Function *F = CGM.getIntrinsic(CRCIntrinsicID);
5911 return Builder.CreateCall(F, {Arg0, Arg1});
5912 }
5913 }
5914
5915 if (BuiltinID == ARM::BI__builtin_arm_rsr ||
5916 BuiltinID == ARM::BI__builtin_arm_rsr64 ||
5917 BuiltinID == ARM::BI__builtin_arm_rsrp ||
5918 BuiltinID == ARM::BI__builtin_arm_wsr ||
5919 BuiltinID == ARM::BI__builtin_arm_wsr64 ||
5920 BuiltinID == ARM::BI__builtin_arm_wsrp) {
5921
5922 bool IsRead = BuiltinID == ARM::BI__builtin_arm_rsr ||
5923 BuiltinID == ARM::BI__builtin_arm_rsr64 ||
5924 BuiltinID == ARM::BI__builtin_arm_rsrp;
5925
5926 bool IsPointerBuiltin = BuiltinID == ARM::BI__builtin_arm_rsrp ||
5927 BuiltinID == ARM::BI__builtin_arm_wsrp;
5928
5929 bool Is64Bit = BuiltinID == ARM::BI__builtin_arm_rsr64 ||
5930 BuiltinID == ARM::BI__builtin_arm_wsr64;
5931
5932 llvm::Type *ValueType;
5933 llvm::Type *RegisterType;
5934 if (IsPointerBuiltin) {
5935 ValueType = VoidPtrTy;
5936 RegisterType = Int32Ty;
5937 } else if (Is64Bit) {
5938 ValueType = RegisterType = Int64Ty;
5939 } else {
5940 ValueType = RegisterType = Int32Ty;
5941 }
5942
5943 return EmitSpecialRegisterBuiltin(*this, E, RegisterType, ValueType, IsRead);
5944 }
5945
5946 // Find out if any arguments are required to be integer constant
5947 // expressions.
5948 unsigned ICEArguments = 0;
5949 ASTContext::GetBuiltinTypeError Error;
5950 getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments);
5951 assert(Error == ASTContext::GE_None && "Should not codegen an error")((Error == ASTContext::GE_None && "Should not codegen an error"
) ? static_cast<void> (0) : __assert_fail ("Error == ASTContext::GE_None && \"Should not codegen an error\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 5951, __PRETTY_FUNCTION__))
;
5952
5953 auto getAlignmentValue32 = [&](Address addr) -> Value* {
5954 return Builder.getInt32(addr.getAlignment().getQuantity());
5955 };
5956
5957 Address PtrOp0 = Address::invalid();
5958 Address PtrOp1 = Address::invalid();
5959 SmallVector<Value*, 4> Ops;
5960 bool HasExtraArg = HasExtraNeonArgument(BuiltinID);
5961 unsigned NumArgs = E->getNumArgs() - (HasExtraArg ? 1 : 0);
5962 for (unsigned i = 0, e = NumArgs; i != e; i++) {
5963 if (i == 0) {
5964 switch (BuiltinID) {
5965 case NEON::BI__builtin_neon_vld1_v:
5966 case NEON::BI__builtin_neon_vld1q_v:
5967 case NEON::BI__builtin_neon_vld1q_lane_v:
5968 case NEON::BI__builtin_neon_vld1_lane_v:
5969 case NEON::BI__builtin_neon_vld1_dup_v:
5970 case NEON::BI__builtin_neon_vld1q_dup_v:
5971 case NEON::BI__builtin_neon_vst1_v:
5972 case NEON::BI__builtin_neon_vst1q_v:
5973 case NEON::BI__builtin_neon_vst1q_lane_v:
5974 case NEON::BI__builtin_neon_vst1_lane_v:
5975 case NEON::BI__builtin_neon_vst2_v:
5976 case NEON::BI__builtin_neon_vst2q_v:
5977 case NEON::BI__builtin_neon_vst2_lane_v:
5978 case NEON::BI__builtin_neon_vst2q_lane_v:
5979 case NEON::BI__builtin_neon_vst3_v:
5980 case NEON::BI__builtin_neon_vst3q_v:
5981 case NEON::BI__builtin_neon_vst3_lane_v:
5982 case NEON::BI__builtin_neon_vst3q_lane_v:
5983 case NEON::BI__builtin_neon_vst4_v:
5984 case NEON::BI__builtin_neon_vst4q_v:
5985 case NEON::BI__builtin_neon_vst4_lane_v:
5986 case NEON::BI__builtin_neon_vst4q_lane_v:
5987 // Get the alignment for the argument in addition to the value;
5988 // we'll use it later.
5989 PtrOp0 = EmitPointerWithAlignment(E->getArg(0));
5990 Ops.push_back(PtrOp0.getPointer());
5991 continue;
5992 }
5993 }
5994 if (i == 1) {
5995 switch (BuiltinID) {
5996 case NEON::BI__builtin_neon_vld2_v:
5997 case NEON::BI__builtin_neon_vld2q_v:
5998 case NEON::BI__builtin_neon_vld3_v:
5999 case NEON::BI__builtin_neon_vld3q_v:
6000 case NEON::BI__builtin_neon_vld4_v:
6001 case NEON::BI__builtin_neon_vld4q_v:
6002 case NEON::BI__builtin_neon_vld2_lane_v:
6003 case NEON::BI__builtin_neon_vld2q_lane_v:
6004 case NEON::BI__builtin_neon_vld3_lane_v:
6005 case NEON::BI__builtin_neon_vld3q_lane_v:
6006 case NEON::BI__builtin_neon_vld4_lane_v:
6007 case NEON::BI__builtin_neon_vld4q_lane_v:
6008 case NEON::BI__builtin_neon_vld2_dup_v:
6009 case NEON::BI__builtin_neon_vld2q_dup_v:
6010 case NEON::BI__builtin_neon_vld3_dup_v:
6011 case NEON::BI__builtin_neon_vld3q_dup_v:
6012 case NEON::BI__builtin_neon_vld4_dup_v:
6013 case NEON::BI__builtin_neon_vld4q_dup_v:
6014 // Get the alignment for the argument in addition to the value;
6015 // we'll use it later.
6016 PtrOp1 = EmitPointerWithAlignment(E->getArg(1));
6017 Ops.push_back(PtrOp1.getPointer());
6018 continue;
6019 }
6020 }
6021
6022 if ((ICEArguments & (1 << i)) == 0) {
6023 Ops.push_back(EmitScalarExpr(E->getArg(i)));
6024 } else {
6025 // If this is required to be a constant, constant fold it so that we know
6026 // that the generated intrinsic gets a ConstantInt.
6027 llvm::APSInt Result;
6028 bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result, getContext());
6029 assert(IsConst && "Constant arg isn't actually constant?")((IsConst && "Constant arg isn't actually constant?")
? static_cast<void> (0) : __assert_fail ("IsConst && \"Constant arg isn't actually constant?\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/lib/CodeGen/CGBuiltin.cpp"
, 6029, __PRETTY_FUNCTION__))
; (void)IsConst;
6030 Ops.push_back(llvm::ConstantInt::get(getLLVMContext(), Result));
6031 }
6032 }
6033
6034 switch (BuiltinID) {
6035 default: break;
6036
6037 case NEON::BI__builtin_neon_vget_lane_i8:
6038 case NEON::BI__builtin_neon_vget_lane_i16:
6039 case NEON::BI__builtin_neon_vget_lane_i32:
6040 case NEON::BI__builtin_neon_vget_lane_i64:
6041 case NEON::BI__builtin_neon_vget_lane_f32:
6042 case NEON::BI__builtin_neon_vgetq_lane_i8:
6043 case NEON::BI__builtin_neon_vgetq_lane_i16:
6044 case NEON::BI__builtin_neon_vgetq_lane_i32:
6045 case NEON::BI__builtin_neon_vgetq_lane_i64:
6046 case NEON::BI__builtin_neon_vgetq_lane_f32:
6047 return Builder.CreateExtractElement(Ops[0], Ops[1], "vget_lane");
6048
6049 case NEON::BI__builtin_neon_vrndns_f32: {
6050 Value *Arg = EmitScalarExpr(E->getArg(0));
6051 llvm::Type *Tys[] = {Arg->getType()};
6052 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vrintn, Tys);
6053 return Builder.CreateCall(F, {Arg}, "vrndn"); }
6054
6055 case NEON::BI__builtin_neon_vset_lane_i8:
6056 case NEON::BI__builtin_neon_vset_lane_i16:
6057 case NEON::BI__builtin_neon_vset_lane_i32:
6058 case NEON::BI__builtin_neon_vset_lane_i64:
6059 case NEON::BI__builtin_neon_vset_lane_f32:
6060 case NEON::BI__builtin_neon_vsetq_lane_i8:
6061 case NEON::BI__builtin_neon_vsetq_lane_i16:
6062 case NEON::BI__builtin_neon_vsetq_lane_i32:
6063 case NEON::BI__builtin_neon_vsetq_lane_i64:
6064 case NEON::BI__builtin_neon_vsetq_lane_f32:
6065 return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vset_lane");
6066
6067 case NEON::BI__builtin_neon_vsha1h_u32: