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

Chris Lattner lattner at cs.uiuc.edu
Tue Nov 25 15:10:04 PST 2003


Changes in directory llvm/lib/Transforms/Scalar:

ScalarReplAggregates.cpp updated: 1.18 -> 1.19

---
Log message:

Do not use index type to determine what it is indexing into!


---
Diffs of the changes:  (+26 -26)

Index: llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp
diff -u llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp:1.18 llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp:1.19
--- llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp:1.18	Tue Nov 11 16:41:34 2003
+++ llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp	Tue Nov 25 15:09:18 2003
@@ -26,6 +26,7 @@
 #include "llvm/Pass.h"
 #include "llvm/iMemory.h"
 #include "llvm/Analysis/Dominators.h"
+#include "llvm/Support/GetElementPtrTypeIterator.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
 #include "Support/Debug.h"
@@ -222,35 +223,34 @@
 /// aggregate allocation.
 ///
 bool SROA::isSafeUseOfAllocation(Instruction *User) {
-  if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
-    // The GEP is safe to transform if it is of the form GEP <ptr>, 0, <cst>
-    if (GEPI->getNumOperands() <= 2 ||
-        GEPI->getOperand(1) != Constant::getNullValue(Type::LongTy) ||
-        !isa<Constant>(GEPI->getOperand(2)) ||
-        isa<ConstantExpr>(GEPI->getOperand(2)))
-      return false;
+  if (!isa<GetElementPtrInst>(User)) return false;
 
-    // If this is a use of an array allocation, do a bit more checking for
-    // sanity.
-    if (GEPI->getOperand(2)->getType() == Type::LongTy) {
-      const PointerType *PTy =cast<PointerType>(GEPI->getOperand(0)->getType());
-      const ArrayType *AT = cast<ArrayType>(PTy->getElementType());
-      int64_t NumElements = AT->getNumElements();
-
-      // 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 (cast<ConstantSInt>(GEPI->getOperand(2))->getValue() >= NumElements ||
-          cast<ConstantSInt>(GEPI->getOperand(2))->getValue() < 0)
-        return false;
-    }
+  GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User);
+  gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI);
 
-    // If there are any non-simple uses of this getelementptr, make sure to
-    // reject them.
-    if (isSafeElementUse(GEPI))
-      return true;
+  // The GEP is safe to transform if it is of the form GEP <ptr>, 0, <cst>
+  if (I == E ||
+      I.getOperand() != Constant::getNullValue(I.getOperand()->getType()))
+    return false;
+
+  ++I;
+  if (I != E || !isa<ConstantInt>(I.getOperand()))
+    return false;
+
+  // If this is a use of an array allocation, do a bit more checking for sanity.
+  if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) {
+    uint64_t NumElements = AT->getNumElements();
+    
+    // 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 (cast<ConstantInt>(GEPI->getOperand(2))->getRawValue() >= NumElements)
+      return false;
   }
-  return false;
+
+  // If there are any non-simple uses of this getelementptr, make sure to reject
+  // them.
+  return isSafeElementUse(GEPI);
 }
 
 /// isSafeElementUse - Check to see if this use is an allowed use for a





More information about the llvm-commits mailing list