Skip to content

Commit 85a1228

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 646aaef commit 85a1228

File tree

2 files changed

+38
-48
lines changed

2 files changed

+38
-48
lines changed

llvm/include/llvm/CodeGen/MachineInstrBuilder.h

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ enum {
6868

6969
} // end namespace RegState
7070

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

@@ -398,8 +392,7 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
398392
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
399393
BB.insert(I, MI);
400394
return MachineInstrBuilder(MF, MI)
401-
.setPCSections(MIMD.getPCSections())
402-
.setMMRAMetadata(MIMD.getMMRAMetadata())
395+
.copyMIMetadata(MIMD)
403396
.addReg(DestReg, RegState::Define);
404397
}
405398

@@ -417,8 +410,7 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
417410
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
418411
BB.insert(I, MI);
419412
return MachineInstrBuilder(MF, MI)
420-
.setPCSections(MIMD.getPCSections())
421-
.setMMRAMetadata(MIMD.getMMRAMetadata())
413+
.copyMIMetadata(MIMD)
422414
.addReg(DestReg, RegState::Define);
423415
}
424416

@@ -450,8 +442,7 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
450442
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
451443
BB.insert(I, MI);
452444
return MachineInstrBuilder(MF, MI)
453-
.setPCSections(MIMD.getPCSections())
454-
.setMMRAMetadata(MIMD.getMMRAMetadata());
445+
.copyMIMetadata(MIMD);
455446
}
456447

457448
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
@@ -462,8 +453,7 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
462453
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
463454
BB.insert(I, MI);
464455
return MachineInstrBuilder(MF, MI)
465-
.setPCSections(MIMD.getPCSections())
466-
.setMMRAMetadata(MIMD.getMMRAMetadata());
456+
.copyMIMetadata(MIMD);
467457
}
468458

469459
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
@@ -1380,7 +1380,7 @@ void TargetInstrInfo::reassociateOps(
13801380
const MCInstrDesc &MCID, Register DestReg) {
13811381
return MachineInstrBuilder(
13821382
MF, MF.CreateMachineInstr(MCID, MIMD.getDL(), /*NoImpl=*/true))
1383-
.setPCSections(MIMD.getPCSections())
1383+
.copyMIMetadata(MIMD)
13841384
.addReg(DestReg, RegState::Define);
13851385
};
13861386

0 commit comments

Comments
 (0)