Search in sources :

Example 11 with Operand

use of org.jikesrvm.compilers.opt.ir.operand.Operand in project JikesRVM by JikesRVM.

the class ShortArrayReplacer method transform.

@Override
public void transform() {
    // first set up temporary scalars for the array elements
    // initialize them before the def.
    RegisterOperand[] scalars = new RegisterOperand[size];
    TypeReference elementType = vmArray.getElementType().getTypeRef();
    RegisterOperand def = reg.defList;
    Instruction defI = def.instruction;
    Operand defaultValue = IRTools.getDefaultOperand(elementType);
    for (int i = 0; i < size; i++) {
        scalars[i] = IRTools.moveIntoRegister(elementType, IRTools.getMoveOp(elementType), ir.regpool, defI, defaultValue.copy());
    }
    transform2(this.reg, defI, scalars);
}
Also used : RegisterOperand(org.jikesrvm.compilers.opt.ir.operand.RegisterOperand) RegisterOperand(org.jikesrvm.compilers.opt.ir.operand.RegisterOperand) TrueGuardOperand(org.jikesrvm.compilers.opt.ir.operand.TrueGuardOperand) ConditionOperand(org.jikesrvm.compilers.opt.ir.operand.ConditionOperand) Operand(org.jikesrvm.compilers.opt.ir.operand.Operand) TIBConstantOperand(org.jikesrvm.compilers.opt.ir.operand.TIBConstantOperand) TrapCodeOperand(org.jikesrvm.compilers.opt.ir.operand.TrapCodeOperand) TypeReference(org.jikesrvm.classloader.TypeReference) Instruction(org.jikesrvm.compilers.opt.ir.Instruction)

Example 12 with Operand

use of org.jikesrvm.compilers.opt.ir.operand.Operand in project JikesRVM by JikesRVM.

the class ShortArrayReplacer method scalarReplace.

/**
 * Replace a given use of an array with its scalar equivalent.
 *
 * @param use the use to replace
 * @param scalars an array of scalar register operands to replace
 *                  the array with
 * @param visited TODO currently useless. Is this parameter
 *  necessary or should it be removed?
 */
