llvm/lib/Analysis/RegionInfo.cpp

222 lines
6.4 KiB
C++
Raw Normal View History

//===- RegionInfo.cpp - SESE region detection analysis --------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Detects single entry single exit regions in the control flow graph.
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/RegionInfo.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/RegionInfoImpl.h"
#include "llvm/Analysis/RegionIterator.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#ifndef NDEBUG
#include "llvm/Analysis/RegionPrinter.h"
#endif
using namespace llvm;
#define DEBUG_TYPE "region"
namespace llvm {
template class RegionBase<RegionTraits<Function>>;
template class RegionNodeBase<RegionTraits<Function>>;
template class RegionInfoBase<RegionTraits<Function>>;
}
STATISTIC(numRegions, "The # of regions");
STATISTIC(numSimpleRegions, "The # of simple regions");
// Always verify if expensive checking is enabled.
static cl::opt<bool,true>
VerifyRegionInfoX(
"verify-region-info",
cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
cl::desc("Verify region info (time consuming)"));
static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
cl::location(RegionInfo::printStyle),
cl::Hidden,
cl::desc("style of printing regions"),
cl::values(
clEnumValN(Region::PrintNone, "none", "print no details"),
clEnumValN(Region::PrintBB, "bb",
"print regions in detail with block_iterator"),
clEnumValN(Region::PrintRN, "rn",
"print regions in detail with element_iterator")));
//===----------------------------------------------------------------------===//
// Region implementation
//
Region::Region(BasicBlock *Entry, BasicBlock *Exit,
RegionInfo* RI,
DominatorTree *DT, Region *Parent) :
RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
}
Region::~Region() { }
//===----------------------------------------------------------------------===//
// RegionInfo implementation
//
RegionInfo::RegionInfo() :
RegionInfoBase<RegionTraits<Function>>() {
}
RegionInfo::~RegionInfo() {
}
bool RegionInfo::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {
// Check whether the analysis, all analyses on functions, or the function's
// CFG have been preserved.
auto PAC = PA.getChecker<RegionInfoAnalysis>();
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
PAC.preservedSet<CFGAnalyses>());
}
void RegionInfo::updateStatistics(Region *R) {
++numRegions;
// TODO: Slow. Should only be enabled if -stats is used.
if (R->isSimple())
++numSimpleRegions;
}
void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
PostDominatorTree *PDT_, DominanceFrontier *DF_) {
DT = DT_;
PDT = PDT_;
DF = DF_;
TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
this, DT, nullptr);
updateStatistics(TopLevelRegion);
calculate(F);
}
#ifndef NDEBUG
void RegionInfo::view() { viewRegion(this); }
void RegionInfo::viewOnly() { viewRegionOnly(this); }
#endif
//===----------------------------------------------------------------------===//
// RegionInfoPass implementation
//
RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
}
RegionInfoPass::~RegionInfoPass() {
}
bool RegionInfoPass::runOnFunction(Function &F) {
releaseMemory();
auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
RI.recalculate(F, DT, PDT, DF);
return false;
}
void RegionInfoPass::releaseMemory() {
RI.releaseMemory();
}
void RegionInfoPass::verifyAnalysis() const {
RI.verifyAnalysis();
}
void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequiredTransitive<DominatorTreeWrapperPass>();
AU.addRequired<PostDominatorTreeWrapperPass>();
AU.addRequired<DominanceFrontierWrapperPass>();
}
void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
RI.print(OS);
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
RI.dump();
}
#endif
char RegionInfoPass::ID = 0;
INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
"Detect single entry single exit regions", true, true)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
INITIALIZE_PASS_END(RegionInfoPass, "regions",
"Detect single entry single exit regions", true, true)
// Create methods available outside of this file, to use them
// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
// the link time optimization.
namespace llvm {
FunctionPass *createRegionInfoPass() {
return new RegionInfoPass();
}
}
//===----------------------------------------------------------------------===//
// RegionInfoAnalysis implementation
//
[PM] Change the static object whose address is used to uniquely identify analyses to have a common type which is enforced rather than using a char object and a `void *` type when used as an identifier. This has a number of advantages. First, it at least helps some of the confusion raised in Justin Lebar's code review of why `void *` was being used everywhere by having a stronger type that connects to documentation about this. However, perhaps more importantly, it addresses a serious issue where the alignment of these pointer-like identifiers was unknown. This made it hard to use them in pointer-like data structures. We were already dodging this in dangerous ways to create the "all analyses" entry. In a subsequent patch I attempted to use these with TinyPtrVector and things fell apart in a very bad way. And it isn't just a compile time or type system issue. Worse than that, the actual alignment of these pointer-like opaque identifiers wasn't guaranteed to be a useful alignment as they were just characters. This change introduces a type to use as the "key" object whose address forms the opaque identifier. This both forces the objects to have proper alignment, and provides type checking that we get it right everywhere. It also makes the types somewhat less mysterious than `void *`. We could go one step further and introduce a truly opaque pointer-like type to return from the `ID()` static function rather than returning `AnalysisKey *`, but that didn't seem to be a clear win so this is just the initial change to get to a reliably typed and aligned object serving is a key for all the analyses. Thanks to Richard Smith and Justin Lebar for helping pick plausible names and avoid making this refactoring many times. =] And thanks to Sean for the super fast review! While here, I've tried to move away from the "PassID" nomenclature entirely as it wasn't really helping and is overloaded with old pass manager constructs. Now we have IDs for analyses, and key objects whose address can be used as IDs. Where possible and clear I've shortened this to just "ID". In a few places I kept "AnalysisID" to make it clear what was being identified. Differential Revision: https://reviews.llvm.org/D27031 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@287783 91177308-0d34-0410-b5e6-96231b3b80d8
2016-11-23 17:53:26 +00:00
AnalysisKey RegionInfoAnalysis::Key;
RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
RegionInfo RI;
auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
auto *DF = &AM.getResult<DominanceFrontierAnalysis>(F);
RI.recalculate(F, DT, PDT, DF);
return RI;
}
RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream &OS)
: OS(OS) {}
PreservedAnalyses RegionInfoPrinterPass::run(Function &F,
FunctionAnalysisManager &AM) {
OS << "Region Tree for function: " << F.getName() << "\n";
AM.getResult<RegionInfoAnalysis>(F).print(OS);
return PreservedAnalyses::all();
}
PreservedAnalyses RegionInfoVerifierPass::run(Function &F,
FunctionAnalysisManager &AM) {
AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
return PreservedAnalyses::all();
}