OpenASIP 2.2
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
ProcedurePass Class Reference

#include <ProcedurePass.hh>

Inheritance diagram for ProcedurePass:
Inheritance graph
Collaboration diagram for ProcedurePass:
Collaboration graph

Public Member Functions

 ProcedurePass (InterPassData &data)
 
virtual ~ProcedurePass ()
 
virtual void handleProcedure (TTAProgram::Procedure &procedure, const TTAMachine::Machine &targetMachine)
 
- Public Member Functions inherited from SchedulerPass
 SchedulerPass (InterPassData &data)
 
virtual ~SchedulerPass ()
 
InterPassDatainterPassData ()
 
virtual std::string shortDescription () const =0
 
virtual std::string longDescription () const
 

Static Public Member Functions

static void copyCfgToProcedure (TTAProgram::Procedure &procedure, ControlFlowGraph &cfg)
 
static void executeControlFlowGraphPass (TTAProgram::Procedure &procedure, const TTAMachine::Machine &targetmachine, ControlFlowGraphPass &cfgp)
 

Detailed Description

Interface for scheduler passes that handle procedures.

Definition at line 53 of file ProcedurePass.hh.

Constructor & Destructor Documentation

◆ ProcedurePass()

ProcedurePass::ProcedurePass ( InterPassData data)

Constructor.

Definition at line 53 of file ProcedurePass.cc.

53 :
54 SchedulerPass(data) {
55}

◆ ~ProcedurePass()

ProcedurePass::~ProcedurePass ( )
virtual

Destructor.

Definition at line 60 of file ProcedurePass.cc.

60 {
61}

Member Function Documentation

◆ copyCfgToProcedure()

void ProcedurePass::copyCfgToProcedure ( TTAProgram::Procedure procedure,
ControlFlowGraph cfg 
)
static

Definition at line 86 of file ProcedurePass.cc.

87 {
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}
#define __func__
#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
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

References __func__, TTAProgram::Instruction::address(), assert, BasicBlockNode::basicBlock(), TTAProgram::Instruction::copy(), TTAMachine::ControlUnit::delaySlots(), TTAProgram::Move::destination(), TTAProgram::TerminalFUPort::functionUnit(), BasicBlockNode::hasOriginalAddress(), TTAProgram::InstructionReferenceManager::hasReference(), TTAProgram::Procedure::insertAfter(), TTAProgram::InstructionReference::instruction(), TTAProgram::CodeSnippet::instructionAt(), TTAProgram::CodeSnippet::instructionAtIndex(), TTAProgram::CodeSnippet::instructionCount(), TTAProgram::Terminal::instructionReference(), TTAProgram::Program::instructionReferenceManager(), TTAProgram::CodeSnippet::isInProgram(), TTAProgram::Terminal::isInstructionAddress(), TTAProgram::Move::isJump(), BasicBlockNode::isNormalBB(), TTAProgram::Program::lastProcedure(), TTAProgram::Address::location(), TTAProgram::Instruction::move(), TTAProgram::Instruction::moveCount(), TTAProgram::Program::moveProcedure(), TTAProgram::Program::nextProcedure(), BoostGraph< GraphNode, GraphEdge >::node(), BoostGraph< GraphNode, GraphEdge >::nodeCount(), BasicBlockNode::originalEndAddress(), BasicBlockNode::originalStartAddress(), TTAProgram::CodeSnippet::parent(), TTAProgram::Procedure::remove(), TTAProgram::Instruction::removeMove(), TTAProgram::InstructionReferenceManager::replace(), TTAProgram::BasicBlock::skippedFirstInstructions(), and TTAProgram::Move::source().

Referenced by executeControlFlowGraphPass().

Here is the call graph for this function:

◆ executeControlFlowGraphPass()

void ProcedurePass::executeControlFlowGraphPass ( TTAProgram::Procedure procedure,
const TTAMachine::Machine targetmachine,
ControlFlowGraphPass cfgp 
)
static

Definition at line 257 of file ProcedurePass.cc.

259 {
260 ControlFlowGraph cfg(procedure);
261 cfgp.handleControlFlowGraph(cfg, targetMachine);
262 copyCfgToProcedure(procedure, cfg);
263}
virtual void handleControlFlowGraph(ControlFlowGraph &cfg, const TTAMachine::Machine &targetMachine)
static void copyCfgToProcedure(TTAProgram::Procedure &procedure, ControlFlowGraph &cfg)

References copyCfgToProcedure(), and ControlFlowGraphPass::handleControlFlowGraph().

Referenced by handleProcedure().

Here is the call graph for this function:

◆ handleProcedure()

void ProcedurePass::handleProcedure ( TTAProgram::Procedure procedure,
const TTAMachine::Machine targetMachine 
)
virtual

Handles a single procedure.

Parameters
proecudeThe procedure to handle.
machineThe target machine if any. (NullMachine::instance() if target machine is irrelevant).
Exceptions
Incase handling is unsuccesful for any reason (procedure might still get modified).

Reimplemented in PreOptimizer, SequentialScheduler, SimpleIfConverter, AbsoluteToRelativeJumps, and BBSchedulerController.

Definition at line 73 of file ProcedurePass.cc.

75 {
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}
#define abortWithError(message)
static void executeControlFlowGraphPass(TTAProgram::Procedure &procedure, const TTAMachine::Machine &targetmachine, ControlFlowGraphPass &cfgp)

References abortWithError, and executeControlFlowGraphPass().

Referenced by ProgramPass::executeProcedurePass().

Here is the call graph for this function:

The documentation for this class was generated from the following files: