StructRetPromotion.cpp [plain text]
#define DEBUG_TYPE "sretpromotion"
#include "llvm/Transforms/IPO.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/CallGraphSCCPass.h"
#include "llvm/Instructions.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Support/CallSite.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Debug.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Compiler.h"
using namespace llvm;
STATISTIC(NumRejectedSRETUses , "Number of sret rejected due to unexpected uses");
STATISTIC(NumSRET , "Number of sret promoted");
namespace {
struct VISIBILITY_HIDDEN SRETPromotion : public CallGraphSCCPass {
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
CallGraphSCCPass::getAnalysisUsage(AU);
}
virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
static char ID; SRETPromotion() : CallGraphSCCPass(&ID) {}
private:
bool PromoteReturn(CallGraphNode *CGN);
bool isSafeToUpdateAllCallers(Function *F);
Function *cloneFunctionBody(Function *F, const StructType *STy);
void updateCallSites(Function *F, Function *NF);
bool nestedStructType(const StructType *STy);
};
}
char SRETPromotion::ID = 0;
static RegisterPass<SRETPromotion>
X("sretpromotion", "Promote sret arguments to multiple ret values");
Pass *llvm::createStructRetPromotionPass() {
return new SRETPromotion();
}
bool SRETPromotion::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
bool Changed = false;
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
Changed |= PromoteReturn(SCC[i]);
return Changed;
}
bool SRETPromotion::PromoteReturn(CallGraphNode *CGN) {
Function *F = CGN->getFunction();
if (!F || F->isDeclaration() || !F->hasLocalLinkage())
return false;
if (F->arg_size() == 0 || !F->hasStructRetAttr() || F->doesNotReturn())
return false;
DOUT << "SretPromotion: Looking at sret function " << F->getNameStart() << "\n";
assert (F->getReturnType() == Type::VoidTy && "Invalid function return type");
Function::arg_iterator AI = F->arg_begin();
const llvm::PointerType *FArgType = dyn_cast<PointerType>(AI->getType());
assert (FArgType && "Invalid sret parameter type");
const llvm::StructType *STy =
dyn_cast<StructType>(FArgType->getElementType());
assert (STy && "Invalid sret parameter element type");
if (isSafeToUpdateAllCallers(F) == false) {
DOUT << "SretPromotion: Not all callers can be updated\n";
NumRejectedSRETUses++;
return false;
}
DOUT << "SretPromotion: sret argument will be promoted\n";
NumSRET++;
AllocaInst *TheAlloca = new AllocaInst (STy, NULL, "mrv",
F->getEntryBlock().begin());
Value *NFirstArg = F->arg_begin();
NFirstArg->replaceAllUsesWith(TheAlloca);
for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
for(BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
Instruction *I = BI;
++BI;
if (isa<ReturnInst>(I)) {
Value *NV = new LoadInst(TheAlloca, "mrv.ld", I);
ReturnInst *NR = ReturnInst::Create(NV, I);
I->replaceAllUsesWith(NR);
I->eraseFromParent();
}
}
Function *NF = cloneFunctionBody(F, STy);
updateCallSites(F, NF);
F->eraseFromParent();
getAnalysis<CallGraph>().changeFunction(F, NF);
return true;
}
bool SRETPromotion::isSafeToUpdateAllCallers(Function *F) {
if (F->use_empty())
return true;
for (Value::use_iterator FnUseI = F->use_begin(), FnUseE = F->use_end();
FnUseI != FnUseE; ++FnUseI) {
CallSite CS = CallSite::get(*FnUseI);
Instruction *Call = CS.getInstruction();
if (!Call || !CS.isCallee(FnUseI))
return false;
CallSite::arg_iterator AI = CS.arg_begin();
Value *FirstArg = *AI;
if (!isa<AllocaInst>(FirstArg))
return false;
for (Value::use_iterator ArgI = FirstArg->use_begin(),
ArgE = FirstArg->use_end(); ArgI != ArgE; ++ArgI) {
if (CallInst *CI = dyn_cast<CallInst>(ArgI)) {
if (CI != Call)
return false;
}
else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(ArgI)) {
if (GEP->getParent() != Call->getParent())
return false;
for (Value::use_iterator GEPI = GEP->use_begin(), GEPE = GEP->use_end();
GEPI != GEPE; ++GEPI)
if (!isa<LoadInst>(GEPI))
return false;
}
else
return false;
}
}
return true;
}
Function *SRETPromotion::cloneFunctionBody(Function *F,
const StructType *STy) {
const FunctionType *FTy = F->getFunctionType();
std::vector<const Type*> Params;
SmallVector<AttributeWithIndex, 8> AttributesVec;
const AttrListPtr &PAL = F->getAttributes();
if (Attributes attrs = PAL.getRetAttributes())
AttributesVec.push_back(AttributeWithIndex::get(0, attrs));
Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
++I;
unsigned ParamIndex = 2;
while (I != E) {
Params.push_back(I->getType());
if (Attributes Attrs = PAL.getParamAttributes(ParamIndex))
AttributesVec.push_back(AttributeWithIndex::get(ParamIndex - 1, Attrs));
++I;
++ParamIndex;
}
if (Attributes attrs = PAL.getFnAttributes())
AttributesVec.push_back(AttributeWithIndex::get(~0, attrs));
FunctionType *NFTy = FunctionType::get(STy, Params, FTy->isVarArg());
Function *NF = Function::Create(NFTy, F->getLinkage());
NF->takeName(F);
NF->copyAttributesFrom(F);
NF->setAttributes(AttrListPtr::get(AttributesVec.begin(), AttributesVec.end()));
F->getParent()->getFunctionList().insert(F, NF);
NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
I = F->arg_begin();
E = F->arg_end();
Function::arg_iterator NI = NF->arg_begin();
++I;
while (I != E) {
I->replaceAllUsesWith(NI);
NI->takeName(I);
++I;
++NI;
}
return NF;
}
void SRETPromotion::updateCallSites(Function *F, Function *NF) {
CallGraph &CG = getAnalysis<CallGraph>();
SmallVector<Value*, 16> Args;
SmallVector<AttributeWithIndex, 8> ArgAttrsVec;
while (!F->use_empty()) {
CallSite CS = CallSite::get(*F->use_begin());
Instruction *Call = CS.getInstruction();
const AttrListPtr &PAL = F->getAttributes();
if (Attributes attrs = PAL.getRetAttributes())
ArgAttrsVec.push_back(AttributeWithIndex::get(0, attrs));
CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
Value *FirstCArg = *AI;
++AI;
unsigned ParamIndex = 2;
while (AI != AE) {
Args.push_back(*AI);
if (Attributes Attrs = PAL.getParamAttributes(ParamIndex))
ArgAttrsVec.push_back(AttributeWithIndex::get(ParamIndex - 1, Attrs));
++ParamIndex;
++AI;
}
if (Attributes attrs = PAL.getFnAttributes())
ArgAttrsVec.push_back(AttributeWithIndex::get(~0, attrs));
AttrListPtr NewPAL = AttrListPtr::get(ArgAttrsVec.begin(), ArgAttrsVec.end());
Instruction *New;
if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
Args.begin(), Args.end(), "", Call);
cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
cast<InvokeInst>(New)->setAttributes(NewPAL);
} else {
New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
cast<CallInst>(New)->setAttributes(NewPAL);
if (cast<CallInst>(Call)->isTailCall())
cast<CallInst>(New)->setTailCall();
}
Args.clear();
ArgAttrsVec.clear();
New->takeName(Call);
CG[Call->getParent()->getParent()]->replaceCallSite(Call, New);
for (Value::use_iterator UI = FirstCArg->use_begin(),
UE = FirstCArg->use_end(); UI != UE; ) {
User *U2 = *UI++;
CallInst *C2 = dyn_cast<CallInst>(U2);
if (C2 && (C2 == Call))
continue;
else if (GetElementPtrInst *UGEP = dyn_cast<GetElementPtrInst>(U2)) {
ConstantInt *Idx = dyn_cast<ConstantInt>(UGEP->getOperand(2));
assert (Idx && "Unexpected getelementptr index!");
Value *GR = ExtractValueInst::Create(New, Idx->getZExtValue(),
"evi", UGEP);
while(!UGEP->use_empty()) {
LoadInst *L = cast<LoadInst>(*UGEP->use_begin());
L->replaceAllUsesWith(GR);
L->eraseFromParent();
}
UGEP->eraseFromParent();
}
else assert( 0 && "Unexpected sret parameter use");
}
Call->eraseFromParent();
}
}
bool SRETPromotion::nestedStructType(const StructType *STy) {
unsigned Num = STy->getNumElements();
for (unsigned i = 0; i < Num; i++) {
const Type *Ty = STy->getElementType(i);
if (!Ty->isSingleValueType() && Ty != Type::VoidTy)
return true;
}
return false;
}