JumpThreading leaves around useless phis when removing dead basic blocks
| Bugzilla Link | 33142 |
| Resolution | FIXED |
| Resolved on | Sep 26, 2018 00:38 |
| Version | trunk |
| OS | All |
| Reporter | LLVM Bugzilla Contributor |
| CC | @annamthomas,@efriedma-quic,@fhahn,@hfinkel,@mikaelholmen,@sanjoy |
Extended Description
The following code crashes the current trunk version at "-O2" and above with "-mllvm -enable-newgvn". Level "-O1" works fine.
$ clang-trunk --version clang version 5.0.0 (trunk 303617) Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /usr/local/bin
$ clang-trunk -mllvm -enable-newgvn abc.c -O2 abc.c:8:7: warning: incompatible pointer types assigning to 'long ' from 'void ()' [-Wincompatible-pointer-types] d = fn1; ^ ~~~ abc.c:11:14: warning: incompatible pointer to integer conversion assigning to 'char' from 'long ' [-Wint-conversion] d || (c = d); ^ ~ clang-5.0: /home/absozero/trunk/llvm/include/llvm/ADT/SmallVector.h:148: llvm::SmallVectorTemplateCommon::reference llvm::SmallVectorTemplateCommon<llvm::Value , void>::operator [T = llvm::Value ]: Assertion `idx < size()' failed. #0 0x0000000001cf8624 PrintStackTraceSignalHandler(void) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x1cf8624) #1 0x0000000001cf8946 SignalHandler(int) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x1cf8946) #2 0x00007ff42fe22330 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x10330) #3 0x00007ff42e99fc37 gsignal /build/eglibc-oGUzwX/eglibc-2.19/signal/../nptl/sysdeps/unix/sysv/linux/raise.c:56:0 #4 0x00007ff42e9a3028 abort /build/eglibc-oGUzwX/eglibc-2.19/stdlib/abort.c:91:0 #5 0x00007ff42e998bf6 __assert_fail_base /build/eglibc-oGUzwX/eglibc-2.19/assert/assert.c:92:0 #6 0x00007ff42e998ca2 (/lib/x86_64-linux-gnu/libc.so.6+0x2fca2) #7 0x00000000013664ff llvm::BasicAAResult::aliasPHI(llvm::PHINode const, unsigned long, llvm::AAMDNodes const&, llvm::Value const, unsigned long, llvm::AAMDNodes const&, llvm::Value const) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x13664ff) #8 0x0000000001363341 llvm::BasicAAResult::aliasCheck(llvm::Value const, unsigned long, llvm::AAMDNodes, llvm::Value const*, unsigned long, llvm::AAMDNodes, llvm::Value const*, llvm::Value const*) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x1363341) #9 0x0000000001362c1f llvm::BasicAAResult::alias(llvm::MemoryLocation const&, llvm::MemoryLocation const&) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x1362c1f) #10 0x0000000001352740 llvm::AAResults::getModRefInfo(llvm::StoreInst const*, llvm::MemoryLocation const&) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x1352740) #11 0x00000000013fb5c3 llvm::FindAvailablePtrLoadStore(llvm::Value*, llvm::Type*, bool, llvm::BasicBlock*, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, false, false>&, unsigned int, llvm::AAResults*, bool*, unsigned int*) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x13fb5c3) #12 0x0000000001b4789d llvm::JumpThreadingPass::SimplifyPartiallyRedundantLoad(llvm::LoadInst*) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x1b4789d) #13 0x0000000001b42c9a llvm::JumpThreadingPass::ProcessBlock(llvm::BasicBlock*) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x1b42c9a) #14 0x0000000001b42190 llvm::JumpThreadingPass::runImpl(llvm::Function&, llvm::TargetLibraryInfo*, llvm::LazyValueInfo*, llvm::AAResults*, bool, std::unique_ptr<llvm::BlockFrequencyInfo, std::default_deletellvm::BlockFrequencyInfo >, std::unique_ptr<llvm::BranchProbabilityInfo, std::default_deletellvm::BranchProbabilityInfo >) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x1b42190) #15 0x0000000001b4df8b (anonymous namespace)::JumpThreading::runOnFunction(llvm::Function&) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x1b4df8b) #16 0x000000000188127f llvm::FPPassManager::runOnFunction(llvm::Function&) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x188127f) #17 0x00000000026ff066 (anonymous namespace)::CGPassManager::runOnModule(llvm::Module&) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x26ff066) #18 0x00000000018819d5 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x18819d5) #19 0x0000000001e82beb clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::DataLayout const&, llvm::Module*, clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_deletellvm::raw_pwrite_stream >) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x1e82beb) #20 0x000000000258c907 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x258c907) #21 0x00000000029f0926 clang::ParseAST(clang::Sema&, bool, bool) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x29f0926) #22 0x000000000225da48 clang::FrontendAction::Execute() (/home/absozero/trunk/root-clang/bin/clang-5.0+0x225da48) #23 0x000000000220fea1 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x220fea1) #24 0x00000000022e17c5 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x22e17c5) #25 0x000000000083e824 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/home/absozero/trunk/root-clang/bin/clang-5.0+0x83e824) #26 0x000000000083c6e5 main (/home/absozero/trunk/root-clang/bin/clang-5.0+0x83c6e5) #27 0x00007ff42e98af45 __libc_start_main /build/eglibc-oGUzwX/eglibc-2.19/csu/libc-start.c:321:0 #28 0x00000000008397e9 _start (/home/absozero/trunk/root-clang/bin/clang-5.0+0x8397e9) Stack dump: 0. Program arguments: /home/absozero/trunk/root-clang/bin/clang-5.0 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -disable-free -main-file-name abc.c -mrelocation-model static -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -momit-leaf-frame-pointer -dwarf-column-info -debugger-tuning=gdb -resource-dir /home/absozero/trunk/root-clang/lib/clang/5.0.0 -internal-isystem /usr/local/include -internal-isystem /home/absozero/trunk/root-clang/lib/clang/5.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir /home/absozero/projects/reduction/crash -ferror-limit 19 -fmessage-length 172 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -mllvm -enable-newgvn -o /tmp/abc-edbe72.o -x c abc.c
-
<eof> parser at end of file -
Per-module optimization passes -
Running pass 'CallGraph Pass Manager' on module 'abc.c'. -
Running pass 'Jump Threading' on function '@fn1'
clang-5.0: error: unable to execute command: Aborted (core dumped) clang-5.0: error: clang frontend command failed due to signal (use -v to see invocation) clang version 5.0.0 (trunk 303617) Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /usr/local/bin clang-5.0: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script. clang-5.0: note: diagnostic msg:
PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed source(s) and associated run script(s) are located at: clang-5.0: note: diagnostic msg: /tmp/abc-2fc14e.c clang-5.0: note: diagnostic msg: /tmp/abc-2fc14e.sh clang-5.0: note: diagnostic msg:
$ cat abc.c int a; long b; char c; void fn1() { long *d = &b; for (; a <= 71;) { char e = e && b; d = fn1; } for (; a <= 2;) *d || (c = d); }
mentioned in issue llvm/llvm-bugzilla-archive#33994
I don't know about the original fault, but the one I reported in llvm/llvm-project#32489 #c19 crashes even after rL322401.
It stopped crashing recently with r338242 though:
[BasicAA] Use PhiValuesAnalysis if available when handling phi alias
By using PhiValuesAnalysis we can get all the values reachable from a phi, so
we can be more precise instead of giving up when a phi has phi operands. We
can't make BaseicAA directly use PhiValuesAnalysis though, as the user of
BasicAA may modify the function in ways that PhiValuesAnalysis can't cope with.
For this optional usage to work correctly BasicAAWrapperPass now needs to be not
marked as CFG-only (i.e. it is now invalidated even when CFG is preserved) due
to how the legacy pass manager handles dependent passes being invalidated,
namely the depending pass still has a pointer to the now-dead dependent pass.
Differential Revision: https://reviews.llvm.org/D44564
It's not clear to me if that really fixes the problem or it just does things differently so the bug isn't triggered in this case anymore.
Anyway, it doesn't crash on trunk now so if something like this pops up again I'll open a new issue.
The reproducers attached here compile without crashes now. I suspect this was fixed by https://reviews.llvm.org/rL322401, which preserves DT & LVI in jump threading and avoids threading over blocks marked for deletion.
No update... except that jump-threading is probably not going to get incrementally updated domtrees due to performance issues.
I stumbled upon this as well.
opt -jump-threading -o /dev/null red.ll
gives
opt: ../include/llvm/ADT/SmallVector.h:148: reference llvm::SmallVectorTemplateCommon<llvm::Value , void>::operator [T = llvm::Value ]: Assertion `idx < size()' failed. #0 0x000000000311a99c llvm::sys::PrintStackTrace(llvm::raw_ostream&) /data/repo/llvm-dev2/build-all-Debug/../lib/Support/Unix/Signals.inc:398:5 #1 0x000000000311ab49 PrintStackTraceSignalHandler(void) /data/repo/llvm-dev2/build-all-Debug/../lib/Support/Unix/Signals.inc:462:1 #2 0x0000000003119153 llvm::sys::RunSignalHandlers() /data/repo/llvm-dev2/build-all-Debug/../lib/Support/Signals.cpp:50:5 #3 0x000000000311b18e SignalHandler(int) /data/repo/llvm-dev2/build-all-Debug/../lib/Support/Unix/Signals.inc:252:1 #4 0x00007f23d0cba330 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x10330) #5 0x00007f23cf8a9c37 gsignal /build/eglibc-SvCtMH/eglibc-2.19/signal/../nptl/sysdeps/unix/sysv/linux/raise.c:56:0 #6 0x00007f23cf8ad028 abort /build/eglibc-SvCtMH/eglibc-2.19/stdlib/abort.c:91:0 #7 0x00007f23cf8a2bf6 __assert_fail_base /build/eglibc-SvCtMH/eglibc-2.19/assert/assert.c:92:0 #8 0x00007f23cf8a2ca2 (/lib/x86_64-linux-gnu/libc.so.6+0x2fca2) #9 0x0000000001fb5e20 llvm::SmallVectorTemplateCommon<llvm::Value, void>::operator[](unsigned long) /data/repo/llvm-dev2/build-all-Debug/../include/llvm/ADT/SmallVector.h:148:5 #10 0x0000000001fb5e20 llvm::BasicAAResult::aliasPHI(llvm::PHINode const*, unsigned long, llvm::AAMDNodes const&, llvm::Value const*, unsigned long, llvm::AAMDNodes const&, llvm::Value const*) /data/repo/llvm-dev2/build-all-Debug/../lib/Analysis/BasicAliasAnalysis.cpp:1485:0 #11 0x0000000001fb1bc6 llvm::BasicAAResult::aliasCheck(llvm::Value const*, unsigned long, llvm::AAMDNodes, llvm::Value const*, unsigned long, llvm::AAMDNodes, llvm::Value const*, llvm::Value const*) /data/repo/llvm-dev2/build-all-Debug/../lib/Analysis/BasicAliasAnalysis.cpp:1633:17 #12 0x0000000001fb139b llvm::BasicAAResult::alias(llvm::MemoryLocation const&, llvm::MemoryLocation const&) /data/repo/llvm-dev2/build-all-Debug/../lib/Analysis/BasicAliasAnalysis.cpp:719:15 #13 0x0000000001f9d979 llvm::AAResults::Modelllvm::BasicAAResult::alias(llvm::MemoryLocation const&, llvm::MemoryLocation const&) /data/repo/llvm-dev2/build-all-Debug/../include/llvm/Analysis/AliasAnalysis.h:723:5 #14 0x0000000001f96769 llvm::AAResults::alias(llvm::MemoryLocation const&, llvm::MemoryLocation const&) /data/repo/llvm-dev2/build-all-Debug/../lib/Analysis/AliasAnalysis.cpp:96:10 #15 0x0000000001f976a9 llvm::AAResults::getModRefInfo(llvm::StoreInst const*, llvm::MemoryLocation const&) /data/repo/llvm-dev2/build-all-Debug/../lib/Analysis/AliasAnalysis.cpp:357:9 #16 0x000000000213c5cc llvm::AAResults::getModRefInfo(llvm::StoreInst const*, llvm::Value const*, unsigned long) /data/repo/llvm-dev2/build-all-Debug/../include/llvm/Analysis/AliasAnalysis.h:441:5 #17 0x000000000213c0ce llvm::FindAvailablePtrLoadStore(llvm::Value*, llvm::Type*, bool, llvm::BasicBlock*, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, false, false>&, unsigned int, llvm::AAResults*, bool*, unsigned int*) /data/repo/llvm-dev2/build-all-Debug/../lib/Analysis/Loads.cpp:417:18 #18 0x000000000213bd16 llvm::FindAvailableLoadedValue(llvm::LoadInst*, llvm::BasicBlock*, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, false, false>&, unsigned int, llvm::AAResults*, bool*, unsigned int*) /data/repo/llvm-dev2/build-all-Debug/../lib/Analysis/Loads.cpp:331:3 #19 0x0000000002e3ffad llvm::JumpThreadingPass::SimplifyPartiallyRedundantLoad(llvm::LoadInst*) /data/repo/llvm-dev2/build-all-Debug/../lib/Transforms/Scalar/JumpThreading.cpp:1082:14 #20 0x0000000002e3c06c llvm::JumpThreadingPass::ProcessBlock(llvm::BasicBlock*) /data/repo/llvm-dev2/build-all-Debug/../lib/Transforms/Scalar/JumpThreading.cpp:979:9 #21 0x0000000002e3b07c llvm::JumpThreadingPass::runImpl(llvm::Function&, llvm::TargetLibraryInfo*, llvm::LazyValueInfo*, llvm::AAResults*, bool, std::unique_ptr<llvm::BlockFrequencyInfo, std::default_deletellvm::BlockFrequencyInfo >, std::unique_ptr<llvm::BranchProbabilityInfo, std::default_deletellvm::BranchProbabilityInfo >) /data/repo/llvm-dev2/build-all-Debug/../lib/Transforms/Scalar/JumpThreading.cpp:211:7 #22 0x0000000002e460d5 (anonymous namespace)::JumpThreading::runOnFunction(llvm::Function&) /data/repo/llvm-dev2/build-all-Debug/../lib/Transforms/Scalar/JumpThreading.cpp:140:10 #23 0x000000000294198d llvm::FPPassManager::runOnFunction(llvm::Function&) /data/repo/llvm-dev2/build-all-Debug/../lib/IR/LegacyPassManager.cpp:1514:23 #24 0x0000000002941cc5 llvm::FPPassManager::runOnModule(llvm::Module&) /data/repo/llvm-dev2/build-all-Debug/../lib/IR/LegacyPassManager.cpp:1535:16 #25 0x00000000029424ae (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /data/repo/llvm-dev2/build-all-Debug/../lib/IR/LegacyPassManager.cpp:1591:23 #26 0x0000000002941fab llvm::legacy::PassManagerImpl::run(llvm::Module&) /data/repo/llvm-dev2/build-all-Debug/../lib/IR/LegacyPassManager.cpp:1694:16 #27 0x00000000029429f1 llvm::legacy::PassManager::run(llvm::Module&) /data/repo/llvm-dev2/build-all-Debug/../lib/IR/LegacyPassManager.cpp:1725:3 #28 0x0000000000b1b63f main /data/repo/llvm-dev2/build-all-Debug/../tools/opt/opt.cpp:756:3 #29 0x00007f23cf894f45 __libc_start_main /build/eglibc-SvCtMH/eglibc-2.19/csu/libc-start.c:321:0 #30 0x0000000000af4029 _start (build-all-Debug/bin/opt+0xaf4029)
For me, this crash started happening with commit r308422:
[SimplifyCFG] Defer folding unconditional branches to LateSimplifyCFG if it can destroy canonical loop structure.
Summary:
When simplifying unconditional branches from empty blocks, we pre-test if the
BB belongs to a set of loop headers and keep the block to prevent passes from
destroying canonical loop structure. However, the current algorithm fails if
the destination of the branch is a loop header. Especially when such a loop's
latch block is folded into loop header it results in additional backedges and
LoopSimplify turns it into a nested loop which prevent later optimizations
from being applied (e.g., loop unrolling and loop interleaving).
This patch augments the existing algorithm by further checking if the
destination of the branch belongs to a set of loop headers and defer
eliminating it if yes to LateSimplifyCFG.
Fixes llvm/llvm-bugzilla-archive#33605 : llvm/llvm-bugzilla-archive#33605
but I suppose the real problem is this PR, 33142.
There's been a lot of discussions, what is the current status?
*** Bug llvm/llvm-bugzilla-archive#33994 has been marked as a duplicate of this bug. ***
See the related discussion in https://reviews.llvm.org/D34135
A quick solution would be that of removing the successor BB once that becomes unreachable to avoid these issues, but I'm suspecting we have a deeper problem with iteration order here, maybe. I agree. So, it looks like we leave the dead phis around in the hope that the caller of
ProcessBlockwill zap the blocks itself. However, the iteration order does not guarantee that these successor blocks are called before any other blocks that can crash on these dead phis.So, I looked at this closely early this morning and I think we should still probably delegate the processing of dead blocks to
ProcessBlock.What we have clearly doesn't work, but I guess that if we use a WorkList stack which is initialized with the BB in reverse function order (so we keep the original visitation order) and once we find a block that's dead we'll push on the top of the stac, that should guarantee us that it will be analyzed next[1].
[1] I think this should also take care of transitively discovered dead blocks, when you have chains, e.g.
X -> Y -> Z
and X is discovered dead, so if there's a trivial PHI in Y that becomes a zero-arg PHI so we need to delete Y and so on.
Would that work for you?
I think that should work. What we should check is if there is code in jumpthreading that implies requiring the current BB List order.
A quick solution would be that of removing the successor BB once that becomes unreachable to avoid these issues, but I'm suspecting we have a deeper problem with iteration order here, maybe. I agree. So, it looks like we leave the dead phis around in the hope that the caller of
ProcessBlockwill zap the blocks itself. However, the iteration order does not guarantee that these successor blocks are called before any other blocks that can crash on these dead phis.
So, I looked at this closely early this morning and I think we should still probably delegate the processing of dead blocks to ProcessBlock.
What we have clearly doesn't work, but I guess that if we use a WorkList stack which is initialized with the BB in reverse function order (so we keep the original visitation order) and once we find a block that's dead we'll push on the top of the stac, that should guarantee us that it will be analyzed next[1].
[1] I think this should also take care of transitively discovered dead blocks, when you have chains, e.g.
X -> Y -> Z
and X is discovered dead, so if there's a trivial PHI in Y that becomes a zero-arg PHI so we need to delete Y and so on.
Would that work for you?
A quick solution would be that of removing the successor BB once that becomes unreachable to avoid these issues, but I'm suspecting we have a deeper problem with iteration order here, maybe. I agree. So, it looks like we leave the dead phis around in the hope that the caller of
ProcessBlockwill zap the blocks itself. However, the iteration order does not guarantee that these successor blocks are called before any other blocks that can crash on these dead phis.
I think the proposed fix maybe hiding the real problem. The
processBlockis called for every basic block in the function. If a block is trivially dead (i.e. no predecessor and not the entry block) after running processBlock , then we zap it in the caller function JumpThreadingPass::runImpl. It seems a useless phi implies we do not have any predecessors for the block, so it should have been deleted while iterating through blocks in the function.Also, the problem cannot be in the iteration order of basic blocks for jump-threading, since we iterate till we reach fixed point. So, leaving useless phis around should have been caused that basic block itself to be deleted.
Something as follows:
- We process a block
BBSucc(which hasBBas the only predecessor). After processBlock, we check if BBSucc is trivially dead, and it's not.- Next we process BB, and update it's branch and remove the edge to BBSucc. Now BBSucc is trivially dead, but we only check that BB is dead.
- In the next fixed point iteration, we should have removed BBSucc.
I agree with your analysis but I'm not sure you have this guarantee when you're visiting in basic block ordering (which might mean we need a different visitation order for this).
In particular, this is what happens:
You visit bb8
You remove the edge
You now have a dead PHI in bb4:
bb4: ; No predecessors! %pat = phi i64* %tmp6 = icmp slt i32 %tmp, 3 br label %bb15
Then, as you're visiting in BB order, you visit BB9 (and you don't remove BB4!)
bb9: %tmp10 = load i64, i64* %pat, align 8 [...]
You look at the load, the operand is a use of the dead PHI -> AA gets angry.
A quick solution would be that of removing the successor BB once that becomes unreachable to avoid these issues, but I'm suspecting we have a deeper problem with iteration order here, maybe.
The reason why
DontDeleteUselessPHIexists, at least from what I can see, is that SimplifyCFG caches PHI nodes and then looks at them again after calling removePredecessor(), so it asks BBUtils to not remove them in order to avoid to look at a dangling pointer/reference.I don't see JumpThreading doing anything like that, but I could miss something.
I don't see why we need to leave around the useless phi's either. However, I noticed 2 things:
- there are multiple cases in jumpthreading where we leave the useless phi's around, i.e. removePredecessor(Block, true). In some cases, we rely on recursively simplifying these phis as a follow on step: see SimplifyInstructionsInBlock call within JumpThreadingPass::ThreadEdge
I'm not sure about this one, let me check.
I have to say the CFG is a little weird, but I still think removing the useless PHIs from the BB is valid.
you have a structure like:
|----> BB8----------- | | false | | true| | -------- V BB4
LVI (apparently, correctly) tells you that the predicate is always true, so you can remove the edge corresponding to the false successor so you have:
|----> BB8 | | | true| BB4
The only situation |----> BB8----------- | | false | | true| | -------- V BB4
I think you end up with an empty PHI only when you don't have anymore predecessor, and in this case, you had just one and remove it.
But if you don't have anymore predecessors this means the block is unreachable from entry therefore JumpThreading don't (or shouldn't do) anything else with it.
I think the proposed fix maybe hiding the real problem. The processBlock is called for every basic block in the function. If a block is trivially dead (i.e. no predecessor and not the entry block) after running processBlock , then we zap it in the caller function JumpThreadingPass::runImpl. It seems a useless phi implies we do not have any predecessors for the block, so it should have been deleted while iterating through blocks in the function.
Also, the problem cannot be in the iteration order of basic blocks for jump-threading, since we iterate till we reach fixed point. So, leaving useless phis around should have been caused that basic block itself to be deleted.
Something as follows:
- We process a block
BBSucc(which hasBBas the only predecessor). After processBlock, we check if BBSucc is trivially dead, and it's not. - Next we process BB, and update it's branch and remove the edge to BBSucc. Now BBSucc is trivially dead, but we only check that BB is dead.
- In the next fixed point iteration, we should have removed BBSucc.
Is JumpThreading required to preserve LCSSA?
No.
The reason why
DontDeleteUselessPHIexists, at least from what I can see, is that SimplifyCFG caches PHI nodes and then looks at them again after calling removePredecessor(), so it asks BBUtils to not remove them in order to avoid to look at a dangling pointer/reference.I don't see JumpThreading doing anything like that, but I could miss something.
I don't see why we need to leave around the useless phi's either. However, I noticed 2 things:
- there are multiple cases in jumpthreading where we leave the useless phi's around, i.e. removePredecessor(Block, true). In some cases, we rely on recursively simplifying these phis as a follow on step: see SimplifyInstructionsInBlock call within JumpThreadingPass::ThreadEdge
I'm not sure about this one, let me check.
- In loop rotation, we avoid deleting useless phi to preserve LCSSA form.
Is JumpThreading required to preserve LCSSA?
The reason why
DontDeleteUselessPHIexists, at least from what I can see, is that SimplifyCFG caches PHI nodes and then looks at them again after calling removePredecessor(), so it asks BBUtils to not remove them in order to avoid to look at a dangling pointer/reference.I don't see JumpThreading doing anything like that, but I could miss something.
I don't see why we need to leave around the useless phi's either. However, I noticed 2 things:
- there are multiple cases in jumpthreading where we leave the useless phi's around, i.e. removePredecessor(Block, true). In some cases, we rely on recursively simplifying these phis as a follow on step: see SimplifyInstructionsInBlock call within JumpThreadingPass::ThreadEdge
- In loop rotation, we avoid deleting useless phi to preserve LCSSA form.
The reason why DontDeleteUselessPHI exists, at least from what I can see, is that SimplifyCFG caches PHI nodes and then looks at them again after calling removePredecessor(), so it asks BBUtils to not remove them in order to avoid to look at a dangling pointer/reference.
I don't see JumpThreading doing anything like that, but I could miss something.
The reason why DontDeleteUselessPHI exists, at least from what I can see, is that SimplifyCFG caches PHI nodes and then looks at them again after calling removePredecessor(), so it asks BBUtils to not remove them in order to avoid to look at a dangling pointer/reference.
I don't see JumpThreading doing anything like that, but I could miss something.
I don't see immediately why we may want to leave an useless PHI around, and, FWIW, the following fixes my case and passes the test suite.
diff --git a/lib/Transforms/Scalar/JumpThreading.cpp b/lib/Transforms/Scalar/JumpThreading.cpp index 2ef8f85..3d80a9c 100644 --- a/lib/Transforms/Scalar/JumpThreading.cpp +++ b/lib/Transforms/Scalar/JumpThreading.cpp @@ -857,7 +857,7 @@ bool JumpThreadingPass::ProcessBlock(BasicBlock *BB) { if (Ret != LazyValueInfo::Unknown) { unsigned ToRemove = Ret == LazyValueInfo::True ? 1 : 0; unsigned ToKeep = Ret == LazyValueInfo::True ? 0 : 1;
-
CondBr->getSuccessor(ToRemove)->removePredecessor(BB, true);
-
CondBr->getSuccessor(ToRemove)->removePredecessor(BB, false); BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr); CondBr->eraseFromParent(); if (CondCmp->use_empty())
Hal, you're the last one who touched this code. Does this seem reasonable to you or I'm being crazy here?