Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 56 additions & 44 deletions compiler/src/dmd/backend/blockopt.d
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ void block_pred(ref BlockOpt bo)
for (block* b = bo.startblock; b; b = b.Bnext) // for each block
{
//printf("b = %p, BC = BC.%s\n", b, bc_str(b.bc));
foreach (bp; ListRange(b.Bsucc))
foreach (bp; BsuccArray(b.Bsucc))
{ /* for each successor to b */
//printf("\tbs = %p\n",list_block(bp));
list_block(bp).Bpred.push(b); // original inserts at the beginning, don't think it matters
bp.Bpred.push(b); // original inserts at the beginning, don't think it matters
}
}
assert(bo.startblock.Bpred.length == 0); /* startblock has no preds */
Expand All @@ -233,13 +233,14 @@ void block_clearvisit(ref BlockOpt bo)
/********************************************
* Visit block and each of its predecessors.
*/

@trusted
void block_visit(block* b)
{
b.Bflags |= BFL.visited;
foreach (l; ListRange(b.Bsucc))
foreach (bs; BsuccArray(b.Bsucc))
//foreach (l; ListRange(b.Bsucc))
{
block* bs = list_block(l);
//block* bs = list_block(l);
assert(bs);
if ((bs.Bflags & BFL.visited) == 0) // if not visited
block_visit(bs);
Expand Down Expand Up @@ -519,8 +520,8 @@ void brcombine(ref GlobalOptimizer go, ref BlockOpt bo)
const bc = b.bc;
if (bc == BC.iftrue)
{
block* b2 = b.nthSucc(0);
block* b3 = b.nthSucc(1);
block* b2 = b.Bsuccx(0);
block* b3 = b.Bsuccx(1);

if (b2.Bpred.length > 1) // if more than one predecessor
continue;
Expand All @@ -533,7 +534,7 @@ void brcombine(ref GlobalOptimizer go, ref BlockOpt bo)

const bc2 = b2.bc;
if (bc2 == BC.goto_ &&
b3 == b2.nthSucc(0))
b3 == b2.Bsuccx(0))
{
b.bc = BC.goto_;
if (b2.Belem)
Expand Down Expand Up @@ -570,9 +571,9 @@ void brcombine(ref GlobalOptimizer go, ref BlockOpt bo)
}
else if (bc2 == BC.goto_ &&
b3.bc == BC.goto_ &&
b2.nthSucc(0) == b3.nthSucc(0))
b2.Bsuccx(0) == b3.Bsuccx(0))
{
block* bsucc = b2.nthSucc(0);
block* bsucc = b2.Bsuccx(0);
if (b2.Belem)
{
elem* e;
Expand Down Expand Up @@ -646,9 +647,10 @@ private void bropt(ref GlobalOptimizer go, ref BlockOpt bo)
{
b.bc = BC.exit;
// Exit block has no successors, so remove them
foreach (bp; ListRange(b.Bsucc))
foreach (bp; BsuccArray(b.Bsucc))
//foreach (bp; ListRange(b.Bsucc))
{
list_block(bp).Bpred.subtract(b);
bp.Bpred.subtract(b);
}
list_free(&b.Bsucc, FPNULL);
debug if (debugc) printf("CHANGE: noreturn becomes BC.exit\n");
Expand Down Expand Up @@ -680,13 +682,13 @@ private void bropt(ref GlobalOptimizer go, ref BlockOpt bo)
if (iftrue(n)) /* if elem is true */
{
// select first succ
db = b.nthSucc(1);
db = b.Bsuccx(1);
goto L1;
}
else if (iffalse(n))
{
// select second succ
db = b.nthSucc(0);
db = b.Bsuccx(0);

L1:
list_subtract(&(b.Bsucc),db);
Expand All @@ -699,11 +701,11 @@ private void bropt(ref GlobalOptimizer go, ref BlockOpt bo)
}

/* Look for both destinations being the same */
else if (b.nthSucc(0) ==
b.nthSucc(1))
else if (b.Bsuccx(0) ==
b.Bsuccx(1))
{
b.bc = BC.goto_;
db = b.nthSucc(0);
db = b.Bsuccx(0);
list_subtract(&(b.Bsucc),db);
db.Bpred.subtract(b);
debug if (debugc) printf("CHANGE: if (e) goto L1; else goto L1;\n");
Expand All @@ -727,14 +729,15 @@ private void bropt(ref GlobalOptimizer go, ref BlockOpt bo)
break;
}
}
block* db = b.nthSucc(i);
block* db = b.Bsuccx(i);

/* delete predecessors of successors (!) */
foreach (bl; ListRange(b.Bsucc))
foreach (bl; BsuccArray(b.Bsucc))
//foreach (bl; ListRange(b.Bsucc))
{
if (i--) // but not the db successor
{
bool bx = list_block(bl).Bpred.subtract(b);
bool bx = bl.Bpred.subtract(b);
assert(bx);
}
}
Expand Down Expand Up @@ -776,7 +779,7 @@ private void brrear(ref BlockOpt bo)

static if (NTEXCEPTIONS)
enum additionalAnd = "b.Btry == bt.Btry &&
bt.Btry == bt.nthSucc(0).Btry";
bt.Btry == bt.Bsuccx(0).Btry";
else
enum additionalAnd = "true";

Expand Down Expand Up @@ -814,8 +817,8 @@ private void brrear(ref BlockOpt bo)

if (b.bc == BC.iftrue || b.bc == BC.iffalse)
{
block* bif = b.nthSucc(0);
block* belse = b.nthSucc(1);
block* bif = b.Bsuccx(0);
block* belse = b.Bsuccx(1);

if (bif == b.Bnext)
{
Expand Down Expand Up @@ -848,14 +851,16 @@ void compdfo(ref BlockOpt bo, ref Barray!(block*) dfo, block* startblock)
/******************************
* Add b's successors to dfo[], then b
*/
@trusted
void walkDFO(block* b)
{
assert(b);
b.Bflags |= BFL.visited; // executed at least once

foreach (bl; ListRange(b.Bsucc)) // for each successor
foreach (bs; BsuccArray(b.Bsucc))
//foreach (bl; ListRange(b.Bsucc)) // for each successor
{
block* bs = list_block(bl);
//block* bs = list_block(bl);
assert(bs);
if ((bs.Bflags & BFL.visited) == 0) // if not visited
walkDFO(bs);
Expand Down Expand Up @@ -914,11 +919,12 @@ private void elimblks(ref GlobalOptimizer go, ref BlockOpt bo)
/* for each marked successor S to b */
/* remove b from S.Bpred. */
/* Presumably all predecessors to b are unmarked also. */
foreach (s; ListRange(b.Bsucc))
foreach (s; BsuccArray(b.Bsucc))
//foreach (s; ListRange(b.Bsucc))
{
assert(list_block(s));
if (list_block(s).Bflags & BFL.visited) /* if it is marked */
list_block(s).Bpred.subtract(b);
//assert(list_block(s));
if (s.Bflags & BFL.visited) /* if it is marked */
s.Bpred.subtract(b);
}
if (b.Balign && b.Bnext && b.Balign > b.Bnext.Balign)
b.Bnext.Balign = b.Balign;
Expand Down Expand Up @@ -1396,11 +1402,12 @@ private void bltailmerge(ref GlobalOptimizer go, ref BlockOpt bo)
/* Update the predecessor list of the successor list
of bnew, from b to bnew, and removing bn
*/
foreach (bl; ListRange(bnew.Bsucc))
foreach (bl; BsuccArray(bnew.Bsucc))
//foreach (bl; ListRange(bnew.Bsucc))
{
list_block(bl).Bpred.subtract(b);
list_block(bl).Bpred.subtract(bn);
list_block(bl).Bpred.push(bnew);
bl.Bpred.subtract(b);
bl.Bpred.subtract(bn);
bl.Bpred.push(bnew);
}

/* The predecessors to bnew are b and bn */
Expand Down Expand Up @@ -1441,7 +1448,7 @@ private void bltailmerge(ref GlobalOptimizer go, ref BlockOpt bo)
}

/**********************************
* Rearrange blocks to minimize jmp's.
* Rearrange blocks to minimize jmps.
*/

@trusted
Expand Down Expand Up @@ -1476,18 +1483,20 @@ private void brmin(ref GlobalOptimizer go, ref BlockOpt bo)
if (!isExceptionHandler(b.Bnext))
{
// Skip the block if one of the successors is already at Bnext.
foreach (bl; ListRange(b.Bsucc))
foreach (bl; BsuccArray(b.Bsucc))
//foreach (bl; ListRange(b.Bsucc))
{
if (list_block(bl) == b.Bnext)
if (bl == b.Bnext)
continue Lbb;
}
}

// Look for a successor of b for which everyone must jmp to.
Lsucc:
foreach (bl; ListRange(b.Bsucc))
foreach (bs; BsuccArray(b.Bsucc))
//foreach (bl; ListRange(b.Bsucc))
{
block* bs = list_block(bl);
//block* bs = list_block(bl);

// BC.exit should have been optimized by blexit().
// Also ignore exception handlers.
Expand Down Expand Up @@ -1608,9 +1617,10 @@ private void block_check()
break;
}

foreach (bl; ListRange(b.Bsucc))
foreach (bs; BsuccArray(b.Bsucc))
//foreach (bl; ListRange(b.Bsucc))
{
block* bs = list_block(bl);
//block* bs = list_block(bl);

foreach (bls; bs.Bpred[])
{
Expand Down Expand Up @@ -2093,9 +2103,10 @@ private void blassertsplit(ref GlobalOptimizer go, ref BlockOpt bo)
*/
b2.Bsucc = b.Bsucc;
b.Bsucc = null;
foreach (b2sl; ListRange(b2.Bsucc))
foreach (b2s; BsuccArray(b2.Bsucc))
//foreach (b2sl; ListRange(b2.Bsucc))
{
block* b2s = list_block(b2sl);
//block* b2s = list_block(b2sl);
foreach (ref b2spl; b2s.Bpred[])
{
if (b2spl == b)
Expand Down Expand Up @@ -2159,9 +2170,10 @@ private void blexit(ref GlobalOptimizer go, ref BlockOpt bo)

b.bc = BC.exit;

foreach (bsl; ListRange(b.Bsucc))
foreach (bs; BsuccArray(b.Bsucc))
//foreach (bsl; ListRange(b.Bsucc))
{
block* bs = list_block(bsl);
//block* bs = list_block(bsl);
bs.Bpred.subtract(b);
}
list_free(&b.Bsucc, FPNULL);
Expand Down
27 changes: 24 additions & 3 deletions compiler/src/dmd/backend/cc.d
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,37 @@ nothrow:

int numSucc() { return list_nitems(this.Bsucc); }

@trusted
block* nthSucc(int n) { return cast(block*)list_ptr(list_nth(Bsucc, n)); }

@trusted
void setNthSucc(int n, block* b) { list_nth(Bsucc, n).ptr = b; }
}

@trusted
block* Bsuccx(block* b, int n) { return cast(block*)list_ptr(list_nth(b.Bsucc, n)); }

@trusted
int lengthx(LIST* bl) { return list_nitems((cast(block*)list_ptr(bl)).Bsucc); }

@trusted
inout(block)* list_block(inout list_t lst) { return cast(inout(block)*)list_ptr(lst); }

struct BsuccArray
{
nothrow @nogc @trusted:

this(list_t li)
{
this.li = li;
}

block* front() return { return list_block(li); }
void popFront() { li = li.next; }
bool empty() const { return !li; }

private:
list_t li;
}


/** Basic block control flow operators. **/

enum BC : ubyte
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/backend/cgcs.d
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ void comsubs2(block* startblock, ref CGCS cgcs, ref GlobalOptimizer go, ref Bloc
auto blc = bl;
while (bln && bln.Bpred.length == 1 &&
((blc.bc == BC.iftrue &&
blc.nthSucc(1) == bln) ||
(blc.bc == BC.goto_ && blc.nthSucc(0) == bln)
blc.Bsuccx(1) == bln) ||
(blc.bc == BC.goto_ && blc.Bsuccx(0) == bln)
) &&
bln.bc != BC.asm_ // no CSE's extending across ASM blocks
)
Expand Down
4 changes: 1 addition & 3 deletions compiler/src/dmd/backend/debugprint.d
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,8 @@ void WRblockarray(block*[] bl)
@trusted
void WRblocklist(list_t bl)
{
foreach (bl2; ListRange(bl))
foreach (b; BsuccArray(bl))
{
block* b = list_block(bl2);

if (b && b.Bweight)
printf("B%d (%p) ",b.Bdfoidx,b);
else
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/backend/dwarfeh.d
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static if (0)
DwEhTableEntry* d = deh.push();
d.start = cast(uint)b.Boffset;

block* bf = b.nthSucc(1);
block* bf = b.Bsuccx(1);
if (bf.bc == BC.jcatch)
{
d.lpad = cast(uint)bf.Boffset;
Expand All @@ -138,7 +138,7 @@ static if (0)
d.action = offset + 1;
}
else
d.lpad = cast(uint)bf.nthSucc(0).Boffset;
d.lpad = cast(uint)bf.Bsuccx(0).Boffset;
d.prev = index;
index = i;
bprev = b.Btry;
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dmd/backend/eh.d
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void except_fillInEHTable(Symbol* s)
}
i = b.Bscope_index + 1;

int nsucc = b.numSucc();
int nsucc = b.numSucc;

if (config.ehmethod == EHmethod.EH_DM)
{
Expand Down Expand Up @@ -216,10 +216,10 @@ void except_fillInEHTable(Symbol* s)
{
assert(nsucc == 2);
dtb.dword(0); // no catch offset
block* bhandler = b.nthSucc(1);
block* bhandler = b.Bsuccx(1);
assert(bhandler.bc == BC._finally);
// To successor of BC._finally block
bhandler = bhandler.nthSucc(0);
bhandler = bhandler.Bsuccx(0);
// finally handler address
if (config.ehmethod == EHmethod.EH_DM)
{
Expand Down Expand Up @@ -341,7 +341,7 @@ void except_fillInEHTable(Symbol* s)

for (int j = 1; j < nsucc; ++j)
{
block* bcatch = b.nthSucc(j);
block* bcatch = b.Bsuccx(j);

dtb.xoff(bcatch.Bcatchtype,0,TYnptr);

Expand Down
Loading
Loading