blob: 8fee0f35c3556f8f677dc2dfecb679637a0a0343 [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)
604 Indices[i] = NumElts;
605 Cmp = Builder.CreateShuffleVector(Cmp, UndefValue::get(Cmp->getType()),
606 Indices);
607 }
608 return Builder.CreateBitCast(Cmp, IntegerType::get(CI.getContext(),
609 std::max(NumElts, 8U)));
610}
611
Sanjay Patel595098f2016-06-15 22:01:28 +0000612/// Upgrade a call to an old intrinsic. All argument and return casting must be
613/// provided to seamlessly integrate with existing context.
Chandler Carruth7132e002007-08-04 01:51:18 +0000614void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000615 Function *F = CI->getCalledFunction();
Nick Lewycky2eb3ade2011-12-12 22:59:34 +0000616 LLVMContext &C = CI->getContext();
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000617 IRBuilder<> Builder(C);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000618 Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000619
Craig Topper3b1817d2012-02-03 06:10:55 +0000620 assert(F && "Intrinsic call is not direct?");
621
622 if (!NewFn) {
623 // Get the Function's name.
624 StringRef Name = F->getName();
625
Craig Topper5aebb862016-07-04 20:56:38 +0000626 assert(Name.startswith("llvm.") && "Intrinsic doesn't start with 'llvm.'");
627 Name = Name.substr(5);
628
629 bool IsX86 = Name.startswith("x86.");
630 if (IsX86)
631 Name = Name.substr(4);
632
Craig Topper3b1817d2012-02-03 06:10:55 +0000633 Value *Rep;
Sanjay Patel595098f2016-06-15 22:01:28 +0000634 // Upgrade packed integer vector compare intrinsics to compare instructions.
Craig Topper5aebb862016-07-04 20:56:38 +0000635 if (IsX86 && (Name.startswith("sse2.pcmpeq.") ||
636 Name.startswith("avx2.pcmpeq."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000637 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
638 "pcmpeq");
Craig Topper3b1817d2012-02-03 06:10:55 +0000639 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000640 } else if (IsX86 && (Name.startswith("sse2.pcmpgt.") ||
641 Name.startswith("avx2.pcmpgt."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000642 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
643 "pcmpgt");
Craig Topper3b1817d2012-02-03 06:10:55 +0000644 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000645 } else if (IsX86 && Name.startswith("avx512.mask.pcmpeq.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000646 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_EQ);
Craig Topper5aebb862016-07-04 20:56:38 +0000647 } else if (IsX86 && Name.startswith("avx512.mask.pcmpgt.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000648 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000649 } else if (IsX86 && (Name == "sse41.pmaxsb" ||
650 Name == "sse2.pmaxs.w" ||
651 Name == "sse41.pmaxsd" ||
652 Name.startswith("avx2.pmaxs"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000653 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000654 } else if (IsX86 && (Name == "sse2.pmaxu.b" ||
655 Name == "sse41.pmaxuw" ||
656 Name == "sse41.pmaxud" ||
657 Name.startswith("avx2.pmaxu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000658 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_UGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000659 } else if (IsX86 && (Name == "sse41.pminsb" ||
660 Name == "sse2.pmins.w" ||
661 Name == "sse41.pminsd" ||
662 Name.startswith("avx2.pmins"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000663 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SLT);
Craig Topper5aebb862016-07-04 20:56:38 +0000664 } else if (IsX86 && (Name == "sse2.pminu.b" ||
665 Name == "sse41.pminuw" ||
666 Name == "sse41.pminud" ||
667 Name.startswith("avx2.pminu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000668 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_ULT);
Craig Topper5aebb862016-07-04 20:56:38 +0000669 } else if (IsX86 && (Name == "sse2.cvtdq2pd" ||
670 Name == "sse2.cvtps2pd" ||
671 Name == "avx.cvtdq2.pd.256" ||
672 Name == "avx.cvt.ps2.pd.256")) {
Simon Pilgrim4298d062016-05-25 08:59:18 +0000673 // Lossless i32/float to double conversion.
674 // Extract the bottom elements if necessary and convert to double vector.
675 Value *Src = CI->getArgOperand(0);
676 VectorType *SrcTy = cast<VectorType>(Src->getType());
677 VectorType *DstTy = cast<VectorType>(CI->getType());
678 Rep = CI->getArgOperand(0);
679
680 unsigned NumDstElts = DstTy->getNumElements();
681 if (NumDstElts < SrcTy->getNumElements()) {
682 assert(NumDstElts == 2 && "Unexpected vector size");
Craig Topper99d1eab2016-06-12 00:41:19 +0000683 uint32_t ShuffleMask[2] = { 0, 1 };
684 Rep = Builder.CreateShuffleVector(Rep, UndefValue::get(SrcTy),
685 ShuffleMask);
Simon Pilgrim4298d062016-05-25 08:59:18 +0000686 }
687
688 bool Int2Double = (StringRef::npos != Name.find("cvtdq2"));
689 if (Int2Double)
690 Rep = Builder.CreateSIToFP(Rep, DstTy, "cvtdq2pd");
691 else
692 Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd");
Craig Topper5aebb862016-07-04 20:56:38 +0000693 } else if (IsX86 && (Name == "sse2.cvttps2dq" ||
694 Name.startswith("avx.cvtt."))) {
Simon Pilgrim0afd5a42016-06-02 10:55:21 +0000695 // Truncation (round to zero) float/double to i32 vector conversion.
696 Value *Src = CI->getArgOperand(0);
697 VectorType *DstTy = cast<VectorType>(CI->getType());
698 Rep = Builder.CreateFPToSI(Src, DstTy, "cvtt");
Craig Topper5aebb862016-07-04 20:56:38 +0000699 } else if (IsX86 && Name.startswith("sse4a.movnt.")) {
Simon Pilgrimf4b2af12016-06-18 02:38:26 +0000700 Module *M = F->getParent();
701 SmallVector<Metadata *, 1> Elts;
702 Elts.push_back(
703 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
704 MDNode *Node = MDNode::get(C, Elts);
705
706 Value *Arg0 = CI->getArgOperand(0);
707 Value *Arg1 = CI->getArgOperand(1);
708
709 // Nontemporal (unaligned) store of the 0'th element of the float/double
710 // vector.
711 Type *SrcEltTy = cast<VectorType>(Arg1->getType())->getElementType();
712 PointerType *EltPtrTy = PointerType::getUnqual(SrcEltTy);
713 Value *Addr = Builder.CreateBitCast(Arg0, EltPtrTy, "cast");
714 Value *Extract =
715 Builder.CreateExtractElement(Arg1, (uint64_t)0, "extractelement");
716
717 StoreInst *SI = Builder.CreateAlignedStore(Extract, Addr, 1);
718 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
719
720 // Remove intrinsic.
721 CI->eraseFromParent();
722 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000723 } else if (IsX86 && Name.startswith("avx.movnt.")) {
Craig Topper7daf8972012-05-08 06:58:15 +0000724 Module *M = F->getParent();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000725 SmallVector<Metadata *, 1> Elts;
726 Elts.push_back(
727 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
Craig Topper7daf8972012-05-08 06:58:15 +0000728 MDNode *Node = MDNode::get(C, Elts);
729
730 Value *Arg0 = CI->getArgOperand(0);
731 Value *Arg1 = CI->getArgOperand(1);
732
733 // Convert the type of the pointer to a pointer to the stored type.
734 Value *BC = Builder.CreateBitCast(Arg0,
735 PointerType::getUnqual(Arg1->getType()),
736 "cast");
Craig Topper29ce55d2016-05-30 22:54:12 +0000737 StoreInst *SI = Builder.CreateAlignedStore(Arg1, BC, 32);
Craig Topper7daf8972012-05-08 06:58:15 +0000738 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
Craig Topper7daf8972012-05-08 06:58:15 +0000739
740 // Remove intrinsic.
741 CI->eraseFromParent();
742 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000743 } else if (IsX86 && Name == "sse2.storel.dq") {
Craig Topper12e322a2016-05-25 06:56:32 +0000744 Value *Arg0 = CI->getArgOperand(0);
745 Value *Arg1 = CI->getArgOperand(1);
746
747 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
748 Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
749 Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0);
750 Value *BC = Builder.CreateBitCast(Arg0,
751 PointerType::getUnqual(Elt->getType()),
752 "cast");
Craig Topper29ce55d2016-05-30 22:54:12 +0000753 Builder.CreateAlignedStore(Elt, BC, 1);
Craig Topper12e322a2016-05-25 06:56:32 +0000754
755 // Remove intrinsic.
756 CI->eraseFromParent();
757 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000758 } else if (IsX86 && (Name.startswith("sse.storeu.") ||
759 Name.startswith("sse2.storeu.") ||
760 Name.startswith("avx.storeu."))) {
Craig Topper8287fd82016-05-30 23:15:56 +0000761 Value *Arg0 = CI->getArgOperand(0);
762 Value *Arg1 = CI->getArgOperand(1);
763
764 Arg0 = Builder.CreateBitCast(Arg0,
765 PointerType::getUnqual(Arg1->getType()),
766 "cast");
767 Builder.CreateAlignedStore(Arg1, Arg0, 1);
768
769 // Remove intrinsic.
770 CI->eraseFromParent();
771 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000772 } else if (IsX86 && (Name.startswith("avx512.mask.storeu.p") ||
773 Name.startswith("avx512.mask.storeu.b.") ||
774 Name.startswith("avx512.mask.storeu.w.") ||
775 Name.startswith("avx512.mask.storeu.d.") ||
776 Name.startswith("avx512.mask.storeu.q."))) {
Craig Topper50f85c22016-05-31 01:50:02 +0000777 UpgradeMaskedStore(Builder, C, CI->getArgOperand(0), CI->getArgOperand(1),
778 CI->getArgOperand(2), /*Aligned*/false);
779
780 // Remove intrinsic.
781 CI->eraseFromParent();
782 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000783 } else if (IsX86 && (Name.startswith("avx512.mask.store.p") ||
784 Name.startswith("avx512.mask.store.b.") ||
785 Name.startswith("avx512.mask.store.w.") ||
786 Name.startswith("avx512.mask.store.d.") ||
787 Name.startswith("avx512.mask.store.q."))) {
Craig Topper50f85c22016-05-31 01:50:02 +0000788 UpgradeMaskedStore(Builder, C, CI->getArgOperand(0), CI->getArgOperand(1),
789 CI->getArgOperand(2), /*Aligned*/true);
790
791 // Remove intrinsic.
792 CI->eraseFromParent();
793 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000794 } else if (IsX86 && (Name.startswith("avx512.mask.loadu.p") ||
795 Name.startswith("avx512.mask.loadu.b.") ||
796 Name.startswith("avx512.mask.loadu.w.") ||
797 Name.startswith("avx512.mask.loadu.d.") ||
798 Name.startswith("avx512.mask.loadu.q."))) {
Craig Topperf10fbfa2016-06-02 04:19:36 +0000799 Rep = UpgradeMaskedLoad(Builder, C, CI->getArgOperand(0),
800 CI->getArgOperand(1), CI->getArgOperand(2),
801 /*Aligned*/false);
Craig Topper5aebb862016-07-04 20:56:38 +0000802 } else if (IsX86 && (Name.startswith("avx512.mask.load.p") ||
803 Name.startswith("avx512.mask.load.b.") ||
804 Name.startswith("avx512.mask.load.w.") ||
805 Name.startswith("avx512.mask.load.d.") ||
806 Name.startswith("avx512.mask.load.q."))) {
Craig Topperf10fbfa2016-06-02 04:19:36 +0000807 Rep = UpgradeMaskedLoad(Builder, C, CI->getArgOperand(0),
808 CI->getArgOperand(1),CI->getArgOperand(2),
809 /*Aligned*/true);
Craig Topper5aebb862016-07-04 20:56:38 +0000810 } else if (IsX86 && Name.startswith("xop.vpcom")) {
Craig Topper3352ba52012-06-09 16:46:13 +0000811 Intrinsic::ID intID;
812 if (Name.endswith("ub"))
813 intID = Intrinsic::x86_xop_vpcomub;
814 else if (Name.endswith("uw"))
815 intID = Intrinsic::x86_xop_vpcomuw;
816 else if (Name.endswith("ud"))
817 intID = Intrinsic::x86_xop_vpcomud;
818 else if (Name.endswith("uq"))
819 intID = Intrinsic::x86_xop_vpcomuq;
820 else if (Name.endswith("b"))
821 intID = Intrinsic::x86_xop_vpcomb;
822 else if (Name.endswith("w"))
823 intID = Intrinsic::x86_xop_vpcomw;
824 else if (Name.endswith("d"))
825 intID = Intrinsic::x86_xop_vpcomd;
826 else if (Name.endswith("q"))
827 intID = Intrinsic::x86_xop_vpcomq;
828 else
829 llvm_unreachable("Unknown suffix");
830
Craig Topper5aebb862016-07-04 20:56:38 +0000831 Name = Name.substr(9); // strip off "xop.vpcom"
Craig Topper3352ba52012-06-09 16:46:13 +0000832 unsigned Imm;
833 if (Name.startswith("lt"))
834 Imm = 0;
835 else if (Name.startswith("le"))
836 Imm = 1;
837 else if (Name.startswith("gt"))
838 Imm = 2;
839 else if (Name.startswith("ge"))
840 Imm = 3;
841 else if (Name.startswith("eq"))
842 Imm = 4;
843 else if (Name.startswith("ne"))
844 Imm = 5;
Craig Topper3352ba52012-06-09 16:46:13 +0000845 else if (Name.startswith("false"))
Craig Toppere32546d2015-02-13 07:42:15 +0000846 Imm = 6;
847 else if (Name.startswith("true"))
Craig Topper3352ba52012-06-09 16:46:13 +0000848 Imm = 7;
849 else
850 llvm_unreachable("Unknown condition");
851
852 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
David Blaikieff6409d2015-05-18 22:13:54 +0000853 Rep =
854 Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1),
855 Builder.getInt8(Imm)});
Craig Topper5aebb862016-07-04 20:56:38 +0000856 } else if (IsX86 && Name == "xop.vpcmov") {
Simon Pilgrime88dc042015-11-03 20:27:01 +0000857 Value *Arg0 = CI->getArgOperand(0);
858 Value *Arg1 = CI->getArgOperand(1);
859 Value *Sel = CI->getArgOperand(2);
860 unsigned NumElts = CI->getType()->getVectorNumElements();
861 Constant *MinusOne = ConstantVector::getSplat(NumElts, Builder.getInt64(-1));
862 Value *NotSel = Builder.CreateXor(Sel, MinusOne);
863 Value *Sel0 = Builder.CreateAnd(Arg0, Sel);
864 Value *Sel1 = Builder.CreateAnd(Arg1, NotSel);
865 Rep = Builder.CreateOr(Sel0, Sel1);
Craig Topper5aebb862016-07-04 20:56:38 +0000866 } else if (IsX86 && Name == "sse42.crc32.64.8") {
Craig Topperef9e9932013-10-15 05:20:47 +0000867 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
868 Intrinsic::x86_sse42_crc32_32_8);
869 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
David Blaikieff6409d2015-05-18 22:13:54 +0000870 Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)});
Craig Topperef9e9932013-10-15 05:20:47 +0000871 Rep = Builder.CreateZExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000872 } else if (IsX86 && Name.startswith("avx.vbroadcast")) {
Adam Nemet39066802014-05-29 23:35:33 +0000873 // Replace broadcasts with a series of insertelements.
874 Type *VecTy = CI->getType();
875 Type *EltTy = VecTy->getVectorElementType();
876 unsigned EltNum = VecTy->getVectorNumElements();
877 Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
878 EltTy->getPointerTo());
David Blaikie0c28fd72015-05-20 21:46:30 +0000879 Value *Load = Builder.CreateLoad(EltTy, Cast);
Adam Nemet39066802014-05-29 23:35:33 +0000880 Type *I32Ty = Type::getInt32Ty(C);
881 Rep = UndefValue::get(VecTy);
882 for (unsigned I = 0; I < EltNum; ++I)
883 Rep = Builder.CreateInsertElement(Rep, Load,
884 ConstantInt::get(I32Ty, I));
Craig Topper5aebb862016-07-04 20:56:38 +0000885 } else if (IsX86 && (Name.startswith("sse41.pmovsx") ||
886 Name.startswith("sse41.pmovzx") ||
887 Name.startswith("avx2.pmovsx") ||
888 Name.startswith("avx2.pmovzx"))) {
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000889 VectorType *SrcTy = cast<VectorType>(CI->getArgOperand(0)->getType());
890 VectorType *DstTy = cast<VectorType>(CI->getType());
891 unsigned NumDstElts = DstTy->getNumElements();
892
Simon Pilgrim9602d672016-05-28 18:03:41 +0000893 // Extract a subvector of the first NumDstElts lanes and sign/zero extend.
Craig Topperc0a5fa02016-06-12 04:48:00 +0000894 SmallVector<uint32_t, 8> ShuffleMask(NumDstElts);
Craig Topper99d1eab2016-06-12 00:41:19 +0000895 for (unsigned i = 0; i != NumDstElts; ++i)
Craig Topperc0a5fa02016-06-12 04:48:00 +0000896 ShuffleMask[i] = i;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000897
898 Value *SV = Builder.CreateShuffleVector(
899 CI->getArgOperand(0), UndefValue::get(SrcTy), ShuffleMask);
Simon Pilgrim9602d672016-05-28 18:03:41 +0000900
901 bool DoSext = (StringRef::npos != Name.find("pmovsx"));
902 Rep = DoSext ? Builder.CreateSExt(SV, DstTy)
903 : Builder.CreateZExt(SV, DstTy);
Craig Topper5aebb862016-07-04 20:56:38 +0000904 } else if (IsX86 && Name == "avx2.vbroadcasti128") {
Juergen Ributzka1f7a1762015-03-04 00:13:25 +0000905 // Replace vbroadcasts with a vector shuffle.
David Blaikie0c28fd72015-05-20 21:46:30 +0000906 Type *VT = VectorType::get(Type::getInt64Ty(C), 2);
907 Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0),
908 PointerType::getUnqual(VT));
909 Value *Load = Builder.CreateLoad(VT, Op);
Craig Topper99d1eab2016-06-12 00:41:19 +0000910 uint32_t Idxs[4] = { 0, 1, 0, 1 };
Juergen Ributzka1f7a1762015-03-04 00:13:25 +0000911 Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
Sanjay Patel2db6d382015-03-12 15:27:07 +0000912 Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +0000913 } else if (IsX86 && (Name.startswith("avx2.pbroadcast") ||
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +0000914 Name.startswith("avx2.vbroadcast") ||
915 Name.startswith("avx512.pbroadcast") ||
916 Name.startswith("avx512.mask.broadcast.s"))) {
Ahmed Bougacha1a4987052015-08-20 20:36:19 +0000917 // Replace vp?broadcasts with a vector shuffle.
918 Value *Op = CI->getArgOperand(0);
919 unsigned NumElts = CI->getType()->getVectorNumElements();
920 Type *MaskTy = VectorType::get(Type::getInt32Ty(C), NumElts);
921 Rep = Builder.CreateShuffleVector(Op, UndefValue::get(Op->getType()),
922 Constant::getNullValue(MaskTy));
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +0000923
924 if (CI->getNumArgOperands() == 3)
925 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
926 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +0000927 } else if (IsX86 && Name.startswith("avx512.mask.palignr.")) {
Craig Topper33350cc2016-06-06 06:12:54 +0000928 Rep = UpgradeX86PALIGNRIntrinsics(Builder, C, CI->getArgOperand(0),
929 CI->getArgOperand(1),
930 CI->getArgOperand(2),
931 CI->getArgOperand(3),
932 CI->getArgOperand(4));
Craig Topper5aebb862016-07-04 20:56:38 +0000933 } else if (IsX86 && (Name == "sse2.psll.dq" ||
934 Name == "avx2.psll.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +0000935 // 128/256-bit shift left specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +0000936 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000937 Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +0000938 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +0000939 } else if (IsX86 && (Name == "sse2.psrl.dq" ||
940 Name == "avx2.psrl.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +0000941 // 128/256-bit shift right specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +0000942 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000943 Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +0000944 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +0000945 } else if (IsX86 && (Name == "sse2.psll.dq.bs" ||
946 Name == "avx2.psll.dq.bs" ||
947 Name == "avx512.psll.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +0000948 // 128/256/512-bit shift left specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +0000949 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000950 Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +0000951 } else if (IsX86 && (Name == "sse2.psrl.dq.bs" ||
952 Name == "avx2.psrl.dq.bs" ||
953 Name == "avx512.psrl.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +0000954 // 128/256/512-bit shift right specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +0000955 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000956 Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +0000957 } else if (IsX86 && (Name == "sse41.pblendw" ||
958 Name.startswith("sse41.blendp") ||
959 Name.startswith("avx.blend.p") ||
960 Name == "avx2.pblendw" ||
961 Name.startswith("avx2.pblendd."))) {
Craig Topper782d6202015-02-28 19:33:17 +0000962 Value *Op0 = CI->getArgOperand(0);
963 Value *Op1 = CI->getArgOperand(1);
964 unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue();
965 VectorType *VecTy = cast<VectorType>(CI->getType());
966 unsigned NumElts = VecTy->getNumElements();
967
Craig Topperc0a5fa02016-06-12 04:48:00 +0000968 SmallVector<uint32_t, 16> Idxs(NumElts);
969 for (unsigned i = 0; i != NumElts; ++i)
970 Idxs[i] = ((Imm >> (i%8)) & 1) ? i + NumElts : i;
Craig Topper782d6202015-02-28 19:33:17 +0000971
Craig Topper2f561822016-06-12 01:05:59 +0000972 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +0000973 } else if (IsX86 && (Name.startswith("avx.vinsertf128.") ||
974 Name == "avx2.vinserti128")) {
Sanjay Patel19792fb2015-03-10 16:08:36 +0000975 Value *Op0 = CI->getArgOperand(0);
976 Value *Op1 = CI->getArgOperand(1);
977 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
978 VectorType *VecTy = cast<VectorType>(CI->getType());
979 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000980
Sanjay Patel19792fb2015-03-10 16:08:36 +0000981 // Mask off the high bits of the immediate value; hardware ignores those.
982 Imm = Imm & 1;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000983
Sanjay Patel19792fb2015-03-10 16:08:36 +0000984 // Extend the second operand into a vector that is twice as big.
985 Value *UndefV = UndefValue::get(Op1->getType());
Craig Topperc0a5fa02016-06-12 04:48:00 +0000986 SmallVector<uint32_t, 8> Idxs(NumElts);
987 for (unsigned i = 0; i != NumElts; ++i)
988 Idxs[i] = i;
Craig Topper2f561822016-06-12 01:05:59 +0000989 Rep = Builder.CreateShuffleVector(Op1, UndefV, Idxs);
Sanjay Patel19792fb2015-03-10 16:08:36 +0000990
991 // Insert the second operand into the first operand.
992
993 // Note that there is no guarantee that instruction lowering will actually
994 // produce a vinsertf128 instruction for the created shuffles. In
995 // particular, the 0 immediate case involves no lane changes, so it can
996 // be handled as a blend.
997
998 // Example of shuffle mask for 32-bit elements:
999 // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
1000 // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 >
1001
Sanjay Patel19792fb2015-03-10 16:08:36 +00001002 // The low half of the result is either the low half of the 1st operand
1003 // or the low half of the 2nd operand (the inserted vector).
Craig Topperc0a5fa02016-06-12 04:48:00 +00001004 for (unsigned i = 0; i != NumElts / 2; ++i)
1005 Idxs[i] = Imm ? i : (i + NumElts);
Sanjay Patel19792fb2015-03-10 16:08:36 +00001006 // The high half of the result is either the low half of the 2nd operand
1007 // (the inserted vector) or the high half of the 1st operand.
Craig Topperc0a5fa02016-06-12 04:48:00 +00001008 for (unsigned i = NumElts / 2; i != NumElts; ++i)
1009 Idxs[i] = Imm ? (i + NumElts / 2) : i;
Craig Topper2f561822016-06-12 01:05:59 +00001010 Rep = Builder.CreateShuffleVector(Op0, Rep, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001011 } else if (IsX86 && (Name.startswith("avx.vextractf128.") ||
1012 Name == "avx2.vextracti128")) {
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001013 Value *Op0 = CI->getArgOperand(0);
1014 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1015 VectorType *VecTy = cast<VectorType>(CI->getType());
1016 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001017
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001018 // Mask off the high bits of the immediate value; hardware ignores those.
1019 Imm = Imm & 1;
1020
1021 // Get indexes for either the high half or low half of the input vector.
Craig Topper2f561822016-06-12 01:05:59 +00001022 SmallVector<uint32_t, 4> Idxs(NumElts);
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001023 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topper2f561822016-06-12 01:05:59 +00001024 Idxs[i] = Imm ? (i + NumElts) : i;
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001025 }
1026
1027 Value *UndefV = UndefValue::get(Op0->getType());
Craig Topper2f561822016-06-12 01:05:59 +00001028 Rep = Builder.CreateShuffleVector(Op0, UndefV, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001029 } else if (!IsX86 && Name == "stackprotectorcheck") {
Tim Shen00127562016-04-08 21:26:31 +00001030 Rep = nullptr;
Craig Topper5aebb862016-07-04 20:56:38 +00001031 } else if (IsX86 && (Name.startswith("avx512.mask.perm.df.") ||
1032 Name.startswith("avx512.mask.perm.di."))) {
Simon Pilgrim02d435d2016-07-04 14:19:05 +00001033 Value *Op0 = CI->getArgOperand(0);
1034 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1035 VectorType *VecTy = cast<VectorType>(CI->getType());
1036 unsigned NumElts = VecTy->getNumElements();
1037
1038 SmallVector<uint32_t, 8> Idxs(NumElts);
1039 for (unsigned i = 0; i != NumElts; ++i)
1040 Idxs[i] = (i & ~0x3) + ((Imm >> (2 * (i & 0x3))) & 3);
1041
1042 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1043
1044 if (CI->getNumArgOperands() == 4)
1045 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1046 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001047 } else if (IsX86 && (Name.startswith("avx.vpermil.") ||
1048 Name == "sse2.pshuf.d" ||
1049 Name.startswith("avx512.mask.vpermil.p") ||
1050 Name.startswith("avx512.mask.pshuf.d."))) {
Craig Topper8a105052016-06-12 03:10:47 +00001051 Value *Op0 = CI->getArgOperand(0);
1052 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1053 VectorType *VecTy = cast<VectorType>(CI->getType());
1054 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9fca3002016-07-04 12:40:54 +00001055 // Calculate the size of each index in the immediate.
Craig Topper8a105052016-06-12 03:10:47 +00001056 unsigned IdxSize = 64 / VecTy->getScalarSizeInBits();
1057 unsigned IdxMask = ((1 << IdxSize) - 1);
1058
1059 SmallVector<uint32_t, 8> Idxs(NumElts);
1060 // Lookup the bits for this element, wrapping around the immediate every
1061 // 8-bits. Elements are grouped into sets of 2 or 4 elements so we need
1062 // to offset by the first index of each group.
1063 for (unsigned i = 0; i != NumElts; ++i)
1064 Idxs[i] = ((Imm >> ((i * IdxSize) % 8)) & IdxMask) | (i & ~IdxMask);
1065
1066 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001067
1068 if (CI->getNumArgOperands() == 4)
1069 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1070 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001071 } else if (IsX86 && (Name == "sse2.pshufl.w" ||
1072 Name.startswith("avx512.mask.pshufl.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001073 Value *Op0 = CI->getArgOperand(0);
1074 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1075 unsigned NumElts = CI->getType()->getVectorNumElements();
1076
1077 SmallVector<uint32_t, 16> Idxs(NumElts);
1078 for (unsigned l = 0; l != NumElts; l += 8) {
1079 for (unsigned i = 0; i != 4; ++i)
1080 Idxs[i + l] = ((Imm >> (2 * i)) & 0x3) + l;
1081 for (unsigned i = 4; i != 8; ++i)
1082 Idxs[i + l] = i + l;
1083 }
1084
1085 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001086
1087 if (CI->getNumArgOperands() == 4)
1088 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1089 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001090 } else if (IsX86 && (Name == "sse2.pshufh.w" ||
1091 Name.startswith("avx512.mask.pshufh.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001092 Value *Op0 = CI->getArgOperand(0);
1093 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1094 unsigned NumElts = CI->getType()->getVectorNumElements();
1095
1096 SmallVector<uint32_t, 16> Idxs(NumElts);
1097 for (unsigned l = 0; l != NumElts; l += 8) {
1098 for (unsigned i = 0; i != 4; ++i)
1099 Idxs[i + l] = i + l;
1100 for (unsigned i = 0; i != 4; ++i)
1101 Idxs[i + l + 4] = ((Imm >> (2 * i)) & 0x3) + 4 + l;
1102 }
1103
1104 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001105
1106 if (CI->getNumArgOperands() == 4)
1107 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1108 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001109 } else if (IsX86 && (Name.startswith("avx512.mask.movddup") ||
1110 Name.startswith("avx512.mask.movshdup") ||
1111 Name.startswith("avx512.mask.movsldup"))) {
Simon Pilgrim19adee92016-07-02 14:42:35 +00001112 Value *Op0 = CI->getArgOperand(0);
1113 unsigned NumElts = CI->getType()->getVectorNumElements();
1114 unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1115
1116 unsigned Offset = 0;
Craig Topper5aebb862016-07-04 20:56:38 +00001117 if (Name.startswith("avx512.mask.movshdup."))
Simon Pilgrim19adee92016-07-02 14:42:35 +00001118 Offset = 1;
1119
1120 SmallVector<uint32_t, 16> Idxs(NumElts);
1121 for (unsigned l = 0; l != NumElts; l += NumLaneElts)
1122 for (unsigned i = 0; i != NumLaneElts; i += 2) {
1123 Idxs[i + l + 0] = i + l + Offset;
1124 Idxs[i + l + 1] = i + l + Offset;
1125 }
1126
1127 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1128
1129 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
1130 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +00001131 } else if (IsX86 && (Name.startswith("avx512.mask.punpckl") ||
1132 Name.startswith("avx512.mask.unpckl."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001133 Value *Op0 = CI->getArgOperand(0);
1134 Value *Op1 = CI->getArgOperand(1);
1135 int NumElts = CI->getType()->getVectorNumElements();
1136 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1137
1138 SmallVector<uint32_t, 64> Idxs(NumElts);
1139 for (int l = 0; l != NumElts; l += NumLaneElts)
1140 for (int i = 0; i != NumLaneElts; ++i)
1141 Idxs[i + l] = l + (i / 2) + NumElts * (i % 2);
1142
1143 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1144
1145 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1146 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001147 } else if (IsX86 && (Name.startswith("avx512.mask.punpckh") ||
1148 Name.startswith("avx512.mask.unpckh."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001149 Value *Op0 = CI->getArgOperand(0);
1150 Value *Op1 = CI->getArgOperand(1);
1151 int NumElts = CI->getType()->getVectorNumElements();
1152 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1153
1154 SmallVector<uint32_t, 64> Idxs(NumElts);
1155 for (int l = 0; l != NumElts; l += NumLaneElts)
1156 for (int i = 0; i != NumLaneElts; ++i)
1157 Idxs[i + l] = (NumLaneElts / 2) + l + (i / 2) + NumElts * (i % 2);
1158
1159 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1160
1161 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1162 CI->getArgOperand(2));
Craig Topper3b1817d2012-02-03 06:10:55 +00001163 } else {
Craig Topper8a105052016-06-12 03:10:47 +00001164 llvm_unreachable("Unknown function for CallInst upgrade.");
Craig Topper3b1817d2012-02-03 06:10:55 +00001165 }
1166
Tim Shen00127562016-04-08 21:26:31 +00001167 if (Rep)
1168 CI->replaceAllUsesWith(Rep);
Craig Topper3b1817d2012-02-03 06:10:55 +00001169 CI->eraseFromParent();
1170 return;
1171 }
1172
Yaron Kerend1fdbe72015-03-30 16:10:39 +00001173 std::string Name = CI->getName();
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001174 if (!Name.empty())
1175 CI->setName(Name + ".old");
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001176
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001177 switch (NewFn->getIntrinsicID()) {
1178 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +00001179 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001180
Jeroen Ketemaab99b592015-09-30 10:56:37 +00001181 case Intrinsic::arm_neon_vld1:
1182 case Intrinsic::arm_neon_vld2:
1183 case Intrinsic::arm_neon_vld3:
1184 case Intrinsic::arm_neon_vld4:
1185 case Intrinsic::arm_neon_vld2lane:
1186 case Intrinsic::arm_neon_vld3lane:
1187 case Intrinsic::arm_neon_vld4lane:
1188 case Intrinsic::arm_neon_vst1:
1189 case Intrinsic::arm_neon_vst2:
1190 case Intrinsic::arm_neon_vst3:
1191 case Intrinsic::arm_neon_vst4:
1192 case Intrinsic::arm_neon_vst2lane:
1193 case Intrinsic::arm_neon_vst3lane:
1194 case Intrinsic::arm_neon_vst4lane: {
1195 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1196 CI->arg_operands().end());
1197 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1198 CI->eraseFromParent();
1199 return;
1200 }
1201
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001202 case Intrinsic::ctlz:
Nuno Lopesad40c0a2012-05-22 15:25:31 +00001203 case Intrinsic::cttz:
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001204 assert(CI->getNumArgOperands() == 1 &&
1205 "Mismatch between function args and call args");
David Blaikieff6409d2015-05-18 22:13:54 +00001206 CI->replaceAllUsesWith(Builder.CreateCall(
1207 NewFn, {CI->getArgOperand(0), Builder.getFalse()}, Name));
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001208 CI->eraseFromParent();
1209 return;
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001210
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001211 case Intrinsic::objectsize:
David Blaikieff6409d2015-05-18 22:13:54 +00001212 CI->replaceAllUsesWith(Builder.CreateCall(
1213 NewFn, {CI->getArgOperand(0), CI->getArgOperand(1)}, Name));
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001214 CI->eraseFromParent();
1215 return;
1216
Joel Jonesb84f7be2012-07-18 00:02:16 +00001217 case Intrinsic::ctpop: {
David Blaikieff6409d2015-05-18 22:13:54 +00001218 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {CI->getArgOperand(0)}));
Joel Jonesb84f7be2012-07-18 00:02:16 +00001219 CI->eraseFromParent();
1220 return;
1221 }
Joel Jones43cb8782012-07-13 23:25:25 +00001222
Craig Topper71dc02d2012-06-13 07:18:53 +00001223 case Intrinsic::x86_xop_vfrcz_ss:
1224 case Intrinsic::x86_xop_vfrcz_sd:
David Blaikieff6409d2015-05-18 22:13:54 +00001225 CI->replaceAllUsesWith(
1226 Builder.CreateCall(NewFn, {CI->getArgOperand(1)}, Name));
Craig Topper71dc02d2012-06-13 07:18:53 +00001227 CI->eraseFromParent();
1228 return;
1229
Simon Pilgrime85506b2016-06-03 08:06:03 +00001230 case Intrinsic::x86_xop_vpermil2pd:
1231 case Intrinsic::x86_xop_vpermil2ps:
1232 case Intrinsic::x86_xop_vpermil2pd_256:
1233 case Intrinsic::x86_xop_vpermil2ps_256: {
1234 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1235 CI->arg_operands().end());
1236 VectorType *FltIdxTy = cast<VectorType>(Args[2]->getType());
1237 VectorType *IntIdxTy = VectorType::getInteger(FltIdxTy);
1238 Args[2] = Builder.CreateBitCast(Args[2], IntIdxTy);
1239 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args, Name));
1240 CI->eraseFromParent();
1241 return;
1242 }
1243
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001244 case Intrinsic::x86_sse41_ptestc:
1245 case Intrinsic::x86_sse41_ptestz:
Craig Topper71dc02d2012-06-13 07:18:53 +00001246 case Intrinsic::x86_sse41_ptestnzc: {
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001247 // The arguments for these intrinsics used to be v4f32, and changed
1248 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
1249 // So, the only thing required is a bitcast for both arguments.
1250 // First, check the arguments have the old type.
1251 Value *Arg0 = CI->getArgOperand(0);
1252 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
1253 return;
1254
1255 // Old intrinsic, add bitcasts
1256 Value *Arg1 = CI->getArgOperand(1);
1257
David Blaikie5bacf372015-04-24 21:16:07 +00001258 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001259
David Blaikie5bacf372015-04-24 21:16:07 +00001260 Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast");
1261 Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
1262
David Blaikieff6409d2015-05-18 22:13:54 +00001263 CallInst *NewCall = Builder.CreateCall(NewFn, {BC0, BC1}, Name);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001264 CI->replaceAllUsesWith(NewCall);
1265 CI->eraseFromParent();
1266 return;
Evan Cheng0e179d02007-12-17 22:33:23 +00001267 }
Chandler Carruth373b2b12014-09-06 10:00:01 +00001268
Chandler Carruth373b2b12014-09-06 10:00:01 +00001269 case Intrinsic::x86_sse41_insertps:
1270 case Intrinsic::x86_sse41_dppd:
1271 case Intrinsic::x86_sse41_dpps:
1272 case Intrinsic::x86_sse41_mpsadbw:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001273 case Intrinsic::x86_avx_dp_ps_256:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001274 case Intrinsic::x86_avx2_mpsadbw: {
1275 // Need to truncate the last argument from i32 to i8 -- this argument models
1276 // an inherently 8-bit immediate operand to these x86 instructions.
1277 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1278 CI->arg_operands().end());
1279
1280 // Replace the last argument with a trunc.
1281 Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
1282
1283 CallInst *NewCall = Builder.CreateCall(NewFn, Args);
1284 CI->replaceAllUsesWith(NewCall);
1285 CI->eraseFromParent();
1286 return;
1287 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +00001288
1289 case Intrinsic::thread_pointer: {
1290 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {}));
1291 CI->eraseFromParent();
1292 return;
1293 }
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +00001294
1295 case Intrinsic::masked_load:
1296 case Intrinsic::masked_store: {
1297 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1298 CI->arg_operands().end());
1299 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1300 CI->eraseFromParent();
1301 return;
1302 }
Craig Topper71dc02d2012-06-13 07:18:53 +00001303 }
Chandler Carruth7132e002007-08-04 01:51:18 +00001304}
1305
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001306void llvm::UpgradeCallsToIntrinsic(Function *F) {
Chandler Carruth7132e002007-08-04 01:51:18 +00001307 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
1308
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001309 // Check if this function should be upgraded and get the replacement function
1310 // if there is one.
Chris Lattner80ed9dc2011-06-18 06:05:24 +00001311 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +00001312 if (UpgradeIntrinsicFunction(F, NewFn)) {
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001313 // Replace all users of the old function with the new function or new
1314 // instructions. This is not a range loop because the call is deleted.
1315 for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; )
Duncan P. N. Exon Smith93f53c42016-04-17 03:59:37 +00001316 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001317 UpgradeIntrinsicCall(CI, NewFn);
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001318
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001319 // Remove old function, no longer used, from the module.
1320 F->eraseFromParent();
Chandler Carruth7132e002007-08-04 01:51:18 +00001321 }
1322}
Devang Patel80ae3492009-08-28 23:24:31 +00001323
Manman Ren209b17c2013-09-28 00:22:27 +00001324void llvm::UpgradeInstWithTBAATag(Instruction *I) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001325 MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
Manman Ren209b17c2013-09-28 00:22:27 +00001326 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
1327 // Check if the tag uses struct-path aware TBAA format.
1328 if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
1329 return;
1330
1331 if (MD->getNumOperands() == 3) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001332 Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
Manman Ren209b17c2013-09-28 00:22:27 +00001333 MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
1334 // Create a MDNode <ScalarType, ScalarType, offset 0, const>
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001335 Metadata *Elts2[] = {ScalarType, ScalarType,
1336 ConstantAsMetadata::get(Constant::getNullValue(
1337 Type::getInt64Ty(I->getContext()))),
1338 MD->getOperand(2)};
Manman Ren209b17c2013-09-28 00:22:27 +00001339 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
1340 } else {
1341 // Create a MDNode <MD, MD, offset 0>
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001342 Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
1343 Type::getInt64Ty(I->getContext())))};
Manman Ren209b17c2013-09-28 00:22:27 +00001344 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
1345 }
1346}
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001347
1348Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
1349 Instruction *&Temp) {
1350 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001351 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001352
Craig Topperc6207612014-04-09 06:08:46 +00001353 Temp = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001354 Type *SrcTy = V->getType();
1355 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1356 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1357 LLVMContext &Context = V->getContext();
1358
1359 // We have no information about target data layout, so we assume that
1360 // the maximum pointer size is 64bit.
1361 Type *MidTy = Type::getInt64Ty(Context);
1362 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
1363
1364 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
1365 }
1366
Craig Topperc6207612014-04-09 06:08:46 +00001367 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001368}
1369
1370Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
1371 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001372 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001373
1374 Type *SrcTy = C->getType();
1375 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1376 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1377 LLVMContext &Context = C->getContext();
1378
1379 // We have no information about target data layout, so we assume that
1380 // the maximum pointer size is 64bit.
1381 Type *MidTy = Type::getInt64Ty(Context);
1382
1383 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
1384 DestTy);
1385 }
1386
Craig Topperc6207612014-04-09 06:08:46 +00001387 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001388}
Manman Ren8b4306c2013-12-02 21:29:56 +00001389
1390/// Check the debug info version number, if it is out-dated, drop the debug
1391/// info. Return true if module is modified.
1392bool llvm::UpgradeDebugInfo(Module &M) {
Manman Ren2ebfb422014-01-16 01:51:12 +00001393 unsigned Version = getDebugMetadataVersionFromModule(M);
1394 if (Version == DEBUG_METADATA_VERSION)
Manman Ren8b4306c2013-12-02 21:29:56 +00001395 return false;
1396
Manman Ren2ebfb422014-01-16 01:51:12 +00001397 bool RetCode = StripDebugInfo(M);
1398 if (RetCode) {
1399 DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
1400 M.getContext().diagnose(DiagVersion);
1401 }
1402 return RetCode;
Manman Ren8b4306c2013-12-02 21:29:56 +00001403}
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001404
Manman Renb5d7ff42016-05-25 23:14:48 +00001405bool llvm::UpgradeModuleFlags(Module &M) {
1406 const NamedMDNode *ModFlags = M.getModuleFlagsMetadata();
1407 if (!ModFlags)
1408 return false;
1409
1410 bool HasObjCFlag = false, HasClassProperties = false;
1411 for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
1412 MDNode *Op = ModFlags->getOperand(I);
1413 if (Op->getNumOperands() < 2)
1414 continue;
1415 MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1416 if (!ID)
1417 continue;
1418 if (ID->getString() == "Objective-C Image Info Version")
1419 HasObjCFlag = true;
1420 if (ID->getString() == "Objective-C Class Properties")
1421 HasClassProperties = true;
1422 }
1423 // "Objective-C Class Properties" is recently added for Objective-C. We
1424 // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module
1425 // flag of value 0, so we can correclty report error when trying to link
1426 // an ObjC bitcode without this module flag with an ObjC bitcode with this
1427 // module flag.
1428 if (HasObjCFlag && !HasClassProperties) {
1429 M.addModuleFlag(llvm::Module::Error, "Objective-C Class Properties",
1430 (uint32_t)0);
1431 return true;
1432 }
1433 return false;
1434}
1435
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00001436static bool isOldLoopArgument(Metadata *MD) {
1437 auto *T = dyn_cast_or_null<MDTuple>(MD);
1438 if (!T)
1439 return false;
1440 if (T->getNumOperands() < 1)
1441 return false;
1442 auto *S = dyn_cast_or_null<MDString>(T->getOperand(0));
1443 if (!S)
1444 return false;
1445 return S->getString().startswith("llvm.vectorizer.");
1446}
1447
1448static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) {
1449 StringRef OldPrefix = "llvm.vectorizer.";
1450 assert(OldTag.startswith(OldPrefix) && "Expected old prefix");
1451
1452 if (OldTag == "llvm.vectorizer.unroll")
1453 return MDString::get(C, "llvm.loop.interleave.count");
1454
1455 return MDString::get(
1456 C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size()))
1457 .str());
1458}
1459
1460static Metadata *upgradeLoopArgument(Metadata *MD) {
1461 auto *T = dyn_cast_or_null<MDTuple>(MD);
1462 if (!T)
1463 return MD;
1464 if (T->getNumOperands() < 1)
1465 return MD;
1466 auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0));
1467 if (!OldTag)
1468 return MD;
1469 if (!OldTag->getString().startswith("llvm.vectorizer."))
1470 return MD;
1471
1472 // This has an old tag. Upgrade it.
1473 SmallVector<Metadata *, 8> Ops;
1474 Ops.reserve(T->getNumOperands());
1475 Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString()));
1476 for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I)
1477 Ops.push_back(T->getOperand(I));
1478
1479 return MDTuple::get(T->getContext(), Ops);
1480}
1481
1482MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) {
1483 auto *T = dyn_cast<MDTuple>(&N);
1484 if (!T)
1485 return &N;
1486
1487 if (!llvm::any_of(T->operands(), isOldLoopArgument))
1488 return &N;
1489
1490 SmallVector<Metadata *, 8> Ops;
1491 Ops.reserve(T->getNumOperands());
1492 for (Metadata *MD : T->operands())
1493 Ops.push_back(upgradeLoopArgument(MD));
1494
1495 return MDTuple::get(T->getContext(), Ops);
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001496}