private void scalarReplace(RegisterOperand use, RegisterOperand[] scalars, Set<Register> visited) {
    Instruction inst = use.instruction;
    RVMType type = vmArray.getElementType();
    switch(inst.getOpcode()) {
        case INT_ALOAD_opcode:
        case LONG_ALOAD_opcode:
        case FLOAT_ALOAD_opcode:
        case DOUBLE_ALOAD_opcode:
        case BYTE_ALOAD_opcode:
        case UBYTE_ALOAD_opcode:
        case USHORT_ALOAD_opcode:
        case SHORT_ALOAD_opcode:
        case REF_ALOAD_opcode:
            {
                // of a trap
                if (ALoad.getIndex(inst).isIntConstant()) {
                    Operator moveOp = IRTools.getMoveOp(type.getTypeRef());
                    int index = ALoad.getIndex(inst).asIntConstant().value;
                    if (index >= 0 && index < size) {
                        Instruction i2 = Move.create(moveOp, ALoad.getClearResult(inst), scalars[index].copyRO());
                        DefUse.replaceInstructionAndUpdateDU(inst, i2);
                    } else {
                        DefUse.removeInstructionAndUpdateDU(inst);
                    }
                } else {
                    if (VM.BuildForIA32) {
                        if (size == 0) {
                            DefUse.removeInstructionAndUpdateDU(inst);
                        } else if (size == 1) {
                            int index = 0;
                            Operator moveOp = IRTools.getMoveOp(type.getTypeRef());
                            Instruction i2 = Move.create(moveOp, ALoad.getClearResult(inst), scalars[index].copyRO());
                            DefUse.replaceInstructionAndUpdateDU(inst, i2);
                        } else {
                            Operator moveOp = IRTools.getCondMoveOp(type.getTypeRef());
                            Instruction i2 = CondMove.create(moveOp, ALoad.getClearResult(inst), ALoad.getClearIndex(inst), IC(0), ConditionOperand.EQUAL(), scalars[0].copyRO(), scalars[1].copyRO());
                            DefUse.replaceInstructionAndUpdateDU(inst, i2);
                        }
                    } else {
                        if (size == 1) {
                            int index = 0;
                            Operator moveOp = IRTools.getMoveOp(type.getTypeRef());
                            Instruction i2 = Move.create(moveOp, ALoad.getClearResult(inst), scalars[index].copyRO());
                            DefUse.replaceInstructionAndUpdateDU(inst, i2);
                        } else {
                            DefUse.removeInstructionAndUpdateDU(inst);
                        }
                    }
                }
            }
            break;
        case INT_ASTORE_opcode:
        case LONG_ASTORE_opcode:
        case FLOAT_ASTORE_opcode:
        case DOUBLE_ASTORE_opcode:
        case BYTE_ASTORE_opcode:
        case SHORT_ASTORE_opcode:
        case REF_ASTORE_opcode:
            {
                // of a trap
                if (AStore.getIndex(inst).isIntConstant()) {
                    int index = AStore.getIndex(inst).asIntConstant().value;
                    if (index >= 0 && index < size) {
                        Operator moveOp = IRTools.getMoveOp(type.getTypeRef());
                        Instruction i2 = Move.create(moveOp, scalars[index].copyRO(), AStore.getClearValue(inst));
                        DefUse.replaceInstructionAndUpdateDU(inst, i2);
                    } else {
                        DefUse.removeInstructionAndUpdateDU(inst);
                    }
                } else {
                    if (VM.BuildForIA32) {
                        if (size == 0) {
                            DefUse.removeInstructionAndUpdateDU(inst);
                        } else if (size == 1) {
                            int index = 0;
                            Operator moveOp = IRTools.getMoveOp(type.getTypeRef());
                            Instruction i2 = Move.create(moveOp, scalars[index].copyRO(), AStore.getClearValue(inst));
                            DefUse.replaceInstructionAndUpdateDU(inst, i2);
                        } else {
                            Operator moveOp = IRTools.getCondMoveOp(type.getTypeRef());
                            Operand value = AStore.getClearValue(inst);
                            Instruction i2 = CondMove.create(moveOp, scalars[0].copyRO(), AStore.getIndex(inst), IC(0), ConditionOperand.EQUAL(), value, scalars[0].copyRO());
                            DefUse.replaceInstructionAndUpdateDU(inst, i2);
                            Instruction i3 = CondMove.create(moveOp, scalars[1].copyRO(), AStore.getIndex(inst), IC(0), ConditionOperand.NOT_EQUAL(), value, scalars[1].copyRO());
                            i2.insertAfter(i3);
                            DefUse.updateDUForNewInstruction(i3);
                        }
                    } else {
                        if (size == 1) {
                            int index = 0;
                            Operator moveOp = IRTools.getMoveOp(type.getTypeRef());
                            Instruction i2 = Move.create(moveOp, scalars[index].copyRO(), AStore.getClearValue(inst));
                            DefUse.replaceInstructionAndUpdateDU(inst, i2);
                        } else {
                            DefUse.removeInstructionAndUpdateDU(inst);
                        }
                    }
                }
            }
            break;
        case NULL_CHECK_opcode:
            {
                // Null check on result of new array must succeed
                Instruction i2 = Move.create(GUARD_MOVE, NullCheck.getClearGuardResult(inst), new TrueGuardOperand());
                DefUse.replaceInstructionAndUpdateDU(inst, i2);
            }
            break;
        case BOUNDS_CHECK_opcode:
            {
                // Remove or create trap as appropriate
                Instruction i2 = TrapIf.create(TRAP_IF, BoundsCheck.getClearGuardResult(inst), IC(size), BoundsCheck.getClearIndex(inst), ConditionOperand.LOWER_EQUAL(), TrapCodeOperand.ArrayBounds());
                DefUse.replaceInstructionAndUpdateDU(inst, i2);
            }
            break;
        case CHECKCAST_opcode:
        case CHECKCAST_NOTNULL_opcode:
        case CHECKCAST_UNRESOLVED_opcode:
            {
                // We cannot handle removing the checkcast if the result of the
                // checkcast test is unknown
                TypeReference lhsType = TypeCheck.getType(inst).getTypeRef();
                if (ClassLoaderProxy.includesType(lhsType, vmArray.getTypeRef()) == YES) {
                    if (visited == null) {
                        visited = new HashSet<Register>();
                    }
                    Register copy = TypeCheck.getResult(inst).getRegister();
                    if (!visited.contains(copy)) {
                        visited.add(copy);
                        transform2(copy, inst, scalars);
                    // NB will remove inst
                    } else {
                        DefUse.removeInstructionAndUpdateDU(inst);
                    }
                } else {
                    Instruction i2 = Trap.create(TRAP, null, TrapCodeOperand.CheckCast());
                    DefUse.replaceInstructionAndUpdateDU(inst, i2);
                }
            }
            break;
        case INSTANCEOF_opcode:
        case INSTANCEOF_NOTNULL_opcode:
        case INSTANCEOF_UNRESOLVED_opcode:
            {
                // We cannot handle removing the instanceof if the result of the
                // instanceof test is unknown
                TypeReference lhsType = InstanceOf.getType(inst).getTypeRef();
                Instruction i2;
                if (ClassLoaderProxy.includesType(lhsType, vmArray.getTypeRef()) == YES) {
                    i2 = Move.create(INT_MOVE, InstanceOf.getClearResult(inst), IC(1));
                } else {
                    i2 = Move.create(INT_MOVE, InstanceOf.getClearResult(inst), IC(0));
                }
                DefUse.replaceInstructionAndUpdateDU(inst, i2);
            }
            break;
        case GET_OBJ_TIB_opcode:
            {
                Instruction i2 = Move.create(REF_MOVE, GuardedUnary.getClearResult(inst), new TIBConstantOperand(vmArray));
                DefUse.replaceInstructionAndUpdateDU(inst, i2);
            }
            break;
        case REF_MOVE_opcode:
            {
                if (visited == null) {
                    visited = new HashSet<Register>();
                }
                Register copy = Move.getResult(inst).getRegister();
                if (!visited.contains(copy)) {
                    visited.add(copy);
                    transform2(copy, inst, scalars);
                // NB will remove inst
                } else {
                    DefUse.removeInstructionAndUpdateDU(inst);
                }
            }
            break;
        default:
            throw new OptimizingCompilerException("Unexpected instruction: " + inst);
    }
}
Also used : Operator(org.jikesrvm.compilers.opt.ir.Operator) Register(org.jikesrvm.compilers.opt.ir.Register) RegisterOperand(org.jikesrvm.compilers.opt.ir.operand.RegisterOperand) TrueGuardOperand(org.jikesrvm.compilers.opt.ir.operand.TrueGuardOperand) ConditionOperand(org.jikesrvm.compilers.opt.ir.operand.ConditionOperand) Operand(org.jikesrvm.compilers.opt.ir.operand.Operand) TIBConstantOperand(org.jikesrvm.compilers.opt.ir.operand.TIBConstantOperand) TrapCodeOperand(org.jikesrvm.compilers.opt.ir.operand.TrapCodeOperand) RVMType(org.jikesrvm.classloader.RVMType) TIBConstantOperand(org.jikesrvm.compilers.opt.ir.operand.TIBConstantOperand) TypeReference(org.jikesrvm.classloader.TypeReference) OptimizingCompilerException(org.jikesrvm.compilers.opt.OptimizingCompilerException) Instruction(org.jikesrvm.compilers.opt.ir.Instruction) TrueGuardOperand(org.jikesrvm.compilers.opt.ir.operand.TrueGuardOperand) HashSet(java.util.HashSet)

