OpenASIP 2.2
Loading...
Searching...
No Matches
BFScheduleBU.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/**
26 * @file BFScheduleBU.cc
27 *
28 * Definition of BFScheduleBU class
29 *
30 * Tries to schedule a move in bottom-up order. Before the actual scheduling,
31 * Tries to call many other optimization classes to do things like
32 * bypassing, operand sharing etc.
33 *
34 * @author Heikki Kultala 2014-2020(heikki.kultala-no.spam-tuni.fi)
35 * @note rating: red
36 */
37
38#include "BFScheduleBU.hh"
39#include "BFRegCopyBefore.hh"
40#include "BFRegCopyAfter.hh"
41#include "BFEarlyBypasser.hh"
42#include "BFLateBypasses.hh"
43#include "BFPushAntidepsDown.hh"
46#include "MoveNode.hh"
48#include "BF2Scheduler.hh"
49#include "Move.hh"
50#include "UniversalMachine.hh"
51#include "BFScheduleTD.hh"
52#include "BF2ScheduleFront.hh"
54#include "BFShareOperands.hh"
55#include "BFPushDepsUp.hh"
57#include "Terminal.hh"
59#include "BFTryRemoveGuard.hh"
60#include "BFCopyRegWithOp.hh"
61#include "BFRenameSource.hh"
62
63//#define DEBUG_BUBBLEFISH_SCHEDULER
64
65bool
67#ifdef DEBUG_BUBBLEFISH_SCHEDULER
68 std::cerr << "\tPerforming BFSchduleBU for: " << mn_.toString() <<
69 " lc limit: " << lc_ << std::endl;
70#endif
73 mn_.move().setBus(UniversalMachine::instance().universalBus());
74 }
75 // TODO: what about constants!
76
79 /* this does not yet work, leads to schduler deadlock.
80 so commented out.
81 if (BF2ScheduleFront::getSisterTrigger(mn_, targetMachine()) == &mn_) {
82 ProgramOperationPtr poPtr = mn_.destinationOperationPtr();
83 for (int i = 0; i < poPtr->inputMoveCount(); i++) {
84 MoveNode& operand = poPtr->inputMove(i);
85 if (&operand != &mn_) {
86 if (!operand.isScheduled() && !sched_.isDeadResult(operand)) {
87
88 BFShareOperands* shareOpers = new BFShareOperands(sched_, operand, lc_);
89 if ((*shareOpers)()) {
90 // if (shareOpers->removedNode()) {
91 // return true;
92 // }
93 preChildren_.push(shareOpers);
94 } else {
95 delete shareOpers;
96 }
97 }
98
99 }
100 }
101 }
102 */
103 /** Another op may not come between these, so if this does
104 not work, try without this */
105 BFShareOperands* shareOpers = new BFShareOperands(sched_, mn_, lc_);
106 runPreChild(shareOpers);
107 }
108
109 if (mn_.isSourceVariable()) {
110 bool forceBypass = BF2Scheduler::isSourceUniversalReg(mn_);
111 if (forceBypass && !allowEarlyBypass_) {
112 return false;
113 }
114 if (allowEarlyBypass_) {
116 if (!runPreChild(ebp)) {
117 if (forceBypass) {
118#ifdef DEBUG_BUBBLEFISH_SCHEDULER
119 std::cerr << "\t\tForced bypass failed" << std::endl;
120 ddg().writeToDotFile("forced_bypass_fail.dot");
121#endif
122 return false;
123 }
124 }
125 if (mn_.isScheduled()) {
126 bypasserScheduled_ = true;
127 return true;
128 }
129 }
130 }
131
134 if (runPreChild(lbp)) {
135 if (lbp->removedSource()) {
136 return true;
137 } else {
138 // late bypass without DRE .. we may want to do TD instead?
139 int prefCycle = BF2ScheduleFront::prefResultCycle(mn_);
140 if (prefCycle != INT_MAX) {
141 BFScheduleTD* td =
142 new BFScheduleTD(sched_, mn_, prefCycle, false);
143 if (runPostChild(td)) {
144 return true;
145 }
146 }
147 }
148 }
149 }
150
151 BFRegCopy* regCopy = NULL;
152 BFRegCopy* regCopyBefore = NULL;
153 BFRegCopy* regCopyAfter = NULL;
154 if (!canBeScheduled(mn_)) {
155
156 // can this be solved by dropping guard?
158
159 // no. need to add a regcopy.
160 if (mn_.isSourceOperation()) {
161 regCopy = regCopyAfter = new BFRegCopyAfter(sched_,mn_, lc_);
162 } else {
163 // check that we have copy op with suitable connectivity.
164 auto copyFUs =
166 if (!copyFUs.empty()) {
167 regCopy = regCopyBefore =
168 new BFCopyRegWithOp(sched_, mn_, lc_, copyFUs);
169 } else { // source not op, dest not reg(is op)
170 BFRenameSource* renSrc =
172 if (runPreChild(renSrc)) {
173 auto forbiddenRF =
175 if (forbiddenRF) {
176 renSrc->undo();
177 preChildren_.pop();
178 forbiddenRF =
180 regCopy = regCopyBefore =
181 new BFRegCopyBefore(sched_, mn_, lc_, forbiddenRF);
182 }
183 } else {
184 // could not use renaming to get rid of regcopy.
185 auto forbiddenRF =
187 regCopy = regCopyBefore =
188 new BFRegCopyBefore(sched_, mn_, lc_,forbiddenRF);
189 }
190 }
191 }
192 if (regCopy && !runPreChild(regCopy)) {
194 return false;
195 }
196 }
197 }
198 // ignore unsched successors
199 int ddglc = std::min(lc_,ddg().latestCycle(mn_, ii(), false, true));
200 int ddgec = ddg().earliestCycle(mn_, ii(), false, false);
201 int rmlc = rmLC(ddglc, mn_);
202
203#ifdef DEBUG_BUBBLEFISH_SCHEDULER
204 std::cerr << "\t\tddg lc: " << ddglc << std::endl;
205 std::cerr << "\t\tddg ec: " << ddgec << std::endl;
206#endif
207
208 int latestIfRenamed =
209 std::min(lc_,
210 ddg().latestCycle(mn_, ii(), true, false));
211// ddgReplyLimit = std::min(latestIfRenamed+3, ddgReplyLimit);
212 if (latestIfRenamed > ddglc && !mn_.move().isControlFlowMove()) {
213 latestIfRenamed = rmLC(latestIfRenamed, mn_);
214#ifdef DEBUG_BUBBLEFISH_SCHEDULER
215 std::cerr << "\t\tRenaming could benefit move: " << mn_.toString() <<
216 " lc: " << ddglc << " renamed lc: " << latestIfRenamed
217 << std::endl;
218#endif
219 BFOptimization* adepPush = new BFPushAntidepsDown(
220 sched_, mn_, ddglc, latestIfRenamed);
221 if (runPreChild(adepPush)) {
222 ddglc = std::min(
223 lc_, ddg().latestCycle(mn_, ii(), false, false));
224#ifdef DEBUG_BUBBLEFISH_SCHEDULER
225 std::cerr << "\t\tPushed antidep dests down, new lc: "
226 << ddglc << std::endl;
227#endif
228
229 latestIfRenamed = std::min(lc_,
230 ddg().latestCycle(
231 mn_, ii(), true, false));
232 latestIfRenamed = rmLC(latestIfRenamed, mn_);
233
234#ifdef DEBUG_BUBBLEFISH_SCHEDULER
235 } else {
236
237 std::cerr << "\t\tCould not push antidep dests down" << std::endl;
238#endif
239 }
240 rmlc = rmLC(ddglc, mn_);
241
242 if (latestIfRenamed > ddglc &&
245#ifdef DEBUG_BUBBLEFISH_SCHEDULER
246 std::cerr << "Antideps still limit scheduling of: "
247 << mn_.toString() << " even after pushing? them down!"
248 << std::endl;
249#endif
250 BFOptimization* renameSrc =
251 new BFRenameSource(sched_, mn_, ddglc, latestIfRenamed);
252 if (runPreChild(renameSrc)) {
253 int renamedDDGLC =
254 std::min(
255 lc_, ddg().latestCycle(mn_, ii(), false, false));
256
257 int renamedRMLC = rmLC(renamedDDGLC, mn_);
258 if (renamedRMLC <= rmlc) {
259#ifdef DEBUG_BUBBLEFISH_SCHEDULER
260 std::cerr << "\t\tRenaming did not help rmlc, undoing"
261 << std::endl;
262#endif
263 preChildren_.pop();
264 renameSrc->undo();
265 } else {
266 ddglc = renamedDDGLC;
267 rmlc = renamedRMLC;
268#ifdef DEBUG_BUBBLEFISH_SCHEDULER
269 std::cerr << "\t\tRenamed Source Reg, new lc: "
270 << ddglc << std::endl;
271#endif
272 }
273 }
274 }
275 }
276
277 if ((ddglc < ddgec || rmlc < ddgec) && ddglc >=0 && ii()) {
278#ifdef DEBUG_BUBBLEFISH_SCHEDULER
279 std::cerr << "\t\tPushing up deps could benefit move: "
280 << mn_.toString() << " lc: " << ddglc
281 << " ec: " << ddgec << std::endl;
282#endif
283 if (runPreChild(new BFPushDepsUp(sched_, mn_, ddglc))) {
284 ddgec = ddg().earliestCycle(mn_, ii(), false, false);
285#ifdef DEBUG_BUBBLEFISH_SCHEDULER
286 std::cerr << "pushed antidep something up, ec now: "
287 << ddgec << std::endl;
288#endif
289 }
290 }
291
292
293 if (mn_.move().destination().isGPR() &&
294 mn_.move().destination().registerFile().width() == 1) {
295 int latestNoGuard =
296 std::min(lc_,
297 ddg().latestCycle(mn_, ii(), false, false, true));
298 if (latestNoGuard > ddglc) {
299 BFOptimization* removeGuardsFromSuccs =
300 new BFRemoveGuardsFromSuccs(sched_, mn_, ddglc,latestNoGuard);
301 if (runPreChild(removeGuardsFromSuccs)) {
302 ddglc = std::min(lc_,
303 ddg().latestCycle(mn_, ii(), false, false));
304#ifdef DEBUG_BUBBLEFISH_SCHEDULER
305 std::cerr << "\t\tRemoved guards from dests, new lc: "
306 << ddglc << std::endl;
307#endif
308 } else {
309#ifdef DEBUG_BUBBLEFISH_SCHEDULER
310 std::cerr << "\t\tCould not remove guards from dests"
311 << std::endl;
312#endif
313 }
314 }
315 }
316
317 rmlc = rmLC(ddglc, mn_);
318#ifdef DEBUG_BUBBLEFISH_SCHEDULER
319 std::cerr << "\t\tbfschdbu: rmlc: " << rmlc << std::endl;
320#endif
321
322 if (rmlc < ddgec && ii() != 0) {
323#ifdef DEBUG_BUBBLEFISH_SCHEDULER
324 std::cerr << "\t\t\tDDG ec " << ddgec << " on loop(II:"
325 << ii() << ") limits scheudling of: " << mn_.toString();
326#endif
327 if (ddg().earliestCycle(mn_, INT_MAX/2, false, false) < rmlc) {
328#ifdef DEBUG_BUBBLEFISH_SCHEDULER
329 std::cerr << "Limiter is a loop edge" << std::endl;
330 std::cerr << "\t\t\t\tddgec was: " << ddgec << " ddglc was: "
331 << ddgec << " rmlc was: " << rmlc << std::endl;
332#endif
333 BFOptimization* loopDepPush = new BFPushDepsUp(sched_, mn_, rmlc);
334 if (runPreChild(loopDepPush)) {
335#ifdef DEBUG_BUBBLEFISH_SCHEDULER
336 std::cerr << "Pushed loop deps up." << std::endl;
337#endif
338 ddglc = std::min(lc_,ddg().latestCycle(mn_, ii(), false, false));
339 ddgec = ddg().earliestCycle(mn_, ii(), false, false);
340 rmlc = rmLC(ddglc, mn_);
341#ifdef DEBUG_BUBBLEFISH_SCHEDULER
342 std::cerr << "\t\t\t\tddgec now: " << ddgec << "ddglc now: "
343 << ddgec << "rmlc now: " << rmlc << std::endl;
344#endif
345 } else {
346 // delete loopDepPush;
347#ifdef DEBUG_BUBBLEFISH_SCHEDULER
348 std::cerr << "loop dep psuh failed." << std::endl;
349#endif
350 }
351#ifdef DEBUG_BUBBLEFISH_SCHEDULER
352 } else {
353 std::cerr << "Limiter not a loop edge." << std::endl;
354#endif
355 }
356 }
357 rmlc = rmLC(ddglc, mn_);
358 if (rmlc >= ddgec && rmlc != INT_MAX) {
359
360#ifdef DEBUG_BUBBLEFISH_SCHEDULER
361 std::cerr << "\t\tassigning to: " << rmlc << std::endl;
362#endif
363
364 createdCopy_ = assign(rmlc, mn_);
365#ifdef DEBUG_BUBBLEFISH_SCHEDULER
366 std::cerr << "\t\tAssigned ok:" << mn_.toString()
367 << " bus: " << mn_.move().bus().name() << std::endl;
368#endif
369 if (regCopyBefore != NULL) {
370 MoveNode* regCopy = regCopyBefore->getRegCopy();
371 BFScheduleBU* regCopySched =
372 new BFScheduleBU(sched_, *regCopy, lc_, false, false, false);
373 if (!runPostChild(regCopySched)) {
374 undo();
375 return false;
376 }
377 }
378
380 runPostChild(sol);
381
382#if 0
383 if (mn_.move().destination().isFUPort() &&
387 }
388#endif
389 return true;
390 } else {
392 return false;
393 }
394}
395
396void
398#ifdef DEBUG_BUBBLEFISH_SCHEDULER
399 std::cerr << "\t\tUndoing BU schedule of: " << mn_.toString() <<std::endl;
400#endif
401
402 if (!bypasserScheduled_) {
403#ifdef DEBUG_BUBBLEFISH_SCHEDULER
404 std::cerr << "\t\t\tunassigning here." << std::endl;
405#endif
406 if (mn_.isScheduled()) {
408 }
409 if (&mn_.move().bus() !=
411 mn_.move().setBus(UniversalMachine::instance().universalBus());
412 }
413#ifdef DEBUG_BUBBLEFISH_SCHEDULER
414 } else {
415 std::cerr << "\t\t\tbypasser scheduled this, no need to undo here"
416 << std::endl;
417#endif
418 }
419}
#define assert(condition)
static int prefResultCycle(const MoveNode &mn)
static bool isSourceUniversalReg(const MoveNode &mn)
unsigned int ii() const
BF2Scheduler & sched_
DataDependenceGraph & ddg()
const TTAMachine::RegisterFile * RFReadPortCountPreventsScheduling(const MoveNode &mn)
virtual void unassign(MoveNode &mn, bool disposePrologCopy=true)
virtual int rmLC(int cycle, MoveNode &mn, const TTAMachine::Bus *bus=nullptr, const TTAMachine::FunctionUnit *srcFU=nullptr, const TTAMachine::FunctionUnit *dstFU=nullptr, const TTAMachine::Bus *prologBus=nullptr, int immWriteCycle=-1, int prologImmWriteCycle=-1, const TTAMachine::ImmediateUnit *immu=nullptr, int immRegIndex=-1)
const TTAMachine::Machine & targetMachine() const
bool canBeScheduled(const MoveNode &mn)
virtual bool assign(int cycle, MoveNode &, const TTAMachine::Bus *bus=nullptr, const TTAMachine::FunctionUnit *srcFU_=nullptr, const TTAMachine::FunctionUnit *dstFU=nullptr, const TTAMachine::Bus *prologBus=nullptr, int immWriteCycle=-1, int prologImmWriteCycle=-1, const TTAMachine::ImmediateUnit *immu=nullptr, int immRegIndex=-1, bool ignoreGuardWriteCycle=false)
MoveNode * getRegCopy()
Definition BFRegCopy.hh:52
bool bypasserScheduled_
bool allowEarlySharing_
bool allowLateBypass_
bool allowEarlyBypass_
virtual bool operator()()
int earliestCycle(const MoveNode &moveNode, unsigned int ii=UINT_MAX, bool ignoreRegWaRs=false, bool ignoreRegWaWs=false, bool ignoreGuards=false, bool ignoreFUDeps=false, bool ignoreSameOperationEdges=false, bool assumeBypassing=false) const
virtual void writeToDotFile(const TCEString &fileName) const
static FUSet copyOpFUs(const TTAMachine::Machine &mach, const MoveNode &mn)
bool isSourceVariable() const
Definition MoveNode.cc:196
bool isDestinationOperation() const
std::string toString() const
Definition MoveNode.cc:576
TTAProgram::Move & move()
bool isSourceOperation() const
Definition MoveNode.cc:168
bool isScheduled() const
Definition MoveNode.cc:409
bool isDestinationVariable() const
Definition MoveNode.cc:264
ProgramOperation & destinationOperation(unsigned int index=0) const
bool runPostChild(Reversible *preChild)
std::stack< Reversible * > preChildren_
Definition Reversible.hh:57
virtual void undo()
Definition Reversible.cc:69
void undoAndRemovePreChildren()
Definition Reversible.cc:80
bool runPreChild(Reversible *preChild)
virtual int width() const
virtual TCEString name() const
bool isControlFlowMove() const
Definition Move.cc:233
void setBus(const TTAMachine::Bus &bus)
Definition Move.cc:383
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 bool isGPR() const
Definition Terminal.cc:107
virtual const TTAMachine::RegisterFile & registerFile() const
Definition Terminal.cc:225
virtual bool isFUPort() const
Definition Terminal.cc:118
static UniversalMachine & instance()
TTAMachine::Bus & universalBus() const