blob: 1b0d2bb87004b8979fe95ddad2d62799d08cec9d [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
Craig Toppere6915b82016-11-15 05:04:51 +0000229 // All of the intrinsics matches below should be marked with which llvm
230 // version started autoupgrading them. At some point in the future we would
231 // like to use this information to remove upgrade code for some older
232 // intrinsics. It is currently undecided how we will determine that future
233 // point.
Craig Topper5aebb862016-07-04 20:56:38 +0000234 if (IsX86 &&
Craig Toppere6915b82016-11-15 05:04:51 +0000235 (Name.startswith("sse2.pcmpeq.") || // Added in 3.1
236 Name.startswith("sse2.pcmpgt.") || // Added in 3.1
237 Name.startswith("avx2.pcmpeq.") || // Added in 3.1
238 Name.startswith("avx2.pcmpgt.") || // Added in 3.1
239 Name.startswith("avx512.mask.pcmpeq.") || // Added in 3.9
240 Name.startswith("avx512.mask.pcmpgt.") || // Added in 3.9
Craig Topper6910fa02016-11-16 05:24:10 +0000241 Name == "sse.add.ss" || // Added in 4.0
242 Name == "sse2.add.sd" || // Added in 4.0
243 Name == "sse.sub.ss" || // Added in 4.0
244 Name == "sse2.sub.sd" || // Added in 4.0
245 Name == "sse.mul.ss" || // Added in 4.0
246 Name == "sse2.mul.sd" || // Added in 4.0
247 Name == "sse.div.ss" || // Added in 4.0
248 Name == "sse2.div.sd" || // Added in 4.0
Craig Toppere6915b82016-11-15 05:04:51 +0000249 Name == "sse41.pmaxsb" || // Added in 3.9
250 Name == "sse2.pmaxs.w" || // Added in 3.9
251 Name == "sse41.pmaxsd" || // Added in 3.9
252 Name == "sse2.pmaxu.b" || // Added in 3.9
253 Name == "sse41.pmaxuw" || // Added in 3.9
254 Name == "sse41.pmaxud" || // Added in 3.9
255 Name == "sse41.pminsb" || // Added in 3.9
256 Name == "sse2.pmins.w" || // Added in 3.9
257 Name == "sse41.pminsd" || // Added in 3.9
258 Name == "sse2.pminu.b" || // Added in 3.9
259 Name == "sse41.pminuw" || // Added in 3.9
260 Name == "sse41.pminud" || // Added in 3.9
261 Name == "avx512.mask.pshuf.b.128" || // Added in 4.0
262 Name == "avx512.mask.pshuf.b.256" || // Added in 4.0
263 Name.startswith("avx2.pmax") || // Added in 3.9
264 Name.startswith("avx2.pmin") || // Added in 3.9
265 Name.startswith("avx512.mask.pmax") || // Added in 4.0
266 Name.startswith("avx512.mask.pmin") || // Added in 4.0
267 Name.startswith("avx2.vbroadcast") || // Added in 3.8
268 Name.startswith("avx2.pbroadcast") || // Added in 3.8
269 Name.startswith("avx.vpermil.") || // Added in 3.1
270 Name.startswith("sse2.pshuf") || // Added in 3.9
271 Name.startswith("avx512.pbroadcast") || // Added in 3.9
272 Name.startswith("avx512.mask.broadcast.s") || // Added in 3.9
273 Name.startswith("avx512.mask.movddup") || // Added in 3.9
274 Name.startswith("avx512.mask.movshdup") || // Added in 3.9
275 Name.startswith("avx512.mask.movsldup") || // Added in 3.9
276 Name.startswith("avx512.mask.pshuf.d.") || // Added in 3.9
277 Name.startswith("avx512.mask.pshufl.w.") || // Added in 3.9
278 Name.startswith("avx512.mask.pshufh.w.") || // Added in 3.9
279 Name.startswith("avx512.mask.shuf.p") || // Added in 4.0
280 Name.startswith("avx512.mask.vpermil.p") || // Added in 3.9
281 Name.startswith("avx512.mask.perm.df.") || // Added in 3.9
282 Name.startswith("avx512.mask.perm.di.") || // Added in 3.9
283 Name.startswith("avx512.mask.punpckl") || // Added in 3.9
284 Name.startswith("avx512.mask.punpckh") || // Added in 3.9
285 Name.startswith("avx512.mask.unpckl.") || // Added in 3.9
286 Name.startswith("avx512.mask.unpckh.") || // Added in 3.9
287 Name.startswith("avx512.mask.pand.") || // Added in 3.9
288 Name.startswith("avx512.mask.pandn.") || // Added in 3.9
289 Name.startswith("avx512.mask.por.") || // Added in 3.9
290 Name.startswith("avx512.mask.pxor.") || // Added in 3.9
291 Name.startswith("avx512.mask.and.") || // Added in 3.9
292 Name.startswith("avx512.mask.andn.") || // Added in 3.9
293 Name.startswith("avx512.mask.or.") || // Added in 3.9
294 Name.startswith("avx512.mask.xor.") || // Added in 3.9
295 Name.startswith("avx512.mask.padd.") || // Added in 4.0
296 Name.startswith("avx512.mask.psub.") || // Added in 4.0
297 Name.startswith("avx512.mask.pmull.") || // Added in 4.0
298 Name == "avx512.mask.add.pd.128" || // Added in 4.0
299 Name == "avx512.mask.add.pd.256" || // Added in 4.0
300 Name == "avx512.mask.add.ps.128" || // Added in 4.0
301 Name == "avx512.mask.add.ps.256" || // Added in 4.0
302 Name == "avx512.mask.div.pd.128" || // Added in 4.0
303 Name == "avx512.mask.div.pd.256" || // Added in 4.0
304 Name == "avx512.mask.div.ps.128" || // Added in 4.0
305 Name == "avx512.mask.div.ps.256" || // Added in 4.0
306 Name == "avx512.mask.mul.pd.128" || // Added in 4.0
307 Name == "avx512.mask.mul.pd.256" || // Added in 4.0
308 Name == "avx512.mask.mul.ps.128" || // Added in 4.0
309 Name == "avx512.mask.mul.ps.256" || // Added in 4.0
310 Name == "avx512.mask.sub.pd.128" || // Added in 4.0
311 Name == "avx512.mask.sub.pd.256" || // Added in 4.0
312 Name == "avx512.mask.sub.ps.128" || // Added in 4.0
313 Name == "avx512.mask.sub.ps.256" || // Added in 4.0
314 Name.startswith("avx512.mask.psll.d") || // Added in 4.0
315 Name.startswith("avx512.mask.psll.q") || // Added in 4.0
316 Name.startswith("avx512.mask.psll.w") || // Added in 4.0
317 Name.startswith("avx512.mask.psra.d") || // Added in 4.0
318 Name.startswith("avx512.mask.psra.q") || // Added in 4.0
319 Name.startswith("avx512.mask.psra.w") || // Added in 4.0
320 Name.startswith("avx512.mask.psrl.d") || // Added in 4.0
321 Name.startswith("avx512.mask.psrl.q") || // Added in 4.0
322 Name.startswith("avx512.mask.psrl.w") || // Added in 4.0
323 Name.startswith("avx512.mask.pslli") || // Added in 4.0
324 Name.startswith("avx512.mask.psrai") || // Added in 4.0
325 Name.startswith("avx512.mask.psrli") || // Added in 4.0
326 Name == "avx512.mask.psllv2.di" || // Added in 4.0
327 Name == "avx512.mask.psllv4.di" || // Added in 4.0
328 Name == "avx512.mask.psllv4.si" || // Added in 4.0
329 Name == "avx512.mask.psllv8.si" || // Added in 4.0
330 Name == "avx512.mask.psrav4.si" || // Added in 4.0
331 Name == "avx512.mask.psrav8.si" || // Added in 4.0
332 Name == "avx512.mask.psrlv2.di" || // Added in 4.0
333 Name == "avx512.mask.psrlv4.di" || // Added in 4.0
334 Name == "avx512.mask.psrlv4.si" || // Added in 4.0
335 Name == "avx512.mask.psrlv8.si" || // Added in 4.0
336 Name.startswith("avx512.mask.psllv.") || // Added in 4.0
337 Name.startswith("avx512.mask.psrav.") || // Added in 4.0
338 Name.startswith("avx512.mask.psrlv.") || // Added in 4.0
339 Name.startswith("sse41.pmovsx") || // Added in 3.8
340 Name.startswith("sse41.pmovzx") || // Added in 3.9
341 Name.startswith("avx2.pmovsx") || // Added in 3.9
342 Name.startswith("avx2.pmovzx") || // Added in 3.9
343 Name.startswith("avx512.mask.pmovsx") || // Added in 4.0
344 Name.startswith("avx512.mask.pmovzx") || // Added in 4.0
345 Name == "sse2.cvtdq2pd" || // Added in 3.9
346 Name == "sse2.cvtps2pd" || // Added in 3.9
347 Name == "avx.cvtdq2.pd.256" || // Added in 3.9
348 Name == "avx.cvt.ps2.pd.256" || // Added in 3.9
349 Name.startswith("avx.vinsertf128.") || // Added in 3.7
350 Name == "avx2.vinserti128" || // Added in 3.7
351 Name.startswith("avx.vextractf128.") || // Added in 3.7
352 Name == "avx2.vextracti128" || // Added in 3.7
353 Name.startswith("sse4a.movnt.") || // Added in 3.9
354 Name.startswith("avx.movnt.") || // Added in 3.2
355 Name.startswith("avx512.storent.") || // Added in 3.9
356 Name == "sse2.storel.dq" || // Added in 3.9
357 Name.startswith("sse.storeu.") || // Added in 3.9
358 Name.startswith("sse2.storeu.") || // Added in 3.9
359 Name.startswith("avx.storeu.") || // Added in 3.9
360 Name.startswith("avx512.mask.storeu.") || // Added in 3.9
361 Name.startswith("avx512.mask.store.p") || // Added in 3.9
362 Name.startswith("avx512.mask.store.b.") || // Added in 3.9
363 Name.startswith("avx512.mask.store.w.") || // Added in 3.9
364 Name.startswith("avx512.mask.store.d.") || // Added in 3.9
365 Name.startswith("avx512.mask.store.q.") || // Added in 3.9
366 Name.startswith("avx512.mask.loadu.") || // Added in 3.9
367 Name.startswith("avx512.mask.load.") || // Added in 3.9
368 Name == "sse42.crc32.64.8" || // Added in 3.4
369 Name.startswith("avx.vbroadcast.s") || // Added in 3.5
370 Name.startswith("avx512.mask.palignr.") || // Added in 3.9
371 Name.startswith("sse2.psll.dq") || // Added in 3.7
372 Name.startswith("sse2.psrl.dq") || // Added in 3.7
373 Name.startswith("avx2.psll.dq") || // Added in 3.7
374 Name.startswith("avx2.psrl.dq") || // Added in 3.7
375 Name.startswith("avx512.psll.dq") || // Added in 3.9
376 Name.startswith("avx512.psrl.dq") || // Added in 3.9
377 Name == "sse41.pblendw" || // Added in 3.7
378 Name.startswith("sse41.blendp") || // Added in 3.7
379 Name.startswith("avx.blend.p") || // Added in 3.7
380 Name == "avx2.pblendw" || // Added in 3.7
381 Name.startswith("avx2.pblendd.") || // Added in 3.7
382 Name.startswith("avx.vbroadcastf128") || // Added in 4.0
383 Name == "avx2.vbroadcasti128" || // Added in 3.7
384 Name == "xop.vpcmov" || // Added in 3.8
385 (Name.startswith("xop.vpcom") && // Added in 3.2
386 F->arg_size() == 2))) {
Craig Topperc6207612014-04-09 06:08:46 +0000387 NewFn = nullptr;
Craig Topper3b1817d2012-02-03 06:10:55 +0000388 return true;
389 }
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000390 // SSE4.1 ptest functions may have an old signature.
Craig Toppere6915b82016-11-15 05:04:51 +0000391 if (IsX86 && Name.startswith("sse41.ptest")) { // Added in 3.2
Craig Topper5aebb862016-07-04 20:56:38 +0000392 if (Name.substr(11) == "c")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000393 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000394 if (Name.substr(11) == "z")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000395 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
Craig Topper5aebb862016-07-04 20:56:38 +0000396 if (Name.substr(11) == "nzc")
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000397 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
398 }
Sanjay Patel1c3eaec2015-02-28 22:25:06 +0000399 // Several blend and other instructions with masks used the wrong number of
Chandler Carruth373b2b12014-09-06 10:00:01 +0000400 // bits.
Craig Toppere6915b82016-11-15 05:04:51 +0000401 if (IsX86 && Name == "sse41.insertps") // Added in 3.6
Chandler Carruth373b2b12014-09-06 10:00:01 +0000402 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
403 NewFn);
Craig Toppere6915b82016-11-15 05:04:51 +0000404 if (IsX86 && Name == "sse41.dppd") // Added in 3.6
Chandler Carruth373b2b12014-09-06 10:00:01 +0000405 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
406 NewFn);
Craig Toppere6915b82016-11-15 05:04:51 +0000407 if (IsX86 && Name == "sse41.dpps") // Added in 3.6
Chandler Carruth373b2b12014-09-06 10:00:01 +0000408 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
409 NewFn);
Craig Toppere6915b82016-11-15 05:04:51 +0000410 if (IsX86 && Name == "sse41.mpsadbw") // Added in 3.6
Chandler Carruth373b2b12014-09-06 10:00:01 +0000411 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
412 NewFn);
Craig Toppere6915b82016-11-15 05:04:51 +0000413 if (IsX86 && Name == "avx.dp.ps.256") // Added in 3.6
Chandler Carruth373b2b12014-09-06 10:00:01 +0000414 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
415 NewFn);
Craig Toppere6915b82016-11-15 05:04:51 +0000416 if (IsX86 && Name == "avx2.mpsadbw") // Added in 3.6
Chandler Carruth373b2b12014-09-06 10:00:01 +0000417 return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
418 NewFn);
Craig Topper29f2e952015-01-25 23:26:02 +0000419
Craig Toppere6915b82016-11-15 05:04:51 +0000420 // frcz.ss/sd may need to have an argument dropped. Added in 3.2
Craig Topper5aebb862016-07-04 20:56:38 +0000421 if (IsX86 && Name.startswith("xop.vfrcz.ss") && F->arg_size() == 2) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000422 rename(F);
Craig Topper71dc02d2012-06-13 07:18:53 +0000423 NewFn = Intrinsic::getDeclaration(F->getParent(),
424 Intrinsic::x86_xop_vfrcz_ss);
425 return true;
426 }
Craig Topper5aebb862016-07-04 20:56:38 +0000427 if (IsX86 && Name.startswith("xop.vfrcz.sd") && F->arg_size() == 2) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000428 rename(F);
Craig Topper71dc02d2012-06-13 07:18:53 +0000429 NewFn = Intrinsic::getDeclaration(F->getParent(),
430 Intrinsic::x86_xop_vfrcz_sd);
431 return true;
432 }
Simon Pilgrime85506b2016-06-03 08:06:03 +0000433 // Upgrade any XOP PERMIL2 index operand still using a float/double vector.
Craig Toppere6915b82016-11-15 05:04:51 +0000434 if (IsX86 && Name.startswith("xop.vpermil2")) { // Added in 3.9
Simon Pilgrime85506b2016-06-03 08:06:03 +0000435 auto Params = F->getFunctionType()->params();
436 auto Idx = Params[2];
437 if (Idx->getScalarType()->isFloatingPointTy()) {
Rafael Espindolad7325ee2016-10-03 15:51:42 +0000438 rename(F);
Simon Pilgrime85506b2016-06-03 08:06:03 +0000439 unsigned IdxSize = Idx->getPrimitiveSizeInBits();
440 unsigned EltSize = Idx->getScalarSizeInBits();
441 Intrinsic::ID Permil2ID;
442 if (EltSize == 64 && IdxSize == 128)
443 Permil2ID = Intrinsic::x86_xop_vpermil2pd;
444 else if (EltSize == 32 && IdxSize == 128)
445 Permil2ID = Intrinsic::x86_xop_vpermil2ps;
446 else if (EltSize == 64 && IdxSize == 256)
447 Permil2ID = Intrinsic::x86_xop_vpermil2pd_256;
448 else
449 Permil2ID = Intrinsic::x86_xop_vpermil2ps_256;
450 NewFn = Intrinsic::getDeclaration(F->getParent(), Permil2ID);
451 return true;
452 }
453 }
Craig Topper3b1817d2012-02-03 06:10:55 +0000454 break;
455 }
Chris Lattnerb372f662011-06-18 18:56:39 +0000456 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000457
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000458 // This may not belong here. This function is effectively being overloaded
459 // to both detect an intrinsic which needs upgrading, and to provide the
460 // upgraded form of the intrinsic. We should perhaps have two separate
Chandler Carruth7132e002007-08-04 01:51:18 +0000461 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000462 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000463}
464
Evan Cheng0e179d02007-12-17 22:33:23 +0000465bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
Craig Topperc6207612014-04-09 06:08:46 +0000466 NewFn = nullptr;
Evan Cheng0e179d02007-12-17 22:33:23 +0000467 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Filipe Cabecinhas0011c582015-07-03 20:12:01 +0000468 assert(F != NewFn && "Intrinsic function upgraded to the same function");
Duncan Sands38ef3a82007-12-03 20:06:50 +0000469
470 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000471 if (NewFn)
472 F = NewFn;
Pete Cooper9e1d3352015-05-20 17:16:39 +0000473 if (Intrinsic::ID id = F->getIntrinsicID())
474 F->setAttributes(Intrinsic::getAttributes(F->getContext(), id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000475 return Upgraded;
476}
477
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000478bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000479 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000480 return false;
481}
482
Simon Pilgrimf7186822016-06-09 21:09:03 +0000483// Handles upgrading SSE2/AVX2/AVX512BW PSLLDQ intrinsics by converting them
Craig Topperb324e432015-02-18 06:24:44 +0000484// to byte shuffles.
Craig Topper46b34fe2016-07-12 01:42:33 +0000485static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder,
Craig Topper7355ac32016-05-29 06:37:33 +0000486 Value *Op, unsigned Shift) {
487 Type *ResultTy = Op->getType();
488 unsigned NumElts = ResultTy->getVectorNumElements() * 8;
Craig Topperb324e432015-02-18 06:24:44 +0000489
490 // Bitcast from a 64-bit element type to a byte element type.
Craig Topper46b34fe2016-07-12 01:42:33 +0000491 Type *VecTy = VectorType::get(Builder.getInt8Ty(), NumElts);
Craig Topper7355ac32016-05-29 06:37:33 +0000492 Op = Builder.CreateBitCast(Op, VecTy, "cast");
493
Craig Topperb324e432015-02-18 06:24:44 +0000494 // We'll be shuffling in zeroes.
Craig Topper7355ac32016-05-29 06:37:33 +0000495 Value *Res = Constant::getNullValue(VecTy);
Craig Topperb324e432015-02-18 06:24:44 +0000496
497 // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
498 // we'll just return the zero vector.
499 if (Shift < 16) {
Craig Topper99d1eab2016-06-12 00:41:19 +0000500 uint32_t Idxs[64];
Simon Pilgrimf7186822016-06-09 21:09:03 +0000501 // 256/512-bit version is split into 2/4 16-byte lanes.
Craig Topperb324e432015-02-18 06:24:44 +0000502 for (unsigned l = 0; l != NumElts; l += 16)
503 for (unsigned i = 0; i != 16; ++i) {
504 unsigned Idx = NumElts + i - Shift;
505 if (Idx < NumElts)
506 Idx -= NumElts - 16; // end of lane, switch operand.
Craig Topper7355ac32016-05-29 06:37:33 +0000507 Idxs[l + i] = Idx + l;
Craig Topperb324e432015-02-18 06:24:44 +0000508 }
509
Craig Topper7355ac32016-05-29 06:37:33 +0000510 Res = Builder.CreateShuffleVector(Res, Op, makeArrayRef(Idxs, NumElts));
Craig Topperb324e432015-02-18 06:24:44 +0000511 }
512
513 // Bitcast back to a 64-bit element type.
Craig Topper7355ac32016-05-29 06:37:33 +0000514 return Builder.CreateBitCast(Res, ResultTy, "cast");
Craig Topperb324e432015-02-18 06:24:44 +0000515}
516
Craig Topperea703ae2016-06-13 02:36:42 +0000517// Handles upgrading SSE2/AVX2/AVX512BW PSRLDQ intrinsics by converting them
518// to byte shuffles.
Craig Topper46b34fe2016-07-12 01:42:33 +0000519static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, Value *Op,
Craig Topperea703ae2016-06-13 02:36:42 +0000520 unsigned Shift) {
521 Type *ResultTy = Op->getType();
522 unsigned NumElts = ResultTy->getVectorNumElements() * 8;
523
524 // Bitcast from a 64-bit element type to a byte element type.
Craig Topper46b34fe2016-07-12 01:42:33 +0000525 Type *VecTy = VectorType::get(Builder.getInt8Ty(), NumElts);
Craig Topperea703ae2016-06-13 02:36:42 +0000526 Op = Builder.CreateBitCast(Op, VecTy, "cast");
527
528 // We'll be shuffling in zeroes.
529 Value *Res = Constant::getNullValue(VecTy);
530
531 // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
532 // we'll just return the zero vector.
533 if (Shift < 16) {
534 uint32_t Idxs[64];
535 // 256/512-bit version is split into 2/4 16-byte lanes.
536 for (unsigned l = 0; l != NumElts; l += 16)
537 for (unsigned i = 0; i != 16; ++i) {
538 unsigned Idx = i + Shift;
539 if (Idx >= 16)
540 Idx += NumElts - 16; // end of lane, switch operand.
541 Idxs[l + i] = Idx + l;
542 }
543
544 Res = Builder.CreateShuffleVector(Op, Res, makeArrayRef(Idxs, NumElts));
545 }
546
547 // Bitcast back to a 64-bit element type.
548 return Builder.CreateBitCast(Res, ResultTy, "cast");
549}
550
551static Value *getX86MaskVec(IRBuilder<> &Builder, Value *Mask,
552 unsigned NumElts) {
553 llvm::VectorType *MaskTy = llvm::VectorType::get(Builder.getInt1Ty(),
554 cast<IntegerType>(Mask->getType())->getBitWidth());
555 Mask = Builder.CreateBitCast(Mask, MaskTy);
556
557 // If we have less than 8 elements, then the starting mask was an i8 and
558 // we need to extract down to the right number of elements.
559 if (NumElts < 8) {
560 uint32_t Indices[4];
561 for (unsigned i = 0; i != NumElts; ++i)
562 Indices[i] = i;
563 Mask = Builder.CreateShuffleVector(Mask, Mask,
564 makeArrayRef(Indices, NumElts),
565 "extract");
566 }
567
568 return Mask;
569}
570
571static Value *EmitX86Select(IRBuilder<> &Builder, Value *Mask,
572 Value *Op0, Value *Op1) {
573 // If the mask is all ones just emit the align operation.
574 if (const auto *C = dyn_cast<Constant>(Mask))
575 if (C->isAllOnesValue())
576 return Op0;
577
578 Mask = getX86MaskVec(Builder, Mask, Op0->getType()->getVectorNumElements());
579 return Builder.CreateSelect(Mask, Op0, Op1);
580}
581
Craig Topper46b34fe2016-07-12 01:42:33 +0000582static Value *UpgradeX86PALIGNRIntrinsics(IRBuilder<> &Builder,
Craig Topper33350cc2016-06-06 06:12:54 +0000583 Value *Op0, Value *Op1, Value *Shift,
584 Value *Passthru, Value *Mask) {
585 unsigned ShiftVal = cast<llvm::ConstantInt>(Shift)->getZExtValue();
586
587 unsigned NumElts = Op0->getType()->getVectorNumElements();
588 assert(NumElts % 16 == 0);
589
590 // If palignr is shifting the pair of vectors more than the size of two
591 // lanes, emit zero.
592 if (ShiftVal >= 32)
593 return llvm::Constant::getNullValue(Op0->getType());
594
595 // If palignr is shifting the pair of input vectors more than one lane,
596 // but less than two lanes, convert to shifting in zeroes.
597 if (ShiftVal > 16) {
598 ShiftVal -= 16;
599 Op1 = Op0;
600 Op0 = llvm::Constant::getNullValue(Op0->getType());
601 }
602
Craig Topper99d1eab2016-06-12 00:41:19 +0000603 uint32_t Indices[64];
Craig Topper33350cc2016-06-06 06:12:54 +0000604 // 256-bit palignr operates on 128-bit lanes so we need to handle that
605 for (unsigned l = 0; l != NumElts; l += 16) {
606 for (unsigned i = 0; i != 16; ++i) {
607 unsigned Idx = ShiftVal + i;
608 if (Idx >= 16)
609 Idx += NumElts - 16; // End of lane, switch operand.
610 Indices[l + i] = Idx + l;
611 }
612 }
613
614 Value *Align = Builder.CreateShuffleVector(Op1, Op0,
615 makeArrayRef(Indices, NumElts),
616 "palignr");
617
Craig Topperea703ae2016-06-13 02:36:42 +0000618 return EmitX86Select(Builder, Mask, Align, Passthru);
Craig Topperb324e432015-02-18 06:24:44 +0000619}
620
Craig Topper46b34fe2016-07-12 01:42:33 +0000621static Value *UpgradeMaskedStore(IRBuilder<> &Builder,
Craig Topper50f85c22016-05-31 01:50:02 +0000622 Value *Ptr, Value *Data, Value *Mask,
623 bool Aligned) {
624 // Cast the pointer to the right type.
625 Ptr = Builder.CreateBitCast(Ptr,
626 llvm::PointerType::getUnqual(Data->getType()));
627 unsigned Align =
628 Aligned ? cast<VectorType>(Data->getType())->getBitWidth() / 8 : 1;
629
630 // If the mask is all ones just emit a regular store.
631 if (const auto *C = dyn_cast<Constant>(Mask))
632 if (C->isAllOnesValue())
633 return Builder.CreateAlignedStore(Data, Ptr, Align);
634
635 // Convert the mask from an integer type to a vector of i1.
636 unsigned NumElts = Data->getType()->getVectorNumElements();
Craig Topperea703ae2016-06-13 02:36:42 +0000637 Mask = getX86MaskVec(Builder, Mask, NumElts);
Craig Topper50f85c22016-05-31 01:50:02 +0000638 return Builder.CreateMaskedStore(Data, Ptr, Align, Mask);
639}
640
Craig Topper46b34fe2016-07-12 01:42:33 +0000641static Value *UpgradeMaskedLoad(IRBuilder<> &Builder,
Craig Topperf10fbfa2016-06-02 04:19:36 +0000642 Value *Ptr, Value *Passthru, Value *Mask,
643 bool Aligned) {
644 // Cast the pointer to the right type.
645 Ptr = Builder.CreateBitCast(Ptr,
646 llvm::PointerType::getUnqual(Passthru->getType()));
647 unsigned Align =
648 Aligned ? cast<VectorType>(Passthru->getType())->getBitWidth() / 8 : 1;
649
650 // If the mask is all ones just emit a regular store.
651 if (const auto *C = dyn_cast<Constant>(Mask))
652 if (C->isAllOnesValue())
653 return Builder.CreateAlignedLoad(Ptr, Align);
654
655 // Convert the mask from an integer type to a vector of i1.
656 unsigned NumElts = Passthru->getType()->getVectorNumElements();
Craig Topperea703ae2016-06-13 02:36:42 +0000657 Mask = getX86MaskVec(Builder, Mask, NumElts);
Craig Topperf10fbfa2016-06-02 04:19:36 +0000658 return Builder.CreateMaskedLoad(Ptr, Align, Mask, Passthru);
659}
660
Sanjay Patel51ab7572016-06-16 15:48:30 +0000661static Value *upgradeIntMinMax(IRBuilder<> &Builder, CallInst &CI,
662 ICmpInst::Predicate Pred) {
663 Value *Op0 = CI.getArgOperand(0);
664 Value *Op1 = CI.getArgOperand(1);
665 Value *Cmp = Builder.CreateICmp(Pred, Op0, Op1);
Craig Topper8ec5c732016-10-24 04:04:16 +0000666 Value *Res = Builder.CreateSelect(Cmp, Op0, Op1);
667
668 if (CI.getNumArgOperands() == 4)
669 Res = EmitX86Select(Builder, CI.getArgOperand(3), Res, CI.getArgOperand(2));
670
671 return Res;
Sanjay Patel51ab7572016-06-16 15:48:30 +0000672}
673
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000674static Value *upgradeMaskedCompare(IRBuilder<> &Builder, CallInst &CI,
675 ICmpInst::Predicate Pred) {
676 Value *Op0 = CI.getArgOperand(0);
677 unsigned NumElts = Op0->getType()->getVectorNumElements();
678 Value *Cmp = Builder.CreateICmp(Pred, Op0, CI.getArgOperand(1));
679
680 Value *Mask = CI.getArgOperand(2);
681 const auto *C = dyn_cast<Constant>(Mask);
682 if (!C || !C->isAllOnesValue())
683 Cmp = Builder.CreateAnd(Cmp, getX86MaskVec(Builder, Mask, NumElts));
684
685 if (NumElts < 8) {
686 uint32_t Indices[8];
687 for (unsigned i = 0; i != NumElts; ++i)
688 Indices[i] = i;
689 for (unsigned i = NumElts; i != 8; ++i)
Craig Topperd5d2a352016-07-07 06:11:07 +0000690 Indices[i] = NumElts + i % NumElts;
691 Cmp = Builder.CreateShuffleVector(Cmp,
692 Constant::getNullValue(Cmp->getType()),
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000693 Indices);
694 }
695 return Builder.CreateBitCast(Cmp, IntegerType::get(CI.getContext(),
696 std::max(NumElts, 8U)));
697}
698
Craig Topperc9467ed2016-11-06 16:29:08 +0000699// Replace a masked intrinsic with an older unmasked intrinsic.
700static Value *UpgradeX86MaskedShift(IRBuilder<> &Builder, CallInst &CI,
701 Intrinsic::ID IID) {
702 Function *F = CI.getCalledFunction();
703 Function *Intrin = Intrinsic::getDeclaration(F->getParent(), IID);
704 Value *Rep = Builder.CreateCall(Intrin,
705 { CI.getArgOperand(0), CI.getArgOperand(1) });
706 return EmitX86Select(Builder, CI.getArgOperand(3), Rep, CI.getArgOperand(2));
707}
708
709
Sanjay Patel595098f2016-06-15 22:01:28 +0000710/// Upgrade a call to an old intrinsic. All argument and return casting must be
711/// provided to seamlessly integrate with existing context.
Chandler Carruth7132e002007-08-04 01:51:18 +0000712void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000713 Function *F = CI->getCalledFunction();
Nick Lewycky2eb3ade2011-12-12 22:59:34 +0000714 LLVMContext &C = CI->getContext();
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000715 IRBuilder<> Builder(C);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000716 Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000717
Craig Topper3b1817d2012-02-03 06:10:55 +0000718 assert(F && "Intrinsic call is not direct?");
719
720 if (!NewFn) {
721 // Get the Function's name.
722 StringRef Name = F->getName();
723
Craig Topper5aebb862016-07-04 20:56:38 +0000724 assert(Name.startswith("llvm.") && "Intrinsic doesn't start with 'llvm.'");
725 Name = Name.substr(5);
726
727 bool IsX86 = Name.startswith("x86.");
728 if (IsX86)
729 Name = Name.substr(4);
730
Craig Topper3b1817d2012-02-03 06:10:55 +0000731 Value *Rep;
Sanjay Patel595098f2016-06-15 22:01:28 +0000732 // Upgrade packed integer vector compare intrinsics to compare instructions.
Craig Topper5aebb862016-07-04 20:56:38 +0000733 if (IsX86 && (Name.startswith("sse2.pcmpeq.") ||
734 Name.startswith("avx2.pcmpeq."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000735 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
736 "pcmpeq");
Craig Topper3b1817d2012-02-03 06:10:55 +0000737 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper5aebb862016-07-04 20:56:38 +0000738 } else if (IsX86 && (Name.startswith("sse2.pcmpgt.") ||
739 Name.startswith("avx2.pcmpgt."))) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000740 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
741 "pcmpgt");
Craig Topper3b1817d2012-02-03 06:10:55 +0000742 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper6910fa02016-11-16 05:24:10 +0000743 } else if (IsX86 && (Name == "sse.add.ss" || Name == "sse2.add.sd")) {
744 Type *I32Ty = Type::getInt32Ty(C);
745 Value *Elt0 = Builder.CreateExtractElement(CI->getArgOperand(0),
746 ConstantInt::get(I32Ty, 0));
747 Value *Elt1 = Builder.CreateExtractElement(CI->getArgOperand(1),
748 ConstantInt::get(I32Ty, 0));
749 Rep = Builder.CreateInsertElement(CI->getArgOperand(0),
750 Builder.CreateFAdd(Elt0, Elt1),
751 ConstantInt::get(I32Ty, 0));
752 } else if (IsX86 && (Name == "sse.sub.ss" || Name == "sse2.sub.sd")) {
753 Type *I32Ty = Type::getInt32Ty(C);
754 Value *Elt0 = Builder.CreateExtractElement(CI->getArgOperand(0),
755 ConstantInt::get(I32Ty, 0));
756 Value *Elt1 = Builder.CreateExtractElement(CI->getArgOperand(1),
757 ConstantInt::get(I32Ty, 0));
758 Rep = Builder.CreateInsertElement(CI->getArgOperand(0),
759 Builder.CreateFSub(Elt0, Elt1),
760 ConstantInt::get(I32Ty, 0));
761 } else if (IsX86 && (Name == "sse.mul.ss" || Name == "sse2.mul.sd")) {
762 Type *I32Ty = Type::getInt32Ty(C);
763 Value *Elt0 = Builder.CreateExtractElement(CI->getArgOperand(0),
764 ConstantInt::get(I32Ty, 0));
765 Value *Elt1 = Builder.CreateExtractElement(CI->getArgOperand(1),
766 ConstantInt::get(I32Ty, 0));
767 Rep = Builder.CreateInsertElement(CI->getArgOperand(0),
768 Builder.CreateFMul(Elt0, Elt1),
769 ConstantInt::get(I32Ty, 0));
770 } else if (IsX86 && (Name == "sse.div.ss" || Name == "sse2.div.sd")) {
771 Type *I32Ty = Type::getInt32Ty(C);
772 Value *Elt0 = Builder.CreateExtractElement(CI->getArgOperand(0),
773 ConstantInt::get(I32Ty, 0));
774 Value *Elt1 = Builder.CreateExtractElement(CI->getArgOperand(1),
775 ConstantInt::get(I32Ty, 0));
776 Rep = Builder.CreateInsertElement(CI->getArgOperand(0),
777 Builder.CreateFDiv(Elt0, Elt1),
778 ConstantInt::get(I32Ty, 0));
Craig Topper5aebb862016-07-04 20:56:38 +0000779 } else if (IsX86 && Name.startswith("avx512.mask.pcmpeq.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000780 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_EQ);
Craig Topper5aebb862016-07-04 20:56:38 +0000781 } else if (IsX86 && Name.startswith("avx512.mask.pcmpgt.")) {
Craig Topper0a0fb0f2016-06-21 03:53:24 +0000782 Rep = upgradeMaskedCompare(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000783 } else if (IsX86 && (Name == "sse41.pmaxsb" ||
784 Name == "sse2.pmaxs.w" ||
785 Name == "sse41.pmaxsd" ||
Craig Topper8ec5c732016-10-24 04:04:16 +0000786 Name.startswith("avx2.pmaxs") ||
787 Name.startswith("avx512.mask.pmaxs"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000788 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000789 } else if (IsX86 && (Name == "sse2.pmaxu.b" ||
790 Name == "sse41.pmaxuw" ||
791 Name == "sse41.pmaxud" ||
Craig Topper8ec5c732016-10-24 04:04:16 +0000792 Name.startswith("avx2.pmaxu") ||
793 Name.startswith("avx512.mask.pmaxu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000794 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_UGT);
Craig Topper5aebb862016-07-04 20:56:38 +0000795 } else if (IsX86 && (Name == "sse41.pminsb" ||
796 Name == "sse2.pmins.w" ||
797 Name == "sse41.pminsd" ||
Craig Topper8ec5c732016-10-24 04:04:16 +0000798 Name.startswith("avx2.pmins") ||
799 Name.startswith("avx512.mask.pmins"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000800 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SLT);
Craig Topper5aebb862016-07-04 20:56:38 +0000801 } else if (IsX86 && (Name == "sse2.pminu.b" ||
802 Name == "sse41.pminuw" ||
803 Name == "sse41.pminud" ||
Craig Topper8ec5c732016-10-24 04:04:16 +0000804 Name.startswith("avx2.pminu") ||
805 Name.startswith("avx512.mask.pminu"))) {
Sanjay Patel51ab7572016-06-16 15:48:30 +0000806 Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_ULT);
Craig Topper5aebb862016-07-04 20:56:38 +0000807 } else if (IsX86 && (Name == "sse2.cvtdq2pd" ||
808 Name == "sse2.cvtps2pd" ||
809 Name == "avx.cvtdq2.pd.256" ||
810 Name == "avx.cvt.ps2.pd.256")) {
Simon Pilgrim4298d062016-05-25 08:59:18 +0000811 // Lossless i32/float to double conversion.
812 // Extract the bottom elements if necessary and convert to double vector.
813 Value *Src = CI->getArgOperand(0);
814 VectorType *SrcTy = cast<VectorType>(Src->getType());
815 VectorType *DstTy = cast<VectorType>(CI->getType());
816 Rep = CI->getArgOperand(0);
817
818 unsigned NumDstElts = DstTy->getNumElements();
819 if (NumDstElts < SrcTy->getNumElements()) {
820 assert(NumDstElts == 2 && "Unexpected vector size");
Craig Topper99d1eab2016-06-12 00:41:19 +0000821 uint32_t ShuffleMask[2] = { 0, 1 };
822 Rep = Builder.CreateShuffleVector(Rep, UndefValue::get(SrcTy),
823 ShuffleMask);
Simon Pilgrim4298d062016-05-25 08:59:18 +0000824 }
825
826 bool Int2Double = (StringRef::npos != Name.find("cvtdq2"));
827 if (Int2Double)
828 Rep = Builder.CreateSIToFP(Rep, DstTy, "cvtdq2pd");
829 else
830 Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd");
Craig Topper5aebb862016-07-04 20:56:38 +0000831 } else if (IsX86 && Name.startswith("sse4a.movnt.")) {
Simon Pilgrimf4b2af12016-06-18 02:38:26 +0000832 Module *M = F->getParent();
833 SmallVector<Metadata *, 1> Elts;
834 Elts.push_back(
835 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
836 MDNode *Node = MDNode::get(C, Elts);
837
838 Value *Arg0 = CI->getArgOperand(0);
839 Value *Arg1 = CI->getArgOperand(1);
840
841 // Nontemporal (unaligned) store of the 0'th element of the float/double
842 // vector.
843 Type *SrcEltTy = cast<VectorType>(Arg1->getType())->getElementType();
844 PointerType *EltPtrTy = PointerType::getUnqual(SrcEltTy);
845 Value *Addr = Builder.CreateBitCast(Arg0, EltPtrTy, "cast");
846 Value *Extract =
847 Builder.CreateExtractElement(Arg1, (uint64_t)0, "extractelement");
848
849 StoreInst *SI = Builder.CreateAlignedStore(Extract, Addr, 1);
850 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
851
852 // Remove intrinsic.
853 CI->eraseFromParent();
854 return;
Craig Topper70610cf2016-07-09 04:38:27 +0000855 } else if (IsX86 && (Name.startswith("avx.movnt.") ||
856 Name.startswith("avx512.storent."))) {
Craig Topper7daf8972012-05-08 06:58:15 +0000857 Module *M = F->getParent();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000858 SmallVector<Metadata *, 1> Elts;
859 Elts.push_back(
860 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
Craig Topper7daf8972012-05-08 06:58:15 +0000861 MDNode *Node = MDNode::get(C, Elts);
862
863 Value *Arg0 = CI->getArgOperand(0);
864 Value *Arg1 = CI->getArgOperand(1);
865
866 // Convert the type of the pointer to a pointer to the stored type.
867 Value *BC = Builder.CreateBitCast(Arg0,
868 PointerType::getUnqual(Arg1->getType()),
869 "cast");
Craig Topper70610cf2016-07-09 04:38:27 +0000870 VectorType *VTy = cast<VectorType>(Arg1->getType());
871 StoreInst *SI = Builder.CreateAlignedStore(Arg1, BC,
872 VTy->getBitWidth() / 8);
Craig Topper7daf8972012-05-08 06:58:15 +0000873 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
Craig Topper7daf8972012-05-08 06:58:15 +0000874
875 // Remove intrinsic.
876 CI->eraseFromParent();
877 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000878 } else if (IsX86 && Name == "sse2.storel.dq") {
Craig Topper12e322a2016-05-25 06:56:32 +0000879 Value *Arg0 = CI->getArgOperand(0);
880 Value *Arg1 = CI->getArgOperand(1);
881
882 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
883 Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
884 Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0);
885 Value *BC = Builder.CreateBitCast(Arg0,
886 PointerType::getUnqual(Elt->getType()),
887 "cast");
Craig Topper29ce55d2016-05-30 22:54:12 +0000888 Builder.CreateAlignedStore(Elt, BC, 1);
Craig Topper12e322a2016-05-25 06:56:32 +0000889
890 // Remove intrinsic.
891 CI->eraseFromParent();
892 return;
Craig Topper5aebb862016-07-04 20:56:38 +0000893 } else if (IsX86 && (Name.startswith("sse.storeu.") ||
894 Name.startswith("sse2.storeu.") ||
895 Name.startswith("avx.storeu."))) {
Craig Topper8287fd82016-05-30 23:15:56 +0000896 Value *Arg0 = CI->getArgOperand(0);
897 Value *Arg1 = CI->getArgOperand(1);
898
899 Arg0 = Builder.CreateBitCast(Arg0,
900 PointerType::getUnqual(Arg1->getType()),
901 "cast");
902 Builder.CreateAlignedStore(Arg1, Arg0, 1);
903
904 // Remove intrinsic.
905 CI->eraseFromParent();
906 return;
Craig Toppera57d2ca2016-09-03 23:55:13 +0000907 } else if (IsX86 && (Name.startswith("avx512.mask.storeu."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000908 UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
Craig Topper50f85c22016-05-31 01:50:02 +0000909 CI->getArgOperand(2), /*Aligned*/false);
910
911 // Remove intrinsic.
912 CI->eraseFromParent();
913 return;
Craig Toppera57d2ca2016-09-03 23:55:13 +0000914 } else if (IsX86 && (Name.startswith("avx512.mask.store."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000915 UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
Craig Topper50f85c22016-05-31 01:50:02 +0000916 CI->getArgOperand(2), /*Aligned*/true);
917
918 // Remove intrinsic.
919 CI->eraseFromParent();
920 return;
Craig Toppera57d2ca2016-09-03 23:55:13 +0000921 } else if (IsX86 && (Name.startswith("avx512.mask.loadu."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000922 Rep = UpgradeMaskedLoad(Builder, CI->getArgOperand(0),
Craig Topperf10fbfa2016-06-02 04:19:36 +0000923 CI->getArgOperand(1), CI->getArgOperand(2),
924 /*Aligned*/false);
Craig Toppera57d2ca2016-09-03 23:55:13 +0000925 } else if (IsX86 && (Name.startswith("avx512.mask.load."))) {
Craig Topper46b34fe2016-07-12 01:42:33 +0000926 Rep = UpgradeMaskedLoad(Builder, CI->getArgOperand(0),
Craig Topperf10fbfa2016-06-02 04:19:36 +0000927 CI->getArgOperand(1),CI->getArgOperand(2),
928 /*Aligned*/true);
Craig Topper5aebb862016-07-04 20:56:38 +0000929 } else if (IsX86 && Name.startswith("xop.vpcom")) {
Craig Topper3352ba52012-06-09 16:46:13 +0000930 Intrinsic::ID intID;
931 if (Name.endswith("ub"))
932 intID = Intrinsic::x86_xop_vpcomub;
933 else if (Name.endswith("uw"))
934 intID = Intrinsic::x86_xop_vpcomuw;
935 else if (Name.endswith("ud"))
936 intID = Intrinsic::x86_xop_vpcomud;
937 else if (Name.endswith("uq"))
938 intID = Intrinsic::x86_xop_vpcomuq;
939 else if (Name.endswith("b"))
940 intID = Intrinsic::x86_xop_vpcomb;
941 else if (Name.endswith("w"))
942 intID = Intrinsic::x86_xop_vpcomw;
943 else if (Name.endswith("d"))
944 intID = Intrinsic::x86_xop_vpcomd;
945 else if (Name.endswith("q"))
946 intID = Intrinsic::x86_xop_vpcomq;
947 else
948 llvm_unreachable("Unknown suffix");
949
Craig Topper5aebb862016-07-04 20:56:38 +0000950 Name = Name.substr(9); // strip off "xop.vpcom"
Craig Topper3352ba52012-06-09 16:46:13 +0000951 unsigned Imm;
952 if (Name.startswith("lt"))
953 Imm = 0;
954 else if (Name.startswith("le"))
955 Imm = 1;
956 else if (Name.startswith("gt"))
957 Imm = 2;
958 else if (Name.startswith("ge"))
959 Imm = 3;
960 else if (Name.startswith("eq"))
961 Imm = 4;
962 else if (Name.startswith("ne"))
963 Imm = 5;
Craig Topper3352ba52012-06-09 16:46:13 +0000964 else if (Name.startswith("false"))
Craig Toppere32546d2015-02-13 07:42:15 +0000965 Imm = 6;
966 else if (Name.startswith("true"))
Craig Topper3352ba52012-06-09 16:46:13 +0000967 Imm = 7;
968 else
969 llvm_unreachable("Unknown condition");
970
971 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
David Blaikieff6409d2015-05-18 22:13:54 +0000972 Rep =
973 Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1),
974 Builder.getInt8(Imm)});
Craig Topper5aebb862016-07-04 20:56:38 +0000975 } else if (IsX86 && Name == "xop.vpcmov") {
Simon Pilgrime88dc042015-11-03 20:27:01 +0000976 Value *Arg0 = CI->getArgOperand(0);
977 Value *Arg1 = CI->getArgOperand(1);
978 Value *Sel = CI->getArgOperand(2);
979 unsigned NumElts = CI->getType()->getVectorNumElements();
980 Constant *MinusOne = ConstantVector::getSplat(NumElts, Builder.getInt64(-1));
981 Value *NotSel = Builder.CreateXor(Sel, MinusOne);
982 Value *Sel0 = Builder.CreateAnd(Arg0, Sel);
983 Value *Sel1 = Builder.CreateAnd(Arg1, NotSel);
984 Rep = Builder.CreateOr(Sel0, Sel1);
Craig Topper5aebb862016-07-04 20:56:38 +0000985 } else if (IsX86 && Name == "sse42.crc32.64.8") {
Craig Topperef9e9932013-10-15 05:20:47 +0000986 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
987 Intrinsic::x86_sse42_crc32_32_8);
988 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
David Blaikieff6409d2015-05-18 22:13:54 +0000989 Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)});
Craig Topperef9e9932013-10-15 05:20:47 +0000990 Rep = Builder.CreateZExt(Rep, CI->getType(), "");
Simon Pilgrimea0d4f92016-07-22 13:58:44 +0000991 } else if (IsX86 && Name.startswith("avx.vbroadcast.s")) {
Adam Nemet39066802014-05-29 23:35:33 +0000992 // Replace broadcasts with a series of insertelements.
993 Type *VecTy = CI->getType();
994 Type *EltTy = VecTy->getVectorElementType();
995 unsigned EltNum = VecTy->getVectorNumElements();
996 Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
997 EltTy->getPointerTo());
David Blaikie0c28fd72015-05-20 21:46:30 +0000998 Value *Load = Builder.CreateLoad(EltTy, Cast);
Adam Nemet39066802014-05-29 23:35:33 +0000999 Type *I32Ty = Type::getInt32Ty(C);
1000 Rep = UndefValue::get(VecTy);
1001 for (unsigned I = 0; I < EltNum; ++I)
1002 Rep = Builder.CreateInsertElement(Rep, Load,
1003 ConstantInt::get(I32Ty, I));
Craig Topper5aebb862016-07-04 20:56:38 +00001004 } else if (IsX86 && (Name.startswith("sse41.pmovsx") ||
1005 Name.startswith("sse41.pmovzx") ||
1006 Name.startswith("avx2.pmovsx") ||
Craig Topperb110e042016-11-07 02:12:57 +00001007 Name.startswith("avx2.pmovzx") ||
1008 Name.startswith("avx512.mask.pmovsx") ||
1009 Name.startswith("avx512.mask.pmovzx"))) {
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001010 VectorType *SrcTy = cast<VectorType>(CI->getArgOperand(0)->getType());
1011 VectorType *DstTy = cast<VectorType>(CI->getType());
1012 unsigned NumDstElts = DstTy->getNumElements();
1013
Simon Pilgrim9602d672016-05-28 18:03:41 +00001014 // Extract a subvector of the first NumDstElts lanes and sign/zero extend.
Craig Topperc0a5fa02016-06-12 04:48:00 +00001015 SmallVector<uint32_t, 8> ShuffleMask(NumDstElts);
Craig Topper99d1eab2016-06-12 00:41:19 +00001016 for (unsigned i = 0; i != NumDstElts; ++i)
Craig Topperc0a5fa02016-06-12 04:48:00 +00001017 ShuffleMask[i] = i;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001018
1019 Value *SV = Builder.CreateShuffleVector(
1020 CI->getArgOperand(0), UndefValue::get(SrcTy), ShuffleMask);
Simon Pilgrim9602d672016-05-28 18:03:41 +00001021
1022 bool DoSext = (StringRef::npos != Name.find("pmovsx"));
1023 Rep = DoSext ? Builder.CreateSExt(SV, DstTy)
1024 : Builder.CreateZExt(SV, DstTy);
Craig Topperb110e042016-11-07 02:12:57 +00001025 // If there are 3 arguments, it's a masked intrinsic so we need a select.
1026 if (CI->getNumArgOperands() == 3)
1027 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
1028 CI->getArgOperand(1));
Simon Pilgrimea0d4f92016-07-22 13:58:44 +00001029 } else if (IsX86 && (Name.startswith("avx.vbroadcastf128") ||
1030 Name == "avx2.vbroadcasti128")) {
1031 // Replace vbroadcastf128/vbroadcasti128 with a vector load+shuffle.
1032 Type *EltTy = CI->getType()->getVectorElementType();
1033 unsigned NumSrcElts = 128 / EltTy->getPrimitiveSizeInBits();
1034 Type *VT = VectorType::get(EltTy, NumSrcElts);
David Blaikie0c28fd72015-05-20 21:46:30 +00001035 Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0),
1036 PointerType::getUnqual(VT));
Chandler Carruth0215e762016-08-10 07:41:26 +00001037 Value *Load = Builder.CreateAlignedLoad(Op, 1);
Simon Pilgrimea0d4f92016-07-22 13:58:44 +00001038 if (NumSrcElts == 2)
1039 Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
1040 { 0, 1, 0, 1 });
1041 else
1042 Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
1043 { 0, 1, 2, 3, 0, 1, 2, 3 });
Craig Topper5aebb862016-07-04 20:56:38 +00001044 } else if (IsX86 && (Name.startswith("avx2.pbroadcast") ||
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +00001045 Name.startswith("avx2.vbroadcast") ||
1046 Name.startswith("avx512.pbroadcast") ||
1047 Name.startswith("avx512.mask.broadcast.s"))) {
Ahmed Bougacha1a4987052015-08-20 20:36:19 +00001048 // Replace vp?broadcasts with a vector shuffle.
1049 Value *Op = CI->getArgOperand(0);
1050 unsigned NumElts = CI->getType()->getVectorNumElements();
1051 Type *MaskTy = VectorType::get(Type::getInt32Ty(C), NumElts);
1052 Rep = Builder.CreateShuffleVector(Op, UndefValue::get(Op->getType()),
1053 Constant::getNullValue(MaskTy));
Simon Pilgrim4e96fbf2016-07-05 13:58:47 +00001054
1055 if (CI->getNumArgOperands() == 3)
1056 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
1057 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +00001058 } else if (IsX86 && Name.startswith("avx512.mask.palignr.")) {
Craig Topper46b34fe2016-07-12 01:42:33 +00001059 Rep = UpgradeX86PALIGNRIntrinsics(Builder, CI->getArgOperand(0),
Craig Topper33350cc2016-06-06 06:12:54 +00001060 CI->getArgOperand(1),
1061 CI->getArgOperand(2),
1062 CI->getArgOperand(3),
1063 CI->getArgOperand(4));
Craig Topper5aebb862016-07-04 20:56:38 +00001064 } else if (IsX86 && (Name == "sse2.psll.dq" ||
1065 Name == "avx2.psll.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +00001066 // 128/256-bit shift left specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +00001067 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +00001068 Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +00001069 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +00001070 } else if (IsX86 && (Name == "sse2.psrl.dq" ||
1071 Name == "avx2.psrl.dq")) {
Craig Topper7355ac32016-05-29 06:37:33 +00001072 // 128/256-bit shift right specified in bits.
Craig Topperb324e432015-02-18 06:24:44 +00001073 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +00001074 Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0),
Craig Topperb324e432015-02-18 06:24:44 +00001075 Shift / 8); // Shift is in bits.
Craig Topper5aebb862016-07-04 20:56:38 +00001076 } else if (IsX86 && (Name == "sse2.psll.dq.bs" ||
1077 Name == "avx2.psll.dq.bs" ||
1078 Name == "avx512.psll.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +00001079 // 128/256/512-bit shift left specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +00001080 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +00001081 Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +00001082 } else if (IsX86 && (Name == "sse2.psrl.dq.bs" ||
1083 Name == "avx2.psrl.dq.bs" ||
1084 Name == "avx512.psrl.dq.512")) {
Simon Pilgrimf7186822016-06-09 21:09:03 +00001085 // 128/256/512-bit shift right specified in bytes.
Craig Topperb324e432015-02-18 06:24:44 +00001086 unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
Craig Topper46b34fe2016-07-12 01:42:33 +00001087 Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
Craig Topper5aebb862016-07-04 20:56:38 +00001088 } else if (IsX86 && (Name == "sse41.pblendw" ||
1089 Name.startswith("sse41.blendp") ||
1090 Name.startswith("avx.blend.p") ||
1091 Name == "avx2.pblendw" ||
1092 Name.startswith("avx2.pblendd."))) {
Craig Topper782d6202015-02-28 19:33:17 +00001093 Value *Op0 = CI->getArgOperand(0);
1094 Value *Op1 = CI->getArgOperand(1);
1095 unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue();
1096 VectorType *VecTy = cast<VectorType>(CI->getType());
1097 unsigned NumElts = VecTy->getNumElements();
1098
Craig Topperc0a5fa02016-06-12 04:48:00 +00001099 SmallVector<uint32_t, 16> Idxs(NumElts);
1100 for (unsigned i = 0; i != NumElts; ++i)
1101 Idxs[i] = ((Imm >> (i%8)) & 1) ? i + NumElts : i;
Craig Topper782d6202015-02-28 19:33:17 +00001102
Craig Topper2f561822016-06-12 01:05:59 +00001103 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001104 } else if (IsX86 && (Name.startswith("avx.vinsertf128.") ||
1105 Name == "avx2.vinserti128")) {
Sanjay Patel19792fb2015-03-10 16:08:36 +00001106 Value *Op0 = CI->getArgOperand(0);
1107 Value *Op1 = CI->getArgOperand(1);
1108 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
1109 VectorType *VecTy = cast<VectorType>(CI->getType());
1110 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001111
Sanjay Patel19792fb2015-03-10 16:08:36 +00001112 // Mask off the high bits of the immediate value; hardware ignores those.
1113 Imm = Imm & 1;
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001114
Sanjay Patel19792fb2015-03-10 16:08:36 +00001115 // Extend the second operand into a vector that is twice as big.
1116 Value *UndefV = UndefValue::get(Op1->getType());
Craig Topperc0a5fa02016-06-12 04:48:00 +00001117 SmallVector<uint32_t, 8> Idxs(NumElts);
1118 for (unsigned i = 0; i != NumElts; ++i)
1119 Idxs[i] = i;
Craig Topper2f561822016-06-12 01:05:59 +00001120 Rep = Builder.CreateShuffleVector(Op1, UndefV, Idxs);
Sanjay Patel19792fb2015-03-10 16:08:36 +00001121
1122 // Insert the second operand into the first operand.
1123
1124 // Note that there is no guarantee that instruction lowering will actually
1125 // produce a vinsertf128 instruction for the created shuffles. In
1126 // particular, the 0 immediate case involves no lane changes, so it can
1127 // be handled as a blend.
1128
1129 // Example of shuffle mask for 32-bit elements:
1130 // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
1131 // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 >
1132
Sanjay Patel19792fb2015-03-10 16:08:36 +00001133 // The low half of the result is either the low half of the 1st operand
1134 // or the low half of the 2nd operand (the inserted vector).
Craig Topperc0a5fa02016-06-12 04:48:00 +00001135 for (unsigned i = 0; i != NumElts / 2; ++i)
1136 Idxs[i] = Imm ? i : (i + NumElts);
Sanjay Patel19792fb2015-03-10 16:08:36 +00001137 // The high half of the result is either the low half of the 2nd operand
1138 // (the inserted vector) or the high half of the 1st operand.
Craig Topperc0a5fa02016-06-12 04:48:00 +00001139 for (unsigned i = NumElts / 2; i != NumElts; ++i)
1140 Idxs[i] = Imm ? (i + NumElts / 2) : i;
Craig Topper2f561822016-06-12 01:05:59 +00001141 Rep = Builder.CreateShuffleVector(Op0, Rep, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001142 } else if (IsX86 && (Name.startswith("avx.vextractf128.") ||
1143 Name == "avx2.vextracti128")) {
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001144 Value *Op0 = CI->getArgOperand(0);
1145 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1146 VectorType *VecTy = cast<VectorType>(CI->getType());
1147 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9cb018b2015-09-23 08:48:33 +00001148
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001149 // Mask off the high bits of the immediate value; hardware ignores those.
1150 Imm = Imm & 1;
1151
1152 // Get indexes for either the high half or low half of the input vector.
Craig Topper2f561822016-06-12 01:05:59 +00001153 SmallVector<uint32_t, 4> Idxs(NumElts);
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001154 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topper2f561822016-06-12 01:05:59 +00001155 Idxs[i] = Imm ? (i + NumElts) : i;
Sanjay Patelaf1846c2015-03-12 15:15:19 +00001156 }
1157
1158 Value *UndefV = UndefValue::get(Op0->getType());
Craig Topper2f561822016-06-12 01:05:59 +00001159 Rep = Builder.CreateShuffleVector(Op0, UndefV, Idxs);
Craig Topper5aebb862016-07-04 20:56:38 +00001160 } else if (!IsX86 && Name == "stackprotectorcheck") {
Tim Shen00127562016-04-08 21:26:31 +00001161 Rep = nullptr;
Craig Topper5aebb862016-07-04 20:56:38 +00001162 } else if (IsX86 && (Name.startswith("avx512.mask.perm.df.") ||
1163 Name.startswith("avx512.mask.perm.di."))) {
Simon Pilgrim02d435d2016-07-04 14:19:05 +00001164 Value *Op0 = CI->getArgOperand(0);
1165 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1166 VectorType *VecTy = cast<VectorType>(CI->getType());
1167 unsigned NumElts = VecTy->getNumElements();
1168
1169 SmallVector<uint32_t, 8> Idxs(NumElts);
1170 for (unsigned i = 0; i != NumElts; ++i)
1171 Idxs[i] = (i & ~0x3) + ((Imm >> (2 * (i & 0x3))) & 3);
1172
1173 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1174
1175 if (CI->getNumArgOperands() == 4)
1176 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1177 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001178 } else if (IsX86 && (Name.startswith("avx.vpermil.") ||
1179 Name == "sse2.pshuf.d" ||
1180 Name.startswith("avx512.mask.vpermil.p") ||
1181 Name.startswith("avx512.mask.pshuf.d."))) {
Craig Topper8a105052016-06-12 03:10:47 +00001182 Value *Op0 = CI->getArgOperand(0);
1183 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1184 VectorType *VecTy = cast<VectorType>(CI->getType());
1185 unsigned NumElts = VecTy->getNumElements();
Simon Pilgrim9fca3002016-07-04 12:40:54 +00001186 // Calculate the size of each index in the immediate.
Craig Topper8a105052016-06-12 03:10:47 +00001187 unsigned IdxSize = 64 / VecTy->getScalarSizeInBits();
1188 unsigned IdxMask = ((1 << IdxSize) - 1);
1189
1190 SmallVector<uint32_t, 8> Idxs(NumElts);
1191 // Lookup the bits for this element, wrapping around the immediate every
1192 // 8-bits. Elements are grouped into sets of 2 or 4 elements so we need
1193 // to offset by the first index of each group.
1194 for (unsigned i = 0; i != NumElts; ++i)
1195 Idxs[i] = ((Imm >> ((i * IdxSize) % 8)) & IdxMask) | (i & ~IdxMask);
1196
1197 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001198
1199 if (CI->getNumArgOperands() == 4)
1200 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1201 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001202 } else if (IsX86 && (Name == "sse2.pshufl.w" ||
1203 Name.startswith("avx512.mask.pshufl.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001204 Value *Op0 = CI->getArgOperand(0);
1205 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1206 unsigned NumElts = CI->getType()->getVectorNumElements();
1207
1208 SmallVector<uint32_t, 16> Idxs(NumElts);
1209 for (unsigned l = 0; l != NumElts; l += 8) {
1210 for (unsigned i = 0; i != 4; ++i)
1211 Idxs[i + l] = ((Imm >> (2 * i)) & 0x3) + l;
1212 for (unsigned i = 4; i != 8; ++i)
1213 Idxs[i + l] = i + l;
1214 }
1215
1216 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001217
1218 if (CI->getNumArgOperands() == 4)
1219 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1220 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001221 } else if (IsX86 && (Name == "sse2.pshufh.w" ||
1222 Name.startswith("avx512.mask.pshufh.w."))) {
Craig Topper10679862016-06-12 14:11:32 +00001223 Value *Op0 = CI->getArgOperand(0);
1224 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
1225 unsigned NumElts = CI->getType()->getVectorNumElements();
1226
1227 SmallVector<uint32_t, 16> Idxs(NumElts);
1228 for (unsigned l = 0; l != NumElts; l += 8) {
1229 for (unsigned i = 0; i != 4; ++i)
1230 Idxs[i + l] = i + l;
1231 for (unsigned i = 0; i != 4; ++i)
1232 Idxs[i + l + 4] = ((Imm >> (2 * i)) & 0x3) + 4 + l;
1233 }
1234
1235 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
Craig Topper13cf7ca2016-06-13 02:36:48 +00001236
1237 if (CI->getNumArgOperands() == 4)
1238 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1239 CI->getArgOperand(2));
Craig Topper4619c9e2016-09-13 07:40:53 +00001240 } else if (IsX86 && Name.startswith("avx512.mask.shuf.p")) {
1241 Value *Op0 = CI->getArgOperand(0);
1242 Value *Op1 = CI->getArgOperand(1);
1243 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
1244 unsigned NumElts = CI->getType()->getVectorNumElements();
1245
1246 unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1247 unsigned HalfLaneElts = NumLaneElts / 2;
1248
1249 SmallVector<uint32_t, 16> Idxs(NumElts);
1250 for (unsigned i = 0; i != NumElts; ++i) {
1251 // Base index is the starting element of the lane.
1252 Idxs[i] = i - (i % NumLaneElts);
1253 // If we are half way through the lane switch to the other source.
1254 if ((i % NumLaneElts) >= HalfLaneElts)
1255 Idxs[i] += NumElts;
1256 // Now select the specific element. By adding HalfLaneElts bits from
1257 // the immediate. Wrapping around the immediate every 8-bits.
1258 Idxs[i] += (Imm >> ((i * HalfLaneElts) % 8)) & ((1 << HalfLaneElts) - 1);
1259 }
1260
1261 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1262
1263 Rep = EmitX86Select(Builder, CI->getArgOperand(4), Rep,
1264 CI->getArgOperand(3));
Craig Topper5aebb862016-07-04 20:56:38 +00001265 } else if (IsX86 && (Name.startswith("avx512.mask.movddup") ||
1266 Name.startswith("avx512.mask.movshdup") ||
1267 Name.startswith("avx512.mask.movsldup"))) {
Simon Pilgrim19adee92016-07-02 14:42:35 +00001268 Value *Op0 = CI->getArgOperand(0);
1269 unsigned NumElts = CI->getType()->getVectorNumElements();
1270 unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1271
1272 unsigned Offset = 0;
Craig Topper5aebb862016-07-04 20:56:38 +00001273 if (Name.startswith("avx512.mask.movshdup."))
Simon Pilgrim19adee92016-07-02 14:42:35 +00001274 Offset = 1;
1275
1276 SmallVector<uint32_t, 16> Idxs(NumElts);
1277 for (unsigned l = 0; l != NumElts; l += NumLaneElts)
1278 for (unsigned i = 0; i != NumLaneElts; i += 2) {
1279 Idxs[i + l + 0] = i + l + Offset;
1280 Idxs[i + l + 1] = i + l + Offset;
1281 }
1282
1283 Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
1284
1285 Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
1286 CI->getArgOperand(1));
Craig Topper5aebb862016-07-04 20:56:38 +00001287 } else if (IsX86 && (Name.startswith("avx512.mask.punpckl") ||
1288 Name.startswith("avx512.mask.unpckl."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001289 Value *Op0 = CI->getArgOperand(0);
1290 Value *Op1 = CI->getArgOperand(1);
1291 int NumElts = CI->getType()->getVectorNumElements();
1292 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1293
1294 SmallVector<uint32_t, 64> Idxs(NumElts);
1295 for (int l = 0; l != NumElts; l += NumLaneElts)
1296 for (int i = 0; i != NumLaneElts; ++i)
1297 Idxs[i + l] = l + (i / 2) + NumElts * (i % 2);
1298
1299 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1300
1301 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1302 CI->getArgOperand(2));
Craig Topper5aebb862016-07-04 20:56:38 +00001303 } else if (IsX86 && (Name.startswith("avx512.mask.punpckh") ||
1304 Name.startswith("avx512.mask.unpckh."))) {
Craig Topper597aa422016-06-23 07:37:33 +00001305 Value *Op0 = CI->getArgOperand(0);
1306 Value *Op1 = CI->getArgOperand(1);
1307 int NumElts = CI->getType()->getVectorNumElements();
1308 int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
1309
1310 SmallVector<uint32_t, 64> Idxs(NumElts);
1311 for (int l = 0; l != NumElts; l += NumLaneElts)
1312 for (int i = 0; i != NumLaneElts; ++i)
1313 Idxs[i + l] = (NumLaneElts / 2) + l + (i / 2) + NumElts * (i % 2);
1314
1315 Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
1316
1317 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1318 CI->getArgOperand(2));
Craig Toppera6e6feb2016-07-12 05:27:53 +00001319 } else if (IsX86 && Name.startswith("avx512.mask.pand.")) {
1320 Rep = Builder.CreateAnd(CI->getArgOperand(0), CI->getArgOperand(1));
1321 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1322 CI->getArgOperand(2));
1323 } else if (IsX86 && Name.startswith("avx512.mask.pandn.")) {
1324 Rep = Builder.CreateAnd(Builder.CreateNot(CI->getArgOperand(0)),
1325 CI->getArgOperand(1));
1326 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1327 CI->getArgOperand(2));
1328 } else if (IsX86 && Name.startswith("avx512.mask.por.")) {
1329 Rep = Builder.CreateOr(CI->getArgOperand(0), CI->getArgOperand(1));
1330 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1331 CI->getArgOperand(2));
1332 } else if (IsX86 && Name.startswith("avx512.mask.pxor.")) {
1333 Rep = Builder.CreateXor(CI->getArgOperand(0), CI->getArgOperand(1));
1334 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1335 CI->getArgOperand(2));
Craig Toppere75c4952016-09-02 05:29:17 +00001336 } else if (IsX86 && Name.startswith("avx512.mask.and.")) {
1337 VectorType *FTy = cast<VectorType>(CI->getType());
1338 VectorType *ITy = VectorType::getInteger(FTy);
1339 Rep = Builder.CreateAnd(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
1340 Builder.CreateBitCast(CI->getArgOperand(1), ITy));
1341 Rep = Builder.CreateBitCast(Rep, FTy);
1342 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1343 CI->getArgOperand(2));
1344 } else if (IsX86 && Name.startswith("avx512.mask.andn.")) {
1345 VectorType *FTy = cast<VectorType>(CI->getType());
1346 VectorType *ITy = VectorType::getInteger(FTy);
1347 Rep = Builder.CreateNot(Builder.CreateBitCast(CI->getArgOperand(0), ITy));
1348 Rep = Builder.CreateAnd(Rep,
1349 Builder.CreateBitCast(CI->getArgOperand(1), ITy));
1350 Rep = Builder.CreateBitCast(Rep, FTy);
1351 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1352 CI->getArgOperand(2));
1353 } else if (IsX86 && Name.startswith("avx512.mask.or.")) {
1354 VectorType *FTy = cast<VectorType>(CI->getType());
1355 VectorType *ITy = VectorType::getInteger(FTy);
1356 Rep = Builder.CreateOr(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
1357 Builder.CreateBitCast(CI->getArgOperand(1), ITy));
1358 Rep = Builder.CreateBitCast(Rep, FTy);
1359 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1360 CI->getArgOperand(2));
1361 } else if (IsX86 && Name.startswith("avx512.mask.xor.")) {
1362 VectorType *FTy = cast<VectorType>(CI->getType());
1363 VectorType *ITy = VectorType::getInteger(FTy);
1364 Rep = Builder.CreateXor(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
1365 Builder.CreateBitCast(CI->getArgOperand(1), ITy));
1366 Rep = Builder.CreateBitCast(Rep, FTy);
1367 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1368 CI->getArgOperand(2));
Craig Topperaf0d63d2016-09-04 02:09:53 +00001369 } else if (IsX86 && Name.startswith("avx512.mask.padd.")) {
1370 Rep = Builder.CreateAdd(CI->getArgOperand(0), CI->getArgOperand(1));
1371 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1372 CI->getArgOperand(2));
1373 } else if (IsX86 && Name.startswith("avx512.mask.psub.")) {
1374 Rep = Builder.CreateSub(CI->getArgOperand(0), CI->getArgOperand(1));
1375 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1376 CI->getArgOperand(2));
1377 } else if (IsX86 && Name.startswith("avx512.mask.pmull.")) {
1378 Rep = Builder.CreateMul(CI->getArgOperand(0), CI->getArgOperand(1));
1379 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1380 CI->getArgOperand(2));
Craig Topper8f354a72016-11-05 05:35:23 +00001381 } else if (IsX86 && (Name.startswith("avx512.mask.add.p"))) {
Craig Topper41773452016-09-04 18:13:33 +00001382 Rep = Builder.CreateFAdd(CI->getArgOperand(0), CI->getArgOperand(1));
1383 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1384 CI->getArgOperand(2));
Craig Topper96041c62016-11-07 00:13:42 +00001385 } else if (IsX86 && Name.startswith("avx512.mask.div.p")) {
Craig Topper41773452016-09-04 18:13:33 +00001386 Rep = Builder.CreateFDiv(CI->getArgOperand(0), CI->getArgOperand(1));
1387 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1388 CI->getArgOperand(2));
Craig Topper96041c62016-11-07 00:13:42 +00001389 } else if (IsX86 && Name.startswith("avx512.mask.mul.p")) {
Craig Topper41773452016-09-04 18:13:33 +00001390 Rep = Builder.CreateFMul(CI->getArgOperand(0), CI->getArgOperand(1));
1391 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1392 CI->getArgOperand(2));
Craig Topper96041c62016-11-07 00:13:42 +00001393 } else if (IsX86 && Name.startswith("avx512.mask.sub.p")) {
Craig Topper41773452016-09-04 18:13:33 +00001394 Rep = Builder.CreateFSub(CI->getArgOperand(0), CI->getArgOperand(1));
1395 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1396 CI->getArgOperand(2));
Craig Topper7e545332016-11-07 00:13:39 +00001397 } else if (IsX86 && Name.startswith("avx512.mask.pshuf.b.")) {
1398 VectorType *VecTy = cast<VectorType>(CI->getType());
1399 Intrinsic::ID IID;
1400 if (VecTy->getPrimitiveSizeInBits() == 128)
1401 IID = Intrinsic::x86_ssse3_pshuf_b_128;
1402 else if (VecTy->getPrimitiveSizeInBits() == 256)
1403 IID = Intrinsic::x86_avx2_pshuf_b;
1404 else
1405 llvm_unreachable("Unexpected intrinsic");
1406
1407 Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
1408 { CI->getArgOperand(0), CI->getArgOperand(1) });
1409 Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
1410 CI->getArgOperand(2));
Craig Topper987dad22016-11-13 19:09:56 +00001411 } else if (IsX86 && Name.startswith("avx512.mask.psll")) {
1412 bool IsImmediate = Name[16] == 'i' ||
1413 (Name.size() > 18 && Name[18] == 'i');
1414 bool IsVariable = Name[16] == 'v';
1415 char Size = Name[16] == '.' ? Name[17] :
1416 Name[17] == '.' ? Name[18] :
1417 Name[19];
1418
1419 Intrinsic::ID IID;
Craig Topper353e59b2016-11-14 01:53:22 +00001420 if (IsVariable && Name[17] != '.') {
Craig Topper987dad22016-11-13 19:09:56 +00001421 if (Size == 'd' && Name[17] == '2') // avx512.mask.psllv2.di
1422 IID = Intrinsic::x86_avx2_psllv_q;
1423 else if (Size == 'd' && Name[17] == '4') // avx512.mask.psllv4.di
1424 IID = Intrinsic::x86_avx2_psllv_q_256;
1425 else if (Size == 's' && Name[17] == '4') // avx512.mask.psllv4.si
1426 IID = Intrinsic::x86_avx2_psllv_d;
1427 else if (Size == 's' && Name[17] == '8') // avx512.mask.psllv8.si
1428 IID = Intrinsic::x86_avx2_psllv_d_256;
1429 else
1430 llvm_unreachable("Unexpected size");
1431 } else if (Name.endswith(".128")) {
1432 if (Size == 'd') // avx512.mask.psll.d.128, avx512.mask.psll.di.128
1433 IID = IsImmediate ? Intrinsic::x86_sse2_pslli_d
1434 : Intrinsic::x86_sse2_psll_d;
1435 else if (Size == 'q') // avx512.mask.psll.q.128, avx512.mask.psll.qi.128
1436 IID = IsImmediate ? Intrinsic::x86_sse2_pslli_q
1437 : Intrinsic::x86_sse2_psll_q;
1438 else if (Size == 'w') // avx512.mask.psll.w.128, avx512.mask.psll.wi.128
1439 IID = IsImmediate ? Intrinsic::x86_sse2_pslli_w
1440 : Intrinsic::x86_sse2_psll_w;
1441 else
1442 llvm_unreachable("Unexpected size");
1443 } else if (Name.endswith(".256")) {
1444 if (Size == 'd') // avx512.mask.psll.d.256, avx512.mask.psll.di.256
1445 IID = IsImmediate ? Intrinsic::x86_avx2_pslli_d
1446 : Intrinsic::x86_avx2_psll_d;
1447 else if (Size == 'q') // avx512.mask.psll.q.256, avx512.mask.psll.qi.256
1448 IID = IsImmediate ? Intrinsic::x86_avx2_pslli_q
1449 : Intrinsic::x86_avx2_psll_q;
1450 else if (Size == 'w') // avx512.mask.psll.w.256, avx512.mask.psll.wi.256
1451 IID = IsImmediate ? Intrinsic::x86_avx2_pslli_w
1452 : Intrinsic::x86_avx2_psll_w;
1453 else
1454 llvm_unreachable("Unexpected size");
1455 } else {
Craig Topper353e59b2016-11-14 01:53:22 +00001456 if (Size == 'd') // psll.di.512, pslli.d, psll.d, psllv.d.512
1457 IID = IsImmediate ? Intrinsic::x86_avx512_pslli_d_512 :
1458 IsVariable ? Intrinsic::x86_avx512_psllv_d_512 :
1459 Intrinsic::x86_avx512_psll_d_512;
1460 else if (Size == 'q') // psll.qi.512, pslli.q, psll.q, psllv.q.512
1461 IID = IsImmediate ? Intrinsic::x86_avx512_pslli_q_512 :
1462 IsVariable ? Intrinsic::x86_avx512_psllv_q_512 :
1463 Intrinsic::x86_avx512_psll_q_512;
Craig Topper987dad22016-11-13 19:09:56 +00001464 else if (Size == 'w') // psll.wi.512, pslli.w, psll.w
1465 IID = IsImmediate ? Intrinsic::x86_avx512_pslli_w_512
1466 : Intrinsic::x86_avx512_psll_w_512;
1467 else
1468 llvm_unreachable("Unexpected size");
1469 }
1470
1471 Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
1472 } else if (IsX86 && Name.startswith("avx512.mask.psrl")) {
1473 bool IsImmediate = Name[16] == 'i' ||
1474 (Name.size() > 18 && Name[18] == 'i');
1475 bool IsVariable = Name[16] == 'v';
1476 char Size = Name[16] == '.' ? Name[17] :
1477 Name[17] == '.' ? Name[18] :
1478 Name[19];
1479
1480 Intrinsic::ID IID;
Craig Topper353e59b2016-11-14 01:53:22 +00001481 if (IsVariable && Name[17] != '.') {
Craig Topper987dad22016-11-13 19:09:56 +00001482 if (Size == 'd' && Name[17] == '2') // avx512.mask.psrlv2.di
1483 IID = Intrinsic::x86_avx2_psrlv_q;
1484 else if (Size == 'd' && Name[17] == '4') // avx512.mask.psrlv4.di
1485 IID = Intrinsic::x86_avx2_psrlv_q_256;
1486 else if (Size == 's' && Name[17] == '4') // avx512.mask.psrlv4.si
1487 IID = Intrinsic::x86_avx2_psrlv_d;
1488 else if (Size == 's' && Name[17] == '8') // avx512.mask.psrlv8.si
1489 IID = Intrinsic::x86_avx2_psrlv_d_256;
1490 else
1491 llvm_unreachable("Unexpected size");
1492 } else if (Name.endswith(".128")) {
1493 if (Size == 'd') // avx512.mask.psrl.d.128, avx512.mask.psrl.di.128
1494 IID = IsImmediate ? Intrinsic::x86_sse2_psrli_d
1495 : Intrinsic::x86_sse2_psrl_d;
1496 else if (Size == 'q') // avx512.mask.psrl.q.128, avx512.mask.psrl.qi.128
1497 IID = IsImmediate ? Intrinsic::x86_sse2_psrli_q
1498 : Intrinsic::x86_sse2_psrl_q;
1499 else if (Size == 'w') // avx512.mask.psrl.w.128, avx512.mask.psrl.wi.128
1500 IID = IsImmediate ? Intrinsic::x86_sse2_psrli_w
1501 : Intrinsic::x86_sse2_psrl_w;
1502 else
1503 llvm_unreachable("Unexpected size");
1504 } else if (Name.endswith(".256")) {
1505 if (Size == 'd') // avx512.mask.psrl.d.256, avx512.mask.psrl.di.256
1506 IID = IsImmediate ? Intrinsic::x86_avx2_psrli_d
1507 : Intrinsic::x86_avx2_psrl_d;
1508 else if (Size == 'q') // avx512.mask.psrl.q.256, avx512.mask.psrl.qi.256
1509 IID = IsImmediate ? Intrinsic::x86_avx2_psrli_q
1510 : Intrinsic::x86_avx2_psrl_q;
1511 else if (Size == 'w') // avx512.mask.psrl.w.256, avx512.mask.psrl.wi.256
1512 IID = IsImmediate ? Intrinsic::x86_avx2_psrli_w
1513 : Intrinsic::x86_avx2_psrl_w;
1514 else
1515 llvm_unreachable("Unexpected size");
1516 } else {
Craig Topper353e59b2016-11-14 01:53:22 +00001517 if (Size == 'd') // psrl.di.512, psrli.d, psrl.d, psrl.d.512
1518 IID = IsImmediate ? Intrinsic::x86_avx512_psrli_d_512 :
1519 IsVariable ? Intrinsic::x86_avx512_psrlv_d_512 :
1520 Intrinsic::x86_avx512_psrl_d_512;
1521 else if (Size == 'q') // psrl.qi.512, psrli.q, psrl.q, psrl.q.512
1522 IID = IsImmediate ? Intrinsic::x86_avx512_psrli_q_512 :
1523 IsVariable ? Intrinsic::x86_avx512_psrlv_q_512 :
1524 Intrinsic::x86_avx512_psrl_q_512;
Craig Topper987dad22016-11-13 19:09:56 +00001525 else if (Size == 'w') // psrl.wi.512, psrli.w, psrl.w)
1526 IID = IsImmediate ? Intrinsic::x86_avx512_psrli_w_512
1527 : Intrinsic::x86_avx512_psrl_w_512;
1528 else
1529 llvm_unreachable("Unexpected size");
1530 }
1531
1532 Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
1533 } else if (IsX86 && Name.startswith("avx512.mask.psra")) {
1534 bool IsImmediate = Name[16] == 'i' ||
1535 (Name.size() > 18 && Name[18] == 'i');
1536 bool IsVariable = Name[16] == 'v';
1537 char Size = Name[16] == '.' ? Name[17] :
1538 Name[17] == '.' ? Name[18] :
1539 Name[19];
1540
1541 Intrinsic::ID IID;
Craig Topper353e59b2016-11-14 01:53:22 +00001542 if (IsVariable && Name[17] != '.') {
Craig Topper987dad22016-11-13 19:09:56 +00001543 if (Size == 's' && Name[17] == '4') // avx512.mask.psrav4.si
1544 IID = Intrinsic::x86_avx2_psrav_d;
1545 else if (Size == 's' && Name[17] == '8') // avx512.mask.psrav8.si
1546 IID = Intrinsic::x86_avx2_psrav_d_256;
1547 else
1548 llvm_unreachable("Unexpected size");
1549 } else if (Name.endswith(".128")) {
1550 if (Size == 'd') // avx512.mask.psra.d.128, avx512.mask.psra.di.128
1551 IID = IsImmediate ? Intrinsic::x86_sse2_psrai_d
1552 : Intrinsic::x86_sse2_psra_d;
1553 else if (Size == 'q') // avx512.mask.psra.q.128, avx512.mask.psra.qi.128
Craig Topper353e59b2016-11-14 01:53:22 +00001554 IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_128 :
1555 IsVariable ? Intrinsic::x86_avx512_psrav_q_128 :
1556 Intrinsic::x86_avx512_psra_q_128;
Craig Topper987dad22016-11-13 19:09:56 +00001557 else if (Size == 'w') // avx512.mask.psra.w.128, avx512.mask.psra.wi.128
1558 IID = IsImmediate ? Intrinsic::x86_sse2_psrai_w
1559 : Intrinsic::x86_sse2_psra_w;
1560 else
1561 llvm_unreachable("Unexpected size");
1562 } else if (Name.endswith(".256")) {
1563 if (Size == 'd') // avx512.mask.psra.d.256, avx512.mask.psra.di.256
1564 IID = IsImmediate ? Intrinsic::x86_avx2_psrai_d
1565 : Intrinsic::x86_avx2_psra_d;
1566 else if (Size == 'q') // avx512.mask.psra.q.256, avx512.mask.psra.qi.256
Craig Topper353e59b2016-11-14 01:53:22 +00001567 IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_256 :
1568 IsVariable ? Intrinsic::x86_avx512_psrav_q_256 :
1569 Intrinsic::x86_avx512_psra_q_256;
Craig Topper987dad22016-11-13 19:09:56 +00001570 else if (Size == 'w') // avx512.mask.psra.w.256, avx512.mask.psra.wi.256
1571 IID = IsImmediate ? Intrinsic::x86_avx2_psrai_w
1572 : Intrinsic::x86_avx2_psra_w;
1573 else
1574 llvm_unreachable("Unexpected size");
1575 } else {
Craig Topper353e59b2016-11-14 01:53:22 +00001576 if (Size == 'd') // psra.di.512, psrai.d, psra.d, psrav.d.512
1577 IID = IsImmediate ? Intrinsic::x86_avx512_psrai_d_512 :
1578 IsVariable ? Intrinsic::x86_avx512_psrav_d_512 :
1579 Intrinsic::x86_avx512_psra_d_512;
Craig Topper987dad22016-11-13 19:09:56 +00001580 else if (Size == 'q') // psra.qi.512, psrai.q, psra.q
Craig Topper353e59b2016-11-14 01:53:22 +00001581 IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_512 :
1582 IsVariable ? Intrinsic::x86_avx512_psrav_q_512 :
1583 Intrinsic::x86_avx512_psra_q_512;
Craig Topper987dad22016-11-13 19:09:56 +00001584 else if (Size == 'w') // psra.wi.512, psrai.w, psra.w
1585 IID = IsImmediate ? Intrinsic::x86_avx512_psrai_w_512
1586 : Intrinsic::x86_avx512_psra_w_512;
1587 else
1588 llvm_unreachable("Unexpected size");
1589 }
1590
1591 Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
Craig Topper3b1817d2012-02-03 06:10:55 +00001592 } else {
Craig Topper8a105052016-06-12 03:10:47 +00001593 llvm_unreachable("Unknown function for CallInst upgrade.");
Craig Topper3b1817d2012-02-03 06:10:55 +00001594 }
1595
Tim Shen00127562016-04-08 21:26:31 +00001596 if (Rep)
1597 CI->replaceAllUsesWith(Rep);
Craig Topper3b1817d2012-02-03 06:10:55 +00001598 CI->eraseFromParent();
1599 return;
1600 }
1601
Yaron Kerend1fdbe72015-03-30 16:10:39 +00001602 std::string Name = CI->getName();
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001603 if (!Name.empty())
1604 CI->setName(Name + ".old");
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001605
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001606 switch (NewFn->getIntrinsicID()) {
1607 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +00001608 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001609
Jeroen Ketemaab99b592015-09-30 10:56:37 +00001610 case Intrinsic::arm_neon_vld1:
1611 case Intrinsic::arm_neon_vld2:
1612 case Intrinsic::arm_neon_vld3:
1613 case Intrinsic::arm_neon_vld4:
1614 case Intrinsic::arm_neon_vld2lane:
1615 case Intrinsic::arm_neon_vld3lane:
1616 case Intrinsic::arm_neon_vld4lane:
1617 case Intrinsic::arm_neon_vst1:
1618 case Intrinsic::arm_neon_vst2:
1619 case Intrinsic::arm_neon_vst3:
1620 case Intrinsic::arm_neon_vst4:
1621 case Intrinsic::arm_neon_vst2lane:
1622 case Intrinsic::arm_neon_vst3lane:
1623 case Intrinsic::arm_neon_vst4lane: {
1624 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1625 CI->arg_operands().end());
1626 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1627 CI->eraseFromParent();
1628 return;
1629 }
1630
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001631 case Intrinsic::ctlz:
Nuno Lopesad40c0a2012-05-22 15:25:31 +00001632 case Intrinsic::cttz:
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001633 assert(CI->getNumArgOperands() == 1 &&
1634 "Mismatch between function args and call args");
David Blaikieff6409d2015-05-18 22:13:54 +00001635 CI->replaceAllUsesWith(Builder.CreateCall(
1636 NewFn, {CI->getArgOperand(0), Builder.getFalse()}, Name));
Chandler Carruth58a71ed2011-12-12 04:26:04 +00001637 CI->eraseFromParent();
1638 return;
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001639
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001640 case Intrinsic::objectsize:
David Blaikieff6409d2015-05-18 22:13:54 +00001641 CI->replaceAllUsesWith(Builder.CreateCall(
1642 NewFn, {CI->getArgOperand(0), CI->getArgOperand(1)}, Name));
Matt Arsenaultfbcbce42013-10-07 18:06:48 +00001643 CI->eraseFromParent();
1644 return;
1645
Joel Jonesb84f7be2012-07-18 00:02:16 +00001646 case Intrinsic::ctpop: {
David Blaikieff6409d2015-05-18 22:13:54 +00001647 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {CI->getArgOperand(0)}));
Joel Jonesb84f7be2012-07-18 00:02:16 +00001648 CI->eraseFromParent();
1649 return;
1650 }
Joel Jones43cb8782012-07-13 23:25:25 +00001651
Craig Topper71dc02d2012-06-13 07:18:53 +00001652 case Intrinsic::x86_xop_vfrcz_ss:
1653 case Intrinsic::x86_xop_vfrcz_sd:
David Blaikieff6409d2015-05-18 22:13:54 +00001654 CI->replaceAllUsesWith(
1655 Builder.CreateCall(NewFn, {CI->getArgOperand(1)}, Name));
Craig Topper71dc02d2012-06-13 07:18:53 +00001656 CI->eraseFromParent();
1657 return;
1658
Simon Pilgrime85506b2016-06-03 08:06:03 +00001659 case Intrinsic::x86_xop_vpermil2pd:
1660 case Intrinsic::x86_xop_vpermil2ps:
1661 case Intrinsic::x86_xop_vpermil2pd_256:
1662 case Intrinsic::x86_xop_vpermil2ps_256: {
1663 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1664 CI->arg_operands().end());
1665 VectorType *FltIdxTy = cast<VectorType>(Args[2]->getType());
1666 VectorType *IntIdxTy = VectorType::getInteger(FltIdxTy);
1667 Args[2] = Builder.CreateBitCast(Args[2], IntIdxTy);
1668 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args, Name));
1669 CI->eraseFromParent();
1670 return;
1671 }
1672
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001673 case Intrinsic::x86_sse41_ptestc:
1674 case Intrinsic::x86_sse41_ptestz:
Craig Topper71dc02d2012-06-13 07:18:53 +00001675 case Intrinsic::x86_sse41_ptestnzc: {
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001676 // The arguments for these intrinsics used to be v4f32, and changed
1677 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
1678 // So, the only thing required is a bitcast for both arguments.
1679 // First, check the arguments have the old type.
1680 Value *Arg0 = CI->getArgOperand(0);
1681 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
1682 return;
1683
1684 // Old intrinsic, add bitcasts
1685 Value *Arg1 = CI->getArgOperand(1);
1686
David Blaikie5bacf372015-04-24 21:16:07 +00001687 Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001688
David Blaikie5bacf372015-04-24 21:16:07 +00001689 Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast");
1690 Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
1691
David Blaikieff6409d2015-05-18 22:13:54 +00001692 CallInst *NewCall = Builder.CreateCall(NewFn, {BC0, BC1}, Name);
Nadav Rotem17ee58a2012-06-10 18:42:51 +00001693 CI->replaceAllUsesWith(NewCall);
1694 CI->eraseFromParent();
1695 return;
Evan Cheng0e179d02007-12-17 22:33:23 +00001696 }
Chandler Carruth373b2b12014-09-06 10:00:01 +00001697
Chandler Carruth373b2b12014-09-06 10:00:01 +00001698 case Intrinsic::x86_sse41_insertps:
1699 case Intrinsic::x86_sse41_dppd:
1700 case Intrinsic::x86_sse41_dpps:
1701 case Intrinsic::x86_sse41_mpsadbw:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001702 case Intrinsic::x86_avx_dp_ps_256:
Chandler Carruth373b2b12014-09-06 10:00:01 +00001703 case Intrinsic::x86_avx2_mpsadbw: {
1704 // Need to truncate the last argument from i32 to i8 -- this argument models
1705 // an inherently 8-bit immediate operand to these x86 instructions.
1706 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1707 CI->arg_operands().end());
1708
1709 // Replace the last argument with a trunc.
1710 Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
1711
1712 CallInst *NewCall = Builder.CreateCall(NewFn, Args);
1713 CI->replaceAllUsesWith(NewCall);
1714 CI->eraseFromParent();
1715 return;
1716 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +00001717
1718 case Intrinsic::thread_pointer: {
1719 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {}));
1720 CI->eraseFromParent();
1721 return;
1722 }
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +00001723
Mehdi Amini8c629ec2016-08-13 23:31:24 +00001724 case Intrinsic::invariant_start:
1725 case Intrinsic::invariant_end:
Artur Pilipenko7ad95ec2016-06-28 18:27:25 +00001726 case Intrinsic::masked_load:
1727 case Intrinsic::masked_store: {
1728 SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
1729 CI->arg_operands().end());
1730 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args));
1731 CI->eraseFromParent();
1732 return;
1733 }
Craig Topper71dc02d2012-06-13 07:18:53 +00001734 }
Chandler Carruth7132e002007-08-04 01:51:18 +00001735}
1736
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001737void llvm::UpgradeCallsToIntrinsic(Function *F) {
Chandler Carruth7132e002007-08-04 01:51:18 +00001738 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
1739
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001740 // Check if this function should be upgraded and get the replacement function
1741 // if there is one.
Chris Lattner80ed9dc2011-06-18 06:05:24 +00001742 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +00001743 if (UpgradeIntrinsicFunction(F, NewFn)) {
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001744 // Replace all users of the old function with the new function or new
1745 // instructions. This is not a range loop because the call is deleted.
1746 for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; )
Duncan P. N. Exon Smith93f53c42016-04-17 03:59:37 +00001747 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001748 UpgradeIntrinsicCall(CI, NewFn);
Sanjay Patelfdf0d5f2016-04-18 19:11:57 +00001749
Filipe Cabecinhas0011c582015-07-03 20:12:01 +00001750 // Remove old function, no longer used, from the module.
1751 F->eraseFromParent();
Chandler Carruth7132e002007-08-04 01:51:18 +00001752 }
1753}
Devang Patel80ae3492009-08-28 23:24:31 +00001754
Mehdi Aminie4709272016-09-14 22:29:59 +00001755MDNode *llvm::UpgradeTBAANode(MDNode &MD) {
Manman Ren209b17c2013-09-28 00:22:27 +00001756 // Check if the tag uses struct-path aware TBAA format.
Mehdi Aminie4709272016-09-14 22:29:59 +00001757 if (isa<MDNode>(MD.getOperand(0)) && MD.getNumOperands() >= 3)
1758 return &MD;
Manman Ren209b17c2013-09-28 00:22:27 +00001759
Mehdi Aminie4709272016-09-14 22:29:59 +00001760 auto &Context = MD.getContext();
1761 if (MD.getNumOperands() == 3) {
1762 Metadata *Elts[] = {MD.getOperand(0), MD.getOperand(1)};
1763 MDNode *ScalarType = MDNode::get(Context, Elts);
Manman Ren209b17c2013-09-28 00:22:27 +00001764 // Create a MDNode <ScalarType, ScalarType, offset 0, const>
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001765 Metadata *Elts2[] = {ScalarType, ScalarType,
Mehdi Aminie4709272016-09-14 22:29:59 +00001766 ConstantAsMetadata::get(
1767 Constant::getNullValue(Type::getInt64Ty(Context))),
1768 MD.getOperand(2)};
1769 return MDNode::get(Context, Elts2);
Manman Ren209b17c2013-09-28 00:22:27 +00001770 }
Mehdi Aminie4709272016-09-14 22:29:59 +00001771 // Create a MDNode <MD, MD, offset 0>
1772 Metadata *Elts[] = {&MD, &MD, ConstantAsMetadata::get(Constant::getNullValue(
1773 Type::getInt64Ty(Context)))};
1774 return MDNode::get(Context, Elts);
Manman Ren209b17c2013-09-28 00:22:27 +00001775}
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001776
1777Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
1778 Instruction *&Temp) {
1779 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001780 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001781
Craig Topperc6207612014-04-09 06:08:46 +00001782 Temp = nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001783 Type *SrcTy = V->getType();
1784 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1785 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1786 LLVMContext &Context = V->getContext();
1787
1788 // We have no information about target data layout, so we assume that
1789 // the maximum pointer size is 64bit.
1790 Type *MidTy = Type::getInt64Ty(Context);
1791 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
1792
1793 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
1794 }
1795
Craig Topperc6207612014-04-09 06:08:46 +00001796 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001797}
1798
1799Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
1800 if (Opc != Instruction::BitCast)
Craig Topperc6207612014-04-09 06:08:46 +00001801 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001802
1803 Type *SrcTy = C->getType();
1804 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
1805 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
1806 LLVMContext &Context = C->getContext();
1807
1808 // We have no information about target data layout, so we assume that
1809 // the maximum pointer size is 64bit.
1810 Type *MidTy = Type::getInt64Ty(Context);
1811
1812 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
1813 DestTy);
1814 }
1815
Craig Topperc6207612014-04-09 06:08:46 +00001816 return nullptr;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001817}
Manman Ren8b4306c2013-12-02 21:29:56 +00001818
1819/// Check the debug info version number, if it is out-dated, drop the debug
1820/// info. Return true if module is modified.
1821bool llvm::UpgradeDebugInfo(Module &M) {
Manman Ren2ebfb422014-01-16 01:51:12 +00001822 unsigned Version = getDebugMetadataVersionFromModule(M);
1823 if (Version == DEBUG_METADATA_VERSION)
Manman Ren8b4306c2013-12-02 21:29:56 +00001824 return false;
1825
Manman Ren2ebfb422014-01-16 01:51:12 +00001826 bool RetCode = StripDebugInfo(M);
1827 if (RetCode) {
1828 DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
1829 M.getContext().diagnose(DiagVersion);
1830 }
1831 return RetCode;
Manman Ren8b4306c2013-12-02 21:29:56 +00001832}
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001833
Manman Renb5d7ff42016-05-25 23:14:48 +00001834bool llvm::UpgradeModuleFlags(Module &M) {
1835 const NamedMDNode *ModFlags = M.getModuleFlagsMetadata();
1836 if (!ModFlags)
1837 return false;
1838
1839 bool HasObjCFlag = false, HasClassProperties = false;
1840 for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
1841 MDNode *Op = ModFlags->getOperand(I);
1842 if (Op->getNumOperands() < 2)
1843 continue;
1844 MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1845 if (!ID)
1846 continue;
1847 if (ID->getString() == "Objective-C Image Info Version")
1848 HasObjCFlag = true;
1849 if (ID->getString() == "Objective-C Class Properties")
1850 HasClassProperties = true;
1851 }
1852 // "Objective-C Class Properties" is recently added for Objective-C. We
1853 // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module
Mehdi Aminib53b62e2016-09-16 00:38:18 +00001854 // flag of value 0, so we can correclty downgrade this flag when trying to
1855 // link an ObjC bitcode without this module flag with an ObjC bitcode with
1856 // this module flag.
Manman Renb5d7ff42016-05-25 23:14:48 +00001857 if (HasObjCFlag && !HasClassProperties) {
Mehdi Aminib53b62e2016-09-16 00:38:18 +00001858 M.addModuleFlag(llvm::Module::Override, "Objective-C Class Properties",
Manman Renb5d7ff42016-05-25 23:14:48 +00001859 (uint32_t)0);
1860 return true;
1861 }
1862 return false;
1863}
1864
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00001865static bool isOldLoopArgument(Metadata *MD) {
1866 auto *T = dyn_cast_or_null<MDTuple>(MD);
1867 if (!T)
1868 return false;
1869 if (T->getNumOperands() < 1)
1870 return false;
1871 auto *S = dyn_cast_or_null<MDString>(T->getOperand(0));
1872 if (!S)
1873 return false;
1874 return S->getString().startswith("llvm.vectorizer.");
1875}
1876
1877static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) {
1878 StringRef OldPrefix = "llvm.vectorizer.";
1879 assert(OldTag.startswith(OldPrefix) && "Expected old prefix");
1880
1881 if (OldTag == "llvm.vectorizer.unroll")
1882 return MDString::get(C, "llvm.loop.interleave.count");
1883
1884 return MDString::get(
1885 C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size()))
1886 .str());
1887}
1888
1889static Metadata *upgradeLoopArgument(Metadata *MD) {
1890 auto *T = dyn_cast_or_null<MDTuple>(MD);
1891 if (!T)
1892 return MD;
1893 if (T->getNumOperands() < 1)
1894 return MD;
1895 auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0));
1896 if (!OldTag)
1897 return MD;
1898 if (!OldTag->getString().startswith("llvm.vectorizer."))
1899 return MD;
1900
1901 // This has an old tag. Upgrade it.
1902 SmallVector<Metadata *, 8> Ops;
1903 Ops.reserve(T->getNumOperands());
1904 Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString()));
1905 for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I)
1906 Ops.push_back(T->getOperand(I));
1907
1908 return MDTuple::get(T->getContext(), Ops);
1909}
1910
1911MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) {
1912 auto *T = dyn_cast<MDTuple>(&N);
1913 if (!T)
1914 return &N;
1915
David Majnemer0a16c222016-08-11 21:15:00 +00001916 if (none_of(T->operands(), isOldLoopArgument))
Duncan P. N. Exon Smithefe16c82016-03-25 00:56:13 +00001917 return &N;
1918
1919 SmallVector<Metadata *, 8> Ops;
1920 Ops.reserve(T->getNumOperands());
1921 for (Metadata *MD : T->operands())
1922 Ops.push_back(upgradeLoopArgument(MD));
1923
1924 return MDTuple::get(T->getContext(), Ops);
Eli Bendersky5d5e18d2014-06-25 15:41:00 +00001925}