Skip to content

Commit 5f45f6a

Browse files
committed
Avoid Cil.constFold in Apron invariant generation
Instead of constant folding zero terms and one factors away, don't construct them in the first place.
1 parent be29c40 commit 5f45f6a

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/cdomains/apron/sharedFunctions.apron.ml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,11 @@ struct
298298
| Some vinfo when IntDomain.Size.is_cast_injective ~from_type:vinfo.vtype ~to_type:(TInt(ILongLong,[])) ->
299299
let var = Cilfacade.mkCast ~e:(Lval(Var vinfo,NoOffset)) ~newt:longlong in
300300
let flip, coeff = coeff_to_const ~scalewith c in
301-
let prod = BinOp(Mult, coeff, var, longlong) in
302-
flip, prod
301+
if Z.equal (Option.get (Cil.getInteger coeff)) Z.one then (* Option.get should succeed by construction *) (* TODO: check (scaled!) coeff directly instead of extracting from exp *)
302+
flip, var
303+
else
304+
let prod = BinOp(Mult, coeff, var, longlong) in
305+
flip, prod
303306
| None ->
304307
M.warn ~category:Analyzer "Invariant Apron: cannot convert to cil var: %a" Var.pretty v;
305308
raise Unsupported_Linexpr1
@@ -309,7 +312,13 @@ struct
309312

310313
(** Returned booleans indicates whether returned expressions should be negated. *)
311314
let cil_exp_of_linexpr1 ?scalewith (linexpr1:Linexpr1.t) =
312-
let terms = ref [coeff_to_const ~scalewith (Linexpr1.get_cst linexpr1)] in
315+
let terms =
316+
let c = Linexpr1.get_cst linexpr1 in
317+
if Coeff.is_zero c then
318+
ref []
319+
else
320+
ref [coeff_to_const ~scalewith c]
321+
in
313322
let append_summand (c:Coeff.union_5) v =
314323
if not (Coeff.is_zero c) then
315324
terms := cil_exp_of_linexpr1_term ~scalewith c v :: !terms
@@ -369,7 +378,7 @@ struct
369378
| DISEQ -> Ne
370379
| EQMOD _ -> raise Unsupported_Linexpr1
371380
in
372-
Some (Cil.constFold false @@ BinOp(binop, lhs, rhs, TInt(IInt,[]))) (* constFold removes multiplication by factor 1 *)
381+
Some (BinOp(binop, lhs, rhs, TInt(IInt,[])))
373382
with
374383
Unsupported_Linexpr1 -> None
375384
end

0 commit comments

Comments
 (0)