|
| 1 | +package com.compilerprogramming.ezlang.compiler; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * Implement method to exit SSA by converting to conventional SSA, |
| 7 | + * without coalescing. This is the basic form. |
| 8 | + */ |
| 9 | +public class ExitSSA2 { |
| 10 | + |
| 11 | + CompiledFunction function; |
| 12 | + Map<BasicBlock,PCopy> parallelCopies = new HashMap<>(); |
| 13 | + List<BasicBlock> allBlocks; |
| 14 | + |
| 15 | + public ExitSSA2(CompiledFunction function, EnumSet<Options> options) { |
| 16 | + this.function = function; |
| 17 | + allBlocks = function.getBlocks(); |
| 18 | + insertPCopiesForEachBlock(); |
| 19 | + makeConventionalSSA(); |
| 20 | + removePhis(); |
| 21 | + sequentialzeParallelCopies(); |
| 22 | + } |
| 23 | + |
| 24 | + private void insertPCopiesForEachBlock() { |
| 25 | + // We do not actually insert pcopy instruction until needed |
| 26 | + // but we create an auxiliary data structure to help us track these |
| 27 | + for (BasicBlock block: allBlocks) { |
| 28 | + parallelCopies.put(block,new PCopy(block)); |
| 29 | + } |
| 30 | + } |
| 31 | + private void insertAtEnd(BasicBlock bb, Instruction i) { |
| 32 | + assert bb.instructions.size() > 0; |
| 33 | + // Last instruction is a branch - so new instruction will |
| 34 | + // go before that |
| 35 | + int pos = bb.instructions.size()-1; |
| 36 | + bb.add(pos, i); |
| 37 | + } |
| 38 | + |
| 39 | + private Instruction.ParallelCopyInstruction getParallelCopyAtEnd(BasicBlock block) { |
| 40 | + PCopy pcopy = parallelCopies.get(block); |
| 41 | + if (pcopy.pCopyEnd == null) { |
| 42 | + pcopy.pCopyEnd = new Instruction.ParallelCopyInstruction(); |
| 43 | + insertAtEnd(block,pcopy.pCopyEnd); |
| 44 | + } |
| 45 | + return pcopy.pCopyEnd; |
| 46 | + } |
| 47 | + |
| 48 | + private void insertAfterPhis(BasicBlock bb, Instruction newInst) { |
| 49 | + assert bb.instructions.size() > 0; |
| 50 | + int insertionPos = -1; |
| 51 | + for (int pos = 0; pos < bb.instructions.size(); pos++) { |
| 52 | + Instruction i = bb.instructions.get(pos); |
| 53 | + if (i instanceof Instruction.Phi) { |
| 54 | + insertionPos = pos+1; // After phi |
| 55 | + } |
| 56 | + else |
| 57 | + break; |
| 58 | + } |
| 59 | + if (insertionPos < 0) { |
| 60 | + throw new IllegalStateException(); |
| 61 | + } |
| 62 | + bb.add(insertionPos, newInst); |
| 63 | + } |
| 64 | + |
| 65 | + private Instruction.ParallelCopyInstruction getParallelCopyAtBegin(BasicBlock block) { |
| 66 | + PCopy pcopy = parallelCopies.get(block); |
| 67 | + if (pcopy.pCopyBegin == null) { |
| 68 | + pcopy.pCopyBegin = new Instruction.ParallelCopyInstruction(); |
| 69 | + insertAfterPhis(block,pcopy.pCopyBegin); |
| 70 | + } |
| 71 | + return pcopy.pCopyBegin; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Isolate phi nodes to make SSA conventionalS |
| 76 | + * This is Phase 1 as described in Engineering a Compiler 3rd Edition, p490. |
| 77 | + */ |
| 78 | + private void makeConventionalSSA() { |
| 79 | + var blocks = function.getBlocks(); |
| 80 | + for (BasicBlock block: blocks) { |
| 81 | + var phis = block.phis(); |
| 82 | + if (phis.isEmpty()) |
| 83 | + continue; |
| 84 | + for (var phi: phis) { |
| 85 | + for (int j = 0; j < phi.numInputs(); j++) { |
| 86 | + BasicBlock pred = block.predecessor(j); |
| 87 | + var pCopyBEnd = getParallelCopyAtEnd(pred); |
| 88 | + var oldInput = phi.input(j); |
| 89 | + var newInput = function.registerPool.newTempReg(oldInput.type); |
| 90 | + pCopyBEnd.addCopy(oldInput,new Operand.RegisterOperand(newInput)); |
| 91 | + phi.replaceInput(j,newInput); |
| 92 | + } |
| 93 | + var oldPhiVar = phi.value(); |
| 94 | + var newPhiVar = function.registerPool.newTempReg(oldPhiVar.type); |
| 95 | + phi.replaceValue(newPhiVar); |
| 96 | + var pCopyBBegin = getParallelCopyAtBegin(block); |
| 97 | + pCopyBBegin.addCopy(new Operand.RegisterOperand(newPhiVar),new Operand.RegisterOperand(oldPhiVar)); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void removePhis() { |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + private void sequentialzeParallelCopies() { |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + static final class PCopy { |
| 111 | + BasicBlock block; |
| 112 | + /** |
| 113 | + * Parallel copy instruction after any Phi instructions |
| 114 | + * in the block, null if not present |
| 115 | + */ |
| 116 | + Instruction.ParallelCopyInstruction pCopyBegin = null; |
| 117 | + /** |
| 118 | + * Parallel copy instruction at the end of a block, |
| 119 | + * null if not present |
| 120 | + */ |
| 121 | + Instruction.ParallelCopyInstruction pCopyEnd = null; |
| 122 | + |
| 123 | + public PCopy(BasicBlock block) { |
| 124 | + this.block = block; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments