Replace inferred getCast(V,Ty) calls with more strict variants.
[oota-llvm.git] / lib / Transforms / IPO / FunctionResolution.cpp
index dba44a0dc15674318b86bf4db42d9b61f7de1e29..6f1eea0276730e0685b7446d92a16552abc30232 100644 (file)
@@ -1,10 +1,10 @@
 //===- FunctionResolution.cpp - Resolve declarations to implementations ---===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
 // This file was developed by the LLVM research group and is distributed under
 // the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+//
 //===----------------------------------------------------------------------===//
 //
 // Loop over the functions that are in the module and look for functions that
@@ -32,8 +32,8 @@
 using namespace llvm;
 
 namespace {
-  Statistic<>NumResolved("funcresolve", "Number of varargs functions resolved");
-  Statistic<> NumGlobals("funcresolve", "Number of global variables resolved");
+  Statistic NumResolved("funcresolve", "Number of varargs functions resolved");
+  Statistic NumGlobals("funcresolve", "Number of global variables resolved");
 
   struct FunctionResolvingPass : public ModulePass {
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
@@ -42,7 +42,7 @@ namespace {
 
     bool runOnModule(Module &M);
   };
-  RegisterOpt<FunctionResolvingPass> X("funcresolve", "Resolve Functions");
+  RegisterPass<FunctionResolvingPass> X("funcresolve", "Resolve Functions");
 }
 
 ModulePass *llvm::createFunctionResolvingPass() {
@@ -55,41 +55,43 @@ static bool ResolveFunctions(Module &M, std::vector<GlobalValue*> &Globals,
   for (unsigned i = 0; i != Globals.size(); ++i)
     if (Globals[i] != Concrete) {
       Function *Old = cast<Function>(Globals[i]);
-      const FunctionType *OldMT = Old->getFunctionType();
-      const FunctionType *ConcreteMT = Concrete->getFunctionType();
-      
-      if (OldMT->getNumParams() > ConcreteMT->getNumParams() &&
-          !ConcreteMT->isVarArg())
+      const FunctionType *OldFT = Old->getFunctionType();
+      const FunctionType *ConcreteFT = Concrete->getFunctionType();
+
+      if (OldFT->getNumParams() > ConcreteFT->getNumParams() &&
+          !ConcreteFT->isVarArg())
         if (!Old->use_empty()) {
-          std::cerr << "WARNING: Linking function '" << Old->getName()
-                    << "' is causing arguments to be dropped.\n";
-          std::cerr << "WARNING: Prototype: ";
-          WriteAsOperand(std::cerr, Old);
-          std::cerr << " resolved to ";
-          WriteAsOperand(std::cerr, Concrete);
-          std::cerr << "\n";
+          cerr << "WARNING: Linking function '" << Old->getName()
+               << "' is causing arguments to be dropped.\n";
+          cerr << "WARNING: Prototype: ";
+          WriteAsOperand(*cerr.stream(), Old);
+          cerr << " resolved to ";
+          WriteAsOperand(*cerr.stream(), Concrete);
+          cerr << "\n";
         }
-      
+
       // Check to make sure that if there are specified types, that they
       // match...
       //
-      unsigned NumArguments = std::min(OldMT->getNumParams(),
-                                       ConcreteMT->getNumParams());
+      unsigned NumArguments = std::min(OldFT->getNumParams(),
+                                       ConcreteFT->getNumParams());
 
       if (!Old->use_empty() && !Concrete->use_empty())
         for (unsigned i = 0; i < NumArguments; ++i)
-          if (OldMT->getParamType(i) != ConcreteMT->getParamType(i))
-            if (OldMT->getParamType(i)->getTypeID() != 
-                ConcreteMT->getParamType(i)->getTypeID()) {
-              std::cerr << "WARNING: Function [" << Old->getName()
-                        << "]: Parameter types conflict for: '";
-              WriteTypeSymbolic(std::cerr, OldMT, &M);
-              std::cerr << "' and '";
-              WriteTypeSymbolic(std::cerr, ConcreteMT, &M);
-              std::cerr << "'\n";
+          if (OldFT->getParamType(i) != ConcreteFT->getParamType(i))
+            if (OldFT->getParamType(i)->getTypeID() !=
+                ConcreteFT->getParamType(i)->getTypeID()) {
+              cerr << "WARNING: Function [" << Old->getName()
+                   << "]: Parameter types conflict for: '";
+              WriteTypeSymbolic(*cerr.stream(), OldFT, &M);
+              cerr << "' (in " 
+                   << Old->getParent()->getModuleIdentifier() << ") and '";
+              WriteTypeSymbolic(*cerr.stream(), ConcreteFT, &M);
+              cerr << "'(in " 
+                   << Concrete->getParent()->getModuleIdentifier() << ")\n";
               return Changed;
             }
-      
+
       // Attempt to convert all of the uses of the old function to the concrete
       // form of the function.  If there is a use of the fn that we don't
       // understand here we punt to avoid making a bad transformation.
@@ -101,7 +103,7 @@ static bool ResolveFunctions(Module &M, std::vector<GlobalValue*> &Globals,
       if (!Old->use_empty()) {
         Value *Replacement = Concrete;
         if (Concrete->getType() != Old->getType())
-          Replacement = ConstantExpr::getCast(Concrete, Old->getType());
+          Replacement = ConstantExpr::getBitCast(Concrete, Old->getType());
         NumResolved += Old->getNumUses();
         Old->replaceAllUsesWith(Replacement);
       }
@@ -120,7 +122,7 @@ static bool ResolveGlobalVariables(Module &M,
 
   for (unsigned i = 0; i != Globals.size(); ++i)
     if (Globals[i] != Concrete) {
-      Constant *Cast = ConstantExpr::getCast(Concrete, Globals[i]->getType());
+      Constant *Cast = ConstantExpr::getBitCast(Concrete,Globals[i]->getType());
       Globals[i]->replaceAllUsesWith(Cast);
 
       // Since there are no uses of Old anymore, remove it from the module.
@@ -161,8 +163,8 @@ static bool ProcessGlobalsWithSameName(Module &M, TargetData &TD,
 
   for (unsigned i = 0; i != Globals.size(); ) {
     if (isa<Function>(Globals[i]) != isFunction) {
-      std::cerr << "WARNING: Found function and global variable with the "
-                << "same name: '" << Globals[i]->getName() << "'.\n";
+      cerr << "WARNING: Found function and global variable with the "
+           << "same name: '" << Globals[i]->getName() << "'.\n";
       return false;                 // Don't know how to handle this, bail out!
     }
 
@@ -174,11 +176,11 @@ static bool ProcessGlobalsWithSameName(Module &M, TargetData &TD,
       if (!F->isExternal()) {
         if (Concrete && !Concrete->isExternal())
           return false;   // Found two different functions types.  Can't choose!
-        
+
         Concrete = Globals[i];
       } else if (Concrete) {
         if (Concrete->isExternal()) // If we have multiple external symbols...
-          if (F->getFunctionType()->getNumParams() > 
+          if (F->getFunctionType()->getNumParams() >
               cast<Function>(Concrete)->getFunctionType()->getNumParams())
             Concrete = F;  // We are more concrete than "Concrete"!
 
@@ -189,9 +191,9 @@ static bool ProcessGlobalsWithSameName(Module &M, TargetData &TD,
       GlobalVariable *GV = cast<GlobalVariable>(Globals[i]);
       if (!GV->isExternal()) {
         if (Concrete) {
-          std::cerr << "WARNING: Two global variables with external linkage"
-                    << " exist with the same name: '" << GV->getName()
-                    << "'!\n";
+          cerr << "WARNING: Two global variables with external linkage"
+               << " exist with the same name: '" << GV->getName()
+               << "'!\n";
           return false;
         }
         Concrete = GV;
@@ -213,7 +215,7 @@ static bool ProcessGlobalsWithSameName(Module &M, TargetData &TD,
       else if (!Globals[i]->hasInternalLinkage())
         NumInstancesWithExternalLinkage++;
     }
-    
+
     if (!HasExternal && NumInstancesWithExternalLinkage <= 1)
       return false;  // Nothing to do?  Must have multiple internal definitions.
 
@@ -231,7 +233,7 @@ static bool ProcessGlobalsWithSameName(Module &M, TargetData &TD,
               OtherF->getFunctionType()->isVarArg() &&
               OtherF->getFunctionType()->getNumParams() == 0)
             DontPrintWarning = true;
-      
+
       // Otherwise, if the non-concrete global is a global array variable with a
       // size of 0, and the concrete global is an array with a real size, don't
       // warn.  This occurs due to declaring 'extern int A[];'.
@@ -248,11 +250,11 @@ static bool ProcessGlobalsWithSameName(Module &M, TargetData &TD,
     }
 
     if (0 && !DontPrintWarning) {
-      std::cerr << "WARNING: Found global types that are not compatible:\n";
+      cerr << "WARNING: Found global types that are not compatible:\n";
       for (unsigned i = 0; i < Globals.size(); ++i) {
-        std::cerr << "\t";
-        WriteTypeSymbolic(std::cerr, Globals[i]->getType(), &M);
-        std::cerr << " %" << Globals[i]->getName() << "\n";
+        cerr << "\t";
+        WriteTypeSymbolic(*cerr.stream(), Globals[i]->getType(), &M);
+        cerr << " %" << Globals[i]->getName() << "\n";
       }
     }
 
@@ -311,7 +313,8 @@ bool FunctionResolvingPass::runOnModule(Module &M) {
       Globals[F->getName()].push_back(F);
   }
 
-  for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ) {
+  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
+       I != E; ) {
     GlobalVariable *GV = I++;
     if (GV->use_empty() && GV->isExternal()) {
       M.getGlobalList().erase(GV);
@@ -343,7 +346,8 @@ bool FunctionResolvingPass::runOnModule(Module &M) {
       ++I;
     }
 
-  for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; )
+  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
+       I != E; )
     if (I->isExternal() && I->use_empty()) {
       GlobalVariable *GV = I;
       ++I;