Example 13 with Operand

use of org.jikesrvm.compilers.opt.ir.operand.Operand in project JikesRVM by JikesRVM.

the class SimpleEscape method simpleEscapeAnalysis.

/**
 * Performs the escape analysis for a method.
 *
 * <p> Side effect: updates method summary database to hold
 *                escape analysis result for parameters
 *
 * @param ir IR for the target method
 * @return an object holding the result of the analysis
 */
public FI_EscapeSummary simpleEscapeAnalysis(IR ir) {
    final boolean DEBUG = false;
    if (DEBUG) {
        VM.sysWriteln("ENTER Simple Escape Analysis " + ir.method);
    }
    if (DEBUG) {
        ir.printInstructions();
    }
    // create a method summary object for this method
    RVMMethod m = ir.method;
    MethodSummary summ = SummaryDatabase.findOrCreateMethodSummary(m);
    summ.setInProgress(true);
    FI_EscapeSummary result = new FI_EscapeSummary();
    // set up register lists, SSA flags
    DefUse.computeDU(ir);
    DefUse.recomputeSSA(ir);
    // pass through registers, and mark escape information
    for (Register reg = ir.regpool.getFirstSymbolicRegister(); reg != null; reg = reg.getNext()) {
        // skip the following types of registers:
        if (reg.isFloatingPoint()) {
            continue;
        }
        if (reg.isInteger()) {
            continue;
        }
        if (reg.isLong()) {
            continue;
        }
        if (reg.isCondition()) {
            continue;
        }
        if (reg.isValidation()) {
            continue;
        }
        if (reg.isPhysical()) {
            continue;
        }
        if (!reg.isSSA()) {
            continue;
        }
        AnalysisResult escapes = checkAllAppearances(reg, ir);
        if (escapes.threadLocal) {
            result.setThreadLocal(reg, true);
        }
        if (escapes.methodLocal) {
            result.setMethodLocal(reg, true);
        }
    }
    // update the method summary database to note whether
    // parameters may escape
    int numParam = 0;
    for (Enumeration<Operand> e = ir.getParameters(); e.hasMoreElements(); numParam++) {
        Register p = ((RegisterOperand) e.nextElement()).getRegister();
        if (result.isThreadLocal(p)) {
            summ.setParameterMayEscapeThread(numParam, false);
        } else {
            summ.setParameterMayEscapeThread(numParam, true);
        }
    }
    // update the method summary to note whether the return value
    // may escape
    boolean foundEscapingReturn = false;
    for (Iterator<Operand> itr = iterateReturnValues(ir); itr.hasNext(); ) {
        Operand op = itr.next();
        if (op == null) {
            continue;
        }
        if (op.isRegister()) {
            Register r = op.asRegister().getRegister();
            if (!result.isThreadLocal(r)) {
                foundEscapingReturn = true;
            }
        }
    }
    if (!foundEscapingReturn) {
        summ.setResultMayEscapeThread(false);
    }
    // record that we're done with analysis
    summ.setInProgress(false);
    if (DEBUG) {
        VM.sysWriteln("LEAVE Simple Escape Analysis " + ir.method);
    }
    return result;
}
Also used : RVMMethod(org.jikesrvm.classloader.RVMMethod) RegisterOperand(org.jikesrvm.compilers.opt.ir.operand.RegisterOperand) Register(org.jikesrvm.compilers.opt.ir.Register) MethodOperand(org.jikesrvm.compilers.opt.ir.operand.MethodOperand) RegisterOperand(org.jikesrvm.compilers.opt.ir.operand.RegisterOperand) Operand(org.jikesrvm.compilers.opt.ir.operand.Operand)

Example 14 with Operand

use of org.jikesrvm.compilers.opt.ir.operand.Operand in project JikesRVM by JikesRVM.

the class SimpleEscape method checkEscapesThread.

/**
 * Checks a single use, to see if this use may cause the object
 * referenced to escape from this thread.
 *
 * @param use the use to check
 * @param ir the governing IR
 * @param visited visited registers
 * @return {@code true} if it may escape, {@code false} otherwise
 */
