Skip to content

MachineInstrBuilder: Introduce copyMIMetadata() function. #133535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: users/pcc/spr/main.machineinstrbuilder-introduce-copymimetadata-function
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 39 additions & 53 deletions llvm/include/llvm/CodeGen/MachineInstrBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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 @@ -316,15 +342,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 @@ -342,47 +364,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 @@ -396,10 +390,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 @@ -415,10 +407,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 @@ -448,9 +438,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 @@ -460,9 +448,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 @@ -1380,7 +1380,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