blob: 16f7c27240209d2e89e3c36670ce35a314e750a3 [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
Rafael Espindolad7325ee2016-10-03 15:51:42 +000034static void rename(GlobalValue *GV) { GV->setName(GV->getName() + ".old"); }
35
Nadav Rotem17ee58a2012-06-10 18:42:51 +000036// Upgrade the declarations of the SSE4.1 functions whose arguments have
37// changed their type from v4f32 to v2i64.
38static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
39 Function *&NewFn) {
40 // Check whether this is an old version of the function, which received
41 // v4f32 arguments.
42 Type *Arg0Type = F->getFunctionType()->getParamType(0);
43 if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
44 return false;
45
46 // Yes, it's old, replace it with new version.
Rafael Espindolad7325ee2016-10-03 15:51:42 +000047 rename(F);
Nadav Rotem17ee58a2012-06-10 18:42:51 +000048 NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
49 return true;
50}
Chandler Carruth7132e002007-08-04 01:51:18 +000051
Chandler Carruth373b2b12014-09-06 10:00:01 +000052// Upgrade the declarations of intrinsic functions whose 8-bit immediate mask
53// arguments have changed their type from i32 to i8.
54static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID,
55 Function *&NewFn) {
56 // Check that the last argument is an i32.
57 Type *LastArgType = F->getFunctionType()->getParamType(
58 F->getFunctionType()->getNumParams() - 1);
59 if (!LastArgType->isIntegerTy(32))
60 return false;
61
62 // Move this function aside and map down.
Rafael Espindolad7325ee2016-10-03 15:51:42 +000063 rename(F);
Chandler Carruth373b2b12014-09-06 10:00:01 +000064 NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
65 return true;
66}
67
Evan Cheng0e179d02007-12-17 22:33:23 +000068static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +000069 assert(F && "Illegal to upgrade a non-existent Function.");
70
Chandler Carruth7132e002007-08-04 01:51:18 +000071 // Quickly eliminate it, if it's not a candidate.
Eric Liuc9c68172016-07-08 16:09:51 +000072 StringRef Name = F->getName();
Chris Lattnerb372f662011-06-18 18:56:39 +000073 if (Name.size() <= 8 || !Name.startswith("llvm."))
Evan Cheng0e179d02007-12-17 22:33:23 +000074 return false;
Chris Lattnerb372f662011-06-18 18:56:39 +000075 Name = Name.substr(5); // Strip off "llvm."
Chris Lattner0bcbde42011-11-27 08:42:07 +000076
Chris Lattnerb372f662011-06-18 18:56:39 +000077 switch (Name[0]) {
Chandler Carruth7132e002007-08-04 01:51:18 +000078 default: break;
Joel Jones43cb8782012-07-13 23:25:25 +000079 case 'a': {
80 if (Name.startswith("arm.neon.vclz")) {
81 Type* args[2] = {
Matt Arsenaultc4c92262013-07-20 17:46:00 +000082 F->arg_begin()->getType(),
Joel Jones43cb8782012-07-13 23:25:25 +000083 Type::getInt1Ty(F->getContext())
84 };
85 // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
86 // the end of the name. Change name from llvm.arm.neon.vclz.* to
87 // llvm.ctlz.*
88 FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
Matt Arsenaultc4c92262013-07-20 17:46:00 +000089 NewFn = Function::Create(fType, F->getLinkage(),
Joel Jones43cb8782012-07-13 23:25:25 +000090 "llvm.ctlz." + Name.substr(14), F->getParent());
91 return true;
92 }
Joel Jonesb84f7be2012-07-18 00:02:16 +000093 if (Name.startswith("arm.neon.vcnt")) {
94 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
95 F->arg_begin()->getType());
96 return true;
97 }
Jeroen Ketemaab99b592015-09-30 10:56:37 +000098 Regex vldRegex("^arm\\.neon\\.vld([1234]|[234]lane)\\.v[a-z0-9]*$");
99 if (vldRegex.match(Name)) {
100 auto fArgs = F->getFunctionType()->params();
101 SmallVector<Type *, 4> Tys(fArgs.begin(), fArgs.end());
102 // Can't use Intrinsic::getDeclaration here as the return types might
103 // then only be structurally equal.
104 FunctionType* fType = FunctionType::get(F->getReturnType(), Tys, false);
105 NewFn = Function::Create(fType, F->getLinkage(),
106 "llvm." + Name + ".p0i8", F->getParent());
107 return true;
108 }
109 Regex vstRegex("^arm\\.neon\\.vst([1234]|[234]lane)\\.v[a-z0-9]*$");
110 if (vstRegex.match(Name)) {
Craig Topper26260942015-10-18 05:15:34 +0000111 static const Intrinsic::ID StoreInts[] = {Intrinsic::arm_neon_vst1,
112 Intrinsic::arm_neon_vst2,
113 Intrinsic::arm_neon_vst3,
114 Intrinsic::arm_neon_vst4};
Jeroen Ketemaab99b592015-09-30 10:56:37 +0000115
Craig Topper26260942015-10-18 05:15:34 +0000116 static const Intrinsic::ID StoreLaneInts[] = {
117 Intrinsic::arm_neon_vst2lane, Intrinsic::arm_neon_vst3lane,
118 Intrinsic::arm_neon_vst4lane
119 };
Jeroen Ketemaab99b592015-09-30 10:56:37 +0000120
121 auto fArgs = F->getFunctionType()->params();
122 Type *Tys[] = {fArgs[0], fArgs[1]};
123 if (Name.find("lane") == StringRef::npos)
124 NewFn = Intrinsic::getDeclaration(F->getParent(),
125 StoreInts[fArgs.size() - 3], Tys);
126 else
127 NewFn = Intrinsic::getDeclaration(F->getParent(),
128 StoreLaneInts[fArgs.size() - 5], Tys);
129 return true;
130 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +0000131 if (Name == "aarch64.thread.pointer" || Name == "arm.thread.pointer") {
132 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::thread_pointer);
133 return true;
134 }
Joel Jones43cb8782012-07-13 23:25:25 +0000135 break;
136 }
Jeroen Ketemaab99b592015-09-30 10:56:37 +0000137
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000138 case 'c': {
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000139 if (Name.startswith("ctlz.") && F->arg_size() == 1) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000140 rename(F);
Chandler Carruthd4a02402011-12-12 10:57:20 +0000141 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
142 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000143 return true;
144 }
145 if (Name.startswith("cttz.") && F->arg_size() == 1) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000146 rename(F);
Chandler Carruthd4a02402011-12-12 10:57:20 +0000147 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
148 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000149 return true;
150 }
151 break;
152 }
Mehdi Amini8c629ec2016-08-13 23:31:24 +0000153 case 'i': {
154 if (Name.startswith("invariant.start")) {
155 auto Args = F->getFunctionType()->params();
156 Type* ObjectPtr[1] = {Args[1]};
157 if (F->getName() !=
158 Intrinsic::getName(Intrinsic::invariant_start, ObjectPtr)) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000159 rename(F);
Mehdi Amini8c629ec2016-08-13 23:31:24 +0000160 NewFn = Intrinsic::getDeclaration(
161 F->getParent(), Intrinsic::invariant_start, ObjectPtr);
162 return true;
163 }
164 }
165 if (Name.startswith("invariant.end")) {
166 auto Args = F->getFunctionType()->params();
167 Type* ObjectPtr[1] = {Args[2]};
168 if (F->getName() !=
169 Intrinsic::getName(Intrinsic::invariant_end, ObjectPtr)) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000170 rename(F);
Mehdi Amini8c629ec2016-08-13 23:31:24 +0000171 NewFn = Intrinsic::getDeclaration(F->getParent(),
172 Intrinsic::invariant_end, ObjectPtr);
173 return true;
174 }
175 }
176 break;
177 }
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000178 case 'm': {
179 if (Name.startswith("masked.load.")) {
180 Type *Tys[] = { F->getReturnType(), F->arg_begin()->getType() };
181 if (F->getName() != Intrinsic::getName(Intrinsic::masked_load, Tys)) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000182 rename(F);
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000183 NewFn = Intrinsic::getDeclaration(F->getParent(),
184 Intrinsic::masked_load,
185 Tys);
186 return true;
187 }
188 }
189 if (Name.startswith("masked.store.")) {
190 auto Args = F->getFunctionType()->params();
191 Type *Tys[] = { Args[0], Args[1] };
192 if (F->getName() != Intrinsic::getName(Intrinsic::masked_store, Tys)) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000193 rename(F);
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +0000194 NewFn = Intrinsic::getDeclaration(F->getParent(),
195 Intrinsic::masked_store,
196 Tys);
197 return true;
198 }
199 }
200 break;
201 }
202
Matt Arsenaultfbcbce42013-10-07 18:06:48 +0000203 case 'o':
204 // We only need to change the name to match the mangling including the
205 // address space.
206 if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
207 Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
208 if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000209 rename(F);
Matt Arsenaultfbcbce42013-10-07 18:06:48 +0000210 NewFn = Intrinsic::getDeclaration(F->getParent(),
211 Intrinsic::objectsize, Tys);
212 return true;
213 }
214 }
215 break;
216
Tim Shen00127562016-04-08 21:26:31 +0000217 case 's':
218 if (Name == "stackprotectorcheck") {
219 NewFn = nullptr;
220 return true;
221 }
George Burgess IV63b06e12016-11-08 04:01:50 +0000222 break;
Tim Shen00127562016-04-08 21:26:31 +0000223
Craig Topper3b1817d2012-02-03 06:10:55 +0000224 case 'x': {
Craig Topper5aebb862016-07-04 20:56:38 +0000225 bool IsX86 = Name.startswith("x86.");
226 if (IsX86)
227 Name = Name.substr(4);
228
229 if (IsX86 &&
230 (Name.startswith("sse2.pcmpeq.") ||
231 Name.startswith("sse2.pcmpgt.") ||
232 Name.startswith("avx2.pcmpeq.") ||
233 Name.startswith("avx2.pcmpgt.") ||
234 Name.startswith("avx512.mask.pcmpeq.") ||
235 Name.startswith("avx512.mask.pcmpgt.") ||
236 Name == "sse41.pmaxsb" ||
237 Name == "sse2.pmaxs.w" ||
238 Name == "sse41.pmaxsd" ||
239 Name == "sse2.pmaxu.b" ||
240 Name == "sse41.pmaxuw" ||
241 Name == "sse41.pmaxud" ||
242 Name == "sse41.pminsb" ||
243 Name == "sse2.pmins.w" ||
244 Name == "sse41.pminsd" ||
245 Name == "sse2.pminu.b" ||
246 Name == "sse41.pminuw" ||
247 Name == "sse41.pminud" ||
Craig Topper7e545332016-11-07 00:13:39 +0000248 Name == "avx512.mask.pshuf.b.128" ||
249 Name == "avx512.mask.pshuf.b.256" ||
Craig Topper5aebb862016-07-04 20:56:38 +0000250 Name.startswith("avx2.pmax") ||
251 Name.startswith("avx2.pmin") ||
Craig Topper8ec5c732016-10-24 04:04:16 +0000252 Name.startswith("avx512.mask.pmax") ||
253 Name.startswith("avx512.mask.pmin") ||
Craig Topper5aebb862016-07-04 20:56:38 +0000254 Name.startswith("avx2.vbroadcast") ||
255 Name.startswith("avx2.pbroadcast") ||
256 Name.startswith("avx.vpermil.") ||
257 Name.startswith("sse2.pshuf") ||
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +0000258 Name.startswith("avx512.pbroadcast") ||
259 Name.startswith("avx512.mask.broadcast.s") ||
Craig Topper5aebb862016-07-04 20:56:38 +0000260 Name.startswith("avx512.mask.movddup") ||
261 Name.startswith("avx512.mask.movshdup") ||
262 Name.startswith("avx512.mask.movsldup") ||
263 Name.startswith("avx512.mask.pshuf.d.") ||
264 Name.startswith("avx512.mask.pshufl.w.") ||
265 Name.startswith("avx512.mask.pshufh.w.") ||
Craig Topper4619c9e2016-09-13 07:40:53 +0000266 Name.startswith("avx512.mask.shuf.p") ||
Craig Topper5aebb862016-07-04 20:56:38 +0000267 Name.startswith("avx512.mask.vpermil.p") ||
268 Name.startswith("avx512.mask.perm.df.") ||
269 Name.startswith("avx512.mask.perm.di.") ||
270 Name.startswith("avx512.mask.punpckl") ||
271 Name.startswith("avx512.mask.punpckh") ||
272 Name.startswith("avx512.mask.unpckl.") ||
273 Name.startswith("avx512.mask.unpckh.") ||
Craig Toppera6e6feb2016-07-12 05:27:53 +0000274 Name.startswith("avx512.mask.pand.") ||
275 Name.startswith("avx512.mask.pandn.") ||
276 Name.startswith("avx512.mask.por.") ||
277 Name.startswith("avx512.mask.pxor.") ||
Craig Toppere75c4952016-09-02 05:29:17 +0000278 Name.startswith("avx512.mask.and.") ||
279 Name.startswith("avx512.mask.andn.") ||
280 Name.startswith("avx512.mask.or.") ||
281 Name.startswith("avx512.mask.xor.") ||
Craig Topperaf0d63d2016-09-04 02:09:53 +0000282 Name.startswith("avx512.mask.padd.") ||
283 Name.startswith("avx512.mask.psub.") ||
284 Name.startswith("avx512.mask.pmull.") ||
Craig Topper8f354a72016-11-05 05:35:23 +0000285 Name == "avx512.mask.add.pd.128" ||
286 Name == "avx512.mask.add.pd.256" ||
287 Name == "avx512.mask.add.ps.128" ||
288 Name == "avx512.mask.add.ps.256" ||
289 Name == "avx512.mask.div.pd.128" ||
290 Name == "avx512.mask.div.pd.256" ||
291 Name == "avx512.mask.div.ps.128" ||
292 Name == "avx512.mask.div.ps.256" ||
293 Name == "avx512.mask.mul.pd.128" ||
294 Name == "avx512.mask.mul.pd.256" ||
295 Name == "avx512.mask.mul.ps.128" ||
296 Name == "avx512.mask.mul.ps.256" ||
297 Name == "avx512.mask.sub.pd.128" ||
298 Name == "avx512.mask.sub.pd.256" ||
299 Name == "avx512.mask.sub.ps.128" ||
300 Name == "avx512.mask.sub.ps.256" ||
Craig Topperda6a63d2016-11-12 18:04:46 +0000301 Name.startswith("avx512.mask.psll.d") ||
302 Name.startswith("avx512.mask.psll.q") ||
303 Name.startswith("avx512.mask.psll.w") ||
304 Name.startswith("avx512.mask.psra.d") ||
305 Name.startswith("avx512.mask.psra.q") ||
306 Name.startswith("avx512.mask.psra.w") ||
307 Name.startswith("avx512.mask.psrl.d") ||
308 Name.startswith("avx512.mask.psrl.q") ||
309 Name.startswith("avx512.mask.psrl.w") ||
310 Name.startswith("avx512.mask.pslli") ||
311 Name.startswith("avx512.mask.psrai") ||
312 Name.startswith("avx512.mask.psrli") ||
Craig Topper46de4132016-11-06 16:29:19 +0000313 Name == "avx512.mask.psllv2.di" ||
314 Name == "avx512.mask.psllv4.di" ||
315 Name == "avx512.mask.psllv4.si" ||
316 Name == "avx512.mask.psllv8.si" ||
317 Name == "avx512.mask.psrav4.si" ||
318 Name == "avx512.mask.psrav8.si" ||
319 Name == "avx512.mask.psrlv2.di" ||
320 Name == "avx512.mask.psrlv4.di" ||
321 Name == "avx512.mask.psrlv4.si" ||
322 Name == "avx512.mask.psrlv8.si" ||
Craig Topper5aebb862016-07-04 20:56:38 +0000323 Name.startswith("sse41.pmovsx") ||
324 Name.startswith("sse41.pmovzx") ||
325 Name.startswith("avx2.pmovsx") ||
326 Name.startswith("avx2.pmovzx") ||
Craig Topperb110e042016-11-07 02:12:57 +0000327 Name.startswith("avx512.mask.pmovsx") ||
328 Name.startswith("avx512.mask.pmovzx") ||
Craig Topper5aebb862016-07-04 20:56:38 +0000329 Name == "sse2.cvtdq2pd" ||
330 Name == "sse2.cvtps2pd" ||
331 Name == "avx.cvtdq2.pd.256" ||
332 Name == "avx.cvt.ps2.pd.256" ||
Craig Topper5aebb862016-07-04 20:56:38 +0000333 Name.startswith("avx.vinsertf128.") ||
334 Name == "avx2.vinserti128" ||
335 Name.startswith("avx.vextractf128.") ||
336 Name == "avx2.vextracti128" ||
337 Name.startswith("sse4a.movnt.") ||
338 Name.startswith("avx.movnt.") ||
Craig Topper70610cf2016-07-09 04:38:27 +0000339 Name.startswith("avx512.storent.") ||
Craig Topper5aebb862016-07-04 20:56:38 +0000340 Name == "sse2.storel.dq" ||
341 Name.startswith("sse.storeu.") ||
342 Name.startswith("sse2.storeu.") ||
343 Name.startswith("avx.storeu.") ||
Craig Toppera57d2ca2016-09-03 23:55:13 +0000344 Name.startswith("avx512.mask.storeu.") ||
Craig Topper5aebb862016-07-04 20:56:38 +0000345 Name.startswith("avx512.mask.store.p") ||
346 Name.startswith("avx512.mask.store.b.") ||
347 Name.startswith("avx512.mask.store.w.") ||
348 Name.startswith("avx512.mask.store.d.") ||
349 Name.startswith("avx512.mask.store.q.") ||
Craig Toppera57d2ca2016-09-03 23:55:13 +0000350 Name.startswith("avx512.mask.loadu.") ||
351 Name.startswith("avx512.mask.load.") ||
Craig Topper5aebb862016-07-04 20:56:38 +0000352 Name == "sse42.crc32.64.8" ||
353 Name.startswith("avx.vbroadcast.s") ||
354 Name.startswith("avx512.mask.palignr.") ||
355 Name.startswith("sse2.psll.dq") ||
356 Name.startswith("sse2.psrl.dq") ||
357 Name.startswith("avx2.psll.dq") ||
358 Name.startswith("avx2.psrl.dq") ||
359 Name.startswith("avx512.psll.dq") ||
360 Name.startswith("avx512.psrl.dq") ||
361 Name == "sse41.pblendw" ||
362 Name.startswith("sse41.blendp") ||
363 Name.startswith("avx.blend.p") ||
364 Name == "avx2.pblendw" ||
365 Name.startswith("avx2.pblendd.") ||
Simon Pilgrimea0d4f92016-07-22 13:58:44 +0000366 Name.startswith("avx.vbroadcastf128") ||
Craig Topper5aebb862016-07-04 20:56:38 +0000367 Name == "avx2.vbroadcasti128" ||
368 Name == "xop.vpcmov" ||
369 (Name.startswith("xop.vpcom") && F->arg_size() == 2))) {
Craig Topperc6207612014-04-09 06:08:46 +0000370 NewFn = nullptr;
Craig Topper3b1817d2012-02-03 06:10:55 +0000371 return true;
372 }
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000373 // SSE4.1 ptest functions may have an old signature.
Craig Topper5aebb862016-07-04 20:56:38 +0000374 if (IsX86 && Name.startswith("sse41.ptest")) {
375 if (Name.substr(11) == "c")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000376 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000377 if (Name.substr(11) == "z")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000378 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000379 if (Name.substr(11) == "nzc")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000380 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
381 }
Sanjay Patel1c3eaec2015-02-28 22:25:06 +0000382 // Several blend and other instructions with masks used the wrong number of
Chandler Carruth373b2b12014-09-06 10:00:01 +0000383 // bits.
Craig Topper5aebb862016-07-04 20:56:38 +0000384 if (IsX86 && Name == "sse41.insertps")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000385 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
386 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000387 if (IsX86 && Name == "sse41.dppd")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000388 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
389 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000390 if (IsX86 && Name == "sse41.dpps")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000391 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
392 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000393 if (IsX86 && Name == "sse41.mpsadbw")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000394 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
395 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000396 if (IsX86 && Name == "avx.dp.ps.256")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000397 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
398 NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000399 if (IsX86 && Name == "avx2.mpsadbw")
Chandler Carruth373b2b12014-09-06 10:00:01 +0000400 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
401 NewFn);
Craig Topper29f2e952015-01-25 23:26:02 +0000402
Craig Topper71dc02d2012-06-13 07:18:53 +0000403 // frcz.ss/sd may need to have an argument dropped
Craig Topper5aebb862016-07-04 20:56:38 +0000404 if (IsX86 && Name.startswith("xop.vfrcz.ss") && F->arg_size() == 2) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000405 rename(F);
Craig Topper71dc02d2012-06-13 07:18:53 +0000406 NewFn = Intrinsic::getDeclaration(F->getParent(),
407 Intrinsic::x86_xop_vfrcz_ss);
408 return true;
409 }
Craig Topper5aebb862016-07-04 20:56:38 +0000410 if (IsX86 && Name.startswith("xop.vfrcz.sd") && F->arg_size() == 2) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000411 rename(F);
Craig Topper71dc02d2012-06-13 07:18:53 +0000412 NewFn = Intrinsic::getDeclaration(F->getParent(),
413 Intrinsic::x86_xop_vfrcz_sd);
414 return true;
415 }
Simon Pilgrime85506b2016-06-03 08:06:03 +0000416 // Upgrade any XOP PERMIL2 index operand still using a float/double vector.
Craig Topper5aebb862016-07-04 20:56:38 +0000417 if (IsX86 && Name.startswith("xop.vpermil2")) {
Simon Pilgrime85506b2016-06-03 08:06:03 +0000418 auto Params = F->getFunctionType()->params();
419 auto Idx = Params[2];
420 if (Idx->getScalarType()->isFloatingPointTy()) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000421 rename(F);
Simon Pilgrime85506b2016-06-03 08:06:03 +0000422 unsigned IdxSize = Idx->getPrimitiveSizeInBits();
423 unsigned EltSize = Idx->getScalarSizeInBits();
424 Intrinsic::ID Permil2ID;
425 if (EltSize == 64 && IdxSize == 128)
426 Permil2ID = Intrinsic::x86_xop_vpermil2pd;
427 else if (EltSize == 32 && IdxSize == 128)
428 Permil2ID = Intrinsic::x86_xop_vpermil2ps;
429 else if (EltSize == 64 && IdxSize == 256)
430 Permil2ID = Intrinsic::x86_xop_vpermil2pd_256;
431 else
432 Permil2ID = Intrinsic::x86_xop_vpermil2ps_256;
433 NewFn = Intrinsic::getDeclaration(F->getParent(), Permil2ID);
434 return true;
435 }
436 }
Craig Topper3b1817d2012-02-03 06:10:55 +0000437 break;
438 }
Chris Lattnerb372f662011-06-18 18:56:39 +0000439 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000440
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000441 // This may not belong here. This function is effectively being overloaded
442 // to both detect an intrinsic which needs upgrading, and to provide the
443 // upgraded form of the intrinsic. We should perhaps have two separate
Chandler Carruth7132e002007-08-04 01:51:18 +0000444 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000445 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000446}
447
Evan Cheng0e179d02007-12-17 22:33:23 +0000448bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
Craig Topperc6207612014-04-09 06:08:46 +0000449 NewFn = nullptr;
Evan Cheng0e179d02007-12-17 22:33:23 +0000450 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Filipe Cabecinhas0011c582015-07-03 20:12:01 +0000451 assert(F != NewFn && "Intrinsic function upgraded to the same function");
Duncan Sands38ef3a82007-12-03 20:06:50 +0000452
453 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000454 if (NewFn)
455 F = NewFn;
Pete Cooper9e1d3352015-05-20 17:16:39 +0000456 if (Intrinsic::ID id = F->getIntrinsicID())
457 F->setAttributes(Intrinsic::getAttributes(F->getContext(), id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000458 return Upgraded;
459}
460
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000461bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000462 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000463 return false;
464}
465
Simon Pilgrimf7186822016-06-09 21:09:03 +0000466// Handles upgrading SSE2/AVX2/AVX512BW PSLLDQ intrinsics by converting them
Craig Topperb324e432015-02-18 06:24:44 +0000467// to byte shuffles.
Craig Topper46b34fe2016-07-12 01:42:33 +0000468static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder,
Craig Topper7355ac32016-05-29 06:37:33 +0000469 Value *Op, unsigned Shift) {
470 Type *ResultTy = Op->getType();
471 unsigned NumElts = ResultTy->getVectorNumElements() * 8;
Craig Topperb324e432015-02-18 06:24:44 +0000472
473 // Bitcast from a 64-bit element type to a byte element type.
Craig Topper46b34fe2016-07-12 01:42:33 +0000474 Type *VecTy = VectorType::get(Builder.getInt8Ty(), NumElts);
Craig Topper7355ac32016-05-29 06:37:33 +0000475 Op = Builder.CreateBitCast(Op, VecTy, "cast");
476
Craig Topperb324e432015-02-18 06:24:44 +0000477 // We'll be shuffling in zeroes.
Craig Topper7355ac32016-05-29 06:37:33 +0000478 Value *Res = Constant::getNullValue(VecTy);
Craig Topperb324e432015-02-18 06:24:44 +0000479
480 // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
481 // we'll just return the zero vector.
482 if (Shift < 16) {
Craig Topper99d1eab2016-06-12 00:41:19 +0000483 uint32_t Idxs[64];
Simon Pilgrimf7186822016-06-09 21:09:03 +0000484 // 256/512-bit version is split into 2/4 16-byte lanes.
Craig Topperb324e432015-02-18 06:24:44 +0000485 for (unsigned l = 0; l != NumElts; l += 16)
486 for (unsigned i = 0; i != 16; ++i) {
487 unsigned Idx = NumElts + i - Shift;
488 if (Idx < NumElts)
489 Idx -= NumElts - 16; // end of lane, switch operand.
Craig Topper7355ac32016-05-29 06:37:33 +0000490 Idxs[l + i] = Idx + l;
Craig Topperb324e432015-02-18 06:24:44 +0000491 }
492
Craig Topper7355ac32016-05-29 06:37:33 +0000493 Res = Builder.CreateShuffleVector(Res, Op, makeArrayRef(Idxs, NumElts));
Craig Topperb324e432015-02-18 06:24:44 +0000494 }
495
496 // Bitcast back to a 64-bit element type.
Craig Topper7355ac32016-05-29 06:37:33 +0000497 return Builder.CreateBitCast(Res, ResultTy, "cast");
Craig Topperb324e432015-02-18 06:24:44 +0000498}
499
Craig Topperea703ae2016-06-13 02:36:42 +0000500// Handles upgrading SSE2/AVX2/AVX512BW PSRLDQ intrinsics by converting them
501// to byte shuffles.
Craig Topper46b34fe2016-07-12 01:42:33 +0000502static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, Value *Op,
Craig Topperea703ae2016-06-13 02:36:42 +0000503 unsigned Shift) {
504 Type *ResultTy = Op->getType();
505 unsigned NumElts = ResultTy->getVectorNumElements() * 8;
506
507 // Bitcast from a 64-bit element type to a byte element type.
Craig Topper46b34fe2016-07-12 01:42:33 +0000508 Type *VecTy = VectorType::get(Builder.getInt8Ty(), NumElts);
Craig Topperea703ae2016-06-13 02:36:42 +0000509 Op = Builder.CreateBitCast(Op, VecTy, "cast");
510
511 // We'll be shuffling in zeroes.
512 Value *Res = Constant::getNullValue(VecTy);
513
514 // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
515 // we'll just return the zero vector.
516 if (Shift < 16) {
517 uint32_t Idxs[64];
518 // 256/512-bit version is split into 2/4 16-byte lanes.
519 for (unsigned l = 0; l != NumElts; l += 16)
520 for (unsigned i = 0; i != 16; ++i) {
521 unsigned Idx = i + Shift;
522 if (Idx >= 16)
523 Idx += NumElts - 16; // end of lane, switch operand.
524 Idxs[l + i] = Idx + l;
525 }
526
527 Res = Builder.CreateShuffleVector(Op, Res, makeArrayRef(Idxs, NumElts));
528 }
529
530 // Bitcast back to a 64-bit element type.
531 return Builder.CreateBitCast(Res, ResultTy, "cast");
532}
533
534static Value *getX86MaskVec(IRBuilder<> &Builder, Value *Mask,
535 unsigned NumElts) {
536 llvm::VectorType *MaskTy = llvm::VectorType::get(Builder.getInt1Ty(),
537 cast<IntegerType>(Mask->getType())->getBitWidth());
538 Mask = Builder.CreateBitCast(Mask, MaskTy);
539
540 // If we have less than 8 elements, then the starting mask was an i8 and
541 // we need to extract down to the right number of elements.
542 if (NumElts < 8) {
543 uint32_t Indices[4];
544 for (unsigned i = 0; i != NumElts; ++i)
545 Indices[i] = i;
546 Mask = Builder.CreateShuffleVector(Mask, Mask,
547 makeArrayRef(Indices, NumElts),
548 "extract");
549 }
550
551 return Mask;
552}
553
554static Value *EmitX86Select(IRBuilder<> &Builder, Value *Mask,
555 Value *Op0, Value *Op1) {
556 // If the mask is all ones just emit the align operation.
557 if (const auto *C = dyn_cast<Constant>(Mask))
558 if (C->isAllOnesValue())
559 return Op0;
560
561 Mask = getX86MaskVec(Builder, Mask, Op0->getType()->getVectorNumElements());
562 return Builder.CreateSelect(Mask, Op0, Op1);
563}
564
Craig Topper46b34fe2016-07-12 01:42:33 +0000565static Value *UpgradeX86PALIGNRIntrinsics(IRBuilder<> &Builder,
Craig Topper33350cc2016-06-06 06:12:54 +0000566 Value *Op0, Value *Op1, Value *Shift,
567 Value *Passthru, Value *Mask) {
568 unsigned ShiftVal = cast<llvm::ConstantInt>(Shift)->getZExtValue();
569
570 unsigned NumElts = Op0->getType()->getVectorNumElements();
571 assert(NumElts % 16 == 0);
572
573 // If palignr is shifting the pair of vectors more than the size of two
574 // lanes, emit zero.
575 if (ShiftVal >= 32)
576 return llvm::Constant::getNullValue(Op0->getType());
577
578 // If palignr is shifting the pair of input vectors more than one lane,
579 // but less than two lanes, convert to shifting in zeroes.
580 if (ShiftVal > 16) {
581 ShiftVal -= 16;
582 Op1 = Op0;
583 Op0 = llvm::Constant::getNullValue(Op0->getType());
584 }
585
Craig Topper99d1eab2016-06-12 00:41:19 +0000586 uint32_t Indices[64];
Craig Topper33350cc2016-06-06 06:12:54 +0000587 // 256-bit palignr operates on 128-bit lanes so we need to handle that
588 for (unsigned l = 0; l != NumElts; l += 16) {
589 for (unsigned i = 0; i != 16; ++i) {
590 unsigned Idx = ShiftVal + i;
591 if (Idx >= 16)
592 Idx += NumElts - 16; // End of lane, switch operand.
593 Indices[l + i] = Idx + l;
594 }
595 }
596
597 Value *Align = Builder.CreateShuffleVector(Op1, Op0,
598 makeArrayRef(Indices, NumElts),
599 "palignr");
600
Craig Topperea703ae2016-06-13 02:36:42 +0000601 return EmitX86Select(Builder, Mask, Align, Passthru);
Craig Topperb324e432015-02-18 06:24:44 +0000602}
603
Craig Topper46b34fe2016-07-12 01:42:33 +0000604static Value *UpgradeMaskedStore(IRBuilder<> &Builder,
Craig Topper50f85c22016-05-31 01:50:02 +0000605 Value *Ptr, Value *Data, Value *Mask,
606 bool Aligned) {
607 // Cast the pointer to the right type.
608 Ptr = Builder.CreateBitCast(Ptr,
609 llvm::PointerType::getUnqual(Data->getType()));
610 unsigned Align =
611 Aligned ? cast<VectorType>(Data->getType())->getBitWidth() / 8 : 1;
612
613 // If the mask is all ones just emit a regular store.
614 if (const auto *C = dyn_cast<Constant>(Mask))
615 if (C->isAllOnesValue())
616 return Builder.CreateAlignedStore(Data, Ptr, Align);
617
618 // Convert the mask from an integer type to a vector of i1.
619 unsigned NumElts = Data->getType()->getVectorNumElements();
Craig Topperea703ae2016-06-13 02:36:42 +0000620 Mask = getX86MaskVec(Builder, Mask, NumElts);
Craig Topper50f85c22016-05-31 01:50:02 +0000621 return Builder.CreateMaskedStore(Data, Ptr, Align, Mask);
622}
623
Craig Topper46b34fe2016-07-12 01:42:33 +0000624static Value *UpgradeMaskedLoad(IRBuilder<> &Builder,
Craig Topperf10fbfa2016-06-02 04:19:36 +0000625 Value *Ptr, Value *Passthru, Value *Mask,
626 bool Aligned) {
627 // Cast the pointer to the right type.
628 Ptr = Builder.CreateBitCast(Ptr,
629 llvm::PointerType::getUnqual(Passthru->getType()));
630 unsigned Align =
631 Aligned ? cast<VectorType>(Passthru->getType())->getBitWidth() / 8 : 1;
632
633 // If the mask is all ones just emit a regular store.
634 if (const auto *C = dyn_cast<Constant>(Mask))
635 if (C->isAllOnesValue())
636 return Builder.CreateAlignedLoad(Ptr, Align);
637
638 // Convert the mask from an integer type to a vector of i1.
639 unsigned NumElts = Passthru->getType()->getVectorNumElements();
Craig Topperea703ae2016-06-13 02:36:42 +0000640 Mask = getX86MaskVec(Builder, Mask, NumElts);
Craig Topperf10fbfa2016-06-02 04:19:36 +0000641 return Builder.CreateMaskedLoad(Ptr, Align, Mask, Passthru);
642}
643
Sanjay Patel51ab7572016-06-16 15:48:30 +0000644static Value *upgradeIntMinMax(IRBuilder<> &Builder, CallInst &CI,
645 ICmpInst::Predicate Pred) {
646 Value *Op0 = CI.getArgOperand(0);
647 Value *Op1 = CI.getArgOperand(1);
648 Value *Cmp = Builder.CreateICmp(Pred, Op0, Op1);
Craig Topper8ec5c732016-10-24 04:04:16 +0000649 Value *Res = Builder.CreateSelect(Cmp, Op0, Op1);
650
651 if (CI.getNumArgOperands() == 4)
652 Res = EmitX86Select(Builder, CI.getArgOperand(3), Res, CI.getArgOperand(2));
653
654 return Res;
Sanjay Patel51ab7572016-06-16 15:48:30 +0000655}
656
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000657static Value *upgradeMaskedCompare(IRBuilder<> &Builder, CallInst &CI,
658 ICmpInst::Predicate Pred) {
659 Value *Op0 = CI.getArgOperand(0);
660 unsigned NumElts = Op0->getType()->getVectorNumElements();
661 Value *Cmp = Builder.CreateICmp(Pred, Op0, CI.getArgOperand(1));
662
663 Value *Mask = CI.getArgOperand(2);
664 const auto *C = dyn_cast<Constant>(Mask);
665 if (!C || !C->isAllOnesValue())
666 Cmp = Builder.CreateAnd(Cmp, getX86MaskVec(Builder, Mask, NumElts));
667
668 if (NumElts < 8) {
669 uint32_t Indices[8];
670 for (unsigned i = 0; i != NumElts; ++i)
671 Indices[i] = i;
672 for (unsigned i = NumElts; i != 8; ++i)
Craig Topperd5d2a352016-07-07 06:11:07 +0000673 Indices[i] = NumElts + i % NumElts;
674 Cmp = Builder.CreateShuffleVector(Cmp,
675 Constant::getNullValue(Cmp->getType()),
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000676 Indices);
677 }
678 return Builder.CreateBitCast(Cmp, IntegerType::get(CI.getContext(),
679 std::max(NumElts, 8U)));
680}
681
Craig Topperc9467ed2016-11-06 16:29:08 +0000682// Replace a masked intrinsic with an older unmasked intrinsic.
683static Value *UpgradeX86MaskedShift(IRBuilder<> &Builder, CallInst &CI,
684 Intrinsic::ID IID) {
685 Function *F = CI.getCalledFunction();
686 Function *Intrin = Intrinsic::getDeclaration(F->getParent(), IID);
687 Value *Rep = Builder.CreateCall(Intrin,
688 { CI.getArgOperand(0), CI.getArgOperand(1) });
689 return EmitX86Select(Builder, CI.getArgOperand(3), Rep, CI.getArgOperand(2));
690}
691
692
Sanjay Patel595098f2016-06-15 22:01:28 +0000693/// Upgrade a call to an old intrinsic. All argument and return casting must be
694/// provided to seamlessly integrate with existing context.
Chandler Carruth7132e002007-08-04 01:51:18 +0000695void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000696 Function *F = CI->getCalledFunction();
Nick Lewycky2eb3ade2011-12-12 22:59:34 +0000697 LLVMContext &C = CI->getContext();
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000698 IRBuilder<> Builder(C);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000699 Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000700
Craig Topper3b1817d2012-02-03 06:10:55 +0000701 assert(F && "Intrinsic call is not direct?");
702
703 if (!NewFn) {
704 // Get the Function's name.
705 StringRef Name = F->getName();
706
Craig Topper5aebb862016-07-04 20:56:38 +0000707 assert(Name.startswith("llvm.") && "Intrinsic doesn't start with 'llvm.'");
708 Name = Name.substr(5);
709
710 bool IsX86 = Name.startswith("x86.");
711 if (IsX86)
712 Name = Name.substr(4);
713
Craig Topper3b1817d2012-02-03 06:10:55 +0000714 Value *Rep;
Sanjay Patel595098f2016-06-15 22:01:28 +0000715 // Upgrade packed integer vector compare intrinsics to compare instructions.
Craig Topper5aebb862016-07-04 20:56:38 +0000716 if (IsX86 && (Name.startswith("sse2.pcmpeq.") ||
717 Name.startswith("avx2.pcmpeq."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000718 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
719 "pcmpeq");
Craig Topper3b1817d2012-02-03 06:10:55 +0000720 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000721 } else if (IsX86 && (Name.startswith("sse2.pcmpgt.") ||
722 Name.startswith("avx2.pcmpgt."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000723 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
724 "pcmpgt");
Craig Topper3b1817d2012-02-03 06:10:55 +0000725 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000726 } else if (IsX86 && Name.startswith("avx512.mask.pcmpeq.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000727 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_EQ);
Craig Topper5aebb862016-07-04 20:56:38 +0000728 } else if (IsX86 && Name.startswith("avx512.mask.pcmpgt.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000729 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000730 } else if (IsX86 && (Name == "sse41.pmaxsb" ||
731 Name == "sse2.pmaxs.w" ||
732 Name == "sse41.pmaxsd" ||
Craig Topper8ec5c732016-10-24 04:04:16 +0000733 Name.startswith("avx2.pmaxs") ||
734 Name.startswith("avx512.mask.pmaxs"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000735 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000736 } else if (IsX86 && (Name == "sse2.pmaxu.b" ||
737 Name == "sse41.pmaxuw" ||
738 Name == "sse41.pmaxud" ||
Craig Topper8ec5c732016-10-24 04:04:16 +0000739 Name.startswith("avx2.pmaxu") ||
740 Name.startswith("avx512.mask.pmaxu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000741 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_UGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000742 } else if (IsX86 && (Name == "sse41.pminsb" ||
743 Name == "sse2.pmins.w" ||
744 Name == "sse41.pminsd" ||
Craig Topper8ec5c732016-10-24 04:04:16 +0000745 Name.startswith("avx2.pmins") ||
746 Name.startswith("avx512.mask.pmins"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000747 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SLT);
Craig Topper5aebb862016-07-04 20:56:38 +0000748 } else if (IsX86 && (Name == "sse2.pminu.b" ||
749 Name == "sse41.pminuw" ||
750 Name == "sse41.pminud" ||
Craig Topper8ec5c732016-10-24 04:04:16 +0000751 Name.startswith("avx2.pminu") ||
752 Name.startswith("avx512.mask.pminu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000753 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_ULT);
Craig Topper5aebb862016-07-04 20:56:38 +0000754 } else if (IsX86 && (Name == "sse2.cvtdq2pd" ||
755 Name == "sse2.cvtps2pd" ||
756 Name == "avx.cvtdq2.pd.256" ||
757 Name == "avx.cvt.ps2.pd.256")) {
Simon Pilgrim4298d062016-05-25 08:59:18 +0000758 // Lossless i32/float to double conversion.
759 // Extract the bottom elements if necessary and convert to double vector.
760 Value *Src = CI->getArgOperand(0);
761 VectorType *SrcTy = cast<VectorType>(Src->getType());
762 VectorType *DstTy = cast<VectorType>(CI->getType());
763 Rep = CI->getArgOperand(0);
764
765 unsigned NumDstElts = DstTy->getNumElements();
766 if (NumDstElts < SrcTy->getNumElements()) {
767 assert(NumDstElts == 2 && "Unexpected vector size");
Craig Topper99d1eab2016-06-12 00:41:19 +0000768 uint32_t ShuffleMask[2] = { 0, 1 };
769 Rep = Builder.CreateShuffleVector(Rep, UndefValue::get(SrcTy),
770 ShuffleMask);
Simon Pilgrim4298d062016-05-25 08:59:18 +0000771 }
772
773 bool Int2Double = (StringRef::npos != Name.find("cvtdq2"));
774 if (Int2Double)
775 Rep = Builder.CreateSIToFP(Rep, DstTy, "cvtdq2pd");
776 else
777 Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd");
Craig Topper5aebb862016-07-04 20:56:38 +0000778 } else if (IsX86 && Name.startswith("sse4a.movnt.")) {
Simon Pilgrimf4b2af12016-06-18 02:38:26 +0000779 Module *M = F->getParent();
780 SmallVector<Metadata *, 1> Elts;
781 Elts.push_back(
782 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
783 MDNode *Node = MDNode::get(C, Elts);
784
785 Value *Arg0 = CI->getArgOperand(0);
786 Value *Arg1 = CI->getArgOperand(1);
787
788 // Nontemporal (unaligned) store of the 0'th element of the float/double
789 // vector.
790 Type *SrcEltTy = cast<VectorType>(Arg1->getType())->getElementType();
791 PointerType *EltPtrTy = PointerType::getUnqual(SrcEltTy);
792 Value *Addr = Builder.CreateBitCast(Arg0, EltPtrTy, "cast");
793 Value *Extract =
794 Builder.CreateExtractElement(Arg1, (uint64_t)0, "extractelement");
795
796 StoreInst *SI = Builder.CreateAlignedStore(Extract, Addr, 1);
797 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
798
799 // Remove intrinsic.
800 CI->eraseFromParent();
801 return;
Craig Topper70610cf2016-07-09 04:38:27 +0000802 } else if (IsX86 && (Name.startswith("avx.movnt.") ||
803 Name.startswith("avx512.storent."))) {
Craig Topper7daf8972012-05-08 06:58:15 +0000804 Module *M = F->getParent();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000805 SmallVector<Metadata *, 1> Elts;
806 Elts.push_back(
807 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
Craig Topper7daf8972012-05-08 06:58:15 +0000808 MDNode *Node = MDNode::get(C, Elts);
809
810 Value *Arg0 = CI->getArgOperand(0);
811 Value *Arg1 = CI->getArgOperand(1);
812
813 // Convert the type of the pointer to a pointer to the stored type.
814 Value *BC = Builder.CreateBitCast(Arg0,
815 PointerType::getUnqual(Arg1->getType()),
816 "cast");
Craig Topper70610cf2016-07-09 04:38:27 +0000817 VectorType *VTy = cast<VectorType>(Arg1->getType());
818 StoreInst *SI = Builder.CreateAlignedStore(Arg1, BC,
819 VTy->getBitWidth() / 8);
Craig Topper7daf8972012-05-08 06:58:15 +0000820 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
Craig Topper7daf8972012-05-08 06:58:15 +0000821
822 // Remove intrinsic.
823 CI->eraseFromParent();
824 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000825 } else if (IsX86 && Name == "sse2.storel.dq") {
Craig Topper12e322a2016-05-25 06:56:32 +0000826 Value *Arg0 = CI->getArgOperand(0);
827 Value *Arg1 = CI->getArgOperand(1);
828
829 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
830 Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
831 Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0);
832 Value *BC = Builder.CreateBitCast(Arg0,
833 PointerType::getUnqual(Elt->getType()),
834 "cast");
Craig Topper29ce55d2016-05-30 22:54:12 +0000835 Builder.CreateAlignedStore(Elt, BC, 1);
Craig Topper12e322a2016-05-25 06:56:32 +0000836
837 // Remove intrinsic.
838 CI->eraseFromParent();
839 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000840 } else if (IsX86 && (Name.startswith("sse.storeu.") ||
841 Name.startswith("sse2.storeu.") ||
842 Name.startswith("avx.storeu."))) {
Craig Topper8287fd82016-05-30 23:15:56 +0000843 Value *Arg0 = CI->getArgOperand(0);
844 Value *Arg1 = CI->getArgOperand(1);
845
846 Arg0 = Builder.CreateBitCast(Arg0,
847 PointerType::getUnqual(Arg1->getType()),
848 "cast");
849 Builder.CreateAlignedStore(Arg1, Arg0, 1);
850
851 // Remove intrinsic.
852 CI->eraseFromParent();
853 return;
Craig Toppera57d2ca2016-09-03 23:55:13 +0000854 } else if (IsX86 && (Name.startswith("avx512.mask.storeu."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000855 UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
Craig Topper50f85c22016-05-31 01:50:02 +0000856 CI->getArgOperand(2), /*Aligned*/false);
857
858 // Remove intrinsic.
859 CI->eraseFromParent();
860 return;
Craig Toppera57d2ca2016-09-03 23:55:13 +0000861 } else if (IsX86 && (Name.startswith("avx512.mask.store."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000862 UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
Craig Topper50f85c22016-05-31 01:50:02 +0000863 CI->getArgOperand(2), /*Aligned*/true);
864
865 // Remove intrinsic.
866 CI->eraseFromParent();
867 return;
Craig Toppera57d2ca2016-09-03 23:55:13 +0000868 } else if (IsX86 && (Name.startswith("avx512.mask.loadu."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000869 Rep = UpgradeMaskedLoad(Builder, CI->getArgOperand(0),
Craig Topperf10fbfa2016-06-02 04:19:36 +0000870 CI->getArgOperand(1), CI->getArgOperand(2),
871 /*Aligned*/false);
Craig Toppera57d2ca2016-09-03 23:55:13 +0000872 } else if (IsX86 && (Name.startswith("avx512.mask.load."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000873 Rep = UpgradeMaskedLoad(Builder, CI->getArgOperand(0),
Craig Topperf10fbfa2016-06-02 04:19:36 +0000874 CI->getArgOperand(1),CI->getArgOperand(2),
875 /*Aligned*/true);
Craig Topper5aebb862016-07-04 20:56:38 +0000876 } else if (IsX86 && Name.startswith("xop.vpcom")) {
Craig Topper3352ba52012-06-09 16:46:13 +0000877 Intrinsic::ID intID;
878 if (Name.endswith("ub"))
879 intID = Intrinsic::x86_xop_vpcomub;
880 else if (Name.endswith("uw"))
881 intID = Intrinsic::x86_xop_vpcomuw;
882 else if (Name.endswith("ud"))
883 intID = Intrinsic::x86_xop_vpcomud;
884 else if (Name.endswith("uq"))
885 intID = Intrinsic::x86_xop_vpcomuq;
886 else if (Name.endswith("b"))
887 intID = Intrinsic::x86_xop_vpcomb;
888 else if (Name.endswith("w"))
889 intID = Intrinsic::x86_xop_vpcomw;
890 else if (Name.endswith("d"))
891 intID = Intrinsic::x86_xop_vpcomd;
892 else if (Name.endswith("q"))
893 intID = Intrinsic::x86_xop_vpcomq;
894 else
895 llvm_unreachable("Unknown suffix");
896
Craig Topper5aebb862016-07-04 20:56:38 +0000897 Name = Name.substr(9); // strip off "xop.vpcom"
Craig Topper3352ba52012-06-09 16:46:13 +0000898 unsigned Imm;
899 if (Name.startswith("lt"))
900 Imm = 0;
901 else if (Name.startswith("le"))
902 Imm = 1;
903 else if (Name.startswith("gt"))
904 Imm = 2;
905 else if (Name.startswith("ge"))
906 Imm = 3;
907 else if (Name.startswith("eq"))
908 Imm = 4;
909 else if (Name.startswith("ne"))
910 Imm = 5;
Craig Topper3352ba52012-06-09 16:46:13 +0000911 else if (Name.startswith("false"))
Craig Toppere32546d2015-02-13 07:42:15 +0000912 Imm = 6;
913 else if (Name.startswith("true"))
Craig Topper3352ba52012-06-09 16:46:13 +0000914 Imm = 7;
915 else
916 llvm_unreachable("Unknown condition");
917
918 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
David Blaikieff6409d2015-05-18 22:13:54 +0000919 Rep =
920 Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1),
921 Builder.getInt8(Imm)});
Craig Topper5aebb862016-07-04 20:56:38 +0000922 } else if (IsX86 && Name == "xop.vpcmov") {
Simon Pilgrime88dc042015-11-03 20:27:01 +0000923 Value *Arg0 = CI->getArgOperand(0);
924 Value *Arg1 = CI->getArgOperand(1);
925 Value *Sel = CI->getArgOperand(2);
926 unsigned NumElts = CI->getType()->getVectorNumElements();
927 Constant *MinusOne = ConstantVector::getSplat(NumElts, Builder.getInt64(-1));
928 Value *NotSel = Builder.CreateXor(Sel, MinusOne);
929 Value *Sel0 = Builder.CreateAnd(Arg0, Sel);
930 Value *Sel1 = Builder.CreateAnd(Arg1, NotSel);
931 Rep = Builder.CreateOr(Sel0, Sel1);
Craig Topper5aebb862016-07-04 20:56:38 +0000932 } else if (IsX86 && Name == "sse42.crc32.64.8") {
Craig Topperef9e9932013-10-15 05:20:47 +0000933 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
934 Intrinsic::x86_sse42_crc32_32_8);
935 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
David Blaikieff6409d2015-05-18 22:13:54 +0000936 Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)});
Craig Topperef9e9932013-10-15 05:20:47 +0000937 Rep = Builder.CreateZExt(Rep, CI->getType(), "");
Simon Pilgrimea0d4f92016-07-22 13:58:44 +0000938 } else if (IsX86 && Name.startswith("avx.vbroadcast.s")) {
Adam Nemet39066802014-05-29 23:35:33 +0000939 // Replace broadcasts with a series of insertelements.
940 Type *VecTy = CI->getType();
941 Type *EltTy = VecTy->getVectorElementType();
942 unsigned EltNum = VecTy->getVectorNumElements();
943 Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
944 EltTy->getPointerTo());
David Blaikie0c28fd72015-05-20 21:46:30 +0000945 Value *Load = Builder.CreateLoad(EltTy, Cast);
Adam Nemet39066802014-05-29 23:35:33 +0000946 Type *I32Ty = Type::getInt32Ty(C);
947 Rep = UndefValue::get(VecTy);
948 for (unsigned I = 0; I < EltNum; ++I)
949 Rep = Builder.CreateInsertElement(Rep, Load,
950 ConstantInt::get(I32Ty, I));
Craig Topper5aebb862016-07-04 20:56:38 +0000951 } else if (IsX86 && (Name.startswith("sse41.pmovsx") ||
952 Name.startswith("sse41.pmovzx") ||
953 Name.startswith("avx2.pmovsx") ||
Craig Topperb110e042016-11-07 02:12:57 +0000954 Name.startswith("avx2.pmovzx") ||
955 Name.startswith("avx512.mask.pmovsx") ||
956 Name.startswith("avx512.mask.pmovzx"))) {
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000957 VectorType *SrcTy = cast<VectorType>(CI->getArgOperand(0)->getType());
958 VectorType *DstTy = cast<VectorType>(CI->getType());
959 unsigned NumDstElts = DstTy->getNumElements();
960
Simon Pilgrim9602d672016-05-28 18:03:41 +0000961 // Extract a subvector of the first NumDstElts lanes and sign/zero extend.
Craig Topperc0a5fa02016-06-12 04:48:00 +0000962 SmallVector<uint32_t, 8> ShuffleMask(NumDstElts);
Craig Topper99d1eab2016-06-12 00:41:19 +0000963 for (unsigned i = 0; i != NumDstElts; ++i)
Craig Topperc0a5fa02016-06-12 04:48:00 +0000964 ShuffleMask[i] = i;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +0000965
966 Value *SV = Builder.CreateShuffleVector(
967 CI->getArgOperand(0), UndefValue::get(SrcTy), ShuffleMask);
Simon Pilgrim9602d672016-05-28 18:03:41 +0000968
969 bool DoSext = (StringRef::npos != Name.find("pmovsx"));
970 Rep = DoSext ? Builder.CreateSExt(SV, DstTy)
971 : Builder.CreateZExt(SV, DstTy);
Craig Topperb110e042016-11-07 02:12:57 +0000972 // If there are 3 arguments, it's a masked intrinsic so we need a select.
973 if (CI->getNumArgOperands() == 3)
974 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
975 CI->getArgOperand(1));
Simon Pilgrimea0d4f92016-07-22 13:58:44 +0000976 } else if (IsX86 && (Name.startswith("avx.vbroadcastf128") ||
977 Name == "avx2.vbroadcasti128")) {
978 // Replace vbroadcastf128/vbroadcasti128 with a vector load+shuffle.
979 Type *EltTy = CI->getType()->getVectorElementType();
980 unsigned NumSrcElts = 128 / EltTy->getPrimitiveSizeInBits();
981 Type *VT = VectorType::get(EltTy, NumSrcElts);
David Blaikie0c28fd72015-05-20 21:46:30 +0000982 Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0),
983 PointerType::getUnqual(VT));
Chandler Carruth0215e762016-08-10 07:41:26 +0000984 Value *Load = Builder.CreateAlignedLoad(Op, 1);
Simon Pilgrimea0d4f92016-07-22 13:58:44 +0000985 if (NumSrcElts == 2)
986 Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
987 { 0, 1, 0, 1 });
988 else
989 Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
990 { 0, 1, 2, 3, 0, 1, 2, 3 });
Craig Topper5aebb862016-07-04 20:56:38 +0000991 } else if (IsX86 && (Name.startswith("avx2.pbroadcast") ||
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +0000992 Name.startswith("avx2.vbroadcast") ||
993 Name.startswith("avx512.pbroadcast") ||
994 Name.startswith("avx512.mask.broadcast.s"))) {
Ahmed Bougacha1a4987052015-08-20 20:36:19 +0000995 // Replace vp?broadcasts with a vector shuffle.
996 Value *Op = CI->getArgOperand(0);
997 unsigned NumElts = CI->getType()->getVectorNumElements();
998 Type *MaskTy = VectorType::get(Type::getInt32Ty(C), NumElts);
999 Rep = Builder.CreateShuffleVector(Op, UndefValue::get(Op->getType()),
1000 Constant::getNullValue(MaskTy));
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +00001001
1002 if (CI->getNumArgOperands() == 3)
1003 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
1004 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +00001005 } else if (IsX86 && Name.startswith("avx512.mask.palignr.")) {
Craig Topper46b34fe2016-07-12 01:42:33 +00001006 Rep = UpgradeX86PALIGNRIntrinsics(Builder, CI->getArgOperand(0),
Craig Topper33350cc2016-06-06 06:12:54 +00001007 CI->getArgOperand(1),
1008 CI->getArgOperand(2),
1009 CI->getArgOperand(3),
1010 CI->getArgOperand(4));
Craig Topper5aebb862016-07-04 20:56:38 +00001011 } else if (IsX86 && (Name == "sse2.psll.dq" ||
1012 Name == "avx2.psll.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +00001013 // 128/256-bit shift left specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +00001014 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +00001015 Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +00001016 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +00001017 } else if (IsX86 && (Name == "sse2.psrl.dq" ||
1018 Name == "avx2.psrl.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +00001019 // 128/256-bit shift right specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +00001020 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +00001021 Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +00001022 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +00001023 } else if (IsX86 && (Name == "sse2.psll.dq.bs" ||
1024 Name == "avx2.psll.dq.bs" ||
1025 Name == "avx512.psll.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +00001026 // 128/256/512-bit shift left specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +00001027 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +00001028 Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +00001029 } else if (IsX86 && (Name == "sse2.psrl.dq.bs" ||
1030 Name == "avx2.psrl.dq.bs" ||
1031 Name == "avx512.psrl.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +00001032 // 128/256/512-bit shift right specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +00001033 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +00001034 Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +00001035 } else if (IsX86 && (Name == "sse41.pblendw" ||
1036 Name.startswith("sse41.blendp") ||
1037 Name.startswith("avx.blend.p") ||
1038 Name == "avx2.pblendw" ||
1039 Name.startswith("avx2.pblendd."))) {
Craig Topper782d6202015-02-28 19:33:17 +00001040 Value *Op0 = CI->getArgOperand(0);
1041 Value *Op1 = CI->getArgOperand(1);
1042 unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue();
1043 VectorType *VecTy = cast<VectorType>(CI->getType());
1044 unsigned NumElts = VecTy->getNumElements();
1045
Craig Topperc0a5fa02016-06-12 04:48:00 +00001046 SmallVector<uint32_t, 16> Idxs(NumElts);
1047 for (unsigned i = 0; i != NumElts; ++i)
1048 Idxs[i] = ((Imm >> (i%8)) & 1) ? i + NumElts : i;
Craig Topper782d6202015-02-28 19:33:17 +00001049
Craig Topper2f561822016-06-12 01:05:59 +00001050 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001051 } else if (IsX86 && (Name.startswith("avx.vinsertf128.") ||
1052 Name == "avx2.vinserti128")) {
Sanjay Patel19792fb2015-03-10 16:08:36 +00001053 Value *Op0 = CI->getArgOperand(0);
1054 Value *Op1 = CI->getArgOperand(1);
1055 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
1056 VectorType *VecTy = cast<VectorType>(CI->getType());
1057 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001058
Sanjay Patel19792fb2015-03-10 16:08:36 +00001059 // Mask off the high bits of the immediate value; hardware ignores those.
1060 Imm = Imm & 1;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001061
Sanjay Patel19792fb2015-03-10 16:08:36 +00001062 // Extend the second operand into a vector that is twice as big.
1063 Value *UndefV = UndefValue::get(Op1->getType());
Craig Topperc0a5fa02016-06-12 04:48:00 +00001064 SmallVector<uint32_t, 8> Idxs(NumElts);
1065 for (unsigned i = 0; i != NumElts; ++i)
1066 Idxs[i] = i;
Craig Topper2f561822016-06-12 01:05:59 +00001067 Rep = Builder.CreateShuffleVector(Op1, UndefV, Idxs);
Sanjay Patel19792fb2015-03-10 16:08:36 +00001068
1069 // Insert the second operand into the first operand.
1070
1071 // Note that there is no guarantee that instruction lowering will actually
1072 // produce a vinsertf128 instruction for the created shuffles. In
1073 // particular, the 0 immediate case involves no lane changes, so it can
1074 // be handled as a blend.
1075
1076 // Example of shuffle mask for 32-bit elements:
1077 // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
1078 // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 >
1079
Sanjay Patel19792fb2015-03-10 16:08:36 +00001080 // The low half of the result is either the low half of the 1st operand
1081 // or the low half of the 2nd operand (the inserted vector).
Craig Topperc0a5fa02016-06-12 04:48:00 +00001082 for (unsigned i = 0; i != NumElts / 2; ++i)
1083 Idxs[i] = Imm ? i : (i + NumElts);
Sanjay Patel19792fb2015-03-10 16:08:36 +00001084 // The high half of the result is either the low half of the 2nd operand
1085 // (the inserted vector) or the high half of the 1st operand.
Craig Topperc0a5fa02016-06-12 04:48:00 +00001086 for (unsigned i = NumElts / 2; i != NumElts; ++i)
1087 Idxs[i] = Imm ? (i + NumElts / 2) : i;
Craig Topper2f561822016-06-12 01:05:59 +00001088 Rep = Builder.CreateShuffleVector(Op0, Rep, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001089 } else if (IsX86 && (Name.startswith("avx.vextractf128.") ||
1090 Name == "avx2.vextracti128")) {
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001091 Value *Op0 = CI->getArgOperand(0);
1092 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1093 VectorType *VecTy = cast<VectorType>(CI->getType());
1094 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001095
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001096 // Mask off the high bits of the immediate value; hardware ignores those.
1097 Imm = Imm & 1;
1098
1099 // Get indexes for either the high half or low half of the input vector.
Craig Topper2f561822016-06-12 01:05:59 +00001100 SmallVector<uint32_t, 4> Idxs(NumElts);
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001101 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topper2f561822016-06-12 01:05:59 +00001102 Idxs[i] = Imm ? (i + NumElts) : i;
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001103 }
1104
1105 Value *UndefV = UndefValue::get(Op0->getType());
Craig Topper2f561822016-06-12 01:05:59 +00001106 Rep = Builder.CreateShuffleVector(Op0, UndefV, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001107 } else if (!IsX86 && Name == "stackprotectorcheck") {
Tim Shen00127562016-04-08 21:26:31 +00001108 Rep = nullptr;
Craig Topper5aebb862016-07-04 20:56:38 +00001109 } else if (IsX86 && (Name.startswith("avx512.mask.perm.df.") ||
1110 Name.startswith("avx512.mask.perm.di."))) {
Simon Pilgrim02d435d2016-07-04 14:19:05 +00001111 Value *Op0 = CI->getArgOperand(0);
1112 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1113 VectorType *VecTy = cast<VectorType>(CI->getType());
1114 unsigned NumElts = VecTy->getNumElements();
1115
1116 SmallVector<uint32_t, 8> Idxs(NumElts);
1117 for (unsigned i = 0; i != NumElts; ++i)
1118 Idxs[i] = (i & ~0x3) + ((Imm >> (2 * (i & 0x3))) & 3);
1119
1120 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1121
1122 if (CI->getNumArgOperands() == 4)
1123 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1124 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001125 } else if (IsX86 && (Name.startswith("avx.vpermil.") ||
1126 Name == "sse2.pshuf.d" ||
1127 Name.startswith("avx512.mask.vpermil.p") ||
1128 Name.startswith("avx512.mask.pshuf.d."))) {
Craig Topper8a105052016-06-12 03:10:47 +00001129 Value *Op0 = CI->getArgOperand(0);
1130 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1131 VectorType *VecTy = cast<VectorType>(CI->getType());
1132 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9fca3002016-07-04 12:40:54 +00001133 // Calculate the size of each index in the immediate.
Craig Topper8a105052016-06-12 03:10:47 +00001134 unsigned IdxSize = 64 / VecTy->getScalarSizeInBits();
1135 unsigned IdxMask = ((1 << IdxSize) - 1);
1136
1137 SmallVector<uint32_t, 8> Idxs(NumElts);
1138 // Lookup the bits for this element, wrapping around the immediate every
1139 // 8-bits. Elements are grouped into sets of 2 or 4 elements so we need
1140 // to offset by the first index of each group.
1141 for (unsigned i = 0; i != NumElts; ++i)
1142 Idxs[i] = ((Imm >> ((i * IdxSize) % 8)) & IdxMask) | (i & ~IdxMask);
1143
1144 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001145
1146 if (CI->getNumArgOperands() == 4)
1147 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1148 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001149 } else if (IsX86 && (Name == "sse2.pshufl.w" ||
1150 Name.startswith("avx512.mask.pshufl.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001151 Value *Op0 = CI->getArgOperand(0);
1152 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1153 unsigned NumElts = CI->getType()->getVectorNumElements();
1154
1155 SmallVector<uint32_t, 16> Idxs(NumElts);
1156 for (unsigned l = 0; l != NumElts; l += 8) {
1157 for (unsigned i = 0; i != 4; ++i)
1158 Idxs[i + l] = ((Imm >> (2 * i)) & 0x3) + l;
1159 for (unsigned i = 4; i != 8; ++i)
1160 Idxs[i + l] = i + l;
1161 }
1162
1163 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001164
1165 if (CI->getNumArgOperands() == 4)
1166 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1167 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001168 } else if (IsX86 && (Name == "sse2.pshufh.w" ||
1169 Name.startswith("avx512.mask.pshufh.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001170 Value *Op0 = CI->getArgOperand(0);
1171 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1172 unsigned NumElts = CI->getType()->getVectorNumElements();
1173
1174 SmallVector<uint32_t, 16> Idxs(NumElts);
1175 for (unsigned l = 0; l != NumElts; l += 8) {
1176 for (unsigned i = 0; i != 4; ++i)
1177 Idxs[i + l] = i + l;
1178 for (unsigned i = 0; i != 4; ++i)
1179 Idxs[i + l + 4] = ((Imm >> (2 * i)) & 0x3) + 4 + l;
1180 }
1181
1182 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001183
1184 if (CI->getNumArgOperands() == 4)
1185 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1186 CI->getArgOperand(2));
Craig Topper4619c9e2016-09-13 07:40:53 +00001187 } else if (IsX86 && Name.startswith("avx512.mask.shuf.p")) {
1188 Value *Op0 = CI->getArgOperand(0);
1189 Value *Op1 = CI->getArgOperand(1);
1190 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
1191 unsigned NumElts = CI->getType()->getVectorNumElements();
1192
1193 unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1194 unsigned HalfLaneElts = NumLaneElts / 2;
1195
1196 SmallVector<uint32_t, 16> Idxs(NumElts);
1197 for (unsigned i = 0; i != NumElts; ++i) {
1198 // Base index is the starting element of the lane.
1199 Idxs[i] = i - (i % NumLaneElts);
1200 // If we are half way through the lane switch to the other source.
1201 if ((i % NumLaneElts) >= HalfLaneElts)
1202 Idxs[i] += NumElts;
1203 // Now select the specific element. By adding HalfLaneElts bits from
1204 // the immediate. Wrapping around the immediate every 8-bits.
1205 Idxs[i] += (Imm >> ((i * HalfLaneElts) % 8)) & ((1 << HalfLaneElts) - 1);
1206 }
1207
1208 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1209
1210 Rep = EmitX86Select(Builder, CI->getArgOperand(4), Rep,
1211 CI->getArgOperand(3));
Craig Topper5aebb862016-07-04 20:56:38 +00001212 } else if (IsX86 && (Name.startswith("avx512.mask.movddup") ||
1213 Name.startswith("avx512.mask.movshdup") ||
1214 Name.startswith("avx512.mask.movsldup"))) {
Simon Pilgrim19adee92016-07-02 14:42:35 +00001215 Value *Op0 = CI->getArgOperand(0);
1216 unsigned NumElts = CI->getType()->getVectorNumElements();
1217 unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1218
1219 unsigned Offset = 0;
Craig Topper5aebb862016-07-04 20:56:38 +00001220 if (Name.startswith("avx512.mask.movshdup."))
Simon Pilgrim19adee92016-07-02 14:42:35 +00001221 Offset = 1;
1222
1223 SmallVector<uint32_t, 16> Idxs(NumElts);
1224 for (unsigned l = 0; l != NumElts; l += NumLaneElts)
1225 for (unsigned i = 0; i != NumLaneElts; i += 2) {
1226 Idxs[i + l + 0] = i + l + Offset;
1227 Idxs[i + l + 1] = i + l + Offset;
1228 }
1229
1230 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1231
1232 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
1233 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +00001234 } else if (IsX86 && (Name.startswith("avx512.mask.punpckl") ||
1235 Name.startswith("avx512.mask.unpckl."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001236 Value *Op0 = CI->getArgOperand(0);
1237 Value *Op1 = CI->getArgOperand(1);
1238 int NumElts = CI->getType()->getVectorNumElements();
1239 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1240
1241 SmallVector<uint32_t, 64> Idxs(NumElts);
1242 for (int l = 0; l != NumElts; l += NumLaneElts)
1243 for (int i = 0; i != NumLaneElts; ++i)
1244 Idxs[i + l] = l + (i / 2) + NumElts * (i % 2);
1245
1246 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1247
1248 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1249 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001250 } else if (IsX86 && (Name.startswith("avx512.mask.punpckh") ||
1251 Name.startswith("avx512.mask.unpckh."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001252 Value *Op0 = CI->getArgOperand(0);
1253 Value *Op1 = CI->getArgOperand(1);
1254 int NumElts = CI->getType()->getVectorNumElements();
1255 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1256
1257 SmallVector<uint32_t, 64> Idxs(NumElts);
1258 for (int l = 0; l != NumElts; l += NumLaneElts)
1259 for (int i = 0; i != NumLaneElts; ++i)
1260 Idxs[i + l] = (NumLaneElts / 2) + l + (i / 2) + NumElts * (i % 2);
1261
1262 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1263
1264 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1265 CI->getArgOperand(2));
Craig Toppera6e6feb2016-07-12 05:27:53 +00001266 } else if (IsX86 && Name.startswith("avx512.mask.pand.")) {
1267 Rep = Builder.CreateAnd(CI->getArgOperand(0), CI->getArgOperand(1));
1268 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1269 CI->getArgOperand(2));
1270 } else if (IsX86 && Name.startswith("avx512.mask.pandn.")) {
1271 Rep = Builder.CreateAnd(Builder.CreateNot(CI->getArgOperand(0)),
1272 CI->getArgOperand(1));
1273 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1274 CI->getArgOperand(2));
1275 } else if (IsX86 && Name.startswith("avx512.mask.por.")) {
1276 Rep = Builder.CreateOr(CI->getArgOperand(0), CI->getArgOperand(1));
1277 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1278 CI->getArgOperand(2));
1279 } else if (IsX86 && Name.startswith("avx512.mask.pxor.")) {
1280 Rep = Builder.CreateXor(CI->getArgOperand(0), CI->getArgOperand(1));
1281 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1282 CI->getArgOperand(2));
Craig Toppere75c4952016-09-02 05:29:17 +00001283 } else if (IsX86 && Name.startswith("avx512.mask.and.")) {
1284 VectorType *FTy = cast<VectorType>(CI->getType());
1285 VectorType *ITy = VectorType::getInteger(FTy);
1286 Rep = Builder.CreateAnd(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
1287 Builder.CreateBitCast(CI->getArgOperand(1), ITy));
1288 Rep = Builder.CreateBitCast(Rep, FTy);
1289 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1290 CI->getArgOperand(2));
1291 } else if (IsX86 && Name.startswith("avx512.mask.andn.")) {
1292 VectorType *FTy = cast<VectorType>(CI->getType());
1293 VectorType *ITy = VectorType::getInteger(FTy);
1294 Rep = Builder.CreateNot(Builder.CreateBitCast(CI->getArgOperand(0), ITy));
1295 Rep = Builder.CreateAnd(Rep,
1296 Builder.CreateBitCast(CI->getArgOperand(1), ITy));
1297 Rep = Builder.CreateBitCast(Rep, FTy);
1298 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1299 CI->getArgOperand(2));
1300 } else if (IsX86 && Name.startswith("avx512.mask.or.")) {
1301 VectorType *FTy = cast<VectorType>(CI->getType());
1302 VectorType *ITy = VectorType::getInteger(FTy);
1303 Rep = Builder.CreateOr(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
1304 Builder.CreateBitCast(CI->getArgOperand(1), ITy));
1305 Rep = Builder.CreateBitCast(Rep, FTy);
1306 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1307 CI->getArgOperand(2));
1308 } else if (IsX86 && Name.startswith("avx512.mask.xor.")) {
1309 VectorType *FTy = cast<VectorType>(CI->getType());
1310 VectorType *ITy = VectorType::getInteger(FTy);
1311 Rep = Builder.CreateXor(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
1312 Builder.CreateBitCast(CI->getArgOperand(1), ITy));
1313 Rep = Builder.CreateBitCast(Rep, FTy);
1314 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1315 CI->getArgOperand(2));
Craig Topperaf0d63d2016-09-04 02:09:53 +00001316 } else if (IsX86 && Name.startswith("avx512.mask.padd.")) {
1317 Rep = Builder.CreateAdd(CI->getArgOperand(0), CI->getArgOperand(1));
1318 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1319 CI->getArgOperand(2));
1320 } else if (IsX86 && Name.startswith("avx512.mask.psub.")) {
1321 Rep = Builder.CreateSub(CI->getArgOperand(0), CI->getArgOperand(1));
1322 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1323 CI->getArgOperand(2));
1324 } else if (IsX86 && Name.startswith("avx512.mask.pmull.")) {
1325 Rep = Builder.CreateMul(CI->getArgOperand(0), CI->getArgOperand(1));
1326 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1327 CI->getArgOperand(2));
Craig Topper8f354a72016-11-05 05:35:23 +00001328 } else if (IsX86 && (Name.startswith("avx512.mask.add.p"))) {
Craig Topper41773452016-09-04 18:13:33 +00001329 Rep = Builder.CreateFAdd(CI->getArgOperand(0), CI->getArgOperand(1));
1330 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1331 CI->getArgOperand(2));
Craig Topper96041c62016-11-07 00:13:42 +00001332 } else if (IsX86 && Name.startswith("avx512.mask.div.p")) {
Craig Topper41773452016-09-04 18:13:33 +00001333 Rep = Builder.CreateFDiv(CI->getArgOperand(0), CI->getArgOperand(1));
1334 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1335 CI->getArgOperand(2));
Craig Topper96041c62016-11-07 00:13:42 +00001336 } else if (IsX86 && Name.startswith("avx512.mask.mul.p")) {
Craig Topper41773452016-09-04 18:13:33 +00001337 Rep = Builder.CreateFMul(CI->getArgOperand(0), CI->getArgOperand(1));
1338 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1339 CI->getArgOperand(2));
Craig Topper96041c62016-11-07 00:13:42 +00001340 } else if (IsX86 && Name.startswith("avx512.mask.sub.p")) {
Craig Topper41773452016-09-04 18:13:33 +00001341 Rep = Builder.CreateFSub(CI->getArgOperand(0), CI->getArgOperand(1));
1342 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1343 CI->getArgOperand(2));
Craig Topper7e545332016-11-07 00:13:39 +00001344 } else if (IsX86 && Name.startswith("avx512.mask.pshuf.b.")) {
1345 VectorType *VecTy = cast<VectorType>(CI->getType());
1346 Intrinsic::ID IID;
1347 if (VecTy->getPrimitiveSizeInBits() == 128)
1348 IID = Intrinsic::x86_ssse3_pshuf_b_128;
1349 else if (VecTy->getPrimitiveSizeInBits() == 256)
1350 IID = Intrinsic::x86_avx2_pshuf_b;
1351 else
1352 llvm_unreachable("Unexpected intrinsic");
1353
1354 Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
1355 { CI->getArgOperand(0), CI->getArgOperand(1) });
1356 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1357 CI->getArgOperand(2));
Craig Topper987dad22016-11-13 19:09:56 +00001358 } else if (IsX86 && Name.startswith("avx512.mask.psll")) {
1359 bool IsImmediate = Name[16] == 'i' ||
1360 (Name.size() > 18 && Name[18] == 'i');
1361 bool IsVariable = Name[16] == 'v';
1362 char Size = Name[16] == '.' ? Name[17] :
1363 Name[17] == '.' ? Name[18] :
1364 Name[19];
1365
1366 Intrinsic::ID IID;
1367 if (IsVariable) {
1368 if (Size == 'd' && Name[17] == '2') // avx512.mask.psllv2.di
1369 IID = Intrinsic::x86_avx2_psllv_q;
1370 else if (Size == 'd' && Name[17] == '4') // avx512.mask.psllv4.di
1371 IID = Intrinsic::x86_avx2_psllv_q_256;
1372 else if (Size == 's' && Name[17] == '4') // avx512.mask.psllv4.si
1373 IID = Intrinsic::x86_avx2_psllv_d;
1374 else if (Size == 's' && Name[17] == '8') // avx512.mask.psllv8.si
1375 IID = Intrinsic::x86_avx2_psllv_d_256;
1376 else
1377 llvm_unreachable("Unexpected size");
1378 } else if (Name.endswith(".128")) {
1379 if (Size == 'd') // avx512.mask.psll.d.128, avx512.mask.psll.di.128
1380 IID = IsImmediate ? Intrinsic::x86_sse2_pslli_d
1381 : Intrinsic::x86_sse2_psll_d;
1382 else if (Size == 'q') // avx512.mask.psll.q.128, avx512.mask.psll.qi.128
1383 IID = IsImmediate ? Intrinsic::x86_sse2_pslli_q
1384 : Intrinsic::x86_sse2_psll_q;
1385 else if (Size == 'w') // avx512.mask.psll.w.128, avx512.mask.psll.wi.128
1386 IID = IsImmediate ? Intrinsic::x86_sse2_pslli_w
1387 : Intrinsic::x86_sse2_psll_w;
1388 else
1389 llvm_unreachable("Unexpected size");
1390 } else if (Name.endswith(".256")) {
1391 if (Size == 'd') // avx512.mask.psll.d.256, avx512.mask.psll.di.256
1392 IID = IsImmediate ? Intrinsic::x86_avx2_pslli_d
1393 : Intrinsic::x86_avx2_psll_d;
1394 else if (Size == 'q') // avx512.mask.psll.q.256, avx512.mask.psll.qi.256
1395 IID = IsImmediate ? Intrinsic::x86_avx2_pslli_q
1396 : Intrinsic::x86_avx2_psll_q;
1397 else if (Size == 'w') // avx512.mask.psll.w.256, avx512.mask.psll.wi.256
1398 IID = IsImmediate ? Intrinsic::x86_avx2_pslli_w
1399 : Intrinsic::x86_avx2_psll_w;
1400 else
1401 llvm_unreachable("Unexpected size");
1402 } else {
1403 if (Size == 'd') // psll.di.512, pslli.d, psll.d
1404 IID = IsImmediate ? Intrinsic::x86_avx512_pslli_d_512
1405 : Intrinsic::x86_avx512_psll_d_512;
1406 else if (Size == 'q') // psll.qi.512, pslli.q, psll.q
1407 IID = IsImmediate ? Intrinsic::x86_avx512_pslli_q_512
1408 : Intrinsic::x86_avx512_psll_q_512;
1409 else if (Size == 'w') // psll.wi.512, pslli.w, psll.w
1410 IID = IsImmediate ? Intrinsic::x86_avx512_pslli_w_512
1411 : Intrinsic::x86_avx512_psll_w_512;
1412 else
1413 llvm_unreachable("Unexpected size");
1414 }
1415
1416 Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
1417 } else if (IsX86 && Name.startswith("avx512.mask.psrl")) {
1418 bool IsImmediate = Name[16] == 'i' ||
1419 (Name.size() > 18 && Name[18] == 'i');
1420 bool IsVariable = Name[16] == 'v';
1421 char Size = Name[16] == '.' ? Name[17] :
1422 Name[17] == '.' ? Name[18] :
1423 Name[19];
1424
1425 Intrinsic::ID IID;
1426 if (IsVariable) {
1427 if (Size == 'd' && Name[17] == '2') // avx512.mask.psrlv2.di
1428 IID = Intrinsic::x86_avx2_psrlv_q;
1429 else if (Size == 'd' && Name[17] == '4') // avx512.mask.psrlv4.di
1430 IID = Intrinsic::x86_avx2_psrlv_q_256;
1431 else if (Size == 's' && Name[17] == '4') // avx512.mask.psrlv4.si
1432 IID = Intrinsic::x86_avx2_psrlv_d;
1433 else if (Size == 's' && Name[17] == '8') // avx512.mask.psrlv8.si
1434 IID = Intrinsic::x86_avx2_psrlv_d_256;
1435 else
1436 llvm_unreachable("Unexpected size");
1437 } else if (Name.endswith(".128")) {
1438 if (Size == 'd') // avx512.mask.psrl.d.128, avx512.mask.psrl.di.128
1439 IID = IsImmediate ? Intrinsic::x86_sse2_psrli_d
1440 : Intrinsic::x86_sse2_psrl_d;
1441 else if (Size == 'q') // avx512.mask.psrl.q.128, avx512.mask.psrl.qi.128
1442 IID = IsImmediate ? Intrinsic::x86_sse2_psrli_q
1443 : Intrinsic::x86_sse2_psrl_q;
1444 else if (Size == 'w') // avx512.mask.psrl.w.128, avx512.mask.psrl.wi.128
1445 IID = IsImmediate ? Intrinsic::x86_sse2_psrli_w
1446 : Intrinsic::x86_sse2_psrl_w;
1447 else
1448 llvm_unreachable("Unexpected size");
1449 } else if (Name.endswith(".256")) {
1450 if (Size == 'd') // avx512.mask.psrl.d.256, avx512.mask.psrl.di.256
1451 IID = IsImmediate ? Intrinsic::x86_avx2_psrli_d
1452 : Intrinsic::x86_avx2_psrl_d;
1453 else if (Size == 'q') // avx512.mask.psrl.q.256, avx512.mask.psrl.qi.256
1454 IID = IsImmediate ? Intrinsic::x86_avx2_psrli_q
1455 : Intrinsic::x86_avx2_psrl_q;
1456 else if (Size == 'w') // avx512.mask.psrl.w.256, avx512.mask.psrl.wi.256
1457 IID = IsImmediate ? Intrinsic::x86_avx2_psrli_w
1458 : Intrinsic::x86_avx2_psrl_w;
1459 else
1460 llvm_unreachable("Unexpected size");
1461 } else {
1462 if (Size == 'd') // psrl.di.512, psrli.d, psrl.d
1463 IID = IsImmediate ? Intrinsic::x86_avx512_psrli_d_512
1464 : Intrinsic::x86_avx512_psrl_d_512;
1465 else if (Size == 'q') // psrl.qi.512, psrli.q, psrl.q
1466 IID = IsImmediate ? Intrinsic::x86_avx512_psrli_q_512
1467 : Intrinsic::x86_avx512_psrl_q_512;
1468 else if (Size == 'w') // psrl.wi.512, psrli.w, psrl.w)
1469 IID = IsImmediate ? Intrinsic::x86_avx512_psrli_w_512
1470 : Intrinsic::x86_avx512_psrl_w_512;
1471 else
1472 llvm_unreachable("Unexpected size");
1473 }
1474
1475 Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
1476 } else if (IsX86 && Name.startswith("avx512.mask.psra")) {
1477 bool IsImmediate = Name[16] == 'i' ||
1478 (Name.size() > 18 && Name[18] == 'i');
1479 bool IsVariable = Name[16] == 'v';
1480 char Size = Name[16] == '.' ? Name[17] :
1481 Name[17] == '.' ? Name[18] :
1482 Name[19];
1483
1484 Intrinsic::ID IID;
1485 if (IsVariable) {
1486 if (Size == 's' && Name[17] == '4') // avx512.mask.psrav4.si
1487 IID = Intrinsic::x86_avx2_psrav_d;
1488 else if (Size == 's' && Name[17] == '8') // avx512.mask.psrav8.si
1489 IID = Intrinsic::x86_avx2_psrav_d_256;
1490 else
1491 llvm_unreachable("Unexpected size");
1492 } else if (Name.endswith(".128")) {
1493 if (Size == 'd') // avx512.mask.psra.d.128, avx512.mask.psra.di.128
1494 IID = IsImmediate ? Intrinsic::x86_sse2_psrai_d
1495 : Intrinsic::x86_sse2_psra_d;
1496 else if (Size == 'q') // avx512.mask.psra.q.128, avx512.mask.psra.qi.128
1497 IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_128
1498 : Intrinsic::x86_avx512_psra_q_128;
1499 else if (Size == 'w') // avx512.mask.psra.w.128, avx512.mask.psra.wi.128
1500 IID = IsImmediate ? Intrinsic::x86_sse2_psrai_w
1501 : Intrinsic::x86_sse2_psra_w;
1502 else
1503 llvm_unreachable("Unexpected size");
1504 } else if (Name.endswith(".256")) {
1505 if (Size == 'd') // avx512.mask.psra.d.256, avx512.mask.psra.di.256
1506 IID = IsImmediate ? Intrinsic::x86_avx2_psrai_d
1507 : Intrinsic::x86_avx2_psra_d;
1508 else if (Size == 'q') // avx512.mask.psra.q.256, avx512.mask.psra.qi.256
1509 IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_256
1510 : Intrinsic::x86_avx512_psra_q_256;
1511 else if (Size == 'w') // avx512.mask.psra.w.256, avx512.mask.psra.wi.256
1512 IID = IsImmediate ? Intrinsic::x86_avx2_psrai_w
1513 : Intrinsic::x86_avx2_psra_w;
1514 else
1515 llvm_unreachable("Unexpected size");
1516 } else {
1517 if (Size == 'd') // psra.di.512, psrai.d, psra.d
1518 IID = IsImmediate ? Intrinsic::x86_avx512_psrai_d_512
1519 : Intrinsic::x86_avx512_psra_d_512;
1520 else if (Size == 'q') // psra.qi.512, psrai.q, psra.q
1521 IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_512
1522 : Intrinsic::x86_avx512_psra_q_512;
1523 else if (Size == 'w') // psra.wi.512, psrai.w, psra.w
1524 IID = IsImmediate ? Intrinsic::x86_avx512_psrai_w_512
1525 : Intrinsic::x86_avx512_psra_w_512;
1526 else
1527 llvm_unreachable("Unexpected size");
1528 }
1529
1530 Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
Craig Topper3b1817d2012-02-03 06:10:55 +00001531 } else {
Craig Topper8a105052016-06-12 03:10:47 +00001532 llvm_unreachable("Unknown function for CallInst upgrade.");
Craig Topper3b1817d2012-02-03 06:10:55 +00001533 }
1534
Tim Shen00127562016-04-08 21:26:31 +00001535 if (Rep)
1536 CI->replaceAllUsesWith(Rep);
Craig Topper3b1817d2012-02-03 06:10:55 +00001537 CI->eraseFromParent();
1538 return;
1539 }
1540
Yaron Kerend1fdbe72015-03-30 16:10:39 +00001541 std::string Name = CI->getName();
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001542 if (!Name.empty())
1543 CI->setName(Name + ".old");
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001544
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001545 switch (NewFn->getIntrinsicID()) {
1546 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +00001547 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001548
Jeroen Ketemaab99b592015-09-30 10:56:37 +00001549 case Intrinsic::arm_neon_vld1:
1550 case Intrinsic::arm_neon_vld2:
1551 case Intrinsic::arm_neon_vld3:
1552 case Intrinsic::arm_neon_vld4:
1553 case Intrinsic::arm_neon_vld2lane:
1554 case Intrinsic::arm_neon_vld3lane:
1555 case Intrinsic::arm_neon_vld4lane:
1556 case Intrinsic::arm_neon_vst1:
1557 case Intrinsic::arm_neon_vst2:
1558 case Intrinsic::arm_neon_vst3:
1559 case Intrinsic::arm_neon_vst4:
1560 case Intrinsic::arm_neon_vst2lane:
1561 case Intrinsic::arm_neon_vst3lane:
1562 case Intrinsic::arm_neon_vst4lane: {
1563 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1564 CI->arg_operands().end());
1565 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1566 CI->eraseFromParent();
1567 return;
1568 }
1569
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001570 case Intrinsic::ctlz:
Nuno Lopesad40c0a2012-05-22 15:25:31 +00001571 case Intrinsic::cttz:
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001572 assert(CI->getNumArgOperands() == 1 &&
1573 "Mismatch between function args and call args");
David Blaikieff6409d2015-05-18 22:13:54 +00001574 CI->replaceAllUsesWith(Builder.CreateCall(
1575 NewFn, {CI->getArgOperand(0), Builder.getFalse()}, Name));
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001576 CI->eraseFromParent();
1577 return;
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001578
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001579 case Intrinsic::objectsize:
David Blaikieff6409d2015-05-18 22:13:54 +00001580 CI->replaceAllUsesWith(Builder.CreateCall(
1581 NewFn, {CI->getArgOperand(0), CI->getArgOperand(1)}, Name));
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001582 CI->eraseFromParent();
1583 return;
1584
Joel Jonesb84f7be2012-07-18 00:02:16 +00001585 case Intrinsic::ctpop: {
David Blaikieff6409d2015-05-18 22:13:54 +00001586 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {CI->getArgOperand(0)}));
Joel Jonesb84f7be2012-07-18 00:02:16 +00001587 CI->eraseFromParent();
1588 return;
1589 }
Joel Jones43cb8782012-07-13 23:25:25 +00001590
Craig Topper71dc02d2012-06-13 07:18:53 +00001591 case Intrinsic::x86_xop_vfrcz_ss:
1592 case Intrinsic::x86_xop_vfrcz_sd:
David Blaikieff6409d2015-05-18 22:13:54 +00001593 CI->replaceAllUsesWith(
1594 Builder.CreateCall(NewFn, {CI->getArgOperand(1)}, Name));
Craig Topper71dc02d2012-06-13 07:18:53 +00001595 CI->eraseFromParent();
1596 return;
1597
Simon Pilgrime85506b2016-06-03 08:06:03 +00001598 case Intrinsic::x86_xop_vpermil2pd:
1599 case Intrinsic::x86_xop_vpermil2ps:
1600 case Intrinsic::x86_xop_vpermil2pd_256:
1601 case Intrinsic::x86_xop_vpermil2ps_256: {
1602 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1603 CI->arg_operands().end());
1604 VectorType *FltIdxTy = cast<VectorType>(Args[2]->getType());
1605 VectorType *IntIdxTy = VectorType::getInteger(FltIdxTy);
1606 Args[2] = Builder.CreateBitCast(Args[2], IntIdxTy);
1607 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args, Name));
1608 CI->eraseFromParent();
1609 return;
1610 }
1611
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001612 case Intrinsic::x86_sse41_ptestc:
1613 case Intrinsic::x86_sse41_ptestz:
Craig Topper71dc02d2012-06-13 07:18:53 +00001614 case Intrinsic::x86_sse41_ptestnzc: {
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001615 // The arguments for these intrinsics used to be v4f32, and changed
1616 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
1617 // So, the only thing required is a bitcast for both arguments.
1618 // First, check the arguments have the old type.
1619 Value *Arg0 = CI->getArgOperand(0);
1620 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
1621 return;
1622
1623 // Old intrinsic, add bitcasts
1624 Value *Arg1 = CI->getArgOperand(1);
1625
David Blaikie5bacf372015-04-24 21:16:07 +00001626 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001627
David Blaikie5bacf372015-04-24 21:16:07 +00001628 Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast");
1629 Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
1630
David Blaikieff6409d2015-05-18 22:13:54 +00001631 CallInst *NewCall = Builder.CreateCall(NewFn, {BC0, BC1}, Name);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001632 CI->replaceAllUsesWith(NewCall);
1633 CI->eraseFromParent();
1634 return;
Evan Cheng0e179d02007-12-17 22:33:23 +00001635 }
Chandler Carruth373b2b12014-09-06 10:00:01 +00001636
Chandler Carruth373b2b12014-09-06 10:00:01 +00001637 case Intrinsic::x86_sse41_insertps:
1638 case Intrinsic::x86_sse41_dppd:
1639 case Intrinsic::x86_sse41_dpps:
1640 case Intrinsic::x86_sse41_mpsadbw:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001641 case Intrinsic::x86_avx_dp_ps_256:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001642 case Intrinsic::x86_avx2_mpsadbw: {
1643 // Need to truncate the last argument from i32 to i8 -- this argument models
1644 // an inherently 8-bit immediate operand to these x86 instructions.
1645 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1646 CI->arg_operands().end());
1647
1648 // Replace the last argument with a trunc.
1649 Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
1650
1651 CallInst *NewCall = Builder.CreateCall(NewFn, Args);
1652 CI->replaceAllUsesWith(NewCall);
1653 CI->eraseFromParent();
1654 return;
1655 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +00001656
1657 case Intrinsic::thread_pointer: {
1658 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {}));
1659 CI->eraseFromParent();
1660 return;
1661 }
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +00001662
Mehdi Amini8c629ec2016-08-13 23:31:24 +00001663 case Intrinsic::invariant_start:
1664 case Intrinsic::invariant_end:
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +00001665 case Intrinsic::masked_load:
1666 case Intrinsic::masked_store: {
1667 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1668 CI->arg_operands().end());
1669 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1670 CI->eraseFromParent();
1671 return;
1672 }
Craig Topper71dc02d2012-06-13 07:18:53 +00001673 }
Chandler Carruth7132e002007-08-04 01:51:18 +00001674}
1675
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001676void llvm::UpgradeCallsToIntrinsic(Function *F) {
Chandler Carruth7132e002007-08-04 01:51:18 +00001677 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
1678
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001679 // Check if this function should be upgraded and get the replacement function
1680 // if there is one.
Chris Lattner80ed9dc2011-06-18 06:05:24 +00001681 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +00001682 if (UpgradeIntrinsicFunction(F, NewFn)) {
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001683 // Replace all users of the old function with the new function or new
1684 // instructions. This is not a range loop because the call is deleted.
1685 for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; )
Duncan P. N. Exon Smith93f53c42016-04-17 03:59:37 +00001686 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001687 UpgradeIntrinsicCall(CI, NewFn);
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001688
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001689 // Remove old function, no longer used, from the module.
1690 F->eraseFromParent();
Chandler Carruth7132e002007-08-04 01:51:18 +00001691 }
1692}
Devang Patel80ae3492009-08-28 23:24:31 +00001693
Mehdi Aminie4709272016-09-14 22:29:59 +00001694MDNode *llvm::UpgradeTBAANode(MDNode &MD) {
Manman Ren209b17c2013-09-28 00:22:27 +00001695 // Check if the tag uses struct-path aware TBAA format.
Mehdi Aminie4709272016-09-14 22:29:59 +00001696 if (isa<MDNode>(MD.getOperand(0)) && MD.getNumOperands() >= 3)
1697 return &MD;
Manman Ren209b17c2013-09-28 00:22:27 +00001698
Mehdi Aminie4709272016-09-14 22:29:59 +00001699 auto &Context = MD.getContext();
1700 if (MD.getNumOperands() == 3) {
1701 Metadata *Elts[] = {MD.getOperand(0), MD.getOperand(1)};
1702 MDNode *ScalarType = MDNode::get(Context, Elts);
Manman Ren209b17c2013-09-28 00:22:27 +00001703 // Create a MDNode <ScalarType, ScalarType, offset 0, const>
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001704 Metadata *Elts2[] = {ScalarType, ScalarType,
Mehdi Aminie4709272016-09-14 22:29:59 +00001705 ConstantAsMetadata::get(
1706 Constant::getNullValue(Type::getInt64Ty(Context))),
1707 MD.getOperand(2)};
1708 return MDNode::get(Context, Elts2);
Manman Ren209b17c2013-09-28 00:22:27 +00001709 }
Mehdi Aminie4709272016-09-14 22:29:59 +00001710 // Create a MDNode <MD, MD, offset 0>
1711 Metadata *Elts[] = {&MD, &MD, ConstantAsMetadata::get(Constant::getNullValue(
1712 Type::getInt64Ty(Context)))};
1713 return MDNode::get(Context, Elts);
Manman Ren209b17c2013-09-28 00:22:27 +00001714}
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001715
1716Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
1717 Instruction *&Temp) {
1718 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001719 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001720
Craig Topperc6207612014-04-09 06:08:46 +00001721 Temp = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001722 Type *SrcTy = V->getType();
1723 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1724 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1725 LLVMContext &Context = V->getContext();
1726
1727 // We have no information about target data layout, so we assume that
1728 // the maximum pointer size is 64bit.
1729 Type *MidTy = Type::getInt64Ty(Context);
1730 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
1731
1732 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
1733 }
1734
Craig Topperc6207612014-04-09 06:08:46 +00001735 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001736}
1737
1738Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
1739 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001740 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001741
1742 Type *SrcTy = C->getType();
1743 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1744 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1745 LLVMContext &Context = C->getContext();
1746
1747 // We have no information about target data layout, so we assume that
1748 // the maximum pointer size is 64bit.
1749 Type *MidTy = Type::getInt64Ty(Context);
1750
1751 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
1752 DestTy);
1753 }
1754
Craig Topperc6207612014-04-09 06:08:46 +00001755 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001756}
Manman Ren8b4306c2013-12-02 21:29:56 +00001757
1758/// Check the debug info version number, if it is out-dated, drop the debug
1759/// info. Return true if module is modified.
1760bool llvm::UpgradeDebugInfo(Module &M) {
Manman Ren2ebfb422014-01-16 01:51:12 +00001761 unsigned Version = getDebugMetadataVersionFromModule(M);
1762 if (Version == DEBUG_METADATA_VERSION)
Manman Ren8b4306c2013-12-02 21:29:56 +00001763 return false;
1764
Manman Ren2ebfb422014-01-16 01:51:12 +00001765 bool RetCode = StripDebugInfo(M);
1766 if (RetCode) {
1767 DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
1768 M.getContext().diagnose(DiagVersion);
1769 }
1770 return RetCode;
Manman Ren8b4306c2013-12-02 21:29:56 +00001771}
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001772
Manman Renb5d7ff42016-05-25 23:14:48 +00001773bool llvm::UpgradeModuleFlags(Module &M) {
1774 const NamedMDNode *ModFlags = M.getModuleFlagsMetadata();
1775 if (!ModFlags)
1776 return false;
1777
1778 bool HasObjCFlag = false, HasClassProperties = false;
1779 for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
1780 MDNode *Op = ModFlags->getOperand(I);
1781 if (Op->getNumOperands() < 2)
1782 continue;
1783 MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1784 if (!ID)
1785 continue;
1786 if (ID->getString() == "Objective-C Image Info Version")
1787 HasObjCFlag = true;
1788 if (ID->getString() == "Objective-C Class Properties")
1789 HasClassProperties = true;
1790 }
1791 // "Objective-C Class Properties" is recently added for Objective-C. We
1792 // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module
Mehdi Aminib53b62e2016-09-16 00:38:18 +00001793 // flag of value 0, so we can correclty downgrade this flag when trying to
1794 // link an ObjC bitcode without this module flag with an ObjC bitcode with
1795 // this module flag.
Manman Renb5d7ff42016-05-25 23:14:48 +00001796 if (HasObjCFlag && !HasClassProperties) {
Mehdi Aminib53b62e2016-09-16 00:38:18 +00001797 M.addModuleFlag(llvm::Module::Override, "Objective-C Class Properties",
Manman Renb5d7ff42016-05-25 23:14:48 +00001798 (uint32_t)0);
1799 return true;
1800 }
1801 return false;
1802}
1803
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00001804static bool isOldLoopArgument(Metadata *MD) {
1805 auto *T = dyn_cast_or_null<MDTuple>(MD);
1806 if (!T)
1807 return false;
1808 if (T->getNumOperands() < 1)
1809 return false;
1810 auto *S = dyn_cast_or_null<MDString>(T->getOperand(0));
1811 if (!S)
1812 return false;
1813 return S->getString().startswith("llvm.vectorizer.");
1814}
1815
1816static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) {
1817 StringRef OldPrefix = "llvm.vectorizer.";
1818 assert(OldTag.startswith(OldPrefix) && "Expected old prefix");
1819
1820 if (OldTag == "llvm.vectorizer.unroll")
1821 return MDString::get(C, "llvm.loop.interleave.count");
1822
1823 return MDString::get(
1824 C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size()))
1825 .str());
1826}
1827
1828static Metadata *upgradeLoopArgument(Metadata *MD) {
1829 auto *T = dyn_cast_or_null<MDTuple>(MD);
1830 if (!T)
1831 return MD;
1832 if (T->getNumOperands() < 1)
1833 return MD;
1834 auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0));
1835 if (!OldTag)
1836 return MD;
1837 if (!OldTag->getString().startswith("llvm.vectorizer."))
1838 return MD;
1839
1840 // This has an old tag. Upgrade it.
1841 SmallVector<Metadata *, 8> Ops;
1842 Ops.reserve(T->getNumOperands());
1843 Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString()));
1844 for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I)
1845 Ops.push_back(T->getOperand(I));
1846
1847 return MDTuple::get(T->getContext(), Ops);
1848}
1849
1850MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) {
1851 auto *T = dyn_cast<MDTuple>(&N);
1852 if (!T)
1853 return &N;
1854
David Majnemer0a16c222016-08-11 21:15:00 +00001855 if (none_of(T->operands(), isOldLoopArgument))
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00001856 return &N;
1857
1858 SmallVector<Metadata *, 8> Ops;
1859 Ops.reserve(T->getNumOperands());
1860 for (Metadata *MD : T->operands())
1861 Ops.push_back(upgradeLoopArgument(MD));
1862
1863 return MDTuple::get(T->getContext(), Ops);
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001864}