OpenASIP  2.0
CallsToJumps.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2014 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 #include "CallsToJumps.hh"
26 #include "ControlFlowGraph.hh"
27 #include "Program.hh"
28 #include "Procedure.hh"
29 #include "BasicBlock.hh"
30 #include "Instruction.hh"
31 #include "Move.hh"
32 #include "InstructionReference.hh"
36 #include "TerminalFUPort.hh"
37 #include "Terminal.hh"
38 #include "CodeGenerator.hh"
39 #include "UniversalMachine.hh"
40 #include "MoveGuard.hh"
41 #include "ControlUnit.hh"
42 #include "SpecialRegisterPort.hh"
43 
44 using namespace TTAProgram;
45 
46 //#define DEBUG_CALLS_TO_JUMPS
47 
48 void
50  ControlFlowGraph& cfg, const TTAMachine::Machine& targetMachine) {
53  int nodeCount = cfg.nodeCount();
54  for (int bbIndex = 0; bbIndex < nodeCount; ++bbIndex) {
55  BasicBlockNode& bbn = dynamic_cast<BasicBlockNode&>(cfg.node(bbIndex));
56  if (!bbn.isNormalBB())
57  continue;
58 
59  assert(!bbn.isScheduled());
60 
61  BasicBlock& bb = bbn.basicBlock();
62 
63  // scan through the instructions, looking for CALLs
64  // replace each call with
65  // 1) a move to RA with an instruction reference to the instruction
66  // following the call (the next BB), and
67  // 2) a JUMP to the original CALL destination
68  // 3) some kind of annotation that marks that this JUMP should
69  // be treated like a function call, e.g., in the DDG builder
70  for (int i = 0, count = bb.instructionCount(); i < count; ++i) {
71  Instruction& instr = bb.instructionAtIndex(i);
72  if (instr.hasCall()) {
73 
74  // If unscheduled it should have exactly one move, the call.
75  assert(instr.moveCount() == 1);
76 
77  // In the TCE's CFG, a call splits the basic block, so this
78  // should be the last instruction as there are no delay slots
79  // in the intermediate representation.
80  assert(i + 1 == count);
81 
82 
83  Move& originalCall = instr.move(0);
84 
85  BasicBlockNode* nextBB = cfg.fallThruSuccessor(bbn);
86  assert(nextBB != NULL);
87 
88 #ifdef DEBUG_CALLS_TO_JUMPS
90  << "### found call: " << originalCall.toString()
91  << std::endl;
93  << "### BB: " << bb.toString() << std::endl;
95  << "### FallThroughBB: " << nextBB->basicBlock().toString()
96  << std::endl;
97 #endif
98 
99  // E.g. call to exit does not return and there is no sensible
100  // place to return. Just do not add the RA write in that case.
101  Instruction* returnLocation =
102  (nextBB->basicBlock().instructionCount() > 0) ?
103  &nextBB->basicBlock().firstInstruction() : NULL;
104 
105  CodeGenerator codeGen(targetMachine);
106 
108 
109  std::shared_ptr<Move> returnAddressMove = NULL;
110  std::shared_ptr<Move> jumpToFunction;
111 
112  if (originalCall.isUnconditional()) {
113 
114  if (returnLocation != NULL) {
115  InstructionReference returnRef =
116  irm.createReference(*returnLocation);
117 
118  returnAddressMove = std::make_shared<Move>(
119  new TerminalInstructionReference(returnRef),
121  *uMach.controlUnit()->returnAddressPort()),
122  uMach.universalBus());
123  }
124 
125  jumpToFunction = std::make_shared<Move>(
126  originalCall.source().copy(),
127  codeGen.createTerminalFUPort("jump", 1),
128  uMach.universalBus());
129  } else {
130 
131  if (returnLocation != NULL) {
132  InstructionReference returnRef =
133  irm.createReference(*returnLocation);
134 
135  returnAddressMove = std::make_shared<Move>(
136  new TerminalInstructionReference(returnRef),
138  *uMach.controlUnit()->returnAddressPort()),
139  uMach.universalBus(),
140  originalCall.guard().copy());
141  }
142 
143  jumpToFunction = std::make_shared<Move>(
144  originalCall.source().copy(),
145  codeGen.createTerminalFUPort("jump", 1),
146  uMach.universalBus(),
147  originalCall.guard().copy());
148  }
149 
150  jumpToFunction->addAnnotation(
152 
153  if (returnAddressMove != NULL) {
154  Instruction* raMoveInstr =
156  raMoveInstr->addMove(returnAddressMove);
157  bb.insertBefore(instr, raMoveInstr);
158 
159  if (irm.hasReference(instr))
160  irm.replace(instr, *raMoveInstr);
161  }
162 
163  Instruction* jumpInstr =
165  jumpInstr->addMove(jumpToFunction);
166 
167  bb.insertBefore(instr, jumpInstr);
168 
169  if (returnAddressMove == NULL && irm.hasReference(instr))
170  irm.replace(instr, *jumpInstr);
171 
172  // Keep the instruction references up to date as
173  // the old call can be a jump target (so should be the
174  // new RA-move) if it starts a basic block.
175 
176 #ifdef DEBUG_CALLS_TO_JUMPS
178  << "### removing " << instr.toString() << std::endl;
179 #endif
180  assert(!irm.hasReference(instr));
181  bb.remove(instr);
182  }
183  }
184  }
185 }
TerminalInstructionReference.hh
TTAProgram
Definition: Estimator.hh:65
TTAProgram::Instruction::addMove
void addMove(std::shared_ptr< Move > move)
Definition: Instruction.cc:147
TTAProgram::ProgramAnnotation::ANN_JUMP_FUNCTION_CALL
@ ANN_JUMP_FUNCTION_CALL
The JUMP in the annotated move is a function call and should be treated as such in the data dependenc...
Definition: ProgramAnnotation.hh:129
TTAProgram::CodeSnippet::firstInstruction
virtual Instruction & firstInstruction() const
Definition: CodeSnippet.cc:216
TTAProgram::Instruction::move
Move & move(int i) const
Definition: Instruction.cc:193
BoostGraph::node
Node & node(const int index) const
TerminalSymbolReference.hh
TTAProgram::CodeSnippet::insertBefore
virtual void insertBefore(const Instruction &pos, Instruction *ins)
Definition: CodeSnippet.cc:514
UniversalMachine::universalBus
TTAMachine::Bus & universalBus() const
Definition: UniversalMachine.cc:306
TTAProgram::Instruction
Definition: Instruction.hh:57
TTAProgram::CodeGenerator::createTerminalFUPort
TTAProgram::TerminalFUPort * createTerminalFUPort(const TCEString &opName, int operand)
Definition: CodeGenerator.cc:94
TTAProgram::Move::isUnconditional
bool isUnconditional() const
Definition: Move.cc:154
CallsToJumps.hh
Procedure.hh
TTAProgram::CodeSnippet::remove
virtual void remove(Instruction &ins)
Definition: CodeSnippet.cc:558
TTAProgram::InstructionReferenceManager::createReference
InstructionReference createReference(Instruction &ins)
Definition: InstructionReferenceManager.cc:73
UniversalMachine::instance
static UniversalMachine & instance()
Definition: UniversalMachine.cc:73
TTAProgram::Instruction::toString
std::string toString() const
Definition: Instruction.cc:576
TTAProgram::Move::toString
std::string toString() const
Definition: Move.cc:436
Terminal.hh
Application::logStream
static std::ostream & logStream()
Definition: Application.cc:155
BasicBlockNode::isScheduled
bool isScheduled() const
Definition: BasicBlockNode.hh:93
ControlFlowGraph::instructionReferenceManager
TTAProgram::InstructionReferenceManager & instructionReferenceManager()
Definition: ControlFlowGraph.cc:2401
TTAProgram::Instruction::hasCall
bool hasCall() const
Definition: Instruction.cc:438
BasicBlockNode::basicBlock
TTAProgram::BasicBlock & basicBlock()
Definition: BasicBlockNode.cc:126
assert
#define assert(condition)
Definition: Application.hh:86
UniversalMachine.hh
TTAMachine::Machine::controlUnit
virtual ControlUnit * controlUnit() const
Definition: Machine.cc:345
Instruction.hh
TTAProgram::CodeSnippet::instructionCount
virtual int instructionCount() const
Definition: CodeSnippet.cc:205
ControlFlowGraph.hh
UniversalMachine
Definition: UniversalMachine.hh:56
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
TTAProgram::Move::guard
MoveGuard & guard() const
Definition: Move.cc:345
BasicBlockNode
Definition: BasicBlockNode.hh:64
TTAProgram::TerminalInstructionReference
Definition: TerminalInstructionReference.hh:48
BasicBlockNode::isNormalBB
bool isNormalBB() const
Definition: BasicBlockNode.cc:239
TerminalFUPort.hh
TTAProgram::Move
Definition: Move.hh:55
TTAProgram::TerminalFUPort
Definition: TerminalFUPort.hh:56
TTAProgram::BasicBlock
Definition: BasicBlock.hh:85
TTAProgram::AnnotatedInstructionElement::addAnnotation
void addAnnotation(const ProgramAnnotation &annotation)
Definition: AnnotatedInstructionElement.cc:63
TTAMachine::NullInstructionTemplate::instance
static NullInstructionTemplate & instance()
Definition: NullInstructionTemplate.cc:62
CodeGenerator.hh
TTAProgram::InstructionReferenceManager
Definition: InstructionReferenceManager.hh:82
Program.hh
InstructionReference.hh
BasicBlock.hh
ControlUnit.hh
ControlFlowGraph::fallThruSuccessor
BasicBlockNode * fallThruSuccessor(const BasicBlockNode &bbn) const
Definition: ControlFlowGraph.cc:1358
SpecialRegisterPort.hh
TTAProgram::Terminal::copy
virtual Terminal * copy() const =0
TTAProgram::CodeGenerator
Definition: CodeGenerator.hh:53
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
TTAProgram::MoveGuard::copy
MoveGuard * copy() const
Definition: MoveGuard.cc:96
TTAProgram::CodeSnippet::toString
virtual std::string toString() const
Definition: CodeSnippet.hh:117
TTAProgram::ProgramAnnotation
Definition: ProgramAnnotation.hh:49
Move.hh
TTAMachine::ControlUnit::returnAddressPort
SpecialRegisterPort * returnAddressPort() const
Definition: ControlUnit.cc:307
BoostGraph::nodeCount
int nodeCount() const
TTAProgram::InstructionReference
Definition: InstructionReference.hh:49
CallsToJumps::handleControlFlowGraph
virtual void handleControlFlowGraph(ControlFlowGraph &cfg, const TTAMachine::Machine &targetMachine)
Definition: CallsToJumps.cc:49
TTAProgram::Instruction::moveCount
int moveCount() const
Definition: Instruction.cc:176
ControlFlowGraph
Definition: ControlFlowGraph.hh:100
TTAMachine::Machine
Definition: Machine.hh:73
MoveGuard.hh