Skip to content

Commit 5c814f0

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 62d1a0a commit 5c814f0

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
@@ -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;
@@ -316,15 +342,11 @@ class MachineInstrBuilder {
316342
}
317343
}
318344

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

@@ -342,47 +364,19 @@ class MachineInstrBuilder {
342364
}
343365
};
344366

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

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

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

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

424414
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
@@ -448,9 +438,7 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
448438
MachineFunction &MF = *BB.getParent();
449439
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
450440
BB.insert(I, MI);
451-
return MachineInstrBuilder(MF, MI)
452-
.setPCSections(MIMD.getPCSections())
453-
.setMMRAMetadata(MIMD.getMMRAMetadata());
441+
return MachineInstrBuilder(MF, MI).copyMIMetadata(MIMD);
454442
}
455443

456444
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
@@ -460,9 +448,7 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
460448
MachineFunction &MF = *BB.getParent();
461449
MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
462450
BB.insert(I, MI);
463-
return MachineInstrBuilder(MF, MI)
464-
.setPCSections(MIMD.getPCSections())
465-
.setMMRAMetadata(MIMD.getMMRAMetadata());
451+
return MachineInstrBuilder(MF, MI).copyMIMetadata(MIMD);
466452
}
467453

468454
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)