OpenASIP 2.2
Loading...
Searching...
No Matches
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
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
56
57// this makes the code run slower but gives the trigger operand
58// statistics
59#define GET_BETTER_STATISTICS
60
61void
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
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
237unsigned int PostpassOperandSharer::moveCount_ = 0;
TTAProgram::InstructionReferenceManager * irm_
static unsigned int operandCount_
bool tryRemoveOperandWrite(TTAProgram::BasicBlock &basicBlock, int insIndex, TTAProgram::Move &move)
static unsigned int registerReads_
static unsigned int triggerCannotRemove_
static unsigned int moveCount_
virtual void handleBasicBlock(TTAProgram::BasicBlock &basicBlock, const TTAMachine::Machine &targetMachine, TTAProgram::InstructionReferenceManager &irm, BasicBlockNode *bbn=NULL)
static unsigned int removedOperands_
int width() const
Definition Bus.cc:149
virtual bool isOpposite(const Guard &guard) const =0
virtual bool isEqual(const Guard &guard) const =0
const RegisterFile * registerFile() const
int skippedFirstInstructions() const
Definition BasicBlock.cc:88
virtual int instructionCount() const
virtual Instruction & instructionAtIndex(int index) const
Move & move(int i) const
void removeMove(Move &move)
const TTAMachine::Guard & guard() const
Definition MoveGuard.cc:86
MoveGuard & guard() const
Definition Move.cc:345
bool isUnconditional() const
Definition Move.cc:154
Terminal & source() const
Definition Move.cc:302
int guardLatency() const
Definition Move.cc:503
Terminal & destination() const
Definition Move.cc:323
const TTAMachine::Bus & bus() const
Definition Move.cc:373
virtual bool isTriggering() const
Definition Terminal.cc:298
virtual int index() const
Definition Terminal.cc:274
virtual bool isOpcodeSetting() const
Definition Terminal.cc:285
virtual bool equals(const Terminal &other) const =0
virtual bool isGPR() const
Definition Terminal.cc:107
virtual bool isImmediateRegister() const
Definition Terminal.cc:97
virtual const TTAMachine::Port & port() const
Definition Terminal.cc:378
virtual const TTAMachine::RegisterFile & registerFile() const
Definition Terminal.cc:225
virtual bool isFUPort() const
Definition Terminal.cc:118