2004-10-07 04:16:33 +00:00
|
|
|
//===- GlobalOpt.cpp - Optimize Global Variables --------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-02-25 21:34:36 +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-02-25 21:34:36 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-10-07 04:16:33 +00:00
|
|
|
// This pass transforms simple global variables that never have their address
|
|
|
|
// taken. If obviously true, it marks read/write globals as constant, deletes
|
|
|
|
// variables only stored to, etc.
|
2004-02-25 21:34:36 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-10-07 04:16:33 +00:00
|
|
|
#define DEBUG_TYPE "globalopt"
|
2004-02-25 21:34:36 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
|
|
|
#include "llvm/Analysis/MemoryBuiltins.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/CallingConv.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Operator.h"
|
2004-02-25 21:34:36 +00:00
|
|
|
#include "llvm/Pass.h"
|
2008-02-18 17:32:13 +00:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2007-01-30 23:46:24 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2008-01-14 02:09:12 +00:00
|
|
|
#include "llvm/Support/GetElementPtrTypeIterator.h"
|
2008-04-26 07:40:11 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2009-07-25 00:23:56 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetLibraryInfo.h"
|
2004-10-06 20:57:02 +00:00
|
|
|
#include <algorithm>
|
2004-02-25 21:34:36 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
STATISTIC(NumMarked , "Number of globals marked constant");
|
2011-01-19 16:32:21 +00:00
|
|
|
STATISTIC(NumUnnamed , "Number of globals marked unnamed_addr");
|
2006-12-19 22:09:18 +00:00
|
|
|
STATISTIC(NumSRA , "Number of aggregate globals broken into scalars");
|
|
|
|
STATISTIC(NumHeapSRA , "Number of heap objects SRA'd");
|
|
|
|
STATISTIC(NumSubstitute,"Number of globals with initializers stored into them");
|
|
|
|
STATISTIC(NumDeleted , "Number of globals deleted");
|
|
|
|
STATISTIC(NumFnDeleted , "Number of functions deleted");
|
|
|
|
STATISTIC(NumGlobUses , "Number of global uses devirtualized");
|
|
|
|
STATISTIC(NumLocalized , "Number of globals localized");
|
|
|
|
STATISTIC(NumShrunkToBool , "Number of global vars shrunk to booleans");
|
|
|
|
STATISTIC(NumFastCallFns , "Number of functions converted to fastcc");
|
|
|
|
STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated");
|
2008-02-16 20:56:04 +00:00
|
|
|
STATISTIC(NumNestRemoved , "Number of nest attributes removed");
|
2009-02-15 09:56:08 +00:00
|
|
|
STATISTIC(NumAliasesResolved, "Number of global aliases resolved");
|
|
|
|
STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated");
|
2011-03-20 17:59:11 +00:00
|
|
|
STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed");
|
2004-02-25 21:34:36 +00:00
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
namespace {
|
2011-01-19 16:32:21 +00:00
|
|
|
struct GlobalStatus;
|
2009-10-25 06:33:48 +00:00
|
|
|
struct GlobalOpt : public ModulePass {
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2011-12-01 21:29:16 +00:00
|
|
|
AU.addRequired<TargetLibraryInfo>();
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
}
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
GlobalOpt() : ModulePass(ID) {
|
|
|
|
initializeGlobalOptPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
bool runOnModule(Module &M);
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
|
|
|
|
private:
|
2005-09-26 01:43:45 +00:00
|
|
|
GlobalVariable *FindGlobalCtors(Module &M);
|
|
|
|
bool OptimizeFunctions(Module &M);
|
|
|
|
bool OptimizeGlobalVars(Module &M);
|
2009-03-06 10:21:56 +00:00
|
|
|
bool OptimizeGlobalAliases(Module &M);
|
2005-09-26 01:43:45 +00:00
|
|
|
bool OptimizeGlobalCtorsList(GlobalVariable *&GCL);
|
2011-01-19 16:32:21 +00:00
|
|
|
bool ProcessGlobal(GlobalVariable *GV,Module::global_iterator &GVI);
|
|
|
|
bool ProcessInternalGlobal(GlobalVariable *GV,Module::global_iterator &GVI,
|
|
|
|
const SmallPtrSet<const PHINode*, 16> &PHIUsers,
|
|
|
|
const GlobalStatus &GS);
|
2011-03-20 17:59:11 +00:00
|
|
|
bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn);
|
2012-02-12 01:13:18 +00:00
|
|
|
|
2012-10-08 16:38:25 +00:00
|
|
|
DataLayout *TD;
|
2012-02-12 01:13:18 +00:00
|
|
|
TargetLibraryInfo *TLI;
|
2004-02-25 21:34:36 +00:00
|
|
|
};
|
2004-07-20 03:58:07 +00:00
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char GlobalOpt::ID = 0;
|
2011-12-01 21:29:16 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(GlobalOpt, "globalopt",
|
|
|
|
"Global Variable Optimizer", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
|
|
|
|
INITIALIZE_PASS_END(GlobalOpt, "globalopt",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Global Variable Optimizer", false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2004-10-07 04:16:33 +00:00
|
|
|
ModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); }
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
|
|
|
|
2004-10-07 04:16:33 +00:00
|
|
|
/// GlobalStatus - As we analyze each global, keep track of some information
|
|
|
|
/// about it. If we find out that the address of the global is taken, none of
|
2004-10-07 21:30:30 +00:00
|
|
|
/// this info will be accurate.
|
2009-10-25 06:33:48 +00:00
|
|
|
struct GlobalStatus {
|
2011-01-19 16:32:21 +00:00
|
|
|
/// isCompared - True if the global's address is used in a comparison.
|
|
|
|
bool isCompared;
|
|
|
|
|
2004-10-07 21:30:30 +00:00
|
|
|
/// isLoaded - True if the global is ever loaded. If the global isn't ever
|
|
|
|
/// loaded it can be deleted.
|
2004-10-07 04:16:33 +00:00
|
|
|
bool isLoaded;
|
2004-10-07 21:30:30 +00:00
|
|
|
|
|
|
|
/// StoredType - Keep track of what stores to the global look like.
|
|
|
|
///
|
2004-10-07 04:16:33 +00:00
|
|
|
enum StoredType {
|
2004-10-07 21:30:30 +00:00
|
|
|
/// NotStored - There is no store to this global. It can thus be marked
|
|
|
|
/// constant.
|
|
|
|
NotStored,
|
|
|
|
|
|
|
|
/// isInitializerStored - This global is stored to, but the only thing
|
|
|
|
/// stored is the constant it was initialized with. This is only tracked
|
|
|
|
/// for scalar globals.
|
|
|
|
isInitializerStored,
|
|
|
|
|
|
|
|
/// isStoredOnce - This global is stored to, but only its initializer and
|
|
|
|
/// one other value is ever stored to it. If this global isStoredOnce, we
|
|
|
|
/// track the value stored to it in StoredOnceValue below. This is only
|
|
|
|
/// tracked for scalar globals.
|
|
|
|
isStoredOnce,
|
|
|
|
|
|
|
|
/// isStored - This global is stored to by multiple values or something else
|
|
|
|
/// that we cannot track.
|
|
|
|
isStored
|
2004-10-07 04:16:33 +00:00
|
|
|
} StoredType;
|
2004-10-07 21:30:30 +00:00
|
|
|
|
|
|
|
/// StoredOnceValue - If only one value (besides the initializer constant) is
|
|
|
|
/// ever stored to this global, keep track of what value it is.
|
|
|
|
Value *StoredOnceValue;
|
|
|
|
|
2006-11-01 18:03:33 +00:00
|
|
|
/// AccessingFunction/HasMultipleAccessingFunctions - These start out
|
|
|
|
/// null/false. When the first accessing function is noticed, it is recorded.
|
|
|
|
/// When a second different accessing function is noticed,
|
|
|
|
/// HasMultipleAccessingFunctions is set to true.
|
2010-04-01 08:21:08 +00:00
|
|
|
const Function *AccessingFunction;
|
2005-02-10 18:36:30 +00:00
|
|
|
bool HasMultipleAccessingFunctions;
|
|
|
|
|
2006-11-01 18:03:33 +00:00
|
|
|
/// HasNonInstructionUser - Set to true if this global has a user that is not
|
|
|
|
/// an instruction (e.g. a constant expr or GV initializer).
|
2005-06-15 21:11:48 +00:00
|
|
|
bool HasNonInstructionUser;
|
|
|
|
|
2012-02-05 19:56:38 +00:00
|
|
|
/// AtomicOrdering - Set to the strongest atomic ordering requirement.
|
|
|
|
AtomicOrdering Ordering;
|
|
|
|
|
2011-01-19 16:32:21 +00:00
|
|
|
GlobalStatus() : isCompared(false), isLoaded(false), StoredType(NotStored),
|
|
|
|
StoredOnceValue(0), AccessingFunction(0),
|
2012-02-05 19:56:38 +00:00
|
|
|
HasMultipleAccessingFunctions(false),
|
2012-12-06 22:08:59 +00:00
|
|
|
HasNonInstructionUser(false), Ordering(NotAtomic) {}
|
2004-10-07 04:16:33 +00:00
|
|
|
};
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
}
|
2004-10-08 20:59:28 +00:00
|
|
|
|
2012-02-05 19:56:38 +00:00
|
|
|
/// StrongerOrdering - Return the stronger of the two ordering. If the two
|
|
|
|
/// orderings are acquire and release, then return AcquireRelease.
|
|
|
|
///
|
|
|
|
static AtomicOrdering StrongerOrdering(AtomicOrdering X, AtomicOrdering Y) {
|
|
|
|
if (X == Acquire && Y == Release) return AcquireRelease;
|
|
|
|
if (Y == Acquire && X == Release) return AcquireRelease;
|
|
|
|
return (AtomicOrdering)std::max(X, Y);
|
|
|
|
}
|
|
|
|
|
2012-09-27 10:14:43 +00:00
|
|
|
/// SafeToDestroyConstant - It is safe to destroy a constant iff it is only used
|
2012-02-05 19:48:37 +00:00
|
|
|
/// by constants itself. Note that constants cannot be cyclic, so this test is
|
|
|
|
/// pretty easy to implement recursively.
|
|
|
|
///
|
2010-04-01 08:21:08 +00:00
|
|
|
static bool SafeToDestroyConstant(const Constant *C) {
|
2004-10-08 20:59:28 +00:00
|
|
|
if (isa<GlobalValue>(C)) return false;
|
|
|
|
|
2010-04-07 18:59:26 +00:00
|
|
|
for (Value::const_use_iterator UI = C->use_begin(), E = C->use_end(); UI != E;
|
|
|
|
++UI)
|
2010-04-01 08:21:08 +00:00
|
|
|
if (const Constant *CU = dyn_cast<Constant>(*UI)) {
|
2009-06-09 21:37:11 +00:00
|
|
|
if (!SafeToDestroyConstant(CU)) return false;
|
2004-10-08 20:59:28 +00:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-07 04:16:33 +00:00
|
|
|
/// AnalyzeGlobal - Look at all uses of the global and fill in the GlobalStatus
|
|
|
|
/// structure. If the global has its address taken, return true to indicate we
|
|
|
|
/// can't do anything with it.
|
2004-02-25 21:34:36 +00:00
|
|
|
///
|
2010-04-01 08:21:08 +00:00
|
|
|
static bool AnalyzeGlobal(const Value *V, GlobalStatus &GS,
|
|
|
|
SmallPtrSet<const PHINode*, 16> &PHIUsers) {
|
2010-04-07 18:59:26 +00:00
|
|
|
for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;
|
2010-07-09 16:51:20 +00:00
|
|
|
++UI) {
|
|
|
|
const User *U = *UI;
|
|
|
|
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
|
2005-06-15 21:11:48 +00:00
|
|
|
GS.HasNonInstructionUser = true;
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2011-01-01 22:31:46 +00:00
|
|
|
// If the result of the constantexpr isn't pointer type, then we won't
|
|
|
|
// know to expect it in various places. Just reject early.
|
|
|
|
if (!isa<PointerType>(CE->getType())) return true;
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2004-10-07 04:16:33 +00:00
|
|
|
if (AnalyzeGlobal(CE, GS, PHIUsers)) return true;
|
2010-07-09 16:51:20 +00:00
|
|
|
} else if (const Instruction *I = dyn_cast<Instruction>(U)) {
|
2005-02-10 18:36:30 +00:00
|
|
|
if (!GS.HasMultipleAccessingFunctions) {
|
2010-04-01 08:21:08 +00:00
|
|
|
const Function *F = I->getParent()->getParent();
|
2005-02-10 18:36:30 +00:00
|
|
|
if (GS.AccessingFunction == 0)
|
|
|
|
GS.AccessingFunction = F;
|
|
|
|
else if (GS.AccessingFunction != F)
|
|
|
|
GS.HasMultipleAccessingFunctions = true;
|
|
|
|
}
|
2010-04-01 08:21:08 +00:00
|
|
|
if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
|
2004-10-07 04:16:33 +00:00
|
|
|
GS.isLoaded = true;
|
2012-02-05 19:56:38 +00:00
|
|
|
// Don't hack on volatile loads.
|
|
|
|
if (LI->isVolatile()) return true;
|
|
|
|
GS.Ordering = StrongerOrdering(GS.Ordering, LI->getOrdering());
|
2010-04-01 08:21:08 +00:00
|
|
|
} else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
|
2004-10-07 06:01:25 +00:00
|
|
|
// Don't allow a store OF the address, only stores TO the address.
|
|
|
|
if (SI->getOperand(0) == V) return true;
|
|
|
|
|
2012-02-05 19:56:38 +00:00
|
|
|
// Don't hack on volatile stores.
|
|
|
|
if (SI->isVolatile()) return true;
|
2012-11-15 11:40:00 +00:00
|
|
|
|
2012-02-05 19:56:38 +00:00
|
|
|
GS.Ordering = StrongerOrdering(GS.Ordering, SI->getOrdering());
|
2008-01-29 19:01:37 +00:00
|
|
|
|
2004-10-07 21:30:30 +00:00
|
|
|
// If this is a direct store to the global (i.e., the global is a scalar
|
|
|
|
// value, not an aggregate), keep more specific information about
|
|
|
|
// stores.
|
2008-02-20 11:26:25 +00:00
|
|
|
if (GS.StoredType != GlobalStatus::isStored) {
|
2010-04-07 18:59:26 +00:00
|
|
|
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(
|
|
|
|
SI->getOperand(1))) {
|
2004-11-14 20:50:30 +00:00
|
|
|
Value *StoredVal = SI->getOperand(0);
|
2012-11-15 11:40:00 +00:00
|
|
|
|
|
|
|
if (Constant *C = dyn_cast<Constant>(StoredVal)) {
|
|
|
|
if (C->isThreadDependent()) {
|
|
|
|
// The stored value changes between threads; don't track it.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-14 20:50:30 +00:00
|
|
|
if (StoredVal == GV->getInitializer()) {
|
|
|
|
if (GS.StoredType < GlobalStatus::isInitializerStored)
|
|
|
|
GS.StoredType = GlobalStatus::isInitializerStored;
|
|
|
|
} else if (isa<LoadInst>(StoredVal) &&
|
|
|
|
cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
|
2004-10-07 21:30:30 +00:00
|
|
|
if (GS.StoredType < GlobalStatus::isInitializerStored)
|
|
|
|
GS.StoredType = GlobalStatus::isInitializerStored;
|
|
|
|
} else if (GS.StoredType < GlobalStatus::isStoredOnce) {
|
|
|
|
GS.StoredType = GlobalStatus::isStoredOnce;
|
2004-11-14 20:50:30 +00:00
|
|
|
GS.StoredOnceValue = StoredVal;
|
2004-10-07 21:30:30 +00:00
|
|
|
} else if (GS.StoredType == GlobalStatus::isStoredOnce &&
|
2004-11-14 20:50:30 +00:00
|
|
|
GS.StoredOnceValue == StoredVal) {
|
2004-10-07 21:30:30 +00:00
|
|
|
// noop.
|
|
|
|
} else {
|
|
|
|
GS.StoredType = GlobalStatus::isStored;
|
|
|
|
}
|
|
|
|
} else {
|
2004-10-07 04:16:33 +00:00
|
|
|
GS.StoredType = GlobalStatus::isStored;
|
2004-10-07 21:30:30 +00:00
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
2012-07-02 18:55:39 +00:00
|
|
|
} else if (isa<BitCastInst>(I)) {
|
|
|
|
if (AnalyzeGlobal(I, GS, PHIUsers)) return true;
|
2005-02-27 18:58:52 +00:00
|
|
|
} else if (isa<GetElementPtrInst>(I)) {
|
2004-10-07 04:16:33 +00:00
|
|
|
if (AnalyzeGlobal(I, GS, PHIUsers)) return true;
|
2005-02-27 18:58:52 +00:00
|
|
|
} else if (isa<SelectInst>(I)) {
|
2004-10-07 04:16:33 +00:00
|
|
|
if (AnalyzeGlobal(I, GS, PHIUsers)) return true;
|
2010-04-01 08:21:08 +00:00
|
|
|
} else if (const PHINode *PN = dyn_cast<PHINode>(I)) {
|
2004-10-07 04:16:33 +00:00
|
|
|
// PHI nodes we can check just like select or GEP instructions, but we
|
|
|
|
// have to be careful about infinite recursion.
|
2008-12-16 07:34:30 +00:00
|
|
|
if (PHIUsers.insert(PN)) // Not already visited.
|
2004-10-07 04:16:33 +00:00
|
|
|
if (AnalyzeGlobal(I, GS, PHIUsers)) return true;
|
2006-12-23 06:05:41 +00:00
|
|
|
} else if (isa<CmpInst>(I)) {
|
2011-01-19 16:32:21 +00:00
|
|
|
GS.isCompared = true;
|
2011-05-29 18:41:56 +00:00
|
|
|
} else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
|
|
|
|
if (MTI->isVolatile()) return true;
|
2010-06-24 14:42:01 +00:00
|
|
|
if (MTI->getArgOperand(0) == V)
|
2010-04-16 23:37:20 +00:00
|
|
|
GS.StoredType = GlobalStatus::isStored;
|
2010-06-24 14:42:01 +00:00
|
|
|
if (MTI->getArgOperand(1) == V)
|
2005-02-27 18:58:52 +00:00
|
|
|
GS.isLoaded = true;
|
2011-05-29 18:41:56 +00:00
|
|
|
} else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
|
|
|
|
assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
|
|
|
|
if (MSI->isVolatile()) return true;
|
2005-02-27 18:58:52 +00:00
|
|
|
GS.StoredType = GlobalStatus::isStored;
|
2004-10-07 04:16:33 +00:00
|
|
|
} else {
|
|
|
|
return true; // Any other non-load instruction might take address!
|
2004-07-20 03:58:07 +00:00
|
|
|
}
|
2010-07-09 16:51:20 +00:00
|
|
|
} else if (const Constant *C = dyn_cast<Constant>(U)) {
|
2005-06-15 21:11:48 +00:00
|
|
|
GS.HasNonInstructionUser = true;
|
2004-10-08 20:59:28 +00:00
|
|
|
// We might have a dead and dangling constant hanging off of here.
|
2009-06-09 21:37:11 +00:00
|
|
|
if (!SafeToDestroyConstant(C))
|
2004-10-08 20:59:28 +00:00
|
|
|
return true;
|
2004-02-25 21:34:36 +00:00
|
|
|
} else {
|
2005-06-15 21:11:48 +00:00
|
|
|
GS.HasNonInstructionUser = true;
|
|
|
|
// Otherwise must be some other user.
|
2004-02-25 21:34:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-07-09 16:51:20 +00:00
|
|
|
}
|
2004-02-25 21:34:36 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-24 07:21:08 +00:00
|
|
|
/// isLeakCheckerRoot - Is this global variable possibly used by a leak checker
|
|
|
|
/// as a root? If so, we might not really want to eliminate the stores to it.
|
|
|
|
static bool isLeakCheckerRoot(GlobalVariable *GV) {
|
|
|
|
// A global variable is a root if it is a pointer, or could plausibly contain
|
|
|
|
// a pointer. There are two challenges; one is that we could have a struct
|
|
|
|
// the has an inner member which is a pointer. We recurse through the type to
|
|
|
|
// detect these (up to a point). The other is that we may actually be a union
|
|
|
|
// of a pointer and another type, and so our LLVM type is an integer which
|
|
|
|
// gets converted into a pointer, or our type is an [i8 x #] with a pointer
|
|
|
|
// potentially contained here.
|
|
|
|
|
|
|
|
if (GV->hasPrivateLinkage())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallVector<Type *, 4> Types;
|
|
|
|
Types.push_back(cast<PointerType>(GV->getType())->getElementType());
|
|
|
|
|
|
|
|
unsigned Limit = 20;
|
|
|
|
do {
|
|
|
|
Type *Ty = Types.pop_back_val();
|
|
|
|
switch (Ty->getTypeID()) {
|
|
|
|
default: break;
|
|
|
|
case Type::PointerTyID: return true;
|
|
|
|
case Type::ArrayTyID:
|
|
|
|
case Type::VectorTyID: {
|
|
|
|
SequentialType *STy = cast<SequentialType>(Ty);
|
|
|
|
Types.push_back(STy->getElementType());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::StructTyID: {
|
|
|
|
StructType *STy = cast<StructType>(Ty);
|
|
|
|
if (STy->isOpaque()) return true;
|
|
|
|
for (StructType::element_iterator I = STy->element_begin(),
|
|
|
|
E = STy->element_end(); I != E; ++I) {
|
|
|
|
Type *InnerTy = *I;
|
|
|
|
if (isa<PointerType>(InnerTy)) return true;
|
|
|
|
if (isa<CompositeType>(InnerTy))
|
|
|
|
Types.push_back(InnerTy);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (--Limit == 0) return true;
|
|
|
|
} while (!Types.empty());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a value that is stored to a global but never read, determine whether
|
|
|
|
/// it's safe to remove the store and the chain of computation that feeds the
|
|
|
|
/// store.
|
2012-08-29 15:32:21 +00:00
|
|
|
static bool IsSafeComputationToRemove(Value *V, const TargetLibraryInfo *TLI) {
|
2012-07-24 07:21:08 +00:00
|
|
|
do {
|
|
|
|
if (isa<Constant>(V))
|
|
|
|
return true;
|
|
|
|
if (!V->hasOneUse())
|
|
|
|
return false;
|
2012-07-25 21:19:40 +00:00
|
|
|
if (isa<LoadInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V) ||
|
|
|
|
isa<GlobalValue>(V))
|
2012-07-24 07:21:08 +00:00
|
|
|
return false;
|
2012-08-29 15:32:21 +00:00
|
|
|
if (isAllocationFn(V, TLI))
|
2012-07-24 07:21:08 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
Instruction *I = cast<Instruction>(V);
|
|
|
|
if (I->mayHaveSideEffects())
|
|
|
|
return false;
|
|
|
|
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
|
|
|
|
if (!GEP->hasAllConstantIndices())
|
|
|
|
return false;
|
|
|
|
} else if (I->getNumOperands() != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
V = I->getOperand(0);
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CleanupPointerRootUsers - This GV is a pointer root. Loop over all users
|
|
|
|
/// of the global and clean up any that obviously don't assign the global a
|
|
|
|
/// value that isn't dynamically allocated.
|
|
|
|
///
|
2012-08-29 15:32:21 +00:00
|
|
|
static bool CleanupPointerRootUsers(GlobalVariable *GV,
|
|
|
|
const TargetLibraryInfo *TLI) {
|
2012-07-24 07:21:08 +00:00
|
|
|
// A brief explanation of leak checkers. The goal is to find bugs where
|
|
|
|
// pointers are forgotten, causing an accumulating growth in memory
|
|
|
|
// usage over time. The common strategy for leak checkers is to whitelist the
|
|
|
|
// memory pointed to by globals at exit. This is popular because it also
|
|
|
|
// solves another problem where the main thread of a C++ program may shut down
|
|
|
|
// before other threads that are still expecting to use those globals. To
|
|
|
|
// handle that case, we expect the program may create a singleton and never
|
|
|
|
// destroy it.
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// If Dead[n].first is the only use of a malloc result, we can delete its
|
|
|
|
// chain of computation and the store to the global in Dead[n].second.
|
|
|
|
SmallVector<std::pair<Instruction *, Instruction *>, 32> Dead;
|
|
|
|
|
|
|
|
// Constants can't be pointers to dynamically allocated memory.
|
|
|
|
for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end();
|
|
|
|
UI != E;) {
|
|
|
|
User *U = *UI++;
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
|
|
|
|
Value *V = SI->getValueOperand();
|
|
|
|
if (isa<Constant>(V)) {
|
|
|
|
Changed = true;
|
|
|
|
SI->eraseFromParent();
|
|
|
|
} else if (Instruction *I = dyn_cast<Instruction>(V)) {
|
|
|
|
if (I->hasOneUse())
|
|
|
|
Dead.push_back(std::make_pair(I, SI));
|
|
|
|
}
|
|
|
|
} else if (MemSetInst *MSI = dyn_cast<MemSetInst>(U)) {
|
|
|
|
if (isa<Constant>(MSI->getValue())) {
|
|
|
|
Changed = true;
|
|
|
|
MSI->eraseFromParent();
|
|
|
|
} else if (Instruction *I = dyn_cast<Instruction>(MSI->getValue())) {
|
|
|
|
if (I->hasOneUse())
|
|
|
|
Dead.push_back(std::make_pair(I, MSI));
|
|
|
|
}
|
|
|
|
} else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(U)) {
|
|
|
|
GlobalVariable *MemSrc = dyn_cast<GlobalVariable>(MTI->getSource());
|
|
|
|
if (MemSrc && MemSrc->isConstant()) {
|
|
|
|
Changed = true;
|
|
|
|
MTI->eraseFromParent();
|
|
|
|
} else if (Instruction *I = dyn_cast<Instruction>(MemSrc)) {
|
|
|
|
if (I->hasOneUse())
|
|
|
|
Dead.push_back(std::make_pair(I, MTI));
|
|
|
|
}
|
|
|
|
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
|
|
|
|
if (CE->use_empty()) {
|
|
|
|
CE->destroyConstant();
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
} else if (Constant *C = dyn_cast<Constant>(U)) {
|
|
|
|
if (SafeToDestroyConstant(C)) {
|
|
|
|
C->destroyConstant();
|
|
|
|
// This could have invalidated UI, start over from scratch.
|
|
|
|
Dead.clear();
|
2012-08-29 15:32:21 +00:00
|
|
|
CleanupPointerRootUsers(GV, TLI);
|
2012-07-24 07:21:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0, e = Dead.size(); i != e; ++i) {
|
2012-08-29 15:32:21 +00:00
|
|
|
if (IsSafeComputationToRemove(Dead[i].first, TLI)) {
|
2012-07-24 07:21:08 +00:00
|
|
|
Dead[i].second->eraseFromParent();
|
|
|
|
Instruction *I = Dead[i].first;
|
|
|
|
do {
|
2013-01-11 23:08:52 +00:00
|
|
|
if (isAllocationFn(I, TLI))
|
|
|
|
break;
|
2012-07-24 07:21:08 +00:00
|
|
|
Instruction *J = dyn_cast<Instruction>(I->getOperand(0));
|
|
|
|
if (!J)
|
|
|
|
break;
|
|
|
|
I->eraseFromParent();
|
|
|
|
I = J;
|
2012-07-24 21:33:00 +00:00
|
|
|
} while (1);
|
2012-07-24 07:21:08 +00:00
|
|
|
I->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2004-10-06 20:57:02 +00:00
|
|
|
/// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all
|
|
|
|
/// users of the global, cleaning up the obvious ones. This is largely just a
|
2004-10-10 16:43:46 +00:00
|
|
|
/// quick scan over the use list to clean up the easy and obvious cruft. This
|
|
|
|
/// returns true if it made a change.
|
2012-02-12 01:13:18 +00:00
|
|
|
static bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
|
2012-10-08 16:38:25 +00:00
|
|
|
DataLayout *TD, TargetLibraryInfo *TLI) {
|
2004-10-10 16:43:46 +00:00
|
|
|
bool Changed = false;
|
2004-10-07 04:16:33 +00:00
|
|
|
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;) {
|
|
|
|
User *U = *UI++;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-10-07 04:16:33 +00:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
|
2005-02-27 18:58:52 +00:00
|
|
|
if (Init) {
|
|
|
|
// Replace the load with the initializer.
|
|
|
|
LI->replaceAllUsesWith(Init);
|
|
|
|
LI->eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
}
|
2004-10-07 04:16:33 +00:00
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
|
2004-10-06 20:57:02 +00:00
|
|
|
// Store must be unreachable or storing Init into the global.
|
2004-10-16 18:09:00 +00:00
|
|
|
SI->eraseFromParent();
|
2004-10-10 16:43:46 +00:00
|
|
|
Changed = true;
|
2004-10-07 04:16:33 +00:00
|
|
|
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
|
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr) {
|
2005-09-26 07:34:35 +00:00
|
|
|
Constant *SubInit = 0;
|
|
|
|
if (Init)
|
2009-10-05 16:36:26 +00:00
|
|
|
SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
|
2012-02-12 01:13:18 +00:00
|
|
|
Changed |= CleanupConstantGlobalUsers(CE, SubInit, TD, TLI);
|
2010-10-18 21:16:00 +00:00
|
|
|
} else if (CE->getOpcode() == Instruction::BitCast &&
|
2010-02-16 11:11:14 +00:00
|
|
|
CE->getType()->isPointerTy()) {
|
2005-02-27 18:58:52 +00:00
|
|
|
// Pointer cast, delete any stores and memsets to the global.
|
2012-02-12 01:13:18 +00:00
|
|
|
Changed |= CleanupConstantGlobalUsers(CE, 0, TD, TLI);
|
2004-10-07 04:16:33 +00:00
|
|
|
}
|
2005-02-27 18:58:52 +00:00
|
|
|
|
|
|
|
if (CE->use_empty()) {
|
|
|
|
CE->destroyConstant();
|
|
|
|
Changed = true;
|
2004-10-10 16:47:33 +00:00
|
|
|
}
|
2005-02-27 18:58:52 +00:00
|
|
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
|
2007-11-09 17:33:02 +00:00
|
|
|
// Do not transform "gepinst (gep constexpr (GV))" here, because forming
|
|
|
|
// "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold
|
|
|
|
// and will invalidate our notion of what Init is.
|
2007-11-13 21:46:23 +00:00
|
|
|
Constant *SubInit = 0;
|
2007-11-09 17:33:02 +00:00
|
|
|
if (!isa<ConstantExpr>(GEP->getOperand(0))) {
|
2010-10-18 21:16:00 +00:00
|
|
|
ConstantExpr *CE =
|
2012-02-12 01:13:18 +00:00
|
|
|
dyn_cast_or_null<ConstantExpr>(ConstantFoldInstruction(GEP, TD, TLI));
|
2007-11-09 17:33:02 +00:00
|
|
|
if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr)
|
2009-10-05 16:36:26 +00:00
|
|
|
SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
|
2012-03-28 14:50:09 +00:00
|
|
|
|
|
|
|
// If the initializer is an all-null value and we have an inbounds GEP,
|
|
|
|
// we already know what the result of any load from that GEP is.
|
|
|
|
// TODO: Handle splats.
|
|
|
|
if (Init && isa<ConstantAggregateZero>(Init) && GEP->isInBounds())
|
|
|
|
SubInit = Constant::getNullValue(GEP->getType()->getElementType());
|
2007-11-09 17:33:02 +00:00
|
|
|
}
|
2012-02-12 01:13:18 +00:00
|
|
|
Changed |= CleanupConstantGlobalUsers(GEP, SubInit, TD, TLI);
|
2004-10-10 16:47:33 +00:00
|
|
|
|
2004-10-10 16:43:46 +00:00
|
|
|
if (GEP->use_empty()) {
|
2004-10-16 18:09:00 +00:00
|
|
|
GEP->eraseFromParent();
|
2004-10-10 16:43:46 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
2005-02-27 18:58:52 +00:00
|
|
|
} else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv
|
|
|
|
if (MI->getRawDest() == V) {
|
|
|
|
MI->eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2004-10-08 20:59:28 +00:00
|
|
|
} else if (Constant *C = dyn_cast<Constant>(U)) {
|
|
|
|
// If we have a chain of dead constantexprs or other things dangling from
|
|
|
|
// us, and if they are all dead, nuke them without remorse.
|
2009-06-09 21:37:11 +00:00
|
|
|
if (SafeToDestroyConstant(C)) {
|
2009-03-06 01:37:41 +00:00
|
|
|
C->destroyConstant();
|
2005-02-27 18:58:52 +00:00
|
|
|
// This could have invalidated UI, start over from scratch.
|
2012-02-12 01:13:18 +00:00
|
|
|
CleanupConstantGlobalUsers(V, Init, TD, TLI);
|
2004-10-10 16:43:46 +00:00
|
|
|
return true;
|
2004-10-08 20:59:28 +00:00
|
|
|
}
|
2004-10-06 20:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
2004-10-10 16:43:46 +00:00
|
|
|
return Changed;
|
2004-10-06 20:57:02 +00:00
|
|
|
}
|
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
/// isSafeSROAElementUse - Return true if the specified instruction is a safe
|
|
|
|
/// user of a derived expression from a global that we want to SROA.
|
|
|
|
static bool isSafeSROAElementUse(Value *V) {
|
|
|
|
// We might have a dead and dangling constant hanging off of here.
|
|
|
|
if (Constant *C = dyn_cast<Constant>(V))
|
2009-06-09 21:37:11 +00:00
|
|
|
return SafeToDestroyConstant(C);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
|
|
|
if (!I) return false;
|
|
|
|
|
|
|
|
// Loads are ok.
|
|
|
|
if (isa<LoadInst>(I)) return true;
|
|
|
|
|
|
|
|
// Stores *to* the pointer are ok.
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(I))
|
|
|
|
return SI->getOperand(0) != V;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
// Otherwise, it must be a GEP.
|
|
|
|
GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I);
|
|
|
|
if (GEPI == 0) return false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
if (GEPI->getNumOperands() < 3 || !isa<Constant>(GEPI->getOperand(1)) ||
|
|
|
|
!cast<Constant>(GEPI->getOperand(1))->isNullValue())
|
|
|
|
return false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
for (Value::use_iterator I = GEPI->use_begin(), E = GEPI->use_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (!isSafeSROAElementUse(*I))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2008-01-14 01:31:05 +00:00
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
|
|
|
|
/// IsUserOfGlobalSafeForSRA - U is a direct user of the specified global value.
|
|
|
|
/// Look at it and its uses and decide whether it is safe to SROA this global.
|
2008-01-14 01:31:05 +00:00
|
|
|
///
|
2008-01-14 02:09:12 +00:00
|
|
|
static bool IsUserOfGlobalSafeForSRA(User *U, GlobalValue *GV) {
|
|
|
|
// The user of the global must be a GEP Inst or a ConstantExpr GEP.
|
2010-10-18 21:16:00 +00:00
|
|
|
if (!isa<GetElementPtrInst>(U) &&
|
|
|
|
(!isa<ConstantExpr>(U) ||
|
2008-01-14 02:09:12 +00:00
|
|
|
cast<ConstantExpr>(U)->getOpcode() != Instruction::GetElementPtr))
|
|
|
|
return false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
// Check to see if this ConstantExpr GEP is SRA'able. In particular, we
|
|
|
|
// don't like < 3 operand CE's, and we don't like non-constant integer
|
|
|
|
// indices. This enforces that all uses are 'gep GV, 0, C, ...' for some
|
|
|
|
// value of C.
|
|
|
|
if (U->getNumOperands() < 3 || !isa<Constant>(U->getOperand(1)) ||
|
|
|
|
!cast<Constant>(U->getOperand(1))->isNullValue() ||
|
|
|
|
!isa<ConstantInt>(U->getOperand(2)))
|
|
|
|
return false;
|
2008-01-14 01:31:05 +00:00
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
gep_type_iterator GEPI = gep_type_begin(U), E = gep_type_end(U);
|
|
|
|
++GEPI; // Skip over the pointer index.
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
// If this is a use of an array allocation, do a bit more checking for sanity.
|
2011-07-18 04:54:35 +00:00
|
|
|
if (ArrayType *AT = dyn_cast<ArrayType>(*GEPI)) {
|
2008-01-14 02:09:12 +00:00
|
|
|
uint64_t NumElements = AT->getNumElements();
|
|
|
|
ConstantInt *Idx = cast<ConstantInt>(U->getOperand(2));
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
// Check to make sure that index falls within the array. If not,
|
|
|
|
// something funny is going on, so we won't do the optimization.
|
|
|
|
//
|
|
|
|
if (Idx->getZExtValue() >= NumElements)
|
|
|
|
return false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
// We cannot scalar repl this level of the array unless any array
|
|
|
|
// sub-indices are in-range constants. In particular, consider:
|
|
|
|
// A[0][i]. We cannot know that the user isn't doing invalid things like
|
|
|
|
// allowing i to index an out-of-range subscript that accesses A[1].
|
|
|
|
//
|
|
|
|
// Scalar replacing *just* the outer index of the array is probably not
|
|
|
|
// going to be a win anyway, so just give up.
|
|
|
|
for (++GEPI; // Skip array index.
|
2009-08-18 14:58:19 +00:00
|
|
|
GEPI != E;
|
2008-01-14 02:09:12 +00:00
|
|
|
++GEPI) {
|
|
|
|
uint64_t NumElements;
|
2011-07-18 04:54:35 +00:00
|
|
|
if (ArrayType *SubArrayTy = dyn_cast<ArrayType>(*GEPI))
|
2008-01-14 02:09:12 +00:00
|
|
|
NumElements = SubArrayTy->getNumElements();
|
2011-07-18 04:54:35 +00:00
|
|
|
else if (VectorType *SubVectorTy = dyn_cast<VectorType>(*GEPI))
|
2009-08-18 14:58:19 +00:00
|
|
|
NumElements = SubVectorTy->getNumElements();
|
|
|
|
else {
|
2010-02-16 11:11:14 +00:00
|
|
|
assert((*GEPI)->isStructTy() &&
|
2009-08-18 14:58:19 +00:00
|
|
|
"Indexed GEP type is not array, vector, or struct!");
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-01-14 02:09:12 +00:00
|
|
|
ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPI.getOperand());
|
|
|
|
if (!IdxVal || IdxVal->getZExtValue() >= NumElements)
|
2008-01-14 01:31:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-01-14 02:09:12 +00:00
|
|
|
|
|
|
|
for (Value::use_iterator I = U->use_begin(), E = U->use_end(); I != E; ++I)
|
|
|
|
if (!isSafeSROAElementUse(*I))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GlobalUsersSafeToSRA - Look at all uses of the global and decide whether it
|
|
|
|
/// is safe for us to perform this transformation.
|
|
|
|
///
|
|
|
|
static bool GlobalUsersSafeToSRA(GlobalValue *GV) {
|
|
|
|
for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end();
|
|
|
|
UI != E; ++UI) {
|
|
|
|
if (!IsUserOfGlobalSafeForSRA(*UI, GV))
|
|
|
|
return false;
|
|
|
|
}
|
2008-01-14 01:31:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-01-14 01:31:05 +00:00
|
|
|
|
2004-10-08 17:32:09 +00:00
|
|
|
/// SRAGlobal - Perform scalar replacement of aggregates on the specified global
|
|
|
|
/// variable. This opens the door for other optimizations by exposing the
|
|
|
|
/// behavior of the program in a more fine-grained way. We have determined that
|
|
|
|
/// this transformation is safe already. We return the first global variable we
|
|
|
|
/// insert so that the caller can reprocess it.
|
2012-10-08 16:38:25 +00:00
|
|
|
static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &TD) {
|
2008-01-14 01:31:05 +00:00
|
|
|
// Make sure this global only has simple uses that we can SRA.
|
2008-01-14 02:09:12 +00:00
|
|
|
if (!GlobalUsersSafeToSRA(GV))
|
2008-01-14 01:31:05 +00:00
|
|
|
return 0;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-01-15 20:18:42 +00:00
|
|
|
assert(GV->hasLocalLinkage() && !GV->isConstant());
|
2004-10-08 17:32:09 +00:00
|
|
|
Constant *Init = GV->getInitializer();
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Ty = Init->getType();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-10-08 17:32:09 +00:00
|
|
|
std::vector<GlobalVariable*> NewGlobals;
|
|
|
|
Module::GlobalListType &Globals = GV->getParent()->getGlobalList();
|
|
|
|
|
2008-04-26 07:40:11 +00:00
|
|
|
// Get the alignment of the global, either explicit or target-specific.
|
|
|
|
unsigned StartAlignment = GV->getAlignment();
|
|
|
|
if (StartAlignment == 0)
|
|
|
|
StartAlignment = TD.getABITypeAlignment(GV->getType());
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(Ty)) {
|
2004-10-08 17:32:09 +00:00
|
|
|
NewGlobals.reserve(STy->getNumElements());
|
2008-04-26 07:40:11 +00:00
|
|
|
const StructLayout &Layout = *TD.getStructLayout(STy);
|
2004-10-08 17:32:09 +00:00
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
|
2012-01-25 06:48:06 +00:00
|
|
|
Constant *In = Init->getAggregateElement(i);
|
2004-10-08 17:32:09 +00:00
|
|
|
assert(In && "Couldn't get element of initializer?");
|
2009-11-06 04:27:31 +00:00
|
|
|
GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
|
2004-10-08 17:32:09 +00:00
|
|
|
GlobalVariable::InternalLinkage,
|
2009-07-30 17:37:43 +00:00
|
|
|
In, GV->getName()+"."+Twine(i),
|
2012-06-23 11:37:03 +00:00
|
|
|
GV->getThreadLocalMode(),
|
2009-07-08 01:26:06 +00:00
|
|
|
GV->getType()->getAddressSpace());
|
2004-10-08 17:32:09 +00:00
|
|
|
Globals.insert(GV, NGV);
|
|
|
|
NewGlobals.push_back(NGV);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-04-26 07:40:11 +00:00
|
|
|
// Calculate the known alignment of the field. If the original aggregate
|
|
|
|
// had 256 byte alignment for example, something might depend on that:
|
|
|
|
// propagate info to each field.
|
|
|
|
uint64_t FieldOffset = Layout.getElementOffset(i);
|
|
|
|
unsigned NewAlign = (unsigned)MinAlign(StartAlignment, FieldOffset);
|
|
|
|
if (NewAlign > TD.getABITypeAlignment(STy->getElementType(i)))
|
|
|
|
NGV->setAlignment(NewAlign);
|
2004-10-08 17:32:09 +00:00
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
} else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
|
2004-10-08 17:32:09 +00:00
|
|
|
unsigned NumElements = 0;
|
2011-07-18 04:54:35 +00:00
|
|
|
if (ArrayType *ATy = dyn_cast<ArrayType>(STy))
|
2004-10-08 17:32:09 +00:00
|
|
|
NumElements = ATy->getNumElements();
|
|
|
|
else
|
2008-04-26 07:40:11 +00:00
|
|
|
NumElements = cast<VectorType>(STy)->getNumElements();
|
2004-10-08 17:32:09 +00:00
|
|
|
|
2005-02-23 16:53:04 +00:00
|
|
|
if (NumElements > 16 && GV->hasNUsesOrMore(16))
|
2005-02-01 01:23:31 +00:00
|
|
|
return 0; // It's not worth it.
|
2004-10-08 17:32:09 +00:00
|
|
|
NewGlobals.reserve(NumElements);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-05-09 07:06:46 +00:00
|
|
|
uint64_t EltSize = TD.getTypeAllocSize(STy->getElementType());
|
2008-04-26 07:40:11 +00:00
|
|
|
unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType());
|
2004-10-08 17:32:09 +00:00
|
|
|
for (unsigned i = 0, e = NumElements; i != e; ++i) {
|
2012-01-25 06:48:06 +00:00
|
|
|
Constant *In = Init->getAggregateElement(i);
|
2004-10-08 17:32:09 +00:00
|
|
|
assert(In && "Couldn't get element of initializer?");
|
|
|
|
|
2009-11-06 04:27:31 +00:00
|
|
|
GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
|
2004-10-08 17:32:09 +00:00
|
|
|
GlobalVariable::InternalLinkage,
|
2009-07-30 17:37:43 +00:00
|
|
|
In, GV->getName()+"."+Twine(i),
|
2012-06-23 11:37:03 +00:00
|
|
|
GV->getThreadLocalMode(),
|
2009-07-08 19:03:57 +00:00
|
|
|
GV->getType()->getAddressSpace());
|
2004-10-08 17:32:09 +00:00
|
|
|
Globals.insert(GV, NGV);
|
|
|
|
NewGlobals.push_back(NGV);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-04-26 07:40:11 +00:00
|
|
|
// Calculate the known alignment of the field. If the original aggregate
|
|
|
|
// had 256 byte alignment for example, something might depend on that:
|
|
|
|
// propagate info to each field.
|
|
|
|
unsigned NewAlign = (unsigned)MinAlign(StartAlignment, EltSize*i);
|
|
|
|
if (NewAlign > EltAlign)
|
|
|
|
NGV->setAlignment(NewAlign);
|
2004-10-08 17:32:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NewGlobals.empty())
|
|
|
|
return 0;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-01-05 01:28:05 +00:00
|
|
|
DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV);
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
|
2009-11-06 04:27:31 +00:00
|
|
|
Constant *NullInt =Constant::getNullValue(Type::getInt32Ty(GV->getContext()));
|
2004-10-08 17:32:09 +00:00
|
|
|
|
|
|
|
// Loop over all of the uses of the global, replacing the constantexpr geps,
|
|
|
|
// with smaller constantexpr geps or direct references.
|
|
|
|
while (!GV->use_empty()) {
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
User *GEP = GV->use_back();
|
|
|
|
assert(((isa<ConstantExpr>(GEP) &&
|
|
|
|
cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)||
|
|
|
|
isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!");
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-10-08 17:32:09 +00:00
|
|
|
// Ignore the 1th operand, which has to be zero or else the program is quite
|
|
|
|
// broken (undefined). Get the 2nd operand, which is the structure or array
|
|
|
|
// index.
|
2006-10-20 07:07:24 +00:00
|
|
|
unsigned Val = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue();
|
2004-10-08 17:32:09 +00:00
|
|
|
if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access.
|
|
|
|
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
Value *NewPtr = NewGlobals[Val];
|
2004-10-08 17:32:09 +00:00
|
|
|
|
|
|
|
// Form a shorter GEP if needed.
|
2008-02-20 11:26:25 +00:00
|
|
|
if (GEP->getNumOperands() > 3) {
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) {
|
2007-01-31 04:40:53 +00:00
|
|
|
SmallVector<Constant*, 8> Idxs;
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
Idxs.push_back(NullInt);
|
|
|
|
for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i)
|
|
|
|
Idxs.push_back(CE->getOperand(i));
|
2011-07-21 14:31:17 +00:00
|
|
|
NewPtr = ConstantExpr::getGetElementPtr(cast<Constant>(NewPtr), Idxs);
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
} else {
|
|
|
|
GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP);
|
2007-01-31 19:59:55 +00:00
|
|
|
SmallVector<Value*, 8> Idxs;
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
Idxs.push_back(NullInt);
|
|
|
|
for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i)
|
|
|
|
Idxs.push_back(GEPI->getOperand(i));
|
2011-07-25 09:48:08 +00:00
|
|
|
NewPtr = GetElementPtrInst::Create(NewPtr, Idxs,
|
2009-07-30 17:37:43 +00:00
|
|
|
GEPI->getName()+"."+Twine(Val),GEPI);
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
GEP->replaceAllUsesWith(NewPtr);
|
|
|
|
|
|
|
|
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP))
|
2004-10-16 18:09:00 +00:00
|
|
|
GEPI->eraseFromParent();
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
else
|
|
|
|
cast<ConstantExpr>(GEP)->destroyConstant();
|
2004-10-08 17:32:09 +00:00
|
|
|
}
|
|
|
|
|
2004-10-08 20:25:55 +00:00
|
|
|
// Delete the old global, now that it is dead.
|
|
|
|
Globals.erase(GV);
|
2004-10-08 17:32:09 +00:00
|
|
|
++NumSRA;
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
|
|
|
|
// Loop over the new globals array deleting any globals that are obviously
|
|
|
|
// dead. This can arise due to scalarization of a structure or an array that
|
|
|
|
// has elements that are dead.
|
|
|
|
unsigned FirstGlobal = 0;
|
|
|
|
for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i)
|
|
|
|
if (NewGlobals[i]->use_empty()) {
|
|
|
|
Globals.erase(NewGlobals[i]);
|
|
|
|
if (FirstGlobal == i) ++FirstGlobal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : 0;
|
2004-10-08 17:32:09 +00:00
|
|
|
}
|
|
|
|
|
2004-10-09 21:48:45 +00:00
|
|
|
/// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified
|
2010-10-18 21:16:00 +00:00
|
|
|
/// value will trap if the value is dynamically null. PHIs keeps track of any
|
2007-09-13 16:30:19 +00:00
|
|
|
/// phi nodes we've seen to avoid reprocessing them.
|
2010-04-06 19:24:18 +00:00
|
|
|
static bool AllUsesOfValueWillTrapIfNull(const Value *V,
|
|
|
|
SmallPtrSet<const PHINode*, 8> &PHIs) {
|
|
|
|
for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;
|
2010-04-06 19:14:05 +00:00
|
|
|
++UI) {
|
2010-04-06 19:24:18 +00:00
|
|
|
const User *U = *UI;
|
2010-04-06 19:14:05 +00:00
|
|
|
|
|
|
|
if (isa<LoadInst>(U)) {
|
2004-10-09 21:48:45 +00:00
|
|
|
// Will trap.
|
2010-04-06 19:24:18 +00:00
|
|
|
} else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
|
2004-10-09 21:48:45 +00:00
|
|
|
if (SI->getOperand(0) == V) {
|
2010-04-06 19:14:05 +00:00
|
|
|
//cerr << "NONTRAPPING USE: " << *U;
|
2004-10-09 21:48:45 +00:00
|
|
|
return false; // Storing the value.
|
|
|
|
}
|
2010-04-06 19:24:18 +00:00
|
|
|
} else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
|
2010-03-20 21:00:25 +00:00
|
|
|
if (CI->getCalledValue() != V) {
|
2010-04-06 19:14:05 +00:00
|
|
|
//cerr << "NONTRAPPING USE: " << *U;
|
2004-10-09 21:48:45 +00:00
|
|
|
return false; // Not calling the ptr
|
|
|
|
}
|
2010-04-06 19:24:18 +00:00
|
|
|
} else if (const InvokeInst *II = dyn_cast<InvokeInst>(U)) {
|
2010-03-20 21:00:25 +00:00
|
|
|
if (II->getCalledValue() != V) {
|
2010-04-06 19:14:05 +00:00
|
|
|
//cerr << "NONTRAPPING USE: " << *U;
|
2004-10-09 21:48:45 +00:00
|
|
|
return false; // Not calling the ptr
|
|
|
|
}
|
2010-04-06 19:24:18 +00:00
|
|
|
} else if (const BitCastInst *CI = dyn_cast<BitCastInst>(U)) {
|
2007-09-13 16:30:19 +00:00
|
|
|
if (!AllUsesOfValueWillTrapIfNull(CI, PHIs)) return false;
|
2010-04-06 19:24:18 +00:00
|
|
|
} else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
|
2007-09-13 16:30:19 +00:00
|
|
|
if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false;
|
2010-04-06 19:24:18 +00:00
|
|
|
} else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
|
2007-09-13 16:30:19 +00:00
|
|
|
// If we've already seen this phi node, ignore it, it has already been
|
|
|
|
// checked.
|
2010-01-29 23:54:14 +00:00
|
|
|
if (PHIs.insert(PN) && !AllUsesOfValueWillTrapIfNull(PN, PHIs))
|
|
|
|
return false;
|
2010-04-06 19:14:05 +00:00
|
|
|
} else if (isa<ICmpInst>(U) &&
|
2004-10-22 06:43:28 +00:00
|
|
|
isa<ConstantPointerNull>(UI->getOperand(1))) {
|
2010-02-25 06:39:10 +00:00
|
|
|
// Ignore icmp X, null
|
2004-10-09 21:48:45 +00:00
|
|
|
} else {
|
2010-04-06 19:14:05 +00:00
|
|
|
//cerr << "NONTRAPPING USE: " << *U;
|
2004-10-09 21:48:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-06 19:14:05 +00:00
|
|
|
}
|
2004-10-09 21:48:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads
|
2004-10-22 06:43:28 +00:00
|
|
|
/// from GV will trap if the loaded value is null. Note that this also permits
|
|
|
|
/// comparisons of the loaded value against null, as a special case.
|
2010-04-06 19:24:18 +00:00
|
|
|
static bool AllUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) {
|
|
|
|
for (Value::const_use_iterator UI = GV->use_begin(), E = GV->use_end();
|
2010-04-06 19:14:05 +00:00
|
|
|
UI != E; ++UI) {
|
2010-04-06 19:24:18 +00:00
|
|
|
const User *U = *UI;
|
2010-04-06 19:14:05 +00:00
|
|
|
|
2010-04-06 19:24:18 +00:00
|
|
|
if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
|
|
|
|
SmallPtrSet<const PHINode*, 8> PHIs;
|
2007-09-13 16:30:19 +00:00
|
|
|
if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))
|
2004-10-09 21:48:45 +00:00
|
|
|
return false;
|
2010-04-06 19:14:05 +00:00
|
|
|
} else if (isa<StoreInst>(U)) {
|
2004-10-09 21:48:45 +00:00
|
|
|
// Ignore stores to the global.
|
|
|
|
} else {
|
|
|
|
// We don't know or understand this user, bail out.
|
2010-04-06 19:14:05 +00:00
|
|
|
//cerr << "UNKNOWN USER OF GLOBAL!: " << *U;
|
2004-10-09 21:48:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-06 19:14:05 +00:00
|
|
|
}
|
2004-10-09 21:48:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-06 04:27:31 +00:00
|
|
|
static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) {
|
2004-10-10 23:14:11 +00:00
|
|
|
bool Changed = false;
|
|
|
|
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) {
|
|
|
|
Instruction *I = cast<Instruction>(*UI++);
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
|
|
|
|
LI->setOperand(0, NewV);
|
|
|
|
Changed = true;
|
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
|
|
|
|
if (SI->getOperand(1) == V) {
|
|
|
|
SI->setOperand(1, NewV);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
} else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
|
2010-04-06 18:45:08 +00:00
|
|
|
CallSite CS(I);
|
|
|
|
if (CS.getCalledValue() == V) {
|
2004-10-10 23:14:11 +00:00
|
|
|
// Calling through the pointer! Turn into a direct call, but be careful
|
|
|
|
// that the pointer is not also being passed as an argument.
|
2010-04-06 18:45:08 +00:00
|
|
|
CS.setCalledFunction(NewV);
|
2004-10-10 23:14:11 +00:00
|
|
|
Changed = true;
|
|
|
|
bool PassedAsArg = false;
|
2010-04-06 18:45:08 +00:00
|
|
|
for (unsigned i = 0, e = CS.arg_size(); i != e; ++i)
|
|
|
|
if (CS.getArgument(i) == V) {
|
2004-10-10 23:14:11 +00:00
|
|
|
PassedAsArg = true;
|
2010-04-06 18:45:08 +00:00
|
|
|
CS.setArgument(i, NewV);
|
2004-10-10 23:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PassedAsArg) {
|
|
|
|
// Being passed as an argument also. Be careful to not invalidate UI!
|
|
|
|
UI = V->use_begin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (CastInst *CI = dyn_cast<CastInst>(I)) {
|
|
|
|
Changed |= OptimizeAwayTrappingUsesOfValue(CI,
|
2009-07-29 18:55:55 +00:00
|
|
|
ConstantExpr::getCast(CI->getOpcode(),
|
2009-11-06 04:27:31 +00:00
|
|
|
NewV, CI->getType()));
|
2004-10-10 23:14:11 +00:00
|
|
|
if (CI->use_empty()) {
|
|
|
|
Changed = true;
|
2004-10-16 18:09:00 +00:00
|
|
|
CI->eraseFromParent();
|
2004-10-10 23:14:11 +00:00
|
|
|
}
|
|
|
|
} else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
|
|
|
|
// Should handle GEP here.
|
2007-01-31 04:40:53 +00:00
|
|
|
SmallVector<Constant*, 8> Idxs;
|
|
|
|
Idxs.reserve(GEPI->getNumOperands()-1);
|
2008-05-29 01:59:18 +00:00
|
|
|
for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end();
|
|
|
|
i != e; ++i)
|
|
|
|
if (Constant *C = dyn_cast<Constant>(*i))
|
2007-01-31 04:40:53 +00:00
|
|
|
Idxs.push_back(C);
|
2004-10-10 23:14:11 +00:00
|
|
|
else
|
|
|
|
break;
|
2007-01-31 04:40:53 +00:00
|
|
|
if (Idxs.size() == GEPI->getNumOperands()-1)
|
2004-10-10 23:14:11 +00:00
|
|
|
Changed |= OptimizeAwayTrappingUsesOfValue(GEPI,
|
2011-07-21 14:31:17 +00:00
|
|
|
ConstantExpr::getGetElementPtr(NewV, Idxs));
|
2004-10-10 23:14:11 +00:00
|
|
|
if (GEPI->use_empty()) {
|
|
|
|
Changed = true;
|
2004-10-16 18:09:00 +00:00
|
|
|
GEPI->eraseFromParent();
|
2004-10-10 23:14:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null
|
|
|
|
/// value stored into it. If there are uses of the loaded value that would trap
|
|
|
|
/// if the loaded value is dynamically null, then we know that they cannot be
|
|
|
|
/// reachable with a null optimize away the load.
|
2012-02-12 01:13:18 +00:00
|
|
|
static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV,
|
2012-10-08 16:38:25 +00:00
|
|
|
DataLayout *TD,
|
2012-02-12 01:13:18 +00:00
|
|
|
TargetLibraryInfo *TLI) {
|
2004-10-10 23:14:11 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
2009-01-14 00:12:58 +00:00
|
|
|
// Keep track of whether we are able to remove all the uses of the global
|
|
|
|
// other than the store that defines it.
|
|
|
|
bool AllNonStoreUsesGone = true;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2004-10-10 23:14:11 +00:00
|
|
|
// Replace all uses of loads with uses of uses of the stored value.
|
2009-01-14 00:12:58 +00:00
|
|
|
for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end(); GUI != E;){
|
|
|
|
User *GlobalUser = *GUI++;
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) {
|
2009-11-06 04:27:31 +00:00
|
|
|
Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
|
2009-01-14 00:12:58 +00:00
|
|
|
// If we were able to delete all uses of the loads
|
|
|
|
if (LI->use_empty()) {
|
|
|
|
LI->eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
} else {
|
|
|
|
AllNonStoreUsesGone = false;
|
|
|
|
}
|
|
|
|
} else if (isa<StoreInst>(GlobalUser)) {
|
|
|
|
// Ignore the store that stores "LV" to the global.
|
|
|
|
assert(GlobalUser->getOperand(1) == GV &&
|
|
|
|
"Must be storing *to* the global");
|
2004-10-10 23:14:11 +00:00
|
|
|
} else {
|
2009-01-14 00:12:58 +00:00
|
|
|
AllNonStoreUsesGone = false;
|
|
|
|
|
|
|
|
// If we get here we could have other crazy uses that are transitively
|
|
|
|
// loaded.
|
|
|
|
assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) ||
|
2012-09-28 10:01:27 +00:00
|
|
|
isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser) ||
|
|
|
|
isa<BitCastInst>(GlobalUser) ||
|
|
|
|
isa<GetElementPtrInst>(GlobalUser)) &&
|
2011-05-22 07:15:13 +00:00
|
|
|
"Only expect load and stores!");
|
2004-10-10 23:14:11 +00:00
|
|
|
}
|
2009-01-14 00:12:58 +00:00
|
|
|
}
|
2004-10-10 23:14:11 +00:00
|
|
|
|
|
|
|
if (Changed) {
|
2010-01-05 01:28:05 +00:00
|
|
|
DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV);
|
2004-10-10 23:14:11 +00:00
|
|
|
++NumGlobUses;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we nuked all of the loads, then none of the stores are needed either,
|
|
|
|
// nor is the global.
|
2009-01-14 00:12:58 +00:00
|
|
|
if (AllNonStoreUsesGone) {
|
2012-07-24 07:21:08 +00:00
|
|
|
if (isLeakCheckerRoot(GV)) {
|
2012-08-29 15:32:21 +00:00
|
|
|
Changed |= CleanupPointerRootUsers(GV, TLI);
|
2012-07-24 07:21:08 +00:00
|
|
|
} else {
|
|
|
|
Changed = true;
|
|
|
|
CleanupConstantGlobalUsers(GV, 0, TD, TLI);
|
|
|
|
}
|
2004-10-10 23:14:11 +00:00
|
|
|
if (GV->use_empty()) {
|
2012-07-24 07:21:08 +00:00
|
|
|
DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n");
|
|
|
|
Changed = true;
|
2004-10-16 18:09:00 +00:00
|
|
|
GV->eraseFromParent();
|
2004-10-10 23:14:11 +00:00
|
|
|
++NumDeleted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
/// ConstantPropUsersOf - Walk the use list of V, constant folding all of the
|
|
|
|
/// instructions that are foldable.
|
2012-02-12 01:13:18 +00:00
|
|
|
static void ConstantPropUsersOf(Value *V,
|
2012-10-08 16:38:25 +00:00
|
|
|
DataLayout *TD, TargetLibraryInfo *TLI) {
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; )
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(*UI++))
|
2012-02-12 01:13:18 +00:00
|
|
|
if (Constant *NewC = ConstantFoldInstruction(I, TD, TLI)) {
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
I->replaceAllUsesWith(NewC);
|
|
|
|
|
2005-02-01 01:23:31 +00:00
|
|
|
// Advance UI to the next non-I use to avoid invalidating it!
|
|
|
|
// Instructions could multiply use V.
|
|
|
|
while (UI != E && *UI == I)
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
++UI;
|
2005-02-01 01:23:31 +00:00
|
|
|
I->eraseFromParent();
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
/// OptimizeGlobalAddressOfMalloc - This function takes the specified global
|
|
|
|
/// variable, and transforms the program as if it always contained the result of
|
|
|
|
/// the specified malloc. Because it is always the result of the specified
|
|
|
|
/// malloc, there is no reason to actually DO the malloc. Instead, turn the
|
|
|
|
/// malloc into a global, and any loads of GV as uses of the new global.
|
|
|
|
static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
|
|
|
|
CallInst *CI,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *AllocTy,
|
2010-02-25 22:33:52 +00:00
|
|
|
ConstantInt *NElements,
|
2012-10-08 16:38:25 +00:00
|
|
|
DataLayout *TD,
|
2012-02-12 01:13:18 +00:00
|
|
|
TargetLibraryInfo *TLI) {
|
2010-02-25 22:33:52 +00:00
|
|
|
DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI << '\n');
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *GlobalType;
|
2010-02-25 22:33:52 +00:00
|
|
|
if (NElements->getZExtValue() == 1)
|
|
|
|
GlobalType = AllocTy;
|
|
|
|
else
|
|
|
|
// If we have an array allocation, the global variable is of an array.
|
|
|
|
GlobalType = ArrayType::get(AllocTy, NElements->getZExtValue());
|
2009-09-18 22:35:49 +00:00
|
|
|
|
|
|
|
// Create the new global variable. The contents of the malloc'd memory is
|
|
|
|
// undefined, so initialize with an undef value.
|
2010-10-18 21:16:00 +00:00
|
|
|
GlobalVariable *NewGV = new GlobalVariable(*GV->getParent(),
|
2010-02-26 23:42:13 +00:00
|
|
|
GlobalType, false,
|
2010-02-25 22:33:52 +00:00
|
|
|
GlobalValue::InternalLinkage,
|
2010-02-26 23:42:13 +00:00
|
|
|
UndefValue::get(GlobalType),
|
2009-09-18 22:35:49 +00:00
|
|
|
GV->getName()+".body",
|
|
|
|
GV,
|
2012-06-23 11:37:03 +00:00
|
|
|
GV->getThreadLocalMode());
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-02-25 22:33:52 +00:00
|
|
|
// If there are bitcast users of the malloc (which is typical, usually we have
|
|
|
|
// a malloc + bitcast) then replace them with uses of the new global. Update
|
|
|
|
// other users to use the global as well.
|
|
|
|
BitCastInst *TheBC = 0;
|
|
|
|
while (!CI->use_empty()) {
|
|
|
|
Instruction *User = cast<Instruction>(CI->use_back());
|
|
|
|
if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) {
|
|
|
|
if (BCI->getType() == NewGV->getType()) {
|
|
|
|
BCI->replaceAllUsesWith(NewGV);
|
|
|
|
BCI->eraseFromParent();
|
|
|
|
} else {
|
|
|
|
BCI->setOperand(0, NewGV);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (TheBC == 0)
|
|
|
|
TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI);
|
|
|
|
User->replaceUsesOfWith(CI, TheBC);
|
|
|
|
}
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
Constant *RepValue = NewGV;
|
|
|
|
if (NewGV->getType() != GV->getType()->getElementType())
|
2010-10-18 21:16:00 +00:00
|
|
|
RepValue = ConstantExpr::getBitCast(RepValue,
|
2009-09-18 22:35:49 +00:00
|
|
|
GV->getType()->getElementType());
|
|
|
|
|
|
|
|
// If there is a comparison against null, we will insert a global bool to
|
|
|
|
// keep track of whether the global was initialized yet or not.
|
|
|
|
GlobalVariable *InitBool =
|
2009-11-06 04:27:31 +00:00
|
|
|
new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,
|
2009-09-18 22:35:49 +00:00
|
|
|
GlobalValue::InternalLinkage,
|
2009-11-06 04:27:31 +00:00
|
|
|
ConstantInt::getFalse(GV->getContext()),
|
2012-06-23 11:37:03 +00:00
|
|
|
GV->getName()+".init", GV->getThreadLocalMode());
|
2009-09-18 22:35:49 +00:00
|
|
|
bool InitBoolUsed = false;
|
|
|
|
|
|
|
|
// Loop over all uses of GV, processing them in turn.
|
2010-02-25 22:33:52 +00:00
|
|
|
while (!GV->use_empty()) {
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(GV->use_back())) {
|
2009-09-18 22:35:49 +00:00
|
|
|
// The global is initialized when the store to it occurs.
|
2012-02-05 19:56:38 +00:00
|
|
|
new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, false, 0,
|
|
|
|
SI->getOrdering(), SI->getSynchScope(), SI);
|
2009-09-18 22:35:49 +00:00
|
|
|
SI->eraseFromParent();
|
2010-02-25 22:33:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-02-25 22:33:52 +00:00
|
|
|
LoadInst *LI = cast<LoadInst>(GV->use_back());
|
|
|
|
while (!LI->use_empty()) {
|
|
|
|
Use &LoadUse = LI->use_begin().getUse();
|
|
|
|
if (!isa<ICmpInst>(LoadUse.getUser())) {
|
|
|
|
LoadUse = RepValue;
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-02-25 22:33:52 +00:00
|
|
|
ICmpInst *ICI = cast<ICmpInst>(LoadUse.getUser());
|
|
|
|
// Replace the cmp X, 0 with a use of the bool value.
|
2012-02-05 19:56:38 +00:00
|
|
|
// Sink the load to where the compare was, if atomic rules allow us to.
|
|
|
|
Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", false, 0,
|
|
|
|
LI->getOrdering(), LI->getSynchScope(),
|
|
|
|
LI->isUnordered() ? (Instruction*)ICI : LI);
|
2010-02-25 22:33:52 +00:00
|
|
|
InitBoolUsed = true;
|
|
|
|
switch (ICI->getPredicate()) {
|
|
|
|
default: llvm_unreachable("Unknown ICmp Predicate!");
|
|
|
|
case ICmpInst::ICMP_ULT:
|
|
|
|
case ICmpInst::ICMP_SLT: // X < null -> always false
|
|
|
|
LV = ConstantInt::getFalse(GV->getContext());
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_ULE:
|
|
|
|
case ICmpInst::ICMP_SLE:
|
|
|
|
case ICmpInst::ICMP_EQ:
|
|
|
|
LV = BinaryOperator::CreateNot(LV, "notinit", ICI);
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE:
|
|
|
|
case ICmpInst::ICMP_UGE:
|
|
|
|
case ICmpInst::ICMP_SGE:
|
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
case ICmpInst::ICMP_SGT:
|
|
|
|
break; // no change.
|
|
|
|
}
|
|
|
|
ICI->replaceAllUsesWith(LV);
|
|
|
|
ICI->eraseFromParent();
|
2009-09-18 22:35:49 +00:00
|
|
|
}
|
2010-02-25 22:33:52 +00:00
|
|
|
LI->eraseFromParent();
|
|
|
|
}
|
2009-09-18 22:35:49 +00:00
|
|
|
|
|
|
|
// If the initialization boolean was used, insert it, otherwise delete it.
|
|
|
|
if (!InitBoolUsed) {
|
|
|
|
while (!InitBool->use_empty()) // Delete initializations
|
2010-02-25 22:33:52 +00:00
|
|
|
cast<StoreInst>(InitBool->use_back())->eraseFromParent();
|
2009-09-18 22:35:49 +00:00
|
|
|
delete InitBool;
|
|
|
|
} else
|
|
|
|
GV->getParent()->getGlobalList().insert(GV, InitBool);
|
|
|
|
|
2010-02-25 22:33:52 +00:00
|
|
|
// Now the GV is dead, nuke it and the malloc..
|
2009-09-18 22:35:49 +00:00
|
|
|
GV->eraseFromParent();
|
|
|
|
CI->eraseFromParent();
|
|
|
|
|
|
|
|
// To further other optimizations, loop over all users of NewGV and try to
|
|
|
|
// constant prop them. This will promote GEP instructions with constant
|
|
|
|
// indices into GEP constant-exprs, which will allow global-opt to hack on it.
|
2012-02-12 01:13:18 +00:00
|
|
|
ConstantPropUsersOf(NewGV, TD, TLI);
|
2009-09-18 22:35:49 +00:00
|
|
|
if (RepValue != NewGV)
|
2012-02-12 01:13:18 +00:00
|
|
|
ConstantPropUsersOf(RepValue, TD, TLI);
|
2009-09-18 22:35:49 +00:00
|
|
|
|
|
|
|
return NewGV;
|
|
|
|
}
|
|
|
|
|
2004-12-02 07:11:07 +00:00
|
|
|
/// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking
|
|
|
|
/// to make sure that there are no complex uses of V. We permit simple things
|
|
|
|
/// like dereferencing the pointer, but not storing through the address, unless
|
|
|
|
/// it is to the specified global.
|
2010-04-06 18:58:22 +00:00
|
|
|
static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(const Instruction *V,
|
|
|
|
const GlobalVariable *GV,
|
2010-04-06 19:14:05 +00:00
|
|
|
SmallPtrSet<const PHINode*, 8> &PHIs) {
|
2010-04-06 18:58:22 +00:00
|
|
|
for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
|
2010-04-06 19:14:05 +00:00
|
|
|
UI != E; ++UI) {
|
2010-04-06 18:58:22 +00:00
|
|
|
const Instruction *Inst = cast<Instruction>(*UI);
|
2010-04-06 19:14:05 +00:00
|
|
|
|
2008-12-15 21:08:54 +00:00
|
|
|
if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) {
|
|
|
|
continue; // Fine, ignore.
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-04-06 18:58:22 +00:00
|
|
|
if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
2004-12-02 07:11:07 +00:00
|
|
|
if (SI->getOperand(0) == V && SI->getOperand(1) != GV)
|
|
|
|
return false; // Storing the pointer itself... bad.
|
2008-12-15 21:08:54 +00:00
|
|
|
continue; // Otherwise, storing through it, or storing into GV... fine.
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-04-10 18:19:22 +00:00
|
|
|
// Must index into the array and into the struct.
|
|
|
|
if (isa<GetElementPtrInst>(Inst) && Inst->getNumOperands() >= 3) {
|
2008-12-15 21:08:54 +00:00
|
|
|
if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Inst, GV, PHIs))
|
2004-12-02 07:11:07 +00:00
|
|
|
return false;
|
2008-12-15 21:08:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-04-06 18:58:22 +00:00
|
|
|
if (const PHINode *PN = dyn_cast<PHINode>(Inst)) {
|
2007-09-13 16:37:20 +00:00
|
|
|
// PHIs are ok if all uses are ok. Don't infinitely recurse through PHI
|
|
|
|
// cycles.
|
|
|
|
if (PHIs.insert(PN))
|
2007-09-14 03:41:21 +00:00
|
|
|
if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(PN, GV, PHIs))
|
|
|
|
return false;
|
2008-12-15 21:08:54 +00:00
|
|
|
continue;
|
2004-12-02 07:11:07 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-04-06 18:58:22 +00:00
|
|
|
if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Inst)) {
|
2008-12-15 21:08:54 +00:00
|
|
|
if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs))
|
|
|
|
return false;
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-15 21:08:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2004-12-02 07:11:07 +00:00
|
|
|
return true;
|
2006-09-30 23:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ReplaceUsesOfMallocWithGlobal - The Alloc pointer is stored into GV
|
|
|
|
/// somewhere. Transform all uses of the allocation into loads from the
|
|
|
|
/// global and uses of the resultant pointer. Further, delete the store into
|
2010-10-18 21:16:00 +00:00
|
|
|
/// GV. This assumes that these value pass the
|
2006-09-30 23:32:09 +00:00
|
|
|
/// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate.
|
2010-10-18 21:16:00 +00:00
|
|
|
static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc,
|
2006-09-30 23:32:09 +00:00
|
|
|
GlobalVariable *GV) {
|
|
|
|
while (!Alloc->use_empty()) {
|
2007-09-13 18:00:31 +00:00
|
|
|
Instruction *U = cast<Instruction>(*Alloc->use_begin());
|
|
|
|
Instruction *InsertPt = U;
|
2006-09-30 23:32:09 +00:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
|
|
|
|
// If this is the store of the allocation into the global, remove it.
|
|
|
|
if (SI->getOperand(1) == GV) {
|
|
|
|
SI->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
2007-09-13 18:00:31 +00:00
|
|
|
} else if (PHINode *PN = dyn_cast<PHINode>(U)) {
|
|
|
|
// Insert the load in the corresponding predecessor, not right before the
|
|
|
|
// PHI.
|
2009-01-23 19:40:15 +00:00
|
|
|
InsertPt = PN->getIncomingBlock(Alloc->use_begin())->getTerminator();
|
2008-12-15 21:44:34 +00:00
|
|
|
} else if (isa<BitCastInst>(U)) {
|
|
|
|
// Must be bitcast between the malloc and store to initialize the global.
|
|
|
|
ReplaceUsesOfMallocWithGlobal(U, GV);
|
|
|
|
U->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
} else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
|
|
|
|
// If this is a "GEP bitcast" and the user is a store to the global, then
|
|
|
|
// just process it as a bitcast.
|
|
|
|
if (GEPI->hasAllZeroIndices() && GEPI->hasOneUse())
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(GEPI->use_back()))
|
|
|
|
if (SI->getOperand(1) == GV) {
|
|
|
|
// Must be bitcast GEP between the malloc and store to initialize
|
|
|
|
// the global.
|
|
|
|
ReplaceUsesOfMallocWithGlobal(GEPI, GV);
|
|
|
|
GEPI->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
2006-09-30 23:32:09 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2006-09-30 23:32:09 +00:00
|
|
|
// Insert a load from the global, and use it instead of the malloc.
|
2007-09-13 18:00:31 +00:00
|
|
|
Value *NL = new LoadInst(GV, GV->getName()+".val", InsertPt);
|
2006-09-30 23:32:09 +00:00
|
|
|
U->replaceUsesOfWith(Alloc, NL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-16 21:24:51 +00:00
|
|
|
/// LoadUsesSimpleEnoughForHeapSRA - Verify that all uses of V (a load, or a phi
|
|
|
|
/// of a load) are simple enough to perform heap SRA on. This permits GEP's
|
|
|
|
/// that index through the array and struct field, icmps of null, and PHIs.
|
2010-04-01 08:21:08 +00:00
|
|
|
static bool LoadUsesSimpleEnoughForHeapSRA(const Value *V,
|
2010-04-07 18:59:26 +00:00
|
|
|
SmallPtrSet<const PHINode*, 32> &LoadUsingPHIs,
|
|
|
|
SmallPtrSet<const PHINode*, 32> &LoadUsingPHIsPerLoad) {
|
2008-12-16 21:24:51 +00:00
|
|
|
// We permit two users of the load: setcc comparing against the null
|
|
|
|
// pointer, and a getelementptr of a specific form.
|
2010-04-07 18:59:26 +00:00
|
|
|
for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;
|
|
|
|
++UI) {
|
2010-04-01 08:21:08 +00:00
|
|
|
const Instruction *User = cast<Instruction>(*UI);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-16 21:24:51 +00:00
|
|
|
// Comparison against null is ok.
|
2010-04-01 08:21:08 +00:00
|
|
|
if (const ICmpInst *ICI = dyn_cast<ICmpInst>(User)) {
|
2008-12-16 21:24:51 +00:00
|
|
|
if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
|
|
|
|
return false;
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-16 21:24:51 +00:00
|
|
|
// getelementptr is also ok, but only a simple form.
|
2010-04-01 08:21:08 +00:00
|
|
|
if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
|
2008-12-16 21:24:51 +00:00
|
|
|
// Must index into the array and into the struct.
|
|
|
|
if (GEPI->getNumOperands() < 3)
|
|
|
|
return false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-16 21:24:51 +00:00
|
|
|
// Otherwise the GEP is ok.
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-04-01 08:21:08 +00:00
|
|
|
if (const PHINode *PN = dyn_cast<PHINode>(User)) {
|
2009-06-02 00:56:07 +00:00
|
|
|
if (!LoadUsingPHIsPerLoad.insert(PN))
|
|
|
|
// This means some phi nodes are dependent on each other.
|
|
|
|
// Avoid infinite looping!
|
|
|
|
return false;
|
|
|
|
if (!LoadUsingPHIs.insert(PN))
|
|
|
|
// If we have already analyzed this PHI, then it is safe.
|
2008-12-16 21:24:51 +00:00
|
|
|
continue;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
// Make sure all uses of the PHI are simple enough to transform.
|
2009-06-02 00:56:07 +00:00
|
|
|
if (!LoadUsesSimpleEnoughForHeapSRA(PN,
|
|
|
|
LoadUsingPHIs, LoadUsingPHIsPerLoad))
|
2008-12-16 21:24:51 +00:00
|
|
|
return false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-16 21:24:51 +00:00
|
|
|
continue;
|
2006-09-30 23:32:09 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-16 21:24:51 +00:00
|
|
|
// Otherwise we don't know what this is, not ok.
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-16 21:24:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// AllGlobalLoadUsesSimpleEnoughForHeapSRA - If all users of values loaded from
|
|
|
|
/// GV are simple enough to perform HeapSRA, return true.
|
2010-04-01 08:21:08 +00:00
|
|
|
static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(const GlobalVariable *GV,
|
2009-09-18 22:35:49 +00:00
|
|
|
Instruction *StoredVal) {
|
2010-04-01 08:21:08 +00:00
|
|
|
SmallPtrSet<const PHINode*, 32> LoadUsingPHIs;
|
|
|
|
SmallPtrSet<const PHINode*, 32> LoadUsingPHIsPerLoad;
|
2010-04-07 18:59:26 +00:00
|
|
|
for (Value::const_use_iterator UI = GV->use_begin(), E = GV->use_end();
|
|
|
|
UI != E; ++UI)
|
2010-04-01 08:21:08 +00:00
|
|
|
if (const LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
|
2009-06-02 00:56:07 +00:00
|
|
|
if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs,
|
|
|
|
LoadUsingPHIsPerLoad))
|
2008-12-17 05:28:49 +00:00
|
|
|
return false;
|
2009-06-02 00:56:07 +00:00
|
|
|
LoadUsingPHIsPerLoad.clear();
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
// If we reach here, we know that all uses of the loads and transitive uses
|
|
|
|
// (through PHI nodes) are simple enough to transform. However, we don't know
|
2010-10-18 21:16:00 +00:00
|
|
|
// that all inputs the to the PHI nodes are in the same equivalence sets.
|
2008-12-17 05:28:49 +00:00
|
|
|
// Check to verify that all operands of the PHIs are either PHIS that can be
|
|
|
|
// transformed, loads from GV, or MI itself.
|
2010-04-07 18:59:26 +00:00
|
|
|
for (SmallPtrSet<const PHINode*, 32>::const_iterator I = LoadUsingPHIs.begin()
|
|
|
|
, E = LoadUsingPHIs.end(); I != E; ++I) {
|
2010-04-01 08:21:08 +00:00
|
|
|
const PHINode *PN = *I;
|
2008-12-17 05:28:49 +00:00
|
|
|
for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) {
|
|
|
|
Value *InVal = PN->getIncomingValue(op);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
// PHI of the stored value itself is ok.
|
2009-09-18 22:35:49 +00:00
|
|
|
if (InVal == StoredVal) continue;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-04-01 08:21:08 +00:00
|
|
|
if (const PHINode *InPN = dyn_cast<PHINode>(InVal)) {
|
2008-12-17 05:28:49 +00:00
|
|
|
// One of the PHIs in our set is (optimistically) ok.
|
|
|
|
if (LoadUsingPHIs.count(InPN))
|
|
|
|
continue;
|
2008-12-16 21:24:51 +00:00
|
|
|
return false;
|
2008-12-17 05:28:49 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
// Load from GV is ok.
|
2010-04-01 08:21:08 +00:00
|
|
|
if (const LoadInst *LI = dyn_cast<LoadInst>(InVal))
|
2008-12-17 05:28:49 +00:00
|
|
|
if (LI->getOperand(0) == GV)
|
|
|
|
continue;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
// UNDEF? NULL?
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
// Anything else is rejected.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2006-09-30 23:32:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
static Value *GetHeapSROAValue(Value *V, unsigned FieldNo,
|
|
|
|
DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
|
2009-11-06 04:27:31 +00:00
|
|
|
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
|
2008-12-17 05:28:49 +00:00
|
|
|
std::vector<Value*> &FieldVals = InsertedScalarizedValues[V];
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
if (FieldNo >= FieldVals.size())
|
|
|
|
FieldVals.resize(FieldNo+1);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
// If we already have this value, just reuse the previously scalarized
|
|
|
|
// version.
|
|
|
|
if (Value *FieldVal = FieldVals[FieldNo])
|
|
|
|
return FieldVal;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
// Depending on what instruction this is, we have several cases.
|
|
|
|
Value *Result;
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(V)) {
|
|
|
|
// This is a scalarized version of the load from the global. Just create
|
|
|
|
// a new Load of the scalarized global.
|
|
|
|
Result = new LoadInst(GetHeapSROAValue(LI->getOperand(0), FieldNo,
|
|
|
|
InsertedScalarizedValues,
|
2009-11-06 04:27:31 +00:00
|
|
|
PHIsToRewrite),
|
2009-07-30 17:37:43 +00:00
|
|
|
LI->getName()+".f"+Twine(FieldNo), LI);
|
2008-12-17 05:28:49 +00:00
|
|
|
} else if (PHINode *PN = dyn_cast<PHINode>(V)) {
|
|
|
|
// PN's type is pointer to struct. Make a new PHI of pointer to struct
|
|
|
|
// field.
|
2011-07-18 04:54:35 +00:00
|
|
|
StructType *ST =
|
2008-12-17 05:28:49 +00:00
|
|
|
cast<StructType>(cast<PointerType>(PN->getType())->getElementType());
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2011-03-30 11:19:20 +00:00
|
|
|
PHINode *NewPN =
|
2009-07-29 22:17:13 +00:00
|
|
|
PHINode::Create(PointerType::getUnqual(ST->getElementType(FieldNo)),
|
2011-03-30 11:28:46 +00:00
|
|
|
PN->getNumIncomingValues(),
|
2009-07-30 17:37:43 +00:00
|
|
|
PN->getName()+".f"+Twine(FieldNo), PN);
|
2011-03-30 11:19:20 +00:00
|
|
|
Result = NewPN;
|
2008-12-17 05:28:49 +00:00
|
|
|
PHIsToRewrite.push_back(std::make_pair(PN, FieldNo));
|
|
|
|
} else {
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Unknown usable value");
|
2008-12-17 05:28:49 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
return FieldVals[FieldNo] = Result;
|
2007-09-13 18:00:31 +00:00
|
|
|
}
|
|
|
|
|
2007-09-13 17:29:05 +00:00
|
|
|
/// RewriteHeapSROALoadUser - Given a load instruction and a value derived from
|
|
|
|
/// the load, rewrite the derived value to use the HeapSRoA'd load.
|
2010-10-18 21:16:00 +00:00
|
|
|
static void RewriteHeapSROALoadUser(Instruction *LoadUser,
|
2008-12-17 05:28:49 +00:00
|
|
|
DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
|
2009-11-06 04:27:31 +00:00
|
|
|
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
|
2007-09-13 17:29:05 +00:00
|
|
|
// If this is a comparison against null, handle it.
|
|
|
|
if (ICmpInst *SCI = dyn_cast<ICmpInst>(LoadUser)) {
|
|
|
|
assert(isa<ConstantPointerNull>(SCI->getOperand(1)));
|
|
|
|
// If we have a setcc of the loaded pointer, we can use a setcc of any
|
|
|
|
// field.
|
2008-12-17 05:28:49 +00:00
|
|
|
Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0,
|
2009-11-06 04:27:31 +00:00
|
|
|
InsertedScalarizedValues, PHIsToRewrite);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-07-09 23:48:35 +00:00
|
|
|
Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr,
|
2010-10-18 21:16:00 +00:00
|
|
|
Constant::getNullValue(NPtr->getType()),
|
2009-07-09 23:48:35 +00:00
|
|
|
SCI->getName());
|
2007-09-13 17:29:05 +00:00
|
|
|
SCI->replaceAllUsesWith(New);
|
|
|
|
SCI->eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
// Handle 'getelementptr Ptr, Idx, i32 FieldNo ...'
|
2007-09-13 18:00:31 +00:00
|
|
|
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(LoadUser)) {
|
|
|
|
assert(GEPI->getNumOperands() >= 3 && isa<ConstantInt>(GEPI->getOperand(2))
|
|
|
|
&& "Unexpected GEPI!");
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2007-09-13 18:00:31 +00:00
|
|
|
// Load the pointer for this field.
|
|
|
|
unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue();
|
2008-12-17 05:28:49 +00:00
|
|
|
Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo,
|
2009-11-06 04:27:31 +00:00
|
|
|
InsertedScalarizedValues, PHIsToRewrite);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2007-09-13 18:00:31 +00:00
|
|
|
// Create the new GEP idx vector.
|
|
|
|
SmallVector<Value*, 8> GEPIdx;
|
|
|
|
GEPIdx.push_back(GEPI->getOperand(1));
|
|
|
|
GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end());
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2011-07-25 09:48:08 +00:00
|
|
|
Value *NGEPI = GetElementPtrInst::Create(NewPtr, GEPIdx,
|
2008-04-06 20:25:17 +00:00
|
|
|
GEPI->getName(), GEPI);
|
2007-09-13 18:00:31 +00:00
|
|
|
GEPI->replaceAllUsesWith(NGEPI);
|
|
|
|
GEPI->eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
2008-12-17 05:28:49 +00:00
|
|
|
|
|
|
|
// Recursively transform the users of PHI nodes. This will lazily create the
|
|
|
|
// PHIs that are needed for individual elements. Keep track of what PHIs we
|
|
|
|
// see in InsertedScalarizedValues so that we don't get infinite loops (very
|
|
|
|
// antisocial). If the PHI is already in InsertedScalarizedValues, it has
|
|
|
|
// already been seen first by another load, so its uses have already been
|
|
|
|
// processed.
|
2007-09-13 18:00:31 +00:00
|
|
|
PHINode *PN = cast<PHINode>(LoadUser);
|
2011-07-21 06:21:31 +00:00
|
|
|
if (!InsertedScalarizedValues.insert(std::make_pair(PN,
|
|
|
|
std::vector<Value*>())).second)
|
|
|
|
return;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
// If this is the first time we've seen this PHI, recursively process all
|
|
|
|
// users.
|
2008-12-17 05:42:08 +00:00
|
|
|
for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E; ) {
|
|
|
|
Instruction *User = cast<Instruction>(*UI++);
|
2009-11-06 04:27:31 +00:00
|
|
|
RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
|
2008-12-17 05:42:08 +00:00
|
|
|
}
|
2007-09-13 17:29:05 +00:00
|
|
|
}
|
|
|
|
|
2006-09-30 23:32:09 +00:00
|
|
|
/// RewriteUsesOfLoadForHeapSRoA - We are performing Heap SRoA on a global. Ptr
|
|
|
|
/// is a value loaded from the global. Eliminate all uses of Ptr, making them
|
|
|
|
/// use FieldGlobals instead. All uses of loaded values satisfy
|
2008-12-16 21:24:51 +00:00
|
|
|
/// AllGlobalLoadUsesSimpleEnoughForHeapSRA.
|
2010-10-18 21:16:00 +00:00
|
|
|
static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
|
2008-12-17 05:28:49 +00:00
|
|
|
DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
|
2009-11-06 04:27:31 +00:00
|
|
|
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
|
2008-12-17 05:28:49 +00:00
|
|
|
for (Value::use_iterator UI = Load->use_begin(), E = Load->use_end();
|
2008-12-17 05:42:08 +00:00
|
|
|
UI != E; ) {
|
|
|
|
Instruction *User = cast<Instruction>(*UI++);
|
2009-11-06 04:27:31 +00:00
|
|
|
RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
|
2008-12-17 05:42:08 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-12-17 05:28:49 +00:00
|
|
|
if (Load->use_empty()) {
|
|
|
|
Load->eraseFromParent();
|
|
|
|
InsertedScalarizedValues.erase(Load);
|
|
|
|
}
|
2006-09-30 23:32:09 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
/// PerformHeapAllocSRoA - CI is an allocation of an array of structures. Break
|
|
|
|
/// it up into multiple allocations of arrays of the fields.
|
2009-11-07 00:16:28 +00:00
|
|
|
static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI,
|
2012-10-08 16:38:25 +00:00
|
|
|
Value *NElems, DataLayout *TD,
|
2012-08-29 15:32:21 +00:00
|
|
|
const TargetLibraryInfo *TLI) {
|
2010-01-05 01:28:05 +00:00
|
|
|
DEBUG(dbgs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *CI << '\n');
|
2012-08-29 15:32:21 +00:00
|
|
|
Type *MAT = getMallocAllocatedType(CI, TLI);
|
2011-07-18 04:54:35 +00:00
|
|
|
StructType *STy = cast<StructType>(MAT);
|
2009-09-18 22:35:49 +00:00
|
|
|
|
|
|
|
// There is guaranteed to be at least one use of the malloc (storing
|
|
|
|
// it into GV). If there are other uses, change them to be uses of
|
|
|
|
// the global to simplify later code. This also deletes the store
|
|
|
|
// into GV.
|
2009-11-07 00:16:28 +00:00
|
|
|
ReplaceUsesOfMallocWithGlobal(CI, GV);
|
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// Okay, at this point, there are no users of the malloc. Insert N
|
|
|
|
// new mallocs at the same place as CI, and N globals.
|
|
|
|
std::vector<Value*> FieldGlobals;
|
|
|
|
std::vector<Value*> FieldMallocs;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *FieldTy = STy->getElementType(FieldNo);
|
|
|
|
PointerType *PFieldTy = PointerType::getUnqual(FieldTy);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
GlobalVariable *NGV =
|
|
|
|
new GlobalVariable(*GV->getParent(),
|
|
|
|
PFieldTy, false, GlobalValue::InternalLinkage,
|
|
|
|
Constant::getNullValue(PFieldTy),
|
|
|
|
GV->getName() + ".f" + Twine(FieldNo), GV,
|
2012-06-23 11:37:03 +00:00
|
|
|
GV->getThreadLocalMode());
|
2009-09-18 22:35:49 +00:00
|
|
|
FieldGlobals.push_back(NGV);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-11-07 00:16:28 +00:00
|
|
|
unsigned TypeSize = TD->getTypeAllocSize(FieldTy);
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *ST = dyn_cast<StructType>(FieldTy))
|
2009-11-07 00:16:28 +00:00
|
|
|
TypeSize = TD->getStructLayout(ST)->getSizeInBytes();
|
2012-11-01 08:07:29 +00:00
|
|
|
Type *IntPtrTy = TD->getIntPtrType(CI->getContext());
|
2009-11-07 00:16:28 +00:00
|
|
|
Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy,
|
|
|
|
ConstantInt::get(IntPtrTy, TypeSize),
|
2010-07-12 00:57:28 +00:00
|
|
|
NElems, 0,
|
2009-11-07 00:16:28 +00:00
|
|
|
CI->getName() + ".f" + Twine(FieldNo));
|
2010-02-26 18:23:13 +00:00
|
|
|
FieldMallocs.push_back(NMI);
|
2009-11-07 00:16:28 +00:00
|
|
|
new StoreInst(NMI, NGV, CI);
|
2009-09-18 22:35:49 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// The tricky aspect of this transformation is handling the case when malloc
|
|
|
|
// fails. In the original code, malloc failing would set the result pointer
|
|
|
|
// of malloc to null. In this case, some mallocs could succeed and others
|
|
|
|
// could fail. As such, we emit code that looks like this:
|
|
|
|
// F0 = malloc(field0)
|
|
|
|
// F1 = malloc(field1)
|
|
|
|
// F2 = malloc(field2)
|
|
|
|
// if (F0 == 0 || F1 == 0 || F2 == 0) {
|
|
|
|
// if (F0) { free(F0); F0 = 0; }
|
|
|
|
// if (F1) { free(F1); F1 = 0; }
|
|
|
|
// if (F2) { free(F2); F2 = 0; }
|
|
|
|
// }
|
2009-11-10 08:32:25 +00:00
|
|
|
// The malloc can also fail if its argument is too large.
|
2010-06-24 14:42:01 +00:00
|
|
|
Constant *ConstantZero = ConstantInt::get(CI->getArgOperand(0)->getType(), 0);
|
|
|
|
Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getArgOperand(0),
|
2009-11-10 08:32:25 +00:00
|
|
|
ConstantZero, "isneg");
|
2009-09-18 22:35:49 +00:00
|
|
|
for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
|
2009-11-07 00:16:28 +00:00
|
|
|
Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i],
|
|
|
|
Constant::getNullValue(FieldMallocs[i]->getType()),
|
|
|
|
"isnull");
|
2009-11-10 08:32:25 +00:00
|
|
|
RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", CI);
|
2009-09-18 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split the basic block at the old malloc.
|
2009-11-07 00:16:28 +00:00
|
|
|
BasicBlock *OrigBB = CI->getParent();
|
|
|
|
BasicBlock *ContBB = OrigBB->splitBasicBlock(CI, "malloc_cont");
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// Create the block to check the first condition. Put all these blocks at the
|
|
|
|
// end of the function as they are unlikely to be executed.
|
2009-11-06 04:27:31 +00:00
|
|
|
BasicBlock *NullPtrBlock = BasicBlock::Create(OrigBB->getContext(),
|
|
|
|
"malloc_ret_null",
|
2009-09-18 22:35:49 +00:00
|
|
|
OrigBB->getParent());
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// Remove the uncond branch from OrigBB to ContBB, turning it into a cond
|
|
|
|
// branch on RunningOr.
|
|
|
|
OrigBB->getTerminator()->eraseFromParent();
|
|
|
|
BranchInst::Create(NullPtrBlock, ContBB, RunningOr, OrigBB);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// Within the NullPtrBlock, we need to emit a comparison and branch for each
|
|
|
|
// pointer, because some may be null while others are not.
|
|
|
|
for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
|
|
|
|
Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock);
|
2010-10-18 21:16:00 +00:00
|
|
|
Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
|
2011-09-27 20:39:19 +00:00
|
|
|
Constant::getNullValue(GVVal->getType()));
|
2009-11-06 04:27:31 +00:00
|
|
|
BasicBlock *FreeBlock = BasicBlock::Create(Cmp->getContext(), "free_it",
|
2009-09-18 22:35:49 +00:00
|
|
|
OrigBB->getParent());
|
2009-11-06 04:27:31 +00:00
|
|
|
BasicBlock *NextBlock = BasicBlock::Create(Cmp->getContext(), "next",
|
2009-09-18 22:35:49 +00:00
|
|
|
OrigBB->getParent());
|
2009-10-24 04:23:03 +00:00
|
|
|
Instruction *BI = BranchInst::Create(FreeBlock, NextBlock,
|
|
|
|
Cmp, NullPtrBlock);
|
2009-09-18 22:35:49 +00:00
|
|
|
|
|
|
|
// Fill in FreeBlock.
|
2009-10-24 04:23:03 +00:00
|
|
|
CallInst::CreateFree(GVVal, BI);
|
2009-09-18 22:35:49 +00:00
|
|
|
new StoreInst(Constant::getNullValue(GVVal->getType()), FieldGlobals[i],
|
|
|
|
FreeBlock);
|
|
|
|
BranchInst::Create(NextBlock, FreeBlock);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
NullPtrBlock = NextBlock;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
BranchInst::Create(ContBB, NullPtrBlock);
|
2009-11-07 00:16:28 +00:00
|
|
|
|
|
|
|
// CI is no longer needed, remove it.
|
2009-09-18 22:35:49 +00:00
|
|
|
CI->eraseFromParent();
|
|
|
|
|
|
|
|
/// InsertedScalarizedLoads - As we process loads, if we can't immediately
|
|
|
|
/// update all uses of the load, keep track of what scalarized loads are
|
|
|
|
/// inserted for a given load.
|
|
|
|
DenseMap<Value*, std::vector<Value*> > InsertedScalarizedValues;
|
|
|
|
InsertedScalarizedValues[GV] = FieldGlobals;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
std::vector<std::pair<PHINode*, unsigned> > PHIsToRewrite;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// Okay, the malloc site is completely handled. All of the uses of GV are now
|
|
|
|
// loads, and all uses of those loads are simple. Rewrite them to use loads
|
|
|
|
// of the per-field globals instead.
|
|
|
|
for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E;) {
|
|
|
|
Instruction *User = cast<Instruction>(*UI++);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
|
2009-11-06 04:27:31 +00:00
|
|
|
RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite);
|
2009-09-18 22:35:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// Must be a store of null.
|
|
|
|
StoreInst *SI = cast<StoreInst>(User);
|
|
|
|
assert(isa<ConstantPointerNull>(SI->getOperand(0)) &&
|
|
|
|
"Unexpected heap-sra user!");
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// Insert a store of null into each global.
|
|
|
|
for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType *PT = cast<PointerType>(FieldGlobals[i]->getType());
|
2009-09-18 22:35:49 +00:00
|
|
|
Constant *Null = Constant::getNullValue(PT->getElementType());
|
|
|
|
new StoreInst(Null, FieldGlobals[i], SI);
|
|
|
|
}
|
|
|
|
// Erase the original store.
|
|
|
|
SI->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// While we have PHIs that are interesting to rewrite, do it.
|
|
|
|
while (!PHIsToRewrite.empty()) {
|
|
|
|
PHINode *PN = PHIsToRewrite.back().first;
|
|
|
|
unsigned FieldNo = PHIsToRewrite.back().second;
|
|
|
|
PHIsToRewrite.pop_back();
|
|
|
|
PHINode *FieldPN = cast<PHINode>(InsertedScalarizedValues[PN][FieldNo]);
|
|
|
|
assert(FieldPN->getNumIncomingValues() == 0 &&"Already processed this phi");
|
|
|
|
|
|
|
|
// Add all the incoming values. This can materialize more phis.
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
Value *InVal = PN->getIncomingValue(i);
|
|
|
|
InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues,
|
2009-11-06 04:27:31 +00:00
|
|
|
PHIsToRewrite);
|
2009-09-18 22:35:49 +00:00
|
|
|
FieldPN->addIncoming(InVal, PN->getIncomingBlock(i));
|
|
|
|
}
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// Drop all inter-phi links and any loads that made it this far.
|
|
|
|
for (DenseMap<Value*, std::vector<Value*> >::iterator
|
|
|
|
I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(I->first))
|
|
|
|
PN->dropAllReferences();
|
|
|
|
else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
|
|
|
|
LI->dropAllReferences();
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// Delete all the phis and loads now that inter-references are dead.
|
|
|
|
for (DenseMap<Value*, std::vector<Value*> >::iterator
|
|
|
|
I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(I->first))
|
|
|
|
PN->eraseFromParent();
|
|
|
|
else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
|
|
|
|
LI->eraseFromParent();
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// The old global is now dead, remove it.
|
|
|
|
GV->eraseFromParent();
|
|
|
|
|
|
|
|
++NumHeapSRA;
|
|
|
|
return cast<GlobalVariable>(FieldGlobals[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TryToOptimizeStoreOfMallocToGlobal - This function is called when we see a
|
|
|
|
/// pointer global variable with a single value stored it that is a malloc or
|
|
|
|
/// cast of malloc.
|
|
|
|
static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
|
|
|
|
CallInst *CI,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *AllocTy,
|
2012-02-05 19:56:38 +00:00
|
|
|
AtomicOrdering Ordering,
|
2009-09-18 22:35:49 +00:00
|
|
|
Module::global_iterator &GVI,
|
2012-10-08 16:38:25 +00:00
|
|
|
DataLayout *TD,
|
2012-02-12 01:13:18 +00:00
|
|
|
TargetLibraryInfo *TLI) {
|
2010-04-14 20:52:55 +00:00
|
|
|
if (!TD)
|
|
|
|
return false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
// If this is a malloc of an abstract type, don't touch it.
|
|
|
|
if (!AllocTy->isSized())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We can't optimize this global unless all uses of it are *known* to be
|
|
|
|
// of the malloc value, not of the null initializer value (consider a use
|
|
|
|
// that compares the global's value against zero to see if the malloc has
|
|
|
|
// been reached). To do this, we check to see if all uses of the global
|
|
|
|
// would trap if the global were null: this proves that they must all
|
|
|
|
// happen after the malloc.
|
|
|
|
if (!AllUsesOfLoadedValueWillTrapIfNull(GV))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We can't optimize this if the malloc itself is used in a complex way,
|
|
|
|
// for example, being stored into multiple globals. This allows the
|
2012-02-05 19:48:37 +00:00
|
|
|
// malloc to be stored into the specified global, loaded icmp'd, and
|
2009-09-18 22:35:49 +00:00
|
|
|
// GEP'd. These are all things we could transform to using the global
|
|
|
|
// for.
|
2010-04-14 20:52:55 +00:00
|
|
|
SmallPtrSet<const PHINode*, 8> PHIs;
|
|
|
|
if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV, PHIs))
|
|
|
|
return false;
|
2009-09-18 22:35:49 +00:00
|
|
|
|
|
|
|
// If we have a global that is only initialized with a fixed size malloc,
|
|
|
|
// transform the program to use global memory instead of malloc'd memory.
|
|
|
|
// This eliminates dynamic allocation, avoids an indirection accessing the
|
|
|
|
// data, and exposes the resultant global to further GlobalOpt.
|
2009-10-16 23:12:25 +00:00
|
|
|
// We cannot optimize the malloc if we cannot determine malloc array size.
|
2012-08-29 15:32:21 +00:00
|
|
|
Value *NElems = getMallocArraySize(CI, TD, TLI, true);
|
2010-04-14 20:52:55 +00:00
|
|
|
if (!NElems)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
|
|
|
|
// Restrict this transformation to only working on small allocations
|
|
|
|
// (2048 bytes currently), as we don't want to introduce a 16M global or
|
|
|
|
// something.
|
|
|
|
if (NElements->getZExtValue() * TD->getTypeAllocSize(AllocTy) < 2048) {
|
2012-02-12 01:13:18 +00:00
|
|
|
GVI = OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElements, TD, TLI);
|
2010-04-14 20:52:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-04-14 20:52:55 +00:00
|
|
|
// If the allocation is an array of structures, consider transforming this
|
|
|
|
// into multiple malloc'd arrays, one for each field. This is basically
|
|
|
|
// SRoA for malloc'd memory.
|
|
|
|
|
2012-02-05 19:56:38 +00:00
|
|
|
if (Ordering != NotAtomic)
|
|
|
|
return false;
|
|
|
|
|
2010-04-14 20:52:55 +00:00
|
|
|
// If this is an allocation of a fixed size array of structs, analyze as a
|
|
|
|
// variable size array. malloc [100 x struct],1 -> malloc struct, 100
|
2010-06-24 14:42:01 +00:00
|
|
|
if (NElems == ConstantInt::get(CI->getArgOperand(0)->getType(), 1))
|
2011-07-18 04:54:35 +00:00
|
|
|
if (ArrayType *AT = dyn_cast<ArrayType>(AllocTy))
|
2010-04-14 20:52:55 +00:00
|
|
|
AllocTy = AT->getElementType();
|
2010-06-24 14:42:01 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
StructType *AllocSTy = dyn_cast<StructType>(AllocTy);
|
2010-04-14 20:52:55 +00:00
|
|
|
if (!AllocSTy)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// This the structure has an unreasonable number of fields, leave it
|
|
|
|
// alone.
|
|
|
|
if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 &&
|
|
|
|
AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) {
|
|
|
|
|
|
|
|
// If this is a fixed size array, transform the Malloc to be an alloc of
|
|
|
|
// structs. malloc [100 x struct],1 -> malloc struct, 100
|
2012-08-29 15:32:21 +00:00
|
|
|
if (ArrayType *AT = dyn_cast<ArrayType>(getMallocAllocatedType(CI, TLI))) {
|
2012-11-01 08:07:29 +00:00
|
|
|
Type *IntPtrTy = TD->getIntPtrType(CI->getContext());
|
2010-04-14 20:52:55 +00:00
|
|
|
unsigned TypeSize = TD->getStructLayout(AllocSTy)->getSizeInBytes();
|
|
|
|
Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize);
|
|
|
|
Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements());
|
|
|
|
Instruction *Malloc = CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy,
|
|
|
|
AllocSize, NumElements,
|
2010-07-12 00:57:28 +00:00
|
|
|
0, CI->getName());
|
2010-04-14 20:52:55 +00:00
|
|
|
Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI);
|
|
|
|
CI->replaceAllUsesWith(Cast);
|
|
|
|
CI->eraseFromParent();
|
2012-06-22 00:25:01 +00:00
|
|
|
if (BitCastInst *BCI = dyn_cast<BitCastInst>(Malloc))
|
|
|
|
CI = cast<CallInst>(BCI->getOperand(0));
|
|
|
|
else
|
2012-06-22 00:29:58 +00:00
|
|
|
CI = cast<CallInst>(Malloc);
|
2009-09-18 22:35:49 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2012-08-29 15:32:21 +00:00
|
|
|
GVI = PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, TD, TLI, true),
|
|
|
|
TD, TLI);
|
2010-04-14 20:52:55 +00:00
|
|
|
return true;
|
2009-09-18 22:35:49 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-09-18 22:35:49 +00:00
|
|
|
return false;
|
2010-10-18 21:16:00 +00:00
|
|
|
}
|
2009-09-18 22:35:49 +00:00
|
|
|
|
2004-10-09 21:48:45 +00:00
|
|
|
// OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge
|
|
|
|
// that only one value (besides its initializer) is ever stored to the global.
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
|
2012-02-05 19:56:38 +00:00
|
|
|
AtomicOrdering Ordering,
|
2006-08-27 22:42:52 +00:00
|
|
|
Module::global_iterator &GVI,
|
2012-10-08 16:38:25 +00:00
|
|
|
DataLayout *TD, TargetLibraryInfo *TLI) {
|
2008-12-15 21:20:32 +00:00
|
|
|
// Ignore no-op GEPs and bitcasts.
|
|
|
|
StoredOnceVal = StoredOnceVal->stripPointerCasts();
|
2004-10-09 21:48:45 +00:00
|
|
|
|
2004-10-10 23:14:11 +00:00
|
|
|
// If we are dealing with a pointer global that is initialized to null and
|
|
|
|
// only has one (non-null) value stored into it, then we can optimize any
|
|
|
|
// users of the loaded value (often calls and loads) that would trap if the
|
|
|
|
// value was null.
|
2010-02-16 11:11:14 +00:00
|
|
|
if (GV->getInitializer()->getType()->isPointerTy() &&
|
2004-10-09 21:48:45 +00:00
|
|
|
GV->getInitializer()->isNullValue()) {
|
2004-10-10 23:14:11 +00:00
|
|
|
if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
|
|
|
|
if (GV->getInitializer()->getType() != SOVC->getType())
|
2011-05-22 07:15:13 +00:00
|
|
|
SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType());
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-10-10 23:14:11 +00:00
|
|
|
// Optimize away any trapping uses of the loaded value.
|
2012-02-12 01:13:18 +00:00
|
|
|
if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, TD, TLI))
|
2004-10-10 17:07:12 +00:00
|
|
|
return true;
|
2012-08-29 15:32:21 +00:00
|
|
|
} else if (CallInst *CI = extractMallocCall(StoredOnceVal, TLI)) {
|
|
|
|
Type *MallocType = getMallocAllocatedType(CI, TLI);
|
2012-02-12 01:13:18 +00:00
|
|
|
if (MallocType &&
|
|
|
|
TryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType, Ordering, GVI,
|
|
|
|
TD, TLI))
|
2009-11-07 00:16:28 +00:00
|
|
|
return true;
|
2004-10-10 23:14:11 +00:00
|
|
|
}
|
2004-10-09 21:48:45 +00:00
|
|
|
}
|
This patch implements two things (sorry).
First, it allows SRA of globals that have embedded arrays, implementing
GlobalOpt/globalsra-partial.llx. This comes up infrequently, but does allow,
for example, deleting several stores to dead parts of globals in dhrystone.
Second, this implements GlobalOpt/malloc-promote-*.llx, which is the
following nifty transformation:
Basically if a global pointer is initialized with malloc, and we can tell
that the program won't notice, we transform this:
struct foo *FooPtr;
...
FooPtr = malloc(sizeof(struct foo));
...
FooPtr->A FooPtr->B
Into:
struct foo FooPtrBody;
...
FooPtrBody.A FooPtrBody.B
This comes up occasionally, for example, the 'disp' global in 183.equake (where
the xform speeds the CBE version of the program up from 56.16s to 52.40s (7%)
on apoc), and the 'desired_accept', 'fixLRBT', 'macroArray', & 'key_queue'
globals in 300.twolf (speeding it up from 22.29s to 21.55s (3.4%)).
The nice thing about this xform is that it exposes the resulting global to
global variable optimization and makes alias analysis easier in addition to
eliminating a few loads.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16916 91177308-0d34-0410-b5e6-96231b3b80d8
2004-10-11 05:54:41 +00:00
|
|
|
|
2004-10-09 21:48:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2004-10-08 20:59:28 +00:00
|
|
|
|
2008-01-14 01:17:44 +00:00
|
|
|
/// TryToShrinkGlobalToBoolean - At this point, we have learned that the only
|
|
|
|
/// two values ever stored into GV are its initializer and OtherVal. See if we
|
|
|
|
/// can shrink the global into a boolean and select between the two values
|
|
|
|
/// whenever it is used. This exposes the values to other scalar optimizations.
|
2009-11-06 04:27:31 +00:00
|
|
|
static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *GVElType = GV->getType()->getElementType();
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2008-01-14 01:17:44 +00:00
|
|
|
// If GVElType is already i1, it is already shrunk. If the type of the GV is
|
2009-03-07 23:32:02 +00:00
|
|
|
// an FP value, pointer or vector, don't do this optimization because a select
|
|
|
|
// between them is very expensive and unlikely to lead to later
|
|
|
|
// simplification. In these cases, we typically end up with "cond ? v1 : v2"
|
|
|
|
// where v1 and v2 both require constant pool loads, a big loss.
|
2009-11-06 04:27:31 +00:00
|
|
|
if (GVElType == Type::getInt1Ty(GV->getContext()) ||
|
2010-02-15 16:12:20 +00:00
|
|
|
GVElType->isFloatingPointTy() ||
|
2010-02-16 11:11:14 +00:00
|
|
|
GVElType->isPointerTy() || GVElType->isVectorTy())
|
2008-01-14 01:17:44 +00:00
|
|
|
return false;
|
2010-07-12 14:13:15 +00:00
|
|
|
|
2008-01-14 01:17:44 +00:00
|
|
|
// Walk the use list of the global seeing if all the uses are load or store.
|
|
|
|
// If there is anything else, bail out.
|
2010-07-12 14:13:15 +00:00
|
|
|
for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){
|
|
|
|
User *U = *I;
|
|
|
|
if (!isa<LoadInst>(U) && !isa<StoreInst>(U))
|
2008-01-14 01:17:44 +00:00
|
|
|
return false;
|
2010-07-12 14:13:15 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 01:28:05 +00:00
|
|
|
DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2004-12-12 05:53:50 +00:00
|
|
|
// Create the new global, initializing it to false.
|
2009-11-06 04:27:31 +00:00
|
|
|
GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()),
|
|
|
|
false,
|
2010-10-18 21:16:00 +00:00
|
|
|
GlobalValue::InternalLinkage,
|
2009-11-06 04:27:31 +00:00
|
|
|
ConstantInt::getFalse(GV->getContext()),
|
2009-05-03 03:49:08 +00:00
|
|
|
GV->getName()+".b",
|
2013-01-10 10:31:11 +00:00
|
|
|
GV->getThreadLocalMode(),
|
|
|
|
GV->getType()->getAddressSpace());
|
2004-12-12 05:53:50 +00:00
|
|
|
GV->getParent()->getGlobalList().insert(GV, NewGV);
|
|
|
|
|
|
|
|
Constant *InitVal = GV->getInitializer();
|
2009-11-06 04:27:31 +00:00
|
|
|
assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&
|
2009-08-13 21:58:54 +00:00
|
|
|
"No reason to shrink to bool!");
|
2004-12-12 05:53:50 +00:00
|
|
|
|
|
|
|
// If initialized to zero and storing one into the global, we can use a cast
|
|
|
|
// instead of a select to synthesize the desired value.
|
|
|
|
bool IsOneZero = false;
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal))
|
2007-03-02 00:28:52 +00:00
|
|
|
IsOneZero = InitVal->isNullValue() && CI->isOne();
|
2004-12-12 05:53:50 +00:00
|
|
|
|
|
|
|
while (!GV->use_empty()) {
|
2009-03-06 01:39:36 +00:00
|
|
|
Instruction *UI = cast<Instruction>(GV->use_back());
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
|
2004-12-12 05:53:50 +00:00
|
|
|
// Change the store into a boolean store.
|
|
|
|
bool StoringOther = SI->getOperand(0) == OtherVal;
|
|
|
|
// Only do this if we weren't storing a loaded value.
|
2004-12-12 19:34:41 +00:00
|
|
|
Value *StoreVal;
|
2013-02-13 23:00:51 +00:00
|
|
|
if (StoringOther || SI->getOperand(0) == InitVal) {
|
2009-11-06 04:27:31 +00:00
|
|
|
StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()),
|
|
|
|
StoringOther);
|
2013-02-13 23:00:51 +00:00
|
|
|
} else {
|
2004-12-12 19:34:41 +00:00
|
|
|
// Otherwise, we are storing a previously loaded copy. To do this,
|
|
|
|
// change the copy from copying the original value to just copying the
|
|
|
|
// bool.
|
|
|
|
Instruction *StoredVal = cast<Instruction>(SI->getOperand(0));
|
|
|
|
|
2010-06-24 14:42:01 +00:00
|
|
|
// If we've already replaced the input, StoredVal will be a cast or
|
2004-12-12 19:34:41 +00:00
|
|
|
// select instruction. If not, it will be a load of the original
|
|
|
|
// global.
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
|
|
|
|
assert(LI->getOperand(0) == GV && "Not a copy!");
|
|
|
|
// Insert a new load, to preserve the saved value.
|
2012-02-05 19:56:38 +00:00
|
|
|
StoreVal = new LoadInst(NewGV, LI->getName()+".b", false, 0,
|
|
|
|
LI->getOrdering(), LI->getSynchScope(), LI);
|
2004-12-12 19:34:41 +00:00
|
|
|
} else {
|
|
|
|
assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&
|
|
|
|
"This is not a form that we understand!");
|
|
|
|
StoreVal = StoredVal->getOperand(0);
|
|
|
|
assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!");
|
|
|
|
}
|
|
|
|
}
|
2012-02-05 19:56:38 +00:00
|
|
|
new StoreInst(StoreVal, NewGV, false, 0,
|
|
|
|
SI->getOrdering(), SI->getSynchScope(), SI);
|
2009-03-06 01:39:36 +00:00
|
|
|
} else {
|
2004-12-12 05:53:50 +00:00
|
|
|
// Change the load into a load of bool then a select.
|
2009-03-06 01:39:36 +00:00
|
|
|
LoadInst *LI = cast<LoadInst>(UI);
|
2012-02-05 19:56:38 +00:00
|
|
|
LoadInst *NLI = new LoadInst(NewGV, LI->getName()+".b", false, 0,
|
|
|
|
LI->getOrdering(), LI->getSynchScope(), LI);
|
2004-12-12 05:53:50 +00:00
|
|
|
Value *NSI;
|
|
|
|
if (IsOneZero)
|
2007-02-11 01:08:35 +00:00
|
|
|
NSI = new ZExtInst(NLI, LI->getType(), "", LI);
|
2005-04-21 23:48:37 +00:00
|
|
|
else
|
2008-04-06 20:25:17 +00:00
|
|
|
NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI);
|
2007-02-11 01:08:35 +00:00
|
|
|
NSI->takeName(LI);
|
2004-12-12 05:53:50 +00:00
|
|
|
LI->replaceAllUsesWith(NSI);
|
2009-03-06 01:39:36 +00:00
|
|
|
}
|
|
|
|
UI->eraseFromParent();
|
2004-12-12 05:53:50 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 23:00:51 +00:00
|
|
|
// Retain the name of the old global variable. People who are debugging their
|
|
|
|
// programs may expect these variables to be named the same.
|
|
|
|
NewGV->takeName(GV);
|
2004-12-12 05:53:50 +00:00
|
|
|
GV->eraseFromParent();
|
2008-01-14 01:17:44 +00:00
|
|
|
return true;
|
2004-12-12 05:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-12 00:52:26 +00:00
|
|
|
/// ProcessGlobal - Analyze the specified global variable and optimize it if
|
|
|
|
/// possible. If we make a change, return true.
|
2011-01-19 16:32:21 +00:00
|
|
|
bool GlobalOpt::ProcessGlobal(GlobalVariable *GV,
|
|
|
|
Module::global_iterator &GVI) {
|
2012-06-14 22:48:13 +00:00
|
|
|
if (!GV->isDiscardableIfUnused())
|
2011-01-19 16:32:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Do more involved optimizations if the global is internal.
|
2004-10-08 20:59:28 +00:00
|
|
|
GV->removeDeadConstantUsers();
|
|
|
|
|
|
|
|
if (GV->use_empty()) {
|
2010-01-05 01:28:05 +00:00
|
|
|
DEBUG(dbgs() << "GLOBAL DEAD: " << *GV);
|
2004-10-16 18:09:00 +00:00
|
|
|
GV->eraseFromParent();
|
2004-10-08 20:59:28 +00:00
|
|
|
++NumDeleted;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-15 18:00:24 +00:00
|
|
|
if (!GV->hasLocalLinkage())
|
|
|
|
return false;
|
|
|
|
|
2011-01-19 16:32:21 +00:00
|
|
|
SmallPtrSet<const PHINode*, 16> PHIUsers;
|
|
|
|
GlobalStatus GS;
|
|
|
|
|
2011-01-18 04:36:06 +00:00
|
|
|
if (AnalyzeGlobal(GV, GS, PHIUsers))
|
|
|
|
return false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2011-01-19 16:32:21 +00:00
|
|
|
if (!GS.isCompared && !GV->hasUnnamedAddr()) {
|
|
|
|
GV->setUnnamedAddr(true);
|
|
|
|
NumUnnamed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GV->isConstant() || !GV->hasInitializer())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ProcessInternalGlobal(GV, GVI, PHIUsers, GS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ProcessInternalGlobal - Analyze the specified global variable and optimize
|
|
|
|
/// it if possible. If we make a change, return true.
|
|
|
|
bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
|
|
|
|
Module::global_iterator &GVI,
|
2012-02-12 00:52:26 +00:00
|
|
|
const SmallPtrSet<const PHINode*, 16> &PHIUsers,
|
2011-01-19 16:32:21 +00:00
|
|
|
const GlobalStatus &GS) {
|
2011-01-18 04:36:06 +00:00
|
|
|
// If this is a first class global and has only one accessing function
|
|
|
|
// and this function is main (which we know is not recursive we can make
|
|
|
|
// this global a local variable) we replace the global with a local alloca
|
|
|
|
// in this function.
|
|
|
|
//
|
|
|
|
// NOTE: It doesn't make sense to promote non single-value types since we
|
|
|
|
// are just replacing static memory to stack memory.
|
|
|
|
//
|
|
|
|
// If the global is in different address space, don't bring it to stack.
|
|
|
|
if (!GS.HasMultipleAccessingFunctions &&
|
|
|
|
GS.AccessingFunction && !GS.HasNonInstructionUser &&
|
|
|
|
GV->getType()->getElementType()->isSingleValueType() &&
|
|
|
|
GS.AccessingFunction->getName() == "main" &&
|
|
|
|
GS.AccessingFunction->hasExternalLinkage() &&
|
|
|
|
GV->getType()->getAddressSpace() == 0) {
|
|
|
|
DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV);
|
2012-02-05 19:48:37 +00:00
|
|
|
Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction
|
2011-01-18 04:36:06 +00:00
|
|
|
->getEntryBlock().begin());
|
2012-02-05 19:48:37 +00:00
|
|
|
Type *ElemTy = GV->getType()->getElementType();
|
2011-01-18 04:36:06 +00:00
|
|
|
// FIXME: Pass Global's alignment when globals have alignment
|
2012-02-05 19:48:37 +00:00
|
|
|
AllocaInst *Alloca = new AllocaInst(ElemTy, NULL, GV->getName(), &FirstI);
|
2011-01-18 04:36:06 +00:00
|
|
|
if (!isa<UndefValue>(GV->getInitializer()))
|
|
|
|
new StoreInst(GV->getInitializer(), Alloca, &FirstI);
|
|
|
|
|
|
|
|
GV->replaceAllUsesWith(Alloca);
|
|
|
|
GV->eraseFromParent();
|
|
|
|
++NumLocalized;
|
|
|
|
return true;
|
|
|
|
}
|
2004-10-09 03:32:52 +00:00
|
|
|
|
2011-01-18 04:36:06 +00:00
|
|
|
// If the global is never loaded (but may be stored to), it is dead.
|
|
|
|
// Delete it now.
|
|
|
|
if (!GS.isLoaded) {
|
|
|
|
DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV);
|
2004-10-09 03:32:52 +00:00
|
|
|
|
2012-07-24 07:21:08 +00:00
|
|
|
bool Changed;
|
|
|
|
if (isLeakCheckerRoot(GV)) {
|
|
|
|
// Delete any constant stores to the global.
|
2012-08-29 15:32:21 +00:00
|
|
|
Changed = CleanupPointerRootUsers(GV, TLI);
|
2012-07-24 07:21:08 +00:00
|
|
|
} else {
|
|
|
|
// Delete any stores we can find to the global. We may not be able to
|
|
|
|
// make it completely dead though.
|
|
|
|
Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(), TD, TLI);
|
|
|
|
}
|
2011-01-18 04:36:06 +00:00
|
|
|
|
|
|
|
// If the global is dead now, delete it.
|
|
|
|
if (GV->use_empty()) {
|
|
|
|
GV->eraseFromParent();
|
|
|
|
++NumDeleted;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
|
|
|
|
} else if (GS.StoredType <= GlobalStatus::isInitializerStored) {
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n");
|
2011-01-18 04:36:06 +00:00
|
|
|
GV->setConstant(true);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2011-01-18 04:36:06 +00:00
|
|
|
// Clean up any obviously simplifiable users now.
|
2012-02-12 01:13:18 +00:00
|
|
|
CleanupConstantGlobalUsers(GV, GV->getInitializer(), TD, TLI);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2011-01-18 04:36:06 +00:00
|
|
|
// If the global is dead now, just nuke it.
|
|
|
|
if (GV->use_empty()) {
|
|
|
|
DEBUG(dbgs() << " *** Marking constant allowed us to simplify "
|
|
|
|
<< "all users and delete global!\n");
|
|
|
|
GV->eraseFromParent();
|
|
|
|
++NumDeleted;
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2011-01-18 04:36:06 +00:00
|
|
|
++NumMarked;
|
|
|
|
return true;
|
|
|
|
} else if (!GV->getInitializer()->getType()->isSingleValueType()) {
|
2012-10-08 16:38:25 +00:00
|
|
|
if (DataLayout *TD = getAnalysisIfAvailable<DataLayout>())
|
2011-01-18 04:36:06 +00:00
|
|
|
if (GlobalVariable *FirstNewGV = SRAGlobal(GV, *TD)) {
|
|
|
|
GVI = FirstNewGV; // Don't skip the newly produced globals!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (GS.StoredType == GlobalStatus::isStoredOnce) {
|
|
|
|
// If the initial value for the global was an undef value, and if only
|
|
|
|
// one other value was stored into it, we can just change the
|
|
|
|
// initializer to be the stored value, then delete all stores to the
|
|
|
|
// global. This allows us to mark it constant.
|
|
|
|
if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
|
|
|
|
if (isa<UndefValue>(GV->getInitializer())) {
|
|
|
|
// Change the initial value here.
|
|
|
|
GV->setInitializer(SOVConstant);
|
|
|
|
|
|
|
|
// Clean up any obviously simplifiable users now.
|
2012-02-12 01:13:18 +00:00
|
|
|
CleanupConstantGlobalUsers(GV, GV->getInitializer(), TD, TLI);
|
2011-01-18 04:36:06 +00:00
|
|
|
|
|
|
|
if (GV->use_empty()) {
|
|
|
|
DEBUG(dbgs() << " *** Substituting initializer allowed us to "
|
2012-07-24 07:21:08 +00:00
|
|
|
<< "simplify all users and delete global!\n");
|
2011-01-18 04:36:06 +00:00
|
|
|
GV->eraseFromParent();
|
|
|
|
++NumDeleted;
|
|
|
|
} else {
|
|
|
|
GVI = GV;
|
|
|
|
}
|
|
|
|
++NumSubstitute;
|
|
|
|
return true;
|
2004-10-08 20:59:28 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2011-01-18 04:36:06 +00:00
|
|
|
// Try to optimize globals based on the knowledge that only one value
|
|
|
|
// (besides its initializer) is ever stored to the global.
|
2012-02-05 19:56:38 +00:00
|
|
|
if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GS.Ordering, GVI,
|
2012-02-12 01:13:18 +00:00
|
|
|
TD, TLI))
|
2004-10-08 20:59:28 +00:00
|
|
|
return true;
|
2004-10-16 18:09:00 +00:00
|
|
|
|
2011-01-18 04:36:06 +00:00
|
|
|
// Otherwise, if the global was not a boolean, we can shrink it to be a
|
|
|
|
// boolean.
|
|
|
|
if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
|
|
|
|
if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {
|
|
|
|
++NumShrunkToBool;
|
2004-10-09 21:48:45 +00:00
|
|
|
return true;
|
2011-01-18 04:36:06 +00:00
|
|
|
}
|
2004-10-08 20:59:28 +00:00
|
|
|
}
|
2011-01-18 04:36:06 +00:00
|
|
|
|
2004-10-08 20:59:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-05-08 22:18:06 +00:00
|
|
|
/// ChangeCalleesToFastCall - Walk all of the direct calls of the specified
|
|
|
|
/// function, changing them to FastCC.
|
|
|
|
static void ChangeCalleesToFastCall(Function *F) {
|
|
|
|
for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){
|
2012-05-12 08:30:16 +00:00
|
|
|
if (isa<BlockAddress>(*UI))
|
|
|
|
continue;
|
2008-02-18 17:32:13 +00:00
|
|
|
CallSite User(cast<Instruction>(*UI));
|
|
|
|
User.setCallingConv(CallingConv::Fast);
|
2005-05-08 22:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
2004-10-08 20:59:28 +00:00
|
|
|
|
2012-12-07 23:16:57 +00:00
|
|
|
static AttributeSet StripNest(LLVMContext &C, const AttributeSet &Attrs) {
|
2008-03-12 17:45:29 +00:00
|
|
|
for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
|
2013-01-25 23:09:36 +00:00
|
|
|
unsigned Index = Attrs.getSlotIndex(i);
|
|
|
|
if (!Attrs.getSlotAttributes(i).hasAttribute(Index, Attribute::Nest))
|
2008-02-18 17:32:13 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// There can be only one.
|
2013-01-25 23:09:36 +00:00
|
|
|
return Attrs.removeAttribute(C, Index, Attribute::Nest);
|
2008-02-16 20:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RemoveNestAttribute(Function *F) {
|
2012-10-14 06:39:53 +00:00
|
|
|
F->setAttributes(StripNest(F->getContext(), F->getAttributes()));
|
2008-02-16 20:56:04 +00:00
|
|
|
for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){
|
2012-05-12 08:30:16 +00:00
|
|
|
if (isa<BlockAddress>(*UI))
|
|
|
|
continue;
|
2008-02-18 17:32:13 +00:00
|
|
|
CallSite User(cast<Instruction>(*UI));
|
2012-10-14 06:39:53 +00:00
|
|
|
User.setAttributes(StripNest(F->getContext(), User.getAttributes()));
|
2008-02-16 20:56:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
bool GlobalOpt::OptimizeFunctions(Module &M) {
|
2004-02-25 21:34:36 +00:00
|
|
|
bool Changed = false;
|
2005-09-26 01:43:45 +00:00
|
|
|
// Optimize functions.
|
|
|
|
for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) {
|
|
|
|
Function *F = FI++;
|
2009-03-06 10:21:56 +00:00
|
|
|
// Functions without names cannot be referenced outside this module.
|
|
|
|
if (!F->hasName() && !F->isDeclaration())
|
|
|
|
F->setLinkage(GlobalValue::InternalLinkage);
|
2005-09-26 01:43:45 +00:00
|
|
|
F->removeDeadConstantUsers();
|
2011-10-20 05:23:42 +00:00
|
|
|
if (F->isDefTriviallyDead()) {
|
2009-11-01 19:03:42 +00:00
|
|
|
F->eraseFromParent();
|
2005-09-26 01:43:45 +00:00
|
|
|
Changed = true;
|
|
|
|
++NumFnDeleted;
|
2009-01-15 20:18:42 +00:00
|
|
|
} else if (F->hasLocalLinkage()) {
|
2008-02-16 20:56:04 +00:00
|
|
|
if (F->getCallingConv() == CallingConv::C && !F->isVarArg() &&
|
2009-06-10 08:41:11 +00:00
|
|
|
!F->hasAddressTaken()) {
|
2008-02-16 20:56:04 +00:00
|
|
|
// If this function has C calling conventions, is not a varargs
|
|
|
|
// function, and is only called directly, promote it to use the Fast
|
|
|
|
// calling convention.
|
|
|
|
F->setCallingConv(CallingConv::Fast);
|
|
|
|
ChangeCalleesToFastCall(F);
|
|
|
|
++NumFastCallFns;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2012-12-19 07:18:57 +00:00
|
|
|
if (F->getAttributes().hasAttrSomewhere(Attribute::Nest) &&
|
2009-06-10 08:41:11 +00:00
|
|
|
!F->hasAddressTaken()) {
|
2008-02-16 20:56:04 +00:00
|
|
|
// The function is not used by a trampoline intrinsic, so it is safe
|
|
|
|
// to remove the 'nest' attribute.
|
|
|
|
RemoveNestAttribute(F);
|
|
|
|
++NumNestRemoved;
|
|
|
|
Changed = true;
|
|
|
|
}
|
2005-09-26 01:43:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
2004-10-07 04:16:33 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
bool GlobalOpt::OptimizeGlobalVars(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
|
|
|
|
GVI != E; ) {
|
|
|
|
GlobalVariable *GV = GVI++;
|
2009-03-06 10:21:56 +00:00
|
|
|
// Global variables without names cannot be referenced outside this module.
|
|
|
|
if (!GV->hasName() && !GV->isDeclaration())
|
|
|
|
GV->setLinkage(GlobalValue::InternalLinkage);
|
2009-11-23 16:22:21 +00:00
|
|
|
// Simplify the initializer.
|
|
|
|
if (GV->hasInitializer())
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GV->getInitializer())) {
|
2011-12-02 01:26:24 +00:00
|
|
|
Constant *New = ConstantFoldConstantExpression(CE, TD, TLI);
|
2009-11-23 16:22:21 +00:00
|
|
|
if (New && New != CE)
|
|
|
|
GV->setInitializer(New);
|
|
|
|
}
|
2011-01-19 16:32:21 +00:00
|
|
|
|
|
|
|
Changed |= ProcessGlobal(GV, GVI);
|
2005-09-26 01:43:45 +00:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2011-04-08 07:30:21 +00:00
|
|
|
/// FindGlobalCtors - Find the llvm.global_ctors list, verifying that all
|
2005-09-26 01:43:45 +00:00
|
|
|
/// initializers have an init priority of 65535.
|
|
|
|
GlobalVariable *GlobalOpt::FindGlobalCtors(Module &M) {
|
2010-12-06 21:53:07 +00:00
|
|
|
GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
|
|
|
|
if (GV == 0) return 0;
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2010-12-06 21:53:07 +00:00
|
|
|
// Verify that the initializer is simple enough for us to handle. We are
|
|
|
|
// only allowed to optimize the initializer if it is unique.
|
|
|
|
if (!GV->hasUniqueInitializer()) return 0;
|
2011-04-09 09:11:09 +00:00
|
|
|
|
2011-04-11 22:11:20 +00:00
|
|
|
if (isa<ConstantAggregateZero>(GV->getInitializer()))
|
|
|
|
return GV;
|
|
|
|
ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
|
2011-04-09 09:11:09 +00:00
|
|
|
|
2011-04-11 22:11:20 +00:00
|
|
|
for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
|
|
|
|
if (isa<ConstantAggregateZero>(*i))
|
|
|
|
continue;
|
|
|
|
ConstantStruct *CS = cast<ConstantStruct>(*i);
|
2010-12-06 21:53:07 +00:00
|
|
|
if (isa<ConstantPointerNull>(CS->getOperand(1)))
|
|
|
|
continue;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-12-06 21:53:07 +00:00
|
|
|
// Must have a function or null ptr.
|
|
|
|
if (!isa<Function>(CS->getOperand(1)))
|
|
|
|
return 0;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-12-06 21:53:07 +00:00
|
|
|
// Init priority must be standard.
|
2011-04-08 07:30:21 +00:00
|
|
|
ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
|
|
|
|
if (CI->getZExtValue() != 65535)
|
2010-12-06 21:53:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GV;
|
2005-09-26 01:43:45 +00:00
|
|
|
}
|
|
|
|
|
2005-09-26 02:31:18 +00:00
|
|
|
/// ParseGlobalCtors - Given a llvm.global_ctors list that we can understand,
|
|
|
|
/// return a list of the functions and null terminator as a vector.
|
2005-09-26 01:43:45 +00:00
|
|
|
static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) {
|
2011-04-11 22:11:20 +00:00
|
|
|
if (GV->getInitializer()->isNullValue())
|
|
|
|
return std::vector<Function*>();
|
2005-09-26 01:43:45 +00:00
|
|
|
ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
|
|
|
|
std::vector<Function*> Result;
|
|
|
|
Result.reserve(CA->getNumOperands());
|
2008-05-29 01:59:18 +00:00
|
|
|
for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
|
|
|
|
ConstantStruct *CS = cast<ConstantStruct>(*i);
|
2005-09-26 01:43:45 +00:00
|
|
|
Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2005-09-26 02:31:18 +00:00
|
|
|
/// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the
|
|
|
|
/// specified array, returning the new global to use.
|
2010-10-18 21:16:00 +00:00
|
|
|
static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
|
2009-11-06 04:27:31 +00:00
|
|
|
const std::vector<Function*> &Ctors) {
|
2005-09-26 02:31:18 +00:00
|
|
|
// If we made a change, reassemble the initializer list.
|
2011-06-20 04:01:31 +00:00
|
|
|
Constant *CSVals[2];
|
|
|
|
CSVals[0] = ConstantInt::get(Type::getInt32Ty(GCL->getContext()), 65535);
|
|
|
|
CSVals[1] = 0;
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
StructType *StructTy =
|
2011-06-20 04:01:31 +00:00
|
|
|
cast <StructType>(
|
|
|
|
cast<ArrayType>(GCL->getType()->getElementType())->getElementType());
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
// Create the new init list.
|
|
|
|
std::vector<Constant*> CAList;
|
|
|
|
for (unsigned i = 0, e = Ctors.size(); i != e; ++i) {
|
2005-09-26 04:44:35 +00:00
|
|
|
if (Ctors[i]) {
|
2005-09-26 01:43:45 +00:00
|
|
|
CSVals[1] = Ctors[i];
|
2005-09-26 04:44:35 +00:00
|
|
|
} else {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *FTy = FunctionType::get(Type::getVoidTy(GCL->getContext()),
|
2009-11-06 04:27:31 +00:00
|
|
|
false);
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType *PFTy = PointerType::getUnqual(FTy);
|
2009-07-31 20:28:14 +00:00
|
|
|
CSVals[1] = Constant::getNullValue(PFTy);
|
2009-11-06 04:27:31 +00:00
|
|
|
CSVals[0] = ConstantInt::get(Type::getInt32Ty(GCL->getContext()),
|
2011-04-11 22:11:20 +00:00
|
|
|
0x7fffffff);
|
2005-09-26 01:43:45 +00:00
|
|
|
}
|
2011-06-20 04:01:31 +00:00
|
|
|
CAList.push_back(ConstantStruct::get(StructTy, CSVals));
|
2004-10-07 04:16:33 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
// Create the array initializer.
|
2010-10-18 21:16:00 +00:00
|
|
|
Constant *CA = ConstantArray::get(ArrayType::get(StructTy,
|
2009-09-19 20:30:26 +00:00
|
|
|
CAList.size()), CAList);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 02:31:18 +00:00
|
|
|
// If we didn't change the number of elements, don't create a new GV.
|
|
|
|
if (CA->getType() == GCL->getInitializer()->getType()) {
|
|
|
|
GCL->setInitializer(CA);
|
|
|
|
return GCL;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
// Create the new global and insert it next to the existing list.
|
2009-11-06 04:27:31 +00:00
|
|
|
GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(),
|
2007-04-12 18:32:50 +00:00
|
|
|
GCL->getLinkage(), CA, "",
|
2012-06-23 11:37:03 +00:00
|
|
|
GCL->getThreadLocalMode());
|
2005-09-26 01:43:45 +00:00
|
|
|
GCL->getParent()->getGlobalList().insert(GCL, NGV);
|
2007-02-11 01:08:35 +00:00
|
|
|
NGV->takeName(GCL);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
// Nuke the old list, replacing any uses with the new one.
|
|
|
|
if (!GCL->use_empty()) {
|
|
|
|
Constant *V = NGV;
|
|
|
|
if (V->getType() != GCL->getType())
|
2009-07-29 18:55:55 +00:00
|
|
|
V = ConstantExpr::getBitCast(V, GCL->getType());
|
2005-09-26 01:43:45 +00:00
|
|
|
GCL->replaceAllUsesWith(V);
|
|
|
|
}
|
|
|
|
GCL->eraseFromParent();
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
if (Ctors.size())
|
2005-09-26 02:31:18 +00:00
|
|
|
return NGV;
|
2005-09-26 01:43:45 +00:00
|
|
|
else
|
2005-09-26 02:31:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-09-26 04:44:35 +00:00
|
|
|
|
|
|
|
|
2012-12-06 21:57:16 +00:00
|
|
|
static inline bool
|
2010-12-07 04:33:29 +00:00
|
|
|
isSimpleEnoughValueToCommit(Constant *C,
|
2012-01-05 23:03:32 +00:00
|
|
|
SmallPtrSet<Constant*, 8> &SimpleConstants,
|
2012-10-08 16:38:25 +00:00
|
|
|
const DataLayout *TD);
|
2010-12-07 04:33:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// isSimpleEnoughValueToCommit - Return true if the specified constant can be
|
|
|
|
/// handled by the code generator. We don't want to generate something like:
|
|
|
|
/// void *X = &X/42;
|
|
|
|
/// because the code generator doesn't have a relocation that can handle that.
|
|
|
|
///
|
|
|
|
/// This function should be called if C was not found (but just got inserted)
|
|
|
|
/// in SimpleConstants to avoid having to rescan the same constants all the
|
|
|
|
/// time.
|
|
|
|
static bool isSimpleEnoughValueToCommitHelper(Constant *C,
|
2012-01-05 23:03:32 +00:00
|
|
|
SmallPtrSet<Constant*, 8> &SimpleConstants,
|
2012-10-08 16:38:25 +00:00
|
|
|
const DataLayout *TD) {
|
2010-12-07 04:33:29 +00:00
|
|
|
// Simple integer, undef, constant aggregate zero, global addresses, etc are
|
|
|
|
// all supported.
|
|
|
|
if (C->getNumOperands() == 0 || isa<BlockAddress>(C) ||
|
|
|
|
isa<GlobalValue>(C))
|
|
|
|
return true;
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2010-12-07 04:33:29 +00:00
|
|
|
// Aggregate values are safe if all their elements are.
|
|
|
|
if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
|
|
|
|
isa<ConstantVector>(C)) {
|
|
|
|
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
|
|
|
|
Constant *Op = cast<Constant>(C->getOperand(i));
|
2012-01-05 23:03:32 +00:00
|
|
|
if (!isSimpleEnoughValueToCommit(Op, SimpleConstants, TD))
|
2010-12-07 04:33:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2010-12-07 04:33:29 +00:00
|
|
|
// We don't know exactly what relocations are allowed in constant expressions,
|
|
|
|
// so we allow &global+constantoffset, which is safe and uniformly supported
|
|
|
|
// across targets.
|
|
|
|
ConstantExpr *CE = cast<ConstantExpr>(C);
|
|
|
|
switch (CE->getOpcode()) {
|
|
|
|
case Instruction::BitCast:
|
2012-01-05 23:03:32 +00:00
|
|
|
// Bitcast is fine if the casted value is fine.
|
|
|
|
return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD);
|
|
|
|
|
2010-12-07 04:33:29 +00:00
|
|
|
case Instruction::IntToPtr:
|
|
|
|
case Instruction::PtrToInt:
|
2012-01-05 23:03:32 +00:00
|
|
|
// int <=> ptr is fine if the int type is the same size as the
|
|
|
|
// pointer type.
|
|
|
|
if (!TD || TD->getTypeSizeInBits(CE->getType()) !=
|
|
|
|
TD->getTypeSizeInBits(CE->getOperand(0)->getType()))
|
|
|
|
return false;
|
|
|
|
return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD);
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2010-12-07 04:33:29 +00:00
|
|
|
// GEP is fine if it is simple + constant offset.
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
|
|
|
|
if (!isa<ConstantInt>(CE->getOperand(i)))
|
|
|
|
return false;
|
2012-01-05 23:03:32 +00:00
|
|
|
return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD);
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2010-12-07 04:33:29 +00:00
|
|
|
case Instruction::Add:
|
|
|
|
// We allow simple+cst.
|
|
|
|
if (!isa<ConstantInt>(CE->getOperand(1)))
|
|
|
|
return false;
|
2012-01-05 23:03:32 +00:00
|
|
|
return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD);
|
2010-12-07 04:33:29 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-06 21:57:16 +00:00
|
|
|
static inline bool
|
2010-12-07 04:33:29 +00:00
|
|
|
isSimpleEnoughValueToCommit(Constant *C,
|
2012-01-05 23:03:32 +00:00
|
|
|
SmallPtrSet<Constant*, 8> &SimpleConstants,
|
2012-10-08 16:38:25 +00:00
|
|
|
const DataLayout *TD) {
|
2010-12-07 04:33:29 +00:00
|
|
|
// If we already checked this constant, we win.
|
|
|
|
if (!SimpleConstants.insert(C)) return true;
|
|
|
|
// Check the constant.
|
2012-01-05 23:03:32 +00:00
|
|
|
return isSimpleEnoughValueToCommitHelper(C, SimpleConstants, TD);
|
2010-12-07 04:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-26 04:44:35 +00:00
|
|
|
/// isSimpleEnoughPointerToCommit - Return true if this constant is simple
|
2011-01-14 22:19:20 +00:00
|
|
|
/// enough for us to understand. In particular, if it is a cast to anything
|
|
|
|
/// other than from one pointer type to another pointer type, we punt.
|
|
|
|
/// We basically just support direct accesses to globals and GEP's of
|
2005-09-26 04:44:35 +00:00
|
|
|
/// globals. This should be kept up to date with CommitValueTo.
|
2009-11-06 04:27:31 +00:00
|
|
|
static bool isSimpleEnoughPointerToCommit(Constant *C) {
|
2009-09-07 22:42:05 +00:00
|
|
|
// Conservatively, avoid aggregate types. This is because we don't
|
|
|
|
// want to worry about them partially overlapping other stores.
|
|
|
|
if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType())
|
|
|
|
return false;
|
|
|
|
|
2009-09-07 22:31:26 +00:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
|
2010-10-19 16:47:23 +00:00
|
|
|
// Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
|
2009-09-07 22:31:26 +00:00
|
|
|
// external globals.
|
2010-10-19 16:47:23 +00:00
|
|
|
return GV->hasUniqueInitializer();
|
2009-09-07 22:31:26 +00:00
|
|
|
|
2011-01-14 22:31:13 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
|
2005-09-26 06:52:44 +00:00
|
|
|
// Handle a constantexpr gep.
|
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr &&
|
2009-09-07 22:40:13 +00:00
|
|
|
isa<GlobalVariable>(CE->getOperand(0)) &&
|
|
|
|
cast<GEPOperator>(CE)->isInBounds()) {
|
2005-09-26 06:52:44 +00:00
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
|
2010-10-19 16:47:23 +00:00
|
|
|
// Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
|
2009-09-07 22:31:26 +00:00
|
|
|
// external globals.
|
2010-10-19 16:47:23 +00:00
|
|
|
if (!GV->hasUniqueInitializer())
|
2009-09-07 22:31:26 +00:00
|
|
|
return false;
|
2009-09-07 22:44:55 +00:00
|
|
|
|
|
|
|
// The first index must be zero.
|
2010-08-02 06:00:15 +00:00
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(*llvm::next(CE->op_begin()));
|
2009-09-07 22:44:55 +00:00
|
|
|
if (!CI || !CI->isZero()) return false;
|
|
|
|
|
|
|
|
// The remaining indices must be compile-time known integers within the
|
2009-09-10 23:37:55 +00:00
|
|
|
// notional bounds of the corresponding static array types.
|
|
|
|
if (!CE->isGEPWithNoNotionalOverIndexing())
|
|
|
|
return false;
|
2009-09-07 22:44:55 +00:00
|
|
|
|
2009-10-05 16:36:26 +00:00
|
|
|
return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE);
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2011-01-14 22:19:20 +00:00
|
|
|
// A constantexpr bitcast from a pointer to another pointer is a no-op,
|
|
|
|
// and we know how to evaluate it by moving the bitcast from the pointer
|
|
|
|
// operand to the value operand.
|
|
|
|
} else if (CE->getOpcode() == Instruction::BitCast &&
|
2011-01-16 02:05:10 +00:00
|
|
|
isa<GlobalVariable>(CE->getOperand(0))) {
|
2011-01-14 22:19:20 +00:00
|
|
|
// Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
|
|
|
|
// external globals.
|
2011-01-16 02:05:10 +00:00
|
|
|
return cast<GlobalVariable>(CE->getOperand(0))->hasUniqueInitializer();
|
2005-09-26 06:52:44 +00:00
|
|
|
}
|
2011-01-14 22:31:13 +00:00
|
|
|
}
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2005-09-26 04:44:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-09-26 06:52:44 +00:00
|
|
|
/// EvaluateStoreInto - Evaluate a piece of a constantexpr store into a global
|
|
|
|
/// initializer. This returns 'Init' modified to reflect 'Val' stored into it.
|
|
|
|
/// At this point, the GEP operands of Addr [0, OpNo) have been stepped into.
|
|
|
|
static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,
|
2012-12-01 10:54:28 +00:00
|
|
|
ConstantExpr *Addr, unsigned OpNo) {
|
2005-09-26 06:52:44 +00:00
|
|
|
// Base case of the recursion.
|
|
|
|
if (OpNo == Addr->getNumOperands()) {
|
|
|
|
assert(Val->getType() == Init->getType() && "Type mismatch!");
|
|
|
|
return Val;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2012-01-27 03:08:05 +00:00
|
|
|
SmallVector<Constant*, 32> Elts;
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(Init->getType())) {
|
2005-09-26 06:52:44 +00:00
|
|
|
// Break up the constant into its elements.
|
2012-01-26 02:32:04 +00:00
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
|
|
|
|
Elts.push_back(Init->getAggregateElement(i));
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 06:52:44 +00:00
|
|
|
// Replace the element that we are supposed to.
|
2006-10-20 07:07:24 +00:00
|
|
|
ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo));
|
|
|
|
unsigned Idx = CU->getZExtValue();
|
|
|
|
assert(Idx < STy->getNumElements() && "Struct index out of range!");
|
2012-12-01 10:54:28 +00:00
|
|
|
Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 06:52:44 +00:00
|
|
|
// Return the modified struct.
|
2011-06-20 04:01:31 +00:00
|
|
|
return ConstantStruct::get(STy, Elts);
|
|
|
|
}
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2011-06-20 04:01:31 +00:00
|
|
|
ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo));
|
2011-07-18 04:54:35 +00:00
|
|
|
SequentialType *InitTy = cast<SequentialType>(Init->getType());
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2011-06-20 04:01:31 +00:00
|
|
|
uint64_t NumElts;
|
2011-07-18 04:54:35 +00:00
|
|
|
if (ArrayType *ATy = dyn_cast<ArrayType>(InitTy))
|
2011-06-20 04:01:31 +00:00
|
|
|
NumElts = ATy->getNumElements();
|
|
|
|
else
|
2012-01-26 02:32:04 +00:00
|
|
|
NumElts = InitTy->getVectorNumElements();
|
2011-06-20 04:01:31 +00:00
|
|
|
|
|
|
|
// Break up the array into elements.
|
2012-01-26 02:32:04 +00:00
|
|
|
for (uint64_t i = 0, e = NumElts; i != e; ++i)
|
|
|
|
Elts.push_back(Init->getAggregateElement(i));
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2011-06-20 04:01:31 +00:00
|
|
|
assert(CI->getZExtValue() < NumElts);
|
|
|
|
Elts[CI->getZExtValue()] =
|
2012-12-01 10:54:28 +00:00
|
|
|
EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2011-06-20 04:01:31 +00:00
|
|
|
if (Init->getType()->isArrayTy())
|
|
|
|
return ConstantArray::get(cast<ArrayType>(InitTy), Elts);
|
|
|
|
return ConstantVector::get(Elts);
|
2005-09-26 06:52:44 +00:00
|
|
|
}
|
|
|
|
|
2005-09-26 04:44:35 +00:00
|
|
|
/// CommitValueTo - We have decided that Addr (which satisfies the predicate
|
|
|
|
/// isSimpleEnoughPointerToCommit) should get Val as its value. Make it happen.
|
2009-11-06 04:27:31 +00:00
|
|
|
static void CommitValueTo(Constant *Val, Constant *Addr) {
|
2005-09-26 06:52:44 +00:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
|
|
|
|
assert(GV->hasInitializer());
|
|
|
|
GV->setInitializer(Val);
|
|
|
|
return;
|
|
|
|
}
|
2010-01-07 01:16:21 +00:00
|
|
|
|
2005-09-26 06:52:44 +00:00
|
|
|
ConstantExpr *CE = cast<ConstantExpr>(Addr);
|
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
|
2012-12-01 10:54:28 +00:00
|
|
|
GV->setInitializer(EvaluateStoreInto(GV->getInitializer(), Val, CE, 2));
|
2005-09-26 04:44:35 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 03:25:59 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Evaluator - This class evaluates LLVM IR, producing the Constant
|
|
|
|
/// representing each SSA instruction. Changes to global variables are stored
|
|
|
|
/// in a mapping that can be iterated over after the evaluation is complete.
|
|
|
|
/// Once an evaluation call fails, the evaluation object should not be reused.
|
|
|
|
class Evaluator {
|
2012-02-19 23:26:27 +00:00
|
|
|
public:
|
2012-10-08 16:38:25 +00:00
|
|
|
Evaluator(const DataLayout *TD, const TargetLibraryInfo *TLI)
|
2012-02-19 23:26:27 +00:00
|
|
|
: TD(TD), TLI(TLI) {
|
|
|
|
ValueStack.push_back(new DenseMap<Value*, Constant*>);
|
|
|
|
}
|
|
|
|
|
2012-02-20 03:25:59 +00:00
|
|
|
~Evaluator() {
|
2012-02-19 23:26:27 +00:00
|
|
|
DeleteContainerPointers(ValueStack);
|
|
|
|
while (!AllocaTmps.empty()) {
|
|
|
|
GlobalVariable *Tmp = AllocaTmps.back();
|
|
|
|
AllocaTmps.pop_back();
|
|
|
|
|
|
|
|
// If there are still users of the alloca, the program is doing something
|
|
|
|
// silly, e.g. storing the address of the alloca somewhere and using it
|
|
|
|
// later. Since this is undefined, we'll just make it be null.
|
|
|
|
if (!Tmp->use_empty())
|
|
|
|
Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType()));
|
|
|
|
delete Tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EvaluateFunction - Evaluate a call to function F, returning true if
|
|
|
|
/// successful, false if we can't evaluate it. ActualArgs contains the formal
|
|
|
|
/// arguments for the function.
|
|
|
|
bool EvaluateFunction(Function *F, Constant *&RetVal,
|
|
|
|
const SmallVectorImpl<Constant*> &ActualArgs);
|
|
|
|
|
|
|
|
/// EvaluateBlock - Evaluate all instructions in block BB, returning true if
|
|
|
|
/// successful, false if we can't evaluate it. NewBB returns the next BB that
|
|
|
|
/// control flows into, or null upon return.
|
|
|
|
bool EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB);
|
|
|
|
|
|
|
|
Constant *getVal(Value *V) {
|
|
|
|
if (Constant *CV = dyn_cast<Constant>(V)) return CV;
|
|
|
|
Constant *R = ValueStack.back()->lookup(V);
|
|
|
|
assert(R && "Reference to an uncomputed value!");
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setVal(Value *V, Constant *C) {
|
|
|
|
ValueStack.back()->operator[](V) = C;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DenseMap<Constant*, Constant*> &getMutatedMemory() const {
|
|
|
|
return MutatedMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SmallPtrSet<GlobalVariable*, 8> &getInvariants() const {
|
|
|
|
return Invariants;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Constant *ComputeLoadResult(Constant *P);
|
|
|
|
|
|
|
|
/// ValueStack - As we compute SSA register values, we store their contents
|
|
|
|
/// here. The back of the vector contains the current function and the stack
|
|
|
|
/// contains the values in the calling frames.
|
|
|
|
SmallVector<DenseMap<Value*, Constant*>*, 4> ValueStack;
|
|
|
|
|
|
|
|
/// CallStack - This is used to detect recursion. In pathological situations
|
|
|
|
/// we could hit exponential behavior, but at least there is nothing
|
|
|
|
/// unbounded.
|
|
|
|
SmallVector<Function*, 4> CallStack;
|
|
|
|
|
|
|
|
/// MutatedMemory - For each store we execute, we update this map. Loads
|
|
|
|
/// check this to get the most up-to-date value. If evaluation is successful,
|
|
|
|
/// this state is committed to the process.
|
|
|
|
DenseMap<Constant*, Constant*> MutatedMemory;
|
|
|
|
|
|
|
|
/// AllocaTmps - To 'execute' an alloca, we create a temporary global variable
|
|
|
|
/// to represent its body. This vector is needed so we can delete the
|
|
|
|
/// temporary globals when we are done.
|
|
|
|
SmallVector<GlobalVariable*, 32> AllocaTmps;
|
|
|
|
|
|
|
|
/// Invariants - These global variables have been marked invariant by the
|
|
|
|
/// static constructor.
|
|
|
|
SmallPtrSet<GlobalVariable*, 8> Invariants;
|
|
|
|
|
|
|
|
/// SimpleConstants - These are constants we have checked and know to be
|
|
|
|
/// simple enough to live in a static initializer of a global.
|
|
|
|
SmallPtrSet<Constant*, 8> SimpleConstants;
|
|
|
|
|
2012-10-08 16:38:25 +00:00
|
|
|
const DataLayout *TD;
|
2012-02-19 23:26:27 +00:00
|
|
|
const TargetLibraryInfo *TLI;
|
|
|
|
};
|
|
|
|
|
2012-02-20 03:25:59 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2005-09-26 05:16:34 +00:00
|
|
|
/// ComputeLoadResult - Return the value that would be computed by a load from
|
|
|
|
/// P after the stores reflected by 'memory' have been performed. If we can't
|
|
|
|
/// decide, return null.
|
2012-02-20 03:25:59 +00:00
|
|
|
Constant *Evaluator::ComputeLoadResult(Constant *P) {
|
2005-09-26 05:15:37 +00:00
|
|
|
// If this memory location has been recently stored, use the stored value: it
|
|
|
|
// is the most up-to-date.
|
2012-02-19 23:26:27 +00:00
|
|
|
DenseMap<Constant*, Constant*>::const_iterator I = MutatedMemory.find(P);
|
|
|
|
if (I != MutatedMemory.end()) return I->second;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 05:15:37 +00:00
|
|
|
// Access it.
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) {
|
2009-08-19 18:20:44 +00:00
|
|
|
if (GV->hasDefinitiveInitializer())
|
2005-09-26 05:15:37 +00:00
|
|
|
return GV->getInitializer();
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 06:52:44 +00:00
|
|
|
// Handle a constantexpr getelementptr.
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P))
|
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr &&
|
|
|
|
isa<GlobalVariable>(CE->getOperand(0))) {
|
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
|
2009-08-19 18:20:44 +00:00
|
|
|
if (GV->hasDefinitiveInitializer())
|
2009-10-05 16:36:26 +00:00
|
|
|
return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE);
|
2005-09-26 06:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0; // don't know how to evaluate.
|
2005-09-26 05:15:37 +00:00
|
|
|
}
|
|
|
|
|
2012-02-06 08:24:44 +00:00
|
|
|
/// EvaluateBlock - Evaluate all instructions in block BB, returning true if
|
|
|
|
/// successful, false if we can't evaluate it. NewBB returns the next BB that
|
|
|
|
/// control flows into, or null upon return.
|
2012-02-20 03:25:59 +00:00
|
|
|
bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst,
|
|
|
|
BasicBlock *&NextBB) {
|
2005-09-26 04:44:35 +00:00
|
|
|
// This is the main evaluation loop.
|
|
|
|
while (1) {
|
|
|
|
Constant *InstResult = 0;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Evaluating Instruction: " << *CurInst << "\n");
|
|
|
|
|
2005-09-26 04:44:35 +00:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) {
|
2013-01-11 20:07:53 +00:00
|
|
|
if (!SI->isSimple()) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Store is not simple! Can not evaluate.\n");
|
|
|
|
return false; // no volatile/atomic accesses.
|
2013-01-11 20:07:53 +00:00
|
|
|
}
|
2012-02-19 23:26:27 +00:00
|
|
|
Constant *Ptr = getVal(SI->getOperand(1));
|
2013-01-11 20:07:53 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Folding constant ptr expression: " << *Ptr);
|
2012-02-21 22:08:06 +00:00
|
|
|
Ptr = ConstantFoldConstantExpression(CE, TD, TLI);
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "; To: " << *Ptr << "\n");
|
2013-01-11 20:07:53 +00:00
|
|
|
}
|
|
|
|
if (!isSimpleEnoughPointerToCommit(Ptr)) {
|
2005-09-26 04:44:35 +00:00
|
|
|
// If this is too complex for us to commit, reject it.
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Pointer is too complex for us to evaluate store.");
|
2005-09-27 04:45:34 +00:00
|
|
|
return false;
|
2013-01-11 20:07:53 +00:00
|
|
|
}
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2012-02-19 23:26:27 +00:00
|
|
|
Constant *Val = getVal(SI->getOperand(0));
|
2010-12-07 04:33:29 +00:00
|
|
|
|
|
|
|
// If this might be too difficult for the backend to handle (e.g. the addr
|
|
|
|
// of one global variable divided by another) then we can't commit it.
|
2013-01-11 20:07:53 +00:00
|
|
|
if (!isSimpleEnoughValueToCommit(Val, SimpleConstants, TD)) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Store value is too complex to evaluate store. " << *Val
|
|
|
|
<< "\n");
|
2010-12-07 04:33:29 +00:00
|
|
|
return false;
|
2013-01-11 20:07:53 +00:00
|
|
|
}
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2013-01-11 20:07:53 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
|
2011-01-14 22:19:20 +00:00
|
|
|
if (CE->getOpcode() == Instruction::BitCast) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Attempting to resolve bitcast on constant ptr.\n");
|
2011-01-14 22:19:20 +00:00
|
|
|
// If we're evaluating a store through a bitcast, then we need
|
|
|
|
// to pull the bitcast off the pointer type and push it onto the
|
|
|
|
// stored value.
|
2011-01-16 02:05:10 +00:00
|
|
|
Ptr = CE->getOperand(0);
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2012-02-06 08:24:44 +00:00
|
|
|
Type *NewTy = cast<PointerType>(Ptr->getType())->getElementType();
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2011-01-16 04:33:33 +00:00
|
|
|
// In order to push the bitcast onto the stored value, a bitcast
|
|
|
|
// from NewTy to Val's type must be legal. If it's not, we can try
|
|
|
|
// introspecting NewTy to find a legal conversion.
|
|
|
|
while (!Val->getType()->canLosslesslyBitCastTo(NewTy)) {
|
|
|
|
// If NewTy is a struct, we can convert the pointer to the struct
|
|
|
|
// into a pointer to its first member.
|
|
|
|
// FIXME: This could be extended to support arrays as well.
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(NewTy)) {
|
2011-01-16 04:33:33 +00:00
|
|
|
NewTy = STy->getTypeAtIndex(0U);
|
|
|
|
|
2012-02-06 08:24:44 +00:00
|
|
|
IntegerType *IdxTy = IntegerType::get(NewTy->getContext(), 32);
|
2011-01-16 04:33:33 +00:00
|
|
|
Constant *IdxZero = ConstantInt::get(IdxTy, 0, false);
|
|
|
|
Constant * const IdxList[] = {IdxZero, IdxZero};
|
|
|
|
|
2011-07-22 08:52:50 +00:00
|
|
|
Ptr = ConstantExpr::getGetElementPtr(Ptr, IdxList);
|
2012-02-21 22:08:06 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
|
|
|
|
Ptr = ConstantFoldConstantExpression(CE, TD, TLI);
|
|
|
|
|
2011-01-16 04:33:33 +00:00
|
|
|
// If we can't improve the situation by introspecting NewTy,
|
|
|
|
// we have to give up.
|
|
|
|
} else {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Failed to bitcast constant ptr, can not "
|
|
|
|
"evaluate.\n");
|
2012-02-06 08:24:44 +00:00
|
|
|
return false;
|
2011-01-16 04:33:33 +00:00
|
|
|
}
|
2011-01-14 22:19:20 +00:00
|
|
|
}
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2011-01-16 04:33:33 +00:00
|
|
|
// If we found compatible types, go ahead and push the bitcast
|
|
|
|
// onto the stored value.
|
2011-01-14 22:19:20 +00:00
|
|
|
Val = ConstantExpr::getBitCast(Val, NewTy);
|
2013-01-11 20:07:53 +00:00
|
|
|
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Evaluated bitcast: " << *Val << "\n");
|
2011-01-14 22:19:20 +00:00
|
|
|
}
|
2013-01-11 20:07:53 +00:00
|
|
|
}
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2005-09-26 04:44:35 +00:00
|
|
|
MutatedMemory[Ptr] = Val;
|
|
|
|
} else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CurInst)) {
|
2009-07-29 18:55:55 +00:00
|
|
|
InstResult = ConstantExpr::get(BO->getOpcode(),
|
2012-02-19 23:26:27 +00:00
|
|
|
getVal(BO->getOperand(0)),
|
|
|
|
getVal(BO->getOperand(1)));
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Found a BinaryOperator! Simplifying: " << *InstResult
|
2013-01-11 23:08:52 +00:00
|
|
|
<< "\n");
|
2006-12-23 06:05:41 +00:00
|
|
|
} else if (CmpInst *CI = dyn_cast<CmpInst>(CurInst)) {
|
2009-07-29 18:55:55 +00:00
|
|
|
InstResult = ConstantExpr::getCompare(CI->getPredicate(),
|
2012-02-19 23:26:27 +00:00
|
|
|
getVal(CI->getOperand(0)),
|
|
|
|
getVal(CI->getOperand(1)));
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Found a CmpInst! Simplifying: " << *InstResult
|
2013-01-11 23:08:52 +00:00
|
|
|
<< "\n");
|
2005-09-26 04:44:35 +00:00
|
|
|
} else if (CastInst *CI = dyn_cast<CastInst>(CurInst)) {
|
2009-07-29 18:55:55 +00:00
|
|
|
InstResult = ConstantExpr::getCast(CI->getOpcode(),
|
2012-02-19 23:26:27 +00:00
|
|
|
getVal(CI->getOperand(0)),
|
2005-09-26 04:44:35 +00:00
|
|
|
CI->getType());
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Found a Cast! Simplifying: " << *InstResult
|
2013-01-11 23:08:52 +00:00
|
|
|
<< "\n");
|
2005-09-26 04:44:35 +00:00
|
|
|
} else if (SelectInst *SI = dyn_cast<SelectInst>(CurInst)) {
|
2012-02-19 23:26:27 +00:00
|
|
|
InstResult = ConstantExpr::getSelect(getVal(SI->getOperand(0)),
|
|
|
|
getVal(SI->getOperand(1)),
|
|
|
|
getVal(SI->getOperand(2)));
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Found a Select! Simplifying: " << *InstResult
|
2013-01-11 23:08:52 +00:00
|
|
|
<< "\n");
|
2005-09-26 05:15:37 +00:00
|
|
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) {
|
2012-02-19 23:26:27 +00:00
|
|
|
Constant *P = getVal(GEP->getOperand(0));
|
2007-01-31 04:40:53 +00:00
|
|
|
SmallVector<Constant*, 8> GEPOps;
|
2008-05-29 01:59:18 +00:00
|
|
|
for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
|
|
|
|
i != e; ++i)
|
2012-02-19 23:26:27 +00:00
|
|
|
GEPOps.push_back(getVal(*i));
|
2011-07-21 15:15:37 +00:00
|
|
|
InstResult =
|
|
|
|
ConstantExpr::getGetElementPtr(P, GEPOps,
|
|
|
|
cast<GEPOperator>(GEP)->isInBounds());
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Found a GEP! Simplifying: " << *InstResult
|
2013-01-11 23:08:52 +00:00
|
|
|
<< "\n");
|
2005-09-26 05:15:37 +00:00
|
|
|
} else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) {
|
2013-01-11 20:07:53 +00:00
|
|
|
|
|
|
|
if (!LI->isSimple()) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Found a Load! Not a simple load, can not evaluate.\n");
|
|
|
|
return false; // no volatile/atomic accesses.
|
2013-01-11 20:07:53 +00:00
|
|
|
}
|
|
|
|
|
2012-02-21 22:08:06 +00:00
|
|
|
Constant *Ptr = getVal(LI->getOperand(0));
|
2013-01-11 20:07:53 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
|
2012-02-21 22:08:06 +00:00
|
|
|
Ptr = ConstantFoldConstantExpression(CE, TD, TLI);
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Found a constant pointer expression, constant "
|
|
|
|
"folding: " << *Ptr << "\n");
|
2013-01-11 20:07:53 +00:00
|
|
|
}
|
2012-02-21 22:08:06 +00:00
|
|
|
InstResult = ComputeLoadResult(Ptr);
|
2013-01-11 20:07:53 +00:00
|
|
|
if (InstResult == 0) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Failed to compute load result. Can not evaluate load."
|
|
|
|
"\n");
|
|
|
|
return false; // Could not evaluate load.
|
2013-01-11 20:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Evaluated load: " << *InstResult << "\n");
|
2005-09-26 17:07:09 +00:00
|
|
|
} else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) {
|
2013-01-11 20:07:53 +00:00
|
|
|
if (AI->isArrayAllocation()) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Found an array alloca. Can not evaluate.\n");
|
|
|
|
return false; // Cannot handle array allocs.
|
2013-01-11 20:07:53 +00:00
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Ty = AI->getType()->getElementType();
|
2009-11-06 04:27:31 +00:00
|
|
|
AllocaTmps.push_back(new GlobalVariable(Ty, false,
|
2005-09-26 17:07:09 +00:00
|
|
|
GlobalValue::InternalLinkage,
|
2009-07-30 23:03:37 +00:00
|
|
|
UndefValue::get(Ty),
|
2005-09-26 17:07:09 +00:00
|
|
|
AI->getName()));
|
2010-10-18 21:16:00 +00:00
|
|
|
InstResult = AllocaTmps.back();
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Found an alloca. Result: " << *InstResult << "\n");
|
2012-02-12 05:09:35 +00:00
|
|
|
} else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) {
|
|
|
|
CallSite CS(CurInst);
|
2009-03-09 23:04:12 +00:00
|
|
|
|
|
|
|
// Debug info can safely be ignored here.
|
2012-02-12 05:09:35 +00:00
|
|
|
if (isa<DbgInfoIntrinsic>(CS.getInstruction())) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Ignoring debug info.\n");
|
2009-03-09 23:04:12 +00:00
|
|
|
++CurInst;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-07-07 21:37:01 +00:00
|
|
|
// Cannot handle inline asm.
|
2013-01-11 20:07:53 +00:00
|
|
|
if (isa<InlineAsm>(CS.getCalledValue())) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Found inline asm, can not evaluate.\n");
|
|
|
|
return false;
|
2013-01-11 20:07:53 +00:00
|
|
|
}
|
2006-07-07 21:37:01 +00:00
|
|
|
|
2012-02-17 06:59:21 +00:00
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
|
|
|
|
if (MemSetInst *MSI = dyn_cast<MemSetInst>(II)) {
|
2013-01-11 20:07:53 +00:00
|
|
|
if (MSI->isVolatile()) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Can not optimize a volatile memset " <<
|
|
|
|
"intrinsic.\n");
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-19 23:26:27 +00:00
|
|
|
Constant *Ptr = getVal(MSI->getDest());
|
|
|
|
Constant *Val = getVal(MSI->getValue());
|
|
|
|
Constant *DestVal = ComputeLoadResult(getVal(Ptr));
|
2012-02-17 06:59:21 +00:00
|
|
|
if (Val->isNullValue() && DestVal && DestVal->isNullValue()) {
|
|
|
|
// This memset is a no-op.
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Ignoring no-op memset.\n");
|
2012-02-17 06:59:21 +00:00
|
|
|
++CurInst;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
|
|
|
|
II->getIntrinsicID() == Intrinsic::lifetime_end) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Ignoring lifetime intrinsic.\n");
|
2012-02-17 06:59:21 +00:00
|
|
|
++CurInst;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (II->getIntrinsicID() == Intrinsic::invariant_start) {
|
|
|
|
// We don't insert an entry into Values, as it doesn't have a
|
|
|
|
// meaningful return value.
|
2013-01-11 20:07:53 +00:00
|
|
|
if (!II->use_empty()) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Found unused invariant_start. Cant evaluate.\n");
|
2012-02-17 06:59:21 +00:00
|
|
|
return false;
|
2013-01-11 23:08:52 +00:00
|
|
|
}
|
2012-02-17 06:59:21 +00:00
|
|
|
ConstantInt *Size = cast<ConstantInt>(II->getArgOperand(0));
|
2012-02-20 23:32:26 +00:00
|
|
|
Value *PtrArg = getVal(II->getArgOperand(1));
|
|
|
|
Value *Ptr = PtrArg->stripPointerCasts();
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
|
|
|
|
Type *ElemTy = cast<PointerType>(GV->getType())->getElementType();
|
|
|
|
if (!Size->isAllOnesValue() &&
|
|
|
|
Size->getValue().getLimitedValue() >=
|
2013-01-11 20:07:53 +00:00
|
|
|
TD->getTypeStoreSize(ElemTy)) {
|
2012-02-17 06:59:21 +00:00
|
|
|
Invariants.insert(GV);
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Found a global var that is an invariant: " << *GV
|
|
|
|
<< "\n");
|
|
|
|
} else {
|
|
|
|
DEBUG(dbgs() << "Found a global var, but can not treat it as an "
|
|
|
|
"invariant.\n");
|
|
|
|
}
|
2012-02-17 06:59:21 +00:00
|
|
|
}
|
|
|
|
// Continue even if we do nothing.
|
2011-05-29 18:41:56 +00:00
|
|
|
++CurInst;
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-11 20:07:53 +00:00
|
|
|
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Unknown intrinsic. Can not evaluate.\n");
|
2011-05-29 18:41:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-09-27 04:45:34 +00:00
|
|
|
// Resolve function pointers.
|
2012-02-19 23:26:27 +00:00
|
|
|
Function *Callee = dyn_cast<Function>(getVal(CS.getCalledValue()));
|
2013-01-11 20:07:53 +00:00
|
|
|
if (!Callee || Callee->mayBeOverridden()) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Can not resolve function pointer.\n");
|
2012-02-12 05:09:35 +00:00
|
|
|
return false; // Cannot resolve.
|
2013-01-11 20:07:53 +00:00
|
|
|
}
|
2005-09-27 05:02:43 +00:00
|
|
|
|
2009-08-17 14:33:27 +00:00
|
|
|
SmallVector<Constant*, 8> Formals;
|
2012-02-12 05:09:35 +00:00
|
|
|
for (User::op_iterator i = CS.arg_begin(), e = CS.arg_end(); i != e; ++i)
|
2012-02-19 23:26:27 +00:00
|
|
|
Formals.push_back(getVal(*i));
|
2009-08-17 14:33:27 +00:00
|
|
|
|
2007-01-30 20:08:39 +00:00
|
|
|
if (Callee->isDeclaration()) {
|
2005-09-27 05:02:43 +00:00
|
|
|
// If this is a function we can constant fold, do it.
|
2011-12-01 21:29:16 +00:00
|
|
|
if (Constant *C = ConstantFoldCall(Callee, Formals, TLI)) {
|
2005-09-27 05:02:43 +00:00
|
|
|
InstResult = C;
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Constant folded function call. Result: " <<
|
|
|
|
*InstResult << "\n");
|
2005-09-27 05:02:43 +00:00
|
|
|
} else {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Can not constant fold function call.\n");
|
2005-09-27 05:02:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2013-01-11 20:07:53 +00:00
|
|
|
if (Callee->getFunctionType()->isVarArg()) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Can not constant fold vararg function call.\n");
|
2005-09-27 05:02:43 +00:00
|
|
|
return false;
|
2013-01-11 23:08:52 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2013-01-12 15:34:31 +00:00
|
|
|
Constant *RetVal = 0;
|
2005-09-27 05:02:43 +00:00
|
|
|
// Execute the call, if successful, use the return value.
|
2012-02-19 23:26:27 +00:00
|
|
|
ValueStack.push_back(new DenseMap<Value*, Constant*>);
|
2013-01-11 20:07:53 +00:00
|
|
|
if (!EvaluateFunction(Callee, RetVal, Formals)) {
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Failed to evaluate function.\n");
|
2005-09-27 05:02:43 +00:00
|
|
|
return false;
|
2013-01-11 23:08:52 +00:00
|
|
|
}
|
2012-02-27 12:48:24 +00:00
|
|
|
delete ValueStack.pop_back_val();
|
2005-09-27 05:02:43 +00:00
|
|
|
InstResult = RetVal;
|
2013-01-11 20:07:53 +00:00
|
|
|
|
2013-01-11 23:08:52 +00:00
|
|
|
if (InstResult != NULL) {
|
|
|
|
DEBUG(dbgs() << "Successfully evaluated function. Result: " <<
|
|
|
|
InstResult << "\n\n");
|
|
|
|
} else {
|
|
|
|
DEBUG(dbgs() << "Successfully evaluated function. Result: 0\n\n");
|
|
|
|
}
|
2005-09-27 05:02:43 +00:00
|
|
|
}
|
2006-11-02 20:25:50 +00:00
|
|
|
} else if (isa<TerminatorInst>(CurInst)) {
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Found a terminator instruction.\n");
|
|
|
|
|
2005-09-26 04:57:38 +00:00
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(CurInst)) {
|
|
|
|
if (BI->isUnconditional()) {
|
2012-02-06 08:24:44 +00:00
|
|
|
NextBB = BI->getSuccessor(0);
|
2005-09-26 04:57:38 +00:00
|
|
|
} else {
|
2007-01-11 12:24:14 +00:00
|
|
|
ConstantInt *Cond =
|
2012-02-19 23:26:27 +00:00
|
|
|
dyn_cast<ConstantInt>(getVal(BI->getCondition()));
|
2007-01-12 18:30:11 +00:00
|
|
|
if (!Cond) return false; // Cannot determine.
|
2007-01-11 12:24:14 +00:00
|
|
|
|
2012-02-06 08:24:44 +00:00
|
|
|
NextBB = BI->getSuccessor(!Cond->getZExtValue());
|
2005-09-26 04:57:38 +00:00
|
|
|
}
|
|
|
|
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) {
|
|
|
|
ConstantInt *Val =
|
2012-02-19 23:26:27 +00:00
|
|
|
dyn_cast<ConstantInt>(getVal(SI->getCondition()));
|
2005-09-27 04:45:34 +00:00
|
|
|
if (!Val) return false; // Cannot determine.
|
2012-03-08 07:06:20 +00:00
|
|
|
NextBB = SI->findCaseValue(Val).getCaseSuccessor();
|
2009-10-29 05:51:50 +00:00
|
|
|
} else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(CurInst)) {
|
2012-02-19 23:26:27 +00:00
|
|
|
Value *Val = getVal(IBI->getAddress())->stripPointerCasts();
|
2009-10-29 05:51:50 +00:00
|
|
|
if (BlockAddress *BA = dyn_cast<BlockAddress>(Val))
|
2012-02-06 08:24:44 +00:00
|
|
|
NextBB = BA->getBasicBlock();
|
2009-11-01 01:27:45 +00:00
|
|
|
else
|
|
|
|
return false; // Cannot determine.
|
2012-02-06 08:24:44 +00:00
|
|
|
} else if (isa<ReturnInst>(CurInst)) {
|
|
|
|
NextBB = 0;
|
2005-09-26 04:57:38 +00:00
|
|
|
} else {
|
2011-07-31 06:30:59 +00:00
|
|
|
// invoke, unwind, resume, unreachable.
|
2013-01-11 23:08:52 +00:00
|
|
|
DEBUG(dbgs() << "Can not handle terminator.");
|
2005-09-27 04:45:34 +00:00
|
|
|
return false; // Cannot handle this terminator.
|
2005-09-26 04:57:38 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2012-02-06 08:24:44 +00:00
|
|
|
// We succeeded at evaluating this block!
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Successfully evaluated block.\n");
|
2012-02-06 08:24:44 +00:00
|
|
|
return true;
|
2005-09-26 04:44:35 +00:00
|
|
|
} else {
|
|
|
|
// Did not know how to evaluate this!
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Failed to evaluate block due to unhandled instruction."
|
2013-01-11 23:08:52 +00:00
|
|
|
"\n");
|
2005-09-27 04:45:34 +00:00
|
|
|
return false;
|
2005-09-26 04:44:35 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2010-12-07 04:33:29 +00:00
|
|
|
if (!CurInst->use_empty()) {
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InstResult))
|
2011-12-02 01:26:24 +00:00
|
|
|
InstResult = ConstantFoldConstantExpression(CE, TD, TLI);
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2012-02-19 23:26:27 +00:00
|
|
|
setVal(CurInst, InstResult);
|
2010-12-07 04:33:29 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2012-03-13 18:01:37 +00:00
|
|
|
// If we just processed an invoke, we finished evaluating the block.
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(CurInst)) {
|
|
|
|
NextBB = II->getNormalDest();
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Found an invoke instruction. Finished Block.\n\n");
|
2012-03-13 18:01:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-09-26 04:44:35 +00:00
|
|
|
// Advance program counter.
|
|
|
|
++CurInst;
|
|
|
|
}
|
2005-09-27 04:27:01 +00:00
|
|
|
}
|
|
|
|
|
2012-02-06 08:24:44 +00:00
|
|
|
/// EvaluateFunction - Evaluate a call to function F, returning true if
|
|
|
|
/// successful, false if we can't evaluate it. ActualArgs contains the formal
|
|
|
|
/// arguments for the function.
|
2012-02-20 03:25:59 +00:00
|
|
|
bool Evaluator::EvaluateFunction(Function *F, Constant *&RetVal,
|
|
|
|
const SmallVectorImpl<Constant*> &ActualArgs) {
|
2012-02-06 08:24:44 +00:00
|
|
|
// Check to see if this function is already executing (recursion). If so,
|
|
|
|
// bail out. TODO: we might want to accept limited recursion.
|
|
|
|
if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CallStack.push_back(F);
|
|
|
|
|
|
|
|
// Initialize arguments to the incoming values specified.
|
|
|
|
unsigned ArgNo = 0;
|
|
|
|
for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
|
|
|
|
++AI, ++ArgNo)
|
2012-02-19 23:26:27 +00:00
|
|
|
setVal(AI, ActualArgs[ArgNo]);
|
2012-02-06 08:24:44 +00:00
|
|
|
|
|
|
|
// ExecutedBlocks - We only handle non-looping, non-recursive code. As such,
|
|
|
|
// we can only evaluate any one basic block at most once. This set keeps
|
|
|
|
// track of what we have executed so we can detect recursive cases etc.
|
|
|
|
SmallPtrSet<BasicBlock*, 32> ExecutedBlocks;
|
|
|
|
|
|
|
|
// CurBB - The current basic block we're evaluating.
|
|
|
|
BasicBlock *CurBB = F->begin();
|
|
|
|
|
2012-02-12 00:47:24 +00:00
|
|
|
BasicBlock::iterator CurInst = CurBB->begin();
|
|
|
|
|
2012-02-06 08:24:44 +00:00
|
|
|
while (1) {
|
2012-02-23 08:23:06 +00:00
|
|
|
BasicBlock *NextBB = 0; // Initialized to avoid compiler warnings.
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Trying to evaluate BB: " << *CurBB << "\n");
|
|
|
|
|
2012-02-19 23:26:27 +00:00
|
|
|
if (!EvaluateBlock(CurInst, NextBB))
|
2012-02-06 08:24:44 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (NextBB == 0) {
|
|
|
|
// Successfully running until there's no next block means that we found
|
|
|
|
// the return. Fill it the return value and pop the call stack.
|
|
|
|
ReturnInst *RI = cast<ReturnInst>(CurBB->getTerminator());
|
|
|
|
if (RI->getNumOperands())
|
2012-02-19 23:26:27 +00:00
|
|
|
RetVal = getVal(RI->getOperand(0));
|
2012-02-06 08:24:44 +00:00
|
|
|
CallStack.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, we succeeded in evaluating this control flow. See if we have
|
|
|
|
// executed the new block before. If so, we have a looping function,
|
|
|
|
// which we cannot evaluate in reasonable time.
|
|
|
|
if (!ExecutedBlocks.insert(NextBB))
|
|
|
|
return false; // looped!
|
|
|
|
|
|
|
|
// Okay, we have never been in this block before. Check to see if there
|
|
|
|
// are any PHI nodes. If so, evaluate them with information about where
|
|
|
|
// we came from.
|
|
|
|
PHINode *PN = 0;
|
2012-02-12 00:47:24 +00:00
|
|
|
for (CurInst = NextBB->begin();
|
|
|
|
(PN = dyn_cast<PHINode>(CurInst)); ++CurInst)
|
2012-02-19 23:26:27 +00:00
|
|
|
setVal(PN, getVal(PN->getIncomingValueForBlock(CurBB)));
|
2012-02-06 08:24:44 +00:00
|
|
|
|
|
|
|
// Advance to the next block.
|
|
|
|
CurBB = NextBB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-27 04:27:01 +00:00
|
|
|
/// EvaluateStaticConstructor - Evaluate static constructors in the function, if
|
|
|
|
/// we can. Return true if we can, false otherwise.
|
2012-10-08 16:38:25 +00:00
|
|
|
static bool EvaluateStaticConstructor(Function *F, const DataLayout *TD,
|
2011-12-01 21:29:16 +00:00
|
|
|
const TargetLibraryInfo *TLI) {
|
2005-09-27 04:27:01 +00:00
|
|
|
// Call the function.
|
2012-02-20 03:25:59 +00:00
|
|
|
Evaluator Eval(TD, TLI);
|
2005-09-27 04:45:34 +00:00
|
|
|
Constant *RetValDummy;
|
2012-02-19 23:26:27 +00:00
|
|
|
bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy,
|
|
|
|
SmallVector<Constant*, 0>());
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2005-09-27 04:27:01 +00:00
|
|
|
if (EvalSuccess) {
|
2005-09-26 17:07:09 +00:00
|
|
|
// We succeeded at evaluation: commit the result.
|
2010-01-05 01:28:05 +00:00
|
|
|
DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '"
|
2012-02-19 23:26:27 +00:00
|
|
|
<< F->getName() << "' to " << Eval.getMutatedMemory().size()
|
2009-07-25 00:23:56 +00:00
|
|
|
<< " stores.\n");
|
2012-02-19 23:26:27 +00:00
|
|
|
for (DenseMap<Constant*, Constant*>::const_iterator I =
|
|
|
|
Eval.getMutatedMemory().begin(), E = Eval.getMutatedMemory().end();
|
2012-06-24 04:07:14 +00:00
|
|
|
I != E; ++I)
|
2009-11-06 04:27:31 +00:00
|
|
|
CommitValueTo(I->second, I->first);
|
2012-02-19 23:26:27 +00:00
|
|
|
for (SmallPtrSet<GlobalVariable*, 8>::const_iterator I =
|
|
|
|
Eval.getInvariants().begin(), E = Eval.getInvariants().end();
|
|
|
|
I != E; ++I)
|
2012-02-17 06:59:21 +00:00
|
|
|
(*I)->setConstant(true);
|
2005-09-26 17:07:09 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-27 04:27:01 +00:00
|
|
|
return EvalSuccess;
|
2005-09-26 04:44:35 +00:00
|
|
|
}
|
|
|
|
|
2005-09-26 02:31:18 +00:00
|
|
|
/// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible.
|
|
|
|
/// Return true if anything changed.
|
|
|
|
bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
|
|
|
|
std::vector<Function*> Ctors = ParseGlobalCtors(GCL);
|
|
|
|
bool MadeChange = false;
|
|
|
|
if (Ctors.empty()) return false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 02:31:18 +00:00
|
|
|
// Loop over global ctors, optimizing them when we can.
|
|
|
|
for (unsigned i = 0; i != Ctors.size(); ++i) {
|
|
|
|
Function *F = Ctors[i];
|
|
|
|
// Found a null terminator in the middle of the list, prune off the rest of
|
|
|
|
// the list.
|
|
|
|
if (F == 0) {
|
|
|
|
if (i != Ctors.size()-1) {
|
|
|
|
Ctors.resize(i+1);
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-01-11 20:07:53 +00:00
|
|
|
DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 04:44:35 +00:00
|
|
|
// We cannot simplify external ctor functions.
|
|
|
|
if (F->empty()) continue;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 04:44:35 +00:00
|
|
|
// If we can evaluate the ctor at compile time, do.
|
2011-12-01 21:29:16 +00:00
|
|
|
if (EvaluateStaticConstructor(F, TD, TLI)) {
|
2005-09-26 04:44:35 +00:00
|
|
|
Ctors.erase(Ctors.begin()+i);
|
|
|
|
MadeChange = true;
|
|
|
|
--i;
|
|
|
|
++NumCtorsEvaluated;
|
|
|
|
continue;
|
|
|
|
}
|
2005-09-26 02:31:18 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 02:31:18 +00:00
|
|
|
if (!MadeChange) return false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2009-11-06 04:27:31 +00:00
|
|
|
GCL = InstallGlobalCtors(GCL, Ctors);
|
2005-09-26 01:43:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-06 10:21:56 +00:00
|
|
|
bool GlobalOpt::OptimizeGlobalAliases(Module &M) {
|
2008-09-09 19:04:59 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
2009-01-07 20:01:06 +00:00
|
|
|
for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
|
2009-02-15 09:56:08 +00:00
|
|
|
I != E;) {
|
|
|
|
Module::alias_iterator J = I++;
|
2009-03-06 10:21:56 +00:00
|
|
|
// Aliases without names cannot be referenced outside this module.
|
|
|
|
if (!J->hasName() && !J->isDeclaration())
|
|
|
|
J->setLinkage(GlobalValue::InternalLinkage);
|
2009-02-15 09:56:08 +00:00
|
|
|
// If the aliasee may change at link time, nothing can be done - bail out.
|
|
|
|
if (J->mayBeOverridden())
|
2008-09-09 19:04:59 +00:00
|
|
|
continue;
|
|
|
|
|
2009-02-15 09:56:08 +00:00
|
|
|
Constant *Aliasee = J->getAliasee();
|
|
|
|
GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
|
2009-02-18 17:55:38 +00:00
|
|
|
Target->removeDeadConstantUsers();
|
2009-02-15 09:56:08 +00:00
|
|
|
bool hasOneUse = Target->hasOneUse() && Aliasee->hasOneUse();
|
|
|
|
|
|
|
|
// Make all users of the alias use the aliasee instead.
|
|
|
|
if (!J->use_empty()) {
|
|
|
|
J->replaceAllUsesWith(Aliasee);
|
|
|
|
++NumAliasesResolved;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2009-12-08 10:10:20 +00:00
|
|
|
// If the alias is externally visible, we may still be able to simplify it.
|
|
|
|
if (!J->hasLocalLinkage()) {
|
|
|
|
// If the aliasee has internal linkage, give it the name and linkage
|
|
|
|
// of the alias, and delete the alias. This turns:
|
|
|
|
// define internal ... @f(...)
|
|
|
|
// @a = alias ... @f
|
|
|
|
// into:
|
|
|
|
// define ... @a(...)
|
|
|
|
if (!Target->hasLocalLinkage())
|
|
|
|
continue;
|
2009-02-15 09:56:08 +00:00
|
|
|
|
2009-12-08 10:10:20 +00:00
|
|
|
// Do not perform the transform if multiple aliases potentially target the
|
2010-04-07 18:59:26 +00:00
|
|
|
// aliasee. This check also ensures that it is safe to replace the section
|
2009-12-08 10:10:20 +00:00
|
|
|
// and other attributes of the aliasee with those of the alias.
|
|
|
|
if (!hasOneUse)
|
|
|
|
continue;
|
2009-02-15 09:56:08 +00:00
|
|
|
|
2009-12-08 10:10:20 +00:00
|
|
|
// Give the aliasee the name, linkage and other attributes of the alias.
|
|
|
|
Target->takeName(J);
|
|
|
|
Target->setLinkage(J->getLinkage());
|
|
|
|
Target->GlobalValue::copyAttributesFrom(J);
|
|
|
|
}
|
2009-02-15 09:56:08 +00:00
|
|
|
|
|
|
|
// Delete the alias.
|
|
|
|
M.getAliasList().erase(J);
|
|
|
|
++NumAliasesRemoved;
|
|
|
|
Changed = true;
|
2008-09-09 19:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
2005-09-26 01:43:45 +00:00
|
|
|
|
2012-02-12 02:15:20 +00:00
|
|
|
static Function *FindCXAAtExit(Module &M, TargetLibraryInfo *TLI) {
|
|
|
|
if (!TLI->has(LibFunc::cxa_atexit))
|
2012-02-12 02:17:18 +00:00
|
|
|
return 0;
|
2012-02-12 02:15:20 +00:00
|
|
|
|
|
|
|
Function *Fn = M.getFunction(TLI->getName(LibFunc::cxa_atexit));
|
2012-12-06 21:57:16 +00:00
|
|
|
|
2011-03-20 17:59:11 +00:00
|
|
|
if (!Fn)
|
|
|
|
return 0;
|
2012-02-12 02:15:20 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *FTy = Fn->getFunctionType();
|
2012-12-06 21:57:16 +00:00
|
|
|
|
|
|
|
// Checking that the function has the right return type, the right number of
|
2011-03-20 19:51:13 +00:00
|
|
|
// parameters and that they all have pointer types should be enough.
|
|
|
|
if (!FTy->getReturnType()->isIntegerTy() ||
|
|
|
|
FTy->getNumParams() != 3 ||
|
2011-03-20 17:59:11 +00:00
|
|
|
!FTy->getParamType(0)->isPointerTy() ||
|
|
|
|
!FTy->getParamType(1)->isPointerTy() ||
|
|
|
|
!FTy->getParamType(2)->isPointerTy())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// cxxDtorIsEmpty - Returns whether the given function is an empty C++
|
|
|
|
/// destructor and can therefore be eliminated.
|
|
|
|
/// Note that we assume that other optimization passes have already simplified
|
|
|
|
/// the code so we only look for a function with a single basic block, where
|
2012-02-09 16:28:15 +00:00
|
|
|
/// the only allowed instructions are 'ret', 'call' to an empty C++ dtor and
|
|
|
|
/// other side-effect free instructions.
|
2011-03-20 20:16:43 +00:00
|
|
|
static bool cxxDtorIsEmpty(const Function &Fn,
|
|
|
|
SmallPtrSet<const Function *, 8> &CalledFunctions) {
|
2011-03-20 19:51:13 +00:00
|
|
|
// FIXME: We could eliminate C++ destructors if they're readonly/readnone and
|
2011-03-21 02:26:01 +00:00
|
|
|
// nounwind, but that doesn't seem worth doing.
|
2011-03-20 19:51:13 +00:00
|
|
|
if (Fn.isDeclaration())
|
|
|
|
return false;
|
2011-03-20 17:59:11 +00:00
|
|
|
|
|
|
|
if (++Fn.begin() != Fn.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const BasicBlock &EntryBlock = Fn.getEntryBlock();
|
|
|
|
for (BasicBlock::const_iterator I = EntryBlock.begin(), E = EntryBlock.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (const CallInst *CI = dyn_cast<CallInst>(I)) {
|
2011-03-21 14:54:40 +00:00
|
|
|
// Ignore debug intrinsics.
|
|
|
|
if (isa<DbgInfoIntrinsic>(CI))
|
|
|
|
continue;
|
|
|
|
|
2011-03-20 17:59:11 +00:00
|
|
|
const Function *CalledFn = CI->getCalledFunction();
|
|
|
|
|
|
|
|
if (!CalledFn)
|
|
|
|
return false;
|
|
|
|
|
2011-03-22 03:21:01 +00:00
|
|
|
SmallPtrSet<const Function *, 8> NewCalledFunctions(CalledFunctions);
|
|
|
|
|
2011-03-20 19:51:13 +00:00
|
|
|
// Don't treat recursive functions as empty.
|
2011-03-22 03:21:01 +00:00
|
|
|
if (!NewCalledFunctions.insert(CalledFn))
|
2011-03-20 19:51:13 +00:00
|
|
|
return false;
|
|
|
|
|
2011-03-22 03:21:01 +00:00
|
|
|
if (!cxxDtorIsEmpty(*CalledFn, NewCalledFunctions))
|
2011-03-20 17:59:11 +00:00
|
|
|
return false;
|
|
|
|
} else if (isa<ReturnInst>(*I))
|
2012-02-09 14:26:06 +00:00
|
|
|
return true; // We're done.
|
|
|
|
else if (I->mayHaveSideEffects())
|
|
|
|
return false; // Destructor with side effects, bail.
|
2011-03-20 17:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalOpt::OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) {
|
|
|
|
/// Itanium C++ ABI p3.3.5:
|
|
|
|
///
|
|
|
|
/// After constructing a global (or local static) object, that will require
|
|
|
|
/// destruction on exit, a termination function is registered as follows:
|
|
|
|
///
|
|
|
|
/// extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
|
|
|
|
///
|
|
|
|
/// This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the
|
|
|
|
/// call f(p) when DSO d is unloaded, before all such termination calls
|
|
|
|
/// registered before this one. It returns zero if registration is
|
2011-03-21 02:26:01 +00:00
|
|
|
/// successful, nonzero on failure.
|
2011-03-20 17:59:11 +00:00
|
|
|
|
|
|
|
// This pass will look for calls to __cxa_atexit where the function is trivial
|
|
|
|
// and remove them.
|
|
|
|
bool Changed = false;
|
|
|
|
|
2012-12-06 21:57:16 +00:00
|
|
|
for (Function::use_iterator I = CXAAtExitFn->use_begin(),
|
2011-03-20 17:59:11 +00:00
|
|
|
E = CXAAtExitFn->use_end(); I != E;) {
|
2011-03-20 20:21:33 +00:00
|
|
|
// We're only interested in calls. Theoretically, we could handle invoke
|
|
|
|
// instructions as well, but neither llvm-gcc nor clang generate invokes
|
|
|
|
// to __cxa_atexit.
|
2011-03-21 14:54:40 +00:00
|
|
|
CallInst *CI = dyn_cast<CallInst>(*I++);
|
|
|
|
if (!CI)
|
2011-03-20 20:21:33 +00:00
|
|
|
continue;
|
|
|
|
|
2012-12-06 21:57:16 +00:00
|
|
|
Function *DtorFn =
|
2011-03-21 14:54:40 +00:00
|
|
|
dyn_cast<Function>(CI->getArgOperand(0)->stripPointerCasts());
|
2011-03-20 17:59:11 +00:00
|
|
|
if (!DtorFn)
|
|
|
|
continue;
|
|
|
|
|
2011-03-20 20:16:43 +00:00
|
|
|
SmallPtrSet<const Function *, 8> CalledFunctions;
|
|
|
|
if (!cxxDtorIsEmpty(*DtorFn, CalledFunctions))
|
2011-03-20 17:59:11 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Just remove the call.
|
2011-03-21 14:54:40 +00:00
|
|
|
CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
|
|
|
|
CI->eraseFromParent();
|
2011-03-20 19:51:13 +00:00
|
|
|
|
2011-03-20 17:59:11 +00:00
|
|
|
++NumCXXDtorsRemoved;
|
|
|
|
|
|
|
|
Changed |= true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
bool GlobalOpt::runOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2012-10-08 16:38:25 +00:00
|
|
|
TD = getAnalysisIfAvailable<DataLayout>();
|
2012-02-12 02:15:20 +00:00
|
|
|
TLI = &getAnalysis<TargetLibraryInfo>();
|
2012-02-12 01:13:18 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
// Try to find the llvm.globalctors list.
|
|
|
|
GlobalVariable *GlobalCtors = FindGlobalCtors(M);
|
|
|
|
|
2012-02-12 02:15:20 +00:00
|
|
|
Function *CXAAtExitFn = FindCXAAtExit(M, TLI);
|
2011-03-20 17:59:11 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
bool LocalChange = true;
|
2004-10-08 20:59:28 +00:00
|
|
|
while (LocalChange) {
|
|
|
|
LocalChange = false;
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
// Delete functions that are trivially dead, ccc -> fastcc
|
|
|
|
LocalChange |= OptimizeFunctions(M);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
// Optimize global_ctors list.
|
|
|
|
if (GlobalCtors)
|
|
|
|
LocalChange |= OptimizeGlobalCtorsList(GlobalCtors);
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
// Optimize non-address-taken globals.
|
|
|
|
LocalChange |= OptimizeGlobalVars(M);
|
2008-09-09 19:04:59 +00:00
|
|
|
|
|
|
|
// Resolve aliases, when possible.
|
2009-03-06 10:21:56 +00:00
|
|
|
LocalChange |= OptimizeGlobalAliases(M);
|
2011-03-20 17:59:11 +00:00
|
|
|
|
|
|
|
// Try to remove trivial global destructors.
|
|
|
|
if (CXAAtExitFn)
|
|
|
|
LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn);
|
|
|
|
|
2008-09-09 19:04:59 +00:00
|
|
|
Changed |= LocalChange;
|
2004-10-06 20:57:02 +00:00
|
|
|
}
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2005-09-26 01:43:45 +00:00
|
|
|
// TODO: Move all global ctors functions to the end of the module for code
|
|
|
|
// layout.
|
2010-10-18 21:16:00 +00:00
|
|
|
|
2004-02-25 21:34:36 +00:00
|
|
|
return Changed;
|
|
|
|
}
|