[SROA] Fix the loop exit placement to be prior to indexing the splits
authorChandler Carruth <chandlerc@gmail.com>
Fri, 2 Jan 2015 00:10:22 +0000 (00:10 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Fri, 2 Jan 2015 00:10:22 +0000 (00:10 +0000)
array. This prevents it from walking out of bounds on the splits array.

Bug found with the existing tests by ASan and by the MSVC debug build.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225069 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/SROA.cpp

index 683fab5cb651f5967f888f4c9f20c698691e088a..3d00f38f7efd24030cc5f55b7b22ccbadf897c58 100644 (file)
@@ -3706,11 +3706,13 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) {
                    << ", " << NewSlices.back().endOffset() << "): " << *PLoad
                    << "\n");
 
+      // See if we've handled all the splits.
+      if (Idx >= Size)
+        break;
+
       // Setup the next partition.
       PartOffset = Offsets.Splits[Idx];
       ++Idx;
-      if (Idx > Size)
-        break;
       PartSize = (Idx < Size ? Offsets.Splits[Idx] : LoadSize) - PartOffset;
     }
 
@@ -3845,11 +3847,13 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) {
         DEBUG(dbgs() << "      of split load: " << *PLoad << "\n");
       }
 
+      // See if we've finished all the splits.
+      if (Idx >= Size)
+        break;
+
       // Setup the next partition.
       PartOffset = Offsets.Splits[Idx];
       ++Idx;
-      if (Idx > Size)
-        break;
       PartSize = (Idx < Size ? Offsets.Splits[Idx] : StoreSize) - PartOffset;
     }