Bug Summary

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