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"
24 static Function *getUpgradedUnaryFn(Function *F) {
25 const std::string &Name = F->getName();
26 Module *M = F->getParent();
27 switch (F->getReturnType()->getTypeID()) {
31 return M->getOrInsertFunction(Name+".i8",
32 Type::UByteTy, Type::UByteTy, NULL);
33 case Type::UShortTyID:
35 return M->getOrInsertFunction(Name+".i16",
36 Type::UShortTy, Type::UShortTy, NULL);
39 return M->getOrInsertFunction(Name+".i32",
40 Type::UIntTy, Type::UIntTy, NULL);
43 return M->getOrInsertFunction(Name+".i64",
44 Type::ULongTy, Type::ULongTy, NULL);
46 return M->getOrInsertFunction(Name+".f32",
47 Type::FloatTy, Type::FloatTy, NULL);
48 case Type::DoubleTyID:
49 return M->getOrInsertFunction(Name+".f64",
50 Type::DoubleTy, Type::DoubleTy, NULL);
54 static Function *getUpgradedIntrinsic(Function *F) {
55 // If there's no function, we can't get the argument type.
58 // Get the Function's name.
59 const std::string& Name = F->getName();
61 // Quickly eliminate it, if it's not a candidate.
62 if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
63 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
66 Module *M = F->getParent();
70 if (Name == "llvm.bswap") return getUpgradedUnaryFn(F);
73 if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
74 return getUpgradedUnaryFn(F);
77 if (Name == "llvm.isunordered" && F->arg_begin() != F->arg_end()) {
78 if (F->arg_begin()->getType() == Type::FloatTy)
79 return M->getOrInsertFunction(Name+".f32", F->getFunctionType());
80 if (F->arg_begin()->getType() == Type::DoubleTy)
81 return M->getOrInsertFunction(Name+".f64", F->getFunctionType());
85 if (Name == "llvm.memcpy" || Name == "llvm.memset" ||
86 Name == "llvm.memmove") {
87 if (F->getFunctionType()->getParamType(2) == Type::UIntTy)
88 return M->getOrInsertFunction(Name+".i32", F->getFunctionType());
89 if (F->getFunctionType()->getParamType(2) == Type::ULongTy)
90 return M->getOrInsertFunction(Name+".i64", F->getFunctionType());
94 if (Name == "llvm.sqrt")
95 return getUpgradedUnaryFn(F);
101 // UpgradeIntrinsicFunction - Convert overloaded intrinsic function names to
102 // their non-overloaded variants by appending the appropriate suffix based on
103 // the argument types.
104 Function *llvm::UpgradeIntrinsicFunction(Function* F) {
105 // See if its one of the name's we're interested in.
106 if (Function *R = getUpgradedIntrinsic(F)) {
107 std::cerr << "WARNING: change " << F->getName() << " to "
108 << R->getName() << "\n";
115 Instruction* llvm::MakeUpgradedCall(Function *F,
116 const std::vector<Value*> &Params,
117 BasicBlock *BB, bool isTailCall,
118 unsigned CallingConv) {
119 assert(F && "Need a Function to make a CallInst");
120 assert(BB && "Need a BasicBlock to make a CallInst");
122 // Convert the params
123 bool signedArg = false;
124 std::vector<Value*> Oprnds;
125 for (std::vector<Value*>::const_iterator PI = Params.begin(),
126 PE = Params.end(); PI != PE; ++PI) {
127 const Type* opTy = (*PI)->getType();
128 if (opTy->isSigned()) {
131 new CastInst(*PI,opTy->getUnsignedVersion(), "autoupgrade_cast");
132 BB->getInstList().push_back(cast);
133 Oprnds.push_back(cast);
136 Oprnds.push_back(*PI);
139 Instruction *result = new CallInst(F, Oprnds);
140 if (result->getType() != Type::VoidTy) result->setName("autoupgrade_call");
141 if (isTailCall) cast<CallInst>(result)->setTailCall();
142 if (CallingConv) cast<CallInst>(result)->setCallingConv(CallingConv);
144 const Type* newTy = F->getReturnType()->getUnsignedVersion();
145 CastInst* final = new CastInst(result, newTy, "autoupgrade_uncast");
146 BB->getInstList().push_back(result);
152 // UpgradeIntrinsicCall - In the BC reader, change a call to some intrinsic to
153 // be a called to the specified intrinsic. We expect the callees to have the
154 // same number of arguments, but their types may be different.
155 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
156 Function *F = CI->getCalledFunction();
158 const FunctionType *NewFnTy = NewFn->getFunctionType();
159 std::vector<Value*> Oprnds;
160 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) {
161 Value *V = CI->getOperand(i);
162 if (V->getType() != NewFnTy->getParamType(i-1))
163 V = new CastInst(V, NewFnTy->getParamType(i-1), V->getName(), CI);
166 CallInst *NewCI = new CallInst(NewFn, Oprnds, CI->getName(), CI);
167 NewCI->setTailCall(CI->isTailCall());
168 NewCI->setCallingConv(CI->getCallingConv());
170 if (!CI->use_empty()) {
171 Instruction *RetVal = NewCI;
172 if (F->getReturnType() != NewFn->getReturnType()) {
173 RetVal = new CastInst(NewCI, NewFn->getReturnType(),
174 NewCI->getName(), CI);
175 NewCI->moveBefore(RetVal);
177 CI->replaceAllUsesWith(RetVal);
179 CI->eraseFromParent();
182 bool llvm::UpgradeCallsToIntrinsic(Function* F) {
183 if (Function* newF = UpgradeIntrinsicFunction(F)) {
184 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
186 if (CallInst* CI = dyn_cast<CallInst>(*UI++)) {
187 std::vector<Value*> Oprnds;
188 User::op_iterator OI = CI->op_begin();
190 for (User::op_iterator OE = CI->op_end(); OI != OE; ++OI) {
191 const Type* opTy = OI->get()->getType();
192 if (opTy->isSigned()) {
194 new CastInst(OI->get(),opTy->getUnsignedVersion(),
195 "autoupgrade_cast",CI));
197 Oprnds.push_back(*OI);
200 CallInst* newCI = new CallInst(newF, Oprnds,
201 CI->hasName() ? "autoupcall" : "", CI);
202 newCI->setTailCall(CI->isTailCall());
203 newCI->setCallingConv(CI->getCallingConv());
204 if (CI->use_empty()) {
206 } else if (CI->getType() != newCI->getType()) {
207 CastInst *final = new CastInst(newCI, CI->getType(),
208 "autoupgrade_uncast", newCI);
209 newCI->moveBefore(final);
210 CI->replaceAllUsesWith(final);
212 CI->replaceAllUsesWith(newCI);
214 CI->eraseFromParent();
218 F->eraseFromParent();