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

    <tr>
        <th>Summary</th>
        <td>
            [AsmPrinter] Global Constant with Big Endian crashes backend with assertion `ShiftAmt <= BitWidth && "Invalid shift amount"` failed
        </td>
    </tr>

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

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

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

<pre>
    ## Description

When targeting `aarch64_be`, the following code crashes the backend with error `ShiftAmt <= BitWidth && "Invalid shift amount"`.

This does not occur when targeting `aarch64`.

## Minimal Reproduction

https://godbolt.org/z/sf17n5Me6

### Code

```ll
@G = global <4 x i1> <i1 true, i1 false, i1 true, i1 true>
```

### Stack Trace

```console
llc: /home/henry/aflplusplus-isel/llvm-project/llvm/include/llvm/ADT/APInt.h:840: void llvm::APInt::lshrInPlace(unsigned int): Assertion `ShiftAmt <= BitWidth && "Invalid shift amount"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /home/henry/aflplusplus-isel/llvm-project/build-debug/bin/llc -mtriple=aarch64_be ./crash-reports/dagisel-aarch64_be/1.ll
 #0 0x00000000047c438a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:569:11
 #1 0x00000000047c453b PrintStackTraceSignalHandler(void*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:636:1
 #2 0x00000000047c2b76 llvm::sys::RunSignalHandlers() /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Signals.cpp:104:5
 #3 0x00000000047c4c65 SignalHandler(int) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:407:1
 #4 0x00007f37d8ff5980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #5 0x00007f37d7ee5e87 raise /build/glibc-CVJwZb/glibc-2.27/signal/../sysdeps/unix/sysv/linux/raise.c:51:0
 #6 0x00007f37d7ee77f1 abort /build/glibc-CVJwZb/glibc-2.27/stdlib/abort.c:81:0
 #7 0x00007f37d7ed73fa __assert_fail_base /build/glibc-CVJwZb/glibc-2.27/assert/assert.c:89:0
 #8 0x00007f37d7ed7472 (/lib/x86_64-linux-gnu/libc.so.6+0x30472)
 #9 0x0000000000db84e6 llvm::APInt::lshrInPlace(unsigned int) /home/henry/aflplusplus-isel/llvm-project/llvm/include/llvm/ADT/APInt.h:0:5
#10 0x00000000031b908a emitGlobalConstantLargeInt(llvm::ConstantInt const*, llvm::AsmPrinter&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:3272:5
#11 0x00000000031b9bba emitGlobalConstantVector(llvm::DataLayout const&, llvm::ConstantVector const*, llvm::AsmPrinter&, llvm::DenseMap<unsigned long, llvm::SmallVector<llvm::GlobalAlias const*, 1u>, llvm::DenseMapInfo<unsigned long, void>, llvm::detail::DenseMapPair<unsigned long, llvm::SmallVector<llvm::GlobalAlias const*, 1u>>>*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:3147:19
#12 0x00000000031b6993 emitGlobalConstantImpl(llvm::DataLayout const&, llvm::Constant const*, llvm::AsmPrinter&, llvm::Constant const*, unsigned long, llvm::DenseMap<unsigned long, llvm::SmallVector<llvm::GlobalAlias const*, 1u>, llvm::DenseMapInfo<unsigned long, void>, llvm::detail::DenseMapPair<unsigned long, llvm::SmallVector<llvm::GlobalAlias const*, 1u>>>*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:3458:5
#13 0x00000000031aaba8 llvm::AsmPrinter::emitGlobalConstant(llvm::DataLayout const&, llvm::Constant const*, llvm::DenseMap<unsigned long, llvm::SmallVector<llvm::GlobalAlias const*, 1u>, llvm::DenseMapInfo<unsigned long, void>, llvm::detail::DenseMapPair<unsigned long, llvm::SmallVector<llvm::GlobalAlias const*, 1u>>>*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:3478:5
#14 0x00000000031aa539 llvm::AsmPrinter::emitGlobalVariable(llvm::GlobalVariable const*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:861:3
#15 0x00000000031b1c80 llvm::AsmPrinter::doFinalization(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:2069:22
#16 0x0000000003c71d3e llvm::FPPassManager::doFinalization(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1494:16
#17 0x0000000003c6d93c (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1581:16
#18 0x0000000003c6d22d llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:535:16
#19 0x0000000003c71ef1 llvm::legacy::PassManager::run(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1672:3
#20 0x0000000000d1edec compileModule(char**, llvm::LLVMContext&) /home/henry/aflplusplus-isel/llvm-project/llvm/tools/llc/llc.cpp:737:41
#21 0x0000000000d1d192 main /home/henry/aflplusplus-isel/llvm-project/llvm/tools/llc/llc.cpp:418:13
#22 0x00007f37d7ec8c87 __libc_start_main /build/glibc-CVJwZb/glibc-2.27/csu/../csu/libc-start.c:344:0
#23 0x0000000000d1c99a _start (/home/henry/aflplusplus-isel/llvm-project/build-debug/bin/llc+0xd1c99a)
```

## Cause

The following code from function `emitGlobalConstantLargeInt` in `AsmPrinter.cpp` tries to right shift `Realigned` by an amount `ExtraBitSize` in place. However, if the constant to emit has a bit width less than 64 and the bit width is not a multiple of 8, the shift amount will be greater than the bit width of `Realigned`, which causes the crash. 

https://github.com/llvm/llvm-project/blob/aecae68b074fcfce09a6d2f0eab73a1ef50300fc/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp#L3243-L3274
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWUlz27gS_jXSBSUV9-Wggyw7iafsGlecyVS9iwoEQRHvgYAKAL3k108DlMQlnoyf49iXcckkiKW7v-5GowEUsnxczYIQfuicaqLY3jApZt75zFt3zz9rKpDBakcNEzs0SzyMFamTaFtQ-JgFG2RqiirJuby3PYgsKSIK65pq11Rg8j8qSnTPTI2oUlJZKrc1q8y6MWgWbmbhOTpj5k9WQo9ZkMAPXsGluMOclUjbrgg3shUGqmHwcijhl5ppVErgJqRBkpBWofu_k3o6-AD-mgnWYI4-072SZUumWqiN2etZCN0_wG8ny0Jys5RqB1_f4F9Xfiria5p8T9uS34BORi2gOPfj_FAReR-RVcOOywLkAKVE6AExfxZe2A_mI6NaarUNxQpzfSwPql0xvJiweFqiWwNWQV8UJk8LRqTQkh_aOCeAHUzyoZYNtS8q1CO8ccX3vNX2f8E05VDF-V2zACX-lxJz-IQXE4S3Je0r1udf7PPmUphlDcSzyLMs7iTY23UBXYdr194Vua7VpbjhVuAga4VmO0FLxKxL5HboWmuqrN1-0ruCFPTLOC0PfnJzdbG-vUC6LRoG_VDR7pCie6kMMhJNHANcvC2WRDY90olCmNYt1VBAWFjxnV7cPHFzxs0WY81yYN8ZqmybveXiqqwH5zdK7hRuEPh421Bh9EstVLSMl4uSAi77xYTrQtCiMRAOwAXC837GoyW0OkEXnQ4skhLvLOnFIDAEH_zl0bdBqtBD3oN3_ItSEoUZHthZP-qucKPAoA5y55pB1ndS-H4rtVEUN86Km4Pxf8YvOSvgedvuLRYo_SHYg60A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