[PATCH] D71217: Fix incorrect logic in maintaining the side-effect of compiler generated outliner functions

Jin Lin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 9 11:02:08 PST 2019


jinlin created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
jinlin added reviewers: paquette, tellenbach.

Fix incorrect logic in maintaining the side-effect of compiler generated outliner functions by adding the up-exposed uses.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71217

Files:
  llvm/lib/CodeGen/MachineOutliner.cpp


Index: llvm/lib/CodeGen/MachineOutliner.cpp
===================================================================
--- llvm/lib/CodeGen/MachineOutliner.cpp
+++ llvm/lib/CodeGen/MachineOutliner.cpp
@@ -56,6 +56,7 @@
 //===----------------------------------------------------------------------===//
 #include "llvm/CodeGen/MachineOutliner.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/CodeGen/MachineFunction.h"
@@ -1243,28 +1244,41 @@
         // Helper lambda for adding implicit def operands to the call
         // instruction. It also updates call site information for moved
         // code.
-        auto CopyDefsAndUpdateCalls = [&CallInst](MachineInstr &MI) {
-          for (MachineOperand &MOP : MI.operands()) {
+        SmallSet<unsigned, 2> UseRegs;
+        MachineBasicBlock::reverse_iterator Last =
+            std::next(CallInst.getReverse());
+        MachineBasicBlock::reverse_iterator Iter = EndIt.getReverse();
+        // Copy over the defs in the outlined range.
+        // First inst in outlined range <-- Anything that's defined in this
+        // ...                           .. range has to be added as an
+        // implicit Last inst in outlined range  <-- def to the call
+        // instruction. Also remove call site information for outlined block
+        // of code. The exposed uses need to be copied in the outlined range.
+        for (; Iter != Last; Iter++) {
+          MachineInstr *MI = &*Iter;
+          for (MachineOperand &MOP : MI->operands()) {
             // Skip over anything that isn't a register.
             if (!MOP.isReg())
               continue;
 
             // If it's a def, add it to the call instruction.
-            if (MOP.isDef())
+            if (MOP.isDef()) {
               CallInst->addOperand(MachineOperand::CreateReg(
                   MOP.getReg(), true, /* isDef = true */
                   true /* isImp = true */));
+              if (UseRegs.count(MOP.getReg()))
+                UseRegs.erase(MOP.getReg());
+            } else if (!MOP.isUndef())
+              UseRegs.insert(MOP.getReg());
           }
-          if (MI.isCall())
-            MI.getMF()->eraseCallSiteInfo(&MI);
+          if (MI->isCall())
+            MI->getMF()->eraseCallSiteInfo(MI);
         };
-        // Copy over the defs in the outlined range.
-        // First inst in outlined range <-- Anything that's defined in this
-        // ...                           .. range has to be added as an
-        // implicit Last inst in outlined range  <-- def to the call
-        // instruction. Also remove call site information for outlined block
-        // of code.
-        std::for_each(CallInst, std::next(EndIt), CopyDefsAndUpdateCalls);
+        for (auto I : UseRegs)
+          // If it's a exposed use, add it to the call instruction.
+          CallInst->addOperand(
+              MachineOperand::CreateReg(I, false, /* isDef = false */
+                                        true /* isImp = true */));
       }
 
       // Erase from the point after where the call was inserted up to, and


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71217.232898.patch
Type: text/x-patch
Size: 3170 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191209/f39ab970/attachment.bin>


More information about the llvm-commits mailing list