Skip to content

Commit 11ca950

Browse files
committed
minor fixes to registerizeHarder, and progress on conversion to native optimizer
1 parent 5c88830 commit 11ca950

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

tools/js-optimizer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3654,9 +3654,9 @@ function registerizeHarder(ast) {
36543654
junctionVariables[name].conf[otherName] = 1;
36553655
}
36563656
for (var b in junc.outblocks) {
3657-
// It conflits with any output vars of successor blocks,
3657+
// It conflicts with any output vars of successor blocks,
36583658
// if they're assigned before it goes dead in that block.
3659-
block = blocks[b];
3659+
var block = blocks[b];
36603660
var jSucc = junctions[block.exit];
36613661
for (var otherName in jSucc.live) {
36623662
if (junc.live[otherName]) continue;

tools/optimizer/optimizer.cpp

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,11 +2711,11 @@ void registerizeHarder(Ref ast) {
27112711
// Traverse each node type according to its particular control-flow semantics.
27122712
// TODO: switchify this
27132713
if (type == DEFUN) {
2714-
var jEntry = markJunction();
2714+
int jEntry = markJunction();
27152715
assert(jEntry == ENTRY_JUNCTION);
27162716
int jExit = addJunction();
27172717
assert(jExit == EXIT_JUNCTION);
2718-
for (var i = 0; i < node[3].length; i++) {
2718+
for (int i = 0; i < node[3].length; i++) {
27192719
buildFlowGraph(node[3][i]);
27202720
}
27212721
joinJunction(jExit);
@@ -2780,7 +2780,7 @@ void registerizeHarder(Ref ast) {
27802780
joinJunction(jLoop);
27812781
setJunction(jExit);
27822782
} else {
2783-
var jCond = markJunction();
2783+
int jCond = markJunction();
27842784
int jLoop = addJunction();
27852785
int jExit = addJunction();
27862786
isInExpr++;
@@ -2996,7 +2996,7 @@ void registerizeHarder(Ref ast) {
29962996
}
29972997
// Does it assign a specific label value at exit?
29982998
if (block.kill.has(LABEL)) {
2999-
var finalNode = block->nodes.back();
2999+
Ref finalNode = block->nodes.back();
30003000
if (finalNode[0] == ASSIGN && finalNode[2][1] == LABEL) {
30013001
// If labels are computed dynamically then all bets are off.
30023002
// This can happen due to indirect branching in llvm output.
@@ -3023,7 +3023,7 @@ void registerizeHarder(Ref ast) {
30233023
}
30243024
}
30253025
for (auto labelVal : labelledBlocks) {
3026-
var block = labelVal.second;
3026+
Block* block = labelVal.second;
30273027
// Disconnect it from the graph, and create a
30283028
// new junction for jumps targetting this label.
30293029
junctions[block.entry].outblocks.erase(block.id);
@@ -3245,40 +3245,44 @@ void registerizeHarder(Ref ast) {
32453245
// as pairs of variables that we'd like to have share the same register
32463246
// (the "links").
32473247
3248-
var junctionVariables = {};
3248+
struct JuncVar {
3249+
StringSet conf, link, excl;
3250+
IString reg;
3251+
};
3252+
std::unordered_map<IString, JuncVar> junctionVariables;
32493253
3250-
function initializeJunctionVariable(name) {
3251-
junctionVariables[name] = { conf: {}, link: {}, excl: {}, reg: null };
3252-
}
3254+
auto initializeJunctionVariable = [&](IString name) {
3255+
junctionVariables[name] = JuncVar(); // XXX
3256+
};
32533257
3254-
for (var i = 0; i < junctions.length; i++) {
3255-
var junc = junctions[i];
3256-
for (var name in junc.live) {
3257-
if (!junctionVariables[name]) initializeJunctionVariable(name);
3258+
for (int i = 0; i < junctions.size(); i++) {
3259+
Junction& junc = junctions[i];
3260+
for (auto name : junc.live) {
3261+
if (junctionVariables.count(name) == 0) initializeJunctionVariable(name);
32583262
// It conflicts with all other names live at this junction.
3259-
for (var otherName in junc.live) {
3263+
for (auto otherName : junc.live) {
32603264
if (otherName == name) continue;
3261-
junctionVariables[name].conf[otherName] = 1;
3265+
junctionVariables[name].conf.insert(otherName);
32623266
}
3263-
for (var b in junc.outblocks) {
3264-
// It conflits with any output vars of successor blocks,
3267+
for (auto b : junc.outblocks) {
3268+
// It conflicts with any output vars of successor blocks,
32653269
// if they're assigned before it goes dead in that block.
3266-
block = blocks[b];
3267-
var jSucc = junctions[block.exit];
3268-
for (var otherName in jSucc.live) {
3269-
if (junc.live[otherName]) continue;
3270-
if (block.lastKillLoc[otherName] < block.firstDeadLoc[name]) {
3271-
if (!junctionVariables[otherName]) initializeJunctionVariable(otherName);
3272-
junctionVariables[name].conf[otherName] = 1;
3273-
junctionVariables[otherName].conf[name] = 1;
3270+
Block* block = blocks[b];
3271+
Junction& jSucc = junctions[block->exit];
3272+
for (jSucc.live.has(otherName)) {
3273+
if (junc.live.has(otherName)) continue;
3274+
if (block->lastKillLoc[otherName] < block->firstDeadLoc[name]) {
3275+
if (junctionVariables.count(otherName) == 0) initializeJunctionVariable(otherName);
3276+
junctionVariables[name].conf.insert(otherName);
3277+
junctionVariables[otherName].conf.insert(name);
32743278
}
32753279
}
32763280
// It links with any linkages in the outgoing blocks.
3277-
var linkName = block.link[name];
3278-
if (linkName && linkName != name) {
3279-
if (!junctionVariables[linkName]) initializeJunctionVariable(linkName);
3280-
junctionVariables[name].link[linkName] = 1;
3281-
junctionVariables[linkName].link[name] = 1;
3281+
IString linkName = block->link[name];
3282+
if (!!linkName && linkName != name) {
3283+
if (junctionVariables.count(linkName) == 0) initializeJunctionVariable(linkName);
3284+
junctionVariables[name].link.insert(linkName);
3285+
junctionVariables[linkName].link.insert(name);
32823286
}
32833287
}
32843288
}
@@ -3288,17 +3292,12 @@ void registerizeHarder(Ref ast) {
32883292
// Simple starting point: handle the most-conflicted variables first.
32893293
// This seems to work pretty well.
32903294
3291-
var sortedJunctionVariables = keys(junctionVariables);
3292-
sortedJunctionVariables.sort(function(name1, name2) {
3293-
var jv1 = junctionVariables[name1];
3294-
var jv2 = junctionVariables[name2];
3295-
if (jv1.numConfs == undefined) {
3296-
jv1.numConfs = setSize(jv1.conf);
3297-
}
3298-
if (jv2.numConfs == undefined) {
3299-
jv2.numConfs = setSize(jv2.conf);
3300-
}
3301-
return jv2.numConfs - jv1.numConfs;
3295+
StringVec sortedJunctionVariables;
3296+
for (auto pair : junctionVariables) {
3297+
sortedJunctionVariables.push_back(pair->first);
3298+
}
3299+
std::sort(sortedJunctionVariables.begin(), sortedJunctionVariables.end(), [](const IString name1, const IString name2) {
3300+
return junctionVariables[name2].conf.size() < junctionVariables[name1].conf.size(); //XXX
33023301
});
33033302
33043303
// We can now assign a register to each junction variable.

0 commit comments

Comments
 (0)