<commit_before>(ns minijava.liveness
  (:use (minijava gas flow))
  (:require [minijava.temp :as tm])
  (:require [clojure.set :as set]))

;; union all the sets in the list together into one set
(defn union-all [sets-to-union]
  (loop [sets sets-to-union
         u    #{}]
   (if (empty? sets)
       u ;; return the complete union of the sets
       (recur (rest sets) (set/union u (first sets))))))

;; assumes that uses and defs have been defined for each GAS instruction
;; given a list of GAS instructions, return a hashmap defining live-in
(defn live [program]
(let [succs (flow program)]
  (loop [instrs   program
         live-in  {}
         live-out {}
         changed  false]
    (if (empty? instrs)
      (if changed
        ;; if any changes were made, start the loop again, with the
        ;; full set of instructions again
        (recur program live-in live-out false)
        ;; if we need both, instead return (list live-in live-out))
        ;; otherwise, if no changes occurred during this iteration, return
        ;; live in and live out
        live-in)

      ;;otherwise, if instrs is not empty, continue with the loop:
      (let [n           (first instrs)
            in          (get live-in n)
            out         (get live-out n)
            new-in-set  (set/union (uses n) (set/difference out (defs n)))
            get-live    (fn get-live [from]
                          (get live-in from))
            new-out-set (union-all (map get-live (get succs n)))
            new-ins     (assoc live-in n new-in-set)
            new-outs    (assoc live-out n new-out-set)
            isChanged   (or changed
                            (not (= in new-in-set))
                            (not (= out new-out-set)))]
        ;;determine if any changes were made during the course of the inner loop
        (recur (rest instrs) new-ins new-outs isChanged))))))<commit_msg>Fix to liveness (was doing analysis in reverse)<commit_after>(ns minijava.liveness
  (:use (minijava gas flow))
  (:require [minijava.temp :as tm])
  (:require [clojure.set :as set]))

;; union all the sets in the list together into one set
(defn union-all [sets-to-union]
  (loop [sets sets-to-union
         u    #{}]
   (if (empty? sets)
       u ;; return the complete union of the sets
       (recur (rest sets) (set/union u (first sets))))))

;; assumes that uses and defs have been defined for each GAS instruction
;; given a list of GAS instructions, return a hashmap defining live-in
(defn live-loop [program]
(let [succs (flow program)]
  (loop [instrs   program
         live-in  {}
         live-out {}
         changed  false]
    (if (empty? instrs)
      (if changed
        ;; if any changes were made, start the loop again, with the
        ;; full set of instructions again
        (recur program live-in live-out false)
        ;; if we need both, instead return (list live-in live-out))
        ;; otherwise, if no changes occurred during this iteration, return
        ;; live in and live out
        live-in)

      ;;otherwise, if instrs is not empty, continue with the loop:
      (let [n           (first instrs)
            in          (get live-in n)
            out         (get live-out n)
            new-in-set  (set/union (uses n) (set/difference out (defs n)))
            get-live    (fn get-live [from]
                          (get live-in from))
            new-out-set (union-all (map get-live (get succs n)))
            new-ins     (assoc live-in n new-in-set)
            new-outs    (assoc live-out n new-out-set)
            isChanged   (or changed
                            (not (= in new-in-set))
                            (not (= out new-out-set)))]
        ;;determine if any changes were made during the course of the inner loop
        (recur (rest instrs) new-ins new-outs isChanged))))))

(defn live [program]
  (live-loop (reverse program)))
{'fraction_non_alphanumeric': 0.10609943618657099, 'fraction_numerical': 0.0, 'mean_word_length': 1.9105145413870246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1855872', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1138, 'n_words': 450}