[SROA] Fix the loop exit placement to be prior to indexing the splits
[oota-llvm.git] / lib / Transforms / Scalar / SROA.cpp
index 0869114e7f7a3707e6f1aacb7c28868f40ca7b27..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;
     }
 
@@ -3726,7 +3728,7 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) {
         continue;
       }
 
-      Instruction *StoreBasePtr = cast<Instruction>(SI->getPointerOperand());
+      Value *StoreBasePtr = SI->getPointerOperand();
       IRB.SetInsertPoint(BasicBlock::iterator(SI));
 
       DEBUG(dbgs() << "    Splitting store of load: " << *SI << "\n");
@@ -3789,7 +3791,7 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) {
     assert(BaseOffset + StoreSize > BaseOffset &&
            "Cannot represent alloca access size using 64-bit integers!");
 
-    Instruction *LoadBasePtr = cast<Instruction>(LI->getPointerOperand());
+    Value *LoadBasePtr = LI->getPointerOperand();
     Instruction *StoreBasePtr = cast<Instruction>(SI->getPointerOperand());
 
     DEBUG(dbgs() << "  Splitting store: " << *SI << "\n");
@@ -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;
     }