From 8701c671c8e6d6eedc01436ea38bde01fba634f0 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Sun, 30 Mar 2025 10:54:39 -0700 Subject: Fix BB layout bug Fix split of popular value bug. --- .../java/com/seaofnodes/simple/codegen/Encoding.java | 20 +++++++++++++++----- .../java/com/seaofnodes/simple/codegen/RegAlloc.java | 1 + .../java/com/seaofnodes/simple/progs/HellowWorld.smp | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 chapter21/src/test/java/com/seaofnodes/simple/progs/HellowWorld.smp (limited to 'chapter21/src') diff --git a/chapter21/src/main/java/com/seaofnodes/simple/codegen/Encoding.java b/chapter21/src/main/java/com/seaofnodes/simple/codegen/Encoding.java index f2a2449e..fa703edc 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/codegen/Encoding.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/codegen/Encoding.java @@ -186,9 +186,14 @@ public class Encoding { // If the *next* BB has already been visited, we may need an // unconditional jump here if( visit.get(next._nid) && !(next instanceof StopNode) ) { - // If all the blocks, in RPO order, to our target - // are empty, we will fall in and not need a jump. - if( next instanceof LoopNode || !isEmptyBackwardsScan(rpo,next) ) { + boolean needJump = next instanceof LoopNode + // If backwards to a loop, and the block has statements, + // will need a jump. Empty blocks can just backwards branch. + ? (bb.nOuts()>1) + // Forwards jump. If all the blocks, in RPO order, to our + // target are empty, we will fall in and not need a jump. + : !isEmptyBackwardsScan(rpo,next); + if( needJump ) { CFGNode jmp = _code._mach.jump(); jmp.setDefX(0,bb); next.setDef(next._inputs.find(bb),jmp); @@ -230,8 +235,13 @@ public class Encoding { // when the False RPO visit returns, the IF is immediately next. // When the RPO is reversed, the fall-through path will always be // following the IF. - _rpo_cfg(t,visit,rpo); - _rpo_cfg(f,visit,rpo); + if( t.nOuts()==1 ) { + _rpo_cfg(f,visit,rpo); + _rpo_cfg(t,visit,rpo); + } else { + _rpo_cfg(t,visit,rpo); + _rpo_cfg(f,visit,rpo); + } } rpo.add(bb); } diff --git a/chapter21/src/main/java/com/seaofnodes/simple/codegen/RegAlloc.java b/chapter21/src/main/java/com/seaofnodes/simple/codegen/RegAlloc.java index 36de2cdb..9c0f5f51 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/codegen/RegAlloc.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/codegen/RegAlloc.java @@ -284,6 +284,7 @@ public class RegAlloc { for( RegMask rmask : rclass ) { Node split = makeSplit(def,"popular",round,lrg); split.insertAfter( def ); + if( split.nIns()>1 ) split.setDef(1,def); // all uses by class to split for( int j=0; j < def._outputs._len; j++ ) { Node use = def._outputs.at(j); diff --git a/chapter21/src/test/java/com/seaofnodes/simple/progs/HellowWorld.smp b/chapter21/src/test/java/com/seaofnodes/simple/progs/HellowWorld.smp new file mode 100644 index 00000000..172dffa0 --- /dev/null +++ b/chapter21/src/test/java/com/seaofnodes/simple/progs/HellowWorld.smp @@ -0,0 +1,18 @@ + +{ i32 byte[] i64 -> i64 } write = extern; +byte[] buf = new byte[100]; +buf[ 0] = 'H'; +buf[ 1] = 'e'; +buf[ 2] = 'l'; +buf[ 3] = 'l'; +buf[ 4] = 'o'; +buf[ 5] = ','; +buf[ 6] = 'W'; +buf[ 7] = 'o'; +buf[ 8] = 'r'; +buf[ 9] = 'l'; +buf[10] = 'd'; +buf[11] = '!'; +buf[12] = '\n'; + +return write(0,buf,12); -- cgit v1.2.3