[llvm] r238785 - LiveRangeEdit: Fix liveranges not shrinking on subrange kill.

Matthias Braun matze at braunis.de
Mon Jun 1 14:26:27 PDT 2015


Author: matze
Date: Mon Jun  1 16:26:26 2015
New Revision: 238785

URL: http://llvm.org/viewvc/llvm-project?rev=238785&view=rev
Log:
LiveRangeEdit: Fix liveranges not shrinking on subrange kill.

If a dead instruction we may not only have a last-use in the main live
range but also in a subregister range if subregisters are tracked. We
need to partially rebuild live ranges in both cases.

The testcase only broke when subregister liveness was enabled. I
commited it in the current form because there is currently no flag to
enable/disable subregister liveness.

This fixes PR23720.

Added:
    llvm/trunk/test/CodeGen/R600/subreg-eliminate-dead.ll
Modified:
    llvm/trunk/include/llvm/CodeGen/LiveRangeEdit.h
    llvm/trunk/lib/CodeGen/LiveRangeEdit.cpp

Modified: llvm/trunk/include/llvm/CodeGen/LiveRangeEdit.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/LiveRangeEdit.h?rev=238785&r1=238784&r2=238785&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/LiveRangeEdit.h (original)
+++ llvm/trunk/include/llvm/CodeGen/LiveRangeEdit.h Mon Jun  1 16:26:26 2015
@@ -102,6 +102,10 @@ private:
   /// registers are created.
   void MRI_NoteNewVirtualRegister(unsigned VReg) override;
 
+  /// \brief Check if MachineOperand \p MO is a last use/kill either in the
+  /// main live range of \p LI or in one of the matching subregister ranges.
+  bool useIsKill(const LiveInterval &LI, const MachineOperand &MO) const;
+
 public:
   /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
   /// @param parent The register being spilled or split.

Modified: llvm/trunk/lib/CodeGen/LiveRangeEdit.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/LiveRangeEdit.cpp?rev=238785&r1=238784&r2=238785&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/LiveRangeEdit.cpp (original)
+++ llvm/trunk/lib/CodeGen/LiveRangeEdit.cpp Mon Jun  1 16:26:26 2015
@@ -218,6 +218,22 @@ bool LiveRangeEdit::foldAsLoad(LiveInter
   return true;
 }
 
+bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
+                              const MachineOperand &MO) const {
+  const MachineInstr *MI = MO.getParent();
+  SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
+  if (LI.Query(Idx).isKill())
+    return true;
+  const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
+  unsigned SubReg = MO.getSubReg();
+  unsigned LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
+  for (const LiveInterval::SubRange &S : LI.subranges()) {
+    if ((S.LaneMask & LaneMask) != 0 && S.Query(Idx).isKill())
+      return true;
+  }
+  return false;
+}
+
 /// Find all live intervals that need to shrink, then remove the instruction.
 void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink) {
   assert(MI->allDefsAreDead() && "Def isn't really dead");
@@ -266,9 +282,8 @@ void LiveRangeEdit::eliminateDeadDef(Mac
     // unlikely to change anything. We typically don't want to shrink the
     // PIC base register that has lots of uses everywhere.
     // Always shrink COPY uses that probably come from live range splitting.
-    if (MI->readsVirtualRegister(Reg) &&
-        (MI->isCopy() || MOI->isDef() || MRI.hasOneNonDBGUse(Reg) ||
-         LI.Query(Idx).isKill()))
+    if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MOI->isDef())) ||
+        (MOI->readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
       ToShrink.insert(&LI);
 
     // Remove defined value.

Added: llvm/trunk/test/CodeGen/R600/subreg-eliminate-dead.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/R600/subreg-eliminate-dead.ll?rev=238785&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/R600/subreg-eliminate-dead.ll (added)
+++ llvm/trunk/test/CodeGen/R600/subreg-eliminate-dead.ll Mon Jun  1 16:26:26 2015
@@ -0,0 +1,55 @@
+; RUN: llc -verify-machineinstrs -o - %s | FileCheck %s
+; LiveRangeEdit::eliminateDeadDef did not update LiveInterval sub ranges
+; properly.
+target datalayout = "e-p:32:32-p1:64:64-p2:64:64-p3:32:32-p4:64:64-p5:32:32-p24:64:64-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64"
+target triple = "amdgcn--"
+
+; CHECK-LABEL: foobar:
+; Output with subregister liveness disabled:
+; CHECK: v_mov_b32_e32 v1, 1
+; CHECK: v_mov_b32_e32 v0, 0
+; CHECK: v_cmp_eq_i32_e32 vcc, s0, v0
+; CHECK: v_cmp_eq_i32_e64 s[0:1], s0, v1
+; CHECK: v_mov_b32_e32 v1, 3
+; CHECK: v_mov_b32_e32 v0, 2
+; CHECK: v_cmp_eq_i32_e64 s[2:3], s0, v0
+; CHECK: v_cmp_eq_i32_e64 s[4:5], s0, v1
+; CHECK: v_cndmask_b32_e64 v3, 0, -1, s[4:5]
+; CHECK: v_cndmask_b32_e64 v2, 0, -1, s[2:3]
+; CHECK: v_cndmask_b32_e64 v1, 0, -1, s[0:1]
+; CHECK: v_cndmask_b32_e64 v0, 0, -1, vcc
+; CHECK: v_cmp_ne_i32_e32 vcc, 0, v1
+; CHECK: v_cndmask_b32_e64 v1, 0, v0, vcc
+; CHECK: s_mov_b32 s3, 0xf000
+; CHECK: s_mov_b32 s2, -1
+; CHECK: buffer_store_dwordx2 v[0:1], s[0:3], 0
+; CHECK: s_endpgm
+; Output with subregister liveness enabled:
+; XCHECK: v_mov_b32_e32 v1, 1
+; XCHECK: v_mov_b32_e32 v0, 0
+; XCHECK: v_cmp_eq_i32_e32 vcc, s0, v1
+; XCHECK: v_mov_b32_e32 v1, 3
+; XCHECK: v_mov_b32_e32 v0, 2
+; XCHECK: v_cmp_eq_i32_e64 s[0:1], s0, v0
+; XCHECK: v_cmp_eq_i32_e64 s[2:3], s0, v1
+; XCHECK: v_cndmask_b32_e64 v3, 0, -1, s[2:3]
+; XCHECK: v_cndmask_b32_e64 v2, 0, -1, s[0:1]
+; XCHECK: v_cndmask_b32_e64 v1, 0, -1, vcc
+; XCHECK: v_cmp_ne_i32_e32 vcc, 0, v1
+; XCHECK: v_cndmask_b32_e64 v1, 0, v0, vcc
+; XCHECK: s_mov_b32 s3, 0xf000
+; XCHECK: s_mov_b32 s2, -1
+; XCHECK: buffer_store_dwordx2 v[0:1], s[0:3], 0
+; XCHECK: s_endpgm
+define void @foobar() {
+  %v0 = icmp eq <4 x i32> undef, <i32 0, i32 1, i32 2, i32 3>
+  %v3 = sext <4 x i1> %v0 to <4 x i32>
+  %v4 = extractelement <4 x i32> %v3, i32 1
+  %v5 = icmp ne i32 %v4, 0
+  %v6 = select i1 %v5, i32 undef, i32 0
+  %v15 = insertelement <2 x i32> undef, i32 %v6, i32 1
+  store <2 x i32> %v15, <2 x i32> addrspace(1)* undef, align 8
+  ret void
+}
+
+declare double @llvm.fma.f64(double, double, double)





More information about the llvm-commits mailing list