Rather than having special rules like "intrinsics cannot
[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 static Function* UpgradeIntrinsicFunction1(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 Function* llvm::UpgradeIntrinsicFunction(Function *F) {
123   Function *Upgraded = UpgradeIntrinsicFunction1(F);
124
125   // Upgrade intrinsic attributes.  This does not change the function.
126   if (Upgraded)
127     F = Upgraded;
128   if (unsigned id = F->getIntrinsicID(true))
129     F->setParamAttrs(Intrinsic::getParamAttrs((Intrinsic::ID)id));
130   return Upgraded;
131 }
132
133 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 
134 // upgraded intrinsic. All argument and return casting must be provided in 
135 // order to seamlessly integrate with existing context.
136 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
137   assert(NewFn && "Cannot upgrade an intrinsic call without a new function.");
138
139   Function *F = CI->getCalledFunction();
140   assert(F && "CallInst has no function associated with it.");
141   
142   switch(NewFn->getIntrinsicID()) {
143   default:  assert(0 && "Unknown function for CallInst upgrade.");
144   case Intrinsic::ctlz:
145   case Intrinsic::ctpop:
146   case Intrinsic::cttz:
147     //  Build a small vector of the 1..(N-1) operands, which are the 
148     //  parameters.
149     SmallVector<Value*, 8>   Operands(CI->op_begin()+1, CI->op_end());
150
151     //  Construct a new CallInst
152     CallInst *NewCI = new CallInst(NewFn, Operands.begin(), Operands.end(), 
153                                    "upgraded."+CI->getName(), CI);
154     NewCI->setTailCall(CI->isTailCall());
155     NewCI->setCallingConv(CI->getCallingConv());
156
157     //  Handle any uses of the old CallInst.
158     if (!CI->use_empty()) {
159       //  Check for sign extend parameter attributes on the return values.
160       bool SrcSExt = NewFn->getParamAttrs() &&
161                      NewFn->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt);
162       bool DestSExt = F->getParamAttrs() &&
163                       F->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt);
164       
165       //  Construct an appropriate cast from the new return type to the old.
166       CastInst *RetCast = CastInst::create(
167                             CastInst::getCastOpcode(NewCI, SrcSExt,
168                                                     F->getReturnType(),
169                                                     DestSExt),
170                             NewCI, F->getReturnType(),
171                             NewCI->getName(), CI);
172       NewCI->moveBefore(RetCast);
173
174       //  Replace all uses of the old call with the new cast which has the 
175       //  correct type.
176       CI->replaceAllUsesWith(RetCast);
177     }
178
179     //  Clean up the old call now that it has been completely upgraded.
180     CI->eraseFromParent();
181     break;
182   }
183 }
184
185 // This tests each Function to determine if it needs upgrading. When we find 
186 // one we are interested in, we then upgrade all calls to reflect the new 
187 // function.
188 void llvm::UpgradeCallsToIntrinsic(Function* F) {
189   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
190
191   // Upgrade the function and check if it is a totaly new function.
192   if (Function* NewFn = UpgradeIntrinsicFunction(F)) {
193     if (NewFn != F) {
194       // Replace all uses to the old function with the new one if necessary.
195       for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
196            UI != UE; ) {
197         if (CallInst* CI = dyn_cast<CallInst>(*UI++))
198           UpgradeIntrinsicCall(CI, NewFn);
199       }
200       // Remove old function, no longer used, from the module.
201       F->eraseFromParent();
202     }
203   }
204 }
205