Search in sources :

Example 1 with LiveSet

use of org.jikesrvm.compilers.opt.liveness.LiveSet in project JikesRVM by JikesRVM.

the class EnterSSA method patchPEIgeneratedValues.

/**
 * Work around some problems with PEI-generated values and
 * handlers.  Namely, if a PEI has a return value, rename the
 * result register before and after the PEI in order to reflect the fact
 * that the PEI may not actually assign the result register.
 */
private void patchPEIgeneratedValues() {
    // this only applies if there are exception handlers
    if (!ir.hasReachableExceptionHandlers())
        return;
    HashSet<Pair<BasicBlock, RegisterOperand>> needed = new HashSet<Pair<BasicBlock, RegisterOperand>>(4);
    Enumeration<BasicBlock> blocks = ir.getBasicBlocks();
    while (blocks.hasMoreElements()) {
        BasicBlock block = blocks.nextElement();
        if (block.getExceptionalOut().hasMoreElements()) {
            Instruction pei = block.lastRealInstruction();
            if (pei != null && pei.isPEI() && ResultCarrier.conforms(pei)) {
                boolean copyNeeded = false;
                RegisterOperand v = ResultCarrier.getResult(pei);
                // void calls and the like... :(
                if (v != null) {
                    Register orig = v.getRegister();
                    {
                        Enumeration<BasicBlock> out = block.getApplicableExceptionalOut(pei);
                        while (out.hasMoreElements()) {
                            BasicBlock exp = out.nextElement();
                            LiveSet explive = live.getLiveInfo(exp).getIn();
                            if (explive.contains(orig)) {
                                copyNeeded = true;
                                break;
                            }
                        }
                    }
                    if (copyNeeded) {
                        Enumeration<BasicBlock> out = block.getApplicableExceptionalOut(pei);
                        while (out.hasMoreElements()) {
                            BasicBlock exp = out.nextElement();
                            needed.add(new Pair<BasicBlock, RegisterOperand>(exp, v));
                        }
                    }
                }
            }
        }
    }
    // having determine where copies should be inserted, now insert them.
    if (!needed.isEmpty()) {
        for (Pair<BasicBlock, RegisterOperand> copy : needed) {
            BasicBlock inBlock = copy.first;
            RegisterOperand registerOp = copy.second;
            TypeReference type = registerOp.getType();
            Register register = registerOp.getRegister();
            Register temp = ir.regpool.getReg(register);
            inBlock.prependInstruction(SSA.makeMoveInstruction(ir, register, temp, type));
            Enumeration<BasicBlock> outBlocks = inBlock.getIn();
            while (outBlocks.hasMoreElements()) {
                BasicBlock outBlock = outBlocks.nextElement();
                Instruction x = SSA.makeMoveInstruction(ir, temp, register, type);
                SSA.addAtEnd(ir, outBlock, x, true);
            }
        }
        // Recompute liveness information.  You might be tempted to incrementally
        // update it, but it's tricky, so resist.....do the obvious, but easy thing!
        prepare();
    }
}
Also used : LiveSet(org.jikesrvm.compilers.opt.liveness.LiveSet) Enumeration(java.util.Enumeration) BasicBlock(org.jikesrvm.compilers.opt.ir.BasicBlock) Instruction(org.jikesrvm.compilers.opt.ir.Instruction) RegisterOperand(org.jikesrvm.compilers.opt.ir.operand.RegisterOperand) Register(org.jikesrvm.compilers.opt.ir.Register) TypeReference(org.jikesrvm.classloader.TypeReference) Pair(org.jikesrvm.util.Pair) HashSet(java.util.HashSet)

Example 2 with LiveSet

use of org.jikesrvm.compilers.opt.liveness.LiveSet in project JikesRVM by JikesRVM.

the class LeaveSSA method scheduleCopies.

/**
 * Record pending copy operations needed to insert at the end of a basic
 * block.<p>
 *
 * TODO: this procedure is getting long and ugly.  Rewrite or refactor
 * it.
 * @param bb the basic block to process
 * @param live valid liveness information for the IR
 */
