Bug Summary

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