OpenASIP  2.0
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  {
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 }

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 }

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 BBSchedulerController, AbsoluteToRelativeJumps, PreOptimizer, SequentialScheduler, and SimpleIfConverter.

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 }

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:
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
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
TTAProgram::Move::destination
Terminal & destination() const
Definition: Move.cc:323
TTAProgram::Procedure::insertAfter
void insertAfter(const Instruction &pos, Instruction *ins)
Definition: Procedure.cc:193
SchedulerPass::SchedulerPass
SchedulerPass(InterPassData &data)
Definition: SchedulerPass.cc:40
TTAProgram::Terminal::instructionReference
virtual const InstructionReference & instructionReference() const
Definition: Terminal.cc:188
BasicBlockNode::basicBlock
TTAProgram::BasicBlock & basicBlock()
Definition: BasicBlockNode.cc:126
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
TTAProgram::Program::moveProcedure
void moveProcedure(Procedure &proc, int howMuch)
Definition: Program.cc:588
TTAProgram::CodeSnippet::instructionCount
virtual int instructionCount() const
Definition: CodeSnippet.cc:205
TTAMachine::ControlUnit
Definition: ControlUnit.hh:50
TTAProgram::Instruction::copy
Instruction * copy() const
Definition: Instruction.cc:379
TTAProgram::InstructionReferenceManager::hasReference
bool hasReference(Instruction &ins) const
Definition: InstructionReferenceManager.cc:143
TTAProgram::InstructionReferenceManager::replace
void replace(Instruction &insA, Instruction &insB)
Definition: InstructionReferenceManager.cc:96
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
GraphNode::Comparator
Definition: GraphNode.hh:56
ControlFlowGraphPass::handleControlFlowGraph
virtual void handleControlFlowGraph(ControlFlowGraph &cfg, const TTAMachine::Machine &targetMachine)
Definition: ControlFlowGraphPass.cc:65
TTAProgram::Address::location
InstructionAddress location() const
TTAProgram::Move
Definition: Move.hh:55
TTAMachine::ControlUnit::delaySlots
int delaySlots() const
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
TTAProgram::CodeSnippet::parent
virtual Program & parent() const
Definition: CodeSnippet.cc:118
BasicBlockNode::hasOriginalAddress
bool hasOriginalAddress() const
Definition: BasicBlockNode.cc:150
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
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
BoostGraph::nodeCount
int nodeCount() const
ProcedurePass::executeControlFlowGraphPass
static void executeControlFlowGraphPass(TTAProgram::Procedure &procedure, const TTAMachine::Machine &targetmachine, ControlFlowGraphPass &cfgp)
Definition: ProcedurePass.cc:257
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