[llvm-commits] [poolalloc] r131562 - in /poolalloc/trunk: include/assistDS/VarArgsFunc.h lib/AssistDS/ArgCast.cpp lib/AssistDS/VarArgsFunc.cpp

Arushi Aggarwal aggarwa4 at illinois.edu
Wed May 18 13:23:04 PDT 2011


Author: aggarwa4
Date: Wed May 18 15:23:03 2011
New Revision: 131562

URL: http://llvm.org/viewvc/llvm-project?rev=131562&view=rev
Log:
ArgCast supersedes VarArgsFunc. Also code cleanup.

Removed:
    poolalloc/trunk/include/assistDS/VarArgsFunc.h
    poolalloc/trunk/lib/AssistDS/VarArgsFunc.cpp
Modified:
    poolalloc/trunk/lib/AssistDS/ArgCast.cpp

Removed: poolalloc/trunk/include/assistDS/VarArgsFunc.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/assistDS/VarArgsFunc.h?rev=131561&view=auto
==============================================================================
--- poolalloc/trunk/include/assistDS/VarArgsFunc.h (original)
+++ poolalloc/trunk/include/assistDS/VarArgsFunc.h (removed)
@@ -1,31 +0,0 @@
-//===--- VarArgsFunc.cpp - Simplify calls to bitcasted const funcs --------===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-// Convert calls of type
-// call(bitcast F to (...)*) ()
-// to
-// call F()
-// if the number and types of arguments passed matches.
-//===----------------------------------------------------------------------===//
-
-#include "llvm/Instructions.h"
-#include "llvm/Module.h"
-#include "llvm/Pass.h"
-
-namespace llvm {
-  //
-  // Class: VarArgsFunc
-  //
-  class VarArgsFunc : public ModulePass {
-  public:
-    static char ID;
-    VarArgsFunc() : ModulePass(&ID) {}
-    virtual bool runOnModule(Module& M);
-  };
-}
-

