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::idom() const
86 Graph::Node
*dn
= dom
.parent();
87 return dn
? BasicBlock::get(dn
) : NULL
;
91 BasicBlock::insertHead(Instruction
*inst
)
93 assert(inst
->next
== 0 && inst
->prev
== 0);
95 if (inst
->op
== OP_PHI
) {
97 insertBefore(phi
, inst
);
100 insertBefore(entry
, inst
);
110 insertBefore(entry
, inst
);
113 insertAfter(exit
, inst
); // after last phi
125 BasicBlock::insertTail(Instruction
*inst
)
127 assert(inst
->next
== 0 && inst
->prev
== 0);
129 if (inst
->op
== OP_PHI
) {
131 insertBefore(entry
, inst
);
135 insertAfter(exit
, inst
);
144 insertAfter(exit
, inst
);
155 BasicBlock::insertBefore(Instruction
*q
, Instruction
*p
)
159 assert(p
->next
== 0 && p
->prev
== 0);
162 if (p
->op
== OP_PHI
) {
170 assert(p
->op
== OP_PHI
);
185 BasicBlock::insertAfter(Instruction
*p
, Instruction
*q
)
188 assert(q
->op
!= OP_PHI
|| p
->op
== OP_PHI
);
190 assert(q
->next
== 0 && q
->prev
== 0);
194 if (p
->op
== OP_PHI
&& q
->op
!= OP_PHI
)
208 BasicBlock::remove(Instruction
*insn
)
210 assert(insn
->bb
== this);
213 insn
->prev
->next
= insn
->next
;
216 insn
->next
->prev
= insn
->prev
;
224 if (insn
->prev
&& insn
->prev
->op
!= OP_PHI
)
231 phi
= (insn
->next
&& insn
->next
->op
== OP_PHI
) ? insn
->next
: 0;
239 void BasicBlock::permuteAdjacent(Instruction
*a
, Instruction
*b
)
241 assert(a
->bb
== b
->bb
);
248 assert(a
->next
== b
);
249 assert(a
->op
!= OP_PHI
&& b
->op
!= OP_PHI
);
268 BasicBlock::splitCommon(Instruction
*insn
, BasicBlock
*bb
, bool attach
)
282 while (!cfg
.outgoing(true).end()) {
283 Graph::Edge
*e
= cfg
.outgoing(true).getEdge();
284 bb
->cfg
.attach(e
->getTarget(), e
->getType());
285 this->cfg
.detach(e
->getTarget());
288 for (; insn
; insn
= insn
->next
) {
295 this->cfg
.attach(&bb
->cfg
, Graph::Edge::TREE
);
299 BasicBlock::splitBefore(Instruction
*insn
, bool attach
)
301 BasicBlock
*bb
= new BasicBlock(func
);
302 assert(!insn
|| insn
->op
!= OP_PHI
);
304 splitCommon(insn
, bb
, attach
);
309 BasicBlock::splitAfter(Instruction
*insn
, bool attach
)
311 BasicBlock
*bb
= new BasicBlock(func
);
312 assert(!insn
|| insn
->op
!= OP_PHI
);
317 splitCommon(insn
? insn
->next
: NULL
, bb
, attach
);
322 BasicBlock::dominatedBy(BasicBlock
*that
)
324 Graph::Node
*bn
= &that
->dom
;
325 Graph::Node
*dn
= &this->dom
;
327 while (dn
&& dn
!= bn
)
334 BasicBlock::initiatesSimpleConditional() const
338 Graph::Edge::Type eR
;
340 if (cfg
.outgoingCount() != 2) // -> if and -> else/endif
344 for (Graph::EdgeIterator ei
= cfg
.outgoing(); !ei
.end(); ei
.next())
345 out
[n
++] = ei
.getNode();
346 eR
= out
[1]->outgoing().getType();
348 // IF block is out edge to the right
349 if (eR
== Graph::Edge::CROSS
|| eR
== Graph::Edge::BACK
)
352 if (out
[1]->outgoingCount() != 1) // 0 is IF { RET; }, >1 is more divergence
354 // do they reconverge immediately ?
355 if (out
[1]->outgoing().getNode() == out
[0])
357 if (out
[0]->outgoingCount() == 1)
358 if (out
[0]->outgoing().getNode() == out
[1]->outgoing().getNode())
365 Function::setEntry(BasicBlock
*bb
)
369 cfg
.insert(&bb
->cfg
);
374 Function::setExit(BasicBlock
*bb
)
383 Function::orderInstructions(ArrayList
&result
)
386 for (iter
= cfg
.iteratorCFG(); !iter
->end(); iter
->next()) {
388 BasicBlock::get(reinterpret_cast<Graph::Node
*>(iter
->get()));
390 for (Instruction
*insn
= bb
->getFirst(); insn
; insn
= insn
->next
)
391 result
.insert(insn
, insn
->serial
);
394 cfg
.putIterator(iter
);
395 return result
.getSize();
399 Pass::run(Program
*prog
, bool ordered
, bool skipPhi
)
403 return doRun(prog
, ordered
, skipPhi
);
407 Pass::doRun(Program
*prog
, bool ordered
, bool skipPhi
)
409 for (ArrayList::Iterator fi
= prog
->allFuncs
.iterator();
410 !fi
.end(); fi
.next()) {
411 Function
*fn
= reinterpret_cast<Function
*>(fi
.get());
412 if (!doRun(fn
, ordered
, skipPhi
))
419 Pass::run(Function
*func
, bool ordered
, bool skipPhi
)
421 prog
= func
->getProgram();
423 return doRun(func
, ordered
, skipPhi
);
427 Pass::doRun(Function
*func
, bool ordered
, bool skipPhi
)
431 Instruction
*insn
, *next
;
437 bbIter
= ordered
? func
->cfg
.iteratorCFG() : func
->cfg
.iteratorDFS();
439 for (; !bbIter
->end(); bbIter
->next()) {
440 bb
= BasicBlock::get(reinterpret_cast<Graph::Node
*>(bbIter
->get()));
443 for (insn
= skipPhi
? bb
->getEntry() : bb
->getFirst(); insn
!= NULL
;
450 func
->cfg
.putIterator(bbIter
);
455 Function::printCFGraph(const char *filePath
)
457 FILE *out
= fopen(filePath
, "a");
459 ERROR("failed to open file: %s\n", filePath
);
462 INFO("printing control flow graph to: %s\n", filePath
);
464 fprintf(out
, "digraph G {\n");
467 for (iter
= cfg
.iteratorDFS(); !iter
->end(); iter
->next()) {
468 BasicBlock
*bb
= BasicBlock::get(
469 reinterpret_cast<Graph::Node
*>(iter
->get()));
470 int idA
= bb
->getId();
471 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
472 int idB
= BasicBlock::get(ei
.getNode())->getId();
473 switch (ei
.getType()) {
474 case Graph::Edge::TREE
:
475 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
477 case Graph::Edge::FORWARD
:
478 fprintf(out
, "\t%i -> %i [color=green];\n", idA
, idB
);
480 case Graph::Edge::CROSS
:
481 fprintf(out
, "\t%i -> %i [color=red];\n", idA
, idB
);
483 case Graph::Edge::BACK
:
484 fprintf(out
, "\t%i -> %i;\n", idA
, idB
);
486 case Graph::Edge::DUMMY
:
487 fprintf(out
, "\t%i -> %i [style=dotted];\n", idA
, idB
);
495 cfg
.putIterator(iter
);
501 } // namespace nv50_ir