OpenASIP 2.2
Loading...
Searching...
No Matches
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"
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 */
56
57/**
58 * Destructor.
59 */
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 */
72void
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
85void
87 TTAProgram::Procedure& procedure, ControlFlowGraph& cfg) {
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++) {
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
256void
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}
#define __func__
#define abortWithError(message)
#define assert(condition)
TTAProgram::BasicBlock & basicBlock()
bool isNormalBB() const
InstructionAddress originalEndAddress() const
InstructionAddress originalStartAddress() const
bool hasOriginalAddress() const
int nodeCount() const
Node & node(const int index) const
virtual void handleControlFlowGraph(ControlFlowGraph &cfg, const TTAMachine::Machine &targetMachine)
virtual void handleProcedure(TTAProgram::Procedure &procedure, const TTAMachine::Machine &targetMachine)
static void copyCfgToProcedure(TTAProgram::Procedure &procedure, ControlFlowGraph &cfg)
ProcedurePass(InterPassData &data)
virtual ~ProcedurePass()
static void executeControlFlowGraphPass(TTAProgram::Procedure &procedure, const TTAMachine::Machine &targetmachine, ControlFlowGraphPass &cfgp)
InstructionAddress location() const
int skippedFirstInstructions() const
Definition BasicBlock.cc:88
virtual bool isInProgram() const
virtual int instructionCount() const
virtual Instruction & instructionAt(UIntWord address) const
virtual Program & parent() const
virtual Instruction & instructionAtIndex(int index) const
void replace(Instruction &insA, Instruction &insB)
Instruction * copy() const
Move & move(int i) const
Address address() const
void removeMove(Move &move)
Terminal & source() const
Definition Move.cc:302
bool isJump() const
Definition Move.cc:164
Terminal & destination() const
Definition Move.cc:323
void remove(Instruction &ins)
Definition Procedure.cc:297
void insertAfter(const Instruction &pos, Instruction *ins)
Definition Procedure.cc:193
void moveProcedure(Procedure &proc, int howMuch)
Definition Program.cc:588
Procedure & nextProcedure(const Procedure &proc) const
Definition Program.cc:250
InstructionReferenceManager & instructionReferenceManager() const
Definition Program.cc:688
Procedure & lastProcedure() const
Definition Program.cc:230
virtual const TTAMachine::FunctionUnit & functionUnit() const
virtual const InstructionReference & instructionReference() const
Definition Terminal.cc:188
virtual bool isInstructionAddress() const
Definition Terminal.cc:87