2004-05-23 21:21:17 +00:00
|
|
|
//===-- ArgumentPromotion.cpp - Promote by-reference arguments ------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-03-07 21:29:54 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-03-07 21:29:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass promotes "by reference" arguments to be "by value" arguments. In
|
|
|
|
// practice, this means looking for internal functions that have pointer
|
2007-10-26 03:03:51 +00:00
|
|
|
// 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
|
2004-03-07 21:29:54 +00:00
|
|
|
// instead of the address of the value. This can cause recursive simplification
|
2004-05-23 21:21:17 +00:00
|
|
|
// of code and lead to the elimination of allocas (especially in C++ template
|
|
|
|
// code like the STL).
|
2004-03-07 21:29:54 +00:00
|
|
|
//
|
2004-03-08 01:04:36 +00:00
|
|
|
// 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
|
2008-07-29 10:00:13 +00:00
|
|
|
// by default it refuses to scalarize aggregates which would require passing in
|
|
|
|
// more than three operands to the function, because passing thousands of
|
2008-09-07 09:54:09 +00:00
|
|
|
// operands for a large array or structure is unprofitable! This limit can be
|
2008-07-29 10:00:13 +00:00
|
|
|
// configured or disabled, however.
|
2004-03-08 01:04:36 +00:00
|
|
|
//
|
2004-03-07 21:29:54 +00:00
|
|
|
// Note that this transformation could also be done for arguments that are only
|
2007-10-26 03:03:51 +00:00
|
|
|
// 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.
|
2004-03-07 21:29:54 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-05-23 21:21:17 +00:00
|
|
|
#define DEBUG_TYPE "argpromotion"
|
2004-03-07 21:29:54 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
2004-09-18 00:34:13 +00:00
|
|
|
#include "llvm/CallGraphSCCPass.h"
|
2004-03-07 21:29:54 +00:00
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2004-09-18 00:34:13 +00:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2004-03-07 21:29:54 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Support/CallSite.h"
|
|
|
|
#include "llvm/Support/CFG.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-03-07 21:29:54 +00:00
|
|
|
#include <set>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
STATISTIC(NumArgumentsPromoted , "Number of pointer arguments promoted");
|
|
|
|
STATISTIC(NumAggregatesPromoted, "Number of aggregate arguments promoted");
|
2008-01-11 22:31:41 +00:00
|
|
|
STATISTIC(NumByValArgsPromoted , "Number of byval arguments promoted");
|
2006-12-19 22:09:18 +00:00
|
|
|
STATISTIC(NumArgumentsDead , "Number of dead pointer args eliminated");
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
namespace {
|
2004-03-07 21:29:54 +00:00
|
|
|
/// ArgPromotion - The 'by reference' to 'by value' argument promotion pass.
|
|
|
|
///
|
2007-02-05 23:32:05 +00:00
|
|
|
struct VISIBILITY_HIDDEN ArgPromotion : public CallGraphSCCPass {
|
2004-03-07 21:29:54 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<AliasAnalysis>();
|
|
|
|
AU.addRequired<TargetData>();
|
2004-09-18 00:34:13 +00:00
|
|
|
CallGraphSCCPass::getAnalysisUsage(AU);
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
2004-09-18 00:34:13 +00:00
|
|
|
virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
ArgPromotion(unsigned maxElements = 3) : CallGraphSCCPass(&ID),
|
2008-05-13 01:48:26 +00:00
|
|
|
maxElements(maxElements) {}
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-07-29 10:00:13 +00:00
|
|
|
/// A vector used to hold the indices of a single GEP instruction
|
|
|
|
typedef std::vector<uint64_t> IndicesVector;
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2004-03-07 21:29:54 +00:00
|
|
|
private:
|
2004-09-18 00:34:13 +00:00
|
|
|
bool PromoteArguments(CallGraphNode *CGN);
|
2008-01-11 19:20:39 +00:00
|
|
|
bool isSafeToPromoteArgument(Argument *Arg, bool isByVal) const;
|
2008-09-07 09:54:09 +00:00
|
|
|
Function *DoPromotion(Function *F,
|
2008-01-11 22:31:41 +00:00
|
|
|
SmallPtrSet<Argument*, 8> &ArgsToPromote,
|
|
|
|
SmallPtrSet<Argument*, 8> &ByValArgsToTransform);
|
2008-05-23 07:57:02 +00:00
|
|
|
/// The maximum number of elements to expand, or 0 for unlimited.
|
|
|
|
unsigned maxElements;
|
2004-03-07 21:29:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char ArgPromotion::ID = 0;
|
|
|
|
static RegisterPass<ArgPromotion>
|
|
|
|
X("argpromotion", "Promote 'by reference' arguments to scalars");
|
|
|
|
|
2008-04-19 19:50:01 +00:00
|
|
|
Pass *llvm::createArgumentPromotionPass(unsigned maxElements) {
|
|
|
|
return new ArgPromotion(maxElements);
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
2004-09-18 00:34:13 +00:00
|
|
|
bool ArgPromotion::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
|
|
|
|
bool Changed = false, LocalChange;
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2004-09-19 01:05:16 +00:00
|
|
|
do { // Iterate until we stop promoting from this SCC.
|
2004-09-18 00:34:13 +00:00
|
|
|
LocalChange = false;
|
|
|
|
// Attempt to promote arguments from all functions in this SCC.
|
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
|
|
|
|
LocalChange |= PromoteArguments(SCC[i]);
|
|
|
|
Changed |= LocalChange; // Remember that we changed something.
|
|
|
|
} while (LocalChange);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-03-07 21:29:54 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2004-05-23 21:21:17 +00:00
|
|
|
/// PromoteArguments - This method checks the specified function to see if there
|
|
|
|
/// are any promotable arguments and if it is safe to promote the function (for
|
|
|
|
/// example, all callers are direct). If safe to promote some arguments, it
|
|
|
|
/// calls the DoPromotion method.
|
|
|
|
///
|
2004-09-18 00:34:13 +00:00
|
|
|
bool ArgPromotion::PromoteArguments(CallGraphNode *CGN) {
|
|
|
|
Function *F = CGN->getFunction();
|
|
|
|
|
|
|
|
// Make sure that it is local to this module.
|
|
|
|
if (!F || !F->hasInternalLinkage()) return false;
|
2004-03-07 21:29:54 +00:00
|
|
|
|
|
|
|
// First check: see if there are any pointer arguments! If not, quick exit.
|
2008-01-11 19:20:39 +00:00
|
|
|
SmallVector<std::pair<Argument*, unsigned>, 16> PointerArgs;
|
|
|
|
unsigned ArgNo = 0;
|
|
|
|
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
|
|
|
|
I != E; ++I, ++ArgNo)
|
2004-03-07 21:29:54 +00:00
|
|
|
if (isa<PointerType>(I->getType()))
|
2008-01-11 19:20:39 +00:00
|
|
|
PointerArgs.push_back(std::pair<Argument*, unsigned>(I, ArgNo));
|
2004-03-07 21:29:54 +00:00
|
|
|
if (PointerArgs.empty()) return false;
|
|
|
|
|
|
|
|
// Second check: make sure that all callers are direct callers. We can't
|
|
|
|
// transform functions that have indirect callers.
|
|
|
|
for (Value::use_iterator UI = F->use_begin(), E = F->use_end();
|
2004-03-07 22:43:27 +00:00
|
|
|
UI != E; ++UI) {
|
|
|
|
CallSite CS = CallSite::get(*UI);
|
2004-05-23 21:21:17 +00:00
|
|
|
if (!CS.getInstruction()) // "Taking the address" of the function
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Ensure that this call site is CALLING the function, not passing it as
|
|
|
|
// an argument.
|
2008-09-07 09:54:09 +00:00
|
|
|
if (UI.getOperandNo() != 0)
|
2008-01-11 18:55:10 +00:00
|
|
|
return false;
|
2004-03-07 22:43:27 +00:00
|
|
|
}
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2008-01-11 19:20:39 +00:00
|
|
|
// Check to see which arguments are promotable. If an argument is promotable,
|
|
|
|
// add it to ArgsToPromote.
|
|
|
|
SmallPtrSet<Argument*, 8> ArgsToPromote;
|
2008-01-11 22:31:41 +00:00
|
|
|
SmallPtrSet<Argument*, 8> ByValArgsToTransform;
|
2008-01-11 19:20:39 +00:00
|
|
|
for (unsigned i = 0; i != PointerArgs.size(); ++i) {
|
2008-01-11 22:31:41 +00:00
|
|
|
bool isByVal = F->paramHasAttr(PointerArgs[i].second+1, ParamAttr::ByVal);
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
// If this is a byval argument, and if the aggregate type is small, just
|
|
|
|
// pass the elements, which is always safe.
|
|
|
|
Argument *PtrArg = PointerArgs[i].first;
|
|
|
|
if (isByVal) {
|
|
|
|
const Type *AgTy = cast<PointerType>(PtrArg->getType())->getElementType();
|
2008-05-27 11:50:51 +00:00
|
|
|
if (const StructType *STy = dyn_cast<StructType>(AgTy)) {
|
2008-04-19 19:50:01 +00:00
|
|
|
if (maxElements > 0 && STy->getNumElements() > maxElements) {
|
|
|
|
DOUT << "argpromotion disable promoting argument '"
|
|
|
|
<< PtrArg->getName() << "' because it would require adding more "
|
|
|
|
<< "than " << maxElements << " arguments to the function.\n";
|
|
|
|
} else {
|
2008-05-23 00:17:26 +00:00
|
|
|
// If all the elements are single-value types, we can promote it.
|
2008-01-11 22:31:41 +00:00
|
|
|
bool AllSimple = true;
|
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
|
2008-05-23 00:17:26 +00:00
|
|
|
if (!STy->getElementType(i)->isSingleValueType()) {
|
2008-01-11 22:31:41 +00:00
|
|
|
AllSimple = false;
|
|
|
|
break;
|
|
|
|
}
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
// Safe to transform, don't even bother trying to "promote" it.
|
|
|
|
// Passing the elements as a scalar will allow scalarrepl to hack on
|
|
|
|
// the new alloca we introduce.
|
|
|
|
if (AllSimple) {
|
|
|
|
ByValArgsToTransform.insert(PtrArg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2008-05-27 11:50:51 +00:00
|
|
|
}
|
2008-01-11 22:31:41 +00:00
|
|
|
}
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
// Otherwise, see if we can promote the pointer to its value.
|
|
|
|
if (isSafeToPromoteArgument(PtrArg, isByVal))
|
|
|
|
ArgsToPromote.insert(PtrArg);
|
2008-01-11 19:20:39 +00:00
|
|
|
}
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2004-03-07 21:29:54 +00:00
|
|
|
// No promotable pointer arguments.
|
2008-01-11 22:31:41 +00:00
|
|
|
if (ArgsToPromote.empty() && ByValArgsToTransform.empty()) return false;
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
Function *NewF = DoPromotion(F, ArgsToPromote, ByValArgsToTransform);
|
2004-09-18 00:34:13 +00:00
|
|
|
|
2007-11-27 13:23:08 +00:00
|
|
|
// Update the call graph to know that the function has been transformed.
|
2004-09-18 00:34:13 +00:00
|
|
|
getAnalysis<CallGraph>().changeFunction(F, NewF);
|
2004-03-07 21:29:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-11-13 23:31:34 +00:00
|
|
|
/// IsAlwaysValidPointer - Return true if the specified pointer is always legal
|
|
|
|
/// to load.
|
|
|
|
static bool IsAlwaysValidPointer(Value *V) {
|
|
|
|
if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true;
|
|
|
|
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V))
|
|
|
|
return IsAlwaysValidPointer(GEP->getOperand(0));
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
|
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr)
|
|
|
|
return IsAlwaysValidPointer(CE->getOperand(0));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AllCalleesPassInValidPointerForArgument - Return true if we can prove that
|
|
|
|
/// all callees pass in a valid pointer for the specified function argument.
|
|
|
|
static bool AllCalleesPassInValidPointerForArgument(Argument *Arg) {
|
|
|
|
Function *Callee = Arg->getParent();
|
|
|
|
|
2007-02-13 02:10:56 +00:00
|
|
|
unsigned ArgNo = std::distance(Callee->arg_begin(),
|
|
|
|
Function::arg_iterator(Arg));
|
2004-11-13 23:31:34 +00:00
|
|
|
|
|
|
|
// Look at all call sites of the function. At this pointer we know we only
|
|
|
|
// have direct callees.
|
|
|
|
for (Value::use_iterator UI = Callee->use_begin(), E = Callee->use_end();
|
|
|
|
UI != E; ++UI) {
|
|
|
|
CallSite CS = CallSite::get(*UI);
|
|
|
|
assert(CS.getInstruction() && "Should only have direct calls!");
|
|
|
|
|
|
|
|
if (!IsAlwaysValidPointer(CS.getArgument(ArgNo)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-29 10:00:13 +00:00
|
|
|
/// Returns true if Prefix is a prefix of longer. That means, Longer has a size
|
|
|
|
/// that is greater than or equal to the size of prefix, and each of the
|
|
|
|
/// elements in Prefix is the same as the corresponding elements in Longer.
|
|
|
|
///
|
|
|
|
/// This means it also returns true when Prefix and Longer are equal!
|
|
|
|
static bool IsPrefix(const ArgPromotion::IndicesVector &Prefix,
|
|
|
|
const ArgPromotion::IndicesVector &Longer) {
|
|
|
|
if (Prefix.size() > Longer.size())
|
|
|
|
return false;
|
|
|
|
for (unsigned i = 0, e = Prefix.size(); i != e; ++i)
|
|
|
|
if (Prefix[i] != Longer[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Checks if Indices, or a prefix of Indices, is in Set.
|
|
|
|
static bool PrefixIn(const ArgPromotion::IndicesVector &Indices,
|
|
|
|
std::set<ArgPromotion::IndicesVector> &Set) {
|
|
|
|
std::set<ArgPromotion::IndicesVector>::iterator Low;
|
|
|
|
Low = Set.upper_bound(Indices);
|
|
|
|
if (Low != Set.begin())
|
|
|
|
Low--;
|
|
|
|
// Low is now the last element smaller than or equal to Indices. This means
|
|
|
|
// it points to a prefix of Indices (possibly Indices itself), if such
|
|
|
|
// prefix exists.
|
|
|
|
//
|
|
|
|
// This load is safe if any prefix of its operands is safe to load.
|
|
|
|
return Low != Set.end() && IsPrefix(*Low, Indices);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mark the given indices (ToMark) as safe in the the given set of indices
|
|
|
|
/// (Safe). Marking safe usually means adding ToMark to Safe. However, if there
|
|
|
|
/// is already a prefix of Indices in Safe, Indices are implicitely marked safe
|
|
|
|
/// already. Furthermore, any indices that Indices is itself a prefix of, are
|
|
|
|
/// removed from Safe (since they are implicitely safe because of Indices now).
|
|
|
|
static void MarkIndicesSafe(const ArgPromotion::IndicesVector &ToMark,
|
|
|
|
std::set<ArgPromotion::IndicesVector> &Safe) {
|
|
|
|
std::set<ArgPromotion::IndicesVector>::iterator Low;
|
|
|
|
Low = Safe.upper_bound(ToMark);
|
|
|
|
// Guard against the case where Safe is empty
|
|
|
|
if (Low != Safe.begin())
|
|
|
|
Low--;
|
|
|
|
// Low is now the last element smaller than or equal to Indices. This
|
|
|
|
// means it points to a prefix of Indices (possibly Indices itself), if
|
|
|
|
// such prefix exists.
|
|
|
|
if (Low != Safe.end()) {
|
|
|
|
if (IsPrefix(*Low, ToMark))
|
|
|
|
// If there is already a prefix of these indices (or exactly these
|
|
|
|
// indices) marked a safe, don't bother adding these indices
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Increment Low, so we can use it as a "insert before" hint
|
2008-09-07 09:54:09 +00:00
|
|
|
++Low;
|
2008-07-29 10:00:13 +00:00
|
|
|
}
|
2008-09-07 09:54:09 +00:00
|
|
|
// Insert
|
2008-07-29 10:00:13 +00:00
|
|
|
Low = Safe.insert(Low, ToMark);
|
|
|
|
++Low;
|
|
|
|
// If there we're a prefix of longer index list(s), remove those
|
|
|
|
std::set<ArgPromotion::IndicesVector>::iterator End = Safe.end();
|
|
|
|
while (Low != End && IsPrefix(ToMark, *Low)) {
|
|
|
|
std::set<ArgPromotion::IndicesVector>::iterator Remove = Low;
|
|
|
|
++Low;
|
|
|
|
Safe.erase(Remove);
|
|
|
|
}
|
|
|
|
}
|
2004-05-23 21:21:17 +00:00
|
|
|
|
|
|
|
/// isSafeToPromoteArgument - As you might guess from the name of this method,
|
|
|
|
/// it checks to see if it is both safe and useful to promote the argument.
|
|
|
|
/// This method limits promotion of aggregates to only promote up to three
|
|
|
|
/// elements of the aggregate in order to avoid exploding the number of
|
|
|
|
/// arguments passed in.
|
2008-01-11 19:20:39 +00:00
|
|
|
bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg, bool isByVal) const {
|
2008-07-29 10:00:13 +00:00
|
|
|
typedef std::set<IndicesVector> GEPIndicesSet;
|
|
|
|
|
|
|
|
// Quick exit for unused arguments
|
|
|
|
if (Arg->use_empty())
|
|
|
|
return true;
|
|
|
|
|
2004-03-08 01:04:36 +00:00
|
|
|
// We can only promote this argument if all of the uses are loads, or are GEP
|
|
|
|
// instructions (with constant indices) that are subsequently loaded.
|
2008-07-29 10:00:13 +00:00
|
|
|
//
|
|
|
|
// Promoting the argument causes it to be loaded in the caller
|
|
|
|
// unconditionally. This is only safe if we can prove that either the load
|
|
|
|
// would have happened in the callee anyway (ie, there is a load in the entry
|
|
|
|
// block) or the pointer passed in at every call site is guaranteed to be
|
|
|
|
// valid.
|
|
|
|
// In the former case, invalid loads can happen, but would have happened
|
|
|
|
// anyway, in the latter case, invalid loads won't happen. This prevents us
|
|
|
|
// from introducing an invalid load that wouldn't have happened in the
|
|
|
|
// original code.
|
|
|
|
//
|
|
|
|
// This set will contain all sets of indices that are loaded in the entry
|
|
|
|
// block, and thus are safe to unconditionally load in the caller.
|
|
|
|
GEPIndicesSet SafeToUnconditionallyLoad;
|
2008-01-11 19:34:32 +00:00
|
|
|
|
2008-07-29 10:00:13 +00:00
|
|
|
// This set contains all the sets of indices that we are planning to promote.
|
|
|
|
// This makes it possible to limit the number of arguments added.
|
|
|
|
GEPIndicesSet ToPromote;
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-07-29 10:00:13 +00:00
|
|
|
// If the pointer is always valid, any load with first index 0 is valid.
|
|
|
|
if(isByVal || AllCalleesPassInValidPointerForArgument(Arg))
|
|
|
|
SafeToUnconditionallyLoad.insert(IndicesVector(1, 0));
|
|
|
|
|
|
|
|
// First, iterate the entry block and mark loads of (geps of) arguments as
|
|
|
|
// safe.
|
2004-11-13 23:31:34 +00:00
|
|
|
BasicBlock *EntryBlock = Arg->getParent()->begin();
|
2008-07-29 10:00:13 +00:00
|
|
|
// Declare this here so we can reuse it
|
|
|
|
IndicesVector Indices;
|
|
|
|
for (BasicBlock::iterator I = EntryBlock->begin(), E = EntryBlock->end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
|
|
|
|
Value *V = LI->getPointerOperand();
|
|
|
|
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
|
|
|
|
V = GEP->getPointerOperand();
|
|
|
|
if (V == Arg) {
|
|
|
|
// This load actually loads (part of) Arg? Check the indices then.
|
|
|
|
Indices.reserve(GEP->getNumIndices());
|
|
|
|
for (User::op_iterator II = GEP->idx_begin(), IE = GEP->idx_end();
|
|
|
|
II != IE; ++II)
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(*II))
|
|
|
|
Indices.push_back(CI->getSExtValue());
|
|
|
|
else
|
|
|
|
// We found a non-constant GEP index for this argument? Bail out
|
|
|
|
// right away, can't promote this argument at all.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Indices checked out, mark them as safe
|
|
|
|
MarkIndicesSafe(Indices, SafeToUnconditionallyLoad);
|
|
|
|
Indices.clear();
|
|
|
|
}
|
|
|
|
} else if (V == Arg) {
|
|
|
|
// Direct loads are equivalent to a GEP with a single 0 index.
|
|
|
|
MarkIndicesSafe(IndicesVector(1, 0), SafeToUnconditionallyLoad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, iterate all uses of the argument to see if there are any uses that are
|
|
|
|
// not (GEP+)loads, or any (GEP+)loads that are not safe to promote.
|
2008-01-11 18:43:58 +00:00
|
|
|
SmallVector<LoadInst*, 16> Loads;
|
2008-07-29 10:00:13 +00:00
|
|
|
IndicesVector Operands;
|
2004-03-07 21:29:54 +00:00
|
|
|
for (Value::use_iterator UI = Arg->use_begin(), E = Arg->use_end();
|
2008-07-29 10:00:13 +00:00
|
|
|
UI != E; ++UI) {
|
|
|
|
Operands.clear();
|
2004-03-07 21:29:54 +00:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
|
|
|
|
if (LI->isVolatile()) return false; // Don't hack volatile loads
|
|
|
|
Loads.push_back(LI);
|
2008-07-29 10:00:13 +00:00
|
|
|
// Direct loads are equivalent to a GEP with a zero index and then a load.
|
|
|
|
Operands.push_back(0);
|
2004-03-08 01:04:36 +00:00
|
|
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
|
|
|
|
if (GEP->use_empty()) {
|
|
|
|
// Dead GEP's cause trouble later. Just remove them if we run into
|
|
|
|
// them.
|
2004-05-23 21:21:17 +00:00
|
|
|
getAnalysis<AliasAnalysis>().deleteValue(GEP);
|
2008-01-11 19:20:39 +00:00
|
|
|
GEP->eraseFromParent();
|
2008-07-29 10:00:13 +00:00
|
|
|
// TODO: This runs the above loop over and over again for dead GEPS
|
|
|
|
// Couldn't we just do increment the UI iterator earlier and erase the
|
|
|
|
// use?
|
2008-01-11 19:20:39 +00:00
|
|
|
return isSafeToPromoteArgument(Arg, isByVal);
|
2004-03-08 01:04:36 +00:00
|
|
|
}
|
2008-07-29 10:00:13 +00:00
|
|
|
|
2004-03-08 01:04:36 +00:00
|
|
|
// Ensure that all of the indices are constants.
|
2008-07-29 10:00:13 +00:00
|
|
|
for (User::op_iterator i = GEP->idx_begin(), e = GEP->idx_end();
|
|
|
|
i != e; ++i)
|
2008-05-29 01:59:18 +00:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(*i))
|
2008-07-29 10:00:13 +00:00
|
|
|
Operands.push_back(C->getSExtValue());
|
2004-03-08 01:04:36 +00:00
|
|
|
else
|
|
|
|
return false; // Not a constant operand GEP!
|
|
|
|
|
|
|
|
// Ensure that the only users of the GEP are load instructions.
|
|
|
|
for (Value::use_iterator UI = GEP->use_begin(), E = GEP->use_end();
|
|
|
|
UI != E; ++UI)
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
|
|
|
|
if (LI->isVolatile()) return false; // Don't hack volatile loads
|
|
|
|
Loads.push_back(LI);
|
|
|
|
} else {
|
2008-07-29 10:00:13 +00:00
|
|
|
// Other uses than load?
|
2004-03-08 01:04:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false; // Not a load or a GEP.
|
|
|
|
}
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-07-29 10:00:13 +00:00
|
|
|
// Now, see if it is safe to promote this load / loads of this GEP. Loading
|
|
|
|
// is safe if Operands, or a prefix of Operands, is marked as safe.
|
|
|
|
if (!PrefixIn(Operands, SafeToUnconditionallyLoad))
|
|
|
|
return false;
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2008-07-29 10:00:13 +00:00
|
|
|
// See if we are already promoting a load with these indices. If not, check
|
|
|
|
// to make sure that we aren't promoting too many elements. If so, nothing
|
|
|
|
// to do.
|
|
|
|
if (ToPromote.find(Operands) == ToPromote.end()) {
|
|
|
|
if (maxElements > 0 && ToPromote.size() == maxElements) {
|
|
|
|
DOUT << "argpromotion not promoting argument '"
|
|
|
|
<< Arg->getName() << "' because it would require adding more "
|
|
|
|
<< "than " << maxElements << " arguments to the function.\n";
|
|
|
|
// We limit aggregate promotion to only promoting up to a fixed number
|
|
|
|
// of elements of the aggregate.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ToPromote.insert(Operands);
|
|
|
|
}
|
|
|
|
}
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2008-07-29 10:00:13 +00:00
|
|
|
if (Loads.empty()) return true; // No users, this is a dead argument.
|
2004-11-13 23:31:34 +00:00
|
|
|
|
|
|
|
// Okay, now we know that the argument is only used by load instructions and
|
2008-07-29 10:00:13 +00:00
|
|
|
// it is safe to unconditionally perform all of them. Use alias analysis to
|
2004-11-13 23:31:34 +00:00
|
|
|
// check to see if the pointer is guaranteed to not be modified from entry of
|
|
|
|
// the function to each of the load instructions.
|
2004-03-07 21:29:54 +00:00
|
|
|
|
|
|
|
// Because there could be several/many load instructions, remember which
|
|
|
|
// blocks we know to be transparent to the load.
|
2008-01-11 19:36:30 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 16> TranspBlocks;
|
2006-09-15 05:22:51 +00:00
|
|
|
|
|
|
|
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
|
2004-03-08 01:04:36 +00:00
|
|
|
TargetData &TD = getAnalysis<TargetData>();
|
2004-03-07 21:29:54 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = Loads.size(); i != e; ++i) {
|
|
|
|
// Check to see if the load is invalidated from the start of the block to
|
|
|
|
// the load itself.
|
|
|
|
LoadInst *Load = Loads[i];
|
|
|
|
BasicBlock *BB = Load->getParent();
|
2004-03-08 01:04:36 +00:00
|
|
|
|
|
|
|
const PointerType *LoadTy =
|
2008-07-29 10:00:13 +00:00
|
|
|
cast<PointerType>(Load->getPointerOperand()->getType());
|
Executive summary: getTypeSize -> getTypeStoreSize / getABITypeSize.
The meaning of getTypeSize was not clear - clarifying it is important
now that we have x86 long double and arbitrary precision integers.
The issue with long double is that it requires 80 bits, and this is
not a multiple of its alignment. This gives a primitive type for
which getTypeSize differed from getABITypeSize. For arbitrary precision
integers it is even worse: there is the minimum number of bits needed to
hold the type (eg: 36 for an i36), the maximum number of bits that will
be overwriten when storing the type (40 bits for i36) and the ABI size
(i.e. the storage size rounded up to a multiple of the alignment; 64 bits
for i36).
This patch removes getTypeSize (not really - it is still there but
deprecated to allow for a gradual transition). Instead there is:
(1) getTypeSizeInBits - a number of bits that suffices to hold all
values of the type. For a primitive type, this is the minimum number
of bits. For an i36 this is 36 bits. For x86 long double it is 80.
This corresponds to gcc's TYPE_PRECISION.
(2) getTypeStoreSizeInBits - the maximum number of bits that is
written when storing the type (or read when reading it). For an
i36 this is 40 bits, for an x86 long double it is 80 bits. This
is the size alias analysis is interested in (getTypeStoreSize
returns the number of bytes). There doesn't seem to be anything
corresponding to this in gcc.
(3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded
up to a multiple of the alignment. For an i36 this is 64, for an
x86 long double this is 96 or 128 depending on the OS. This is the
spacing between consecutive elements when you form an array out of
this type (getABITypeSize returns the number of bytes). This is
TYPE_SIZE in gcc.
Since successive elements in a SequentialType (arrays, pointers
and vectors) need to be aligned, the spacing between them will be
given by getABITypeSize. This means that the size of an array
is the length times the getABITypeSize. It also means that GEP
computations need to use getABITypeSize when computing offsets.
Furthermore, if an alloca allocates several elements at once then
these too need to be aligned, so the size of the alloca has to be
the number of elements multiplied by getABITypeSize. Logically
speaking this doesn't have to be the case when allocating just
one element, but it is simpler to also use getABITypeSize in this
case. So alloca's and mallocs should use getABITypeSize. Finally,
since gcc's only notion of size is that given by getABITypeSize, if
you want to output assembler etc the same as gcc then getABITypeSize
is the size you want.
Since a store will overwrite no more than getTypeStoreSize bytes,
and a read will read no more than that many bytes, this is the
notion of size appropriate for alias analysis calculations.
In this patch I have corrected all type size uses except some of
those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard
cases). I will get around to auditing these too at some point,
but I could do with some help.
Finally, I made one change which I think wise but others might
consider pointless and suboptimal: in an unpacked struct the
amount of space allocated for a field is now given by the ABI
size rather than getTypeStoreSize. I did this because every
other place that reserves memory for a type (eg: alloca) now
uses getABITypeSize, and I didn't want to make an exception
for unpacked structs, i.e. I did it to make things more uniform.
This only effects structs containing long doubles and arbitrary
precision integers. If someone wants to pack these types more
tightly they can always use a packed struct.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-01 20:53:16 +00:00
|
|
|
unsigned LoadSize = (unsigned)TD.getTypeStoreSize(LoadTy->getElementType());
|
2004-03-08 01:04:36 +00:00
|
|
|
|
2004-03-07 21:29:54 +00:00
|
|
|
if (AA.canInstructionRangeModify(BB->front(), *Load, Arg, LoadSize))
|
|
|
|
return false; // Pointer is invalidated!
|
|
|
|
|
|
|
|
// Now check every path from the entry block to the load for transparency.
|
|
|
|
// To do this, we perform a depth first search on the inverse CFG from the
|
|
|
|
// loading block.
|
|
|
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
|
2008-01-11 19:36:30 +00:00
|
|
|
for (idf_ext_iterator<BasicBlock*, SmallPtrSet<BasicBlock*, 16> >
|
|
|
|
I = idf_ext_begin(*PI, TranspBlocks),
|
2004-03-07 21:29:54 +00:00
|
|
|
E = idf_ext_end(*PI, TranspBlocks); I != E; ++I)
|
|
|
|
if (AA.canBasicBlockModify(**I, Arg, LoadSize))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the path from the entry of the function to each load is free of
|
|
|
|
// instructions that potentially invalidate the load, we can make the
|
|
|
|
// transformation!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-05-23 21:21:17 +00:00
|
|
|
/// DoPromotion - This method actually performs the promotion of the specified
|
2004-09-18 00:34:13 +00:00
|
|
|
/// arguments, and returns the new function. At this point, we know that it's
|
|
|
|
/// safe to do so.
|
|
|
|
Function *ArgPromotion::DoPromotion(Function *F,
|
2008-01-11 22:31:41 +00:00
|
|
|
SmallPtrSet<Argument*, 8> &ArgsToPromote,
|
|
|
|
SmallPtrSet<Argument*, 8> &ByValArgsToTransform) {
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-03-07 21:29:54 +00:00
|
|
|
// Start by computing a new prototype for the function, which is the same as
|
|
|
|
// the old function, but has modified arguments.
|
|
|
|
const FunctionType *FTy = F->getFunctionType();
|
|
|
|
std::vector<const Type*> Params;
|
|
|
|
|
2008-07-29 10:00:13 +00:00
|
|
|
typedef std::set<IndicesVector> ScalarizeTable;
|
2004-06-21 00:07:58 +00:00
|
|
|
|
2004-03-08 01:04:36 +00:00
|
|
|
// ScalarizedElements - If we are promoting a pointer that has elements
|
|
|
|
// accessed out of it, keep track of which elements are accessed so that we
|
|
|
|
// can add one argument for each.
|
|
|
|
//
|
|
|
|
// Arguments that are directly loaded will have a zero element value here, to
|
|
|
|
// handle cases where there are both a direct load and GEP accesses.
|
|
|
|
//
|
2004-06-21 00:07:58 +00:00
|
|
|
std::map<Argument*, ScalarizeTable> ScalarizedElements;
|
2004-03-08 01:04:36 +00:00
|
|
|
|
2004-05-23 21:21:17 +00:00
|
|
|
// OriginalLoads - Keep track of a representative load instruction from the
|
|
|
|
// original function so that we can tell the alias analysis implementation
|
|
|
|
// what the new GEP/Load instructions we are inserting look like.
|
2008-07-29 10:00:13 +00:00
|
|
|
std::map<IndicesVector, LoadInst*> OriginalLoads;
|
2004-05-23 21:21:17 +00:00
|
|
|
|
2007-11-27 13:23:08 +00:00
|
|
|
// ParamAttrs - Keep track of the parameter attributes for the arguments
|
|
|
|
// that we are *not* promoting. For the ones that we do promote, the parameter
|
|
|
|
// attributes are lost
|
2008-09-24 00:55:02 +00:00
|
|
|
SmallVector<FnAttributeWithIndex, 8> ParamAttrsVec;
|
2008-03-12 17:45:29 +00:00
|
|
|
const PAListPtr &PAL = F->getParamAttrs();
|
2007-11-27 13:23:08 +00:00
|
|
|
|
2008-02-01 20:37:16 +00:00
|
|
|
// Add any return attributes.
|
2008-09-23 23:03:40 +00:00
|
|
|
if (Attributes attrs = PAL.getParamAttrs(0))
|
2008-09-24 00:55:02 +00:00
|
|
|
ParamAttrsVec.push_back(FnAttributeWithIndex::get(0, attrs));
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-07-29 10:00:13 +00:00
|
|
|
// First, determine the new argument list
|
2008-01-17 01:17:03 +00:00
|
|
|
unsigned ArgIndex = 1;
|
2007-11-27 13:23:08 +00:00
|
|
|
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
|
2008-01-17 01:17:03 +00:00
|
|
|
++I, ++ArgIndex) {
|
2008-01-11 22:31:41 +00:00
|
|
|
if (ByValArgsToTransform.count(I)) {
|
2008-07-29 10:00:13 +00:00
|
|
|
// Simple byval argument? Just add all the struct element types.
|
2008-01-11 22:31:41 +00:00
|
|
|
const Type *AgTy = cast<PointerType>(I->getType())->getElementType();
|
|
|
|
const StructType *STy = cast<StructType>(AgTy);
|
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
|
|
|
|
Params.push_back(STy->getElementType(i));
|
|
|
|
++NumByValArgsPromoted;
|
|
|
|
} else if (!ArgsToPromote.count(I)) {
|
2008-07-29 10:00:13 +00:00
|
|
|
// Unchanged argument
|
2004-03-07 21:29:54 +00:00
|
|
|
Params.push_back(I->getType());
|
2008-09-23 23:03:40 +00:00
|
|
|
if (Attributes attrs = PAL.getParamAttrs(ArgIndex))
|
2008-09-24 00:55:02 +00:00
|
|
|
ParamAttrsVec.push_back(FnAttributeWithIndex::get(Params.size(), attrs));
|
2004-05-23 21:21:17 +00:00
|
|
|
} else if (I->use_empty()) {
|
2008-07-29 10:00:13 +00:00
|
|
|
// Dead argument (which are always marked as promotable)
|
2004-05-23 21:21:17 +00:00
|
|
|
++NumArgumentsDead;
|
|
|
|
} else {
|
2008-07-29 10:00:13 +00:00
|
|
|
// Okay, this is being promoted. This means that the only uses are loads
|
|
|
|
// or GEPs which are only used by loads
|
|
|
|
|
|
|
|
// In this table, we will track which indices are loaded from the argument
|
|
|
|
// (where direct loads are tracked as no indices).
|
2004-06-21 00:07:58 +00:00
|
|
|
ScalarizeTable &ArgIndices = ScalarizedElements[I];
|
2004-03-08 01:04:36 +00:00
|
|
|
for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
|
|
|
|
++UI) {
|
|
|
|
Instruction *User = cast<Instruction>(*UI);
|
2006-09-15 05:22:51 +00:00
|
|
|
assert(isa<LoadInst>(User) || isa<GetElementPtrInst>(User));
|
2008-07-29 10:00:13 +00:00
|
|
|
IndicesVector Indices;
|
|
|
|
Indices.reserve(User->getNumOperands() - 1);
|
|
|
|
// Since loads will only have a single operand, and GEPs only a single
|
|
|
|
// non-index operand, this will record direct loads without any indices,
|
|
|
|
// and gep+loads with the GEP indices.
|
|
|
|
for (User::op_iterator II = User->op_begin() + 1, IE = User->op_end();
|
|
|
|
II != IE; ++II)
|
|
|
|
Indices.push_back(cast<ConstantInt>(*II)->getSExtValue());
|
|
|
|
// GEPs with a single 0 index can be merged with direct loads
|
|
|
|
if (Indices.size() == 1 && Indices.front() == 0)
|
|
|
|
Indices.clear();
|
2006-09-15 05:22:51 +00:00
|
|
|
ArgIndices.insert(Indices);
|
|
|
|
LoadInst *OrigLoad;
|
|
|
|
if (LoadInst *L = dyn_cast<LoadInst>(User))
|
|
|
|
OrigLoad = L;
|
|
|
|
else
|
2008-07-29 10:00:13 +00:00
|
|
|
// Take any load, we will use it only to update Alias Analysis
|
2006-09-15 05:22:51 +00:00
|
|
|
OrigLoad = cast<LoadInst>(User->use_back());
|
|
|
|
OriginalLoads[Indices] = OrigLoad;
|
2004-03-08 01:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a parameter to the function for each element passed in.
|
2004-06-21 00:07:58 +00:00
|
|
|
for (ScalarizeTable::iterator SI = ArgIndices.begin(),
|
2008-07-29 10:00:13 +00:00
|
|
|
E = ArgIndices.end(); SI != E; ++SI) {
|
2007-02-12 22:56:41 +00:00
|
|
|
Params.push_back(GetElementPtrInst::getIndexedType(I->getType(),
|
2008-07-29 10:00:13 +00:00
|
|
|
&*SI->begin(),
|
|
|
|
SI->size()));
|
|
|
|
assert(Params.back());
|
|
|
|
}
|
2004-03-08 01:04:36 +00:00
|
|
|
|
|
|
|
if (ArgIndices.size() == 1 && ArgIndices.begin()->empty())
|
|
|
|
++NumArgumentsPromoted;
|
|
|
|
else
|
|
|
|
++NumAggregatesPromoted;
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
2008-01-11 22:31:41 +00:00
|
|
|
}
|
2004-03-07 21:29:54 +00:00
|
|
|
|
|
|
|
const Type *RetTy = FTy->getReturnType();
|
|
|
|
|
|
|
|
// Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
|
|
|
|
// have zero fixed arguments.
|
|
|
|
bool ExtraArgHack = false;
|
|
|
|
if (Params.empty() && FTy->isVarArg()) {
|
|
|
|
ExtraArgHack = true;
|
2006-12-31 05:48:39 +00:00
|
|
|
Params.push_back(Type::Int32Ty);
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
2007-11-27 13:23:08 +00:00
|
|
|
|
|
|
|
// Construct the new function type using the new arguments.
|
2004-03-07 21:29:54 +00:00
|
|
|
FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2007-11-27 13:23:08 +00:00
|
|
|
// Create the new function body and insert it into the module...
|
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-01-17 01:17:03 +00:00
|
|
|
|
|
|
|
// Recompute the parameter attributes list based on the new arguments for
|
|
|
|
// the function.
|
2008-03-12 17:45:29 +00:00
|
|
|
NF->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end()));
|
|
|
|
ParamAttrsVec.clear();
|
2008-05-26 19:58:59 +00:00
|
|
|
|
2004-03-07 21:29:54 +00:00
|
|
|
F->getParent()->getFunctionList().insert(F, NF);
|
2008-03-20 08:05:05 +00:00
|
|
|
NF->takeName(F);
|
2004-05-23 21:21:17 +00:00
|
|
|
|
|
|
|
// Get the alias analysis information that we need to update to reflect our
|
|
|
|
// changes.
|
|
|
|
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
|
|
|
|
|
2008-09-08 11:07:35 +00:00
|
|
|
// Get the callgraph information that we need to update to reflect our
|
|
|
|
// changes.
|
|
|
|
CallGraph &CG = getAnalysis<CallGraph>();
|
|
|
|
|
2004-03-07 21:29:54 +00:00
|
|
|
// Loop over all of the callers of the function, transforming the call sites
|
|
|
|
// to pass in the loaded pointers.
|
|
|
|
//
|
2008-01-17 01:17:03 +00:00
|
|
|
SmallVector<Value*, 16> Args;
|
2004-03-07 21:29:54 +00:00
|
|
|
while (!F->use_empty()) {
|
|
|
|
CallSite CS = CallSite::get(F->use_back());
|
|
|
|
Instruction *Call = CS.getInstruction();
|
2008-03-12 17:45:29 +00:00
|
|
|
const PAListPtr &CallPAL = CS.getParamAttrs();
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-02-01 20:37:16 +00:00
|
|
|
// Add any return attributes.
|
2008-09-23 23:03:40 +00:00
|
|
|
if (Attributes attrs = CallPAL.getParamAttrs(0))
|
2008-09-24 00:55:02 +00:00
|
|
|
ParamAttrsVec.push_back(FnAttributeWithIndex::get(0, attrs));
|
2008-02-01 20:37:16 +00:00
|
|
|
|
2004-05-23 21:21:17 +00:00
|
|
|
// Loop over the operands, inserting GEP and loads in the caller as
|
|
|
|
// appropriate.
|
2004-03-07 21:29:54 +00:00
|
|
|
CallSite::arg_iterator AI = CS.arg_begin();
|
2008-01-17 01:17:03 +00:00
|
|
|
ArgIndex = 1;
|
2005-05-09 01:05:50 +00:00
|
|
|
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
|
2008-01-17 01:17:03 +00:00
|
|
|
I != E; ++I, ++AI, ++ArgIndex)
|
2008-01-11 22:31:41 +00:00
|
|
|
if (!ArgsToPromote.count(I) && !ByValArgsToTransform.count(I)) {
|
2004-03-07 21:29:54 +00:00
|
|
|
Args.push_back(*AI); // Unmodified argument
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-09-23 23:03:40 +00:00
|
|
|
if (Attributes Attrs = CallPAL.getParamAttrs(ArgIndex))
|
2008-09-24 00:55:02 +00:00
|
|
|
ParamAttrsVec.push_back(FnAttributeWithIndex::get(Args.size(), Attrs));
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
} else if (ByValArgsToTransform.count(I)) {
|
|
|
|
// Emit a GEP and load for each element of the struct.
|
|
|
|
const Type *AgTy = cast<PointerType>(I->getType())->getElementType();
|
|
|
|
const StructType *STy = cast<StructType>(AgTy);
|
|
|
|
Value *Idxs[2] = { ConstantInt::get(Type::Int32Ty, 0), 0 };
|
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
|
|
|
|
Idxs[1] = ConstantInt::get(Type::Int32Ty, i);
|
2008-04-06 20:25:17 +00:00
|
|
|
Value *Idx = GetElementPtrInst::Create(*AI, Idxs, Idxs+2,
|
|
|
|
(*AI)->getName()+"."+utostr(i),
|
|
|
|
Call);
|
2008-01-11 22:31:41 +00:00
|
|
|
// TODO: Tell AA about the new values?
|
|
|
|
Args.push_back(new LoadInst(Idx, Idx->getName()+".val", Call));
|
2008-09-07 09:54:09 +00:00
|
|
|
}
|
2008-01-11 22:31:41 +00:00
|
|
|
} else if (!I->use_empty()) {
|
2004-05-23 21:21:17 +00:00
|
|
|
// Non-dead argument: insert GEPs and loads as appropriate.
|
2004-06-21 00:07:58 +00:00
|
|
|
ScalarizeTable &ArgIndices = ScalarizedElements[I];
|
2008-07-29 10:00:13 +00:00
|
|
|
// Store the Value* version of the indices in here, but declare it now
|
|
|
|
// for reuse
|
|
|
|
std::vector<Value*> Ops;
|
2004-06-21 00:07:58 +00:00
|
|
|
for (ScalarizeTable::iterator SI = ArgIndices.begin(),
|
2004-03-08 01:04:36 +00:00
|
|
|
E = ArgIndices.end(); SI != E; ++SI) {
|
|
|
|
Value *V = *AI;
|
2004-05-23 21:21:17 +00:00
|
|
|
LoadInst *OrigLoad = OriginalLoads[*SI];
|
|
|
|
if (!SI->empty()) {
|
2008-07-29 10:00:13 +00:00
|
|
|
Ops.reserve(SI->size());
|
|
|
|
const Type *ElTy = V->getType();
|
2008-09-07 09:54:09 +00:00
|
|
|
for (IndicesVector::const_iterator II = SI->begin(),
|
2008-07-29 10:00:13 +00:00
|
|
|
IE = SI->end(); II != IE; ++II) {
|
|
|
|
// Use i32 to index structs, and i64 for others (pointers/arrays).
|
|
|
|
// This satisfies GEP constraints.
|
|
|
|
const Type *IdxTy = (isa<StructType>(ElTy) ? Type::Int32Ty : Type::Int64Ty);
|
|
|
|
Ops.push_back(ConstantInt::get(IdxTy, *II));
|
|
|
|
// Keep track of the type we're currently indexing
|
|
|
|
ElTy = cast<CompositeType>(ElTy)->getTypeAtIndex(*II);
|
|
|
|
}
|
|
|
|
// And create a GEP to extract those indices
|
|
|
|
V = GetElementPtrInst::Create(V, Ops.begin(), Ops.end(),
|
2008-04-06 20:25:17 +00:00
|
|
|
V->getName()+".idx", Call);
|
2008-07-29 10:00:13 +00:00
|
|
|
Ops.clear();
|
2004-05-23 21:21:17 +00:00
|
|
|
AA.copyValue(OrigLoad->getOperand(0), V);
|
|
|
|
}
|
2004-03-08 01:04:36 +00:00
|
|
|
Args.push_back(new LoadInst(V, V->getName()+".val", Call));
|
2004-05-23 21:21:17 +00:00
|
|
|
AA.copyValue(OrigLoad, Args.back());
|
2004-03-08 01:04:36 +00:00
|
|
|
}
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ExtraArgHack)
|
2006-12-31 05:48:39 +00:00
|
|
|
Args.push_back(Constant::getNullValue(Type::Int32Ty));
|
2004-03-07 21:29:54 +00:00
|
|
|
|
|
|
|
// Push any varargs arguments on the list
|
2008-01-17 01:17:03 +00:00
|
|
|
for (; AI != CS.arg_end(); ++AI, ++ArgIndex) {
|
2004-03-07 21:29:54 +00:00
|
|
|
Args.push_back(*AI);
|
2008-09-23 23:03:40 +00:00
|
|
|
if (Attributes Attrs = CallPAL.getParamAttrs(ArgIndex))
|
2008-09-24 00:55:02 +00:00
|
|
|
ParamAttrsVec.push_back(FnAttributeWithIndex::get(Args.size(), Attrs));
|
2008-01-17 01:17:03 +00:00
|
|
|
}
|
2004-03-07 21:29:54 +00:00
|
|
|
|
|
|
|
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);
|
2005-05-09 01:05:50 +00:00
|
|
|
cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
|
2008-03-12 17:45:29 +00:00
|
|
|
cast<InvokeInst>(New)->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(),
|
|
|
|
ParamAttrsVec.end()));
|
2004-03-07 21:29:54 +00:00
|
|
|
} else {
|
2008-04-06 20:25:17 +00:00
|
|
|
New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
|
2005-05-09 01:05:50 +00:00
|
|
|
cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
|
2008-03-12 17:45:29 +00:00
|
|
|
cast<CallInst>(New)->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(),
|
|
|
|
ParamAttrsVec.end()));
|
2005-05-06 06:46:58 +00:00
|
|
|
if (cast<CallInst>(Call)->isTailCall())
|
|
|
|
cast<CallInst>(New)->setTailCall();
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
|
|
|
Args.clear();
|
2008-01-17 01:17:03 +00:00
|
|
|
ParamAttrsVec.clear();
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2004-05-23 21:21:17 +00:00
|
|
|
// Update the alias analysis implementation to know that we are replacing
|
|
|
|
// the old call with a new one.
|
|
|
|
AA.replaceWithNewValue(Call, New);
|
|
|
|
|
2008-09-08 11:07:35 +00:00
|
|
|
// Update the callgraph to know that the callsite has been transformed.
|
|
|
|
CG[Call->getParent()->getParent()]->replaceCallSite(Call, New);
|
|
|
|
|
2004-03-07 21:29:54 +00:00
|
|
|
if (!Call->use_empty()) {
|
|
|
|
Call->replaceAllUsesWith(New);
|
2007-02-11 01:08:35 +00:00
|
|
|
New->takeName(Call);
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-03-07 21:29:54 +00:00
|
|
|
// Finally, remove the old call from the program, reducing the use-count of
|
|
|
|
// F.
|
2008-01-11 19:20:39 +00:00
|
|
|
Call->eraseFromParent();
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since we have now created the new function, splice the body of the old
|
|
|
|
// function right into the new function, leaving the old rotting hulk of the
|
|
|
|
// function empty.
|
|
|
|
NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
|
|
|
|
|
|
|
|
// Loop over the argument list, transfering uses of the old arguments over to
|
|
|
|
// the new arguments, also transfering over the names as well.
|
|
|
|
//
|
2007-02-13 02:10:56 +00:00
|
|
|
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
|
2008-01-11 22:31:41 +00:00
|
|
|
I2 = NF->arg_begin(); I != E; ++I) {
|
|
|
|
if (!ArgsToPromote.count(I) && !ByValArgsToTransform.count(I)) {
|
2004-03-07 21:29:54 +00:00
|
|
|
// If this is an unmodified argument, move the name and users over to the
|
|
|
|
// new version.
|
|
|
|
I->replaceAllUsesWith(I2);
|
2007-02-11 01:08:35 +00:00
|
|
|
I2->takeName(I);
|
2004-05-23 21:21:17 +00:00
|
|
|
AA.replaceWithNewValue(I, I2);
|
2004-03-07 21:29:54 +00:00
|
|
|
++I2;
|
2008-01-11 22:31:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
if (ByValArgsToTransform.count(I)) {
|
|
|
|
// In the callee, we create an alloca, and store each of the new incoming
|
|
|
|
// arguments into the alloca.
|
|
|
|
Instruction *InsertPt = NF->begin()->begin();
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
// Just add all the struct element types.
|
|
|
|
const Type *AgTy = cast<PointerType>(I->getType())->getElementType();
|
|
|
|
Value *TheAlloca = new AllocaInst(AgTy, 0, "", InsertPt);
|
|
|
|
const StructType *STy = cast<StructType>(AgTy);
|
|
|
|
Value *Idxs[2] = { ConstantInt::get(Type::Int32Ty, 0), 0 };
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
|
|
|
|
Idxs[1] = ConstantInt::get(Type::Int32Ty, i);
|
2008-07-29 10:00:13 +00:00
|
|
|
std::string Name = TheAlloca->getName()+"."+utostr(i);
|
2008-04-06 20:25:17 +00:00
|
|
|
Value *Idx = GetElementPtrInst::Create(TheAlloca, Idxs, Idxs+2,
|
2008-07-29 10:00:13 +00:00
|
|
|
Name, InsertPt);
|
2008-01-11 22:31:41 +00:00
|
|
|
I2->setName(I->getName()+"."+utostr(i));
|
|
|
|
new StoreInst(I2++, Idx, InsertPt);
|
|
|
|
}
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
// Anything that used the arg should now use the alloca.
|
|
|
|
I->replaceAllUsesWith(TheAlloca);
|
|
|
|
TheAlloca->takeName(I);
|
|
|
|
AA.replaceWithNewValue(I, TheAlloca);
|
|
|
|
continue;
|
2008-09-07 09:54:09 +00:00
|
|
|
}
|
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
if (I->use_empty()) {
|
2004-05-23 21:21:17 +00:00
|
|
|
AA.deleteValue(I);
|
2008-01-11 22:31:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
// Otherwise, if we promoted this argument, then all users are load
|
2008-07-29 10:00:13 +00:00
|
|
|
// instructions (or GEPs with only load users), and all loads should be
|
|
|
|
// using the new argument that we added.
|
2008-01-11 22:31:41 +00:00
|
|
|
ScalarizeTable &ArgIndices = ScalarizedElements[I];
|
|
|
|
|
|
|
|
while (!I->use_empty()) {
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I->use_back())) {
|
|
|
|
assert(ArgIndices.begin()->empty() &&
|
|
|
|
"Load element should sort to front!");
|
|
|
|
I2->setName(I->getName()+".val");
|
|
|
|
LI->replaceAllUsesWith(I2);
|
|
|
|
AA.replaceWithNewValue(LI, I2);
|
|
|
|
LI->eraseFromParent();
|
|
|
|
DOUT << "*** Promoted load of argument '" << I->getName()
|
|
|
|
<< "' in function '" << F->getName() << "'\n";
|
|
|
|
} else {
|
|
|
|
GetElementPtrInst *GEP = cast<GetElementPtrInst>(I->use_back());
|
2008-07-29 10:00:13 +00:00
|
|
|
IndicesVector Operands;
|
|
|
|
Operands.reserve(GEP->getNumIndices());
|
|
|
|
for (User::op_iterator II = GEP->idx_begin(), IE = GEP->idx_end();
|
|
|
|
II != IE; ++II)
|
|
|
|
Operands.push_back(cast<ConstantInt>(*II)->getSExtValue());
|
|
|
|
|
|
|
|
// GEPs with a single 0 index can be merged with direct loads
|
|
|
|
if (Operands.size() == 1 && Operands.front() == 0)
|
|
|
|
Operands.clear();
|
2008-01-11 22:31:41 +00:00
|
|
|
|
|
|
|
Function::arg_iterator TheArg = I2;
|
|
|
|
for (ScalarizeTable::iterator It = ArgIndices.begin();
|
|
|
|
*It != Operands; ++It, ++TheArg) {
|
|
|
|
assert(It != ArgIndices.end() && "GEP not handled??");
|
|
|
|
}
|
2004-03-08 01:04:36 +00:00
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
std::string NewName = I->getName();
|
2008-07-29 10:00:13 +00:00
|
|
|
for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
|
|
|
|
NewName += "." + utostr(Operands[i]);
|
|
|
|
}
|
|
|
|
NewName += ".val";
|
|
|
|
TheArg->setName(NewName);
|
2008-01-11 22:31:41 +00:00
|
|
|
|
|
|
|
DOUT << "*** Promoted agg argument '" << TheArg->getName()
|
2008-07-29 10:00:13 +00:00
|
|
|
<< "' of function '" << NF->getName() << "'\n";
|
2008-01-11 22:31:41 +00:00
|
|
|
|
|
|
|
// All of the uses must be load instructions. Replace them all with
|
|
|
|
// the argument specified by ArgNo.
|
|
|
|
while (!GEP->use_empty()) {
|
|
|
|
LoadInst *L = cast<LoadInst>(GEP->use_back());
|
|
|
|
L->replaceAllUsesWith(TheArg);
|
|
|
|
AA.replaceWithNewValue(L, TheArg);
|
|
|
|
L->eraseFromParent();
|
2004-03-08 01:04:36 +00:00
|
|
|
}
|
2008-01-11 22:31:41 +00:00
|
|
|
AA.deleteValue(GEP);
|
|
|
|
GEP->eraseFromParent();
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
// Increment I2 past all of the arguments added for this promoted pointer.
|
|
|
|
for (unsigned i = 0, e = ArgIndices.size(); i != e; ++i)
|
|
|
|
++I2;
|
|
|
|
}
|
|
|
|
|
2004-05-23 21:21:17 +00:00
|
|
|
// Notify the alias analysis implementation that we inserted a new argument.
|
|
|
|
if (ExtraArgHack)
|
2006-12-31 05:48:39 +00:00
|
|
|
AA.copyValue(Constant::getNullValue(Type::Int32Ty), NF->arg_begin());
|
2004-05-23 21:21:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Tell the alias analysis that the old function is about to disappear.
|
|
|
|
AA.replaceWithNewValue(F, NF);
|
|
|
|
|
2004-03-07 21:29:54 +00:00
|
|
|
// Now that the old function is dead, delete it.
|
2008-01-11 19:20:39 +00:00
|
|
|
F->eraseFromParent();
|
2004-09-18 00:34:13 +00:00
|
|
|
return NF;
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|