29
29
; Original author: Jared Davis <jared@centtech.com>
32
(include-book "../xf-resolve-ranges")
32
(include-book "../../mlib/consteval")
33
33
(include-book "../../mlib/stmt-tools")
34
(include-book "../../mlib/blocks")
34
35
(local (include-book "../../util/arithmetic"))
36
37
(defxdoc stmtrewrite
175
176
<p>We only try to unroll when @('n') is easily resolved to a constant that is
176
less than the @('unroll-limit'). In particular, we use @(see
177
vl-constexpr-reduce) to try to evaluate the condition. This lets us handle
178
things like @('repeat(width-1) body') after @(see unparameterization) has
177
less than the @('unroll-limit'). In particular, we use @(see vl-consteval) to
178
try to evaluate the condition. This lets us handle things like
179
@('repeat(width-1) body') after @(see unparameterization) has occurred.</p>"
181
(b* ((count (vl-constexpr-reduce condition))
181
(b* (((mv ok count-expr) (vl-consteval condition nil))
182
(count (and ok (vl-resolved->val count-expr)))
182
183
((when (and count (<= count unroll-limit)))
184
185
;; This works even when N is 0 or 1. We expect our later block
259
260
(define vl-caselist-all-null-p ((x vl-caselist-p))
262
(and (vl-nullstmt-p (cdar x))
263
(vl-caselist-all-null-p (cdr x)))))
266
(define vl-casestmt-rewrite ((casetype vl-casetype-p)
268
(cases vl-caselist-p)
261
(b* (((when (atom x))
263
((cons ?exprs stmt1) (first x)))
264
(and (vl-nullstmt-p stmt1)
265
(vl-caselist-all-null-p (rest x)))))
267
(define vl-casestmt-rewrite ((check vl-casecheck-p)
268
(casetype vl-casetype-p)
270
(caselist vl-caselist-p)
271
273
:returns (stmt vl-stmt-p :hyp :fguard)
272
274
:short "Eliminate pure-null case statements."
273
275
:long "<p>This is a pretty silly rewrite:</p>
275
case/casex/casez(expr): --> [null stmt]
277
[priority/unique/unique0/nil] [case/casex/casez](expr): --> [null stmt]
276
278
expr1 : [null stmt];
277
279
expr2 : [null stmt];
289
291
(if (and (vl-nullstmt-p default)
290
(vl-caselist-all-null-p cases))
292
(vl-caselist-all-null-p caselist))
291
293
;; All statements are null, just turn into null.
292
294
(make-vl-nullstmt)
293
295
;; Otherwise don't change it. Eventually convert all case statements
294
296
;; into if statements?
295
(make-vl-casestmt :casetype casetype
297
(make-vl-casestmt :check check
581
584
((vl-casestmt-p x)
582
585
(b* (((vl-casestmt x) x)
583
((mv warnings cases) (vl-caselist-rewrite x.cases unroll-limit warnings))
584
((mv warnings default) (vl-stmt-rewrite x.default unroll-limit warnings))
585
(x-prime (vl-casestmt-rewrite x.casetype x.test cases default x.atts)))
586
((mv warnings caselist) (vl-caselist-rewrite x.caselist unroll-limit warnings))
587
((mv warnings default) (vl-stmt-rewrite x.default unroll-limit warnings))
588
(x-prime (vl-casestmt-rewrite x.check x.casetype x.test caselist default x.atts)))
586
589
(mv warnings x-prime)))
588
591
((vl-forstmt-p x)
727
730
(cons car-prime cdr-prime)))))
732
(def-genblob-transform vl-genblob-stmtrewrite ((unroll-limit natp)
733
(warnings vl-warninglist-p))
734
:returns ((warnings vl-warninglist-p))
735
;; :verify-guards nil
736
(b* (((vl-genblob x) x)
738
(unroll-limit (lnfix unroll-limit))
739
(warnings (vl-warninglist-fix warnings))
740
((mv warnings alwayses)
741
(vl-alwayslist-stmtrewrite x.alwayses unroll-limit warnings))
742
((mv warnings initials)
743
(vl-initiallist-stmtrewrite x.initials unroll-limit warnings))
745
((mv warnings generates) (vl-generates-stmtrewrite x.generates unroll-limit warnings)))
752
:generates generates)))
753
:apply-to-generates vl-generates-stmtrewrite)
729
755
(define vl-module-stmtrewrite ((x vl-module-p)
730
756
(unroll-limit natp))
731
757
:returns (new-x vl-module-p :hyp :fguard)
732
758
(b* (((vl-module x) x)
733
759
((when (vl-module->hands-offp x))
736
((unless (or x.alwayses x.initials))
737
;; Optimization: bail early on modules with no blocks.
740
(warnings x.warnings)
741
((mv warnings alwayses)
742
(vl-alwayslist-stmtrewrite x.alwayses unroll-limit warnings))
743
((mv warnings initials)
744
(vl-initiallist-stmtrewrite x.initials unroll-limit warnings)))
748
:initials initials)))
761
(genblob (vl-module->genblob x))
762
((mv warnings new-genblob) (vl-genblob-stmtrewrite genblob unroll-limit x.warnings))
763
(x-warn (change-vl-module x :warnings warnings)))
764
(vl-genblob->module new-genblob x-warn)))
750
766
(defprojection vl-modulelist-stmtrewrite (x unroll-limit)
751
767
(vl-module-stmtrewrite x unroll-limit)