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/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/Module.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/SymbolTable.h"
25 static Function *getUpgradedUnaryFn(Function *F) {
26 const std::string &Name = F->getName();
27 Module *M = F->getParent();
28 switch (F->getReturnType()->getTypeID()) {
32 return M->getOrInsertFunction(Name+".i8",
33 Type::UByteTy, Type::UByteTy, NULL);
34 case Type::UShortTyID:
36 return M->getOrInsertFunction(Name+".i16",
37 Type::UShortTy, Type::UShortTy, NULL);
40 return M->getOrInsertFunction(Name+".i32",
41 Type::UIntTy, Type::UIntTy, NULL);
44 return M->getOrInsertFunction(Name+".i64",
45 Type::ULongTy, Type::ULongTy, NULL);
47 return M->getOrInsertFunction(Name+".f32",
48 Type::FloatTy, Type::FloatTy, NULL);
49 case Type::DoubleTyID:
50 return M->getOrInsertFunction(Name+".f64",
51 Type::DoubleTy, Type::DoubleTy, NULL);
55 static Function *getUpgradedIntrinsic(Function *F) {
56 // If there's no function, we can't get the argument type.
59 // Get the Function's name.
60 const std::string& Name = F->getName();
62 // Quickly eliminate it, if it's not a candidate.
63 if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
64 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
67 Module *M = F->getParent();
71 if (Name == "llvm.bswap") return getUpgradedUnaryFn(F);
74 if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
75 return getUpgradedUnaryFn(F);
78 if (Name == "llvm.dbg.stoppoint") {
79 if (F->getReturnType() != Type::VoidTy) {
80 return M->getOrInsertFunction(Name, Type::VoidTy,
83 F->getFunctionType()->getParamType(3),
86 } else if (Name == "llvm.dbg.func.start") {
87 if (F->getReturnType() != Type::VoidTy) {
88 return M->getOrInsertFunction(Name, Type::VoidTy,
89 F->getFunctionType()->getParamType(0),
92 } else if (Name == "llvm.dbg.region.start") {
93 if (F->getReturnType() != Type::VoidTy) {
94 return M->getOrInsertFunction(Name, Type::VoidTy, NULL);
96 } else if (Name == "llvm.dbg.region.end") {
97 if (F->getReturnType() != Type::VoidTy) {
98 return M->getOrInsertFunction(Name, Type::VoidTy, NULL);
103 if (Name == "llvm.isunordered" && F->arg_begin() != F->arg_end()) {
104 if (F->arg_begin()->getType() == Type::FloatTy)
105 return M->getOrInsertFunction(Name+".f32", F->getFunctionType());
106 if (F->arg_begin()->getType() == Type::DoubleTy)
107 return M->getOrInsertFunction(Name+".f64", F->getFunctionType());
111 if (Name == "llvm.memcpy" || Name == "llvm.memset" ||
112 Name == "llvm.memmove") {
113 if (F->getFunctionType()->getParamType(2) == Type::UIntTy ||
114 F->getFunctionType()->getParamType(2) == Type::IntTy)
115 return M->getOrInsertFunction(Name+".i32", Type::VoidTy,
116 PointerType::get(Type::SByteTy),
117 F->getFunctionType()->getParamType(1),
118 Type::UIntTy, Type::UIntTy, NULL);
119 if (F->getFunctionType()->getParamType(2) == Type::ULongTy ||
120 F->getFunctionType()->getParamType(2) == Type::LongTy)
121 return M->getOrInsertFunction(Name+".i64", Type::VoidTy,
122 PointerType::get(Type::SByteTy),
123 F->getFunctionType()->getParamType(1),
124 Type::ULongTy, Type::UIntTy, NULL);
128 if (Name == "llvm.sqrt")
129 return getUpgradedUnaryFn(F);
135 // Occasionally upgraded function call site arguments need to be permutated to
136 // some new order. The result of getArgumentPermutation is an array of size
137 // F->getFunctionType()getNumParams() indicating the new operand order. A value
138 // of zero in the array indicates replacing with UndefValue for the arg type.
139 // NULL is returned if there is no permutation. It's assumed that the function
140 // name is in the form "llvm.?????"
141 static unsigned *getArgumentPermutation(Function* F) {
142 // Get the Function's name.
143 const std::string& Name = F->getName();
146 if (Name == "llvm.dbg.stoppoint") {
147 static unsigned Permutation[] = { 2, 3, 4 };
148 assert(F->getFunctionType()->getNumParams() ==
149 (sizeof(Permutation) / sizeof(unsigned)) &&
150 "Permutation is wrong length");
158 // UpgradeIntrinsicFunction - Convert overloaded intrinsic function names to
159 // their non-overloaded variants by appending the appropriate suffix based on
160 // the argument types.
161 Function *llvm::UpgradeIntrinsicFunction(Function* F) {
162 // See if its one of the name's we're interested in.
163 if (Function *R = getUpgradedIntrinsic(F)) {
164 std::cerr << "WARNING: change " << F->getName() << " to "
165 << R->getName() << "\n";
172 Instruction* llvm::MakeUpgradedCall(Function *F,
173 const std::vector<Value*> &Params,
174 BasicBlock *BB, bool isTailCall,
175 unsigned CallingConv) {
176 assert(F && "Need a Function to make a CallInst");
177 assert(BB && "Need a BasicBlock to make a CallInst");
179 // Convert the params
180 bool signedArg = false;
181 std::vector<Value*> Oprnds;
182 for (std::vector<Value*>::const_iterator PI = Params.begin(),
183 PE = Params.end(); PI != PE; ++PI) {
184 const Type* opTy = (*PI)->getType();
185 if (opTy->isSigned()) {
188 new CastInst(*PI,opTy->getUnsignedVersion(), "autoupgrade_cast");
189 BB->getInstList().push_back(cast);
190 Oprnds.push_back(cast);
193 Oprnds.push_back(*PI);
196 Instruction *result = new CallInst(F, Oprnds);
197 if (result->getType() != Type::VoidTy) result->setName("autoupgrade_call");
198 if (isTailCall) cast<CallInst>(result)->setTailCall();
199 if (CallingConv) cast<CallInst>(result)->setCallingConv(CallingConv);
201 const Type* newTy = F->getReturnType()->getUnsignedVersion();
202 CastInst* final = new CastInst(result, newTy, "autoupgrade_uncast");
203 BB->getInstList().push_back(result);
209 // UpgradeIntrinsicCall - In the BC reader, change a call to an intrinsic to be
210 // a call to an upgraded intrinsic. We may have to permute the order or promote
211 // some arguments with a cast.
212 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
213 Function *F = CI->getCalledFunction();
215 const FunctionType *NewFnTy = NewFn->getFunctionType();
216 std::vector<Value*> Oprnds;
218 unsigned *Permutation = getArgumentPermutation(NewFn);
219 unsigned N = NewFnTy->getNumParams();
222 for (unsigned i = 0; i != N; ++i) {
223 unsigned p = Permutation[i];
226 Value *V = CI->getOperand(p);
227 if (V->getType() != NewFnTy->getParamType(i))
228 V = new CastInst(V, NewFnTy->getParamType(i), V->getName(), CI);
231 Oprnds.push_back(UndefValue::get(NewFnTy->getParamType(i)));
234 assert(N == (CI->getNumOperands() - 1) &&
235 "Upgraded function needs permutation");
236 for (unsigned i = 0; i != N; ++i) {
237 Value *V = CI->getOperand(i + 1);
238 if (V->getType() != NewFnTy->getParamType(i))
239 V = new CastInst(V, NewFnTy->getParamType(i), V->getName(), CI);
244 bool NewIsVoid = NewFn->getReturnType() == Type::VoidTy;
246 CallInst *NewCI = new CallInst(NewFn, Oprnds,
247 NewIsVoid ? "" : CI->getName(),
249 NewCI->setTailCall(CI->isTailCall());
250 NewCI->setCallingConv(CI->getCallingConv());
252 if (!CI->use_empty()) {
254 CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
256 Instruction *RetVal = NewCI;
258 if (F->getReturnType() != NewFn->getReturnType()) {
259 RetVal = new CastInst(NewCI, NewFn->getReturnType(),
260 NewCI->getName(), CI);
261 NewCI->moveBefore(RetVal);
264 CI->replaceAllUsesWith(RetVal);
267 CI->eraseFromParent();
270 bool llvm::UpgradeCallsToIntrinsic(Function* F) {
271 if (Function* NewFn = UpgradeIntrinsicFunction(F)) {
272 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
274 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
275 UpgradeIntrinsicCall(CI, NewFn);
278 F->eraseFromParent();