-//===-- StructRetPromotion.cpp - Promote sret arguments -000000------------===//
+//===-- StructRetPromotion.cpp - Promote sret arguments ------------------===//
//
// The LLVM Compiler Infrastructure
//
//
//===----------------------------------------------------------------------===//
//
-// This pass promotes "by reference" arguments to be "by value" arguments. In
-// practice, this means looking for internal functions that have pointer
-// arguments. If it can prove, through the use of alias analysis, that an
-// argument is *only* loaded, then it can pass the value into the function
-// instead of the address of the value. This can cause recursive simplification
-// of code and lead to the elimination of allocas (especially in C++ template
-// code like the STL).
+// This pass finds functions that return a struct (using a pointer to the struct
+// as the first argument of the function, marked with the 'sret' attribute) and
+// replaces them with a new function that simply returns each of the elements of
+// that struct (using multiple return values).
//
-// This pass also handles aggregate arguments that are passed into a function,
-// scalarizing them if the elements of the aggregate are only loaded. Note that
-// it refuses to scalarize aggregates which would require passing in more than
-// three operands to the function, because passing thousands of operands for a
-// large array or structure is unprofitable!
-//
-// Note that this transformation could also be done for arguments that are only
-// stored to (returning the value instead), but does not currently. This case
-// would be best handled when and if LLVM begins supporting multiple return
-// values from functions.
+// This pass works under a number of conditions:
+// 1. The returned struct must not contain other structs
+// 2. The returned struct must only be used to load values from
+// 3. The placeholder struct passed in is the result of an alloca
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/IPO.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
+#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/CallGraphSCCPass.h"
#include "llvm/Instructions.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"
+#include "llvm/Support/raw_ostream.h"
using namespace llvm;
+STATISTIC(NumRejectedSRETUses , "Number of sret rejected due to unexpected uses");
+STATISTIC(NumSRET , "Number of sret promoted");
namespace {
/// SRETPromotion - This pass removes sret parameter and updates
/// function to use multiple return value.
virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
static char ID; // Pass identification, replacement for typeid
- SRETPromotion() : CallGraphSCCPass((intptr_t)&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;
- RegisterPass<SRETPromotion> X("sretpromotion",
- "Promote sret arguments to multiple ret values");
}
+char SRETPromotion::ID = 0;
+static RegisterPass<SRETPromotion>
+X("sretpromotion", "Promote sret arguments to multiple ret values");
+
Pass *llvm::createStructRetPromotionPass() {
return new SRETPromotion();
}
bool SRETPromotion::PromoteReturn(CallGraphNode *CGN) {
Function *F = CGN->getFunction();
- // Make sure that it is local to this module.
- if (!F || !F->hasInternalLinkage())
+ if (!F || F->isDeclaration() || !F->hasLocalLinkage())
return false;
// Make sure that function returns struct.
- if (F->arg_size() == 0 || !F->isStructReturn() || F->doesNotReturn())
+ if (F->arg_size() == 0 || !F->hasStructRetAttr() || F->doesNotReturn())
return false;
- assert (F->getReturnType() == Type::VoidTy && "Invalid function return type");
+ DEBUG(errs() << "SretPromotion: Looking at sret function "
+ << F->getName() << "\n");
+
+ assert (F->getReturnType() == Type::getVoidTy(F->getContext()) &&
+ "Invalid function return type");
Function::arg_iterator AI = F->arg_begin();
const llvm::PointerType *FArgType = dyn_cast<PointerType>(AI->getType());
- assert (FArgType && "Invalid sret paramater type");
+ assert (FArgType && "Invalid sret parameter type");
const llvm::StructType *STy =
dyn_cast<StructType>(FArgType->getElementType());
assert (STy && "Invalid sret parameter element type");
// Check if it is ok to perform this promotion.
- if (isSafeToUpdateAllCallers(F) == false)
+ 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++;
// [1] Replace use of sret parameter
- AllocaInst *TheAlloca = new AllocaInst (STy, NULL, "mrv", F->getEntryBlock().begin());
+ AllocaInst *TheAlloca = new AllocaInst(STy, NULL, "mrv",
+ F->getEntryBlock().begin());
Value *NFirstArg = F->arg_begin();
NFirstArg->replaceAllUsesWith(TheAlloca);
- // Find and replace ret instructions
- SmallVector<Value *,4> RetVals;
+ // [2] Find and replace ret instructions
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)) {
- RetVals.clear();
- for (unsigned idx = 0; idx < STy->getNumElements(); ++idx) {
- SmallVector<Value*, 2> GEPIdx;
- GEPIdx.push_back(ConstantInt::get(Type::Int32Ty, 0));
- GEPIdx.push_back(ConstantInt::get(Type::Int32Ty, idx));
- Value *NGEPI = new GetElementPtrInst(TheAlloca, GEPIdx.begin(), GEPIdx.end(),
- "mrv.gep", I);
- Value *NV = new LoadInst(NGEPI, "mrv.ld", I);
- RetVals.push_back(NV);
- }
-
- ReturnInst *NR = new ReturnInst(&RetVals[0], RetVals.size(), I);
+ Value *NV = new LoadInst(TheAlloca, "mrv.ld", I);
+ ReturnInst *NR = ReturnInst::Create(F->getContext(), NV, I);
I->replaceAllUsesWith(NR);
I->eraseFromParent();
}
}
- // Create the new function body and insert it into the module.
+ // [3] Create the new function body and insert it into the module.
Function *NF = cloneFunctionBody(F, STy);
- // Update all call sites to use new function
+ // [4] Update all call sites to use new function
updateCallSites(F, NF);
F->eraseFromParent();
return true;
}
- // Check if it is ok to perform this promotion.
+// Check if it is ok to perform this promotion.
bool SRETPromotion::isSafeToUpdateAllCallers(Function *F) {
if (F->use_empty())
for (Value::use_iterator FnUseI = F->use_begin(), FnUseE = F->use_end();
FnUseI != FnUseE; ++FnUseI) {
-
+ // The function is passed in as an argument to (possibly) another function,
+ // we can't change it!
CallSite CS = CallSite::get(*FnUseI);
Instruction *Call = CS.getInstruction();
+ // The function is used by something else than a call or invoke instruction,
+ // we can't change it!
+ if (!Call || !CS.isCallee(FnUseI))
+ return false;
CallSite::arg_iterator AI = CS.arg_begin();
Value *FirstArg = *AI;
// If FirstArg user is a GEP whose all users are not LoadInst then
// this function F is not suitable for sret promotion.
else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(ArgI)) {
+ // TODO : Use dom info and insert PHINodes to collect get results
+ // from multiple call sites for this GEP.
+ 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))
Function *SRETPromotion::cloneFunctionBody(Function *F,
const StructType *STy) {
- // FIXME : Do not drop param attributes on the floor.
const FunctionType *FTy = F->getFunctionType();
std::vector<const Type*> Params;
+ // Attributes - Keep track of the parameter attributes for the arguments.
+ SmallVector<AttributeWithIndex, 8> AttributesVec;
+ const AttrListPtr &PAL = F->getAttributes();
+
+ // Add any return attributes.
+ if (Attributes attrs = PAL.getRetAttributes())
+ AttributesVec.push_back(AttributeWithIndex::get(0, attrs));
+
// Skip first argument.
Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
++I;
+ // 0th parameter attribute is reserved for return type.
+ // 1th parameter attribute is for first 1st sret argument.
+ 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;
}
+ // Add any fn attributes.
+ if (Attributes attrs = PAL.getFnAttributes())
+ AttributesVec.push_back(AttributeWithIndex::get(~0, attrs));
+
+
FunctionType *NFTy = FunctionType::get(STy, Params, FTy->isVarArg());
- Function *NF = new Function(NFTy, F->getLinkage(), F->getName());
- NF->setCallingConv(F->getCallingConv());
+ 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());
/// updateCallSites - Update all sites that call F to use NF.
void SRETPromotion::updateCallSites(Function *F, Function *NF) {
-
- // FIXME : Handle parameter attributes
+ CallGraph &CG = getAnalysis<CallGraph>();
SmallVector<Value*, 16> Args;
- for (Value::use_iterator FUI = F->use_begin(), FUE = F->use_end(); FUI != FUE;) {
- CallSite CS = CallSite::get(*FUI);
- ++FUI;
+ // Attributes - Keep track of the parameter attributes for the arguments.
+ SmallVector<AttributeWithIndex, 8> ArgAttrsVec;
+
+ while (!F->use_empty()) {
+ CallSite CS = CallSite::get(*F->use_begin());
Instruction *Call = CS.getInstruction();
+ const AttrListPtr &PAL = F->getAttributes();
+ // Add any return attributes.
+ if (Attributes attrs = PAL.getRetAttributes())
+ ArgAttrsVec.push_back(AttributeWithIndex::get(0, attrs));
+
// Copy arguments, however skip first one.
CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
Value *FirstCArg = *AI;
++AI;
+ // 0th parameter attribute is reserved for return type.
+ // 1th parameter attribute is for first 1st sret argument.
+ 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;
}
+ // Add any function attributes.
+ if (Attributes attrs = PAL.getFnAttributes())
+ ArgAttrsVec.push_back(AttributeWithIndex::get(~0, attrs));
+
+ AttrListPtr NewPAL = AttrListPtr::get(ArgAttrsVec.begin(), ArgAttrsVec.end());
+
// Build new call instruction.
Instruction *New;
if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
- New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
- Args.begin(), Args.end(), "", 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 = new CallInst(NF, Args.begin(), Args.end(), "", Call);
+ 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);
- // Update all users of sret parameter to extract value using getresult.
+ // Update the callgraph to know that the callsite has been transformed.
+ CG[Call->getParent()->getParent()]->replaceCallSite(Call, New);
+
+ // Update all users of sret parameter to extract value using extractvalue.
for (Value::use_iterator UI = FirstCArg->use_begin(),
UE = FirstCArg->use_end(); UI != UE; ) {
User *U2 = *UI++;
if (C2 && (C2 == Call))
continue;
else if (GetElementPtrInst *UGEP = dyn_cast<GetElementPtrInst>(U2)) {
- Value *GR = new GetResultInst(New, 5, "xxx", UGEP);
- for (Value::use_iterator GI = UGEP->use_begin(),
- GE = UGEP->use_end(); GI != GE; ++GI) {
- if (LoadInst *L = dyn_cast<LoadInst>(*GI)) {
- L->replaceAllUsesWith(GR);
- L->eraseFromParent();
- }
+ 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()) {
+ // isSafeToUpdateAllCallers has checked that all GEP uses are
+ // LoadInsts
+ LoadInst *L = cast<LoadInst>(*UGEP->use_begin());
+ L->replaceAllUsesWith(GR);
+ L->eraseFromParent();
}
UGEP->eraseFromParent();
}
Call->eraseFromParent();
}
}
+
+/// nestedStructType - Return true if STy includes any
+/// other aggregate types
+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::getVoidTy(STy->getContext()))
+ return true;
+ }
+ return false;
+}