[PATCH] Experiment with keeping GEPs near calls

Daniel Jasper djasper at google.com
Mon Feb 23 13:28:54 PST 2015


Current work-in-progress.

To summarize what was discussed through other channels (IRC mostly):

- We eventually want to have a lazy code motion pass which sinks GEPs into loops if they can be folded into address mode instructions (well, if sinking does not significantly increase the cost of the operation inside the loop) and dependent on register pressure.
- For the first attempts, it seems easier to re-use MachineLICM. Conceptually, this move loop-invariant code, so it  isn't the "wrong" place and it already calculates much of the required information.

Still incomplete, but feedback is appreciated.


http://reviews.llvm.org/D7259

Files:
  lib/CodeGen/MachineLICM.cpp

Index: lib/CodeGen/MachineLICM.cpp
===================================================================
--- lib/CodeGen/MachineLICM.cpp
+++ lib/CodeGen/MachineLICM.cpp
@@ -769,6 +769,42 @@
     // If it's a leaf node, it's done. Traverse upwards to pop ancestors.
     ExitScopeIfDone(Node, OpenChildren, ParentMap);
   }
+
+  SmallVector<MachineInstr *, 8> Candidates;
+  for (MachineBasicBlock::instr_iterator I = Preheader->instr_begin();
+       I != Preheader->instr_end(); ++I) {
+    if (IsLoopInvariantInst(*I) && !HasLoopPHIUse(I))
+      Candidates.push_back(I);
+  }
+
+  for (MachineInstr *I : Candidates) {
+    const MachineOperand &MO = I->getOperand(0);
+    if (!MO.isDef() || !MO.isReg() || MO.getReg() == 0)
+      continue;
+    if (!MRI->hasOneDef(MO.getReg()) || !MRI->hasOneUse(MO.getReg()))
+      continue;
+    bool CanSink = true;
+    MachineBasicBlock *B = nullptr;
+    for (MachineInstr &MI : MRI->use_instructions(MO.getReg())) {
+      if (!MI.isCopy()) {
+        MI.dump();
+        CanSink = false;
+        break;
+      }
+      if (!B) {
+        B = MI.getParent();
+      } else {
+        B = DT->findNearestCommonDominator(B, MI.getParent());
+        if (!B) {
+          CanSink = false;
+          break;
+        }
+      }
+    }
+    if (!CanSink || !B || B == Preheader)
+      continue;
+    B->splice(B->getFirstNonPHI(), Preheader, I);
+  }
 }
 
 static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) {

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7259.20538.patch
Type: text/x-patch
Size: 1479 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150223/a0aaaea5/attachment.bin>


More information about the llvm-commits mailing list