2008-05-08 17:46:35 +00:00
|
|
|
//===-- StructRetPromotion.cpp - Promote sret arguments ------------------===//
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-05-08 17:46:35 +00:00
|
|
|
// 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 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
|
|
|
|
//
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
2008-03-04 17:44:37 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2008-03-04 17:44:37 +00:00
|
|
|
STATISTIC(NumRejectedSRETUses , "Number of sret rejected due to unexpected uses");
|
|
|
|
STATISTIC(NumSRET , "Number of sret promoted");
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
namespace {
|
|
|
|
/// SRETPromotion - This pass removes sret parameter and updates
|
|
|
|
/// function to use multiple return value.
|
|
|
|
///
|
|
|
|
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; // Pass identification, replacement for typeid
|
|
|
|
SRETPromotion() : CallGraphSCCPass((intptr_t)&ID) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool PromoteReturn(CallGraphNode *CGN);
|
|
|
|
bool isSafeToUpdateAllCallers(Function *F);
|
|
|
|
Function *cloneFunctionBody(Function *F, const StructType *STy);
|
|
|
|
void updateCallSites(Function *F, Function *NF);
|
2008-03-04 21:32:09 +00:00
|
|
|
bool nestedStructType(const StructType *STy);
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char SRETPromotion::ID = 0;
|
|
|
|
static RegisterPass<SRETPromotion>
|
|
|
|
X("sretpromotion", "Promote sret arguments to multiple ret values");
|
|
|
|
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PromoteReturn - This method promotes function that uses StructRet paramater
|
|
|
|
/// into a function that uses mulitple return value.
|
|
|
|
bool SRETPromotion::PromoteReturn(CallGraphNode *CGN) {
|
|
|
|
Function *F = CGN->getFunction();
|
|
|
|
|
2008-05-08 15:08:39 +00:00
|
|
|
if (!F || F->isDeclaration() || !F->hasInternalLinkage())
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Make sure that function returns struct.
|
2008-03-03 21:46:28 +00:00
|
|
|
if (F->arg_size() == 0 || !F->hasStructRetAttr() || F->doesNotReturn())
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
return false;
|
|
|
|
|
2008-08-07 15:14:04 +00:00
|
|
|
DOUT << "SretPromotion: Looking at sret function " << F->getNameStart() << "\n";
|
|
|
|
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
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());
|
2008-05-09 12:20:10 +00:00
|
|
|
assert (FArgType && "Invalid sret parameter type");
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
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.
|
2008-03-04 17:44:37 +00:00
|
|
|
if (isSafeToUpdateAllCallers(F) == false) {
|
2008-08-07 15:14:04 +00:00
|
|
|
DOUT << "SretPromotion: Not all callers can be updated\n";
|
2008-03-04 17:44:37 +00:00
|
|
|
NumRejectedSRETUses++;
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
return false;
|
2008-03-04 17:44:37 +00:00
|
|
|
}
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
|
2008-08-07 15:14:04 +00:00
|
|
|
DOUT << "SretPromotion: sret argument will be promoted\n";
|
2008-03-04 17:48:11 +00:00
|
|
|
NumSRET++;
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
// [1] Replace use of sret parameter
|
2008-03-04 17:44:37 +00:00
|
|
|
AllocaInst *TheAlloca = new AllocaInst (STy, NULL, "mrv",
|
|
|
|
F->getEntryBlock().begin());
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
Value *NFirstArg = F->arg_begin();
|
|
|
|
NFirstArg->replaceAllUsesWith(TheAlloca);
|
|
|
|
|
2008-03-04 17:44:37 +00:00
|
|
|
// [2] Find and replace ret instructions
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
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)) {
|
2008-07-23 00:34:11 +00:00
|
|
|
Value *NV = new LoadInst(TheAlloca, "mrv.ld", I);
|
2008-08-07 15:58:09 +00:00
|
|
|
ReturnInst *NR = ReturnInst::Create(NV, I);
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
I->replaceAllUsesWith(NR);
|
|
|
|
I->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-04 17:44:37 +00:00
|
|
|
// [3] Create the new function body and insert it into the module.
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
Function *NF = cloneFunctionBody(F, STy);
|
|
|
|
|
2008-03-04 17:44:37 +00:00
|
|
|
// [4] Update all call sites to use new function
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
updateCallSites(F, NF);
|
|
|
|
|
|
|
|
F->eraseFromParent();
|
|
|
|
getAnalysis<CallGraph>().changeFunction(F, NF);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-09 12:20:10 +00:00
|
|
|
// Check if it is ok to perform this promotion.
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
bool SRETPromotion::isSafeToUpdateAllCallers(Function *F) {
|
|
|
|
|
|
|
|
if (F->use_empty())
|
|
|
|
// No users. OK to modify signature.
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (Value::use_iterator FnUseI = F->use_begin(), FnUseE = F->use_end();
|
|
|
|
FnUseI != FnUseE; ++FnUseI) {
|
2008-06-05 08:48:32 +00:00
|
|
|
// The function is passed in as an argument to (possibly) another function,
|
|
|
|
// we can't change it!
|
|
|
|
if (FnUseI.getOperandNo() != 0)
|
|
|
|
return false;
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
|
|
|
|
CallSite CS = CallSite::get(*FnUseI);
|
|
|
|
Instruction *Call = CS.getInstruction();
|
2008-06-05 08:57:20 +00:00
|
|
|
// The function is used by something else than a call or invoke instruction,
|
|
|
|
// we can't change it!
|
|
|
|
if (!Call)
|
|
|
|
return false;
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
CallSite::arg_iterator AI = CS.arg_begin();
|
|
|
|
Value *FirstArg = *AI;
|
|
|
|
|
|
|
|
if (!isa<AllocaInst>(FirstArg))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check FirstArg's users.
|
|
|
|
for (Value::use_iterator ArgI = FirstArg->use_begin(),
|
|
|
|
ArgE = FirstArg->use_end(); ArgI != ArgE; ++ArgI) {
|
|
|
|
|
|
|
|
// If FirstArg user is a CallInst that does not correspond to current
|
|
|
|
// call site then this function F is not suitable for sret promotion.
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(ArgI)) {
|
|
|
|
if (CI != Call)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 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)) {
|
2008-03-05 23:39:23 +00:00
|
|
|
// 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;
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
for (Value::use_iterator GEPI = GEP->use_begin(), GEPE = GEP->use_end();
|
|
|
|
GEPI != GEPE; ++GEPI)
|
|
|
|
if (!isa<LoadInst>(GEPI))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Any other FirstArg users make this function unsuitable for sret
|
|
|
|
// promotion.
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// cloneFunctionBody - Create a new function based on F and
|
|
|
|
/// insert it into module. Remove first argument. Use STy as
|
|
|
|
/// the return type for new function.
|
|
|
|
Function *SRETPromotion::cloneFunctionBody(Function *F,
|
|
|
|
const StructType *STy) {
|
|
|
|
|
|
|
|
const FunctionType *FTy = F->getFunctionType();
|
|
|
|
std::vector<const Type*> Params;
|
|
|
|
|
2008-03-03 18:36:03 +00:00
|
|
|
// ParamAttrs - Keep track of the parameter attributes for the arguments.
|
2008-03-12 17:45:29 +00:00
|
|
|
SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
|
|
|
|
const PAListPtr &PAL = F->getParamAttrs();
|
2008-03-03 18:36:03 +00:00
|
|
|
|
|
|
|
// Add any return attributes.
|
2008-03-12 17:45:29 +00:00
|
|
|
if (ParameterAttributes attrs = PAL.getParamAttrs(0))
|
2008-03-03 18:36:03 +00:00
|
|
|
ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
|
|
|
|
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
// Skip first argument.
|
|
|
|
Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
|
|
|
|
++I;
|
2008-03-12 00:07:03 +00:00
|
|
|
// 0th parameter attribute is reserved for return type.
|
|
|
|
// 1th parameter attribute is for first 1st sret argument.
|
|
|
|
unsigned ParamIndex = 2;
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
while (I != E) {
|
|
|
|
Params.push_back(I->getType());
|
2008-03-12 17:45:29 +00:00
|
|
|
if (ParameterAttributes Attrs = PAL.getParamAttrs(ParamIndex))
|
|
|
|
ParamAttrsVec.push_back(ParamAttrsWithIndex::get(ParamIndex - 1, Attrs));
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
++I;
|
2008-03-03 18:36:03 +00:00
|
|
|
++ParamIndex;
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FunctionType *NFTy = FunctionType::get(STy, Params, FTy->isVarArg());
|
2008-04-06 20:25:17 +00:00
|
|
|
Function *NF = Function::Create(NFTy, F->getLinkage(), F->getName());
|
2008-05-26 19:58:59 +00:00
|
|
|
NF->copyAttributesFrom(F);
|
2008-03-12 17:45:29 +00:00
|
|
|
NF->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end()));
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
F->getParent()->getFunctionList().insert(F, NF);
|
|
|
|
NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
|
|
|
|
|
|
|
|
// Replace arguments
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// updateCallSites - Update all sites that call F to use NF.
|
|
|
|
void SRETPromotion::updateCallSites(Function *F, Function *NF) {
|
|
|
|
|
|
|
|
SmallVector<Value*, 16> Args;
|
|
|
|
|
2008-03-03 18:36:03 +00:00
|
|
|
// ParamAttrs - Keep track of the parameter attributes for the arguments.
|
2008-03-12 17:45:29 +00:00
|
|
|
SmallVector<ParamAttrsWithIndex, 8> ArgAttrsVec;
|
2008-03-03 18:36:03 +00:00
|
|
|
|
2008-03-12 17:45:29 +00:00
|
|
|
for (Value::use_iterator FUI = F->use_begin(), FUE = F->use_end();
|
|
|
|
FUI != FUE;) {
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
CallSite CS = CallSite::get(*FUI);
|
|
|
|
++FUI;
|
|
|
|
Instruction *Call = CS.getInstruction();
|
|
|
|
|
2008-03-12 17:45:29 +00:00
|
|
|
const PAListPtr &PAL = F->getParamAttrs();
|
2008-03-03 18:36:03 +00:00
|
|
|
// Add any return attributes.
|
2008-03-12 17:45:29 +00:00
|
|
|
if (ParameterAttributes attrs = PAL.getParamAttrs(0))
|
2008-03-04 19:12:58 +00:00
|
|
|
ArgAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
|
2008-03-03 18:36:03 +00:00
|
|
|
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
// Copy arguments, however skip first one.
|
|
|
|
CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
|
|
|
|
Value *FirstCArg = *AI;
|
|
|
|
++AI;
|
2008-03-12 00:07:03 +00:00
|
|
|
// 0th parameter attribute is reserved for return type.
|
|
|
|
// 1th parameter attribute is for first 1st sret argument.
|
|
|
|
unsigned ParamIndex = 2;
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
while (AI != AE) {
|
|
|
|
Args.push_back(*AI);
|
2008-03-12 17:45:29 +00:00
|
|
|
if (ParameterAttributes Attrs = PAL.getParamAttrs(ParamIndex))
|
|
|
|
ArgAttrsVec.push_back(ParamAttrsWithIndex::get(ParamIndex - 1, Attrs));
|
2008-03-03 18:36:03 +00:00
|
|
|
++ParamIndex;
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
++AI;
|
|
|
|
}
|
|
|
|
|
2008-03-12 17:45:29 +00:00
|
|
|
|
|
|
|
PAListPtr NewPAL = PAListPtr::get(ArgAttrsVec.begin(), ArgAttrsVec.end());
|
|
|
|
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
// Build new call instruction.
|
|
|
|
Instruction *New;
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
|
2008-04-06 20:25:17 +00:00
|
|
|
New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
|
|
|
|
Args.begin(), Args.end(), "", Call);
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
|
2008-03-12 17:45:29 +00:00
|
|
|
cast<InvokeInst>(New)->setParamAttrs(NewPAL);
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
} else {
|
2008-04-06 20:25:17 +00:00
|
|
|
New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
|
2008-03-12 17:45:29 +00:00
|
|
|
cast<CallInst>(New)->setParamAttrs(NewPAL);
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
if (cast<CallInst>(Call)->isTailCall())
|
|
|
|
cast<CallInst>(New)->setTailCall();
|
|
|
|
}
|
|
|
|
Args.clear();
|
2008-03-04 19:12:58 +00:00
|
|
|
ArgAttrsVec.clear();
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
New->takeName(Call);
|
|
|
|
|
2008-07-23 00:34:11 +00:00
|
|
|
// Update all users of sret parameter to extract value using extractvalue.
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
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)) {
|
2008-03-04 19:22:54 +00:00
|
|
|
ConstantInt *Idx = dyn_cast<ConstantInt>(UGEP->getOperand(2));
|
|
|
|
assert (Idx && "Unexpected getelementptr index!");
|
2008-07-23 00:34:11 +00:00
|
|
|
Value *GR = ExtractValueInst::Create(New, Idx->getZExtValue(),
|
|
|
|
"evi", UGEP);
|
Add pass to promote sret.
This pass transforms
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal void @foo(%struct._Point* sret %agg.result)
into
%struct._Point = type { i32, i32, i32, i32, i32, i32 }
define internal %struct._Point @foo()
This pass updates foo() clients appropriately to use
getresult instruction to extract return values.
This pass is not yet ready for prime time.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47776 91177308-0d34-0410-b5e6-96231b3b80d8
2008-02-29 23:34:08 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UGEP->eraseFromParent();
|
|
|
|
}
|
|
|
|
else assert( 0 && "Unexpected sret parameter use");
|
|
|
|
}
|
|
|
|
Call->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
2008-03-04 21:32:09 +00:00
|
|
|
|
|
|
|
/// 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);
|
2008-05-23 00:12:03 +00:00
|
|
|
if (!Ty->isSingleValueType() && Ty != Type::VoidTy)
|
2008-03-04 21:32:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|