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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2013-01-07 15:26:48 +00:00
|
|
|
#include "llvm/Analysis/CallGraphSCCPass.h"
|
2014-03-04 11:45:46 +00:00
|
|
|
#include "llvm/IR/CFG.h"
|
2014-03-04 11:01:28 +00:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-25 00:23:56 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-03-07 21:29:54 +00:00
|
|
|
#include <set>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:55:47 +00:00
|
|
|
#define DEBUG_TYPE "argpromotion"
|
|
|
|
|
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.
|
|
|
|
///
|
2009-10-25 06:33:48 +00:00
|
|
|
struct ArgPromotion : public CallGraphSCCPass {
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2004-03-07 21:29:54 +00:00
|
|
|
AU.addRequired<AliasAnalysis>();
|
2004-09-18 00:34:13 +00:00
|
|
|
CallGraphSCCPass::getAnalysisUsage(AU);
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnSCC(CallGraphSCC &SCC) override;
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2009-02-18 16:37:45 +00:00
|
|
|
explicit ArgPromotion(unsigned maxElements = 3)
|
2010-10-19 17:21:58 +00:00
|
|
|
: CallGraphSCCPass(ID), maxElements(maxElements) {
|
|
|
|
initializeArgPromotionPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
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:
|
2009-08-31 00:19:58 +00:00
|
|
|
CallGraphNode *PromoteArguments(CallGraphNode *CGN);
|
2008-01-11 19:20:39 +00:00
|
|
|
bool isSafeToPromoteArgument(Argument *Arg, bool isByVal) const;
|
2009-08-31 00:19:58 +00:00
|
|
|
CallGraphNode *DoPromotion(Function *F,
|
|
|
|
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;
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(ArgPromotion, "argpromotion",
|
|
|
|
"Promote 'by reference' arguments to scalars", false, false)
|
|
|
|
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
|
2013-11-26 04:19:30 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_END(ArgPromotion, "argpromotion",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Promote 'by reference' arguments to scalars", false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-04-16 22:42:17 +00:00
|
|
|
bool ArgPromotion::runOnSCC(CallGraphSCC &SCC) {
|
2004-09-18 00:34:13 +00:00
|
|
|
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.
|
2010-04-16 22:42:17 +00:00
|
|
|
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
|
|
|
|
if (CallGraphNode *CGN = PromoteArguments(*I)) {
|
2009-08-31 00:19:58 +00:00
|
|
|
LocalChange = true;
|
2010-04-16 22:42:17 +00:00
|
|
|
SCC.ReplaceNode(*I, CGN);
|
2009-08-31 00:19:58 +00:00
|
|
|
}
|
2010-04-16 22:42:17 +00:00
|
|
|
}
|
2004-09-18 00:34:13 +00:00
|
|
|
Changed |= LocalChange; // Remember that we changed something.
|
|
|
|
} while (LocalChange);
|
2010-04-20 00:46:50 +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.
|
|
|
|
///
|
2009-08-31 00:19:58 +00:00
|
|
|
CallGraphNode *ArgPromotion::PromoteArguments(CallGraphNode *CGN) {
|
2004-09-18 00:34:13 +00:00
|
|
|
Function *F = CGN->getFunction();
|
|
|
|
|
|
|
|
// Make sure that it is local to this module.
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!F || !F->hasLocalLinkage()) return nullptr;
|
2004-03-07 21:29:54 +00:00
|
|
|
|
|
|
|
// First check: see if there are any pointer arguments! If not, quick exit.
|
2013-07-18 22:32:32 +00:00
|
|
|
SmallVector<Argument*, 16> PointerArgs;
|
|
|
|
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
|
2010-02-16 11:11:14 +00:00
|
|
|
if (I->getType()->isPointerTy())
|
2013-07-18 22:32:32 +00:00
|
|
|
PointerArgs.push_back(I);
|
2014-04-25 05:29:35 +00:00
|
|
|
if (PointerArgs.empty()) return nullptr;
|
2004-03-07 21:29:54 +00:00
|
|
|
|
|
|
|
// Second check: make sure that all callers are direct callers. We can't
|
2011-01-16 08:09:24 +00:00
|
|
|
// transform functions that have indirect callers. Also see if the function
|
|
|
|
// is self-recursive.
|
|
|
|
bool isSelfRecursive = false;
|
2014-03-09 03:16:01 +00:00
|
|
|
for (Use &U : F->uses()) {
|
|
|
|
CallSite CS(U.getUser());
|
2011-01-16 08:09:24 +00:00
|
|
|
// Must be a direct call.
|
2014-04-25 05:29:35 +00:00
|
|
|
if (CS.getInstruction() == nullptr || !CS.isCallee(&U)) return nullptr;
|
2011-01-16 08:09:24 +00:00
|
|
|
|
|
|
|
if (CS.getInstruction()->getParent()->getParent() == F)
|
|
|
|
isSelfRecursive = true;
|
|
|
|
}
|
|
|
|
|
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;
|
2013-07-18 22:32:32 +00:00
|
|
|
for (unsigned i = 0, e = PointerArgs.size(); i != e; ++i) {
|
|
|
|
Argument *PtrArg = PointerArgs[i];
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *AgTy = cast<PointerType>(PtrArg->getType())->getElementType();
|
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
|
2014-01-28 02:38:36 +00:00
|
|
|
// pass the elements, which is always safe. This does not apply to
|
|
|
|
// inalloca.
|
2013-07-18 22:32:32 +00:00
|
|
|
if (PtrArg->hasByValAttr()) {
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(AgTy)) {
|
2008-04-19 19:50:01 +00:00
|
|
|
if (maxElements > 0 && STy->getNumElements() > maxElements) {
|
2010-01-05 01:28:37 +00:00
|
|
|
DEBUG(dbgs() << "argpromotion disable promoting argument '"
|
2009-07-25 00:23:56 +00:00
|
|
|
<< PtrArg->getName() << "' because it would require adding more"
|
|
|
|
<< " than " << maxElements << " arguments to the function.\n");
|
2011-01-16 08:09:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all the elements are single-value types, we can promote it.
|
|
|
|
bool AllSimple = true;
|
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
|
|
|
|
if (!STy->getElementType(i)->isSingleValueType()) {
|
|
|
|
AllSimple = false;
|
|
|
|
break;
|
2008-01-11 22:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-16 08:09:24 +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
|
|
|
|
2011-01-16 08:09:24 +00:00
|
|
|
// If the argument is a recursive type and we're in a recursive
|
|
|
|
// function, we could end up infinitely peeling the function argument.
|
|
|
|
if (isSelfRecursive) {
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(AgTy)) {
|
2011-01-16 08:09:24 +00:00
|
|
|
bool RecursiveType = false;
|
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
|
|
|
|
if (STy->getElementType(i) == PtrArg->getType()) {
|
|
|
|
RecursiveType = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (RecursiveType)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-11 22:31:41 +00:00
|
|
|
// Otherwise, see if we can promote the pointer to its value.
|
2014-01-28 02:38:36 +00:00
|
|
|
if (isSafeToPromoteArgument(PtrArg, PtrArg->hasByValOrInAllocaAttr()))
|
2008-01-11 22:31:41 +00:00
|
|
|
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.
|
2009-08-31 00:19:58 +00:00
|
|
|
if (ArgsToPromote.empty() && ByValArgsToTransform.empty())
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2009-08-31 00:19:58 +00:00
|
|
|
return DoPromotion(F, ArgsToPromote, ByValArgsToTransform);
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
2011-01-16 08:09:24 +00:00
|
|
|
/// AllCallersPassInValidPointerForArgument - Return true if we can prove that
|
2004-11-13 23:31:34 +00:00
|
|
|
/// all callees pass in a valid pointer for the specified function argument.
|
2011-01-16 08:09:24 +00:00
|
|
|
static bool AllCallersPassInValidPointerForArgument(Argument *Arg) {
|
2004-11-13 23:31:34 +00:00
|
|
|
Function *Callee = Arg->getParent();
|
|
|
|
|
2013-07-18 22:32:32 +00:00
|
|
|
unsigned ArgNo = Arg->getArgNo();
|
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.
|
2014-03-09 03:16:01 +00:00
|
|
|
for (User *U : Callee->users()) {
|
|
|
|
CallSite CS(U);
|
2010-07-28 22:50:26 +00:00
|
|
|
assert(CS && "Should only have direct calls!");
|
2004-11-13 23:31:34 +00:00
|
|
|
|
2010-11-11 21:23:25 +00:00
|
|
|
if (!CS.getArgument(ArgNo)->isDereferenceablePointer())
|
2004-11-13 23:31:34 +00:00
|
|
|
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;
|
2012-07-19 10:46:05 +00:00
|
|
|
return std::equal(Prefix.begin(), Prefix.end(), Longer.begin());
|
2008-07-29 10:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2010-02-10 16:03:48 +00:00
|
|
|
/// Mark the given indices (ToMark) as safe in the given set of indices
|
2008-07-29 10:00:13 +00:00
|
|
|
/// (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.
|
2014-01-28 02:38:36 +00:00
|
|
|
bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg,
|
|
|
|
bool isByValOrInAlloca) 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.
|
2014-01-28 02:38:36 +00:00
|
|
|
//
|
|
|
|
// This optimization is also safe for InAlloca parameters, because it verifies
|
|
|
|
// that the address isn't captured.
|
2008-07-29 10:00:13 +00:00
|
|
|
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.
|
2014-01-28 02:38:36 +00:00
|
|
|
if (isByValOrInAlloca || AllCallersPassInValidPointerForArgument(Arg))
|
2008-07-29 10:00:13 +00:00
|
|
|
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;
|
2014-03-09 03:16:01 +00:00
|
|
|
for (Use &U : Arg->uses()) {
|
|
|
|
User *UR = U.getUser();
|
2008-07-29 10:00:13 +00:00
|
|
|
Operands.clear();
|
2014-03-09 03:16:01 +00:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(UR)) {
|
2011-08-15 22:16:46 +00:00
|
|
|
// Don't hack volatile/atomic loads
|
|
|
|
if (!LI->isSimple()) return false;
|
2004-03-07 21:29:54 +00:00
|
|
|
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);
|
2014-03-09 03:16:01 +00:00
|
|
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(UR)) {
|
2004-03-08 01:04:36 +00:00
|
|
|
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();
|
2010-07-12 14:15:10 +00:00
|
|
|
// TODO: This runs the above loop over and over again for dead GEPs
|
2008-07-29 10:00:13 +00:00
|
|
|
// Couldn't we just do increment the UI iterator earlier and erase the
|
|
|
|
// use?
|
2014-01-28 02:38:36 +00:00
|
|
|
return isSafeToPromoteArgument(Arg, isByValOrInAlloca);
|
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.
|
2014-03-09 03:16:01 +00:00
|
|
|
for (User *GEPU : GEP->users())
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(GEPU)) {
|
2011-08-15 22:16:46 +00:00
|
|
|
// Don't hack volatile/atomic loads
|
|
|
|
if (!LI->isSimple()) return false;
|
2004-03-08 01:04:36 +00:00
|
|
|
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) {
|
2010-01-05 01:28:37 +00:00
|
|
|
DEBUG(dbgs() << "argpromotion not promoting argument '"
|
2009-07-25 00:23:56 +00:00
|
|
|
<< Arg->getName() << "' because it would require adding more "
|
|
|
|
<< "than " << maxElements << " arguments to the function.\n");
|
2008-07-29 10:00:13 +00:00
|
|
|
// 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-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
|
|
|
|
2010-11-11 21:50:19 +00:00
|
|
|
AliasAnalysis::Location Loc = AA.getLocation(Load);
|
2010-11-11 18:09:32 +00:00
|
|
|
if (AA.canInstructionRangeModify(BB->front(), *Load, Loc))
|
2004-03-07 21:29:54 +00:00
|
|
|
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.
|
2010-07-12 14:15:10 +00:00
|
|
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
|
|
|
|
BasicBlock *P = *PI;
|
2008-01-11 19:36:30 +00:00
|
|
|
for (idf_ext_iterator<BasicBlock*, SmallPtrSet<BasicBlock*, 16> >
|
2010-07-12 14:15:10 +00:00
|
|
|
I = idf_ext_begin(P, TranspBlocks),
|
|
|
|
E = idf_ext_end(P, TranspBlocks); I != E; ++I)
|
2010-11-11 18:09:32 +00:00
|
|
|
if (AA.canBasicBlockModify(**I, Loc))
|
2004-03-07 21:29:54 +00:00
|
|
|
return false;
|
2010-07-12 14:15:10 +00:00
|
|
|
}
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2009-08-31 00:19:58 +00:00
|
|
|
CallGraphNode *ArgPromotion::DoPromotion(Function *F,
|
|
|
|
SmallPtrSet<Argument*, 8> &ArgsToPromote,
|
2008-01-11 22:31:41 +00:00
|
|
|
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.
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *FTy = F->getFunctionType();
|
2011-07-12 14:06:48 +00:00
|
|
|
std::vector<Type*> Params;
|
2004-03-07 21:29:54 +00:00
|
|
|
|
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.
|
2013-11-15 20:41:15 +00:00
|
|
|
// We need to keep the original loads for each argument and the elements
|
|
|
|
// of the argument that are accessed.
|
|
|
|
std::map<std::pair<Argument*, IndicesVector>, LoadInst*> OriginalLoads;
|
2004-05-23 21:21:17 +00:00
|
|
|
|
2012-12-19 07:18:57 +00:00
|
|
|
// Attribute - Keep track of the parameter attributes for the arguments
|
2007-11-27 13:23:08 +00:00
|
|
|
// that we are *not* promoting. For the ones that we do promote, the parameter
|
|
|
|
// attributes are lost
|
2013-01-27 01:57:28 +00:00
|
|
|
SmallVector<AttributeSet, 8> AttributesVec;
|
2012-12-07 23:16:57 +00:00
|
|
|
const AttributeSet &PAL = F->getAttributes();
|
2007-11-27 13:23:08 +00:00
|
|
|
|
2008-02-01 20:37:16 +00:00
|
|
|
// Add any return attributes.
|
2013-01-18 21:53:16 +00:00
|
|
|
if (PAL.hasAttributes(AttributeSet::ReturnIndex))
|
2013-01-27 01:57:28 +00:00
|
|
|
AttributesVec.push_back(AttributeSet::get(F->getContext(),
|
|
|
|
PAL.getRetAttributes()));
|
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.
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *AgTy = cast<PointerType>(I->getType())->getElementType();
|
|
|
|
StructType *STy = cast<StructType>(AgTy);
|
2008-01-11 22:31:41 +00:00
|
|
|
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());
|
2013-01-23 06:14:59 +00:00
|
|
|
AttributeSet attrs = PAL.getParamAttributes(ArgIndex);
|
|
|
|
if (attrs.hasAttributes(ArgIndex)) {
|
2013-01-27 01:57:28 +00:00
|
|
|
AttrBuilder B(attrs, ArgIndex);
|
2013-01-23 06:14:59 +00:00
|
|
|
AttributesVec.
|
2013-01-27 01:57:28 +00:00
|
|
|
push_back(AttributeSet::get(F->getContext(), Params.size(), B));
|
2013-01-23 06:14:59 +00:00
|
|
|
}
|
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];
|
2014-03-09 03:16:01 +00:00
|
|
|
for (User *U : I->users()) {
|
|
|
|
Instruction *UI = cast<Instruction>(U);
|
|
|
|
assert(isa<LoadInst>(UI) || isa<GetElementPtrInst>(UI));
|
2008-07-29 10:00:13 +00:00
|
|
|
IndicesVector Indices;
|
2014-03-09 03:16:01 +00:00
|
|
|
Indices.reserve(UI->getNumOperands() - 1);
|
2008-07-29 10:00:13 +00:00
|
|
|
// 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.
|
2014-03-09 03:16:01 +00:00
|
|
|
for (User::op_iterator II = UI->op_begin() + 1, IE = UI->op_end();
|
2008-07-29 10:00:13 +00:00
|
|
|
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;
|
2014-03-09 03:16:01 +00:00
|
|
|
if (LoadInst *L = dyn_cast<LoadInst>(UI))
|
2006-09-15 05:22:51 +00:00
|
|
|
OrigLoad = L;
|
|
|
|
else
|
2008-07-29 10:00:13 +00:00
|
|
|
// Take any load, we will use it only to update Alias Analysis
|
2014-03-09 03:16:01 +00:00
|
|
|
OrigLoad = cast<LoadInst>(UI->user_back());
|
2013-11-15 20:41:15 +00:00
|
|
|
OriginalLoads[std::make_pair(I, 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) {
|
2008-11-16 17:21:25 +00:00
|
|
|
// not allowed to dereference ->begin() if size() is 0
|
2011-07-25 09:48:08 +00:00
|
|
|
Params.push_back(GetElementPtrInst::getIndexedType(I->getType(), *SI));
|
2008-07-29 10:00:13 +00:00
|
|
|
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
|
|
|
|
2008-09-26 22:53:05 +00:00
|
|
|
// Add any function attributes.
|
2013-01-18 21:11:39 +00:00
|
|
|
if (PAL.hasAttributes(AttributeSet::FunctionIndex))
|
2013-01-27 01:57:28 +00:00
|
|
|
AttributesVec.push_back(AttributeSet::get(FTy->getContext(),
|
|
|
|
PAL.getFnAttributes()));
|
2008-09-26 22:53:05 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *RetTy = FTy->getReturnType();
|
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.
|
2009-07-29 22:17:13 +00:00
|
|
|
FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2009-09-15 05:40:35 +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
|
|
|
|
2009-08-31 05:22:48 +00:00
|
|
|
|
2010-01-05 01:28:37 +00:00
|
|
|
DEBUG(dbgs() << "ARG PROMOTION: Promoting to:" << *NF << "\n"
|
2009-08-31 05:22:48 +00:00
|
|
|
<< "From: " << *F);
|
|
|
|
|
2008-01-17 01:17:03 +00:00
|
|
|
// Recompute the parameter attributes list based on the new arguments for
|
|
|
|
// the function.
|
2012-12-07 23:16:57 +00:00
|
|
|
NF->setAttributes(AttributeSet::get(F->getContext(), AttributesVec));
|
2008-09-25 21:00:45 +00:00
|
|
|
AttributesVec.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.
|
2013-11-26 04:19:30 +00:00
|
|
|
CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
|
|
|
|
|
2009-08-31 00:19:58 +00:00
|
|
|
// Get a new callgraph node for NF.
|
|
|
|
CallGraphNode *NF_CGN = CG.getOrInsertFunction(NF);
|
2008-09-08 11:07:35 +00:00
|
|
|
|
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()) {
|
2014-03-09 03:16:01 +00:00
|
|
|
CallSite CS(F->user_back());
|
2010-03-23 14:40:20 +00:00
|
|
|
assert(CS.getCalledFunction() == F);
|
2004-03-07 21:29:54 +00:00
|
|
|
Instruction *Call = CS.getInstruction();
|
2012-12-07 23:16:57 +00:00
|
|
|
const AttributeSet &CallPAL = CS.getAttributes();
|
2008-09-07 09:54:09 +00:00
|
|
|
|
2008-02-01 20:37:16 +00:00
|
|
|
// Add any return attributes.
|
2013-01-18 21:53:16 +00:00
|
|
|
if (CallPAL.hasAttributes(AttributeSet::ReturnIndex))
|
2013-01-27 01:57:28 +00:00
|
|
|
AttributesVec.push_back(AttributeSet::get(F->getContext(),
|
|
|
|
CallPAL.getRetAttributes()));
|
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
|
|
|
|
2013-01-23 06:14:59 +00:00
|
|
|
if (CallPAL.hasAttributes(ArgIndex)) {
|
2013-01-27 01:57:28 +00:00
|
|
|
AttrBuilder B(CallPAL, ArgIndex);
|
2013-01-23 06:14:59 +00:00
|
|
|
AttributesVec.
|
2013-01-27 01:57:28 +00:00
|
|
|
push_back(AttributeSet::get(F->getContext(), Args.size(), B));
|
2013-01-23 06:14:59 +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.
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *AgTy = cast<PointerType>(I->getType())->getElementType();
|
|
|
|
StructType *STy = cast<StructType>(AgTy);
|
2009-08-13 21:58:54 +00:00
|
|
|
Value *Idxs[2] = {
|
2014-04-25 05:29:35 +00:00
|
|
|
ConstantInt::get(Type::getInt32Ty(F->getContext()), 0), nullptr };
|
2008-01-11 22:31:41 +00:00
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
|
2009-08-13 21:58:54 +00:00
|
|
|
Idxs[1] = ConstantInt::get(Type::getInt32Ty(F->getContext()), i);
|
2011-07-25 09:48:08 +00:00
|
|
|
Value *Idx = GetElementPtrInst::Create(*AI, Idxs,
|
2008-04-06 20:25:17 +00:00
|
|
|
(*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
|
2010-03-23 14:40:20 +00:00
|
|
|
// for reuse.
|
2008-07-29 10:00:13 +00:00
|
|
|
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;
|
2013-11-15 20:41:15 +00:00
|
|
|
LoadInst *OrigLoad = OriginalLoads[std::make_pair(I, *SI)];
|
2004-05-23 21:21:17 +00:00
|
|
|
if (!SI->empty()) {
|
2008-07-29 10:00:13 +00:00
|
|
|
Ops.reserve(SI->size());
|
2011-07-18 04:54:35 +00:00
|
|
|
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.
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *IdxTy = (ElTy->isStructTy() ?
|
2009-08-13 21:58:54 +00:00
|
|
|
Type::getInt32Ty(F->getContext()) :
|
|
|
|
Type::getInt64Ty(F->getContext()));
|
2009-07-24 23:12:02 +00:00
|
|
|
Ops.push_back(ConstantInt::get(IdxTy, *II));
|
2010-03-23 14:40:20 +00:00
|
|
|
// Keep track of the type we're currently indexing.
|
2008-07-29 10:00:13 +00:00
|
|
|
ElTy = cast<CompositeType>(ElTy)->getTypeAtIndex(*II);
|
|
|
|
}
|
2010-03-23 14:40:20 +00:00
|
|
|
// And create a GEP to extract those indices.
|
2011-07-25 09:48:08 +00:00
|
|
|
V = GetElementPtrInst::Create(V, Ops, 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);
|
|
|
|
}
|
2010-03-27 01:54:00 +00:00
|
|
|
// Since we're replacing a load make sure we take the alignment
|
|
|
|
// of the previous load.
|
|
|
|
LoadInst *newLoad = new LoadInst(V, V->getName()+".val", Call);
|
|
|
|
newLoad->setAlignment(OrigLoad->getAlignment());
|
2010-11-11 18:09:32 +00:00
|
|
|
// Transfer the TBAA info too.
|
|
|
|
newLoad->setMetadata(LLVMContext::MD_tbaa,
|
|
|
|
OrigLoad->getMetadata(LLVMContext::MD_tbaa));
|
2010-03-27 01:54:00 +00:00
|
|
|
Args.push_back(newLoad);
|
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
|
|
|
}
|
|
|
|
|
2010-03-23 14:40:20 +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);
|
2013-01-23 06:14:59 +00:00
|
|
|
if (CallPAL.hasAttributes(ArgIndex)) {
|
2013-01-27 01:57:28 +00:00
|
|
|
AttrBuilder B(CallPAL, ArgIndex);
|
2013-01-23 06:14:59 +00:00
|
|
|
AttributesVec.
|
2013-01-27 01:57:28 +00:00
|
|
|
push_back(AttributeSet::get(F->getContext(), Args.size(), B));
|
2013-01-23 06:14:59 +00:00
|
|
|
}
|
2008-01-17 01:17:03 +00:00
|
|
|
}
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2008-09-26 22:53:05 +00:00
|
|
|
// Add any function attributes.
|
2013-01-18 21:11:39 +00:00
|
|
|
if (CallPAL.hasAttributes(AttributeSet::FunctionIndex))
|
2013-01-27 01:57:28 +00:00
|
|
|
AttributesVec.push_back(AttributeSet::get(Call->getContext(),
|
|
|
|
CallPAL.getFnAttributes()));
|
2008-09-26 22:53:05 +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(),
|
2011-07-15 08:37:34 +00:00
|
|
|
Args, "", Call);
|
2005-05-09 01:05:50 +00:00
|
|
|
cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
|
2012-12-07 23:16:57 +00:00
|
|
|
cast<InvokeInst>(New)->setAttributes(AttributeSet::get(II->getContext(),
|
2012-11-20 05:09:20 +00:00
|
|
|
AttributesVec));
|
2004-03-07 21:29:54 +00:00
|
|
|
} else {
|
2011-07-15 08:37:34 +00:00
|
|
|
New = CallInst::Create(NF, Args, "", Call);
|
2005-05-09 01:05:50 +00:00
|
|
|
cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
|
2012-12-07 23:16:57 +00:00
|
|
|
cast<CallInst>(New)->setAttributes(AttributeSet::get(New->getContext(),
|
2012-11-20 05:09:20 +00:00
|
|
|
AttributesVec));
|
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-09-25 21:00:45 +00:00
|
|
|
AttributesVec.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.
|
2009-09-01 18:52:39 +00:00
|
|
|
CallGraphNode *CalleeNode = CG[Call->getParent()->getParent()];
|
2009-09-15 05:40:35 +00:00
|
|
|
CalleeNode->replaceCallEdge(Call, New, NF_CGN);
|
2008-09-08 11:07:35 +00:00
|
|
|
|
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());
|
|
|
|
|
2011-04-15 05:18:47 +00:00
|
|
|
// Loop over the argument list, transferring uses of the old arguments over to
|
|
|
|
// the new arguments, also transferring over the names as well.
|
2004-03-07 21:29:54 +00:00
|
|
|
//
|
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.
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *AgTy = cast<PointerType>(I->getType())->getElementType();
|
2014-04-25 05:29:35 +00:00
|
|
|
Value *TheAlloca = new AllocaInst(AgTy, nullptr, "", InsertPt);
|
2011-07-18 04:54:35 +00:00
|
|
|
StructType *STy = cast<StructType>(AgTy);
|
2009-08-13 21:58:54 +00:00
|
|
|
Value *Idxs[2] = {
|
2014-04-25 05:29:35 +00:00
|
|
|
ConstantInt::get(Type::getInt32Ty(F->getContext()), 0), nullptr };
|
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) {
|
2009-08-13 21:58:54 +00:00
|
|
|
Idxs[1] = ConstantInt::get(Type::getInt32Ty(F->getContext()), i);
|
2009-07-22 20:46:46 +00:00
|
|
|
Value *Idx =
|
2011-07-25 09:48:08 +00:00
|
|
|
GetElementPtrInst::Create(TheAlloca, Idxs,
|
2009-07-30 17:37:43 +00:00
|
|
|
TheAlloca->getName()+"."+Twine(i),
|
2009-07-22 20:46:46 +00:00
|
|
|
InsertPt);
|
2009-07-30 17:37:43 +00:00
|
|
|
I2->setName(I->getName()+"."+Twine(i));
|
2008-01-11 22:31:41 +00:00
|
|
|
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);
|
2014-01-23 17:19:42 +00:00
|
|
|
|
|
|
|
// If the alloca is used in a call, we must clear the tail flag since
|
|
|
|
// the callee now uses an alloca from the caller.
|
2014-03-09 03:16:01 +00:00
|
|
|
for (User *U : TheAlloca->users()) {
|
|
|
|
CallInst *Call = dyn_cast<CallInst>(U);
|
2014-01-23 17:19:42 +00:00
|
|
|
if (!Call)
|
|
|
|
continue;
|
|
|
|
Call->setTailCall(false);
|
|
|
|
}
|
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 (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()) {
|
2014-03-09 03:16:01 +00:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I->user_back())) {
|
2008-01-11 22:31:41 +00:00
|
|
|
assert(ArgIndices.begin()->empty() &&
|
|
|
|
"Load element should sort to front!");
|
|
|
|
I2->setName(I->getName()+".val");
|
|
|
|
LI->replaceAllUsesWith(I2);
|
|
|
|
AA.replaceWithNewValue(LI, I2);
|
|
|
|
LI->eraseFromParent();
|
2010-01-05 01:28:37 +00:00
|
|
|
DEBUG(dbgs() << "*** Promoted load of argument '" << I->getName()
|
2009-07-25 00:23:56 +00:00
|
|
|
<< "' in function '" << F->getName() << "'\n");
|
2008-01-11 22:31:41 +00:00
|
|
|
} else {
|
2014-03-09 03:16:01 +00:00
|
|
|
GetElementPtrInst *GEP = cast<GetElementPtrInst>(I->user_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
|
|
|
|
2010-01-05 01:28:37 +00:00
|
|
|
DEBUG(dbgs() << "*** Promoted agg argument '" << TheArg->getName()
|
2009-07-25 00:23:56 +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()) {
|
2014-03-09 03:16:01 +00:00
|
|
|
LoadInst *L = cast<LoadInst>(GEP->user_back());
|
2008-01-11 22:31:41 +00:00
|
|
|
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.
|
2012-09-30 17:31:56 +00:00
|
|
|
std::advance(I2, ArgIndices.size());
|
2008-01-11 22:31:41 +00:00
|
|
|
}
|
|
|
|
|
2004-05-23 21:21:17 +00:00
|
|
|
// Tell the alias analysis that the old function is about to disappear.
|
|
|
|
AA.replaceWithNewValue(F, NF);
|
|
|
|
|
2009-08-31 00:19:58 +00:00
|
|
|
|
|
|
|
NF_CGN->stealCalledFunctionsFrom(CG[F]);
|
|
|
|
|
2010-04-20 00:46:50 +00:00
|
|
|
// Now that the old function is dead, delete it. If there is a dangling
|
|
|
|
// reference to the CallgraphNode, just leave the dead function around for
|
|
|
|
// someone else to nuke.
|
|
|
|
CallGraphNode *CGN = CG[F];
|
|
|
|
if (CGN->getNumReferences() == 0)
|
|
|
|
delete CG.removeFunctionFromModule(CGN);
|
|
|
|
else
|
|
|
|
F->setLinkage(Function::ExternalLinkage);
|
2009-08-31 00:19:58 +00:00
|
|
|
|
|
|
|
return NF_CGN;
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|