Search in sources :

Example 1 with InstructionValueConsumer

use of org.graalvm.compiler.lir.InstructionValueConsumer in project graal by oracle.

the class LinearScanLifetimeAnalysisPhase method buildIntervals.

@SuppressWarnings("try")
protected void buildIntervals(boolean detailedAsserts) {
    try (Indent indent = debug.logAndIndent("build intervals")) {
        InstructionValueConsumer outputConsumer = (op, operand, mode, flags) -> {
            if (LinearScan.isVariableOrRegister(operand)) {
                addDef((AllocatableValue) operand, op, registerPriorityOfOutputOperand(op), operand.getValueKind(), detailedAsserts);
                addRegisterHint(op, operand, mode, flags, true);
            }
        };
        InstructionValueConsumer tempConsumer = (op, operand, mode, flags) -> {
            if (LinearScan.isVariableOrRegister(operand)) {
                addTemp((AllocatableValue) operand, op.id(), RegisterPriority.MustHaveRegister, operand.getValueKind(), detailedAsserts);
                addRegisterHint(op, operand, mode, flags, false);
            }
        };
        InstructionValueConsumer aliveConsumer = (op, operand, mode, flags) -> {
            if (LinearScan.isVariableOrRegister(operand)) {
                RegisterPriority p = registerPriorityOfInputOperand(flags);
                int opId = op.id();
                int blockFrom = allocator.getFirstLirInstructionId((allocator.blockForId(opId)));
                addUse((AllocatableValue) operand, blockFrom, opId + 1, p, operand.getValueKind(), detailedAsserts);
                addRegisterHint(op, operand, mode, flags, false);
            }
        };
        InstructionValueConsumer inputConsumer = (op, operand, mode, flags) -> {
            if (LinearScan.isVariableOrRegister(operand)) {
                int opId = op.id();
                int blockFrom = allocator.getFirstLirInstructionId((allocator.blockForId(opId)));
                RegisterPriority p = registerPriorityOfInputOperand(flags);
                addUse((AllocatableValue) operand, blockFrom, opId, p, operand.getValueKind(), detailedAsserts);
                addRegisterHint(op, operand, mode, flags, false);
            }
        };
        InstructionValueConsumer stateProc = (op, operand, mode, flags) -> {
            if (LinearScan.isVariableOrRegister(operand)) {
                int opId = op.id();
                int blockFrom = allocator.getFirstLirInstructionId((allocator.blockForId(opId)));
                addUse((AllocatableValue) operand, blockFrom, opId + 1, RegisterPriority.None, operand.getValueKind(), detailedAsserts);
            }
        };
        // create a list with all caller-save registers (cpu, fpu, xmm)
        RegisterArray callerSaveRegs = allocator.getRegisterAllocationConfig().getRegisterConfig().getCallerSaveRegisters();
        // iterate all blocks in reverse order
        for (int i = allocator.blockCount() - 1; i >= 0; i--) {
            AbstractBlockBase<?> block = allocator.blockAt(i);
            try (Indent indent2 = debug.logAndIndent("handle block %d", block.getId())) {
                ArrayList<LIRInstruction> instructions = allocator.getLIR().getLIRforBlock(block);
                final int blockFrom = allocator.getFirstLirInstructionId(block);
                int blockTo = allocator.getLastLirInstructionId(block);
                assert blockFrom == instructions.get(0).id();
                assert blockTo == instructions.get(instructions.size() - 1).id();
                // Update intervals for operands live at the end of this block;
                BitSet live = allocator.getBlockData(block).liveOut;
                for (int operandNum = live.nextSetBit(0); operandNum >= 0; operandNum = live.nextSetBit(operandNum + 1)) {
                    assert live.get(operandNum) : "should not stop here otherwise";
                    AllocatableValue operand = allocator.intervalFor(operandNum).operand;
                    if (debug.isLogEnabled()) {
                        debug.log("live in %d: %s", operandNum, operand);
                    }
                    addUse(operand, blockFrom, blockTo + 2, RegisterPriority.None, LIRKind.Illegal, detailedAsserts);
                    /*
                         * Add special use positions for loop-end blocks when the interval is used
                         * anywhere inside this loop. It's possible that the block was part of a
                         * non-natural loop, so it might have an invalid loop index.
                         */
                    if (block.isLoopEnd() && block.getLoop() != null && isIntervalInLoop(operandNum, block.getLoop().getIndex())) {
                        allocator.intervalFor(operandNum).addUsePos(blockTo + 1, RegisterPriority.LiveAtLoopEnd, detailedAsserts);
                    }
                }
                /*
                     * Iterate all instructions of the block in reverse order. definitions of
                     * intervals are processed before uses.
                     */
                for (int j = instructions.size() - 1; j >= 0; j--) {
                    final LIRInstruction op = instructions.get(j);
                    final int opId = op.id();
                    try (Indent indent3 = debug.logAndIndent("handle inst %d: %s", opId, op)) {
                        // caller-save registers
                        if (op.destroysCallerSavedRegisters()) {
                            for (Register r : callerSaveRegs) {
                                if (allocator.attributes(r).isAllocatable()) {
                                    addTemp(r.asValue(), opId, RegisterPriority.None, LIRKind.Illegal, detailedAsserts);
                                }
                            }
                            if (debug.isLogEnabled()) {
                                debug.log("operation destroys all caller-save registers");
                            }
                        }
                        op.visitEachOutput(outputConsumer);
                        op.visitEachTemp(tempConsumer);
                        op.visitEachAlive(aliveConsumer);
                        op.visitEachInput(inputConsumer);
                        /*
                             * Add uses of live locals from interpreter's point of view for proper
                             * debug information generation. Treat these operands as temp values (if
                             * the live range is extended to a call site, the value would be in a
                             * register at the call otherwise).
                             */
                        op.visitEachState(stateProc);
                        // special steps for some instructions (especially moves)
                        handleMethodArguments(op);
                    }
                }
            // end of instruction iteration
            }
        }
        /*
             * Add the range [0, 1] to all fixed intervals. the register allocator need not handle
             * unhandled fixed intervals.
             */
        for (Interval interval : allocator.intervals()) {
            if (interval != null && isRegister(interval.operand)) {
                interval.addRange(0, 1);
            }
        }
    }
}
Also used : AbstractBlockBase(org.graalvm.compiler.core.common.cfg.AbstractBlockBase) PermanentBailoutException(org.graalvm.compiler.core.common.PermanentBailoutException) Constant(jdk.vm.ci.meta.Constant) LoadConstantOp(org.graalvm.compiler.lir.StandardOp.LoadConstantOp) BitMap2D(org.graalvm.compiler.core.common.util.BitMap2D) LIRValueUtil.asVariable(org.graalvm.compiler.lir.LIRValueUtil.asVariable) EconomicSet(org.graalvm.collections.EconomicSet) LIRInstruction(org.graalvm.compiler.lir.LIRInstruction) LIRGenerationResult(org.graalvm.compiler.lir.gen.LIRGenerationResult) Register(jdk.vm.ci.code.Register) Assertions(org.graalvm.compiler.debug.Assertions) ValueUtil.asStackSlot(jdk.vm.ci.code.ValueUtil.asStackSlot) ArrayList(java.util.ArrayList) InstructionValueConsumer(org.graalvm.compiler.lir.InstructionValueConsumer) SpillState(org.graalvm.compiler.lir.alloc.lsra.Interval.SpillState) OperandFlag(org.graalvm.compiler.lir.LIRInstruction.OperandFlag) DebugContext(org.graalvm.compiler.debug.DebugContext) RegisterArray(jdk.vm.ci.code.RegisterArray) ValueUtil.isRegister(jdk.vm.ci.code.ValueUtil.isRegister) ValueUtil.asRegister(jdk.vm.ci.code.ValueUtil.asRegister) ComputeBlockOrder(org.graalvm.compiler.core.common.alloc.ComputeBlockOrder) BlockData(org.graalvm.compiler.lir.alloc.lsra.LinearScan.BlockData) LIRGenerationDebugContext.getSourceForOperandFromDebugContext(org.graalvm.compiler.lir.debug.LIRGenerationDebugContext.getSourceForOperandFromDebugContext) Equivalence(org.graalvm.collections.Equivalence) EnumSet(java.util.EnumSet) ValueUtil.isStackSlot(jdk.vm.ci.code.ValueUtil.isStackSlot) OperandMode(org.graalvm.compiler.lir.LIRInstruction.OperandMode) ValueConsumer(org.graalvm.compiler.lir.ValueConsumer) LIRValueUtil.isVariable(org.graalvm.compiler.lir.LIRValueUtil.isVariable) LIRKind(org.graalvm.compiler.core.common.LIRKind) AllocationContext(org.graalvm.compiler.lir.phases.AllocationPhase.AllocationContext) TargetDescription(jdk.vm.ci.code.TargetDescription) JavaConstant(jdk.vm.ci.meta.JavaConstant) RegisterPriority(org.graalvm.compiler.lir.alloc.lsra.Interval.RegisterPriority) Value(jdk.vm.ci.meta.Value) ValueMoveOp(org.graalvm.compiler.lir.StandardOp.ValueMoveOp) Indent(org.graalvm.compiler.debug.Indent) GraalError(org.graalvm.compiler.debug.GraalError) StackSlot(jdk.vm.ci.code.StackSlot) BitSet(java.util.BitSet) ArrayDeque(java.util.ArrayDeque) ValueKind(jdk.vm.ci.meta.ValueKind) AllocatableValue(jdk.vm.ci.meta.AllocatableValue) RegisterPriority(org.graalvm.compiler.lir.alloc.lsra.Interval.RegisterPriority) Indent(org.graalvm.compiler.debug.Indent) InstructionValueConsumer(org.graalvm.compiler.lir.InstructionValueConsumer) LIRInstruction(org.graalvm.compiler.lir.LIRInstruction) BitSet(java.util.BitSet) RegisterArray(jdk.vm.ci.code.RegisterArray) AllocatableValue(jdk.vm.ci.meta.AllocatableValue) Register(jdk.vm.ci.code.Register) ValueUtil.isRegister(jdk.vm.ci.code.ValueUtil.isRegister) ValueUtil.asRegister(jdk.vm.ci.code.ValueUtil.asRegister)

