<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/78942>78942</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Calculation of regunitmasks seems wrong
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          kaz7
      </td>
    </tr>
</table>

<pre>
    Calculation of regunitmasks seems wrong after https://reviews.llvm.org/D157864.

Hi, @jayfoad and @qcolombet, VE is having following troubles after above patch.  The existing regression tests are passed, but the calculation of livein and regunitmasks become wrong. I'll explain what are causing problems.  Please consider what is the best way to solve this problem.  (I add @jayfoad since I though you are foad on llvm phablicator.  Sorry if it's not correct.)

1. Livein is calculated wrongly because of following code.

```
diff --git a/llvm/lib/CodeGen/MachineSink.cpp b/llvm/lib/CodeGen/MachineSink.cpp
index 8fda4c3e3e83..b4cbb93d758e 100644
--- a/llvm/lib/CodeGen/MachineSink.cpp
+++ b/llvm/lib/CodeGen/MachineSink.cpp
@@ -1704,10 +1704,9 @@ static void updateLiveIn(MachineInstr *MI, MachineBasicBlock *SuccBB,
   for (auto U : UsedOpsInCopy) {
     Register SrcReg = MI->getOperand(U).getReg();
 LaneBitmask Mask;
-    for (MCRegUnitMaskIterator S(SrcReg, TRI); S.isValid(); ++S) {
+    for (MCRegUnitMaskIterator S(SrcReg, TRI); S.isValid(); ++S)
       Mask |= (*S).second;
-    }
- SuccBB->addLiveIn(SrcReg, Mask.any() ? Mask : LaneBitmask::getAll());
+ SuccBB->addLiveIn(SrcReg, Mask);
   }
 SuccBB->sortUniqueLiveIns();
 }
```

- LaneBitmasks from all MCRegUnitMaskIterator of SrcReg sometimes becomes none() because of 2nd problem.  As a result, the register is treated as not livein although it is alive.  IMO, it's better to think a register is alive here.  Since, even whole lanebitmasks don't match, a register is used somehow from the point of livein view.

2. computeRegUnitLaneMasks calculates different lanebitmask for each regunit.

```
diff --git a/llvm/utils/TableGen/CodeGenRegisters.cpp b/llvm/utils/TableGen/CodeGenRegisters.cpp
index 74fb80610b9c..b8dde6e946a4 100644
--- a/llvm/utils/TableGen/CodeGenRegisters.cpp
+++ b/llvm/utils/TableGen/CodeGenRegisters.cpp
@@ -2123,8 +2123,8 @@ void CodeGenRegBank::computeRegUnitLaneMasks() {
   for (auto &Register : Registers) {
     // Create an initial lane mask for all register units.
     const auto &RegUnits = Register.getRegUnits();
- CodeGenRegister::RegUnitLaneMaskList
- RegUnitLaneMasks(RegUnits.count(), LaneBitmask::getNone());
+ CodeGenRegister::RegUnitLaneMaskList RegUnitLaneMasks(
+ RegUnits.count(), LaneBitmask::getAll());
     // Iterate through SubRegisters.
     typedef CodeGenRegister::SubRegMap SubRegMap;
 const SubRegMap &SubRegs = Register.getSubRegs();
@@ -2143,7 +2143,7 @@ void CodeGenRegBank::computeRegUnitLaneMasks() {
         unsigned u = 0;
         for (unsigned RU : RegUnits) {
           if (SUI == RU) {
-            RegUnitLaneMasks[u] |= LaneMask;
+ RegUnitLaneMasks[u] &= LaneMask;
             assert(!Found);
 Found = true;
           }
```

I'm not sure this is correct or not.  However, the result of calculation is wrong.  We have two subreg with 0x0001 and 0x0002 lanemasks.  original code works like this.  0x0001 | 0x0002 => 0x0003.

```
computeRegUnitLaneMasks 0, 0000000000000000 |= 0000000000000001 -> 0000000000000001
computeRegUnitLaneMasks 0, 0000000000000001 |= 0000000000000002 -> 0000000000000003
```

The modified code works like this.  0x0001 & 0x0002 => 0x0000.

```
computeRegUnitLaneMasks 0, FFFFFFFFFFFFFFFF &= 0000000000000001 -> 0000000000000001
computeRegUnitLaneMasks 0, 0000000000000001 &= 0000000000000002 -> 0000000000000000 
```

This calculation causes none() LaneBitmask in the 1st problem.

I'm still investigating these problems.  So, I'm not sure what is the best, just reverting previous patch or solving this problem. But, I know above patch is causing above problems as a fact.  So, I just decide to let you guys know it.  Thanks.
</pre>
<img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