2010-07-22 07:46:31 +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"
|
2010-07-27 04:17:13 +00:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2015-01-14 11:23:27 +00:00
|
|
|
#include "llvm/Analysis/RegionInfoImpl.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Analysis/RegionIterator.h"
|
2016-02-25 17:54:25 +00:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-07-22 07:46:31 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-08-10 13:21:59 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "llvm/Analysis/RegionPrinter.h"
|
|
|
|
#endif
|
2010-07-22 07:46:31 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:48:03 +00:00
|
|
|
#define DEBUG_TYPE "region"
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
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");
|
|
|
|
|
2010-07-22 07:46:31 +00:00
|
|
|
// Always verify if expensive checking is enabled.
|
|
|
|
|
|
|
|
static cl::opt<bool,true>
|
2014-07-19 18:29:29 +00:00
|
|
|
VerifyRegionInfoX(
|
|
|
|
"verify-region-info",
|
|
|
|
cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
|
|
|
|
cl::desc("Verify region info (time consuming)"));
|
2010-07-22 07:46:31 +00:00
|
|
|
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
|
|
|
|
cl::location(RegionInfo::printStyle),
|
2011-04-04 07:19:18 +00:00
|
|
|
cl::Hidden,
|
2010-07-22 07:46:31 +00:00
|
|
|
cl::desc("style of printing regions"),
|
|
|
|
cl::values(
|
2011-04-04 07:19:18 +00:00
|
|
|
clEnumValN(Region::PrintNone, "none", "print no details"),
|
|
|
|
clEnumValN(Region::PrintBB, "bb",
|
2012-08-27 13:49:24 +00:00
|
|
|
"print regions in detail with block_iterator"),
|
2011-04-04 07:19:18 +00:00
|
|
|
clEnumValN(Region::PrintRN, "rn",
|
2016-10-08 19:41:06 +00:00
|
|
|
"print regions in detail with element_iterator")));
|
2010-07-22 07:46:31 +00:00
|
|
|
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region implementation
|
|
|
|
//
|
2010-07-22 07:46:31 +00:00
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
Region::Region(BasicBlock *Entry, BasicBlock *Exit,
|
|
|
|
RegionInfo* RI,
|
|
|
|
DominatorTree *DT, Region *Parent) :
|
|
|
|
RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
|
2010-07-22 07:46:31 +00:00
|
|
|
|
|
|
|
}
|
2010-10-13 00:07:59 +00:00
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
Region::~Region() { }
|
2010-07-22 07:46:31 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RegionInfo implementation
|
|
|
|
//
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
RegionInfo::RegionInfo() :
|
|
|
|
RegionInfoBase<RegionTraits<Function>>() {
|
2010-07-22 07:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
RegionInfo::~RegionInfo() {
|
2010-07-22 07:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-15 06:32:49 +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-07-22 07:46:31 +00:00
|
|
|
void RegionInfo::updateStatistics(Region *R) {
|
|
|
|
++numRegions;
|
|
|
|
|
|
|
|
// TODO: Slow. Should only be enabled if -stats is used.
|
2014-07-19 18:29:29 +00:00
|
|
|
if (R->isSimple())
|
|
|
|
++numSimpleRegions;
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 03:57:51 +00:00
|
|
|
void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
|
|
|
|
PostDominatorTree *PDT_, DominanceFrontier *DF_) {
|
2014-07-19 18:29:29 +00:00
|
|
|
DT = DT_;
|
|
|
|
PDT = PDT_;
|
|
|
|
DF = DF_;
|
2010-07-22 07:46:31 +00:00
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
|
|
|
|
this, DT, nullptr);
|
|
|
|
updateStatistics(TopLevelRegion);
|
|
|
|
calculate(F);
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 13:21:59 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void RegionInfo::view() { viewRegion(this); }
|
|
|
|
|
|
|
|
void RegionInfo::viewOnly() { viewRegionOnly(this); }
|
|
|
|
#endif
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RegionInfoPass implementation
|
|
|
|
//
|
2010-07-22 07:46:31 +00:00
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
|
|
|
|
initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
RegionInfoPass::~RegionInfoPass() {
|
2010-07-22 07:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
bool RegionInfoPass::runOnFunction(Function &F) {
|
2010-07-22 07:46:31 +00:00
|
|
|
releaseMemory();
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2016-02-25 17:54:07 +00:00
|
|
|
auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
|
2016-02-25 17:54:15 +00:00
|
|
|
auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
|
2010-07-22 07:46:31 +00:00
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
RI.recalculate(F, DT, PDT, DF);
|
|
|
|
return false;
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
void RegionInfoPass::releaseMemory() {
|
|
|
|
RI.releaseMemory();
|
|
|
|
}
|
2010-07-22 07:46:31 +00:00
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
void RegionInfoPass::verifyAnalysis() const {
|
|
|
|
RI.verifyAnalysis();
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
2010-07-22 07:46:31 +00:00
|
|
|
AU.setPreservesAll();
|
2014-01-13 13:07:17 +00:00
|
|
|
AU.addRequiredTransitive<DominatorTreeWrapperPass>();
|
2016-02-25 17:54:07 +00:00
|
|
|
AU.addRequired<PostDominatorTreeWrapperPass>();
|
2016-02-25 17:54:15 +00:00
|
|
|
AU.addRequired<DominanceFrontierWrapperPass>();
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
|
|
|
|
RI.print(OS);
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 00:00:42 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-29 20:50:44 +00:00
|
|
|
LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
|
2014-07-19 18:29:29 +00:00
|
|
|
RI.dump();
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
2014-07-20 00:00:42 +00:00
|
|
|
#endif
|
2010-07-22 07:46:31 +00:00
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
char RegionInfoPass::ID = 0;
|
2010-10-13 05:54:13 +00:00
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
|
2010-10-12 19:48:12 +00:00
|
|
|
"Detect single entry single exit regions", true, true)
|
2014-01-13 13:07:17 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2016-02-25 17:54:07 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
|
2016-02-25 17:54:15 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
|
2014-07-19 18:29:29 +00:00
|
|
|
INITIALIZE_PASS_END(RegionInfoPass, "regions",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Detect single entry single exit regions", true, true)
|
2010-07-22 07:46:31 +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-07-19 18:29:29 +00:00
|
|
|
return new RegionInfoPass();
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 17:54:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RegionInfoAnalysis implementation
|
|
|
|
//
|
|
|
|
|
2016-11-23 17:53:26 +00:00
|
|
|
AnalysisKey RegionInfoAnalysis::Key;
|
2016-02-28 17:17:00 +00:00
|
|
|
|
2016-08-09 00:28:15 +00:00
|
|
|
RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
|
2016-02-25 17:54:25 +00:00
|
|
|
RegionInfo RI;
|
2016-03-11 11:05:24 +00:00
|
|
|
auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
|
|
|
|
auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
|
|
|
|
auto *DF = &AM.getResult<DominanceFrontierAnalysis>(F);
|
2016-02-25 17:54:25 +00:00
|
|
|
|
|
|
|
RI.recalculate(F, DT, PDT, DF);
|
|
|
|
return RI;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream &OS)
|
|
|
|
: OS(OS) {}
|
|
|
|
|
2016-03-11 11:05:24 +00:00
|
|
|
PreservedAnalyses RegionInfoPrinterPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
2016-02-25 17:54:25 +00:00
|
|
|
OS << "Region Tree for function: " << F.getName() << "\n";
|
2016-03-11 11:05:24 +00:00
|
|
|
AM.getResult<RegionInfoAnalysis>(F).print(OS);
|
2016-02-25 17:54:25 +00:00
|
|
|
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses RegionInfoVerifierPass::run(Function &F,
|
2016-08-09 00:28:15 +00:00
|
|
|
FunctionAnalysisManager &AM) {
|
2016-03-11 11:05:24 +00:00
|
|
|
AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
|
2016-02-25 17:54:25 +00:00
|
|
|
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|