1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the auto-upgrade helper functions
12 //===----------------------------------------------------------------------===//
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"
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";
39 static inline const Type* getTypeFromFunctionName(Function* F) {
40 // If there's no function, we can't get the argument type.
44 // Get the Function's name.
45 const std::string& Name = F->getName();
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] != '.')
54 if (Name == "llvm.bswap")
55 return F->getReturnType();
58 if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
59 return F->getReturnType();
62 if (Name == "llvm.isunordered") {
63 Function::const_arg_iterator ArgIt = F->arg_begin();
64 if (ArgIt != F->arg_end())
65 return ArgIt->getType();
69 if (Name == "llvm.sqrt")
70 return F->getReturnType();
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())
84 return Ty->getUnsignedVersion();
87 if (Ty == Type::BoolTy) {
88 Function::const_arg_iterator ArgIt = F->arg_begin();
89 if (ArgIt != F->arg_end())
90 return ArgIt->getType();
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] != '.')
103 if (Name == "llvm.bswap")
107 if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
111 if (Name == "llvm.isunordered")
115 if (Name == "llvm.sqrt")
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)) {
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))
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,
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);
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");
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()) {
176 new CastInst(*PI,opTy->getUnsignedVersion(), "autoupgrade_cast");
177 BB->getInstList().push_back(cast);
178 Oprnds.push_back(cast);
181 Oprnds.push_back(*PI);
184 Instruction* result = new CallInst(F,Oprnds,"autoupgrade_call");
185 if (isTailCall) cast<CallInst>(result)->setTailCall();
186 if (CallingConv) cast<CallInst>(result)->setCallingConv(CallingConv);
188 const Type* newTy = F->getReturnType()->getUnsignedVersion();
189 CastInst* final = new CastInst(result, newTy, "autoupgrade_uncast");
190 BB->getInstList().push_back(result);
196 Instruction* llvm::UpgradeIntrinsicCall(CallInst *CI, Function* newF) {
197 Function *F = CI->getCalledFunction();
199 (newF ? getTypeFromFunction(newF) : getTypeFromFunctionName(F))) {
200 std::vector<Value*> Oprnds;
201 User::op_iterator OI = CI->op_begin();
203 for (User::op_iterator OE = CI->op_end() ; OI != OE; ++OI) {
204 const Type* opTy = OI->get()->getType();
205 if (opTy->isSigned())
207 new CastInst(OI->get(),opTy->getUnsignedVersion(),
208 "autoupgrade_cast",CI));
210 Oprnds.push_back(*OI);
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()) {
218 new CastInst(newCI, oldType, "autoupgrade_uncast",newCI);
219 newCI->moveBefore(final);
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();
231 if (CallInst* CI = dyn_cast<CallInst>(*UI++)) {
232 std::vector<Value*> Oprnds;
233 User::op_iterator OI = CI->op_begin();
235 for (User::op_iterator OE = CI->op_end(); OI != OE; ++OI) {
236 const Type* opTy = OI->get()->getType();
237 if (opTy->isSigned()) {
239 new CastInst(OI->get(),opTy->getUnsignedVersion(),
240 "autoupgrade_cast",CI));
243 Oprnds.push_back(*OI);
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()) {
251 new CastInst(newCI, Ty, "autoupgrade_uncast",newCI);
252 newCI->moveBefore(final);
253 CI->replaceAllUsesWith(final);
255 CI->replaceAllUsesWith(newCI);
257 CI->eraseFromParent();
261 F->eraseFromParent();