- } else if (I->getOpcode() == Instruction::Add &&
- isa<CastInst>(I->getOperand(1))) {
-
- if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
- cast<CastInst>(I->getOperand(1)))) {
- ++NumGEPInstFormed;
+ } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
+ // If we have a call with all varargs arguments, convert the call to use the
+ // actual argument types present...
+ //
+ const PointerType *PTy = cast<PointerType>(CI->getCalledValue()->getType());
+ const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
+
+ // Is the call to a vararg variable with no real parameters?
+ if (FTy->isVarArg() && FTy->getNumParams() == 0 &&
+ !CI->getCalledFunction()) {
+ // If so, insert a new cast instruction, casting it to a function type
+ // that matches the current arguments...
+ //
+ std::vector<const Type *> Params; // Parameter types...
+ for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
+ Params.push_back(CI->getOperand(i)->getType());
+
+ FunctionType *NewFT = FunctionType::get(FTy->getReturnType(),
+ Params, false);
+ PointerType *NewPFunTy = PointerType::get(NewFT);
+
+ // Create a new cast, inserting it right before the function call...
+ Value *NewCast;
+ if (Constant *CS = dyn_cast<Constant>(CI->getCalledValue()))
+ NewCast = ConstantExpr::getBitCast(CS, NewPFunTy);
+ else
+ NewCast = CastInst::create(Instruction::BitCast, CI->getCalledValue(),
+ NewPFunTy,
+ CI->getCalledValue()->getName()+"_c", CI);
+
+ // Create a new call instruction...
+ CallInst *NewCall = new CallInst(NewCast,
+ std::vector<Value*>(CI->op_begin()+1, CI->op_end()));
+ if (CI->isTailCall()) NewCall->setTailCall();
+ NewCall->setCallingConv(CI->getCallingConv());
+ ++BI;
+ ReplaceInstWithInst(CI, NewCall);
+
+ ++NumVarargCallChanges;