<commit_before>//===- InstructionCombining.cpp - Combine multiple instructions -------------=//
//
// InstructionCombining - Combine instructions to form fewer, simple
//   instructions.  This pass does not modify the CFG, and has a tendancy to
//   make instructions dead, so a subsequent DIE pass is useful.  This pass is
//   where algebraic simplification happens.
//
// This pass combines things like:
//    %Y = add int 1, %X
//    %Z = add int 1, %Y
// into:
//    %Z = add int 2, %X
//
// This is a simple worklist driven algorithm.
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Scalar/InstructionCombining.h"
#include "llvm/ConstantHandling.h"
#include "llvm/iMemory.h"
#include "llvm/iOther.h"
#include "llvm/iOperators.h"
#include "llvm/Pass.h"
#include "llvm/Support/InstIterator.h"
#include "llvm/Support/InstVisitor.h"
#include "../TransformInternals.h"


namespace {
  class InstCombiner : public FunctionPass,
                       public InstVisitor<InstCombiner, Instruction*> {
    // Worklist of all of the instructions that need to be simplified.
    std::vector<Instruction*> WorkList;

    void AddUsesToWorkList(Instruction *I) {
      // The instruction was simplified, add all users of the instruction to
      // the work lists because they might get more simplified now...
      //
      for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
           UI != UE; ++UI)
        WorkList.push_back(cast<Instruction>(*UI));
    }

  public:
    const char *getPassName() const { return "Instruction Combining"; }

    virtual bool runOnFunction(Function *F);

    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
      AU.preservesCFG();
    }

    // Visitation implementation - Implement instruction combining for different
    // instruction types.  The semantics are as follows:
    // Return Value:
    //    null        - No change was made
    //     I          - Change was made, I is still valid
    //   otherwise    - Change was made, replace I with returned instruction
    //   

    Instruction *visitAdd(BinaryOperator *I);
    Instruction *visitSub(BinaryOperator *I);
    Instruction *visitMul(BinaryOperator *I);
    Instruction *visitDiv(BinaryOperator *I);
    Instruction *visitRem(BinaryOperator *I);
    Instruction *visitAnd(BinaryOperator *I);
    Instruction *visitOr (BinaryOperator *I);
    Instruction *visitXor(BinaryOperator *I);
    Instruction *visitSetCondInst(BinaryOperator *I);
    Instruction *visitShiftInst(Instruction *I);
    Instruction *visitCastInst(CastInst *CI);
    Instruction *visitGetElementPtrInst(GetElementPtrInst *GEP);
    Instruction *visitMemAccessInst(MemAccessInst *MAI);

    // visitInstruction - Specify what to return for unhandled instructions...
    Instruction *visitInstruction(Instruction *I) { return 0; }
  };
}



// Make sure that this instruction has a constant on the right hand side if it
// has any constant arguments.  If not, fix it an return true.<commit_msg>Combine not (not X) -> X<commit_after>//===- InstructionCombining.cpp - Combine multiple instructions -------------=//
//
// InstructionCombining - Combine instructions to form fewer, simple
//   instructions.  This pass does not modify the CFG, and has a tendancy to
//   make instructions dead, so a subsequent DIE pass is useful.  This pass is
//   where algebraic simplification happens.
//
// This pass combines things like:
//    %Y = add int 1, %X
//    %Z = add int 1, %Y
// into:
//    %Z = add int 2, %X
//
// This is a simple worklist driven algorithm.
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Scalar/InstructionCombining.h"
#include "llvm/ConstantHandling.h"
#include "llvm/iMemory.h"
#include "llvm/iOther.h"
#include "llvm/iOperators.h"
#include "llvm/Pass.h"
#include "llvm/Support/InstIterator.h"
#include "llvm/Support/InstVisitor.h"
#include "../TransformInternals.h"


namespace {
  class InstCombiner : public FunctionPass,
                       public InstVisitor<InstCombiner, Instruction*> {
    // Worklist of all of the instructions that need to be simplified.
    std::vector<Instruction*> WorkList;

    void AddUsesToWorkList(Instruction *I) {
      // The instruction was simplified, add all users of the instruction to
      // the work lists because they might get more simplified now...
      //
      for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
           UI != UE; ++UI)
        WorkList.push_back(cast<Instruction>(*UI));
    }

  public:
    const char *getPassName() const { return "Instruction Combining"; }

    virtual bool runOnFunction(Function *F);

    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
      AU.preservesCFG();
    }

    // Visitation implementation - Implement instruction combining for different
    // instruction types.  The semantics are as follows:
    // Return Value:
    //    null        - No change was made
    //     I          - Change was made, I is still valid
    //   otherwise    - Change was made, replace I with returned instruction
    //   
    Instruction *visitNot(UnaryOperator *I);
    Instruction *visitAdd(BinaryOperator *I);
    Instruction *visitSub(BinaryOperator *I);
    Instruction *visitMul(BinaryOperator *I);
    Instruction *visitDiv(BinaryOperator *I);
    Instruction *visitRem(BinaryOperator *I);
    Instruction *visitAnd(BinaryOperator *I);
    Instruction *visitOr (BinaryOperator *I);
    Instruction *visitXor(BinaryOperator *I);
    Instruction *visitSetCondInst(BinaryOperator *I);
    Instruction *visitShiftInst(Instruction *I);
    Instruction *visitCastInst(CastInst *CI);
    Instruction *visitGetElementPtrInst(GetElementPtrInst *GEP);
    Instruction *visitMemAccessInst(MemAccessInst *MAI);

    // visitInstruction - Specify what to return for unhandled instructions...
    Instruction *visitInstruction(Instruction *I) { return 0; }
  };
}


Instruction *InstCombiner::visitNot(UnaryOperator *I) {
  if (I->use_empty()) return 0;       // Don't fix dead instructions...

  // not (not X) = X
  if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(0)))
    if (Op->getOpcode() == Instruction::Not) {
      AddUsesToWorkList(I);         // Add all modified instrs to worklist
      I->replaceAllUsesWith(Op->getOperand(0));
      return I;
    }
  return 0;
}


// Make sure that this instruction has a constant on the right hand side if it
// has any constant arguments.  If not, fix it an return true.
{'fraction_non_alphanumeric': 0.13399239543726235, 'fraction_numerical': 0.0018250950570342205, 'mean_word_length': 2.856891495601173, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 20, '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': '2550462', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1826, 'n_words': 596}