1 // optimize TCG using extract op
3 // Copyright: (C) 2017 Philippe Mathieu-Daudé. GPLv2+.
5 // Options: --macro-file scripts/cocci-macro-file.h
7 // Nikunj A Dadhania optimization:
8 // http://lists.nongnu.org/archive/html/qemu-devel/2017-02/msg05211.html
9 // Aurelien Jarno optimization:
10 // http://lists.nongnu.org/archive/html/qemu-devel/2017-05/msg01466.html
12 // This script can be run either using spatch locally or via a docker image:
15 // --macro-file scripts/cocci-macro-file.h \
16 // --sp-file scripts/coccinelle/tcg_gen_extract.cocci \
17 // --keep-comments --in-place \
18 // --use-gitgrep --dir target
20 // $ docker run --rm -v $PWD:$PWD -w $PWD philmd/coccinelle \
21 // --macro-file scripts/cocci-macro-file.h \
22 // --sp-file scripts/coccinelle/tcg_gen_extract.cocci \
23 // --keep-comments --in-place \
24 // --use-gitgrep --dir target
30 def debug(msg="", trailer="\n"):
31 fd.write("[DBG] " + msg + trailer)
32 def low_bits_count(value):
34 while (value & (1 << bits_count)):
37 def Mn(order): # Mersenne number
38 return (1 << order) - 1
44 position shr_p, and_p;
47 tcg_gen_shri_i32@shr_p
49 tcg_gen_shri_i64@shr_p
55 tcg_gen_andi_i32@and_p
57 tcg_gen_andi_i64@and_p
62 @script:python verify_len depends on match@
68 is_optimizable = False
69 debug("candidate at %s:%s" % (shr_p[0].file, shr_p[0].line))
70 try: # only eval integer, no #define like 'SR_M' (cpp did this, else some headers are missing).
71 msk_v = long(msk_s.strip("UL"), 0)
72 msk_b = low_bits_count(msk_v)
74 debug(" value: 0x%x low_bits: %d" % (msk_v, msk_b))
76 debug(" value: 0x%x low_bits: %d [Mersenne number: 0x%x]" % (msk_v, msk_b, Mn(msk_b)))
77 is_optimizable = Mn(msk_b) == msk_v # check low_bits
78 coccinelle.extract_len = "%d" % msk_b
79 debug(" candidate %s optimizable" % ("IS" if is_optimizable else "is NOT"))
81 debug(" ERROR (check included headers?)")
82 cocci.include_match(is_optimizable)
85 @replacement depends on verify_len@
87 metavariable match.arg;
88 constant match.ofs, match.msk;
89 position match.shr_p, match.and_p;
90 identifier verify_len.extract_len;
93 -tcg_gen_shri_i32@shr_p(ret, arg, ofs);
94 +tcg_gen_extract_i32(ret, arg, ofs, extract_len);
96 -tcg_gen_andi_i32@and_p(ret, ret, msk);
98 -tcg_gen_shri_i64@shr_p(ret, arg, ofs);
99 +tcg_gen_extract_i64(ret, arg, ofs, extract_len);
101 -tcg_gen_andi_i64@and_p(ret, ret, msk);
103 -tcg_gen_shri_tl@shr_p(ret, arg, ofs);
104 +tcg_gen_extract_tl(ret, arg, ofs, extract_len);
106 -tcg_gen_andi_tl@and_p(ret, ret, msk);