Skip to content

Commit

Permalink
backend: Pass global variable go as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Korpel committed Sep 27, 2024
1 parent 0402319 commit 524a49e
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 309 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dmd/backend/backconfig.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import dmd.backend.cdef;
import dmd.backend.cc;
import dmd.backend.code;
import dmd.backend.global;
import dmd.backend.goh : GlobalOptimizer;
import dmd.backend.ty;
import dmd.backend.type;

Expand Down Expand Up @@ -82,7 +83,8 @@ extern (C) void out_config_init(
string _version,
exefmt_t exefmt,
bool generatedMain, // a main entrypoint is generated
bool dataimports)
bool dataimports,
ref GlobalOptimizer go)
{
//printf("out_config_init()\n");

Expand Down Expand Up @@ -314,7 +316,7 @@ static if (0)
configv.verbose = verbose;

if (optimize)
go_flag(cast(char*)"-o".ptr);
go_flag(go, cast(char*)"-o".ptr);

if (symdebug)
{
Expand Down
56 changes: 28 additions & 28 deletions compiler/src/dmd/backend/blockopt.d
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ void block_visit(block *b)
* Compute number of parents (Bcount) of each basic block.
*/
@trusted
void block_compbcount()
void block_compbcount(ref GlobalOptimizer go)
{
block_clearvisit();
block_visit(startblock); // visit all reachable blocks
elimblks(); // eliminate unvisited blocks
elimblks(go); // eliminate unvisited blocks
}

/*******************************
Expand Down Expand Up @@ -541,11 +541,11 @@ void block_endfunc(int flag)
*/

@trusted
void blockopt(int iter)
void blockopt(ref GlobalOptimizer go, int iter)
{
if (OPTIMIZER)
{
blassertsplit(); // only need this once
blassertsplit(go); // only need this once

int iterationLimit = 200;
if (iterationLimit < dfo.length)
Expand All @@ -555,24 +555,24 @@ void blockopt(int iter)
{
//printf("changes = %d, count = %d, dfo.length = %d\n",go.changes,count,dfo.length);
go.changes = 0;
bropt(); // branch optimization
bropt(go); // branch optimization
brrear(); // branch rearrangement
blident(); // combine identical blocks
blreturn(); // split out return blocks
bltailmerge(); // do tail merging
brtailrecursion(); // do tail recursion
brcombine(); // convert graph to expressions
blexit();
blident(go); // combine identical blocks
blreturn(go); // split out return blocks
bltailmerge(go); // do tail merging
brtailrecursion(go); // do tail recursion
brcombine(go); // convert graph to expressions
blexit(go);
if (iter >= 2)
brmin(); // minimize branching
brmin(go); // minimize branching

Check warning on line 567 in compiler/src/dmd/backend/blockopt.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/blockopt.d#L567

Added line #L567 was not covered by tests

// Switched to one block per Statement, do not undo it
enum merge = false;

do
{
compdfo(dfo, startblock); // compute depth first order (DFO)
elimblks(); /* remove blocks not in DFO */
elimblks(go); /* remove blocks not in DFO */
assert(count < iterationLimit);
count++;
} while (merge && mergeblks()); // merge together blocks
Expand Down Expand Up @@ -603,7 +603,7 @@ void blockopt(int iter)
{
b.Belem = doptelem(b.Belem,bc_goal[b.BC] | Goal.struct_);
if (b.Belem)
b.Belem = el_convert(b.Belem);
b.Belem = el_convert(go, b.Belem);
}

debug if (debugb)
Expand All @@ -621,9 +621,9 @@ void blockopt(int iter)
startblock.Belem = el_combine(e, startblock.Belem);
}

bropt(); /* branch optimization */
bropt(go); /* branch optimization */
brrear(); /* branch rearrangement */
comsubs(); /* eliminate common subexpressions */
comsubs(go); /* eliminate common subexpressions */

debug if (debugb)
{
Expand All @@ -639,7 +639,7 @@ void blockopt(int iter)
*/

@trusted
void brcombine()
void brcombine(ref GlobalOptimizer go)
{
debug if (debugc) printf("brcombine()\n");
//WRfunc("brcombine()", funcsym_p, startblock);
Expand Down Expand Up @@ -843,7 +843,7 @@ void brcombine()
*/

@trusted
private void bropt()
private void bropt(ref GlobalOptimizer go)
{
debug if (debugc) printf("bropt()\n");
assert(!PARSER);
Expand Down Expand Up @@ -1115,7 +1115,7 @@ void compdfo(ref Barray!(block*) dfo, block* startblock)
*/

@trusted
private void elimblks()
private void elimblks(ref GlobalOptimizer go)
{
debug if (debugc) printf("elimblks()\n");
block *bf = null;
Expand Down Expand Up @@ -1254,7 +1254,7 @@ private int mergeblks()
*/

@trusted
private void blident()
private void blident(ref GlobalOptimizer go)
{
debug if (debugc) printf("blident()\n");
assert(startblock);
Expand Down Expand Up @@ -1385,7 +1385,7 @@ private void blident()
*/

@trusted
private void blreturn()
private void blreturn(ref GlobalOptimizer go)
{
if (!(go.mfoptim & MFtime)) /* if optimized for space */
{
Expand Down Expand Up @@ -1444,7 +1444,7 @@ private void blreturn()
}
}

blident(); /* combine return blocks */
blident(go); /* combine return blocks */

Check warning on line 1447 in compiler/src/dmd/backend/blockopt.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/blockopt.d#L1447

Added line #L1447 was not covered by tests
}
}

Expand Down Expand Up @@ -1529,7 +1529,7 @@ private elem * bl_delist(list_t el)
*/

@trusted
private void bltailmerge()
private void bltailmerge(ref GlobalOptimizer go)
{
debug if (debugc) printf("bltailmerge()\n");
assert(!PARSER && OPTIMIZER);
Expand Down Expand Up @@ -1663,7 +1663,7 @@ private void bltailmerge()
*/

@trusted
private void brmin()
private void brmin(ref GlobalOptimizer go)
{
debug if (debugc) printf("brmin()\n");
debug assert(startblock);
Expand Down Expand Up @@ -1764,7 +1764,7 @@ private void block_check()
*/

@trusted
private void brtailrecursion()
private void brtailrecursion(ref GlobalOptimizer go)
{
if (funcsym_p.Sfunc.Fflags3 & Fnotailrecursion)
return;
Expand Down Expand Up @@ -1964,7 +1964,7 @@ private elem * assignparams(elem **pe,int *psi,elem **pe2)
*/

@trusted
private void emptyloops()
private void emptyloops(ref GlobalOptimizer go)
{
debug if (debugc) printf("emptyloops()\n");
for (block *b = startblock; b; b = b.Bnext)
Expand Down Expand Up @@ -2119,7 +2119,7 @@ private int el_anyframeptr(elem *e)
*/

@trusted
private void blassertsplit()
private void blassertsplit(ref GlobalOptimizer go)
{
debug if (debugc) printf("blassertsplit()\n");
Barray!(elem*) elems;
Expand Down Expand Up @@ -2260,7 +2260,7 @@ private void blassertsplit()
* Detect exit blocks and move them to the end.
*/
@trusted
private void blexit()
private void blexit(ref GlobalOptimizer go)
{
debug if (debugc)
printf("blexit()\n");
Expand Down
9 changes: 5 additions & 4 deletions compiler/src/dmd/backend/cgcs.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import dmd.backend.cc;
import dmd.backend.cdef;
import dmd.backend.code;
import dmd.backend.el;
import dmd.backend.goh : GlobalOptimizer;
import dmd.backend.global;
import dmd.backend.oper;
import dmd.backend.ty;
Expand All @@ -41,11 +42,11 @@ nothrow:
*/

@trusted
public void comsubs()
public void comsubs(ref GlobalOptimizer go)
{
debug if (debugx) printf("comsubs(%p)\n",startblock);

comsubs2(startblock, cgcsdata);
comsubs2(startblock, cgcsdata, go);

debug if (debugx)
printf("done with comsubs()\n");
Expand Down Expand Up @@ -73,10 +74,10 @@ alias hash_t = uint; // for hash values
* String together as many blocks as we can.
*/
@trusted
void comsubs2(block* startblock, ref CGCS cgcs)
void comsubs2(block* startblock, ref CGCS cgcs, ref GlobalOptimizer go)
{
// No longer just compute Bcount - eliminate unreachable blocks too
block_compbcount(); // eliminate unreachable blocks
block_compbcount(go); // eliminate unreachable blocks

cgcs.start();

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/backend/debugprint.d
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ void WRblocklist(list_t bl)
}

@trusted
void WRdefnod()
void WRdefnod(ref GlobalOptimizer go)
{ int i;

for (i = 0; i < go.defnod.length; i++)
Expand Down
14 changes: 8 additions & 6 deletions compiler/src/dmd/backend/dout.d
Original file line number Diff line number Diff line change
Expand Up @@ -851,13 +851,14 @@ private void out_regcand_walk(elem *e, ref bool addressOfParam)
@trusted
void writefunc(Symbol *sfunc)
{
import dmd.backend.var : go;
cstate.CSpsymtab = &globsym;
writefunc2(sfunc);
writefunc2(sfunc, go);
cstate.CSpsymtab = null;
}

@trusted
private void writefunc2(Symbol *sfunc)
private void writefunc2(Symbol *sfunc, ref GlobalOptimizer go)
{
func_t *f = sfunc.Sfunc;

Expand Down Expand Up @@ -989,7 +990,7 @@ private void writefunc2(Symbol *sfunc)
}

block_pred(); // compute predecessors to blocks
block_compbcount(); // eliminate unreachable blocks
block_compbcount(go); // eliminate unreachable blocks

debug { } else
{
Expand All @@ -1000,14 +1001,15 @@ private void writefunc2(Symbol *sfunc)
}

if (go.mfoptim)
{ OPTIMIZER = 1;
optfunc(); /* optimize function */
{
OPTIMIZER = 1;
optfunc(go); /* optimize function */
OPTIMIZER = 0;
}
else
{
//printf("blockopt()\n");
blockopt(0); /* optimize */
blockopt(go, 0); /* optimize */
}

assert(funcsym_p == sfunc);
Expand Down
18 changes: 9 additions & 9 deletions compiler/src/dmd/backend/elem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ int el_countCommas(const(elem)* e)
* Needed iff floating point code can't load immediate constants.
*/
@trusted
elem *el_convfloat(elem *e)
elem* el_convfloat(ref GlobalOptimizer go, elem* e)
{
ubyte[32] buffer = void;

Expand Down Expand Up @@ -1313,7 +1313,7 @@ elem *el_convfloat(elem *e)
*/

@trusted
elem *el_convxmm(elem *e)
elem* el_convxmm(ref GlobalOptimizer go, elem* e)
{
ubyte[Vconst.sizeof] buffer = void;

Expand Down Expand Up @@ -1484,7 +1484,7 @@ void shrinkLongDoubleConstantIfPossible(elem *e)
* Run through a tree converting it to CODGEN.
*/
@trusted
elem *el_convert(elem *e)
elem* el_convert(ref GlobalOptimizer go, elem* e)
{
//printf("el_convert(%p)\n", e);
elem_debug(e);
Expand All @@ -1496,9 +1496,9 @@ elem *el_convert(elem *e)

case OPconst:
if (tyvector(e.Ety))
e = el_convxmm(e);
e = el_convxmm(go, e);
else if (tyfloating(e.Ety) && config.inline8087)
e = el_convfloat(e);
e = el_convfloat(go, e);
break;

case OPstring:
Expand All @@ -1517,7 +1517,7 @@ elem *el_convert(elem *e)
if (tyreal(e.Ety) && // don't bother with imaginary or complex
e.E2.Eoper == OPconst && el_toldoubled(e.E2) == 2.0L)
{
e.E1 = el_convert(e.E1);
e.E1 = el_convert(go, e.E1);
/* Don't call el_convert(e.E2), we want it to stay as a constant
* which will be detected by code gen.
*/
Expand All @@ -1538,12 +1538,12 @@ elem *el_convert(elem *e)
default:
if (OTbinary(op))
{
e.E1 = el_convert(e.E1);
e.E2 = el_convert(e.E2);
e.E1 = el_convert(go, e.E1);
e.E2 = el_convert(go, e.E2);
}
else if (OTunary(op))
{
e.E1 = el_convert(e.E1);
e.E1 = el_convert(go, e.E1);
}
break;
}
Expand Down
Loading

0 comments on commit 524a49e

Please sign in to comment.