blob: 468644314e3f3b46a3320c36d0bf50b3616e05ac [file] [log] [blame]
Chandler Carruth7132e002007-08-04 01:51:18 +00001//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chandler Carruth7132e002007-08-04 01:51:18 +00007//
8//===----------------------------------------------------------------------===//
9//
Sanjay Patel19792fb2015-03-10 16:08:36 +000010// This file implements the auto-upgrade helper functions.
11// This is where deprecated IR intrinsics and other IR features are updated to
12// current specifications.
Chandler Carruth7132e002007-08-04 01:51:18 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruth91065212014-03-05 10:34:14 +000016#include "llvm/IR/AutoUpgrade.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000017#include "llvm/IR/CFG.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000018#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "llvm/IR/DIBuilder.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000021#include "llvm/IR/DebugInfo.h"
Manman Ren2ebfb422014-01-16 01:51:12 +000022#include "llvm/IR/DiagnosticInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Function.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/Instruction.h"
26#include "llvm/IR/IntrinsicInst.h"
27#include "llvm/IR/LLVMContext.h"
28#include "llvm/IR/Module.h"
Torok Edwin56d06592009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Jeroen Ketemaab99b592015-09-30 10:56:37 +000030#include "llvm/Support/Regex.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000031#include <cstring>
Chandler Carruth7132e002007-08-04 01:51:18 +000032using namespace llvm;
33
Nadav Rotem17ee58a2012-06-10 18:42:51 +000034// Upgrade the declarations of the SSE4.1 functions whose arguments have
35// changed their type from v4f32 to v2i64.
36static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
37 Function *&NewFn) {
38 // Check whether this is an old version of the function, which received
39 // v4f32 arguments.
40 Type *Arg0Type = F->getFunctionType()->getParamType(0);
41 if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
42 return false;
43
44 // Yes, it's old, replace it with new version.
45 F->setName(F->getName() + ".old");
46 NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
47 return true;
48}
Chandler Carruth7132e002007-08-04 01:51:18 +000049
Chandler Carruth373b2b12014-09-06 10:00:01 +000050// Upgrade the declarations of intrinsic functions whose 8-bit immediate mask
51// arguments have changed their type from i32 to i8.
52static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID,
53 Function *&NewFn) {
54 // Check that the last argument is an i32.
55 Type *LastArgType = F->getFunctionType()->getParamType(
56 F->getFunctionType()->getNumParams() - 1);
57 if (!LastArgType->isIntegerTy(32))
58 return false;
59
60 // Move this function aside and map down.
61 F->setName(F->getName() + ".old");
62 NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
63 return true;
64}
65
Evan Cheng0e179d02007-12-17 22:33:23 +000066static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +000067 assert(F && "Illegal to upgrade a non-existent Function.");
68
Chandler Carruth7132e002007-08-04 01:51:18 +000069 // Quickly eliminate it, if it's not a candidate.
Chris Lattnerb372f662011-06-18 18:56:39 +000070 StringRef Name = F->getName();
71 if (Name.size() <= 8 || !Name.startswith("llvm."))
Evan Cheng0e179d02007-12-17 22:33:23 +000072 return false;
Chris Lattnerb372f662011-06-18 18:56:39 +000073 Name = Name.substr(5); // Strip off "llvm."
Chris Lattner0bcbde42011-11-27 08:42:07 +000074
Chris Lattnerb372f662011-06-18 18:56:39 +000075 switch (Name[0]) {
Chandler Carruth7132e002007-08-04 01:51:18 +000076 default: break;
Joel Jones43cb8782012-07-13 23:25:25 +000077 case 'a': {
78 if (Name.startswith("arm.neon.vclz")) {
79 Type* args[2] = {
Matt Arsenaultc4c92262013-07-20 17:46:00 +000080 F->arg_begin()->getType(),
Joel Jones43cb8782012-07-13 23:25:25 +000081 Type::getInt1Ty(F->getContext())
82 };
83 // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
84 // the end of the name. Change name from llvm.arm.neon.vclz.* to
85 // llvm.ctlz.*
86 FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
Matt Arsenaultc4c92262013-07-20 17:46:00 +000087 NewFn = Function::Create(fType, F->getLinkage(),
Joel Jones43cb8782012-07-13 23:25:25 +000088 "llvm.ctlz." + Name.substr(14), F->getParent());
89 return true;
90 }
Joel Jonesb84f7be2012-07-18 00:02:16 +000091 if (Name.startswith("arm.neon.vcnt")) {
92 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
93 F->arg_begin()->getType());
94 return true;
95 }
Jeroen Ketemaab99b592015-09-30 10:56:37 +000096 Regex vldRegex("^arm\\.neon\\.vld([1234]|[234]lane)\\.v[a-z0-9]*$");
97 if (vldRegex.match(Name)) {
98 auto fArgs = F->getFunctionType()->params();
99 SmallVector<Type *, 4> Tys(fArgs.begin(), fArgs.end());
100 // Can't use Intrinsic::getDeclaration here as the return types might
101 // then only be structurally equal.
102 FunctionType* fType = FunctionType::get(F->getReturnType(), Tys, false);
103 NewFn = Function::Create(fType, F->getLinkage(),
104 "llvm." + Name + ".p0i8", F->getParent());
105 return true;
106 }
107 Regex vstRegex("^arm\\.neon\\.vst([1234]|[234]lane)\\.v[a-z0-9]*$");
108 if (vstRegex.match(Name)) {
Craig Topper26260942015-10-18 05:15:34 +0000109 static const Intrinsic::ID StoreInts[] = {Intrinsic::arm_neon_vst1,
110 Intrinsic::arm_neon_vst2,
111 Intrinsic::arm_neon_vst3,
112 Intrinsic::arm_neon_vst4};
Jeroen Ketemaab99b592015-09-30 10:56:37 +0000113
Craig Topper26260942015-10-18 05:15:34 +0000114 static const Intrinsic::ID StoreLaneInts[] = {
115 Intrinsic::arm_neon_vst2lane, Intrinsic::arm_neon_vst3lane,
116 Intrinsic::arm_neon_vst4lane
117 };
Jeroen Ketemaab99b592015-09-30 10:56:37 +0000118
119 auto fArgs = F->getFunctionType()->params();
120 Type *Tys[] = {fArgs[0], fArgs[1]};
121 if (Name.find("lane") == StringRef::npos)
122 NewFn = Intrinsic::getDeclaration(F->getParent(),
123 StoreInts[fArgs.size() - 3], Tys);
124 else
125 NewFn = Intrinsic::getDeclaration(F->getParent(),
126 StoreLaneInts[fArgs.size() - 5], Tys);
127 return true;
128 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +0000129 if (Name == "aarch64.thread.pointer" || Name == "arm.thread.pointer") {
130 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::thread_pointer);
131 return true;
132 }
Joel Jones43cb8782012-07-13 23:25:25 +0000133 break;
134 }
Jeroen Ketemaab99b592015-09-30 10:56:37 +0000135
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000136 case 'c': {
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000137 if (Name.startswith("ctlz.") && F->arg_size() == 1) {
138 F->setName(Name + ".old");
Chandler Carruthd4a02402011-12-12 10:57:20 +0000139 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
140 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000141 return true;
142 }
143 if (Name.startswith("cttz.") && F->arg_size() == 1) {
144 F->setName(Name + ".old");
Chandler Carruthd4a02402011-12-12 10:57:20 +0000145 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
146 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000147 return true;
148 }
149 break;
150 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000151
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000152 case 'm': {
153 if (Name.startswith("masked.load.")) {
154 Type *Tys[] = { F->getReturnType(), F->arg_begin()->getType() };
155 if (F->getName() != Intrinsic::getName(Intrinsic::masked_load, Tys)) {
156 F->setName(Name + ".old");
157 NewFn = Intrinsic::getDeclaration(F->getParent(),
158 Intrinsic::masked_load,
159 Tys);
160 return true;
161 }
162 }
163 if (Name.startswith("masked.store.")) {
164 auto Args = F->getFunctionType()->params();
165 Type *Tys[] = { Args[0], Args[1] };
166 if (F->getName() != Intrinsic::getName(Intrinsic::masked_store, Tys)) {
167 F->setName(Name + ".old");
168 NewFn = Intrinsic::getDeclaration(F->getParent(),
169 Intrinsic::masked_store,
170 Tys);
171 return true;
172 }
173 }
174 break;
175 }
176
Matt Arsenaultfbcbce42013-10-07 18:06:48 +0000177 case 'o':
178 // We only need to change the name to match the mangling including the
179 // address space.
180 if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
181 Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
182 if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
183 F->setName(Name + ".old");
184 NewFn = Intrinsic::getDeclaration(F->getParent(),
185 Intrinsic::objectsize, Tys);
186 return true;
187 }
188 }
189 break;
190
Tim Shen00127562016-04-08 21:26:31 +0000191 case 's':
192 if (Name == "stackprotectorcheck") {
193 NewFn = nullptr;
194 return true;
195 }
196
Craig Topper3b1817d2012-02-03 06:10:55 +0000197 case 'x': {
Craig Topper5aebb862016-07-04 20:56:38 +0000198 bool IsX86 = Name.startswith("x86.");
199 if (IsX86)
200 Name = Name.substr(4);
201
202 if (IsX86 &&
203 (Name.startswith("sse2.pcmpeq.") ||
204 Name.startswith("sse2.pcmpgt.") ||
205 Name.startswith("avx2.pcmpeq.") ||
206 Name.startswith("avx2.pcmpgt.") ||
207 Name.startswith("avx512.mask.pcmpeq.") ||
208 Name.startswith("avx512.mask.pcmpgt.") ||
209 Name == "sse41.pmaxsb" ||
210 Name == "sse2.pmaxs.w" ||
211 Name == "sse41.pmaxsd" ||
212 Name == "sse2.pmaxu.b" ||
213 Name == "sse41.pmaxuw" ||
214 Name == "sse41.pmaxud" ||
215 Name == "sse41.pminsb" ||
216 Name == "sse2.pmins.w" ||
217 Name == "sse41.pminsd" ||
218 Name == "sse2.pminu.b" ||
219 Name == "sse41.pminuw" ||
220 Name == "sse41.pminud" ||
221 Name.startswith("avx2.pmax") ||
222 Name.startswith("avx2.pmin") ||
223 Name.startswith("avx2.vbroadcast") ||
224 Name.startswith("avx2.pbroadcast") ||
225 Name.startswith("avx.vpermil.") ||
226 Name.startswith("sse2.pshuf") ||
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +0000227 Name.startswith("avx512.pbroadcast") ||
228 Name.startswith("avx512.mask.broadcast.s") ||
Craig Topper5aebb862016-07-04 20:56:38 +0000229 Name.startswith("avx512.mask.movddup") ||
230 Name.startswith("avx512.mask.movshdup") ||
231 Name.startswith("avx512.mask.movsldup") ||
232 Name.startswith("avx512.mask.pshuf.d.") ||
233 Name.startswith("avx512.mask.pshufl.w.") ||
234 Name.startswith("avx512.mask.pshufh.w.") ||
235 Name.startswith("avx512.mask.vpermil.p") ||
236 Name.startswith("avx512.mask.perm.df.") ||
237 Name.startswith("avx512.mask.perm.di.") ||
238 Name.startswith("avx512.mask.punpckl") ||
239 Name.startswith("avx512.mask.punpckh") ||
240 Name.startswith("avx512.mask.unpckl.") ||
241 Name.startswith("avx512.mask.unpckh.") ||
242 Name.startswith("sse41.pmovsx") ||
243 Name.startswith("sse41.pmovzx") ||
244 Name.startswith("avx2.pmovsx") ||
245 Name.startswith("avx2.pmovzx") ||
246 Name == "sse2.cvtdq2pd" ||
247 Name == "sse2.cvtps2pd" ||
248 Name == "avx.cvtdq2.pd.256" ||
249 Name == "avx.cvt.ps2.pd.256" ||
250 Name == "sse2.cvttps2dq" ||
251 Name.startswith("avx.cvtt.") ||
252 Name.startswith("avx.vinsertf128.") ||
253 Name == "avx2.vinserti128" ||
254 Name.startswith("avx.vextractf128.") ||
255 Name == "avx2.vextracti128" ||
256 Name.startswith("sse4a.movnt.") ||
257 Name.startswith("avx.movnt.") ||
258 Name == "sse2.storel.dq" ||
259 Name.startswith("sse.storeu.") ||
260 Name.startswith("sse2.storeu.") ||
261 Name.startswith("avx.storeu.") ||
262 Name.startswith("avx512.mask.storeu.p") ||
263 Name.startswith("avx512.mask.storeu.b.") ||
264 Name.startswith("avx512.mask.storeu.w.") ||
265 Name.startswith("avx512.mask.storeu.d.") ||
266 Name.startswith("avx512.mask.storeu.q.") ||
267 Name.startswith("avx512.mask.store.p") ||
268 Name.startswith("avx512.mask.store.b.") ||
269 Name.startswith("avx512.mask.store.w.") ||
270 Name.startswith("avx512.mask.store.d.") ||
271 Name.startswith("avx512.mask.store.q.") ||
272 Name.startswith("avx512.mask.loadu.p") ||
273 Name.startswith("avx512.mask.loadu.b.") ||
274 Name.startswith("avx512.mask.loadu.w.") ||
275 Name.startswith("avx512.mask.loadu.d.") ||
276 Name.startswith("avx512.mask.loadu.q.") ||
277 Name.startswith("avx512.mask.load.p") ||
278 Name.startswith("avx512.mask.load.b.") ||
279 Name.startswith("avx512.mask.load.w.") ||
280 Name.startswith("avx512.mask.load.d.") ||
281 Name.startswith("avx512.mask.load.q.") ||
282 Name == "sse42.crc32.64.8" ||
283 Name.startswith("avx.vbroadcast.s") ||
284 Name.startswith("avx512.mask.palignr.") ||
285 Name.startswith("sse2.psll.dq") ||
286 Name.startswith("sse2.psrl.dq") ||
287 Name.startswith("avx2.psll.dq") ||
288 Name.startswith("avx2.psrl.dq") ||
289 Name.startswith("avx512.psll.dq") ||
290 Name.startswith("avx512.psrl.dq") ||
291 Name == "sse41.pblendw" ||
292 Name.startswith("sse41.blendp") ||
293 Name.startswith("avx.blend.p") ||
294 Name == "avx2.pblendw" ||
295 Name.startswith("avx2.pblendd.") ||
296 Name == "avx2.vbroadcasti128" ||
297 Name == "xop.vpcmov" ||
298 (Name.startswith("xop.vpcom") && F->arg_size() == 2))) {
Craig Topperc6207612014-04-09 06:08:46 +0000299 NewFn = nullptr;
Craig Topper3b1817d2012-02-03 06:10:55 +0000300 return true;
301 }
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000302 // SSE4.1 ptest functions may have an old signature.
Craig Topper5aebb862016-07-04 20:56:38 +0000303 if (IsX86 && Name.startswith("sse41.ptest")) {
304 if (Name.substr(11) == "c")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000305 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000306 if (Name.substr(11) == "z")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000307 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000308 if (Name.substr(11) == "nzc")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000309 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
310 }
Sanjay Patel1c3eaec2015-02-28 22:25:06 +0000311 // Several blend and other instructions with masks used the wrong number of
Chandler Carruth373b2b12014-09-06 10:00:01 +0000312 // bits.
Craig Topper5aebb862016-07-04 20:56:38 +0000313 if (IsX86 && Name == "sse41.insertps")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000314 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
315 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000316 if (IsX86 && Name == "sse41.dppd")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000317 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
318 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000319 if (IsX86 && Name == "sse41.dpps")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000320 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
321 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000322 if (IsX86 && Name == "sse41.mpsadbw")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000323 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
324 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000325 if (IsX86 && Name == "avx.dp.ps.256")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000326 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
327 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000328 if (IsX86 && Name == "avx2.mpsadbw")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000329 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
330 NewFn);
Craig Topper29f2e952015-01-25 23:26:02 +0000331
Craig Topper71dc02d2012-06-13 07:18:53 +0000332 // frcz.ss/sd may need to have an argument dropped
Craig Topper5aebb862016-07-04 20:56:38 +0000333 if (IsX86 && Name.startswith("xop.vfrcz.ss") && F->arg_size() == 2) {
Craig Topper71dc02d2012-06-13 07:18:53 +0000334 F->setName(Name + ".old");
335 NewFn = Intrinsic::getDeclaration(F->getParent(),
336 Intrinsic::x86_xop_vfrcz_ss);
337 return true;
338 }
Craig Topper5aebb862016-07-04 20:56:38 +0000339 if (IsX86 && Name.startswith("xop.vfrcz.sd") && F->arg_size() == 2) {
Craig Topper71dc02d2012-06-13 07:18:53 +0000340 F->setName(Name + ".old");
341 NewFn = Intrinsic::getDeclaration(F->getParent(),
342 Intrinsic::x86_xop_vfrcz_sd);
343 return true;
344 }
Craig Topper720c7bd2012-06-03 08:07:25 +0000345 // Fix the FMA4 intrinsics to remove the 4
Craig Topper5aebb862016-07-04 20:56:38 +0000346 if (IsX86 && Name.startswith("fma4.")) {
347 F->setName("llvm.x86.fma" + Name.substr(5));
Craig Topper2c5ccd82012-06-03 16:48:52 +0000348 NewFn = F;
349 return true;
Craig Topper720c7bd2012-06-03 08:07:25 +0000350 }
Simon Pilgrime85506b2016-06-03 08:06:03 +0000351 // Upgrade any XOP PERMIL2 index operand still using a float/double vector.
Craig Topper5aebb862016-07-04 20:56:38 +0000352 if (IsX86 && Name.startswith("xop.vpermil2")) {
Simon Pilgrime85506b2016-06-03 08:06:03 +0000353 auto Params = F->getFunctionType()->params();
354 auto Idx = Params[2];
355 if (Idx->getScalarType()->isFloatingPointTy()) {
356 F->setName(Name + ".old");
357 unsigned IdxSize = Idx->getPrimitiveSizeInBits();
358 unsigned EltSize = Idx->getScalarSizeInBits();
359 Intrinsic::ID Permil2ID;
360 if (EltSize == 64 && IdxSize == 128)
361 Permil2ID = Intrinsic::x86_xop_vpermil2pd;
362 else if (EltSize == 32 && IdxSize == 128)
363 Permil2ID = Intrinsic::x86_xop_vpermil2ps;
364 else if (EltSize == 64 && IdxSize == 256)
365 Permil2ID = Intrinsic::x86_xop_vpermil2pd_256;
366 else
367 Permil2ID = Intrinsic::x86_xop_vpermil2ps_256;
368 NewFn = Intrinsic::getDeclaration(F->getParent(), Permil2ID);
369 return true;
370 }
371 }
Craig Topper3b1817d2012-02-03 06:10:55 +0000372 break;
373 }
Chris Lattnerb372f662011-06-18 18:56:39 +0000374 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000375
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000376 // This may not belong here. This function is effectively being overloaded
377 // to both detect an intrinsic which needs upgrading, and to provide the
378 // upgraded form of the intrinsic. We should perhaps have two separate
Chandler Carruth7132e002007-08-04 01:51:18 +0000379 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000380 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000381}
382
Evan Cheng0e179d02007-12-17 22:33:23 +0000383bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
Craig Topperc6207612014-04-09 06:08:46 +0000384 NewFn = nullptr;
Evan Cheng0e179d02007-12-17 22:33:23 +0000385 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Filipe Cabecinhas0011c582015-07-03 20:12:01 +0000386 assert(F != NewFn && "Intrinsic function upgraded to the same function");
Duncan Sands38ef3a82007-12-03 20:06:50 +0000387
388 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000389 if (NewFn)
390 F = NewFn;
Pete Cooper9e1d3352015-05-20 17:16:39 +0000391 if (Intrinsic::ID id = F->getIntrinsicID())
392 F->setAttributes(Intrinsic::getAttributes(F->getContext(), id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000393 return Upgraded;
394}
395
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000396bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000397 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000398 return false;
399}
400
Simon Pilgrimf7186822016-06-09 21:09:03 +0000401// Handles upgrading SSE2/AVX2/AVX512BW PSLLDQ intrinsics by converting them
Craig Topperb324e432015-02-18 06:24:44 +0000402// to byte shuffles.
403static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C,
Craig Topper7355ac32016-05-29 06:37:33 +0000404 Value *Op, unsigned Shift) {
405 Type *ResultTy = Op->getType();
406 unsigned NumElts = ResultTy->getVectorNumElements() * 8;
Craig Topperb324e432015-02-18 06:24:44 +0000407
408 // Bitcast from a 64-bit element type to a byte element type.
Craig Topper7355ac32016-05-29 06:37:33 +0000409 Type *VecTy = VectorType::get(Type::getInt8Ty(C), NumElts);
410 Op = Builder.CreateBitCast(Op, VecTy, "cast");
411
Craig Topperb324e432015-02-18 06:24:44 +0000412 // We'll be shuffling in zeroes.
Craig Topper7355ac32016-05-29 06:37:33 +0000413 Value *Res = Constant::getNullValue(VecTy);
Craig Topperb324e432015-02-18 06:24:44 +0000414
415 // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
416 // we'll just return the zero vector.
417 if (Shift < 16) {
Craig Topper99d1eab2016-06-12 00:41:19 +0000418 uint32_t Idxs[64];
Simon Pilgrimf7186822016-06-09 21:09:03 +0000419 // 256/512-bit version is split into 2/4 16-byte lanes.
Craig Topperb324e432015-02-18 06:24:44 +0000420 for (unsigned l = 0; l != NumElts; l += 16)
421 for (unsigned i = 0; i != 16; ++i) {
422 unsigned Idx = NumElts + i - Shift;
423 if (Idx < NumElts)
424 Idx -= NumElts - 16; // end of lane, switch operand.
Craig Topper7355ac32016-05-29 06:37:33 +0000425 Idxs[l + i] = Idx + l;
Craig Topperb324e432015-02-18 06:24:44 +0000426 }
427
Craig Topper7355ac32016-05-29 06:37:33 +0000428 Res = Builder.CreateShuffleVector(Res, Op, makeArrayRef(Idxs, NumElts));
Craig Topperb324e432015-02-18 06:24:44 +0000429 }
430
431 // Bitcast back to a 64-bit element type.
Craig Topper7355ac32016-05-29 06:37:33 +0000432 return Builder.CreateBitCast(Res, ResultTy, "cast");
Craig Topperb324e432015-02-18 06:24:44 +0000433}
434
Craig Topperea703ae2016-06-13 02:36:42 +0000435// Handles upgrading SSE2/AVX2/AVX512BW PSRLDQ intrinsics by converting them
436// to byte shuffles.
437static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C,
438 Value *Op,
439 unsigned Shift) {
440 Type *ResultTy = Op->getType();
441 unsigned NumElts = ResultTy->getVectorNumElements() * 8;
442
443 // Bitcast from a 64-bit element type to a byte element type.
444 Type *VecTy = VectorType::get(Type::getInt8Ty(C), NumElts);
445 Op = Builder.CreateBitCast(Op, VecTy, "cast");
446
447 // We'll be shuffling in zeroes.
448 Value *Res = Constant::getNullValue(VecTy);
449
450 // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
451 // we'll just return the zero vector.
452 if (Shift < 16) {
453 uint32_t Idxs[64];
454 // 256/512-bit version is split into 2/4 16-byte lanes.
455 for (unsigned l = 0; l != NumElts; l += 16)
456 for (unsigned i = 0; i != 16; ++i) {
457 unsigned Idx = i + Shift;
458 if (Idx >= 16)
459 Idx += NumElts - 16; // end of lane, switch operand.
460 Idxs[l + i] = Idx + l;
461 }
462
463 Res = Builder.CreateShuffleVector(Op, Res, makeArrayRef(Idxs, NumElts));
464 }
465
466 // Bitcast back to a 64-bit element type.
467 return Builder.CreateBitCast(Res, ResultTy, "cast");
468}
469
470static Value *getX86MaskVec(IRBuilder<> &Builder, Value *Mask,
471 unsigned NumElts) {
472 llvm::VectorType *MaskTy = llvm::VectorType::get(Builder.getInt1Ty(),
473 cast<IntegerType>(Mask->getType())->getBitWidth());
474 Mask = Builder.CreateBitCast(Mask, MaskTy);
475
476 // If we have less than 8 elements, then the starting mask was an i8 and
477 // we need to extract down to the right number of elements.
478 if (NumElts < 8) {
479 uint32_t Indices[4];
480 for (unsigned i = 0; i != NumElts; ++i)
481 Indices[i] = i;
482 Mask = Builder.CreateShuffleVector(Mask, Mask,
483 makeArrayRef(Indices, NumElts),
484 "extract");
485 }
486
487 return Mask;
488}
489
490static Value *EmitX86Select(IRBuilder<> &Builder, Value *Mask,
491 Value *Op0, Value *Op1) {
492 // If the mask is all ones just emit the align operation.
493 if (const auto *C = dyn_cast<Constant>(Mask))
494 if (C->isAllOnesValue())
495 return Op0;
496
497 Mask = getX86MaskVec(Builder, Mask, Op0->getType()->getVectorNumElements());
498 return Builder.CreateSelect(Mask, Op0, Op1);
499}
500
Craig Topper33350cc2016-06-06 06:12:54 +0000501static Value *UpgradeX86PALIGNRIntrinsics(IRBuilder<> &Builder, LLVMContext &C,
502 Value *Op0, Value *Op1, Value *Shift,
503 Value *Passthru, Value *Mask) {
504 unsigned ShiftVal = cast<llvm::ConstantInt>(Shift)->getZExtValue();
505
506 unsigned NumElts = Op0->getType()->getVectorNumElements();
507 assert(NumElts % 16 == 0);
508
509 // If palignr is shifting the pair of vectors more than the size of two
510 // lanes, emit zero.
511 if (ShiftVal >= 32)
512 return llvm::Constant::getNullValue(Op0->getType());
513
514 // If palignr is shifting the pair of input vectors more than one lane,
515 // but less than two lanes, convert to shifting in zeroes.
516 if (ShiftVal > 16) {
517 ShiftVal -= 16;
518 Op1 = Op0;
519 Op0 = llvm::Constant::getNullValue(Op0->getType());
520 }
521
Craig Topper99d1eab2016-06-12 00:41:19 +0000522 uint32_t Indices[64];
Craig Topper33350cc2016-06-06 06:12:54 +0000523 // 256-bit palignr operates on 128-bit lanes so we need to handle that
524 for (unsigned l = 0; l != NumElts; l += 16) {
525 for (unsigned i = 0; i != 16; ++i) {
526 unsigned Idx = ShiftVal + i;
527 if (Idx >= 16)
528 Idx += NumElts - 16; // End of lane, switch operand.
529 Indices[l + i] = Idx + l;
530 }
531 }
532
533 Value *Align = Builder.CreateShuffleVector(Op1, Op0,
534 makeArrayRef(Indices, NumElts),
535 "palignr");
536
Craig Topperea703ae2016-06-13 02:36:42 +0000537 return EmitX86Select(Builder, Mask, Align, Passthru);
Craig Topperb324e432015-02-18 06:24:44 +0000538}
539
Craig Topper50f85c22016-05-31 01:50:02 +0000540static Value *UpgradeMaskedStore(IRBuilder<> &Builder, LLVMContext &C,
541 Value *Ptr, Value *Data, Value *Mask,
542 bool Aligned) {
543 // Cast the pointer to the right type.
544 Ptr = Builder.CreateBitCast(Ptr,
545 llvm::PointerType::getUnqual(Data->getType()));
546 unsigned Align =
547 Aligned ? cast<VectorType>(Data->getType())->getBitWidth() / 8 : 1;
548
549 // If the mask is all ones just emit a regular store.
550 if (const auto *C = dyn_cast<Constant>(Mask))
551 if (C->isAllOnesValue())
552 return Builder.CreateAlignedStore(Data, Ptr, Align);
553
554 // Convert the mask from an integer type to a vector of i1.
555 unsigned NumElts = Data->getType()->getVectorNumElements();
Craig Topperea703ae2016-06-13 02:36:42 +0000556 Mask = getX86MaskVec(Builder, Mask, NumElts);
Craig Topper50f85c22016-05-31 01:50:02 +0000557 return Builder.CreateMaskedStore(Data, Ptr, Align, Mask);
558}
559
Craig Topperf10fbfa2016-06-02 04:19:36 +0000560static Value *UpgradeMaskedLoad(IRBuilder<> &Builder, LLVMContext &C,
561 Value *Ptr, Value *Passthru, Value *Mask,
562 bool Aligned) {
563 // Cast the pointer to the right type.
564 Ptr = Builder.CreateBitCast(Ptr,
565 llvm::PointerType::getUnqual(Passthru->getType()));
566 unsigned Align =
567 Aligned ? cast<VectorType>(Passthru->getType())->getBitWidth() / 8 : 1;
568
569 // If the mask is all ones just emit a regular store.
570 if (const auto *C = dyn_cast<Constant>(Mask))
571 if (C->isAllOnesValue())
572 return Builder.CreateAlignedLoad(Ptr, Align);
573
574 // Convert the mask from an integer type to a vector of i1.
575 unsigned NumElts = Passthru->getType()->getVectorNumElements();
Craig Topperea703ae2016-06-13 02:36:42 +0000576 Mask = getX86MaskVec(Builder, Mask, NumElts);
Craig Topperf10fbfa2016-06-02 04:19:36 +0000577 return Builder.CreateMaskedLoad(Ptr, Align, Mask, Passthru);
578}
579
Sanjay Patel51ab7572016-06-16 15:48:30 +0000580static Value *upgradeIntMinMax(IRBuilder<> &Builder, CallInst &CI,
581 ICmpInst::Predicate Pred) {
582 Value *Op0 = CI.getArgOperand(0);
583 Value *Op1 = CI.getArgOperand(1);
584 Value *Cmp = Builder.CreateICmp(Pred, Op0, Op1);
585 return Builder.CreateSelect(Cmp, Op0, Op1);
586}
587
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000588static Value *upgradeMaskedCompare(IRBuilder<> &Builder, CallInst &CI,
589 ICmpInst::Predicate Pred) {
590 Value *Op0 = CI.getArgOperand(0);
591 unsigned NumElts = Op0->getType()->getVectorNumElements();
592 Value *Cmp = Builder.CreateICmp(Pred, Op0, CI.getArgOperand(1));
593
594 Value *Mask = CI.getArgOperand(2);
595 const auto *C = dyn_cast<Constant>(Mask);
596 if (!C || !C->isAllOnesValue())
597 Cmp = Builder.CreateAnd(Cmp, getX86MaskVec(Builder, Mask, NumElts));
598
599 if (NumElts < 8) {
600 uint32_t Indices[8];
601 for (unsigned i = 0; i != NumElts; ++i)
602 Indices[i] = i;
603 for (unsigned i = NumElts; i != 8; ++i)
Craig Topperd5d2a352016-07-07 06:11:07 +0000604 Indices[i] = NumElts + i % NumElts;
605 Cmp = Builder.CreateShuffleVector(Cmp,
606 Constant::getNullValue(Cmp->getType()),
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000607 Indices);
608 }
609 return Builder.CreateBitCast(Cmp, IntegerType::get(CI.getContext(),
610 std::max(NumElts, 8U)));
611}
612
Sanjay Patel595098f2016-06-15 22:01:28 +0000613/// Upgrade a call to an old intrinsic. All argument and return casting must be
614/// provided to seamlessly integrate with existing context.
Chandler Carruth7132e002007-08-04 01:51:18 +0000615void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000616 Function *F = CI->getCalledFunction();
Nick Lewycky2eb3ade2011-12-12 22:59:34 +0000617 LLVMContext &C = CI->getContext();
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000618 IRBuilder<> Builder(C);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000619 Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000620
Craig Topper3b1817d2012-02-03 06:10:55 +0000621 assert(F && "Intrinsic call is not direct?");
622
623 if (!NewFn) {
624 // Get the Function's name.
625 StringRef Name = F->getName();
626
Craig Topper5aebb862016-07-04 20:56:38 +0000627 assert(Name.startswith("llvm.") && "Intrinsic doesn't start with 'llvm.'");
628 Name = Name.substr(5);
629
630 bool IsX86 = Name.startswith("x86.");
631 if (IsX86)
632 Name = Name.substr(4);
633
Craig Topper3b1817d2012-02-03 06:10:55 +0000634 Value *Rep;
Sanjay Patel595098f2016-06-15 22:01:28 +0000635 // Upgrade packed integer vector compare intrinsics to compare instructions.
Craig Topper5aebb862016-07-04 20:56:38 +0000636 if (IsX86 && (Name.startswith("sse2.pcmpeq.") ||
637 Name.startswith("avx2.pcmpeq."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000638 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
639 "pcmpeq");
Craig Topper3b1817d2012-02-03 06:10:55 +0000640 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000641 } else if (IsX86 && (Name.startswith("sse2.pcmpgt.") ||
642 Name.startswith("avx2.pcmpgt."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000643 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
644 "pcmpgt");
Craig Topper3b1817d2012-02-03 06:10:55 +0000645 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000646 } else if (IsX86 && Name.startswith("avx512.mask.pcmpeq.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000647 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_EQ);
Craig Topper5aebb862016-07-04 20:56:38 +0000648 } else if (IsX86 && Name.startswith("avx512.mask.pcmpgt.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000649 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000650 } else if (IsX86 && (Name == "sse41.pmaxsb" ||
651 Name == "sse2.pmaxs.w" ||
652 Name == "sse41.pmaxsd" ||
653 Name.startswith("avx2.pmaxs"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000654 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000655 } else if (IsX86 && (Name == "sse2.pmaxu.b" ||
656 Name == "sse41.pmaxuw" ||
657 Name == "sse41.pmaxud" ||
658 Name.startswith("avx2.pmaxu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000659 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_UGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000660 } else if (IsX86 && (Name == "sse41.pminsb" ||
661 Name == "sse2.pmins.w" ||
662 Name == "sse41.pminsd" ||
663 Name.startswith("avx2.pmins"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000664 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SLT);
Craig Topper5aebb862016-07-04 20:56:38 +0000665 } else if (IsX86 && (Name == "sse2.pminu.b" ||
666 Name == "sse41.pminuw" ||
667 Name == "sse41.pminud" ||
668 Name.startswith("avx2.pminu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000669 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_ULT);
Craig Topper5aebb862016-07-04 20:56:38 +0000670 } else if (IsX86 && (Name == "sse2.cvtdq2pd" ||
671 Name == "sse2.cvtps2pd" ||
672 Name == "avx.cvtdq2.pd.256" ||
673 Name == "avx.cvt.ps2.pd.256")) {
Simon Pilgrim4298d062016-05-25 08:59:18 +0000674 // Lossless i32/float to double conversion.
675 // Extract the bottom elements if necessary and convert to double vector.
676 Value *Src = CI->getArgOperand(0);
677 VectorType *SrcTy = cast<VectorType>(Src->getType());
678 VectorType *DstTy = cast<VectorType>(CI->getType());
679 Rep = CI->getArgOperand(0);
680
681 unsigned NumDstElts = DstTy->getNumElements();
682 if (NumDstElts < SrcTy->getNumElements()) {
683 assert(NumDstElts == 2 && "Unexpected vector size");
Craig Topper99d1eab2016-06-12 00:41:19 +0000684 uint32_t ShuffleMask[2] = { 0, 1 };
685 Rep = Builder.CreateShuffleVector(Rep, UndefValue::get(SrcTy),
686 ShuffleMask);
Simon Pilgrim4298d062016-05-25 08:59:18 +0000687 }
688
689 bool Int2Double = (StringRef::npos != Name.find("cvtdq2"));
690 if (Int2Double)
691 Rep = Builder.CreateSIToFP(Rep, DstTy, "cvtdq2pd");
692 else
693 Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd");
Craig Topper5aebb862016-07-04 20:56:38 +0000694 } else if (IsX86 && (Name == "sse2.cvttps2dq" ||
695 Name.startswith("avx.cvtt."))) {
Simon Pilgrim0afd5a42016-06-02 10:55:21 +0000696 // Truncation (round to zero) float/double to i32 vector conversion.
697 Value *Src = CI->getArgOperand(0);
698 VectorType *DstTy = cast<VectorType>(CI->getType());
699 Rep = Builder.CreateFPToSI(Src, DstTy, "cvtt");
Craig Topper5aebb862016-07-04 20:56:38 +0000700 } else if (IsX86 && Name.startswith("sse4a.movnt.")) {
Simon Pilgrimf4b2af12016-06-18 02:38:26 +0000701 Module *M = F->getParent();
702 SmallVector<Metadata *, 1> Elts;
703 Elts.push_back(
704 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
705 MDNode *Node = MDNode::get(C, Elts);
706
707 Value *Arg0 = CI->getArgOperand(0);
708 Value *Arg1 = CI->getArgOperand(1);
709
710 // Nontemporal (unaligned) store of the 0'th element of the float/double
711 // vector.
712 Type *SrcEltTy = cast<VectorType>(Arg1->getType())->getElementType();
713 PointerType *EltPtrTy = PointerType::getUnqual(SrcEltTy);
714 Value *Addr = Builder.CreateBitCast(Arg0, EltPtrTy, "cast");
715 Value *Extract =
716 Builder.CreateExtractElement(Arg1, (uint64_t)0, "extractelement");
717
718 StoreInst *SI = Builder.CreateAlignedStore(Extract, Addr, 1);
719 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
720
721 // Remove intrinsic.
722 CI->eraseFromParent();
723 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000724 } else if (IsX86 && Name.startswith("avx.movnt.")) {
Craig Topper7daf8972012-05-08 06:58:15 +0000725 Module *M = F->getParent();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000726 SmallVector<Metadata *, 1> Elts;
727 Elts.push_back(
728 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
Craig Topper7daf8972012-05-08 06:58:15 +0000729 MDNode *Node = MDNode::get(C, Elts);
730
731 Value *Arg0 = CI->getArgOperand(0);
732 Value *Arg1 = CI->getArgOperand(1);
733
734 // Convert the type of the pointer to a pointer to the stored type.
735 Value *BC = Builder.CreateBitCast(Arg0,
736 PointerType::getUnqual(Arg1->getType()),
737 "cast");
Craig Topper29ce55d2016-05-30 22:54:12 +0000738 StoreInst *SI = Builder.CreateAlignedStore(Arg1, BC, 32);
Craig Topper7daf8972012-05-08 06:58:15 +0000739 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
Craig Topper7daf8972012-05-08 06:58:15 +0000740
741 // Remove intrinsic.
742 CI->eraseFromParent();
743 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000744 } else if (IsX86 && Name == "sse2.storel.dq") {
Craig Topper12e322a2016-05-25 06:56:32 +0000745 Value *Arg0 = CI->getArgOperand(0);
746 Value *Arg1 = CI->getArgOperand(1);
747
748 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
749 Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
750 Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0);
751 Value *BC = Builder.CreateBitCast(Arg0,
752 PointerType::getUnqual(Elt->getType()),
753 "cast");
Craig Topper29ce55d2016-05-30 22:54:12 +0000754 Builder.CreateAlignedStore(Elt, BC, 1);
Craig Topper12e322a2016-05-25 06:56:32 +0000755
756 // Remove intrinsic.
757 CI->eraseFromParent();
758 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000759 } else if (IsX86 && (Name.startswith("sse.storeu.") ||
760 Name.startswith("sse2.storeu.") ||
761 Name.startswith("avx.storeu."))) {
Craig Topper8287fd82016-05-30 23:15:56 +0000762 Value *Arg0 = CI->getArgOperand(0);
763 Value *Arg1 = CI->getArgOperand(1);
764
765 Arg0 = Builder.CreateBitCast(Arg0,
766 PointerType::getUnqual(Arg1->getType()),
767 "cast");
768 Builder.CreateAlignedStore(Arg1, Arg0, 1);
769
770 // Remove intrinsic.
771 CI->eraseFromParent();
772 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000773 } else if (IsX86 && (Name.startswith("avx512.mask.storeu.p") ||
774 Name.startswith("avx512.mask.storeu.b.") ||
775 Name.startswith("avx512.mask.storeu.w.") ||
776 Name.startswith("avx512.mask.storeu.d.") ||
777 Name.startswith("avx512.mask.storeu.q."))) {
Craig Topper50f85c22016-05-31 01:50:02 +0000778 UpgradeMaskedStore(Builder, C, CI->getArgOperand(0), CI->getArgOperand(1),
779 CI->getArgOperand(2), /*Aligned*/false);
780
781 // Remove intrinsic.
782 CI->eraseFromParent();
783 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000784 } else if (IsX86 && (Name.startswith("avx512.mask.store.p") ||
785 Name.startswith("avx512.mask.store.b.") ||
786 Name.startswith("avx512.mask.store.w.") ||
787 Name.startswith("avx512.mask.store.d.") ||
788 Name.startswith("avx512.mask.store.q."))) {
Craig Topper50f85c22016-05-31 01:50:02 +0000789 UpgradeMaskedStore(Builder, C, CI->getArgOperand(0), CI->getArgOperand(1),
790 CI->getArgOperand(2), /*Aligned*/true);
791
792 // Remove intrinsic.
793 CI->eraseFromParent();
794 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000795 } else if (IsX86 && (Name.startswith("avx512.mask.loadu.p") ||
796 Name.startswith("avx512.mask.loadu.b.") ||
797 Name.startswith("avx512.mask.loadu.w.") ||
798 Name.startswith("avx512.mask.loadu.d.") ||
799 Name.startswith("avx512.mask.loadu.q."))) {
Craig Topperf10fbfa2016-06-02 04:19:36 +0000800 Rep = UpgradeMaskedLoad(Builder, C, CI->getArgOperand(0),
801 CI->getArgOperand(1), CI->getArgOperand(2),
802 /*Aligned*/false);
Craig Topper5aebb862016-07-04 20:56:38 +0000803 } else if (IsX86 && (Name.startswith("avx512.mask.load.p") ||
804 Name.startswith("avx512.mask.load.b.") ||
805 Name.startswith("avx512.mask.load.w.") ||
806 Name.startswith("avx512.mask.load.d.") ||
807 Name.startswith("avx512.mask.load.q."))) {
Craig Topperf10fbfa2016-06-02 04:19:36 +0000808 Rep = UpgradeMaskedLoad(Builder, C, CI->getArgOperand(0),
809 CI->getArgOperand(1),CI->getArgOperand(2),
810 /*Aligned*/true);
Craig Topper5aebb862016-07-04 20:56:38 +0000811 } else if (IsX86 && Name.startswith("xop.vpcom")) {
Craig Topper3352ba52012-06-09 16:46:13 +0000812 Intrinsic::ID intID;
813 if (Name.endswith("ub"))
814 intID = Intrinsic::x86_xop_vpcomub;
815 else if (Name.endswith("uw"))
816 intID = Intrinsic::x86_xop_vpcomuw;
817 else if (Name.endswith("ud"))
818 intID = Intrinsic::x86_xop_vpcomud;
819 else if (Name.endswith("uq"))
820 intID = Intrinsic::x86_xop_vpcomuq;
821 else if (Name.endswith("b"))
822 intID = Intrinsic::x86_xop_vpcomb;
823 else if (Name.endswith("w"))
824 intID = Intrinsic::x86_xop_vpcomw;
825 else if (Name.endswith("d"))
826 intID = Intrinsic::x86_xop_vpcomd;
827 else if (Name.endswith("q"))
828 intID = Intrinsic::x86_xop_vpcomq;
829 else
830 llvm_unreachable("Unknown suffix");
831
Craig Topper5aebb862016-07-04 20:56:38 +0000832 Name = Name.substr(9); // strip off "xop.vpcom"
Craig Topper3352ba52012-06-09 16:46:13 +0000833 unsigned Imm;
834 if (Name.startswith("lt"))
835 Imm = 0;
836 else if (Name.startswith("le"))
837 Imm = 1;
838 else if (Name.startswith("gt"))
839 Imm = 2;
840 else if (Name.startswith("ge"))
841 Imm = 3;
842 else if (Name.startswith("eq"))
843 Imm = 4;
844 else if (Name.startswith("ne"))
845 Imm = 5;
Craig Topper3352ba52012-06-09 16:46:13 +0000846 else if (Name.startswith("false"))
Craig Toppere32546d2015-02-13 07:42:15 +0000847 Imm = 6;
848 else if (Name.startswith("true"))
Craig Topper3352ba52012-06-09 16:46:13 +0000849 Imm = 7;
850 else
851 llvm_unreachable("Unknown condition");
852
853 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
David Blaikieff6409d2015-05-18 22:13:54 +0000854 Rep =
855 Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1),
856 Builder.getInt8(Imm)});
Craig Topper5aebb862016-07-04 20:56:38 +0000857 } else if (IsX86 && Name == "xop.vpcmov") {
Simon Pilgrime88dc042015-11-03 20:27:01 +0000858 Value *Arg0 = CI->getArgOperand(0);
859 Value *Arg1 = CI->getArgOperand(1);
860 Value *Sel = CI->getArgOperand(2);
861 unsigned NumElts = CI->getType()->getVectorNumElements();
862 Constant *MinusOne = ConstantVector::getSplat(NumElts, Builder.getInt64(-1));
863 Value *NotSel = Builder.CreateXor(Sel, MinusOne);
864 Value *Sel0 = Builder.CreateAnd(Arg0, Sel);
865 Value *Sel1 = Builder.CreateAnd(Arg1, NotSel);
866 Rep = Builder.CreateOr(Sel0, Sel1);
Craig Topper5aebb862016-07-04 20:56:38 +0000867 } else if (IsX86 && Name == "sse42.crc32.64.8") {
Craig Topperef9e9932013-10-15 05:20:47 +0000868 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
869 Intrinsic::x86_sse42_crc32_32_8);
870 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
David Blaikieff6409d2015-05-18 22:13:54 +0000871 Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)});
Craig Topperef9e9932013-10-15 05:20:47 +0000872 Rep = Builder.CreateZExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000873 } else if (IsX86 && Name.startswith("avx.vbroadcast")) {
Adam Nemet39066802014-05-29 23:35:33 +0000874 // Replace broadcasts with a series of insertelements.
875 Type *VecTy = CI->getType();
876 Type *EltTy = VecTy->getVectorElementType();
877 unsigned EltNum = VecTy->getVectorNumElements();
878 Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
879 EltTy->getPointerTo());
David Blaikie0c28fd72015-05-20 21:46:30 +0000880 Value *Load = Builder.CreateLoad(EltTy, Cast);
Adam Nemet39066802014-05-29 23:35:33 +0000881 Type *I32Ty = Type::getInt32Ty(C);
882 Rep = UndefValue::get(VecTy);
883 for (unsigned I = 0; I < EltNum; ++I)
884 Rep = Builder.CreateInsertElement(Rep, Load,
885 ConstantInt::get(I32Ty, I));
Craig Topper5aebb862016-07-04 20:56:38 +0000886 } else if (IsX86 && (Name.startswith("sse41.pmovsx") ||
887 Name.startswith("sse41.pmovzx") ||
888 Name.startswith("avx2.pmovsx") ||
889 Name.startswith("avx2.pmovzx"))) {
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000890 VectorType *SrcTy = cast<VectorType>(CI->getArgOperand(0)->getType());
891 VectorType *DstTy = cast<VectorType>(CI->getType());
892 unsigned NumDstElts = DstTy->getNumElements();
893
Simon Pilgrim9602d672016-05-28 18:03:41 +0000894 // Extract a subvector of the first NumDstElts lanes and sign/zero extend.
Craig Topperc0a5fa02016-06-12 04:48:00 +0000895 SmallVector<uint32_t, 8> ShuffleMask(NumDstElts);
Craig Topper99d1eab2016-06-12 00:41:19 +0000896 for (unsigned i = 0; i != NumDstElts; ++i)
Craig Topperc0a5fa02016-06-12 04:48:00 +0000897 ShuffleMask[i] = i;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000898
899 Value *SV = Builder.CreateShuffleVector(
900 CI->getArgOperand(0), UndefValue::get(SrcTy), ShuffleMask);
Simon Pilgrim9602d672016-05-28 18:03:41 +0000901
902 bool DoSext = (StringRef::npos != Name.find("pmovsx"));
903 Rep = DoSext ? Builder.CreateSExt(SV, DstTy)
904 : Builder.CreateZExt(SV, DstTy);
Craig Topper5aebb862016-07-04 20:56:38 +0000905 } else if (IsX86 && Name == "avx2.vbroadcasti128") {
Juergen Ributzka1f7a1762015-03-04 00:13:25 +0000906 // Replace vbroadcasts with a vector shuffle.
David Blaikie0c28fd72015-05-20 21:46:30 +0000907 Type *VT = VectorType::get(Type::getInt64Ty(C), 2);
908 Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0),
909 PointerType::getUnqual(VT));
910 Value *Load = Builder.CreateLoad(VT, Op);
Craig Topper99d1eab2016-06-12 00:41:19 +0000911 uint32_t Idxs[4] = { 0, 1, 0, 1 };
Juergen Ributzka1f7a1762015-03-04 00:13:25 +0000912 Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
Sanjay Patel2db6d382015-03-12 15:27:07 +0000913 Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +0000914 } else if (IsX86 && (Name.startswith("avx2.pbroadcast") ||
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +0000915 Name.startswith("avx2.vbroadcast") ||
916 Name.startswith("avx512.pbroadcast") ||
917 Name.startswith("avx512.mask.broadcast.s"))) {
Ahmed Bougacha1a4987052015-08-20 20:36:19 +0000918 // Replace vp?broadcasts with a vector shuffle.
919 Value *Op = CI->getArgOperand(0);
920 unsigned NumElts = CI->getType()->getVectorNumElements();
921 Type *MaskTy = VectorType::get(Type::getInt32Ty(C), NumElts);
922 Rep = Builder.CreateShuffleVector(Op, UndefValue::get(Op->getType()),
923 Constant::getNullValue(MaskTy));
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +0000924
925 if (CI->getNumArgOperands() == 3)
926 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
927 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +0000928 } else if (IsX86 && Name.startswith("avx512.mask.palignr.")) {
Craig Topper33350cc2016-06-06 06:12:54 +0000929 Rep = UpgradeX86PALIGNRIntrinsics(Builder, C, CI->getArgOperand(0),
930 CI->getArgOperand(1),
931 CI->getArgOperand(2),
932 CI->getArgOperand(3),
933 CI->getArgOperand(4));
Craig Topper5aebb862016-07-04 20:56:38 +0000934 } else if (IsX86 && (Name == "sse2.psll.dq" ||
935 Name == "avx2.psll.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +0000936 // 128/256-bit shift left specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +0000937 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000938 Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +0000939 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +0000940 } else if (IsX86 && (Name == "sse2.psrl.dq" ||
941 Name == "avx2.psrl.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +0000942 // 128/256-bit shift right specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +0000943 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000944 Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +0000945 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +0000946 } else if (IsX86 && (Name == "sse2.psll.dq.bs" ||
947 Name == "avx2.psll.dq.bs" ||
948 Name == "avx512.psll.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +0000949 // 128/256/512-bit shift left specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +0000950 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000951 Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +0000952 } else if (IsX86 && (Name == "sse2.psrl.dq.bs" ||
953 Name == "avx2.psrl.dq.bs" ||
954 Name == "avx512.psrl.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +0000955 // 128/256/512-bit shift right specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +0000956 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000957 Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +0000958 } else if (IsX86 && (Name == "sse41.pblendw" ||
959 Name.startswith("sse41.blendp") ||
960 Name.startswith("avx.blend.p") ||
961 Name == "avx2.pblendw" ||
962 Name.startswith("avx2.pblendd."))) {
Craig Topper782d6202015-02-28 19:33:17 +0000963 Value *Op0 = CI->getArgOperand(0);
964 Value *Op1 = CI->getArgOperand(1);
965 unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue();
966 VectorType *VecTy = cast<VectorType>(CI->getType());
967 unsigned NumElts = VecTy->getNumElements();
968
Craig Topperc0a5fa02016-06-12 04:48:00 +0000969 SmallVector<uint32_t, 16> Idxs(NumElts);
970 for (unsigned i = 0; i != NumElts; ++i)
971 Idxs[i] = ((Imm >> (i%8)) & 1) ? i + NumElts : i;
Craig Topper782d6202015-02-28 19:33:17 +0000972
Craig Topper2f561822016-06-12 01:05:59 +0000973 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +0000974 } else if (IsX86 && (Name.startswith("avx.vinsertf128.") ||
975 Name == "avx2.vinserti128")) {
Sanjay Patel19792fb2015-03-10 16:08:36 +0000976 Value *Op0 = CI->getArgOperand(0);
977 Value *Op1 = CI->getArgOperand(1);
978 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
979 VectorType *VecTy = cast<VectorType>(CI->getType());
980 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000981
Sanjay Patel19792fb2015-03-10 16:08:36 +0000982 // Mask off the high bits of the immediate value; hardware ignores those.
983 Imm = Imm & 1;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000984
Sanjay Patel19792fb2015-03-10 16:08:36 +0000985 // Extend the second operand into a vector that is twice as big.
986 Value *UndefV = UndefValue::get(Op1->getType());
Craig Topperc0a5fa02016-06-12 04:48:00 +0000987 SmallVector<uint32_t, 8> Idxs(NumElts);
988 for (unsigned i = 0; i != NumElts; ++i)
989 Idxs[i] = i;
Craig Topper2f561822016-06-12 01:05:59 +0000990 Rep = Builder.CreateShuffleVector(Op1, UndefV, Idxs);
Sanjay Patel19792fb2015-03-10 16:08:36 +0000991
992 // Insert the second operand into the first operand.
993
994 // Note that there is no guarantee that instruction lowering will actually
995 // produce a vinsertf128 instruction for the created shuffles. In
996 // particular, the 0 immediate case involves no lane changes, so it can
997 // be handled as a blend.
998
999 // Example of shuffle mask for 32-bit elements:
1000 // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
1001 // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 >
1002
Sanjay Patel19792fb2015-03-10 16:08:36 +00001003 // The low half of the result is either the low half of the 1st operand
1004 // or the low half of the 2nd operand (the inserted vector).
Craig Topperc0a5fa02016-06-12 04:48:00 +00001005 for (unsigned i = 0; i != NumElts / 2; ++i)
1006 Idxs[i] = Imm ? i : (i + NumElts);
Sanjay Patel19792fb2015-03-10 16:08:36 +00001007 // The high half of the result is either the low half of the 2nd operand
1008 // (the inserted vector) or the high half of the 1st operand.
Craig Topperc0a5fa02016-06-12 04:48:00 +00001009 for (unsigned i = NumElts / 2; i != NumElts; ++i)
1010 Idxs[i] = Imm ? (i + NumElts / 2) : i;
Craig Topper2f561822016-06-12 01:05:59 +00001011 Rep = Builder.CreateShuffleVector(Op0, Rep, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001012 } else if (IsX86 && (Name.startswith("avx.vextractf128.") ||
1013 Name == "avx2.vextracti128")) {
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001014 Value *Op0 = CI->getArgOperand(0);
1015 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1016 VectorType *VecTy = cast<VectorType>(CI->getType());
1017 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001018
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001019 // Mask off the high bits of the immediate value; hardware ignores those.
1020 Imm = Imm & 1;
1021
1022 // Get indexes for either the high half or low half of the input vector.
Craig Topper2f561822016-06-12 01:05:59 +00001023 SmallVector<uint32_t, 4> Idxs(NumElts);
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001024 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topper2f561822016-06-12 01:05:59 +00001025 Idxs[i] = Imm ? (i + NumElts) : i;
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001026 }
1027
1028 Value *UndefV = UndefValue::get(Op0->getType());
Craig Topper2f561822016-06-12 01:05:59 +00001029 Rep = Builder.CreateShuffleVector(Op0, UndefV, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001030 } else if (!IsX86 && Name == "stackprotectorcheck") {
Tim Shen00127562016-04-08 21:26:31 +00001031 Rep = nullptr;
Craig Topper5aebb862016-07-04 20:56:38 +00001032 } else if (IsX86 && (Name.startswith("avx512.mask.perm.df.") ||
1033 Name.startswith("avx512.mask.perm.di."))) {
Simon Pilgrim02d435d2016-07-04 14:19:05 +00001034 Value *Op0 = CI->getArgOperand(0);
1035 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1036 VectorType *VecTy = cast<VectorType>(CI->getType());
1037 unsigned NumElts = VecTy->getNumElements();
1038
1039 SmallVector<uint32_t, 8> Idxs(NumElts);
1040 for (unsigned i = 0; i != NumElts; ++i)
1041 Idxs[i] = (i & ~0x3) + ((Imm >> (2 * (i & 0x3))) & 3);
1042
1043 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1044
1045 if (CI->getNumArgOperands() == 4)
1046 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1047 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001048 } else if (IsX86 && (Name.startswith("avx.vpermil.") ||
1049 Name == "sse2.pshuf.d" ||
1050 Name.startswith("avx512.mask.vpermil.p") ||
1051 Name.startswith("avx512.mask.pshuf.d."))) {
Craig Topper8a105052016-06-12 03:10:47 +00001052 Value *Op0 = CI->getArgOperand(0);
1053 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1054 VectorType *VecTy = cast<VectorType>(CI->getType());
1055 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9fca3002016-07-04 12:40:54 +00001056 // Calculate the size of each index in the immediate.
Craig Topper8a105052016-06-12 03:10:47 +00001057 unsigned IdxSize = 64 / VecTy->getScalarSizeInBits();
1058 unsigned IdxMask = ((1 << IdxSize) - 1);
1059
1060 SmallVector<uint32_t, 8> Idxs(NumElts);
1061 // Lookup the bits for this element, wrapping around the immediate every
1062 // 8-bits. Elements are grouped into sets of 2 or 4 elements so we need
1063 // to offset by the first index of each group.
1064 for (unsigned i = 0; i != NumElts; ++i)
1065 Idxs[i] = ((Imm >> ((i * IdxSize) % 8)) & IdxMask) | (i & ~IdxMask);
1066
1067 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001068
1069 if (CI->getNumArgOperands() == 4)
1070 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1071 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001072 } else if (IsX86 && (Name == "sse2.pshufl.w" ||
1073 Name.startswith("avx512.mask.pshufl.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001074 Value *Op0 = CI->getArgOperand(0);
1075 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1076 unsigned NumElts = CI->getType()->getVectorNumElements();
1077
1078 SmallVector<uint32_t, 16> Idxs(NumElts);
1079 for (unsigned l = 0; l != NumElts; l += 8) {
1080 for (unsigned i = 0; i != 4; ++i)
1081 Idxs[i + l] = ((Imm >> (2 * i)) & 0x3) + l;
1082 for (unsigned i = 4; i != 8; ++i)
1083 Idxs[i + l] = i + l;
1084 }
1085
1086 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001087
1088 if (CI->getNumArgOperands() == 4)
1089 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1090 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001091 } else if (IsX86 && (Name == "sse2.pshufh.w" ||
1092 Name.startswith("avx512.mask.pshufh.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001093 Value *Op0 = CI->getArgOperand(0);
1094 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1095 unsigned NumElts = CI->getType()->getVectorNumElements();
1096
1097 SmallVector<uint32_t, 16> Idxs(NumElts);
1098 for (unsigned l = 0; l != NumElts; l += 8) {
1099 for (unsigned i = 0; i != 4; ++i)
1100 Idxs[i + l] = i + l;
1101 for (unsigned i = 0; i != 4; ++i)
1102 Idxs[i + l + 4] = ((Imm >> (2 * i)) & 0x3) + 4 + l;
1103 }
1104
1105 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001106
1107 if (CI->getNumArgOperands() == 4)
1108 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1109 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001110 } else if (IsX86 && (Name.startswith("avx512.mask.movddup") ||
1111 Name.startswith("avx512.mask.movshdup") ||
1112 Name.startswith("avx512.mask.movsldup"))) {
Simon Pilgrim19adee92016-07-02 14:42:35 +00001113 Value *Op0 = CI->getArgOperand(0);
1114 unsigned NumElts = CI->getType()->getVectorNumElements();
1115 unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1116
1117 unsigned Offset = 0;
Craig Topper5aebb862016-07-04 20:56:38 +00001118 if (Name.startswith("avx512.mask.movshdup."))
Simon Pilgrim19adee92016-07-02 14:42:35 +00001119 Offset = 1;
1120
1121 SmallVector<uint32_t, 16> Idxs(NumElts);
1122 for (unsigned l = 0; l != NumElts; l += NumLaneElts)
1123 for (unsigned i = 0; i != NumLaneElts; i += 2) {
1124 Idxs[i + l + 0] = i + l + Offset;
1125 Idxs[i + l + 1] = i + l + Offset;
1126 }
1127
1128 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1129
1130 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
1131 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +00001132 } else if (IsX86 && (Name.startswith("avx512.mask.punpckl") ||
1133 Name.startswith("avx512.mask.unpckl."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001134 Value *Op0 = CI->getArgOperand(0);
1135 Value *Op1 = CI->getArgOperand(1);
1136 int NumElts = CI->getType()->getVectorNumElements();
1137 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1138
1139 SmallVector<uint32_t, 64> Idxs(NumElts);
1140 for (int l = 0; l != NumElts; l += NumLaneElts)
1141 for (int i = 0; i != NumLaneElts; ++i)
1142 Idxs[i + l] = l + (i / 2) + NumElts * (i % 2);
1143
1144 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1145
1146 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1147 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001148 } else if (IsX86 && (Name.startswith("avx512.mask.punpckh") ||
1149 Name.startswith("avx512.mask.unpckh."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001150 Value *Op0 = CI->getArgOperand(0);
1151 Value *Op1 = CI->getArgOperand(1);
1152 int NumElts = CI->getType()->getVectorNumElements();
1153 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1154
1155 SmallVector<uint32_t, 64> Idxs(NumElts);
1156 for (int l = 0; l != NumElts; l += NumLaneElts)
1157 for (int i = 0; i != NumLaneElts; ++i)
1158 Idxs[i + l] = (NumLaneElts / 2) + l + (i / 2) + NumElts * (i % 2);
1159
1160 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1161
1162 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1163 CI->getArgOperand(2));
Craig Topper3b1817d2012-02-03 06:10:55 +00001164 } else {
Craig Topper8a105052016-06-12 03:10:47 +00001165 llvm_unreachable("Unknown function for CallInst upgrade.");
Craig Topper3b1817d2012-02-03 06:10:55 +00001166 }
1167
Tim Shen00127562016-04-08 21:26:31 +00001168 if (Rep)
1169 CI->replaceAllUsesWith(Rep);
Craig Topper3b1817d2012-02-03 06:10:55 +00001170 CI->eraseFromParent();
1171 return;
1172 }
1173
Yaron Kerend1fdbe72015-03-30 16:10:39 +00001174 std::string Name = CI->getName();
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001175 if (!Name.empty())
1176 CI->setName(Name + ".old");
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001177
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001178 switch (NewFn->getIntrinsicID()) {
1179 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +00001180 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001181
Jeroen Ketemaab99b592015-09-30 10:56:37 +00001182 case Intrinsic::arm_neon_vld1:
1183 case Intrinsic::arm_neon_vld2:
1184 case Intrinsic::arm_neon_vld3:
1185 case Intrinsic::arm_neon_vld4:
1186 case Intrinsic::arm_neon_vld2lane:
1187 case Intrinsic::arm_neon_vld3lane:
1188 case Intrinsic::arm_neon_vld4lane:
1189 case Intrinsic::arm_neon_vst1:
1190 case Intrinsic::arm_neon_vst2:
1191 case Intrinsic::arm_neon_vst3:
1192 case Intrinsic::arm_neon_vst4:
1193 case Intrinsic::arm_neon_vst2lane:
1194 case Intrinsic::arm_neon_vst3lane:
1195 case Intrinsic::arm_neon_vst4lane: {
1196 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1197 CI->arg_operands().end());
1198 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1199 CI->eraseFromParent();
1200 return;
1201 }
1202
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001203 case Intrinsic::ctlz:
Nuno Lopesad40c0a2012-05-22 15:25:31 +00001204 case Intrinsic::cttz:
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001205 assert(CI->getNumArgOperands() == 1 &&
1206 "Mismatch between function args and call args");
David Blaikieff6409d2015-05-18 22:13:54 +00001207 CI->replaceAllUsesWith(Builder.CreateCall(
1208 NewFn, {CI->getArgOperand(0), Builder.getFalse()}, Name));
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001209 CI->eraseFromParent();
1210 return;
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001211
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001212 case Intrinsic::objectsize:
David Blaikieff6409d2015-05-18 22:13:54 +00001213 CI->replaceAllUsesWith(Builder.CreateCall(
1214 NewFn, {CI->getArgOperand(0), CI->getArgOperand(1)}, Name));
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001215 CI->eraseFromParent();
1216 return;
1217
Joel Jonesb84f7be2012-07-18 00:02:16 +00001218 case Intrinsic::ctpop: {
David Blaikieff6409d2015-05-18 22:13:54 +00001219 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {CI->getArgOperand(0)}));
Joel Jonesb84f7be2012-07-18 00:02:16 +00001220 CI->eraseFromParent();
1221 return;
1222 }
Joel Jones43cb8782012-07-13 23:25:25 +00001223
Craig Topper71dc02d2012-06-13 07:18:53 +00001224 case Intrinsic::x86_xop_vfrcz_ss:
1225 case Intrinsic::x86_xop_vfrcz_sd:
David Blaikieff6409d2015-05-18 22:13:54 +00001226 CI->replaceAllUsesWith(
1227 Builder.CreateCall(NewFn, {CI->getArgOperand(1)}, Name));
Craig Topper71dc02d2012-06-13 07:18:53 +00001228 CI->eraseFromParent();
1229 return;
1230
Simon Pilgrime85506b2016-06-03 08:06:03 +00001231 case Intrinsic::x86_xop_vpermil2pd:
1232 case Intrinsic::x86_xop_vpermil2ps:
1233 case Intrinsic::x86_xop_vpermil2pd_256:
1234 case Intrinsic::x86_xop_vpermil2ps_256: {
1235 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1236 CI->arg_operands().end());
1237 VectorType *FltIdxTy = cast<VectorType>(Args[2]->getType());
1238 VectorType *IntIdxTy = VectorType::getInteger(FltIdxTy);
1239 Args[2] = Builder.CreateBitCast(Args[2], IntIdxTy);
1240 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args, Name));
1241 CI->eraseFromParent();
1242 return;
1243 }
1244
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001245 case Intrinsic::x86_sse41_ptestc:
1246 case Intrinsic::x86_sse41_ptestz:
Craig Topper71dc02d2012-06-13 07:18:53 +00001247 case Intrinsic::x86_sse41_ptestnzc: {
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001248 // The arguments for these intrinsics used to be v4f32, and changed
1249 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
1250 // So, the only thing required is a bitcast for both arguments.
1251 // First, check the arguments have the old type.
1252 Value *Arg0 = CI->getArgOperand(0);
1253 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
1254 return;
1255
1256 // Old intrinsic, add bitcasts
1257 Value *Arg1 = CI->getArgOperand(1);
1258
David Blaikie5bacf372015-04-24 21:16:07 +00001259 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001260
David Blaikie5bacf372015-04-24 21:16:07 +00001261 Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast");
1262 Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
1263
David Blaikieff6409d2015-05-18 22:13:54 +00001264 CallInst *NewCall = Builder.CreateCall(NewFn, {BC0, BC1}, Name);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001265 CI->replaceAllUsesWith(NewCall);
1266 CI->eraseFromParent();
1267 return;
Evan Cheng0e179d02007-12-17 22:33:23 +00001268 }
Chandler Carruth373b2b12014-09-06 10:00:01 +00001269
Chandler Carruth373b2b12014-09-06 10:00:01 +00001270 case Intrinsic::x86_sse41_insertps:
1271 case Intrinsic::x86_sse41_dppd:
1272 case Intrinsic::x86_sse41_dpps:
1273 case Intrinsic::x86_sse41_mpsadbw:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001274 case Intrinsic::x86_avx_dp_ps_256:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001275 case Intrinsic::x86_avx2_mpsadbw: {
1276 // Need to truncate the last argument from i32 to i8 -- this argument models
1277 // an inherently 8-bit immediate operand to these x86 instructions.
1278 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1279 CI->arg_operands().end());
1280
1281 // Replace the last argument with a trunc.
1282 Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
1283
1284 CallInst *NewCall = Builder.CreateCall(NewFn, Args);
1285 CI->replaceAllUsesWith(NewCall);
1286 CI->eraseFromParent();
1287 return;
1288 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +00001289
1290 case Intrinsic::thread_pointer: {
1291 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {}));
1292 CI->eraseFromParent();
1293 return;
1294 }
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +00001295
1296 case Intrinsic::masked_load:
1297 case Intrinsic::masked_store: {
1298 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1299 CI->arg_operands().end());
1300 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1301 CI->eraseFromParent();
1302 return;
1303 }
Craig Topper71dc02d2012-06-13 07:18:53 +00001304 }
Chandler Carruth7132e002007-08-04 01:51:18 +00001305}
1306
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001307void llvm::UpgradeCallsToIntrinsic(Function *F) {
Chandler Carruth7132e002007-08-04 01:51:18 +00001308 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
1309
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001310 // Check if this function should be upgraded and get the replacement function
1311 // if there is one.
Chris Lattner80ed9dc2011-06-18 06:05:24 +00001312 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +00001313 if (UpgradeIntrinsicFunction(F, NewFn)) {
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001314 // Replace all users of the old function with the new function or new
1315 // instructions. This is not a range loop because the call is deleted.
1316 for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; )
Duncan P. N. Exon Smith93f53c42016-04-17 03:59:37 +00001317 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001318 UpgradeIntrinsicCall(CI, NewFn);
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001319
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001320 // Remove old function, no longer used, from the module.
1321 F->eraseFromParent();
Chandler Carruth7132e002007-08-04 01:51:18 +00001322 }
1323}
Devang Patel80ae3492009-08-28 23:24:31 +00001324
Manman Ren209b17c2013-09-28 00:22:27 +00001325void llvm::UpgradeInstWithTBAATag(Instruction *I) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001326 MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
Manman Ren209b17c2013-09-28 00:22:27 +00001327 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
1328 // Check if the tag uses struct-path aware TBAA format.
1329 if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
1330 return;
1331
1332 if (MD->getNumOperands() == 3) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001333 Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
Manman Ren209b17c2013-09-28 00:22:27 +00001334 MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
1335 // Create a MDNode <ScalarType, ScalarType, offset 0, const>
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001336 Metadata *Elts2[] = {ScalarType, ScalarType,
1337 ConstantAsMetadata::get(Constant::getNullValue(
1338 Type::getInt64Ty(I->getContext()))),
1339 MD->getOperand(2)};
Manman Ren209b17c2013-09-28 00:22:27 +00001340 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
1341 } else {
1342 // Create a MDNode <MD, MD, offset 0>
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001343 Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
1344 Type::getInt64Ty(I->getContext())))};
Manman Ren209b17c2013-09-28 00:22:27 +00001345 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
1346 }
1347}
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001348
1349Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
1350 Instruction *&Temp) {
1351 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001352 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001353
Craig Topperc6207612014-04-09 06:08:46 +00001354 Temp = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001355 Type *SrcTy = V->getType();
1356 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1357 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1358 LLVMContext &Context = V->getContext();
1359
1360 // We have no information about target data layout, so we assume that
1361 // the maximum pointer size is 64bit.
1362 Type *MidTy = Type::getInt64Ty(Context);
1363 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
1364
1365 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
1366 }
1367
Craig Topperc6207612014-04-09 06:08:46 +00001368 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001369}
1370
1371Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
1372 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001373 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001374
1375 Type *SrcTy = C->getType();
1376 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1377 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1378 LLVMContext &Context = C->getContext();
1379
1380 // We have no information about target data layout, so we assume that
1381 // the maximum pointer size is 64bit.
1382 Type *MidTy = Type::getInt64Ty(Context);
1383
1384 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
1385 DestTy);
1386 }
1387
Craig Topperc6207612014-04-09 06:08:46 +00001388 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001389}
Manman Ren8b4306c2013-12-02 21:29:56 +00001390
1391/// Check the debug info version number, if it is out-dated, drop the debug
1392/// info. Return true if module is modified.
1393bool llvm::UpgradeDebugInfo(Module &M) {
Manman Ren2ebfb422014-01-16 01:51:12 +00001394 unsigned Version = getDebugMetadataVersionFromModule(M);
1395 if (Version == DEBUG_METADATA_VERSION)
Manman Ren8b4306c2013-12-02 21:29:56 +00001396 return false;
1397
Manman Ren2ebfb422014-01-16 01:51:12 +00001398 bool RetCode = StripDebugInfo(M);
1399 if (RetCode) {
1400 DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
1401 M.getContext().diagnose(DiagVersion);
1402 }
1403 return RetCode;
Manman Ren8b4306c2013-12-02 21:29:56 +00001404}
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001405
Manman Renb5d7ff42016-05-25 23:14:48 +00001406bool llvm::UpgradeModuleFlags(Module &M) {
1407 const NamedMDNode *ModFlags = M.getModuleFlagsMetadata();
1408 if (!ModFlags)
1409 return false;
1410
1411 bool HasObjCFlag = false, HasClassProperties = false;
1412 for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
1413 MDNode *Op = ModFlags->getOperand(I);
1414 if (Op->getNumOperands() < 2)
1415 continue;
1416 MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1417 if (!ID)
1418 continue;
1419 if (ID->getString() == "Objective-C Image Info Version")
1420 HasObjCFlag = true;
1421 if (ID->getString() == "Objective-C Class Properties")
1422 HasClassProperties = true;
1423 }
1424 // "Objective-C Class Properties" is recently added for Objective-C. We
1425 // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module
1426 // flag of value 0, so we can correclty report error when trying to link
1427 // an ObjC bitcode without this module flag with an ObjC bitcode with this
1428 // module flag.
1429 if (HasObjCFlag && !HasClassProperties) {
1430 M.addModuleFlag(llvm::Module::Error, "Objective-C Class Properties",
1431 (uint32_t)0);
1432 return true;
1433 }
1434 return false;
1435}
1436
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00001437static bool isOldLoopArgument(Metadata *MD) {
1438 auto *T = dyn_cast_or_null<MDTuple>(MD);
1439 if (!T)
1440 return false;
1441 if (T->getNumOperands() < 1)
1442 return false;
1443 auto *S = dyn_cast_or_null<MDString>(T->getOperand(0));
1444 if (!S)
1445 return false;
1446 return S->getString().startswith("llvm.vectorizer.");
1447}
1448
1449static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) {
1450 StringRef OldPrefix = "llvm.vectorizer.";
1451 assert(OldTag.startswith(OldPrefix) && "Expected old prefix");
1452
1453 if (OldTag == "llvm.vectorizer.unroll")
1454 return MDString::get(C, "llvm.loop.interleave.count");
1455
1456 return MDString::get(
1457 C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size()))
1458 .str());
1459}
1460
1461static Metadata *upgradeLoopArgument(Metadata *MD) {
1462 auto *T = dyn_cast_or_null<MDTuple>(MD);
1463 if (!T)
1464 return MD;
1465 if (T->getNumOperands() < 1)
1466 return MD;
1467 auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0));
1468 if (!OldTag)
1469 return MD;
1470 if (!OldTag->getString().startswith("llvm.vectorizer."))
1471 return MD;
1472
1473 // This has an old tag. Upgrade it.
1474 SmallVector<Metadata *, 8> Ops;
1475 Ops.reserve(T->getNumOperands());
1476 Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString()));
1477 for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I)
1478 Ops.push_back(T->getOperand(I));
1479
1480 return MDTuple::get(T->getContext(), Ops);
1481}
1482
1483MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) {
1484 auto *T = dyn_cast<MDTuple>(&N);
1485 if (!T)
1486 return &N;
1487
1488 if (!llvm::any_of(T->operands(), isOldLoopArgument))
1489 return &N;
1490
1491 SmallVector<Metadata *, 8> Ops;
1492 Ops.reserve(T->getNumOperands());
1493 for (Metadata *MD : T->operands())
1494 Ops.push_back(upgradeLoopArgument(MD));
1495
1496 return MDTuple::get(T->getContext(), Ops);
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001497}