private static boolean checkEscapesThread(RegisterOperand use, IR ir, Set<Register> visited) {
    Instruction inst = use.instruction;
    switch(inst.getOpcode()) {
        case INT_ASTORE_opcode:
        case LONG_ASTORE_opcode:
        case FLOAT_ASTORE_opcode:
        case DOUBLE_ASTORE_opcode:
        case BYTE_ASTORE_opcode:
        case SHORT_ASTORE_opcode:
        case REF_ASTORE_opcode:
            // as long as we don't store this operand elsewhere, all
            // is OK
            Operand value = AStore.getValue(inst);
            return value == use;
        case GETFIELD_opcode:
        case GETSTATIC_opcode:
        case INT_ALOAD_opcode:
        case LONG_ALOAD_opcode:
        case FLOAT_ALOAD_opcode:
        case DOUBLE_ALOAD_opcode:
        case BYTE_ALOAD_opcode:
        case UBYTE_ALOAD_opcode:
        case BYTE_LOAD_opcode:
        case UBYTE_LOAD_opcode:
        case SHORT_ALOAD_opcode:
        case USHORT_ALOAD_opcode:
        case SHORT_LOAD_opcode:
        case USHORT_LOAD_opcode:
        case REF_ALOAD_opcode:
        case INT_LOAD_opcode:
        case LONG_LOAD_opcode:
        case FLOAT_LOAD_opcode:
        case DOUBLE_LOAD_opcode:
        case REF_LOAD_opcode:
            // all is OK, unless we load this register from memory
            Operand result = ResultCarrier.getResult(inst);
            return result == use;
        case PUTFIELD_opcode:
            // as long as we don't store this operand elsewhere, all
            // is OK. TODO: add more smarts.
            value = PutField.getValue(inst);
            return value == use;
        case PUTSTATIC_opcode:
            // as long as we don't store this operand elsewhere, all
            // is OK. TODO: add more smarts.
            value = PutStatic.getValue(inst);
            return value == use;
        case BYTE_STORE_opcode:
        case SHORT_STORE_opcode:
        case REF_STORE_opcode:
        case INT_STORE_opcode:
        case LONG_STORE_opcode:
        case FLOAT_STORE_opcode:
        case DOUBLE_STORE_opcode:
            // as long as we don't store this operand elsewhere, all
            // is OK. TODO: add more smarts.
            value = Store.getValue(inst);
            return value == use;
        // escape
        case BOUNDS_CHECK_opcode:
        case MONITORENTER_opcode:
        case MONITOREXIT_opcode:
        case NULL_CHECK_opcode:
        case ARRAYLENGTH_opcode:
        case REF_IFCMP_opcode:
        case INT_IFCMP_opcode:
        case IG_PATCH_POINT_opcode:
        case IG_CLASS_TEST_opcode:
        case IG_METHOD_TEST_opcode:
        case BOOLEAN_CMP_INT_opcode:
        case BOOLEAN_CMP_ADDR_opcode:
        case OBJARRAY_STORE_CHECK_opcode:
        case OBJARRAY_STORE_CHECK_NOTNULL_opcode:
        case GET_OBJ_TIB_opcode:
        case GET_TYPE_FROM_TIB_opcode:
        case NEW_opcode:
        case NEWARRAY_opcode:
        case NEWOBJMULTIARRAY_opcode:
        case NEW_UNRESOLVED_opcode:
        case NEWARRAY_UNRESOLVED_opcode:
        case INSTANCEOF_opcode:
        case INSTANCEOF_NOTNULL_opcode:
        case INSTANCEOF_UNRESOLVED_opcode:
        case MUST_IMPLEMENT_INTERFACE_opcode:
        case GET_CAUGHT_EXCEPTION_opcode:
        case IR_PROLOGUE_opcode:
            return false;
        case RETURN_opcode:
            // by caller)
            return !ir.isParameter(use);
        case CALL_opcode:
            MethodOperand mop = Call.getMethod(inst);
            if (mop == null) {
                return true;
            }
            if (!mop.hasPreciseTarget()) {
                // if we're not sure of the dynamic target, give up
                return true;
            }
            // pure methods don't let object escape
            if (mop.getTarget().isPure()) {
                return false;
            }
            // Assume non-annotated native methods let object escape
            if (mop.getTarget().isNative()) {
                return true;
            }
            // try to get a method summary for the called method
            MethodSummary summ = getMethodSummaryIfAvailable(mop.getTarget(), ir.options);
            if (summ == null) {
                // couldn't get one. assume the object escapes
                return true;
            }
            // if use is result of the call...
            if (use == Call.getResult(inst)) {
                return summ.resultMayEscapeThread();
            }
            // use is a parameter to the call.  Find out which one.
            int p = getParameterIndex(use, inst);
            return summ.parameterMayEscapeThread(p);
        case CHECKCAST_opcode:
        case CHECKCAST_NOTNULL_opcode:
        case CHECKCAST_UNRESOLVED_opcode:
        case REF_MOVE_opcode:
            {
                Register copy = ResultCarrier.getResult(inst).getRegister();
                if (!copy.isSSA()) {
                    return true;
                } else {
                    if (visited == null) {
                        visited = new HashSet<Register>();
                    }
                    visited.add(use.getRegister());
                    if (visited.contains(copy)) {
                        return false;
                    } else {
                        return checkIfUseEscapesThread(copy, ir, visited);
                    }
                }
            }
        case ATHROW_opcode:
        case PREPARE_INT_opcode:
        case PREPARE_ADDR_opcode:
        case PREPARE_LONG_opcode:
        case ATTEMPT_LONG_opcode:
        case ATTEMPT_INT_opcode:
        case ATTEMPT_ADDR_opcode:
        case INT_MOVE_opcode:
        case INT_ADD_opcode:
        case REF_ADD_opcode:
        case INT_MUL_opcode:
        case INT_DIV_opcode:
        case INT_REM_opcode:
        case INT_NEG_opcode:
        case INT_ZERO_CHECK_opcode:
        case INT_OR_opcode:
        case INT_AND_opcode:
        case INT_XOR_opcode:
        case REF_OR_opcode:
        case REF_AND_opcode:
        case REF_XOR_opcode:
        case INT_SUB_opcode:
        case REF_SUB_opcode:
        case INT_SHL_opcode:
        case INT_SHR_opcode:
        case INT_USHR_opcode:
        case SYSCALL_opcode:
        case REF_SHL_opcode:
        case REF_SHR_opcode:
        case REF_USHR_opcode:
        case SET_CAUGHT_EXCEPTION_opcode:
        case PHI_opcode:
        case INT_2LONG_opcode:
        case REF_COND_MOVE_opcode:
        case INT_COND_MOVE_opcode:
        case INT_2ADDRSigExt_opcode:
        case INT_2ADDRZerExt_opcode:
        case ADDR_2INT_opcode:
        case ADDR_2LONG_opcode:
        case LONG_OR_opcode:
        case LONG_AND_opcode:
        case LONG_XOR_opcode:
        case LONG_SUB_opcode:
        case LONG_SHL_opcode:
        case LONG_ADD_opcode:
        case LONG_SHR_opcode:
        case LONG_USHR_opcode:
        case LONG_NEG_opcode:
        case LONG_MOVE_opcode:
        case LONG_2ADDR_opcode:
        // TODO: add more smarts
        case YIELDPOINT_OSR_opcode:
            // we do not know exactly, so be conservative
            return true;
        default:
            if (VM.BuildForPowerPC) {
                switch(inst.getOpcode()) {
                    case DCBST_opcode:
                    case DCBT_opcode:
                    case DCBTST_opcode:
                    case DCBZ_opcode:
                    case DCBZL_opcode:
                    case ICBI_opcode:
                        return false;
                }
            } else {
                switch(inst.getOpcode()) {
                    case PREFETCH_opcode:
                        return false;
                }
            }
            throw new OptimizingCompilerException("SimpleEscapge: Unexpected " + inst);
    }
}
Also used : Register(org.jikesrvm.compilers.opt.ir.Register) MethodOperand(org.jikesrvm.compilers.opt.ir.operand.MethodOperand) RegisterOperand(org.jikesrvm.compilers.opt.ir.operand.RegisterOperand) Operand(org.jikesrvm.compilers.opt.ir.operand.Operand) OptimizingCompilerException(org.jikesrvm.compilers.opt.OptimizingCompilerException) Instruction(org.jikesrvm.compilers.opt.ir.Instruction) MethodOperand(org.jikesrvm.compilers.opt.ir.operand.MethodOperand) HashSet(java.util.HashSet)

