Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 18f2445

Browse files
committed
TargetInstrInfo: Change duplicate() to work on bundles.
Adds infrastructure to clone whole instruction bundles rather than just single instructions. This fixes a bug where tail duplication would unbundle instructions while cloning. This should unbreak the "Clang Stage 1: cmake, RA, with expensive checks enabled" build on greendragon. The bot broke with r311139 hitting this pre-existing bug. A proper testcase will come next. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@311511 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 31bf47e commit 18f2445

File tree

7 files changed

+73
-36
lines changed

7 files changed

+73
-36
lines changed

include/llvm/CodeGen/MachineFunction.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -625,14 +625,23 @@ class MachineFunction {
625625
MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID, const DebugLoc &DL,
626626
bool NoImp = false);
627627

628-
/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
629-
/// 'Orig' instruction, identical in all ways except the instruction
630-
/// has no parent, prev, or next.
628+
/// Create a new MachineInstr which is a copy of \p Orig, identical in all
629+
/// ways except the instruction has no parent, prev, or next. Bundling flags
630+
/// are reset.
631631
///
632-
/// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
633-
/// instructions.
632+
/// Note: Clones a single instruction, not whole instruction bundles.
633+
/// Does not perform target specific adjustments; consider using
634+
/// TargetInstrInfo::duplicate() instead.
634635
MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
635636

637+
/// Clones instruction or the whole instruction bundle \p Orig and insert
638+
/// into \p MBB before \p InsertBefore.
639+
///
640+
/// Note: Does not perform target specific adjustments; consider using
641+
/// TargetInstrInfo::duplicate() intead.
642+
MachineInstr &CloneMachineInstrBundle(MachineBasicBlock &MBB,
643+
MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig);
644+
636645
/// DeleteMachineInstr - Delete the given MachineInstr.
637646
void DeleteMachineInstr(MachineInstr *MI);
638647

include/llvm/Target/TargetInstrInfo.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,13 @@ class TargetInstrInfo : public MCInstrInfo {
324324
unsigned SubIdx, const MachineInstr &Orig,
325325
const TargetRegisterInfo &TRI) const;
326326

327-
/// Create a duplicate of the Orig instruction in MF. This is like
328-
/// MachineFunction::CloneMachineInstr(), but the target may update operands
327+
/// \brief Clones instruction or the whole instruction bundle \p Orig and
328+
/// insert into \p MBB before \p InsertBefore. The target may update operands
329329
/// that are required to be unique.
330330
///
331-
/// The instruction must be duplicable as indicated by isNotDuplicable().
332-
virtual MachineInstr *duplicate(MachineInstr &Orig,
333-
MachineFunction &MF) const;
331+
/// \p Orig must not return true for MachineInstr::isNotDuplicable().
332+
virtual MachineInstr &duplicate(MachineBasicBlock &MBB,
333+
MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) const;
334334

335335
/// This method must be implemented by targets that
336336
/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target

lib/CodeGen/MachineFunction.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,26 @@ MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
270270
MachineInstr(*this, *Orig);
271271
}
272272

273+
MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB,
274+
MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) {
275+
MachineInstr *FirstClone = nullptr;
276+
MachineBasicBlock::const_instr_iterator I = Orig.getIterator();
277+
for (;;) {
278+
MachineInstr *Cloned = CloneMachineInstr(&*I);
279+
MBB.insert(InsertBefore, Cloned);
280+
if (FirstClone == nullptr) {
281+
FirstClone = Cloned;
282+
} else {
283+
Cloned->bundleWithPred();
284+
}
285+
286+
if (!I->isBundledWithSucc())
287+
break;
288+
++I;
289+
}
290+
return *FirstClone;
291+
}
292+
273293
/// Delete the given MachineInstr.
274294
///
275295
/// This function also serves as the MachineInstr destructor - the real

lib/CodeGen/TailDuplicator.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,10 @@ void TailDuplicator::duplicateInstruction(
369369
MachineInstr *MI, MachineBasicBlock *TailBB, MachineBasicBlock *PredBB,
370370
DenseMap<unsigned, RegSubRegPair> &LocalVRMap,
371371
const DenseSet<unsigned> &UsedByPhi) {
372-
MachineInstr *NewMI = TII->duplicate(*MI, *MF);
372+
MachineInstr &NewMI = TII->duplicate(*PredBB, PredBB->end(), *MI);
373373
if (PreRegAlloc) {
374-
for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) {
375-
MachineOperand &MO = NewMI->getOperand(i);
374+
for (unsigned i = 0, e = NewMI.getNumOperands(); i != e; ++i) {
375+
MachineOperand &MO = NewMI.getOperand(i);
376376
if (!MO.isReg())
377377
continue;
378378
unsigned Reg = MO.getReg();
@@ -443,7 +443,6 @@ void TailDuplicator::duplicateInstruction(
443443
}
444444
}
445445
}
446-
PredBB->insert(PredBB->instr_end(), NewMI);
447446
}
448447

