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
|
2007-10-26 03:03:51 +00:00
|
|
|
// it refuses to scalarize aggregates which would require passing in more than
|
|
|
|
// three operands to the function, because passing thousands of operands for a
|
|
|
|
// large array or structure is unprofitable!
|
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"
|
2007-11-27 13:23:08 +00:00
|
|
|
#include "llvm/ParameterAttributes.h"
|
2004-03-07 21:29:54 +00:00
|
|
|
#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
|
2007-05-01 21:15:47 +00:00
|
|
|
ArgPromotion() : CallGraphSCCPass((intptr_t)&ID) {}
|
|
|
|
|
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-01-11 18:43:58 +00:00
|
|
|
Function *DoPromotion(Function *F,
|
2008-01-11 22:31:41 +00:00
|
|
|
SmallPtrSet<Argument*, 8> &ArgsToPromote,
|
|
|
|
SmallPtrSet<Argument*, 8> &ByValArgsToTransform);
|
2004-03-07 21:29:54 +00:00
|
|
|
};
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char ArgPromotion::ID = 0;
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<ArgPromotion> X("argpromotion",
|
|
|
|
"Promote 'by reference' arguments to scalars");
|
2004-03-07 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
2007-01-26 00:47:38 +00:00
|
|
|
Pass *llvm::createArgumentPromotionPass() {
|
2004-03-07 21:29:54 +00:00
|
|
|
return new ArgPromotion();
|
|
|
|
}
|
|
|
|
|
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-01-11 18:55:10 +00:00
|
|
|
if (UI.getOperandNo() != 0)
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
if (const StructType *STy = dyn_cast<StructType>(AgTy))
|
|
|
|
if (STy->getNumElements() <= 3) {
|
|
|
|
// If all the elements are first class types, we can promote it.
|
|
|
|
bool AllSimple = true;
|
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
|
|
|
|
if (!STy->getElementType(i)->isFirstClassType()) {
|
|
|
|
AllSimple = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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 {
|
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-01-11 19:34:32 +00:00
|
|
|
|
|
|
|
// We can also only promote the load if we can guarantee that it will happen.
|
|
|
|
// Promoting a load causes the load to be unconditionally executed in the
|
|
|
|
// caller, so we can't turn a conditional load into an unconditional load in
|
|
|
|
// general.
|
|
|
|
bool SafeToUnconditionallyLoad = false;
|
|
|
|
if (isByVal) // ByVal arguments are always safe to load from.
|
|
|
|
SafeToUnconditionallyLoad = true;
|
|
|
|
|
2004-11-13 23:31:34 +00:00
|
|
|
BasicBlock *EntryBlock = Arg->getParent()->begin();
|
2008-01-11 18:43:58 +00:00
|
|
|
SmallVector<LoadInst*, 16> Loads;
|
|
|
|
std::vector<SmallVector<ConstantInt*, 8> > GEPIndices;
|
2004-03-07 21:29:54 +00:00
|
|
|
for (Value::use_iterator UI = Arg->use_begin(), E = Arg->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);
|
2008-01-11 19:34:32 +00:00
|
|
|
|
|
|
|
// If this load occurs in the entry block, then the pointer is
|
|
|
|
// unconditionally loaded.
|
|
|
|
SafeToUnconditionallyLoad |= LI->getParent() == EntryBlock;
|
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();
|
|
|
|
return isSafeToPromoteArgument(Arg, isByVal);
|
2004-03-08 01:04:36 +00:00
|
|
|
}
|
|
|
|
// Ensure that all of the indices are constants.
|
2008-01-11 18:43:58 +00:00
|
|
|
SmallVector<ConstantInt*, 8> Operands;
|
2004-03-08 01:04:36 +00:00
|
|
|
for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
|
2004-06-21 00:07:58 +00:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(GEP->getOperand(i)))
|
2004-03-08 01:04:36 +00:00
|
|
|
Operands.push_back(C);
|
|
|
|
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);
|
2008-01-11 19:34:32 +00:00
|
|
|
|
|
|
|
// If this load occurs in the entry block, then the pointer is
|
|
|
|
// unconditionally loaded.
|
|
|
|
SafeToUnconditionallyLoad |= LI->getParent() == EntryBlock;
|
2004-03-08 01:04:36 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2004-05-23 21:21:17 +00:00
|
|
|
// See if there is already a GEP with these indices. If not, check to
|
|
|
|
// make sure that we aren't promoting too many elements. If so, nothing
|
|
|
|
// to do.
|
2004-03-08 01:04:36 +00:00
|
|
|
if (std::find(GEPIndices.begin(), GEPIndices.end(), Operands) ==
|
|
|
|
GEPIndices.end()) {
|
|
|
|
if (GEPIndices.size() == 3) {
|
2006-11-26 10:02:32 +00:00
|
|
|
DOUT << "argpromotion disable promoting argument '"
|
|
|
|
<< Arg->getName() << "' because it would require adding more "
|
|
|
|
<< "than 3 arguments to the function.\n";
|
2004-03-08 01:04:36 +00:00
|
|
|
// We limit aggregate promotion to only promoting up to three elements
|
|
|
|
// of the aggregate.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
GEPIndices.push_back(Operands);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false; // Not a load or a GEP.
|
|
|
|
}
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2004-05-23 21:21:17 +00:00
|
|
|
if (Loads.empty()) return true; // No users, this is a dead argument.
|
2004-03-07 21:29:54 +00:00
|
|
|
|
2004-11-13 23:31:34 +00:00
|
|
|
// If we decide that we want to promote this argument, the value is going to
|
|
|
|
// be unconditionally loaded in all callees. This is only safe to do if the
|
|
|
|
// pointer was going to be unconditionally loaded anyway (i.e. there is a load
|
|
|
|
// of the pointer in the entry block of the function) or if we can prove that
|
|
|
|
// all pointers passed in are always to legal locations (for example, no null
|
|
|
|
// pointers are passed in, no pointers to free'd memory, etc).
|
2008-01-11 19:34:32 +00:00
|
|
|
if (!SafeToUnconditionallyLoad &&
|
|
|
|
!AllCalleesPassInValidPointerForArgument(Arg))
|
2004-11-13 23:31:34 +00:00
|
|
|
return false; // Cannot prove that this is safe!!
|
|
|
|
|
|
|
|
// Okay, now we know that the argument is only used by load instructions and
|
|
|
|
// it is safe to unconditionally load the pointer. Use alias analysis to
|
|
|
|
// 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 =
|
|
|
|
cast<PointerType>(Load->getOperand(0)->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-06-21 00:07:58 +00:00
|
|
|
namespace {
|
|
|
|
/// GEPIdxComparator - Provide a strong ordering for GEP indices. All Value*
|
|
|
|
/// elements are instances of ConstantInt.
|
|
|
|
///
|
|
|
|
struct GEPIdxComparator {
|
|
|
|
bool operator()(const std::vector<Value*> &LHS,
|
|
|
|
const std::vector<Value*> &RHS) const {
|
|
|
|
unsigned idx = 0;
|
|
|
|
for (; idx < LHS.size() && idx < RHS.size(); ++idx) {
|
|
|
|
if (LHS[idx] != RHS[idx]) {
|
2006-10-20 07:07:24 +00:00
|
|
|
return cast<ConstantInt>(LHS[idx])->getZExtValue() <
|
|
|
|
cast<ConstantInt>(RHS[idx])->getZExtValue();
|
2004-06-21 00:07:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return less than if we ran out of stuff in LHS and we didn't run out of
|
|
|
|
// stuff in RHS.
|
|
|
|
return idx == LHS.size() && idx != RHS.size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2004-06-21 00:07:58 +00:00
|
|
|
typedef std::set<std::vector<Value*>, GEPIdxComparator> ScalarizeTable;
|
|
|
|
|
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.
|
|
|
|
std::map<std::vector<Value*>, LoadInst*> OriginalLoads;
|
|
|
|
|
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
|
|
|
|
ParamAttrsVector ParamAttrsVec;
|
|
|
|
const ParamAttrsList *PAL = F->getParamAttrs();
|
|
|
|
|
2008-02-01 20:37:16 +00:00
|
|
|
// Add any return attributes.
|
2008-02-19 21:38:47 +00:00
|
|
|
if (ParameterAttributes attrs = PAL ? PAL->getParamAttrs(0) : ParamAttr::None)
|
2008-02-01 20:37:16 +00:00
|
|
|
ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
|
|
|
|
|
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)) {
|
|
|
|
// Just add all the struct element types.
|
|
|
|
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)) {
|
2004-03-07 21:29:54 +00:00
|
|
|
Params.push_back(I->getType());
|
2008-02-19 21:38:47 +00:00
|
|
|
if (ParameterAttributes attrs = PAL ? PAL->getParamAttrs(ArgIndex) :
|
|
|
|
ParamAttr::None)
|
2008-01-11 22:31:41 +00:00
|
|
|
ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Params.size(), attrs));
|
2004-05-23 21:21:17 +00:00
|
|
|
} else if (I->use_empty()) {
|
|
|
|
++NumArgumentsDead;
|
|
|
|
} else {
|
2004-03-08 01:04:36 +00:00
|
|
|
// Okay, this is being promoted. Check to see if there are any GEP uses
|
|
|
|
// of the argument.
|
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));
|
|
|
|
std::vector<Value*> Indices(User->op_begin()+1, User->op_end());
|
|
|
|
ArgIndices.insert(Indices);
|
|
|
|
LoadInst *OrigLoad;
|
|
|
|
if (LoadInst *L = dyn_cast<LoadInst>(User))
|
|
|
|
OrigLoad = L;
|
|
|
|
else
|
|
|
|
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(),
|
2004-03-08 01:04:36 +00:00
|
|
|
E = ArgIndices.end(); SI != E; ++SI)
|
2007-02-12 22:56:41 +00:00
|
|
|
Params.push_back(GetElementPtrInst::getIndexedType(I->getType(),
|
2007-09-04 15:46:09 +00:00
|
|
|
SI->begin(),
|
|
|
|
SI->end()));
|
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...
|
2004-03-07 21:29:54 +00:00
|
|
|
Function *NF = new Function(NFTy, F->getLinkage(), F->getName());
|
2005-05-09 01:05:50 +00:00
|
|
|
NF->setCallingConv(F->getCallingConv());
|
2008-01-17 01:17:03 +00:00
|
|
|
|
|
|
|
// Recompute the parameter attributes list based on the new arguments for
|
|
|
|
// the function.
|
|
|
|
NF->setParamAttrs(ParamAttrsList::get(ParamAttrsVec));
|
|
|
|
ParamAttrsVec.clear(); PAL = 0;
|
|
|
|
|
2007-12-25 22:16:06 +00:00
|
|
|
if (F->hasCollector())
|
|
|
|
NF->setCollector(F->getCollector());
|
2004-03-07 21:29:54 +00:00
|
|
|
F->getParent()->getFunctionList().insert(F, NF);
|
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>();
|
|
|
|
|
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-01-17 01:17:03 +00:00
|
|
|
PAL = CS.getParamAttrs();
|
|
|
|
|
2008-02-01 20:37:16 +00:00
|
|
|
// Add any return attributes.
|
2008-02-19 21:38:47 +00:00
|
|
|
if (ParameterAttributes attrs = PAL ? PAL->getParamAttrs(0) :
|
|
|
|
ParamAttr::None)
|
2008-02-01 20:37:16 +00:00
|
|
|
ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
|
|
|
|
|
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-01-17 01:17:03 +00:00
|
|
|
|
2008-02-19 21:38:47 +00:00
|
|
|
if (ParameterAttributes Attrs = PAL ? PAL->getParamAttrs(ArgIndex) :
|
|
|
|
ParamAttr::None)
|
2008-01-17 01:17:03 +00:00
|
|
|
ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
|
|
|
|
|
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);
|
|
|
|
Value *Idx = new GetElementPtrInst(*AI, Idxs, Idxs+2,
|
|
|
|
(*AI)->getName()+"."+utostr(i),
|
|
|
|
Call);
|
|
|
|
// TODO: Tell AA about the new values?
|
|
|
|
Args.push_back(new LoadInst(Idx, Idx->getName()+".val", Call));
|
|
|
|
}
|
|
|
|
} 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];
|
|
|
|
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()) {
|
2007-09-04 15:46:09 +00:00
|
|
|
V = new GetElementPtrInst(V, SI->begin(), SI->end(),
|
2007-02-12 22:56:41 +00:00
|
|
|
V->getName()+".idx", Call);
|
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-02-19 21:38:47 +00:00
|
|
|
if (ParameterAttributes Attrs = PAL ? PAL->getParamAttrs(ArgIndex) :
|
|
|
|
ParamAttr::None)
|
2008-01-17 01:17:03 +00:00
|
|
|
ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
|
|
|
|
}
|
2004-03-07 21:29:54 +00:00
|
|
|
|
|
|
|
Instruction *New;
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
|
|
|
|
New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
|
2007-08-27 19:04:21 +00:00
|
|
|
Args.begin(), Args.end(), "", Call);
|
2005-05-09 01:05:50 +00:00
|
|
|
cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
|
2008-01-17 01:17:03 +00:00
|
|
|
cast<InvokeInst>(New)->setParamAttrs(ParamAttrsList::get(ParamAttrsVec));
|
2004-03-07 21:29:54 +00:00
|
|
|
} else {
|
2007-08-01 03:43:44 +00:00
|
|
|
New = new CallInst(NF, Args.begin(), Args.end(), "", Call);
|
2005-05-09 01:05:50 +00:00
|
|
|
cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
|
2008-01-17 01:17:03 +00:00
|
|
|
cast<CallInst>(New)->setParamAttrs(ParamAttrsList::get(ParamAttrsVec));
|
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);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// 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 };
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
|
|
|
|
Idxs[1] = ConstantInt::get(Type::Int32Ty, i);
|
|
|
|
Value *Idx = new GetElementPtrInst(TheAlloca, Idxs, Idxs+2,
|
|
|
|
TheAlloca->getName()+"."+utostr(i),
|
|
|
|
InsertPt);
|
|
|
|
I2->setName(I->getName()+"."+utostr(i));
|
|
|
|
new StoreInst(I2++, Idx, InsertPt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anything that used the arg should now use the alloca.
|
|
|
|
I->replaceAllUsesWith(TheAlloca);
|
|
|
|
TheAlloca->takeName(I);
|
|
|
|
AA.replaceWithNewValue(I, TheAlloca);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I->use_empty()) {
|
2004-05-23 21:21:17 +00:00
|
|
|
AA.deleteValue(I);
|
2008-01-11 22:31:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if we promoted this argument, then all users are load
|
|
|
|
// instructions, and all loads should be using the new argument that we
|
|
|
|
// added.
|
|
|
|
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());
|
|
|
|
std::vector<Value*> Operands(GEP->op_begin()+1, GEP->op_end());
|
|
|
|
|
|
|
|
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();
|
|
|
|
for (unsigned i = 0, e = Operands.size(); i != e; ++i)
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(Operands[i]))
|
|
|
|
NewName += "." + CI->getValue().toStringUnsigned(10);
|
|
|
|
else
|
|
|
|
NewName += ".x";
|
|
|
|
TheArg->setName(NewName+".val");
|
|
|
|
|
|
|
|
DOUT << "*** Promoted agg argument '" << TheArg->getName()
|
|
|
|
<< "' of function '" << F->getName() << "'\n";
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|