Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

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