diff --git a/module/language/tree-il/effects.scm b/module/language/tree-il/effects.scm index 656b262ae..4610f7f8f 100644 --- a/module/language/tree-il/effects.scm +++ b/module/language/tree-il/effects.scm @@ -264,13 +264,34 @@ of an expression." ;; Effect-free primitives. (($ _ - ($ _ (and name - (? effect+exception-free-primitive?))) + ($ _ (or 'values 'eq? 'eqv? 'equal?)) args) - (logior (accumulate-effects args) - (if (constructor-primitive? name) - (cause &allocation) - &no-effects))) + (accumulate-effects args)) + + (($ _ + ($ _ (or 'not 'pair? 'null? 'list? 'symbol? + 'vector? 'struct? 'string? 'number? + 'char?)) + (arg)) + (compute-effects arg)) + + ;; Primitives that allocate memory. + (($ _ ($ _ 'cons) (x y)) + (logior (compute-effects x) (compute-effects y) + &allocation)) + + (($ _ ($ _ (or 'list 'vector)) args) + (logior (accumulate-effects args) &allocation)) + + (($ _ ($ _ 'make-prompt-tag) ()) + &allocation) + + (($ _ ($ _ 'make-prompt-tag) (arg)) + (logior (compute-effects arg) &allocation)) + + ;; Primitives that are normally effect-free, but which might + ;; cause type checks, allocate memory, or access mutable + ;; memory. FIXME: expand, to be more precise. (($ _ ($ _ (and name (? effect-free-primitive?))) diff --git a/test-suite/tests/cse.test b/test-suite/tests/cse.test index d01d31874..523635fc7 100644 --- a/test-suite/tests/cse.test +++ b/test-suite/tests/cse.test @@ -286,4 +286,10 @@ (apply (primitive car) (toplevel x)) (if (apply (primitive car) (toplevel x)) (const one) - (const two))))) + (const two)))) + + (pass-if-cse + (begin (cons 1 2 3) 4) + (begin + (apply (primitive cons) (const 1) (const 2) (const 3)) + (const 4))))