-
- // Only proceed if we have detected all of our conditions successfully...
- if (Offset && StructTy && SrcPtr && Offset < TD.getTypeSize(StructTy)) {
- const StructLayout *SL = TD.getStructLayout(StructTy);
- vector<ConstPoolVal*> Offsets;
- unsigned ActualOffset = Offset;
- const Type *ElTy = getStructOffsetType(StructTy, ActualOffset, Offsets);
-
- if (getPointedToStruct(AddOp1->getType())) { // case 1
- PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, I);
- } else {
- PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, I);
- }
-
- GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Offsets);
- //AddOp2->getName());
- BI = BB->getInstList().insert(BI, GEP)+1;
-
- assert(Offset-ActualOffset == 0 &&
- "GEP to middle of element not implemented yet!");
-
- ReplaceInstWithInst(BB->getInstList(), BI,
- I = new CastInst(GEP, I->getType()));
- PRINT_PEEPHOLE2("add-to-gep:out", GEP, I);
+ } 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;
+ Constant *ConstantCallSrc = 0;
+ if (Constant *CS = dyn_cast<Constant>(CI->getCalledValue()))
+ ConstantCallSrc = CS;
+ else if (GlobalValue *GV = dyn_cast<GlobalValue>(CI->getCalledValue()))
+ ConstantCallSrc = ConstantPointerRef::get(GV);
+
+ if (ConstantCallSrc)
+ NewCast = ConstantExpr::getCast(ConstantCallSrc, NewPFunTy);
+ else
+ NewCast = new CastInst(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()));
+ ++BI;
+ ReplaceInstWithInst(CI, NewCall);
+
+ ++NumVarargCallChanges;