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

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 30 06:10:55 PDT 2020


fhahn created this revision.
fhahn added reviewers: lebedev.ri, dblaikie.
Herald added a subscriber: danielkiss.
Herald added a project: LLVM.
fhahn requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D86849

Files:
  llvm/test/Reduce/remove-bbs-ret-nonvoid.ll
  llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp


Index: llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
===================================================================
--- llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
+++ 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<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) {
Index: llvm/test/Reduce/remove-bbs-ret-nonvoid.ll
===================================================================
--- /dev/null
+++ 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
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86849.288852.patch
Type: text/x-patch
Size: 2410 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200830/bec0a105/attachment.bin>


More information about the llvm-commits mailing list