Example 2 with InstructionValueConsumer

use of org.graalvm.compiler.lir.InstructionValueConsumer in project graal by oracle.

the class RegisterVerifier method processOperations.

void processOperations(AbstractBlockBase<?> block, final Interval[] inputState) {
    ArrayList<LIRInstruction> ops = allocator.getLIR().getLIRforBlock(block);
    DebugContext debug = allocator.getDebug();
    InstructionValueConsumer useConsumer = new InstructionValueConsumer() {

        @Override
        public void visitValue(LIRInstruction op, Value operand, OperandMode mode, EnumSet<OperandFlag> flags) {
            // we skip spill moves inserted by the spill position optimization
            if (LinearScan.isVariableOrRegister(operand) && allocator.isProcessed(operand) && op.id() != LinearScan.DOMINATOR_SPILL_MOVE_ID) {
                Interval interval = intervalAt(operand);
                if (op.id() != -1) {
                    interval = interval.getSplitChildAtOpId(op.id(), mode, allocator);
                }
                assert checkState(block, op, inputState, interval.operand, interval.location(), interval.splitParent());
            }
        }
    };
    InstructionValueConsumer defConsumer = (op, operand, mode, flags) -> {
        if (LinearScan.isVariableOrRegister(operand) && allocator.isProcessed(operand)) {
            Interval interval = intervalAt(operand);
            if (op.id() != -1) {
                interval = interval.getSplitChildAtOpId(op.id(), mode, allocator);
            }
            statePut(debug, inputState, interval.location(), interval.splitParent());
        }
    };
    // visit all instructions of the block
    for (int i = 0; i < ops.size(); i++) {
        final LIRInstruction op = ops.get(i);
        if (debug.isLogEnabled()) {
            debug.log("%s", op.toStringWithIdPrefix());
        }
        // check if input operands are correct
        op.visitEachInput(useConsumer);
        // invalidate all caller save registers at calls
        if (op.destroysCallerSavedRegisters()) {
            for (Register r : allocator.getRegisterAllocationConfig().getRegisterConfig().getCallerSaveRegisters()) {
                statePut(debug, inputState, r.asValue(), null);
            }
        }
        op.visitEachAlive(useConsumer);
        // set temp operands (some operations use temp operands also as output operands, so
        // can't set them null)
        op.visitEachTemp(defConsumer);
        // set output operands
        op.visitEachOutput(defConsumer);
    }
}
Also used : AbstractBlockBase(org.graalvm.compiler.core.common.cfg.AbstractBlockBase) OperandMode(org.graalvm.compiler.lir.LIRInstruction.OperandMode) LIRInstruction(org.graalvm.compiler.lir.LIRInstruction) Register(jdk.vm.ci.code.Register) ArrayList(java.util.ArrayList) BlockMap(org.graalvm.compiler.core.common.cfg.BlockMap) InstructionValueConsumer(org.graalvm.compiler.lir.InstructionValueConsumer) Value(jdk.vm.ci.meta.Value) OperandFlag(org.graalvm.compiler.lir.LIRInstruction.OperandFlag) DebugContext(org.graalvm.compiler.debug.DebugContext) ValueUtil.isRegister(jdk.vm.ci.code.ValueUtil.isRegister) Indent(org.graalvm.compiler.debug.Indent) GraalError(org.graalvm.compiler.debug.GraalError) ValueUtil.asRegister(jdk.vm.ci.code.ValueUtil.asRegister) EnumSet(java.util.EnumSet) InstructionValueConsumer(org.graalvm.compiler.lir.InstructionValueConsumer) Register(jdk.vm.ci.code.Register) ValueUtil.isRegister(jdk.vm.ci.code.ValueUtil.isRegister) ValueUtil.asRegister(jdk.vm.ci.code.ValueUtil.asRegister) LIRInstruction(org.graalvm.compiler.lir.LIRInstruction) EnumSet(java.util.EnumSet) Value(jdk.vm.ci.meta.Value) DebugContext(org.graalvm.compiler.debug.DebugContext) OperandMode(org.graalvm.compiler.lir.LIRInstruction.OperandMode)

