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"
|
|
|
|
#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"
|
2010-07-22 07:46:31 +00:00
|
|
|
#include <algorithm>
|
2014-04-15 18:32:43 +00:00
|
|
|
#include <iterator>
|
2013-08-21 21:14:19 +00:00
|
|
|
#include <set>
|
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",
|
|
|
|
"print regions in detail with element_iterator"),
|
2010-07-22 07:46:31 +00:00
|
|
|
clEnumValEnd));
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
auto PDT = &getAnalysis<PostDominatorTree>();
|
|
|
|
auto DF = &getAnalysis<DominanceFrontier>();
|
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>();
|
2010-07-22 07:46:31 +00:00
|
|
|
AU.addRequired<PostDominatorTree>();
|
|
|
|
AU.addRequired<DominanceFrontier>();
|
|
|
|
}
|
|
|
|
|
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)
|
2014-07-19 18:29:29 +00:00
|
|
|
void RegionInfoPass::dump() const {
|
|
|
|
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)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|