Skip to content

Commit 464cb33

Browse files
authored
Revert "[LoongArch] Add isSafeToMove hook to prevent unsafe instruction motion" (#167463)
Reverts #163725
1 parent 4c4b1a9 commit 464cb33

File tree

4 files changed

+18
-46
lines changed

4 files changed

+18
-46
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,17 +1765,6 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
17651765
return true;
17661766
}
17671767

1768-
/// Return true if it's safe to move a machine instruction.
1769-
/// This allows the backend to prevent certain special instruction
1770-
/// sequences from being broken by instruction motion in optimization
1771-
/// passes.
1772-
/// By default, this returns true for every instruction.
1773-
virtual bool isSafeToMove(const MachineInstr &MI,
1774-
const MachineBasicBlock *MBB,
1775-
const MachineFunction &MF) const {
1776-
return true;
1777-
}
1778-
17791768
/// Test if the given instruction should be considered a scheduling boundary.
17801769
/// This primarily includes labels and terminators.
17811770
virtual bool isSchedulingBoundary(const MachineInstr &MI,

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,6 @@ bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
19791979
MachineBasicBlock::iterator FIB = FBB->begin();
19801980
MachineBasicBlock::iterator TIE = TBB->end();
19811981
MachineBasicBlock::iterator FIE = FBB->end();
1982-
MachineFunction &MF = *MBB->getParent();
19831982
while (TIB != TIE && FIB != FIE) {
19841983
// Skip dbg_value instructions. These do not count.
19851984
TIB = skipDebugInstructionsForward(TIB, TIE, false);
@@ -1994,10 +1993,6 @@ bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
19941993
// Hard to reason about register liveness with predicated instruction.
19951994
break;
19961995

1997-
if (!TII->isSafeToMove(*TIB, MBB, MF))
1998-
// Don't hoist the instruction if it isn't safe to move.
1999-
break;
2000-
20011996
bool IsSafe = true;
20021997
for (MachineOperand &MO : TIB->operands()) {
20031998
// Don't attempt to hoist instructions with register masks.

llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,12 @@ bool LoongArchInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
378378
}
379379
}
380380

381-
bool LoongArchInstrInfo::isSafeToMove(const MachineInstr &MI,
382-
const MachineBasicBlock *MBB,
383-
const MachineFunction &MF) const {
381+
bool LoongArchInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
382+
const MachineBasicBlock *MBB,
383+
const MachineFunction &MF) const {
384+
if (TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF))
385+
return true;
386+
384387
auto MII = MI.getIterator();
385388
auto MIE = MBB->end();
386389

@@ -426,25 +429,25 @@ bool LoongArchInstrInfo::isSafeToMove(const MachineInstr &MI,
426429
auto MO2 = Lu32I->getOperand(2).getTargetFlags();
427430
if (MO0 == LoongArchII::MO_PCREL_HI && MO1 == LoongArchII::MO_PCREL_LO &&
428431
MO2 == LoongArchII::MO_PCREL64_LO)
429-
return false;
432+
return true;
430433
if ((MO0 == LoongArchII::MO_GOT_PC_HI || MO0 == LoongArchII::MO_LD_PC_HI ||
431434
MO0 == LoongArchII::MO_GD_PC_HI) &&
432435
MO1 == LoongArchII::MO_GOT_PC_LO && MO2 == LoongArchII::MO_GOT_PC64_LO)
433-
return false;
436+
return true;
434437
if (MO0 == LoongArchII::MO_IE_PC_HI && MO1 == LoongArchII::MO_IE_PC_LO &&
435438
MO2 == LoongArchII::MO_IE_PC64_LO)
436-
return false;
439+
return true;
437440
if (MO0 == LoongArchII::MO_DESC_PC_HI &&
438441
MO1 == LoongArchII::MO_DESC_PC_LO &&
439442
MO2 == LoongArchII::MO_DESC64_PC_LO)
440-
return false;
443+
return true;
441444
break;
442445
}
443446
case LoongArch::LU52I_D: {
444447
auto MO = MI.getOperand(2).getTargetFlags();
445448
if (MO == LoongArchII::MO_PCREL64_HI || MO == LoongArchII::MO_GOT_PC64_HI ||
446449
MO == LoongArchII::MO_IE_PC64_HI || MO == LoongArchII::MO_DESC64_PC_HI)
447-
return false;
450+
return true;
448451
break;
449452
}
450453
default:
@@ -484,7 +487,7 @@ bool LoongArchInstrInfo::isSafeToMove(const MachineInstr &MI,
484487
auto MO1 = LoongArchII::getDirectFlags(SecondOp->getOperand(2));
485488
auto MO2 = LoongArchII::getDirectFlags(Ld->getOperand(2));
486489
if (MO1 == LoongArchII::MO_DESC_PC_LO && MO2 == LoongArchII::MO_DESC_LD)
487-
return false;
490+
return true;
488491
break;
489492
}
490493
if (SecondOp == MIE ||
@@ -493,53 +496,41 @@ bool LoongArchInstrInfo::isSafeToMove(const MachineInstr &MI,
493496
auto MO1 = LoongArchII::getDirectFlags(SecondOp->getOperand(2));
494497
if (MO0 == LoongArchII::MO_PCREL_HI && SecondOp->getOpcode() == AddiOp &&
495498
MO1 == LoongArchII::MO_PCREL_LO)
496-
return false;
499+
return true;
497500
if (MO0 == LoongArchII::MO_GOT_PC_HI && SecondOp->getOpcode() == LdOp &&
498501
MO1 == LoongArchII::MO_GOT_PC_LO)
499-
return false;
502+
return true;
500503
if ((MO0 == LoongArchII::MO_LD_PC_HI ||
501504
MO0 == LoongArchII::MO_GD_PC_HI) &&
502505
SecondOp->getOpcode() == AddiOp && MO1 == LoongArchII::MO_GOT_PC_LO)
503-
return false;
506+
return true;
504507
break;
505508
}
506509
case LoongArch::ADDI_W:
507510
case LoongArch::ADDI_D: {
508511
auto MO = LoongArchII::getDirectFlags(MI.getOperand(2));
509512
if (MO == LoongArchII::MO_PCREL_LO || MO == LoongArchII::MO_GOT_PC_LO)
510-
return false;
513+
return true;
511514
break;
512515
}
513516
case LoongArch::LD_W:
514517
case LoongArch::LD_D: {
515518
auto MO = LoongArchII::getDirectFlags(MI.getOperand(2));
516519
if (MO == LoongArchII::MO_GOT_PC_LO)
517-
return false;
520+
return true;
518521
break;
519522
}
520523
case LoongArch::PseudoDESC_CALL: {
521524
auto MO = LoongArchII::getDirectFlags(MI.getOperand(2));
522525
if (MO == LoongArchII::MO_DESC_CALL)
523-
return false;
526+
return true;
524527
break;
525528
}
526529
default:
527530
break;
528531
}
529532
}
530533

531-
return true;
532-
}
533-
534-
bool LoongArchInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
535-
const MachineBasicBlock *MBB,
536-
const MachineFunction &MF) const {
537-
if (TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF))
538-
return true;
539-
540-
if (!isSafeToMove(MI, MBB, MF))
541-
return true;
542-
543534
return false;
544535
}
545536

llvm/lib/Target/LoongArch/LoongArchInstrInfo.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ class LoongArchInstrInfo : public LoongArchGenInstrInfo {
6666
bool isBranchOffsetInRange(unsigned BranchOpc,
6767
int64_t BrOffset) const override;
6868

69-
bool isSafeToMove(const MachineInstr &MI, const MachineBasicBlock *MBB,
70-
const MachineFunction &MF) const override;
71-
7269
bool isSchedulingBoundary(const MachineInstr &MI,
7370
const MachineBasicBlock *MBB,
7471
const MachineFunction &MF) const override;

0 commit comments

Comments
 (0)