- Fix bug: LevelRaise/2002-10-08-VarArgCall.ll
[oota-llvm.git] / lib / Transforms / ExprTypeConvert.cpp
index a84f1d0f5e3f9feb5eb7a8126d8aab62d9afb3bc..f03253fac8d1bb3fed880ec58b08e8bc5156ddfd 100644 (file)
@@ -1,4 +1,4 @@
-//===- ExprTypeConvert.cpp - Code to change an LLVM Expr Type ---------------=//
+//===- ExprTypeConvert.cpp - Code to change an LLVM Expr Type -------------===//
 //
 // This file implements the part of level raising that checks to see if it is
 // possible to coerce an entire expression tree into a different type.  If
@@ -13,9 +13,8 @@
 #include "llvm/ConstantHandling.h"
 #include "llvm/Analysis/Expressions.h"
 #include "Support/STLExtras.h"
-#include "Support/StatisticReporter.h"
+#include "Support/Statistic.h"
 #include <algorithm>
-#include <iostream>
 using std::cerr;
 
 static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
@@ -24,19 +23,6 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
 static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
                                  ValueMapCache &VMC);
 
-// AllIndicesZero - Return true if all of the indices of the specified memory
-// access instruction are zero, indicating an effectively nil offset to the 
-// pointer value.
-//
-static bool AllIndicesZero(const MemAccessInst *MAI) {
-  for (User::const_op_iterator S = MAI->idx_begin(), E = MAI->idx_end();
-       S != E; ++S)
-    if (!isa<Constant>(S->get()) || !cast<Constant>(S->get())->isNullValue())
-      return false;
-  return true;
-}
-
-
 // Peephole Malloc instructions: we take a look at the use chain of the
 // malloc instruction, and try to find out if the following conditions hold:
 //   1. The malloc is of the form: 'malloc [sbyte], uint <constant>'
@@ -57,7 +43,7 @@ static bool MallocConvertableToType(MallocInst *MI, const Type *Ty,
   if (!Ty->isSized()) return false;      // Can only alloc something with a size
 
   // Analyze the number of bytes allocated...
-  analysis::ExprType Expr = analysis::ClassifyExpression(MI->getArraySize());
+  ExprType Expr = ClassifyExpression(MI->getArraySize());
 
   // Get information about the base datatype being allocated, before & after
   int ReqTypeSize = TD.getTypeSize(Ty);
@@ -67,13 +53,13 @@ static bool MallocConvertableToType(MallocInst *MI, const Type *Ty,
   if (!Expr.Offset && !Expr.Scale && OldTypeSize == 1) return false;
 
   // Get the offset and scale of the allocation...
-  int OffsetVal = Expr.Offset ? getConstantValue(Expr.Offset) : 0;
-  int ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) : (Expr.Var ? 1 : 0);
+  int64_t OffsetVal = Expr.Offset ? getConstantValue(Expr.Offset) : 0;
+  int64_t ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) :(Expr.Var != 0);
 
   // The old type might not be of unit size, take old size into consideration
   // here...
-  int Offset = OffsetVal * OldTypeSize;
-  int Scale  = ScaleVal  * OldTypeSize;
+  int64_t Offset = OffsetVal * OldTypeSize;
+  int64_t Scale  = ScaleVal  * OldTypeSize;
   
   // In order to be successful, both the scale and the offset must be a multiple
   // of the requested data type's size.
@@ -92,7 +78,7 @@ static Instruction *ConvertMallocToType(MallocInst *MI, const Type *Ty,
   BasicBlock::iterator It = BB->end();
 
   // Analyze the number of bytes allocated...
-  analysis::ExprType Expr = analysis::ClassifyExpression(MI->getArraySize());
+  ExprType Expr = ClassifyExpression(MI->getArraySize());
 
   const PointerType *AllocTy = cast<PointerType>(Ty);
   const Type *ElType = AllocTy->getElementType();
@@ -101,13 +87,13 @@ static Instruction *ConvertMallocToType(MallocInst *MI, const Type *Ty,
   unsigned OldTypeSize = TD.getTypeSize(MI->getType()->getElementType());
 
   // Get the offset and scale coefficients that we are allocating...
-  int OffsetVal = (Expr.Offset ? getConstantValue(Expr.Offset) : 0);
-  int ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) : (Expr.Var ? 1 : 0);
+  int64_t OffsetVal = (Expr.Offset ? getConstantValue(Expr.Offset) : 0);
+  int64_t ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) : (Expr.Var !=0);
 
   // The old type might not be of unit size, take old size into consideration
   // here...
-  unsigned Offset = (unsigned)OffsetVal * OldTypeSize / DataSize;
-  unsigned Scale  = (unsigned)ScaleVal  * OldTypeSize / DataSize;
+  unsigned Offset = (uint64_t)OffsetVal * OldTypeSize / DataSize;
+  unsigned Scale  = (uint64_t)ScaleVal  * OldTypeSize / DataSize;
 
   // Locate the malloc instruction, because we may be inserting instructions
   It = MI;
