<commit_before>#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Pass.h"
#include "llvm/DataLayout.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/GlobalVariable.h"
#include "accept.h"
#include <fstream>
#include <string>

using namespace llvm;

namespace {
  struct AcceptAA : public ImmutablePass, public AliasAnalysis {
    static char ID;
    ACCEPTPass *transformPass;
    ApproxInfo *AI;
    int relaxId;
    int relaxParam;

    AcceptAA() : ImmutablePass(ID) {
      initializeAcceptAAPass(*PassRegistry::getPassRegistry());
      if (!sharedAcceptTransformPass) {
        errs() << "Alias analysis loaded without transform pass!\n";
        return;
      }
      transformPass = (ACCEPTPass*)sharedAcceptTransformPass;
      AI = transformPass->AI;
    }

    virtual const char *getPassName() const {
      return "ACCEPT approximate alias analysis";
    }

    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
      AliasAnalysis::getAnalysisUsage(AU);
      // dependencies?
    }

    virtual void initializePass() {
      InitializeAliasAnalysis(this);

      // One global optimization parameter controls whether we should enable
      // alias relaxation. (For now.)
      relaxId = transformPass->opportunityId;
      ++(transformPass->opportunityId);
      if (transformPass->relax) {
        relaxParam = transformPass->relaxConfig[relaxId];
      } else {
        relaxParam = 0;
        transformPass->relaxConfig[relaxId] = 0;
        transformPass->configDesc[relaxId] = "alias relaxation";
      }
    }

    virtual AliasResult alias(const Location &LocA, const Location &LocB) {
      if (!relaxParam)
        return MayAlias;

      // Mallocs, callocs...
      if (const Instruction *inst = dyn_cast<Instruction>(LocA.Ptr)) {
        if (const CallInst *ci = dyn_cast<CallInst>(inst)) {
          std::string type_str;
          llvm::raw_string_ostream rso(type_str);
          rso << *inst;
          if (rso.str().find("alloc") != std::string::npos) {
            for (Value::const_use_iterator ui = inst->use_begin();
                  ui != inst->use_end();
                  ++ui) {
              if (const Instruction *user = dyn_cast<Instruction>(*ui)) {
                if (const StoreInst *si = dyn_cast<StoreInst>(user)) {
                  if (isApproxPtr(si) || isApprox(si)) return NoAlias;
                } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(user)) {
                  if (isApproxPtr(user)) return NoAlias;
                } else if (const BitCastInst *bc = dyn_cast<BitCastInst>(user)) {
                  for (Value::const_use_iterator bc_ui = user->use_begin();
                        bc_ui != user->use_end();
                        ++bc_ui) {
                    if (const Instruction *bc_user = dyn_cast<Instruction>(*bc_ui))
                      if (const StoreInst *si_bc_user = dyn_cast<StoreInst>(bc_user))
                        if (isApproxPtr(si_bc_user) || isApprox(si_bc_user)) return NoAlias;
                  }
                }
              }
            }
          }
        }
      }
      if (const Instruction *inst = dyn_cast<Instruction>(LocB.Ptr)) {
        if (const CallInst *ci = dyn_cast<CallInst>(inst)) {
          std::string type_str;
          llvm::raw_string_ostream rso(type_str);
          rso << *inst;
          if (rso.str().find("alloc") != std::string::npos) {
            for (Value::const_use_iterator ui = inst->use_begin();
                  ui != inst->use_end();
                  ++ui) {
              if (const Instruction *user = dyn_cast<Instruction>(*ui)) {
                if (const StoreInst *si = dyn_cast<StoreInst>(user)) {
                  if (isApproxPtr(si) || isApprox(si)) return NoAlias;
                } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(user)) {
                  if (isApproxPtr(user)) return NoAlias;
                } else if (const BitCastInst *bc = dyn_cast<BitCastInst>(user)) {
                  for (Value::const_use_iterator bc_ui = user->use_begin();
                        bc_ui != user->use_end();
                        ++bc_ui) {
                    if (const Instruction *bc_user = dyn_cast<Instruction>(*bc_ui))
                      if (const StoreInst *si_bc_user = dyn_cast<StoreInst>(bc_user))
                        if (isApproxPtr(si_bc_user) || isApprox(si_bc_user)) return NoAlias;
                  }
                }
              }
            }
          }
        }
      }

      // Globals
      if (const GlobalValue *GV = dyn_cast<GlobalValue>(LocA.Ptr))
        if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
          if (isApproxPtr(V)) return NoAlias;
      if (const GlobalValue *GV = dyn_cast<GlobalValue>(LocB.Ptr))
        if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
          if (isApproxPtr(V)) return NoAlias;


      // Getelementptr
      if (const Instruction *inst = dyn_cast<Instruction>(LocA.Ptr))
        if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(inst))
          if (isApproxPtr(GEP)) return NoAlias;
      if (const Instruction *inst = dyn_cast<Instruction>(LocB.Ptr))
        if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(inst))
          if (isApproxPtr(GEP)) return NoAlias;

      // Bitcasts
      if (const Instruction *inst = dyn_cast<Instruction>(LocA.Ptr))
        if (const BitCastInst *BC = dyn_cast<BitCastInst>(inst)) {
          std::string type_str;
          llvm::raw_string_ostream rso(type_str);
          rso << *(inst->getOperand(0));
          if (rso.str().find("alloc") != std::string::npos) {
            for (Value::const_use_iterator ui = inst->use_begin();
                  ui != inst->use_end();
                  ++ui) {
              const Instruction *user = dyn_cast<Instruction>(*ui);
              if (const StoreInst *st = dyn_cast<StoreInst>(user))
                if (isApprox(st) || isApproxPtr(st)) return NoAlias;
            }
          }
        }
      if (const Instruction *inst = dyn_cast<Instruction>(LocB.Ptr))
        if (const BitCastInst *BC = dyn_cast<BitCastInst>(inst)) {
          std::string type_str;
          llvm::raw_string_ostream rso(type_str);
          rso << *(inst->getOperand(0));
          if (rso.str().find("alloc") != std::string::npos) {
            for (Value::const_use_iterator ui = inst->use_begin();
                  ui != inst->use_end();
                  ++ui) {
              const Instruction *user = dyn_cast<Instruction>(*ui);
              if (const StoreInst *st = dyn_cast<StoreInst>(user))
                if (isApprox(st) || isApproxPtr(st)) return NoAlias;
            }
          }
        }

      // Ordinary instructions
      if (const Instruction *inst = dyn_cast<Instruction>(LocA.Ptr))
        if (isApprox(inst) || isApproxPtr(inst)) return NoAlias;
      if (const Instruction *inst = dyn_cast<Instruction>(LocB.Ptr))
        if (isApprox(inst) || isApproxPtr(inst)) return NoAlias;

      /* DEBUG
      else if (instA && instB) {
        errs() << "instructions:\n";
        instA->dump();
        instB->dump();
      } else {
        const GlobalValue *gvA = dyn_cast<GlobalValue>(LocA.Ptr);
        const GlobalValue *gvB = dyn_cast<GlobalValue>(LocB.Ptr);
        if (gvA && gvB) {
          errs() << "global values:\n";
          gvA->dump();
          gvB->dump();
        }
      }
      */

      // Delegate to other alias analyses.<commit_msg>aa: helper function to check for mallocs<commit_after>#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Pass.h"
