Skip to content
92 changes: 39 additions & 53 deletions llvm/include/llvm/CodeGen/MachineInstrBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,32 @@ enum {

} // end namespace RegState

/// Set of metadata that should be preserved when using BuildMI(). This provides
/// a more convenient way of preserving DebugLoc, PCSections and MMRA.
class MIMetadata {
public:
MIMetadata() = default;
MIMetadata(DebugLoc DL, MDNode *PCSections = nullptr, MDNode *MMRA = nullptr)
: DL(std::move(DL)), PCSections(PCSections), MMRA(MMRA) {}
MIMetadata(const DILocation *DI, MDNode *PCSections = nullptr,
MDNode *MMRA = nullptr)
: DL(DI), PCSections(PCSections), MMRA(MMRA) {}
explicit MIMetadata(const Instruction &From)
: DL(From.getDebugLoc()),
PCSections(From.getMetadata(LLVMContext::MD_pcsections)) {}
explicit MIMetadata(const MachineInstr &From)
: DL(From.getDebugLoc()), PCSections(From.getPCSections()) {}

const DebugLoc &getDL() const { return DL; }
MDNode *getPCSections() const { return PCSections; }
MDNode *getMMRAMetadata() const { return MMRA; }

private:
DebugLoc DL;
MDNode *PCSections = nullptr;
MDNode *MMRA = nullptr;
};

class MachineInstrBuilder {
MachineFunction *MF = nullptr;
MachineInstr *MI = nullptr;
Expand Down Expand Up @@ -317,15 +343,11 @@ class MachineInstrBuilder {
}
}

const MachineInstrBuilder &setPCSections(MDNode *MD) const {
if (MD)
MI->setPCSections(*MF, MD);
return *this;
}

const MachineInstrBuilder &setMMRAMetadata(MDNode *MMRA) const {
if (MMRA)
MI->setMMRAMetadata(*MF, MMRA);
const MachineInstrBuilder &copyMIMetadata(const MIMetadata &MIMD) const {
if (MIMD.getPCSections())
MI->setPCSections(*MF, MIMD.getPCSections());
if (MIMD.getMMRAMetadata())
MI->setMMRAMetadata(*MF, MIMD.getMMRAMetadata());
return *this;
}

Expand All @@ -343,47 +365,19 @@ class MachineInstrBuilder {
}
};

/// Set of metadata that should be preserved when using BuildMI(). This provides
/// a more convenient way of preserving DebugLoc, PCSections and MMRA.
class MIMetadata {
public:
MIMetadata() = default;
MIMetadata(DebugLoc DL, MDNode *PCSections = nullptr, MDNode *MMRA = nullptr)
: DL(std::move(DL)), PCSections(PCSections), MMRA(MMRA) {}
MIMetadata(const DILocation *DI, MDNode *PCSections = nullptr,
MDNode *MMRA = nullptr)
: DL(DI), PCSections(PCSections), MMRA(MMRA) {}
explicit MIMetadata(const Instruction &From)
: DL(From.getDebugLoc()),
PCSections(From.getMetadata(LLVMContext::MD_pcsections)) {}
explicit MIMetadata(const MachineInstr &From)
: DL(From.getDebugLoc()), PCSections(From.getPCSections()) {}

const DebugLoc &getDL() const { return DL; }
MDNode *getPCSections() const { return PCSections; }
MDNode *getMMRAMetadata() const { return MMRA; }

private:
DebugLoc DL;
MDNode *PCSections = nullptr;
MDNode *MMRA = nullptr;
};

/// Builder interface. Specify how to create the initial instruction itself.
inline MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD,
const MCInstrDesc &MCID) {
return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, MIMD.getDL()))
.setPCSections(MIMD.getPCSections())
.setMMRAMetadata(MIMD.getMMRAMetadata());
.copyMIMetadata(MIMD);
}

/// This version of the builder sets up the first operand as a
/// destination virtual register.
inline MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD,
const MCInstrDesc &MCID, Register DestReg) {
return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, MIMD.getDL()))
.setPCSections(MIMD.getPCSections())
.setMMRAMetadata(MIMD.getMMRAMetadata())
.copyMIMetadata(MIMD)
.addReg(DestReg, RegState::Define);
}

Expand All @@ -397,10 +391,8 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
MachineFunction &MF = *BB.getParent();
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
BB.insert(I, MI);
return MachineInstrBuilder(MF, MI)
.setPCSections(MIMD.getPCSections())
.setMMRAMetadata(MIMD.getMMRAMetadata())
.addReg(DestReg, RegState::Define);
return MachineInstrBuilder(MF, MI).copyMIMetadata(MIMD).addReg(
DestReg, RegState::Define);
}

/// This version of the builder inserts the newly-built instruction before
Expand All @@ -416,10 +408,8 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
MachineFunction &MF = *BB.getParent();
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
BB.insert(I, MI);
return MachineInstrBuilder(MF, MI)
.setPCSections(MIMD.getPCSections())
.setMMRAMetadata(MIMD.getMMRAMetadata())
.addReg(DestReg, RegState::Define);
return MachineInstrBuilder(MF, MI).copyMIMetadata(MIMD).addReg(
DestReg, RegState::Define);
}

inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
Expand Down Expand Up @@ -449,9 +439,7 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
MachineFunction &MF = *BB.getParent();
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
BB.insert(I, MI);
return MachineInstrBuilder(MF, MI)
.setPCSections(MIMD.getPCSections())
.setMMRAMetadata(MIMD.getMMRAMetadata());
return MachineInstrBuilder(MF, MI).copyMIMetadata(MIMD);
}

inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
Expand All @@ -461,9 +449,7 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
MachineFunction &MF = *BB.getParent();
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
BB.insert(I, MI);
return MachineInstrBuilder(MF, MI)
.setPCSections(MIMD.getPCSections())
.setMMRAMetadata(MIMD.getMMRAMetadata());
return MachineInstrBuilder(MF, MI).copyMIMetadata(MIMD);
}

inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/TargetInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ void TargetInstrInfo::reassociateOps(
const MCInstrDesc &MCID, Register DestReg) {
return MachineInstrBuilder(
MF, MF.CreateMachineInstr(MCID, MIMD.getDL(), /*NoImpl=*/true))
.setPCSections(MIMD.getPCSections())
.copyMIMetadata(MIMD)
.addReg(DestReg, RegState::Define);
};

Expand Down
Loading