Fix big bug introduced with symbol table changes
[oota-llvm.git] / lib / Transforms / ExprTypeConvert.cpp
index f03253fac8d1bb3fed880ec58b08e8bc5156ddfd..85f9bb0714cf09b8d657fbe17f80c73bcf329fb6 100644 (file)
@@ -139,22 +139,18 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty,
   ValueTypeCache::iterator CTMI = CTMap.find(V);
   if (CTMI != CTMap.end()) return CTMI->second == Ty;
 
+  // If it's a constant... all constants can be converted to a different type We
+  // just ask the constant propogator to see if it can convert the value...
+  //
+  if (Constant *CPV = dyn_cast<Constant>(V))
+    return ConstantFoldCastInstruction(CPV, Ty);
+  
+
   CTMap[V] = Ty;
   if (V->getType() == Ty) return true;  // Expression already correct type!
 
   Instruction *I = dyn_cast<Instruction>(V);
-  if (I == 0) {
-    // It's not an instruction, check to see if it's a constant... all constants
-    // can be converted to an equivalent value (except pointers, they can't be
-    // const prop'd in general).  We just ask the constant propogator to see if
-    // it can convert the value...
-    //
-    if (Constant *CPV = dyn_cast<Constant>(V))
-      if (ConstantFoldCastInstruction(CPV, Ty))
-        return true;  // Don't worry about deallocating, it's a constant.
-
-    return false;              // Otherwise, we can't convert!
-  }
+  if (I == 0) return false;              // Otherwise, we can't convert!
 
   switch (I->getOpcode()) {
   case Instruction::Cast:
@@ -289,6 +285,24 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty,
     return false;   // No match, maybe next time.
   }
 
+  case Instruction::Call: {
+    if (isa<Function>(I->getOperand(0)))
+      return false;  // Don't even try to change direct calls.
+
+    // If this is a function pointer, we can convert the return type if we can
+    // convert the source function pointer.
+    //
+    const PointerType *PT = cast<PointerType>(I->getOperand(0)->getType());
+    const FunctionType *FT = cast<FunctionType>(PT->getElementType());
+    std::vector<const Type *> ArgTys(FT->getParamTypes().begin(),
+                                     FT->getParamTypes().end());
+    const FunctionType *NewTy =
+      FunctionType::get(Ty, ArgTys, FT->isVarArg());
+    if (!ExpressionConvertableToType(I->getOperand(0),
+                                     PointerType::get(NewTy), CTMap))
+      return false;
+    break;
+  }
   default:
     return false;
   }
@@ -323,18 +337,18 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) {
   DEBUG(cerr << "CETT: " << (void*)V << " " << V);
 
   Instruction *I = dyn_cast<Instruction>(V);
-  if (I == 0)
-    if (Constant *CPV = cast<Constant>(V)) {
-      // Constants are converted by constant folding the cast that is required.
-      // We assume here that all casts are implemented for constant prop.
-      Value *Result = ConstantFoldCastInstruction(CPV, Ty);
-      assert(Result && "ConstantFoldCastInstruction Failed!!!");
-      assert(Result->getType() == Ty && "Const prop of cast failed!");
-
-      // Add the instruction to the expression map
-      VMC.ExprMap[V] = Result;
-      return Result;
-    }
+  if (I == 0) {
+    Constant *CPV = cast<Constant>(V);
+    // Constants are converted by constant folding the cast that is required.
+    // We assume here that all casts are implemented for constant prop.
+    Value *Result = ConstantFoldCastInstruction(CPV, Ty);
+    assert(Result && "ConstantFoldCastInstruction Failed!!!");
+    assert(Result->getType() == Ty && "Const prop of cast failed!");
+
+    // Add the instruction to the expression map
+    //VMC.ExprMap[V] = Result;
+    return Result;
+  }
 
 
   BasicBlock *BB = I->getParent();
@@ -481,9 +495,30 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) {
 
 
     assert(Res && "Didn't find match!");
-    break;   // No match, maybe next time.
+    break;
   }
 
