Index: llvm/include/llvm/CodeGen/TargetInstrInfo.h =================================================================== --- llvm/include/llvm/CodeGen/TargetInstrInfo.h +++ llvm/include/llvm/CodeGen/TargetInstrInfo.h @@ -1064,6 +1064,14 @@ /// has the potential of causing nasty silent breakage in out-of-tree targets. virtual bool isSubregFoldable() const { return false; } + /// For given MI instruction returns the range of operands indices. Outside + /// of this range [first, second) memory operands are foldable. + /// The answer is conservative. Returning range might contain foldable memory + /// operands while operands outside of range does not contain non-foldable + /// memory operands. + virtual std::pair + getNonFoldableRange(const MachineInstr &MI) const; + /// Attempt to fold a load or store of the specified stack /// slot into the specified machine instruction for the specified operand(s). /// If this is possible, a new instruction is returned with the specified Index: llvm/lib/CodeGen/TargetInstrInfo.cpp =================================================================== --- llvm/lib/CodeGen/TargetInstrInfo.cpp +++ llvm/lib/CodeGen/TargetInstrInfo.cpp @@ -474,9 +474,8 @@ MCInst TargetInstrInfo::getNop() const { llvm_unreachable("Not implemented"); } -static MachineInstr *foldPatchpoint(MachineFunction &MF, MachineInstr &MI, - ArrayRef Ops, int FrameIndex, - const TargetInstrInfo &TII) { +std::pair +TargetInstrInfo::getNonFoldableRange(const MachineInstr &MI) const { unsigned StartIdx = 0; unsigned NumDefs = 0; switch (MI.getOpcode()) { @@ -498,8 +497,22 @@ break; } default: - llvm_unreachable("unexpected stackmap opcode"); + StartIdx = MI.getNumOperands(); + break; } + return std::make_pair(NumDefs, StartIdx); +} + +static MachineInstr *foldPatchpoint(MachineFunction &MF, MachineInstr &MI, + ArrayRef Ops, int FrameIndex, + const TargetInstrInfo &TII) { + unsigned StartIdx = 0; + unsigned NumDefs = 0; + assert((MI.getOpcode() == TargetOpcode::STACKMAP || + MI.getOpcode() == TargetOpcode::PATCHPOINT || + MI.getOpcode() == TargetOpcode::STATEPOINT) && + "Unexpected stackmap opcode"); + std::tie(NumDefs, StartIdx) = TII.getNonFoldableRange(MI); unsigned DefToFoldIdx = MI.getNumOperands();