[llvm] [AMDGPU] Fix phi injection in si-i1-lowering (PR #179267)

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 2 08:17:03 PST 2026


================
@@ -465,9 +469,148 @@ PhiLoweringHelper::PhiLoweringHelper(MachineFunction *MF,
     XorOp = AMDGPU::S_XOR_B64;
     AndN2Op = AMDGPU::S_ANDN2_B64;
     OrN2Op = AMDGPU::S_ORN2_B64;
+    CSelectOp = AMDGPU::S_CSELECT_B64;
+    CmpLGOp = AMDGPU::S_CMP_LG_U64;
   }
 }
 
+static void instrDefsUsesSCC(const MachineInstr &MI, bool &Def, bool &Use) {
+  Def = false;
+  Use = false;
+
+  for (const MachineOperand &MO : MI.operands()) {
+    if (MO.isReg() && MO.getReg() == AMDGPU::SCC) {
+      if (MO.isUse())
+        Use = true;
+      else
+        Def = true;
+    }
+  }
+}
+
+/// Move instruction to a new position inside the same MBB, if there is no
+/// operand's dependencies. Change the InstrToMovePos after the moved
+/// instruction. returns true if instruction moved, false if not.
+bool moveIfPossible(MachineBasicBlock &MBB,
+                    llvm::MachineBasicBlock::iterator &InstrToMovePos,
+                    const llvm::MachineBasicBlock::iterator &MoveAfterPos) {
+  MachineInstr &MI = *InstrToMovePos;
+
+  for (const MachineOperand &MO : MI.operands()) {
+    // Check if any operands are defined between current position and target
+    if (!MO.isReg())
+      continue;
+    if (MO.isUse()) {
+      for (auto I = std::next(MI.getIterator()); I != MoveAfterPos; ++I) {
+        for (const MachineOperand &MOI : I->operands())
+          if (MOI.isReg() && MOI.isDef() && MOI.getReg() == MO.getReg())
+            return false;
----------------
shiltian wrote:

```suggestion
        for (const MachineOperand &MOI : I->operands()) {
          if (MOI.isReg() && MOI.isDef() && MOI.getReg() == MO.getReg())
            return false;
        }
```

https://github.com/llvm/llvm-project/pull/179267


More information about the llvm-commits mailing list