@@ -115,21 +101,14 @@ static Instruction *ConvertMallocToType(MallocInst *MI, const Type *Ty,
   // If we have a scale, apply it first...
   if (Expr.Var) {
     // Expr.Var is not neccesarily unsigned right now, insert a cast now.
-    if (Expr.Var->getType() != Type::UIntTy) {
-      Instruction *CI = new CastInst(Expr.Var, Type::UIntTy);
-      if (Expr.Var->hasName()) CI->setName(Expr.Var->getName()+"-uint");
-      It = ++BB->getInstList().insert(It, CI);
-      Expr.Var = CI;
-    }
+    if (Expr.Var->getType() != Type::UIntTy)
+      Expr.Var = new CastInst(Expr.Var, Type::UIntTy,
+                              Expr.Var->getName()+"-uint", It);
 
-    if (Scale != 1) {
-      Instruction *ScI =
-        BinaryOperator::create(Instruction::Mul, Expr.Var,
-                               ConstantUInt::get(Type::UIntTy, Scale));
-      if (Expr.Var->hasName()) ScI->setName(Expr.Var->getName()+"-scl");
-      It = ++BB->getInstList().insert(It, ScI);
-      Expr.Var = ScI;
-    }
+    if (Scale != 1)
+      Expr.Var = BinaryOperator::create(Instruction::Mul, Expr.Var,
+                                        ConstantUInt::get(Type::UIntTy, Scale),
+                                        Expr.Var->getName()+"-scl", It);
 
   } else {
     // If we are not scaling anything, just make the offset be the "var"...
@@ -140,19 +119,13 @@ static Instruction *ConvertMallocToType(MallocInst *MI, const Type *Ty,
   // If we have an offset now, add it in...
   if (Offset != 0) {
     assert(Expr.Var && "Var must be nonnull by now!");
-
-    Instruction *AddI =
-      BinaryOperator::create(Instruction::Add, Expr.Var,
-                             ConstantUInt::get(Type::UIntTy, Offset));
-    if (Expr.Var->hasName()) AddI->setName(Expr.Var->getName()+"-off");
-    It = ++BB->getInstList().insert(It, AddI);
-    Expr.Var = AddI;
+    Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var,
+                                      ConstantUInt::get(Type::UIntTy, Offset),
+                                      Expr.Var->getName()+"-off", It);
   }
 
-  Instruction *NewI = new MallocInst(AllocTy, Expr.Var, Name);
-
   assert(AllocTy == Ty);
-  return NewI;
+  return new MallocInst(AllocTy->getElementType(), Expr.Var, Name);
 }
 
 
@@ -202,14 +175,17 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty,
 
   case Instruction::Add:
   case Instruction::Sub:
+    if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false;
     if (!ExpressionConvertableToType(I->getOperand(0), Ty, CTMap) ||
         !ExpressionConvertableToType(I->getOperand(1), Ty, CTMap))
       return false;
     break;
   case Instruction::Shr:
+    if (!Ty->isInteger()) return false;
     if (Ty->isSigned() != V->getType()->isSigned()) return false;
     // FALL THROUGH
   case Instruction::Shl:
+    if (!Ty->isInteger()) return false;
     if (!ExpressionConvertableToType(I->getOperand(0), Ty, CTMap))
       return false;
     break;
@@ -253,12 +229,12 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty,
     // index array.  If there are, check to see if removing them causes us to
     // get to the right type...
     //
-    std::vector<Value*> Indices = GEP->copyIndices();
+    std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
     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)
@@ -269,11 +245,11 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty,
     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
@@ -296,12 +272,12 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty,
     }
 
     // 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);
@@ -362,7 +338,6 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) {
 
 
   BasicBlock *BB = I->getParent();
-  BasicBlock::InstListType &BIL = BB->getInstList();
   std::string Name = I->getName();  if (!Name.empty()) I->setName("");
   Instruction *Res;     // Result of conversion
 
@@ -417,7 +392,7 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) {
       BasicBlock *BB = OldPN->getIncomingBlock(0);
       Value *OldVal = OldPN->getIncomingValue(0);
       ValueHandle OldValHandle(VMC, OldVal);
-      OldPN->removeIncomingValue(BB);
+      OldPN->removeIncomingValue(BB, false);
       Value *V = ConvertExpressionToType(OldVal, Ty, VMC);
       NewPN->addIncoming(V, BB);
     }
