blob: 4976c22ad534aaad1754e459a51c7518bdeb03dc [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"
Bill Wendling45449b12011-08-25 23:22:40 +000017#include "llvm/Instruction.h"
Owen Anderson155dccd82009-07-07 23:43:39 +000018#include "llvm/LLVMContext.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000019#include "llvm/Module.h"
Devang Patel80ae3492009-08-28 23:24:31 +000020#include "llvm/IntrinsicInst.h"
Gabor Greife5406532010-06-23 08:45:32 +000021#include "llvm/Support/CallSite.h"
Bill Wendling45449b12011-08-25 23:22:40 +000022#include "llvm/Support/CFG.h"
Torok Edwin56d06592009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Eric Christopher64831c62010-04-20 00:59:54 +000024#include "llvm/Support/IRBuilder.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;
Chandler Carruth58a71ed2011-12-12 04:26:04 +000055 case 'c': {
Chandler Carruth58a71ed2011-12-12 04:26:04 +000056 if (Name.startswith("ctlz.") && F->arg_size() == 1) {
57 F->setName(Name + ".old");
Chandler Carruthd4a02402011-12-12 10:57:20 +000058 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
59 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +000060 return true;
61 }
62 if (Name.startswith("cttz.") && F->arg_size() == 1) {
63 F->setName(Name + ".old");
Chandler Carruthd4a02402011-12-12 10:57:20 +000064 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
65 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +000066 return true;
67 }
68 break;
69 }
Craig Topper3b1817d2012-02-03 06:10:55 +000070 case 'x': {
71 if (Name.startswith("x86.sse2.pcmpeq.") ||
72 Name.startswith("x86.sse2.pcmpgt.") ||
73 Name.startswith("x86.avx2.pcmpeq.") ||
Craig Topperd3c9e402012-04-18 05:24:00 +000074 Name.startswith("x86.avx2.pcmpgt.") ||
Craig Topper7daf8972012-05-08 06:58:15 +000075 Name.startswith("x86.avx.vpermil.") ||
76 Name == "x86.avx.movnt.dq.256" ||
77 Name == "x86.avx.movnt.pd.256" ||
Craig Topper3352ba52012-06-09 16:46:13 +000078 Name == "x86.avx.movnt.ps.256" ||
79 (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
Craig Topper3b1817d2012-02-03 06:10:55 +000080 NewFn = 0;
81 return true;
82 }
Nadav Rotem17ee58a2012-06-10 18:42:51 +000083 // SSE4.1 ptest functions may have an old signature.
84 if (Name.startswith("x86.sse41.ptest")) {
85 if (Name == "x86.sse41.ptestc")
86 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
87 if (Name == "x86.sse41.ptestz")
88 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
89 if (Name == "x86.sse41.ptestnzc")
90 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
91 }
Craig Topper720c7bd2012-06-03 08:07:25 +000092 // Fix the FMA4 intrinsics to remove the 4
93 if (Name.startswith("x86.fma4.")) {
Craig Topper2c5ccd82012-06-03 16:48:52 +000094 F->setName("llvm.x86.fma" + Name.substr(8));
95 NewFn = F;
96 return true;
Craig Topper720c7bd2012-06-03 08:07:25 +000097 }
Craig Topper3b1817d2012-02-03 06:10:55 +000098 break;
99 }
Chris Lattnerb372f662011-06-18 18:56:39 +0000100 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000101
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000102 // This may not belong here. This function is effectively being overloaded
103 // to both detect an intrinsic which needs upgrading, and to provide the
104 // upgraded form of the intrinsic. We should perhaps have two separate
Chandler Carruth7132e002007-08-04 01:51:18 +0000105 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000106 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000107}
108
Evan Cheng0e179d02007-12-17 22:33:23 +0000109bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
110 NewFn = 0;
111 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000112
113 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000114 if (NewFn)
115 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +0000116 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000117 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000118 return Upgraded;
119}
120
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000121bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000122 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000123 return false;
124}
125
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000126// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
127// upgraded intrinsic. All argument and return casting must be provided in
Chandler Carruth7132e002007-08-04 01:51:18 +0000128// order to seamlessly integrate with existing context.
129void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000130 Function *F = CI->getCalledFunction();
Nick Lewycky2eb3ade2011-12-12 22:59:34 +0000131 LLVMContext &C = CI->getContext();
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000132 IRBuilder<> Builder(C);
133 Builder.SetInsertPoint(CI->getParent(), CI);
134
Craig Topper3b1817d2012-02-03 06:10:55 +0000135 assert(F && "Intrinsic call is not direct?");
136
137 if (!NewFn) {
138 // Get the Function's name.
139 StringRef Name = F->getName();
140
141 Value *Rep;
142 // Upgrade packed integer vector compares intrinsics to compare instructions
143 if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
144 Name.startswith("llvm.x86.avx2.pcmpeq.")) {
145 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
146 "pcmpeq");
147 // need to sign extend since icmp returns vector of i1
148 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
149 } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
150 Name.startswith("llvm.x86.avx2.pcmpgt.")) {
151 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
152 "pcmpgt");
153 // need to sign extend since icmp returns vector of i1
154 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper7daf8972012-05-08 06:58:15 +0000155 } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
156 Name == "llvm.x86.avx.movnt.ps.256" ||
157 Name == "llvm.x86.avx.movnt.pd.256") {
158 IRBuilder<> Builder(C);
159 Builder.SetInsertPoint(CI->getParent(), CI);
160
161 Module *M = F->getParent();
162 SmallVector<Value *, 1> Elts;
163 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
164 MDNode *Node = MDNode::get(C, Elts);
165
166 Value *Arg0 = CI->getArgOperand(0);
167 Value *Arg1 = CI->getArgOperand(1);
168
169 // Convert the type of the pointer to a pointer to the stored type.
170 Value *BC = Builder.CreateBitCast(Arg0,
171 PointerType::getUnqual(Arg1->getType()),
172 "cast");
173 StoreInst *SI = Builder.CreateStore(Arg1, BC);
174 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
175 SI->setAlignment(16);
176
177 // Remove intrinsic.
178 CI->eraseFromParent();
179 return;
Craig Topper3352ba52012-06-09 16:46:13 +0000180 } else if (Name.startswith("llvm.x86.xop.vpcom")) {
181 Intrinsic::ID intID;
182 if (Name.endswith("ub"))
183 intID = Intrinsic::x86_xop_vpcomub;
184 else if (Name.endswith("uw"))
185 intID = Intrinsic::x86_xop_vpcomuw;
186 else if (Name.endswith("ud"))
187 intID = Intrinsic::x86_xop_vpcomud;
188 else if (Name.endswith("uq"))
189 intID = Intrinsic::x86_xop_vpcomuq;
190 else if (Name.endswith("b"))
191 intID = Intrinsic::x86_xop_vpcomb;
192 else if (Name.endswith("w"))
193 intID = Intrinsic::x86_xop_vpcomw;
194 else if (Name.endswith("d"))
195 intID = Intrinsic::x86_xop_vpcomd;
196 else if (Name.endswith("q"))
197 intID = Intrinsic::x86_xop_vpcomq;
198 else
199 llvm_unreachable("Unknown suffix");
200
201 Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
202 unsigned Imm;
203 if (Name.startswith("lt"))
204 Imm = 0;
205 else if (Name.startswith("le"))
206 Imm = 1;
207 else if (Name.startswith("gt"))
208 Imm = 2;
209 else if (Name.startswith("ge"))
210 Imm = 3;
211 else if (Name.startswith("eq"))
212 Imm = 4;
213 else if (Name.startswith("ne"))
214 Imm = 5;
215 else if (Name.startswith("true"))
216 Imm = 6;
217 else if (Name.startswith("false"))
218 Imm = 7;
219 else
220 llvm_unreachable("Unknown condition");
221
222 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
223 Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
224 CI->getArgOperand(1), Builder.getInt8(Imm));
Craig Topper3b1817d2012-02-03 06:10:55 +0000225 } else {
Craig Topperd3c9e402012-04-18 05:24:00 +0000226 bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
Craig Topper7daf8972012-05-08 06:58:15 +0000227 if (Name == "llvm.x86.avx.vpermil.pd.256")
Craig Topperd3c9e402012-04-18 05:24:00 +0000228 PD256 = true;
Craig Topper7daf8972012-05-08 06:58:15 +0000229 else if (Name == "llvm.x86.avx.vpermil.pd")
Craig Topperd3c9e402012-04-18 05:24:00 +0000230 PD128 = true;
Craig Topper7daf8972012-05-08 06:58:15 +0000231 else if (Name == "llvm.x86.avx.vpermil.ps.256")
Craig Topperd3c9e402012-04-18 05:24:00 +0000232 PS256 = true;
Craig Topper7daf8972012-05-08 06:58:15 +0000233 else if (Name == "llvm.x86.avx.vpermil.ps")
Craig Topperd3c9e402012-04-18 05:24:00 +0000234 PS128 = true;
235
236 if (PD256 || PD128 || PS256 || PS128) {
237 Value *Op0 = CI->getArgOperand(0);
238 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
239 SmallVector<Constant*, 8> Idxs;
240
241 if (PD128)
242 for (unsigned i = 0; i != 2; ++i)
243 Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
244 else if (PD256)
245 for (unsigned l = 0; l != 4; l+=2)
246 for (unsigned i = 0; i != 2; ++i)
247 Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
248 else if (PS128)
249 for (unsigned i = 0; i != 4; ++i)
250 Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
251 else if (PS256)
252 for (unsigned l = 0; l != 8; l+=4)
253 for (unsigned i = 0; i != 4; ++i)
254 Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
255 else
256 llvm_unreachable("Unexpected function");
257
258 Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
259 } else {
260 llvm_unreachable("Unknown function for CallInst upgrade.");
261 }
Craig Topper3b1817d2012-02-03 06:10:55 +0000262 }
263
264 CI->replaceAllUsesWith(Rep);
265 CI->eraseFromParent();
266 return;
267 }
268
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000269 StringRef Name = CI->getName();
270
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000271 switch (NewFn->getIntrinsicID()) {
272 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +0000273 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000274
275 case Intrinsic::ctlz:
Nuno Lopesad40c0a2012-05-22 15:25:31 +0000276 case Intrinsic::cttz:
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000277 assert(CI->getNumArgOperands() == 1 &&
278 "Mismatch between function args and call args");
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000279 CI->setName(Name + ".old");
280 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
281 Builder.getFalse(), Name));
282 CI->eraseFromParent();
283 return;
Nadav Rotem17ee58a2012-06-10 18:42:51 +0000284
285 case Intrinsic::x86_sse41_ptestc:
286 case Intrinsic::x86_sse41_ptestz:
287 case Intrinsic::x86_sse41_ptestnzc:
288 // The arguments for these intrinsics used to be v4f32, and changed
289 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
290 // So, the only thing required is a bitcast for both arguments.
291 // First, check the arguments have the old type.
292 Value *Arg0 = CI->getArgOperand(0);
293 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
294 return;
295
296 // Old intrinsic, add bitcasts
297 Value *Arg1 = CI->getArgOperand(1);
298
299 Value *BC0 =
300 Builder.CreateBitCast(Arg0,
301 VectorType::get(Type::getInt64Ty(C), 2),
302 "cast");
303 Value *BC1 =
304 Builder.CreateBitCast(Arg1,
305 VectorType::get(Type::getInt64Ty(C), 2),
306 "cast");
307
308 CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
309 CI->replaceAllUsesWith(NewCall);
310 CI->eraseFromParent();
311 return;
Evan Cheng0e179d02007-12-17 22:33:23 +0000312 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000313}
314
315// This tests each Function to determine if it needs upgrading. When we find
316// one we are interested in, we then upgrade all calls to reflect the new
317// function.
318void llvm::UpgradeCallsToIntrinsic(Function* F) {
319 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
320
321 // Upgrade the function and check if it is a totaly new function.
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000322 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +0000323 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000324 if (NewFn != F) {
325 // Replace all uses to the old function with the new one if necessary.
326 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
327 UI != UE; ) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000328 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Chandler Carruth7132e002007-08-04 01:51:18 +0000329 UpgradeIntrinsicCall(CI, NewFn);
330 }
331 // Remove old function, no longer used, from the module.
332 F->eraseFromParent();
333 }
334 }
335}
Devang Patel80ae3492009-08-28 23:24:31 +0000336