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 *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";
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()) {
46 return M->getOrInsertFunction(Name,
47 Type::UByteTy, Type::UByteTy, NULL);
48 case Type::UShortTyID:
50 return M->getOrInsertFunction(Name,
51 Type::UShortTy, Type::UShortTy, NULL);
54 return M->getOrInsertFunction(Name,
55 Type::UIntTy, Type::UIntTy, NULL);
58 return M->getOrInsertFunction(Name,
59 Type::ULongTy, Type::ULongTy, NULL);
63 static Function *getUpgradedIntrinsic(Function *F) {
64 // If there's no function, we can't get the argument type.
67 // Get the Function's name.
68 const std::string& Name = F->getName();
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] != '.')
75 Module *M = F->getParent();
79 if (Name == "llvm.bswap") return getUpgradedUnaryFn(F);
82 if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
83 return getUpgradedUnaryFn(F);
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());
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());
103 if (Name == "llvm.sqrt")
104 return getUpgradedUnaryFn(F);
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())
116 return Ty->getUnsignedVersion();
119 if (Ty == Type::BoolTy) {
120 Function::const_arg_iterator ArgIt = F->arg_begin();
121 if (ArgIt != F->arg_end())
122 return ArgIt->getType();
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";
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");
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()) {
157 new CastInst(*PI,opTy->getUnsignedVersion(), "autoupgrade_cast");
158 BB->getInstList().push_back(cast);
159 Oprnds.push_back(cast);
162 Oprnds.push_back(*PI);
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);
170 const Type* newTy = F->getReturnType()->getUnsignedVersion();
171 CastInst* final = new CastInst(result, newTy, "autoupgrade_uncast");
172 BB->getInstList().push_back(result);
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();
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);
192 CallInst *NewCI = new CallInst(NewFn, Oprnds, CI->getName(), CI);
193 NewCI->setTailCall(CI->isTailCall());
194 NewCI->setCallingConv(CI->getCallingConv());
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);
203 CI->replaceAllUsesWith(RetVal);
205 CI->eraseFromParent();
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();
212 if (CallInst* CI = dyn_cast<CallInst>(*UI++)) {
213 std::vector<Value*> Oprnds;
214 User::op_iterator OI = CI->op_begin();
216 for (User::op_iterator OE = CI->op_end(); OI != OE; ++OI) {
217 const Type* opTy = OI->get()->getType();
218 if (opTy->isSigned()) {
220 new CastInst(OI->get(),opTy->getUnsignedVersion(),
221 "autoupgrade_cast",CI));
223 Oprnds.push_back(*OI);
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()) {
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);
238 CI->replaceAllUsesWith(newCI);
240 CI->eraseFromParent();
244 F->eraseFromParent();