[RISCV][InsertVSETVLI] Reverse traversal order of block in post pass [nfc]
authorPhilip Reames <preames@rivosinc.com>
Tue, 13 Dec 2022 15:47:53 +0000 (07:47 -0800)
committerPhilip Reames <listmail@philipreames.com>
Tue, 13 Dec 2022 15:54:05 +0000 (07:54 -0800)
his unblocks a following change to be more sophisticated during post pass rewriting.

Review wise, I basically just want a second set of eyes. This change should be straight forward, but since it took me an embarrassing number of attempts to get make check to pass. Let's make sure I'm not missing yet another cornercase.

Differential Revision: https://reviews.llvm.org/D139877

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

index 08430f8..b2156e7 100644 (file)
@@ -1220,34 +1220,38 @@ static bool canMutatePriorConfig(const MachineInstr &PrevMI,
 }
 
 void RISCVInsertVSETVLI::doLocalPostpass(MachineBasicBlock &MBB) {
-  MachineInstr *PrevMI = nullptr;
+  MachineInstr *NextMI = nullptr;
+  // We can have arbitrary code in successors, so VL and VTYPE
+  // must be considered demanded.
   DemandedFields Used;
+  Used.VL = true;
+  Used.demandVTYPE();
   SmallVector<MachineInstr*> ToDelete;
-  for (MachineInstr &MI : MBB) {
-    // Note: Must be *before* vsetvli handling to account for config cases
-    // which only change some subfields.
-    doUnion(Used, getDemanded(MI));
+  for (MachineInstr &MI : iterator_range(MBB.rbegin(), MBB.rend())) {
 
-    if (!isVectorConfigInstr(MI))
+    if (!isVectorConfigInstr(MI)) {
+      doUnion(Used, getDemanded(MI));
       continue;
+    }
+
+    Register VRegDef = MI.getOperand(0).getReg();
+    if (VRegDef != RISCV::X0 &&
+        !(VRegDef.isVirtual() && MRI->use_nodbg_empty(VRegDef)))
+      Used.VL = true;
 
-    if (PrevMI) {
+    if (NextMI) {
       if (!Used.VL && !Used.usedVTYPE()) {
-        ToDelete.push_back(PrevMI);
-        // fallthrough
-      } else if (canMutatePriorConfig(*PrevMI, MI, Used)) {
-        PrevMI->getOperand(2).setImm(MI.getOperand(2).getImm());
         ToDelete.push_back(&MI);
-        // Leave PrevMI unchanged
+        // Leave NextMI unchanged
         continue;
+      } else if (canMutatePriorConfig(MI, *NextMI, Used)) {
+        MI.getOperand(2).setImm(NextMI->getOperand(2).getImm());
+        ToDelete.push_back(NextMI);
+        // fallthrough
       }
     }
-    PrevMI = &MI;
+    NextMI = &MI;
     Used = getDemanded(MI);
-    Register VRegDef = MI.getOperand(0).getReg();
-    if (VRegDef != RISCV::X0 &&
-        !(VRegDef.isVirtual() && MRI->use_nodbg_empty(VRegDef)))
-      Used.VL = true;
   }
 
   for (auto *MI : ToDelete)