OpenASIP  2.0
ProcedurePass.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 ProcedurePass.cc
26  *
27  * Definition of ProcedurePass class.
28  *
29  * @author Pekka J��skel�inen 2007 (pjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include "ProcedurePass.hh"
34 #include "Application.hh"
35 #include "Procedure.hh"
36 #include "Machine.hh"
37 #include "BasicBlockPass.hh"
38 #include "ControlFlowGraph.hh"
39 #include "ControlUnit.hh"
41 #include "ControlFlowGraphPass.hh"
42 #include "InstructionReference.hh"
44 #include "TerminalFUPort.hh"
45 #include "Program.hh"
46 #include "Instruction.hh"
47 #include "BasicBlock.hh"
48 #include "Move.hh"
49 
50 /**
51  * Constructor.
52  */
54  SchedulerPass(data) {
55 }
56 
57 /**
58  * Destructor.
59  */
61 }
62 
63 /**
64  * Handles a single procedure.
65  *
66  * @param proecude The procedure to handle.
67  * @param machine The target machine if any. (NullMachine::instance() if
68  * target machine is irrelevant).
69  * @exception In case handling is unsuccesful for any reason (procedure might
70  * still get modified).
71  */
72 void
74  TTAProgram::Procedure& procedure,
75  const TTAMachine::Machine& targetMachine) {
76  ControlFlowGraphPass* cfgPass = dynamic_cast<ControlFlowGraphPass*>(this);
77  if (cfgPass != NULL) {
78  executeControlFlowGraphPass(procedure, targetMachine, *cfgPass);
79  } else {
80  abortWithError("Procedure pass is not also a cfg pass so you "
81  "must overload handleProcedure method!");
82  }
83 }
84 
85 void
87  TTAProgram::Procedure& procedure, ControlFlowGraph& cfg) {
89  procedure.parent().instructionReferenceManager();
90 
91  int insCountDelta = 0;
92 
93  // collect the starting point instructions to keep as place holders
94  typedef std::map<const BasicBlockNode*, TTAProgram::Instruction*,
95  BasicBlockNode::Comparator> PlaceHolders;
96  PlaceHolders placeHolders;
97 
98  // store pointers to al original instrs so we can remove them
99  std::set<TTAProgram::Instruction*> originalInstr;
100  for (int i = 0; i < procedure.instructionCount(); i++) {
101  originalInstr.insert(&procedure.instructionAtIndex(i));
102  }
103 
104  for (int bbIndex = 0; bbIndex < cfg.nodeCount(); ++bbIndex) {
105  BasicBlockNode& bb = dynamic_cast<BasicBlockNode&>(cfg.node(bbIndex));
106  if (!bb.isNormalBB())
107  continue;
108  if (!bb.hasOriginalAddress())
109  throw Exception(
110  __FILE__, __LINE__, __func__,
111  "Cannot replace a basic block without original address.");
112  placeHolders[&bb] = &procedure.instructionAt(bb.originalStartAddress());
113  }
114 
115  for (int bbIndex = 0; bbIndex < cfg.nodeCount(); ++bbIndex) {
116  BasicBlockNode& bb = dynamic_cast<BasicBlockNode&>(cfg.node(bbIndex));
117 
118  if (!bb.isNormalBB())
119  continue;
120 
121  PlaceHolders::iterator placeHolderIter = placeHolders.find(&bb);
122  TTAProgram::Instruction& placeHolder = *placeHolderIter->second;
123  placeHolders.erase(placeHolderIter);
124 
125  // Remove all original instructions (after the place holder first
126  // instruction) from the procedure as we are dealing with basic blocks,
127  // we can assume that only at most the first instruction is referred to.
128  const int originalBBSize =
130  int addr = placeHolder.address().location() + 1;
131  for (int i = 1; i < originalBBSize; ++i) {
133  procedure.instructionAt(addr);
134  originalInstr.erase(&ins);
135  procedure.CodeSnippet::remove(ins);
136  delete &ins;
137  insCountDelta--;
138  }
139 
140  TTAProgram::Instruction* firstNewInstruction = NULL;
141  TTAProgram::Instruction* lastNewInstruction = &placeHolder;
142 
143  // consistency check.
144  for (int i = 0; i < bb.basicBlock().skippedFirstInstructions(); i++) {
147  if (irm.hasReference(ins)) {
148  throw IllegalProgram(__FILE__,__LINE__, __func__,
149  "Skipped ins has a ref");
150  }
151  }
152 
153  // Copy the instructions from the scheduled basic block back
154  // to the program.
155  for (int i = bb.basicBlock().skippedFirstInstructions();
156  i < bb.basicBlock().instructionCount(); ++i) {
157  TTAProgram::Instruction& instrToCopy =
159  TTAProgram::Instruction* newInstruction = instrToCopy.copy();
160  procedure.insertAfter(
161  *lastNewInstruction, newInstruction);
162  // update references..
163  if (irm.hasReference(instrToCopy)) {
164  irm.replace(instrToCopy,*newInstruction);
165  }
166  lastNewInstruction = newInstruction;
167  if (firstNewInstruction == NULL)
168  firstNewInstruction = newInstruction;
169  }
170 
171  // replace all references to the placeholder instruction to point to
172  // the new first instruction
174  placeHolder)) {
175  assert(firstNewInstruction != NULL);
177  placeHolder, *firstNewInstruction);
178  }
179  // now we can delete also the place holder old instruction
180  // @todo what if the instruction itself had a reference?
181  originalInstr.erase(&placeHolder);
182 
183  // might have code labels so do not optimize this
184  procedure.remove(placeHolder);
185  delete &placeHolder;
186  // ...and we're done with this basic block
187  }
188 
189  // refs to dead instructions are dead refs but there is no way to
190  // remove insr refs. so replace them to refs to first instr.
191 // TTAProgram::Instruction& firstIns = procedure.instructionAtIndex(0);
192 
193  // delete dead instructios (ones from the original procedure
194  for(std::set<TTAProgram::Instruction*>::iterator i =
195  originalInstr.begin(); i != originalInstr.end(); i++) {
196  TTAProgram::Instruction* ins = *i;
197  assert(!irm.hasReference(*ins));
198  procedure.CodeSnippet::remove(*ins);
199  delete ins;
200  insCountDelta--;
201  }
202 
203  // fix the addresses of following procedures.
204  if (insCountDelta != 0) {
205  if (procedure.isInProgram()) {
206  if (!(&procedure == &procedure.parent().lastProcedure())) {
207  procedure.parent().moveProcedure(
208  procedure.parent().nextProcedure(procedure),
209  insCountDelta);
210  }
211  }
212  }
213 
214  // scan for meaningless jumps and remove them.
215  // currently does not work for jump addresses in long immediates,
216  // just ignores them.
217  // but this gets rid of unnecessary jumps after tce side if conversion.
218  for (int i = 0; i < procedure.instructionCount()-1; i++) {
219  TTAProgram::Instruction& ins = procedure.instructionAtIndex(i);
220  for (int j = 0; j < ins.moveCount(); j++) {
221  TTAProgram::Move& move = ins.move(j);
222  // jump those target is easily known?
223  if (move.isJump() && move.source().isInstructionAddress()) {
226  move.source());
227  TTAProgram::Instruction& targetIns =
230  dynamic_cast<TTAProgram::TerminalFUPort&>(
231  move.destination());
232  const TTAMachine::FunctionUnit* fu = &tfp.functionUnit();
233  const TTAMachine::ControlUnit* gcu =
234  dynamic_cast<const TTAMachine::ControlUnit*>(fu);
235 
236  // index of the instruction after delay slots
237  int nextIndex = gcu == NULL ?
238  i+1 :
239  i+1+ gcu->delaySlots();
240 
241  // is index inside the procedure?
242  if (nextIndex < procedure.instructionCount()) {
243  TTAProgram::Instruction& nextIns =
244  procedure.instructionAtIndex(nextIndex);
245  // if jump to next ins, remove jump
246  if (&targetIns == &nextIns) {
247  ins.removeMove(move);
248  delete &move;
249  }
250  }
251  }
252  }
253  }
254 }
255 
256 void
258  TTAProgram::Procedure& procedure, const TTAMachine::Machine& targetMachine,
259  ControlFlowGraphPass& cfgp) {
260  ControlFlowGraph cfg(procedure);
261  cfgp.handleControlFlowGraph(cfg, targetMachine);
262  copyCfgToProcedure(procedure, cfg);
263 }
TTAProgram::TerminalInstructionAddress
Definition: TerminalInstructionAddress.hh:45
TTAProgram::Instruction::removeMove
void removeMove(Move &move)
Definition: Instruction.cc:536
TTAProgram::InstructionReference::instruction
Instruction & instruction() const
Definition: InstructionReference.cc:138
SchedulerPass
Definition: SchedulerPass.hh:43
TTAProgram::TerminalFUPort::functionUnit
virtual const TTAMachine::FunctionUnit & functionUnit() const
Definition: TerminalFUPort.cc:202
TTAProgram::Instruction::move
Move & move(int i) const
Definition: Instruction.cc:193
TTAProgram::Terminal::isInstructionAddress
virtual bool isInstructionAddress() const
Definition: Terminal.cc:87
TTAProgram::Procedure::remove
void remove(Instruction &ins)
Definition: Procedure.cc:297
BoostGraph::node
Node & node(const int index) const
BasicBlockNode::originalEndAddress
InstructionAddress originalEndAddress() const
Definition: BasicBlockNode.cc:174
TTAProgram::Instruction
Definition: Instruction.hh:57
Procedure.hh
TTAProgram::Move::destination
Terminal & destination() const
Definition: Move.cc:323
ProcedurePass::~ProcedurePass
virtual ~ProcedurePass()
Definition: ProcedurePass.cc:60
TTAProgram::Procedure::insertAfter
void insertAfter(const Instruction &pos, Instruction *ins)
Definition: Procedure.cc:193
TTAProgram::Terminal::instructionReference
virtual const InstructionReference & instructionReference() const
Definition: Terminal.cc:188
BasicBlockNode::basicBlock
TTAProgram::BasicBlock & basicBlock()
Definition: BasicBlockNode.cc:126
ProcedurePass::handleProcedure
virtual void handleProcedure(TTAProgram::Procedure &procedure, const TTAMachine::Machine &targetMachine)
Definition: ProcedurePass.cc:73
assert
#define assert(condition)
Definition: Application.hh:86
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
TTAProgram::Program::nextProcedure
Procedure & nextProcedure(const Procedure &proc) const
Definition: Program.cc:250
TTAProgram::BasicBlock::skippedFirstInstructions
int skippedFirstInstructions() const
Definition: BasicBlock.cc:88
abortWithError
#define abortWithError(message)
Definition: Application.hh:72
Instruction.hh
TTAProgram::Program::moveProcedure
void moveProcedure(Procedure &proc, int howMuch)
Definition: Program.cc:588
TTAProgram::CodeSnippet::instructionCount
virtual int instructionCount() const
Definition: CodeSnippet.cc:205
ControlFlowGraph.hh
TTAMachine::ControlUnit
Definition: ControlUnit.hh:50
ProcedurePass.hh
TTAProgram::Instruction::copy
Instruction * copy() const
Definition: Instruction.cc:379
TTAProgram::InstructionReferenceManager::hasReference
bool hasReference(Instruction &ins) const
Definition: InstructionReferenceManager.cc:143
BasicBlockPass.hh
TTAProgram::InstructionReferenceManager::replace
void replace(Instruction &insA, Instruction &insB)
Definition: InstructionReferenceManager.cc:96
Application.hh
IllegalProgram
Definition: Exception.hh:895
__func__
#define __func__
Definition: Application.hh:67
BasicBlockNode
Definition: BasicBlockNode.hh:64
BasicBlockNode::isNormalBB
bool isNormalBB() const
Definition: BasicBlockNode.cc:239
InterPassData
Definition: InterPassData.hh:48
GraphNode::Comparator
Definition: GraphNode.hh:56
ControlFlowGraphPass::handleControlFlowGraph
virtual void handleControlFlowGraph(ControlFlowGraph &cfg, const TTAMachine::Machine &targetMachine)
Definition: ControlFlowGraphPass.cc:65
TerminalFUPort.hh
TTAProgram::Address::location
InstructionAddress location() const
TTAProgram::Move
Definition: Move.hh:55
TTAMachine::ControlUnit::delaySlots
int delaySlots() const
Machine.hh
Exception
Definition: Exception.hh:54
TTAProgram::TerminalFUPort
Definition: TerminalFUPort.hh:56
ProcedurePass::copyCfgToProcedure
static void copyCfgToProcedure(TTAProgram::Procedure &procedure, ControlFlowGraph &cfg)
Definition: ProcedurePass.cc:86
TTAProgram::InstructionReferenceManager
Definition: InstructionReferenceManager.hh:82
TTAProgram::Program::instructionReferenceManager
InstructionReferenceManager & instructionReferenceManager() const
Definition: Program.cc:688
Program.hh
InstructionReference.hh
TTAProgram::CodeSnippet::parent
virtual Program & parent() const
Definition: CodeSnippet.cc:118
BasicBlock.hh
ControlFlowGraphPass.hh
ControlUnit.hh
BasicBlockNode::hasOriginalAddress
bool hasOriginalAddress() const
Definition: BasicBlockNode.cc:150
InstructionReferenceManager.hh
TTAProgram::CodeSnippet::instructionAtIndex
virtual Instruction & instructionAtIndex(int index) const
Definition: CodeSnippet.cc:285
TTAProgram::Move::source
Terminal & source() const
Definition: Move.cc:302
ControlFlowGraphPass
Definition: ControlFlowGraphPass.hh:50
TerminalInstructionAddress.hh
TTAProgram::Program::lastProcedure
Procedure & lastProcedure() const
Definition: Program.cc:230
TTAProgram::Move::isJump
bool isJump() const
Definition: Move.cc:164
TTAProgram::CodeSnippet::instructionAt
virtual Instruction & instructionAt(UIntWord address) const
Definition: CodeSnippet.cc:241
Move.hh
ProcedurePass::ProcedurePass
ProcedurePass(InterPassData &data)
Definition: ProcedurePass.cc:53
BoostGraph::nodeCount
int nodeCount() const
ProcedurePass::executeControlFlowGraphPass
static void executeControlFlowGraphPass(TTAProgram::Procedure &procedure, const TTAMachine::Machine &targetmachine, ControlFlowGraphPass &cfgp)
Definition: ProcedurePass.cc:257
TTAProgram::Procedure
Definition: Procedure.hh:55
BasicBlockNode::originalStartAddress
InstructionAddress originalStartAddress() const
Definition: BasicBlockNode.cc:162
TTAProgram::Instruction::moveCount
int moveCount() const
Definition: Instruction.cc:176
TTAProgram::Instruction::address
Address address() const
Definition: Instruction.cc:327
ControlFlowGraph
Definition: ControlFlowGraph.hh:100
TTAProgram::CodeSnippet::isInProgram
virtual bool isInProgram() const
Definition: CodeSnippet.cc:151
TTAMachine::Machine
Definition: Machine.hh:73