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