@@ -67,7 +67,19 @@ let size_eos = function
67
67
| Coq_inl e -> size_expr e
68
68
| Coq_inr id -> 0
69
69
70
- let rec size_stmt = function
70
+ (* [more_likely] is called once for every if-then-else conditional in
71
+ the current function. Therefore, [size_stmt] is called once for
72
+ every "then" or "else" branch of a conditional. This can result in
73
+ time quadratic in the size of the function. To avoid this,
74
+ we bound the recursion depth of [size_stmt] w.r.t. if-then-else
75
+ conditionals. With a maximal depth of [D], each statement of the current
76
+ function is visited at most [D + 1] times. *)
77
+
78
+ let max_depth = 4
79
+ let max_size = 100
80
+
81
+ let rec size_stmt depth s =
82
+ match s with
71
83
| Sskip -> 0
72
84
| Sassign (id , a ) -> size_expr a
73
85
| Sstore (chunk , addr , args , src ) -> 1 + size_exprs args + size_expr src
@@ -77,18 +89,24 @@ let rec size_stmt = function
77
89
3 + size_eos eos + size_exprs args + length_exprs args
78
90
| Sbuiltin (_ , (EF_annot _ | EF_debug _ ), _ ) -> 0
79
91
| Sbuiltin (optid , ef , args ) -> 1 + size_builtin_args args
80
- | Sseq (s1 , s2 ) -> size_stmt s1 + size_stmt s2
92
+ | Sseq (s1 , s2 ) -> size_stmt depth s1 + size_stmt depth s2
81
93
| Sifthenelse (ce , s1 , s2 ) ->
82
- size_condexpr ce + max (size_stmt s1) (size_stmt s2)
83
- | Sloop s -> 1 + 4 * size_stmt s
84
- | Sblock s -> size_stmt s
94
+ if depth < = 0
95
+ then max_size
96
+ else size_condexpr ce
97
+ + max (size_stmt (depth - 1 ) s1) (size_stmt (depth - 1 ) s2)
98
+ | Sloop s -> 1 + 4 * size_stmt depth s
99
+ | Sblock s -> size_stmt depth s
85
100
| Sexit n -> 1
86
101
| Sswitch xe -> size_exitexpr xe
87
102
| Sreturn None -> 2
88
103
| Sreturn (Some arg ) -> 2 + size_expr arg
89
- | Slabel (lbl , s ) -> size_stmt s
104
+ | Slabel (lbl , s ) -> size_stmt depth s
90
105
| Sgoto lbl -> 1
91
106
107
+ let size_stmt s =
108
+ min (size_stmt max_depth s) max_size
109
+
92
110
let more_likely (c : condexpr ) (ifso : stmt ) (ifnot : stmt ) =
93
111
size_stmt ifso > size_stmt ifnot
94
112
0 commit comments