@@ -34,7 +34,7 @@ private void removePhis() {
3434
3535 /* Algorithm for iterating through blocks to perform phi replacement */
3636 private void insertCopies (BasicBlock block ) {
37- List <Register > pushed = new ArrayList <>();
37+ List <Integer > pushed = new ArrayList <>();
3838 for (Instruction i : block .instructions ) {
3939 // replace all uses u with stacks[i]
4040 if (i .usesVars ()) {
@@ -45,8 +45,8 @@ private void insertCopies(BasicBlock block) {
4545 for (BasicBlock c : block .dominatedChildren ) {
4646 insertCopies (c );
4747 }
48- for (Register name : pushed ) {
49- stacks [name . id ].pop ();
48+ for (Integer name : pushed ) {
49+ stacks [name ].pop ();
5050 }
5151 }
5252
@@ -67,8 +67,8 @@ private void replaceUses(Instruction i) {
6767 }
6868
6969 static class CopyItem {
70- Register src ;
71- Register dest ;
70+ final Register src ;
71+ final Register dest ;
7272 boolean removed ;
7373
7474 public CopyItem (Register src , Register dest ) {
@@ -78,7 +78,7 @@ public CopyItem(Register src, Register dest) {
7878 }
7979 }
8080
81- private void scheduleCopies (BasicBlock block , List <Register > pushed ) {
81+ private void scheduleCopies (BasicBlock block , List <Integer > pushed ) {
8282 /* Pass 1 - Initialize data structures */
8383 /* In this pass we count the number of times a name is used by other phi-nodes */
8484 List <CopyItem > copySet = new ArrayList <>();
@@ -100,7 +100,7 @@ private void scheduleCopies(BasicBlock block, List<Register> pushed) {
100100 /* In this pass we build a worklist of names that are not used in other phi nodes */
101101 List <CopyItem > workList = new ArrayList <>();
102102 for (CopyItem copyItem : copySet ) {
103- if (! usedByAnother .get (copyItem .dest .id )) {
103+ if (usedByAnother .get (copyItem .dest .id ) != true ) {
104104 copyItem .removed = true ;
105105 workList .add (copyItem );
106106 }
@@ -112,17 +112,17 @@ private void scheduleCopies(BasicBlock block, List<Register> pushed) {
112112 /* Each time we insert a copy operation we add the source of that op to the worklist */
113113 while (!workList .isEmpty () || !copySet .isEmpty ()) {
114114 while (!workList .isEmpty ()) {
115- CopyItem copyItem = workList .remove (0 );
116- Register src = copyItem .src ;
117- Register dest = copyItem .dest ;
115+ final CopyItem copyItem = workList .remove (0 );
116+ final Register src = copyItem .src ;
117+ final Register dest = copyItem .dest ;
118118 if (block .liveOut .get (dest .id )) {
119119 /* Insert a copy from dest to a new temp t at phi node defining dest */
120- Register t = insertCopy (block , dest );
120+ final Register t = addMoveToTempAfterPhi (block , dest );
121121 stacks [dest .id ].push (t );
122- pushed .add (t );
122+ pushed .add (dest . id );
123123 }
124- /* Insert a copy operation from map[src] to dst at end of BB */
125- appendCopy (block , map .get (src .id ), dest );
124+ /* Insert a copy operation from map[src] to dest at end of BB */
125+ addMoveAtBBEnd (block , map .get (src .id ), dest );
126126 map .put (src .id , dest );
127127 /* If src is the name of a dest in copySet add item to worklist */
128128 CopyItem item = isDest (copySet , src );
@@ -133,7 +133,7 @@ private void scheduleCopies(BasicBlock block, List<Register> pushed) {
133133 if (!copySet .isEmpty ()) {
134134 CopyItem copyItem = copySet .remove (0 );
135135 /* Insert a copy from dst to new temp at the end of Block */
136- Register t = appendCopy (block , copyItem .dest );
136+ Register t = addMoveToTempAtBBEnd (block , copyItem .dest );
137137 map .put (copyItem .dest .id , t );
138138 workList .add (copyItem );
139139 }
@@ -142,30 +142,33 @@ private void scheduleCopies(BasicBlock block, List<Register> pushed) {
142142
143143 private void insertAtEnd (BasicBlock bb , Instruction i ) {
144144 assert bb .instructions .size () > 0 ;
145+ // Last instruction is a branch - so new instruction will
146+ // go before that
145147 int pos = bb .instructions .size ()-1 ;
146148 bb .instructions .add (pos , i );
147149 }
148150
149151 private void insertAfterPhi (BasicBlock bb , Register phiDef , Instruction newInst ) {
150152 assert bb .instructions .size () > 0 ;
151- int pos = 0 ;
152- while (pos < bb .instructions .size ()) {
153- Instruction i = bb .instructions .get (pos ++);
154- if (i instanceof Instruction .Phi phi &&
155- phi .def ().id == phiDef .id ) {
156- break ;
153+ int insertionPos = -1 ;
154+ for (int pos = 0 ; pos < bb .instructions .size (); pos ++) {
155+ Instruction i = bb .instructions .get (pos );
156+ if (i instanceof Instruction .Phi phi ) {
157+ if (phi .def ().id == phiDef .id ) {
158+ insertionPos = pos +1 ; // After phi
159+ break ;
160+ }
157161 }
158162 }
159- if (pos == bb . instructions . size () ) {
163+ if (insertionPos < 0 ) {
160164 throw new IllegalStateException ();
161165 }
162- bb .instructions .add (pos , newInst );
166+ bb .instructions .add (insertionPos , newInst );
163167 }
164168
165-
166169 /* Insert a copy from dest to new temp at end of BB, and return temp */
167- private Register appendCopy (BasicBlock block , Register dest ) {
168- var temp = function .registerPool .newReg (dest .name (), dest .type );
170+ private Register addMoveToTempAtBBEnd (BasicBlock block , Register dest ) {
171+ var temp = function .registerPool .newTempReg (dest .name (), dest .type );
169172 var inst = new Instruction .Move (new Operand .RegisterOperand (dest ), new Operand .RegisterOperand (temp ));
170173 insertAtEnd (block , inst );
171174 return temp ;
@@ -181,16 +184,16 @@ private CopyItem isDest(List<CopyItem> copySet, Register src) {
181184 }
182185
183186 /* Insert a copy from src to dst at end of BB */
184- private void appendCopy (BasicBlock block , Register src , Register dest ) {
187+ private void addMoveAtBBEnd (BasicBlock block , Register src , Register dest ) {
185188 var inst = new Instruction .Move (new Operand .RegisterOperand (src ), new Operand .RegisterOperand (dest ));
186189 insertAtEnd (block , inst );
187190 }
188191
189192 /* Insert a copy dest to a new temp at phi node defining dest, return temp */
190- private Register insertCopy (BasicBlock block , Register dst ) {
191- var temp = function .registerPool .newReg ( dst .name (), dst .type );
192- var inst = new Instruction .Move (new Operand .RegisterOperand (dst ), new Operand .RegisterOperand (temp ));
193- insertAfterPhi (block , dst , inst );
193+ private Register addMoveToTempAfterPhi (BasicBlock block , Register dest ) {
194+ var temp = function .registerPool .newTempReg ( dest .name (), dest .type );
195+ var inst = new Instruction .Move (new Operand .RegisterOperand (dest ), new Operand .RegisterOperand (temp ));
196+ insertAfterPhi (block , dest , inst );
194197 return temp ;
195198 }
196199
0 commit comments