Support getelementptr instructions which use uint's to index into structure
types and can have arbitrary 32- and 64-bit integer types indexing into
sequential types.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12653 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/TargetData.cpp b/lib/Target/TargetData.cpp
index 271b6d8..78d5d2e 100644
--- a/lib/Target/TargetData.cpp
+++ b/lib/Target/TargetData.cpp
@@ -20,6 +20,7 @@
 #include "llvm/Module.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Constants.h"
+#include "llvm/Support/GetElementPtrTypeIterator.h"
 using namespace llvm;
 
 // Handle the Pass registration stuff necessary to use TargetData's.
@@ -218,17 +219,11 @@
   assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
   uint64_t Result = 0;
 
-  for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX) {
-    if (Idx[CurIDX]->getType() == Type::LongTy) {
-      // Update Ty to refer to current element
-      Ty = cast<SequentialType>(Ty)->getElementType();
-
-      // Get the array index and the size of each array element.
-      int64_t arrayIdx = cast<ConstantSInt>(Idx[CurIDX])->getValue();
-      Result += arrayIdx * (int64_t)getTypeSize(Ty);
-    } else {
-      const StructType *STy = cast<StructType>(Ty);
-      assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx");
+  generic_gep_type_iterator<std::vector<Value*>::const_iterator>
+    TI = gep_type_begin(ptrTy, Idx.begin(), Idx.end());
+  for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX, ++TI) {
+    if (const StructType *STy = dyn_cast<StructType>(*TI)) {
+      assert(Idx[CurIDX]->getType() == Type::UIntTy && "Illegal struct idx");
       unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
 
       // Get structure layout information...
@@ -240,6 +235,13 @@
 
       // Update Ty to refer to current element
       Ty = STy->getElementType(FieldNo);
+    } else {
+      // Update Ty to refer to current element
+      Ty = cast<SequentialType>(Ty)->getElementType();
+
+      // Get the array index and the size of each array element.
+      int64_t arrayIdx = cast<ConstantInt>(Idx[CurIDX])->getRawValue();
+      Result += arrayIdx * (int64_t)getTypeSize(Ty);
     }
   }
 
diff --git a/lib/Target/X86/InstSelectSimple.cpp b/lib/Target/X86/InstSelectSimple.cpp
index fa7eef1..ad52353 100644
--- a/lib/Target/X86/InstSelectSimple.cpp
+++ b/lib/Target/X86/InstSelectSimple.cpp
@@ -2704,12 +2704,13 @@
       // idx is the index into the array.  Unlike with structure
       // indices, we may not know its actual value at code-generation
       // time.
-      assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
 
       // If idx is a constant, fold it into the offset.
       unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
       if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
         Disp += TypeSize*CSI->getValue();
+      } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
+        Disp += TypeSize*CUI->getValue();
       } else {
         // If the index reg is already taken, we can't handle this index.
         if (IndexReg) return;
@@ -2833,12 +2834,7 @@
       GEPOps.pop_back();        // Consume a GEP operand
       GEPTypes.pop_back();
 
-      // idx is the index into the array.  Unlike with structure
-      // indices, we may not know its actual value at code-generation
-      // time.
-      assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
-
-      // Most GEP instructions use a [cast (int/uint) to LongTy] as their
+      // Many GEP instructions use a [cast (int/uint) to LongTy] as their
       // operand on X86.  Handle this case directly now...
       if (CastInst *CI = dyn_cast<CastInst>(idx))
         if (CI->getOperand(0)->getType() == Type::IntTy ||
@@ -2852,9 +2848,9 @@
       unsigned elementSize = TD.getTypeSize(ElTy);
 
       // If idxReg is a constant, we don't need to perform the multiply!
-      if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
+      if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
         if (!CSI->isNullValue()) {
-          unsigned Offset = elementSize*CSI->getValue();
+          unsigned Offset = elementSize*CSI->getRawValue();
           unsigned Reg = makeAnotherReg(Type::UIntTy);
           BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
                                 .addReg(Reg).addImm(Offset);
diff --git a/lib/Target/X86/X86ISelSimple.cpp b/lib/Target/X86/X86ISelSimple.cpp
index fa7eef1..ad52353 100644
--- a/lib/Target/X86/X86ISelSimple.cpp
+++ b/lib/Target/X86/X86ISelSimple.cpp
@@ -2704,12 +2704,13 @@
       // idx is the index into the array.  Unlike with structure
       // indices, we may not know its actual value at code-generation
       // time.
-      assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
 
       // If idx is a constant, fold it into the offset.
       unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
       if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
         Disp += TypeSize*CSI->getValue();
+      } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
+        Disp += TypeSize*CUI->getValue();
       } else {
         // If the index reg is already taken, we can't handle this index.
         if (IndexReg) return;
@@ -2833,12 +2834,7 @@
       GEPOps.pop_back();        // Consume a GEP operand
       GEPTypes.pop_back();
 
-      // idx is the index into the array.  Unlike with structure
-      // indices, we may not know its actual value at code-generation
-      // time.
-      assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
-
-      // Most GEP instructions use a [cast (int/uint) to LongTy] as their
+      // Many GEP instructions use a [cast (int/uint) to LongTy] as their
       // operand on X86.  Handle this case directly now...
       if (CastInst *CI = dyn_cast<CastInst>(idx))
         if (CI->getOperand(0)->getType() == Type::IntTy ||
@@ -2852,9 +2848,9 @@
       unsigned elementSize = TD.getTypeSize(ElTy);
 
       // If idxReg is a constant, we don't need to perform the multiply!
-      if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
+      if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
         if (!CSI->isNullValue()) {
-          unsigned Offset = elementSize*CSI->getValue();
+          unsigned Offset = elementSize*CSI->getRawValue();
           unsigned Reg = makeAnotherReg(Type::UIntTy);
           BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
                                 .addReg(Reg).addImm(Offset);