#include "llvm/DataLayout.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/GlobalVariable.h"
#include "accept.h"
#include <fstream>
#include <string>
#include <ctime>

using namespace llvm;

namespace {
  bool isMalloc(const Value *val) {
      if (const CallInst *call = dyn_cast<CallInst>(val)) {
          if (Function *func = call->getCalledFunction()) {
              return func->getName().find("alloc") != StringRef::npos;
          }
      }
      return false;
  }

  struct AcceptAA : public ImmutablePass, public AliasAnalysis {
    static char ID;
    ACCEPTPass *transformPass;
    ApproxInfo *AI;
    int relaxId;
    int relaxParam;

    AcceptAA() : ImmutablePass(ID) {
      initializeAcceptAAPass(*PassRegistry::getPassRegistry());
      if (!sharedAcceptTransformPass) {
        errs() << "Alias analysis loaded without transform pass!\n";
        return;
      }
      transformPass = (ACCEPTPass*)sharedAcceptTransformPass;
      AI = transformPass->AI;
    }

    virtual const char *getPassName() const {
      return "ACCEPT approximate alias analysis";
    }

    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
      AliasAnalysis::getAnalysisUsage(AU);
      // dependencies?
    }

    virtual void initializePass() {
      InitializeAliasAnalysis(this);

      // One global optimization parameter controls whether we should enable
      // alias relaxation. (For now.)
      relaxId = transformPass->opportunityId;
      ++(transformPass->opportunityId);
      if (transformPass->relax) {
        relaxParam = transformPass->relaxConfig[relaxId];
      } else {
        relaxParam = 0;
        transformPass->relaxConfig[relaxId] = 0;
        transformPass->configDesc[relaxId] = "alias relaxation";
      }
    }

