[PM] Refactor the core logic to run EarlyCSE over a function into an

object that manages a single run of this pass.

This was already essentially how it worked. Within the run function, it
would point members at *stack local* allocations that were only live for
a single run. Instead, it seems much cleaner to have a utility object
whose lifetime is clearly bounded by the run of the pass over the
function and can use member variables in a more direct way.

This also makes it easy to plumb the analyses used into it from the pass
and will make it re-usable with the new pass manager.

No functionality changed here, its just a refactoring.

llvm-svn: 227162
This commit is contained in:
Chandler Carruth 2015-01-27 01:34:14 +00:00
parent aa772feb8e
commit d649c0ad56
3 changed files with 96 additions and 77 deletions

View File

@ -129,7 +129,7 @@ void initializeThreadSanitizerPass(PassRegistry&);
void initializeSanitizerCoverageModulePass(PassRegistry&); void initializeSanitizerCoverageModulePass(PassRegistry&);
void initializeDataFlowSanitizerPass(PassRegistry&); void initializeDataFlowSanitizerPass(PassRegistry&);
void initializeScalarizerPass(PassRegistry&); void initializeScalarizerPass(PassRegistry&);
void initializeEarlyCSEPass(PassRegistry&); void initializeEarlyCSELegacyPassPass(PassRegistry &);
void initializeExpandISelPseudosPass(PassRegistry&); void initializeExpandISelPseudosPass(PassRegistry&);
void initializeFunctionAttrsPass(PassRegistry&); void initializeFunctionAttrsPass(PassRegistry&);
void initializeGCMachineCodeAnalysisPass(PassRegistry&); void initializeGCMachineCodeAnalysisPass(PassRegistry&);

View File

