2010-09-17 15:48:55 +00:00
|
|
|
//===- 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"
|
2015-01-18 16:17:27 +00:00
|
|
|
#include "llvm/Analysis/RegionInfoImpl.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/Analysis/RegionIterator.h"
|
2016-07-23 20:41:05 +00:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-09-17 15:48:55 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-12-30 11:46:15 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "llvm/Analysis/RegionPrinter.h"
|
|
|
|
#endif
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
#define DEBUG_TYPE "region"
|
2010-09-17 15:48:55 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
namespace llvm {
|
|
|
|
template class RegionBase<RegionTraits<Function>>;
|
|
|
|
template class RegionNodeBase<RegionTraits<Function>>;
|
|
|
|
template class RegionInfoBase<RegionTraits<Function>>;
|
|
|
|
}
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
STATISTIC(numRegions, "The # of regions");
|
|
|
|
STATISTIC(numSimpleRegions, "The # of simple regions");
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
// 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),
|
2011-05-02 19:34:44 +00:00
|
|
|
cl::Hidden,
|
2010-09-17 15:48:55 +00:00
|
|
|
cl::desc("style of printing regions"),
|
|
|
|
cl::values(
|
2011-05-02 19:34:44 +00:00
|
|
|
clEnumValN(Region::PrintNone, "none", "print no details"),
|
|
|
|
clEnumValN(Region::PrintBB, "bb",
|
2012-12-02 13:10:19 +00:00
|
|
|
"print regions in detail with block_iterator"),
|
2011-05-02 19:34:44 +00:00
|
|
|
clEnumValN(Region::PrintRN, "rn",
|
2017-01-02 19:17:04 +00:00
|
|
|
"print regions in detail with element_iterator")));
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region implementation
|
|
|
|
//
|
2010-09-17 15:48:55 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
Region::Region(BasicBlock *Entry, BasicBlock *Exit,
|
|
|
|
RegionInfo* RI,
|
|
|
|
DominatorTree *DT, Region *Parent) :
|
|
|
|
RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
Region::~Region() { }
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RegionInfo implementation
|
|
|
|
//
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
RegionInfo::RegionInfo() :
|
|
|
|
RegionInfoBase<RegionTraits<Function>>() {
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
RegionInfo::~RegionInfo() {
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-16 16:01:22 +00:00
|
|
|
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>());
|
|
|
|
}
|
|
|
|
|
2010-09-17 15:48:55 +00:00
|
|
|
void RegionInfo::updateStatistics(Region *R) {
|
|
|
|
++numRegions;
|
|
|
|
|
|
|
|
// TODO: Slow. Should only be enabled if -stats is used.
|
2014-11-24 09:08:18 +00:00
|
|
|
if (R->isSimple())
|
|
|
|
++numSimpleRegions;
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
|
|
|
|
PostDominatorTree *PDT_, DominanceFrontier *DF_) {
|
|
|
|
DT = DT_;
|
|
|
|
PDT = PDT_;
|
|
|
|
DF = DF_;
|
2010-09-17 15:48:55 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
|
|
|
|
this, DT, nullptr);
|
|
|
|
updateStatistics(TopLevelRegion);
|
|
|
|
calculate(F);
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void RegionInfo::view() { viewRegion(this); }
|
|
|
|
|
|
|
|
void RegionInfo::viewOnly() { viewRegionOnly(this); }
|
|
|
|
#endif
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RegionInfoPass implementation
|
|
|
|
//
|
2010-09-17 15:48:55 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
|
|
|
|
initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
RegionInfoPass::~RegionInfoPass() {
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
bool RegionInfoPass::runOnFunction(Function &F) {
|
2010-09-17 15:48:55 +00:00
|
|
|
releaseMemory();
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2016-07-23 20:41:05 +00:00
|
|
|
auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
|
|
|
|
auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
|
2010-09-17 15:48:55 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
RI.recalculate(F, DT, PDT, DF);
|
|
|
|
return false;
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
void RegionInfoPass::releaseMemory() {
|
|
|
|
RI.releaseMemory();
|
|
|
|
}
|
2010-09-17 15:48:55 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
void RegionInfoPass::verifyAnalysis() const {
|
|
|
|
RI.verifyAnalysis();
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
2010-09-17 15:48:55 +00:00
|
|
|
AU.setPreservesAll();
|
2014-11-24 09:08:18 +00:00
|
|
|
AU.addRequiredTransitive<DominatorTreeWrapperPass>();
|
2016-07-23 20:41:05 +00:00
|
|
|
AU.addRequired<PostDominatorTreeWrapperPass>();
|
|
|
|
AU.addRequired<DominanceFrontierWrapperPass>();
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
|
|
|
|
RI.print(OS);
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-07-23 20:41:05 +00:00
|
|
|
LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
|
2014-11-24 09:08:18 +00:00
|
|
|
RI.dump();
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
2014-11-24 09:08:18 +00:00
|
|
|
#endif
|
2010-09-17 15:48:55 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
char RegionInfoPass::ID = 0;
|
2011-02-20 12:57:14 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
|
2011-02-20 12:57:14 +00:00
|
|
|
"Detect single entry single exit regions", true, true)
|
2014-11-24 09:08:18 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2016-07-23 20:41:05 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
|
2014-11-24 09:08:18 +00:00
|
|
|
INITIALIZE_PASS_END(RegionInfoPass, "regions",
|
2011-02-20 12:57:14 +00:00
|
|
|
"Detect single entry single exit regions", true, true)
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
// 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() {
|
2014-11-24 09:08:18 +00:00
|
|
|
return new RegionInfoPass();
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RegionInfoAnalysis implementation
|
|
|
|
//
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
AnalysisKey RegionInfoAnalysis::Key;
|
2016-07-23 20:41:05 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
|
2016-07-23 20:41:05 +00:00
|
|
|
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,
|
2017-01-02 19:17:04 +00:00
|
|
|
FunctionAnalysisManager &AM) {
|
2016-07-23 20:41:05 +00:00
|
|
|
AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
|
|
|
|
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|