1*9880d681SAndroid Build Coastguard Worker //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the auto-upgrade helper functions.
11*9880d681SAndroid Build Coastguard Worker // This is where deprecated IR intrinsics and other IR features are updated to
12*9880d681SAndroid Build Coastguard Worker // current specifications.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/AutoUpgrade.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CFG.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallSite.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DIBuilder.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DebugInfo.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DiagnosticInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IRBuilder.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instruction.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Regex.h"
31*9880d681SAndroid Build Coastguard Worker #include <cstring>
32*9880d681SAndroid Build Coastguard Worker using namespace llvm;
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker // Upgrade the declarations of the SSE4.1 functions whose arguments have
35*9880d681SAndroid Build Coastguard Worker // changed their type from v4f32 to v2i64.
UpgradeSSE41Function(Function * F,Intrinsic::ID IID,Function * & NewFn)36*9880d681SAndroid Build Coastguard Worker static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
37*9880d681SAndroid Build Coastguard Worker Function *&NewFn) {
38*9880d681SAndroid Build Coastguard Worker // Check whether this is an old version of the function, which received
39*9880d681SAndroid Build Coastguard Worker // v4f32 arguments.
40*9880d681SAndroid Build Coastguard Worker Type *Arg0Type = F->getFunctionType()->getParamType(0);
41*9880d681SAndroid Build Coastguard Worker if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
42*9880d681SAndroid Build Coastguard Worker return false;
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker // Yes, it's old, replace it with new version.
45*9880d681SAndroid Build Coastguard Worker F->setName(F->getName() + ".old");
46*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
47*9880d681SAndroid Build Coastguard Worker return true;
48*9880d681SAndroid Build Coastguard Worker }
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker // Upgrade the declarations of intrinsic functions whose 8-bit immediate mask
51*9880d681SAndroid Build Coastguard Worker // arguments have changed their type from i32 to i8.
UpgradeX86IntrinsicsWith8BitMask(Function * F,Intrinsic::ID IID,Function * & NewFn)52*9880d681SAndroid Build Coastguard Worker static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID,
53*9880d681SAndroid Build Coastguard Worker Function *&NewFn) {
54*9880d681SAndroid Build Coastguard Worker // Check that the last argument is an i32.
55*9880d681SAndroid Build Coastguard Worker Type *LastArgType = F->getFunctionType()->getParamType(
56*9880d681SAndroid Build Coastguard Worker F->getFunctionType()->getNumParams() - 1);
57*9880d681SAndroid Build Coastguard Worker if (!LastArgType->isIntegerTy(32))
58*9880d681SAndroid Build Coastguard Worker return false;
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Worker // Move this function aside and map down.
61*9880d681SAndroid Build Coastguard Worker F->setName(F->getName() + ".old");
62*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
63*9880d681SAndroid Build Coastguard Worker return true;
64*9880d681SAndroid Build Coastguard Worker }
65*9880d681SAndroid Build Coastguard Worker
UpgradeIntrinsicFunction1(Function * F,Function * & NewFn)66*9880d681SAndroid Build Coastguard Worker static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
67*9880d681SAndroid Build Coastguard Worker assert(F && "Illegal to upgrade a non-existent Function.");
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker // Quickly eliminate it, if it's not a candidate.
70*9880d681SAndroid Build Coastguard Worker StringRef Name = F->getName();
71*9880d681SAndroid Build Coastguard Worker if (Name.size() <= 8 || !Name.startswith("llvm."))
72*9880d681SAndroid Build Coastguard Worker return false;
73*9880d681SAndroid Build Coastguard Worker Name = Name.substr(5); // Strip off "llvm."
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Worker switch (Name[0]) {
76*9880d681SAndroid Build Coastguard Worker default: break;
77*9880d681SAndroid Build Coastguard Worker case 'a': {
78*9880d681SAndroid Build Coastguard Worker if (Name.startswith("arm.neon.vclz")) {
79*9880d681SAndroid Build Coastguard Worker Type* args[2] = {
80*9880d681SAndroid Build Coastguard Worker F->arg_begin()->getType(),
81*9880d681SAndroid Build Coastguard Worker Type::getInt1Ty(F->getContext())
82*9880d681SAndroid Build Coastguard Worker };
83*9880d681SAndroid Build Coastguard Worker // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
84*9880d681SAndroid Build Coastguard Worker // the end of the name. Change name from llvm.arm.neon.vclz.* to
85*9880d681SAndroid Build Coastguard Worker // llvm.ctlz.*
86*9880d681SAndroid Build Coastguard Worker FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
87*9880d681SAndroid Build Coastguard Worker NewFn = Function::Create(fType, F->getLinkage(),
88*9880d681SAndroid Build Coastguard Worker "llvm.ctlz." + Name.substr(14), F->getParent());
89*9880d681SAndroid Build Coastguard Worker return true;
90*9880d681SAndroid Build Coastguard Worker }
91*9880d681SAndroid Build Coastguard Worker if (Name.startswith("arm.neon.vcnt")) {
92*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
93*9880d681SAndroid Build Coastguard Worker F->arg_begin()->getType());
94*9880d681SAndroid Build Coastguard Worker return true;
95*9880d681SAndroid Build Coastguard Worker }
96*9880d681SAndroid Build Coastguard Worker Regex vldRegex("^arm\\.neon\\.vld([1234]|[234]lane)\\.v[a-z0-9]*$");
97*9880d681SAndroid Build Coastguard Worker if (vldRegex.match(Name)) {
98*9880d681SAndroid Build Coastguard Worker auto fArgs = F->getFunctionType()->params();
99*9880d681SAndroid Build Coastguard Worker SmallVector<Type *, 4> Tys(fArgs.begin(), fArgs.end());
100*9880d681SAndroid Build Coastguard Worker // Can't use Intrinsic::getDeclaration here as the return types might
101*9880d681SAndroid Build Coastguard Worker // then only be structurally equal.
102*9880d681SAndroid Build Coastguard Worker FunctionType* fType = FunctionType::get(F->getReturnType(), Tys, false);
103*9880d681SAndroid Build Coastguard Worker NewFn = Function::Create(fType, F->getLinkage(),
104*9880d681SAndroid Build Coastguard Worker "llvm." + Name + ".p0i8", F->getParent());
105*9880d681SAndroid Build Coastguard Worker return true;
106*9880d681SAndroid Build Coastguard Worker }
107*9880d681SAndroid Build Coastguard Worker Regex vstRegex("^arm\\.neon\\.vst([1234]|[234]lane)\\.v[a-z0-9]*$");
108*9880d681SAndroid Build Coastguard Worker if (vstRegex.match(Name)) {
109*9880d681SAndroid Build Coastguard Worker static const Intrinsic::ID StoreInts[] = {Intrinsic::arm_neon_vst1,
110*9880d681SAndroid Build Coastguard Worker Intrinsic::arm_neon_vst2,
111*9880d681SAndroid Build Coastguard Worker Intrinsic::arm_neon_vst3,
112*9880d681SAndroid Build Coastguard Worker Intrinsic::arm_neon_vst4};
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker static const Intrinsic::ID StoreLaneInts[] = {
115*9880d681SAndroid Build Coastguard Worker Intrinsic::arm_neon_vst2lane, Intrinsic::arm_neon_vst3lane,
116*9880d681SAndroid Build Coastguard Worker Intrinsic::arm_neon_vst4lane
117*9880d681SAndroid Build Coastguard Worker };
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard Worker auto fArgs = F->getFunctionType()->params();
120*9880d681SAndroid Build Coastguard Worker Type *Tys[] = {fArgs[0], fArgs[1]};
121*9880d681SAndroid Build Coastguard Worker if (Name.find("lane") == StringRef::npos)
122*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(),
123*9880d681SAndroid Build Coastguard Worker StoreInts[fArgs.size() - 3], Tys);
124*9880d681SAndroid Build Coastguard Worker else
125*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(),
126*9880d681SAndroid Build Coastguard Worker StoreLaneInts[fArgs.size() - 5], Tys);
127*9880d681SAndroid Build Coastguard Worker return true;
128*9880d681SAndroid Build Coastguard Worker }
129*9880d681SAndroid Build Coastguard Worker if (Name == "aarch64.thread.pointer" || Name == "arm.thread.pointer") {
130*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::thread_pointer);
131*9880d681SAndroid Build Coastguard Worker return true;
132*9880d681SAndroid Build Coastguard Worker }
133*9880d681SAndroid Build Coastguard Worker break;
134*9880d681SAndroid Build Coastguard Worker }
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker case 'c': {
137*9880d681SAndroid Build Coastguard Worker if (Name.startswith("ctlz.") && F->arg_size() == 1) {
138*9880d681SAndroid Build Coastguard Worker F->setName(Name + ".old");
139*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
140*9880d681SAndroid Build Coastguard Worker F->arg_begin()->getType());
141*9880d681SAndroid Build Coastguard Worker return true;
142*9880d681SAndroid Build Coastguard Worker }
143*9880d681SAndroid Build Coastguard Worker if (Name.startswith("cttz.") && F->arg_size() == 1) {
144*9880d681SAndroid Build Coastguard Worker F->setName(Name + ".old");
145*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
146*9880d681SAndroid Build Coastguard Worker F->arg_begin()->getType());
147*9880d681SAndroid Build Coastguard Worker return true;
148*9880d681SAndroid Build Coastguard Worker }
149*9880d681SAndroid Build Coastguard Worker break;
150*9880d681SAndroid Build Coastguard Worker }
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker case 'm': {
153*9880d681SAndroid Build Coastguard Worker if (Name.startswith("masked.load.")) {
154*9880d681SAndroid Build Coastguard Worker Type *Tys[] = { F->getReturnType(), F->arg_begin()->getType() };
155*9880d681SAndroid Build Coastguard Worker if (F->getName() != Intrinsic::getName(Intrinsic::masked_load, Tys)) {
156*9880d681SAndroid Build Coastguard Worker F->setName(Name + ".old");
157*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(),
158*9880d681SAndroid Build Coastguard Worker Intrinsic::masked_load,
159*9880d681SAndroid Build Coastguard Worker Tys);
160*9880d681SAndroid Build Coastguard Worker return true;
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker }
163*9880d681SAndroid Build Coastguard Worker if (Name.startswith("masked.store.")) {
164*9880d681SAndroid Build Coastguard Worker auto Args = F->getFunctionType()->params();
165*9880d681SAndroid Build Coastguard Worker Type *Tys[] = { Args[0], Args[1] };
166*9880d681SAndroid Build Coastguard Worker if (F->getName() != Intrinsic::getName(Intrinsic::masked_store, Tys)) {
167*9880d681SAndroid Build Coastguard Worker F->setName(Name + ".old");
168*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(),
169*9880d681SAndroid Build Coastguard Worker Intrinsic::masked_store,
170*9880d681SAndroid Build Coastguard Worker Tys);
171*9880d681SAndroid Build Coastguard Worker return true;
172*9880d681SAndroid Build Coastguard Worker }
173*9880d681SAndroid Build Coastguard Worker }
174*9880d681SAndroid Build Coastguard Worker break;
175*9880d681SAndroid Build Coastguard Worker }
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker case 'o':
178*9880d681SAndroid Build Coastguard Worker // We only need to change the name to match the mangling including the
179*9880d681SAndroid Build Coastguard Worker // address space.
180*9880d681SAndroid Build Coastguard Worker if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
181*9880d681SAndroid Build Coastguard Worker Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
182*9880d681SAndroid Build Coastguard Worker if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
183*9880d681SAndroid Build Coastguard Worker F->setName(Name + ".old");
184*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(),
185*9880d681SAndroid Build Coastguard Worker Intrinsic::objectsize, Tys);
186*9880d681SAndroid Build Coastguard Worker return true;
187*9880d681SAndroid Build Coastguard Worker }
188*9880d681SAndroid Build Coastguard Worker }
189*9880d681SAndroid Build Coastguard Worker break;
190*9880d681SAndroid Build Coastguard Worker
191*9880d681SAndroid Build Coastguard Worker case 's':
192*9880d681SAndroid Build Coastguard Worker if (Name == "stackprotectorcheck") {
193*9880d681SAndroid Build Coastguard Worker NewFn = nullptr;
194*9880d681SAndroid Build Coastguard Worker return true;
195*9880d681SAndroid Build Coastguard Worker }
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker case 'x': {
198*9880d681SAndroid Build Coastguard Worker bool IsX86 = Name.startswith("x86.");
199*9880d681SAndroid Build Coastguard Worker if (IsX86)
200*9880d681SAndroid Build Coastguard Worker Name = Name.substr(4);
201*9880d681SAndroid Build Coastguard Worker
202*9880d681SAndroid Build Coastguard Worker if (IsX86 &&
203*9880d681SAndroid Build Coastguard Worker (Name.startswith("sse2.pcmpeq.") ||
204*9880d681SAndroid Build Coastguard Worker Name.startswith("sse2.pcmpgt.") ||
205*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pcmpeq.") ||
206*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pcmpgt.") ||
207*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.pcmpeq.") ||
208*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.pcmpgt.") ||
209*9880d681SAndroid Build Coastguard Worker Name == "sse41.pmaxsb" ||
210*9880d681SAndroid Build Coastguard Worker Name == "sse2.pmaxs.w" ||
211*9880d681SAndroid Build Coastguard Worker Name == "sse41.pmaxsd" ||
212*9880d681SAndroid Build Coastguard Worker Name == "sse2.pmaxu.b" ||
213*9880d681SAndroid Build Coastguard Worker Name == "sse41.pmaxuw" ||
214*9880d681SAndroid Build Coastguard Worker Name == "sse41.pmaxud" ||
215*9880d681SAndroid Build Coastguard Worker Name == "sse41.pminsb" ||
216*9880d681SAndroid Build Coastguard Worker Name == "sse2.pmins.w" ||
217*9880d681SAndroid Build Coastguard Worker Name == "sse41.pminsd" ||
218*9880d681SAndroid Build Coastguard Worker Name == "sse2.pminu.b" ||
219*9880d681SAndroid Build Coastguard Worker Name == "sse41.pminuw" ||
220*9880d681SAndroid Build Coastguard Worker Name == "sse41.pminud" ||
221*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pmax") ||
222*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pmin") ||
223*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.vbroadcast") ||
224*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pbroadcast") ||
225*9880d681SAndroid Build Coastguard Worker Name.startswith("avx.vpermil.") ||
226*9880d681SAndroid Build Coastguard Worker Name.startswith("sse2.pshuf") ||
227*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.pbroadcast") ||
228*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.broadcast.s") ||
229*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.movddup") ||
230*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.movshdup") ||
231*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.movsldup") ||
232*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.pshuf.d.") ||
233*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.pshufl.w.") ||
234*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.pshufh.w.") ||
235*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.vpermil.p") ||
236*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.perm.df.") ||
237*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.perm.di.") ||
238*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.punpckl") ||
239*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.punpckh") ||
240*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.unpckl.") ||
241*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.unpckh.") ||
242*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.pand.") ||
243*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.pandn.") ||
244*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.por.") ||
245*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.pxor.") ||
246*9880d681SAndroid Build Coastguard Worker Name.startswith("sse41.pmovsx") ||
247*9880d681SAndroid Build Coastguard Worker Name.startswith("sse41.pmovzx") ||
248*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pmovsx") ||
249*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pmovzx") ||
250*9880d681SAndroid Build Coastguard Worker Name == "sse2.cvtdq2pd" ||
251*9880d681SAndroid Build Coastguard Worker Name == "sse2.cvtps2pd" ||
252*9880d681SAndroid Build Coastguard Worker Name == "avx.cvtdq2.pd.256" ||
253*9880d681SAndroid Build Coastguard Worker Name == "avx.cvt.ps2.pd.256" ||
254*9880d681SAndroid Build Coastguard Worker Name == "sse2.cvttps2dq" ||
255*9880d681SAndroid Build Coastguard Worker Name.startswith("avx.cvtt.") ||
256*9880d681SAndroid Build Coastguard Worker Name.startswith("avx.vinsertf128.") ||
257*9880d681SAndroid Build Coastguard Worker Name == "avx2.vinserti128" ||
258*9880d681SAndroid Build Coastguard Worker Name.startswith("avx.vextractf128.") ||
259*9880d681SAndroid Build Coastguard Worker Name == "avx2.vextracti128" ||
260*9880d681SAndroid Build Coastguard Worker Name.startswith("sse4a.movnt.") ||
261*9880d681SAndroid Build Coastguard Worker Name.startswith("avx.movnt.") ||
262*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.storent.") ||
263*9880d681SAndroid Build Coastguard Worker Name == "sse2.storel.dq" ||
264*9880d681SAndroid Build Coastguard Worker Name.startswith("sse.storeu.") ||
265*9880d681SAndroid Build Coastguard Worker Name.startswith("sse2.storeu.") ||
266*9880d681SAndroid Build Coastguard Worker Name.startswith("avx.storeu.") ||
267*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.storeu.p") ||
268*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.storeu.b.") ||
269*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.storeu.w.") ||
270*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.storeu.d.") ||
271*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.storeu.q.") ||
272*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.store.p") ||
273*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.store.b.") ||
274*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.store.w.") ||
275*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.store.d.") ||
276*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.store.q.") ||
277*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.loadu.p") ||
278*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.loadu.b.") ||
279*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.loadu.w.") ||
280*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.loadu.d.") ||
281*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.loadu.q.") ||
282*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.load.p") ||
283*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.load.b.") ||
284*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.load.w.") ||
285*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.load.d.") ||
286*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.load.q.") ||
287*9880d681SAndroid Build Coastguard Worker Name == "sse42.crc32.64.8" ||
288*9880d681SAndroid Build Coastguard Worker Name.startswith("avx.vbroadcast.s") ||
289*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.palignr.") ||
290*9880d681SAndroid Build Coastguard Worker Name.startswith("sse2.psll.dq") ||
291*9880d681SAndroid Build Coastguard Worker Name.startswith("sse2.psrl.dq") ||
292*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.psll.dq") ||
293*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.psrl.dq") ||
294*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.psll.dq") ||
295*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.psrl.dq") ||
296*9880d681SAndroid Build Coastguard Worker Name == "sse41.pblendw" ||
297*9880d681SAndroid Build Coastguard Worker Name.startswith("sse41.blendp") ||
298*9880d681SAndroid Build Coastguard Worker Name.startswith("avx.blend.p") ||
299*9880d681SAndroid Build Coastguard Worker Name == "avx2.pblendw" ||
300*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pblendd.") ||
301*9880d681SAndroid Build Coastguard Worker Name == "avx2.vbroadcasti128" ||
302*9880d681SAndroid Build Coastguard Worker Name == "xop.vpcmov" ||
303*9880d681SAndroid Build Coastguard Worker (Name.startswith("xop.vpcom") && F->arg_size() == 2))) {
304*9880d681SAndroid Build Coastguard Worker NewFn = nullptr;
305*9880d681SAndroid Build Coastguard Worker return true;
306*9880d681SAndroid Build Coastguard Worker }
307*9880d681SAndroid Build Coastguard Worker // SSE4.1 ptest functions may have an old signature.
308*9880d681SAndroid Build Coastguard Worker if (IsX86 && Name.startswith("sse41.ptest")) {
309*9880d681SAndroid Build Coastguard Worker if (Name.substr(11) == "c")
310*9880d681SAndroid Build Coastguard Worker return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
311*9880d681SAndroid Build Coastguard Worker if (Name.substr(11) == "z")
312*9880d681SAndroid Build Coastguard Worker return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
313*9880d681SAndroid Build Coastguard Worker if (Name.substr(11) == "nzc")
314*9880d681SAndroid Build Coastguard Worker return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
315*9880d681SAndroid Build Coastguard Worker }
316*9880d681SAndroid Build Coastguard Worker // Several blend and other instructions with masks used the wrong number of
317*9880d681SAndroid Build Coastguard Worker // bits.
318*9880d681SAndroid Build Coastguard Worker if (IsX86 && Name == "sse41.insertps")
319*9880d681SAndroid Build Coastguard Worker return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
320*9880d681SAndroid Build Coastguard Worker NewFn);
321*9880d681SAndroid Build Coastguard Worker if (IsX86 && Name == "sse41.dppd")
322*9880d681SAndroid Build Coastguard Worker return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
323*9880d681SAndroid Build Coastguard Worker NewFn);
324*9880d681SAndroid Build Coastguard Worker if (IsX86 && Name == "sse41.dpps")
325*9880d681SAndroid Build Coastguard Worker return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
326*9880d681SAndroid Build Coastguard Worker NewFn);
327*9880d681SAndroid Build Coastguard Worker if (IsX86 && Name == "sse41.mpsadbw")
328*9880d681SAndroid Build Coastguard Worker return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
329*9880d681SAndroid Build Coastguard Worker NewFn);
330*9880d681SAndroid Build Coastguard Worker if (IsX86 && Name == "avx.dp.ps.256")
331*9880d681SAndroid Build Coastguard Worker return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
332*9880d681SAndroid Build Coastguard Worker NewFn);
333*9880d681SAndroid Build Coastguard Worker if (IsX86 && Name == "avx2.mpsadbw")
334*9880d681SAndroid Build Coastguard Worker return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
335*9880d681SAndroid Build Coastguard Worker NewFn);
336*9880d681SAndroid Build Coastguard Worker
337*9880d681SAndroid Build Coastguard Worker // frcz.ss/sd may need to have an argument dropped
338*9880d681SAndroid Build Coastguard Worker if (IsX86 && Name.startswith("xop.vfrcz.ss") && F->arg_size() == 2) {
339*9880d681SAndroid Build Coastguard Worker F->setName(Name + ".old");
340*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(),
341*9880d681SAndroid Build Coastguard Worker Intrinsic::x86_xop_vfrcz_ss);
342*9880d681SAndroid Build Coastguard Worker return true;
343*9880d681SAndroid Build Coastguard Worker }
344*9880d681SAndroid Build Coastguard Worker if (IsX86 && Name.startswith("xop.vfrcz.sd") && F->arg_size() == 2) {
345*9880d681SAndroid Build Coastguard Worker F->setName(Name + ".old");
346*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(),
347*9880d681SAndroid Build Coastguard Worker Intrinsic::x86_xop_vfrcz_sd);
348*9880d681SAndroid Build Coastguard Worker return true;
349*9880d681SAndroid Build Coastguard Worker }
350*9880d681SAndroid Build Coastguard Worker if (IsX86 && (Name.startswith("avx512.mask.pslli.") ||
351*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.psrai.") ||
352*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.psrli."))) {
353*9880d681SAndroid Build Coastguard Worker Intrinsic::ID ShiftID;
354*9880d681SAndroid Build Coastguard Worker if (Name.slice(12, 16) == "psll")
355*9880d681SAndroid Build Coastguard Worker ShiftID = Name[18] == 'd' ? Intrinsic::x86_avx512_mask_psll_di_512
356*9880d681SAndroid Build Coastguard Worker : Intrinsic::x86_avx512_mask_psll_qi_512;
357*9880d681SAndroid Build Coastguard Worker else if (Name.slice(12, 16) == "psra")
358*9880d681SAndroid Build Coastguard Worker ShiftID = Name[18] == 'd' ? Intrinsic::x86_avx512_mask_psra_di_512
359*9880d681SAndroid Build Coastguard Worker : Intrinsic::x86_avx512_mask_psra_qi_512;
360*9880d681SAndroid Build Coastguard Worker else
361*9880d681SAndroid Build Coastguard Worker ShiftID = Name[18] == 'd' ? Intrinsic::x86_avx512_mask_psrl_di_512
362*9880d681SAndroid Build Coastguard Worker : Intrinsic::x86_avx512_mask_psrl_qi_512;
363*9880d681SAndroid Build Coastguard Worker F->setName("llvm.x86." + Name + ".old");
364*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(), ShiftID);
365*9880d681SAndroid Build Coastguard Worker return true;
366*9880d681SAndroid Build Coastguard Worker }
367*9880d681SAndroid Build Coastguard Worker // Fix the FMA4 intrinsics to remove the 4
368*9880d681SAndroid Build Coastguard Worker if (IsX86 && Name.startswith("fma4.")) {
369*9880d681SAndroid Build Coastguard Worker F->setName("llvm.x86.fma" + Name.substr(5));
370*9880d681SAndroid Build Coastguard Worker NewFn = F;
371*9880d681SAndroid Build Coastguard Worker return true;
372*9880d681SAndroid Build Coastguard Worker }
373*9880d681SAndroid Build Coastguard Worker // Upgrade any XOP PERMIL2 index operand still using a float/double vector.
374*9880d681SAndroid Build Coastguard Worker if (IsX86 && Name.startswith("xop.vpermil2")) {
375*9880d681SAndroid Build Coastguard Worker auto Params = F->getFunctionType()->params();
376*9880d681SAndroid Build Coastguard Worker auto Idx = Params[2];
377*9880d681SAndroid Build Coastguard Worker if (Idx->getScalarType()->isFloatingPointTy()) {
378*9880d681SAndroid Build Coastguard Worker F->setName("llvm.x86." + Name + ".old");
379*9880d681SAndroid Build Coastguard Worker unsigned IdxSize = Idx->getPrimitiveSizeInBits();
380*9880d681SAndroid Build Coastguard Worker unsigned EltSize = Idx->getScalarSizeInBits();
381*9880d681SAndroid Build Coastguard Worker Intrinsic::ID Permil2ID;
382*9880d681SAndroid Build Coastguard Worker if (EltSize == 64 && IdxSize == 128)
383*9880d681SAndroid Build Coastguard Worker Permil2ID = Intrinsic::x86_xop_vpermil2pd;
384*9880d681SAndroid Build Coastguard Worker else if (EltSize == 32 && IdxSize == 128)
385*9880d681SAndroid Build Coastguard Worker Permil2ID = Intrinsic::x86_xop_vpermil2ps;
386*9880d681SAndroid Build Coastguard Worker else if (EltSize == 64 && IdxSize == 256)
387*9880d681SAndroid Build Coastguard Worker Permil2ID = Intrinsic::x86_xop_vpermil2pd_256;
388*9880d681SAndroid Build Coastguard Worker else
389*9880d681SAndroid Build Coastguard Worker Permil2ID = Intrinsic::x86_xop_vpermil2ps_256;
390*9880d681SAndroid Build Coastguard Worker NewFn = Intrinsic::getDeclaration(F->getParent(), Permil2ID);
391*9880d681SAndroid Build Coastguard Worker return true;
392*9880d681SAndroid Build Coastguard Worker }
393*9880d681SAndroid Build Coastguard Worker }
394*9880d681SAndroid Build Coastguard Worker break;
395*9880d681SAndroid Build Coastguard Worker }
396*9880d681SAndroid Build Coastguard Worker }
397*9880d681SAndroid Build Coastguard Worker
398*9880d681SAndroid Build Coastguard Worker // This may not belong here. This function is effectively being overloaded
399*9880d681SAndroid Build Coastguard Worker // to both detect an intrinsic which needs upgrading, and to provide the
400*9880d681SAndroid Build Coastguard Worker // upgraded form of the intrinsic. We should perhaps have two separate
401*9880d681SAndroid Build Coastguard Worker // functions for this.
402*9880d681SAndroid Build Coastguard Worker return false;
403*9880d681SAndroid Build Coastguard Worker }
404*9880d681SAndroid Build Coastguard Worker
UpgradeIntrinsicFunction(Function * F,Function * & NewFn)405*9880d681SAndroid Build Coastguard Worker bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
406*9880d681SAndroid Build Coastguard Worker NewFn = nullptr;
407*9880d681SAndroid Build Coastguard Worker bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
408*9880d681SAndroid Build Coastguard Worker assert(F != NewFn && "Intrinsic function upgraded to the same function");
409*9880d681SAndroid Build Coastguard Worker
410*9880d681SAndroid Build Coastguard Worker // Upgrade intrinsic attributes. This does not change the function.
411*9880d681SAndroid Build Coastguard Worker if (NewFn)
412*9880d681SAndroid Build Coastguard Worker F = NewFn;
413*9880d681SAndroid Build Coastguard Worker if (Intrinsic::ID id = F->getIntrinsicID())
414*9880d681SAndroid Build Coastguard Worker F->setAttributes(Intrinsic::getAttributes(F->getContext(), id));
415*9880d681SAndroid Build Coastguard Worker return Upgraded;
416*9880d681SAndroid Build Coastguard Worker }
417*9880d681SAndroid Build Coastguard Worker
UpgradeGlobalVariable(GlobalVariable * GV)418*9880d681SAndroid Build Coastguard Worker bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
419*9880d681SAndroid Build Coastguard Worker // Nothing to do yet.
420*9880d681SAndroid Build Coastguard Worker return false;
421*9880d681SAndroid Build Coastguard Worker }
422*9880d681SAndroid Build Coastguard Worker
423*9880d681SAndroid Build Coastguard Worker // Handles upgrading SSE2/AVX2/AVX512BW PSLLDQ intrinsics by converting them
424*9880d681SAndroid Build Coastguard Worker // to byte shuffles.
UpgradeX86PSLLDQIntrinsics(IRBuilder<> & Builder,Value * Op,unsigned Shift)425*9880d681SAndroid Build Coastguard Worker static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder,
426*9880d681SAndroid Build Coastguard Worker Value *Op, unsigned Shift) {
427*9880d681SAndroid Build Coastguard Worker Type *ResultTy = Op->getType();
428*9880d681SAndroid Build Coastguard Worker unsigned NumElts = ResultTy->getVectorNumElements() * 8;
429*9880d681SAndroid Build Coastguard Worker
430*9880d681SAndroid Build Coastguard Worker // Bitcast from a 64-bit element type to a byte element type.
431*9880d681SAndroid Build Coastguard Worker Type *VecTy = VectorType::get(Builder.getInt8Ty(), NumElts);
432*9880d681SAndroid Build Coastguard Worker Op = Builder.CreateBitCast(Op, VecTy, "cast");
433*9880d681SAndroid Build Coastguard Worker
434*9880d681SAndroid Build Coastguard Worker // We'll be shuffling in zeroes.
435*9880d681SAndroid Build Coastguard Worker Value *Res = Constant::getNullValue(VecTy);
436*9880d681SAndroid Build Coastguard Worker
437*9880d681SAndroid Build Coastguard Worker // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
438*9880d681SAndroid Build Coastguard Worker // we'll just return the zero vector.
439*9880d681SAndroid Build Coastguard Worker if (Shift < 16) {
440*9880d681SAndroid Build Coastguard Worker uint32_t Idxs[64];
441*9880d681SAndroid Build Coastguard Worker // 256/512-bit version is split into 2/4 16-byte lanes.
442*9880d681SAndroid Build Coastguard Worker for (unsigned l = 0; l != NumElts; l += 16)
443*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != 16; ++i) {
444*9880d681SAndroid Build Coastguard Worker unsigned Idx = NumElts + i - Shift;
445*9880d681SAndroid Build Coastguard Worker if (Idx < NumElts)
446*9880d681SAndroid Build Coastguard Worker Idx -= NumElts - 16; // end of lane, switch operand.
447*9880d681SAndroid Build Coastguard Worker Idxs[l + i] = Idx + l;
448*9880d681SAndroid Build Coastguard Worker }
449*9880d681SAndroid Build Coastguard Worker
450*9880d681SAndroid Build Coastguard Worker Res = Builder.CreateShuffleVector(Res, Op, makeArrayRef(Idxs, NumElts));
451*9880d681SAndroid Build Coastguard Worker }
452*9880d681SAndroid Build Coastguard Worker
453*9880d681SAndroid Build Coastguard Worker // Bitcast back to a 64-bit element type.
454*9880d681SAndroid Build Coastguard Worker return Builder.CreateBitCast(Res, ResultTy, "cast");
455*9880d681SAndroid Build Coastguard Worker }
456*9880d681SAndroid Build Coastguard Worker
457*9880d681SAndroid Build Coastguard Worker // Handles upgrading SSE2/AVX2/AVX512BW PSRLDQ intrinsics by converting them
458*9880d681SAndroid Build Coastguard Worker // to byte shuffles.
UpgradeX86PSRLDQIntrinsics(IRBuilder<> & Builder,Value * Op,unsigned Shift)459*9880d681SAndroid Build Coastguard Worker static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, Value *Op,
460*9880d681SAndroid Build Coastguard Worker unsigned Shift) {
461*9880d681SAndroid Build Coastguard Worker Type *ResultTy = Op->getType();
462*9880d681SAndroid Build Coastguard Worker unsigned NumElts = ResultTy->getVectorNumElements() * 8;
463*9880d681SAndroid Build Coastguard Worker
464*9880d681SAndroid Build Coastguard Worker // Bitcast from a 64-bit element type to a byte element type.
465*9880d681SAndroid Build Coastguard Worker Type *VecTy = VectorType::get(Builder.getInt8Ty(), NumElts);
466*9880d681SAndroid Build Coastguard Worker Op = Builder.CreateBitCast(Op, VecTy, "cast");
467*9880d681SAndroid Build Coastguard Worker
468*9880d681SAndroid Build Coastguard Worker // We'll be shuffling in zeroes.
469*9880d681SAndroid Build Coastguard Worker Value *Res = Constant::getNullValue(VecTy);
470*9880d681SAndroid Build Coastguard Worker
471*9880d681SAndroid Build Coastguard Worker // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
472*9880d681SAndroid Build Coastguard Worker // we'll just return the zero vector.
473*9880d681SAndroid Build Coastguard Worker if (Shift < 16) {
474*9880d681SAndroid Build Coastguard Worker uint32_t Idxs[64];
475*9880d681SAndroid Build Coastguard Worker // 256/512-bit version is split into 2/4 16-byte lanes.
476*9880d681SAndroid Build Coastguard Worker for (unsigned l = 0; l != NumElts; l += 16)
477*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != 16; ++i) {
478*9880d681SAndroid Build Coastguard Worker unsigned Idx = i + Shift;
479*9880d681SAndroid Build Coastguard Worker if (Idx >= 16)
480*9880d681SAndroid Build Coastguard Worker Idx += NumElts - 16; // end of lane, switch operand.
481*9880d681SAndroid Build Coastguard Worker Idxs[l + i] = Idx + l;
482*9880d681SAndroid Build Coastguard Worker }
483*9880d681SAndroid Build Coastguard Worker
484*9880d681SAndroid Build Coastguard Worker Res = Builder.CreateShuffleVector(Op, Res, makeArrayRef(Idxs, NumElts));
485*9880d681SAndroid Build Coastguard Worker }
486*9880d681SAndroid Build Coastguard Worker
487*9880d681SAndroid Build Coastguard Worker // Bitcast back to a 64-bit element type.
488*9880d681SAndroid Build Coastguard Worker return Builder.CreateBitCast(Res, ResultTy, "cast");
489*9880d681SAndroid Build Coastguard Worker }
490*9880d681SAndroid Build Coastguard Worker
getX86MaskVec(IRBuilder<> & Builder,Value * Mask,unsigned NumElts)491*9880d681SAndroid Build Coastguard Worker static Value *getX86MaskVec(IRBuilder<> &Builder, Value *Mask,
492*9880d681SAndroid Build Coastguard Worker unsigned NumElts) {
493*9880d681SAndroid Build Coastguard Worker llvm::VectorType *MaskTy = llvm::VectorType::get(Builder.getInt1Ty(),
494*9880d681SAndroid Build Coastguard Worker cast<IntegerType>(Mask->getType())->getBitWidth());
495*9880d681SAndroid Build Coastguard Worker Mask = Builder.CreateBitCast(Mask, MaskTy);
496*9880d681SAndroid Build Coastguard Worker
497*9880d681SAndroid Build Coastguard Worker // If we have less than 8 elements, then the starting mask was an i8 and
498*9880d681SAndroid Build Coastguard Worker // we need to extract down to the right number of elements.
499*9880d681SAndroid Build Coastguard Worker if (NumElts < 8) {
500*9880d681SAndroid Build Coastguard Worker uint32_t Indices[4];
501*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElts; ++i)
502*9880d681SAndroid Build Coastguard Worker Indices[i] = i;
503*9880d681SAndroid Build Coastguard Worker Mask = Builder.CreateShuffleVector(Mask, Mask,
504*9880d681SAndroid Build Coastguard Worker makeArrayRef(Indices, NumElts),
505*9880d681SAndroid Build Coastguard Worker "extract");
506*9880d681SAndroid Build Coastguard Worker }
507*9880d681SAndroid Build Coastguard Worker
508*9880d681SAndroid Build Coastguard Worker return Mask;
509*9880d681SAndroid Build Coastguard Worker }
510*9880d681SAndroid Build Coastguard Worker
EmitX86Select(IRBuilder<> & Builder,Value * Mask,Value * Op0,Value * Op1)511*9880d681SAndroid Build Coastguard Worker static Value *EmitX86Select(IRBuilder<> &Builder, Value *Mask,
512*9880d681SAndroid Build Coastguard Worker Value *Op0, Value *Op1) {
513*9880d681SAndroid Build Coastguard Worker // If the mask is all ones just emit the align operation.
514*9880d681SAndroid Build Coastguard Worker if (const auto *C = dyn_cast<Constant>(Mask))
515*9880d681SAndroid Build Coastguard Worker if (C->isAllOnesValue())
516*9880d681SAndroid Build Coastguard Worker return Op0;
517*9880d681SAndroid Build Coastguard Worker
518*9880d681SAndroid Build Coastguard Worker Mask = getX86MaskVec(Builder, Mask, Op0->getType()->getVectorNumElements());
519*9880d681SAndroid Build Coastguard Worker return Builder.CreateSelect(Mask, Op0, Op1);
520*9880d681SAndroid Build Coastguard Worker }
521*9880d681SAndroid Build Coastguard Worker
UpgradeX86PALIGNRIntrinsics(IRBuilder<> & Builder,Value * Op0,Value * Op1,Value * Shift,Value * Passthru,Value * Mask)522*9880d681SAndroid Build Coastguard Worker static Value *UpgradeX86PALIGNRIntrinsics(IRBuilder<> &Builder,
523*9880d681SAndroid Build Coastguard Worker Value *Op0, Value *Op1, Value *Shift,
524*9880d681SAndroid Build Coastguard Worker Value *Passthru, Value *Mask) {
525*9880d681SAndroid Build Coastguard Worker unsigned ShiftVal = cast<llvm::ConstantInt>(Shift)->getZExtValue();
526*9880d681SAndroid Build Coastguard Worker
527*9880d681SAndroid Build Coastguard Worker unsigned NumElts = Op0->getType()->getVectorNumElements();
528*9880d681SAndroid Build Coastguard Worker assert(NumElts % 16 == 0);
529*9880d681SAndroid Build Coastguard Worker
530*9880d681SAndroid Build Coastguard Worker // If palignr is shifting the pair of vectors more than the size of two
531*9880d681SAndroid Build Coastguard Worker // lanes, emit zero.
532*9880d681SAndroid Build Coastguard Worker if (ShiftVal >= 32)
533*9880d681SAndroid Build Coastguard Worker return llvm::Constant::getNullValue(Op0->getType());
534*9880d681SAndroid Build Coastguard Worker
535*9880d681SAndroid Build Coastguard Worker // If palignr is shifting the pair of input vectors more than one lane,
536*9880d681SAndroid Build Coastguard Worker // but less than two lanes, convert to shifting in zeroes.
537*9880d681SAndroid Build Coastguard Worker if (ShiftVal > 16) {
538*9880d681SAndroid Build Coastguard Worker ShiftVal -= 16;
539*9880d681SAndroid Build Coastguard Worker Op1 = Op0;
540*9880d681SAndroid Build Coastguard Worker Op0 = llvm::Constant::getNullValue(Op0->getType());
541*9880d681SAndroid Build Coastguard Worker }
542*9880d681SAndroid Build Coastguard Worker
543*9880d681SAndroid Build Coastguard Worker uint32_t Indices[64];
544*9880d681SAndroid Build Coastguard Worker // 256-bit palignr operates on 128-bit lanes so we need to handle that
545*9880d681SAndroid Build Coastguard Worker for (unsigned l = 0; l != NumElts; l += 16) {
546*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != 16; ++i) {
547*9880d681SAndroid Build Coastguard Worker unsigned Idx = ShiftVal + i;
548*9880d681SAndroid Build Coastguard Worker if (Idx >= 16)
549*9880d681SAndroid Build Coastguard Worker Idx += NumElts - 16; // End of lane, switch operand.
550*9880d681SAndroid Build Coastguard Worker Indices[l + i] = Idx + l;
551*9880d681SAndroid Build Coastguard Worker }
552*9880d681SAndroid Build Coastguard Worker }
553*9880d681SAndroid Build Coastguard Worker
554*9880d681SAndroid Build Coastguard Worker Value *Align = Builder.CreateShuffleVector(Op1, Op0,
555*9880d681SAndroid Build Coastguard Worker makeArrayRef(Indices, NumElts),
556*9880d681SAndroid Build Coastguard Worker "palignr");
557*9880d681SAndroid Build Coastguard Worker
558*9880d681SAndroid Build Coastguard Worker return EmitX86Select(Builder, Mask, Align, Passthru);
559*9880d681SAndroid Build Coastguard Worker }
560*9880d681SAndroid Build Coastguard Worker
UpgradeMaskedStore(IRBuilder<> & Builder,Value * Ptr,Value * Data,Value * Mask,bool Aligned)561*9880d681SAndroid Build Coastguard Worker static Value *UpgradeMaskedStore(IRBuilder<> &Builder,
562*9880d681SAndroid Build Coastguard Worker Value *Ptr, Value *Data, Value *Mask,
563*9880d681SAndroid Build Coastguard Worker bool Aligned) {
564*9880d681SAndroid Build Coastguard Worker // Cast the pointer to the right type.
565*9880d681SAndroid Build Coastguard Worker Ptr = Builder.CreateBitCast(Ptr,
566*9880d681SAndroid Build Coastguard Worker llvm::PointerType::getUnqual(Data->getType()));
567*9880d681SAndroid Build Coastguard Worker unsigned Align =
568*9880d681SAndroid Build Coastguard Worker Aligned ? cast<VectorType>(Data->getType())->getBitWidth() / 8 : 1;
569*9880d681SAndroid Build Coastguard Worker
570*9880d681SAndroid Build Coastguard Worker // If the mask is all ones just emit a regular store.
571*9880d681SAndroid Build Coastguard Worker if (const auto *C = dyn_cast<Constant>(Mask))
572*9880d681SAndroid Build Coastguard Worker if (C->isAllOnesValue())
573*9880d681SAndroid Build Coastguard Worker return Builder.CreateAlignedStore(Data, Ptr, Align);
574*9880d681SAndroid Build Coastguard Worker
575*9880d681SAndroid Build Coastguard Worker // Convert the mask from an integer type to a vector of i1.
576*9880d681SAndroid Build Coastguard Worker unsigned NumElts = Data->getType()->getVectorNumElements();
577*9880d681SAndroid Build Coastguard Worker Mask = getX86MaskVec(Builder, Mask, NumElts);
578*9880d681SAndroid Build Coastguard Worker return Builder.CreateMaskedStore(Data, Ptr, Align, Mask);
579*9880d681SAndroid Build Coastguard Worker }
580*9880d681SAndroid Build Coastguard Worker
UpgradeMaskedLoad(IRBuilder<> & Builder,Value * Ptr,Value * Passthru,Value * Mask,bool Aligned)581*9880d681SAndroid Build Coastguard Worker static Value *UpgradeMaskedLoad(IRBuilder<> &Builder,
582*9880d681SAndroid Build Coastguard Worker Value *Ptr, Value *Passthru, Value *Mask,
583*9880d681SAndroid Build Coastguard Worker bool Aligned) {
584*9880d681SAndroid Build Coastguard Worker // Cast the pointer to the right type.
585*9880d681SAndroid Build Coastguard Worker Ptr = Builder.CreateBitCast(Ptr,
586*9880d681SAndroid Build Coastguard Worker llvm::PointerType::getUnqual(Passthru->getType()));
587*9880d681SAndroid Build Coastguard Worker unsigned Align =
588*9880d681SAndroid Build Coastguard Worker Aligned ? cast<VectorType>(Passthru->getType())->getBitWidth() / 8 : 1;
589*9880d681SAndroid Build Coastguard Worker
590*9880d681SAndroid Build Coastguard Worker // If the mask is all ones just emit a regular store.
591*9880d681SAndroid Build Coastguard Worker if (const auto *C = dyn_cast<Constant>(Mask))
592*9880d681SAndroid Build Coastguard Worker if (C->isAllOnesValue())
593*9880d681SAndroid Build Coastguard Worker return Builder.CreateAlignedLoad(Ptr, Align);
594*9880d681SAndroid Build Coastguard Worker
595*9880d681SAndroid Build Coastguard Worker // Convert the mask from an integer type to a vector of i1.
596*9880d681SAndroid Build Coastguard Worker unsigned NumElts = Passthru->getType()->getVectorNumElements();
597*9880d681SAndroid Build Coastguard Worker Mask = getX86MaskVec(Builder, Mask, NumElts);
598*9880d681SAndroid Build Coastguard Worker return Builder.CreateMaskedLoad(Ptr, Align, Mask, Passthru);
599*9880d681SAndroid Build Coastguard Worker }
600*9880d681SAndroid Build Coastguard Worker
upgradeIntMinMax(IRBuilder<> & Builder,CallInst & CI,ICmpInst::Predicate Pred)601*9880d681SAndroid Build Coastguard Worker static Value *upgradeIntMinMax(IRBuilder<> &Builder, CallInst &CI,
602*9880d681SAndroid Build Coastguard Worker ICmpInst::Predicate Pred) {
603*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI.getArgOperand(0);
604*9880d681SAndroid Build Coastguard Worker Value *Op1 = CI.getArgOperand(1);
605*9880d681SAndroid Build Coastguard Worker Value *Cmp = Builder.CreateICmp(Pred, Op0, Op1);
606*9880d681SAndroid Build Coastguard Worker return Builder.CreateSelect(Cmp, Op0, Op1);
607*9880d681SAndroid Build Coastguard Worker }
608*9880d681SAndroid Build Coastguard Worker
upgradeMaskedCompare(IRBuilder<> & Builder,CallInst & CI,ICmpInst::Predicate Pred)609*9880d681SAndroid Build Coastguard Worker static Value *upgradeMaskedCompare(IRBuilder<> &Builder, CallInst &CI,
610*9880d681SAndroid Build Coastguard Worker ICmpInst::Predicate Pred) {
611*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI.getArgOperand(0);
612*9880d681SAndroid Build Coastguard Worker unsigned NumElts = Op0->getType()->getVectorNumElements();
613*9880d681SAndroid Build Coastguard Worker Value *Cmp = Builder.CreateICmp(Pred, Op0, CI.getArgOperand(1));
614*9880d681SAndroid Build Coastguard Worker
615*9880d681SAndroid Build Coastguard Worker Value *Mask = CI.getArgOperand(2);
616*9880d681SAndroid Build Coastguard Worker const auto *C = dyn_cast<Constant>(Mask);
617*9880d681SAndroid Build Coastguard Worker if (!C || !C->isAllOnesValue())
618*9880d681SAndroid Build Coastguard Worker Cmp = Builder.CreateAnd(Cmp, getX86MaskVec(Builder, Mask, NumElts));
619*9880d681SAndroid Build Coastguard Worker
620*9880d681SAndroid Build Coastguard Worker if (NumElts < 8) {
621*9880d681SAndroid Build Coastguard Worker uint32_t Indices[8];
622*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElts; ++i)
623*9880d681SAndroid Build Coastguard Worker Indices[i] = i;
624*9880d681SAndroid Build Coastguard Worker for (unsigned i = NumElts; i != 8; ++i)
625*9880d681SAndroid Build Coastguard Worker Indices[i] = NumElts + i % NumElts;
626*9880d681SAndroid Build Coastguard Worker Cmp = Builder.CreateShuffleVector(Cmp,
627*9880d681SAndroid Build Coastguard Worker Constant::getNullValue(Cmp->getType()),
628*9880d681SAndroid Build Coastguard Worker Indices);
629*9880d681SAndroid Build Coastguard Worker }
630*9880d681SAndroid Build Coastguard Worker return Builder.CreateBitCast(Cmp, IntegerType::get(CI.getContext(),
631*9880d681SAndroid Build Coastguard Worker std::max(NumElts, 8U)));
632*9880d681SAndroid Build Coastguard Worker }
633*9880d681SAndroid Build Coastguard Worker
634*9880d681SAndroid Build Coastguard Worker /// Upgrade a call to an old intrinsic. All argument and return casting must be
635*9880d681SAndroid Build Coastguard Worker /// provided to seamlessly integrate with existing context.
UpgradeIntrinsicCall(CallInst * CI,Function * NewFn)636*9880d681SAndroid Build Coastguard Worker void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
637*9880d681SAndroid Build Coastguard Worker Function *F = CI->getCalledFunction();
638*9880d681SAndroid Build Coastguard Worker LLVMContext &C = CI->getContext();
639*9880d681SAndroid Build Coastguard Worker IRBuilder<> Builder(C);
640*9880d681SAndroid Build Coastguard Worker Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
641*9880d681SAndroid Build Coastguard Worker
642*9880d681SAndroid Build Coastguard Worker assert(F && "Intrinsic call is not direct?");
643*9880d681SAndroid Build Coastguard Worker
644*9880d681SAndroid Build Coastguard Worker if (!NewFn) {
645*9880d681SAndroid Build Coastguard Worker // Get the Function's name.
646*9880d681SAndroid Build Coastguard Worker StringRef Name = F->getName();
647*9880d681SAndroid Build Coastguard Worker
648*9880d681SAndroid Build Coastguard Worker assert(Name.startswith("llvm.") && "Intrinsic doesn't start with 'llvm.'");
649*9880d681SAndroid Build Coastguard Worker Name = Name.substr(5);
650*9880d681SAndroid Build Coastguard Worker
651*9880d681SAndroid Build Coastguard Worker bool IsX86 = Name.startswith("x86.");
652*9880d681SAndroid Build Coastguard Worker if (IsX86)
653*9880d681SAndroid Build Coastguard Worker Name = Name.substr(4);
654*9880d681SAndroid Build Coastguard Worker
655*9880d681SAndroid Build Coastguard Worker Value *Rep;
656*9880d681SAndroid Build Coastguard Worker // Upgrade packed integer vector compare intrinsics to compare instructions.
657*9880d681SAndroid Build Coastguard Worker if (IsX86 && (Name.startswith("sse2.pcmpeq.") ||
658*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pcmpeq."))) {
659*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
660*9880d681SAndroid Build Coastguard Worker "pcmpeq");
661*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateSExt(Rep, CI->getType(), "");
662*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("sse2.pcmpgt.") ||
663*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pcmpgt."))) {
664*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
665*9880d681SAndroid Build Coastguard Worker "pcmpgt");
666*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateSExt(Rep, CI->getType(), "");
667*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name.startswith("avx512.mask.pcmpeq.")) {
668*9880d681SAndroid Build Coastguard Worker Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_EQ);
669*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name.startswith("avx512.mask.pcmpgt.")) {
670*9880d681SAndroid Build Coastguard Worker Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_SGT);
671*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse41.pmaxsb" ||
672*9880d681SAndroid Build Coastguard Worker Name == "sse2.pmaxs.w" ||
673*9880d681SAndroid Build Coastguard Worker Name == "sse41.pmaxsd" ||
674*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pmaxs"))) {
675*9880d681SAndroid Build Coastguard Worker Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SGT);
676*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse2.pmaxu.b" ||
677*9880d681SAndroid Build Coastguard Worker Name == "sse41.pmaxuw" ||
678*9880d681SAndroid Build Coastguard Worker Name == "sse41.pmaxud" ||
679*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pmaxu"))) {
680*9880d681SAndroid Build Coastguard Worker Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_UGT);
681*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse41.pminsb" ||
682*9880d681SAndroid Build Coastguard Worker Name == "sse2.pmins.w" ||
683*9880d681SAndroid Build Coastguard Worker Name == "sse41.pminsd" ||
684*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pmins"))) {
685*9880d681SAndroid Build Coastguard Worker Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SLT);
686*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse2.pminu.b" ||
687*9880d681SAndroid Build Coastguard Worker Name == "sse41.pminuw" ||
688*9880d681SAndroid Build Coastguard Worker Name == "sse41.pminud" ||
689*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pminu"))) {
690*9880d681SAndroid Build Coastguard Worker Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_ULT);
691*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse2.cvtdq2pd" ||
692*9880d681SAndroid Build Coastguard Worker Name == "sse2.cvtps2pd" ||
693*9880d681SAndroid Build Coastguard Worker Name == "avx.cvtdq2.pd.256" ||
694*9880d681SAndroid Build Coastguard Worker Name == "avx.cvt.ps2.pd.256")) {
695*9880d681SAndroid Build Coastguard Worker // Lossless i32/float to double conversion.
696*9880d681SAndroid Build Coastguard Worker // Extract the bottom elements if necessary and convert to double vector.
697*9880d681SAndroid Build Coastguard Worker Value *Src = CI->getArgOperand(0);
698*9880d681SAndroid Build Coastguard Worker VectorType *SrcTy = cast<VectorType>(Src->getType());
699*9880d681SAndroid Build Coastguard Worker VectorType *DstTy = cast<VectorType>(CI->getType());
700*9880d681SAndroid Build Coastguard Worker Rep = CI->getArgOperand(0);
701*9880d681SAndroid Build Coastguard Worker
702*9880d681SAndroid Build Coastguard Worker unsigned NumDstElts = DstTy->getNumElements();
703*9880d681SAndroid Build Coastguard Worker if (NumDstElts < SrcTy->getNumElements()) {
704*9880d681SAndroid Build Coastguard Worker assert(NumDstElts == 2 && "Unexpected vector size");
705*9880d681SAndroid Build Coastguard Worker uint32_t ShuffleMask[2] = { 0, 1 };
706*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Rep, UndefValue::get(SrcTy),
707*9880d681SAndroid Build Coastguard Worker ShuffleMask);
708*9880d681SAndroid Build Coastguard Worker }
709*9880d681SAndroid Build Coastguard Worker
710*9880d681SAndroid Build Coastguard Worker bool Int2Double = (StringRef::npos != Name.find("cvtdq2"));
711*9880d681SAndroid Build Coastguard Worker if (Int2Double)
712*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateSIToFP(Rep, DstTy, "cvtdq2pd");
713*9880d681SAndroid Build Coastguard Worker else
714*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd");
715*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse2.cvttps2dq" ||
716*9880d681SAndroid Build Coastguard Worker Name.startswith("avx.cvtt."))) {
717*9880d681SAndroid Build Coastguard Worker // Truncation (round to zero) float/double to i32 vector conversion.
718*9880d681SAndroid Build Coastguard Worker Value *Src = CI->getArgOperand(0);
719*9880d681SAndroid Build Coastguard Worker VectorType *DstTy = cast<VectorType>(CI->getType());
720*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateFPToSI(Src, DstTy, "cvtt");
721*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name.startswith("sse4a.movnt.")) {
722*9880d681SAndroid Build Coastguard Worker Module *M = F->getParent();
723*9880d681SAndroid Build Coastguard Worker SmallVector<Metadata *, 1> Elts;
724*9880d681SAndroid Build Coastguard Worker Elts.push_back(
725*9880d681SAndroid Build Coastguard Worker ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
726*9880d681SAndroid Build Coastguard Worker MDNode *Node = MDNode::get(C, Elts);
727*9880d681SAndroid Build Coastguard Worker
728*9880d681SAndroid Build Coastguard Worker Value *Arg0 = CI->getArgOperand(0);
729*9880d681SAndroid Build Coastguard Worker Value *Arg1 = CI->getArgOperand(1);
730*9880d681SAndroid Build Coastguard Worker
731*9880d681SAndroid Build Coastguard Worker // Nontemporal (unaligned) store of the 0'th element of the float/double
732*9880d681SAndroid Build Coastguard Worker // vector.
733*9880d681SAndroid Build Coastguard Worker Type *SrcEltTy = cast<VectorType>(Arg1->getType())->getElementType();
734*9880d681SAndroid Build Coastguard Worker PointerType *EltPtrTy = PointerType::getUnqual(SrcEltTy);
735*9880d681SAndroid Build Coastguard Worker Value *Addr = Builder.CreateBitCast(Arg0, EltPtrTy, "cast");
736*9880d681SAndroid Build Coastguard Worker Value *Extract =
737*9880d681SAndroid Build Coastguard Worker Builder.CreateExtractElement(Arg1, (uint64_t)0, "extractelement");
738*9880d681SAndroid Build Coastguard Worker
739*9880d681SAndroid Build Coastguard Worker StoreInst *SI = Builder.CreateAlignedStore(Extract, Addr, 1);
740*9880d681SAndroid Build Coastguard Worker SI->setMetadata(M->getMDKindID("nontemporal"), Node);
741*9880d681SAndroid Build Coastguard Worker
742*9880d681SAndroid Build Coastguard Worker // Remove intrinsic.
743*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
744*9880d681SAndroid Build Coastguard Worker return;
745*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx.movnt.") ||
746*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.storent."))) {
747*9880d681SAndroid Build Coastguard Worker Module *M = F->getParent();
748*9880d681SAndroid Build Coastguard Worker SmallVector<Metadata *, 1> Elts;
749*9880d681SAndroid Build Coastguard Worker Elts.push_back(
750*9880d681SAndroid Build Coastguard Worker ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
751*9880d681SAndroid Build Coastguard Worker MDNode *Node = MDNode::get(C, Elts);
752*9880d681SAndroid Build Coastguard Worker
753*9880d681SAndroid Build Coastguard Worker Value *Arg0 = CI->getArgOperand(0);
754*9880d681SAndroid Build Coastguard Worker Value *Arg1 = CI->getArgOperand(1);
755*9880d681SAndroid Build Coastguard Worker
756*9880d681SAndroid Build Coastguard Worker // Convert the type of the pointer to a pointer to the stored type.
757*9880d681SAndroid Build Coastguard Worker Value *BC = Builder.CreateBitCast(Arg0,
758*9880d681SAndroid Build Coastguard Worker PointerType::getUnqual(Arg1->getType()),
759*9880d681SAndroid Build Coastguard Worker "cast");
760*9880d681SAndroid Build Coastguard Worker VectorType *VTy = cast<VectorType>(Arg1->getType());
761*9880d681SAndroid Build Coastguard Worker StoreInst *SI = Builder.CreateAlignedStore(Arg1, BC,
762*9880d681SAndroid Build Coastguard Worker VTy->getBitWidth() / 8);
763*9880d681SAndroid Build Coastguard Worker SI->setMetadata(M->getMDKindID("nontemporal"), Node);
764*9880d681SAndroid Build Coastguard Worker
765*9880d681SAndroid Build Coastguard Worker // Remove intrinsic.
766*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
767*9880d681SAndroid Build Coastguard Worker return;
768*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name == "sse2.storel.dq") {
769*9880d681SAndroid Build Coastguard Worker Value *Arg0 = CI->getArgOperand(0);
770*9880d681SAndroid Build Coastguard Worker Value *Arg1 = CI->getArgOperand(1);
771*9880d681SAndroid Build Coastguard Worker
772*9880d681SAndroid Build Coastguard Worker Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
773*9880d681SAndroid Build Coastguard Worker Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
774*9880d681SAndroid Build Coastguard Worker Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0);
775*9880d681SAndroid Build Coastguard Worker Value *BC = Builder.CreateBitCast(Arg0,
776*9880d681SAndroid Build Coastguard Worker PointerType::getUnqual(Elt->getType()),
777*9880d681SAndroid Build Coastguard Worker "cast");
778*9880d681SAndroid Build Coastguard Worker Builder.CreateAlignedStore(Elt, BC, 1);
779*9880d681SAndroid Build Coastguard Worker
780*9880d681SAndroid Build Coastguard Worker // Remove intrinsic.
781*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
782*9880d681SAndroid Build Coastguard Worker return;
783*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("sse.storeu.") ||
784*9880d681SAndroid Build Coastguard Worker Name.startswith("sse2.storeu.") ||
785*9880d681SAndroid Build Coastguard Worker Name.startswith("avx.storeu."))) {
786*9880d681SAndroid Build Coastguard Worker Value *Arg0 = CI->getArgOperand(0);
787*9880d681SAndroid Build Coastguard Worker Value *Arg1 = CI->getArgOperand(1);
788*9880d681SAndroid Build Coastguard Worker
789*9880d681SAndroid Build Coastguard Worker Arg0 = Builder.CreateBitCast(Arg0,
790*9880d681SAndroid Build Coastguard Worker PointerType::getUnqual(Arg1->getType()),
791*9880d681SAndroid Build Coastguard Worker "cast");
792*9880d681SAndroid Build Coastguard Worker Builder.CreateAlignedStore(Arg1, Arg0, 1);
793*9880d681SAndroid Build Coastguard Worker
794*9880d681SAndroid Build Coastguard Worker // Remove intrinsic.
795*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
796*9880d681SAndroid Build Coastguard Worker return;
797*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx512.mask.storeu.p") ||
798*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.storeu.b.") ||
799*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.storeu.w.") ||
800*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.storeu.d.") ||
801*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.storeu.q."))) {
802*9880d681SAndroid Build Coastguard Worker UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
803*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2), /*Aligned*/false);
804*9880d681SAndroid Build Coastguard Worker
805*9880d681SAndroid Build Coastguard Worker // Remove intrinsic.
806*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
807*9880d681SAndroid Build Coastguard Worker return;
808*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx512.mask.store.p") ||
809*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.store.b.") ||
810*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.store.w.") ||
811*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.store.d.") ||
812*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.store.q."))) {
813*9880d681SAndroid Build Coastguard Worker UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
814*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2), /*Aligned*/true);
815*9880d681SAndroid Build Coastguard Worker
816*9880d681SAndroid Build Coastguard Worker // Remove intrinsic.
817*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
818*9880d681SAndroid Build Coastguard Worker return;
819*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx512.mask.loadu.p") ||
820*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.loadu.b.") ||
821*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.loadu.w.") ||
822*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.loadu.d.") ||
823*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.loadu.q."))) {
824*9880d681SAndroid Build Coastguard Worker Rep = UpgradeMaskedLoad(Builder, CI->getArgOperand(0),
825*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(1), CI->getArgOperand(2),
826*9880d681SAndroid Build Coastguard Worker /*Aligned*/false);
827*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx512.mask.load.p") ||
828*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.load.b.") ||
829*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.load.w.") ||
830*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.load.d.") ||
831*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.load.q."))) {
832*9880d681SAndroid Build Coastguard Worker Rep = UpgradeMaskedLoad(Builder, CI->getArgOperand(0),
833*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(1),CI->getArgOperand(2),
834*9880d681SAndroid Build Coastguard Worker /*Aligned*/true);
835*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name.startswith("xop.vpcom")) {
836*9880d681SAndroid Build Coastguard Worker Intrinsic::ID intID;
837*9880d681SAndroid Build Coastguard Worker if (Name.endswith("ub"))
838*9880d681SAndroid Build Coastguard Worker intID = Intrinsic::x86_xop_vpcomub;
839*9880d681SAndroid Build Coastguard Worker else if (Name.endswith("uw"))
840*9880d681SAndroid Build Coastguard Worker intID = Intrinsic::x86_xop_vpcomuw;
841*9880d681SAndroid Build Coastguard Worker else if (Name.endswith("ud"))
842*9880d681SAndroid Build Coastguard Worker intID = Intrinsic::x86_xop_vpcomud;
843*9880d681SAndroid Build Coastguard Worker else if (Name.endswith("uq"))
844*9880d681SAndroid Build Coastguard Worker intID = Intrinsic::x86_xop_vpcomuq;
845*9880d681SAndroid Build Coastguard Worker else if (Name.endswith("b"))
846*9880d681SAndroid Build Coastguard Worker intID = Intrinsic::x86_xop_vpcomb;
847*9880d681SAndroid Build Coastguard Worker else if (Name.endswith("w"))
848*9880d681SAndroid Build Coastguard Worker intID = Intrinsic::x86_xop_vpcomw;
849*9880d681SAndroid Build Coastguard Worker else if (Name.endswith("d"))
850*9880d681SAndroid Build Coastguard Worker intID = Intrinsic::x86_xop_vpcomd;
851*9880d681SAndroid Build Coastguard Worker else if (Name.endswith("q"))
852*9880d681SAndroid Build Coastguard Worker intID = Intrinsic::x86_xop_vpcomq;
853*9880d681SAndroid Build Coastguard Worker else
854*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unknown suffix");
855*9880d681SAndroid Build Coastguard Worker
856*9880d681SAndroid Build Coastguard Worker Name = Name.substr(9); // strip off "xop.vpcom"
857*9880d681SAndroid Build Coastguard Worker unsigned Imm;
858*9880d681SAndroid Build Coastguard Worker if (Name.startswith("lt"))
859*9880d681SAndroid Build Coastguard Worker Imm = 0;
860*9880d681SAndroid Build Coastguard Worker else if (Name.startswith("le"))
861*9880d681SAndroid Build Coastguard Worker Imm = 1;
862*9880d681SAndroid Build Coastguard Worker else if (Name.startswith("gt"))
863*9880d681SAndroid Build Coastguard Worker Imm = 2;
864*9880d681SAndroid Build Coastguard Worker else if (Name.startswith("ge"))
865*9880d681SAndroid Build Coastguard Worker Imm = 3;
866*9880d681SAndroid Build Coastguard Worker else if (Name.startswith("eq"))
867*9880d681SAndroid Build Coastguard Worker Imm = 4;
868*9880d681SAndroid Build Coastguard Worker else if (Name.startswith("ne"))
869*9880d681SAndroid Build Coastguard Worker Imm = 5;
870*9880d681SAndroid Build Coastguard Worker else if (Name.startswith("false"))
871*9880d681SAndroid Build Coastguard Worker Imm = 6;
872*9880d681SAndroid Build Coastguard Worker else if (Name.startswith("true"))
873*9880d681SAndroid Build Coastguard Worker Imm = 7;
874*9880d681SAndroid Build Coastguard Worker else
875*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unknown condition");
876*9880d681SAndroid Build Coastguard Worker
877*9880d681SAndroid Build Coastguard Worker Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
878*9880d681SAndroid Build Coastguard Worker Rep =
879*9880d681SAndroid Build Coastguard Worker Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1),
880*9880d681SAndroid Build Coastguard Worker Builder.getInt8(Imm)});
881*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name == "xop.vpcmov") {
882*9880d681SAndroid Build Coastguard Worker Value *Arg0 = CI->getArgOperand(0);
883*9880d681SAndroid Build Coastguard Worker Value *Arg1 = CI->getArgOperand(1);
884*9880d681SAndroid Build Coastguard Worker Value *Sel = CI->getArgOperand(2);
885*9880d681SAndroid Build Coastguard Worker unsigned NumElts = CI->getType()->getVectorNumElements();
886*9880d681SAndroid Build Coastguard Worker Constant *MinusOne = ConstantVector::getSplat(NumElts, Builder.getInt64(-1));
887*9880d681SAndroid Build Coastguard Worker Value *NotSel = Builder.CreateXor(Sel, MinusOne);
888*9880d681SAndroid Build Coastguard Worker Value *Sel0 = Builder.CreateAnd(Arg0, Sel);
889*9880d681SAndroid Build Coastguard Worker Value *Sel1 = Builder.CreateAnd(Arg1, NotSel);
890*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateOr(Sel0, Sel1);
891*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name == "sse42.crc32.64.8") {
892*9880d681SAndroid Build Coastguard Worker Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
893*9880d681SAndroid Build Coastguard Worker Intrinsic::x86_sse42_crc32_32_8);
894*9880d681SAndroid Build Coastguard Worker Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
895*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)});
896*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateZExt(Rep, CI->getType(), "");
897*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name.startswith("avx.vbroadcast")) {
898*9880d681SAndroid Build Coastguard Worker // Replace broadcasts with a series of insertelements.
899*9880d681SAndroid Build Coastguard Worker Type *VecTy = CI->getType();
900*9880d681SAndroid Build Coastguard Worker Type *EltTy = VecTy->getVectorElementType();
901*9880d681SAndroid Build Coastguard Worker unsigned EltNum = VecTy->getVectorNumElements();
902*9880d681SAndroid Build Coastguard Worker Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
903*9880d681SAndroid Build Coastguard Worker EltTy->getPointerTo());
904*9880d681SAndroid Build Coastguard Worker Value *Load = Builder.CreateLoad(EltTy, Cast);
905*9880d681SAndroid Build Coastguard Worker Type *I32Ty = Type::getInt32Ty(C);
906*9880d681SAndroid Build Coastguard Worker Rep = UndefValue::get(VecTy);
907*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0; I < EltNum; ++I)
908*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateInsertElement(Rep, Load,
909*9880d681SAndroid Build Coastguard Worker ConstantInt::get(I32Ty, I));
910*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("sse41.pmovsx") ||
911*9880d681SAndroid Build Coastguard Worker Name.startswith("sse41.pmovzx") ||
912*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pmovsx") ||
913*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pmovzx"))) {
914*9880d681SAndroid Build Coastguard Worker VectorType *SrcTy = cast<VectorType>(CI->getArgOperand(0)->getType());
915*9880d681SAndroid Build Coastguard Worker VectorType *DstTy = cast<VectorType>(CI->getType());
916*9880d681SAndroid Build Coastguard Worker unsigned NumDstElts = DstTy->getNumElements();
917*9880d681SAndroid Build Coastguard Worker
918*9880d681SAndroid Build Coastguard Worker // Extract a subvector of the first NumDstElts lanes and sign/zero extend.
919*9880d681SAndroid Build Coastguard Worker SmallVector<uint32_t, 8> ShuffleMask(NumDstElts);
920*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumDstElts; ++i)
921*9880d681SAndroid Build Coastguard Worker ShuffleMask[i] = i;
922*9880d681SAndroid Build Coastguard Worker
923*9880d681SAndroid Build Coastguard Worker Value *SV = Builder.CreateShuffleVector(
924*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(0), UndefValue::get(SrcTy), ShuffleMask);
925*9880d681SAndroid Build Coastguard Worker
926*9880d681SAndroid Build Coastguard Worker bool DoSext = (StringRef::npos != Name.find("pmovsx"));
927*9880d681SAndroid Build Coastguard Worker Rep = DoSext ? Builder.CreateSExt(SV, DstTy)
928*9880d681SAndroid Build Coastguard Worker : Builder.CreateZExt(SV, DstTy);
929*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name == "avx2.vbroadcasti128") {
930*9880d681SAndroid Build Coastguard Worker // Replace vbroadcasts with a vector shuffle.
931*9880d681SAndroid Build Coastguard Worker Type *VT = VectorType::get(Type::getInt64Ty(C), 2);
932*9880d681SAndroid Build Coastguard Worker Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0),
933*9880d681SAndroid Build Coastguard Worker PointerType::getUnqual(VT));
934*9880d681SAndroid Build Coastguard Worker Value *Load = Builder.CreateLoad(VT, Op);
935*9880d681SAndroid Build Coastguard Worker uint32_t Idxs[4] = { 0, 1, 0, 1 };
936*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
937*9880d681SAndroid Build Coastguard Worker Idxs);
938*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx2.pbroadcast") ||
939*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.vbroadcast") ||
940*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.pbroadcast") ||
941*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.broadcast.s"))) {
942*9880d681SAndroid Build Coastguard Worker // Replace vp?broadcasts with a vector shuffle.
943*9880d681SAndroid Build Coastguard Worker Value *Op = CI->getArgOperand(0);
944*9880d681SAndroid Build Coastguard Worker unsigned NumElts = CI->getType()->getVectorNumElements();
945*9880d681SAndroid Build Coastguard Worker Type *MaskTy = VectorType::get(Type::getInt32Ty(C), NumElts);
946*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op, UndefValue::get(Op->getType()),
947*9880d681SAndroid Build Coastguard Worker Constant::getNullValue(MaskTy));
948*9880d681SAndroid Build Coastguard Worker
949*9880d681SAndroid Build Coastguard Worker if (CI->getNumArgOperands() == 3)
950*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
951*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(1));
952*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name.startswith("avx512.mask.palignr.")) {
953*9880d681SAndroid Build Coastguard Worker Rep = UpgradeX86PALIGNRIntrinsics(Builder, CI->getArgOperand(0),
954*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(1),
955*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2),
956*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(3),
957*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(4));
958*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse2.psll.dq" ||
959*9880d681SAndroid Build Coastguard Worker Name == "avx2.psll.dq")) {
960*9880d681SAndroid Build Coastguard Worker // 128/256-bit shift left specified in bits.
961*9880d681SAndroid Build Coastguard Worker unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
962*9880d681SAndroid Build Coastguard Worker Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0),
963*9880d681SAndroid Build Coastguard Worker Shift / 8); // Shift is in bits.
964*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse2.psrl.dq" ||
965*9880d681SAndroid Build Coastguard Worker Name == "avx2.psrl.dq")) {
966*9880d681SAndroid Build Coastguard Worker // 128/256-bit shift right specified in bits.
967*9880d681SAndroid Build Coastguard Worker unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
968*9880d681SAndroid Build Coastguard Worker Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0),
969*9880d681SAndroid Build Coastguard Worker Shift / 8); // Shift is in bits.
970*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse2.psll.dq.bs" ||
971*9880d681SAndroid Build Coastguard Worker Name == "avx2.psll.dq.bs" ||
972*9880d681SAndroid Build Coastguard Worker Name == "avx512.psll.dq.512")) {
973*9880d681SAndroid Build Coastguard Worker // 128/256/512-bit shift left specified in bytes.
974*9880d681SAndroid Build Coastguard Worker unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
975*9880d681SAndroid Build Coastguard Worker Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
976*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse2.psrl.dq.bs" ||
977*9880d681SAndroid Build Coastguard Worker Name == "avx2.psrl.dq.bs" ||
978*9880d681SAndroid Build Coastguard Worker Name == "avx512.psrl.dq.512")) {
979*9880d681SAndroid Build Coastguard Worker // 128/256/512-bit shift right specified in bytes.
980*9880d681SAndroid Build Coastguard Worker unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
981*9880d681SAndroid Build Coastguard Worker Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
982*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse41.pblendw" ||
983*9880d681SAndroid Build Coastguard Worker Name.startswith("sse41.blendp") ||
984*9880d681SAndroid Build Coastguard Worker Name.startswith("avx.blend.p") ||
985*9880d681SAndroid Build Coastguard Worker Name == "avx2.pblendw" ||
986*9880d681SAndroid Build Coastguard Worker Name.startswith("avx2.pblendd."))) {
987*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI->getArgOperand(0);
988*9880d681SAndroid Build Coastguard Worker Value *Op1 = CI->getArgOperand(1);
989*9880d681SAndroid Build Coastguard Worker unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue();
990*9880d681SAndroid Build Coastguard Worker VectorType *VecTy = cast<VectorType>(CI->getType());
991*9880d681SAndroid Build Coastguard Worker unsigned NumElts = VecTy->getNumElements();
992*9880d681SAndroid Build Coastguard Worker
993*9880d681SAndroid Build Coastguard Worker SmallVector<uint32_t, 16> Idxs(NumElts);
994*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElts; ++i)
995*9880d681SAndroid Build Coastguard Worker Idxs[i] = ((Imm >> (i%8)) & 1) ? i + NumElts : i;
996*9880d681SAndroid Build Coastguard Worker
997*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
998*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx.vinsertf128.") ||
999*9880d681SAndroid Build Coastguard Worker Name == "avx2.vinserti128")) {
1000*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI->getArgOperand(0);
1001*9880d681SAndroid Build Coastguard Worker Value *Op1 = CI->getArgOperand(1);
1002*9880d681SAndroid Build Coastguard Worker unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
1003*9880d681SAndroid Build Coastguard Worker VectorType *VecTy = cast<VectorType>(CI->getType());
1004*9880d681SAndroid Build Coastguard Worker unsigned NumElts = VecTy->getNumElements();
1005*9880d681SAndroid Build Coastguard Worker
1006*9880d681SAndroid Build Coastguard Worker // Mask off the high bits of the immediate value; hardware ignores those.
1007*9880d681SAndroid Build Coastguard Worker Imm = Imm & 1;
1008*9880d681SAndroid Build Coastguard Worker
1009*9880d681SAndroid Build Coastguard Worker // Extend the second operand into a vector that is twice as big.
1010*9880d681SAndroid Build Coastguard Worker Value *UndefV = UndefValue::get(Op1->getType());
1011*9880d681SAndroid Build Coastguard Worker SmallVector<uint32_t, 8> Idxs(NumElts);
1012*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElts; ++i)
1013*9880d681SAndroid Build Coastguard Worker Idxs[i] = i;
1014*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op1, UndefV, Idxs);
1015*9880d681SAndroid Build Coastguard Worker
1016*9880d681SAndroid Build Coastguard Worker // Insert the second operand into the first operand.
1017*9880d681SAndroid Build Coastguard Worker
1018*9880d681SAndroid Build Coastguard Worker // Note that there is no guarantee that instruction lowering will actually
1019*9880d681SAndroid Build Coastguard Worker // produce a vinsertf128 instruction for the created shuffles. In
1020*9880d681SAndroid Build Coastguard Worker // particular, the 0 immediate case involves no lane changes, so it can
1021*9880d681SAndroid Build Coastguard Worker // be handled as a blend.
1022*9880d681SAndroid Build Coastguard Worker
1023*9880d681SAndroid Build Coastguard Worker // Example of shuffle mask for 32-bit elements:
1024*9880d681SAndroid Build Coastguard Worker // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
1025*9880d681SAndroid Build Coastguard Worker // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 >
1026*9880d681SAndroid Build Coastguard Worker
1027*9880d681SAndroid Build Coastguard Worker // The low half of the result is either the low half of the 1st operand
1028*9880d681SAndroid Build Coastguard Worker // or the low half of the 2nd operand (the inserted vector).
1029*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElts / 2; ++i)
1030*9880d681SAndroid Build Coastguard Worker Idxs[i] = Imm ? i : (i + NumElts);
1031*9880d681SAndroid Build Coastguard Worker // The high half of the result is either the low half of the 2nd operand
1032*9880d681SAndroid Build Coastguard Worker // (the inserted vector) or the high half of the 1st operand.
1033*9880d681SAndroid Build Coastguard Worker for (unsigned i = NumElts / 2; i != NumElts; ++i)
1034*9880d681SAndroid Build Coastguard Worker Idxs[i] = Imm ? (i + NumElts / 2) : i;
1035*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op0, Rep, Idxs);
1036*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx.vextractf128.") ||
1037*9880d681SAndroid Build Coastguard Worker Name == "avx2.vextracti128")) {
1038*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI->getArgOperand(0);
1039*9880d681SAndroid Build Coastguard Worker unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1040*9880d681SAndroid Build Coastguard Worker VectorType *VecTy = cast<VectorType>(CI->getType());
1041*9880d681SAndroid Build Coastguard Worker unsigned NumElts = VecTy->getNumElements();
1042*9880d681SAndroid Build Coastguard Worker
1043*9880d681SAndroid Build Coastguard Worker // Mask off the high bits of the immediate value; hardware ignores those.
1044*9880d681SAndroid Build Coastguard Worker Imm = Imm & 1;
1045*9880d681SAndroid Build Coastguard Worker
1046*9880d681SAndroid Build Coastguard Worker // Get indexes for either the high half or low half of the input vector.
1047*9880d681SAndroid Build Coastguard Worker SmallVector<uint32_t, 4> Idxs(NumElts);
1048*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElts; ++i) {
1049*9880d681SAndroid Build Coastguard Worker Idxs[i] = Imm ? (i + NumElts) : i;
1050*9880d681SAndroid Build Coastguard Worker }
1051*9880d681SAndroid Build Coastguard Worker
1052*9880d681SAndroid Build Coastguard Worker Value *UndefV = UndefValue::get(Op0->getType());
1053*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op0, UndefV, Idxs);
1054*9880d681SAndroid Build Coastguard Worker } else if (!IsX86 && Name == "stackprotectorcheck") {
1055*9880d681SAndroid Build Coastguard Worker Rep = nullptr;
1056*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx512.mask.perm.df.") ||
1057*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.perm.di."))) {
1058*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI->getArgOperand(0);
1059*9880d681SAndroid Build Coastguard Worker unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1060*9880d681SAndroid Build Coastguard Worker VectorType *VecTy = cast<VectorType>(CI->getType());
1061*9880d681SAndroid Build Coastguard Worker unsigned NumElts = VecTy->getNumElements();
1062*9880d681SAndroid Build Coastguard Worker
1063*9880d681SAndroid Build Coastguard Worker SmallVector<uint32_t, 8> Idxs(NumElts);
1064*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElts; ++i)
1065*9880d681SAndroid Build Coastguard Worker Idxs[i] = (i & ~0x3) + ((Imm >> (2 * (i & 0x3))) & 3);
1066*9880d681SAndroid Build Coastguard Worker
1067*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1068*9880d681SAndroid Build Coastguard Worker
1069*9880d681SAndroid Build Coastguard Worker if (CI->getNumArgOperands() == 4)
1070*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1071*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2));
1072*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx.vpermil.") ||
1073*9880d681SAndroid Build Coastguard Worker Name == "sse2.pshuf.d" ||
1074*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.vpermil.p") ||
1075*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.pshuf.d."))) {
1076*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI->getArgOperand(0);
1077*9880d681SAndroid Build Coastguard Worker unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1078*9880d681SAndroid Build Coastguard Worker VectorType *VecTy = cast<VectorType>(CI->getType());
1079*9880d681SAndroid Build Coastguard Worker unsigned NumElts = VecTy->getNumElements();
1080*9880d681SAndroid Build Coastguard Worker // Calculate the size of each index in the immediate.
1081*9880d681SAndroid Build Coastguard Worker unsigned IdxSize = 64 / VecTy->getScalarSizeInBits();
1082*9880d681SAndroid Build Coastguard Worker unsigned IdxMask = ((1 << IdxSize) - 1);
1083*9880d681SAndroid Build Coastguard Worker
1084*9880d681SAndroid Build Coastguard Worker SmallVector<uint32_t, 8> Idxs(NumElts);
1085*9880d681SAndroid Build Coastguard Worker // Lookup the bits for this element, wrapping around the immediate every
1086*9880d681SAndroid Build Coastguard Worker // 8-bits. Elements are grouped into sets of 2 or 4 elements so we need
1087*9880d681SAndroid Build Coastguard Worker // to offset by the first index of each group.
1088*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElts; ++i)
1089*9880d681SAndroid Build Coastguard Worker Idxs[i] = ((Imm >> ((i * IdxSize) % 8)) & IdxMask) | (i & ~IdxMask);
1090*9880d681SAndroid Build Coastguard Worker
1091*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1092*9880d681SAndroid Build Coastguard Worker
1093*9880d681SAndroid Build Coastguard Worker if (CI->getNumArgOperands() == 4)
1094*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1095*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2));
1096*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse2.pshufl.w" ||
1097*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.pshufl.w."))) {
1098*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI->getArgOperand(0);
1099*9880d681SAndroid Build Coastguard Worker unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1100*9880d681SAndroid Build Coastguard Worker unsigned NumElts = CI->getType()->getVectorNumElements();
1101*9880d681SAndroid Build Coastguard Worker
1102*9880d681SAndroid Build Coastguard Worker SmallVector<uint32_t, 16> Idxs(NumElts);
1103*9880d681SAndroid Build Coastguard Worker for (unsigned l = 0; l != NumElts; l += 8) {
1104*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != 4; ++i)
1105*9880d681SAndroid Build Coastguard Worker Idxs[i + l] = ((Imm >> (2 * i)) & 0x3) + l;
1106*9880d681SAndroid Build Coastguard Worker for (unsigned i = 4; i != 8; ++i)
1107*9880d681SAndroid Build Coastguard Worker Idxs[i + l] = i + l;
1108*9880d681SAndroid Build Coastguard Worker }
1109*9880d681SAndroid Build Coastguard Worker
1110*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1111*9880d681SAndroid Build Coastguard Worker
1112*9880d681SAndroid Build Coastguard Worker if (CI->getNumArgOperands() == 4)
1113*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1114*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2));
1115*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name == "sse2.pshufh.w" ||
1116*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.pshufh.w."))) {
1117*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI->getArgOperand(0);
1118*9880d681SAndroid Build Coastguard Worker unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1119*9880d681SAndroid Build Coastguard Worker unsigned NumElts = CI->getType()->getVectorNumElements();
1120*9880d681SAndroid Build Coastguard Worker
1121*9880d681SAndroid Build Coastguard Worker SmallVector<uint32_t, 16> Idxs(NumElts);
1122*9880d681SAndroid Build Coastguard Worker for (unsigned l = 0; l != NumElts; l += 8) {
1123*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != 4; ++i)
1124*9880d681SAndroid Build Coastguard Worker Idxs[i + l] = i + l;
1125*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != 4; ++i)
1126*9880d681SAndroid Build Coastguard Worker Idxs[i + l + 4] = ((Imm >> (2 * i)) & 0x3) + 4 + l;
1127*9880d681SAndroid Build Coastguard Worker }
1128*9880d681SAndroid Build Coastguard Worker
1129*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1130*9880d681SAndroid Build Coastguard Worker
1131*9880d681SAndroid Build Coastguard Worker if (CI->getNumArgOperands() == 4)
1132*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1133*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2));
1134*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx512.mask.movddup") ||
1135*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.movshdup") ||
1136*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.movsldup"))) {
1137*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI->getArgOperand(0);
1138*9880d681SAndroid Build Coastguard Worker unsigned NumElts = CI->getType()->getVectorNumElements();
1139*9880d681SAndroid Build Coastguard Worker unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1140*9880d681SAndroid Build Coastguard Worker
1141*9880d681SAndroid Build Coastguard Worker unsigned Offset = 0;
1142*9880d681SAndroid Build Coastguard Worker if (Name.startswith("avx512.mask.movshdup."))
1143*9880d681SAndroid Build Coastguard Worker Offset = 1;
1144*9880d681SAndroid Build Coastguard Worker
1145*9880d681SAndroid Build Coastguard Worker SmallVector<uint32_t, 16> Idxs(NumElts);
1146*9880d681SAndroid Build Coastguard Worker for (unsigned l = 0; l != NumElts; l += NumLaneElts)
1147*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumLaneElts; i += 2) {
1148*9880d681SAndroid Build Coastguard Worker Idxs[i + l + 0] = i + l + Offset;
1149*9880d681SAndroid Build Coastguard Worker Idxs[i + l + 1] = i + l + Offset;
1150*9880d681SAndroid Build Coastguard Worker }
1151*9880d681SAndroid Build Coastguard Worker
1152*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1153*9880d681SAndroid Build Coastguard Worker
1154*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
1155*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(1));
1156*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx512.mask.punpckl") ||
1157*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.unpckl."))) {
1158*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI->getArgOperand(0);
1159*9880d681SAndroid Build Coastguard Worker Value *Op1 = CI->getArgOperand(1);
1160*9880d681SAndroid Build Coastguard Worker int NumElts = CI->getType()->getVectorNumElements();
1161*9880d681SAndroid Build Coastguard Worker int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1162*9880d681SAndroid Build Coastguard Worker
1163*9880d681SAndroid Build Coastguard Worker SmallVector<uint32_t, 64> Idxs(NumElts);
1164*9880d681SAndroid Build Coastguard Worker for (int l = 0; l != NumElts; l += NumLaneElts)
1165*9880d681SAndroid Build Coastguard Worker for (int i = 0; i != NumLaneElts; ++i)
1166*9880d681SAndroid Build Coastguard Worker Idxs[i + l] = l + (i / 2) + NumElts * (i % 2);
1167*9880d681SAndroid Build Coastguard Worker
1168*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1169*9880d681SAndroid Build Coastguard Worker
1170*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1171*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2));
1172*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && (Name.startswith("avx512.mask.punpckh") ||
1173*9880d681SAndroid Build Coastguard Worker Name.startswith("avx512.mask.unpckh."))) {
1174*9880d681SAndroid Build Coastguard Worker Value *Op0 = CI->getArgOperand(0);
1175*9880d681SAndroid Build Coastguard Worker Value *Op1 = CI->getArgOperand(1);
1176*9880d681SAndroid Build Coastguard Worker int NumElts = CI->getType()->getVectorNumElements();
1177*9880d681SAndroid Build Coastguard Worker int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1178*9880d681SAndroid Build Coastguard Worker
1179*9880d681SAndroid Build Coastguard Worker SmallVector<uint32_t, 64> Idxs(NumElts);
1180*9880d681SAndroid Build Coastguard Worker for (int l = 0; l != NumElts; l += NumLaneElts)
1181*9880d681SAndroid Build Coastguard Worker for (int i = 0; i != NumLaneElts; ++i)
1182*9880d681SAndroid Build Coastguard Worker Idxs[i + l] = (NumLaneElts / 2) + l + (i / 2) + NumElts * (i % 2);
1183*9880d681SAndroid Build Coastguard Worker
1184*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1185*9880d681SAndroid Build Coastguard Worker
1186*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1187*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2));
1188*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name.startswith("avx512.mask.pand.")) {
1189*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateAnd(CI->getArgOperand(0), CI->getArgOperand(1));
1190*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1191*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2));
1192*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name.startswith("avx512.mask.pandn.")) {
1193*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateAnd(Builder.CreateNot(CI->getArgOperand(0)),
1194*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(1));
1195*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1196*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2));
1197*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name.startswith("avx512.mask.por.")) {
1198*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateOr(CI->getArgOperand(0), CI->getArgOperand(1));
1199*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1200*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2));
1201*9880d681SAndroid Build Coastguard Worker } else if (IsX86 && Name.startswith("avx512.mask.pxor.")) {
1202*9880d681SAndroid Build Coastguard Worker Rep = Builder.CreateXor(CI->getArgOperand(0), CI->getArgOperand(1));
1203*9880d681SAndroid Build Coastguard Worker Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1204*9880d681SAndroid Build Coastguard Worker CI->getArgOperand(2));
1205*9880d681SAndroid Build Coastguard Worker } else {
1206*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unknown function for CallInst upgrade.");
1207*9880d681SAndroid Build Coastguard Worker }
1208*9880d681SAndroid Build Coastguard Worker
1209*9880d681SAndroid Build Coastguard Worker if (Rep)
1210*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(Rep);
1211*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
1212*9880d681SAndroid Build Coastguard Worker return;
1213*9880d681SAndroid Build Coastguard Worker }
1214*9880d681SAndroid Build Coastguard Worker
1215*9880d681SAndroid Build Coastguard Worker std::string Name = CI->getName();
1216*9880d681SAndroid Build Coastguard Worker if (!Name.empty())
1217*9880d681SAndroid Build Coastguard Worker CI->setName(Name + ".old");
1218*9880d681SAndroid Build Coastguard Worker
1219*9880d681SAndroid Build Coastguard Worker switch (NewFn->getIntrinsicID()) {
1220*9880d681SAndroid Build Coastguard Worker default:
1221*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unknown function for CallInst upgrade.");
1222*9880d681SAndroid Build Coastguard Worker
1223*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_avx512_mask_psll_di_512:
1224*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_avx512_mask_psra_di_512:
1225*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_avx512_mask_psrl_di_512:
1226*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_avx512_mask_psll_qi_512:
1227*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_avx512_mask_psra_qi_512:
1228*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_avx512_mask_psrl_qi_512:
1229*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vld1:
1230*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vld2:
1231*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vld3:
1232*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vld4:
1233*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vld2lane:
1234*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vld3lane:
1235*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vld4lane:
1236*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vst1:
1237*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vst2:
1238*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vst3:
1239*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vst4:
1240*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vst2lane:
1241*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vst3lane:
1242*9880d681SAndroid Build Coastguard Worker case Intrinsic::arm_neon_vst4lane: {
1243*9880d681SAndroid Build Coastguard Worker SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1244*9880d681SAndroid Build Coastguard Worker CI->arg_operands().end());
1245*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1246*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
1247*9880d681SAndroid Build Coastguard Worker return;
1248*9880d681SAndroid Build Coastguard Worker }
1249*9880d681SAndroid Build Coastguard Worker
1250*9880d681SAndroid Build Coastguard Worker case Intrinsic::ctlz:
1251*9880d681SAndroid Build Coastguard Worker case Intrinsic::cttz:
1252*9880d681SAndroid Build Coastguard Worker assert(CI->getNumArgOperands() == 1 &&
1253*9880d681SAndroid Build Coastguard Worker "Mismatch between function args and call args");
1254*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(Builder.CreateCall(
1255*9880d681SAndroid Build Coastguard Worker NewFn, {CI->getArgOperand(0), Builder.getFalse()}, Name));
1256*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
1257*9880d681SAndroid Build Coastguard Worker return;
1258*9880d681SAndroid Build Coastguard Worker
1259*9880d681SAndroid Build Coastguard Worker case Intrinsic::objectsize:
1260*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(Builder.CreateCall(
1261*9880d681SAndroid Build Coastguard Worker NewFn, {CI->getArgOperand(0), CI->getArgOperand(1)}, Name));
1262*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
1263*9880d681SAndroid Build Coastguard Worker return;
1264*9880d681SAndroid Build Coastguard Worker
1265*9880d681SAndroid Build Coastguard Worker case Intrinsic::ctpop: {
1266*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {CI->getArgOperand(0)}));
1267*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
1268*9880d681SAndroid Build Coastguard Worker return;
1269*9880d681SAndroid Build Coastguard Worker }
1270*9880d681SAndroid Build Coastguard Worker
1271*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_xop_vfrcz_ss:
1272*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_xop_vfrcz_sd:
1273*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(
1274*9880d681SAndroid Build Coastguard Worker Builder.CreateCall(NewFn, {CI->getArgOperand(1)}, Name));
1275*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
1276*9880d681SAndroid Build Coastguard Worker return;
1277*9880d681SAndroid Build Coastguard Worker
1278*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_xop_vpermil2pd:
1279*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_xop_vpermil2ps:
1280*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_xop_vpermil2pd_256:
1281*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_xop_vpermil2ps_256: {
1282*9880d681SAndroid Build Coastguard Worker SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1283*9880d681SAndroid Build Coastguard Worker CI->arg_operands().end());
1284*9880d681SAndroid Build Coastguard Worker VectorType *FltIdxTy = cast<VectorType>(Args[2]->getType());
1285*9880d681SAndroid Build Coastguard Worker VectorType *IntIdxTy = VectorType::getInteger(FltIdxTy);
1286*9880d681SAndroid Build Coastguard Worker Args[2] = Builder.CreateBitCast(Args[2], IntIdxTy);
1287*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args, Name));
1288*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
1289*9880d681SAndroid Build Coastguard Worker return;
1290*9880d681SAndroid Build Coastguard Worker }
1291*9880d681SAndroid Build Coastguard Worker
1292*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse41_ptestc:
1293*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse41_ptestz:
1294*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse41_ptestnzc: {
1295*9880d681SAndroid Build Coastguard Worker // The arguments for these intrinsics used to be v4f32, and changed
1296*9880d681SAndroid Build Coastguard Worker // to v2i64. This is purely a nop, since those are bitwise intrinsics.
1297*9880d681SAndroid Build Coastguard Worker // So, the only thing required is a bitcast for both arguments.
1298*9880d681SAndroid Build Coastguard Worker // First, check the arguments have the old type.
1299*9880d681SAndroid Build Coastguard Worker Value *Arg0 = CI->getArgOperand(0);
1300*9880d681SAndroid Build Coastguard Worker if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
1301*9880d681SAndroid Build Coastguard Worker return;
1302*9880d681SAndroid Build Coastguard Worker
1303*9880d681SAndroid Build Coastguard Worker // Old intrinsic, add bitcasts
1304*9880d681SAndroid Build Coastguard Worker Value *Arg1 = CI->getArgOperand(1);
1305*9880d681SAndroid Build Coastguard Worker
1306*9880d681SAndroid Build Coastguard Worker Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
1307*9880d681SAndroid Build Coastguard Worker
1308*9880d681SAndroid Build Coastguard Worker Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast");
1309*9880d681SAndroid Build Coastguard Worker Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
1310*9880d681SAndroid Build Coastguard Worker
1311*9880d681SAndroid Build Coastguard Worker CallInst *NewCall = Builder.CreateCall(NewFn, {BC0, BC1}, Name);
1312*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(NewCall);
1313*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
1314*9880d681SAndroid Build Coastguard Worker return;
1315*9880d681SAndroid Build Coastguard Worker }
1316*9880d681SAndroid Build Coastguard Worker
1317*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse41_insertps:
1318*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse41_dppd:
1319*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse41_dpps:
1320*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse41_mpsadbw:
1321*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_avx_dp_ps_256:
1322*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_avx2_mpsadbw: {
1323*9880d681SAndroid Build Coastguard Worker // Need to truncate the last argument from i32 to i8 -- this argument models
1324*9880d681SAndroid Build Coastguard Worker // an inherently 8-bit immediate operand to these x86 instructions.
1325*9880d681SAndroid Build Coastguard Worker SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1326*9880d681SAndroid Build Coastguard Worker CI->arg_operands().end());
1327*9880d681SAndroid Build Coastguard Worker
1328*9880d681SAndroid Build Coastguard Worker // Replace the last argument with a trunc.
1329*9880d681SAndroid Build Coastguard Worker Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
1330*9880d681SAndroid Build Coastguard Worker
1331*9880d681SAndroid Build Coastguard Worker CallInst *NewCall = Builder.CreateCall(NewFn, Args);
1332*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(NewCall);
1333*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
1334*9880d681SAndroid Build Coastguard Worker return;
1335*9880d681SAndroid Build Coastguard Worker }
1336*9880d681SAndroid Build Coastguard Worker
1337*9880d681SAndroid Build Coastguard Worker case Intrinsic::thread_pointer: {
1338*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {}));
1339*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
1340*9880d681SAndroid Build Coastguard Worker return;
1341*9880d681SAndroid Build Coastguard Worker }
1342*9880d681SAndroid Build Coastguard Worker
1343*9880d681SAndroid Build Coastguard Worker case Intrinsic::masked_load:
1344*9880d681SAndroid Build Coastguard Worker case Intrinsic::masked_store: {
1345*9880d681SAndroid Build Coastguard Worker SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1346*9880d681SAndroid Build Coastguard Worker CI->arg_operands().end());
1347*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1348*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent();
1349*9880d681SAndroid Build Coastguard Worker return;
1350*9880d681SAndroid Build Coastguard Worker }
1351*9880d681SAndroid Build Coastguard Worker }
1352*9880d681SAndroid Build Coastguard Worker }
1353*9880d681SAndroid Build Coastguard Worker
UpgradeCallsToIntrinsic(Function * F)1354*9880d681SAndroid Build Coastguard Worker void llvm::UpgradeCallsToIntrinsic(Function *F) {
1355*9880d681SAndroid Build Coastguard Worker assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
1356*9880d681SAndroid Build Coastguard Worker
1357*9880d681SAndroid Build Coastguard Worker // Check if this function should be upgraded and get the replacement function
1358*9880d681SAndroid Build Coastguard Worker // if there is one.
1359*9880d681SAndroid Build Coastguard Worker Function *NewFn;
1360*9880d681SAndroid Build Coastguard Worker if (UpgradeIntrinsicFunction(F, NewFn)) {
1361*9880d681SAndroid Build Coastguard Worker // Replace all users of the old function with the new function or new
1362*9880d681SAndroid Build Coastguard Worker // instructions. This is not a range loop because the call is deleted.
1363*9880d681SAndroid Build Coastguard Worker for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; )
1364*9880d681SAndroid Build Coastguard Worker if (CallInst *CI = dyn_cast<CallInst>(*UI++))
1365*9880d681SAndroid Build Coastguard Worker UpgradeIntrinsicCall(CI, NewFn);
1366*9880d681SAndroid Build Coastguard Worker
1367*9880d681SAndroid Build Coastguard Worker // Remove old function, no longer used, from the module.
1368*9880d681SAndroid Build Coastguard Worker F->eraseFromParent();
1369*9880d681SAndroid Build Coastguard Worker }
1370*9880d681SAndroid Build Coastguard Worker }
1371*9880d681SAndroid Build Coastguard Worker
UpgradeInstWithTBAATag(Instruction * I)1372*9880d681SAndroid Build Coastguard Worker void llvm::UpgradeInstWithTBAATag(Instruction *I) {
1373*9880d681SAndroid Build Coastguard Worker MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
1374*9880d681SAndroid Build Coastguard Worker assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
1375*9880d681SAndroid Build Coastguard Worker // Check if the tag uses struct-path aware TBAA format.
1376*9880d681SAndroid Build Coastguard Worker if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
1377*9880d681SAndroid Build Coastguard Worker return;
1378*9880d681SAndroid Build Coastguard Worker
1379*9880d681SAndroid Build Coastguard Worker if (MD->getNumOperands() == 3) {
1380*9880d681SAndroid Build Coastguard Worker Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
1381*9880d681SAndroid Build Coastguard Worker MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
1382*9880d681SAndroid Build Coastguard Worker // Create a MDNode <ScalarType, ScalarType, offset 0, const>
1383*9880d681SAndroid Build Coastguard Worker Metadata *Elts2[] = {ScalarType, ScalarType,
1384*9880d681SAndroid Build Coastguard Worker ConstantAsMetadata::get(Constant::getNullValue(
1385*9880d681SAndroid Build Coastguard Worker Type::getInt64Ty(I->getContext()))),
1386*9880d681SAndroid Build Coastguard Worker MD->getOperand(2)};
1387*9880d681SAndroid Build Coastguard Worker I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
1388*9880d681SAndroid Build Coastguard Worker } else {
1389*9880d681SAndroid Build Coastguard Worker // Create a MDNode <MD, MD, offset 0>
1390*9880d681SAndroid Build Coastguard Worker Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
1391*9880d681SAndroid Build Coastguard Worker Type::getInt64Ty(I->getContext())))};
1392*9880d681SAndroid Build Coastguard Worker I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
1393*9880d681SAndroid Build Coastguard Worker }
1394*9880d681SAndroid Build Coastguard Worker }
1395*9880d681SAndroid Build Coastguard Worker
UpgradeBitCastInst(unsigned Opc,Value * V,Type * DestTy,Instruction * & Temp)1396*9880d681SAndroid Build Coastguard Worker Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
1397*9880d681SAndroid Build Coastguard Worker Instruction *&Temp) {
1398*9880d681SAndroid Build Coastguard Worker if (Opc != Instruction::BitCast)
1399*9880d681SAndroid Build Coastguard Worker return nullptr;
1400*9880d681SAndroid Build Coastguard Worker
1401*9880d681SAndroid Build Coastguard Worker Temp = nullptr;
1402*9880d681SAndroid Build Coastguard Worker Type *SrcTy = V->getType();
1403*9880d681SAndroid Build Coastguard Worker if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1404*9880d681SAndroid Build Coastguard Worker SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1405*9880d681SAndroid Build Coastguard Worker LLVMContext &Context = V->getContext();
1406*9880d681SAndroid Build Coastguard Worker
1407*9880d681SAndroid Build Coastguard Worker // We have no information about target data layout, so we assume that
1408*9880d681SAndroid Build Coastguard Worker // the maximum pointer size is 64bit.
1409*9880d681SAndroid Build Coastguard Worker Type *MidTy = Type::getInt64Ty(Context);
1410*9880d681SAndroid Build Coastguard Worker Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
1411*9880d681SAndroid Build Coastguard Worker
1412*9880d681SAndroid Build Coastguard Worker return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
1413*9880d681SAndroid Build Coastguard Worker }
1414*9880d681SAndroid Build Coastguard Worker
1415*9880d681SAndroid Build Coastguard Worker return nullptr;
1416*9880d681SAndroid Build Coastguard Worker }
1417*9880d681SAndroid Build Coastguard Worker
UpgradeBitCastExpr(unsigned Opc,Constant * C,Type * DestTy)1418*9880d681SAndroid Build Coastguard Worker Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
1419*9880d681SAndroid Build Coastguard Worker if (Opc != Instruction::BitCast)
1420*9880d681SAndroid Build Coastguard Worker return nullptr;
1421*9880d681SAndroid Build Coastguard Worker
1422*9880d681SAndroid Build Coastguard Worker Type *SrcTy = C->getType();
1423*9880d681SAndroid Build Coastguard Worker if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1424*9880d681SAndroid Build Coastguard Worker SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1425*9880d681SAndroid Build Coastguard Worker LLVMContext &Context = C->getContext();
1426*9880d681SAndroid Build Coastguard Worker
1427*9880d681SAndroid Build Coastguard Worker // We have no information about target data layout, so we assume that
1428*9880d681SAndroid Build Coastguard Worker // the maximum pointer size is 64bit.
1429*9880d681SAndroid Build Coastguard Worker Type *MidTy = Type::getInt64Ty(Context);
1430*9880d681SAndroid Build Coastguard Worker
1431*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
1432*9880d681SAndroid Build Coastguard Worker DestTy);
1433*9880d681SAndroid Build Coastguard Worker }
1434*9880d681SAndroid Build Coastguard Worker
1435*9880d681SAndroid Build Coastguard Worker return nullptr;
1436*9880d681SAndroid Build Coastguard Worker }
1437*9880d681SAndroid Build Coastguard Worker
1438*9880d681SAndroid Build Coastguard Worker /// Check the debug info version number, if it is out-dated, drop the debug
1439*9880d681SAndroid Build Coastguard Worker /// info. Return true if module is modified.
UpgradeDebugInfo(Module & M)1440*9880d681SAndroid Build Coastguard Worker bool llvm::UpgradeDebugInfo(Module &M) {
1441*9880d681SAndroid Build Coastguard Worker unsigned Version = getDebugMetadataVersionFromModule(M);
1442*9880d681SAndroid Build Coastguard Worker if (Version == DEBUG_METADATA_VERSION)
1443*9880d681SAndroid Build Coastguard Worker return false;
1444*9880d681SAndroid Build Coastguard Worker
1445*9880d681SAndroid Build Coastguard Worker bool RetCode = StripDebugInfo(M);
1446*9880d681SAndroid Build Coastguard Worker if (RetCode) {
1447*9880d681SAndroid Build Coastguard Worker DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
1448*9880d681SAndroid Build Coastguard Worker M.getContext().diagnose(DiagVersion);
1449*9880d681SAndroid Build Coastguard Worker }
1450*9880d681SAndroid Build Coastguard Worker return RetCode;
1451*9880d681SAndroid Build Coastguard Worker }
1452*9880d681SAndroid Build Coastguard Worker
UpgradeModuleFlags(Module & M)1453*9880d681SAndroid Build Coastguard Worker bool llvm::UpgradeModuleFlags(Module &M) {
1454*9880d681SAndroid Build Coastguard Worker const NamedMDNode *ModFlags = M.getModuleFlagsMetadata();
1455*9880d681SAndroid Build Coastguard Worker if (!ModFlags)
1456*9880d681SAndroid Build Coastguard Worker return false;
1457*9880d681SAndroid Build Coastguard Worker
1458*9880d681SAndroid Build Coastguard Worker bool HasObjCFlag = false, HasClassProperties = false;
1459*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
1460*9880d681SAndroid Build Coastguard Worker MDNode *Op = ModFlags->getOperand(I);
1461*9880d681SAndroid Build Coastguard Worker if (Op->getNumOperands() < 2)
1462*9880d681SAndroid Build Coastguard Worker continue;
1463*9880d681SAndroid Build Coastguard Worker MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1464*9880d681SAndroid Build Coastguard Worker if (!ID)
1465*9880d681SAndroid Build Coastguard Worker continue;
1466*9880d681SAndroid Build Coastguard Worker if (ID->getString() == "Objective-C Image Info Version")
1467*9880d681SAndroid Build Coastguard Worker HasObjCFlag = true;
1468*9880d681SAndroid Build Coastguard Worker if (ID->getString() == "Objective-C Class Properties")
1469*9880d681SAndroid Build Coastguard Worker HasClassProperties = true;
1470*9880d681SAndroid Build Coastguard Worker }
1471*9880d681SAndroid Build Coastguard Worker // "Objective-C Class Properties" is recently added for Objective-C. We
1472*9880d681SAndroid Build Coastguard Worker // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module
1473*9880d681SAndroid Build Coastguard Worker // flag of value 0, so we can correclty report error when trying to link
1474*9880d681SAndroid Build Coastguard Worker // an ObjC bitcode without this module flag with an ObjC bitcode with this
1475*9880d681SAndroid Build Coastguard Worker // module flag.
1476*9880d681SAndroid Build Coastguard Worker if (HasObjCFlag && !HasClassProperties) {
1477*9880d681SAndroid Build Coastguard Worker M.addModuleFlag(llvm::Module::Error, "Objective-C Class Properties",
1478*9880d681SAndroid Build Coastguard Worker (uint32_t)0);
1479*9880d681SAndroid Build Coastguard Worker return true;
1480*9880d681SAndroid Build Coastguard Worker }
1481*9880d681SAndroid Build Coastguard Worker return false;
1482*9880d681SAndroid Build Coastguard Worker }
1483*9880d681SAndroid Build Coastguard Worker
isOldLoopArgument(Metadata * MD)1484*9880d681SAndroid Build Coastguard Worker static bool isOldLoopArgument(Metadata *MD) {
1485*9880d681SAndroid Build Coastguard Worker auto *T = dyn_cast_or_null<MDTuple>(MD);
1486*9880d681SAndroid Build Coastguard Worker if (!T)
1487*9880d681SAndroid Build Coastguard Worker return false;
1488*9880d681SAndroid Build Coastguard Worker if (T->getNumOperands() < 1)
1489*9880d681SAndroid Build Coastguard Worker return false;
1490*9880d681SAndroid Build Coastguard Worker auto *S = dyn_cast_or_null<MDString>(T->getOperand(0));
1491*9880d681SAndroid Build Coastguard Worker if (!S)
1492*9880d681SAndroid Build Coastguard Worker return false;
1493*9880d681SAndroid Build Coastguard Worker return S->getString().startswith("llvm.vectorizer.");
1494*9880d681SAndroid Build Coastguard Worker }
1495*9880d681SAndroid Build Coastguard Worker
upgradeLoopTag(LLVMContext & C,StringRef OldTag)1496*9880d681SAndroid Build Coastguard Worker static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) {
1497*9880d681SAndroid Build Coastguard Worker StringRef OldPrefix = "llvm.vectorizer.";
1498*9880d681SAndroid Build Coastguard Worker assert(OldTag.startswith(OldPrefix) && "Expected old prefix");
1499*9880d681SAndroid Build Coastguard Worker
1500*9880d681SAndroid Build Coastguard Worker if (OldTag == "llvm.vectorizer.unroll")
1501*9880d681SAndroid Build Coastguard Worker return MDString::get(C, "llvm.loop.interleave.count");
1502*9880d681SAndroid Build Coastguard Worker
1503*9880d681SAndroid Build Coastguard Worker return MDString::get(
1504*9880d681SAndroid Build Coastguard Worker C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size()))
1505*9880d681SAndroid Build Coastguard Worker .str());
1506*9880d681SAndroid Build Coastguard Worker }
1507*9880d681SAndroid Build Coastguard Worker
upgradeLoopArgument(Metadata * MD)1508*9880d681SAndroid Build Coastguard Worker static Metadata *upgradeLoopArgument(Metadata *MD) {
1509*9880d681SAndroid Build Coastguard Worker auto *T = dyn_cast_or_null<MDTuple>(MD);
1510*9880d681SAndroid Build Coastguard Worker if (!T)
1511*9880d681SAndroid Build Coastguard Worker return MD;
1512*9880d681SAndroid Build Coastguard Worker if (T->getNumOperands() < 1)
1513*9880d681SAndroid Build Coastguard Worker return MD;
1514*9880d681SAndroid Build Coastguard Worker auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0));
1515*9880d681SAndroid Build Coastguard Worker if (!OldTag)
1516*9880d681SAndroid Build Coastguard Worker return MD;
1517*9880d681SAndroid Build Coastguard Worker if (!OldTag->getString().startswith("llvm.vectorizer."))
1518*9880d681SAndroid Build Coastguard Worker return MD;
1519*9880d681SAndroid Build Coastguard Worker
1520*9880d681SAndroid Build Coastguard Worker // This has an old tag. Upgrade it.
1521*9880d681SAndroid Build Coastguard Worker SmallVector<Metadata *, 8> Ops;
1522*9880d681SAndroid Build Coastguard Worker Ops.reserve(T->getNumOperands());
1523*9880d681SAndroid Build Coastguard Worker Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString()));
1524*9880d681SAndroid Build Coastguard Worker for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I)
1525*9880d681SAndroid Build Coastguard Worker Ops.push_back(T->getOperand(I));
1526*9880d681SAndroid Build Coastguard Worker
1527*9880d681SAndroid Build Coastguard Worker return MDTuple::get(T->getContext(), Ops);
1528*9880d681SAndroid Build Coastguard Worker }
1529*9880d681SAndroid Build Coastguard Worker
upgradeInstructionLoopAttachment(MDNode & N)1530*9880d681SAndroid Build Coastguard Worker MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) {
1531*9880d681SAndroid Build Coastguard Worker auto *T = dyn_cast<MDTuple>(&N);
1532*9880d681SAndroid Build Coastguard Worker if (!T)
1533*9880d681SAndroid Build Coastguard Worker return &N;
1534*9880d681SAndroid Build Coastguard Worker
1535*9880d681SAndroid Build Coastguard Worker if (!llvm::any_of(T->operands(), isOldLoopArgument))
1536*9880d681SAndroid Build Coastguard Worker return &N;
1537*9880d681SAndroid Build Coastguard Worker
1538*9880d681SAndroid Build Coastguard Worker SmallVector<Metadata *, 8> Ops;
1539*9880d681SAndroid Build Coastguard Worker Ops.reserve(T->getNumOperands());
1540*9880d681SAndroid Build Coastguard Worker for (Metadata *MD : T->operands())
1541*9880d681SAndroid Build Coastguard Worker Ops.push_back(upgradeLoopArgument(MD));
1542*9880d681SAndroid Build Coastguard Worker
1543*9880d681SAndroid Build Coastguard Worker return MDTuple::get(T->getContext(), Ops);
1544*9880d681SAndroid Build Coastguard Worker }
1545