Example 15 with Operand

use of org.jikesrvm.compilers.opt.ir.operand.Operand in project JikesRVM by JikesRVM.

the class LoopUnrolling method unrollLeaf.

boolean unrollLeaf(LSTNode t, IR ir) {
    int instructionsInLoop = 0;
    BasicBlock exitBlock = null, backEdgeBlock = null, succBlock = null, predBlock = null;
    BitVector nloop = t.getLoop();
    BasicBlock header = t.header;
    Instruction tmp;
    if (ir.hasReachableExceptionHandlers()) {
        report("0 IR may have exception handlers");
        return false;
    }
    // determine loop structure by looking at its blocks
    Enumeration<BasicBlock> loopBlocks = ir.getBasicBlocks(nloop);
    int blocks = 0;
    while (loopBlocks.hasMoreElements()) {
        BasicBlock b = loopBlocks.nextElement();
        blocks++;
        // check for size
        instructionsInLoop += b.getNumberOfRealInstructions();
        if (instructionsInLoop > MaxInstructions) {
            report("1 is too big");
            return false;
        }
        // look at the in edges. We want the header to be the only
        // block with out of loop incoming edges.
        Enumeration<BasicBlock> e = b.getIn();
        if (b != header) {
            while (e.hasMoreElements()) {
                BasicBlock o = e.nextElement();
                if (!CFGTransformations.inLoop(o, nloop)) {
                    report("2 interior pointers.");
                    return true;
                }
            }
        } else {
            // check the headers predecessors: there should be
            // one out of loop input and one backedge.
            // We can extend this for loops with several backedges,
            // if they all have the same conditions.
            int inEdges = 0;
            while (e.hasMoreElements()) {
                inEdges++;
                BasicBlock o = e.nextElement();
                if (!CFGTransformations.inLoop(o, nloop)) {
                    if (predBlock == null) {
                        predBlock = o;
                    } else {
                        report("3 multi entry header.");
                        return true;
                    }
                } else {
                    if (backEdgeBlock == null) {
                        backEdgeBlock = o;
                    } else {
                        report("4 multiple back edges.");
                        return true;
                    }
                }
            }
        }
        // look at the out edges to find loop exits
        e = b.getOut();
        while (e.hasMoreElements()) {
            BasicBlock out = e.nextElement();
            if (!CFGTransformations.inLoop(out, nloop)) {
                if (exitBlock == null) {
                    exitBlock = b;
                } else {
                    report("5 multiple exit blocks.");
                    return true;
                }
            }
        }
    }
    // exitBlock must equal backEdgeBlock
    if (exitBlock == null) {
        report("6 no exit block found...infinite loop?");
        return true;
    }
    if (exitBlock != backEdgeBlock) {
        report("7 exit block is not immediate predecessor of loop head");
        return true;
    }
    // exitBlock must exit (skip over pads in critical edges)
    while (exitBlock.getNumberOfOut() == 1 && exitBlock.getNumberOfIn() == 1) {
        exitBlock = exitBlock.getIn().nextElement();
    }
    if (exitBlock == header && blocks > 1) {
        report("6 while loop? (" + blocks + ")");
        return true;
    }
    // So far, so good. Examine the exit test.
    Instruction origBranch = exitBlock.firstBranchInstruction();
    if (origBranch != exitBlock.lastRealInstruction()) {
        Instruction aGoto = origBranch.nextInstructionInCodeOrder();
        if (aGoto.getOpcode() != GOTO_opcode) {
            report("7 too complex exit");
            return true;
        }
        succBlock = Label.getBlock(Goto.getTarget(aGoto).target).block;
        if (VM.VerifyAssertions) {
            VM._assert(aGoto == exitBlock.lastRealInstruction());
        }
    } else {
        succBlock = exitBlock.getFallThroughBlock();
    }
    if (origBranch.getOpcode() != INT_IFCMP_opcode) {
        report("8 branch isn't int_ifcmp: " + origBranch.operator() + ".");
        return true;
    }
    // examine operands:
    Operand op1 = follow(IfCmp.getVal1(origBranch));
    Operand op2 = follow(IfCmp.getVal2(origBranch));
    ConditionOperand cond = (ConditionOperand) IfCmp.getCond(origBranch).copy();
    RegisterOperand ifcmpGuard = IfCmp.getGuardResult(origBranch);
    float backBranchProbability = IfCmp.getBranchProfile(origBranch).takenProbability;
    if (!loopInvariant(op2, nloop, 4)) {
        if (loopInvariant(op1, nloop, 4)) {
            Operand op = op1;
            op1 = op2;
            op2 = op;
            cond.flipOperands();
        } else {
            if (DEBUG) {
                printDefs(op1, nloop, 4);
                printDefs(op2, nloop, 4);
                VM.sysWriteln(origBranch.toString());
            }
            report("8a op1 and op2 may not be loop invariant");
            return true;
        }
    }
    BasicBlock target = Label.getBlock(IfCmp.getTarget(origBranch).target).block;
    if (!(op1 instanceof RegisterOperand)) {
        report("9 op1 of ifcmp isn't a register");
        return true;
    }
    RegisterOperand rop1 = (RegisterOperand) op1;
    Register reg = rop1.getRegister();
    if (reg.isPhysical()) {
        report("10 loops over physical register");
        return false;
    }
    if (succBlock == header && !CFGTransformations.inLoop(target, nloop)) {
        succBlock = target;
        target = header;
        cond.flipCode();
    }
    if (target != header) {
        report("11 ifcmp doesn't jump to header");
        return true;
    }
    Instruction iterator = null;
    Enumeration<Operand> defs = new RealDefs(rop1);
    while (defs.hasMoreElements()) {
        Operand def = defs.nextElement();
        Instruction inst = def.instruction;
        BasicBlock block = inst.getBasicBlock();
        // VM.sysWriteln(block + ": " + inst);
        if (CFGTransformations.inLoop(block, nloop)) {
            if (iterator == null) {
                iterator = inst;
            } else {
                report("12 iterator not unique.");
                return true;
            }
        }
    }
    if (iterator == null) {
        report("15 iterator not found.");
        return true;
    }
    if (iterator.getOpcode() != INT_ADD_opcode) {
        // dumpIR (ir, "malformed");
        report("16 iterator is no addition: " + iterator.operator());
        return true;
    }
    if (!rop1.similar(follow(Binary.getVal1(iterator)))) {
        // dumpIR (ir, "malformed");
        report("17 malformed iterator.\n" + iterator);
        return true;
    }
    Operand strideOp = follow(Binary.getVal2(iterator));
    if (!(strideOp instanceof IntConstantOperand)) {
        report("18 stride not constant");
        return true;
    }
    int stride = ((IntConstantOperand) strideOp).value;
    if (stride != 1 && stride != -1) {
        report("18b stride != +/-1 (" + stride + ")");
        return true;
    }
    if ((stride == 1 && ((cond.value != ConditionOperand.LESS) && cond.value != ConditionOperand.LESS_EQUAL && cond.value != ConditionOperand.NOT_EQUAL)) || (stride == -1 && ((cond.value != ConditionOperand.GREATER) && cond.value != ConditionOperand.GREATER_EQUAL && cond.value != ConditionOperand.NOT_EQUAL))) {
        report("19 unexpected condition: " + cond + "\n" + iterator + "\n" + origBranch);
        return true;
    }
    RegisterOperand outerGuard;
    BasicBlock outer = predBlock;
    while (outer.getNumberOfOut() == 1 && outer.getNumberOfIn() == 1) {
        outer = outer.getIn().nextElement();
    }
    if (outer.getNumberOfIn() > 0 && outer.getNumberOfOut() < 2) {
        report("23 no suitable outer guard found.");
        return true;
    }
    tmp = outer.firstBranchInstruction();
    if (tmp != null && GuardResultCarrier.conforms(tmp)) {
        outerGuard = GuardResultCarrier.getGuardResult(tmp);
    } else {
        outerGuard = ir.regpool.makeTempValidation();
    }
    // //////////
    // transfom
    // transform this:
    // 
    // Orig:
    // B
    // if i CC b goto Orig
    // else goto exit
    // 
    // exit:
    // 
    // into this:
    // 
    // 
    // stride == 1:           common:                      stride == -1:
    // --------------------------------------------------------------------------
    // guard0:
    // limit = b;
    // if a > b goto Orig                                  if b > a goto Orig
    // else guard1
    // 
    // 
    // guard 1:
    // remainder = b - a;                                  remainder = a - b;
    // if cond == '<='                                    if cond == '>='
    // remainder++;                                         remainder++;
    // remainder = remainder & 3
    // limit = a + remainder                               limit = a - remainder
    // if cond == '<='                                    if cond == '>='
    // limit--;                                            limit++;
    // if remainder == 0 goto mllp
    // goto Orig
    // 
    // Orig:
    // LOOP;
    // if i CC limit goto Orig
    // else guard2
    // 
    // guard2: if i CC b goto mllp
    // else exit
    // 
    // mllp: // landing pad
    // goto ml
    // 
    // ml:
    // LOOP;LOOP;LOOP;LOOP;
    // if i CC b goto ml
    // else exit
    // 
    // exit:
    // --------------------------------------------------------------------------
    report("...transforming.");
    if (DEBUG && ir.options.hasMETHOD_TO_PRINT() && ir.options.fuzzyMatchMETHOD_TO_PRINT(ir.method.toString())) {
        dumpIR(ir, "before unroll");
    }
    CFGTransformations.killFallThroughs(ir, nloop);
    BasicBlock[] handles = makeSomeCopies(unrollFactor, ir, nloop, blocks, header, exitBlock, exitBlock);
    BasicBlock mainHeader = handles[0];
    BasicBlock mainExit = handles[1];
    // test block for well formed bounds
    BasicBlock guardBlock0 = header.createSubBlock(header.firstInstruction().getBytecodeIndex(), ir);
    predBlock.redirectOuts(header, guardBlock0, ir);
    // test block for iteration alignemnt
    BasicBlock guardBlock1 = header.createSubBlock(header.firstInstruction().getBytecodeIndex(), ir);
    // landing pad for orig loop
    BasicBlock olp = header.createSubBlock(header.firstInstruction().getBytecodeIndex(), ir);
    olp.setLandingPad();
    BasicBlock predSucc = predBlock.nextBasicBlockInCodeOrder();
    if (predSucc != null) {
        ir.cfg.breakCodeOrder(predBlock, predSucc);
        ir.cfg.linkInCodeOrder(olp, predSucc);
    }
    ir.cfg.linkInCodeOrder(predBlock, guardBlock0);
    ir.cfg.linkInCodeOrder(guardBlock0, guardBlock1);
    ir.cfg.linkInCodeOrder(guardBlock1, olp);
    // guard block for main loop
    BasicBlock guardBlock2 = header.createSubBlock(header.firstInstruction().getBytecodeIndex(), ir);
    // landing pad for main loop
    BasicBlock landingPad = header.createSubBlock(header.firstInstruction().getBytecodeIndex(), ir);
    landingPad.setLandingPad();
    BasicBlock mainLoop = exitBlock.nextBasicBlockInCodeOrder();
    ir.cfg.breakCodeOrder(exitBlock, mainLoop);
    ir.cfg.linkInCodeOrder(exitBlock, guardBlock2);
    ir.cfg.linkInCodeOrder(guardBlock2, landingPad);
    ir.cfg.linkInCodeOrder(landingPad, mainLoop);
    RegisterOperand remainder = ir.regpool.makeTemp(rop1.getType());
    RegisterOperand limit = ir.regpool.makeTemp(rop1.getType());
    // test whether a <= b for stride == 1 and a >= b for stride == -1
    tmp = guardBlock0.lastInstruction();
    tmp.insertBefore(Move.create(INT_MOVE, limit, op2.copy()));
    ConditionOperand g0cond = ConditionOperand.GREATER_EQUAL();
    if (stride == -1)
        g0cond = ConditionOperand.LESS_EQUAL();
    tmp.insertBefore(IfCmp.create(INT_IFCMP, outerGuard.copyD2D(), rop1.copyD2U(), op2.copy(), g0cond, olp.makeJumpTarget(), BranchProfileOperand.unlikely()));
    tmp.insertBefore(Goto.create(GOTO, guardBlock1.makeJumpTarget()));
    // align the loop iterations
    tmp = guardBlock1.lastInstruction();
    if (stride == 1) {
        tmp.insertBefore(Binary.create(INT_SUB, remainder, op2.copy(), rop1.copyD2U()));
    } else {
        tmp.insertBefore(Binary.create(INT_SUB, remainder, rop1.copyD2U(), op2.copy()));
    }
    if (cond.isGREATER_EQUAL() || cond.isLESS_EQUAL()) {
        tmp.insertBefore(Binary.create(INT_ADD, remainder.copyD2D(), remainder.copyD2U(), new IntConstantOperand(1)));
    }
    tmp.insertBefore(Binary.create(INT_ADD, remainder.copyD2D(), remainder.copyD2U(), new IntConstantOperand(-1)));
    tmp.insertBefore(Binary.create(INT_AND, remainder.copyD2D(), remainder.copyD2U(), new IntConstantOperand(unrollFactor - 1)));
    tmp.insertBefore(Binary.create(INT_ADD, remainder.copyD2D(), remainder.copyD2U(), new IntConstantOperand(1)));
    if (stride == 1) {
        tmp.insertBefore(Binary.create(INT_ADD, limit.copyD2U(), op1.copy(), remainder.copyD2U()));
    } else {
        tmp.insertBefore(Binary.create(INT_SUB, limit.copyD2U(), op1.copy(), remainder.copyD2U()));
    }
    if (cond.isLESS_EQUAL()) {
        tmp.insertBefore(Binary.create(INT_ADD, limit.copyD2D(), limit.copyD2U(), new IntConstantOperand(-1)));
    }
    if (cond.isGREATER_EQUAL()) {
        tmp.insertBefore(Binary.create(INT_ADD, limit.copyD2D(), limit.copyD2U(), new IntConstantOperand(1)));
    }
    tmp.insertBefore(Goto.create(GOTO, olp.makeJumpTarget()));
    // build landing pad for original loop
    tmp = olp.lastInstruction();
    tmp.insertBefore(Goto.create(GOTO, header.makeJumpTarget()));
    // change the back branch in the original loop
    deleteBranches(exitBlock);
    tmp = exitBlock.lastInstruction();
    tmp.insertBefore(IfCmp.create(INT_IFCMP, outerGuard.copyD2D(), rop1.copyU2U(), limit.copyD2U(), (ConditionOperand) cond.copy(), header.makeJumpTarget(), new BranchProfileOperand(1.0f - 1.0f / (unrollFactor / 2))));
    tmp.insertBefore(Goto.create(GOTO, guardBlock2.makeJumpTarget()));
    // only enter main loop if iterations left
    tmp = guardBlock2.lastInstruction();
    tmp.insertBefore(IfCmp.create(INT_IFCMP, outerGuard.copyD2D(), rop1.copyU2U(), op2.copy(), (ConditionOperand) cond.copy(), landingPad.makeJumpTarget(), new BranchProfileOperand(backBranchProbability)));
    tmp.insertBefore(Goto.create(GOTO, succBlock.makeJumpTarget()));
    // landing pad jumps to mainHeader
    tmp = landingPad.lastInstruction();
    tmp.insertBefore(Goto.create(GOTO, mainHeader.makeJumpTarget()));
    // repair back edge in mainExit
    if (VM.VerifyAssertions)
        VM._assert(mainExit != null);
    tmp = mainExit.lastInstruction();
    if (VM.VerifyAssertions) {
        VM._assert((mainExit.lastRealInstruction() == null) || !mainExit.lastRealInstruction().isBranch());
    }
    tmp.insertBefore(IfCmp.create(INT_IFCMP, ifcmpGuard.copyU2U(), rop1.copyU2U(), op2.copy(), (ConditionOperand) cond.copy(), mainHeader.makeJumpTarget(), new BranchProfileOperand(1.0f - (1.0f - backBranchProbability) * unrollFactor)));
    tmp.insertBefore(Goto.create(GOTO, succBlock.makeJumpTarget()));
    // recompute normal outs
    guardBlock0.recomputeNormalOut(ir);
    guardBlock1.recomputeNormalOut(ir);
    olp.recomputeNormalOut(ir);
    guardBlock2.recomputeNormalOut(ir);
    exitBlock.recomputeNormalOut(ir);
    landingPad.recomputeNormalOut(ir);
    mainExit.recomputeNormalOut(ir);
    if (DEBUG && ir.options.hasMETHOD_TO_PRINT() && ir.options.fuzzyMatchMETHOD_TO_PRINT(ir.method.toString())) {
        dumpIR(ir, "after unroll");
    }
    return false;
}
Also used : BitVector(org.jikesrvm.util.BitVector) IntConstantOperand(org.jikesrvm.compilers.opt.ir.operand.IntConstantOperand) BranchProfileOperand(org.jikesrvm.compilers.opt.ir.operand.BranchProfileOperand) RegisterOperand(org.jikesrvm.compilers.opt.ir.operand.RegisterOperand) ConditionOperand(org.jikesrvm.compilers.opt.ir.operand.ConditionOperand) Operand(org.jikesrvm.compilers.opt.ir.operand.Operand) IntConstantOperand(org.jikesrvm.compilers.opt.ir.operand.IntConstantOperand) ConstantOperand(org.jikesrvm.compilers.opt.ir.operand.ConstantOperand) ExceptionHandlerBasicBlock(org.jikesrvm.compilers.opt.ir.ExceptionHandlerBasicBlock) BasicBlock(org.jikesrvm.compilers.opt.ir.BasicBlock) ConditionOperand(org.jikesrvm.compilers.opt.ir.operand.ConditionOperand) Instruction(org.jikesrvm.compilers.opt.ir.Instruction) RegisterOperand(org.jikesrvm.compilers.opt.ir.operand.RegisterOperand) Register(org.jikesrvm.compilers.opt.ir.Register) BranchProfileOperand(org.jikesrvm.compilers.opt.ir.operand.BranchProfileOperand)

