For PR411:
[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 bool llvm::IsUpgradeableIntrinsicName(const std::string& Name) {
79   // Quickly eliminate it, if it's not a candidate.
80   if (Name.length() <= 5 || Name[0] != 'l' || Name[1] != 'l' || Name[2] !=
81     'v' || Name[3] != 'm' || Name[4] != '.')
82     return false;
83
84   switch (Name[5]) {
85     case 'b':
86       if (Name == "llvm.bswap")
87         return true;
88       break;
89     case 'c':
90       if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
91         return true;
92       break;
93     case 'i':
94       if (Name == "llvm.isunordered")
95         return true;
96       break;
97     case 's':
98       if (Name == "llvm.sqrt")
99         return true;
100       break;
101     default:
102       break;
103   }
104   return false;
105 }
106
107 // UpgradeIntrinsicFunction - Convert overloaded intrinsic function names to
108 // their non-overloaded variants by appending the appropriate suffix based on
109 // the argument types.
110 Function* llvm::UpgradeIntrinsicFunction(Function* F) {
111   // See if its one of the name's we're interested in.
112   if (const Type* Ty = getTypeFromFunctionName(F)) {
113     const char* suffix = 
114       get_suffix((Ty->isSigned() ? Ty->getUnsignedVersion() : Ty));
115     assert(suffix && "Intrinsic parameter type not recognized");
116     const std::string& Name = F->getName();
117     std::string new_name = Name + suffix;
118     std::cerr << "WARNING: change " << Name << " to " << new_name << "\n";
119     SymbolTable& SymTab = F->getParent()->getSymbolTable();
120     if (Value* V = SymTab.lookup(F->getType(),new_name))
121       if (Function* OtherF = dyn_cast<Function>(V))
122         return OtherF;
123     
124     // There wasn't an existing function for the intrinsic, so now make sure the
125     // signedness of the arguments is correct.
126     if (Ty->isSigned()) {
127       const Type* newTy = Ty->getUnsignedVersion();
128       std::vector<const Type*> Params;
129       Params.push_back(newTy);
130       FunctionType* FT = FunctionType::get(newTy, Params,false);
131       return new Function(FT, GlobalValue::ExternalLinkage, new_name, 
132                           F->getParent());
133     }
134
135     // The argument was the correct type (unsigned or floating), so just
136     // rename the function to its correct name and return it.
137     F->setName(new_name);
138     return F;
139   }
140   return 0;
141 }
142
143 Instruction* llvm::UpgradeIntrinsicCall(CallInst *CI) {
144   Function *F = CI->getCalledFunction();
145   if (const Type* Ty = getTypeFromFunctionName(F)) {
146     Function* newF = UpgradeIntrinsicFunction(F);
147     std::vector<Value*> Oprnds;
148     for (User::op_iterator OI = CI->op_begin(), OE = CI->op_end(); 
149          OI != OE; ++OI)
150       Oprnds.push_back(CI);
151     CallInst* newCI = new CallInst(newF,Oprnds,"autoupgrade_call",CI);
152     if (Ty->isSigned()) {
153       const Type* newTy = Ty->getUnsignedVersion();
154       newCI->setOperand(1,new CastInst(newCI->getOperand(1), newTy, 
155                      "autoupgrade_cast", newCI));
156       CastInst* final = new CastInst(newCI, Ty, "autoupgrade_uncast",newCI);
157       newCI->moveBefore(final);
158       return final;
159     }
160     return newCI;
161   }
162   return 0;
163 }
164
165 bool llvm::UpgradeCallsToIntrinsic(Function* F) {
166   if (Function* newF = UpgradeIntrinsicFunction(F)) {
167     for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
168          UI != UE; ) {
169       if (CallInst* CI = dyn_cast<CallInst>(*UI++)) {
170         std::vector<Value*> Oprnds;
171         User::op_iterator OI = CI->op_begin();
172         ++OI;
173         for (User::op_iterator OE = CI->op_end(); OI != OE; ++OI)
174           Oprnds.push_back(*OI);
175         CallInst* newCI = new CallInst(newF,Oprnds,"autoupgrade_call",CI);
176         const Type* Ty = Oprnds[0]->getType();
177         if (Ty->isSigned()) {
178           const Type* newTy = Ty->getUnsignedVersion();
179           newCI->setOperand(1,new CastInst(newCI->getOperand(1), newTy, 
180                          "autoupgrade_cast", newCI));
181           CastInst* final = new CastInst(newCI, Ty, "autoupgrade_uncast",newCI);
182           newCI->moveBefore(final);
183           CI->replaceAllUsesWith(final);
184         } else {
185           CI->replaceAllUsesWith(newCI);
186         }
187         CI->eraseFromParent();
188       }
189     }
190     if (newF != F)
191       F->eraseFromParent();
192     return true;
193   }
194   return false;
195 }