OpenASIP 2.2
Loading...
Searching...
No Matches
ControlFlowGraphPass.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file ControlFlowGraphPass.cc
26 *
27 * Definition of ControlFlowGraphPass class.
28 *
29 * @author Pekka Jääskeläinen 2007 (pjaaskel-no.spam-cs.tut.fi)
30 * @note rating: red
31 */
32
34#include "Application.hh"
35#include "ControlFlowGraph.hh"
36#include "Machine.hh"
37#include "BasicBlockPass.hh"
38
39/**
40 * Constructor.
41 */
45
46/**
47 * Destructor.
48 */
51
52/**
53 * Handles a single control flow graph.
54 *
55 * The pass should work with any kind of control flow graph, it should not
56 * assume the CFG represents a whole procedure, for example.
57 *
58 * @param cfg The control flow graph to handle.
59 * @param machine The target machine if any. (NullMachine::instance() if
60 * target machine is irrelevant).
61 * @exception In case handling is unsuccesful for any reason (cfg might
62 * still get modified).
63 */
64void
66 ControlFlowGraph& cfg, const TTAMachine::Machine& targetMachine) {
67 BasicBlockPass* bbPass = dynamic_cast<BasicBlockPass*>(this);
68 if (bbPass != NULL) {
69 executeBasicBlockPass(cfg, targetMachine, *bbPass);
70 } else {
71 abortWithError("CFG Pass is not also a BB pass so you "
72 "must overload handleControlFlowGraph method!");
73 }
74}
75
76/**
77 * Executes the given basic block pass on each basic block of the given
78 * control flow graph in the original program order.
79 *
80 * A helper function for implementing most simplest types of CFG passes.
81 *
82 * @param cfg The control flow graph to handle.
83 * @param targetMachine The target machine, if any. (NullMachine::instance() if
84 * target machine is irrelevant).
85 * @param bbPass The basic block pass to execute.
86 * @exception In case handling is unsuccesful for any reason (cfg might
87 * still get modified).
88 */
89void
91 ControlFlowGraph& cfg, const TTAMachine::Machine& targetMachine,
92 BasicBlockPass& bbPass) {
93 // first schedule all inner-loop basic blocks.
94 int nodeCount = cfg.nodeCount();
95 for (int bbIndex = 0; bbIndex < nodeCount; ++bbIndex) {
96 BasicBlockNode& bb = dynamic_cast<BasicBlockNode&>(cfg.node(bbIndex));
97 if (!bb.isNormalBB() || bb.isScheduled() || !cfg.isSingleBBLoop(bb)) {
98 continue;
99 }
100 bbPass.handleBasicBlock(
101 bb.basicBlock(), targetMachine,
102 cfg.instructionReferenceManager(), &bb);
103 bb.setScheduled();
104 // if some node is removed, make sure does not skip some node and
105 // then try to handle too many nodes.
106 if (cfg.nodeCount() != nodeCount) {
107 nodeCount = cfg.nodeCount();
108 bbIndex = 0;
109 }
110 }
111
112 // then other basic blocks.
113 nodeCount = cfg.nodeCount();
114 for (int bbIndex = 0; bbIndex < nodeCount; ++bbIndex) {
115 BasicBlockNode& bb = dynamic_cast<BasicBlockNode&>(cfg.node(bbIndex));
116 if (!bb.isNormalBB())
117 continue;
118 if (bb.isScheduled()) {
119 continue;
120 }
121
122 bbPass.handleBasicBlock(
123 bb.basicBlock(), targetMachine,
124 cfg.instructionReferenceManager(), &bb);
125 bb.setScheduled();
126 // if some node is removed, make sure does not skip some node and
127 // then try to handle too many nodes.
128 if (cfg.nodeCount() != nodeCount) {
129 nodeCount = cfg.nodeCount();
130 bbIndex = 0;
131 }
132 }
133}
#define abortWithError(message)
void setScheduled(bool state=true)
TTAProgram::BasicBlock & basicBlock()
bool isNormalBB() const
bool isScheduled() const
virtual void handleBasicBlock(TTAProgram::BasicBlock &basicBlock, const TTAMachine::Machine &targetMachine, TTAProgram::InstructionReferenceManager &irm, BasicBlockNode *bbn=NULL)
int nodeCount() const
Node & node(const int index) const
ControlFlowGraphPass(InterPassData &data)
virtual void handleControlFlowGraph(ControlFlowGraph &cfg, const TTAMachine::Machine &targetMachine)
void executeBasicBlockPass(ControlFlowGraph &cfg, const TTAMachine::Machine &targetMachine, BasicBlockPass &bbPass)
TTAProgram::InstructionReferenceManager & instructionReferenceManager()
bool isSingleBBLoop(const BasicBlockNode &node) const