Currently if instructions defined in a block are used in unreachable blocks and SimpleLoopUnswitch attempts deleting the block, it triggers following assertion:
While deleting: i32 %tmp2 Use still stuck around after Def is destroyed: %tmp4 = add i32 %tmp2, 1 opt: llvm-project/llvm/lib/IR/Value.cpp:96: llvm::Value::~Value(): Assertion `materialized_use_empty() && "Uses remain when a value is destroyed!"' failed. Stack dump: 0. Program arguments: bin/opt -passes=unswitch<nontrivial> -disable-output dead-blocks-uses-in-unreachablel-blocks.ll <...> #6 0x00007f584b9590e6 __assert_fail_base (/lib64/libc.so.6+0x2f0e6) #7 0x00007f584b959192 (/lib64/libc.so.6+0x2f192) #8 0x000000000216b58d (bin/opt+0x216b58d) #9 0x000000000216b5e0 llvm::Value::deleteValue() (bin/opt+0x216b5e0) #10 0x00000000020212ca llvm::BasicBlock::~BasicBlock() (bin/opt+0x20212ca) #11 0x0000000002021535 llvm::BasicBlock::eraseFromParent() (bin/opt+0x2021535) #12 0x00000000006c5e2e deleteDeadBlocksFromLoop(llvm::Loop&, llvm::SmallVectorImpl<llvm::BasicBlock*>&, llvm::DominatorTree&, llvm::LoopInfo&, llvm::MemorySSAUpdater*) (bin/opt+0x6c5e2e) #13 0x00000000006c8489 unswitchNontrivialInvariants(llvm::Loop&, llvm::Instruction&, llvm::ArrayRef<llvm::Value*>, llvm::SmallVectorImpl<llvm::BasicBlock*>&, llvm::DominatorTree&, llvm::LoopInfo&, llvm::AssumptionCache&, llvm::function_ref<void (bool, llvm::ArrayRef<llvm::Loop*>)>, llvm::ScalarEvolution*, llvm::MemorySSAUpdater*) (bin/opt+0x6c8489) #14 0x0000000002737d07 unswitchBestCondition(llvm::Loop&, llvm::DominatorTree&, llvm::LoopInfo&, llvm::AssumptionCache&, llvm::TargetTransformInfo&, llvm::function_ref<void (bool, llvm::ArrayRef<llvm::Loop*>)>, llvm::ScalarEvolution*, llvm::MemorySSAUpdater*) (bin/opt+0x2737d07) #15 0x0000000002738d91 llvm::SimpleLoopUnswitchPass::run(llvm::Loop&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&) (bin/opt+0x2738d91) <...>
This patch fixes it by replacing all uses of instructions from BB with undefs before BB deletion.