[llvm-commits] CVS: llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp

Chris Lattner lattner at cs.uiuc.edu
Tue May 27 11:10:05 PDT 2003


Changes in directory llvm/lib/Transforms/Scalar:

ScalarReplAggregates.cpp updated: 1.1 -> 1.2

---
Log message:

* Actually USE the statistic that we made
* Implement SRoA for arrays


---
Diffs of the changes:

Index: llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp
diff -u llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp:1.1 llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp:1.2
--- llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp:1.1	Tue May 27 10:45:27 2003
+++ llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp	Tue May 27 11:09:27 2003
@@ -52,12 +52,15 @@
     WorkList.pop_back();
 
     // We cannot transform the allocation instruction if it is an array
-    // allocation, and an allocation of a scalar value cannot be decomposed
+    // allocation (allocations OF arrays are ok though), and an allocation of a
+    // scalar value cannot be decomposed at all.
+    //
     if (AI->isArrayAllocation() ||
-        (!isa<StructType>(AI->getAllocatedType()) /*&&
-                                                    !isa<ArrayType>(AI->getAllocatedType())*/
-         )) continue;
-    
+        (!isa<StructType>(AI->getAllocatedType()) &&
+         !isa<ArrayType>(AI->getAllocatedType()))) continue;
+
+    const ArrayType *AT = dyn_cast<ArrayType>(AI->getAllocatedType());
+
     // Loop over the use list of the alloca.  We can only transform it if there
     // are only getelementptr instructions (with a zero first index) and free
     // instructions.
@@ -77,6 +80,18 @@
           CannotTransform = true;
           break;
         }
+
+        // If this is an array access, check to make sure that index falls
+        // within the array.  If not, something funny is going on, so we won't
+        // do the optimization.
+        if (AT && cast<ConstantSInt>(GEPI->getOperand(2))->getValue() >=
+            AT->getNumElements()) {
+          DEBUG(std::cerr << "Cannot transform: " << *AI << "  due to user: "
+                          << User);
+          CannotTransform = true;
+          break;
+        }
+
       } else {
         DEBUG(std::cerr << "Cannot transform: " << *AI << "  due to user: "
                         << User);
@@ -100,7 +115,6 @@
         WorkList.push_back(NA);  // Add to worklist for recursive processing
       }
     } else {
-      const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
       ElementAllocas.reserve(AT->getNumElements());
       const Type *ElTy = AT->getElementType();
       for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
@@ -158,6 +172,7 @@
 
     // Finally, delete the Alloca instruction
     AI->getParent()->getInstList().erase(AI);
+    NumReplaced++;
   }
 
   return Changed;





More information about the llvm-commits mailing list