All MMX shift instructions took a <2 x i32> vector as the shift amount parameter...
[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 Function* UpgradeIntrinsicFunction1(Function *F) {
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 0;
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         return F;
53       }
54     }
55     break;
56
57   case 'c':
58     //  We only want to fix the 'llvm.ct*' intrinsics which do not have the 
59     //  correct return type, so we check for the name, and then check if the 
60     //  return type does not match the parameter type.
61     if ( (Name.compare(5,5,"ctpop",5) == 0 ||
62           Name.compare(5,4,"ctlz",4) == 0 ||
63           Name.compare(5,4,"cttz",4) == 0) &&
64         FTy->getReturnType() != FTy->getParamType(0)) {
65       //  We first need to change the name of the old (bad) intrinsic, because 
66       //  its type is incorrect, but we cannot overload that name. We 
67       //  arbitrarily unique it here allowing us to construct a correctly named 
68       //  and typed function below.
69       F->setName("");
70
71       //  Now construct the new intrinsic with the correct name and type. We 
72       //  leave the old function around in order to query its type, whatever it 
73       //  may be, and correctly convert up to the new type.
74       return cast<Function>(M->getOrInsertFunction(Name, 
75                                                    FTy->getParamType(0),
76                                                    FTy->getParamType(0),
77                                                    (Type *)0));
78     }
79     break;
80
81   case 'p':
82     //  This upgrades the llvm.part.select overloaded intrinsic names to only 
83     //  use one type specifier in the name. We only care about the old format
84     //  'llvm.part.select.i*.i*', and solve as above with bswap.
85     if (Name.compare(5,12,"part.select.",12) == 0) {
86       std::string::size_type delim = Name.find('.',17);
87       
88       if (delim != std::string::npos) {
89         //  Construct a new name as 'llvm.part.select' + '.i*'
90         F->setName(Name.substr(0,16)+Name.substr(delim));
91         return F;
92       }
93       break;
94     }
95
96     //  This upgrades the llvm.part.set intrinsics similarly as above, however 
97     //  we care about 'llvm.part.set.i*.i*.i*', but only the first two types 
98     //  must match. There is an additional type specifier after these two 
99     //  matching types that we must retain when upgrading.  Thus, we require 
100     //  finding 2 periods, not just one, after the intrinsic name.
101     if (Name.compare(5,9,"part.set.",9) == 0) {
102       std::string::size_type delim = Name.find('.',14);
103
104       if (delim != std::string::npos &&
105           Name.find('.',delim+1) != std::string::npos) {
106         //  Construct a new name as 'llvm.part.select' + '.i*.i*'
107         F->setName(Name.substr(0,13)+Name.substr(delim));
108         return F;
109       }
110       break;
111     }
112
113     break;
114   case 'x': 
115     // This fixes all MMX shift intrinsic instructions to take a
116     // v1i64 instead of a v2i32 as the second parameter.
117     if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
118         (Name.compare(13,4,"psll", 4) == 0 ||
119          Name.compare(13,4,"psra", 4) == 0 ||
120          Name.compare(13,4,"psrl", 4) == 0)) {
121       
122       const llvm::Type *VT = VectorType::get(IntegerType::get(64), 1);
123       
124       // We don't have to do anything if the parameter already has
125       // the correct type.
126       if (FTy->getParamType(1) == VT)
127         break;
128       
129       //  We first need to change the name of the old (bad) intrinsic, because 
130       //  its type is incorrect, but we cannot overload that name. We 
131       //  arbitrarily unique it here allowing us to construct a correctly named 
132       //  and typed function below.
133       F->setName("");
134
135       assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
136       
137       //  Now construct the new intrinsic with the correct name and type. We 
138       //  leave the old function around in order to query its type, whatever it 
139       //  may be, and correctly convert up to the new type.
140       return cast<Function>(M->getOrInsertFunction(Name, 
141                                                    FTy->getReturnType(),
142                                                    FTy->getParamType(0),
143                                                    VT,
144                                                    (Type *)0));
145     }
146     break;
147   }
148
149   //  This may not belong here. This function is effectively being overloaded 
150   //  to both detect an intrinsic which needs upgrading, and to provide the 
151   //  upgraded form of the intrinsic. We should perhaps have two separate 
152   //  functions for this.
153   return 0;
154 }
155
156 Function* llvm::UpgradeIntrinsicFunction(Function *F) {
157   Function *Upgraded = UpgradeIntrinsicFunction1(F);
158
159   // Upgrade intrinsic attributes.  This does not change the function.
160   if (Upgraded)
161     F = Upgraded;
162   if (unsigned id = F->getIntrinsicID(true))
163     F->setParamAttrs(Intrinsic::getParamAttrs((Intrinsic::ID)id));
164   return Upgraded;
165 }
166
167 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 
168 // upgraded intrinsic. All argument and return casting must be provided in 
169 // order to seamlessly integrate with existing context.
170 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
171   assert(NewFn && "Cannot upgrade an intrinsic call without a new function.");
172
173   Function *F = CI->getCalledFunction();
174   assert(F && "CallInst has no function associated with it.");
175   
176   switch(NewFn->getIntrinsicID()) {
177   default:  assert(0 && "Unknown function for CallInst upgrade.");
178   case Intrinsic::x86_mmx_psll_d:
179   case Intrinsic::x86_mmx_psll_q:
180   case Intrinsic::x86_mmx_psll_w:
181   case Intrinsic::x86_mmx_psra_d:
182   case Intrinsic::x86_mmx_psra_w:
183   case Intrinsic::x86_mmx_psrl_d:
184   case Intrinsic::x86_mmx_psrl_q:
185   case Intrinsic::x86_mmx_psrl_w: {
186     SmallVector<Value*, 2> Operands;
187     
188     Operands.push_back(CI->getOperand(1));
189     
190     // Cast the second parameter to the correct type.
191     BitCastInst *BC = new BitCastInst(CI->getOperand(2), 
192                                       NewFn->getFunctionType()->getParamType(1),
193                                       "upgraded", CI);
194     Operands.push_back(BC);
195     
196     //  Construct a new CallInst
197     CallInst *NewCI = new CallInst(NewFn, Operands.begin(), Operands.end(), 
198                                    "upgraded."+CI->getName(), CI);
199     NewCI->setTailCall(CI->isTailCall());
200     NewCI->setCallingConv(CI->getCallingConv());
201     
202     //  Handle any uses of the old CallInst.
203     if (!CI->use_empty())
204       //  Replace all uses of the old call with the new cast which has the 
205       //  correct type.
206       CI->replaceAllUsesWith(NewCI);
207     
208     //  Clean up the old call now that it has been completely upgraded.
209     CI->eraseFromParent();
210     break;
211   }        
212   case Intrinsic::ctlz:
213   case Intrinsic::ctpop:
214   case Intrinsic::cttz:
215     //  Build a small vector of the 1..(N-1) operands, which are the 
216     //  parameters.
217     SmallVector<Value*, 8>   Operands(CI->op_begin()+1, CI->op_end());
218
219     //  Construct a new CallInst
220     CallInst *NewCI = new CallInst(NewFn, Operands.begin(), Operands.end(), 
221                                    "upgraded."+CI->getName(), CI);
222     NewCI->setTailCall(CI->isTailCall());
223     NewCI->setCallingConv(CI->getCallingConv());
224
225     //  Handle any uses of the old CallInst.
226     if (!CI->use_empty()) {
227       //  Check for sign extend parameter attributes on the return values.
228       bool SrcSExt = NewFn->getParamAttrs() &&
229                      NewFn->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt);
230       bool DestSExt = F->getParamAttrs() &&
231                       F->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt);
232       
233       //  Construct an appropriate cast from the new return type to the old.
234       CastInst *RetCast = CastInst::create(
235                             CastInst::getCastOpcode(NewCI, SrcSExt,
236                                                     F->getReturnType(),
237                                                     DestSExt),
238                             NewCI, F->getReturnType(),
239                             NewCI->getName(), CI);
240       NewCI->moveBefore(RetCast);
241
242       //  Replace all uses of the old call with the new cast which has the 
243       //  correct type.
244       CI->replaceAllUsesWith(RetCast);
245     }
246
247     //  Clean up the old call now that it has been completely upgraded.
248     CI->eraseFromParent();
249     break;
250   }
251 }
252
253 // This tests each Function to determine if it needs upgrading. When we find 
254 // one we are interested in, we then upgrade all calls to reflect the new 
255 // function.
256 void llvm::UpgradeCallsToIntrinsic(Function* F) {
257   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
258
259   // Upgrade the function and check if it is a totaly new function.
260   if (Function* NewFn = UpgradeIntrinsicFunction(F)) {
261     if (NewFn != F) {
262       // Replace all uses to the old function with the new one if necessary.
263       for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
264            UI != UE; ) {
265         if (CallInst* CI = dyn_cast<CallInst>(*UI++))
266           UpgradeIntrinsicCall(CI, NewFn);
267       }
268       // Remove old function, no longer used, from the module.
269       F->eraseFromParent();
270     }
271   }
272 }
273