Skip to content

Conversation

@guy-david
Copy link
Contributor

@guy-david guy-david commented Feb 25, 2025

spill_p0_setb.ll and knowCRBitSpill.ll rely on a probability threshold in MachineSink to pull down CRSET/CRUNSET. It's worth to do so anyway and let register allocation decide otherwise.

Required by #127666.

@llvmbot
Copy link
Member

llvmbot commented Feb 25, 2025

@llvm/pr-subscribers-backend-powerpc

Author: Guy David (guy-david)

Changes

spill_p0_setb.ll and knowCRBitSpill.ll rely on a probability threshold in MachineSink to pull down CRSET/CRUNSET. It's worth to do so anyway and let register allocation decide otherwise.
Additionally, make the interface accept a const MI.

Required by #127666.


Full diff: https://github.com/llvm/llvm-project/pull/128745.diff

5 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/TargetInstrInfo.h (+1-1)
  • (modified) llvm/lib/Target/PowerPC/PPCInstrInfo.cpp (+7)
  • (modified) llvm/lib/Target/PowerPC/PPCInstrInfo.h (+1)
  • (modified) llvm/lib/Target/RISCV/RISCVInstrInfo.h (+1-1)
  • (modified) llvm/test/CodeGen/PowerPC/spill_p9_setb.ll (+4-4)
diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index 9e7893d5c4142..bbbe7e667d73b 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -165,7 +165,7 @@ class TargetInstrInfo : public MCInstrInfo {
 
   /// For a "cheap" instruction which doesn't enable additional sinking,
   /// should MachineSink break a critical edge to sink it anyways?
-  virtual bool shouldBreakCriticalEdgeToSink(MachineInstr &MI) const {
+  virtual bool shouldBreakCriticalEdgeToSink(const MachineInstr &MI) const {
     return false;
   }
 
diff --git a/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp b/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
index 9b526066fe75b..974db8c918c2e 100644
--- a/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
+++ b/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
@@ -1072,6 +1072,13 @@ Register PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
   return 0;
 }
 
+/// Sink down CodeGen-only, cheap instructions to allow further
+/// optimizations which are only applied intra-block.
+bool PPCInstrInfo::shouldBreakCriticalEdgeToSink(const MachineInstr &MI) const {
+  // These can turn into immediates, see PPCRegisterInfo::lowerCRBitRestore.
+  return MI.getOpcode() == PPC::CRSET || MI.getOpcode() == PPC::CRUNSET;
+}
+
 // For opcodes with the ReMaterializable flag set, this function is called to
 // verify the instruction is really rematable.
 bool PPCInstrInfo::isReallyTriviallyReMaterializable(
diff --git a/llvm/lib/Target/PowerPC/PPCInstrInfo.h b/llvm/lib/Target/PowerPC/PPCInstrInfo.h
index d4554379cdb1d..892a670dbfce1 100644
--- a/llvm/lib/Target/PowerPC/PPCInstrInfo.h
+++ b/llvm/lib/Target/PowerPC/PPCInstrInfo.h
@@ -424,6 +424,7 @@ class PPCInstrInfo : public PPCGenInstrInfo {
                              unsigned &SubIdx) const override;
   Register isLoadFromStackSlot(const MachineInstr &MI,
                                int &FrameIndex) const override;
+  bool shouldBreakCriticalEdgeToSink(const MachineInstr &MI) const override;
   bool isReallyTriviallyReMaterializable(const MachineInstr &MI) const override;
   Register isStoreToStackSlot(const MachineInstr &MI,
                               int &FrameIndex) const override;
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.h b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
index 1c46d761a7e1e..465bbd5dc2b60 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.h
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
@@ -78,7 +78,7 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
 
   bool isReallyTriviallyReMaterializable(const MachineInstr &MI) const override;
 
-  bool shouldBreakCriticalEdgeToSink(MachineInstr &MI) const override {
+  bool shouldBreakCriticalEdgeToSink(const MachineInstr &MI) const override {
     return MI.getOpcode() == RISCV::ADDI && MI.getOperand(1).isReg() &&
            MI.getOperand(1).getReg() == RISCV::X0;
   }
diff --git a/llvm/test/CodeGen/PowerPC/spill_p9_setb.ll b/llvm/test/CodeGen/PowerPC/spill_p9_setb.ll
index 81c43ce85d59a..b740296e7d710 100644
--- a/llvm/test/CodeGen/PowerPC/spill_p9_setb.ll
+++ b/llvm/test/CodeGen/PowerPC/spill_p9_setb.ll
@@ -16,21 +16,21 @@
 
 define void @p9_setb_spill() {
 ; CHECK-P9-LABEL: p9_setb_spill:
-; CHECK-P9:       # %bb.1: # %if.then
+; CHECK-P9:       # %if.then
 ; CHECK-P9-DAG:    crnot 4*cr[[CREG:.*]]+lt, eq
 ; CHECK-P9-DAG:    setb [[REG1:.*]], cr[[CREG]]
 ; CHECK-P9-DAG:    stw [[REG1]]
 ; CHECK-P9:        blr
-; CHECK-P9:        .LBB0_4: # %if.then1
+; CHECK-P9:       # %if.then1
 ;
 ; CHECK-P8-LABEL: p9_setb_spill:
-; CHECK-P8:       # %bb.1: # %if.then
+; CHECK-P8:       # %if.then
 ; CHECK-P8-DAG:    crnot 4*cr[[CREG2:.*]]+lt, eq
 ; CHECK-P8-DAG:    mfocrf [[REG2:.*]],
 ; CHECK-P8-DAG:    rlwinm [[REG2]], [[REG2]]
 ; CHECK-P8-DAG:    stw [[REG2]]
 ; CHECK-P8:        blr
-; CHECK-P8:        .LBB0_4: # %if.then1
+; CHECK-P8:       # %if.then1
 entry:
   br i1 undef, label %if.end, label %if.then
 

@llvmbot
Copy link
Member

llvmbot commented Feb 25, 2025

@llvm/pr-subscribers-backend-risc-v

Author: Guy David (guy-david)

Changes

spill_p0_setb.ll and knowCRBitSpill.ll rely on a probability threshold in MachineSink to pull down CRSET/CRUNSET. It's worth to do so anyway and let register allocation decide otherwise.
Additionally, make the interface accept a const MI.

Required by #127666.


Full diff: https://github.com/llvm/llvm-project/pull/128745.diff

5 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/TargetInstrInfo.h (+1-1)
  • (modified) llvm/lib/Target/PowerPC/PPCInstrInfo.cpp (+7)
  • (modified) llvm/lib/Target/PowerPC/PPCInstrInfo.h (+1)
  • (modified) llvm/lib/Target/RISCV/RISCVInstrInfo.h (+1-1)
  • (modified) llvm/test/CodeGen/PowerPC/spill_p9_setb.ll (+4-4)
diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index 9e7893d5c4142..bbbe7e667d73b 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -165,7 +165,7 @@ class TargetInstrInfo : public MCInstrInfo {
 
   /// For a "cheap" instruction which doesn't enable additional sinking,
   /// should MachineSink break a critical edge to sink it anyways?
-  virtual bool shouldBreakCriticalEdgeToSink(MachineInstr &MI) const {
+  virtual bool shouldBreakCriticalEdgeToSink(const MachineInstr &MI) const {
     return false;
   }
 
diff --git a/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp b/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
index 9b526066fe75b..974db8c918c2e 100644
--- a/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
+++ b/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
@@ -1072,6 +1072,13 @@ Register PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
   return 0;
 }
 
+/// Sink down CodeGen-only, cheap instructions to allow further
+/// optimizations which are only applied intra-block.
+bool PPCInstrInfo::shouldBreakCriticalEdgeToSink(const MachineInstr &MI) const {
+  // These can turn into immediates, see PPCRegisterInfo::lowerCRBitRestore.
+  return MI.getOpcode() == PPC::CRSET || MI.getOpcode() == PPC::CRUNSET;
+}
+
 // For opcodes with the ReMaterializable flag set, this function is called to
 // verify the instruction is really rematable.
 bool PPCInstrInfo::isReallyTriviallyReMaterializable(
diff --git a/llvm/lib/Target/PowerPC/PPCInstrInfo.h b/llvm/lib/Target/PowerPC/PPCInstrInfo.h
index d4554379cdb1d..892a670dbfce1 100644
--- a/llvm/lib/Target/PowerPC/PPCInstrInfo.h
+++ b/llvm/lib/Target/PowerPC/PPCInstrInfo.h
@@ -424,6 +424,7 @@ class PPCInstrInfo : public PPCGenInstrInfo {
                              unsigned &SubIdx) const override;
   Register isLoadFromStackSlot(const MachineInstr &MI,
                                int &FrameIndex) const override;
+  bool shouldBreakCriticalEdgeToSink(const MachineInstr &MI) const override;
   bool isReallyTriviallyReMaterializable(const MachineInstr &MI) const override;
   Register isStoreToStackSlot(const MachineInstr &MI,
                               int &FrameIndex) const override;
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.h b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
index 1c46d761a7e1e..465bbd5dc2b60 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.h
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
@@ -78,7 +78,7 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
 
   bool isReallyTriviallyReMaterializable(const MachineInstr &MI) const override;
 
-  bool shouldBreakCriticalEdgeToSink(MachineInstr &MI) const override {
+  bool shouldBreakCriticalEdgeToSink(const MachineInstr &MI) const override {
     return MI.getOpcode() == RISCV::ADDI && MI.getOperand(1).isReg() &&
            MI.getOperand(1).getReg() == RISCV::X0;
   }
diff --git a/llvm/test/CodeGen/PowerPC/spill_p9_setb.ll b/llvm/test/CodeGen/PowerPC/spill_p9_setb.ll
index 81c43ce85d59a..b740296e7d710 100644
--- a/llvm/test/CodeGen/PowerPC/spill_p9_setb.ll
+++ b/llvm/test/CodeGen/PowerPC/spill_p9_setb.ll
@@ -16,21 +16,21 @@
 
 define void @p9_setb_spill() {
 ; CHECK-P9-LABEL: p9_setb_spill:
-; CHECK-P9:       # %bb.1: # %if.then
+; CHECK-P9:       # %if.then
 ; CHECK-P9-DAG:    crnot 4*cr[[CREG:.*]]+lt, eq
 ; CHECK-P9-DAG:    setb [[REG1:.*]], cr[[CREG]]
 ; CHECK-P9-DAG:    stw [[REG1]]
 ; CHECK-P9:        blr
-; CHECK-P9:        .LBB0_4: # %if.then1
+; CHECK-P9:       # %if.then1
 ;
 ; CHECK-P8-LABEL: p9_setb_spill:
-; CHECK-P8:       # %bb.1: # %if.then
+; CHECK-P8:       # %if.then
 ; CHECK-P8-DAG:    crnot 4*cr[[CREG2:.*]]+lt, eq
 ; CHECK-P8-DAG:    mfocrf [[REG2:.*]],
 ; CHECK-P8-DAG:    rlwinm [[REG2]], [[REG2]]
 ; CHECK-P8-DAG:    stw [[REG2]]
 ; CHECK-P8:        blr
-; CHECK-P8:        .LBB0_4: # %if.then1
+; CHECK-P8:       # %if.then1
 entry:
   br i1 undef, label %if.end, label %if.then
 

@guy-david guy-david force-pushed the users/guy-david/machine-sink-powerpc branch from 2001146 to fdcf246 Compare February 25, 2025 22:37
@guy-david guy-david requested a review from mshockwave February 25, 2025 22:39
Copy link
Member

@mshockwave mshockwave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me. However, I'm not really familiar with the PPC instructions mentioned in this patch, could you tag some PowerPC folks to give this a look too?

@guy-david guy-david force-pushed the users/guy-david/machine-sink-powerpc branch from fdcf246 to 40b923a Compare February 26, 2025 17:20
@guy-david guy-david force-pushed the users/guy-david/machine-sink-powerpc branch from 40b923a to cebcc2f Compare February 27, 2025 00:00
spill_p0_setb.ll and knowCRBitSpill.ll rely on a probability threshold
in MachineSink to pull down CRSET/CRUNSET. It's worth to do so anyway
and let register allocation decide otherwise.
@guy-david guy-david force-pushed the users/guy-david/machine-sink-powerpc branch from cebcc2f to 95af9b3 Compare February 27, 2025 00:01
@guy-david
Copy link
Contributor Author

Closing because the PR that required this is no longer relevant: #127666.

@guy-david guy-david closed this Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants