<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/57335>57335</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SCEV] Assertion `isAvailableAtLoopEntry(OrigRHS, L) && "Must be!"' failed.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
max-quazan
</td>
</tr>
</table>
<pre>
https://godbolt.org/z/5Y9df9YsY
Run `opt -passes=irce -S` on the following test:
```
define void @test() {
bb:
%tmp = icmp ult i32 0, undef
br i1 %tmp, label %bb1, label %bb31
bb1: ; preds = %bb
br label %bb5
bb2: ; preds = %bb20
%tmp3 = phi i32 [ %tmp21, %bb20 ]
%tmp4 = add nuw nsw i32 %tmp3, 100
br label %bb5
bb5: ; preds = %bb2, %bb1
%tmp6 = phi i32 [ 102, %bb1 ], [ %tmp4, %bb2 ]
%tmp7 = phi i32 [ 2, %bb1 ], [ %tmp3, %bb2 ]
br label %bb12
bb8: ; preds = %bb12
%tmp9 = phi i32 [ %tmp14, %bb12 ]
%tmp10 = add nsw i32 %tmp9, -1
%tmp11 = icmp ult i32 %tmp10, undef
br i1 %tmp11, label %bb12, label %bb32
bb12: ; preds = %bb8, %bb5
%tmp13 = phi i32 [ 1, %bb5 ], [ %tmp9, %bb8 ]
%tmp14 = add nuw nsw i32 %tmp13, 1
%tmp15 = load atomic i32, i32 addrspace(1)* undef unordered, align 8
%tmp16 = icmp ult i32 %tmp14, %tmp6
br i1 %tmp16, label %bb8, label %bb17
bb17: ; preds = %bb12
%tmp18 = phi i32 [ %tmp15, %bb12 ]
%tmp19 = icmp ult i32 %tmp7, %tmp18
br i1 %tmp19, label %bb20, label %bb33
bb20: ; preds = %bb17
%tmp21 = add nuw nsw i32 %tmp7, 2
br i1 undef, label %bb22, label %bb2
bb22: ; preds = %bb20
%tmp23 = phi i32 [ %tmp18, %bb20 ]
br label %bb24
bb24: ; preds = %bb30, %bb22
%tmp25 = phi i32 [ 3, %bb22 ], [ %tmp26, %bb30 ]
%tmp26 = add i32 %tmp25, 1
%tmp27 = icmp ult i32 %tmp26, %tmp23
%tmp28 = and i1 undef, %tmp27
br i1 %tmp28, label %bb30, label %bb29
bb29: ; preds = %bb24
ret void
bb30: ; preds = %bb24
br label %bb24
bb31: ; preds = %bb
ret void
bb32: ; preds = %bb8
unreachable
bb33: ; preds = %bb17
ret void
}
```
Failure:
```
opt: /root/llvm-project/llvm/lib/Analysis/ScalarEvolution.cpp:12706: llvm::ScalarEvolution::ExitLimit llvm::ScalarEvolution::howManyLessThans(const llvm::SCEV*, const llvm::SCEV*, const llvm::Loop*, bool, bool, bool): Assertion `isAvailableAtLoopEntry(OrigRHS, L) && "Must be!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=irce <source>
#0 0x000055f8639273a4 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
#1 0x000055f863924b64 SignalHandler(int) Signals.cpp:0:0
#2 0x00007f384064a420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
#3 0x00007f384011700b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
#4 0x00007f38400f6859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
#5 0x00007f38400f6729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
#6 0x00007f3840107fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
#7 0x000055f8627b5031 llvm::ScalarEvolution::howManyLessThans(llvm::SCEV const*, llvm::SCEV const*, llvm::Loop const*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x268e031)
#8 0x000055f8627b53f3 llvm::ScalarEvolution::computeExitLimitFromICmp(llvm::Loop const*, llvm::CmpInst::Predicate, llvm::SCEV const*, llvm::SCEV const*, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x268e3f3)
#9 0x000055f8627b59e8 llvm::ScalarEvolution::computeExitLimitFromICmp(llvm::Loop const*, llvm::ICmpInst*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x268e9e8)
#10 0x000055f8627b5dd2 llvm::ScalarEvolution::computeExitLimitFromCondImpl(llvm::ScalarEvolution::ExitLimitCache&, llvm::Loop const*, llvm::Value*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x268edd2)
#11 0x000055f8627b62b3 llvm::ScalarEvolution::computeExitLimitFromCondCached(llvm::ScalarEvolution::ExitLimitCache&, llvm::Loop const*, llvm::Value*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x268f2b3)
#12 0x000055f8627b6662 llvm::ScalarEvolution::computeExitLimitFromCondFromBinOp(llvm::ScalarEvolution::ExitLimitCache&, llvm::Loop const*, llvm::Value*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x268f662)
#13 0x000055f8627b5b71 llvm::ScalarEvolution::computeExitLimitFromCondImpl(llvm::ScalarEvolution::ExitLimitCache&, llvm::Loop const*, llvm::Value*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x268eb71)
#14 0x000055f8627b62b3 llvm::ScalarEvolution::computeExitLimitFromCondCached(llvm::ScalarEvolution::ExitLimitCache&, llvm::Loop const*, llvm::Value*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x268f2b3)
#15 0x000055f8627b63ed llvm::ScalarEvolution::computeExitLimitFromCond(llvm::Loop const*, llvm::Value*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x268f3ed)
#16 0x000055f862783888 llvm::ScalarEvolution::computeExitLimit(llvm::Loop const*, llvm::BasicBlock*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x265c888)
#17 0x000055f862783f01 llvm::ScalarEvolution::computeBackedgeTakenCount(llvm::Loop const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x265cf01)
#18 0x000055f862784aa8 llvm::ScalarEvolution::getBackedgeTakenInfo(llvm::Loop const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x265daa8)
#19 0x000055f862785329 llvm::ScalarEvolution::getBackedgeTakenCount(llvm::Loop const*, llvm::ScalarEvolution::ExitCountKind) (.constprop.0) ScalarEvolution.cpp:0:0
#20 0x000055f86278792f llvm::ScalarEvolution::getRangeRef(llvm::SCEV const*, llvm::ScalarEvolution::RangeSignHint) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x266092f)
#21 0x000055f8627887c7 StrengthenNoWrapFlags(llvm::ScalarEvolution*, llvm::SCEVTypes, llvm::ArrayRef<llvm::SCEV const*>, llvm::SCEV::NoWrapFlags) ScalarEvolution.cpp:0:0
#22 0x000055f862795757 llvm::ScalarEvolution::getAddExpr(llvm::SmallVectorImpl<llvm::SCEV const*>&, llvm::SCEV::NoWrapFlags, unsigned int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x266e757)
#23 0x000055f86278ee60 llvm::ScalarEvolution::createSCEV(llvm::Value*) (.part.0) ScalarEvolution.cpp:0:0
#24 0x000055f86278f8a9 llvm::ScalarEvolution::createSCEVIter(llvm::Value*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x26688a9)
#25 0x000055f8635baa39 (anonymous namespace)::InductiveRangeCheck::extractRangeChecksFromCond(llvm::Loop*, llvm::ScalarEvolution&, llvm::Use&, llvm::SmallVectorImpl<(anonymous namespace)::InductiveRangeCheck>&, llvm::SmallPtrSetImpl<llvm::Value*>&) InductiveRangeCheckElimination.cpp:0:0
#26 0x000055f8635bacbf (anonymous namespace)::InductiveRangeCheck::extractRangeChecksFromCond(llvm::Loop*, llvm::ScalarEvolution&, llvm::Use&, llvm::SmallVectorImpl<(anonymous namespace)::InductiveRangeCheck>&, llvm::SmallPtrSetImpl<llvm::Value*>&) InductiveRangeCheckElimination.cpp:0:0
#27 0x000055f8635be609 (anonymous namespace)::InductiveRangeCheckElimination::run(llvm::Loop*, llvm::function_ref<void (llvm::Loop*, bool)>) InductiveRangeCheckElimination.cpp:0:0
#28 0x000055f8635c2637 llvm::IRCEPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x349b637)
#29 0x000055f863c91ce6 llvm::detail::PassModel<llvm::Function, llvm::IRCEPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x3b6ace6)
#30 0x000055f86303a93a llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x2f1393a)
#31 0x000055f86118e096 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>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x1067096)
#32 0x000055f8630394a6 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x2f124a6)
#33 0x000055f86118ea96 llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x1067a96)
#34 0x000055f86303754e llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x2f1054e)
#35 0x000055f860d69ce8 llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::StringRef>, llvm::ArrayRef<llvm::PassPlugin>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xc42ce8)
#36 0x000055f860c8ecca main (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xb67cca)
#37 0x00007f38400f8083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#38 0x000055f860d5c69e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xc3569e)
Compiler returned: 139
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztW1tT4zgW_jXhRQVlS77IDzyEdKimlt6hgO2tfqJkW0m87UheWaZhfv0c2blJcULChK7dnqEgiS3p6HzfuehIManMXy9nWlf1gAwH-Bp-pzJPZakvpJrC1e_wF35L8knyrf428D4NvGH3et8INIg8WWl0XrG65iDhU6Eyjs4f4D6SAukZRxNZlvJHIaZI81qbSToZkbf4bS9zPikER8-yyNEg8NqumA5wggbxVdclTVeDERrgUM8rBDOiIoMPTalRQTDyBniEGgHilj1ThQp_0d80lizlpblOU9-5Jv4mPtNOQMAxPwNyhSrF87rVrBW6ocfGVKE9Ez7FTNiz2SFtUzUrWmYG4dXiPm5xL4bA_U_2sKAdxvIcieYHEvWPbngn0oz0Pe8gUOFJQK109W01oy10vrfRucVlrlawgzXqbdDxlrS9ski_LJsOH9t80FPwsRS6VDzZYWR_Ddfvwet7aytbFk7MsHOHa9_fjrSlnL0B57sh5mM35hyS_KNDoYckusIeOkC2Y2IdDOG2nZNVI-3hcF-k-F2oOCPCdkQ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