449448
/// After FromBB is tail duplicated into its predecessor blocks, the successors
@@ -825,10 +824,8 @@ bool TailDuplicator::tailDuplicate(bool IsSimple, MachineBasicBlock *TailBB,
825824
// Clone the contents of TailBB into PredBB.
826825
DenseMap<unsigned, RegSubRegPair> LocalVRMap;
827826
SmallVector<std::pair<unsigned, RegSubRegPair>, 4> CopyInfos;
828-
// Use instr_iterator here to properly handle bundles, e.g.
829-
// ARM Thumb2 IT block.
830-
MachineBasicBlock::instr_iterator I = TailBB->instr_begin();
831-
while (I != TailBB->instr_end()) {
827+
for (MachineBasicBlock::iterator I = TailBB->begin(), E = TailBB->end();
828+
I != E; /* empty */) {
832829
MachineInstr *MI = &*I;
833830
++I;
834831
if (MI->isPHI()) {

lib/CodeGen/TargetInstrInfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,11 @@ bool TargetInstrInfo::produceSameValue(const MachineInstr &MI0,
388388
return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
389389
}
390390

391-
MachineInstr *TargetInstrInfo::duplicate(MachineInstr &Orig,
392-
MachineFunction &MF) const {
391+
MachineInstr &TargetInstrInfo::duplicate(MachineBasicBlock &MBB,
392+
MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) const {
393393
assert(!Orig.isNotDuplicable() && "Instruction cannot be duplicated");
394-
return MF.CloneMachineInstr(&Orig);
394+
MachineFunction &MF = *MBB.getParent();
395+
return MF.CloneMachineInstrBundle(MBB, InsertBefore, Orig);
395396
}
396397

397398
// If the COPY instruction in MI can be folded to a stack operation, return

lib/Target/ARM/ARMBaseInstrInfo.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,20 +1550,29 @@ void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
15501550
}
15511551
}
15521552

1553-
MachineInstr *ARMBaseInstrInfo::duplicate(MachineInstr &Orig,
1554-
MachineFunction &MF) const {
1555-
MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
1556-
switch (Orig.getOpcode()) {
1557-
case ARM::tLDRpci_pic:
1558-
case ARM::t2LDRpci_pic: {
1559-
unsigned CPI = Orig.getOperand(1).getIndex();
1560-
unsigned PCLabelId = duplicateCPV(MF, CPI);
1561-
Orig.getOperand(1).setIndex(CPI);
1562-
Orig.getOperand(2).setImm(PCLabelId);
1563-
break;
1564-
}
1553+
MachineInstr &
1554+
ARMBaseInstrInfo::duplicate(MachineBasicBlock &MBB,
1555+
MachineBasicBlock::iterator InsertBefore,
1556+
const MachineInstr &Orig) const {
1557+
MachineInstr &Cloned = TargetInstrInfo::duplicate(MBB, InsertBefore, Orig);
1558+
MachineBasicBlock::instr_iterator I = Cloned.getIterator();
1559+
for (;;) {
1560+
switch (I->getOpcode()) {
1561+
case ARM::tLDRpci_pic:
1562+
case ARM::t2LDRpci_pic: {
1563+
MachineFunction &MF = *MBB.getParent();
1564+
unsigned CPI = I->getOperand(1).getIndex();
1565+
unsigned PCLabelId = duplicateCPV(MF, CPI);
1566+
I->getOperand(1).setIndex(CPI);
1567+
I->getOperand(2).setImm(PCLabelId);
1568+
break;
1569+
}
1570+
}
1571+
if (!I->isBundledWithSucc())
1572+
break;
1573+
++I;
15651574
}
1566-
return MI;
1575+
return Cloned;
15671576
}
15681577

15691578
bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,

lib/Target/ARM/ARMBaseInstrInfo.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,9 @@ class ARMBaseInstrInfo : public ARMGenInstrInfo {
220220
const MachineInstr &Orig,
221221
const TargetRegisterInfo &TRI) const override;
222222

223-
MachineInstr *duplicate(MachineInstr &Orig,
224-
MachineFunction &MF) const override;
223+
MachineInstr &
224+
duplicate(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
225+
const MachineInstr &Orig) const override;
225226

226227
const MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
227228
unsigned SubIdx, unsigned State,

0 commit comments

Comments
 (0)