Modified: poolalloc/trunk/lib/AssistDS/ArgCast.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/ArgCast.cpp?rev=131562&r1=131561&r2=131562&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/ArgCast.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/ArgCast.cpp Wed May 18 15:23:03 2011
@@ -52,42 +52,56 @@
 bool ArgCast::runOnModule(Module& M) {
 
   std::vector<CallInst*> worklist;
-  for (Module::iterator I = M.begin(); I != M.end(); ++I)
-    if (!I->isDeclaration() && !I->mayBeOverridden())
-      // Find all uses of this function
-      for(Value::use_iterator ui = I->use_begin(), ue = I->use_end(); ui != ue; ++ui)
-        // check if is ever casted to a different function type
-        if (Constant *C = dyn_cast<Constant>(ui)) 
-          if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) 
-            if (CE->getOpcode() == Instruction::BitCast) 
-              if(CE->getOperand(0) == I) 
-                if(const FunctionType *FTy  = dyn_cast<FunctionType>
-                   ((cast<PointerType>(CE->getType()))->getElementType())) {
-                  //casting to a varargs funtion
-                  if(FTy->isVarArg())
-                    for(Value::use_iterator uii = CE->use_begin(),
-                        uee = CE->use_end(); uii != uee; ++uii) {
-                      // Find all uses of the casted value, and check if it is 
-                      // used in a Call Instruction
-                      if (CallInst* CI = dyn_cast<CallInst>(uii)) {
-                        // Check that it is the called value, and not an argument
-                        if(CI->getCalledValue() != CE) 
-                          continue;
-                        // Check that the number of arguments passed, and expected
-                        // by the function are the same.
-                        if(CI->getNumOperands() != I->arg_size() + 1)
-                          continue;
-                        // Check that the return type of the function matches that
-                        // expected by the call inst(ensures that the reason for the
-                        // cast is not the return type).
-                        if(CI->getType() != I->getReturnType())
-                          continue;
-
-                        // If so, add to worklist
-                        worklist.push_back(CI);
-                      }
-                    }
-                }
+  for (Module::iterator I = M.begin(); I != M.end(); ++I) {
+    if (I->isDeclaration() || I->mayBeOverridden())
+      continue;
+    // Find all uses of this function
+    for(Value::use_iterator ui = I->use_begin(), ue = I->use_end(); ui != ue; ) {
+      // check if is ever casted to a different function type
+      ConstantExpr *CE = dyn_cast<ConstantExpr>(ui++);
+      if(!CE)
+        continue;
+      if (CE->getOpcode() != Instruction::BitCast) 
+        continue;
+      if(CE->getOperand(0) != I) 
+        continue;
+      const PointerType *PTy = dyn_cast<PointerType>(CE->getType());
+      if (!PTy)
+        continue;
+      const Type *ETy = PTy->getElementType();
+      const FunctionType *FTy  = dyn_cast<FunctionType>(ETy); 
+      if(!FTy)
+        continue;
+      // casting to a varargs funtion
+      // or function with same number of arguments
+      // possibly varying types of arguments
+      if(FTy->getNumParams() != I->arg_size() && !FTy->isVarArg())
+        continue;
+      for(Value::use_iterator uii = CE->use_begin(),
+          uee = CE->use_end(); uii != uee; ++uii) {
+        // Find all uses of the casted value, and check if it is 
+        // used in a Call Instruction
+        if (CallInst* CI = dyn_cast<CallInst>(uii)) {
+          // Check that it is the called value, and not an argument
+          if(CI->getCalledValue() != CE) 
+            continue;
+          // Check that the number of arguments passed, and expected
+          // by the function are the same.
+          if(CI->getNumOperands() != I->arg_size() + 1)
+            continue;
+          // Check that the return type of the function matches that
+          // expected by the call inst(ensures that the reason for the
+          // cast is not the return type).
+          if(CI->getType() != I->getReturnType()) {
+            if(CI->getNumUses() != 0)
+              continue;
+          }
+          // If so, add to worklist
+          worklist.push_back(CI);
+        }
+      }
+    }
+  }
 
   // Proces the worklist of potential call sites to transform
   while(!worklist.empty()) {
@@ -154,7 +168,17 @@
     CallInst *CINew = CallInst::Create(F, Args.begin(), Args.end(), "", CI);
     CINew->setCallingConv(CI->getCallingConv());
     CINew->setAttributes(CI->getAttributes());
-    CI->replaceAllUsesWith(CINew);
+    if(!CI->use_empty())
+      CI->replaceAllUsesWith(CINew);
+
+    // Debug printing
+    DEBUG(errs() << "ARGCAST:");
+    DEBUG(errs() << "ERASE:");
+    DEBUG(CI->dump());
+    DEBUG(errs() << "ARGCAST:");
+    DEBUG(errs() << "ADDED:");
+    DEBUG(CINew->dump());
+
     CI->eraseFromParent();
     numChanged++;
   }

