- Change getelementptr instruction to use long indexes instead of uint
    indexes for sequential types.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@3681 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/InstrSelection/InstrSelectionSupport.cpp b/lib/CodeGen/InstrSelection/InstrSelectionSupport.cpp
index 870bbbd..036b1a7 100644
--- a/lib/CodeGen/InstrSelection/InstrSelectionSupport.cpp
+++ b/lib/CodeGen/InstrSelection/InstrSelectionSupport.cpp
@@ -113,12 +113,12 @@
 FoldGetElemChain(InstrTreeNode* ptrNode, vector<Value*>& chainIdxVec)
 {
   InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
-  if (gepNode == NULL)
-    return NULL;                // ptr value is not computed in this tree
-
   GetElementPtrInst* gepInst =
-    dyn_cast<GetElementPtrInst>(gepNode->getInstruction());
-  if (gepInst == NULL)          // ptr value does not come from GEP instruction
+    dyn_cast_or_null<GetElementPtrInst>(gepNode->getInstruction());
+
+  // ptr value is not computed in this tree or ptr value does not come from GEP
+  // instruction
+  if (gepInst == NULL)
     return NULL;
 
   // Return NULL if we don't fold any instructions in.
@@ -149,8 +149,8 @@
           ptrVal = gepInst->getPointerOperand();
 
           // Check for a leading [0] index, if any.  It will be discarded later.
-          ConstantUInt* CV = dyn_cast<ConstantUInt>((Value*) *firstIdx);
-          hasLeadingZero = bool(CV && CV->getValue() == 0);
+          hasLeadingZero = (*firstIdx ==
+                              Constant::getNullValue((*firstIdx)->getType()));
 
           // Insert its index vector at the start, skipping any leading [0]
           chainIdxVec.insert(chainIdxVec.begin(),
@@ -168,7 +168,7 @@
   // If the first getElementPtr instruction had a leading [0], add it back.
   // Note that this instruction is the *last* one successfully folded above.
   if (ptrVal && hasLeadingZero) 
-    chainIdxVec.insert(chainIdxVec.begin(), ConstantUInt::get(Type::UIntTy,0));
+    chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
 
   return ptrVal;
 }
diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp
index 941af03..bc3829f 100644
--- a/lib/Target/CBackend/CBackend.cpp
+++ b/lib/Target/CBackend/CBackend.cpp
@@ -916,7 +916,7 @@
   }
 
   for (; I != E; ++I)
-    if ((*I)->getType() == Type::UIntTy) {
+    if ((*I)->getType() == Type::LongTy) {
       Out << "[((int) (";                 // sign-extend from 32 (to 64) bits
       writeOperand(*I);
       Out << " * sizeof(";
@@ -925,7 +925,7 @@
       printType(cast<PointerType>(Ptr->getType())->getElementType());
       Out << ")]";
     } else {
-      Out << ".field" << cast<ConstantUInt>(*I)->getValue();
+      Out << ".field" << cast<ConstantSInt>(*I)->getValue();
     }
 }
 
diff --git a/lib/Target/CBackend/Writer.cpp b/lib/Target/CBackend/Writer.cpp
index 941af03..bc3829f 100644
--- a/lib/Target/CBackend/Writer.cpp
+++ b/lib/Target/CBackend/Writer.cpp
@@ -916,7 +916,7 @@
   }
 
   for (; I != E; ++I)
-    if ((*I)->getType() == Type::UIntTy) {
+    if ((*I)->getType() == Type::LongTy) {
       Out << "[((int) (";                 // sign-extend from 32 (to 64) bits
       writeOperand(*I);
       Out << " * sizeof(";
@@ -925,7 +925,7 @@
       printType(cast<PointerType>(Ptr->getType())->getElementType());
       Out << ")]";
     } else {
-      Out << ".field" << cast<ConstantUInt>(*I)->getValue();
+      Out << ".field" << cast<ConstantSInt>(*I)->getValue();
     }
 }
 
