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

    <tr>
        <th>Summary</th>
        <td>
            Cached disposition of [...] is incorrect: cached Variant, actual Invariant for opt -verify-scev -passes="loop-mssa(simple-loop-unswitch<nontrivial>),loop(loop-unroll-full),loop-mssa(licm)"
        </td>
    </tr>

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

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

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

<pre>
    llvm commit: eef35c287ee0
Reproduce with: ```opt -verify-scev -passes="loop-mssa(simple-loop-unswitch<nontrivial>),loop(loop-unroll-full),loop-mssa(licm)" bbi-87260.ll -o /dev/null```
[bbi-87260.ll.gz](https://github.com/llvm/llvm-project/files/12812937/bbi-87260.ll.gz)

Result:
```
Cached disposition of %tobool2 for loop Loop at depth 1 containing: %for.body<header>,%land.rhs,%land.end<latch><exiting>
 is incorrect: cached Variant, actual Invariant
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ../../main-github/llvm/build-all/bin/opt -verify-scev -passes=loop-mssa(simple-loop-unswitch<nontrivial>),loop(loop-unroll-full),loop-mssa(licm) bbi-87260.ll -o /dev/null
 #0 0x000055b8d8a66627 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (../../main-github/llvm/build-all/bin/opt+0x2f26627)
 #1 0x000055b8d8a6417e llvm::sys::RunSignalHandlers() (../../main-github/llvm/build-all/bin/opt+0x2f2417e)
 #2 0x000055b8d8a66cef SignalHandler(int) (../../main-github/llvm/build-all/bin/opt+0x2f26cef)
 #3 0x00007f2a71620630 __restore_rt (/lib64/libpthread.so.0+0xf630)
 #4 0x00007f2a6ed67387 raise (/lib64/libc.so.6+0x36387)
 #5 0x00007f2a6ed68a78 abort (/lib64/libc.so.6+0x37a78)
 #6 0x000055b8d7c40e97 llvm::ScalarEvolution::verify() const (../../main-github/llvm/build-all/bin/opt+0x2100e97)
 #7 0x000055b8d95cc306 llvm::FunctionToLoopPassAdaptor::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (../../main-github/llvm/build-all/bin/opt+0x3a8c306)
 #8 0x000055b8d8c87c1d llvm::detail::PassModel<llvm::Function, llvm::FunctionToLoopPassAdaptor, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function> >::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (../../main-github/llvm/build-all/bin/opt+0x3147c1d)
 #9 0x000055b8d8487eb4 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function> >::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (../../main-github/llvm/build-all/bin/opt+0x2947eb4)
#10 0x000055b8d686268d llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function> >, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function> >::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (../../main-github/llvm/build-all/bin/opt+0xd2268d)
#11 0x000055b8d848c29e llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (../../main-github/llvm/build-all/bin/opt+0x294c29e)
#12 0x000055b8d686242d llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (../../main-github/llvm/build-all/bin/opt+0xd2242d)
#13 0x000055b8d8487044 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (../../main-github/llvm/build-all/bin/opt+0x2947044)
#14 0x000055b8d6467c33 llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (../../main-github/llvm/build-all/bin/opt+0x927c33)
#15 0x000055b8d647527c main (../../main-github/llvm/build-all/bin/opt+0x93527c)
#16 0x00007f2a6ed53555 __libc_start_main (/lib64/libc.so.6+0x22555)
#17 0x000055b8d6461dd0 _start (../../main-github/llvm/build-all/bin/opt+0x921dd0)
Abort (core dumped)
```
This starts happening with 2b6683fd5f74
```
Expand loop peeling phi computation to handle binary ops and casts

Summary:
Expand the capabilities of the code for computing how many peels are
needed to make phis determined.  A cast gets the peel count for the
value being casted while a binary op gets the maximum of the operands.

Respond to review comments: remove redundant asserts.

Author: Jamie Schmeiser <schmeise@ca.ibm.com>
Reviewed By:mkazantsev (Max Kazantsev),syzaara (Zaara Syeda)
Differential Revision: https://reviews.llvm.org/D138719
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkWF1z27gV_TXQC0YcCvzUgx8U2ZpuN5l64sw-9MVzCVyJaECAA4CylV_fASjZouJkN7HbbtsZjyWBwLnnHhxckheckzuNeEWKd6S4nsHgW2OvOvkZULVGdahnjRGHK6X2HeWm66Qn2YoibrOCs7pCTEl6TdLVR-ytEQNH-iB9G-aQMh3_TO_pfI9Wbg9zx3FP5z04h45k14QxZUw_75wDwmonu17hPA4N2j1Iz1uSrbXR3sq9BEWyG8KWhK3DFMLq40xrlJpvB6WeL54gleRdHGW0aeS8rliZJkrRuaGEbQTuCdvosPLEdkyHFO_Opye7L6S4Jqxuve8dyVaEbQjb7KRvhybhpiNsEyQ6fsx7a_6B3BO22UqFjrDNgtULtswqwjaXwGx5jHkU0g0qiHwcnPJaA29RUCFdb5z00mhqtpSwwpvGGMXo1lga8qfvwz_wVGDvW7qg3GgPUku9i5vDiq2xSdhbkq1bBIE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