Skip to content

Commit 11f7682

Browse files
committed
MachineInstrBuilder: Introduce copyMIMetadata() function.
This reduces the amount of boilerplate required when adding a new field to MIMetadata and reduces the chance of bugs like the one I fixed in TargetInstrInfo::reassociateOps. Pull Request: llvm#133535
1 parent 8401ab8 commit 11f7682

File tree

2 files changed

+40
-54
lines changed

2 files changed

+40
-54
lines changed

llvm/include/llvm/CodeGen/MachineInstrBuilder.h

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ enum {
6969

7070
} // end namespace RegState
7171

72+
/// Set of metadata that should be preserved when using BuildMI(). This provides
73+
/// a more convenient way of preserving DebugLoc, PCSections and MMRA.
74+
class MIMetadata {
75+
public:
76+
MIMetadata() = default;
77+
MIMetadata(DebugLoc DL, MDNode *PCSections = nullptr, MDNode *MMRA = nullptr)
78+
: DL(std::move(DL)), PCSections(PCSections), MMRA(MMRA) {}
79+
MIMetadata(const DILocation *DI, MDNode *PCSections = nullptr,
80+
MDNode *MMRA = nullptr)
81+
: DL(DI), PCSections(PCSections), MMRA(MMRA) {}
82+
explicit MIMetadata(const Instruction &From)
83+
: DL(From.getDebugLoc()),
84+
PCSections(From.getMetadata(LLVMContext::MD_pcsections)) {}
85+
explicit MIMetadata(const MachineInstr &From)
86+
: DL(From.getDebugLoc()), PCSections(From.getPCSections()) {}
87+
88+
const DebugLoc &getDL() const { return DL; }
89+
MDNode *getPCSections() const { return PCSections; }
90+
MDNode *getMMRAMetadata() const { return MMRA; }
91+
92+
private:
93+
DebugLoc DL;
94+
MDNode *PCSections = nullptr;
95+
MDNode *MMRA = nullptr;
96+
};
97+
7298
class MachineInstrBuilder {
7399
MachineFunction *MF = nullptr;
74100
MachineInstr *MI = nullptr;
@@ -317,15 +343,11 @@ class MachineInstrBuilder {
317343
}
318344
}
319345

320-
const MachineInstrBuilder &setPCSections(MDNode *MD) const {
321-
if (MD)
322-
MI->setPCSections(*MF, MD);
323-
return *this;
324-
}
325-
326-
const MachineInstrBuilder &setMMRAMetadata(MDNode *MMRA) const {
327-
if (MMRA)
328-
MI->setMMRAMetadata(*MF, MMRA);
346+
const MachineInstrBuilder &copyMIMetadata(const MIMetadata &MIMD) const {
347+
if (MIMD.getPCSections())
348+
MI->setPCSections(*MF, MIMD.getPCSections());
349+
if (MIMD.getMMRAMetadata())
350+
MI->setMMRAMetadata(*MF, MIMD.getMMRAMetadata());
329351
return *this;
330352
}
331353

@@ -343,47 +365,19 @@ class MachineInstrBuilder {
343365
}
344366
};
345367

