blob: ad73897aff81881ee30d2659e811926f4923108f [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.
421static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C,
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 Topper7355ac32016-05-29 06:37:33 +0000427 Type *VecTy = VectorType::get(Type::getInt8Ty(C), NumElts);
428 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.
455static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C,
456 Value *Op,
457 unsigned Shift) {
458 Type *ResultTy = Op->getType();
459 unsigned NumElts = ResultTy->getVectorNumElements() * 8;
460
461 // Bitcast from a 64-bit element type to a byte element type.
462 Type *VecTy = VectorType::get(Type::getInt8Ty(C), NumElts);
463 Op = Builder.CreateBitCast(Op, VecTy, "cast");
464
465 // We'll be shuffling in zeroes.
466 Value *Res = Constant::getNullValue(VecTy);
467
468 // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
469 // we'll just return the zero vector.
470 if (Shift < 16) {
471 uint32_t Idxs[64];
472 // 256/512-bit version is split into 2/4 16-byte lanes.
473 for (unsigned l = 0; l != NumElts; l += 16)
474 for (unsigned i = 0; i != 16; ++i) {
475 unsigned Idx = i + Shift;
476 if (Idx >= 16)
477 Idx += NumElts - 16; // end of lane, switch operand.
478 Idxs[l + i] = Idx + l;
479 }
480
481 Res = Builder.CreateShuffleVector(Op, Res, makeArrayRef(Idxs, NumElts));
482 }
483
484 // Bitcast back to a 64-bit element type.
485 return Builder.CreateBitCast(Res, ResultTy, "cast");
486}
487
488static Value *getX86MaskVec(IRBuilder<> &Builder, Value *Mask,
489 unsigned NumElts) {
490 llvm::VectorType *MaskTy = llvm::VectorType::get(Builder.getInt1Ty(),
491 cast<IntegerType>(Mask->getType())->getBitWidth());
492 Mask = Builder.CreateBitCast(Mask, MaskTy);
493
494 // If we have less than 8 elements, then the starting mask was an i8 and
495 // we need to extract down to the right number of elements.
496 if (NumElts < 8) {
497 uint32_t Indices[4];
498 for (unsigned i = 0; i != NumElts; ++i)
499 Indices[i] = i;
500 Mask = Builder.CreateShuffleVector(Mask, Mask,
501 makeArrayRef(Indices, NumElts),
502 "extract");
503 }
504
505 return Mask;
506}
507
508static Value *EmitX86Select(IRBuilder<> &Builder, Value *Mask,
509 Value *Op0, Value *Op1) {
510 // If the mask is all ones just emit the align operation.
511 if (const auto *C = dyn_cast<Constant>(Mask))
512 if (C->isAllOnesValue())
513 return Op0;
514
515 Mask = getX86MaskVec(Builder, Mask, Op0->getType()->getVectorNumElements());
516 return Builder.CreateSelect(Mask, Op0, Op1);
517}
518
Craig Topper33350cc2016-06-06 06:12:54 +0000519static Value *UpgradeX86PALIGNRIntrinsics(IRBuilder<> &Builder, LLVMContext &C,
520 Value *Op0, Value *Op1, Value *Shift,
521 Value *Passthru, Value *Mask) {
522 unsigned ShiftVal = cast<llvm::ConstantInt>(Shift)->getZExtValue();
523
524 unsigned NumElts = Op0->getType()->getVectorNumElements();
525 assert(NumElts % 16 == 0);
526
527 // If palignr is shifting the pair of vectors more than the size of two
528 // lanes, emit zero.
529 if (ShiftVal >= 32)
530 return llvm::Constant::getNullValue(Op0->getType());
531
532 // If palignr is shifting the pair of input vectors more than one lane,
533 // but less than two lanes, convert to shifting in zeroes.
534 if (ShiftVal > 16) {
535 ShiftVal -= 16;
536 Op1 = Op0;
537 Op0 = llvm::Constant::getNullValue(Op0->getType());
538 }
539
Craig Topper99d1eab2016-06-12 00:41:19 +0000540 uint32_t Indices[64];
Craig Topper33350cc2016-06-06 06:12:54 +0000541 // 256-bit palignr operates on 128-bit lanes so we need to handle that
542 for (unsigned l = 0; l != NumElts; l += 16) {
543 for (unsigned i = 0; i != 16; ++i) {
544 unsigned Idx = ShiftVal + i;
545 if (Idx >= 16)
546 Idx += NumElts - 16; // End of lane, switch operand.
547 Indices[l + i] = Idx + l;
548 }
549 }
550
551 Value *Align = Builder.CreateShuffleVector(Op1, Op0,
552 makeArrayRef(Indices, NumElts),
553 "palignr");
554
Craig Topperea703ae2016-06-13 02:36:42 +0000555 return EmitX86Select(Builder, Mask, Align, Passthru);
Craig Topperb324e432015-02-18 06:24:44 +0000556}
557
Craig Topper50f85c22016-05-31 01:50:02 +0000558static Value *UpgradeMaskedStore(IRBuilder<> &Builder, LLVMContext &C,
559 Value *Ptr, Value *Data, Value *Mask,
560 bool Aligned) {
561 // Cast the pointer to the right type.
562 Ptr = Builder.CreateBitCast(Ptr,
563 llvm::PointerType::getUnqual(Data->getType()));
564 unsigned Align =
565 Aligned ? cast<VectorType>(Data->getType())->getBitWidth() / 8 : 1;
566
567 // If the mask is all ones just emit a regular store.
568 if (const auto *C = dyn_cast<Constant>(Mask))
569 if (C->isAllOnesValue())
570 return Builder.CreateAlignedStore(Data, Ptr, Align);
571
572 // Convert the mask from an integer type to a vector of i1.
573 unsigned NumElts = Data->getType()->getVectorNumElements();
Craig Topperea703ae2016-06-13 02:36:42 +0000574 Mask = getX86MaskVec(Builder, Mask, NumElts);
Craig Topper50f85c22016-05-31 01:50:02 +0000575 return Builder.CreateMaskedStore(Data, Ptr, Align, Mask);
576}
577
Craig Topperf10fbfa2016-06-02 04:19:36 +0000578static Value *UpgradeMaskedLoad(IRBuilder<> &Builder, LLVMContext &C,
579 Value *Ptr, Value *Passthru, Value *Mask,
580 bool Aligned) {
581 // Cast the pointer to the right type.
582 Ptr = Builder.CreateBitCast(Ptr,
583 llvm::PointerType::getUnqual(Passthru->getType()));
584 unsigned Align =
585 Aligned ? cast<VectorType>(Passthru->getType())->getBitWidth() / 8 : 1;
586
587 // If the mask is all ones just emit a regular store.
588 if (const auto *C = dyn_cast<Constant>(Mask))
589 if (C->isAllOnesValue())
590 return Builder.CreateAlignedLoad(Ptr, Align);
591
592 // Convert the mask from an integer type to a vector of i1.
593 unsigned NumElts = Passthru->getType()->getVectorNumElements();
Craig Topperea703ae2016-06-13 02:36:42 +0000594 Mask = getX86MaskVec(Builder, Mask, NumElts);
Craig Topperf10fbfa2016-06-02 04:19:36 +0000595 return Builder.CreateMaskedLoad(Ptr, Align, Mask, Passthru);
596}
597
Sanjay Patel51ab7572016-06-16 15:48:30 +0000598static Value *upgradeIntMinMax(IRBuilder<> &Builder, CallInst &CI,
599 ICmpInst::Predicate Pred) {
600 Value *Op0 = CI.getArgOperand(0);
601 Value *Op1 = CI.getArgOperand(1);
602 Value *Cmp = Builder.CreateICmp(Pred, Op0, Op1);
603 return Builder.CreateSelect(Cmp, Op0, Op1);
604}
605
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000606static Value *upgradeMaskedCompare(IRBuilder<> &Builder, CallInst &CI,
607 ICmpInst::Predicate Pred) {
608 Value *Op0 = CI.getArgOperand(0);
609 unsigned NumElts = Op0->getType()->getVectorNumElements();
610 Value *Cmp = Builder.CreateICmp(Pred, Op0, CI.getArgOperand(1));
611
612 Value *Mask = CI.getArgOperand(2);
613 const auto *C = dyn_cast<Constant>(Mask);
614 if (!C || !C->isAllOnesValue())
615 Cmp = Builder.CreateAnd(Cmp, getX86MaskVec(Builder, Mask, NumElts));
616
617 if (NumElts < 8) {
618 uint32_t Indices[8];
619 for (unsigned i = 0; i != NumElts; ++i)
620 Indices[i] = i;
621 for (unsigned i = NumElts; i != 8; ++i)
Craig Topperd5d2a352016-07-07 06:11:07 +0000622 Indices[i] = NumElts + i % NumElts;
623 Cmp = Builder.CreateShuffleVector(Cmp,
624 Constant::getNullValue(Cmp->getType()),
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000625 Indices);
626 }
627 return Builder.CreateBitCast(Cmp, IntegerType::get(CI.getContext(),
628 std::max(NumElts, 8U)));
629}
630
Sanjay Patel595098f2016-06-15 22:01:28 +0000631/// Upgrade a call to an old intrinsic. All argument and return casting must be
632/// provided to seamlessly integrate with existing context.
Chandler Carruth7132e002007-08-04 01:51:18 +0000633void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000634 Function *F = CI->getCalledFunction();
Nick Lewycky2eb3ade2011-12-12 22:59:34 +0000635 LLVMContext &C = CI->getContext();
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000636 IRBuilder<> Builder(C);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000637 Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000638
Craig Topper3b1817d2012-02-03 06:10:55 +0000639 assert(F && "Intrinsic call is not direct?");
640
641 if (!NewFn) {
642 // Get the Function's name.
643 StringRef Name = F->getName();
644
Craig Topper5aebb862016-07-04 20:56:38 +0000645 assert(Name.startswith("llvm.") && "Intrinsic doesn't start with 'llvm.'");
646 Name = Name.substr(5);
647
648 bool IsX86 = Name.startswith("x86.");
649 if (IsX86)
650 Name = Name.substr(4);
651
Craig Topper3b1817d2012-02-03 06:10:55 +0000652 Value *Rep;
Sanjay Patel595098f2016-06-15 22:01:28 +0000653 // Upgrade packed integer vector compare intrinsics to compare instructions.
Craig Topper5aebb862016-07-04 20:56:38 +0000654 if (IsX86 && (Name.startswith("sse2.pcmpeq.") ||
655 Name.startswith("avx2.pcmpeq."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000656 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
657 "pcmpeq");
Craig Topper3b1817d2012-02-03 06:10:55 +0000658 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000659 } else if (IsX86 && (Name.startswith("sse2.pcmpgt.") ||
660 Name.startswith("avx2.pcmpgt."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000661 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
662 "pcmpgt");
Craig Topper3b1817d2012-02-03 06:10:55 +0000663 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000664 } else if (IsX86 && Name.startswith("avx512.mask.pcmpeq.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000665 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_EQ);
Craig Topper5aebb862016-07-04 20:56:38 +0000666 } else if (IsX86 && Name.startswith("avx512.mask.pcmpgt.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000667 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000668 } else if (IsX86 && (Name == "sse41.pmaxsb" ||
669 Name == "sse2.pmaxs.w" ||
670 Name == "sse41.pmaxsd" ||
671 Name.startswith("avx2.pmaxs"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000672 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000673 } else if (IsX86 && (Name == "sse2.pmaxu.b" ||
674 Name == "sse41.pmaxuw" ||
675 Name == "sse41.pmaxud" ||
676 Name.startswith("avx2.pmaxu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000677 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_UGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000678 } else if (IsX86 && (Name == "sse41.pminsb" ||
679 Name == "sse2.pmins.w" ||
680 Name == "sse41.pminsd" ||
681 Name.startswith("avx2.pmins"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000682 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SLT);
Craig Topper5aebb862016-07-04 20:56:38 +0000683 } else if (IsX86 && (Name == "sse2.pminu.b" ||
684 Name == "sse41.pminuw" ||
685 Name == "sse41.pminud" ||
686 Name.startswith("avx2.pminu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000687 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_ULT);
Craig Topper5aebb862016-07-04 20:56:38 +0000688 } else if (IsX86 && (Name == "sse2.cvtdq2pd" ||
689 Name == "sse2.cvtps2pd" ||
690 Name == "avx.cvtdq2.pd.256" ||
691 Name == "avx.cvt.ps2.pd.256")) {
Simon Pilgrim4298d062016-05-25 08:59:18 +0000692 // Lossless i32/float to double conversion.
693 // Extract the bottom elements if necessary and convert to double vector.
694 Value *Src = CI->getArgOperand(0);
695 VectorType *SrcTy = cast<VectorType>(Src->getType());
696 VectorType *DstTy = cast<VectorType>(CI->getType());
697 Rep = CI->getArgOperand(0);
698
699 unsigned NumDstElts = DstTy->getNumElements();
700 if (NumDstElts < SrcTy->getNumElements()) {
701 assert(NumDstElts == 2 && "Unexpected vector size");
Craig Topper99d1eab2016-06-12 00:41:19 +0000702 uint32_t ShuffleMask[2] = { 0, 1 };
703 Rep = Builder.CreateShuffleVector(Rep, UndefValue::get(SrcTy),
704 ShuffleMask);
Simon Pilgrim4298d062016-05-25 08:59:18 +0000705 }
706
707 bool Int2Double = (StringRef::npos != Name.find("cvtdq2"));
708 if (Int2Double)
709 Rep = Builder.CreateSIToFP(Rep, DstTy, "cvtdq2pd");
710 else
711 Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd");
Craig Topper5aebb862016-07-04 20:56:38 +0000712 } else if (IsX86 && (Name == "sse2.cvttps2dq" ||
713 Name.startswith("avx.cvtt."))) {
Simon Pilgrim0afd5a42016-06-02 10:55:21 +0000714 // Truncation (round to zero) float/double to i32 vector conversion.
715 Value *Src = CI->getArgOperand(0);
716 VectorType *DstTy = cast<VectorType>(CI->getType());
717 Rep = Builder.CreateFPToSI(Src, DstTy, "cvtt");
Craig Topper5aebb862016-07-04 20:56:38 +0000718 } else if (IsX86 && Name.startswith("sse4a.movnt.")) {
Simon Pilgrimf4b2af12016-06-18 02:38:26 +0000719 Module *M = F->getParent();
720 SmallVector<Metadata *, 1> Elts;
721 Elts.push_back(
722 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
723 MDNode *Node = MDNode::get(C, Elts);
724
725 Value *Arg0 = CI->getArgOperand(0);
726 Value *Arg1 = CI->getArgOperand(1);
727
728 // Nontemporal (unaligned) store of the 0'th element of the float/double
729 // vector.
730 Type *SrcEltTy = cast<VectorType>(Arg1->getType())->getElementType();
731 PointerType *EltPtrTy = PointerType::getUnqual(SrcEltTy);
732 Value *Addr = Builder.CreateBitCast(Arg0, EltPtrTy, "cast");
733 Value *Extract =
734 Builder.CreateExtractElement(Arg1, (uint64_t)0, "extractelement");
735
736 StoreInst *SI = Builder.CreateAlignedStore(Extract, Addr, 1);
737 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
738
739 // Remove intrinsic.
740 CI->eraseFromParent();
741 return;
Craig Topper70610cf2016-07-09 04:38:27 +0000742 } else if (IsX86 && (Name.startswith("avx.movnt.") ||
743 Name.startswith("avx512.storent."))) {
Craig Topper7daf8972012-05-08 06:58:15 +0000744 Module *M = F->getParent();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000745 SmallVector<Metadata *, 1> Elts;
746 Elts.push_back(
747 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
Craig Topper7daf8972012-05-08 06:58:15 +0000748 MDNode *Node = MDNode::get(C, Elts);
749
750 Value *Arg0 = CI->getArgOperand(0);
751 Value *Arg1 = CI->getArgOperand(1);
752
753 // Convert the type of the pointer to a pointer to the stored type.
754 Value *BC = Builder.CreateBitCast(Arg0,
755 PointerType::getUnqual(Arg1->getType()),
756 "cast");
Craig Topper70610cf2016-07-09 04:38:27 +0000757 VectorType *VTy = cast<VectorType>(Arg1->getType());
758 StoreInst *SI = Builder.CreateAlignedStore(Arg1, BC,
759 VTy->getBitWidth() / 8);
Craig Topper7daf8972012-05-08 06:58:15 +0000760 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
Craig Topper7daf8972012-05-08 06:58:15 +0000761
762 // Remove intrinsic.
763 CI->eraseFromParent();
764 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000765 } else if (IsX86 && Name == "sse2.storel.dq") {
Craig Topper12e322a2016-05-25 06:56:32 +0000766 Value *Arg0 = CI->getArgOperand(0);
767 Value *Arg1 = CI->getArgOperand(1);
768
769 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
770 Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
771 Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0);
772 Value *BC = Builder.CreateBitCast(Arg0,
773 PointerType::getUnqual(Elt->getType()),
774 "cast");
Craig Topper29ce55d2016-05-30 22:54:12 +0000775 Builder.CreateAlignedStore(Elt, BC, 1);
Craig Topper12e322a2016-05-25 06:56:32 +0000776
777 // Remove intrinsic.
778 CI->eraseFromParent();
779 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000780 } else if (IsX86 && (Name.startswith("sse.storeu.") ||
781 Name.startswith("sse2.storeu.") ||
782 Name.startswith("avx.storeu."))) {
Craig Topper8287fd82016-05-30 23:15:56 +0000783 Value *Arg0 = CI->getArgOperand(0);
784 Value *Arg1 = CI->getArgOperand(1);
785
786 Arg0 = Builder.CreateBitCast(Arg0,
787 PointerType::getUnqual(Arg1->getType()),
788 "cast");
789 Builder.CreateAlignedStore(Arg1, Arg0, 1);
790
791 // Remove intrinsic.
792 CI->eraseFromParent();
793 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000794 } else if (IsX86 && (Name.startswith("avx512.mask.storeu.p") ||
795 Name.startswith("avx512.mask.storeu.b.") ||
796 Name.startswith("avx512.mask.storeu.w.") ||
797 Name.startswith("avx512.mask.storeu.d.") ||
798 Name.startswith("avx512.mask.storeu.q."))) {
Craig Topper50f85c22016-05-31 01:50:02 +0000799 UpgradeMaskedStore(Builder, C, CI->getArgOperand(0), CI->getArgOperand(1),
800 CI->getArgOperand(2), /*Aligned*/false);
801
802 // Remove intrinsic.
803 CI->eraseFromParent();
804 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000805 } else if (IsX86 && (Name.startswith("avx512.mask.store.p") ||
806 Name.startswith("avx512.mask.store.b.") ||
807 Name.startswith("avx512.mask.store.w.") ||
808 Name.startswith("avx512.mask.store.d.") ||
809 Name.startswith("avx512.mask.store.q."))) {
Craig Topper50f85c22016-05-31 01:50:02 +0000810 UpgradeMaskedStore(Builder, C, CI->getArgOperand(0), CI->getArgOperand(1),
811 CI->getArgOperand(2), /*Aligned*/true);
812
813 // Remove intrinsic.
814 CI->eraseFromParent();
815 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000816 } else if (IsX86 && (Name.startswith("avx512.mask.loadu.p") ||
817 Name.startswith("avx512.mask.loadu.b.") ||
818 Name.startswith("avx512.mask.loadu.w.") ||
819 Name.startswith("avx512.mask.loadu.d.") ||
820 Name.startswith("avx512.mask.loadu.q."))) {
Craig Topperf10fbfa2016-06-02 04:19:36 +0000821 Rep = UpgradeMaskedLoad(Builder, C, CI->getArgOperand(0),
822 CI->getArgOperand(1), CI->getArgOperand(2),
823 /*Aligned*/false);
Craig Topper5aebb862016-07-04 20:56:38 +0000824 } else if (IsX86 && (Name.startswith("avx512.mask.load.p") ||
825 Name.startswith("avx512.mask.load.b.") ||
826 Name.startswith("avx512.mask.load.w.") ||
827 Name.startswith("avx512.mask.load.d.") ||
828 Name.startswith("avx512.mask.load.q."))) {
Craig Topperf10fbfa2016-06-02 04:19:36 +0000829 Rep = UpgradeMaskedLoad(Builder, C, CI->getArgOperand(0),
830 CI->getArgOperand(1),CI->getArgOperand(2),
831 /*Aligned*/true);
Craig Topper5aebb862016-07-04 20:56:38 +0000832 } else if (IsX86 && Name.startswith("xop.vpcom")) {
Craig Topper3352ba52012-06-09 16:46:13 +0000833 Intrinsic::ID intID;
834 if (Name.endswith("ub"))
835 intID = Intrinsic::x86_xop_vpcomub;
836 else if (Name.endswith("uw"))
837 intID = Intrinsic::x86_xop_vpcomuw;
838 else if (Name.endswith("ud"))
839 intID = Intrinsic::x86_xop_vpcomud;
840 else if (Name.endswith("uq"))
841 intID = Intrinsic::x86_xop_vpcomuq;
842 else if (Name.endswith("b"))
843 intID = Intrinsic::x86_xop_vpcomb;
844 else if (Name.endswith("w"))
845 intID = Intrinsic::x86_xop_vpcomw;
846 else if (Name.endswith("d"))
847 intID = Intrinsic::x86_xop_vpcomd;
848 else if (Name.endswith("q"))
849 intID = Intrinsic::x86_xop_vpcomq;
850 else
851 llvm_unreachable("Unknown suffix");
852
Craig Topper5aebb862016-07-04 20:56:38 +0000853 Name = Name.substr(9); // strip off "xop.vpcom"
Craig Topper3352ba52012-06-09 16:46:13 +0000854 unsigned Imm;
855 if (Name.startswith("lt"))
856 Imm = 0;
857 else if (Name.startswith("le"))
858 Imm = 1;
859 else if (Name.startswith("gt"))
860 Imm = 2;
861 else if (Name.startswith("ge"))
862 Imm = 3;
863 else if (Name.startswith("eq"))
864 Imm = 4;
865 else if (Name.startswith("ne"))
866 Imm = 5;
Craig Topper3352ba52012-06-09 16:46:13 +0000867 else if (Name.startswith("false"))
Craig Toppere32546d2015-02-13 07:42:15 +0000868 Imm = 6;
869 else if (Name.startswith("true"))
Craig Topper3352ba52012-06-09 16:46:13 +0000870 Imm = 7;
871 else
872 llvm_unreachable("Unknown condition");
873
874 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
David Blaikieff6409d2015-05-18 22:13:54 +0000875 Rep =
876 Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1),
877 Builder.getInt8(Imm)});
Craig Topper5aebb862016-07-04 20:56:38 +0000878 } else if (IsX86 && Name == "xop.vpcmov") {
Simon Pilgrime88dc042015-11-03 20:27:01 +0000879 Value *Arg0 = CI->getArgOperand(0);
880 Value *Arg1 = CI->getArgOperand(1);
881 Value *Sel = CI->getArgOperand(2);
882 unsigned NumElts = CI->getType()->getVectorNumElements();
883 Constant *MinusOne = ConstantVector::getSplat(NumElts, Builder.getInt64(-1));
884 Value *NotSel = Builder.CreateXor(Sel, MinusOne);
885 Value *Sel0 = Builder.CreateAnd(Arg0, Sel);
886 Value *Sel1 = Builder.CreateAnd(Arg1, NotSel);
887 Rep = Builder.CreateOr(Sel0, Sel1);
Craig Topper5aebb862016-07-04 20:56:38 +0000888 } else if (IsX86 && Name == "sse42.crc32.64.8") {
Craig Topperef9e9932013-10-15 05:20:47 +0000889 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
890 Intrinsic::x86_sse42_crc32_32_8);
891 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
David Blaikieff6409d2015-05-18 22:13:54 +0000892 Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)});
Craig Topperef9e9932013-10-15 05:20:47 +0000893 Rep = Builder.CreateZExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000894 } else if (IsX86 && Name.startswith("avx.vbroadcast")) {
Adam Nemet39066802014-05-29 23:35:33 +0000895 // Replace broadcasts with a series of insertelements.
896 Type *VecTy = CI->getType();
897 Type *EltTy = VecTy->getVectorElementType();
898 unsigned EltNum = VecTy->getVectorNumElements();
899 Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
900 EltTy->getPointerTo());
David Blaikie0c28fd72015-05-20 21:46:30 +0000901 Value *Load = Builder.CreateLoad(EltTy, Cast);
Adam Nemet39066802014-05-29 23:35:33 +0000902 Type *I32Ty = Type::getInt32Ty(C);
903 Rep = UndefValue::get(VecTy);
904 for (unsigned I = 0; I < EltNum; ++I)
905 Rep = Builder.CreateInsertElement(Rep, Load,
906 ConstantInt::get(I32Ty, I));
Craig Topper5aebb862016-07-04 20:56:38 +0000907 } else if (IsX86 && (Name.startswith("sse41.pmovsx") ||
908 Name.startswith("sse41.pmovzx") ||
909 Name.startswith("avx2.pmovsx") ||
910 Name.startswith("avx2.pmovzx"))) {
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000911 VectorType *SrcTy = cast<VectorType>(CI->getArgOperand(0)->getType());
912 VectorType *DstTy = cast<VectorType>(CI->getType());
913 unsigned NumDstElts = DstTy->getNumElements();
914
Simon Pilgrim9602d672016-05-28 18:03:41 +0000915 // Extract a subvector of the first NumDstElts lanes and sign/zero extend.
Craig Topperc0a5fa02016-06-12 04:48:00 +0000916 SmallVector<uint32_t, 8> ShuffleMask(NumDstElts);
Craig Topper99d1eab2016-06-12 00:41:19 +0000917 for (unsigned i = 0; i != NumDstElts; ++i)
Craig Topperc0a5fa02016-06-12 04:48:00 +0000918 ShuffleMask[i] = i;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000919
920 Value *SV = Builder.CreateShuffleVector(
921 CI->getArgOperand(0), UndefValue::get(SrcTy), ShuffleMask);
Simon Pilgrim9602d672016-05-28 18:03:41 +0000922
923 bool DoSext = (StringRef::npos != Name.find("pmovsx"));
924 Rep = DoSext ? Builder.CreateSExt(SV, DstTy)
925 : Builder.CreateZExt(SV, DstTy);
Craig Topper5aebb862016-07-04 20:56:38 +0000926 } else if (IsX86 && Name == "avx2.vbroadcasti128") {
Juergen Ributzka1f7a1762015-03-04 00:13:25 +0000927 // Replace vbroadcasts with a vector shuffle.
David Blaikie0c28fd72015-05-20 21:46:30 +0000928 Type *VT = VectorType::get(Type::getInt64Ty(C), 2);
929 Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0),
930 PointerType::getUnqual(VT));
931 Value *Load = Builder.CreateLoad(VT, Op);
Craig Topper99d1eab2016-06-12 00:41:19 +0000932 uint32_t Idxs[4] = { 0, 1, 0, 1 };
Juergen Ributzka1f7a1762015-03-04 00:13:25 +0000933 Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
Sanjay Patel2db6d382015-03-12 15:27:07 +0000934 Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +0000935 } else if (IsX86 && (Name.startswith("avx2.pbroadcast") ||
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +0000936 Name.startswith("avx2.vbroadcast") ||
937 Name.startswith("avx512.pbroadcast") ||
938 Name.startswith("avx512.mask.broadcast.s"))) {
Ahmed Bougacha1a4987052015-08-20 20:36:19 +0000939 // Replace vp?broadcasts with a vector shuffle.
940 Value *Op = CI->getArgOperand(0);
941 unsigned NumElts = CI->getType()->getVectorNumElements();
942 Type *MaskTy = VectorType::get(Type::getInt32Ty(C), NumElts);
943 Rep = Builder.CreateShuffleVector(Op, UndefValue::get(Op->getType()),
944 Constant::getNullValue(MaskTy));
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +0000945
946 if (CI->getNumArgOperands() == 3)
947 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
948 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +0000949 } else if (IsX86 && Name.startswith("avx512.mask.palignr.")) {
Craig Topper33350cc2016-06-06 06:12:54 +0000950 Rep = UpgradeX86PALIGNRIntrinsics(Builder, C, CI->getArgOperand(0),
951 CI->getArgOperand(1),
952 CI->getArgOperand(2),
953 CI->getArgOperand(3),
954 CI->getArgOperand(4));
Craig Topper5aebb862016-07-04 20:56:38 +0000955 } else if (IsX86 && (Name == "sse2.psll.dq" ||
956 Name == "avx2.psll.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +0000957 // 128/256-bit shift left specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +0000958 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000959 Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +0000960 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +0000961 } else if (IsX86 && (Name == "sse2.psrl.dq" ||
962 Name == "avx2.psrl.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +0000963 // 128/256-bit shift right specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +0000964 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000965 Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +0000966 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +0000967 } else if (IsX86 && (Name == "sse2.psll.dq.bs" ||
968 Name == "avx2.psll.dq.bs" ||
969 Name == "avx512.psll.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +0000970 // 128/256/512-bit shift left specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +0000971 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000972 Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +0000973 } else if (IsX86 && (Name == "sse2.psrl.dq.bs" ||
974 Name == "avx2.psrl.dq.bs" ||
975 Name == "avx512.psrl.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +0000976 // 128/256/512-bit shift right specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +0000977 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper7355ac32016-05-29 06:37:33 +0000978 Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +0000979 } else if (IsX86 && (Name == "sse41.pblendw" ||
980 Name.startswith("sse41.blendp") ||
981 Name.startswith("avx.blend.p") ||
982 Name == "avx2.pblendw" ||
983 Name.startswith("avx2.pblendd."))) {
Craig Topper782d6202015-02-28 19:33:17 +0000984 Value *Op0 = CI->getArgOperand(0);
985 Value *Op1 = CI->getArgOperand(1);
986 unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue();
987 VectorType *VecTy = cast<VectorType>(CI->getType());
988 unsigned NumElts = VecTy->getNumElements();
989
Craig Topperc0a5fa02016-06-12 04:48:00 +0000990 SmallVector<uint32_t, 16> Idxs(NumElts);
991 for (unsigned i = 0; i != NumElts; ++i)
992 Idxs[i] = ((Imm >> (i%8)) & 1) ? i + NumElts : i;
Craig Topper782d6202015-02-28 19:33:17 +0000993
Craig Topper2f561822016-06-12 01:05:59 +0000994 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +0000995 } else if (IsX86 && (Name.startswith("avx.vinsertf128.") ||
996 Name == "avx2.vinserti128")) {
Sanjay Patel19792fb2015-03-10 16:08:36 +0000997 Value *Op0 = CI->getArgOperand(0);
998 Value *Op1 = CI->getArgOperand(1);
999 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
1000 VectorType *VecTy = cast<VectorType>(CI->getType());
1001 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001002
Sanjay Patel19792fb2015-03-10 16:08:36 +00001003 // Mask off the high bits of the immediate value; hardware ignores those.
1004 Imm = Imm & 1;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001005
Sanjay Patel19792fb2015-03-10 16:08:36 +00001006 // Extend the second operand into a vector that is twice as big.
1007 Value *UndefV = UndefValue::get(Op1->getType());
Craig Topperc0a5fa02016-06-12 04:48:00 +00001008 SmallVector<uint32_t, 8> Idxs(NumElts);
1009 for (unsigned i = 0; i != NumElts; ++i)
1010 Idxs[i] = i;
Craig Topper2f561822016-06-12 01:05:59 +00001011 Rep = Builder.CreateShuffleVector(Op1, UndefV, Idxs);
Sanjay Patel19792fb2015-03-10 16:08:36 +00001012
1013 // Insert the second operand into the first operand.
1014
1015 // Note that there is no guarantee that instruction lowering will actually
1016 // produce a vinsertf128 instruction for the created shuffles. In
1017 // particular, the 0 immediate case involves no lane changes, so it can
1018 // be handled as a blend.
1019
1020 // Example of shuffle mask for 32-bit elements:
1021 // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
1022 // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 >
1023
Sanjay Patel19792fb2015-03-10 16:08:36 +00001024 // The low half of the result is either the low half of the 1st operand
1025 // or the low half of the 2nd operand (the inserted vector).
Craig Topperc0a5fa02016-06-12 04:48:00 +00001026 for (unsigned i = 0; i != NumElts / 2; ++i)
1027 Idxs[i] = Imm ? i : (i + NumElts);
Sanjay Patel19792fb2015-03-10 16:08:36 +00001028 // The high half of the result is either the low half of the 2nd operand
1029 // (the inserted vector) or the high half of the 1st operand.
Craig Topperc0a5fa02016-06-12 04:48:00 +00001030 for (unsigned i = NumElts / 2; i != NumElts; ++i)
1031 Idxs[i] = Imm ? (i + NumElts / 2) : i;
Craig Topper2f561822016-06-12 01:05:59 +00001032 Rep = Builder.CreateShuffleVector(Op0, Rep, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001033 } else if (IsX86 && (Name.startswith("avx.vextractf128.") ||
1034 Name == "avx2.vextracti128")) {
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001035 Value *Op0 = CI->getArgOperand(0);
1036 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1037 VectorType *VecTy = cast<VectorType>(CI->getType());
1038 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001039
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001040 // Mask off the high bits of the immediate value; hardware ignores those.
1041 Imm = Imm & 1;
1042
1043 // Get indexes for either the high half or low half of the input vector.
Craig Topper2f561822016-06-12 01:05:59 +00001044 SmallVector<uint32_t, 4> Idxs(NumElts);
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001045 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topper2f561822016-06-12 01:05:59 +00001046 Idxs[i] = Imm ? (i + NumElts) : i;
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001047 }
1048
1049 Value *UndefV = UndefValue::get(Op0->getType());
Craig Topper2f561822016-06-12 01:05:59 +00001050 Rep = Builder.CreateShuffleVector(Op0, UndefV, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001051 } else if (!IsX86 && Name == "stackprotectorcheck") {
Tim Shen00127562016-04-08 21:26:31 +00001052 Rep = nullptr;
Craig Topper5aebb862016-07-04 20:56:38 +00001053 } else if (IsX86 && (Name.startswith("avx512.mask.perm.df.") ||
1054 Name.startswith("avx512.mask.perm.di."))) {
Simon Pilgrim02d435d2016-07-04 14:19:05 +00001055 Value *Op0 = CI->getArgOperand(0);
1056 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1057 VectorType *VecTy = cast<VectorType>(CI->getType());
1058 unsigned NumElts = VecTy->getNumElements();
1059
1060 SmallVector<uint32_t, 8> Idxs(NumElts);
1061 for (unsigned i = 0; i != NumElts; ++i)
1062 Idxs[i] = (i & ~0x3) + ((Imm >> (2 * (i & 0x3))) & 3);
1063
1064 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1065
1066 if (CI->getNumArgOperands() == 4)
1067 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1068 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001069 } else if (IsX86 && (Name.startswith("avx.vpermil.") ||
1070 Name == "sse2.pshuf.d" ||
1071 Name.startswith("avx512.mask.vpermil.p") ||
1072 Name.startswith("avx512.mask.pshuf.d."))) {
Craig Topper8a105052016-06-12 03:10:47 +00001073 Value *Op0 = CI->getArgOperand(0);
1074 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1075 VectorType *VecTy = cast<VectorType>(CI->getType());
1076 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9fca3002016-07-04 12:40:54 +00001077 // Calculate the size of each index in the immediate.
Craig Topper8a105052016-06-12 03:10:47 +00001078 unsigned IdxSize = 64 / VecTy->getScalarSizeInBits();
1079 unsigned IdxMask = ((1 << IdxSize) - 1);
1080
1081 SmallVector<uint32_t, 8> Idxs(NumElts);
1082 // Lookup the bits for this element, wrapping around the immediate every
1083 // 8-bits. Elements are grouped into sets of 2 or 4 elements so we need
1084 // to offset by the first index of each group.
1085 for (unsigned i = 0; i != NumElts; ++i)
1086 Idxs[i] = ((Imm >> ((i * IdxSize) % 8)) & IdxMask) | (i & ~IdxMask);
1087
1088 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001089
1090 if (CI->getNumArgOperands() == 4)
1091 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1092 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001093 } else if (IsX86 && (Name == "sse2.pshufl.w" ||
1094 Name.startswith("avx512.mask.pshufl.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001095 Value *Op0 = CI->getArgOperand(0);
1096 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1097 unsigned NumElts = CI->getType()->getVectorNumElements();
1098
1099 SmallVector<uint32_t, 16> Idxs(NumElts);
1100 for (unsigned l = 0; l != NumElts; l += 8) {
1101 for (unsigned i = 0; i != 4; ++i)
1102 Idxs[i + l] = ((Imm >> (2 * i)) & 0x3) + l;
1103 for (unsigned i = 4; i != 8; ++i)
1104 Idxs[i + l] = i + l;
1105 }
1106
1107 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001108
1109 if (CI->getNumArgOperands() == 4)
1110 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1111 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001112 } else if (IsX86 && (Name == "sse2.pshufh.w" ||
1113 Name.startswith("avx512.mask.pshufh.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001114 Value *Op0 = CI->getArgOperand(0);
1115 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1116 unsigned NumElts = CI->getType()->getVectorNumElements();
1117
1118 SmallVector<uint32_t, 16> Idxs(NumElts);
1119 for (unsigned l = 0; l != NumElts; l += 8) {
1120 for (unsigned i = 0; i != 4; ++i)
1121 Idxs[i + l] = i + l;
1122 for (unsigned i = 0; i != 4; ++i)
1123 Idxs[i + l + 4] = ((Imm >> (2 * i)) & 0x3) + 4 + l;
1124 }
1125
1126 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001127
1128 if (CI->getNumArgOperands() == 4)
1129 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1130 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001131 } else if (IsX86 && (Name.startswith("avx512.mask.movddup") ||
1132 Name.startswith("avx512.mask.movshdup") ||
1133 Name.startswith("avx512.mask.movsldup"))) {
Simon Pilgrim19adee92016-07-02 14:42:35 +00001134 Value *Op0 = CI->getArgOperand(0);
1135 unsigned NumElts = CI->getType()->getVectorNumElements();
1136 unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1137
1138 unsigned Offset = 0;
Craig Topper5aebb862016-07-04 20:56:38 +00001139 if (Name.startswith("avx512.mask.movshdup."))
Simon Pilgrim19adee92016-07-02 14:42:35 +00001140 Offset = 1;
1141
1142 SmallVector<uint32_t, 16> Idxs(NumElts);
1143 for (unsigned l = 0; l != NumElts; l += NumLaneElts)
1144 for (unsigned i = 0; i != NumLaneElts; i += 2) {
1145 Idxs[i + l + 0] = i + l + Offset;
1146 Idxs[i + l + 1] = i + l + Offset;
1147 }
1148
1149 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1150
1151 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
1152 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +00001153 } else if (IsX86 && (Name.startswith("avx512.mask.punpckl") ||
1154 Name.startswith("avx512.mask.unpckl."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001155 Value *Op0 = CI->getArgOperand(0);
1156 Value *Op1 = CI->getArgOperand(1);
1157 int NumElts = CI->getType()->getVectorNumElements();
1158 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1159
1160 SmallVector<uint32_t, 64> Idxs(NumElts);
1161 for (int l = 0; l != NumElts; l += NumLaneElts)
1162 for (int i = 0; i != NumLaneElts; ++i)
1163 Idxs[i + l] = l + (i / 2) + NumElts * (i % 2);
1164
1165 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1166
1167 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1168 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001169 } else if (IsX86 && (Name.startswith("avx512.mask.punpckh") ||
1170 Name.startswith("avx512.mask.unpckh."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001171 Value *Op0 = CI->getArgOperand(0);
1172 Value *Op1 = CI->getArgOperand(1);
1173 int NumElts = CI->getType()->getVectorNumElements();
1174 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1175
1176 SmallVector<uint32_t, 64> Idxs(NumElts);
1177 for (int l = 0; l != NumElts; l += NumLaneElts)
1178 for (int i = 0; i != NumLaneElts; ++i)
1179 Idxs[i + l] = (NumLaneElts / 2) + l + (i / 2) + NumElts * (i % 2);
1180
1181 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1182
1183 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1184 CI->getArgOperand(2));
Craig Topper3b1817d2012-02-03 06:10:55 +00001185 } else {
Craig Topper8a105052016-06-12 03:10:47 +00001186 llvm_unreachable("Unknown function for CallInst upgrade.");
Craig Topper3b1817d2012-02-03 06:10:55 +00001187 }
1188
Tim Shen00127562016-04-08 21:26:31 +00001189 if (Rep)
1190 CI->replaceAllUsesWith(Rep);
Craig Topper3b1817d2012-02-03 06:10:55 +00001191 CI->eraseFromParent();
1192 return;
1193 }
1194
Yaron Kerend1fdbe72015-03-30 16:10:39 +00001195 std::string Name = CI->getName();
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001196 if (!Name.empty())
1197 CI->setName(Name + ".old");
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001198
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001199 switch (NewFn->getIntrinsicID()) {
1200 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +00001201 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001202
Craig Topperf7bf6de2016-07-08 06:14:47 +00001203 case Intrinsic::x86_avx512_mask_psll_di_512:
1204 case Intrinsic::x86_avx512_mask_psra_di_512:
1205 case Intrinsic::x86_avx512_mask_psrl_di_512:
1206 case Intrinsic::x86_avx512_mask_psll_qi_512:
1207 case Intrinsic::x86_avx512_mask_psra_qi_512:
1208 case Intrinsic::x86_avx512_mask_psrl_qi_512:
Jeroen Ketemaab99b592015-09-30 10:56:37 +00001209 case Intrinsic::arm_neon_vld1:
1210 case Intrinsic::arm_neon_vld2:
1211 case Intrinsic::arm_neon_vld3:
1212 case Intrinsic::arm_neon_vld4:
1213 case Intrinsic::arm_neon_vld2lane:
1214 case Intrinsic::arm_neon_vld3lane:
1215 case Intrinsic::arm_neon_vld4lane:
1216 case Intrinsic::arm_neon_vst1:
1217 case Intrinsic::arm_neon_vst2:
1218 case Intrinsic::arm_neon_vst3:
1219 case Intrinsic::arm_neon_vst4:
1220 case Intrinsic::arm_neon_vst2lane:
1221 case Intrinsic::arm_neon_vst3lane:
1222 case Intrinsic::arm_neon_vst4lane: {
1223 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1224 CI->arg_operands().end());
1225 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1226 CI->eraseFromParent();
1227 return;
1228 }
1229
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001230 case Intrinsic::ctlz:
Nuno Lopesad40c0a2012-05-22 15:25:31 +00001231 case Intrinsic::cttz:
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001232 assert(CI->getNumArgOperands() == 1 &&
1233 "Mismatch between function args and call args");
David Blaikieff6409d2015-05-18 22:13:54 +00001234 CI->replaceAllUsesWith(Builder.CreateCall(
1235 NewFn, {CI->getArgOperand(0), Builder.getFalse()}, Name));
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001236 CI->eraseFromParent();
1237 return;
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001238
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001239 case Intrinsic::objectsize:
David Blaikieff6409d2015-05-18 22:13:54 +00001240 CI->replaceAllUsesWith(Builder.CreateCall(
1241 NewFn, {CI->getArgOperand(0), CI->getArgOperand(1)}, Name));
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001242 CI->eraseFromParent();
1243 return;
1244
Joel Jonesb84f7be2012-07-18 00:02:16 +00001245 case Intrinsic::ctpop: {
David Blaikieff6409d2015-05-18 22:13:54 +00001246 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {CI->getArgOperand(0)}));
Joel Jonesb84f7be2012-07-18 00:02:16 +00001247 CI->eraseFromParent();
1248 return;
1249 }
Joel Jones43cb8782012-07-13 23:25:25 +00001250
Craig Topper71dc02d2012-06-13 07:18:53 +00001251 case Intrinsic::x86_xop_vfrcz_ss:
1252 case Intrinsic::x86_xop_vfrcz_sd:
David Blaikieff6409d2015-05-18 22:13:54 +00001253 CI->replaceAllUsesWith(
1254 Builder.CreateCall(NewFn, {CI->getArgOperand(1)}, Name));
Craig Topper71dc02d2012-06-13 07:18:53 +00001255 CI->eraseFromParent();
1256 return;
1257
Simon Pilgrime85506b2016-06-03 08:06:03 +00001258 case Intrinsic::x86_xop_vpermil2pd:
1259 case Intrinsic::x86_xop_vpermil2ps:
1260 case Intrinsic::x86_xop_vpermil2pd_256:
1261 case Intrinsic::x86_xop_vpermil2ps_256: {
1262 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1263 CI->arg_operands().end());
1264 VectorType *FltIdxTy = cast<VectorType>(Args[2]->getType());
1265 VectorType *IntIdxTy = VectorType::getInteger(FltIdxTy);
1266 Args[2] = Builder.CreateBitCast(Args[2], IntIdxTy);
1267 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args, Name));
1268 CI->eraseFromParent();
1269 return;
1270 }
1271
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001272 case Intrinsic::x86_sse41_ptestc:
1273 case Intrinsic::x86_sse41_ptestz:
Craig Topper71dc02d2012-06-13 07:18:53 +00001274 case Intrinsic::x86_sse41_ptestnzc: {
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001275 // The arguments for these intrinsics used to be v4f32, and changed
1276 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
1277 // So, the only thing required is a bitcast for both arguments.
1278 // First, check the arguments have the old type.
1279 Value *Arg0 = CI->getArgOperand(0);
1280 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
1281 return;
1282
1283 // Old intrinsic, add bitcasts
1284 Value *Arg1 = CI->getArgOperand(1);
1285
David Blaikie5bacf372015-04-24 21:16:07 +00001286 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001287
David Blaikie5bacf372015-04-24 21:16:07 +00001288 Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast");
1289 Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
1290
David Blaikieff6409d2015-05-18 22:13:54 +00001291 CallInst *NewCall = Builder.CreateCall(NewFn, {BC0, BC1}, Name);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001292 CI->replaceAllUsesWith(NewCall);
1293 CI->eraseFromParent();
1294 return;
Evan Cheng0e179d02007-12-17 22:33:23 +00001295 }
Chandler Carruth373b2b12014-09-06 10:00:01 +00001296
Chandler Carruth373b2b12014-09-06 10:00:01 +00001297 case Intrinsic::x86_sse41_insertps:
1298 case Intrinsic::x86_sse41_dppd:
1299 case Intrinsic::x86_sse41_dpps:
1300 case Intrinsic::x86_sse41_mpsadbw:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001301 case Intrinsic::x86_avx_dp_ps_256:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001302 case Intrinsic::x86_avx2_mpsadbw: {
1303 // Need to truncate the last argument from i32 to i8 -- this argument models
1304 // an inherently 8-bit immediate operand to these x86 instructions.
1305 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1306 CI->arg_operands().end());
1307
1308 // Replace the last argument with a trunc.
1309 Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
1310
1311 CallInst *NewCall = Builder.CreateCall(NewFn, Args);
1312 CI->replaceAllUsesWith(NewCall);
1313 CI->eraseFromParent();
1314 return;
1315 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +00001316
1317 case Intrinsic::thread_pointer: {
1318 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {}));
1319 CI->eraseFromParent();
1320 return;
1321 }
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +00001322
1323 case Intrinsic::masked_load:
1324 case Intrinsic::masked_store: {
1325 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1326 CI->arg_operands().end());
1327 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1328 CI->eraseFromParent();
1329 return;
1330 }
Craig Topper71dc02d2012-06-13 07:18:53 +00001331 }
Chandler Carruth7132e002007-08-04 01:51:18 +00001332}
1333
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001334void llvm::UpgradeCallsToIntrinsic(Function *F) {
Chandler Carruth7132e002007-08-04 01:51:18 +00001335 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
1336
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001337 // Check if this function should be upgraded and get the replacement function
1338 // if there is one.
Chris Lattner80ed9dc2011-06-18 06:05:24 +00001339 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +00001340 if (UpgradeIntrinsicFunction(F, NewFn)) {
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001341 // Replace all users of the old function with the new function or new
1342 // instructions. This is not a range loop because the call is deleted.
1343 for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; )
Duncan P. N. Exon Smith93f53c42016-04-17 03:59:37 +00001344 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001345 UpgradeIntrinsicCall(CI, NewFn);
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001346
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001347 // Remove old function, no longer used, from the module.
1348 F->eraseFromParent();
Chandler Carruth7132e002007-08-04 01:51:18 +00001349 }
1350}
Devang Patel80ae3492009-08-28 23:24:31 +00001351
Manman Ren209b17c2013-09-28 00:22:27 +00001352void llvm::UpgradeInstWithTBAATag(Instruction *I) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001353 MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
Manman Ren209b17c2013-09-28 00:22:27 +00001354 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
1355 // Check if the tag uses struct-path aware TBAA format.
1356 if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
1357 return;
1358
1359 if (MD->getNumOperands() == 3) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001360 Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
Manman Ren209b17c2013-09-28 00:22:27 +00001361 MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
1362 // Create a MDNode <ScalarType, ScalarType, offset 0, const>
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001363 Metadata *Elts2[] = {ScalarType, ScalarType,
1364 ConstantAsMetadata::get(Constant::getNullValue(
1365 Type::getInt64Ty(I->getContext()))),
1366 MD->getOperand(2)};
Manman Ren209b17c2013-09-28 00:22:27 +00001367 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
1368 } else {
1369 // Create a MDNode <MD, MD, offset 0>
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001370 Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
1371 Type::getInt64Ty(I->getContext())))};
Manman Ren209b17c2013-09-28 00:22:27 +00001372 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
1373 }
1374}
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001375
1376Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
1377 Instruction *&Temp) {
1378 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001379 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001380
Craig Topperc6207612014-04-09 06:08:46 +00001381 Temp = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001382 Type *SrcTy = V->getType();
1383 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1384 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1385 LLVMContext &Context = V->getContext();
1386
1387 // We have no information about target data layout, so we assume that
1388 // the maximum pointer size is 64bit.
1389 Type *MidTy = Type::getInt64Ty(Context);
1390 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
1391
1392 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
1393 }
1394
Craig Topperc6207612014-04-09 06:08:46 +00001395 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001396}
1397
1398Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
1399 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001400 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001401
1402 Type *SrcTy = C->getType();
1403 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1404 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1405 LLVMContext &Context = C->getContext();
1406
1407 // We have no information about target data layout, so we assume that
1408 // the maximum pointer size is 64bit.
1409 Type *MidTy = Type::getInt64Ty(Context);
1410
1411 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
1412 DestTy);
1413 }
1414
Craig Topperc6207612014-04-09 06:08:46 +00001415 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001416}
Manman Ren8b4306c2013-12-02 21:29:56 +00001417
1418/// Check the debug info version number, if it is out-dated, drop the debug
1419/// info. Return true if module is modified.
1420bool llvm::UpgradeDebugInfo(Module &M) {
Manman Ren2ebfb422014-01-16 01:51:12 +00001421 unsigned Version = getDebugMetadataVersionFromModule(M);
1422 if (Version == DEBUG_METADATA_VERSION)
Manman Ren8b4306c2013-12-02 21:29:56 +00001423 return false;
1424
Manman Ren2ebfb422014-01-16 01:51:12 +00001425 bool RetCode = StripDebugInfo(M);
1426 if (RetCode) {
1427 DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
1428 M.getContext().diagnose(DiagVersion);
1429 }
1430 return RetCode;
Manman Ren8b4306c2013-12-02 21:29:56 +00001431}
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001432
Manman Renb5d7ff42016-05-25 23:14:48 +00001433bool llvm::UpgradeModuleFlags(Module &M) {
1434 const NamedMDNode *ModFlags = M.getModuleFlagsMetadata();
1435 if (!ModFlags)
1436 return false;
1437
1438 bool HasObjCFlag = false, HasClassProperties = false;
1439 for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
1440 MDNode *Op = ModFlags->getOperand(I);
1441 if (Op->getNumOperands() < 2)
1442 continue;
1443 MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1444 if (!ID)
1445 continue;
1446 if (ID->getString() == "Objective-C Image Info Version")
1447 HasObjCFlag = true;
1448 if (ID->getString() == "Objective-C Class Properties")
1449 HasClassProperties = true;
1450 }
1451 // "Objective-C Class Properties" is recently added for Objective-C. We
1452 // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module
1453 // flag of value 0, so we can correclty report error when trying to link
1454 // an ObjC bitcode without this module flag with an ObjC bitcode with this
1455 // module flag.
1456 if (HasObjCFlag && !HasClassProperties) {
1457 M.addModuleFlag(llvm::Module::Error, "Objective-C Class Properties",
1458 (uint32_t)0);
1459 return true;
1460 }
1461 return false;
1462}
1463
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00001464static bool isOldLoopArgument(Metadata *MD) {
1465 auto *T = dyn_cast_or_null<MDTuple>(MD);
1466 if (!T)
1467 return false;
1468 if (T->getNumOperands() < 1)
1469 return false;
1470 auto *S = dyn_cast_or_null<MDString>(T->getOperand(0));
1471 if (!S)
1472 return false;
1473 return S->getString().startswith("llvm.vectorizer.");
1474}
1475
1476static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) {
1477 StringRef OldPrefix = "llvm.vectorizer.";
1478 assert(OldTag.startswith(OldPrefix) && "Expected old prefix");
1479
1480 if (OldTag == "llvm.vectorizer.unroll")
1481 return MDString::get(C, "llvm.loop.interleave.count");
1482
1483 return MDString::get(
1484 C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size()))
1485 .str());
1486}
1487
1488static Metadata *upgradeLoopArgument(Metadata *MD) {
1489 auto *T = dyn_cast_or_null<MDTuple>(MD);
1490 if (!T)
1491 return MD;
1492 if (T->getNumOperands() < 1)
1493 return MD;
1494 auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0));
1495 if (!OldTag)
1496 return MD;
1497 if (!OldTag->getString().startswith("llvm.vectorizer."))
1498 return MD;
1499
1500 // This has an old tag. Upgrade it.
1501 SmallVector<Metadata *, 8> Ops;
1502 Ops.reserve(T->getNumOperands());
1503 Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString()));
1504 for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I)
1505 Ops.push_back(T->getOperand(I));
1506
1507 return MDTuple::get(T->getContext(), Ops);
1508}
1509
1510MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) {
1511 auto *T = dyn_cast<MDTuple>(&N);
1512 if (!T)
1513 return &N;
1514
1515 if (!llvm::any_of(T->operands(), isOldLoopArgument))
1516 return &N;
1517
1518 SmallVector<Metadata *, 8> Ops;
1519 Ops.reserve(T->getNumOperands());
1520 for (Metadata *MD : T->operands())
1521 Ops.push_back(upgradeLoopArgument(MD));
1522
1523 return MDTuple::get(T->getContext(), Ops);
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001524}