Example 3 with InstructionValueConsumer

use of org.graalvm.compiler.lir.InstructionValueConsumer in project graal by oracle.

the class RegisterVerifier method processOperations.

void processOperations(AbstractBlockBase<?> block, final TraceInterval[] inputState) {
    ArrayList<LIRInstruction> ops = allocator.getLIR().getLIRforBlock(block);
    DebugContext debug = allocator.getDebug();
    InstructionValueConsumer useConsumer = new InstructionValueConsumer() {

        @Override
        public void visitValue(LIRInstruction op, Value operand, OperandMode mode, EnumSet<OperandFlag> flags) {
            // we skip spill moves inserted by the spill position optimization
            if (isVariableOrRegister(operand) && allocator.isProcessed(operand) && op.id() != TraceLinearScanPhase.DOMINATOR_SPILL_MOVE_ID) {
                TraceInterval interval = intervalAt(asVariable(operand));
                if (op.id() != -1) {
                    interval = interval.getSplitChildAtOpId(op.id(), mode);
                }
                assert checkState(block, op, inputState, allocator.getOperand(interval), interval.location(), interval.splitParent());
            }
        }
    };
    InstructionValueConsumer defConsumer = (op, operand, mode, flags) -> {
        if (isVariableOrRegister(operand) && allocator.isProcessed(operand)) {
            TraceInterval interval = intervalAt(asVariable(operand));
            if (op.id() != -1) {
                interval = interval.getSplitChildAtOpId(op.id(), mode);
            }
            statePut(debug, inputState, interval.location(), interval.splitParent());
        }
    };
    // visit all instructions of the block
    for (int i = 0; i < ops.size(); i++) {
        final LIRInstruction op = ops.get(i);
        if (debug.isLogEnabled()) {
            debug.log("%s", op.toStringWithIdPrefix());
        }
        // check if input operands are correct
        op.visitEachInput(useConsumer);
        // invalidate all caller save registers at calls
        if (op.destroysCallerSavedRegisters()) {
            for (Register r : allocator.getRegisterAllocationConfig().getRegisterConfig().getCallerSaveRegisters()) {
                statePut(debug, inputState, r.asValue(), null);
            }
        }
        op.visitEachAlive(useConsumer);
        // set temp operands (some operations use temp operands also as output operands, so
        // can't set them null)
        op.visitEachTemp(defConsumer);
        // set output operands
        op.visitEachOutput(defConsumer);
    }
}
Also used : AbstractBlockBase(org.graalvm.compiler.core.common.cfg.AbstractBlockBase) OperandMode(org.graalvm.compiler.lir.LIRInstruction.OperandMode) Variable(org.graalvm.compiler.lir.Variable) TraceLinearScanPhase.isVariableOrRegister(org.graalvm.compiler.lir.alloc.trace.lsra.TraceLinearScanPhase.isVariableOrRegister) TraceLinearScan(org.graalvm.compiler.lir.alloc.trace.lsra.TraceLinearScanPhase.TraceLinearScan) LIRValueUtil.asVariable(org.graalvm.compiler.lir.LIRValueUtil.asVariable) LIRInstruction(org.graalvm.compiler.lir.LIRInstruction) Register(jdk.vm.ci.code.Register) ArrayList(java.util.ArrayList) BlockMap(org.graalvm.compiler.core.common.cfg.BlockMap) InstructionValueConsumer(org.graalvm.compiler.lir.InstructionValueConsumer) Value(jdk.vm.ci.meta.Value) OperandFlag(org.graalvm.compiler.lir.LIRInstruction.OperandFlag) DebugContext(org.graalvm.compiler.debug.DebugContext) ValueUtil.isRegister(jdk.vm.ci.code.ValueUtil.isRegister) Indent(org.graalvm.compiler.debug.Indent) GraalError(org.graalvm.compiler.debug.GraalError) ValueUtil.asRegister(jdk.vm.ci.code.ValueUtil.asRegister) EnumSet(java.util.EnumSet) InstructionValueConsumer(org.graalvm.compiler.lir.InstructionValueConsumer) TraceLinearScanPhase.isVariableOrRegister(org.graalvm.compiler.lir.alloc.trace.lsra.TraceLinearScanPhase.isVariableOrRegister) Register(jdk.vm.ci.code.Register) ValueUtil.isRegister(jdk.vm.ci.code.ValueUtil.isRegister) ValueUtil.asRegister(jdk.vm.ci.code.ValueUtil.asRegister) LIRInstruction(org.graalvm.compiler.lir.LIRInstruction) EnumSet(java.util.EnumSet) Value(jdk.vm.ci.meta.Value) DebugContext(org.graalvm.compiler.debug.DebugContext) OperandMode(org.graalvm.compiler.lir.LIRInstruction.OperandMode)

