Bug Summary

File:tools/clang/lib/CodeGen/CGBuiltin.cpp
Warning:line 11747, column 12
Value stored to 'Store' during its initialization is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

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