Skip to content

Commit

Permalink
Add structual analysis by using given CFG and add _hasBackEdges flag …
Browse files Browse the repository at this point in the history
…when generating CFG

This is a part of the BenefitInliner contribution.

* Dominators.hpp, Dominators.cpp, StructuralAnalysis.hpp, StructuralAnalysis.cpp: add structural analysis by using given CFG.
* OMRCfg.hpp: add _hasBackEdges flag to mark having loop in the CFG.

Co-authored-by: Cijie Xia <[email protected]>
Signed-off-by: Mingwei Li <[email protected]>
  • Loading branch information
mingweiarthurli and xiacijie committed Feb 16, 2024
1 parent 8414472 commit d2d79a1
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/infra/OMRCfg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class CFG
_calledFrequency = 0;
_initialBlockFrequency = -1;
_edgeProbabilities = NULL;
_hasBackEdges = false;
}

TR::CFG * self();
Expand Down Expand Up @@ -357,6 +358,9 @@ class CFG
IsOrphanedRegion
};

void setHasBackEdges() { _hasBackEdges = true; }
bool hasBackEdges() { return _hasBackEdges; }

protected:
TR::Compilation *_compilation;
TR::ResolvedMethodSymbol *_method;
Expand All @@ -376,6 +380,7 @@ class CFG
bool _ignoreUnreachableBlocks;
bool _removingUnreachableBlocks;

bool _hasBackEdges;

TR::CFGNode **_forwardTraversalOrder;
int32_t _forwardTraversalLength;
Expand Down
81 changes: 81 additions & 0 deletions compiler/optimizer/Dominators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,87 @@ TR_Dominators::TR_Dominators(TR::Compilation *c, bool post) :
_info.clear();
}

TR_Dominators::TR_Dominators(TR::Compilation *c, TR::CFG* cfg, bool post) :
_region(c->trMemory()->heapMemoryRegion()),
_compilation(c),
_info(cfg->getNextNodeNumber()+1, BBInfo(_region), _region),
_dfNumbers(cfg->getNextNodeNumber()+1, 0, _region),
_dominators(cfg->getNextNodeNumber()+1, static_cast<TR::Block *>(NULL), _region)
{
LexicalTimer tlex("TR_Dominators::TR_Dominators", _compilation->phaseTimer());

_postDominators = post;
_isValid = true;
_topDfNum = 0;
_visitCount = c->incOrResetVisitCount();
_trace = comp()->getOption(TR_TraceDominators);

_cfg = cfg;
_numNodes = cfg->getNumberOfNodes()+1;

if (trace())
{
traceMsg(comp(), "Starting %sdominator calculation\n", _postDominators ? "post-" : "");
traceMsg(comp(), " Number of nodes is %d\n", _numNodes-1);
}

if (_postDominators)
_dfNumbers[cfg->getStart()->getNumber()] = -1;
else
_dfNumbers[cfg->getEnd()->getNumber()] = -1;

findDominators(toBlock( _postDominators ? cfg->getEnd() : cfg->getStart() ));

int32_t i;
for (i = _topDfNum; i > 1; i--)
{
BBInfo &info = getInfo(i);
TR::Block *dominated = info._block;
TR::Block *dominator = getInfo(info._idom)._block;
_dominators[dominated->getNumber()] = dominator;
if (trace())
traceMsg(comp(), " %sDominator of block_%d is block_%d\n", _postDominators ? "post-" : "",
dominated->getNumber(), dominator->getNumber());
}

// The exit block may not be reachable from the entry node. In this case just
// give the exit block the highest depth-first numbering.
// No other blocks should be unreachable.
//

if (_postDominators)
{
if (_dfNumbers[cfg->getStart()->getNumber()] < 0)
_dfNumbers[cfg->getStart()->getNumber()] = _topDfNum++;
}
else
{
if (_dfNumbers[cfg->getEnd()->getNumber()] < 0)
_dfNumbers[cfg->getEnd()->getNumber()] = _topDfNum++;
}

// Assert that we've found every node in the cfg.
//
if (_topDfNum != _numNodes-1)
{
_isValid = false;
return;
}

#if DEBUG
for (block = toBlock(cfg->getFirstNode()); block; block = toBlock(block->getNext()))
{
TR_ASSERT(_dfNumbers[block->getNumber()] >= 0, "Unreachable block in the CFG");
}
#endif

if (trace())
traceMsg(comp(), "End of %sdominator calculation\n", _postDominators ? "post-" : "");

// Release no-longer-used data
_info.clear();
}

TR::Block * TR_Dominators::getDominator(TR::Block *block)
{
if (block->getNumber() >= _dominators.size())
Expand Down
2 changes: 2 additions & 0 deletions compiler/optimizer/Dominators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ class TR_Dominators
TR_ALLOC(TR_Memory::Dominators)

TR_Dominators(TR::Compilation *, bool post = false);
TR_Dominators(TR::Compilation *, TR::CFG* cfg, bool post = false);
TR::Block *getDominator(TR::Block *);
int dominates(TR::Block *block, TR::Block *other);

TR::Compilation * comp() { return _compilation; }
bool trace() { return _trace; }
bool isValid() { return _isValid; }

protected:

Expand Down
44 changes: 44 additions & 0 deletions compiler/optimizer/StructuralAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,50 @@ void TR_RegionAnalysis::createLeafStructures(TR::CFG *cfg, TR::Region &region)
}
}

/**
* Mainline for performing Region Analysis.
*/
TR_Structure *TR_RegionAnalysis::getRegions(TR::Compilation *comp, TR::CFG* cfg)
{
TR::StackMemoryRegion stackMemoryRegion(*comp->trMemory());

// Calculate dominators
// This has the side-effect of renumbering the blocks in depth-first order
//
TR_Dominators dominators = TR_Dominators(comp, cfg);

if (!dominators.isValid())
return NULL;

#if DEBUG
if (debug("verifyDominator"))
{
TR_DominatorVerifier verifyDominator(dominators);
}
#endif

TR_ASSERT(cfg, "cfg is NULL\n");

TR_RegionAnalysis ra(comp, dominators, cfg, stackMemoryRegion);
ra._trace = comp->getOption(TR_TraceSA);

ra._useNew = !comp->getOption(TR_DisableIterativeSA);
if (ra.trace())
{
traceMsg(comp, "Blocks before Region Analysis:\n");
comp->getDebug()->print(comp->getOutFile(), cfg);
}

ra.createLeafStructures(cfg, stackMemoryRegion);

// Loop through the node set until there is only one node left - this is the
// root of the control tree.
//
TR_Structure *result = ra.findRegions(stackMemoryRegion);

return result;
}

/**
* Mainline for performing Region Analysis.
*/
Expand Down
1 change: 1 addition & 0 deletions compiler/optimizer/StructuralAnalysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class TR_RegionAnalysis

static TR_Structure *getRegions(TR::Compilation *);
static TR_Structure *getRegions(TR::Compilation *, TR::ResolvedMethodSymbol *);
static TR_Structure *getRegions(TR::Compilation *, TR::CFG *);

friend class TR_Debug;

Expand Down

0 comments on commit d2d79a1

Please sign in to comment.