    virtual AliasResult alias(const Location &LocA, const Location &LocB) {
      if (!relaxParam)
        return MayAlias;

      // Mallocs, callocs...
      if (const Instruction *inst = dyn_cast<Instruction>(LocA.Ptr)) {
        if (isMalloc(inst)) {
            for (Value::const_use_iterator ui = inst->use_begin();
                  ui != inst->use_end();
                  ++ui) {
              if (const Instruction *user = dyn_cast<Instruction>(*ui)) {
                if (const StoreInst *si = dyn_cast<StoreInst>(user)) {
                  if (isApproxPtr(si) || isApprox(si)) {
                      return NoAlias;
                  }
                } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(user)) {
                  if (isApproxPtr(user)) {
                      return NoAlias;
                  }
                } else if (const BitCastInst *bc = dyn_cast<BitCastInst>(user)) {
                  for (Value::const_use_iterator bc_ui = user->use_begin();
                        bc_ui != user->use_end();
                        ++bc_ui) {
                    if (const Instruction *bc_user = dyn_cast<Instruction>(*bc_ui))
                      if (const StoreInst *si_bc_user = dyn_cast<StoreInst>(bc_user))
                        if (isApproxPtr(si_bc_user) || isApprox(si_bc_user)) {
                            return NoAlias;
                        }
                  }
                }
              }
            }
        }
      }
      if (const Instruction *inst = dyn_cast<Instruction>(LocB.Ptr)) {
        if (isMalloc(inst)) {
            for (Value::const_use_iterator ui = inst->use_begin();
                  ui != inst->use_end();
                  ++ui) {
              if (const Instruction *user = dyn_cast<Instruction>(*ui)) {
                if (const StoreInst *si = dyn_cast<StoreInst>(user)) {
                  if (isApproxPtr(si) || isApprox(si)) {
                      return NoAlias;
                  }
                } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(user)) {
                  if (isApproxPtr(user)) {
                      return NoAlias;
                  }
                } else if (const BitCastInst *bc = dyn_cast<BitCastInst>(user)) {
                  for (Value::const_use_iterator bc_ui = user->use_begin();
                        bc_ui != user->use_end();
                        ++bc_ui) {
                    if (const Instruction *bc_user = dyn_cast<Instruction>(*bc_ui))
                      if (const StoreInst *si_bc_user = dyn_cast<StoreInst>(bc_user))
                        if (isApproxPtr(si_bc_user) || isApprox(si_bc_user)) {
                            return NoAlias;
                        }
                  }
                }
              }
            }
        }
      }

      // Globals
      if (const GlobalValue *GV = dyn_cast<GlobalValue>(LocA.Ptr))
        if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
          if (isApproxPtr(V)) {
              return NoAlias;
          }
      if (const GlobalValue *GV = dyn_cast<GlobalValue>(LocB.Ptr))
        if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
          if (isApproxPtr(V)) {
              return NoAlias;
          }

      // Getelementptr
      if (const Instruction *inst = dyn_cast<Instruction>(LocA.Ptr))
        if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(inst))
          if (isApproxPtr(GEP)) {
              return NoAlias;
          }
      if (const Instruction *inst = dyn_cast<Instruction>(LocB.Ptr))
        if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(inst))
          if (isApproxPtr(GEP)) {
              return NoAlias;
          }

      // Bitcasts
      if (const Instruction *inst = dyn_cast<Instruction>(LocA.Ptr))
        if (const BitCastInst *BC = dyn_cast<BitCastInst>(inst)) {
          if (isMalloc(inst->getOperand(0))) {
            for (Value::const_use_iterator ui = inst->use_begin();
                  ui != inst->use_end();
                  ++ui) {
              const Instruction *user = dyn_cast<Instruction>(*ui);
              if (const StoreInst *st = dyn_cast<StoreInst>(user))
                if (isApprox(st) || isApproxPtr(st)) {
                    return NoAlias;
                }
            }
          }
        }
      if (const Instruction *inst = dyn_cast<Instruction>(LocB.Ptr))
        if (const BitCastInst *BC = dyn_cast<BitCastInst>(inst)) {
          if (isMalloc(inst->getOperand(0))) {
            for (Value::const_use_iterator ui = inst->use_begin();
                  ui != inst->use_end();
                  ++ui) {
              const Instruction *user = dyn_cast<Instruction>(*ui);
              if (const StoreInst *st = dyn_cast<StoreInst>(user))
                if (isApprox(st) || isApproxPtr(st)) {
                    return NoAlias;
                }
            }
          }
        }

      // Ordinary instructions
      if (const Instruction *inst = dyn_cast<Instruction>(LocA.Ptr))
        if (isApprox(inst) || isApproxPtr(inst)) {
            return NoAlias;
        }
      if (const Instruction *inst = dyn_cast<Instruction>(LocB.Ptr))
        if (isApprox(inst) || isApproxPtr(inst)) {
            return NoAlias;
        } 

      /* DEBUG
      else if (instA && instB) {
        errs() << "instructions:\n";
        instA->dump();
        instB->dump();
      } else {
        const GlobalValue *gvA = dyn_cast<GlobalValue>(LocA.Ptr);
        const GlobalValue *gvB = dyn_cast<GlobalValue>(LocB.Ptr);
        if (gvA && gvB) {
          errs() << "global values:\n";
          gvA->dump();
          gvB->dump();
        }
      }
      */

      // Delegate to other alias analyses.
{'fraction_non_alphanumeric': 0.12143139060649914, 'fraction_numerical': 0.0005262465465070386, 'mean_word_length': 1.9259045419553502, 'pattern_counts': {'":': 0, '<': 99, '<?xml version=': 0, '>': 131, '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': '6913875', 'n_tokens_mistral': 4497, 'n_tokens_neox': 4345, 'n_words': 925}