diff --git a/llvm/test/Reduce/remove-bbs-ret-nonvoid.ll b/llvm/test/Reduce/remove-bbs-ret-nonvoid.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Reduce/remove-bbs-ret-nonvoid.ll @@ -0,0 +1,31 @@ +; Test that llvm-reduce inserts valid return instructions for functions with +; on-void return types. +; Note: if an uninteresting BB is the default case for a switch, the instruction +; is removed altogether (since the default case cannot be replaced) +; +; RUN: llvm-reduce --test %python --test-arg %p/Inputs/remove-bbs.py %s -o %t +; RUN: cat %t | FileCheck -implicit-check-not=uninteresting %s + +define i32 @main() { +interesting: + ; CHECK-NOT: switch i32 0, label %uninteresting + switch i32 0, label %uninteresting [ + i32 0, label %uninteresting + ] + +uninteresting: + ret i32 10 + +interesting2: + ; CHECK: switch i32 1, label %interesting3 + switch i32 1, label %interesting3 [ + ; CHECK-NOT: i32 0, label %uninteresting + i32 0, label %uninteresting + ; CHECK: i32 1, label %interesting3 + i32 1, label %interesting3 + ] + +interesting3: + ; CHECK: br label %interesting2 + br i1 true, label %interesting2, label %uninteresting +} diff --git a/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp b/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp --- a/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp +++ b/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp @@ -45,7 +45,10 @@ Term->eraseFromParent(); if (ChunkSucessors.empty()) { - ReturnInst::Create(BB.getContext(), nullptr, &BB); + auto *FnRetTy = BB.getParent()->getReturnType(); + ReturnInst::Create(BB.getContext(), + FnRetTy->isVoidTy() ? nullptr : UndefValue::get(FnRetTy), + &BB); return; } @@ -66,7 +69,10 @@ static void removeUninterestingBBsFromSwitch(SwitchInst &SwInst, std::set BBsToKeep) { if (!BBsToKeep.count(SwInst.getDefaultDest())) { - ReturnInst::Create(SwInst.getContext(), nullptr, SwInst.getParent()); + auto *FnRetTy = SwInst.getParent()->getParent()->getReturnType(); + ReturnInst::Create(SwInst.getContext(), + FnRetTy->isVoidTy() ? nullptr : UndefValue::get(FnRetTy), + SwInst.getParent()); SwInst.eraseFromParent(); } else for (int I = 0, E = SwInst.getNumCases(); I != E; ++I) {