Skip to content

Commit 355872b

Browse files
committed
CHB:ARM: use register vars in use-high if values change
1 parent f0b7efa commit 355872b

File tree

7 files changed

+126
-9
lines changed

7 files changed

+126
-9
lines changed

CodeHawk/CHB/bchlib/bCHFunctionInfo.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,9 @@ object (self)
12771277
method get_memvar_offset (v:variable_t): memory_offset_t traceresult =
12781278
varmgr#get_memvar_offset v
12791279

1280+
method has_variable_index_offset (v: variable_t): bool =
1281+
varmgr#has_variable_index_offset v
1282+
12801283
method get_memval_offset (v:variable_t): memory_offset_t traceresult =
12811284
varmgr#get_memval_offset v
12821285

CodeHawk/CHB/bchlib/bCHLibTypes.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,6 +3888,8 @@ object
38883888
not a memory variable. *)
38893889
method get_memvar_offset: variable_t -> memory_offset_t traceresult
38903890

3891+
method has_variable_index_offset: variable_t -> bool
3892+
38913893
(** [get_memval_reference var] returns the base memory reference of
38923894
memory-variable value [var].
38933895
@@ -4429,6 +4431,8 @@ class type function_environment_int =
44294431
Returns [Error] if [index] is not a valid index of a memory reference. *)
44304432
method is_unknown_reference: int -> bool traceresult
44314433

4434+
method has_variable_index_offset: variable_t -> bool
4435+
44324436

44334437
(** {2 Memory address variables} *)
44344438

CodeHawk/CHB/bchlib/bCHLocationInvariant.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ object (self)
804804
STR "Variable does not have an initial value: ";
805805
var#toPretty]))
806806

807-
method get_init_disequalities =
807+
method get_init_disequalities: variable_t list =
808808
let result = ref [] in
809809
let add v = result := v :: !result in
810810
let _ = H.iter (fun _ facts ->
@@ -813,7 +813,7 @@ object (self)
813813
facts) table in
814814
!result
815815

