<commit_before>#include "llvm/Pass.h"
#include "llvm/Function.h"
#include "llvm/Module.h"
#include "llvm/Instructions.h"
#include "llvm/Constants.h"
#include "llvm/GlobalVariable.h"
#include "llvm/Support/IRBuilder.h"
#include "llvm/Analysis/LoopInfo.h"
#include <string>

using namespace llvm;
using std::string;
using std::pair;

namespace 
{
  class ClassLookupCachePass : public FunctionPass 
  {
    Module *M;
    typedef std::pair<CallInst*,std::string> ClassLookup;

    public:
    static char ID;
    ClassLookupCachePass() : FunctionPass((intptr_t)&ID) {}

    virtual bool doInitialization(Module &Mod) {
      M = &Mod;
      return false;  
    }

    virtual bool runOnFunction(Function &F) {
      bool modified = false;
      SmallVector<ClassLookup, 16> Lookups;
      BasicBlock *entry = &F.getEntryBlock();

      for (Function::iterator i=F.begin(), end=F.end() ;
          i != end ; ++i) {
        for (BasicBlock::iterator b=i->begin(), last=i->end() ;
            b != last ; ++b) {
          if (CallInst *call = dyn_cast<CallInst>(b)) {
            Value *callee = call->getCalledValue()->stripPointerCasts();
            if (Function *func = dyn_cast<Function>(callee)) {
              if (func->getName() == "objc_lookup_class") {
                ClassLookup lookup;
                GlobalVariable *classNameVar = dyn_cast<GlobalVariable>(
                    call->getOperand(1)->stripPointerCasts());
                if (0 == classNameVar) { continue; }
                ConstantArray *init = dyn_cast<ConstantArray>(
                    classNameVar->getInitializer());
                if (0 == init || !init->isCString()) { continue; }
                lookup.first = call;
                lookup.second = init->getAsString();
                modified = true;
                Lookups.push_back(lookup);
              }
            }
          }
        }
      }
      IRBuilder<> B = IRBuilder<>(entry);
      for (SmallVectorImpl<ClassLookup>::iterator i=Lookups.begin(), 
          e=Lookups.end() ; e!=i ; i++) {
        Value *global = M->getGlobalVariable(("_OBJC_CLASS_" + i->second).c_str(), true);
        fprintf(stderr, "%s\n", ("_OBJC_CLASS_" + i->second).c_str());
        if (global) {
          Value *cls = new BitCastInst(global, i->first->getType(), "class", i->first);
          i->first->replaceAllUsesWith(cls);
          i->first->removeFromParent();
        }
      }
      return modified;
    }
  };

  char ClassLookupCachePass::ID = 0;
  RegisterPass<ClassLookupCachePass> X("gnu-class-lookup-cache", 
          "Cache class lookups");
}

FunctionPass *createClassLookupCachePass(void)
{
  return new ClassLookupCachePass();
}<commit_msg>Removed debugging code that shouldn't have been in last commit.<commit_after>#include "llvm/Pass.h"
#include "llvm/Function.h"
#include "llvm/Module.h"
#include "llvm/Instructions.h"
#include "llvm/Constants.h"
#include "llvm/GlobalVariable.h"
#include "llvm/Support/IRBuilder.h"
#include "llvm/Analysis/LoopInfo.h"
#include <string>

using namespace llvm;
using std::string;
using std::pair;

namespace 
{
  class ClassLookupCachePass : public FunctionPass 
  {
    Module *M;
    typedef std::pair<CallInst*,std::string> ClassLookup;

    public:
    static char ID;
    ClassLookupCachePass() : FunctionPass((intptr_t)&ID) {}

    virtual bool doInitialization(Module &Mod) {
      M = &Mod;
      return false;  
    }

    virtual bool runOnFunction(Function &F) {
      bool modified = false;
      SmallVector<ClassLookup, 16> Lookups;
      BasicBlock *entry = &F.getEntryBlock();

      for (Function::iterator i=F.begin(), end=F.end() ;
          i != end ; ++i) {
        for (BasicBlock::iterator b=i->begin(), last=i->end() ;
            b != last ; ++b) {
          if (CallInst *call = dyn_cast<CallInst>(b)) {
            Value *callee = call->getCalledValue()->stripPointerCasts();
            if (Function *func = dyn_cast<Function>(callee)) {
              if (func->getName() == "objc_lookup_class") {
                ClassLookup lookup;
                GlobalVariable *classNameVar = dyn_cast<GlobalVariable>(
                    call->getOperand(1)->stripPointerCasts());
                if (0 == classNameVar) { continue; }
                ConstantArray *init = dyn_cast<ConstantArray>(
                    classNameVar->getInitializer());
                if (0 == init || !init->isCString()) { continue; }
                lookup.first = call;
                lookup.second = init->getAsString();
                modified = true;
                Lookups.push_back(lookup);
              }
            }
          }
        }
      }
      IRBuilder<> B = IRBuilder<>(entry);
      for (SmallVectorImpl<ClassLookup>::iterator i=Lookups.begin(), 
          e=Lookups.end() ; e!=i ; i++) {
        Value *global = M->getGlobalVariable(("_OBJC_CLASS_" + i->second).c_str(), true);
        if (global) {
          Value *cls = new BitCastInst(global, i->first->getType(), "class", i->first);
          i->first->replaceAllUsesWith(cls);
          i->first->removeFromParent();
        }
      }
      return modified;
    }
  };

  char ClassLookupCachePass::ID = 0;
  RegisterPass<ClassLookupCachePass> X("gnu-class-lookup-cache", 
          "Cache class lookups");
}

FunctionPass *createClassLookupCachePass(void)
{
  return new ClassLookupCachePass();
}
{'fraction_non_alphanumeric': 0.1428039471234407, 'fraction_numerical': 0.0022342208154905977, 'mean_word_length': 2.4238368387507965, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6242246', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1574, 'n_words': 325}