7b211a2e0d12477792d76470903c94a592dd3915
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
)
56 for (ArrayList::Iterator it
= allInsns
.iterator(); !it
.end(); it
.next())
57 delete_Instruction(prog
, reinterpret_cast<Instruction
*>(it
.get()));
59 for (ArrayList::Iterator it
= allLValues
.iterator(); !it
.end(); it
.next())
60 delete_Value(prog
, reinterpret_cast<LValue
*>(it
.get()));
62 for (ArrayList::Iterator BBs
= allBBlocks
.iterator(); !BBs
.end(); BBs
.next())
63 delete reinterpret_cast<BasicBlock
*>(BBs
.get());
66 BasicBlock::BasicBlock(Function
*fn
) : cfg(this), dom(this), func(fn
)
68 program
= func
->getProgram();
70 joinAt
= phi
= entry
= exit
= NULL
;
78 func
->add(this, this->id
);
81 BasicBlock::~BasicBlock()
87 BasicBlock::clone(ClonePolicy
<Function
>& pol
) const
89 BasicBlock
*bb
= new BasicBlock(pol
.context());
93 for (Instruction
*i
= getFirst(); i
; i
= i
->next
)
94 bb
->insertTail(i
->clone(pol
));
96 pol
.context()->cfg
.insert(&bb
->cfg
);
98 for (Graph::EdgeIterator it
= cfg
.outgoing(); !it
.end(); it
.next()) {
99 BasicBlock
*obb
= BasicBlock::get(it
.getNode());
100 bb
->cfg
.attach(&pol
.get(obb
)->cfg
, it
.getType());
107 BasicBlock::idom() const
109 Graph::Node
*dn
= dom
.parent();
110 return dn
? BasicBlock::get(dn
) : NULL
;
114 BasicBlock::insertHead(Instruction
*inst
)
116 assert(inst
->next
== 0 && inst
->prev
== 0);
118 if (inst
->op
== OP_PHI
) {
120 insertBefore(phi
, inst
);
123 insertBefore(entry
, inst
);
133 insertBefore(entry
, inst
);
136 insertAfter(exit
, inst
); // after last phi
148 BasicBlock::insertTail(Instruction
*inst
)
150 assert(inst
->next
== 0 && inst
->prev
== 0);
152 if (inst
->op
== OP_PHI
) {
154 insertBefore(entry
, inst
);
158 insertAfter(exit
, inst
);
167 insertAfter(exit
, inst
);
178 BasicBlock::insertBefore(Instruction
*q
, Instruction
*p
)
182 assert(p
->next
== 0 && p
->prev
== 0);
185 if (p
->op
== OP_PHI
) {
193 assert(p
->op
== OP_PHI
);
208 BasicBlock::insertAfter(Instruction
*p
, Instruction
*q
)
211 assert(q
->op
!= OP_PHI
|| p
->op
== OP_PHI
);
213 assert(q
->next
== 0 && q
->prev
== 0);
217 if (p
->op
== OP_PHI
&& q
->op
!= OP_PHI
)
231 BasicBlock::remove(Instruction
*insn
)
233 assert(insn
->bb
== this);
236 insn
->prev
->next
= insn
->next
;
239 insn
->next
->prev
= insn
->prev
;
247 if (insn
->prev
&& insn
->prev
->op
!= OP_PHI
)
254 phi
= (insn
->next
&& insn
->next
->op
== OP_PHI
) ? insn
->next
: 0;
262 void BasicBlock::permuteAdjacent(Instruction
*a
, Instruction
*b
)
264 assert(a
->bb
== b
->bb
);
271 assert(a
->next
== b
);
272 assert(a
->op
!= OP_PHI
&& b
->op
!= OP_PHI
);
291 BasicBlock::splitCommon(Instruction
*insn
, BasicBlock
*bb
, bool attach
)
305 while (!cfg
.outgoing(true).end()) {
306 Graph::Edge
*e
= cfg
.outgoing(true).getEdge();
307 bb
->cfg
.attach(e
->getTarget(), e
->getType());
308 this->cfg
.detach(e
->getTarget());
311 for (; insn
; insn
= insn
->next
) {
318 this->cfg
.attach(&bb
->cfg
, Graph::Edge::TREE
);
322 BasicBlock::splitBefore(Instruction
*insn
, bool attach
)
324 BasicBlock
*bb
= new BasicBlock(func
);
325 assert(!insn
|| insn
->op
!= OP_PHI
);
327 splitCommon(insn
, bb
, attach
);
332 BasicBlock::splitAfter(Instruction
*insn
, bool attach
)
334 BasicBlock
*bb
= new BasicBlock(func
);
335 assert(!insn
|| insn
->op
!= OP_PHI
);
340 splitCommon(insn
? insn
->next
: NULL
, bb
, attach
);
345 BasicBlock::dominatedBy(BasicBlock
*that
)
347 Graph::Node
*bn
= &that
->dom
;
348 Graph::Node
*dn
= &this->dom
;
350 while (dn
&& dn
!= bn
)
357 BasicBlock::initiatesSimpleConditional() const
361 Graph::Edge::Type eR
;
363 if (cfg
.outgoingCount() != 2) // -> if and -> else/endif
367 for (Graph::EdgeIterator ei
= cfg
.outgoing(); !ei
.end(); ei
.next())
368 out
[n
++] = ei
.getNode();
369 eR
= out
[1]->outgoing().getType();
371 // IF block is out edge to the right
372 if (eR
== Graph::Edge::CROSS
|| eR
== Graph::Edge::BACK
)
375 if (out
[1]->outgoingCount() != 1) // 0 is IF { RET; }, >1 is more divergence
377 // do they reconverge immediately ?
378 if (out
[1]->outgoing().getNode() == out
[0])
380 if (out
[0]->outgoingCount() == 1)
381 if (out
[0]->outgoing().getNode() == out
[1]->outgoing().getNode())
388 Function::setEntry(BasicBlock
*bb
)
392 cfg
.insert(&bb
->cfg
);
397 Function::setExit(BasicBlock
*bb
)
406 Function::orderInstructions(ArrayList
&result
)
410 for (IteratorRef it
= cfg
.iteratorCFG(); !it
->end(); it
->next()) {
412 BasicBlock::get(reinterpret_cast<Graph::Node
*>(it
->get()));
414 for (Instruction
*insn
= bb
->getFirst(); insn
; insn
= insn
->next
)
415 result
.insert(insn
, insn
->serial
);
418 return result
.getSize();
422 Function::buildLiveSets()
424 for (unsigned i
= 0; i
<= loopNestingBound
; ++i
)
425 buildLiveSetsPreSSA(BasicBlock::get(cfg
.getRoot()), cfg
.nextSequence());
427 for (ArrayList::Iterator bi
= allBBlocks
.iterator(); !bi
.end(); bi
.next())
428 BasicBlock::get(bi
)->liveSet
.marker
= false;
432 Function::buildDefSets()
434 for (unsigned i
= 0; i
<= loopNestingBound
; ++i
)
435 buildDefSetsPreSSA(BasicBlock::get(cfgExit
), cfg
.nextSequence());
437 for (ArrayList::Iterator bi
= allBBlocks
.iterator(); !bi
.end(); bi
.next())
438 BasicBlock::get(bi
)->liveSet
.marker
= false;
442 Pass::run(Program
*prog
, bool ordered
, bool skipPhi
)
446 return doRun(prog
, ordered
, skipPhi
);
450 Pass::doRun(Program
*prog
, bool ordered
, bool skipPhi
)
452 for (IteratorRef it
= prog
->calls
.iteratorDFS(false);
453 !it
->end(); it
->next()) {
454 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(it
->get());
455 if (!doRun(Function::get(n
), ordered
, skipPhi
))
462 Pass::run(Function
*func
, bool ordered
, bool skipPhi
)
464 prog
= func
->getProgram();
466 return doRun(func
, ordered
, skipPhi
);
470 Pass::doRun(Function
*func
, bool ordered
, bool skipPhi
)
474 Instruction
*insn
, *next
;
480 bbIter
= ordered
? func
->cfg
.iteratorCFG() : func
->cfg
.iteratorDFS();
482 for (; !bbIter
->end(); bbIter
->next()) {
483 bb
= BasicBlock::get(reinterpret_cast<Graph::Node
*>(bbIter
->get()));
486 for (insn
= skipPhi
? bb
->getEntry() : bb
->getFirst(); insn
!= NULL
;
498 Function::printCFGraph(const char *filePath
)
500 FILE *out
= fopen(filePath
, "a");
502 ERROR("failed to open file: %s\n", filePath
);
505 INFO("printing control flow graph to: %s\n", filePath
);
507 fprintf(out
, "digraph G {\n");
509 for (IteratorRef it
= cfg
.iteratorDFS(); !it
->end(); it
->next()) {
510 BasicBlock
*bb
= BasicBlock::get(
511 reinterpret_cast<Graph::Node
*>(it
->get()));
512 int idA
= bb
->getId();
513 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
514 int idB
= BasicBlock::get(ei
.getNode())->getId();
515 switch (ei
.getType()) {
516 case Graph::Edge::TREE
:
517 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
519 case Graph::Edge::FORWARD
:
520 fprintf(out
, "\t%i -> %i [color=green];\n", idA
, idB
);
522 case Graph::Edge::CROSS
:
523 fprintf(out
, "\t%i -> %i [color=red];\n", idA
, idB
);
525 case Graph::Edge::BACK
:
526 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
528 case Graph::Edge::DUMMY
:
529 fprintf(out
, "\t%i -> %i [style=dotted];\n", idA
, idB
);
542 } // namespace nv50_ir