@@ -446,25 +421,24 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) {
     // index array.  If there are, check to see if removing them causes us to
     // get to the right type...
     //
-    std::vector<Value*> Indices = GEP->copyIndices();
+    std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
     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
@@ -497,8 +471,9 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) {
     //
     if (Res == 0) {
       const PointerType *NewSrcTy = PointerType::get(PVTy);
+      std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
       Res = new GetElementPtrInst(Constant::getNullValue(NewSrcTy),
-                                  GEP->copyIndices(), Name);
+                                  Indices, Name);
       VMC.ExprMap[I] = Res;
       Res->setOperand(0, ConvertExpressionToType(I->getOperand(0),
                                                  NewSrcTy, VMC));
@@ -516,7 +491,7 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) {
 
   assert(Res->getType() == Ty && "Didn't convert expr to correct type!");
 
-  BIL.insert(I, Res);
+  BB->getInstList().insert(I, Res);
 
   // Add the instruction to the expression map
   VMC.ExprMap[I] = Res;
@@ -632,6 +607,8 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
     }
     // FALLTHROUGH
   case Instruction::Sub: {
+    if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false;
+
     Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
     return ValueConvertableToType(I, Ty, CTMap) &&
            ExpressionConvertableToType(OtherOp, Ty, CTMap);
@@ -646,6 +623,7 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
     // FALL THROUGH
   case Instruction::Shl:
     assert(I->getOperand(0) == V);
+    if (!Ty->isInteger()) return false;
     return ValueConvertableToType(I, Ty, CTMap);
 
   case Instruction::Free:
@@ -770,7 +748,7 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
       //
       if (DataSize != 1) {
         TempScale = BinaryOperator::create(Instruction::Mul, Index,
-                                           ConstantUInt::get(Type::UIntTy,
+                                           ConstantSInt::get(Type::LongTy,
                                                              DataSize));
         Index = TempScale;
       }
@@ -889,7 +867,6 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
 
   BasicBlock *BB = I->getParent();
   assert(BB != 0 && "Instruction not embedded in basic block!");
-  BasicBlock::InstListType &BIL = BB->getInstList();
   std::string Name = I->getName();
   I->setName("");
   Instruction *Res;     // Result of conversion
@@ -974,16 +951,15 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
 
     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);
       assert(LoadedTy->isFirstClassType());
 
       if (Indices.size() != 1) {     // Do not generate load X, 0
-        Src = new GetElementPtrInst(Src, Indices, Name+".idx");
         // Insert the GEP instruction before this load.
-        BIL.insert(I, cast<Instruction>(Src));
+        Src = new GetElementPtrInst(Src, Indices, Name+".idx", I);
       }
     }
     
@@ -1011,17 +987,16 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
           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);
           assert(Offset == 0 && "Offset changed!");
           assert(NewTy == Ty && "Did not convert to correct type!");
 
+          // Insert the GEP instruction before this store.
           SrcPtr = new GetElementPtrInst(SrcPtr, Indices,
-                                         SrcPtr->getName()+".idx");
-          // Insert the GEP instruction before this load.
-          BIL.insert(I, cast<Instruction>(SrcPtr));
+                                         SrcPtr->getName()+".idx", I);
         }
         Res = new StoreInst(NewVal, SrcPtr);
 
@@ -1041,17 +1016,16 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
 
       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);
 
         assert(Offset == 0 && ValTy);
 
+        // Insert the GEP instruction before this store.
         SrcPtr = new GetElementPtrInst(SrcPtr, Indices,
-                                       SrcPtr->getName()+".idx");
-        // Insert the GEP instruction before this load.
-        BIL.insert(I, cast<Instruction>(SrcPtr));
+                                       SrcPtr->getName()+".idx", I);
       }
 
       Res = new StoreInst(Constant::getNullValue(ValTy), SrcPtr);
@@ -1074,8 +1048,8 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
     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));
-      It = ++BIL.insert(It, cast<Instruction>(Index));
+                                     ConstantSInt::get(Type::LongTy, DataSize),
+                                     "scale", It);
     }
 
     // Perform the conversion now...
@@ -1108,9 +1082,9 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
       // to        getelementptr  long * %reg123, uint %N
       // ... where the type must simply stay the same size...
       //
-      Res = new GetElementPtrInst(NewVal,
-                                  cast<GetElementPtrInst>(I)->copyIndices(),
-                                  Name);
+      GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
+      std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
+      Res = new GetElementPtrInst(NewVal, Indices, Name);
     }
 #endif
     break;
@@ -1123,7 +1097,7 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
     while (OldPN->getNumOperands()) {
       BasicBlock *BB = OldPN->getIncomingBlock(0);
       Value *OldVal = OldPN->getIncomingValue(0);
-      OldPN->removeIncomingValue(BB);
+      OldPN->removeIncomingValue(BB, false);
       Value *V = ConvertExpressionToType(OldVal, NewTy, VMC);
       NewPN->addIncoming(V, BB);
     }
@@ -1156,8 +1130,7 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
           // Create a cast to convert it to the right type, we know that this
           // is a lossless cast...
           //
-          Params[i] = new CastInst(Params[i], PTs[i], "call.resolve.cast");
-          It = ++BIL.insert(It, cast<Instruction>(Params[i]));
+          Params[i] = new CastInst(Params[i], PTs[i], "call.resolve.cast", It);
         }
       Meth = NewVal;  // Update call destination to new value
 
@@ -1181,8 +1154,8 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
   // stream.
   //
   BasicBlock::iterator It = I;
-  assert(It != BIL.end() && "Instruction not in own basic block??");
-  BIL.insert(It, Res);   // Keep It pointing to old instruction
+  assert(It != BB->end() && "Instruction not in own basic block??");
+  BB->getInstList().insert(It, Res);   // Keep It pointing to old instruction
 
   DEBUG(cerr << "COT CREATED: "  << (void*)Res << " " << Res
              << "In: " << (void*)I << " " << I << "Out: " << (void*)Res