Example 4 with InstructionValueConsumer

use of org.graalvm.compiler.lir.InstructionValueConsumer in project graal by oracle.

the class FrameMapBuilderImpl method verifyStackSlotAllocation.

private static void verifyStackSlotAllocation(LIRGenerationResult res) {
    LIR lir = res.getLIR();
    InstructionValueConsumer verifySlots = (LIRInstruction op, Value value, OperandMode mode, EnumSet<OperandFlag> flags) -> {
        assert !isVirtualStackSlot(value) : String.format("Instruction %s contains a virtual stack slot %s", op, value);
    };
    for (AbstractBlockBase<?> block : lir.getControlFlowGraph().getBlocks()) {
        lir.getLIRforBlock(block).forEach(op -> {
            op.visitEachInput(verifySlots);
            op.visitEachAlive(verifySlots);
            op.visitEachState(verifySlots);
            op.visitEachTemp(verifySlots);
            op.visitEachOutput(verifySlots);
        });
    }
}
Also used : LIR(org.graalvm.compiler.lir.LIR) InstructionValueConsumer(org.graalvm.compiler.lir.InstructionValueConsumer) LIRInstruction(org.graalvm.compiler.lir.LIRInstruction) Value(jdk.vm.ci.meta.Value) OperandFlag(org.graalvm.compiler.lir.LIRInstruction.OperandFlag) OperandMode(org.graalvm.compiler.lir.LIRInstruction.OperandMode)

