blob: 35273725bf3858ddb4016a4281aba5bdd942e6af [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.
Eric Liuc9c68172016-07-08 16:09:51 +000070 StringRef Name = F->getName();
Chris Lattnerb372f662011-06-18 18:56:39 +000071 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.") ||
Craig Topper70610cf2016-07-09 04:38:27 +0000258 Name.startswith("avx512.storent.") ||
Craig Topper5aebb862016-07-04 20:56:38 +0000259 Name == "sse2.storel.dq" ||
260 Name.startswith("sse.storeu.") ||
261 Name.startswith("sse2.storeu.") ||
262 Name.startswith("avx.storeu.") ||
263 Name.startswith("avx512.mask.storeu.p") ||
264 Name.startswith("avx512.mask.storeu.b.") ||
265 Name.startswith("avx512.mask.storeu.w.") ||
266 Name.startswith("avx512.mask.storeu.d.") ||
267 Name.startswith("avx512.mask.storeu.q.") ||
268 Name.startswith("avx512.mask.store.p") ||
269 Name.startswith("avx512.mask.store.b.") ||
270 Name.startswith("avx512.mask.store.w.") ||
271 Name.startswith("avx512.mask.store.d.") ||
272 Name.startswith("avx512.mask.store.q.") ||
273 Name.startswith("avx512.mask.loadu.p") ||
274 Name.startswith("avx512.mask.loadu.b.") ||
275 Name.startswith("avx512.mask.loadu.w.") ||
276 Name.startswith("avx512.mask.loadu.d.") ||
277 Name.startswith("avx512.mask.loadu.q.") ||
278 Name.startswith("avx512.mask.load.p") ||
279 Name.startswith("avx512.mask.load.b.") ||
280 Name.startswith("avx512.mask.load.w.") ||
281 Name.startswith("avx512.mask.load.d.") ||
282 Name.startswith("avx512.mask.load.q.") ||
283 Name == "sse42.crc32.64.8" ||
284 Name.startswith("avx.vbroadcast.s") ||
285 Name.startswith("avx512.mask.palignr.") ||
286 Name.startswith("sse2.psll.dq") ||
287 Name.startswith("sse2.psrl.dq") ||
288 Name.startswith("avx2.psll.dq") ||
289 Name.startswith("avx2.psrl.dq") ||
290 Name.startswith("avx512.psll.dq") ||
291 Name.startswith("avx512.psrl.dq") ||
292 Name == "sse41.pblendw" ||
293 Name.startswith("sse41.blendp") ||
294 Name.startswith("avx.blend.p") ||
295 Name == "avx2.pblendw" ||
296 Name.startswith("avx2.pblendd.") ||
297 Name == "avx2.vbroadcasti128" ||
298 Name == "xop.vpcmov" ||
299 (Name.startswith("xop.vpcom") && F->arg_size() == 2))) {
Craig Topperc6207612014-04-09 06:08:46 +0000300 NewFn = nullptr;
Craig Topper3b1817d2012-02-03 06:10:55 +0000301 return true;
302 }
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000303 // SSE4.1 ptest functions may have an old signature.
Craig Topper5aebb862016-07-04 20:56:38 +0000304 if (IsX86 && Name.startswith("sse41.ptest")) {
305 if (Name.substr(11) == "c")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000306 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000307 if (Name.substr(11) == "z")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000308 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000309 if (Name.substr(11) == "nzc")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000310 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
311 }
Sanjay Patel1c3eaec2015-02-28 22:25:06 +0000312 // Several blend and other instructions with masks used the wrong number of
Chandler Carruth373b2b12014-09-06 10:00:01 +0000313 // bits.
Craig Topper5aebb862016-07-04 20:56:38 +0000314 if (IsX86 && Name == "sse41.insertps")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000315 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
316 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000317 if (IsX86 && Name == "sse41.dppd")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000318 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
319 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000320 if (IsX86 && Name == "sse41.dpps")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000321 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
322 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000323 if (IsX86 && Name == "sse41.mpsadbw")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000324 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
325 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000326 if (IsX86 && Name == "avx.dp.ps.256")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000327 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
328 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000329 if (IsX86 && Name == "avx2.mpsadbw")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000330 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
331 NewFn);
Craig Topper29f2e952015-01-25 23:26:02 +0000332
Craig Topper71dc02d2012-06-13 07:18:53 +0000333 // frcz.ss/sd may need to have an argument dropped
Craig Topper5aebb862016-07-04 20:56:38 +0000334 if (IsX86 && Name.startswith("xop.vfrcz.ss") && F->arg_size() == 2) {
Craig Topper71dc02d2012-06-13 07:18:53 +0000335 F->setName(Name + ".old");
336 NewFn = Intrinsic::getDeclaration(F->getParent(),
337 Intrinsic::x86_xop_vfrcz_ss);
338 return true;
339 }
Craig Topper5aebb862016-07-04 20:56:38 +0000340 if (IsX86 && Name.startswith("xop.vfrcz.sd") && F->arg_size() == 2) {
Craig Topper71dc02d2012-06-13 07:18:53 +0000341 F->setName(Name + ".old");
342 NewFn = Intrinsic::getDeclaration(F->getParent(),
343 Intrinsic::x86_xop_vfrcz_sd);
344 return true;
345 }
Craig Topperf7bf6de2016-07-08 06:14:47 +0000346 if (IsX86 && (Name.startswith("avx512.mask.pslli.") ||
347 Name.startswith("avx512.mask.psrai.") ||
348 Name.startswith("avx512.mask.psrli."))) {
Craig Topperf7bf6de2016-07-08 06:14:47 +0000349 Intrinsic::ID ShiftID;
350 if (Name.slice(12, 16) == "psll")
351 ShiftID = Name[18] == 'd' ? Intrinsic::x86_avx512_mask_psll_di_512
352 : Intrinsic::x86_avx512_mask_psll_qi_512;
353 else if (Name.slice(12, 16) == "psra")
354 ShiftID = Name[18] == 'd' ? Intrinsic::x86_avx512_mask_psra_di_512
355 : Intrinsic::x86_avx512_mask_psra_qi_512;
356 else
357 ShiftID = Name[18] == 'd' ? Intrinsic::x86_avx512_mask_psrl_di_512
358 : Intrinsic::x86_avx512_mask_psrl_qi_512;
Eric Liuc9c68172016-07-08 16:09:51 +0000359 F->setName("llvm.x86." + Name + ".old");
Craig Topperf7bf6de2016-07-08 06:14:47 +0000360 NewFn = Intrinsic::getDeclaration(F->getParent(), ShiftID);
361 return true;
362 }
Craig Topper720c7bd2012-06-03 08:07:25 +0000363 // Fix the FMA4 intrinsics to remove the 4
Craig Topper5aebb862016-07-04 20:56:38 +0000364 if (IsX86 && Name.startswith("fma4.")) {
365 F->setName("llvm.x86.fma" + Name.substr(5));
Craig Topper2c5ccd82012-06-03 16:48:52 +0000366 NewFn = F;
367 return true;
Craig Topper720c7bd2012-06-03 08:07:25 +0000368 }
Simon Pilgrime85506b2016-06-03 08:06:03 +0000369 // Upgrade any XOP PERMIL2 index operand still using a float/double vector.
Craig Topper5aebb862016-07-04 20:56:38 +0000370 if (IsX86 && Name.startswith("xop.vpermil2")) {
Simon Pilgrime85506b2016-06-03 08:06:03 +0000371 auto Params = F->getFunctionType()->params();
372 auto Idx = Params[2];
373 if (Idx->getScalarType()->isFloatingPointTy()) {
Craig Topperf7bf6de2016-07-08 06:14:47 +0000374 F->setName("llvm.x86." + Name + ".old");
Simon Pilgrime85506b2016-06-03 08:06:03 +0000375 unsigned IdxSize = Idx->getPrimitiveSizeInBits();
376 unsigned EltSize = Idx->getScalarSizeInBits();
377 Intrinsic::ID Permil2ID;
378 if (EltSize == 64 && IdxSize == 128)
379 Permil2ID = Intrinsic::x86_xop_vpermil2pd;
380 else if (EltSize == 32 && IdxSize == 128)
381 Permil2ID = Intrinsic::x86_xop_vpermil2ps;
382 else if (EltSize == 64 && IdxSize == 256)
383 Permil2ID = Intrinsic::x86_xop_vpermil2pd_256;
384 else
385 Permil2ID = Intrinsic::x86_xop_vpermil2ps_256;
386 NewFn = Intrinsic::getDeclaration(F->getParent(), Permil2ID);
387 return true;
388 }
389 }
Craig Topper3b1817d2012-02-03 06:10:55 +0000390 break;
391 }
Chris Lattnerb372f662011-06-18 18:56:39 +0000392 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000393
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000394 // This may not belong here. This function is effectively being overloaded
395 // to both detect an intrinsic which needs upgrading, and to provide the
396 // upgraded form of the intrinsic. We should perhaps have two separate
Chandler Carruth7132e002007-08-04 01:51:18 +0000397 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000398 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000399}
400
Evan Cheng0e179d02007-12-17 22:33:23 +0000401bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
Craig Topperc6207612014-04-09 06:08:46 +0000402 NewFn = nullptr;
Evan Cheng0e179d02007-12-17 22:33:23 +0000403 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Filipe Cabecinhas0011c582015-07-03 20:12:01 +0000404 assert(F != NewFn && "Intrinsic function upgraded to the same function");
Duncan Sands38ef3a82007-12-03 20:06:50 +0000405
406 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000407 if (NewFn)
408 F = NewFn;
Pete Cooper9e1d3352015-05-20 17:16:39 +0000409 if (Intrinsic::ID id = F->getIntrinsicID())
410 F->setAttributes(Intrinsic::getAttributes(F->getContext(), id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000411 return Upgraded;
412}
413
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000414bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000415 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000416 return false;
417}
418
Simon Pilgrimf7186822016-06-09 21:09:03 +0000419// Handles upgrading SSE2/AVX2/AVX512BW PSLLDQ intrinsics by converting them
Craig Topperb324e432015-02-18 06:24:44 +0000420// to byte shuffles.
Craig Topper46b34fe2016-07-12 01:42:33 +0000421static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder,
Craig Topper7355ac32016-05-29 06:37:33 +0000422 Value *Op, unsigned Shift) {
423 Type *ResultTy = Op->getType();
424 unsigned NumElts = ResultTy->getVectorNumElements() * 8;
Craig Topperb324e432015-02-18 06:24:44 +0000425
426 // Bitcast from a 64-bit element type to a byte element type.
Craig Topper46b34fe2016-07-12 01:42:33 +0000427 Type *VecTy = VectorType::get(Builder.getInt8Ty(), NumElts);
Craig Topper7355ac32016-05-29 06:37:33 +0000428 Op = Builder.CreateBitCast(Op, VecTy, "cast");
429
Craig Topperb324e432015-02-18 06:24:44 +0000430 // We'll be shuffling in zeroes.
Craig Topper7355ac32016-05-29 06:37:33 +0000431 Value *Res = Constant::getNullValue(VecTy);
Craig Topperb324e432015-02-18 06:24:44 +0000432
433 // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
434 // we'll just return the zero vector.
435 if (Shift < 16) {
Craig Topper99d1eab2016-06-12 00:41:19 +0000436 uint32_t Idxs[64];
Simon Pilgrimf7186822016-06-09 21:09:03 +0000437 // 256/512-bit version is split into 2/4 16-byte lanes.
Craig Topperb324e432015-02-18 06:24:44 +0000438 for (unsigned l = 0; l != NumElts; l += 16)
439 for (unsigned i = 0; i != 16; ++i) {
440 unsigned Idx = NumElts + i - Shift;
441 if (Idx < NumElts)
442 Idx -= NumElts - 16; // end of lane, switch operand.
Craig Topper7355ac32016-05-29 06:37:33 +0000443 Idxs[l + i] = Idx + l;
Craig Topperb324e432015-02-18 06:24:44 +0000444 }
445
Craig Topper7355ac32016-05-29 06:37:33 +0000446 Res = Builder.CreateShuffleVector(Res, Op, makeArrayRef(Idxs, NumElts));
Craig Topperb324e432015-02-18 06:24:44 +0000447 }
448
449 // Bitcast back to a 64-bit element type.
Craig Topper7355ac32016-05-29 06:37:33 +0000450 return Builder.CreateBitCast(Res, ResultTy, "cast");
Craig Topperb324e432015-02-18 06:24:44 +0000451}
452
Craig Topperea703ae2016-06-13 02:36:42 +0000453// Handles upgrading SSE2/AVX2/AVX512BW PSRLDQ intrinsics by converting them
454// to byte shuffles.
Craig Topper46b34fe2016-07-12 01:42:33 +0000455static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, Value *Op,
Craig Topperea703ae2016-06-13 02:36:42 +0000456 unsigned Shift) {
457 Type *ResultTy = Op->getType();
458 unsigned NumElts = ResultTy->getVectorNumElements() * 8;
459
460 // Bitcast from a 64-bit element type to a byte element type.
Craig Topper46b34fe2016-07-12 01:42:33 +0000461 Type *VecTy = VectorType::get(Builder.getInt8Ty(), NumElts);
Craig Topperea703ae2016-06-13 02:36:42 +0000462 Op = Builder.CreateBitCast(Op, VecTy, "cast");
463
464 // We'll be shuffling in zeroes.
465 Value *Res = Constant::getNullValue(VecTy);
466
467 // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
468 // we'll just return the zero vector.
469 if (Shift < 16) {
470 uint32_t Idxs[64];
471 // 256/512-bit version is split into 2/4 16-byte lanes.
472 for (unsigned l = 0; l != NumElts; l += 16)
473 for (unsigned i = 0; i != 16; ++i) {
474 unsigned Idx = i + Shift;
475 if (Idx >= 16)
476 Idx += NumElts - 16; // end of lane, switch operand.
477 Idxs[l + i] = Idx + l;
478 }
479
480 Res = Builder.CreateShuffleVector(Op, Res, makeArrayRef(Idxs, NumElts));
481 }
482
483 // Bitcast back to a 64-bit element type.
484 return Builder.CreateBitCast(Res, ResultTy, "cast");
485}
486
487static Value *getX86MaskVec(IRBuilder<> &Builder, Value *Mask,
488 unsigned NumElts) {
489 llvm::VectorType *MaskTy = llvm::VectorType::get(Builder.getInt1Ty(),
490 cast<IntegerType>(Mask->getType())->getBitWidth());
491 Mask = Builder.CreateBitCast(Mask, MaskTy);
492
493 // If we have less than 8 elements, then the starting mask was an i8 and
494 // we need to extract down to the right number of elements.
495 if (NumElts < 8) {
496 uint32_t Indices[4];
497 for (unsigned i = 0; i != NumElts; ++i)
498 Indices[i] = i;
499 Mask = Builder.CreateShuffleVector(Mask, Mask,
500 makeArrayRef(Indices, NumElts),
501 "extract");
502 }
503
504 return Mask;
505}
506
507static Value *EmitX86Select(IRBuilder<> &Builder, Value *Mask,
508 Value *Op0, Value *Op1) {
509 // If the mask is all ones just emit the align operation.
510 if (const auto *C = dyn_cast<Constant>(Mask))
511 if (C->isAllOnesValue())
512 return Op0;
513
514 Mask = getX86MaskVec(Builder, Mask, Op0->getType()->getVectorNumElements());
515 return Builder.CreateSelect(Mask, Op0, Op1);
516}
517
Craig Topper46b34fe2016-07-12 01:42:33 +0000518static Value *UpgradeX86PALIGNRIntrinsics(IRBuilder<> &Builder,
Craig Topper33350cc2016-06-06 06:12:54 +0000519 Value *Op0, Value *Op1, Value *Shift,
520 Value *Passthru, Value *Mask) {
521 unsigned ShiftVal = cast<llvm::ConstantInt>(Shift)->getZExtValue();
522
523 unsigned NumElts = Op0->getType()->getVectorNumElements();
524 assert(NumElts % 16 == 0);
525
526 // If palignr is shifting the pair of vectors more than the size of two
527 // lanes, emit zero.
528 if (ShiftVal >= 32)
529 return llvm::Constant::getNullValue(Op0->getType());
530
531 // If palignr is shifting the pair of input vectors more than one lane,
532 // but less than two lanes, convert to shifting in zeroes.
533 if (ShiftVal > 16) {
534 ShiftVal -= 16;
535 Op1 = Op0;
536 Op0 = llvm::Constant::getNullValue(Op0->getType());
537 }
538
Craig Topper99d1eab2016-06-12 00:41:19 +0000539 uint32_t Indices[64];
Craig Topper33350cc2016-06-06 06:12:54 +0000540 // 256-bit palignr operates on 128-bit lanes so we need to handle that
541 for (unsigned l = 0; l != NumElts; l += 16) {
542 for (unsigned i = 0; i != 16; ++i) {
543 unsigned Idx = ShiftVal + i;
544 if (Idx >= 16)
545 Idx += NumElts - 16; // End of lane, switch operand.
546 Indices[l + i] = Idx + l;
547 }
548 }
549
550 Value *Align = Builder.CreateShuffleVector(Op1, Op0,
551 makeArrayRef(Indices, NumElts),
552 "palignr");
553
Craig Topperea703ae2016-06-13 02:36:42 +0000554 return EmitX86Select(Builder, Mask, Align, Passthru);
Craig Topperb324e432015-02-18 06:24:44 +0000555}
556
Craig Topper46b34fe2016-07-12 01:42:33 +0000557static Value *UpgradeMaskedStore(IRBuilder<> &Builder,
Craig Topper50f85c22016-05-31 01:50:02 +0000558 Value *Ptr, Value *Data, Value *Mask,
559 bool Aligned) {
560 // Cast the pointer to the right type.
561 Ptr = Builder.CreateBitCast(Ptr,
562 llvm::PointerType::getUnqual(Data->getType()));
563 unsigned Align =
564 Aligned ? cast<VectorType>(Data->getType())->getBitWidth() / 8 : 1;
565
566 // If the mask is all ones just emit a regular store.
567 if (const auto *C = dyn_cast<Constant>(Mask))
568 if (C->isAllOnesValue())
569 return Builder.CreateAlignedStore(Data, Ptr, Align);
570
571 // Convert the mask from an integer type to a vector of i1.
572 unsigned NumElts = Data->getType()->getVectorNumElements();
Craig Topperea703ae2016-06-13 02:36:42 +0000573 Mask = getX86MaskVec(Builder, Mask, NumElts);
Craig Topper50f85c22016-05-31 01:50:02 +0000574 return Builder.CreateMaskedStore(Data, Ptr, Align, Mask);
575}
576
Craig Topper46b34fe2016-07-12 01:42:33 +0000577static Value *UpgradeMaskedLoad(IRBuilder<> &Builder,
Craig Topperf10fbfa2016-06-02 04:19:36 +0000578 Value *Ptr, Value *Passthru, Value *Mask,
579 bool Aligned) {
580 // Cast the pointer to the right type.
581 Ptr = Builder.CreateBitCast(Ptr,
582 llvm::PointerType::getUnqual(Passthru->getType()));
583 unsigned Align =
584 Aligned ? cast<VectorType>(Passthru->getType())->getBitWidth() / 8 : 1;
585
586 // If the mask is all ones just emit a regular store.
587 if (const auto *C = dyn_cast<Constant>(Mask))
588 if (C->isAllOnesValue())
589 return Builder.CreateAlignedLoad(Ptr, Align);
590
591 // Convert the mask from an integer type to a vector of i1.
592 unsigned NumElts = Passthru->getType()->getVectorNumElements();
Craig Topperea703ae2016-06-13 02:36:42 +0000593 Mask = getX86MaskVec(Builder, Mask, NumElts);
Craig Topperf10fbfa2016-06-02 04:19:36 +0000594 return Builder.CreateMaskedLoad(Ptr, Align, Mask, Passthru);
595}
596
Sanjay Patel51ab7572016-06-16 15:48:30 +0000597static Value *upgradeIntMinMax(IRBuilder<> &Builder, CallInst &CI,
598 ICmpInst::Predicate Pred) {
599 Value *Op0 = CI.getArgOperand(0);
600 Value *Op1 = CI.getArgOperand(1);
601 Value *Cmp = Builder.CreateICmp(Pred, Op0, Op1);
602 return Builder.CreateSelect(Cmp, Op0, Op1);
603}
604
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000605static Value *upgradeMaskedCompare(IRBuilder<> &Builder, CallInst &CI,
606 ICmpInst::Predicate Pred) {
607 Value *Op0 = CI.getArgOperand(0);
608 unsigned NumElts = Op0->getType()->getVectorNumElements();
609 Value *Cmp = Builder.CreateICmp(Pred, Op0, CI.getArgOperand(1));
610
611 Value *Mask = CI.getArgOperand(2);
612 const auto *C = dyn_cast<Constant>(Mask);
613 if (!C || !C->isAllOnesValue())
614 Cmp = Builder.CreateAnd(Cmp, getX86MaskVec(Builder, Mask, NumElts));
615
616 if (NumElts < 8) {
617 uint32_t Indices[8];
618 for (unsigned i = 0; i != NumElts; ++i)
619 Indices[i] = i;
620 for (unsigned i = NumElts; i != 8; ++i)
Craig Topperd5d2a352016-07-07 06:11:07 +0000621 Indices[i] = NumElts + i % NumElts;
622 Cmp = Builder.CreateShuffleVector(Cmp,
623 Constant::getNullValue(Cmp->getType()),
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000624 Indices);
625 }
626 return Builder.CreateBitCast(Cmp, IntegerType::get(CI.getContext(),
627 std::max(NumElts, 8U)));
628}
629
Sanjay Patel595098f2016-06-15 22:01:28 +0000630/// Upgrade a call to an old intrinsic. All argument and return casting must be
631/// provided to seamlessly integrate with existing context.
Chandler Carruth7132e002007-08-04 01:51:18 +0000632void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000633 Function *F = CI->getCalledFunction();
Nick Lewycky2eb3ade2011-12-12 22:59:34 +0000634 LLVMContext &C = CI->getContext();
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000635 IRBuilder<> Builder(C);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000636 Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000637
Craig Topper3b1817d2012-02-03 06:10:55 +0000638 assert(F && "Intrinsic call is not direct?");
639
640 if (!NewFn) {
641 // Get the Function's name.
642 StringRef Name = F->getName();
643
Craig Topper5aebb862016-07-04 20:56:38 +0000644 assert(Name.startswith("llvm.") && "Intrinsic doesn't start with 'llvm.'");
645 Name = Name.substr(5);
646
647 bool IsX86 = Name.startswith("x86.");
648 if (IsX86)
649 Name = Name.substr(4);
650
Craig Topper3b1817d2012-02-03 06:10:55 +0000651 Value *Rep;
Sanjay Patel595098f2016-06-15 22:01:28 +0000652 // Upgrade packed integer vector compare intrinsics to compare instructions.
Craig Topper5aebb862016-07-04 20:56:38 +0000653 if (IsX86 && (Name.startswith("sse2.pcmpeq.") ||
654 Name.startswith("avx2.pcmpeq."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000655 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
656 "pcmpeq");
Craig Topper3b1817d2012-02-03 06:10:55 +0000657 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000658 } else if (IsX86 && (Name.startswith("sse2.pcmpgt.") ||
659 Name.startswith("avx2.pcmpgt."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000660 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
661 "pcmpgt");
Craig Topper3b1817d2012-02-03 06:10:55 +0000662 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000663 } else if (IsX86 && Name.startswith("avx512.mask.pcmpeq.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000664 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_EQ);
Craig Topper5aebb862016-07-04 20:56:38 +0000665 } else if (IsX86 && Name.startswith("avx512.mask.pcmpgt.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000666 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000667 } else if (IsX86 && (Name == "sse41.pmaxsb" ||
668 Name == "sse2.pmaxs.w" ||
669 Name == "sse41.pmaxsd" ||
670 Name.startswith("avx2.pmaxs"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000671 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000672 } else if (IsX86 && (Name == "sse2.pmaxu.b" ||
673 Name == "sse41.pmaxuw" ||
674 Name == "sse41.pmaxud" ||
675 Name.startswith("avx2.pmaxu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000676 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_UGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000677 } else if (IsX86 && (Name == "sse41.pminsb" ||
678 Name == "sse2.pmins.w" ||
679 Name == "sse41.pminsd" ||
680 Name.startswith("avx2.pmins"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000681 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SLT);
Craig Topper5aebb862016-07-04 20:56:38 +0000682 } else if (IsX86 && (Name == "sse2.pminu.b" ||
683 Name == "sse41.pminuw" ||
684 Name == "sse41.pminud" ||
685 Name.startswith("avx2.pminu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000686 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_ULT);
Craig Topper5aebb862016-07-04 20:56:38 +0000687 } else if (IsX86 && (Name == "sse2.cvtdq2pd" ||
688 Name == "sse2.cvtps2pd" ||
689 Name == "avx.cvtdq2.pd.256" ||
690 Name == "avx.cvt.ps2.pd.256")) {
Simon Pilgrim4298d062016-05-25 08:59:18 +0000691 // Lossless i32/float to double conversion.
692 // Extract the bottom elements if necessary and convert to double vector.
693 Value *Src = CI->getArgOperand(0);
694 VectorType *SrcTy = cast<VectorType>(Src->getType());
695 VectorType *DstTy = cast<VectorType>(CI->getType());
696 Rep = CI->getArgOperand(0);
697
698 unsigned NumDstElts = DstTy->getNumElements();
699 if (NumDstElts < SrcTy->getNumElements()) {
700 assert(NumDstElts == 2 && "Unexpected vector size");
Craig Topper99d1eab2016-06-12 00:41:19 +0000701 uint32_t ShuffleMask[2] = { 0, 1 };
702 Rep = Builder.CreateShuffleVector(Rep, UndefValue::get(SrcTy),
703 ShuffleMask);
Simon Pilgrim4298d062016-05-25 08:59:18 +0000704 }
705
706 bool Int2Double = (StringRef::npos != Name.find("cvtdq2"));
707 if (Int2Double)
708 Rep = Builder.CreateSIToFP(Rep, DstTy, "cvtdq2pd");
709 else
710 Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd");
Craig Topper5aebb862016-07-04 20:56:38 +0000711 } else if (IsX86 && (Name == "sse2.cvttps2dq" ||
712 Name.startswith("avx.cvtt."))) {
Simon Pilgrim0afd5a42016-06-02 10:55:21 +0000713 // Truncation (round to zero) float/double to i32 vector conversion.
714 Value *Src = CI->getArgOperand(0);
715 VectorType *DstTy = cast<VectorType>(CI->getType());
716 Rep = Builder.CreateFPToSI(Src, DstTy, "cvtt");
Craig Topper5aebb862016-07-04 20:56:38 +0000717 } else if (IsX86 && Name.startswith("sse4a.movnt.")) {
Simon Pilgrimf4b2af12016-06-18 02:38:26 +0000718 Module *M = F->getParent();
719 SmallVector<Metadata *, 1> Elts;
720 Elts.push_back(
721 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
722 MDNode *Node = MDNode::get(C, Elts);
723
724 Value *Arg0 = CI->getArgOperand(0);
725 Value *Arg1 = CI->getArgOperand(1);
726
727 // Nontemporal (unaligned) store of the 0'th element of the float/double
728 // vector.
729 Type *SrcEltTy = cast<VectorType>(Arg1->getType())->getElementType();
730 PointerType *EltPtrTy = PointerType::getUnqual(SrcEltTy);
731 Value *Addr = Builder.CreateBitCast(Arg0, EltPtrTy, "cast");
732 Value *Extract =
733 Builder.CreateExtractElement(Arg1, (uint64_t)0, "extractelement");
734
735 StoreInst *SI = Builder.CreateAlignedStore(Extract, Addr, 1);
736 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
737
738 // Remove intrinsic.
739 CI->eraseFromParent();
740 return;
Craig Topper70610cf2016-07-09 04:38:27 +0000741 } else if (IsX86 && (Name.startswith("avx.movnt.") ||
742 Name.startswith("avx512.storent."))) {
Craig Topper7daf8972012-05-08 06:58:15 +0000743 Module *M = F->getParent();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000744 SmallVector<Metadata *, 1> Elts;
745 Elts.push_back(
746 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
Craig Topper7daf8972012-05-08 06:58:15 +0000747 MDNode *Node = MDNode::get(C, Elts);
748
749 Value *Arg0 = CI->getArgOperand(0);
750 Value *Arg1 = CI->getArgOperand(1);
751
752 // Convert the type of the pointer to a pointer to the stored type.
753 Value *BC = Builder.CreateBitCast(Arg0,
754 PointerType::getUnqual(Arg1->getType()),
755 "cast");
Craig Topper70610cf2016-07-09 04:38:27 +0000756 VectorType *VTy = cast<VectorType>(Arg1->getType());
757 StoreInst *SI = Builder.CreateAlignedStore(Arg1, BC,
758 VTy->getBitWidth() / 8);
Craig Topper7daf8972012-05-08 06:58:15 +0000759 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
Craig Topper7daf8972012-05-08 06:58:15 +0000760
761 // Remove intrinsic.
762 CI->eraseFromParent();
763 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000764 } else if (IsX86 && Name == "sse2.storel.dq") {
Craig Topper12e322a2016-05-25 06:56:32 +0000765 Value *Arg0 = CI->getArgOperand(0);
766 Value *Arg1 = CI->getArgOperand(1);
767
768 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
769 Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
770 Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0);
771 Value *BC = Builder.CreateBitCast(Arg0,
772 PointerType::getUnqual(Elt->getType()),
773 "cast");
Craig Topper29ce55d2016-05-30 22:54:12 +0000774 Builder.CreateAlignedStore(Elt, BC, 1);
Craig Topper12e322a2016-05-25 06:56:32 +0000775
776 // Remove intrinsic.
777 CI->eraseFromParent();
778 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000779 } else if (IsX86 && (Name.startswith("sse.storeu.") ||
780 Name.startswith("sse2.storeu.") ||
781 Name.startswith("avx.storeu."))) {
Craig Topper8287fd82016-05-30 23:15:56 +0000782 Value *Arg0 = CI->getArgOperand(0);
783 Value *Arg1 = CI->getArgOperand(1);
784
785 Arg0 = Builder.CreateBitCast(Arg0,
786 PointerType::getUnqual(Arg1->getType()),
787 "cast");
788 Builder.CreateAlignedStore(Arg1, Arg0, 1);
789
790 // Remove intrinsic.
791 CI->eraseFromParent();
792 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000793 } else if (IsX86 && (Name.startswith("avx512.mask.storeu.p") ||
794 Name.startswith("avx512.mask.storeu.b.") ||
795 Name.startswith("avx512.mask.storeu.w.") ||
796 Name.startswith("avx512.mask.storeu.d.") ||
797 Name.startswith("avx512.mask.storeu.q."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000798 UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
Craig Topper50f85c22016-05-31 01:50:02 +0000799 CI->getArgOperand(2), /*Aligned*/false);
800
801 // Remove intrinsic.
802 CI->eraseFromParent();
803 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000804 } else if (IsX86 && (Name.startswith("avx512.mask.store.p") ||
805 Name.startswith("avx512.mask.store.b.") ||
806 Name.startswith("avx512.mask.store.w.") ||
807 Name.startswith("avx512.mask.store.d.") ||
808 Name.startswith("avx512.mask.store.q."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000809 UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
Craig Topper50f85c22016-05-31 01:50:02 +0000810 CI->getArgOperand(2), /*Aligned*/true);
811
812 // Remove intrinsic.
813 CI->eraseFromParent();
814 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000815 } else if (IsX86 && (Name.startswith("avx512.mask.loadu.p") ||
816 Name.startswith("avx512.mask.loadu.b.") ||
817 Name.startswith("avx512.mask.loadu.w.") ||
818 Name.startswith("avx512.mask.loadu.d.") ||
819 Name.startswith("avx512.mask.loadu.q."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000820 Rep = UpgradeMaskedLoad(Builder, CI->getArgOperand(0),
Craig Topperf10fbfa2016-06-02 04:19:36 +0000821 CI->getArgOperand(1), CI->getArgOperand(2),
822 /*Aligned*/false);
Craig Topper5aebb862016-07-04 20:56:38 +0000823 } else if (IsX86 && (Name.startswith("avx512.mask.load.p") ||
824 Name.startswith("avx512.mask.load.b.") ||
825 Name.startswith("avx512.mask.load.w.") ||
826 Name.startswith("avx512.mask.load.d.") ||
827 Name.startswith("avx512.mask.load.q."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000828 Rep = UpgradeMaskedLoad(Builder, CI->getArgOperand(0),
Craig Topperf10fbfa2016-06-02 04:19:36 +0000829 CI->getArgOperand(1),CI->getArgOperand(2),
830 /*Aligned*/true);
Craig Topper5aebb862016-07-04 20:56:38 +0000831 } else if (IsX86 && Name.startswith("xop.vpcom")) {
Craig Topper3352ba52012-06-09 16:46:13 +0000832 Intrinsic::ID intID;
833 if (Name.endswith("ub"))
834 intID = Intrinsic::x86_xop_vpcomub;
835 else if (Name.endswith("uw"))
836 intID = Intrinsic::x86_xop_vpcomuw;
837 else if (Name.endswith("ud"))
838 intID = Intrinsic::x86_xop_vpcomud;
839 else if (Name.endswith("uq"))
840 intID = Intrinsic::x86_xop_vpcomuq;
841 else if (Name.endswith("b"))
842 intID = Intrinsic::x86_xop_vpcomb;
843 else if (Name.endswith("w"))
844 intID = Intrinsic::x86_xop_vpcomw;
845 else if (Name.endswith("d"))
846 intID = Intrinsic::x86_xop_vpcomd;
847 else if (Name.endswith("q"))
848 intID = Intrinsic::x86_xop_vpcomq;
849 else
850 llvm_unreachable("Unknown suffix");
851
Craig Topper5aebb862016-07-04 20:56:38 +0000852 Name = Name.substr(9); // strip off "xop.vpcom"
Craig Topper3352ba52012-06-09 16:46:13 +0000853 unsigned Imm;
854 if (Name.startswith("lt"))
855 Imm = 0;
856 else if (Name.startswith("le"))
857 Imm = 1;
858 else if (Name.startswith("gt"))
859 Imm = 2;
860 else if (Name.startswith("ge"))
861 Imm = 3;
862 else if (Name.startswith("eq"))
863 Imm = 4;
864 else if (Name.startswith("ne"))
865 Imm = 5;
Craig Topper3352ba52012-06-09 16:46:13 +0000866 else if (Name.startswith("false"))
Craig Toppere32546d2015-02-13 07:42:15 +0000867 Imm = 6;
868 else if (Name.startswith("true"))
Craig Topper3352ba52012-06-09 16:46:13 +0000869 Imm = 7;
870 else
871 llvm_unreachable("Unknown condition");
872
873 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
David Blaikieff6409d2015-05-18 22:13:54 +0000874 Rep =
875 Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1),
876 Builder.getInt8(Imm)});
Craig Topper5aebb862016-07-04 20:56:38 +0000877 } else if (IsX86 && Name == "xop.vpcmov") {
Simon Pilgrime88dc042015-11-03 20:27:01 +0000878 Value *Arg0 = CI->getArgOperand(0);
879 Value *Arg1 = CI->getArgOperand(1);
880 Value *Sel = CI->getArgOperand(2);
881 unsigned NumElts = CI->getType()->getVectorNumElements();
882 Constant *MinusOne = ConstantVector::getSplat(NumElts, Builder.getInt64(-1));
883 Value *NotSel = Builder.CreateXor(Sel, MinusOne);
884 Value *Sel0 = Builder.CreateAnd(Arg0, Sel);
885 Value *Sel1 = Builder.CreateAnd(Arg1, NotSel);
886 Rep = Builder.CreateOr(Sel0, Sel1);
Craig Topper5aebb862016-07-04 20:56:38 +0000887 } else if (IsX86 && Name == "sse42.crc32.64.8") {
Craig Topperef9e9932013-10-15 05:20:47 +0000888 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
889 Intrinsic::x86_sse42_crc32_32_8);
890 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
David Blaikieff6409d2015-05-18 22:13:54 +0000891 Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)});
Craig Topperef9e9932013-10-15 05:20:47 +0000892 Rep = Builder.CreateZExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000893 } else if (IsX86 && Name.startswith("avx.vbroadcast")) {
Adam Nemet39066802014-05-29 23:35:33 +0000894 // Replace broadcasts with a series of insertelements.
895 Type *VecTy = CI->getType();
896 Type *EltTy = VecTy->getVectorElementType();
897 unsigned EltNum = VecTy->getVectorNumElements();
898 Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
899 EltTy->getPointerTo());
David Blaikie0c28fd72015-05-20 21:46:30 +0000900 Value *Load = Builder.CreateLoad(EltTy, Cast);
Adam Nemet39066802014-05-29 23:35:33 +0000901 Type *I32Ty = Type::getInt32Ty(C);
902 Rep = UndefValue::get(VecTy);
903 for (unsigned I = 0; I < EltNum; ++I)
904 Rep = Builder.CreateInsertElement(Rep, Load,
905 ConstantInt::get(I32Ty, I));
Craig Topper5aebb862016-07-04 20:56:38 +0000906 } else if (IsX86 && (Name.startswith("sse41.pmovsx") ||
907 Name.startswith("sse41.pmovzx") ||
908 Name.startswith("avx2.pmovsx") ||
909 Name.startswith("avx2.pmovzx"))) {
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000910 VectorType *SrcTy = cast<VectorType>(CI->getArgOperand(0)->getType());
911 VectorType *DstTy = cast<VectorType>(CI->getType());
912 unsigned NumDstElts = DstTy->getNumElements();
913
Simon Pilgrim9602d672016-05-28 18:03:41 +0000914 // Extract a subvector of the first NumDstElts lanes and sign/zero extend.
Craig Topperc0a5fa02016-06-12 04:48:00 +0000915 SmallVector<uint32_t, 8> ShuffleMask(NumDstElts);
Craig Topper99d1eab2016-06-12 00:41:19 +0000916 for (unsigned i = 0; i != NumDstElts; ++i)
Craig Topperc0a5fa02016-06-12 04:48:00 +0000917 ShuffleMask[i] = i;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000918
919 Value *SV = Builder.CreateShuffleVector(
920 CI->getArgOperand(0), UndefValue::get(SrcTy), ShuffleMask);
Simon Pilgrim9602d672016-05-28 18:03:41 +0000921
922 bool DoSext = (StringRef::npos != Name.find("pmovsx"));
923 Rep = DoSext ? Builder.CreateSExt(SV, DstTy)
924 : Builder.CreateZExt(SV, DstTy);
Craig Topper5aebb862016-07-04 20:56:38 +0000925 } else if (IsX86 && Name == "avx2.vbroadcasti128") {
Juergen Ributzka1f7a1762015-03-04 00:13:25 +0000926 // Replace vbroadcasts with a vector shuffle.
David Blaikie0c28fd72015-05-20 21:46:30 +0000927 Type *VT = VectorType::get(Type::getInt64Ty(C), 2);
928 Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0),
929 PointerType::getUnqual(VT));
930 Value *Load = Builder.CreateLoad(VT, Op);
Craig Topper99d1eab2016-06-12 00:41:19 +0000931 uint32_t Idxs[4] = { 0, 1, 0, 1 };
Juergen Ributzka1f7a1762015-03-04 00:13:25 +0000932 Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
Sanjay Patel2db6d382015-03-12 15:27:07 +0000933 Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +0000934 } else if (IsX86 && (Name.startswith("avx2.pbroadcast") ||
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +0000935 Name.startswith("avx2.vbroadcast") ||
936 Name.startswith("avx512.pbroadcast") ||
937 Name.startswith("avx512.mask.broadcast.s"))) {
Ahmed Bougacha1a4987052015-08-20 20:36:19 +0000938 // Replace vp?broadcasts with a vector shuffle.
939 Value *Op = CI->getArgOperand(0);
940 unsigned NumElts = CI->getType()->getVectorNumElements();
941 Type *MaskTy = VectorType::get(Type::getInt32Ty(C), NumElts);
942 Rep = Builder.CreateShuffleVector(Op, UndefValue::get(Op->getType()),
943 Constant::getNullValue(MaskTy));
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +0000944
945 if (CI->getNumArgOperands() == 3)
946 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
947 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +0000948 } else if (IsX86 && Name.startswith("avx512.mask.palignr.")) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000949 Rep = UpgradeX86PALIGNRIntrinsics(Builder, CI->getArgOperand(0),
Craig Topper33350cc2016-06-06 06:12:54 +0000950 CI->getArgOperand(1),
951 CI->getArgOperand(2),
952 CI->getArgOperand(3),
953 CI->getArgOperand(4));
Craig Topper5aebb862016-07-04 20:56:38 +0000954 } else if (IsX86 && (Name == "sse2.psll.dq" ||
955 Name == "avx2.psll.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +0000956 // 128/256-bit shift left specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +0000957 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +0000958 Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +0000959 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +0000960 } else if (IsX86 && (Name == "sse2.psrl.dq" ||
961 Name == "avx2.psrl.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +0000962 // 128/256-bit shift right specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +0000963 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +0000964 Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +0000965 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +0000966 } else if (IsX86 && (Name == "sse2.psll.dq.bs" ||
967 Name == "avx2.psll.dq.bs" ||
968 Name == "avx512.psll.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +0000969 // 128/256/512-bit shift left specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +0000970 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +0000971 Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +0000972 } else if (IsX86 && (Name == "sse2.psrl.dq.bs" ||
973 Name == "avx2.psrl.dq.bs" ||
974 Name == "avx512.psrl.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +0000975 // 128/256/512-bit shift right specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +0000976 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +0000977 Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +0000978 } else if (IsX86 && (Name == "sse41.pblendw" ||
979 Name.startswith("sse41.blendp") ||
980 Name.startswith("avx.blend.p") ||
981 Name == "avx2.pblendw" ||
982 Name.startswith("avx2.pblendd."))) {
Craig Topper782d6202015-02-28 19:33:17 +0000983 Value *Op0 = CI->getArgOperand(0);
984 Value *Op1 = CI->getArgOperand(1);
985 unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue();
986 VectorType *VecTy = cast<VectorType>(CI->getType());
987 unsigned NumElts = VecTy->getNumElements();
988
Craig Topperc0a5fa02016-06-12 04:48:00 +0000989 SmallVector<uint32_t, 16> Idxs(NumElts);
990 for (unsigned i = 0; i != NumElts; ++i)
991 Idxs[i] = ((Imm >> (i%8)) & 1) ? i + NumElts : i;
Craig Topper782d6202015-02-28 19:33:17 +0000992
Craig Topper2f561822016-06-12 01:05:59 +0000993 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +0000994 } else if (IsX86 && (Name.startswith("avx.vinsertf128.") ||
995 Name == "avx2.vinserti128")) {
Sanjay Patel19792fb2015-03-10 16:08:36 +0000996 Value *Op0 = CI->getArgOperand(0);
997 Value *Op1 = CI->getArgOperand(1);
998 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
999 VectorType *VecTy = cast<VectorType>(CI->getType());
1000 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001001
Sanjay Patel19792fb2015-03-10 16:08:36 +00001002 // Mask off the high bits of the immediate value; hardware ignores those.
1003 Imm = Imm & 1;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001004
Sanjay Patel19792fb2015-03-10 16:08:36 +00001005 // Extend the second operand into a vector that is twice as big.
1006 Value *UndefV = UndefValue::get(Op1->getType());
Craig Topperc0a5fa02016-06-12 04:48:00 +00001007 SmallVector<uint32_t, 8> Idxs(NumElts);
1008 for (unsigned i = 0; i != NumElts; ++i)
1009 Idxs[i] = i;
Craig Topper2f561822016-06-12 01:05:59 +00001010 Rep = Builder.CreateShuffleVector(Op1, UndefV, Idxs);
Sanjay Patel19792fb2015-03-10 16:08:36 +00001011
1012 // Insert the second operand into the first operand.
1013
1014 // Note that there is no guarantee that instruction lowering will actually
1015 // produce a vinsertf128 instruction for the created shuffles. In
1016 // particular, the 0 immediate case involves no lane changes, so it can
1017 // be handled as a blend.
1018
1019 // Example of shuffle mask for 32-bit elements:
1020 // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
1021 // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 >
1022
Sanjay Patel19792fb2015-03-10 16:08:36 +00001023 // The low half of the result is either the low half of the 1st operand
1024 // or the low half of the 2nd operand (the inserted vector).
Craig Topperc0a5fa02016-06-12 04:48:00 +00001025 for (unsigned i = 0; i != NumElts / 2; ++i)
1026 Idxs[i] = Imm ? i : (i + NumElts);
Sanjay Patel19792fb2015-03-10 16:08:36 +00001027 // The high half of the result is either the low half of the 2nd operand
1028 // (the inserted vector) or the high half of the 1st operand.
Craig Topperc0a5fa02016-06-12 04:48:00 +00001029 for (unsigned i = NumElts / 2; i != NumElts; ++i)
1030 Idxs[i] = Imm ? (i + NumElts / 2) : i;
Craig Topper2f561822016-06-12 01:05:59 +00001031 Rep = Builder.CreateShuffleVector(Op0, Rep, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001032 } else if (IsX86 && (Name.startswith("avx.vextractf128.") ||
1033 Name == "avx2.vextracti128")) {
Sanjay Patelaf1846c2015-03-12 15:15:19 +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();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001038
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001039 // Mask off the high bits of the immediate value; hardware ignores those.
1040 Imm = Imm & 1;
1041
1042 // Get indexes for either the high half or low half of the input vector.
Craig Topper2f561822016-06-12 01:05:59 +00001043 SmallVector<uint32_t, 4> Idxs(NumElts);
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001044 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topper2f561822016-06-12 01:05:59 +00001045 Idxs[i] = Imm ? (i + NumElts) : i;
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001046 }
1047
1048 Value *UndefV = UndefValue::get(Op0->getType());
Craig Topper2f561822016-06-12 01:05:59 +00001049 Rep = Builder.CreateShuffleVector(Op0, UndefV, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001050 } else if (!IsX86 && Name == "stackprotectorcheck") {
Tim Shen00127562016-04-08 21:26:31 +00001051 Rep = nullptr;
Craig Topper5aebb862016-07-04 20:56:38 +00001052 } else if (IsX86 && (Name.startswith("avx512.mask.perm.df.") ||
1053 Name.startswith("avx512.mask.perm.di."))) {
Simon Pilgrim02d435d2016-07-04 14:19:05 +00001054 Value *Op0 = CI->getArgOperand(0);
1055 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1056 VectorType *VecTy = cast<VectorType>(CI->getType());
1057 unsigned NumElts = VecTy->getNumElements();
1058
1059 SmallVector<uint32_t, 8> Idxs(NumElts);
1060 for (unsigned i = 0; i != NumElts; ++i)
1061 Idxs[i] = (i & ~0x3) + ((Imm >> (2 * (i & 0x3))) & 3);
1062
1063 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1064
1065 if (CI->getNumArgOperands() == 4)
1066 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1067 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001068 } else if (IsX86 && (Name.startswith("avx.vpermil.") ||
1069 Name == "sse2.pshuf.d" ||
1070 Name.startswith("avx512.mask.vpermil.p") ||
1071 Name.startswith("avx512.mask.pshuf.d."))) {
Craig Topper8a105052016-06-12 03:10:47 +00001072 Value *Op0 = CI->getArgOperand(0);
1073 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1074 VectorType *VecTy = cast<VectorType>(CI->getType());
1075 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9fca3002016-07-04 12:40:54 +00001076 // Calculate the size of each index in the immediate.
Craig Topper8a105052016-06-12 03:10:47 +00001077 unsigned IdxSize = 64 / VecTy->getScalarSizeInBits();
1078 unsigned IdxMask = ((1 << IdxSize) - 1);
1079
1080 SmallVector<uint32_t, 8> Idxs(NumElts);
1081 // Lookup the bits for this element, wrapping around the immediate every
1082 // 8-bits. Elements are grouped into sets of 2 or 4 elements so we need
1083 // to offset by the first index of each group.
1084 for (unsigned i = 0; i != NumElts; ++i)
1085 Idxs[i] = ((Imm >> ((i * IdxSize) % 8)) & IdxMask) | (i & ~IdxMask);
1086
1087 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001088
1089 if (CI->getNumArgOperands() == 4)
1090 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1091 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001092 } else if (IsX86 && (Name == "sse2.pshufl.w" ||
1093 Name.startswith("avx512.mask.pshufl.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001094 Value *Op0 = CI->getArgOperand(0);
1095 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1096 unsigned NumElts = CI->getType()->getVectorNumElements();
1097
1098 SmallVector<uint32_t, 16> Idxs(NumElts);
1099 for (unsigned l = 0; l != NumElts; l += 8) {
1100 for (unsigned i = 0; i != 4; ++i)
1101 Idxs[i + l] = ((Imm >> (2 * i)) & 0x3) + l;
1102 for (unsigned i = 4; i != 8; ++i)
1103 Idxs[i + l] = i + l;
1104 }
1105
1106 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001107
1108 if (CI->getNumArgOperands() == 4)
1109 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1110 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001111 } else if (IsX86 && (Name == "sse2.pshufh.w" ||
1112 Name.startswith("avx512.mask.pshufh.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001113 Value *Op0 = CI->getArgOperand(0);
1114 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1115 unsigned NumElts = CI->getType()->getVectorNumElements();
1116
1117 SmallVector<uint32_t, 16> Idxs(NumElts);
1118 for (unsigned l = 0; l != NumElts; l += 8) {
1119 for (unsigned i = 0; i != 4; ++i)
1120 Idxs[i + l] = i + l;
1121 for (unsigned i = 0; i != 4; ++i)
1122 Idxs[i + l + 4] = ((Imm >> (2 * i)) & 0x3) + 4 + l;
1123 }
1124
1125 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001126
1127 if (CI->getNumArgOperands() == 4)
1128 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1129 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001130 } else if (IsX86 && (Name.startswith("avx512.mask.movddup") ||
1131 Name.startswith("avx512.mask.movshdup") ||
1132 Name.startswith("avx512.mask.movsldup"))) {
Simon Pilgrim19adee92016-07-02 14:42:35 +00001133 Value *Op0 = CI->getArgOperand(0);
1134 unsigned NumElts = CI->getType()->getVectorNumElements();
1135 unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1136
1137 unsigned Offset = 0;
Craig Topper5aebb862016-07-04 20:56:38 +00001138 if (Name.startswith("avx512.mask.movshdup."))
Simon Pilgrim19adee92016-07-02 14:42:35 +00001139 Offset = 1;
1140
1141 SmallVector<uint32_t, 16> Idxs(NumElts);
1142 for (unsigned l = 0; l != NumElts; l += NumLaneElts)
1143 for (unsigned i = 0; i != NumLaneElts; i += 2) {
1144 Idxs[i + l + 0] = i + l + Offset;
1145 Idxs[i + l + 1] = i + l + Offset;
1146 }
1147
1148 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1149
1150 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
1151 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +00001152 } else if (IsX86 && (Name.startswith("avx512.mask.punpckl") ||
1153 Name.startswith("avx512.mask.unpckl."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001154 Value *Op0 = CI->getArgOperand(0);
1155 Value *Op1 = CI->getArgOperand(1);
1156 int NumElts = CI->getType()->getVectorNumElements();
1157 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1158
1159 SmallVector<uint32_t, 64> Idxs(NumElts);
1160 for (int l = 0; l != NumElts; l += NumLaneElts)
1161 for (int i = 0; i != NumLaneElts; ++i)
1162 Idxs[i + l] = l + (i / 2) + NumElts * (i % 2);
1163
1164 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1165
1166 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1167 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001168 } else if (IsX86 && (Name.startswith("avx512.mask.punpckh") ||
1169 Name.startswith("avx512.mask.unpckh."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001170 Value *Op0 = CI->getArgOperand(0);
1171 Value *Op1 = CI->getArgOperand(1);
1172 int NumElts = CI->getType()->getVectorNumElements();
1173 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1174
1175 SmallVector<uint32_t, 64> Idxs(NumElts);
1176 for (int l = 0; l != NumElts; l += NumLaneElts)
1177 for (int i = 0; i != NumLaneElts; ++i)
1178 Idxs[i + l] = (NumLaneElts / 2) + l + (i / 2) + NumElts * (i % 2);
1179
1180 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1181
1182 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1183 CI->getArgOperand(2));
Craig Topper3b1817d2012-02-03 06:10:55 +00001184 } else {
Craig Topper8a105052016-06-12 03:10:47 +00001185 llvm_unreachable("Unknown function for CallInst upgrade.");
Craig Topper3b1817d2012-02-03 06:10:55 +00001186 }
1187
Tim Shen00127562016-04-08 21:26:31 +00001188 if (Rep)
1189 CI->replaceAllUsesWith(Rep);
Craig Topper3b1817d2012-02-03 06:10:55 +00001190 CI->eraseFromParent();
1191 return;
1192 }
1193
Yaron Kerend1fdbe72015-03-30 16:10:39 +00001194 std::string Name = CI->getName();
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001195 if (!Name.empty())
1196 CI->setName(Name + ".old");
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001197
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001198 switch (NewFn->getIntrinsicID()) {
1199 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +00001200 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001201
Craig Topperf7bf6de2016-07-08 06:14:47 +00001202 case Intrinsic::x86_avx512_mask_psll_di_512:
1203 case Intrinsic::x86_avx512_mask_psra_di_512:
1204 case Intrinsic::x86_avx512_mask_psrl_di_512:
1205 case Intrinsic::x86_avx512_mask_psll_qi_512:
1206 case Intrinsic::x86_avx512_mask_psra_qi_512:
1207 case Intrinsic::x86_avx512_mask_psrl_qi_512:
Jeroen Ketemaab99b592015-09-30 10:56:37 +00001208 case Intrinsic::arm_neon_vld1:
1209 case Intrinsic::arm_neon_vld2:
1210 case Intrinsic::arm_neon_vld3:
1211 case Intrinsic::arm_neon_vld4:
1212 case Intrinsic::arm_neon_vld2lane:
1213 case Intrinsic::arm_neon_vld3lane:
1214 case Intrinsic::arm_neon_vld4lane:
1215 case Intrinsic::arm_neon_vst1:
1216 case Intrinsic::arm_neon_vst2:
1217 case Intrinsic::arm_neon_vst3:
1218 case Intrinsic::arm_neon_vst4:
1219 case Intrinsic::arm_neon_vst2lane:
1220 case Intrinsic::arm_neon_vst3lane:
1221 case Intrinsic::arm_neon_vst4lane: {
1222 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1223 CI->arg_operands().end());
1224 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1225 CI->eraseFromParent();
1226 return;
1227 }
1228
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001229 case Intrinsic::ctlz:
Nuno Lopesad40c0a2012-05-22 15:25:31 +00001230 case Intrinsic::cttz:
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001231 assert(CI->getNumArgOperands() == 1 &&
1232 "Mismatch between function args and call args");
David Blaikieff6409d2015-05-18 22:13:54 +00001233 CI->replaceAllUsesWith(Builder.CreateCall(
1234 NewFn, {CI->getArgOperand(0), Builder.getFalse()}, Name));
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001235 CI->eraseFromParent();
1236 return;
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001237
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001238 case Intrinsic::objectsize:
David Blaikieff6409d2015-05-18 22:13:54 +00001239 CI->replaceAllUsesWith(Builder.CreateCall(
1240 NewFn, {CI->getArgOperand(0), CI->getArgOperand(1)}, Name));
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001241 CI->eraseFromParent();
1242 return;
1243
Joel Jonesb84f7be2012-07-18 00:02:16 +00001244 case Intrinsic::ctpop: {
David Blaikieff6409d2015-05-18 22:13:54 +00001245 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {CI->getArgOperand(0)}));
Joel Jonesb84f7be2012-07-18 00:02:16 +00001246 CI->eraseFromParent();
1247 return;
1248 }
Joel Jones43cb8782012-07-13 23:25:25 +00001249
Craig Topper71dc02d2012-06-13 07:18:53 +00001250 case Intrinsic::x86_xop_vfrcz_ss:
1251 case Intrinsic::x86_xop_vfrcz_sd:
David Blaikieff6409d2015-05-18 22:13:54 +00001252 CI->replaceAllUsesWith(
1253 Builder.CreateCall(NewFn, {CI->getArgOperand(1)}, Name));
Craig Topper71dc02d2012-06-13 07:18:53 +00001254 CI->eraseFromParent();
1255 return;
1256
Simon Pilgrime85506b2016-06-03 08:06:03 +00001257 case Intrinsic::x86_xop_vpermil2pd:
1258 case Intrinsic::x86_xop_vpermil2ps:
1259 case Intrinsic::x86_xop_vpermil2pd_256:
1260 case Intrinsic::x86_xop_vpermil2ps_256: {
1261 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1262 CI->arg_operands().end());
1263 VectorType *FltIdxTy = cast<VectorType>(Args[2]->getType());
1264 VectorType *IntIdxTy = VectorType::getInteger(FltIdxTy);
1265 Args[2] = Builder.CreateBitCast(Args[2], IntIdxTy);
1266 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args, Name));
1267 CI->eraseFromParent();
1268 return;
1269 }
1270
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001271 case Intrinsic::x86_sse41_ptestc:
1272 case Intrinsic::x86_sse41_ptestz:
Craig Topper71dc02d2012-06-13 07:18:53 +00001273 case Intrinsic::x86_sse41_ptestnzc: {
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001274 // The arguments for these intrinsics used to be v4f32, and changed
1275 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
1276 // So, the only thing required is a bitcast for both arguments.
1277 // First, check the arguments have the old type.
1278 Value *Arg0 = CI->getArgOperand(0);
1279 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
1280 return;
1281
1282 // Old intrinsic, add bitcasts
1283 Value *Arg1 = CI->getArgOperand(1);
1284
David Blaikie5bacf372015-04-24 21:16:07 +00001285 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001286
David Blaikie5bacf372015-04-24 21:16:07 +00001287 Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast");
1288 Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
1289
David Blaikieff6409d2015-05-18 22:13:54 +00001290 CallInst *NewCall = Builder.CreateCall(NewFn, {BC0, BC1}, Name);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001291 CI->replaceAllUsesWith(NewCall);
1292 CI->eraseFromParent();
1293 return;
Evan Cheng0e179d02007-12-17 22:33:23 +00001294 }
Chandler Carruth373b2b12014-09-06 10:00:01 +00001295
Chandler Carruth373b2b12014-09-06 10:00:01 +00001296 case Intrinsic::x86_sse41_insertps:
1297 case Intrinsic::x86_sse41_dppd:
1298 case Intrinsic::x86_sse41_dpps:
1299 case Intrinsic::x86_sse41_mpsadbw:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001300 case Intrinsic::x86_avx_dp_ps_256:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001301 case Intrinsic::x86_avx2_mpsadbw: {
1302 // Need to truncate the last argument from i32 to i8 -- this argument models
1303 // an inherently 8-bit immediate operand to these x86 instructions.
1304 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1305 CI->arg_operands().end());
1306
1307 // Replace the last argument with a trunc.
1308 Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
1309
1310 CallInst *NewCall = Builder.CreateCall(NewFn, Args);
1311 CI->replaceAllUsesWith(NewCall);
1312 CI->eraseFromParent();
1313 return;
1314 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +00001315
1316 case Intrinsic::thread_pointer: {
1317 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {}));
1318 CI->eraseFromParent();
1319 return;
1320 }
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +00001321
1322 case Intrinsic::masked_load:
1323 case Intrinsic::masked_store: {
1324 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1325 CI->arg_operands().end());
1326 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1327 CI->eraseFromParent();
1328 return;
1329 }
Craig Topper71dc02d2012-06-13 07:18:53 +00001330 }
Chandler Carruth7132e002007-08-04 01:51:18 +00001331}
1332
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001333void llvm::UpgradeCallsToIntrinsic(Function *F) {
Chandler Carruth7132e002007-08-04 01:51:18 +00001334 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
1335
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001336 // Check if this function should be upgraded and get the replacement function
1337 // if there is one.
Chris Lattner80ed9dc2011-06-18 06:05:24 +00001338 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +00001339 if (UpgradeIntrinsicFunction(F, NewFn)) {
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001340 // Replace all users of the old function with the new function or new
1341 // instructions. This is not a range loop because the call is deleted.
1342 for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; )
Duncan P. N. Exon Smith93f53c42016-04-17 03:59:37 +00001343 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001344 UpgradeIntrinsicCall(CI, NewFn);
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001345
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001346 // Remove old function, no longer used, from the module.
1347 F->eraseFromParent();
Chandler Carruth7132e002007-08-04 01:51:18 +00001348 }
1349}
Devang Patel80ae3492009-08-28 23:24:31 +00001350
Manman Ren209b17c2013-09-28 00:22:27 +00001351void llvm::UpgradeInstWithTBAATag(Instruction *I) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001352 MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
Manman Ren209b17c2013-09-28 00:22:27 +00001353 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
1354 // Check if the tag uses struct-path aware TBAA format.
1355 if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
1356 return;
1357
1358 if (MD->getNumOperands() == 3) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001359 Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
Manman Ren209b17c2013-09-28 00:22:27 +00001360 MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
1361 // Create a MDNode <ScalarType, ScalarType, offset 0, const>
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001362 Metadata *Elts2[] = {ScalarType, ScalarType,
1363 ConstantAsMetadata::get(Constant::getNullValue(
1364 Type::getInt64Ty(I->getContext()))),
1365 MD->getOperand(2)};
Manman Ren209b17c2013-09-28 00:22:27 +00001366 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
1367 } else {
1368 // Create a MDNode <MD, MD, offset 0>
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001369 Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
1370 Type::getInt64Ty(I->getContext())))};
Manman Ren209b17c2013-09-28 00:22:27 +00001371 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
1372 }
1373}
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001374
1375Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
1376 Instruction *&Temp) {
1377 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001378 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001379
Craig Topperc6207612014-04-09 06:08:46 +00001380 Temp = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001381 Type *SrcTy = V->getType();
1382 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1383 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1384 LLVMContext &Context = V->getContext();
1385
1386 // We have no information about target data layout, so we assume that
1387 // the maximum pointer size is 64bit.
1388 Type *MidTy = Type::getInt64Ty(Context);
1389 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
1390
1391 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
1392 }
1393
Craig Topperc6207612014-04-09 06:08:46 +00001394 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001395}
1396
1397Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
1398 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001399 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001400
1401 Type *SrcTy = C->getType();
1402 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1403 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1404 LLVMContext &Context = C->getContext();
1405
1406 // We have no information about target data layout, so we assume that
1407 // the maximum pointer size is 64bit.
1408 Type *MidTy = Type::getInt64Ty(Context);
1409
1410 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
1411 DestTy);
1412 }
1413
Craig Topperc6207612014-04-09 06:08:46 +00001414 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001415}
Manman Ren8b4306c2013-12-02 21:29:56 +00001416
1417/// Check the debug info version number, if it is out-dated, drop the debug
1418/// info. Return true if module is modified.
1419bool llvm::UpgradeDebugInfo(Module &M) {
Manman Ren2ebfb422014-01-16 01:51:12 +00001420 unsigned Version = getDebugMetadataVersionFromModule(M);
1421 if (Version == DEBUG_METADATA_VERSION)
Manman Ren8b4306c2013-12-02 21:29:56 +00001422 return false;
1423
Manman Ren2ebfb422014-01-16 01:51:12 +00001424 bool RetCode = StripDebugInfo(M);
1425 if (RetCode) {
1426 DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
1427 M.getContext().diagnose(DiagVersion);
1428 }
1429 return RetCode;
Manman Ren8b4306c2013-12-02 21:29:56 +00001430}
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001431
Manman Renb5d7ff42016-05-25 23:14:48 +00001432bool llvm::UpgradeModuleFlags(Module &M) {
1433 const NamedMDNode *ModFlags = M.getModuleFlagsMetadata();
1434 if (!ModFlags)
1435 return false;
1436
1437 bool HasObjCFlag = false, HasClassProperties = false;
1438 for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
1439 MDNode *Op = ModFlags->getOperand(I);
1440 if (Op->getNumOperands() < 2)
1441 continue;
1442 MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1443 if (!ID)
1444 continue;
1445 if (ID->getString() == "Objective-C Image Info Version")
1446 HasObjCFlag = true;
1447 if (ID->getString() == "Objective-C Class Properties")
1448 HasClassProperties = true;
1449 }
1450 // "Objective-C Class Properties" is recently added for Objective-C. We
1451 // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module
1452 // flag of value 0, so we can correclty report error when trying to link
1453 // an ObjC bitcode without this module flag with an ObjC bitcode with this
1454 // module flag.
1455 if (HasObjCFlag && !HasClassProperties) {
1456 M.addModuleFlag(llvm::Module::Error, "Objective-C Class Properties",
1457 (uint32_t)0);
1458 return true;
1459 }
1460 return false;
1461}
1462
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00001463static bool isOldLoopArgument(Metadata *MD) {
1464 auto *T = dyn_cast_or_null<MDTuple>(MD);
1465 if (!T)
1466 return false;
1467 if (T->getNumOperands() < 1)
1468 return false;
1469 auto *S = dyn_cast_or_null<MDString>(T->getOperand(0));
1470 if (!S)
1471 return false;
1472 return S->getString().startswith("llvm.vectorizer.");
1473}
1474
1475static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) {
1476 StringRef OldPrefix = "llvm.vectorizer.";
1477 assert(OldTag.startswith(OldPrefix) && "Expected old prefix");
1478
1479 if (OldTag == "llvm.vectorizer.unroll")
1480 return MDString::get(C, "llvm.loop.interleave.count");
1481
1482 return MDString::get(
1483 C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size()))
1484 .str());
1485}
1486
1487static Metadata *upgradeLoopArgument(Metadata *MD) {
1488 auto *T = dyn_cast_or_null<MDTuple>(MD);
1489 if (!T)
1490 return MD;
1491 if (T->getNumOperands() < 1)
1492 return MD;
1493 auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0));
1494 if (!OldTag)
1495 return MD;
1496 if (!OldTag->getString().startswith("llvm.vectorizer."))
1497 return MD;
1498
1499 // This has an old tag. Upgrade it.
1500 SmallVector<Metadata *, 8> Ops;
1501 Ops.reserve(T->getNumOperands());
1502 Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString()));
1503 for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I)
1504 Ops.push_back(T->getOperand(I));
1505
1506 return MDTuple::get(T->getContext(), Ops);
1507}
1508
1509MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) {
1510 auto *T = dyn_cast<MDTuple>(&N);
1511 if (!T)
1512 return &N;
1513
1514 if (!llvm::any_of(T->operands(), isOldLoopArgument))
1515 return &N;
1516
1517 SmallVector<Metadata *, 8> Ops;
1518 Ops.reserve(T->getNumOperands());
1519 for (Metadata *MD : T->operands())
1520 Ops.push_back(upgradeLoopArgument(MD));
1521
1522 return MDTuple::get(T->getContext(), Ops);
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001523}