Now with less tabs!
[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/Function.h"
16 #include "llvm/Module.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/ParameterAttributes.h"
19 #include "llvm/Intrinsics.h"
20 using namespace llvm;
21
22
23 Function* llvm::UpgradeIntrinsicFunction(Function *F) {
24   assert(F && "Illegal to upgrade a non-existent Function.");
25
26   // Get the Function's name.
27   const std::string& Name = F->getName();
28
29   // Convenience
30   const FunctionType *FTy = F->getFunctionType();
31
32   // Quickly eliminate it, if it's not a candidate.
33   if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' || 
34       Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
35     return 0;
36
37   Module *M = F->getParent();
38   switch (Name[5]) {
39   default: break;
40   case 'b':
41     //  This upgrades the name of the llvm.bswap intrinsic function to only use 
42     //  a single type name for overloading. We only care about the old format
43     //  'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being 
44     //  a '.' after 'bswap.'
45     if (Name.compare(5,6,"bswap.",6) == 0) {
46       std::string::size_type delim = Name.find('.',11);
47       
48       if (delim != std::string::npos) {
49         //  Construct the new name as 'llvm.bswap' + '.i*'
50         F->setName(Name.substr(0,10)+Name.substr(delim));
51         return F;
52       }
53     }
54     break;
55
56   case 'c':
57     //  We only want to fix the 'llvm.ct*' intrinsics which do not have the 
58     //  correct return type, so we check for the name, and then check if the 
59     //  return type does not match the parameter type.
60     if ( (Name.compare(5,5,"ctpop",5) == 0 ||
61           Name.compare(5,4,"ctlz",4) == 0 ||
62           Name.compare(5,4,"cttz",4) == 0) &&
63         FTy->getReturnType() != FTy->getParamType(0)) {
64       //  We first need to change the name of the old (bad) intrinsic, because 
65       //  its type is incorrect, but we cannot overload that name. We 
66       //  arbitrarily unique it here allowing us to construct a correctly named 
67       //  and typed function below.
68       F->setName("");
69
70       //  Now construct the new intrinsic with the correct name and type. We 
71       //  leave the old function around in order to query its type, whatever it 
72       //  may be, and correctly convert up to the new type.
73       return cast<Function>(M->getOrInsertFunction(Name, 
74                                                    FTy->getParamType(0),
75                                                    FTy->getParamType(0),
76                                                    (Type *)0));
77     }
78     break;
79
80   case 'p':
81     //  This upgrades the llvm.part.select overloaded intrinsic names to only 
82     //  use one type specifier in the name. We only care about the old format
83     //  'llvm.part.select.i*.i*', and solve as above with bswap.
84     if (Name.compare(5,12,"part.select.",12) == 0) {
85       std::string::size_type delim = Name.find('.',17);
86       
87       if (delim != std::string::npos) {
88         //  Construct a new name as 'llvm.part.select' + '.i*'
89         F->setName(Name.substr(0,16)+Name.substr(delim));
90         return F;
91       }
92       break;
93     }
94
95     //  This upgrades the llvm.part.set intrinsics similarly as above, however 
96     //  we care about 'llvm.part.set.i*.i*.i*', but only the first two types 
97     //  must match. There is an additional type specifier after these two 
98     //  matching types that we must retain when upgrading.  Thus, we require 
99     //  finding 2 periods, not just one, after the intrinsic name.
100     if (Name.compare(5,9,"part.set.",9) == 0) {
101       std::string::size_type delim = Name.find('.',14);
102
103       if (delim != std::string::npos &&
104           Name.find('.',delim+1) != std::string::npos) {
105         //  Construct a new name as 'llvm.part.select' + '.i*.i*'
106         F->setName(Name.substr(0,13)+Name.substr(delim));
107         return F;
108       }
109       break;
110     }
111
112     break;
113   }
114
115   //  This may not belong here. This function is effectively being overloaded 
116   //  to both detect an intrinsic which needs upgrading, and to provide the 
117   //  upgraded form of the intrinsic. We should perhaps have two separate 
118   //  functions for this.
119   return 0;
120 }
121
122 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 
123 // upgraded intrinsic. All argument and return casting must be provided in 
124 // order to seamlessly integrate with existing context.
125 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
126   assert(NewFn && "Cannot upgrade an intrinsic call without a new function.");
127
128   Function *F = CI->getCalledFunction();
129   assert(F && "CallInst has no function associated with it.");
130
131   const FunctionType *FTy = F->getFunctionType();
132   const FunctionType *NewFnTy = NewFn->getFunctionType();
133   
134   switch(NewFn->getIntrinsicID()) {
135   default:  assert(0 && "Unknown function for CallInst upgrade.");
136   case Intrinsic::ctlz:
137   case Intrinsic::ctpop:
138   case Intrinsic::cttz:
139     //  Build a small vector of the 1..(N-1) operands, which are the 
140     //  parameters.
141     SmallVector<Value*, 8>   Operands(CI->op_begin()+1, CI->op_end());
142
143     //  Construct a new CallInst
144     CallInst *NewCI = new CallInst(NewFn, Operands.begin(), Operands.end(), 
145                                    "upgraded."+CI->getName(), CI);
146     NewCI->setTailCall(CI->isTailCall());
147     NewCI->setCallingConv(CI->getCallingConv());
148
149     //  Handle any uses of the old CallInst.
150     if (!CI->use_empty()) {
151       //  Check for sign extend parameter attributes on the return values.
152       bool SrcSExt = NewFnTy->getParamAttrs() &&
153                      NewFnTy->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt);
154       bool DestSExt = FTy->getParamAttrs() &&
155                       FTy->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt);
156       
157       //  Construct an appropriate cast from the new return type to the old.
158       CastInst *RetCast = CastInst::create(
159                             CastInst::getCastOpcode(NewCI, SrcSExt,
160                                                     F->getReturnType(),
161                                                     DestSExt),
162                             NewCI, F->getReturnType(),
163                             NewCI->getName(), CI);
164       NewCI->moveBefore(RetCast);
165
166       //  Replace all uses of the old call with the new cast which has the 
167       //  correct type.
168       CI->replaceAllUsesWith(RetCast);
169     }
170
171     //  Clean up the old call now that it has been completely upgraded.
172     CI->eraseFromParent();
173     break;
174   }
175 }
176
177 // This tests each Function to determine if it needs upgrading. When we find 
178 // one we are interested in, we then upgrade all calls to reflect the new 
179 // function.
180 void llvm::UpgradeCallsToIntrinsic(Function* F) {
181   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
182
183   // Upgrade the function and check if it is a totaly new function.
184   if (Function* NewFn = UpgradeIntrinsicFunction(F)) {
185     if (NewFn != F) {
186       // Replace all uses to the old function with the new one if necessary.
187       for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
188            UI != UE; ) {
189         if (CallInst* CI = dyn_cast<CallInst>(*UI++))
190           UpgradeIntrinsicCall(CI, NewFn);
191       }
192       // Remove old function, no longer used, from the module.
193       F->eraseFromParent();
194     }
195   }
196 }
197