diff --git a/lib/Target/SparcV9/InstrSelection/InstrSelectionSupport.cpp b/lib/Target/SparcV9/InstrSelection/InstrSelectionSupport.cpp
index 870bbbd..036b1a7 100644
--- a/lib/Target/SparcV9/InstrSelection/InstrSelectionSupport.cpp
+++ b/lib/Target/SparcV9/InstrSelection/InstrSelectionSupport.cpp
@@ -113,12 +113,12 @@
 FoldGetElemChain(InstrTreeNode* ptrNode, vector<Value*>& chainIdxVec)
 {
   InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
-  if (gepNode == NULL)
-    return NULL;                // ptr value is not computed in this tree
-
   GetElementPtrInst* gepInst =
-    dyn_cast<GetElementPtrInst>(gepNode->getInstruction());
-  if (gepInst == NULL)          // ptr value does not come from GEP instruction
+    dyn_cast_or_null<GetElementPtrInst>(gepNode->getInstruction());
+
+  // ptr value is not computed in this tree or ptr value does not come from GEP
+  // instruction
+  if (gepInst == NULL)
     return NULL;
 
   // Return NULL if we don't fold any instructions in.
@@ -149,8 +149,8 @@
           ptrVal = gepInst->getPointerOperand();
 
           // Check for a leading [0] index, if any.  It will be discarded later.
-          ConstantUInt* CV = dyn_cast<ConstantUInt>((Value*) *firstIdx);
-          hasLeadingZero = bool(CV && CV->getValue() == 0);
+          hasLeadingZero = (*firstIdx ==
+                              Constant::getNullValue((*firstIdx)->getType()));
 
           // Insert its index vector at the start, skipping any leading [0]
           chainIdxVec.insert(chainIdxVec.begin(),
@@ -168,7 +168,7 @@
   // If the first getElementPtr instruction had a leading [0], add it back.
   // Note that this instruction is the *last* one successfully folded above.
   if (ptrVal && hasLeadingZero) 
-    chainIdxVec.insert(chainIdxVec.begin(), ConstantUInt::get(Type::UIntTy,0));
+    chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
 
   return ptrVal;
 }
diff --git a/lib/Transforms/ExprTypeConvert.cpp b/lib/Transforms/ExprTypeConvert.cpp
index ab56c52..4abbbeb 100644
--- a/lib/Transforms/ExprTypeConvert.cpp
+++ b/lib/Transforms/ExprTypeConvert.cpp
@@ -233,8 +233,8 @@
     const Type *BaseType = GEP->getPointerOperand()->getType();
     const Type *ElTy = 0;
 
-    while (!Indices.empty() && isa<ConstantUInt>(Indices.back()) &&
-           cast<ConstantUInt>(Indices.back())->getValue() == 0) {
+    while (!Indices.empty() &&
+           Indices.back() == Constant::getNullValue(Indices.back()->getType())){
       Indices.pop_back();
       ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices, true);
       if (ElTy == PVTy)
@@ -245,11 +245,11 @@
     if (ElTy) break;   // Found a number of zeros we can strip off!
 
     // Otherwise, we can convert a GEP from one form to the other iff the
-    // current gep is of the form 'getelementptr sbyte*, unsigned N
+    // current gep is of the form 'getelementptr sbyte*, long N
     // and we could convert this to an appropriate GEP for the new type.
     //
     if (GEP->getNumOperands() == 2 &&
-        GEP->getOperand(1)->getType() == Type::UIntTy &&
+        GEP->getOperand(1)->getType() == Type::LongTy &&
         GEP->getType() == PointerType::get(Type::SByteTy)) {
 
       // Do not Check to see if our incoming pointer can be converted
@@ -272,12 +272,12 @@
     }
 
     // Otherwise, it could be that we have something like this:
-    //     getelementptr [[sbyte] *] * %reg115, uint %reg138    ; [sbyte]**
+    //     getelementptr [[sbyte] *] * %reg115, long %reg138    ; [sbyte]**
     // and want to convert it into something like this:
-    //     getelemenptr [[int] *] * %reg115, uint %reg138      ; [int]**
+    //     getelemenptr [[int] *] * %reg115, long %reg138      ; [int]**
     //
     if (GEP->getNumOperands() == 2 && 
-        GEP->getOperand(1)->getType() == Type::UIntTy &&
+        GEP->getOperand(1)->getType() == Type::LongTy &&
         TD.getTypeSize(PTy->getElementType()) == 
         TD.getTypeSize(GEP->getType()->getElementType())) {
       const PointerType *NewSrcTy = PointerType::get(PVTy);
@@ -425,21 +425,20 @@
     const Type *BaseType = GEP->getPointerOperand()->getType();
     const Type *PVTy = cast<PointerType>(Ty)->getElementType();
     Res = 0;
-    while (!Indices.empty() && isa<ConstantUInt>(Indices.back()) &&
-           cast<ConstantUInt>(Indices.back())->getValue() == 0) {
+    while (!Indices.empty() &&
+           Indices.back() == Constant::getNullValue(Indices.back()->getType())){
       Indices.pop_back();
       if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) {
-        if (Indices.size() == 0) {
-          Res = new CastInst(GEP->getPointerOperand(), BaseType); // NOOP
-        } else {
+        if (Indices.size() == 0)
+          Res = new CastInst(GEP->getPointerOperand(), BaseType); // NOOP CAST
+        else
           Res = new GetElementPtrInst(GEP->getPointerOperand(), Indices, Name);
-        }
         break;
       }
     }
 
     if (Res == 0 && GEP->getNumOperands() == 2 &&
-        GEP->getOperand(1)->getType() == Type::UIntTy &&
+        GEP->getOperand(1)->getType() == Type::LongTy &&
         GEP->getType() == PointerType::get(Type::SByteTy)) {
       
       // Otherwise, we can convert a GEP from one form to the other iff the
@@ -749,7 +748,7 @@
       //
       if (DataSize != 1) {
         TempScale = BinaryOperator::create(Instruction::Mul, Index,
-                                           ConstantUInt::get(Type::UIntTy,
+                                           ConstantSInt::get(Type::LongTy,
                                                              DataSize));
         Index = TempScale;
       }
@@ -952,7 +951,7 @@
 
     if (const CompositeType *CT = dyn_cast<CompositeType>(LoadedTy)) {
       std::vector<Value*> Indices;
-      Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
+      Indices.push_back(ConstantSInt::get(Type::LongTy, 0));
 
       unsigned Offset = 0;   // No offset, get first leaf.
       LoadedTy = getStructOffsetType(CT, Offset, Indices, false);
@@ -988,7 +987,7 @@
           const StructType *SElTy = cast<StructType>(ElTy);
           
           std::vector<Value*> Indices;
-          Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
+          Indices.push_back(Constant::getNullValue(Type::LongTy));
 
           unsigned Offset = 0;
           const Type *Ty = getStructOffsetType(ElTy, Offset, Indices, false);
@@ -1017,7 +1016,7 @@
 
       if (isa<StructType>(ValTy)) {
         std::vector<Value*> Indices;
-        Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
+        Indices.push_back(Constant::getNullValue(Type::LongTy));
 
         unsigned Offset = 0;
         ValTy = getStructOffsetType(ValTy, Offset, Indices, false);
@@ -1049,7 +1048,7 @@
     if (DataSize != 1) {
       // Insert a multiply of the old element type is not a unit size...
       Index = BinaryOperator::create(Instruction::Mul, Index,
-                                     ConstantUInt::get(Type::UIntTy, DataSize),
+                                     ConstantSInt::get(Type::LongTy, DataSize),
                                      "scale", It);
     }
 
diff --git a/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp b/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp
index 2e5c0e7..b8edb22 100644
--- a/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp
+++ b/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp
@@ -55,9 +55,9 @@
   
   //M->getGlobalList().push_back(gbl);
 
-  vector<Value *> elargs;
-  elargs.push_back(ConstantUInt::get(Type::UIntTy, 0));
-  elargs.push_back(ConstantUInt::get(Type::UIntTy, 0));
+  //vector<Value *> elargs;
+  //elargs.push_back(ConstantSInt::get(Type::LongTy, 0));
+  //elargs.push_back(ConstantSInt::get(Type::LongTy, 0));
 
   // commented out bb name frm which its called
   //Instruction *getElmntInst=new GetElementPtrInst(gbl,elargs,"elmntInst");
@@ -119,7 +119,7 @@
     assert(inc>=0 && inc<=numPaths && "inc out of bound!");
    
     Instruction *Idx = new GetElementPtrInst(countInst, 
-                 vector<Value*>(1,ConstantUInt::get(Type::UIntTy, inc)),
+                 vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
                                              "", InsertPos);
 
     Instruction *ldInst=new LoadInst(Idx, "ti1", InsertPos);
@@ -154,7 +154,7 @@
     //now load count[addIndex]
     
     Instruction *castInst=new CastInst(addIndex, 
-				       Type::UIntTy,"ctin", InsertPos);
+				       Type::LongTy,"ctin", InsertPos);
     Instruction *Idx = new GetElementPtrInst(countInst, 
                                              vector<Value*>(1,castInst), "",
                                              InsertPos);
@@ -184,7 +184,7 @@
     Instruction *ldIndex=new LoadInst(rInst, "ti1", InsertPos);
     
     //now load count[addIndex]
-    Instruction *castInst2=new CastInst(ldIndex, Type::UIntTy,"ctin",InsertPos);
+    Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin",InsertPos);
     Instruction *Idx = new GetElementPtrInst(countInst, 
                                              vector<Value*>(1,castInst2), "",
                                              InsertPos);
@@ -236,10 +236,6 @@
 
   Value *Int0 = ConstantInt::get(Type::IntTy, 0);
   
-  //store uint 0, uint *%R, uint 0
-  vector<Value *> idx;
-  idx.push_back(ConstantUInt::get(Type::UIntTy, 0));
-
   //now push all instructions in front of the BB
   BasicBlock::iterator here=front->begin();
   front->getInstList().insert(here, rVar);
@@ -249,13 +245,13 @@
 
   for (int i=0;i<k; i++){
     Value *GEP2 = new GetElementPtrInst(countVar,
-                          vector<Value *>(1,ConstantUInt::get(Type::UIntTy, i)),
+                          vector<Value *>(1,ConstantSInt::get(Type::LongTy, i)),
                                         "", here);
     new StoreInst(Int0, GEP2, here);
   }
 
-  Instruction *GEP = new GetElementPtrInst(rVar, idx, "", here);
-  new StoreInst(Int0, GEP, here);
+  //store uint 0, uint *%R
+  new StoreInst(Int0, rVar, here);
 }
 
 
diff --git a/lib/Transforms/Instrumentation/TraceValues.cpp b/lib/Transforms/Instrumentation/TraceValues.cpp
index f3fc7ba..5abfe27 100644
--- a/lib/Transforms/Instrumentation/TraceValues.cpp
+++ b/lib/Transforms/Instrumentation/TraceValues.cpp
@@ -226,7 +226,7 @@
   // Turn the format string into an sbyte *
   Instruction *GEP = 
     new GetElementPtrInst(fmtVal,
-                          vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
+                          vector<Value*>(2,ConstantSInt::get(Type::LongTy, 0)),
                           "trstr", InsertBefore);
   
   // Insert a call to the hash function if this is a pointer value
diff --git a/lib/Transforms/LevelRaise.cpp b/lib/Transforms/LevelRaise.cpp
index 92f2f79..fd32251 100644
--- a/lib/Transforms/LevelRaise.cpp
+++ b/lib/Transforms/LevelRaise.cpp
@@ -323,7 +323,7 @@
 
           // Build the index vector, full of all zeros
           std::vector<Value*> Indices;
-          Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
+          Indices.push_back(ConstantSInt::get(Type::LongTy, 0));
           while (CurCTy && !isa<PointerType>(CurCTy)) {
             if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
               // Check for a zero element struct type... if we have one, bail.
@@ -338,7 +338,7 @@
             }
 
             // Insert a zero to index through this type...
-            Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0));
+            Indices.push_back(Constant::getNullValue(CurCTy->getIndexType()));
 
             // Did we find what we're looking for?
             if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break;
diff --git a/lib/Transforms/TransformInternals.cpp b/lib/Transforms/TransformInternals.cpp
index f9ee232..8586832 100644
--- a/lib/Transforms/TransformInternals.cpp
+++ b/lib/Transforms/TransformInternals.cpp
@@ -66,7 +66,7 @@
 
     NextType = ATy->getElementType();
     unsigned ChildSize = TD.getTypeSize(NextType);
-    Indices.push_back(ConstantUInt::get(Type::UIntTy, Offset/ChildSize));
+    Indices.push_back(ConstantSInt::get(Type::LongTy, Offset/ChildSize));
     ThisOffset = (Offset/ChildSize)*ChildSize;
   } else {
     Offset = 0;   // Return the offset that we were able to acheive
@@ -141,13 +141,13 @@
 
         if (BI) {              // Generate code?
           BasicBlock *BB = (*BI)->getParent();
-          if (Expr.Var->getType() != Type::UIntTy)
-            Expr.Var = new CastInst(Expr.Var, Type::UIntTy,
+          if (Expr.Var->getType() != Type::LongTy)
+            Expr.Var = new CastInst(Expr.Var, Type::LongTy,
                                     Expr.Var->getName()+"-idxcast", *BI);
 
           if (ScaleAmt && ScaleAmt != 1) {
             // If we have to scale up our index, do so now
-            Value *ScaleAmtVal = ConstantUInt::get(Type::UIntTy,
+            Value *ScaleAmtVal = ConstantSInt::get(Type::LongTy,
                                                    (unsigned)ScaleAmt);
             Expr.Var = BinaryOperator::create(Instruction::Mul, Expr.Var,
                                               ScaleAmtVal,
@@ -155,7 +155,7 @@
           }
 
           if (Index) {  // Add an offset to the index
-            Value *IndexAmt = ConstantUInt::get(Type::UIntTy, (unsigned)Index);
+            Value *IndexAmt = ConstantSInt::get(Type::LongTy, (unsigned)Index);
             Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var,
                                               IndexAmt,
                                               Expr.Var->getName()+"-offset",
@@ -168,14 +168,14 @@
       } else if (Offset >= (int)ElSize || -Offset >= (int)ElSize) {
         // Calculate the index that we are entering into the array cell with
         unsigned Index = Offset/ElSize;
-        Indices.push_back(ConstantUInt::get(Type::UIntTy, Index));
+        Indices.push_back(ConstantSInt::get(Type::LongTy, Index));
         Offset -= (int)(Index*ElSize);            // Consume part of the offset
 
       } else if (isa<ArrayType>(CompTy) || Indices.empty()) {
         // Must be indexing a small amount into the first cell of the array
         // Just index into element zero of the array here.
         //
-        Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
+        Indices.push_back(ConstantSInt::get(Type::LongTy, 0));
       } else {
         return 0;  // Hrm. wierd, can't handle this case.  Bail
       }