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
, phi
);
104 insertBefore(entry
, inst
);
107 insertAfter(phi
, inst
);
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
;
215 entry
= insn
->next
? insn
->next
: insn
->prev
;
218 phi
= (insn
->next
&& insn
->next
->op
== OP_PHI
) ? insn
->next
: 0;
226 void BasicBlock::permuteAdjacent(Instruction
*a
, Instruction
*b
)
228 assert(a
->bb
== b
->bb
);
235 assert(a
->next
== b
);
236 assert(a
->op
!= OP_PHI
&& b
->op
!= OP_PHI
);
255 BasicBlock::dominatedBy(BasicBlock
*that
)
257 Graph::Node
*bn
= &that
->dom
;
258 Graph::Node
*dn
= &this->dom
;
260 while (dn
&& dn
!= bn
)
267 BasicBlock::initiatesSimpleConditional() const
271 Graph::Edge::Type eR
;
273 if (cfg
.outgoingCount() != 2) // -> if and -> else/endif
277 for (Graph::EdgeIterator ei
= cfg
.outgoing(); !ei
.end(); ei
.next())
278 out
[n
++] = ei
.getNode();
279 eR
= out
[1]->outgoing().getType();
281 // IF block is out edge to the right
282 if (eR
== Graph::Edge::CROSS
|| eR
== Graph::Edge::BACK
)
285 if (out
[1]->outgoingCount() != 1) // 0 is IF { RET; }, >1 is more divergence
287 // do they reconverge immediately ?
288 if (out
[1]->outgoing().getNode() == out
[0])
290 if (out
[0]->outgoingCount() == 1)
291 if (out
[0]->outgoing().getNode() == out
[1]->outgoing().getNode())
298 Function::setEntry(BasicBlock
*bb
)
302 cfg
.insert(&bb
->cfg
);
307 Function::setExit(BasicBlock
*bb
)
316 Function::orderInstructions(ArrayList
&result
)
319 for (iter
= cfg
.iteratorCFG(); !iter
->end(); iter
->next())
320 for (Instruction
*insn
= BasicBlock::get(*iter
)->getFirst();
321 insn
; insn
= insn
->next
)
322 result
.insert(insn
, insn
->serial
);
323 cfg
.putIterator(iter
);
324 return result
.getSize();
328 Pass::run(Program
*prog
, bool ordered
, bool skipPhi
)
332 return doRun(prog
, ordered
, skipPhi
);
336 Pass::doRun(Program
*prog
, bool ordered
, bool skipPhi
)
338 for (ArrayList::Iterator fi
= prog
->allFuncs
.iterator();
339 !fi
.end(); fi
.next()) {
340 Function
*fn
= reinterpret_cast<Function
*>(fi
.get());
341 if (!doRun(fn
, ordered
, skipPhi
))
348 Pass::run(Function
*func
, bool ordered
, bool skipPhi
)
350 prog
= func
->getProgram();
352 return doRun(func
, ordered
, skipPhi
);
356 Pass::doRun(Function
*func
, bool ordered
, bool skipPhi
)
360 Instruction
*insn
, *next
;
366 bbIter
= ordered
? func
->cfg
.iteratorCFG() : func
->cfg
.iteratorDFS();
368 for (; !bbIter
->end(); bbIter
->next()) {
369 bb
= BasicBlock::get(reinterpret_cast<Graph::Node
*>(bbIter
->get()));
372 for (insn
= skipPhi
? bb
->getEntry() : bb
->getFirst(); insn
!= NULL
;
379 func
->cfg
.putIterator(bbIter
);
384 Function::printCFGraph(const char *filePath
)
386 FILE *out
= fopen(filePath
, "a");
388 ERROR("failed to open file: %s\n", filePath
);
391 INFO("printing control flow graph to: %s\n", filePath
);
393 fprintf(out
, "digraph G {\n");
396 for (iter
= cfg
.iteratorDFS(); !iter
->end(); iter
->next()) {
397 BasicBlock
*bb
= BasicBlock::get(
398 reinterpret_cast<Graph::Node
*>(iter
->get()));
399 int idA
= bb
->getId();
400 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
401 int idB
= BasicBlock::get(ei
.getNode())->getId();
402 switch (ei
.getType()) {
403 case Graph::Edge::TREE
:
404 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
406 case Graph::Edge::FORWARD
:
407 fprintf(out
, "\t%i -> %i [color=green];\n", idA
, idB
);
409 case Graph::Edge::CROSS
:
410 fprintf(out
, "\t%i -> %i [color=red];\n", idA
, idB
);
412 case Graph::Edge::BACK
:
413 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
415 case Graph::Edge::DUMMY
:
416 fprintf(out
, "\t%i -> %i [style=dotted];\n", idA
, idB
);
424 cfg
.putIterator(iter
);
430 } // namespace nv50_ir