Fix auto-upgrade of intrinsics to work properly with both assembly and
[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 Reid Spencer 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/Assembly/AutoUpgrade.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Function.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Intrinsics.h"
20 #include "llvm/SymbolTable.h"
21 #include <iostream>
22
23 using namespace llvm;
24
25 // Utility function for getting the correct suffix given a type
26 static inline const char* get_suffix(const Type* Ty) {
27   switch (Ty->getTypeID()) {
28     case Type::UIntTyID:    return ".i32";
29     case Type::UShortTyID:  return ".i16";
30     case Type::UByteTyID:   return ".i8";
31     case Type::ULongTyID:   return ".i64";
32     case Type::FloatTyID:   return ".f32";
33     case Type::DoubleTyID:  return ".f64";
34     default:                break;                        
35   }
36   return 0;
37 }
38
39 static inline const Type* getTypeFromFunctionName(Function* F) {
40   // If there's no function, we can't get the argument type.
41   if (!F)
42     return 0;
43
44   // Get the Function's name.
45   const std::string& Name = F->getName();
46
47   // Quickly eliminate it, if it's not a candidate.
48   if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' || Name[2] !=
49     'v' || Name[3] != 'm' || Name[4] != '.')
50     return 0;
51
52   switch (Name[5]) {
53     case 'b':
54       if (Name == "llvm.bswap")
55         return F->getReturnType();
56       break;
57     case 'c':
58       if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
59         return F->getReturnType();
60       break;
61     case 'i':
62       if (Name == "llvm.isunordered") {
63         Function::const_arg_iterator ArgIt = F->arg_begin();
64         if (ArgIt != F->arg_end()) 
65           return ArgIt->getType();
66       }
67       break;
68     case 's':
69       if (Name == "llvm.sqrt")
70         return F->getReturnType();
71       break;
72     default:
73       break;
74   }
75   return 0;
76 }
77
78 // This assumes the Function is one of the intrinsics we upgraded.
79 static inline const Type* getTypeFromFunction(Function *F) {
80   const Type* Ty = F->getReturnType();
81   if (Ty->isFloatingPoint())
82     return Ty;
83   if (Ty->isSigned())
84     return Ty->getUnsignedVersion();
85   if (Ty->isInteger())
86     return Ty;
87   if (Ty == Type::BoolTy) {
88     Function::const_arg_iterator ArgIt = F->arg_begin();
89     if (ArgIt != F->arg_end()) 
90       return ArgIt->getType();
91   }
92   return 0;
93 }
94
95 bool llvm::IsUpgradeableIntrinsicName(const std::string& Name) {
96   // Quickly eliminate it, if it's not a candidate.
97   if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' || Name[2] !=
98     'v' || Name[3] != 'm' || Name[4] != '.')
99     return false;
100
101   switch (Name[5]) {
102     case 'b':
103       if (Name == "llvm.bswap")
104         return true;
105       break;
106     case 'c':
107       if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
108         return true;
109       break;
110     case 'i':
111       if (Name == "llvm.isunordered")
112         return true;
113       break;
114     case 's':
115       if (Name == "llvm.sqrt")
116         return true;
117       break;
118     default:
119       break;
120   }
121   return false;
122 }
123
124 // UpgradeIntrinsicFunction - Convert overloaded intrinsic function names to
125 // their non-overloaded variants by appending the appropriate suffix based on
126 // the argument types.
127 Function* llvm::UpgradeIntrinsicFunction(Function* F) {
128   // See if its one of the name's we're interested in.
129   if (const Type* Ty = getTypeFromFunctionName(F)) {
130     const char* suffix = 
131       get_suffix((Ty->isSigned() ? Ty->getUnsignedVersion() : Ty));
132     assert(suffix && "Intrinsic parameter type not recognized");
133     const std::string& Name = F->getName();
134     std::string new_name = Name + suffix;
135     std::cerr << "WARNING: change " << Name << " to " << new_name << "\n";
136     SymbolTable& SymTab = F->getParent()->getSymbolTable();
137     if (Value* V = SymTab.lookup(F->getType(),new_name))
138       if (Function* OtherF = dyn_cast<Function>(V))
139         return OtherF;
140     
141     // There wasn't an existing function for the intrinsic, so now make sure the
142     // signedness of the arguments is correct.
143     if (Ty->isSigned()) {
144       const Type* newTy = Ty->getUnsignedVersion();
145       std::vector<const Type*> Params;
146       Params.push_back(newTy);
147       FunctionType* FT = FunctionType::get(newTy, Params,false);
148       return new Function(FT, GlobalValue::ExternalLinkage, new_name, 
149                           F->getParent());
150     }
151
152     // The argument was the correct type (unsigned or floating), so just
153     // rename the function to its correct name and return it.
154     F->setName(new_name);
155     return F;
156   }
157   return 0;
158 }
159
160
161 Instruction* llvm::MakeUpgradedCall(
162     Function* F, const std::vector<Value*>& Params, BasicBlock* BB,
163     bool isTailCall, unsigned CallingConv) {
164   assert(F && "Need a Function to make a CallInst");
165   assert(BB && "Need a BasicBlock to make a CallInst");
166
167   // Convert the params
168   bool signedArg = false;
169   std::vector<Value*> Oprnds;
170   for (std::vector<Value*>::const_iterator PI = Params.begin(), 
171        PE = Params.end(); PI != PE; ++PI) {
172     const Type* opTy = (*PI)->getType();
173     if (opTy->isSigned()) {
174       signedArg = true;
175       CastInst* cast = 
176         new CastInst(*PI,opTy->getUnsignedVersion(), "autoupgrade_cast");
177       BB->getInstList().push_back(cast);
178       Oprnds.push_back(cast);
179     }
180     else
181       Oprnds.push_back(*PI);
182   }
183
184   Instruction* result = new CallInst(F,Oprnds,"autoupgrade_call");
185   if (isTailCall) cast<CallInst>(result)->setTailCall();
186   if (CallingConv) cast<CallInst>(result)->setCallingConv(CallingConv);
187   if (signedArg) {
188     const Type* newTy = F->getReturnType()->getUnsignedVersion();
189     CastInst* final = new CastInst(result, newTy, "autoupgrade_uncast");
190     BB->getInstList().push_back(result);
191     result = final;
192   }
193   return result;
194 }
195
196 Instruction* llvm::UpgradeIntrinsicCall(CallInst *CI, Function* newF) {
197   Function *F = CI->getCalledFunction();
198   if (const Type* Ty = 
199       (newF ? getTypeFromFunction(newF) : getTypeFromFunctionName(F))) {
200     std::vector<Value*> Oprnds;
201     User::op_iterator OI = CI->op_begin(); 
202     ++OI;
203     for (User::op_iterator OE = CI->op_end() ; OI != OE; ++OI) {
204       const Type* opTy = OI->get()->getType();
205       if (opTy->isSigned())
206         Oprnds.push_back(
207           new CastInst(OI->get(),opTy->getUnsignedVersion(), 
208             "autoupgrade_cast",CI));
209       else
210         Oprnds.push_back(*OI);
211     }
212     CallInst* newCI = new CallInst((newF?newF:F),Oprnds,"autoupgrade_call",CI);
213     newCI->setTailCall(CI->isTailCall());
214     newCI->setCallingConv(CI->getCallingConv());
215     if (const Type* oldType = CI->getCalledFunction()->getReturnType())
216       if (oldType->isSigned()) {
217         CastInst* final = 
218           new CastInst(newCI, oldType, "autoupgrade_uncast",newCI);
219         newCI->moveBefore(final);
220         return final;
221       }
222     return newCI;
223   }
224   return 0;
225 }
226
227 bool llvm::UpgradeCallsToIntrinsic(Function* F) {
228   if (Function* newF = UpgradeIntrinsicFunction(F)) {
229     for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
230          UI != UE; ) {
231       if (CallInst* CI = dyn_cast<CallInst>(*UI++)) {
232         std::vector<Value*> Oprnds;
233         User::op_iterator OI = CI->op_begin();
234         ++OI;
235         for (User::op_iterator OE = CI->op_end(); OI != OE; ++OI) {
236           const Type* opTy = OI->get()->getType();
237           if (opTy->isSigned()) {
238             Oprnds.push_back(
239               new CastInst(OI->get(),opTy->getUnsignedVersion(), 
240                   "autoupgrade_cast",CI));
241           }
242           else
243             Oprnds.push_back(*OI);
244         }
245         CallInst* newCI = new CallInst(newF,Oprnds,"autoupgrade_call",CI);
246         newCI->setTailCall(CI->isTailCall());
247         newCI->setCallingConv(CI->getCallingConv());
248         if (const Type* Ty = CI->getCalledFunction()->getReturnType())
249           if (Ty->isSigned()) {
250             CastInst* final = 
251               new CastInst(newCI, Ty, "autoupgrade_uncast",newCI);
252             newCI->moveBefore(final);
253             CI->replaceAllUsesWith(final);
254           } else {
255             CI->replaceAllUsesWith(newCI);
256           }
257         CI->eraseFromParent();
258       }
259     }
260     if (newF != F)
261       F->eraseFromParent();
262     return true;
263   }
264   return false;
265 }