Removed: poolalloc/trunk/lib/AssistDS/VarArgsFunc.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/VarArgsFunc.cpp?rev=131561&view=auto
==============================================================================
--- poolalloc/trunk/lib/AssistDS/VarArgsFunc.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/VarArgsFunc.cpp (removed)
@@ -1,132 +0,0 @@
-//===-- VarArgsFunc.cpp - Simplify calls to bitcasted const funcs --------===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-// Convert calls of type 
-// call(bitcast F to (...)*) () 
-// to 
-// call F()
-// if the number and types of arguments passed matches.
-//===----------------------------------------------------------------------===//
-#define DEBUG_TYPE "varargfunc"
-
-#include "assistDS/VarArgsFunc.h"
-#include "llvm/Transforms/Utils/Cloning.h"
-#include "llvm/ADT/Statistic.h"
-#include "llvm/Support/FormattedStream.h"
-#include "llvm/Support/Debug.h"
-
-#include <set>
-#include <map>
-#include <vector>
-
-using namespace llvm;
-
-// Pass statistics
-STATISTIC(numSimplified, "Number of Calls Simplified");
-
-//
-// Method: runOnModule()
-// Description:
-//  Entry point for this LLVM pass. Search for functions that are
-//  unnecessarily casted to varargs type, in a CallInst.
-//  Replace with direct calls to the function
-//
-// Inputs:
-// M - A reference to the LLVM module to transform.
-//
-// Outputs:
-// M - The transformed LLVM module.
-//
-// Return value:
-//  true  - The module was modified.
-//  false - The module was not modified.
-//
-bool VarArgsFunc::runOnModule(Module& M) {
-  std::vector<CallInst*> worklist;
-
-  for (Module::iterator I = M.begin(); I != M.end(); ++I) {
-    // Go through all the functions
-    if (I->mayBeOverridden()) 
-      continue;
-    //Uses of Function I
-    for(Value::use_iterator ui = I->use_begin(), ue = I->use_end();
-        ui != ue; ++ui) 
-      //Find all casted uses of the function
-      if (Constant *C = dyn_cast<Constant>(ui)) 
-        if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) 
-          if (CE->getOpcode() == Instruction::BitCast) 
-            if(CE->getOperand(0) == I) 
-              if(const FunctionType *FTy  = dyn_cast<FunctionType>
-                 ((cast<PointerType>(CE->getType()))->getElementType())) 
-                //casting to a varargs funtion
-                if(FTy->isVarArg()) { 
-                  // Check if bitcasted Value is used in a callInst
-                  for(Value::use_iterator uii = CE->use_begin(),
-                      uee = CE->use_end(); uii != uee; ++uii) 
-                    if (CallInst* CI = dyn_cast<CallInst>(uii)) 
-                      if(CI->getCalledValue() == CE) { 
-                        // add to a worklist to process
-                        worklist.push_back(CI);
-                      }
-                }
-  }
-
-  // process the worklist
-  while(!worklist.empty()) {
-    CallInst *CI = worklist.back();
-    worklist.pop_back();
-    Function *F = cast<Function>(CI->getCalledValue()->stripPointerCasts());
-    // Only continue, if we are passing the exact number of arguments
-    if(F->arg_size() != (CI->getNumOperands()-1))
-      continue;
-    // Only continue if we are getting the same return type value
-    // Or we can discard the returned value.
-    if(F->getReturnType() != CI->getType()) {
-      if(!CI->use_empty())
-        continue;
-    }
-    // Check if the parameters passed match the expected types of the 
-    // formal arguments
-    bool change = true;
-    unsigned arg_count = 1;
-    for (Function::arg_iterator ii = F->arg_begin(), ee = F->arg_end();ii != ee; ++ii,arg_count++) {
-      if(ii->getType() != CI->getOperand(arg_count)->getType()) {
-        change = false;
-        break;
-      }
-    }
-
-    if(change) {
-      // if we want to ignore the returned value, create a new CallInst
-      SmallVector<Value*, 8> Args;
-      for(unsigned j =1;j<CI->getNumOperands();j++) {
-        Args.push_back(CI->getOperand(j));
-      }
-      CallInst *CINew = CallInst::Create(F, Args.begin(), Args.end(), "", CI);
-      if(F->getReturnType() == CI->getType()){ // else means no uses
-        CI->replaceAllUsesWith(CINew);
-      }
-      DEBUG(errs() << "VA:");
-      DEBUG(errs() << "ERASE:");
-      DEBUG(CI->dump());
-      DEBUG(errs() << "VA:");
-      DEBUG(errs() << "ADDED:");
-      DEBUG(CINew->dump());
-      CI->eraseFromParent();
-      numSimplified++;
-    }
-  }
-  return (numSimplified > 0 );
-}
-
-// Pass ID variable
-char VarArgsFunc::ID = 0;
-
-// Register the Pass
-static RegisterPass<VarArgsFunc>
-X("varargsfunc", "Optimize non-varargs to varargs function casts");





More information about the llvm-commits mailing list