346-
/// Set of metadata that should be preserved when using BuildMI(). This provides
347-
/// a more convenient way of preserving DebugLoc, PCSections and MMRA.
348-
class MIMetadata {
349-
public:
350-
MIMetadata() = default;
351-
MIMetadata(DebugLoc DL, MDNode *PCSections = nullptr, MDNode *MMRA = nullptr)
352-
: DL(std::move(DL)), PCSections(PCSections), MMRA(MMRA) {}
353-
MIMetadata(const DILocation *DI, MDNode *PCSections = nullptr,
354-
MDNode *MMRA = nullptr)
355-
: DL(DI), PCSections(PCSections), MMRA(MMRA) {}
356-
explicit MIMetadata(const Instruction &From)
357-
: DL(From.getDebugLoc()),
358-
PCSections(From.getMetadata(LLVMContext::MD_pcsections)) {}
359-
explicit MIMetadata(const MachineInstr &From)
360-
: DL(From.getDebugLoc()), PCSections(From.getPCSections()) {}
361-
362-
const DebugLoc &getDL() const { return DL; }
363-
MDNode *getPCSections() const { return PCSections; }
364-
MDNode *getMMRAMetadata() const { return MMRA; }
365-
366-
private:
367-
DebugLoc DL;
368-
MDNode *PCSections = nullptr;
369-
MDNode *MMRA = nullptr;
370-
};
371-
372368
/// Builder interface. Specify how to create the initial instruction itself.
373369
inline MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD,
374370
const MCInstrDesc &MCID) {
375371
return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, MIMD.getDL()))
376-
.setPCSections(MIMD.getPCSections())
377-
.setMMRAMetadata(MIMD.getMMRAMetadata());
372+
.copyMIMetadata(MIMD);
378373
}
379374

380375
/// This version of the builder sets up the first operand as a
381376
/// destination virtual register.
382377
inline MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD,
383378
const MCInstrDesc &MCID, Register DestReg) {
384379
return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, MIMD.getDL()))
385-
.setPCSections(MIMD.getPCSections())
386-
.setMMRAMetadata(MIMD.getMMRAMetadata())
380+
.copyMIMetadata(MIMD)
387381
.addReg(DestReg, RegState::Define);
388382
}
389383

@@ -397,10 +391,8 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
397391
MachineFunction &MF = *BB.getParent();
398392
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
399393
BB.insert(I, MI);
400-
return MachineInstrBuilder(MF, MI)
401-
.setPCSections(MIMD.getPCSections())
402-
.setMMRAMetadata(MIMD.getMMRAMetadata())
403-
.addReg(DestReg, RegState::Define);
394+
return MachineInstrBuilder(MF, MI).copyMIMetadata(MIMD).addReg(
395+
DestReg, RegState::Define);
404396
}
405397

406398
/// This version of the builder inserts the newly-built instruction before
@@ -416,10 +408,8 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
416408
MachineFunction &MF = *BB.getParent();
417409
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
418410
BB.insert(I, MI);
419-
return MachineInstrBuilder(MF, MI)
420-
.setPCSections(MIMD.getPCSections())
421-
.setMMRAMetadata(MIMD.getMMRAMetadata())
422-
.addReg(DestReg, RegState::Define);
411+
return MachineInstrBuilder(MF, MI).copyMIMetadata(MIMD).addReg(
412+
DestReg, RegState::Define);
423413
}
424414

425415
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
@@ -449,9 +439,7 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
449439
MachineFunction &MF = *BB.getParent();
450440
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
451441
BB.insert(I, MI);
452-
return MachineInstrBuilder(MF, MI)
453-
.setPCSections(MIMD.getPCSections())
454-
.setMMRAMetadata(MIMD.getMMRAMetadata());
442+
return MachineInstrBuilder(MF, MI).copyMIMetadata(MIMD);
455443
}
456444

457445
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
@@ -461,9 +449,7 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
461449
MachineFunction &MF = *BB.getParent();
462450
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
463451
BB.insert(I, MI);
464-
return MachineInstrBuilder(MF, MI)
465-
.setPCSections(MIMD.getPCSections())
466-
.setMMRAMetadata(MIMD.getMMRAMetadata());
452+
return MachineInstrBuilder(MF, MI).copyMIMetadata(MIMD);
467453
}
468454

469455
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,

llvm/lib/CodeGen/TargetInstrInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ void TargetInstrInfo::reassociateOps(
13871387
const MCInstrDesc &MCID, Register DestReg) {
13881388
return MachineInstrBuilder(
13891389
MF, MF.CreateMachineInstr(MCID, MIMD.getDL(), /*NoImpl=*/true))
1390-
.setPCSections(MIMD.getPCSections())
1390+
.copyMIMetadata(MIMD)
13911391
.addReg(DestReg, RegState::Define);
13921392
};
13931393

0 commit comments

Comments
 (0)