[PATCH] D86210: [llvm-reduce] Skip terminators when reducing instructions.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 19 06:06:50 PDT 2020


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

Removing terminators will result in invalid IR, making further
reductions pointless. I do not think there is any valid use case where
we actually want to create invalid IR as part of a reduction.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D86210

Files:
  llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp


Index: llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp
===================================================================
--- llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp
+++ llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp
@@ -24,10 +24,14 @@
   std::set<Instruction *> InstToKeep;
 
   for (auto &F : *Program)
-    for (auto &BB : F)
-      for (auto &Inst : BB)
+    for (auto &BB : F) {
+      // Removing the terminator would make the block invalid. Only iterate over
+      // instructions before the terminator.
+      InstToKeep.insert(BB.getTerminator());
+      for (auto &Inst : make_range(BB.begin(), std::prev(BB.end())))
         if (O.shouldKeep())
           InstToKeep.insert(&Inst);
+    }
 
   std::vector<Instruction *> InstToDelete;
   for (auto &F : *Program)
@@ -49,7 +53,8 @@
   int InstCount = 0;
   for (auto &F : *Program)
     for (auto &BB : F)
-        InstCount += BB.getInstList().size();
+      // Well-formed blocks have terminators, which we cannot remove.
+      InstCount += BB.getInstList().size() - 1;
   outs() << "Number of instructions: " << InstCount << "\n";
 
   return InstCount;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86210.286543.patch
Type: text/x-patch
Size: 1145 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200819/6460fd0e/attachment.bin>


More information about the llvm-commits mailing list