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 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] != '.')
86 if (Name == "llvm.bswap")
90 if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
94 if (Name == "llvm.isunordered")
98 if (Name == "llvm.sqrt")
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)) {
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))
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,
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);
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();
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);
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();
169 if (CallInst* CI = dyn_cast<CallInst>(*UI++)) {
170 std::vector<Value*> Oprnds;
171 User::op_iterator OI = CI->op_begin();
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);
185 CI->replaceAllUsesWith(newCI);
187 CI->eraseFromParent();
191 F->eraseFromParent();