Do not let dead constant expressions hanging off of functions prevent IPCP.
[oota-llvm.git] / lib / Transforms / ExprTypeConvert.cpp
index 9fc9c93d4bfc09ae28fc6878edf04b73544f8594..72733570d2b61978d8d5479b0e66f233f78ffd99 100644 (file)
 
 #include "TransformInternals.h"
 #include "llvm/Constants.h"
-#include "llvm/iOther.h"
-#include "llvm/iPHINode.h"
-#include "llvm/iMemory.h"
-
+#include "llvm/Instructions.h"
 #include "llvm/Analysis/Expressions.h"
-#include "Support/STLExtras.h"
-#include "Support/Debug.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Debug.h"
 #include <algorithm>
 using namespace llvm;
 
@@ -154,7 +151,7 @@ bool llvm::ExpressionConvertibleToType(Value *V, const Type *Ty,
   // If it's a constant... all constants can be converted to a different
   // type.
   //
-  if (Constant *CPV = dyn_cast<Constant>(V))
+  if (isa<Constant>(V) && !isa<GlobalValue>(V))
     return true;
   
   CTMap[V] = Ty;
@@ -206,6 +203,9 @@ bool llvm::ExpressionConvertibleToType(Value *V, const Type *Ty,
   }
   case Instruction::PHI: {
     PHINode *PN = cast<PHINode>(I);
+    // Be conservative if we find a giant PHI node.
+    if (PN->getNumIncomingValues() > 32) return false;
+
     for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i)
       if (!ExpressionConvertibleToType(PN->getIncomingValue(i), Ty, CTMap, TD))
         return false;
@@ -344,7 +344,7 @@ Value *llvm::ConvertExpressionToType(Value *V, const Type *Ty,
     return VMCI->second;
   }
 
-  DEBUG(std::cerr << "CETT: " << (void*)V << " " << V);
+  DEBUG(std::cerr << "CETT: " << (void*)V << " " << *V);
 
   Instruction *I = dyn_cast<Instruction>(V);
   if (I == 0) {
@@ -549,8 +549,8 @@ Value *llvm::ConvertExpressionToType(Value *V, const Type *Ty,
     if (NumUses == OldSize) ++It;
   }
 
-  DEBUG(std::cerr << "ExpIn: " << (void*)I << " " << I
-                  << "ExpOut: " << (void*)Res << " " << Res);
+  DEBUG(std::cerr << "ExpIn: " << (void*)I << " " << *I
+                  << "ExpOut: " << (void*)Res << " " << *Res);
 
   return Res;
 }
@@ -794,10 +794,13 @@ static bool OperandConvertibleToType(User *U, Value *V, const Type *Ty,
       // stream, so we have to delete it when we're done.
       //
       if (DataSize != 1) {
-        // FIXME, PR82
-        TempScale = BinaryOperator::create(Instruction::Mul, Index,
-                                           ConstantSInt::get(Type::LongTy,
-                                                             DataSize));
+        Value *CST;
+        if (Index->getType()->isSigned())
+          CST = ConstantSInt::get(Index->getType(), DataSize);
+        else
+          CST = ConstantUInt::get(Index->getType(), DataSize);
+                                  
+        TempScale = BinaryOperator::create(Instruction::Mul, Index, CST);
         Index = TempScale;
       }
 
@@ -815,6 +818,9 @@ static bool OperandConvertibleToType(User *U, Value *V, const Type *Ty,
 
   case Instruction::PHI: {
     PHINode *PN = cast<PHINode>(I);
+    // Be conservative if we find a giant PHI node.
+    if (PN->getNumIncomingValues() > 32) return false;
+
     for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i)
       if (!ExpressionConvertibleToType(PN->getIncomingValue(i), Ty, CTMap, TD))
         return false;
@@ -822,7 +828,7 @@ static bool OperandConvertibleToType(User *U, Value *V, const Type *Ty,
   }
 
   case Instruction::Call: {
-    User::op_iterator OI = find(I->op_begin(), I->op_end(), V);
+    User::op_iterator OI = std::find(I->op_begin(), I->op_end(), V);
     assert (OI != I->op_end() && "Not using value!");
     unsigned OpNum = OI - I->op_begin();
 
@@ -978,10 +984,9 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
 
     unsigned OtherIdx = (OldVal == I->getOperand(0)) ? 1 : 0;
     Value *OtherOp    = I->getOperand(OtherIdx);
+    Res->setOperand(!OtherIdx, NewVal);
     Value *NewOther   = ConvertExpressionToType(OtherOp, NewTy, VMC, TD);
-
     Res->setOperand(OtherIdx, NewOther);
-    Res->setOperand(!OtherIdx, NewVal);
     break;
   }
   case Instruction::Shl:
@@ -1006,8 +1011,7 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
 
     if (const CompositeType *CT = dyn_cast<CompositeType>(LoadedTy)) {
       std::vector<Value*> Indices;
-      // FIXME, PR82
-      Indices.push_back(ConstantSInt::get(Type::LongTy, 0));
+      Indices.push_back(Constant::getNullValue(Type::UIntTy));
 
       unsigned Offset = 0;   // No offset, get first leaf.
       LoadedTy = getStructOffsetType(CT, Offset, Indices, TD, false);
@@ -1043,8 +1047,7 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
           const StructType *SElTy = cast<StructType>(ElTy);
           
           std::vector<Value*> Indices;
-          // FIXME, PR82
-          Indices.push_back(Constant::getNullValue(Type::LongTy));
+          Indices.push_back(Constant::getNullValue(Type::UIntTy));
 
           unsigned Offset = 0;
           const Type *Ty = getStructOffsetType(ElTy, Offset, Indices, TD,false);
@@ -1073,8 +1076,7 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
 
       if (isa<StructType>(ValTy)) {
         std::vector<Value*> Indices;
-        // FIXME: PR82
-        Indices.push_back(Constant::getNullValue(Type::LongTy));
+        Indices.push_back(Constant::getNullValue(Type::UIntTy));
 
         unsigned Offset = 0;
         ValTy = getStructOffsetType(ValTy, Offset, Indices, TD, false);
@@ -1106,10 +1108,13 @@ 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,
-                                     // FIXME: PR82
-                                     ConstantSInt::get(Type::LongTy, DataSize),
-                                     "scale", It);
+      Value *CST;
+      if (Index->getType()->isSigned())
+        CST = ConstantSInt::get(Index->getType(), DataSize);
+      else
+        CST = ConstantUInt::get(Index->getType(), DataSize);
+
+      Index = BinaryOperator::create(Instruction::Mul, Index, CST, "scale", It);
     }
 
     // Perform the conversion now...
@@ -1157,6 +1162,7 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
     while (OldPN->getNumOperands()) {
       BasicBlock *BB = OldPN->getIncomingBlock(0);
       Value *OldVal = OldPN->getIncomingValue(0);
+      ValueHandle OldValHandle(VMC, OldVal);
       OldPN->removeIncomingValue(BB, false);
       Value *V = ConvertExpressionToType(OldVal, NewTy, VMC, TD);
       NewPN->addIncoming(V, BB);
@@ -1200,7 +1206,7 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
 
     } else {                   // Changing an argument, must be in vararg area
       std::vector<Value*>::iterator OI =
-        find(Params.begin(), Params.end(), OldVal);
+        std::find(Params.begin(), Params.end(), OldVal);
       assert (OI != Params.end() && "Not using value!");
 
       *OI = NewVal;
@@ -1221,9 +1227,9 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
   assert(It != BB->end() && "Instruction not in own basic block??");
   BB->getInstList().insert(It, Res);   // Keep It pointing to old instruction
 
-  DEBUG(std::cerr << "COT CREATED: "  << (void*)Res << " " << Res
-                  << "In: " << (void*)I << " " << I << "Out: " << (void*)Res
-                  << " " << Res);
+  DEBUG(std::cerr << "COT CREATED: "  << (void*)Res << " " << *Res
+                  << "In: " << (void*)I << " " << *I << "Out: " << (void*)Res
+                  << " " << *Res);
 
   // Add the instruction to the expression map
   VMC.ExprMap[I] = Res;