Bug Summary

File:clang/lib/CodeGen/CGBuiltin.cpp
Warning:line 14914, column 9
1st function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

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