[MachinePipeliner] Fix risky iterator usage R++, --R
authorJinsong Ji <jji@us.ibm.com>
Tue, 25 Jun 2019 21:50:56 +0000 (21:50 +0000)
committerJinsong Ji <jji@us.ibm.com>
Tue, 25 Jun 2019 21:50:56 +0000 (21:50 +0000)
When we calculate MII, we use two loops, one with iterator R++ to
check whether we can reserve the resource, then --R to move back
the iterator to do reservation.

This is risky, as R++, --R may not point to the same element at all.
The can cause wrong MII.

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

llvm-svn: 364353

llvm/lib/CodeGen/MachinePipeliner.cpp
llvm/test/CodeGen/PowerPC/sms-iterator.ll

index 570ed4aadab7114dd007114f10356f0b3f6464c1..167c05f9ef4e2b9633b5449ed62de2c277833d51 100644 (file)
@@ -1012,17 +1012,13 @@ unsigned SwingSchedulerDAG::calculateResMII() {
     });
     for (unsigned C = 0; C < NumCycles; ++C)
       while (RI != RE) {
-        if ((*RI++)->canReserveResources(*MI)) {
+        if ((*RI)->canReserveResources(*MI)) {
+          (*RI)->reserveResources(*MI);
           ++ReservedCycles;
           break;
         }
+        RI++;
       }
-    // Start reserving resources using existing DFAs.
-    for (unsigned C = 0; C < ReservedCycles; ++C) {
-      --RI;
-      (*RI)->reserveResources(*MI);
-    }
-
     LLVM_DEBUG(dbgs() << "ReservedCycles:" << ReservedCycles
                       << ", NumCycles:" << NumCycles << "\n");
     // Add new DFAs, if needed, to reserve resources.
index ec8067999968f0ba65849158b2101b7a2ec45689..1210ed98b5b5cdcb8075162b4f80df5c12767618 100644 (file)
@@ -5,7 +5,7 @@
 
 %0 = type { i32, [16 x double] }
 
-; CHECK: MII = 7 MAX_II = 17
+; CHECK: MII = 8 MAX_II = 18
 
 define dso_local fastcc void @_ZN3povL9polysolveEiPdS0_() unnamed_addr #0 {
   br label %1