Split memcpy/memset/memmove intrinsics into i32/i64 versions, resolving
[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 *getTypeSuffix(const Type* Ty) {
27   switch (Ty->getTypeID()) {
28   case Type::ULongTyID:   return ".i64";
29   case Type::UIntTyID:    return ".i32";
30   case Type::UShortTyID:  return ".i16";
31   case Type::UByteTyID:   return ".i8";
32   case Type::FloatTyID:   return ".f32";
33   case Type::DoubleTyID:  return ".f64";
34   default:                break;                        
35   }
36   return 0;
37 }
38
39 static Function *getUpgradedUnaryFn(Function *F) {
40   std::string Name = F->getName()+getTypeSuffix(F->getReturnType());
41   Module *M = F->getParent();
42   switch (F->getReturnType()->getTypeID()) {
43   default: return 0;
44   case Type::UByteTyID:
45   case Type::SByteTyID:
46     return M->getOrInsertFunction(Name, 
47                                   Type::UByteTy, Type::UByteTy, NULL);
48   case Type::UShortTyID:
49   case Type::ShortTyID:
50     return M->getOrInsertFunction(Name, 
51                                   Type::UShortTy, Type::UShortTy, NULL);
52   case Type::UIntTyID:
53   case Type::IntTyID:
54     return M->getOrInsertFunction(Name, 
55                                   Type::UIntTy, Type::UIntTy, NULL);
56   case Type::ULongTyID:
57   case Type::LongTyID:
58     return M->getOrInsertFunction(Name, 
59                                   Type::ULongTy, Type::ULongTy, NULL);
60 }
61 }
62
63 static Function *getUpgradedIntrinsic(Function *F) {
64   // If there's no function, we can't get the argument type.
65   if (!F) return 0;
66
67   // Get the Function's name.
68   const std::string& Name = F->getName();
69
70   // Quickly eliminate it, if it's not a candidate.
71   if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' || 
72       Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
73     return 0;
74
75   Module *M = F->getParent();
76   switch (Name[5]) {
77   default: break;
78   case 'b':
79     if (Name == "llvm.bswap") return getUpgradedUnaryFn(F);
80     break;
81   case 'c':
82     if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
83       return getUpgradedUnaryFn(F);
84     break;
85   case 'i':
86     if (Name == "llvm.isunordered" && F->arg_begin() != F->arg_end()) {
87       if (F->arg_begin()->getType() == Type::FloatTy)
88         return M->getOrInsertFunction(Name+".f32", F->getFunctionType());
89       if (F->arg_begin()->getType() == Type::DoubleTy)
90         return M->getOrInsertFunction(Name+".f64", F->getFunctionType());
91     }
92     break;
93   case 'm':
94     if (Name == "llvm.memcpy" || Name == "llvm.memset" || 
95         Name == "llvm.memmove") {
96       if (F->getFunctionType()->getParamType(2) == Type::UIntTy)
97         return M->getOrInsertFunction(Name+".i32", F->getFunctionType());
98       if (F->getFunctionType()->getParamType(2) == Type::ULongTy)
99         return M->getOrInsertFunction(Name+".i64", F->getFunctionType());
100     }
101     break;
102   case 's':
103     if (Name == "llvm.sqrt")
104       return getUpgradedUnaryFn(F);
105     break;
106   }
107   return 0;
108 }
109
110 // This assumes the Function is one of the intrinsics we upgraded.
111 static inline const Type* getTypeFromFunction(Function *F) {
112   const Type* Ty = F->getReturnType();
113   if (Ty->isFloatingPoint())
114     return Ty;
115   if (Ty->isSigned())
116     return Ty->getUnsignedVersion();
117   if (Ty->isInteger())
118     return Ty;
119   if (Ty == Type::BoolTy) {
120     Function::const_arg_iterator ArgIt = F->arg_begin();
121     if (ArgIt != F->arg_end()) 
122       return ArgIt->getType();
123   }
124   return 0;
125 }
126
127 // UpgradeIntrinsicFunction - Convert overloaded intrinsic function names to
128 // their non-overloaded variants by appending the appropriate suffix based on
129 // the argument types.
130 Function *llvm::UpgradeIntrinsicFunction(Function* F) {
131   // See if its one of the name's we're interested in.
132   if (Function *R = getUpgradedIntrinsic(F)) {
133     std::cerr << "WARNING: change " << F->getName() << " to "
134               << R->getName() << "\n";
135     return R;
136   }
137   return 0;
138 }
139
140
141 Instruction* llvm::MakeUpgradedCall(Function *F, 
142                                     const std::vector<Value*> &Params,
143                                     BasicBlock *BB, bool isTailCall,
144                                     unsigned CallingConv) {
145   assert(F && "Need a Function to make a CallInst");
146   assert(BB && "Need a BasicBlock to make a CallInst");
147
148   // Convert the params
149   bool signedArg = false;
150   std::vector<Value*> Oprnds;
151   for (std::vector<Value*>::const_iterator PI = Params.begin(), 
152        PE = Params.end(); PI != PE; ++PI) {
153     const Type* opTy = (*PI)->getType();
154     if (opTy->isSigned()) {
155       signedArg = true;
156       CastInst* cast = 
157         new CastInst(*PI,opTy->getUnsignedVersion(), "autoupgrade_cast");
158       BB->getInstList().push_back(cast);
159       Oprnds.push_back(cast);
160     }
161     else
162       Oprnds.push_back(*PI);
163   }
164
165   Instruction *result = new CallInst(F, Oprnds);
166   if (result->getType() != Type::VoidTy) result->setName("autoupgrade_call");
167   if (isTailCall) cast<CallInst>(result)->setTailCall();
168   if (CallingConv) cast<CallInst>(result)->setCallingConv(CallingConv);
169   if (signedArg) {
170     const Type* newTy = F->getReturnType()->getUnsignedVersion();
171     CastInst* final = new CastInst(result, newTy, "autoupgrade_uncast");
172     BB->getInstList().push_back(result);
173     result = final;
174   }
175   return result;
176 }
177
178 // UpgradeIntrinsicCall - In the BC reader, change a call to some intrinsic to
179 // be a called to the specified intrinsic.  We expect the callees to have the
180 // same number of arguments, but their types may be different.
181 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
182   Function *F = CI->getCalledFunction();
183
184   const FunctionType *NewFnTy = NewFn->getFunctionType();
185   std::vector<Value*> Oprnds;
186   for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) {
187     Value *V = CI->getOperand(i);
188     if (V->getType() != NewFnTy->getParamType(i-1))
189       V = new CastInst(V, NewFnTy->getParamType(i-1), V->getName(), CI);
190     Oprnds.push_back(V);
191   }
192   CallInst *NewCI = new CallInst(NewFn, Oprnds, CI->getName(), CI);
193   NewCI->setTailCall(CI->isTailCall());
194   NewCI->setCallingConv(CI->getCallingConv());
195   
196   if (!CI->use_empty()) {
197     Instruction *RetVal = NewCI;
198     if (F->getReturnType() != NewFn->getReturnType()) {
199       RetVal = new CastInst(NewCI, NewFn->getReturnType(), 
200                             NewCI->getName(), CI);
201       NewCI->moveBefore(RetVal);
202     }
203     CI->replaceAllUsesWith(RetVal);
204   }
205   CI->eraseFromParent();
206 }
207
208 bool llvm::UpgradeCallsToIntrinsic(Function* F) {
209   if (Function* newF = UpgradeIntrinsicFunction(F)) {
210     for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
211          UI != UE; ) {
212       if (CallInst* CI = dyn_cast<CallInst>(*UI++)) {
213         std::vector<Value*> Oprnds;
214         User::op_iterator OI = CI->op_begin();
215         ++OI;
216         for (User::op_iterator OE = CI->op_end(); OI != OE; ++OI) {
217           const Type* opTy = OI->get()->getType();
218           if (opTy->isSigned()) {
219             Oprnds.push_back(
220               new CastInst(OI->get(),opTy->getUnsignedVersion(), 
221                   "autoupgrade_cast",CI));
222           } else {
223             Oprnds.push_back(*OI);
224           }
225         }
226         CallInst* newCI = new CallInst(newF, Oprnds,
227                                        CI->hasName() ? "autoupcall" : "", CI);
228         newCI->setTailCall(CI->isTailCall());
229         newCI->setCallingConv(CI->getCallingConv());
230         if (CI->use_empty()) {
231           // noop
232         } else if (CI->getType() != newCI->getType()) {
233           CastInst *final = new CastInst(newCI, CI->getType(),
234                                          "autoupgrade_uncast", newCI);
235           newCI->moveBefore(final);
236           CI->replaceAllUsesWith(final);
237         } else {
238           CI->replaceAllUsesWith(newCI);
239         }
240         CI->eraseFromParent();
241       }
242     }
243     if (newF != F)
244       F->eraseFromParent();
245     return true;
246   }
247   return false;
248 }