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

    <tr>
        <th>Summary</th>
        <td>
            Clang assertion failure in CastInst::Create() with "Invalid cast!" when generating code for function with builtin expression
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    Compiler explorer: [https://godbolt.org/z/r55hMorrY](https://godbolt.org/z/r55hMorrY)
generated by fuzzer

**Code:**
```c
int v;

__attribute__((noinline, noclone)) void bar(void) { v++; }

#define T(n, t1, t2, tr, v1, v2, vr, b, o) \
  __attribute__((noinline, noclone)) tr t##n##_1##b(t1 x, t2 y) {              \
    tr r; \
    if (__builtin_##b##_overflow(x, y, &r))                                    \
 bar(); \
    return r; \
  } \
 \
  __attribute__((noinline, noclone)) tr t##n##_3##b(t2 y) {                    \
    tr r; \
    if (__builtin_##b##_overflow((v1), y, &r)) \
      bar(); \
    return r; \
  } \
 \
  __attribute__((noinline, noclone)) tr t##n##_4##b(t1 x) {                    \
    t2 y = (v2); \
    tr r; \
    if (__builtin_##b##_overflow(x, y, &r))                                    \
 bar(); \
    return r; \
  } \
 

#define ST(n, t, v1, v2, vr, b, o) T(n, t, t, t, v1, v2, vr, b, o)

#define TESTS(type, min, max) \
  ST(171, unsigned type, max, 3, 3 * (unsigned type)max, U(u, mul), 1)         


#define U(s, op) op
TESTS(_BitInt(119), (-332306998946228968225951765070086143wb - 1),
 332306998946228968225951765070086143wb)
```

**Stack trace:**
```
clang: /root/llvm-project/llvm/lib/IR/Instructions.cpp:3033: static llvm::CastInst* llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, const llvm::Twine&, llvm::InsertPosition): Assertion `castIsValid(op, S, Ty) && "Invalid cast!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang -gdwarf-4 -g -o /app/output.s -fno-verbose-asm -c --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics <source>
1.      <source>:36:1 <Spelling=<source>:20:3>: current parser token '__attribute__'
2.      <source>:36:1 <Spelling=<scratch space>:11:1>: LLVM IR generation of declaration 't171_3mul'
3.      <source>:36:1 <Spelling=<scratch space>:11:1>: Generating code for declaration 't171_3mul'
 #0 0x0000000003fb0f48 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3fb0f48)
 #1 0x0000000003faebd4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3faebd4)
 #2 0x0000000003ef3728 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x0000772769842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007727698969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x0000772769842476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007727698287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x000077276982871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x0000772769839e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x00000000038b5c54 (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x38b5c54)
#10 0x0000000000ff09b5 llvm::IRBuilderBase::CreateTrunc(llvm::Value*, llvm::Type*, llvm::Twine const&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xff09b5)
#11 0x0000000004847ac3 clang::CodeGen::CodeGenFunction::EmitBuiltinExpr(clang::GlobalDecl, unsigned int, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4847ac3)
#12 0x000000000432050e clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::CallBase**) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x432050e)
#13 0x0000000004392df4 (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) CGExprScalar.cpp:0:0
#14 0x0000000004385c20 clang::StmtVisitorBase<std::add_pointer, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) CGExprScalar.cpp:0:0
#15 0x000000000438bb9c clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x438bb9c)
#16 0x00000000042ef36d clang::CodeGen::CodeGenFunction::EvaluateExprAsBool(clang::Expr const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x42ef36d)
#17 0x000000000450ab4c clang::CodeGen::CodeGenFunction::EmitBranchOnBoolExpr(clang::Expr const*, llvm::BasicBlock*, llvm::BasicBlock*, unsigned long, clang::Stmt::Likelihood, clang::Expr const*, clang::VarDecl const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x450ab4c)
#18 0x000000000449f7b5 clang::CodeGen::CodeGenFunction::EmitIfStmt(clang::IfStmt const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x449f7b5)
#19 0x00000000044982ae clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x44982ae)
#20 0x00000000044a018c clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x44a018c)
#21 0x000000000450640e clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x450640e)
#22 0x0000000004519714 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4519714)
#23 0x0000000004583cad clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4583cad)
#24 0x000000000457eca4 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x457eca4)
#25 0x000000000457f353 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x457f353)
#26 0x0000000004589ef3 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#27 0x000000000496e0f6 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#28 0x000000000495dbd4 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x495dbd4)
#29 0x0000000006664364 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x6664364)
#30 0x000000000496a288 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x496a288)
#31 0x0000000004c640f5 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4c640f5)
#32 0x0000000004be13fe clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4be13fe)
#33 0x0000000004d565e9 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4d565e9)
#34 0x0000000000d821df cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xd821df)
#35 0x0000000000d78dea ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#36 0x00000000049d0d99 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#37 0x0000000003ef3bc4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3ef3bc4)
#38 0x00000000049d13af clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#39 0x000000000499344d clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x499344d)
#40 0x00000000049944de clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x49944de)
#41 0x000000000499c1e5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x499c1e5)
#42 0x0000000000d7e6ef clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xd7e6ef)
#43 0x0000000000c3c994 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc3c994)
#44 0x0000772769829d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#45 0x0000772769829e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#46 0x0000000000d78895 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xd78895)
clang: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzkWltz4yqX_TXkhXJKAl0f8iA7nZ6c6lOnq9PTU_PkQgjZmsigAuROzq-fAmRLKM7FfdxzqS_lyDKCzVqLDXtLgijVbDhjNyBegvj2ivR6K-TNI-k6bg9Xpaieb1Zi1zUtk5A9da2QTAJcQBAvt1p3CuACoDuA7jaiKkWrr4XcAHT3N0B3Mo63fwop_xPEtwBlH6-OchAUG8aZJJpVsHyGdf_330yCoDAfZD4rUTFrzH6CAiSB-1AQFA3XcA_w0jVYr4nWsil7zdZrgDKAMi4a3jacAbSCXNBWmNMcoBzuRVPBkkiAMnNqikC6hHuAluaDlxCktwcguGJ1wxn8bkwaWzq0R2SP0hz3tmRvS_a2pDQHYQ3HKxAUEJ4FUEuoAcIAYe6-1qH7LgHKdAifHAL4fIDu_R26hMaOtHSOJU0NAcrW67JvWt3w9dGs7UXsmaxb8ROgzHbxbA4AJXLA9YG_oSunrmnmdS-Z7iX3QYH09nB-IbHwRKzXRLqgVMaNQgvmpWKjOfi_q0k0d6CPaILgMwT41uhgvHsG_P-dd81m9MM4pd-bxX5N_ZE2L9aPTw_fH4z6z50dsF1jDe7I09RPLKYwtWZ7bpfuCh6bEKsbtgcIUGHUntXKh0r_bi7ZRn07-GY4FXmAN0VomigLvzM1RQeC4oB6vWz0PdcGXJgP9gDKFhgjHCR5nuVRglCWJxlCcR6HaRIHaRBkSRjhnyVcwGGCGJIfazNIeFjyp3HhQRP6CLUk9HR4AEFBW8I3NoahOymEBuiubfe7RSfFfzF6-Gm-mhKgu_tv5sCVlj3VjeDqmnYdwAUOMDZWlCa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