Handle arguments passed in through the va_arg area
[oota-llvm.git] / lib / Transforms / TransformInternals.cpp
index 3e334eb5ebb00e0a8a563197ff94bcfefe7dd787..7aa5564fbee13b6744b39b4248ad85bcddc1ca91 100644 (file)
 #include "llvm/Function.h"
 #include "llvm/iOther.h"
 
-// TargetData Hack: Eventually we will have annotations given to us by the
-// backend so that we know stuff about type size and alignments.  For now
-// though, just use this, because it happens to match the model that GCC uses.
-//
-const TargetData TD("LevelRaise: Should be GCC though!");
-
-
 static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
-                                       std::vector<Value*> &Indices) {
+                                       std::vector<Value*> &Indices,
+                                       const TargetData &TD) {
   assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
   const StructLayout *SL = TD.getStructLayout(STy);
 
@@ -52,7 +46,7 @@ static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
 //
 const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
                                 std::vector<Value*> &Indices,
-                                bool StopEarly) {
+                                const TargetData &TD, bool StopEarly) {
   if (Offset == 0 && StopEarly && !Indices.empty())
     return Ty;    // Return the leaf type
 
@@ -60,9 +54,10 @@ const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
   const Type *NextType;
   if (const StructType *STy = dyn_cast<StructType>(Ty)) {
     ThisOffset = Offset;
-    NextType = getStructOffsetStep(STy, ThisOffset, Indices);
+    NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD);
   } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
-    assert(Offset < TD.getTypeSize(ATy) && "Offset not in composite!");
+    assert(Offset == 0 || Offset < TD.getTypeSize(ATy) &&
+           "Offset not in composite!");
 
     NextType = ATy->getElementType();
     unsigned ChildSize = TD.getTypeSize(NextType);
@@ -75,18 +70,19 @@ const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
 
   unsigned SubOffs = Offset - ThisOffset;
   const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
-                                           Indices, StopEarly);
+                                           Indices, TD, StopEarly);
   Offset = ThisOffset + SubOffs;
   return LeafTy;
 }
 
-// ConvertableToGEP - This function returns true if the specified value V is
+// ConvertibleToGEP - This function returns true if the specified value V is
 // a valid index into a pointer of type Ty.  If it is valid, Idx is filled in
 // with the values that would be appropriate to make this a getelementptr
 // instruction.  The type returned is the root type that the GEP would point to
 //
-const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
+const Type *ConvertibleToGEP(const Type *Ty, Value *OffsetVal,
                              std::vector<Value*> &Indices,
+                             const TargetData &TD,
                              BasicBlock::iterator *BI) {
   const CompositeType *CompTy = dyn_cast<CompositeType>(Ty);
   if (CompTy == 0) return 0;
@@ -116,11 +112,11 @@ const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
     if (const StructType *StructTy = dyn_cast<StructType>(CompTy)) {
       // Step into the appropriate element of the structure...
       uint64_t ActualOffset = (Offset < 0) ? 0 : (uint64_t)Offset;
-      NextTy = getStructOffsetStep(StructTy, ActualOffset, Indices);
+      NextTy = getStructOffsetStep(StructTy, ActualOffset, Indices, TD);
       Offset -= ActualOffset;
     } else {
       const Type *ElTy = cast<SequentialType>(CompTy)->getElementType();
-      if (!ElTy->isSized())
+      if (!ElTy->isSized() || (isa<PointerType>(CompTy) && !Indices.empty()))
         return 0; // Type is unreasonable... escape!
       unsigned ElSize = TD.getTypeSize(ElTy);
       int64_t ElSizeS = ElSize;
@@ -147,15 +143,14 @@ const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
 
           if (ScaleAmt && ScaleAmt != 1) {
             // If we have to scale up our index, do so now
-            Value *ScaleAmtVal = ConstantSInt::get(Type::LongTy,
-                                                   (unsigned)ScaleAmt);
+            Value *ScaleAmtVal = ConstantSInt::get(Type::LongTy, ScaleAmt);
             Expr.Var = BinaryOperator::create(Instruction::Mul, Expr.Var,
                                               ScaleAmtVal,
                                               Expr.Var->getName()+"-scale",*BI);
           }
 
           if (Index) {  // Add an offset to the index
-            Value *IndexAmt = ConstantSInt::get(Type::LongTy, (unsigned)Index);
+            Value *IndexAmt = ConstantSInt::get(Type::LongTy, Index);
             Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var,
                                               IndexAmt,
                                               Expr.Var->getName()+"-offset",