2 * Copyright 2011 Christoph Bumiller
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 Function::Function(Program
*p
, const char *fnName
)
53 for (ArrayList::Iterator BBs
= allBBlocks
.iterator(); !BBs
.end(); BBs
.next())
54 delete reinterpret_cast<BasicBlock
*>(BBs
.get());
57 BasicBlock::BasicBlock(Function
*fn
) : cfg(this), dom(this), func(fn
)
59 program
= func
->getProgram();
61 joinAt
= phi
= entry
= exit
= NULL
;
69 func
->add(this, this->id
);
72 BasicBlock::~BasicBlock()
78 BasicBlock::idom() const
80 Graph::Node
*dn
= dom
.parent();
81 return dn
? BasicBlock::get(dn
) : NULL
;
85 BasicBlock::insertHead(Instruction
*inst
)
87 assert(inst
->next
== 0 && inst
->prev
== 0);
89 if (inst
->op
== OP_PHI
) {
91 insertBefore(phi
, inst
);
94 insertBefore(entry
, inst
);
104 insertBefore(entry
, inst
);
107 insertAfter(exit
, inst
); // after last phi
119 BasicBlock::insertTail(Instruction
*inst
)
121 assert(inst
->next
== 0 && inst
->prev
== 0);
123 if (inst
->op
== OP_PHI
) {
125 insertBefore(entry
, inst
);
129 insertAfter(exit
, inst
);
138 insertAfter(exit
, inst
);
149 BasicBlock::insertBefore(Instruction
*q
, Instruction
*p
)
153 assert(p
->next
== 0 && p
->prev
== 0);
156 if (p
->op
== OP_PHI
) {
164 assert(p
->op
== OP_PHI
);
179 BasicBlock::insertAfter(Instruction
*p
, Instruction
*q
)
182 assert(q
->op
!= OP_PHI
|| p
->op
== OP_PHI
);
184 assert(q
->next
== 0 && q
->prev
== 0);
188 if (p
->op
== OP_PHI
&& q
->op
!= OP_PHI
)
202 BasicBlock::remove(Instruction
*insn
)
204 assert(insn
->bb
== this);
207 insn
->prev
->next
= insn
->next
;
210 insn
->next
->prev
= insn
->prev
;
218 if (insn
->prev
&& insn
->prev
->op
!= OP_PHI
)
225 phi
= (insn
->next
&& insn
->next
->op
== OP_PHI
) ? insn
->next
: 0;
233 void BasicBlock::permuteAdjacent(Instruction
*a
, Instruction
*b
)
235 assert(a
->bb
== b
->bb
);
242 assert(a
->next
== b
);
243 assert(a
->op
!= OP_PHI
&& b
->op
!= OP_PHI
);
262 BasicBlock::splitCommon(Instruction
*insn
, BasicBlock
*bb
, bool attach
)
276 while (!cfg
.outgoing(true).end()) {
277 Graph::Edge
*e
= cfg
.outgoing(true).getEdge();
278 bb
->cfg
.attach(e
->getTarget(), e
->getType());
279 this->cfg
.detach(e
->getTarget());
282 for (; insn
; insn
= insn
->next
) {
289 this->cfg
.attach(&bb
->cfg
, Graph::Edge::TREE
);
293 BasicBlock::splitBefore(Instruction
*insn
, bool attach
)
295 BasicBlock
*bb
= new BasicBlock(func
);
296 assert(!insn
|| insn
->op
!= OP_PHI
);
298 splitCommon(insn
, bb
, attach
);
303 BasicBlock::splitAfter(Instruction
*insn
, bool attach
)
305 BasicBlock
*bb
= new BasicBlock(func
);
306 assert(!insn
|| insn
->op
!= OP_PHI
);
311 splitCommon(insn
? insn
->next
: NULL
, bb
, attach
);
316 BasicBlock::dominatedBy(BasicBlock
*that
)
318 Graph::Node
*bn
= &that
->dom
;
319 Graph::Node
*dn
= &this->dom
;
321 while (dn
&& dn
!= bn
)
328 BasicBlock::initiatesSimpleConditional() const
332 Graph::Edge::Type eR
;
334 if (cfg
.outgoingCount() != 2) // -> if and -> else/endif
338 for (Graph::EdgeIterator ei
= cfg
.outgoing(); !ei
.end(); ei
.next())
339 out
[n
++] = ei
.getNode();
340 eR
= out
[1]->outgoing().getType();
342 // IF block is out edge to the right
343 if (eR
== Graph::Edge::CROSS
|| eR
== Graph::Edge::BACK
)
346 if (out
[1]->outgoingCount() != 1) // 0 is IF { RET; }, >1 is more divergence
348 // do they reconverge immediately ?
349 if (out
[1]->outgoing().getNode() == out
[0])
351 if (out
[0]->outgoingCount() == 1)
352 if (out
[0]->outgoing().getNode() == out
[1]->outgoing().getNode())
359 Function::setEntry(BasicBlock
*bb
)
363 cfg
.insert(&bb
->cfg
);
368 Function::setExit(BasicBlock
*bb
)
377 Function::orderInstructions(ArrayList
&result
)
380 for (iter
= cfg
.iteratorCFG(); !iter
->end(); iter
->next()) {
382 BasicBlock::get(reinterpret_cast<Graph::Node
*>(iter
->get()));
384 for (Instruction
*insn
= bb
->getFirst(); insn
; insn
= insn
->next
)
385 result
.insert(insn
, insn
->serial
);
388 cfg
.putIterator(iter
);
389 return result
.getSize();
393 Pass::run(Program
*prog
, bool ordered
, bool skipPhi
)
397 return doRun(prog
, ordered
, skipPhi
);
401 Pass::doRun(Program
*prog
, bool ordered
, bool skipPhi
)
403 for (ArrayList::Iterator fi
= prog
->allFuncs
.iterator();
404 !fi
.end(); fi
.next()) {
405 Function
*fn
= reinterpret_cast<Function
*>(fi
.get());
406 if (!doRun(fn
, ordered
, skipPhi
))
413 Pass::run(Function
*func
, bool ordered
, bool skipPhi
)
415 prog
= func
->getProgram();
417 return doRun(func
, ordered
, skipPhi
);
421 Pass::doRun(Function
*func
, bool ordered
, bool skipPhi
)
425 Instruction
*insn
, *next
;
431 bbIter
= ordered
? func
->cfg
.iteratorCFG() : func
->cfg
.iteratorDFS();
433 for (; !bbIter
->end(); bbIter
->next()) {
434 bb
= BasicBlock::get(reinterpret_cast<Graph::Node
*>(bbIter
->get()));
437 for (insn
= skipPhi
? bb
->getEntry() : bb
->getFirst(); insn
!= NULL
;
444 func
->cfg
.putIterator(bbIter
);
449 Function::printCFGraph(const char *filePath
)
451 FILE *out
= fopen(filePath
, "a");
453 ERROR("failed to open file: %s\n", filePath
);
456 INFO("printing control flow graph to: %s\n", filePath
);
458 fprintf(out
, "digraph G {\n");
461 for (iter
= cfg
.iteratorDFS(); !iter
->end(); iter
->next()) {
462 BasicBlock
*bb
= BasicBlock::get(
463 reinterpret_cast<Graph::Node
*>(iter
->get()));
464 int idA
= bb
->getId();
465 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
466 int idB
= BasicBlock::get(ei
.getNode())->getId();
467 switch (ei
.getType()) {
468 case Graph::Edge::TREE
:
469 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
471 case Graph::Edge::FORWARD
:
472 fprintf(out
, "\t%i -> %i [color=green];\n", idA
, idB
);
474 case Graph::Edge::CROSS
:
475 fprintf(out
, "\t%i -> %i [color=red];\n", idA
, idB
);
477 case Graph::Edge::BACK
:
478 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
480 case Graph::Edge::DUMMY
:
481 fprintf(out
, "\t%i -> %i [style=dotted];\n", idA
, idB
);
489 cfg
.putIterator(iter
);
495 } // namespace nv50_ir