//
//===----------------------------------------------------------------------===//
-#include "llvm/Transforms/CleanupGCCOutput.h"
+#include "llvm/Transforms/IPO.h"
#include "llvm/Module.h"
#include "llvm/SymbolTable.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Pass.h"
#include "llvm/iOther.h"
#include "llvm/Constant.h"
-#include "Support/StatisticReporter.h"
-#include <iostream>
+#include "Support/Statistic.h"
#include <algorithm>
using std::vector;
using std::cerr;
namespace {
- Statistic<>NumResolved("funcresolve\t- Number of varargs functions resolved");
+ Statistic<>NumResolved("funcresolve", "Number of varargs functions resolved");
struct FunctionResolvingPass : public Pass {
- const char *getPassName() const { return "Resolve Functions"; }
-
bool run(Module &M);
};
+ RegisterOpt<FunctionResolvingPass> X("funcresolve", "Resolve Functions");
}
Pass *createFunctionResolvingPass() {
for (unsigned i = 1; i < CI->getNumOperands(); ++i) {
Value *V = CI->getOperand(i);
- if (V->getType() != ParamTys[i-1]) { // Must insert a cast...
- Instruction *Cast = new CastInst(V, ParamTys[i-1]);
- BBI = ++BB->getInstList().insert(BBI, Cast);
- V = Cast;
- }
+ if (V->getType() != ParamTys[i-1]) // Must insert a cast...
+ V = new CastInst(V, ParamTys[i-1], "argcast", BBI);
Params.push_back(V);
}
- Instruction *NewCall = new CallInst(Dest, Params);
-
// Replace the old call instruction with a new call instruction that calls
// the real function.
//
- BBI = ++BB->getInstList().insert(BBI, NewCall);
+ Instruction *NewCall = new CallInst(Dest, Params, "", BBI);
// Remove the old call instruction from the program...
BB->getInstList().remove(BBI);
+ // Transfer the name over...
+ if (NewCall->getType() != Type::VoidTy)
+ NewCall->setName(CI->getName());
+
// Replace uses of the old instruction with the appropriate values...
//
if (NewCall->getType() == CI->getType()) {
CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
} else if (CI->getType() == Type::VoidTy) {
// If we are gaining a new return value, we don't have to do anything
- // special.
+ // special here, because it will automatically be ignored.
} else {
- assert(0 && "This should have been checked before!");
- abort();
+ // Insert a cast instruction to convert the return value of the function
+ // into it's new type. Of course we only need to do this if the return
+ // value of the function is actually USED.
+ //
+ if (!CI->use_empty()) {
+ // Insert the new cast instruction...
+ CastInst *NewCast = new CastInst(NewCall, CI->getType(),
+ NewCall->getName(), BBI);
+ CI->replaceAllUsesWith(NewCast);
+ }
}
// The old instruction is no longer needed, destroy it!
const FunctionType *ConcreteMT = Concrete->getFunctionType();
bool Broken = false;
- assert((Old->getReturnType() == Concrete->getReturnType() ||
- Concrete->getReturnType() == Type::VoidTy ||
- Old->getReturnType() == Type::VoidTy) &&
- "Differing return types not handled yet!");
assert(OldMT->getParamTypes().size() <=
ConcreteMT->getParamTypes().size() &&
"Concrete type must have more specified parameters!");