54d8ef82bb3d533adef8a1b46abcf23afb968166
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 it
= allInsns
.iterator(); !it
.end(); it
.next())
54 delete_Instruction(prog
, reinterpret_cast<Instruction
*>(it
.get()));
56 for (ArrayList::Iterator it
= allLValues
.iterator(); !it
.end(); it
.next())
57 delete_Value(prog
, reinterpret_cast<LValue
*>(it
.get()));
59 for (ArrayList::Iterator BBs
= allBBlocks
.iterator(); !BBs
.end(); BBs
.next())
60 delete reinterpret_cast<BasicBlock
*>(BBs
.get());
63 BasicBlock::BasicBlock(Function
*fn
) : cfg(this), dom(this), func(fn
)
65 program
= func
->getProgram();
67 joinAt
= phi
= entry
= exit
= NULL
;
75 func
->add(this, this->id
);
78 BasicBlock::~BasicBlock()
84 BasicBlock::clone(ClonePolicy
<Function
>& pol
) const
86 BasicBlock
*bb
= new BasicBlock(pol
.context());
90 for (Instruction
*i
= getFirst(); i
; i
= i
->next
)
91 bb
->insertTail(i
->clone(pol
));
93 pol
.context()->cfg
.insert(&bb
->cfg
);
95 for (Graph::EdgeIterator it
= cfg
.outgoing(); !it
.end(); it
.next()) {
96 BasicBlock
*obb
= BasicBlock::get(it
.getNode());
97 bb
->cfg
.attach(&pol
.get(obb
)->cfg
, it
.getType());
104 BasicBlock::idom() const
106 Graph::Node
*dn
= dom
.parent();
107 return dn
? BasicBlock::get(dn
) : NULL
;
111 BasicBlock::insertHead(Instruction
*inst
)
113 assert(inst
->next
== 0 && inst
->prev
== 0);
115 if (inst
->op
== OP_PHI
) {
117 insertBefore(phi
, inst
);
120 insertBefore(entry
, inst
);
130 insertBefore(entry
, inst
);
133 insertAfter(exit
, inst
); // after last phi
145 BasicBlock::insertTail(Instruction
*inst
)
147 assert(inst
->next
== 0 && inst
->prev
== 0);
149 if (inst
->op
== OP_PHI
) {
151 insertBefore(entry
, inst
);
155 insertAfter(exit
, inst
);
164 insertAfter(exit
, inst
);
175 BasicBlock::insertBefore(Instruction
*q
, Instruction
*p
)
179 assert(p
->next
== 0 && p
->prev
== 0);
182 if (p
->op
== OP_PHI
) {
190 assert(p
->op
== OP_PHI
);
205 BasicBlock::insertAfter(Instruction
*p
, Instruction
*q
)
208 assert(q
->op
!= OP_PHI
|| p
->op
== OP_PHI
);
210 assert(q
->next
== 0 && q
->prev
== 0);
214 if (p
->op
== OP_PHI
&& q
->op
!= OP_PHI
)
228 BasicBlock::remove(Instruction
*insn
)
230 assert(insn
->bb
== this);
233 insn
->prev
->next
= insn
->next
;
236 insn
->next
->prev
= insn
->prev
;
244 if (insn
->prev
&& insn
->prev
->op
!= OP_PHI
)
251 phi
= (insn
->next
&& insn
->next
->op
== OP_PHI
) ? insn
->next
: 0;
259 void BasicBlock::permuteAdjacent(Instruction
*a
, Instruction
*b
)
261 assert(a
->bb
== b
->bb
);
268 assert(a
->next
== b
);
269 assert(a
->op
!= OP_PHI
&& b
->op
!= OP_PHI
);
288 BasicBlock::splitCommon(Instruction
*insn
, BasicBlock
*bb
, bool attach
)
302 while (!cfg
.outgoing(true).end()) {
303 Graph::Edge
*e
= cfg
.outgoing(true).getEdge();
304 bb
->cfg
.attach(e
->getTarget(), e
->getType());
305 this->cfg
.detach(e
->getTarget());
308 for (; insn
; insn
= insn
->next
) {
315 this->cfg
.attach(&bb
->cfg
, Graph::Edge::TREE
);
319 BasicBlock::splitBefore(Instruction
*insn
, bool attach
)
321 BasicBlock
*bb
= new BasicBlock(func
);
322 assert(!insn
|| insn
->op
!= OP_PHI
);
324 splitCommon(insn
, bb
, attach
);
329 BasicBlock::splitAfter(Instruction
*insn
, bool attach
)
331 BasicBlock
*bb
= new BasicBlock(func
);
332 assert(!insn
|| insn
->op
!= OP_PHI
);
337 splitCommon(insn
? insn
->next
: NULL
, bb
, attach
);
342 BasicBlock::dominatedBy(BasicBlock
*that
)
344 Graph::Node
*bn
= &that
->dom
;
345 Graph::Node
*dn
= &this->dom
;
347 while (dn
&& dn
!= bn
)
354 BasicBlock::initiatesSimpleConditional() const
358 Graph::Edge::Type eR
;
360 if (cfg
.outgoingCount() != 2) // -> if and -> else/endif
364 for (Graph::EdgeIterator ei
= cfg
.outgoing(); !ei
.end(); ei
.next())
365 out
[n
++] = ei
.getNode();
366 eR
= out
[1]->outgoing().getType();
368 // IF block is out edge to the right
369 if (eR
== Graph::Edge::CROSS
|| eR
== Graph::Edge::BACK
)
372 if (out
[1]->outgoingCount() != 1) // 0 is IF { RET; }, >1 is more divergence
374 // do they reconverge immediately ?
375 if (out
[1]->outgoing().getNode() == out
[0])
377 if (out
[0]->outgoingCount() == 1)
378 if (out
[0]->outgoing().getNode() == out
[1]->outgoing().getNode())
385 Function::setEntry(BasicBlock
*bb
)
389 cfg
.insert(&bb
->cfg
);
394 Function::setExit(BasicBlock
*bb
)
403 Function::orderInstructions(ArrayList
&result
)
406 for (iter
= cfg
.iteratorCFG(); !iter
->end(); iter
->next()) {
408 BasicBlock::get(reinterpret_cast<Graph::Node
*>(iter
->get()));
410 for (Instruction
*insn
= bb
->getFirst(); insn
; insn
= insn
->next
)
411 result
.insert(insn
, insn
->serial
);
414 cfg
.putIterator(iter
);
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 (ArrayList::Iterator fi
= prog
->allFuncs
.iterator();
450 !fi
.end(); fi
.next()) {
451 Function
*fn
= reinterpret_cast<Function
*>(fi
.get());
452 if (!doRun(fn
, 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
;
490 func
->cfg
.putIterator(bbIter
);
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");
507 for (iter
= cfg
.iteratorDFS(); !iter
->end(); iter
->next()) {
508 BasicBlock
*bb
= BasicBlock::get(
509 reinterpret_cast<Graph::Node
*>(iter
->get()));
510 int idA
= bb
->getId();
511 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
512 int idB
= BasicBlock::get(ei
.getNode())->getId();
513 switch (ei
.getType()) {
514 case Graph::Edge::TREE
:
515 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
517 case Graph::Edge::FORWARD
:
518 fprintf(out
, "\t%i -> %i [color=green];\n", idA
, idB
);
520 case Graph::Edge::CROSS
:
521 fprintf(out
, "\t%i -> %i [color=red];\n", idA
, idB
);
523 case Graph::Edge::BACK
:
524 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
526 case Graph::Edge::DUMMY
:
527 fprintf(out
, "\t%i -> %i [style=dotted];\n", idA
, idB
);
535 cfg
.putIterator(iter
);
541 } // namespace nv50_ir