Bug Summary

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