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

    <tr>
        <th>Summary</th>
        <td>
            LLC Crash [with To = MCTargetExpr; From = const MCExpr]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed
        </td>
    </tr>

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

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

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

<pre>
    Hi, when I compile the following program 

```cpp
#define TAN_MAGIC  0x000002B7

double tan_helper(double arg)
{
    int magic = TAN_MAGIC;
    asm("flds %1; fdivr %%st %%st(1); fistpl %0"
        : "=m" (arg) : "m" (magic));
    return arg;
}

static double tan(double y)
{
 return tan_helper(y);
}

double f = 1.0;

double foo(int i){
    if (f == 1.0){
        if ((12 > i && i + 2 < 10) || (2 * i > 29)){
            for(int i = 0; i < 10000; i++){
                f = tan(tan_helper(i));
            }
            return f;
 }
        else 
            return tan_helper(tan_helper(i));
 }
    else {
        return f / tan(i);
    }
}

int main() {
    int result = 0;
    result = foo(42);
    return 0;
}

```

with 

```bash
clang++ -O0 a.cpp -S -emit-llvm -o a.ll
llc -relocation-model=pic a.ll -O3 -filetype=obj -mtriple=x86_64-unknown-linux-gnu -o a.o
```

I got this error message:

```
llc: llvm/include/llvm/Support/Casting.h:578: decltype(auto) llvm::cast(From*) [with To = MCTargetExpr; From = const MCExpr]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: llc -relocation-model=pic a.ll -O3 -filetype=obj -mtriple=x86_64-unknown-linux-gnu -o a.o
1.      Running pass 'Function Pass Manager' on module 'a.ll'.
2.      Running pass 'X86 Assembly Printer' on function '@_Z10tan_helperd'
 #0 0x00000000025875d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/runyu/projects/llvm-project/llvm/lib/Support/Unix/Signals.inc:723:22
 #1 0x0000000002587a1b PrintStackTraceSignalHandler(void*) /home/runyu/projects/llvm-project/llvm/lib/Support/Unix/Signals.inc:798:1
 #2 0x000000000258519b llvm::sys::RunSignalHandlers() /home/runyu/projects/llvm-project/llvm/lib/Support/Signals.cpp:105:20
 #3 0x0000000002586fad SignalHandler(int) /home/runyu/projects/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
 #4 0x00007fe65380f420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #5 0x00007fe65320c00b raise /build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #6 0x00007fe6531eb859 abort /build/glibc-SzIz7B/glibc-2.31/stdlib/abort.c:81:7
 #7 0x00007fe6531eb729 get_sysdep_segment_value /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:509:8
 #8 0x00007fe6531eb729 _nl_load_domain /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:970:34
 #9 0x00007fe6531fcfd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
#10 0x0000000001de1630 decltype(auto) llvm::cast<llvm::MCTargetExpr, llvm::MCExpr const>(llvm::MCExpr const*) /home/runyu/projects/llvm-project/llvm/include/llvm/Support/Casting.h:579:38
#11 0x0000000001dea1e4 llvm::MCExpr::evaluateAsRelocatableImpl(llvm::MCValue&, llvm::MCAssembler const*, llvm::MCAsmLayout const*, llvm::MCFixup const*, llvm::DenseMap<llvm::MCSection const*, unsigned long, llvm::DenseMapInfo<llvm::MCSection const*, void>, llvm::detail::DenseMapPair<llvm::MCSection const*, unsigned long>> const*, bool) const /home/runyu/projects/llvm-project/llvm/lib/MC/MCExpr.cpp:936:51
#12 0x0000000001de8c84 llvm::MCExpr::evaluateAsAbsolute(long&, llvm::MCAssembler const*, llvm::MCAsmLayout const*, llvm::DenseMap<llvm::MCSection const*, unsigned long, llvm::DenseMapInfo<llvm::MCSection const*, void>, llvm::detail::DenseMapPair<llvm::MCSection const*, unsigned long>> const*, bool) const /home/runyu/projects/llvm-project/llvm/lib/MC/MCExpr.cpp:583:32
#13 0x0000000001de8a82 llvm::MCExpr::evaluateAsAbsolute(long&) const /home/runyu/projects/llvm-project/llvm/lib/MC/MCExpr.cpp:542:1
#14 0x0000000002dcf9d7 (anonymous namespace)::AsmParser::parseExpression(llvm::MCExpr const*&, llvm::SMLoc&) /home/runyu/projects/llvm-project/llvm/lib/MC/MCParser/AsmParser.cpp:1535:3
#15 0x0000000000801aa0 (anonymous namespace)::X86AsmParser::parseATTOperand(llvm::SmallVectorImpl<std::unique_ptr<llvm::MCParsedAsmOperand, std::default_delete<llvm::MCParsedAsmOperand>>>&) /home/runyu/projects/llvm-project/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp:2711:7
#16 0x00000000007faf5a (anonymous namespace)::X86AsmParser::parseOperand(llvm::SmallVectorImpl<std::unique_ptr<llvm::MCParsedAsmOperand, std::default_delete<llvm::MCParsedAsmOperand>>>&, llvm::StringRef) /home/runyu/projects/llvm-project/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp:1742:34
#17 0x0000000000807a7e (anonymous namespace)::X86AsmParser::ParseInstruction(llvm::ParseInstructionInfo&, llvm::StringRef, llvm::SMLoc, llvm::SmallVectorImpl<std::unique_ptr<llvm::MCParsedAsmOperand, std::default_delete<llvm::MCParsedAsmOperand>>>&) /home/runyu/projects/llvm-project/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp:3433:7
#18 0x000000000082922e llvm::MCTargetAsmParser::ParseInstruction(llvm::ParseInstructionInfo&, llvm::StringRef, llvm::AsmToken, llvm::SmallVectorImpl<std::unique_ptr<llvm::MCParsedAsmOperand, std::default_delete<llvm::MCParsedAsmOperand>>>&) /home/runyu/projects/llvm-project/llvm/include/llvm/MC/MCParser/MCTargetAsmParser.h:445:3
#19 0x0000000002dd2a17 (anonymous namespace)::AsmParser::parseAndMatchAndEmitTargetInstruction((anonymous namespace)::ParseStatementInfo&, llvm::StringRef, llvm::AsmToken, llvm::SMLoc) /home/runyu/projects/llvm-project/llvm/lib/MC/MCParser/AsmParser.cpp:2319:58
#20 0x0000000002dd28f8 (anonymous namespace)::AsmParser::parseStatement((anonymous namespace)::ParseStatementInfo&, llvm::MCAsmParserSemaCallback*) /home/runyu/projects/llvm-project/llvm/lib/MC/MCParser/AsmParser.cpp:2309:47
#21 0x0000000002dccb91 (anonymous namespace)::AsmParser::Run(bool, bool) /home/runyu/projects/llvm-project/llvm/lib/MC/MCParser/AsmParser.cpp:1001:33
#22 0x00000000010f5937 llvm::AsmPrinter::emitInlineAsm(llvm::StringRef, llvm::MCSubtargetInfo const&, llvm::MCTargetOptions const&, llvm::MDNode const*, llvm::InlineAsm::AsmDialect) const /home/runyu/projects/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp:130:19
#23 0x00000000010f6ec0 llvm::AsmPrinter::emitInlineAsm(llvm::MachineInstr const*) const /home/runyu/projects/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp:417:16
#24 0x00000000010b92a8 llvm::AsmPrinter::emitFunctionBody() /home/runyu/projects/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:1690:9
#25 0x00000000008325bd llvm::X86AsmPrinter::runOnMachineFunction(llvm::MachineFunction&) /home/runyu/projects/llvm-project/llvm/lib/Target/X86/X86AsmPrinter.cpp:89:16
#26 0x00000000014e0f79 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) /home/runyu/projects/llvm-project/llvm/lib/CodeGen/MachineFunctionPass.cpp:91:33
#27 0x0000000001c4620d llvm::FPPassManager::runOnFunction(llvm::Function&) /home/runyu/projects/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1435:20
#28 0x0000000001c4646f llvm::FPPassManager::runOnModule(llvm::Module&) /home/runyu/projects/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1481:13
#29 0x0000000001c46885 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/runyu/projects/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1550:20
#30 0x0000000001c41b54 llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/runyu/projects/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:535:13
#31 0x0000000001c470af llvm::legacy::PassManager::run(llvm::Module&) /home/runyu/projects/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1678:1
#32 0x00000000007d4eab compileModule(char**, llvm::LLVMContext&) /home/runyu/projects/llvm-project/llvm/tools/llc/llc.cpp:754:66
#33 0x00000000007d2872 main /home/runyu/projects/llvm-project/llvm/tools/llc/llc.cpp:416:35
#34 0x00007fe6531ed083 __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:342:3
#35 0x00000000007d152e _start (/home/runyu/projects/llvm-project/build/bin/llc+0x7d152e)
[1]    2228906 abort      llc -relocation-model=pic a.ll -O3 -filetype=obj  -o a.o
```

I am using the LLVM-18 with `b00b4697ae79bd81c6b8107ba7d66b53654027cb` (Fri Aug 18 2023).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWltz27iS_jXwC0oqEOD1wQ-ybM24yp64Ym9qdl9UIAlKmIAAlwATa379FkBSvNiOHTv2zjl1XCmFwqW7v68b6BZAqjXfScZOQXAGgvMT2pi9qk__m8rdes_koaHyJFX54fR3DvAaft8zCS9hpsqKCwbNnsFCCaG-c7mDVa12NS0hQOcArbrPELX_sqrqWjDJWcElg3erP7bXq98u1xCie2T_8Fk0npyrJrVaqNzumahYDXDctdF6B3DSDY7O2gcIIeTSwJLueAYBOR9UADIaQ3UJcAwwLkSuIcCBB8gZLHL-rbbfAA60OT4AHHtWlR3BtamE7UEA40Ge_QNkBW0jOS8BxhDguDWx7-hbnW1WnhM5yKiZaWrpcPXNIDof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