[llvm-reduce] Create returns with undef values for non-void functions.

Currently replaceBranchTerminator/removeUninterestingBBsFromSwitch
always creates `ret void` instructions if no successor is in the chunk.

This results in invalid IR for functions with non-void return types,
which makes those reductions unfeasible. Instead, create `ret ty undef`
for functions with non-void return types.

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D86849
This commit is contained in:
Florian Hahn 2020-08-31 16:33:46 +01:00
parent 84875f6941
commit a5bb24758d
2 changed files with 9 additions and 6 deletions

View File

@ -13,10 +13,7 @@ define i32 @main(i1 %c) {
; CHECK-NEXT: br label %interesting2
; CHECK-LABEL: interesting2:
; CHECK-NEXT: br label %uninteresting1
; CHECK-LABEL: uninteresting1:
; CHECK-NEXT: ret i32 10
; CHECK-NEXT: ret i32 undef
interesting:
br label %interesting2

View File

@ -45,7 +45,10 @@ static void replaceBranchTerminator(BasicBlock &BB,
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 replaceBranchTerminator(BasicBlock &BB,
static void removeUninterestingBBsFromSwitch(SwitchInst &SwInst,
std::set<BasicBlock *> 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) {