Bug Summary

File:tools/clang/lib/CodeGen/CGBuiltin.cpp
Warning:line 8194, column 22
Division by zero

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