fix grammaro's pointed out by daniel
[oota-llvm.git] / lib / Transforms / IPO / PartialSpecialization.cpp
index da127cb9f5f4b9e52b81f3997f66de7b7a11318a..084b94e53566132bcfab5a3255c4abc9654b3fb4 100644 (file)
@@ -27,7 +27,6 @@
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Transforms/Utils/Cloning.h"
 #include "llvm/Support/CallSite.h"
-#include "llvm/Support/Compiler.h"
 #include "llvm/ADT/DenseSet.h"
 #include <map>
 using namespace llvm;
@@ -41,7 +40,7 @@ static const int CallsMin = 5;
 static const double ConstValPercent = .1;
 
 namespace {
-  class VISIBILITY_HIDDEN PartSpec : public ModulePass {
+  class PartSpec : public ModulePass {
     void scanForInterest(Function&, SmallVector<int, 6>&);
     int scanDistribution(Function&, int, std::map<Constant*, int>&);
   public :
@@ -65,7 +64,7 @@ SpecializeFunction(Function* F,
   DenseSet<unsigned> deleted;
   for (DenseMap<const Value*, Value*>::iterator 
          repb = replacements.begin(), repe = replacements.end();
-       repb != repe; ++ repb)
+       repb != repe; ++repb)
     deleted.insert(cast<Argument>(repb->first)->getArgNo());
 
   Function* NF = CloneFunction(F, replacements);
@@ -74,7 +73,7 @@ SpecializeFunction(Function* F,
 
   for (Value::use_iterator ii = F->use_begin(), ee = F->use_end(); 
        ii != ee; ) {
-    Value::use_iterator i = ii;;
+    Value::use_iterator i = ii;
     ++ii;
     if (isa<CallInst>(i) || isa<InvokeInst>(i)) {
       CallSite CS(cast<Instruction>(i));
@@ -85,16 +84,19 @@ SpecializeFunction(Function* F,
           if (!deleted.count(x))
             args.push_back(CS.getArgument(x));
         Value* NCall;
-        if (isa<CallInst>(i))
+        if (CallInst *CI = dyn_cast<CallInst>(i)) {
           NCall = CallInst::Create(NF, args.begin(), args.end(), 
-                                   CS.getInstruction()->getName(), 
-                                   CS.getInstruction());
-        else
-          NCall = InvokeInst::Create(NF, cast<InvokeInst>(i)->getNormalDest(),
-                                     cast<InvokeInst>(i)->getUnwindDest(),
+                                   CI->getName(), CI);
+          cast<CallInst>(NCall)->setTailCall(CI->isTailCall());
+          cast<CallInst>(NCall)->setCallingConv(CI->getCallingConv());
+        } else {
+          InvokeInst *II = cast<InvokeInst>(i);
+          NCall = InvokeInst::Create(NF, II->getNormalDest(),
+                                     II->getUnwindDest(),
                                      args.begin(), args.end(), 
-                                     CS.getInstruction()->getName(), 
-                                     CS.getInstruction());
+                                     II->getName(), II);
+          cast<InvokeInst>(NCall)->setCallingConv(II->getCallingConv());
+        }
         CS.getInstruction()->replaceAllUsesWith(NCall);
         CS.getInstruction()->eraseFromParent();
       }
@@ -108,7 +110,7 @@ bool PartSpec::runOnModule(Module &M) {
   bool Changed = false;
   for (Module::iterator I = M.begin(); I != M.end(); ++I) {
     Function &F = *I;
-    if (F.isDeclaration()) continue;
+    if (F.isDeclaration() || F.mayBeOverridden()) continue;
     SmallVector<int, 6> interestingArgs;
     scanForInterest(F, interestingArgs);
 
@@ -165,14 +167,16 @@ void PartSpec::scanForInterest(Function& F, SmallVector<int, 6>& args) {
   }
 }
 
+/// scanDistribution - Construct a histogram of constants for arg of F at arg.
 int PartSpec::scanDistribution(Function& F, int arg, 
                                std::map<Constant*, int>& dist) {
   bool hasIndirect = false;
   int total = 0;
   for(Value::use_iterator ii = F.use_begin(), ee = F.use_end();
       ii != ee; ++ii)
-    if (CallInst* CI = dyn_cast<CallInst>(ii)) {
-      ++dist[dyn_cast<Constant>(CI->getOperand(arg + 1))];
+    if ((isa<CallInst>(ii) || isa<InvokeInst>(ii))
+        && ii->getOperand(0) == &F) {
+      ++dist[dyn_cast<Constant>(ii->getOperand(arg + 1))];
       ++total;
     } else
       hasIndirect = true;