Aggregations

Value (jdk.vm.ci.meta.Value)4 InstructionValueConsumer (org.graalvm.compiler.lir.InstructionValueConsumer)4 LIRInstruction (org.graalvm.compiler.lir.LIRInstruction)4 OperandFlag (org.graalvm.compiler.lir.LIRInstruction.OperandFlag)4 OperandMode (org.graalvm.compiler.lir.LIRInstruction.OperandMode)4 ArrayList (java.util.ArrayList)3 EnumSet (java.util.EnumSet)3 Register (jdk.vm.ci.code.Register)3 ValueUtil.asRegister (jdk.vm.ci.code.ValueUtil.asRegister)3 ValueUtil.isRegister (jdk.vm.ci.code.ValueUtil.isRegister)3 AbstractBlockBase (org.graalvm.compiler.core.common.cfg.AbstractBlockBase)3 DebugContext (org.graalvm.compiler.debug.DebugContext)3 GraalError (org.graalvm.compiler.debug.GraalError)3 Indent (org.graalvm.compiler.debug.Indent)3 BlockMap (org.graalvm.compiler.core.common.cfg.BlockMap)2 LIRValueUtil.asVariable (org.graalvm.compiler.lir.LIRValueUtil.asVariable)2 ArrayDeque (java.util.ArrayDeque)1 BitSet (java.util.BitSet)1 RegisterArray (jdk.vm.ci.code.RegisterArray)1 StackSlot (jdk.vm.ci.code.StackSlot)1