private void scheduleCopies(BasicBlock bb, LiveAnalysis live) {
    if (DEBUG)
        VM.sysWriteln("scheduleCopies: " + bb);
    // compute out liveness from information in LiveAnalysis
    LiveSet out = new LiveSet();
    for (Enumeration<BasicBlock> outBlocks = bb.getOut(); outBlocks.hasMoreElements(); ) {
        BasicBlock ob = outBlocks.nextElement();
        LiveAnalysis.BBLiveElement le = live.getLiveInfo(ob);
        out.add(le.getIn());
    }
    // usedByAnother represents the set of registers that appear on the
    // left-hand side of subsequent phi nodes.  This is important, since
    // we be careful to order copies if the same register appears as the
    // source and dest of copies in the same basic block.
    HashSet<Register> usedByAnother = new HashSet<Register>(4);
    // for each basic block successor b of bb, if we make a block on the
    // critical edge bb->b, then store this critical block.
    HashMap<BasicBlock, BasicBlock> criticalBlocks = new HashMap<BasicBlock, BasicBlock>(4);
    // For each critical basic block b in which we are inserting copies: return the
    // mapping of registers to names implied by the copies that have
    // already been inserted into b.
    HashMap<BasicBlock, HashMap<Register, Register>> currentNames = new HashMap<BasicBlock, HashMap<Register, Register>>(4);
    // Additionally store the current names for the current basic block bb.
    HashMap<Register, Register> bbNames = new HashMap<Register, Register>(4);
    // copySet is a linked-list of copies we need to insert in this block.
    final LinkedList<Copy> copySet = new LinkedList<Copy>();
    /* Worklist is actually used like a stack - should we make this an Stack ?? */
    final LinkedList<Copy> workList = new LinkedList<Copy>();
    // collect copies required in this block.  These copies move
    // the appropriate rval into the lval of each phi node in
    // control children of the current block.
    Enumeration<BasicBlock> e = bb.getOut();
    while (e.hasMoreElements()) {
        BasicBlock bbs = e.nextElement();
        if (bbs.isExit())
            continue;
        for (Instruction phi = bbs.firstInstruction(); phi != bbs.lastInstruction(); phi = phi.nextInstructionInCodeOrder()) {
            if (phi.operator() != PHI)
                continue;
            for (int index = 0; index < Phi.getNumberOfPreds(phi); index++) {
                if (Phi.getPred(phi, index).block != bb)
                    continue;
                Operand rval = Phi.getValue(phi, index);
                if (rval.isRegister() && Phi.getResult(phi).asRegister().getRegister() == rval.asRegister().getRegister()) {
                    continue;
                }
                Copy c = new Copy(phi, index);
                copySet.add(0, c);
                if (c.source instanceof RegisterOperand) {
                    Register r = c.source.asRegister().getRegister();
                    usedByAnother.add(r);
                }
            }
        }
    }
    // the set of needed copies.
    for (Iterator<Copy> copySetIter = copySet.iterator(); copySetIter.hasNext(); ) {
        Copy c = copySetIter.next();
        if (!usedByAnother.contains(c.destination.getRegister())) {
            workList.add(0, c);
            copySetIter.remove();
        }
    }
    // while there is any more work to do.
    while (!workList.isEmpty() || !copySet.isEmpty()) {
        // while there are copies that can be correctly inserted.
        while (!workList.isEmpty()) {
            Copy c = workList.remove(0);
            Register r = c.destination.getRegister();
            TypeReference tt = c.destination.getType();
            if (VM.VerifyAssertions && tt == null) {
                tt = TypeReference.Int;
                VM.sysWriteln("SSA, warning: null type in " + c.destination);
            }
            Register rr = null;
            if (c.source.isRegister())
                rr = c.source.asRegister().getRegister();
            boolean shouldSplitBlock = !c.phi.getBasicBlock().isExceptionHandlerBasicBlock() && ((ir.options.SSA_SPLITBLOCK_TO_AVOID_RENAME && out.contains(r)) || (rr != null && ir.options.SSA_SPLITBLOCK_FOR_LOCAL_LIVE && usedBelowCopy(bb, rr)));
            if (ir.options.SSA_SPLITBLOCK_INTO_INFREQUENT) {
                if (!bb.getInfrequent() && c.phi.getBasicBlock().getInfrequent() && !c.phi.getBasicBlock().isExceptionHandlerBasicBlock()) {
                    shouldSplitBlock = true;
                }
            }
            // new name.
            if (out.contains(r) && !shouldSplitBlock) {
                if (!globalRenamePhis.contains(r)) {
                    Register t = ir.regpool.getReg(r);
                    Instruction save = SSA.makeMoveInstruction(ir, t, r, tt);
                    if (DEBUG) {
                        VM.sysWriteln("Inserting " + save + " before " + c.phi + " in " + c.phi.getBasicBlock());
                    }
                    c.phi.insertAfter(save);
                    globalRenamePhis.add(r);
                    globalRenameTable.add(save);
                }
            }
            Instruction ci = null;
            // insert copy operation required to remove phi
            if (c.source instanceof ConstantOperand) {
                if (c.source instanceof UnreachableOperand) {
                    ci = null;
                } else {
                    ci = SSA.makeMoveInstruction(ir, r, (ConstantOperand) c.source);
                }
            } else if (c.source instanceof RegisterOperand) {
                if (shouldSplitBlock) {
                    if (DEBUG)
                        VM.sysWriteln("splitting edge: " + bb + "->" + c.phi.getBasicBlock());
                    BasicBlock criticalBlock = criticalBlocks.get(c.phi.getBasicBlock());
                    if (criticalBlock == null) {
                        criticalBlock = IRTools.makeBlockOnEdge(bb, c.phi.getBasicBlock(), ir);
                        if (c.phi.getBasicBlock().getInfrequent()) {
                            criticalBlock.setInfrequent();
                        }
                        splitSomeBlock = true;
                        criticalBlocks.put(c.phi.getBasicBlock(), criticalBlock);
                        HashMap<Register, Register> newNames = new HashMap<Register, Register>(4);
                        currentNames.put(criticalBlock, newNames);
                    }
                    Register sr = c.source.asRegister().getRegister();
                    HashMap<Register, Register> criticalBlockNames = currentNames.get(criticalBlock);
                    Register nameForSR = criticalBlockNames.get(sr);
                    if (nameForSR == null) {
                        nameForSR = bbNames.get(sr);
                        if (nameForSR == null)
                            nameForSR = sr;
                    }
                    if (DEBUG)
                        VM.sysWriteln("dest(r): " + r);
                    if (DEBUG)
                        VM.sysWriteln("sr: " + sr + ", nameForSR: " + nameForSR);
                    ci = SSA.makeMoveInstruction(ir, r, nameForSR, tt);
                    criticalBlockNames.put(sr, r);
                    criticalBlock.appendInstructionRespectingTerminalBranch(ci);
                } else {
                    Register sr = c.source.asRegister().getRegister();
                    Register nameForSR = bbNames.get(sr);
                    if (nameForSR == null)
                        nameForSR = sr;
                    if (DEBUG)
                        VM.sysWriteln("not splitting edge: " + bb + "->" + c.phi.getBasicBlock());
                    if (DEBUG)
                        VM.sysWriteln("dest(r): " + r);
                    if (DEBUG)
                        VM.sysWriteln("sr: " + sr + ", nameForSR: " + nameForSR);
                    ci = SSA.makeMoveInstruction(ir, r, nameForSR, tt);
                    bbNames.put(sr, r);
                    SSA.addAtEnd(ir, bb, ci, c.phi.getBasicBlock().isExceptionHandlerBasicBlock());
                }
                // ugly hack: having already added ci; set ci to null to skip remaining code;
                ci = null;
            } else {
                throw new OptimizingCompilerException("Unexpected phi operand " + c.source + " encountered during SSA teardown", true);
            }
            if (ci != null) {
                if (shouldSplitBlock) {
                    if (DEBUG)
                        VM.sysWriteln("splitting edge: " + bb + "->" + c.phi.getBasicBlock());
                    BasicBlock criticalBlock = criticalBlocks.get(c.phi.getBasicBlock());
                    if (criticalBlock == null) {
                        criticalBlock = IRTools.makeBlockOnEdge(bb, c.phi.getBasicBlock(), ir);
                        if (c.phi.getBasicBlock().getInfrequent()) {
                            criticalBlock.setInfrequent();
                        }
                        splitSomeBlock = true;
                        criticalBlocks.put(c.phi.getBasicBlock(), criticalBlock);
                        HashMap<Register, Register> newNames = new HashMap<Register, Register>(4);
                        currentNames.put(criticalBlock, newNames);
                    }
                    criticalBlock.appendInstructionRespectingTerminalBranch(ci);
                } else {
                    SSA.addAtEnd(ir, bb, ci, c.phi.getBasicBlock().isExceptionHandlerBasicBlock());
                }
            }
            // current copy to the work list.
            if (c.source instanceof RegisterOperand) {
                Register saved = c.source.asRegister().getRegister();
                Iterator<Copy> copySetIter = copySet.iterator();
                while (copySetIter.hasNext()) {
                    Copy cc = copySetIter.next();
                    if (cc.destination.asRegister().getRegister() == saved) {
                        workList.add(0, cc);
                        copySetIter.remove();
                    }
                }
            }
        }
        // safely overwritten.  so, add that copy to the work list.
        if (!copySet.isEmpty()) {
            Copy c = copySet.remove(0);
            Register tt = ir.regpool.getReg(c.destination.getRegister());
            SSA.addAtEnd(ir, bb, SSA.makeMoveInstruction(ir, tt, c.destination.getRegister(), c.destination.getType()), c.phi.getBasicBlock().isExceptionHandlerBasicBlock());
            bbNames.put(c.destination.getRegister(), tt);
            workList.add(0, c);
        }
    }
}
Also used : LiveSet(org.jikesrvm.compilers.opt.liveness.LiveSet) HashMap(java.util.HashMap) UnreachableOperand(org.jikesrvm.compilers.opt.ir.operand.UnreachableOperand) RegisterOperand(org.jikesrvm.compilers.opt.ir.operand.RegisterOperand) TrueGuardOperand(org.jikesrvm.compilers.opt.ir.operand.TrueGuardOperand) Operand(org.jikesrvm.compilers.opt.ir.operand.Operand) ConstantOperand(org.jikesrvm.compilers.opt.ir.operand.ConstantOperand) UnreachableOperand(org.jikesrvm.compilers.opt.ir.operand.UnreachableOperand) Instruction(org.jikesrvm.compilers.opt.ir.Instruction) RegisterOperand(org.jikesrvm.compilers.opt.ir.operand.RegisterOperand) TypeReference(org.jikesrvm.classloader.TypeReference) OptimizingCompilerException(org.jikesrvm.compilers.opt.OptimizingCompilerException) HashSet(java.util.HashSet) ConstantOperand(org.jikesrvm.compilers.opt.ir.operand.ConstantOperand) LiveAnalysis(org.jikesrvm.compilers.opt.liveness.LiveAnalysis) BasicBlock(org.jikesrvm.compilers.opt.ir.BasicBlock) LinkedList(java.util.LinkedList) Register(org.jikesrvm.compilers.opt.ir.Register)

Aggregations

HashSet (java.util.HashSet)2 TypeReference (org.jikesrvm.classloader.TypeReference)2 BasicBlock (org.jikesrvm.compilers.opt.ir.BasicBlock)2 Instruction (org.jikesrvm.compilers.opt.ir.Instruction)2 Register (org.jikesrvm.compilers.opt.ir.Register)2 RegisterOperand (org.jikesrvm.compilers.opt.ir.operand.RegisterOperand)2 LiveSet (org.jikesrvm.compilers.opt.liveness.LiveSet)2 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 OptimizingCompilerException (org.jikesrvm.compilers.opt.OptimizingCompilerException)1 ConstantOperand (org.jikesrvm.compilers.opt.ir.operand.ConstantOperand)1 Operand (org.jikesrvm.compilers.opt.ir.operand.Operand)1 TrueGuardOperand (org.jikesrvm.compilers.opt.ir.operand.TrueGuardOperand)1 UnreachableOperand (org.jikesrvm.compilers.opt.ir.operand.UnreachableOperand)1 LiveAnalysis (org.jikesrvm.compilers.opt.liveness.LiveAnalysis)1 Pair (org.jikesrvm.util.Pair)1