Aggregations

Operand (org.jikesrvm.compilers.opt.ir.operand.Operand)355 RegisterOperand (org.jikesrvm.compilers.opt.ir.operand.RegisterOperand)328 IntConstantOperand (org.jikesrvm.compilers.opt.ir.operand.IntConstantOperand)242 ConditionOperand (org.jikesrvm.compilers.opt.ir.operand.ConditionOperand)217 BranchProfileOperand (org.jikesrvm.compilers.opt.ir.operand.BranchProfileOperand)212 TrueGuardOperand (org.jikesrvm.compilers.opt.ir.operand.TrueGuardOperand)210 MethodOperand (org.jikesrvm.compilers.opt.ir.operand.MethodOperand)207 TrapCodeOperand (org.jikesrvm.compilers.opt.ir.operand.TrapCodeOperand)185 LongConstantOperand (org.jikesrvm.compilers.opt.ir.operand.LongConstantOperand)174 ConstantOperand (org.jikesrvm.compilers.opt.ir.operand.ConstantOperand)165 TypeOperand (org.jikesrvm.compilers.opt.ir.operand.TypeOperand)153 Instruction (org.jikesrvm.compilers.opt.ir.Instruction)144 AddressConstantOperand (org.jikesrvm.compilers.opt.ir.operand.AddressConstantOperand)143 NullConstantOperand (org.jikesrvm.compilers.opt.ir.operand.NullConstantOperand)141 ObjectConstantOperand (org.jikesrvm.compilers.opt.ir.operand.ObjectConstantOperand)128 TIBConstantOperand (org.jikesrvm.compilers.opt.ir.operand.TIBConstantOperand)121 UnreachableOperand (org.jikesrvm.compilers.opt.ir.operand.UnreachableOperand)117 LocationOperand (org.jikesrvm.compilers.opt.ir.operand.LocationOperand)102 CodeConstantOperand (org.jikesrvm.compilers.opt.ir.operand.CodeConstantOperand)98 Register (org.jikesrvm.compilers.opt.ir.Register)82