Bug Summary

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