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

    <tr>
        <th>Summary</th>
        <td>
            [Thumb] Thumb1's frame lowering emits invalid tMOVr for targets below V6-M
        </td>
    </tr>

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

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

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

<pre>
    When emitting BP-based accesses to the stack, Thumb1's frame lowering is making is of a non-flag-setting low-reg-to-low-reg tMOVr instruction that is invalid for Thumb targets that do not include ArmV6-M instructions.

This behaviour can be reproduced using the folloing test (based on a case from `llvm/test/CodeGen/Thumb/frame-access.ll`):

```
; RUN: llc -mtriple=thumb-eabi -frame-pointer=none %s -o - --verify-machineinstrs

; int g(int *, int, int, int, int, int);
; int h(int *, int *, int *);
declare dso_local i32 @g(i32*, i32, i32, i32, i32, i32) local_unnamed_addr
declare dso_local i32 @h(i32*, i32*, i32*) local_unnamed_addr

; VLAs, access via BP.
; int test_local_vla(int n) {
;   int v[n];
;   int x, y, z;
;   h(&x, &y, &z);
;   return g(v, x, y, z, 0, 0);
; }
define dso_local i32 @test_local_vla(i32 %n) local_unnamed_addr  {
entry:
  %x = alloca i32, align 4
  %y = alloca i32, align 4
  %z = alloca i32, align 4
  %vla = alloca i32, i32 %n, align 4
  %0 = bitcast i32* %x to i8*
  %1 = bitcast i32* %y to i8*
  %2 = bitcast i32* %z to i8*
  %call = call i32 @h(i32* nonnull %x, i32* nonnull %y, i32* nonnull %z)
  %3 = load i32, i32* %x, align 4
  %4 = load i32, i32* %y, align 4
  %5 = load i32, i32* %z, align 4
  %call1 = call i32 @g(i32* nonnull %vla, i32 %3, i32 %4, i32 %5, i32 0, i32 0)
  ret i32 %call1
}
```

Which results in:

```
*** Bad machine code: Non-flag-setting Thumb1 mov is v6-only ***                                                        
- function:    test_local_vla                                                                                           
- basic block: %bb.0 entry (0x7874a98)                                                                                  
- instruction: $r5 = tMOVr killed $r6, 14, $noreg                                                                       
                                                                                                                        
*** Bad machine code: Non-flag-setting Thumb1 mov is v6-only ***                                                        
- function:    test_local_vla                                                                                           
- basic block: %bb.0 entry (0x7874a98)                                                                                  
- instruction: $r2 = tMOVr killed $r6, 14, $noreg                                                                       
LLVM ERROR: Found 2 machine code errors.
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztV81y2zYQfhrqskMNBVKUdNBBjute7KTjSZ2jByRBETUIeABQifz03QUli7JkJ_Gk0x6q4RAr4ttfLLCLwlTb5ZdGaBCt9F7qNVz8ERfciQp4WQrnhANvwDcCnOflQ8Q-wOema4tJxGYOastbAcp8FZZ4pYOWP-woUwMHbXRcK76OnejFIza2Yh17E-9I8Def7ixI7bztSi-NRnXckwipN1zJCmpje63guV0L73pEZVA-AnWpukrAyrZ3eXwzlOTGUXIZJav-_blBmYVo-EaazkLJNf4DKx6tqboSXe4cmUjO1kYpE_4I5yFi8z4maBtHPocAa1qI8kSpTRuxK4Lh8MFU4nehkQrm4hgiFPexHCuFHBFbROlqaBd97J_-b3oBt39-RBAoVULceisflYjSS09CY8ELCXEv-BGN9MLiHEZaoKFTB7GBGOJ4g2tSb-OWl43UIgTFHWlFNcgMa_SOxoitaHWRfHtA6y-OJTQnEk7IA1MlSsWtgMqZe2VKrkCmDKIsCXakbM9J1FvDAgL3fac1BqK651Vlv6OhOdVwRL0u8uDv3fXKEU-_orCRHHfM-DgelAy95vuN4rvgaJIfzQahgwDeRNMLHU0vj6LaT30jRVt6Pb2YJU8ilgcAjtvd-PRydQCz23dWh0XeEGooE1_J7nXMFs0u96GsMXdOI3nqIX1nU30-iHBwXGhvt8_5D8T0DTB9gSvi2y8xbvu1hmyA2v4Q6umHUGjzGdzBh3M8SeAopMft73cp01uP56OcUwIdwJNXwNtzYPYK-OkcGEOrAj4QJ4lNB67uCIKWHXJ7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