[PATCH] D22924: [GlobalISel] Permit select() to erase.

Ahmed Bougacha via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 28 09:26:39 PDT 2016


ab created this revision.
ab added reviewers: t.p.northover, tstellarAMD.
ab added subscribers: qcolombet, llvm-commits.
Herald added subscribers: vkalintiris, mehdi_amini.

This lets the selector erase instructions, which is incompatible with reverse iterators.  The code is pretty awkward, but I'm not sure there's anything cleaner to be done (short of having select() return iterators, which I'd rather avoid if unnecessary).

This also lets us dump all "selected" instructions (really, just the instructions between prev(MI) and next(MI)); I'll commit separately but these seem worth reviewing together.

WDYT?

https://reviews.llvm.org/D22924

Files:
  lib/CodeGen/GlobalISel/InstructionSelect.cpp

Index: lib/CodeGen/GlobalISel/InstructionSelect.cpp
===================================================================
--- lib/CodeGen/GlobalISel/InstructionSelect.cpp
+++ lib/CodeGen/GlobalISel/InstructionSelect.cpp
@@ -57,15 +57,39 @@
 #endif
 
   for (MachineBasicBlock *MBB : post_order(&MF)) {
-    for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
-                                             End = MBB->rend();
-         MII != End;) {
-      MachineInstr &MI = *MII++;
-      DEBUG(dbgs() << "Selecting: " << MI << '\n');
+    if (MBB->empty())
+      continue;
+
+    // Select instructions in reverse block order. We permit erasing so have
+    // to resort to manually iterating and recognizing the begin (rend) case.
+    bool ReachedBegin = false;
+    for (auto MII = std::prev(MBB->end()), Begin = MBB->begin();
+         !ReachedBegin;) {
+      // Keep track of the insertion range for debug printing.
+      const auto AfterIt = std::next(MII);
+
+      // Select this instruction.
+      MachineInstr &MI = *MII;
+
+      // And have our iterator point to the next instruction, if there is one.
+      if (MII == Begin)
+        ReachedBegin = true;
+      else
+        --MII;
+
+      DEBUG(dbgs() << "Selecting: \n" << MI);
+
       if (!ISel->select(MI))
         reportSelectionError(MI, "Cannot select");
-      // FIXME: It would be nice to dump all inserted instructions.  It's not
-      // obvious how, esp. considering select() can insert after MI.
+
+      // Dump the range of instructions that MI expanded into.
+      DEBUG({
+        auto InsertedBegin = ReachedBegin ? MBB->begin() : std::next(MII);
+        dbgs() << "Into:\n";
+        for (auto &InsertedMI : make_range(InsertedBegin, AfterIt))
+          dbgs() << InsertedMI;
+        dbgs() << '\n';
+      });
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22924.65948.patch
Type: text/x-patch
Size: 1832 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160728/b2193aab/attachment.bin>


More information about the llvm-commits mailing list