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
, uint32_t label
)
60 for (ArrayList::Iterator it
= allInsns
.iterator(); !it
.end(); it
.next())
61 delete_Instruction(prog
, reinterpret_cast<Instruction
*>(it
.get()));
63 for (ArrayList::Iterator it
= allLValues
.iterator(); !it
.end(); it
.next())
64 delete_Value(prog
, reinterpret_cast<LValue
*>(it
.get()));
66 for (ArrayList::Iterator BBs
= allBBlocks
.iterator(); !BBs
.end(); BBs
.next())
67 delete reinterpret_cast<BasicBlock
*>(BBs
.get());
70 BasicBlock::BasicBlock(Function
*fn
) : cfg(this), dom(this), func(fn
)
72 program
= func
->getProgram();
74 joinAt
= phi
= entry
= exit
= NULL
;
82 func
->add(this, this->id
);
85 BasicBlock::~BasicBlock()
91 BasicBlock::clone(ClonePolicy
<Function
>& pol
) const
93 BasicBlock
*bb
= new BasicBlock(pol
.context());
97 for (Instruction
*i
= getFirst(); i
; i
= i
->next
)
98 bb
->insertTail(i
->clone(pol
));
100 pol
.context()->cfg
.insert(&bb
->cfg
);
102 for (Graph::EdgeIterator it
= cfg
.outgoing(); !it
.end(); it
.next()) {
103 BasicBlock
*obb
= BasicBlock::get(it
.getNode());
104 bb
->cfg
.attach(&pol
.get(obb
)->cfg
, it
.getType());
111 BasicBlock::idom() const
113 Graph::Node
*dn
= dom
.parent();
114 return dn
? BasicBlock::get(dn
) : NULL
;
118 BasicBlock::insertHead(Instruction
*inst
)
120 assert(inst
->next
== 0 && inst
->prev
== 0);
122 if (inst
->op
== OP_PHI
) {
124 insertBefore(phi
, inst
);
127 insertBefore(entry
, inst
);
137 insertBefore(entry
, inst
);
140 insertAfter(exit
, inst
); // after last phi
152 BasicBlock::insertTail(Instruction
*inst
)
154 assert(inst
->next
== 0 && inst
->prev
== 0);
156 if (inst
->op
== OP_PHI
) {
158 insertBefore(entry
, inst
);
162 insertAfter(exit
, inst
);
171 insertAfter(exit
, inst
);
182 BasicBlock::insertBefore(Instruction
*q
, Instruction
*p
)
186 assert(p
->next
== 0 && p
->prev
== 0);
189 if (p
->op
== OP_PHI
) {
197 assert(p
->op
== OP_PHI
);
212 BasicBlock::insertAfter(Instruction
*p
, Instruction
*q
)
215 assert(q
->op
!= OP_PHI
|| p
->op
== OP_PHI
);
217 assert(q
->next
== 0 && q
->prev
== 0);
221 if (p
->op
== OP_PHI
&& q
->op
!= OP_PHI
)
235 BasicBlock::remove(Instruction
*insn
)
237 assert(insn
->bb
== this);
240 insn
->prev
->next
= insn
->next
;
243 insn
->next
->prev
= insn
->prev
;
251 if (insn
->prev
&& insn
->prev
->op
!= OP_PHI
)
258 phi
= (insn
->next
&& insn
->next
->op
== OP_PHI
) ? insn
->next
: 0;
266 void BasicBlock::permuteAdjacent(Instruction
*a
, Instruction
*b
)
268 assert(a
->bb
== b
->bb
);
275 assert(a
->next
== b
);
276 assert(a
->op
!= OP_PHI
&& b
->op
!= OP_PHI
);
295 BasicBlock::splitCommon(Instruction
*insn
, BasicBlock
*bb
, bool attach
)
309 while (!cfg
.outgoing(true).end()) {
310 Graph::Edge
*e
= cfg
.outgoing(true).getEdge();
311 bb
->cfg
.attach(e
->getTarget(), e
->getType());
312 this->cfg
.detach(e
->getTarget());
315 for (; insn
; insn
= insn
->next
) {
322 this->cfg
.attach(&bb
->cfg
, Graph::Edge::TREE
);
326 BasicBlock::splitBefore(Instruction
*insn
, bool attach
)
328 BasicBlock
*bb
= new BasicBlock(func
);
329 assert(!insn
|| insn
->op
!= OP_PHI
);
331 splitCommon(insn
, bb
, attach
);
336 BasicBlock::splitAfter(Instruction
*insn
, bool attach
)
338 BasicBlock
*bb
= new BasicBlock(func
);
339 assert(!insn
|| insn
->op
!= OP_PHI
);
344 splitCommon(insn
? insn
->next
: NULL
, bb
, attach
);
349 BasicBlock::dominatedBy(BasicBlock
*that
)
351 Graph::Node
*bn
= &that
->dom
;
352 Graph::Node
*dn
= &this->dom
;
354 while (dn
&& dn
!= bn
)
361 BasicBlock::initiatesSimpleConditional() const
365 Graph::Edge::Type eR
;
367 if (cfg
.outgoingCount() != 2) // -> if and -> else/endif
371 for (Graph::EdgeIterator ei
= cfg
.outgoing(); !ei
.end(); ei
.next())
372 out
[n
++] = ei
.getNode();
373 eR
= out
[1]->outgoing().getType();
375 // IF block is out edge to the right
376 if (eR
== Graph::Edge::CROSS
|| eR
== Graph::Edge::BACK
)
379 if (out
[1]->outgoingCount() != 1) // 0 is IF { RET; }, >1 is more divergence
381 // do they reconverge immediately ?
382 if (out
[1]->outgoing().getNode() == out
[0])
384 if (out
[0]->outgoingCount() == 1)
385 if (out
[0]->outgoing().getNode() == out
[1]->outgoing().getNode())
392 Function::setEntry(BasicBlock
*bb
)
396 cfg
.insert(&bb
->cfg
);
401 Function::setExit(BasicBlock
*bb
)
410 Function::orderInstructions(ArrayList
&result
)
414 for (IteratorRef it
= cfg
.iteratorCFG(); !it
->end(); it
->next()) {
416 BasicBlock::get(reinterpret_cast<Graph::Node
*>(it
->get()));
418 for (Instruction
*insn
= bb
->getFirst(); insn
; insn
= insn
->next
)
419 result
.insert(insn
, insn
->serial
);
422 return result
.getSize();
426 Function::buildLiveSets()
428 for (unsigned i
= 0; i
<= loopNestingBound
; ++i
)
429 buildLiveSetsPreSSA(BasicBlock::get(cfg
.getRoot()), cfg
.nextSequence());
431 for (ArrayList::Iterator bi
= allBBlocks
.iterator(); !bi
.end(); bi
.next())
432 BasicBlock::get(bi
)->liveSet
.marker
= false;
436 Function::buildDefSets()
438 for (unsigned i
= 0; i
<= loopNestingBound
; ++i
)
439 buildDefSetsPreSSA(BasicBlock::get(cfgExit
), cfg
.nextSequence());
441 for (ArrayList::Iterator bi
= allBBlocks
.iterator(); !bi
.end(); bi
.next())
442 BasicBlock::get(bi
)->liveSet
.marker
= false;
446 Pass::run(Program
*prog
, bool ordered
, bool skipPhi
)
450 return doRun(prog
, ordered
, skipPhi
);
454 Pass::doRun(Program
*prog
, bool ordered
, bool skipPhi
)
456 for (IteratorRef it
= prog
->calls
.iteratorDFS(false);
457 !it
->end(); it
->next()) {
458 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(it
->get());
459 if (!doRun(Function::get(n
), ordered
, skipPhi
))
466 Pass::run(Function
*func
, bool ordered
, bool skipPhi
)
468 prog
= func
->getProgram();
470 return doRun(func
, ordered
, skipPhi
);
474 Pass::doRun(Function
*func
, bool ordered
, bool skipPhi
)
478 Instruction
*insn
, *next
;
484 bbIter
= ordered
? func
->cfg
.iteratorCFG() : func
->cfg
.iteratorDFS();
486 for (; !bbIter
->end(); bbIter
->next()) {
487 bb
= BasicBlock::get(reinterpret_cast<Graph::Node
*>(bbIter
->get()));
490 for (insn
= skipPhi
? bb
->getEntry() : bb
->getFirst(); insn
!= NULL
;
502 Function::printCFGraph(const char *filePath
)
504 FILE *out
= fopen(filePath
, "a");
506 ERROR("failed to open file: %s\n", filePath
);
509 INFO("printing control flow graph to: %s\n", filePath
);
511 fprintf(out
, "digraph G {\n");
513 for (IteratorRef it
= cfg
.iteratorDFS(); !it
->end(); it
->next()) {
514 BasicBlock
*bb
= BasicBlock::get(
515 reinterpret_cast<Graph::Node
*>(it
->get()));
516 int idA
= bb
->getId();
517 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
518 int idB
= BasicBlock::get(ei
.getNode())->getId();
519 switch (ei
.getType()) {
520 case Graph::Edge::TREE
:
521 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
523 case Graph::Edge::FORWARD
:
524 fprintf(out
, "\t%i -> %i [color=green];\n", idA
, idB
);
526 case Graph::Edge::CROSS
:
527 fprintf(out
, "\t%i -> %i [color=red];\n", idA
, idB
);
529 case Graph::Edge::BACK
:
530 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
532 case Graph::Edge::DUMMY
:
533 fprintf(out
, "\t%i -> %i [style=dotted];\n", idA
, idB
);
546 } // namespace nv50_ir