+  case Instruction::Call: {
+    assert(!isa<Function>(I->getOperand(0)));
+
+    // If this is a function pointer, we can convert the return type if we can
+    // convert the source function pointer.
+    //
+    const PointerType *PT = cast<PointerType>(I->getOperand(0)->getType());
+    const FunctionType *FT = cast<FunctionType>(PT->getElementType());
+    std::vector<const Type *> ArgTys(FT->getParamTypes().begin(),
+                                     FT->getParamTypes().end());
+    const FunctionType *NewTy =
+      FunctionType::get(Ty, ArgTys, FT->isVarArg());
+    const PointerType *NewPTy = PointerType::get(NewTy);
+
+    Res = new CallInst(Constant::getNullValue(NewPTy),
+                       std::vector<Value*>(I->op_begin()+1, I->op_end()),
+                       Name);
+    VMC.ExprMap[I] = Res;
+    Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), NewPTy, VMC));
+    break;
+  }
   default:
     assert(0 && "Expression convertable, but don't know how to convert?");
     return 0;
@@ -782,8 +817,12 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
     if (OpNum == 0) {
       const PointerType *PTy = dyn_cast<PointerType>(Ty);
       if (PTy == 0) return false;  // Can't convert to a non-pointer type...
-      const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
-      if (MTy == 0) return false;  // Can't convert to a non ptr to function...
+      const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
+      if (FTy == 0) return false;  // Can't convert to a non ptr to function...
+
+      // Do not allow converting to a call where all of the operands are ...'s
+      if (FTy->getNumParams() == 0 && FTy->isVarArg())
+        return false;              // Do not permit this conversion!
 
       // Perform sanity checks to make sure that new function type has the
       // correct number of arguments...
@@ -793,12 +832,12 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
       // Cannot convert to a type that requires more fixed arguments than
       // the call provides...
       //
-      if (NumArgs < MTy->getParamTypes().size()) return false;
+      if (NumArgs < FTy->getNumParams()) return false;
       
       // Unless this is a vararg function type, we cannot provide more arguments
       // than are desired...
       //
-      if (!MTy->isVarArg() && NumArgs > MTy->getParamTypes().size())
+      if (!FTy->isVarArg() && NumArgs > FTy->getNumParams())
         return false;
 
       // Okay, at this point, we know that the call and the function type match
@@ -808,7 +847,7 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
       // reason for this is that we prefer to have resolved functions but casted
       // arguments if possible.
       //
-      const FunctionType::ParamTypes &PTs = MTy->getParamTypes();
+      const FunctionType::ParamTypes &PTs = FTy->getParamTypes();
       for (unsigned i = 0, NA = PTs.size(); i < NA; ++i)
         if (!PTs[i]->isLosslesslyConvertableTo(I->getOperand(i+1)->getType()))
           return false;   // Operands must have compatible types!
@@ -817,14 +856,14 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
       // converted.  We succeed if we can change the return type if
       // neccesary...
       //
-      return ValueConvertableToType(I, MTy->getReturnType(), CTMap);
+      return ValueConvertableToType(I, FTy->getReturnType(), CTMap);
     }
     
     const PointerType *MPtr = cast<PointerType>(I->getOperand(0)->getType());
-    const FunctionType *MTy = cast<FunctionType>(MPtr->getElementType());
-    if (!MTy->isVarArg()) return false;
+    const FunctionType *FTy = cast<FunctionType>(MPtr->getElementType());
+    if (!FTy->isVarArg()) return false;
 
-    if ((OpNum-1) < MTy->getParamTypes().size())
+    if ((OpNum-1) < FTy->getParamTypes().size())
       return false;  // It's not in the varargs section...
 
     // If we get this far, we know the value is in the varargs section of the
@@ -1130,7 +1169,8 @@ 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);
+          Params[i] = new CastInst(Params[i], PTs[i],  "callarg.cast." +
+                                   Params[i]->getName(), It);
         }
       Meth = NewVal;  // Update call destination to new value