OpenASIP  2.0
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 
33 #include "ControlFlowGraphPass.hh"
34 #include "Application.hh"
35 #include "ControlFlowGraph.hh"
36 #include "Machine.hh"
37 #include "BasicBlockPass.hh"
38 
39 /**
40  * Constructor.
41  */
43  SchedulerPass(data) {
44 }
45 
46 /**
47  * Destructor.
48  */
50 }
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  */
64 void
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  */
89 void
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 }
SchedulerPass
Definition: SchedulerPass.hh:43
BoostGraph::node
Node & node(const int index) const
BasicBlockNode::isScheduled
bool isScheduled() const
Definition: BasicBlockNode.hh:93
ControlFlowGraphPass::ControlFlowGraphPass
ControlFlowGraphPass(InterPassData &data)
Definition: ControlFlowGraphPass.cc:42
ControlFlowGraph::instructionReferenceManager
TTAProgram::InstructionReferenceManager & instructionReferenceManager()
Definition: ControlFlowGraph.cc:2401
BasicBlockNode::basicBlock
TTAProgram::BasicBlock & basicBlock()
Definition: BasicBlockNode.cc:126
BasicBlockPass
Definition: BasicBlockPass.hh:60
abortWithError
#define abortWithError(message)
Definition: Application.hh:72
BasicBlockPass::handleBasicBlock
virtual void handleBasicBlock(TTAProgram::BasicBlock &basicBlock, const TTAMachine::Machine &targetMachine, TTAProgram::InstructionReferenceManager &irm, BasicBlockNode *bbn=NULL)
Definition: BasicBlockPass.cc:79
ControlFlowGraph.hh
ControlFlowGraphPass::executeBasicBlockPass
void executeBasicBlockPass(ControlFlowGraph &cfg, const TTAMachine::Machine &targetMachine, BasicBlockPass &bbPass)
Definition: ControlFlowGraphPass.cc:90
BasicBlockPass.hh
Application.hh
BasicBlockNode
Definition: BasicBlockNode.hh:64
BasicBlockNode::isNormalBB
bool isNormalBB() const
Definition: BasicBlockNode.cc:239
InterPassData
Definition: InterPassData.hh:48
ControlFlowGraphPass::handleControlFlowGraph
virtual void handleControlFlowGraph(ControlFlowGraph &cfg, const TTAMachine::Machine &targetMachine)
Definition: ControlFlowGraphPass.cc:65
Machine.hh
ControlFlowGraph::isSingleBBLoop
bool isSingleBBLoop(const BasicBlockNode &node) const
Definition: ControlFlowGraph.cc:2919
ControlFlowGraphPass::~ControlFlowGraphPass
virtual ~ControlFlowGraphPass()
Definition: ControlFlowGraphPass.cc:49
BasicBlockNode::setScheduled
void setScheduled(bool state=true)
Definition: BasicBlockNode.hh:94
ControlFlowGraphPass.hh
BoostGraph::nodeCount
int nodeCount() const
ControlFlowGraph
Definition: ControlFlowGraph.hh:100
TTAMachine::Machine
Definition: Machine.hh:73