Skip to content

Commit 88d3794

Browse files
committed
remove a bad assert in cfg.cc and improve cfg comments
1 parent 2a3fe00 commit 88d3794

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

BE/Base/cfg.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
#include <set>
66

7-
#include "IR/opcode_gen.h"
87
#include "BE/Base/sanity.h"
98
#include "BE/Base/serialize.h"
9+
#include "IR/opcode_gen.h"
1010
#include "Util/parse.h"
1111

1212
namespace cwerg::base {
@@ -302,7 +302,8 @@ void FunRemoveEmptyBbls(Fun fun) {
302302
bbls_keep.push_back(bbl);
303303
continue;
304304
}
305-
// ASSERT(FunBblList::Head(fun) != bbl, "cannot remove entry bbl in fun " << Name(fun));
305+
// ASSERT(FunBblList::Head(fun) != bbl, "cannot remove entry bbl in fun " <<
306+
// Name(fun));
306307
ASSERT(out_edg == BblSuccEdgList::Tail(bbl) &&
307308
!BblSuccEdgList::IsSentinel(out_edg),
308309
"must have one out edge:\n"
@@ -382,14 +383,15 @@ void FunAddUnconditionalBranches(Fun fun) {
382383
// If it has a fall-through there is at least one succ edge
383384
ASSERT(!BblSuccEdgList::IsSentinel(edg1), "");
384385
ASSERT(!BblSuccEdgList::IsSentinel(edg2), "");
386+
// Note, next might be a sentinel
385387
const Bbl next = FunBblList::Next(bbl);
386-
ASSERT(!FunBblList::IsSentinel(next), "");
387-
// Single Edge case:
388388
if (edg1 == edg2) {
389+
// Single Edge case:
389390
if (next != EdgSuccBbl(edg1)) {
390391
BblInsAdd(bbl, InsNewBra(EdgSuccBbl(edg1)));
391392
}
392393
} else {
394+
// Two Edges case: if one edge goes to next, we can just flip the condition and target of the cond bra
393395
ASSERT(InsOpcode(last).kind == OPC_KIND::COND_BRA, "");
394396
ASSERT(BblSuccEdgList::Next(edg1) == edg2, "");
395397
const Bbl target = Bbl(InsOperand(last, 2));

BE/Base/cfg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,14 @@ def FunAddUnconditionalBranches(fun: ir.Fun):
263263
bbls.append(bbl)
264264
if bbl.inss and not bbl.inss[-1].opcode.has_fallthrough():
265265
continue
266+
# easy case - only one successor, we just need to add a branch if it's not already the fallthrough
266267
if len(bbl.edge_out) == 1:
267268
assert len(fun.bbls) > n
268269
succ = bbl.edge_out[0]
269270
if n + 1 == len(fun.bbls) or fun.bbls[n + 1] != succ:
270271
bbl.inss.append(ir.Ins(o.BRA, [succ], False))
271272
continue
272-
273+
# more complex case - we have two successors, we need to check if one of them is the fallthrough and if not we need to add a branch to the fallthrough
273274
assert len(bbl.edge_out) == 2
274275
cond_bra = bbl.inss[-1]
275276
assert cond_bra.opcode.kind is o.OPC_KIND.COND_BRA, (

0 commit comments

Comments
 (0)