816-
method get_init_equalities =
816+
method get_init_equalities: variable_t list =
817817
let result = ref [] in
818818
let add v = result := v :: !result in
819819
let _ = H.iter (fun _ facts ->

CodeHawk/CHB/bchlib/bCHVariable.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,11 @@ object (self)
573573
(fun av -> av#get_memory_offset)
574574
(self#get_variable v)
575575

576+
method has_variable_index_offset (v: variable_t): bool =
577+
match self#get_memvar_offset v with
578+
| Ok (IndexOffset (v, _, _)) -> self#is_register_variable v
579+
| _ -> false
580+
576581
method get_memval_offset (v: variable_t): memory_offset_t traceresult =
577582
tbind
578583
~msg:"varmgr:get_memval_offset"

CodeHawk/CHB/bchlib/bCHVersion.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ end
9595

9696

9797
let version = new version_info_t
98-
~version:"0.6.0_20241103"
99-
~date:"2024-11-03"
98+
~version:"0.6.0_20241111"
99+
~date:"2024-11-11"
100100
~licensee: None
101101
~maxfilesize: None
102102
()

CodeHawk/CHB/bchlibarm32/bCHFnARMDictionary.ml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,52 @@ object (self)
18281828
xd#index_xpr result;
18291829
xd#index_xpr rresult])
18301830

1831+
| SignedMultiplyWordB (c, rd, rn, rm) ->
1832+
let vrd = rd#to_variable floc in
1833+
let xrn = rn#to_expr floc in
1834+
let xrm = rm#to_expr floc in
1835+
let xe16 = int_constant_expr BCHDoubleword.e16 in
1836+
let result = XOp (XMult, [xrn; XOp (XMod, [xrm; xe16])]) in
1837+
let xxrn = rewrite_expr xrn in
1838+
let xxrm = rewrite_expr xrm in
1839+
let rresult = rewrite_expr ?restrict:(Some 4) result in
1840+
let rdefs = [get_rdef xrn; get_rdef xrm] @ (get_all_rdefs rresult) in
1841+
let uses = [get_def_use vrd] in
1842+
let useshigh = [get_def_use_high vrd] in
1843+
let (tagstring, args) =
1844+
mk_instrx_data
1845+
~vars:[vrd]
1846+
~xprs:[xrn; xrm; result; rresult; xxrn; xxrm]
1847+
~rdefs
1848+
~uses
1849+
~useshigh
1850+
() in
1851+
let (tags, args) = add_optional_instr_condition tagstring args c in
1852+
(tags, args)
1853+
1854+
| SignedMultiplyWordT (c, rd, rn, rm) ->
1855+
let vrd = rd#to_variable floc in
1856+
let xrn = rn#to_expr floc in
1857+
let xrm = rm#to_expr floc in
1858+
let xe16 = int_constant_expr BCHDoubleword.e16 in
1859+
let result = XOp (XMult, [xrn; XOp (XShiftrt, [xrm; xe16])]) in
1860+
let xxrn = rewrite_expr xrn in
1861+
let xxrm = rewrite_expr xrm in
1862+
let rresult = rewrite_expr ?restrict:(Some 4) result in
1863+
let rdefs = [get_rdef xrn; get_rdef xrm] @ (get_all_rdefs rresult) in
1864+
let uses = [get_def_use vrd] in
1865+
let useshigh = [get_def_use_high vrd] in
1866+
let (tagstring, args) =
1867+
mk_instrx_data
1868+
~vars:[vrd]
1869+
~xprs:[xrn; xrm; result; rresult; xxrn; xxrm]
1870+
~rdefs
1871+
~uses
1872+
~useshigh
1873+
() in
1874+
let (tags, args) = add_optional_instr_condition tagstring args c in
1875+
(tags, args)
1876+
18311877
| StoreMultipleDecrementBefore (wback, c, base, rl, _) ->
18321878
let basereg = base#get_register in
18331879
let regcount = rl#get_register_count in

CodeHawk/CHB/bchlibarm32/bCHTranslateARMToCHIF.ml

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,14 +591,33 @@ let translate_arm_instruction
591591
rvar :: rsvar :: acc
592592
| _ -> acc) [] ops in
593593

594-
let get_use_high_vars (xprs: xpr_t list): variable_t list =
594+
let get_use_high_vars ?(is_pop=false) (xprs: xpr_t list): variable_t list =
595595
let inv = floc#inv in
596596
List.fold_left (fun acc x ->
597597
let xw = inv#rewrite_expr x in
598598
let xs = simplify_xpr xw in
599+
let disvars = inv#get_init_disequalities in
600+
let is_disvar v = List.exists (fun vv -> v#equal vv) disvars in
601+
let xprvars = floc#env#variables_in_expr xs in
599602
let vars =
600-
List.filter (fun v -> not (floc#f#env#is_function_initial_value v))
601-
(floc#env#variables_in_expr xs) in
603+
if List.exists is_disvar xprvars && not is_pop then
604+
let _ =
605+
chlog#add
606+
"get_use_high_vars:basic"
607+
(LBLOCK [
608+
floc#l#toPretty;
609+
STR " ";
610+
x2p xs;
611+
STR " ==> ";
612+
x2p x;
613+
pretty_print_list
614+
(floc#env#variables_in_expr x)
615+
(fun v -> v#toPretty)
616+
" [" "; " "]"]) in
617+
floc#env#variables_in_expr x
618+
else
619+
List.filter (fun v -> not (floc#f#env#is_function_initial_value v))
620+
xprvars in
602621
vars @ acc) [] xprs in
603622

604623
let get_addr_use_high_vars (xprs: xpr_t list): variable_t list =
@@ -1618,7 +1637,9 @@ let translate_arm_instruction
16181637
let usevars = get_register_vars [rn; rm] in
16191638
let memvar = mem#to_variable floc in
16201639
let (usevars, usehigh) =
1621-
if memvar#isTmp || floc#f#env#is_unknown_memory_variable memvar then
1640+
if memvar#isTmp
1641+
|| floc#f#env#is_unknown_memory_variable memvar
1642+
|| floc#f#env#has_variable_index_offset memvar then
16221643
(* elevate address variables to high-use *)
16231644
let xrn = rn#to_expr floc in
16241645
let xrm = rm#to_expr floc in
@@ -2144,7 +2165,7 @@ let translate_arm_instruction
21442165
let stackvar = stackop#to_variable floc in
21452166
let stackrhs = stackop#to_expr floc in
21462167
let (regvar, cmds1) = floc#get_ssa_assign_commands reg stackrhs in
2147-
let usehigh = get_use_high_vars [stackrhs] in
2168+
let usehigh = get_use_high_vars ~is_pop:true [stackrhs] in
21482169
let defcmds1 =
21492170
floc#get_vardef_commands
21502171
~defs:[regvar; splhs]
@@ -2589,6 +2610,44 @@ let translate_arm_instruction
25892610
| ACCAlways -> default cmds
25902611
| _ -> make_conditional_commands c cmds)
25912612

2613+
| SignedMultiplyWordB (c, rd, rn, rm) ->
2614+
let floc = get_floc loc in
2615+
let rdreg = rd#to_register in
2616+
let (vrd, cmds) = floc#get_ssa_abstract_commands rdreg () in
2617+
let xrn = rn#to_expr floc in
2618+
let xrm = rm#to_expr floc in
2619+
let usevars = get_register_vars [rn; rm] in
2620+
let usehigh = get_use_high_vars [xrn; xrm] in
2621+
let defcmds =
2622+
floc#get_vardef_commands
2623+
~defs:[vrd]
2624+
~use:usevars
2625+
~usehigh:usehigh
2626+
ctxtiaddr in
2627+
let cmds = defcmds @ cmds in
2628+
(match c with
2629+
| ACCAlways -> default cmds
2630+
| _ -> make_conditional_commands c cmds)
2631+
2632+
| SignedMultiplyWordT (c, rd, rn, rm) ->
2633+
let floc = get_floc loc in
2634+
let rdreg = rd#to_register in
2635+
let (vrd, cmds) = floc#get_ssa_abstract_commands rdreg () in
2636+
let xrn = rn#to_expr floc in
2637+
let xrm = rm#to_expr floc in
2638+
let usevars = get_register_vars [rn; rm] in
2639+
let usehigh = get_use_high_vars [xrn; xrm] in
2640+
let defcmds =
2641+
floc#get_vardef_commands
2642+
~defs:[vrd]
2643+
~use:usevars
2644+
~usehigh:usehigh
2645+
ctxtiaddr in
2646+
let cmds = defcmds @ cmds in
2647+
(match c with
2648+
| ACCAlways -> default cmds
2649+
| _ -> make_conditional_commands c cmds)
2650+
25922651
(* ---------------------------------------- StoreMultipleIncrementAfter --
25932652
* Stores multiple registers to consecutive memoy locations using an address
25942653
* from a base register. The consecutive memory locations start at this

0 commit comments

Comments
 (0)