Bug Summary

File:tools/clang/lib/CodeGen/CGBuiltin.cpp
Warning:line 1401, column 20
Called C++ object pointer is null

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