From: Duncan Sands Date: Mon, 17 Sep 2007 10:26:40 +0000 (+0000) Subject: Factor the trampoline transformation into a subroutine. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=cdb6d929d0d2d2399a66bc7eb391934a10a752ed;p=oota-llvm.git Factor the trampoline transformation into a subroutine. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42021 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index ab6262b47e5..787a3d5ccf5 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -235,6 +235,7 @@ namespace { private: Instruction *visitCallSite(CallSite CS); bool transformConstExprCastCall(CallSite CS); + Instruction *transformCallThroughTrampoline(CallSite CS); public: // InsertNewInstBefore - insert an instruction New before instruction Old @@ -7834,6 +7835,11 @@ Instruction *InstCombiner::visitCallSite(CallSite CS) { return EraseInstFromFunction(*CS.getInstruction()); } + if (BitCastInst *BC = dyn_cast(Callee)) + if (IntrinsicInst *In = dyn_cast(BC->getOperand(0))) + if (In->getIntrinsicID() == Intrinsic::init_trampoline) + return transformCallThroughTrampoline(CS); + const PointerType *PTy = cast(Callee->getType()); const FunctionType *FTy = cast(PTy->getElementType()); if (FTy->isVarArg()) { @@ -7852,143 +7858,6 @@ Instruction *InstCombiner::visitCallSite(CallSite CS) { } } - if (BitCastInst *BC = dyn_cast(Callee)) { - IntrinsicInst *In = dyn_cast(BC->getOperand(0)); - if (In && In->getIntrinsicID() == Intrinsic::init_trampoline) { - Function *NestF = - cast(IntrinsicInst::StripPointerCasts(In->getOperand(2))); - const PointerType *NestFPTy = cast(NestF->getType()); - const FunctionType *NestFTy = - cast(NestFPTy->getElementType()); - - if (const ParamAttrsList *NestAttrs = NestFTy->getParamAttrs()) { - unsigned NestIdx = 1; - const Type *NestTy = 0; - uint16_t NestAttr = 0; - - Instruction *Caller = CS.getInstruction(); - - // Look for a parameter marked with the 'nest' attribute. - for (FunctionType::param_iterator I = NestFTy->param_begin(), - E = NestFTy->param_end(); I != E; ++NestIdx, ++I) - if (NestAttrs->paramHasAttr(NestIdx, ParamAttr::Nest)) { - // Record the parameter type and any other attributes. - NestTy = *I; - NestAttr = NestAttrs->getParamAttrs(NestIdx); - break; - } - - if (NestTy) { - std::vector NewArgs; - NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); - - // Insert the nest argument into the call argument list, which may - // mean appending it. - { - unsigned Idx = 1; - CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); - do { - if (Idx == NestIdx) { - // Add the chain argument. - Value *NestVal = In->getOperand(3); - if (NestVal->getType() != NestTy) - NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); - NewArgs.push_back(NestVal); - } - - if (I == E) - break; - - // Add the original argument. - NewArgs.push_back(*I); - - ++Idx, ++I; - } while (1); - } - - // The trampoline may have been bitcast to a bogus type (FTy). - // Handle this by synthesizing a new function type, equal to FTy - // with the chain parameter inserted. Likewise for attributes. - - const ParamAttrsList *Attrs = FTy->getParamAttrs(); - std::vector NewTypes; - ParamAttrsVector NewAttrs; - NewTypes.reserve(FTy->getNumParams()+1); - - // Add any function result attributes. - uint16_t Attr = Attrs ? Attrs->getParamAttrs(0) : 0; - if (Attr) - NewAttrs.push_back (ParamAttrsWithIndex::get(0, Attr)); - - // Insert the chain's type into the list of parameter types, which may - // mean appending it. Likewise for the chain's attributes. - { - unsigned Idx = 1; - FunctionType::param_iterator I = FTy->param_begin(), - E = FTy->param_end(); - - do { - if (Idx == NestIdx) { - // Add the chain's type and attributes. - NewTypes.push_back(NestTy); - NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr)); - } - - if (I == E) - break; - - // Add the original type and attributes. - NewTypes.push_back(*I); - Attr = Attrs ? Attrs->getParamAttrs(Idx) : 0; - if (Attr) - NewAttrs.push_back - (ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr)); - - ++Idx, ++I; - } while (1); - } - - // Replace the trampoline call with a direct call. Let the generic - // code sort out any function type mismatches. - FunctionType *NewFTy = - FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg(), - ParamAttrsList::get(NewAttrs)); - Constant *NewCallee = NestF->getType() == PointerType::get(NewFTy) ? - NestF : ConstantExpr::getBitCast(NestF, PointerType::get(NewFTy)); - - Instruction *NewCaller; - if (InvokeInst *II = dyn_cast(Caller)) { - NewCaller = new InvokeInst(NewCallee, II->getNormalDest(), - II->getUnwindDest(), NewArgs.begin(), - NewArgs.end(), Caller->getName(), - Caller); - cast(NewCaller)->setCallingConv(II->getCallingConv()); - } else { - NewCaller = new CallInst(NewCallee, NewArgs.begin(), NewArgs.end(), - Caller->getName(), Caller); - if (cast(Caller)->isTailCall()) - cast(NewCaller)->setTailCall(); - cast(NewCaller)-> - setCallingConv(cast(Caller)->getCallingConv()); - } - if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) - Caller->replaceAllUsesWith(NewCaller); - Caller->eraseFromParent(); - RemoveFromWorkList(Caller); - return 0; - } - } - - // Replace the trampoline call with a direct call. Since there is no - // 'nest' parameter, there is no need to adjust the argument list. Let - // the generic code sort out any function type mismatches. - Constant *NewCallee = NestF->getType() == PTy ? - NestF : ConstantExpr::getBitCast(NestF, PTy); - CS.setCalledFunction(NewCallee); - Changed = true; - } - } - return Changed ? CS.getInstruction() : 0; } @@ -8191,6 +8060,148 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { return true; } +// transformCallThroughTrampoline - Turn a call to a function created by the +// init_trampoline intrinsic into a direct call to the underlying function. +// +Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { + Value *Callee = CS.getCalledValue(); + const PointerType *PTy = cast(Callee->getType()); + const FunctionType *FTy = cast(PTy->getElementType()); + + IntrinsicInst *Tramp = + cast(cast(Callee)->getOperand(0)); + + Function *NestF = + cast(IntrinsicInst::StripPointerCasts(Tramp->getOperand(2))); + const PointerType *NestFPTy = cast(NestF->getType()); + const FunctionType *NestFTy = cast(NestFPTy->getElementType()); + + if (const ParamAttrsList *NestAttrs = NestFTy->getParamAttrs()) { + unsigned NestIdx = 1; + const Type *NestTy = 0; + uint16_t NestAttr = 0; + + // Look for a parameter marked with the 'nest' attribute. + for (FunctionType::param_iterator I = NestFTy->param_begin(), + E = NestFTy->param_end(); I != E; ++NestIdx, ++I) + if (NestAttrs->paramHasAttr(NestIdx, ParamAttr::Nest)) { + // Record the parameter type and any other attributes. + NestTy = *I; + NestAttr = NestAttrs->getParamAttrs(NestIdx); + break; + } + + if (NestTy) { + Instruction *Caller = CS.getInstruction(); + std::vector NewArgs; + NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); + + // Insert the nest argument into the call argument list, which may + // mean appending it. + { + unsigned Idx = 1; + CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); + do { + if (Idx == NestIdx) { + // Add the chain argument. + Value *NestVal = Tramp->getOperand(3); + if (NestVal->getType() != NestTy) + NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); + NewArgs.push_back(NestVal); + } + + if (I == E) + break; + + // Add the original argument. + NewArgs.push_back(*I); + + ++Idx, ++I; + } while (1); + } + + // The trampoline may have been bitcast to a bogus type (FTy). + // Handle this by synthesizing a new function type, equal to FTy + // with the chain parameter inserted. Likewise for attributes. + + const ParamAttrsList *Attrs = FTy->getParamAttrs(); + std::vector NewTypes; + ParamAttrsVector NewAttrs; + NewTypes.reserve(FTy->getNumParams()+1); + + // Add any function result attributes. + uint16_t Attr = Attrs ? Attrs->getParamAttrs(0) : 0; + if (Attr) + NewAttrs.push_back (ParamAttrsWithIndex::get(0, Attr)); + + // Insert the chain's type into the list of parameter types, which may + // mean appending it. Likewise for the chain's attributes. + { + unsigned Idx = 1; + FunctionType::param_iterator I = FTy->param_begin(), + E = FTy->param_end(); + + do { + if (Idx == NestIdx) { + // Add the chain's type and attributes. + NewTypes.push_back(NestTy); + NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr)); + } + + if (I == E) + break; + + // Add the original type and attributes. + NewTypes.push_back(*I); + Attr = Attrs ? Attrs->getParamAttrs(Idx) : 0; + if (Attr) + NewAttrs.push_back + (ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr)); + + ++Idx, ++I; + } while (1); + } + + // Replace the trampoline call with a direct call. Let the generic + // code sort out any function type mismatches. + FunctionType *NewFTy = + FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg(), + ParamAttrsList::get(NewAttrs)); + Constant *NewCallee = NestF->getType() == PointerType::get(NewFTy) ? + NestF : ConstantExpr::getBitCast(NestF, PointerType::get(NewFTy)); + + Instruction *NewCaller; + if (InvokeInst *II = dyn_cast(Caller)) { + NewCaller = new InvokeInst(NewCallee, + II->getNormalDest(), II->getUnwindDest(), + NewArgs.begin(), NewArgs.end(), + Caller->getName(), Caller); + cast(NewCaller)->setCallingConv(II->getCallingConv()); + } else { + NewCaller = new CallInst(NewCallee, NewArgs.begin(), NewArgs.end(), + Caller->getName(), Caller); + if (cast(Caller)->isTailCall()) + cast(NewCaller)->setTailCall(); + cast(NewCaller)-> + setCallingConv(cast(Caller)->getCallingConv()); + } + if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) + Caller->replaceAllUsesWith(NewCaller); + Caller->eraseFromParent(); + RemoveFromWorkList(Caller); + return 0; + } + } + + // Replace the trampoline call with a direct call. Since there is no 'nest' + // parameter, there is no need to adjust the argument list. Let the generic + // code sort out any function type mismatches. + Constant *NewCallee = + NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy); + CS.setCalledFunction(NewCallee); + return CS.getInstruction(); +} + /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] /// and if a/b/c/d and the add's all have a single use, turn this into two phi's /// and a single binop.