[PATCH] D32744: [globalisel] Improve legalizer DEBUG_ONLY output.

Daniel Sanders via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 2 05:19:00 PDT 2017


dsanders created this revision.
Herald added subscribers: igorb, kristof.beyls, rovka.

This patch changes the way we decide which instructions to emit in the
DEBUG_ONLY output. It now emits all instructions that replace the work
list item regardless of their creation method (previously instructions
created with BuildMI() were not emitted) and it no longer requires the
new instructions to be added to the work list.


https://reviews.llvm.org/D32744

Files:
  lib/CodeGen/GlobalISel/Legalizer.cpp


Index: lib/CodeGen/GlobalISel/Legalizer.cpp
===================================================================
--- lib/CodeGen/GlobalISel/Legalizer.cpp
+++ lib/CodeGen/GlobalISel/Legalizer.cpp
@@ -173,42 +173,58 @@
       // and are assumed to be legal.
       if (!isPreISelGenericOpcode(MI->getOpcode()))
         continue;
-      unsigned NumNewInsns = 0;
       SmallVector<MachineInstr *, 4> WorkList;
       Helper.MIRBuilder.recordInsertions([&](MachineInstr *MI) {
-        ++NumNewInsns;
         WorkList.push_back(MI);
       });
       WorkList.push_back(&*MI);
 
       bool Changed = false;
       LegalizerHelper::LegalizeResult Res;
       unsigned Idx = 0;
       do {
-        Res = Helper.legalizeInstrStep(*WorkList[Idx]);
+        MachineInstr &CurrMI = *WorkList[Idx];
+#ifndef NDEBUG
+        // Record the instructions either side of the current instruction so we
+        // can iterate over its replacements (even after CurrMI has been erased).
+        // PrintFrom is set to MBB.end() if there is no instruction before CurrMI.
+        MachineBasicBlock::iterator PrintFrom = MBB.end();
+        if (CurrMI != MBB.begin()) {
+          PrintFrom = CurrMI;
+          --PrintFrom;
+        }
+        MachineBasicBlock::iterator PrintTo =
+            std::next(MachineBasicBlock::iterator(CurrMI));
+#endif // NDEBUG
+
+        Res = Helper.legalizeInstrStep(CurrMI);
         // Error out if we couldn't legalize this instruction. We may want to
         // fall back to DAG ISel instead in the future.
         if (Res == LegalizerHelper::UnableToLegalize) {
           Helper.MIRBuilder.stopRecordingInsertions();
           if (Res == LegalizerHelper::UnableToLegalize) {
             reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
-                               "unable to legalize instruction",
-                               *WorkList[Idx]);
+                               "unable to legalize instruction", CurrMI);
             return false;
           }
         }
         Changed |= Res == LegalizerHelper::Legalized;
-        ++Idx;
 
 #ifndef NDEBUG
-        if (NumNewInsns) {
-          DEBUG(dbgs() << ".. .. Emitted " << NumNewInsns << " insns\n");
-          for (auto I = WorkList.end() - NumNewInsns, E = WorkList.end();
-               I != E; ++I)
-            DEBUG(dbgs() << ".. .. New MI: "; (*I)->print(dbgs()));
-          NumNewInsns = 0;
-        }
-#endif
+        // Now that the current instruction has been erased (if it was going to
+        // be) advancing PrintFrom will select it again if we didn't erase it,
+        // select PrintTo if we erased it but emitted zero instructions, or the
+        // first newly created instruction.
+        if (PrintFrom == MBB.end())
+          PrintFrom = MBB.begin();
+        else
+          ++PrintFrom;
+
+        for (auto I = PrintFrom, E = PrintTo; I != E; ++I)
+          DEBUG(dbgs() << ".. .. MI: "; I->print(dbgs()));
+#endif // NDEBUG
+
+        ++Idx;
       } while (Idx < WorkList.size());
 
       Helper.MIRBuilder.stopRecordingInsertions();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32744.97433.patch
Type: text/x-patch
Size: 3073 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170502/a68b7fe0/attachment.bin>


More information about the llvm-commits mailing list