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
)
55 for (ArrayList::Iterator it
= allInsns
.iterator(); !it
.end(); it
.next())
56 delete_Instruction(prog
, reinterpret_cast<Instruction
*>(it
.get()));
58 for (ArrayList::Iterator it
= allLValues
.iterator(); !it
.end(); it
.next())
59 delete_Value(prog
, reinterpret_cast<LValue
*>(it
.get()));
61 for (ArrayList::Iterator BBs
= allBBlocks
.iterator(); !BBs
.end(); BBs
.next())
62 delete reinterpret_cast<BasicBlock
*>(BBs
.get());
65 BasicBlock::BasicBlock(Function
*fn
) : cfg(this), dom(this), func(fn
)
67 program
= func
->getProgram();
69 joinAt
= phi
= entry
= exit
= NULL
;
77 func
->add(this, this->id
);
80 BasicBlock::~BasicBlock()
86 BasicBlock::clone(ClonePolicy
<Function
>& pol
) const
88 BasicBlock
*bb
= new BasicBlock(pol
.context());
92 for (Instruction
*i
= getFirst(); i
; i
= i
->next
)
93 bb
->insertTail(i
->clone(pol
));
95 pol
.context()->cfg
.insert(&bb
->cfg
);
97 for (Graph::EdgeIterator it
= cfg
.outgoing(); !it
.end(); it
.next()) {
98 BasicBlock
*obb
= BasicBlock::get(it
.getNode());
99 bb
->cfg
.attach(&pol
.get(obb
)->cfg
, it
.getType());
106 BasicBlock::idom() const
108 Graph::Node
*dn
= dom
.parent();
109 return dn
? BasicBlock::get(dn
) : NULL
;
113 BasicBlock::insertHead(Instruction
*inst
)
115 assert(inst
->next
== 0 && inst
->prev
== 0);
117 if (inst
->op
== OP_PHI
) {
119 insertBefore(phi
, inst
);
122 insertBefore(entry
, inst
);
132 insertBefore(entry
, inst
);
135 insertAfter(exit
, inst
); // after last phi
147 BasicBlock::insertTail(Instruction
*inst
)
149 assert(inst
->next
== 0 && inst
->prev
== 0);
151 if (inst
->op
== OP_PHI
) {
153 insertBefore(entry
, inst
);
157 insertAfter(exit
, inst
);
166 insertAfter(exit
, inst
);
177 BasicBlock::insertBefore(Instruction
*q
, Instruction
*p
)
181 assert(p
->next
== 0 && p
->prev
== 0);
184 if (p
->op
== OP_PHI
) {
192 assert(p
->op
== OP_PHI
);
207 BasicBlock::insertAfter(Instruction
*p
, Instruction
*q
)
210 assert(q
->op
!= OP_PHI
|| p
->op
== OP_PHI
);
212 assert(q
->next
== 0 && q
->prev
== 0);
216 if (p
->op
== OP_PHI
&& q
->op
!= OP_PHI
)
230 BasicBlock::remove(Instruction
*insn
)
232 assert(insn
->bb
== this);
235 insn
->prev
->next
= insn
->next
;
238 insn
->next
->prev
= insn
->prev
;
246 if (insn
->prev
&& insn
->prev
->op
!= OP_PHI
)
253 phi
= (insn
->next
&& insn
->next
->op
== OP_PHI
) ? insn
->next
: 0;
261 void BasicBlock::permuteAdjacent(Instruction
*a
, Instruction
*b
)
263 assert(a
->bb
== b
->bb
);
270 assert(a
->next
== b
);
271 assert(a
->op
!= OP_PHI
&& b
->op
!= OP_PHI
);
290 BasicBlock::splitCommon(Instruction
*insn
, BasicBlock
*bb
, bool attach
)
304 while (!cfg
.outgoing(true).end()) {
305 Graph::Edge
*e
= cfg
.outgoing(true).getEdge();
306 bb
->cfg
.attach(e
->getTarget(), e
->getType());
307 this->cfg
.detach(e
->getTarget());
310 for (; insn
; insn
= insn
->next
) {
317 this->cfg
.attach(&bb
->cfg
, Graph::Edge::TREE
);
321 BasicBlock::splitBefore(Instruction
*insn
, bool attach
)
323 BasicBlock
*bb
= new BasicBlock(func
);
324 assert(!insn
|| insn
->op
!= OP_PHI
);
326 splitCommon(insn
, bb
, attach
);
331 BasicBlock::splitAfter(Instruction
*insn
, bool attach
)
333 BasicBlock
*bb
= new BasicBlock(func
);
334 assert(!insn
|| insn
->op
!= OP_PHI
);
339 splitCommon(insn
? insn
->next
: NULL
, bb
, attach
);
344 BasicBlock::dominatedBy(BasicBlock
*that
)
346 Graph::Node
*bn
= &that
->dom
;
347 Graph::Node
*dn
= &this->dom
;
349 while (dn
&& dn
!= bn
)
356 BasicBlock::initiatesSimpleConditional() const
360 Graph::Edge::Type eR
;
362 if (cfg
.outgoingCount() != 2) // -> if and -> else/endif
366 for (Graph::EdgeIterator ei
= cfg
.outgoing(); !ei
.end(); ei
.next())
367 out
[n
++] = ei
.getNode();
368 eR
= out
[1]->outgoing().getType();
370 // IF block is out edge to the right
371 if (eR
== Graph::Edge::CROSS
|| eR
== Graph::Edge::BACK
)
374 if (out
[1]->outgoingCount() != 1) // 0 is IF { RET; }, >1 is more divergence
376 // do they reconverge immediately ?
377 if (out
[1]->outgoing().getNode() == out
[0])
379 if (out
[0]->outgoingCount() == 1)
380 if (out
[0]->outgoing().getNode() == out
[1]->outgoing().getNode())
387 Function::setEntry(BasicBlock
*bb
)
391 cfg
.insert(&bb
->cfg
);
396 Function::setExit(BasicBlock
*bb
)
405 Function::orderInstructions(ArrayList
&result
)
407 for (IteratorRef it
= cfg
.iteratorCFG(); !it
->end(); it
->next()) {
409 BasicBlock::get(reinterpret_cast<Graph::Node
*>(it
->get()));
411 for (Instruction
*insn
= bb
->getFirst(); insn
; insn
= insn
->next
)
412 result
.insert(insn
, insn
->serial
);
415 return result
.getSize();
419 Function::buildLiveSets()
421 for (unsigned i
= 0; i
<= loopNestingBound
; ++i
)
422 buildLiveSetsPreSSA(BasicBlock::get(cfg
.getRoot()), cfg
.nextSequence());
424 for (ArrayList::Iterator bi
= allBBlocks
.iterator(); !bi
.end(); bi
.next())
425 BasicBlock::get(bi
)->liveSet
.marker
= false;
429 Function::buildDefSets()
431 for (unsigned i
= 0; i
<= loopNestingBound
; ++i
)
432 buildDefSetsPreSSA(BasicBlock::get(cfgExit
), cfg
.nextSequence());
434 for (ArrayList::Iterator bi
= allBBlocks
.iterator(); !bi
.end(); bi
.next())
435 BasicBlock::get(bi
)->liveSet
.marker
= false;
439 Pass::run(Program
*prog
, bool ordered
, bool skipPhi
)
443 return doRun(prog
, ordered
, skipPhi
);
447 Pass::doRun(Program
*prog
, bool ordered
, bool skipPhi
)
449 for (IteratorRef it
= prog
->calls
.iteratorDFS(false);
450 !it
->end(); it
->next()) {
451 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(it
->get());
452 if (!doRun(Function::get(n
), ordered
, skipPhi
))
459 Pass::run(Function
*func
, bool ordered
, bool skipPhi
)
461 prog
= func
->getProgram();
463 return doRun(func
, ordered
, skipPhi
);
467 Pass::doRun(Function
*func
, bool ordered
, bool skipPhi
)
471 Instruction
*insn
, *next
;
477 bbIter
= ordered
? func
->cfg
.iteratorCFG() : func
->cfg
.iteratorDFS();
479 for (; !bbIter
->end(); bbIter
->next()) {
480 bb
= BasicBlock::get(reinterpret_cast<Graph::Node
*>(bbIter
->get()));
483 for (insn
= skipPhi
? bb
->getEntry() : bb
->getFirst(); insn
!= NULL
;
495 Function::printCFGraph(const char *filePath
)
497 FILE *out
= fopen(filePath
, "a");
499 ERROR("failed to open file: %s\n", filePath
);
502 INFO("printing control flow graph to: %s\n", filePath
);
504 fprintf(out
, "digraph G {\n");
506 for (IteratorRef it
= cfg
.iteratorDFS(); !it
->end(); it
->next()) {
507 BasicBlock
*bb
= BasicBlock::get(
508 reinterpret_cast<Graph::Node
*>(it
->get()));
509 int idA
= bb
->getId();
510 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
511 int idB
= BasicBlock::get(ei
.getNode())->getId();
512 switch (ei
.getType()) {
513 case Graph::Edge::TREE
:
514 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
516 case Graph::Edge::FORWARD
:
517 fprintf(out
, "\t%i -> %i [color=green];\n", idA
, idB
);
519 case Graph::Edge::CROSS
:
520 fprintf(out
, "\t%i -> %i [color=red];\n", idA
, idB
);
522 case Graph::Edge::BACK
:
523 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
525 case Graph::Edge::DUMMY
:
526 fprintf(out
, "\t%i -> %i [style=dotted];\n", idA
, idB
);
539 } // namespace nv50_ir