<commit_before>#include "astutil.h"
#include "bb.h"
#include "expr.h"
#include "optimizations.h"
#include "stmt.h"

//#define DEBUG_LIVE


//
// Given a function fn, computes:
//
//   locals: a vector of local variables
//   localMap: a map of local variables to indices
//   useSet: a set of SymExprs that are uses of local variables
//   defSet: a set of SymExprs that are defs of local variables
//   OUT: set of live variables at exit of each basic block
//
// if OUT(i)(j) is true then the jth local variable is live at the
// exit of basic block i
//
void
liveVariableAnalysis(FnSymbol* fn,
                     Vec<Symbol*>& locals,
                     Map<Symbol*,int>& localMap,
                     Vec<SymExpr*>& useSet,
                     Vec<SymExpr*>& defSet,
                     Vec<Vec<bool>*>& OUT) {
  buildLocalsVectorMap(fn, locals, localMap);

#ifdef DEBUG_LIVE
  printLocalsVector(locals, localMap);
#endif

  buildDefUseSets(fn, locals, useSet, defSet);

  //
  // USE(i): the set of variables that are used in basic block i
  // before they are defined (if at all) in the block
  // DEF(i): the set of variables that are defined in basic block i
  // before they are used (if at all) in the block
  // IN(i): the set of variables that are live at entry to basic
  // block i
  //
  Vec<Vec<bool>*> USE;
  Vec<Vec<bool>*> DEF;
  Vec<Vec<bool>*> IN;

  forv_Vec(BasicBlock, bb, *fn->basicBlocks) {
    Vec<bool>* use = new Vec<bool>();
    Vec<bool>* def = new Vec<bool>();
    Vec<bool>* lvin = new Vec<bool>();
    Vec<bool>* lvout = new Vec<bool>();
    forv_Vec(Symbol, local, locals) {
      use->add(false);
      def->add(false);
      lvin->add(false);
      lvout->add(false);
    }
    forv_Vec(Expr, expr, bb->exprs) {
      Vec<BaseAST*> asts;
      collect_asts(&asts, expr);
      forv_Vec(BaseAST, ast, asts) {
        if (SymExpr* se = dynamic_cast<SymExpr*>(ast)) {
          if (useSet.set_in(se)) {
            int id = localMap.get(se->var);
            if (!def->v[id])
              use->v[id] = true;
          }
          if (defSet.set_in(se)) {
            int id = localMap.get(se->var);
            if (!use->v[id])
              def->v[id] = true;
          }
        }
      }
    }
    USE.add(use);
    DEF.add(def);
    IN.add(lvin);<commit_msg>Fixed a bug in which variables that were used and defined in the same statement might be marked as "defined before used" instead of "used before defined."  This fixes the bugs in the release directory related to using the flags --no-expand-iterators-inline-opt and --no-single-loop-iterator-opt at the same time.<commit_after>#include "astutil.h"
#include "bb.h"
#include "expr.h"
#include "optimizations.h"
#include "stmt.h"

//#define DEBUG_LIVE


//
// Given a function fn, computes:
//
//   locals: a vector of local variables
//   localMap: a map of local variables to indices
//   useSet: a set of SymExprs that are uses of local variables
//   defSet: a set of SymExprs that are defs of local variables
//   OUT: set of live variables at exit of each basic block
//
// if OUT(i)(j) is true then the jth local variable is live at the
// exit of basic block i
//
void
liveVariableAnalysis(FnSymbol* fn,
                     Vec<Symbol*>& locals,
                     Map<Symbol*,int>& localMap,
                     Vec<SymExpr*>& useSet,
                     Vec<SymExpr*>& defSet,
                     Vec<Vec<bool>*>& OUT) {
  buildLocalsVectorMap(fn, locals, localMap);

#ifdef DEBUG_LIVE
  printLocalsVector(locals, localMap);
#endif

  buildDefUseSets(fn, locals, useSet, defSet);

  //
  // USE(i): the set of variables that are used in basic block i
  // before they are defined (if at all) in the block
  // DEF(i): the set of variables that are defined in basic block i
  // before they are used (if at all) in the block
  // IN(i): the set of variables that are live at entry to basic
  // block i
  //
  Vec<Vec<bool>*> USE;
  Vec<Vec<bool>*> DEF;
  Vec<Vec<bool>*> IN;

  forv_Vec(BasicBlock, bb, *fn->basicBlocks) {
    Vec<bool>* use = new Vec<bool>();
    Vec<bool>* def = new Vec<bool>();
    Vec<bool>* lvin = new Vec<bool>();
    Vec<bool>* lvout = new Vec<bool>();
    forv_Vec(Symbol, local, locals) {
      use->add(false);
      def->add(false);
      lvin->add(false);
      lvout->add(false);
    }
    forv_Vec(Expr, expr, bb->exprs) {
      Vec<BaseAST*> asts;
      collect_asts(&asts, expr);
      forv_Vec(BaseAST, ast, asts) {
        if (SymExpr* se = dynamic_cast<SymExpr*>(ast)) {
          if (useSet.set_in(se)) {
            int id = localMap.get(se->var);
            if (!def->v[id])
              use->v[id] = true;
          }
        }
      }
      forv_Vec(BaseAST, ast, asts) {
        if (SymExpr* se = dynamic_cast<SymExpr*>(ast)) {
          if (defSet.set_in(se)) {
            int id = localMap.get(se->var);
            if (!use->v[id])
              def->v[id] = true;
          }
        }
      }
    }
    USE.add(use);
    DEF.add(def);
    IN.add(lvin);
{'fraction_non_alphanumeric': 0.1372783422992628, 'fraction_numerical': 0.0, 'mean_word_length': 2.272490221642764, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 72, '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': '6605900', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1651, 'n_words': 516}