blob: 8febce18c0365b49bff35e5ae348f0db2e78ac88 [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//
10// This file implements the auto-upgrade helper functions
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/AutoUpgrade.h"
Anders Carlssonf924f342007-12-14 06:38:54 +000015#include "llvm/Constants.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000016#include "llvm/Function.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000017#include "llvm/IRBuilder.h"
Bill Wendling45449b12011-08-25 23:22:40 +000018#include "llvm/Instruction.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000019#include "llvm/IntrinsicInst.h"
Owen Anderson155dccd82009-07-07 23:43:39 +000020#include "llvm/LLVMContext.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000021#include "llvm/Module.h"
Bill Wendling45449b12011-08-25 23:22:40 +000022#include "llvm/Support/CFG.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000023#include "llvm/Support/CallSite.h"
Torok Edwin56d06592009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000025#include <cstring>
Chandler Carruth7132e002007-08-04 01:51:18 +000026using namespace llvm;
27
Nadav Rotem17ee58a2012-06-10 18:42:51 +000028// Upgrade the declarations of the SSE4.1 functions whose arguments have
29// changed their type from v4f32 to v2i64.
30static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
31 Function *&NewFn) {
32 // Check whether this is an old version of the function, which received
33 // v4f32 arguments.
34 Type *Arg0Type = F->getFunctionType()->getParamType(0);
35 if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
36 return false;
37
38 // Yes, it's old, replace it with new version.
39 F->setName(F->getName() + ".old");
40 NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
41 return true;
42}
Chandler Carruth7132e002007-08-04 01:51:18 +000043
Evan Cheng0e179d02007-12-17 22:33:23 +000044static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +000045 assert(F && "Illegal to upgrade a non-existent Function.");
46
Chandler Carruth7132e002007-08-04 01:51:18 +000047 // Quickly eliminate it, if it's not a candidate.
Chris Lattnerb372f662011-06-18 18:56:39 +000048 StringRef Name = F->getName();
49 if (Name.size() <= 8 || !Name.startswith("llvm."))
Evan Cheng0e179d02007-12-17 22:33:23 +000050 return false;
Chris Lattnerb372f662011-06-18 18:56:39 +000051 Name = Name.substr(5); // Strip off "llvm."
Chris Lattner0bcbde42011-11-27 08:42:07 +000052
Chris Lattnerb372f662011-06-18 18:56:39 +000053 switch (Name[0]) {
Chandler Carruth7132e002007-08-04 01:51:18 +000054 default: break;
Joel Jones43cb8782012-07-13 23:25:25 +000055 case 'a': {
56 if (Name.startswith("arm.neon.vclz")) {
57 Type* args[2] = {
58 F->arg_begin()->getType(),
59 Type::getInt1Ty(F->getContext())
60 };
61 // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
62 // the end of the name. Change name from llvm.arm.neon.vclz.* to
63 // llvm.ctlz.*
64 FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
65 NewFn = Function::Create(fType, F->getLinkage(),
66 "llvm.ctlz." + Name.substr(14), F->getParent());
67 return true;
68 }
69 break;
70 }
Chandler Carruth58a71ed2011-12-12 04:26:04 +000071 case 'c': {
Chandler Carruth58a71ed2011-12-12 04:26:04 +000072 if (Name.startswith("ctlz.") && F->arg_size() == 1) {
73 F->setName(Name + ".old");
Chandler Carruthd4a02402011-12-12 10:57:20 +000074 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
75 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +000076 return true;
77 }
78 if (Name.startswith("cttz.") && F->arg_size() == 1) {
79 F->setName(Name + ".old");
Chandler Carruthd4a02402011-12-12 10:57:20 +000080 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
81 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +000082 return true;
83 }
84 break;
85 }
Craig Topper3b1817d2012-02-03 06:10:55 +000086 case 'x': {
87 if (Name.startswith("x86.sse2.pcmpeq.") ||
88 Name.startswith("x86.sse2.pcmpgt.") ||
89 Name.startswith("x86.avx2.pcmpeq.") ||
Craig Topperd3c9e402012-04-18 05:24:00 +000090 Name.startswith("x86.avx2.pcmpgt.") ||
Craig Topper7daf8972012-05-08 06:58:15 +000091 Name.startswith("x86.avx.vpermil.") ||
92 Name == "x86.avx.movnt.dq.256" ||
93 Name == "x86.avx.movnt.pd.256" ||
Craig Topper3352ba52012-06-09 16:46:13 +000094 Name == "x86.avx.movnt.ps.256" ||
95 (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
Craig Topper3b1817d2012-02-03 06:10:55 +000096 NewFn = 0;
97 return true;
98 }
Nadav Rotem17ee58a2012-06-10 18:42:51 +000099 // SSE4.1 ptest functions may have an old signature.
100 if (Name.startswith("x86.sse41.ptest")) {
101 if (Name == "x86.sse41.ptestc")
102 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
103 if (Name == "x86.sse41.ptestz")
104 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
105 if (Name == "x86.sse41.ptestnzc")
106 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
107 }
Craig Topper71dc02d2012-06-13 07:18:53 +0000108 // frcz.ss/sd may need to have an argument dropped
109 if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
110 F->setName(Name + ".old");
111 NewFn = Intrinsic::getDeclaration(F->getParent(),
112 Intrinsic::x86_xop_vfrcz_ss);
113 return true;
114 }
115 if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
116 F->setName(Name + ".old");
117 NewFn = Intrinsic::getDeclaration(F->getParent(),
118 Intrinsic::x86_xop_vfrcz_sd);
119 return true;
120 }
Craig Topper720c7bd2012-06-03 08:07:25 +0000121 // Fix the FMA4 intrinsics to remove the 4
122 if (Name.startswith("x86.fma4.")) {
Craig Topper2c5ccd82012-06-03 16:48:52 +0000123 F->setName("llvm.x86.fma" + Name.substr(8));
124 NewFn = F;
125 return true;
Craig Topper720c7bd2012-06-03 08:07:25 +0000126 }
Craig Topper3b1817d2012-02-03 06:10:55 +0000127 break;
128 }
Chris Lattnerb372f662011-06-18 18:56:39 +0000129 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000130
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000131 // This may not belong here. This function is effectively being overloaded
132 // to both detect an intrinsic which needs upgrading, and to provide the
133 // upgraded form of the intrinsic. We should perhaps have two separate
Chandler Carruth7132e002007-08-04 01:51:18 +0000134 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000135 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000136}
137
Evan Cheng0e179d02007-12-17 22:33:23 +0000138bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
139 NewFn = 0;
140 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000141
142 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000143 if (NewFn)
144 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +0000145 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000146 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000147 return Upgraded;
148}
149
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000150bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000151 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000152 return false;
153}
154
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000155// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
156// upgraded intrinsic. All argument and return casting must be provided in
Chandler Carruth7132e002007-08-04 01:51:18 +0000157// order to seamlessly integrate with existing context.
158void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000159 Function *F = CI->getCalledFunction();
Nick Lewycky2eb3ade2011-12-12 22:59:34 +0000160 LLVMContext &C = CI->getContext();
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000161 IRBuilder<> Builder(C);
162 Builder.SetInsertPoint(CI->getParent(), CI);
163
Craig Topper3b1817d2012-02-03 06:10:55 +0000164 assert(F && "Intrinsic call is not direct?");
165
166 if (!NewFn) {
167 // Get the Function's name.
168 StringRef Name = F->getName();
169
170 Value *Rep;
171 // Upgrade packed integer vector compares intrinsics to compare instructions
172 if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
173 Name.startswith("llvm.x86.avx2.pcmpeq.")) {
174 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
175 "pcmpeq");
176 // need to sign extend since icmp returns vector of i1
177 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
178 } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
179 Name.startswith("llvm.x86.avx2.pcmpgt.")) {
180 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
181 "pcmpgt");
182 // need to sign extend since icmp returns vector of i1
183 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper7daf8972012-05-08 06:58:15 +0000184 } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
185 Name == "llvm.x86.avx.movnt.ps.256" ||
186 Name == "llvm.x86.avx.movnt.pd.256") {
187 IRBuilder<> Builder(C);
188 Builder.SetInsertPoint(CI->getParent(), CI);
189
190 Module *M = F->getParent();
191 SmallVector<Value *, 1> Elts;
192 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
193 MDNode *Node = MDNode::get(C, Elts);
194
195 Value *Arg0 = CI->getArgOperand(0);
196 Value *Arg1 = CI->getArgOperand(1);
197
198 // Convert the type of the pointer to a pointer to the stored type.
199 Value *BC = Builder.CreateBitCast(Arg0,
200 PointerType::getUnqual(Arg1->getType()),
201 "cast");
202 StoreInst *SI = Builder.CreateStore(Arg1, BC);
203 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
204 SI->setAlignment(16);
205
206 // Remove intrinsic.
207 CI->eraseFromParent();
208 return;
Craig Topper3352ba52012-06-09 16:46:13 +0000209 } else if (Name.startswith("llvm.x86.xop.vpcom")) {
210 Intrinsic::ID intID;
211 if (Name.endswith("ub"))
212 intID = Intrinsic::x86_xop_vpcomub;
213 else if (Name.endswith("uw"))
214 intID = Intrinsic::x86_xop_vpcomuw;
215 else if (Name.endswith("ud"))
216 intID = Intrinsic::x86_xop_vpcomud;
217 else if (Name.endswith("uq"))
218 intID = Intrinsic::x86_xop_vpcomuq;
219 else if (Name.endswith("b"))
220 intID = Intrinsic::x86_xop_vpcomb;
221 else if (Name.endswith("w"))
222 intID = Intrinsic::x86_xop_vpcomw;
223 else if (Name.endswith("d"))
224 intID = Intrinsic::x86_xop_vpcomd;
225 else if (Name.endswith("q"))
226 intID = Intrinsic::x86_xop_vpcomq;
227 else
228 llvm_unreachable("Unknown suffix");
229
230 Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
231 unsigned Imm;
232 if (Name.startswith("lt"))
233 Imm = 0;
234 else if (Name.startswith("le"))
235 Imm = 1;
236 else if (Name.startswith("gt"))
237 Imm = 2;
238 else if (Name.startswith("ge"))
239 Imm = 3;
240 else if (Name.startswith("eq"))
241 Imm = 4;
242 else if (Name.startswith("ne"))
243 Imm = 5;
244 else if (Name.startswith("true"))
245 Imm = 6;
246 else if (Name.startswith("false"))
247 Imm = 7;
248 else
249 llvm_unreachable("Unknown condition");
250
251 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
252 Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
253 CI->getArgOperand(1), Builder.getInt8(Imm));
Craig Topper3b1817d2012-02-03 06:10:55 +0000254 } else {
Craig Topperd3c9e402012-04-18 05:24:00 +0000255 bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
Craig Topper7daf8972012-05-08 06:58:15 +0000256 if (Name == "llvm.x86.avx.vpermil.pd.256")
Craig Topperd3c9e402012-04-18 05:24:00 +0000257 PD256 = true;
Craig Topper7daf8972012-05-08 06:58:15 +0000258 else if (Name == "llvm.x86.avx.vpermil.pd")
Craig Topperd3c9e402012-04-18 05:24:00 +0000259 PD128 = true;
Craig Topper7daf8972012-05-08 06:58:15 +0000260 else if (Name == "llvm.x86.avx.vpermil.ps.256")
Craig Topperd3c9e402012-04-18 05:24:00 +0000261 PS256 = true;
Craig Topper7daf8972012-05-08 06:58:15 +0000262 else if (Name == "llvm.x86.avx.vpermil.ps")
Craig Topperd3c9e402012-04-18 05:24:00 +0000263 PS128 = true;
264
265 if (PD256 || PD128 || PS256 || PS128) {
266 Value *Op0 = CI->getArgOperand(0);
267 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
268 SmallVector<Constant*, 8> Idxs;
269
270 if (PD128)
271 for (unsigned i = 0; i != 2; ++i)
272 Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
273 else if (PD256)
274 for (unsigned l = 0; l != 4; l+=2)
275 for (unsigned i = 0; i != 2; ++i)
276 Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
277 else if (PS128)
278 for (unsigned i = 0; i != 4; ++i)
279 Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
280 else if (PS256)
281 for (unsigned l = 0; l != 8; l+=4)
282 for (unsigned i = 0; i != 4; ++i)
283 Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
284 else
285 llvm_unreachable("Unexpected function");
286
287 Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
288 } else {
289 llvm_unreachable("Unknown function for CallInst upgrade.");
290 }
Craig Topper3b1817d2012-02-03 06:10:55 +0000291 }
292
293 CI->replaceAllUsesWith(Rep);
294 CI->eraseFromParent();
295 return;
296 }
297
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000298 StringRef Name = CI->getName();
299
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000300 switch (NewFn->getIntrinsicID()) {
301 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +0000302 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000303
304 case Intrinsic::ctlz:
Nuno Lopesad40c0a2012-05-22 15:25:31 +0000305 case Intrinsic::cttz:
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000306 assert(CI->getNumArgOperands() == 1 &&
307 "Mismatch between function args and call args");
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000308 CI->setName(Name + ".old");
309 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
310 Builder.getFalse(), Name));
311 CI->eraseFromParent();
312 return;
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000313
Joel Jones43cb8782012-07-13 23:25:25 +0000314 case Intrinsic::arm_neon_vclz: {
315 // Change name from llvm.arm.neon.vclz.* to llvm.ctlz.*
316 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
317 Builder.getFalse(),
318 "llvm.ctlz." + Name.substr(14)));
319 CI->eraseFromParent();
320 return;
321 }
322
Craig Topper71dc02d2012-06-13 07:18:53 +0000323 case Intrinsic::x86_xop_vfrcz_ss:
324 case Intrinsic::x86_xop_vfrcz_sd:
325 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
326 Name));
327 CI->eraseFromParent();
328 return;
329
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000330 case Intrinsic::x86_sse41_ptestc:
331 case Intrinsic::x86_sse41_ptestz:
Craig Topper71dc02d2012-06-13 07:18:53 +0000332 case Intrinsic::x86_sse41_ptestnzc: {
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000333 // The arguments for these intrinsics used to be v4f32, and changed
334 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
335 // So, the only thing required is a bitcast for both arguments.
336 // First, check the arguments have the old type.
337 Value *Arg0 = CI->getArgOperand(0);
338 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
339 return;
340
341 // Old intrinsic, add bitcasts
342 Value *Arg1 = CI->getArgOperand(1);
343
344 Value *BC0 =
345 Builder.CreateBitCast(Arg0,
346 VectorType::get(Type::getInt64Ty(C), 2),
347 "cast");
348 Value *BC1 =
349 Builder.CreateBitCast(Arg1,
350 VectorType::get(Type::getInt64Ty(C), 2),
351 "cast");
352
353 CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
354 CI->replaceAllUsesWith(NewCall);
355 CI->eraseFromParent();
356 return;
Evan Cheng0e179d02007-12-17 22:33:23 +0000357 }
Craig Topper71dc02d2012-06-13 07:18:53 +0000358 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000359}
360
361// This tests each Function to determine if it needs upgrading. When we find
362// one we are interested in, we then upgrade all calls to reflect the new
363// function.
364void llvm::UpgradeCallsToIntrinsic(Function* F) {
365 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
366
367 // Upgrade the function and check if it is a totaly new function.
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000368 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +0000369 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000370 if (NewFn != F) {
371 // Replace all uses to the old function with the new one if necessary.
372 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
373 UI != UE; ) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000374 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Chandler Carruth7132e002007-08-04 01:51:18 +0000375 UpgradeIntrinsicCall(CI, NewFn);
376 }
377 // Remove old function, no longer used, from the module.
378 F->eraseFromParent();
379 }
380 }
381}
Devang Patel80ae3492009-08-28 23:24:31 +0000382