[llvm] [CodeGen][TII] Simplify analyzeSelect (PR #175828)

Sam Elliott via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 2 22:19:06 PST 2026


https://github.com/lenary updated https://github.com/llvm/llvm-project/pull/175828

>From 5e5631452f113a02990ba09d8f5f3e96e3114aaf Mon Sep 17 00:00:00 2001
From: Sam Elliott <aelliott at qti.qualcomm.com>
Date: Tue, 13 Jan 2026 11:43:30 -0800
Subject: [PATCH 1/3] [CodeGen][TII] Simplify analyzeSelect

The only caller of this function (`PeepholeOptimizer::optimizeSelect`)
did not use most of the parameters, so remove them.
---
 llvm/include/llvm/CodeGen/TargetInstrInfo.h | 10 ----------
 llvm/lib/CodeGen/PeepholeOptimizer.cpp      |  5 +----
 llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp    | 13 -------------
 llvm/lib/Target/ARM/ARMBaseInstrInfo.h      |  3 +--
 llvm/lib/Target/Lanai/LanaiInstrInfo.cpp    | 10 ----------
 llvm/lib/Target/Lanai/LanaiInstrInfo.h      |  3 +--
 llvm/lib/Target/RISCV/RISCVInstrInfo.cpp    | 14 --------------
 llvm/lib/Target/RISCV/RISCVInstrInfo.h      |  3 +--
 8 files changed, 4 insertions(+), 57 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index 11adc190b2a62..bca9f17181a3d 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -1024,24 +1024,14 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
   /// Analyze the given select instruction, returning true if
   /// it cannot be understood. It is assumed that MI->isSelect() is true.
   ///
-  /// When successful, return the controlling condition and the operands that
-  /// determine the true and false result values.
-  ///
-  ///   Result = SELECT Cond, TrueOp, FalseOp
-  ///
   /// Some targets can optimize select instructions, for example by predicating
   /// the instruction defining one of the operands. Such targets should set
   /// Optimizable.
   ///
   /// @param         MI Select instruction to analyze.
-  /// @param Cond    Condition controlling the select.
-  /// @param TrueOp  Operand number of the value selected when Cond is true.
-  /// @param FalseOp Operand number of the value selected when Cond is false.
   /// @param Optimizable Returned as true if MI is optimizable.
   /// @returns False on success.
   virtual bool analyzeSelect(const MachineInstr &MI,
-                             SmallVectorImpl<MachineOperand> &Cond,
-                             unsigned &TrueOp, unsigned &FalseOp,
                              bool &Optimizable) const {
     assert(MI.getDesc().isSelect() && "MI must be a select instruction");
     return true;
diff --git a/llvm/lib/CodeGen/PeepholeOptimizer.cpp b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
index 493d8dfa8c00a..acbdb701946f8 100644
--- a/llvm/lib/CodeGen/PeepholeOptimizer.cpp
+++ b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
@@ -961,11 +961,8 @@ bool PeepholeOptimizer::optimizeCmpInstr(MachineInstr &MI) {
 /// Optimize a select instruction.
 bool PeepholeOptimizer::optimizeSelect(
     MachineInstr &MI, SmallPtrSetImpl<MachineInstr *> &LocalMIs) {
-  unsigned TrueOp = 0;
-  unsigned FalseOp = 0;
   bool Optimizable = false;
-  SmallVector<MachineOperand, 4> Cond;
-  if (TII->analyzeSelect(MI, Cond, TrueOp, FalseOp, Optimizable))
+  if (TII->analyzeSelect(MI, Optimizable))
     return false;
   if (!Optimizable)
     return false;
diff --git a/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp b/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
index 02887ce93c525..5ea4ad1356aa3 100644
--- a/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
+++ b/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
@@ -2163,22 +2163,9 @@ ARMBaseInstrInfo::canFoldIntoMOVCC(Register Reg, const MachineRegisterInfo &MRI,
 }
 
 bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
-                                     SmallVectorImpl<MachineOperand> &Cond,
-                                     unsigned &TrueOp, unsigned &FalseOp,
                                      bool &Optimizable) const {
   assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
          "Unknown select instruction");
-  // MOVCC operands:
-  // 0: Def.
-  // 1: True use.
-  // 2: False use.
-  // 3: Condition code.
-  // 4: CPSR use.
-  TrueOp = 1;
-  FalseOp = 2;
-  Cond.push_back(MI.getOperand(3));
-  Cond.push_back(MI.getOperand(4));
-  // We can always fold a def.
   Optimizable = true;
   return false;
 }
diff --git a/llvm/lib/Target/ARM/ARMBaseInstrInfo.h b/llvm/lib/Target/ARM/ARMBaseInstrInfo.h
index 04e2ab055cf1a..2a0c6a6ef7cef 100644
--- a/llvm/lib/Target/ARM/ARMBaseInstrInfo.h
+++ b/llvm/lib/Target/ARM/ARMBaseInstrInfo.h
@@ -305,8 +305,7 @@ class ARMBaseInstrInfo : public ARMGenInstrInfo {
                             const MachineRegisterInfo *MRI) const override;
 
   bool analyzeSelect(const MachineInstr &MI,
-                     SmallVectorImpl<MachineOperand> &Cond, unsigned &TrueOp,
-                     unsigned &FalseOp, bool &Optimizable) const override;
+                     bool &Optimizable) const override;
 
   MachineInstr *optimizeSelect(MachineInstr &MI,
                                SmallPtrSetImpl<MachineInstr *> &SeenMIs,
diff --git a/llvm/lib/Target/Lanai/LanaiInstrInfo.cpp b/llvm/lib/Target/Lanai/LanaiInstrInfo.cpp
index 14b7557e7f94a..8938d7b120096 100644
--- a/llvm/lib/Target/Lanai/LanaiInstrInfo.cpp
+++ b/llvm/lib/Target/Lanai/LanaiInstrInfo.cpp
@@ -437,18 +437,8 @@ bool LanaiInstrInfo::optimizeCompareInstr(
 }
 
 bool LanaiInstrInfo::analyzeSelect(const MachineInstr &MI,
-                                   SmallVectorImpl<MachineOperand> &Cond,
-                                   unsigned &TrueOp, unsigned &FalseOp,
                                    bool &Optimizable) const {
   assert(MI.getOpcode() == Lanai::SELECT && "unknown select instruction");
-  // Select operands:
-  // 0: Def.
-  // 1: True use.
-  // 2: False use.
-  // 3: Condition code.
-  TrueOp = 1;
-  FalseOp = 2;
-  Cond.push_back(MI.getOperand(3));
   Optimizable = true;
   return false;
 }
diff --git a/llvm/lib/Target/Lanai/LanaiInstrInfo.h b/llvm/lib/Target/Lanai/LanaiInstrInfo.h
index 155e2f03be630..4295a74a80491 100644
--- a/llvm/lib/Target/Lanai/LanaiInstrInfo.h
+++ b/llvm/lib/Target/Lanai/LanaiInstrInfo.h
@@ -119,8 +119,7 @@ class LanaiInstrInfo : public LanaiGenInstrInfo {
   // Lanai can optimize certain select instructions, for example by predicating
   // the instruction defining one of the operands and sets Optimizable to true.
   bool analyzeSelect(const MachineInstr &MI,
-                     SmallVectorImpl<MachineOperand> &Cond, unsigned &TrueOp,
-                     unsigned &FalseOp, bool &Optimizable) const override;
+                     bool &Optimizable) const override;
 
   // Given a select instruction that was understood by analyzeSelect and
   // returned Optimizable = true, attempt to optimize MI by merging it with one
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
index 5a717654e385c..c7a9a18a3abc0 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
@@ -1887,23 +1887,9 @@ static MachineInstr *canFoldAsPredicatedOp(Register Reg,
 }
 
 bool RISCVInstrInfo::analyzeSelect(const MachineInstr &MI,
-                                   SmallVectorImpl<MachineOperand> &Cond,
-                                   unsigned &TrueOp, unsigned &FalseOp,
                                    bool &Optimizable) const {
   assert(MI.getOpcode() == RISCV::PseudoCCMOVGPR &&
          "Unknown select instruction");
-  // CCMOV operands:
-  // 0: Def.
-  // 1: LHS of compare.
-  // 2: RHS of compare.
-  // 3: Condition code.
-  // 4: False use.
-  // 5: True use.
-  TrueOp = 5;
-  FalseOp = 4;
-  Cond.push_back(MI.getOperand(1));
-  Cond.push_back(MI.getOperand(2));
-  Cond.push_back(MI.getOperand(3));
   // We can only fold when we support short forward branch opt.
   Optimizable = STI.hasShortForwardBranchIALU();
   return false;
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.h b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
index 741a514d82bdd..68f6ef6e2c781 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.h
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
@@ -175,8 +175,7 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
                              int64_t BrOffset) const override;
 
   bool analyzeSelect(const MachineInstr &MI,
-                     SmallVectorImpl<MachineOperand> &Cond, unsigned &TrueOp,
-                     unsigned &FalseOp, bool &Optimizable) const override;
+                     bool &Optimizable) const override;
 
   MachineInstr *optimizeSelect(MachineInstr &MI,
                                SmallPtrSetImpl<MachineInstr *> &SeenMIs,

>From 1df1a2a5b1c39bd75da3fbe803734d44b993c6b2 Mon Sep 17 00:00:00 2001
From: Sam Elliott <aelliott at qti.qualcomm.com>
Date: Tue, 13 Jan 2026 12:56:19 -0800
Subject: [PATCH 2/3] clang-format

---
 llvm/include/llvm/CodeGen/TargetInstrInfo.h | 3 +--
 llvm/lib/Target/ARM/ARMBaseInstrInfo.h      | 3 +--
 llvm/lib/Target/Lanai/LanaiInstrInfo.h      | 3 +--
 llvm/lib/Target/RISCV/RISCVInstrInfo.h      | 3 +--
 4 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index bca9f17181a3d..60c2604e833e7 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -1031,8 +1031,7 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
   /// @param         MI Select instruction to analyze.
   /// @param Optimizable Returned as true if MI is optimizable.
   /// @returns False on success.
-  virtual bool analyzeSelect(const MachineInstr &MI,
-                             bool &Optimizable) const {
+  virtual bool analyzeSelect(const MachineInstr &MI, bool &Optimizable) const {
     assert(MI.getDesc().isSelect() && "MI must be a select instruction");
     return true;
   }
diff --git a/llvm/lib/Target/ARM/ARMBaseInstrInfo.h b/llvm/lib/Target/ARM/ARMBaseInstrInfo.h
index 2a0c6a6ef7cef..7ddbfddc03715 100644
--- a/llvm/lib/Target/ARM/ARMBaseInstrInfo.h
+++ b/llvm/lib/Target/ARM/ARMBaseInstrInfo.h
@@ -304,8 +304,7 @@ class ARMBaseInstrInfo : public ARMGenInstrInfo {
                             Register SrcReg2, int64_t CmpMask, int64_t CmpValue,
                             const MachineRegisterInfo *MRI) const override;
 
-  bool analyzeSelect(const MachineInstr &MI,
-                     bool &Optimizable) const override;
+  bool analyzeSelect(const MachineInstr &MI, bool &Optimizable) const override;
 
   MachineInstr *optimizeSelect(MachineInstr &MI,
                                SmallPtrSetImpl<MachineInstr *> &SeenMIs,
diff --git a/llvm/lib/Target/Lanai/LanaiInstrInfo.h b/llvm/lib/Target/Lanai/LanaiInstrInfo.h
index 4295a74a80491..17261a753ca9d 100644
--- a/llvm/lib/Target/Lanai/LanaiInstrInfo.h
+++ b/llvm/lib/Target/Lanai/LanaiInstrInfo.h
@@ -118,8 +118,7 @@ class LanaiInstrInfo : public LanaiGenInstrInfo {
   //
   // Lanai can optimize certain select instructions, for example by predicating
   // the instruction defining one of the operands and sets Optimizable to true.
-  bool analyzeSelect(const MachineInstr &MI,
-                     bool &Optimizable) const override;
+  bool analyzeSelect(const MachineInstr &MI, bool &Optimizable) const override;
 
   // Given a select instruction that was understood by analyzeSelect and
   // returned Optimizable = true, attempt to optimize MI by merging it with one
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.h b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
index 68f6ef6e2c781..74e69a269870c 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.h
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
@@ -174,8 +174,7 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
   bool isBranchOffsetInRange(unsigned BranchOpc,
                              int64_t BrOffset) const override;
 
-  bool analyzeSelect(const MachineInstr &MI,
-                     bool &Optimizable) const override;
+  bool analyzeSelect(const MachineInstr &MI, bool &Optimizable) const override;
 
   MachineInstr *optimizeSelect(MachineInstr &MI,
                                SmallPtrSetImpl<MachineInstr *> &SeenMIs,

>From f96ba99b2ce444c1ae30f4f229fe52a4776ac7ab Mon Sep 17 00:00:00 2001
From: Sam Elliott <aelliott at qti.qualcomm.com>
Date: Mon, 2 Feb 2026 22:18:54 -0800
Subject: [PATCH 3/3] Remove Optimizable, update Docs

---
 llvm/include/llvm/CodeGen/TargetInstrInfo.h | 10 +++-------
 llvm/lib/CodeGen/PeepholeOptimizer.cpp      |  5 +----
 llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp    |  4 +---
 llvm/lib/Target/ARM/ARMBaseInstrInfo.h      |  2 +-
 llvm/lib/Target/Lanai/LanaiInstrInfo.cpp    |  4 +---
 llvm/lib/Target/Lanai/LanaiInstrInfo.h      |  2 +-
 llvm/lib/Target/RISCV/RISCVInstrInfo.cpp    |  5 +----
 llvm/lib/Target/RISCV/RISCVInstrInfo.h      |  2 +-
 8 files changed, 10 insertions(+), 24 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index 24d924c10309f..c3110d83dfa03 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -1022,16 +1022,12 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
   }
 
   /// Analyze the given select instruction, returning true if
-  /// it cannot be understood. It is assumed that MI->isSelect() is true.
-  ///
-  /// Some targets can optimize select instructions, for example by predicating
-  /// the instruction defining one of the operands. Such targets should set
-  /// Optimizable.
+  /// it cannot be optimised or it cannot be understood. It is assumed that
+  /// MI->isSelect() is true.
   ///
   /// @param         MI Select instruction to analyze.
-  /// @param Optimizable Returned as true if MI is optimizable.
   /// @returns False on success.
-  virtual bool analyzeSelect(const MachineInstr &MI, bool &Optimizable) const {
+  virtual bool analyzeSelect(const MachineInstr &MI) const {
     assert(MI.getDesc().isSelect() && "MI must be a select instruction");
     return true;
   }
diff --git a/llvm/lib/CodeGen/PeepholeOptimizer.cpp b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
index a8a20c5c0548c..8f1cac130feda 100644
--- a/llvm/lib/CodeGen/PeepholeOptimizer.cpp
+++ b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
@@ -959,10 +959,7 @@ bool PeepholeOptimizer::optimizeCmpInstr(MachineInstr &MI) {
 /// Optimize a select instruction.
 bool PeepholeOptimizer::optimizeSelect(
     MachineInstr &MI, SmallPtrSetImpl<MachineInstr *> &LocalMIs) {
-  bool Optimizable = false;
-  if (TII->analyzeSelect(MI, Optimizable))
-    return false;
-  if (!Optimizable)
+  if (TII->analyzeSelect(MI))
     return false;
   if (!TII->optimizeSelect(MI, LocalMIs))
     return false;
diff --git a/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp b/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
index 76b9a18fbee0f..5c713f49b9a88 100644
--- a/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
+++ b/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
@@ -2167,11 +2167,9 @@ ARMBaseInstrInfo::canFoldIntoMOVCC(Register Reg, const MachineRegisterInfo &MRI,
   return MI;
 }
 
-bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
-                                     bool &Optimizable) const {
+bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI) const {
   assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
          "Unknown select instruction");
-  Optimizable = true;
   return false;
 }
 
diff --git a/llvm/lib/Target/ARM/ARMBaseInstrInfo.h b/llvm/lib/Target/ARM/ARMBaseInstrInfo.h
index 3fa0810966423..4c659f4454fa8 100644
--- a/llvm/lib/Target/ARM/ARMBaseInstrInfo.h
+++ b/llvm/lib/Target/ARM/ARMBaseInstrInfo.h
@@ -304,7 +304,7 @@ class ARMBaseInstrInfo : public ARMGenInstrInfo {
                             Register SrcReg2, int64_t CmpMask, int64_t CmpValue,
                             const MachineRegisterInfo *MRI) const override;
 
-  bool analyzeSelect(const MachineInstr &MI, bool &Optimizable) const override;
+  bool analyzeSelect(const MachineInstr &MI) const override;
 
   MachineInstr *optimizeSelect(MachineInstr &MI,
                                SmallPtrSetImpl<MachineInstr *> &SeenMIs,
diff --git a/llvm/lib/Target/Lanai/LanaiInstrInfo.cpp b/llvm/lib/Target/Lanai/LanaiInstrInfo.cpp
index e729347a5cef1..a795ace10b0a8 100644
--- a/llvm/lib/Target/Lanai/LanaiInstrInfo.cpp
+++ b/llvm/lib/Target/Lanai/LanaiInstrInfo.cpp
@@ -436,10 +436,8 @@ bool LanaiInstrInfo::optimizeCompareInstr(
   return false;
 }
 
-bool LanaiInstrInfo::analyzeSelect(const MachineInstr &MI,
-                                   bool &Optimizable) const {
+bool LanaiInstrInfo::analyzeSelect(const MachineInstr &MI) const {
   assert(MI.getOpcode() == Lanai::SELECT && "unknown select instruction");
-  Optimizable = true;
   return false;
 }
 
diff --git a/llvm/lib/Target/Lanai/LanaiInstrInfo.h b/llvm/lib/Target/Lanai/LanaiInstrInfo.h
index 5cf7b9b043d34..3d4e6ea751df1 100644
--- a/llvm/lib/Target/Lanai/LanaiInstrInfo.h
+++ b/llvm/lib/Target/Lanai/LanaiInstrInfo.h
@@ -119,7 +119,7 @@ class LanaiInstrInfo : public LanaiGenInstrInfo {
   //
   // Lanai can optimize certain select instructions, for example by predicating
   // the instruction defining one of the operands and sets Optimizable to true.
-  bool analyzeSelect(const MachineInstr &MI, bool &Optimizable) const override;
+  bool analyzeSelect(const MachineInstr &MI) const override;
 
   // Given a select instruction that was understood by analyzeSelect and
   // returned Optimizable = true, attempt to optimize MI by merging it with one
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
index c69b6305a21e7..a60ccccb33b9d 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
@@ -1898,12 +1898,9 @@ static MachineInstr *canFoldAsPredicatedOp(Register Reg,
   return MI;
 }
 
-bool RISCVInstrInfo::analyzeSelect(const MachineInstr &MI,
-                                   bool &Optimizable) const {
+bool RISCVInstrInfo::analyzeSelect(const MachineInstr &MI) const {
   assert(MI.getOpcode() == RISCV::PseudoCCMOVGPR &&
          "Unknown select instruction");
-  // We can only fold when we support short forward branch opt.
-  Optimizable = STI.hasShortForwardBranchIALU();
   return false;
 }
 
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.h b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
index 3ffb3c9a0e932..ace4d03d090b4 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.h
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
@@ -175,7 +175,7 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
   bool isBranchOffsetInRange(unsigned BranchOpc,
                              int64_t BrOffset) const override;
 
-  bool analyzeSelect(const MachineInstr &MI, bool &Optimizable) const override;
+  bool analyzeSelect(const MachineInstr &MI) const override;
 
   MachineInstr *optimizeSelect(MachineInstr &MI,
                                SmallPtrSetImpl<MachineInstr *> &SeenMIs,



More information about the llvm-commits mailing list