Bring back int_x86_sse2_movl_dq intrinsic for backward compatibility. Make sure
[oota-llvm.git] / lib / VMCore / AutoUpgrade.cpp
1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chandler Carruth and is distributed under the 
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the auto-upgrade helper functions 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/AutoUpgrade.h"
15 #include "llvm/Constants.h"
16 #include "llvm/Function.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/ParameterAttributes.h"
20 #include "llvm/Intrinsics.h"
21 using namespace llvm;
22
23
24 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
25   assert(F && "Illegal to upgrade a non-existent Function.");
26
27   // Get the Function's name.
28   const std::string& Name = F->getName();
29
30   // Convenience
31   const FunctionType *FTy = F->getFunctionType();
32
33   // Quickly eliminate it, if it's not a candidate.
34   if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' || 
35       Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
36     return false;
37
38   Module *M = F->getParent();
39   switch (Name[5]) {
40   default: break;
41   case 'b':
42     //  This upgrades the name of the llvm.bswap intrinsic function to only use 
43     //  a single type name for overloading. We only care about the old format
44     //  'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being 
45     //  a '.' after 'bswap.'
46     if (Name.compare(5,6,"bswap.",6) == 0) {
47       std::string::size_type delim = Name.find('.',11);
48       
49       if (delim != std::string::npos) {
50         //  Construct the new name as 'llvm.bswap' + '.i*'
51         F->setName(Name.substr(0,10)+Name.substr(delim));
52         NewFn = F;
53         return true;
54       }
55     }
56     break;
57
58   case 'c':
59     //  We only want to fix the 'llvm.ct*' intrinsics which do not have the 
60     //  correct return type, so we check for the name, and then check if the 
61     //  return type does not match the parameter type.
62     if ( (Name.compare(5,5,"ctpop",5) == 0 ||
63           Name.compare(5,4,"ctlz",4) == 0 ||
64           Name.compare(5,4,"cttz",4) == 0) &&
65         FTy->getReturnType() != FTy->getParamType(0)) {
66       //  We first need to change the name of the old (bad) intrinsic, because 
67       //  its type is incorrect, but we cannot overload that name. We 
68       //  arbitrarily unique it here allowing us to construct a correctly named 
69       //  and typed function below.
70       F->setName("");
71
72       //  Now construct the new intrinsic with the correct name and type. We 
73       //  leave the old function around in order to query its type, whatever it 
74       //  may be, and correctly convert up to the new type.
75       NewFn = cast<Function>(M->getOrInsertFunction(Name, 
76                                                     FTy->getParamType(0),
77                                                     FTy->getParamType(0),
78                                                     (Type *)0));
79       return true;
80     }
81     break;
82
83   case 'p':
84     //  This upgrades the llvm.part.select overloaded intrinsic names to only 
85     //  use one type specifier in the name. We only care about the old format
86     //  'llvm.part.select.i*.i*', and solve as above with bswap.
87     if (Name.compare(5,12,"part.select.",12) == 0) {
88       std::string::size_type delim = Name.find('.',17);
89       
90       if (delim != std::string::npos) {
91         //  Construct a new name as 'llvm.part.select' + '.i*'
92         F->setName(Name.substr(0,16)+Name.substr(delim));
93         NewFn = F;
94         return true;
95       }
96       break;
97     }
98
99     //  This upgrades the llvm.part.set intrinsics similarly as above, however 
100     //  we care about 'llvm.part.set.i*.i*.i*', but only the first two types 
101     //  must match. There is an additional type specifier after these two 
102     //  matching types that we must retain when upgrading.  Thus, we require 
103     //  finding 2 periods, not just one, after the intrinsic name.
104     if (Name.compare(5,9,"part.set.",9) == 0) {
105       std::string::size_type delim = Name.find('.',14);
106
107       if (delim != std::string::npos &&
108           Name.find('.',delim+1) != std::string::npos) {
109         //  Construct a new name as 'llvm.part.select' + '.i*.i*'
110         F->setName(Name.substr(0,13)+Name.substr(delim));
111         NewFn = F;
112         return true;
113       }
114       break;
115     }
116
117     break;
118   case 'x': 
119     // This fixes all MMX shift intrinsic instructions to take a
120     // v1i64 instead of a v2i32 as the second parameter.
121     if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
122         (Name.compare(13,4,"psll", 4) == 0 ||
123          Name.compare(13,4,"psra", 4) == 0 ||
124          Name.compare(13,4,"psrl", 4) == 0)) {
125       
126       const llvm::Type *VT = VectorType::get(IntegerType::get(64), 1);
127       
128       // We don't have to do anything if the parameter already has
129       // the correct type.
130       if (FTy->getParamType(1) == VT)
131         break;
132       
133       //  We first need to change the name of the old (bad) intrinsic, because 
134       //  its type is incorrect, but we cannot overload that name. We 
135       //  arbitrarily unique it here allowing us to construct a correctly named 
136       //  and typed function below.
137       F->setName("");
138
139       assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
140       
141       //  Now construct the new intrinsic with the correct name and type. We 
142       //  leave the old function around in order to query its type, whatever it 
143       //  may be, and correctly convert up to the new type.
144       NewFn = cast<Function>(M->getOrInsertFunction(Name, 
145                                                     FTy->getReturnType(),
146                                                     FTy->getParamType(0),
147                                                     VT,
148                                                     (Type *)0));
149       return true;
150     } else if (Name.compare(5,16,"x86.sse2.movl.dq",16) == 0) {
151       // Calls to this intrinsic are transformed into ShuffleVector's.
152       NewFn = 0;
153       return true;
154     }
155
156     break;
157   }
158
159   //  This may not belong here. This function is effectively being overloaded 
160   //  to both detect an intrinsic which needs upgrading, and to provide the 
161   //  upgraded form of the intrinsic. We should perhaps have two separate 
162   //  functions for this.
163   return false;
164 }
165
166 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
167   NewFn = 0;
168   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
169
170   // Upgrade intrinsic attributes.  This does not change the function.
171   if (NewFn)
172     F = NewFn;
173   if (unsigned id = F->getIntrinsicID(true))
174     F->setParamAttrs(Intrinsic::getParamAttrs((Intrinsic::ID)id));
175   return Upgraded;
176 }
177
178 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 
179 // upgraded intrinsic. All argument and return casting must be provided in 
180 // order to seamlessly integrate with existing context.
181 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
182   Function *F = CI->getCalledFunction();
183   assert(F && "CallInst has no function associated with it.");
184
185   if (!NewFn) {
186     switch(F->getIntrinsicID()) {
187     default:  assert(0 && "Unknown function for CallInst upgrade.");
188     case Intrinsic::x86_sse2_movl_dq: {
189       std::vector<Constant*> Idxs;
190       Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
191       Idxs.push_back(Zero);
192       Idxs.push_back(Zero);
193       Idxs.push_back(Zero);
194       Idxs.push_back(Zero);
195       Value *ZeroV = ConstantVector::get(Idxs);
196
197       Idxs.clear(); 
198       Idxs.push_back(ConstantInt::get(Type::Int32Ty, 4));
199       Idxs.push_back(ConstantInt::get(Type::Int32Ty, 5));
200       Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2));
201       Idxs.push_back(ConstantInt::get(Type::Int32Ty, 3));
202       Value *Mask = ConstantVector::get(Idxs);
203       ShuffleVectorInst *SI = new ShuffleVectorInst(ZeroV, CI->getOperand(1),
204                                                     Mask, "upgraded", CI);
205
206       // Handle any uses of the old CallInst.
207       if (!CI->use_empty())
208         //  Replace all uses of the old call with the new cast which has the 
209         //  correct type.
210         CI->replaceAllUsesWith(SI);
211       
212       //  Clean up the old call now that it has been completely upgraded.
213       CI->eraseFromParent();
214       break;
215     }
216     }
217     return;
218   }
219
220   switch(NewFn->getIntrinsicID()) {
221   default:  assert(0 && "Unknown function for CallInst upgrade.");
222   case Intrinsic::x86_mmx_psll_d:
223   case Intrinsic::x86_mmx_psll_q:
224   case Intrinsic::x86_mmx_psll_w:
225   case Intrinsic::x86_mmx_psra_d:
226   case Intrinsic::x86_mmx_psra_w:
227   case Intrinsic::x86_mmx_psrl_d:
228   case Intrinsic::x86_mmx_psrl_q:
229   case Intrinsic::x86_mmx_psrl_w: {
230     SmallVector<Value*, 2> Operands;
231     
232     Operands.push_back(CI->getOperand(1));
233     
234     // Cast the second parameter to the correct type.
235     BitCastInst *BC = new BitCastInst(CI->getOperand(2), 
236                                       NewFn->getFunctionType()->getParamType(1),
237                                       "upgraded", CI);
238     Operands.push_back(BC);
239     
240     //  Construct a new CallInst
241     CallInst *NewCI = new CallInst(NewFn, Operands.begin(), Operands.end(), 
242                                    "upgraded."+CI->getName(), CI);
243     NewCI->setTailCall(CI->isTailCall());
244     NewCI->setCallingConv(CI->getCallingConv());
245     
246     //  Handle any uses of the old CallInst.
247     if (!CI->use_empty())
248       //  Replace all uses of the old call with the new cast which has the 
249       //  correct type.
250       CI->replaceAllUsesWith(NewCI);
251     
252     //  Clean up the old call now that it has been completely upgraded.
253     CI->eraseFromParent();
254     break;
255   }        
256   case Intrinsic::ctlz:
257   case Intrinsic::ctpop:
258   case Intrinsic::cttz:
259     //  Build a small vector of the 1..(N-1) operands, which are the 
260     //  parameters.
261     SmallVector<Value*, 8>   Operands(CI->op_begin()+1, CI->op_end());
262
263     //  Construct a new CallInst
264     CallInst *NewCI = new CallInst(NewFn, Operands.begin(), Operands.end(), 
265                                    "upgraded."+CI->getName(), CI);
266     NewCI->setTailCall(CI->isTailCall());
267     NewCI->setCallingConv(CI->getCallingConv());
268
269     //  Handle any uses of the old CallInst.
270     if (!CI->use_empty()) {
271       //  Check for sign extend parameter attributes on the return values.
272       bool SrcSExt = NewFn->getParamAttrs() &&
273                      NewFn->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt);
274       bool DestSExt = F->getParamAttrs() &&
275                       F->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt);
276       
277       //  Construct an appropriate cast from the new return type to the old.
278       CastInst *RetCast = CastInst::create(
279                             CastInst::getCastOpcode(NewCI, SrcSExt,
280                                                     F->getReturnType(),
281                                                     DestSExt),
282                             NewCI, F->getReturnType(),
283                             NewCI->getName(), CI);
284       NewCI->moveBefore(RetCast);
285
286       //  Replace all uses of the old call with the new cast which has the 
287       //  correct type.
288       CI->replaceAllUsesWith(RetCast);
289     }
290
291     //  Clean up the old call now that it has been completely upgraded.
292     CI->eraseFromParent();
293     break;
294   }
295 }
296
297 // This tests each Function to determine if it needs upgrading. When we find 
298 // one we are interested in, we then upgrade all calls to reflect the new 
299 // function.
300 void llvm::UpgradeCallsToIntrinsic(Function* F) {
301   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
302
303   // Upgrade the function and check if it is a totaly new function.
304   Function* NewFn;
305   if (UpgradeIntrinsicFunction(F, NewFn)) {
306     if (NewFn != F) {
307       // Replace all uses to the old function with the new one if necessary.
308       for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
309            UI != UE; ) {
310         if (CallInst* CI = dyn_cast<CallInst>(*UI++))
311           UpgradeIntrinsicCall(CI, NewFn);
312       }
313       // Remove old function, no longer used, from the module.
314       F->eraseFromParent();
315     }
316   }
317 }
318