Example #1
0
/// AddRegisterOperand - Add the specified register as an operand to the
/// specified machine instr. Insert register copies if the register is
/// not in the required register class.
void
InstrEmitter::AddRegisterOperand(MachineInstrBuilder &MIB,
                                 SDValue Op,
                                 unsigned IIOpNum,
                                 const MCInstrDesc *II,
                                 DenseMap<SDValue, unsigned> &VRBaseMap,
                                 bool IsDebug, bool IsClone, bool IsCloned) {
    assert(Op.getValueType() != MVT::Other &&
           Op.getValueType() != MVT::Glue &&
           "Chain and glue operands should occur at end of operand list!");
    // Get/emit the operand.
    unsigned VReg = getVR(Op, VRBaseMap);
    assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");

    const MCInstrDesc &MCID = MIB->getDesc();
    bool isOptDef = IIOpNum < MCID.getNumOperands() &&
                    MCID.OpInfo[IIOpNum].isOptionalDef();

    // If the instruction requires a register in a different class, create
    // a new virtual register and copy the value into it, but first attempt to
    // shrink VReg's register class within reason.  For example, if VReg == GR32
    // and II requires a GR32_NOSP, just constrain VReg to GR32_NOSP.
    if (II) {
        const TargetRegisterClass *DstRC = nullptr;
        if (IIOpNum < II->getNumOperands())
            DstRC = TRI->getAllocatableClass(TII->getRegClass(*II,IIOpNum,TRI,*MF));
        if (DstRC && !MRI->constrainRegClass(VReg, DstRC, MinRCSize)) {
            unsigned NewVReg = MRI->createVirtualRegister(DstRC);
            BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(),
                    TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg);
            VReg = NewVReg;
        }
    }

    // If this value has only one use, that use is a kill. This is a
    // conservative approximation. InstrEmitter does trivial coalescing
    // with CopyFromReg nodes, so don't emit kill flags for them.
    // Avoid kill flags on Schedule cloned nodes, since there will be
    // multiple uses.
    // Tied operands are never killed, so we need to check that. And that
    // means we need to determine the index of the operand.
    bool isKill = Op.hasOneUse() &&
                  Op.getNode()->getOpcode() != ISD::CopyFromReg &&
                  !IsDebug &&
                  !(IsClone || IsCloned);
    if (isKill) {
        unsigned Idx = MIB->getNumOperands();
        while (Idx > 0 &&
                MIB->getOperand(Idx-1).isReg() &&
                MIB->getOperand(Idx-1).isImplicit())
            --Idx;
        bool isTied = MCID.getOperandConstraint(Idx, MCOI::TIED_TO) != -1;
        if (isTied)
            isKill = false;
    }

    MIB.addReg(VReg, getDefRegState(isOptDef) | getKillRegState(isKill) |
               getDebugRegState(IsDebug));
}