@@ -965,6 +965,32 @@ private Register addPhiOperands(Register variable, Instruction.Phi phi) {
965965 return tryRemovingPhi (phi );
966966 }
967967
968+ // The Phi's def is dead so we need to remove
969+ // all occurrences of this def from the memoized defs
970+ // per Basic Block
971+ private void clearDefs (Instruction .Phi phi ) {
972+ // TODO rethink the data structure for currentDef
973+ var def = phi .value ();
974+ var defs = currentDef .get (def .nonSSAId ());
975+ // Make a list of block/reg that we need to delete
976+ var bbList = new ArrayList <BasicBlock >();
977+ var regList = new ArrayList <Register >();
978+ for (var entries : defs .entrySet ()) {
979+ var bb = entries .getKey ();
980+ var reg = entries .getValue ();
981+ if (reg .equals (def )) {
982+ bbList .add (bb );
983+ regList .add (reg );
984+ }
985+ }
986+ // Now delete them
987+ for (int i = 0 ; i < bbList .size (); i ++) {
988+ var bb = bbList .get (i );
989+ var reg = regList .get (i );
990+ defs .remove (bb , reg );
991+ }
992+ }
993+
968994 private Register tryRemovingPhi (Instruction .Phi phi ) {
969995 Register same = null ;
970996 // Check if phi has distinct inputs
@@ -991,6 +1017,9 @@ private Register tryRemovingPhi(Instruction.Phi phi) {
9911017 // remove all uses of phi to same and remove phi
9921018 replacePhiValueAndUsers (phi , same );
9931019 phi .block .deleteInstruction (phi );
1020+ // Since the phi is dead any references to its def
1021+ // must be removed; this is not mentioned in the paper
1022+ clearDefs (phi );
9941023 // try to recursively remove all phi users, which might have become trivial
9951024 for (var use : users ) {
9961025 if (use instanceof Instruction .Phi phiuser )
@@ -1003,25 +1032,28 @@ private Register tryRemovingPhi(Instruction.Phi phi) {
10031032 * Reroute all uses of phi to new value
10041033 */
10051034 private void replacePhiValueAndUsers (Instruction .Phi phi , Register newValue ) {
1006- var oldDefUseChain = ssaDefUses .get (phi .value ());
1035+ var oldValue = phi .value ();
1036+ var oldDefUseChain = ssaDefUses .get (oldValue );
10071037 var newDefUseChain = ssaDefUses .get (newValue );
10081038 if (newDefUseChain == null ) {
1009- // Can be null because this may be existing def
1010- newDefUseChain = SSAEdges .addDef (ssaDefUses , newValue , phi );
1039+ throw new CompilerException ("Expected error: undefined var " + newValue );
10111040 }
10121041 if (oldDefUseChain != null ) {
10131042 for (Instruction instruction : oldDefUseChain .useList ) {
1043+ boolean replaced ;
10141044 if (instruction instanceof Instruction .Phi somePhi ) {
1015- somePhi .replaceInput (phi . value () , newValue );
1045+ replaced = somePhi .replaceInput (oldValue , newValue );
10161046 }
10171047 else {
1018- instruction .replaceUse (phi .value (), newValue );
1048+ replaced = instruction .replaceUse (oldValue , newValue );
1049+ }
1050+ if (!replaced ) {
1051+ throw new CompilerException ("Discrepancy between var use list and var definition" );
10191052 }
10201053 }
10211054 // Users of phi old value become users of the new value
10221055 newDefUseChain .useList .addAll (oldDefUseChain .useList );
10231056 oldDefUseChain .useList .clear ();
1024- // FIXME remove old def from def-use chains
10251057 }
10261058 }
10271059
0 commit comments