OpenASIP  2.0
PostpassOperandSharer.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 PostpassOperandSharer.cc
26  *
27  * Implementation of PostpassOperandSharer class.
28  *
29  * This optimizer removes operand writes which are redundant due
30  * other operations using same value.
31  * This is executed after the main scheduler, so this mainly helps
32  * to reduce power consumption, not to improve performance.
33  *
34  * @author Heikki Kultala 2009 (hkultala-no.spam-cs.tut.fi)
35  * @note rating: red
36  */
37 
38 #include "PostpassOperandSharer.hh"
39 
40 #include "Move.hh"
41 #include "Instruction.hh"
42 #include "Terminal.hh"
43 #include "BasicBlock.hh"
44 #include "Guard.hh"
45 #include "MoveGuard.hh"
46 #include "RegisterFile.hh"
48 #include "Bus.hh"
49 
50 using TTAProgram::Move;
53 using TTAMachine::Guard;
56 
57 // this makes the code run slower but gives the trigger operand
58 // statistics
59 #define GET_BETTER_STATISTICS
60 
61 void
63  TTAProgram::BasicBlock& basicBlock, const TTAMachine::Machine&,
65  for (int i = basicBlock.instructionCount()-1; i >= 0; i--) {
66  Instruction& ins = basicBlock.instructionAtIndex(i);
67  for (int j = 0; j < ins.moveCount(); j++) {
68  Move& move = ins.move(j);
69  moveCount_++;
70  if (tryRemoveOperandWrite(basicBlock, i, move)) {
71  ins.removeMove(move);
72  // TODO: make sure there is no movenode to this.
73 // delete &move;
74  j--;
75  }
76  }
77  }
78 }
79 
80 /**
81  *
82  */
84  TTAProgram::BasicBlock& basicBlock, int insIndex, TTAProgram::Move& move) {
85 
86  bool guardOK = false;
87  Terminal& dest = move.destination();
88  Terminal& src = move.source();
89 
90  if (src.isGPR()) {
92  }
93 
94 #ifdef GET_BETTER_STATISTICS
95  if (!dest.isFUPort())
96  {
97  return false;
98  }
99 #else
100 
101  if (!dest.isFUPort() ||
102  dest.isTriggering() ||
103  dest.isOpcodeSetting()) {
104  return false;
105  }
106 #endif
107 
108  operandCount_++;
109  if (src.isFUPort()) {
110  return false;
111  }
112 
113  if (src.isImmediateRegister()) {
114  return false;
115  }
116 
117  const TTAMachine::RegisterFile* guardRF = NULL;
118  int guardIndex = -1;
119  if (!move.isUnconditional()) {
120  const TTAMachine::Guard& guard = move.guard().guard();
121  const RegisterGuard* rg = dynamic_cast<const TTAMachine::RegisterGuard*>(&guard);
122  if (rg) {
123  guardRF = rg->registerFile();
124  guardIndex = rg->registerIndex();
125  }
126  }
127 
128  int srcIndex = -1;
129  const TTAMachine::RegisterFile* srcRF = NULL;
130  if (src.isGPR()) {
131  srcIndex = src.index();
132  srcRF = &src.registerFile();
133  }
134 
135  int glat = move.guardLatency();
136  bool lastDueJump = false;
137 
138  for (int i = insIndex -1; i >= basicBlock.skippedFirstInstructions() && !lastDueJump; i--) {
139 
140  // first check guard writes..
141  // if guard has longer latency than 1 , this has to go to previous
142  // cycles.
143  int k = i+1 - glat;
144  if (k>= 0) {
145  Instruction& gIns = basicBlock.instructionAtIndex(k);
146 
147  // check that does not overwrite the source
148  for (int j = 0; j < gIns.moveCount(); j++) {
149  Move& gMove = gIns.move(j);
150  Terminal& gDst = gMove.destination();
151  if (gDst.isGPR()) {
152  int pdIndex = gDst.index();
153  const RegisterFile* pdRF = &gDst.registerFile();
154  if (guardIndex == pdIndex && guardRF == pdRF) {
155  guardOK = false;
156  }
157  }
158  }
159  }
160 
161  Instruction& prevIns = basicBlock.instructionAtIndex(i);
162  if (irm_->hasReference(prevIns)) {
163  lastDueJump = true;
164  }
165  // check that does not overwrite the source
166  for (int j = 0; j < prevIns.moveCount(); j++) {
167  Move& prevMove = prevIns.move(j);
168  Terminal& prevDst = prevMove.destination();
169 
170  if (prevDst.isGPR()) {
171  int pdIndex = prevDst.index();
172  const RegisterFile* pdRF = &prevDst.registerFile();
173  if ((srcIndex == pdIndex && srcRF == pdRF) &&
174  (!guardOK || prevMove.isUnconditional() ||
175  move.isUnconditional() ||
176  !prevMove.guard().guard().isOpposite(
177  move.guard().guard()))) {
178  return false;
179  }
180  }
181  }
182 
183  // check if writes the same operand
184  for (int j = 0; j < prevIns.moveCount(); j++) {
185  Move& prevMove = prevIns.move(j);
186  Terminal& prevDest = prevMove.destination();
187 
188  // write to same port..
189  if (&prevDest.port() == &dest.port()) {
190  // Check if previous move used narrower bus and
191  // potentially cut upper bits of the transported value.
192  // This is possible is the value was previously used in
193  // operation which used only the lower bits of the value
194  // (see BusResource::canAssign()).
195  if (move.source().equals(prevMove.source())
196  && move.bus().width() <= prevMove.bus().width()) {
197 
198  // TODO: what if guard is conditional, ok or not?
199  if (!prevMove.isUnconditional() &&
200  (move.isUnconditional() ||
201  (!guardOK || !prevMove.guard().guard().isEqual(
202  move.guard().guard())))) {
203  continue;
204  }
205 
206  if (prevDest.isTriggering() ||
207  prevDest.isOpcodeSetting()) {
209  return false;
210  }
211 
213  return true;
214  } else {
215  return false;
216  }
217  }
218  }
219  }
220  return false;
221 }
222 
224 
226  std::cerr << "PostpassOperandSharer statistics: " << std::endl
227  << "\tTotal moves: " << moveCount_ << std::endl
228  << "\tTotal reg reads: " << registerReads_ << std::endl
229  << "\tTotal operands: "<<operandCount_
230  << std::endl
231  << "\tRemoved operands: " << removedOperands_ << std::endl
232  << "\tCannot share trigger count: " << triggerCannotRemove_
233  << std::endl;
234 
235 }
236 
237 unsigned int PostpassOperandSharer::moveCount_ = 0;
238 unsigned int PostpassOperandSharer::operandCount_ = 0;
TTAMachine::Guard
Definition: Guard.hh:55
TTAProgram::Terminal::isFUPort
virtual bool isFUPort() const
Definition: Terminal.cc:118
TTAProgram::Instruction::removeMove
void removeMove(Move &move)
Definition: Instruction.cc:536
TTAProgram::Terminal::isTriggering
virtual bool isTriggering() const
Definition: Terminal.cc:298
TTAProgram::Instruction::move
Move & move(int i) const
Definition: Instruction.cc:193
TTAProgram::Terminal::index
virtual int index() const
Definition: Terminal.cc:274
TTAProgram::Move::guardLatency
int guardLatency() const
Definition: Move.cc:503
TTAMachine::RegisterGuard::registerIndex
int registerIndex() const
PostpassOperandSharer::registerReads_
static unsigned int registerReads_
Definition: PostpassOperandSharer.hh:79
TTAProgram::Terminal::registerFile
virtual const TTAMachine::RegisterFile & registerFile() const
Definition: Terminal.cc:225
PostpassOperandSharer::~PostpassOperandSharer
virtual ~PostpassOperandSharer()
Definition: PostpassOperandSharer.cc:223
TTAMachine::Bus::width
int width() const
Definition: Bus.cc:149
TTAProgram::Instruction
Definition: Instruction.hh:57
TTAProgram::Move::isUnconditional
bool isUnconditional() const
Definition: Move.cc:154
TTAMachine::Guard::isEqual
virtual bool isEqual(const Guard &guard) const =0
TTAProgram::Move::destination
Terminal & destination() const
Definition: Move.cc:323
TTAProgram::Move::bus
const TTAMachine::Bus & bus() const
Definition: Move.cc:373
PostpassOperandSharer::handleBasicBlock
virtual void handleBasicBlock(TTAProgram::BasicBlock &basicBlock, const TTAMachine::Machine &targetMachine, TTAProgram::InstructionReferenceManager &irm, BasicBlockNode *bbn=NULL)
Definition: PostpassOperandSharer.cc:62
Terminal.hh
PostpassOperandSharer.hh
PostpassOperandSharer::operandCount_
static unsigned int operandCount_
Definition: PostpassOperandSharer.hh:77
TTAProgram::Terminal::isImmediateRegister
virtual bool isImmediateRegister() const
Definition: Terminal.cc:97
PostpassOperandSharer::moveCount_
static unsigned int moveCount_
Definition: PostpassOperandSharer.hh:76
PostpassOperandSharer::triggerCannotRemove_
static unsigned int triggerCannotRemove_
Definition: PostpassOperandSharer.hh:80
TTAProgram::BasicBlock::skippedFirstInstructions
int skippedFirstInstructions() const
Definition: BasicBlock.cc:88
Instruction.hh
TTAProgram::CodeSnippet::instructionCount
virtual int instructionCount() const
Definition: CodeSnippet.cc:205
PostpassOperandSharer::printStats
static void printStats()
Definition: PostpassOperandSharer.cc:225
TTAMachine::RegisterGuard
Definition: Guard.hh:137
TTAProgram::InstructionReferenceManager::hasReference
bool hasReference(Instruction &ins) const
Definition: InstructionReferenceManager.cc:143
TTAProgram::Move::guard
MoveGuard & guard() const
Definition: Move.cc:345
BasicBlockNode
Definition: BasicBlockNode.hh:64
TTAProgram::Terminal::isGPR
virtual bool isGPR() const
Definition: Terminal.cc:107
Guard.hh
TTAProgram::Move
Definition: Move.hh:55
Bus.hh
TTAProgram::Terminal::isOpcodeSetting
virtual bool isOpcodeSetting() const
Definition: Terminal.cc:285
TTAMachine::Guard::isOpposite
virtual bool isOpposite(const Guard &guard) const =0
TTAProgram::BasicBlock
Definition: BasicBlock.hh:85
TTAProgram::InstructionReferenceManager
Definition: InstructionReferenceManager.hh:82
RegisterFile.hh
BasicBlock.hh
InstructionReferenceManager.hh
TTAProgram::Terminal
Definition: Terminal.hh:60
TTAProgram::Terminal::equals
virtual bool equals(const Terminal &other) const =0
TTAProgram::CodeSnippet::instructionAtIndex
virtual Instruction & instructionAtIndex(int index) const
Definition: CodeSnippet.cc:285
TTAProgram::Move::source
Terminal & source() const
Definition: Move.cc:302
PostpassOperandSharer::irm_
TTAProgram::InstructionReferenceManager * irm_
Definition: PostpassOperandSharer.hh:75
TTAProgram::Terminal::port
virtual const TTAMachine::Port & port() const
Definition: Terminal.cc:378
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
TTAProgram::MoveGuard::guard
const TTAMachine::Guard & guard() const
Definition: MoveGuard.cc:86
Move.hh
PostpassOperandSharer::removedOperands_
static unsigned int removedOperands_
Definition: PostpassOperandSharer.hh:78
PostpassOperandSharer::tryRemoveOperandWrite
bool tryRemoveOperandWrite(TTAProgram::BasicBlock &basicBlock, int insIndex, TTAProgram::Move &move)
Definition: PostpassOperandSharer.cc:83
TTAMachine::RegisterGuard::registerFile
const RegisterFile * registerFile() const
TTAProgram::Instruction::moveCount
int moveCount() const
Definition: Instruction.cc:176
TTAMachine::Machine
Definition: Machine.hh:73
MoveGuard.hh