<commit_before>//===- DCE.cpp - Code to perform dead code elimination --------------------===//
//
// This file implements dead code elimination and basic block merging.
//
// Specifically, this:
//   * removes definitions with no uses (including unused constants)
//   * removes basic blocks with no predecessors
//   * merges a basic block into its predecessor if there is only one and the
//     predecessor only has one successor.
//   * Eliminates PHI nodes for basic blocks with a single predecessor
//   * Eliminates a basic block that only contains an unconditional branch
//   * Eliminates method prototypes that are not referenced
//
// TODO: This should REALLY be worklist driven instead of iterative.  Right now,
// we scan linearly through values, removing unused ones as we go.  The problem
// is that this may cause other earlier values to become unused.  To make sure
// that we get them all, we iterate until things stop changing.  Instead, when 
// removing a value, recheck all of its operands to see if they are now unused.
// Piece of cake, and more efficient as well.  
//
// Note, this is not trivial, because we have to worry about invalidating 
// iterators.  :(
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Scalar/DCE.h"
#include "llvm/Module.h"
#include "llvm/GlobalVariable.h"
#include "llvm/Method.h"
#include "llvm/BasicBlock.h"
#include "llvm/iTerminators.h"
#include "llvm/iPHINode.h"
#include "llvm/Assembly/Writer.h"
#include "Support/STLExtras.h"
#include <algorithm>

// dceInstruction - Inspect the instruction at *BBI and figure out if it's
// [trivially] dead.  If so, remove the instruction and update the iterator
// to point to the instruction that immediately succeeded the original
// instruction.
//
bool DeadCodeElimination::dceInstruction(BasicBlock::InstListType &BBIL,
                                         BasicBlock::iterator &BBI) {
  // Look for un"used" definitions...
  if ((*BBI)->use_empty() && !(*BBI)->hasSideEffects() && 
      !isa<TerminatorInst>(*BBI)) {
    delete BBIL.remove(BBI);   // Bye bye
    return true;
  }
  return false;
}

static inline bool RemoveUnusedDefs(BasicBlock::InstListType &Vals) {
  bool Changed = false;
  for (BasicBlock::InstListType::iterator DI = Vals.begin(); 
       DI != Vals.end(); )
    if (DeadCodeElimination::dceInstruction(Vals, DI))
      Changed = true;
    else
      ++DI;
  return Changed;
}

// RemoveSingularPHIs - This removes PHI nodes from basic blocks that have only
// a single predecessor.  This means that the PHI node must only have a single
// RHS value and can be eliminated.
//
// This routine is very simple because we know that PHI nodes must be the first
// things in a basic block, if they are present.
//
static bool RemoveSingularPHIs(BasicBlock *BB) {
  BasicBlock::pred_iterator PI(BB->pred_begin());
  if (PI == BB->pred_end() || ++PI != BB->pred_end()) 
    return false;   // More than one predecessor...

  Instruction *I = BB->front();
  if (!isa<PHINode>(I)) return false;  // No PHI nodes

  //cerr << "Killing PHIs from " << BB;
  //cerr << "Pred #0 = " << *BB->pred_begin();<commit_msg>Implement new DeadInstElmination pass remove old comment<commit_after>//===- DCE.cpp - Code to perform dead code elimination --------------------===//
//
// This file implements dead code elimination and basic block merging.
//
// Specifically, this:
//   * removes definitions with no uses
//   * removes basic blocks with no predecessors
//   * merges a basic block into its predecessor if there is only one and the
//     predecessor only has one successor.
//   * Eliminates PHI nodes for basic blocks with a single predecessor
//   * Eliminates a basic block that only contains an unconditional branch
//   * Eliminates method prototypes that are not referenced
//
// TODO: This should REALLY be worklist driven instead of iterative.  Right now,
// we scan linearly through values, removing unused ones as we go.  The problem
// is that this may cause other earlier values to become unused.  To make sure
// that we get them all, we iterate until things stop changing.  Instead, when 
// removing a value, recheck all of its operands to see if they are now unused.
// Piece of cake, and more efficient as well.  
//
// Note, this is not trivial, because we have to worry about invalidating 
// iterators.  :(
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Scalar/DCE.h"
#include "llvm/Module.h"
#include "llvm/GlobalVariable.h"
#include "llvm/Method.h"
#include "llvm/BasicBlock.h"
#include "llvm/iTerminators.h"
#include "llvm/iPHINode.h"
#include "llvm/Assembly/Writer.h"
#include "Support/STLExtras.h"
#include <algorithm>

// dceInstruction - Inspect the instruction at *BBI and figure out if it's
// [trivially] dead.  If so, remove the instruction and update the iterator
// to point to the instruction that immediately succeeded the original
// instruction.
//
bool DeadCodeElimination::dceInstruction(BasicBlock::InstListType &BBIL,
                                         BasicBlock::iterator &BBI) {
  // Look for un"used" definitions...
  if ((*BBI)->use_empty() && !(*BBI)->hasSideEffects() && 
      !isa<TerminatorInst>(*BBI)) {
    delete BBIL.remove(BBI);   // Bye bye
    return true;
  }
  return false;
}

static inline bool RemoveUnusedDefs(BasicBlock::InstListType &Vals) {
  bool Changed = false;
  for (BasicBlock::InstListType::iterator DI = Vals.begin(); 
       DI != Vals.end(); )
    if (DeadCodeElimination::dceInstruction(Vals, DI))
      Changed = true;
    else
      ++DI;
  return Changed;
}

bool DeadInstElimination::runOnBasicBlock(BasicBlock *BB) {
  return RemoveUnusedDefs(BB->getInstList());
}

// RemoveSingularPHIs - This removes PHI nodes from basic blocks that have only
// a single predecessor.  This means that the PHI node must only have a single
// RHS value and can be eliminated.
//
// This routine is very simple because we know that PHI nodes must be the first
// things in a basic block, if they are present.
//
static bool RemoveSingularPHIs(BasicBlock *BB) {
  BasicBlock::pred_iterator PI(BB->pred_begin());
  if (PI == BB->pred_end() || ++PI != BB->pred_end()) 
    return false;   // More than one predecessor...

  Instruction *I = BB->front();
  if (!isa<PHINode>(I)) return false;  // No PHI nodes

  //cerr << "Killing PHIs from " << BB;
  //cerr << "Pred #0 = " << *BB->pred_begin();
{'fraction_non_alphanumeric': 0.1409921671018277, 'fraction_numerical': 0.0003071724773460298, 'mean_word_length': 2.9490600363856885, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5190579', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1849, 'n_words': 719}