@ -258,11 +258,10 @@ bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) {
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// EarlyCSE pass. // EarlyCSE implementation
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
namespace { namespace {
/// \brief A simple and fast domtree-based CSE pass. /// \brief A simple and fast domtree-based CSE pass.
/// ///
/// This pass does a simple depth-first walk over the dominator tree, /// This pass does a simple depth-first walk over the dominator tree,
@ -270,13 +269,14 @@ namespace {
/// canonicalize things as it goes. It is intended to be fast and catch obvious /// canonicalize things as it goes. It is intended to be fast and catch obvious
/// cases so that instcombine and other passes are more effective. It is /// cases so that instcombine and other passes are more effective. It is
/// expected that a later pass of GVN will catch the interesting/hard cases. /// expected that a later pass of GVN will catch the interesting/hard cases.
class EarlyCSE : public FunctionPass { class EarlyCSE {
public: public:
Function &F;
const DataLayout *DL; const DataLayout *DL;
const TargetLibraryInfo *TLI; const TargetLibraryInfo &TLI;
const TargetTransformInfo *TTI; const TargetTransformInfo &TTI;
DominatorTree *DT; DominatorTree &DT;
AssumptionCache *AC; AssumptionCache &AC;
typedef RecyclingAllocator< typedef RecyclingAllocator<
BumpPtrAllocator, ScopedHashTableVal<SimpleValue, Value *>> AllocatorTy; BumpPtrAllocator, ScopedHashTableVal<SimpleValue, Value *>> AllocatorTy;
typedef ScopedHashTable<SimpleValue, Value *, DenseMapInfo<SimpleValue>, typedef ScopedHashTable<SimpleValue, Value *, DenseMapInfo<SimpleValue>,
@ -288,7 +288,7 @@ public:
/// As we walk down the domtree, we look to see if instructions are in this: /// As we walk down the domtree, we look to see if instructions are in this:
/// if so, we replace them with what we find, otherwise we insert them so /// if so, we replace them with what we find, otherwise we insert them so
/// that dominated values can succeed in their lookup. /// that dominated values can succeed in their lookup.
ScopedHTType *AvailableValues; ScopedHTType AvailableValues;
/// \brief A scoped hash table of the current values of loads. /// \brief A scoped hash table of the current values of loads.
/// ///
@ -304,24 +304,26 @@ public:
LoadMapAllocator; LoadMapAllocator;
typedef ScopedHashTable<Value *, std::pair<Value *, unsigned>, typedef ScopedHashTable<Value *, std::pair<Value *, unsigned>,
DenseMapInfo<Value *>, LoadMapAllocator> LoadHTType; DenseMapInfo<Value *>, LoadMapAllocator> LoadHTType;
LoadHTType *AvailableLoads; LoadHTType AvailableLoads;
/// \brief A scoped hash table of the current values of read-only call /// \brief A scoped hash table of the current values of read-only call
/// values. /// values.
/// ///
/// It uses the same generation count as loads. /// It uses the same generation count as loads.
typedef ScopedHashTable<CallValue, std::pair<Value *, unsigned>> CallHTType; typedef ScopedHashTable<CallValue, std::pair<Value *, unsigned>> CallHTType;
CallHTType *AvailableCalls; CallHTType AvailableCalls;
/// \brief This is the current generation of the memory value. /// \brief This is the current generation of the memory value.
unsigned CurrentGeneration; unsigned CurrentGeneration;
static char ID; /// \brief Set up the EarlyCSE runner for a particular function.
explicit EarlyCSE() : FunctionPass(ID) { EarlyCSE(Function &F, const DataLayout *DL, const TargetLibraryInfo &TLI,
initializeEarlyCSEPass(*PassRegistry::getPassRegistry()); const TargetTransformInfo &TTI, DominatorTree &DT,
AssumptionCache &AC)
: F(F), DL(DL), TLI(TLI), TTI(TTI), DT(DT), AC(AC), CurrentGeneration(0) {
} }
bool runOnFunction(Function &F) override; bool run();
private: private:
// Almost a POD, but needs to call the constructors for the scoped hash // Almost a POD, but needs to call the constructors for the scoped hash
@ -329,10 +331,10 @@ private:
// scope gets popped when the NodeScope is destroyed. // scope gets popped when the NodeScope is destroyed.
class NodeScope { class NodeScope {
public: public:
NodeScope(ScopedHTType *availableValues, LoadHTType *availableLoads, NodeScope(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads,
CallHTType *availableCalls) CallHTType &AvailableCalls)
: Scope(*availableValues), LoadScope(*availableLoads), : Scope(AvailableValues), LoadScope(AvailableLoads),
CallScope(*availableCalls) {} CallScope(AvailableCalls) {}
private: private:
NodeScope(const NodeScope &) LLVM_DELETED_FUNCTION; NodeScope(const NodeScope &) LLVM_DELETED_FUNCTION;
@ -349,11 +351,11 @@ private:
// children do not need to be store spearately. // children do not need to be store spearately.
class StackNode { class StackNode {
public: public:
StackNode(ScopedHTType *availableValues, LoadHTType *availableLoads, StackNode(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads,
CallHTType *availableCalls, unsigned cg, DomTreeNode *n, CallHTType &AvailableCalls, unsigned cg, DomTreeNode *n,
DomTreeNode::iterator child, DomTreeNode::iterator end) DomTreeNode::iterator child, DomTreeNode::iterator end)
: CurrentGeneration(cg), ChildGeneration(cg), Node(n), ChildIter(child), : CurrentGeneration(cg), ChildGeneration(cg), Node(n), ChildIter(child),
EndIter(end), Scopes(availableValues, availableLoads, availableCalls), EndIter(end), Scopes(AvailableValues, AvailableLoads, AvailableCalls),
Processed(false) {} Processed(false) {}
// Accessors. // Accessors.
@ -389,14 +391,14 @@ private:
/// stores and intrinsic loads and stores defined by the target. /// stores and intrinsic loads and stores defined by the target.
class ParseMemoryInst { class ParseMemoryInst {
public: public:
ParseMemoryInst(Instruction *Inst, const TargetTransformInfo *TTI) ParseMemoryInst(Instruction *Inst, const TargetTransformInfo &TTI)
: Load(false), Store(false), Vol(false), MayReadFromMemory(false), : Load(false), Store(false), Vol(false), MayReadFromMemory(false),
MayWriteToMemory(false), MatchingId(-1), Ptr(nullptr) { MayWriteToMemory(false), MatchingId(-1), Ptr(nullptr) {
MayReadFromMemory = Inst->mayReadFromMemory(); MayReadFromMemory = Inst->mayReadFromMemory();
MayWriteToMemory = Inst->mayWriteToMemory(); MayWriteToMemory = Inst->mayWriteToMemory();
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
MemIntrinsicInfo Info; MemIntrinsicInfo Info;
if (!TTI->getTgtMemIntrinsic(II, Info)) if (!TTI.getTgtMemIntrinsic(II, Info))
return; return;
if (Info.NumMemRefs == 1) { if (Info.NumMemRefs == 1) {
Store = Info.WriteMem; Store = Info.WriteMem;
@ -445,36 +447,18 @@ private:
bool processNode(DomTreeNode *Node); bool processNode(DomTreeNode *Node);
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<AssumptionCacheTracker>();
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<TargetLibraryInfoWrapperPass>();
AU.addRequired<TargetTransformInfo>();
AU.setPreservesCFG();
}
Value *getOrCreateResult(Value *Inst, Type *ExpectedType) const { Value *getOrCreateResult(Value *Inst, Type *ExpectedType) const {
if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
return LI; return LI;
else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) else if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
return SI->getValueOperand(); return SI->getValueOperand();
assert(isa<IntrinsicInst>(Inst) && "Instruction not supported"); assert(isa<IntrinsicInst>(Inst) && "Instruction not supported");
return TTI->getOrCreateResultFromMemIntrinsic(cast<IntrinsicInst>(Inst), return TTI.getOrCreateResultFromMemIntrinsic(cast<IntrinsicInst>(Inst),
ExpectedType); ExpectedType);
} }
}; };
} }
char EarlyCSE::ID = 0;
FunctionPass *llvm::createEarlyCSEPass() { return new EarlyCSE(); }
INITIALIZE_PASS_BEGIN(EarlyCSE, "early-cse", "Early CSE", false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
INITIALIZE_PASS_END(EarlyCSE, "early-cse", "Early CSE", false, false)
bool EarlyCSE::processNode(DomTreeNode *Node) { bool EarlyCSE::processNode(DomTreeNode *Node) {
BasicBlock *BB = Node->getBlock(); BasicBlock *BB = Node->getBlock();
@ -501,7 +485,7 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
Instruction *Inst = I++; Instruction *Inst = I++;
// Dead instructions should just be removed. // Dead instructions should just be removed.
if (isInstructionTriviallyDead(Inst, TLI)) { if (isInstructionTriviallyDead(Inst, &TLI)) {
DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n'); DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n');
Inst->eraseFromParent(); Inst->eraseFromParent();
Changed = true; Changed = true;
@ -520,7 +504,7 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
// If the instruction can be simplified (e.g. X+0 = X) then replace it with // If the instruction can be simplified (e.g. X+0 = X) then replace it with
// its simpler value. // its simpler value.
if (Value *V = SimplifyInstruction(Inst, DL, TLI, DT, AC)) { if (Value *V = SimplifyInstruction(Inst, DL, &TLI, &DT, &AC)) {
DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n'); DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n');
Inst->replaceAllUsesWith(V); Inst->replaceAllUsesWith(V);
Inst->eraseFromParent(); Inst->eraseFromParent();
@ -532,7 +516,7 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
// If this is a simple instruction that we can value number, process it. // If this is a simple instruction that we can value number, process it.
if (SimpleValue::canHandle(Inst)) { if (SimpleValue::canHandle(Inst)) {
// See if the instruction has an available value. If so, use it. // See if the instruction has an available value. If so, use it.
if (Value *V = AvailableValues->lookup(Inst)) { if (Value *V = AvailableValues.lookup(Inst)) {
DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n'); DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n');
Inst->replaceAllUsesWith(V); Inst->replaceAllUsesWith(V);
Inst->eraseFromParent(); Inst->eraseFromParent();
@ -542,7 +526,7 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
} }
// Otherwise, just remember that this value is available. // Otherwise, just remember that this value is available.
AvailableValues->insert(Inst, Inst); AvailableValues.insert(Inst, Inst);
continue; continue;
} }
@ -558,7 +542,7 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
// If we have an available version of this load, and if it is the right // If we have an available version of this load, and if it is the right
// generation, replace this instruction. // generation, replace this instruction.
std::pair<Value *, unsigned> InVal = std::pair<Value *, unsigned> InVal =
AvailableLoads->lookup(MemInst.getPtr()); AvailableLoads.lookup(MemInst.getPtr());
if (InVal.first != nullptr && InVal.second == CurrentGeneration) { if (InVal.first != nullptr && InVal.second == CurrentGeneration) {
Value *Op = getOrCreateResult(InVal.first, Inst->getType()); Value *Op = getOrCreateResult(InVal.first, Inst->getType());
if (Op != nullptr) { if (Op != nullptr) {
@ -574,8 +558,8 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
} }
// Otherwise, remember that we have this instruction. // Otherwise, remember that we have this instruction.
AvailableLoads->insert(MemInst.getPtr(), std::pair<Value *, unsigned>( AvailableLoads.insert(MemInst.getPtr(), std::pair<Value *, unsigned>(
Inst, CurrentGeneration)); Inst, CurrentGeneration));
LastStore = nullptr; LastStore = nullptr;
continue; continue;
} }
@ -593,7 +577,7 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
if (CallValue::canHandle(Inst)) { if (CallValue::canHandle(Inst)) {
// If we have an available version of this call, and if it is the right // If we have an available version of this call, and if it is the right
// generation, replace this instruction. // generation, replace this instruction.
std::pair<Value *, unsigned> InVal = AvailableCalls->lookup(Inst); std::pair<Value *, unsigned> InVal = AvailableCalls.lookup(Inst);
if (InVal.first != nullptr && InVal.second == CurrentGeneration) { if (InVal.first != nullptr && InVal.second == CurrentGeneration) {
DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst
<< " to: " << *InVal.first << '\n'); << " to: " << *InVal.first << '\n');
@ -606,7 +590,7 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
} }
// Otherwise, remember that we have this instruction. // Otherwise, remember that we have this instruction.
AvailableCalls->insert( AvailableCalls.insert(
Inst, std::pair<Value *, unsigned>(Inst, CurrentGeneration)); Inst, std::pair<Value *, unsigned>(Inst, CurrentGeneration));
continue; continue;
} }
@ -638,8 +622,8 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
// version of the pointer. It is safe to forward from volatile stores // version of the pointer. It is safe to forward from volatile stores
// to non-volatile loads, so we don't have to check for volatility of // to non-volatile loads, so we don't have to check for volatility of
// the store. // the store.
AvailableLoads->insert(MemInst.getPtr(), std::pair<Value *, unsigned>( AvailableLoads.insert(MemInst.getPtr(), std::pair<Value *, unsigned>(
Inst, CurrentGeneration)); Inst, CurrentGeneration));
// Remember that this was the last store we saw for DSE. // Remember that this was the last store we saw for DSE.
if (!MemInst.isVolatile()) if (!MemInst.isVolatile())
@ -651,10 +635,7 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
return Changed; return Changed;
} }
bool EarlyCSE::runOnFunction(Function &F) { bool EarlyCSE::run() {
if (skipOptnoneFunction(F))
return false;
// Note, deque is being used here because there is significant performance // Note, deque is being used here because there is significant performance
// gains over vector when the container becomes very large due to the // gains over vector when the container becomes very large due to the
// specific access patterns. For more information see the mailing list // specific access patterns. For more information see the mailing list
@ -662,28 +643,12 @@ bool EarlyCSE::runOnFunction(Function &F) {
// http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120116/135228.html // http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120116/135228.html
std::deque<StackNode *> nodesToProcess; std::deque<StackNode *> nodesToProcess;
DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
DL = DLP ? &DLP->getDataLayout() : nullptr;
TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
TTI = &getAnalysis<TargetTransformInfo>();
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
// Tables that the pass uses when walking the domtree.
ScopedHTType AVTable;
AvailableValues = &AVTable;
LoadHTType LoadTable;
AvailableLoads = &LoadTable;
CallHTType CallTable;
AvailableCalls = &CallTable;
CurrentGeneration = 0;
bool Changed = false; bool Changed = false;
// Process the root node. // Process the root node.
nodesToProcess.push_back(new StackNode( nodesToProcess.push_back(new StackNode(
AvailableValues, AvailableLoads, AvailableCalls, CurrentGeneration, AvailableValues, AvailableLoads, AvailableCalls, CurrentGeneration,
DT->getRootNode(), DT->getRootNode()->begin(), DT->getRootNode()->end())); DT.getRootNode(), DT.getRootNode()->begin(), DT.getRootNode()->end()));
// Save the current generation. // Save the current generation.
unsigned LiveOutGeneration = CurrentGeneration; unsigned LiveOutGeneration = CurrentGeneration;
@ -723,3 +688,57 @@ bool EarlyCSE::runOnFunction(Function &F) {
return Changed; return Changed;
} }
namespace {
/// \brief A simple and fast domtree-based CSE pass.
///
/// This pass does a simple depth-first walk over the dominator tree,
/// eliminating trivially redundant instructions and using instsimplify to
/// canonicalize things as it goes. It is intended to be fast and catch obvious
/// cases so that instcombine and other passes are more effective. It is
/// expected that a later pass of GVN will catch the interesting/hard cases.
class EarlyCSELegacyPass : public FunctionPass {
public:
static char ID;
EarlyCSELegacyPass() : FunctionPass(ID) {
initializeEarlyCSELegacyPassPass(*PassRegistry::getPassRegistry());
}
bool runOnFunction(Function &F) override {
if (skipOptnoneFunction(F))
return false;
DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
auto *DL = DLP ? &DLP->getDataLayout() : nullptr;
auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
auto &TTI = getAnalysis<TargetTransformInfo>();
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
EarlyCSE CSE(F, DL, TLI, TTI, DT, AC);
return CSE.run();
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<AssumptionCacheTracker>();
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<TargetLibraryInfoWrapperPass>();
AU.addRequired<TargetTransformInfo>();
AU.setPreservesCFG();
}
};
}
char EarlyCSELegacyPass::ID = 0;
FunctionPass *llvm::createEarlyCSEPass() { return new EarlyCSELegacyPass(); }
INITIALIZE_PASS_BEGIN(EarlyCSELegacyPass, "early-cse", "Early CSE", false,
false)
INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
INITIALIZE_PASS_END(EarlyCSELegacyPass, "early-cse", "Early CSE", false, false)

View File

@ -38,7 +38,7 @@ void llvm::initializeScalarOpts(PassRegistry &Registry) {
initializeScalarizerPass(Registry); initializeScalarizerPass(Registry);
initializeDSEPass(Registry); initializeDSEPass(Registry);
initializeGVNPass(Registry); initializeGVNPass(Registry);
initializeEarlyCSEPass(Registry); initializeEarlyCSELegacyPassPass(Registry);
initializeFlattenCFGPassPass(Registry); initializeFlattenCFGPassPass(Registry);
initializeInductiveRangeCheckEliminationPass(Registry); initializeInductiveRangeCheckEliminationPass(Registry);
initializeIndVarSimplifyPass(Registry); initializeIndVarSimplifyPass(Registry);