optimize.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048
  1. /*
  2. * Optimizations for Tiny Code Generator for QEMU
  3. *
  4. * Copyright (c) 2010 Samsung Electronics.
  5. * Contributed by Kirill Batuzov <batuzovk@ispras.ru>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/int128.h"
  27. #include "qemu/interval-tree.h"
  28. #include "tcg/tcg-op-common.h"
  29. #include "tcg-internal.h"
  30. #define CASE_OP_32_64(x) \
  31. glue(glue(case INDEX_op_, x), _i32): \
  32. glue(glue(case INDEX_op_, x), _i64)
  33. #define CASE_OP_32_64_VEC(x) \
  34. glue(glue(case INDEX_op_, x), _i32): \
  35. glue(glue(case INDEX_op_, x), _i64): \
  36. glue(glue(case INDEX_op_, x), _vec)
  37. typedef struct MemCopyInfo {
  38. IntervalTreeNode itree;
  39. QSIMPLEQ_ENTRY (MemCopyInfo) next;
  40. TCGTemp *ts;
  41. TCGType type;
  42. } MemCopyInfo;
  43. typedef struct TempOptInfo {
  44. bool is_const;
  45. TCGTemp *prev_copy;
  46. TCGTemp *next_copy;
  47. QSIMPLEQ_HEAD(, MemCopyInfo) mem_copy;
  48. uint64_t val;
  49. uint64_t z_mask; /* mask bit is 0 if and only if value bit is 0 */
  50. uint64_t s_mask; /* a left-aligned mask of clrsb(value) bits. */
  51. } TempOptInfo;
  52. typedef struct OptContext {
  53. TCGContext *tcg;
  54. TCGOp *prev_mb;
  55. TCGTempSet temps_used;
  56. IntervalTreeRoot mem_copy;
  57. QSIMPLEQ_HEAD(, MemCopyInfo) mem_free;
  58. /* In flight values from optimization. */
  59. uint64_t a_mask; /* mask bit is 0 iff value identical to first input */
  60. uint64_t z_mask; /* mask bit is 0 iff value bit is 0 */
  61. uint64_t s_mask; /* mask of clrsb(value) bits */
  62. TCGType type;
  63. } OptContext;
  64. /* Calculate the smask for a specific value. */
  65. static uint64_t smask_from_value(uint64_t value)
  66. {
  67. int rep = clrsb64(value);
  68. return ~(~0ull >> rep);
  69. }
  70. /*
  71. * Calculate the smask for a given set of known-zeros.
  72. * If there are lots of zeros on the left, we can consider the remainder
  73. * an unsigned field, and thus the corresponding signed field is one bit
  74. * larger.
  75. */
  76. static uint64_t smask_from_zmask(uint64_t zmask)
  77. {
  78. /*
  79. * Only the 0 bits are significant for zmask, thus the msb itself
  80. * must be zero, else we have no sign information.
  81. */
  82. int rep = clz64(zmask);
  83. if (rep == 0) {
  84. return 0;
  85. }
  86. rep -= 1;
  87. return ~(~0ull >> rep);
  88. }
  89. /*
  90. * Recreate a properly left-aligned smask after manipulation.
  91. * Some bit-shuffling, particularly shifts and rotates, may
  92. * retain sign bits on the left, but may scatter disconnected
  93. * sign bits on the right. Retain only what remains to the left.
  94. */
  95. static uint64_t smask_from_smask(int64_t smask)
  96. {
  97. /* Only the 1 bits are significant for smask */
  98. return smask_from_zmask(~smask);
  99. }
  100. static inline TempOptInfo *ts_info(TCGTemp *ts)
  101. {
  102. return ts->state_ptr;
  103. }
  104. static inline TempOptInfo *arg_info(TCGArg arg)
  105. {
  106. return ts_info(arg_temp(arg));
  107. }
  108. static inline bool ts_is_const(TCGTemp *ts)
  109. {
  110. return ts_info(ts)->is_const;
  111. }
  112. static inline bool ts_is_const_val(TCGTemp *ts, uint64_t val)
  113. {
  114. TempOptInfo *ti = ts_info(ts);
  115. return ti->is_const && ti->val == val;
  116. }
  117. static inline bool arg_is_const(TCGArg arg)
  118. {
  119. return ts_is_const(arg_temp(arg));
  120. }
  121. static inline bool arg_is_const_val(TCGArg arg, uint64_t val)
  122. {
  123. return ts_is_const_val(arg_temp(arg), val);
  124. }
  125. static inline bool ts_is_copy(TCGTemp *ts)
  126. {
  127. return ts_info(ts)->next_copy != ts;
  128. }
  129. static TCGTemp *cmp_better_copy(TCGTemp *a, TCGTemp *b)
  130. {
  131. return a->kind < b->kind ? b : a;
  132. }
  133. /* Initialize and activate a temporary. */
  134. static void init_ts_info(OptContext *ctx, TCGTemp *ts)
  135. {
  136. size_t idx = temp_idx(ts);
  137. TempOptInfo *ti;
  138. if (test_bit(idx, ctx->temps_used.l)) {
  139. return;
  140. }
  141. set_bit(idx, ctx->temps_used.l);
  142. ti = ts->state_ptr;
  143. if (ti == NULL) {
  144. ti = tcg_malloc(sizeof(TempOptInfo));
  145. ts->state_ptr = ti;
  146. }
  147. ti->next_copy = ts;
  148. ti->prev_copy = ts;
  149. QSIMPLEQ_INIT(&ti->mem_copy);
  150. if (ts->kind == TEMP_CONST) {
  151. ti->is_const = true;
  152. ti->val = ts->val;
  153. ti->z_mask = ts->val;
  154. ti->s_mask = smask_from_value(ts->val);
  155. } else {
  156. ti->is_const = false;
  157. ti->z_mask = -1;
  158. ti->s_mask = 0;
  159. }
  160. }
  161. static MemCopyInfo *mem_copy_first(OptContext *ctx, intptr_t s, intptr_t l)
  162. {
  163. IntervalTreeNode *r = interval_tree_iter_first(&ctx->mem_copy, s, l);
  164. return r ? container_of(r, MemCopyInfo, itree) : NULL;
  165. }
  166. static MemCopyInfo *mem_copy_next(MemCopyInfo *mem, intptr_t s, intptr_t l)
  167. {
  168. IntervalTreeNode *r = interval_tree_iter_next(&mem->itree, s, l);
  169. return r ? container_of(r, MemCopyInfo, itree) : NULL;
  170. }
  171. static void remove_mem_copy(OptContext *ctx, MemCopyInfo *mc)
  172. {
  173. TCGTemp *ts = mc->ts;
  174. TempOptInfo *ti = ts_info(ts);
  175. interval_tree_remove(&mc->itree, &ctx->mem_copy);
  176. QSIMPLEQ_REMOVE(&ti->mem_copy, mc, MemCopyInfo, next);
  177. QSIMPLEQ_INSERT_TAIL(&ctx->mem_free, mc, next);
  178. }
  179. static void remove_mem_copy_in(OptContext *ctx, intptr_t s, intptr_t l)
  180. {
  181. while (true) {
  182. MemCopyInfo *mc = mem_copy_first(ctx, s, l);
  183. if (!mc) {
  184. break;
  185. }
  186. remove_mem_copy(ctx, mc);
  187. }
  188. }
  189. static void remove_mem_copy_all(OptContext *ctx)
  190. {
  191. remove_mem_copy_in(ctx, 0, -1);
  192. tcg_debug_assert(interval_tree_is_empty(&ctx->mem_copy));
  193. }
  194. static TCGTemp *find_better_copy(TCGTemp *ts)
  195. {
  196. TCGTemp *i, *ret;
  197. /* If this is already readonly, we can't do better. */
  198. if (temp_readonly(ts)) {
  199. return ts;
  200. }
  201. ret = ts;
  202. for (i = ts_info(ts)->next_copy; i != ts; i = ts_info(i)->next_copy) {
  203. ret = cmp_better_copy(ret, i);
  204. }
  205. return ret;
  206. }
  207. static void move_mem_copies(TCGTemp *dst_ts, TCGTemp *src_ts)
  208. {
  209. TempOptInfo *si = ts_info(src_ts);
  210. TempOptInfo *di = ts_info(dst_ts);
  211. MemCopyInfo *mc;
  212. QSIMPLEQ_FOREACH(mc, &si->mem_copy, next) {
  213. tcg_debug_assert(mc->ts == src_ts);
  214. mc->ts = dst_ts;
  215. }
  216. QSIMPLEQ_CONCAT(&di->mem_copy, &si->mem_copy);
  217. }
  218. /* Reset TEMP's state, possibly removing the temp for the list of copies. */
  219. static void reset_ts(OptContext *ctx, TCGTemp *ts)
  220. {
  221. TempOptInfo *ti = ts_info(ts);
  222. TCGTemp *pts = ti->prev_copy;
  223. TCGTemp *nts = ti->next_copy;
  224. TempOptInfo *pi = ts_info(pts);
  225. TempOptInfo *ni = ts_info(nts);
  226. ni->prev_copy = ti->prev_copy;
  227. pi->next_copy = ti->next_copy;
  228. ti->next_copy = ts;
  229. ti->prev_copy = ts;
  230. ti->is_const = false;
  231. ti->z_mask = -1;
  232. ti->s_mask = 0;
  233. if (!QSIMPLEQ_EMPTY(&ti->mem_copy)) {
  234. if (ts == nts) {
  235. /* Last temp copy being removed, the mem copies die. */
  236. MemCopyInfo *mc;
  237. QSIMPLEQ_FOREACH(mc, &ti->mem_copy, next) {
  238. interval_tree_remove(&mc->itree, &ctx->mem_copy);
  239. }
  240. QSIMPLEQ_CONCAT(&ctx->mem_free, &ti->mem_copy);
  241. } else {
  242. move_mem_copies(find_better_copy(nts), ts);
  243. }
  244. }
  245. }
  246. static void reset_temp(OptContext *ctx, TCGArg arg)
  247. {
  248. reset_ts(ctx, arg_temp(arg));
  249. }
  250. static void record_mem_copy(OptContext *ctx, TCGType type,
  251. TCGTemp *ts, intptr_t start, intptr_t last)
  252. {
  253. MemCopyInfo *mc;
  254. TempOptInfo *ti;
  255. mc = QSIMPLEQ_FIRST(&ctx->mem_free);
  256. if (mc) {
  257. QSIMPLEQ_REMOVE_HEAD(&ctx->mem_free, next);
  258. } else {
  259. mc = tcg_malloc(sizeof(*mc));
  260. }
  261. memset(mc, 0, sizeof(*mc));
  262. mc->itree.start = start;
  263. mc->itree.last = last;
  264. mc->type = type;
  265. interval_tree_insert(&mc->itree, &ctx->mem_copy);
  266. ts = find_better_copy(ts);
  267. ti = ts_info(ts);
  268. mc->ts = ts;
  269. QSIMPLEQ_INSERT_TAIL(&ti->mem_copy, mc, next);
  270. }
  271. static bool ts_are_copies(TCGTemp *ts1, TCGTemp *ts2)
  272. {
  273. TCGTemp *i;
  274. if (ts1 == ts2) {
  275. return true;
  276. }
  277. if (!ts_is_copy(ts1) || !ts_is_copy(ts2)) {
  278. return false;
  279. }
  280. for (i = ts_info(ts1)->next_copy; i != ts1; i = ts_info(i)->next_copy) {
  281. if (i == ts2) {
  282. return true;
  283. }
  284. }
  285. return false;
  286. }
  287. static bool args_are_copies(TCGArg arg1, TCGArg arg2)
  288. {
  289. return ts_are_copies(arg_temp(arg1), arg_temp(arg2));
  290. }
  291. static TCGTemp *find_mem_copy_for(OptContext *ctx, TCGType type, intptr_t s)
  292. {
  293. MemCopyInfo *mc;
  294. for (mc = mem_copy_first(ctx, s, s); mc; mc = mem_copy_next(mc, s, s)) {
  295. if (mc->itree.start == s && mc->type == type) {
  296. return find_better_copy(mc->ts);
  297. }
  298. }
  299. return NULL;
  300. }
  301. static TCGArg arg_new_constant(OptContext *ctx, uint64_t val)
  302. {
  303. TCGType type = ctx->type;
  304. TCGTemp *ts;
  305. if (type == TCG_TYPE_I32) {
  306. val = (int32_t)val;
  307. }
  308. ts = tcg_constant_internal(type, val);
  309. init_ts_info(ctx, ts);
  310. return temp_arg(ts);
  311. }
  312. static TCGArg arg_new_temp(OptContext *ctx)
  313. {
  314. TCGTemp *ts = tcg_temp_new_internal(ctx->type, TEMP_EBB);
  315. init_ts_info(ctx, ts);
  316. return temp_arg(ts);
  317. }
  318. static bool tcg_opt_gen_mov(OptContext *ctx, TCGOp *op, TCGArg dst, TCGArg src)
  319. {
  320. TCGTemp *dst_ts = arg_temp(dst);
  321. TCGTemp *src_ts = arg_temp(src);
  322. TempOptInfo *di;
  323. TempOptInfo *si;
  324. TCGOpcode new_op;
  325. if (ts_are_copies(dst_ts, src_ts)) {
  326. tcg_op_remove(ctx->tcg, op);
  327. return true;
  328. }
  329. reset_ts(ctx, dst_ts);
  330. di = ts_info(dst_ts);
  331. si = ts_info(src_ts);
  332. switch (ctx->type) {
  333. case TCG_TYPE_I32:
  334. new_op = INDEX_op_mov_i32;
  335. break;
  336. case TCG_TYPE_I64:
  337. new_op = INDEX_op_mov_i64;
  338. break;
  339. case TCG_TYPE_V64:
  340. case TCG_TYPE_V128:
  341. case TCG_TYPE_V256:
  342. /* TCGOP_VECL and TCGOP_VECE remain unchanged. */
  343. new_op = INDEX_op_mov_vec;
  344. break;
  345. default:
  346. g_assert_not_reached();
  347. }
  348. op->opc = new_op;
  349. op->args[0] = dst;
  350. op->args[1] = src;
  351. di->z_mask = si->z_mask;
  352. di->s_mask = si->s_mask;
  353. if (src_ts->type == dst_ts->type) {
  354. TempOptInfo *ni = ts_info(si->next_copy);
  355. di->next_copy = si->next_copy;
  356. di->prev_copy = src_ts;
  357. ni->prev_copy = dst_ts;
  358. si->next_copy = dst_ts;
  359. di->is_const = si->is_const;
  360. di->val = si->val;
  361. if (!QSIMPLEQ_EMPTY(&si->mem_copy)
  362. && cmp_better_copy(src_ts, dst_ts) == dst_ts) {
  363. move_mem_copies(dst_ts, src_ts);
  364. }
  365. }
  366. return true;
  367. }
  368. static bool tcg_opt_gen_movi(OptContext *ctx, TCGOp *op,
  369. TCGArg dst, uint64_t val)
  370. {
  371. /* Convert movi to mov with constant temp. */
  372. return tcg_opt_gen_mov(ctx, op, dst, arg_new_constant(ctx, val));
  373. }
  374. static uint64_t do_constant_folding_2(TCGOpcode op, uint64_t x, uint64_t y)
  375. {
  376. uint64_t l64, h64;
  377. switch (op) {
  378. CASE_OP_32_64(add):
  379. return x + y;
  380. CASE_OP_32_64(sub):
  381. return x - y;
  382. CASE_OP_32_64(mul):
  383. return x * y;
  384. CASE_OP_32_64_VEC(and):
  385. return x & y;
  386. CASE_OP_32_64_VEC(or):
  387. return x | y;
  388. CASE_OP_32_64_VEC(xor):
  389. return x ^ y;
  390. case INDEX_op_shl_i32:
  391. return (uint32_t)x << (y & 31);
  392. case INDEX_op_shl_i64:
  393. return (uint64_t)x << (y & 63);
  394. case INDEX_op_shr_i32:
  395. return (uint32_t)x >> (y & 31);
  396. case INDEX_op_shr_i64:
  397. return (uint64_t)x >> (y & 63);
  398. case INDEX_op_sar_i32:
  399. return (int32_t)x >> (y & 31);
  400. case INDEX_op_sar_i64:
  401. return (int64_t)x >> (y & 63);
  402. case INDEX_op_rotr_i32:
  403. return ror32(x, y & 31);
  404. case INDEX_op_rotr_i64:
  405. return ror64(x, y & 63);
  406. case INDEX_op_rotl_i32:
  407. return rol32(x, y & 31);
  408. case INDEX_op_rotl_i64:
  409. return rol64(x, y & 63);
  410. CASE_OP_32_64_VEC(not):
  411. return ~x;
  412. CASE_OP_32_64(neg):
  413. return -x;
  414. CASE_OP_32_64_VEC(andc):
  415. return x & ~y;
  416. CASE_OP_32_64_VEC(orc):
  417. return x | ~y;
  418. CASE_OP_32_64_VEC(eqv):
  419. return ~(x ^ y);
  420. CASE_OP_32_64_VEC(nand):
  421. return ~(x & y);
  422. CASE_OP_32_64_VEC(nor):
  423. return ~(x | y);
  424. case INDEX_op_clz_i32:
  425. return (uint32_t)x ? clz32(x) : y;
  426. case INDEX_op_clz_i64:
  427. return x ? clz64(x) : y;
  428. case INDEX_op_ctz_i32:
  429. return (uint32_t)x ? ctz32(x) : y;
  430. case INDEX_op_ctz_i64:
  431. return x ? ctz64(x) : y;
  432. case INDEX_op_ctpop_i32:
  433. return ctpop32(x);
  434. case INDEX_op_ctpop_i64:
  435. return ctpop64(x);
  436. CASE_OP_32_64(ext8s):
  437. return (int8_t)x;
  438. CASE_OP_32_64(ext16s):
  439. return (int16_t)x;
  440. CASE_OP_32_64(ext8u):
  441. return (uint8_t)x;
  442. CASE_OP_32_64(ext16u):
  443. return (uint16_t)x;
  444. CASE_OP_32_64(bswap16):
  445. x = bswap16(x);
  446. return y & TCG_BSWAP_OS ? (int16_t)x : x;
  447. CASE_OP_32_64(bswap32):
  448. x = bswap32(x);
  449. return y & TCG_BSWAP_OS ? (int32_t)x : x;
  450. case INDEX_op_bswap64_i64:
  451. return bswap64(x);
  452. case INDEX_op_ext_i32_i64:
  453. case INDEX_op_ext32s_i64:
  454. return (int32_t)x;
  455. case INDEX_op_extu_i32_i64:
  456. case INDEX_op_extrl_i64_i32:
  457. case INDEX_op_ext32u_i64:
  458. return (uint32_t)x;
  459. case INDEX_op_extrh_i64_i32:
  460. return (uint64_t)x >> 32;
  461. case INDEX_op_muluh_i32:
  462. return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
  463. case INDEX_op_mulsh_i32:
  464. return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
  465. case INDEX_op_muluh_i64:
  466. mulu64(&l64, &h64, x, y);
  467. return h64;
  468. case INDEX_op_mulsh_i64:
  469. muls64(&l64, &h64, x, y);
  470. return h64;
  471. case INDEX_op_div_i32:
  472. /* Avoid crashing on divide by zero, otherwise undefined. */
  473. return (int32_t)x / ((int32_t)y ? : 1);
  474. case INDEX_op_divu_i32:
  475. return (uint32_t)x / ((uint32_t)y ? : 1);
  476. case INDEX_op_div_i64:
  477. return (int64_t)x / ((int64_t)y ? : 1);
  478. case INDEX_op_divu_i64:
  479. return (uint64_t)x / ((uint64_t)y ? : 1);
  480. case INDEX_op_rem_i32:
  481. return (int32_t)x % ((int32_t)y ? : 1);
  482. case INDEX_op_remu_i32:
  483. return (uint32_t)x % ((uint32_t)y ? : 1);
  484. case INDEX_op_rem_i64:
  485. return (int64_t)x % ((int64_t)y ? : 1);
  486. case INDEX_op_remu_i64:
  487. return (uint64_t)x % ((uint64_t)y ? : 1);
  488. default:
  489. g_assert_not_reached();
  490. }
  491. }
  492. static uint64_t do_constant_folding(TCGOpcode op, TCGType type,
  493. uint64_t x, uint64_t y)
  494. {
  495. uint64_t res = do_constant_folding_2(op, x, y);
  496. if (type == TCG_TYPE_I32) {
  497. res = (int32_t)res;
  498. }
  499. return res;
  500. }
  501. static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
  502. {
  503. switch (c) {
  504. case TCG_COND_EQ:
  505. return x == y;
  506. case TCG_COND_NE:
  507. return x != y;
  508. case TCG_COND_LT:
  509. return (int32_t)x < (int32_t)y;
  510. case TCG_COND_GE:
  511. return (int32_t)x >= (int32_t)y;
  512. case TCG_COND_LE:
  513. return (int32_t)x <= (int32_t)y;
  514. case TCG_COND_GT:
  515. return (int32_t)x > (int32_t)y;
  516. case TCG_COND_LTU:
  517. return x < y;
  518. case TCG_COND_GEU:
  519. return x >= y;
  520. case TCG_COND_LEU:
  521. return x <= y;
  522. case TCG_COND_GTU:
  523. return x > y;
  524. case TCG_COND_TSTEQ:
  525. return (x & y) == 0;
  526. case TCG_COND_TSTNE:
  527. return (x & y) != 0;
  528. case TCG_COND_ALWAYS:
  529. case TCG_COND_NEVER:
  530. break;
  531. }
  532. g_assert_not_reached();
  533. }
  534. static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
  535. {
  536. switch (c) {
  537. case TCG_COND_EQ:
  538. return x == y;
  539. case TCG_COND_NE:
  540. return x != y;
  541. case TCG_COND_LT:
  542. return (int64_t)x < (int64_t)y;
  543. case TCG_COND_GE:
  544. return (int64_t)x >= (int64_t)y;
  545. case TCG_COND_LE:
  546. return (int64_t)x <= (int64_t)y;
  547. case TCG_COND_GT:
  548. return (int64_t)x > (int64_t)y;
  549. case TCG_COND_LTU:
  550. return x < y;
  551. case TCG_COND_GEU:
  552. return x >= y;
  553. case TCG_COND_LEU:
  554. return x <= y;
  555. case TCG_COND_GTU:
  556. return x > y;
  557. case TCG_COND_TSTEQ:
  558. return (x & y) == 0;
  559. case TCG_COND_TSTNE:
  560. return (x & y) != 0;
  561. case TCG_COND_ALWAYS:
  562. case TCG_COND_NEVER:
  563. break;
  564. }
  565. g_assert_not_reached();
  566. }
  567. static int do_constant_folding_cond_eq(TCGCond c)
  568. {
  569. switch (c) {
  570. case TCG_COND_GT:
  571. case TCG_COND_LTU:
  572. case TCG_COND_LT:
  573. case TCG_COND_GTU:
  574. case TCG_COND_NE:
  575. return 0;
  576. case TCG_COND_GE:
  577. case TCG_COND_GEU:
  578. case TCG_COND_LE:
  579. case TCG_COND_LEU:
  580. case TCG_COND_EQ:
  581. return 1;
  582. case TCG_COND_TSTEQ:
  583. case TCG_COND_TSTNE:
  584. return -1;
  585. case TCG_COND_ALWAYS:
  586. case TCG_COND_NEVER:
  587. break;
  588. }
  589. g_assert_not_reached();
  590. }
  591. /*
  592. * Return -1 if the condition can't be simplified,
  593. * and the result of the condition (0 or 1) if it can.
  594. */
  595. static int do_constant_folding_cond(TCGType type, TCGArg x,
  596. TCGArg y, TCGCond c)
  597. {
  598. if (arg_is_const(x) && arg_is_const(y)) {
  599. uint64_t xv = arg_info(x)->val;
  600. uint64_t yv = arg_info(y)->val;
  601. switch (type) {
  602. case TCG_TYPE_I32:
  603. return do_constant_folding_cond_32(xv, yv, c);
  604. case TCG_TYPE_I64:
  605. return do_constant_folding_cond_64(xv, yv, c);
  606. default:
  607. /* Only scalar comparisons are optimizable */
  608. return -1;
  609. }
  610. } else if (args_are_copies(x, y)) {
  611. return do_constant_folding_cond_eq(c);
  612. } else if (arg_is_const_val(y, 0)) {
  613. switch (c) {
  614. case TCG_COND_LTU:
  615. case TCG_COND_TSTNE:
  616. return 0;
  617. case TCG_COND_GEU:
  618. case TCG_COND_TSTEQ:
  619. return 1;
  620. default:
  621. return -1;
  622. }
  623. }
  624. return -1;
  625. }
  626. /**
  627. * swap_commutative:
  628. * @dest: TCGArg of the destination argument, or NO_DEST.
  629. * @p1: first paired argument
  630. * @p2: second paired argument
  631. *
  632. * If *@p1 is a constant and *@p2 is not, swap.
  633. * If *@p2 matches @dest, swap.
  634. * Return true if a swap was performed.
  635. */
  636. #define NO_DEST temp_arg(NULL)
  637. static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
  638. {
  639. TCGArg a1 = *p1, a2 = *p2;
  640. int sum = 0;
  641. sum += arg_is_const(a1);
  642. sum -= arg_is_const(a2);
  643. /* Prefer the constant in second argument, and then the form
  644. op a, a, b, which is better handled on non-RISC hosts. */
  645. if (sum > 0 || (sum == 0 && dest == a2)) {
  646. *p1 = a2;
  647. *p2 = a1;
  648. return true;
  649. }
  650. return false;
  651. }
  652. static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
  653. {
  654. int sum = 0;
  655. sum += arg_is_const(p1[0]);
  656. sum += arg_is_const(p1[1]);
  657. sum -= arg_is_const(p2[0]);
  658. sum -= arg_is_const(p2[1]);
  659. if (sum > 0) {
  660. TCGArg t;
  661. t = p1[0], p1[0] = p2[0], p2[0] = t;
  662. t = p1[1], p1[1] = p2[1], p2[1] = t;
  663. return true;
  664. }
  665. return false;
  666. }
  667. /*
  668. * Return -1 if the condition can't be simplified,
  669. * and the result of the condition (0 or 1) if it can.
  670. */
  671. static int do_constant_folding_cond1(OptContext *ctx, TCGOp *op, TCGArg dest,
  672. TCGArg *p1, TCGArg *p2, TCGArg *pcond)
  673. {
  674. TCGCond cond;
  675. bool swap;
  676. int r;
  677. swap = swap_commutative(dest, p1, p2);
  678. cond = *pcond;
  679. if (swap) {
  680. *pcond = cond = tcg_swap_cond(cond);
  681. }
  682. r = do_constant_folding_cond(ctx->type, *p1, *p2, cond);
  683. if (r >= 0) {
  684. return r;
  685. }
  686. if (!is_tst_cond(cond)) {
  687. return -1;
  688. }
  689. /*
  690. * TSTNE x,x -> NE x,0
  691. * TSTNE x,-1 -> NE x,0
  692. */
  693. if (args_are_copies(*p1, *p2) || arg_is_const_val(*p2, -1)) {
  694. *p2 = arg_new_constant(ctx, 0);
  695. *pcond = tcg_tst_eqne_cond(cond);
  696. return -1;
  697. }
  698. /* TSTNE x,sign -> LT x,0 */
  699. if (arg_is_const_val(*p2, (ctx->type == TCG_TYPE_I32
  700. ? INT32_MIN : INT64_MIN))) {
  701. *p2 = arg_new_constant(ctx, 0);
  702. *pcond = tcg_tst_ltge_cond(cond);
  703. return -1;
  704. }
  705. /* Expand to AND with a temporary if no backend support. */
  706. if (!TCG_TARGET_HAS_tst) {
  707. TCGOpcode and_opc = (ctx->type == TCG_TYPE_I32
  708. ? INDEX_op_and_i32 : INDEX_op_and_i64);
  709. TCGOp *op2 = tcg_op_insert_before(ctx->tcg, op, and_opc, 3);
  710. TCGArg tmp = arg_new_temp(ctx);
  711. op2->args[0] = tmp;
  712. op2->args[1] = *p1;
  713. op2->args[2] = *p2;
  714. *p1 = tmp;
  715. *p2 = arg_new_constant(ctx, 0);
  716. *pcond = tcg_tst_eqne_cond(cond);
  717. }
  718. return -1;
  719. }
  720. static int do_constant_folding_cond2(OptContext *ctx, TCGOp *op, TCGArg *args)
  721. {
  722. TCGArg al, ah, bl, bh;
  723. TCGCond c;
  724. bool swap;
  725. int r;
  726. swap = swap_commutative2(args, args + 2);
  727. c = args[4];
  728. if (swap) {
  729. args[4] = c = tcg_swap_cond(c);
  730. }
  731. al = args[0];
  732. ah = args[1];
  733. bl = args[2];
  734. bh = args[3];
  735. if (arg_is_const(bl) && arg_is_const(bh)) {
  736. tcg_target_ulong blv = arg_info(bl)->val;
  737. tcg_target_ulong bhv = arg_info(bh)->val;
  738. uint64_t b = deposit64(blv, 32, 32, bhv);
  739. if (arg_is_const(al) && arg_is_const(ah)) {
  740. tcg_target_ulong alv = arg_info(al)->val;
  741. tcg_target_ulong ahv = arg_info(ah)->val;
  742. uint64_t a = deposit64(alv, 32, 32, ahv);
  743. r = do_constant_folding_cond_64(a, b, c);
  744. if (r >= 0) {
  745. return r;
  746. }
  747. }
  748. if (b == 0) {
  749. switch (c) {
  750. case TCG_COND_LTU:
  751. case TCG_COND_TSTNE:
  752. return 0;
  753. case TCG_COND_GEU:
  754. case TCG_COND_TSTEQ:
  755. return 1;
  756. default:
  757. break;
  758. }
  759. }
  760. /* TSTNE x,-1 -> NE x,0 */
  761. if (b == -1 && is_tst_cond(c)) {
  762. args[3] = args[2] = arg_new_constant(ctx, 0);
  763. args[4] = tcg_tst_eqne_cond(c);
  764. return -1;
  765. }
  766. /* TSTNE x,sign -> LT x,0 */
  767. if (b == INT64_MIN && is_tst_cond(c)) {
  768. /* bl must be 0, so copy that to bh */
  769. args[3] = bl;
  770. args[4] = tcg_tst_ltge_cond(c);
  771. return -1;
  772. }
  773. }
  774. if (args_are_copies(al, bl) && args_are_copies(ah, bh)) {
  775. r = do_constant_folding_cond_eq(c);
  776. if (r >= 0) {
  777. return r;
  778. }
  779. /* TSTNE x,x -> NE x,0 */
  780. if (is_tst_cond(c)) {
  781. args[3] = args[2] = arg_new_constant(ctx, 0);
  782. args[4] = tcg_tst_eqne_cond(c);
  783. return -1;
  784. }
  785. }
  786. /* Expand to AND with a temporary if no backend support. */
  787. if (!TCG_TARGET_HAS_tst && is_tst_cond(c)) {
  788. TCGOp *op1 = tcg_op_insert_before(ctx->tcg, op, INDEX_op_and_i32, 3);
  789. TCGOp *op2 = tcg_op_insert_before(ctx->tcg, op, INDEX_op_and_i32, 3);
  790. TCGArg t1 = arg_new_temp(ctx);
  791. TCGArg t2 = arg_new_temp(ctx);
  792. op1->args[0] = t1;
  793. op1->args[1] = al;
  794. op1->args[2] = bl;
  795. op2->args[0] = t2;
  796. op2->args[1] = ah;
  797. op2->args[2] = bh;
  798. args[0] = t1;
  799. args[1] = t2;
  800. args[3] = args[2] = arg_new_constant(ctx, 0);
  801. args[4] = tcg_tst_eqne_cond(c);
  802. }
  803. return -1;
  804. }
  805. static void init_arguments(OptContext *ctx, TCGOp *op, int nb_args)
  806. {
  807. for (int i = 0; i < nb_args; i++) {
  808. TCGTemp *ts = arg_temp(op->args[i]);
  809. init_ts_info(ctx, ts);
  810. }
  811. }
  812. static void copy_propagate(OptContext *ctx, TCGOp *op,
  813. int nb_oargs, int nb_iargs)
  814. {
  815. for (int i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
  816. TCGTemp *ts = arg_temp(op->args[i]);
  817. if (ts_is_copy(ts)) {
  818. op->args[i] = temp_arg(find_better_copy(ts));
  819. }
  820. }
  821. }
  822. static void finish_folding(OptContext *ctx, TCGOp *op)
  823. {
  824. const TCGOpDef *def = &tcg_op_defs[op->opc];
  825. int i, nb_oargs;
  826. /*
  827. * We only optimize extended basic blocks. If the opcode ends a BB
  828. * and is not a conditional branch, reset all temp data.
  829. */
  830. if (def->flags & TCG_OPF_BB_END) {
  831. ctx->prev_mb = NULL;
  832. if (!(def->flags & TCG_OPF_COND_BRANCH)) {
  833. memset(&ctx->temps_used, 0, sizeof(ctx->temps_used));
  834. remove_mem_copy_all(ctx);
  835. }
  836. return;
  837. }
  838. nb_oargs = def->nb_oargs;
  839. for (i = 0; i < nb_oargs; i++) {
  840. TCGTemp *ts = arg_temp(op->args[i]);
  841. reset_ts(ctx, ts);
  842. /*
  843. * Save the corresponding known-zero/sign bits mask for the
  844. * first output argument (only one supported so far).
  845. */
  846. if (i == 0) {
  847. ts_info(ts)->z_mask = ctx->z_mask;
  848. ts_info(ts)->s_mask = ctx->s_mask;
  849. }
  850. }
  851. }
  852. /*
  853. * The fold_* functions return true when processing is complete,
  854. * usually by folding the operation to a constant or to a copy,
  855. * and calling tcg_opt_gen_{mov,movi}. They may do other things,
  856. * like collect information about the value produced, for use in
  857. * optimizing a subsequent operation.
  858. *
  859. * These first fold_* functions are all helpers, used by other
  860. * folders for more specific operations.
  861. */
  862. static bool fold_const1(OptContext *ctx, TCGOp *op)
  863. {
  864. if (arg_is_const(op->args[1])) {
  865. uint64_t t;
  866. t = arg_info(op->args[1])->val;
  867. t = do_constant_folding(op->opc, ctx->type, t, 0);
  868. return tcg_opt_gen_movi(ctx, op, op->args[0], t);
  869. }
  870. return false;
  871. }
  872. static bool fold_const2(OptContext *ctx, TCGOp *op)
  873. {
  874. if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
  875. uint64_t t1 = arg_info(op->args[1])->val;
  876. uint64_t t2 = arg_info(op->args[2])->val;
  877. t1 = do_constant_folding(op->opc, ctx->type, t1, t2);
  878. return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
  879. }
  880. return false;
  881. }
  882. static bool fold_commutative(OptContext *ctx, TCGOp *op)
  883. {
  884. swap_commutative(op->args[0], &op->args[1], &op->args[2]);
  885. return false;
  886. }
  887. static bool fold_const2_commutative(OptContext *ctx, TCGOp *op)
  888. {
  889. swap_commutative(op->args[0], &op->args[1], &op->args[2]);
  890. return fold_const2(ctx, op);
  891. }
  892. static bool fold_masks(OptContext *ctx, TCGOp *op)
  893. {
  894. uint64_t a_mask = ctx->a_mask;
  895. uint64_t z_mask = ctx->z_mask;
  896. uint64_t s_mask = ctx->s_mask;
  897. /*
  898. * 32-bit ops generate 32-bit results, which for the purpose of
  899. * simplifying tcg are sign-extended. Certainly that's how we
  900. * represent our constants elsewhere. Note that the bits will
  901. * be reset properly for a 64-bit value when encountering the
  902. * type changing opcodes.
  903. */
  904. if (ctx->type == TCG_TYPE_I32) {
  905. a_mask = (int32_t)a_mask;
  906. z_mask = (int32_t)z_mask;
  907. s_mask |= MAKE_64BIT_MASK(32, 32);
  908. ctx->z_mask = z_mask;
  909. ctx->s_mask = s_mask;
  910. }
  911. if (z_mask == 0) {
  912. return tcg_opt_gen_movi(ctx, op, op->args[0], 0);
  913. }
  914. if (a_mask == 0) {
  915. return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
  916. }
  917. return false;
  918. }
  919. /*
  920. * Convert @op to NOT, if NOT is supported by the host.
  921. * Return true f the conversion is successful, which will still
  922. * indicate that the processing is complete.
  923. */
  924. static bool fold_not(OptContext *ctx, TCGOp *op);
  925. static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx)
  926. {
  927. TCGOpcode not_op;
  928. bool have_not;
  929. switch (ctx->type) {
  930. case TCG_TYPE_I32:
  931. not_op = INDEX_op_not_i32;
  932. have_not = TCG_TARGET_HAS_not_i32;
  933. break;
  934. case TCG_TYPE_I64:
  935. not_op = INDEX_op_not_i64;
  936. have_not = TCG_TARGET_HAS_not_i64;
  937. break;
  938. case TCG_TYPE_V64:
  939. case TCG_TYPE_V128:
  940. case TCG_TYPE_V256:
  941. not_op = INDEX_op_not_vec;
  942. have_not = TCG_TARGET_HAS_not_vec;
  943. break;
  944. default:
  945. g_assert_not_reached();
  946. }
  947. if (have_not) {
  948. op->opc = not_op;
  949. op->args[1] = op->args[idx];
  950. return fold_not(ctx, op);
  951. }
  952. return false;
  953. }
  954. /* If the binary operation has first argument @i, fold to @i. */
  955. static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
  956. {
  957. if (arg_is_const_val(op->args[1], i)) {
  958. return tcg_opt_gen_movi(ctx, op, op->args[0], i);
  959. }
  960. return false;
  961. }
  962. /* If the binary operation has first argument @i, fold to NOT. */
  963. static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
  964. {
  965. if (arg_is_const_val(op->args[1], i)) {
  966. return fold_to_not(ctx, op, 2);
  967. }
  968. return false;
  969. }
  970. /* If the binary operation has second argument @i, fold to @i. */
  971. static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
  972. {
  973. if (arg_is_const_val(op->args[2], i)) {
  974. return tcg_opt_gen_movi(ctx, op, op->args[0], i);
  975. }
  976. return false;
  977. }
  978. /* If the binary operation has second argument @i, fold to identity. */
  979. static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i)
  980. {
  981. if (arg_is_const_val(op->args[2], i)) {
  982. return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
  983. }
  984. return false;
  985. }
  986. /* If the binary operation has second argument @i, fold to NOT. */
  987. static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
  988. {
  989. if (arg_is_const_val(op->args[2], i)) {
  990. return fold_to_not(ctx, op, 1);
  991. }
  992. return false;
  993. }
  994. /* If the binary operation has both arguments equal, fold to @i. */
  995. static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
  996. {
  997. if (args_are_copies(op->args[1], op->args[2])) {
  998. return tcg_opt_gen_movi(ctx, op, op->args[0], i);
  999. }
  1000. return false;
  1001. }
  1002. /* If the binary operation has both arguments equal, fold to identity. */
  1003. static bool fold_xx_to_x(OptContext *ctx, TCGOp *op)
  1004. {
  1005. if (args_are_copies(op->args[1], op->args[2])) {
  1006. return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
  1007. }
  1008. return false;
  1009. }
  1010. /*
  1011. * These outermost fold_<op> functions are sorted alphabetically.
  1012. *
  1013. * The ordering of the transformations should be:
  1014. * 1) those that produce a constant
  1015. * 2) those that produce a copy
  1016. * 3) those that produce information about the result value.
  1017. */
  1018. static bool fold_add(OptContext *ctx, TCGOp *op)
  1019. {
  1020. if (fold_const2_commutative(ctx, op) ||
  1021. fold_xi_to_x(ctx, op, 0)) {
  1022. return true;
  1023. }
  1024. return false;
  1025. }
  1026. /* We cannot as yet do_constant_folding with vectors. */
  1027. static bool fold_add_vec(OptContext *ctx, TCGOp *op)
  1028. {
  1029. if (fold_commutative(ctx, op) ||
  1030. fold_xi_to_x(ctx, op, 0)) {
  1031. return true;
  1032. }
  1033. return false;
  1034. }
  1035. static bool fold_addsub2(OptContext *ctx, TCGOp *op, bool add)
  1036. {
  1037. bool a_const = arg_is_const(op->args[2]) && arg_is_const(op->args[3]);
  1038. bool b_const = arg_is_const(op->args[4]) && arg_is_const(op->args[5]);
  1039. if (a_const && b_const) {
  1040. uint64_t al = arg_info(op->args[2])->val;
  1041. uint64_t ah = arg_info(op->args[3])->val;
  1042. uint64_t bl = arg_info(op->args[4])->val;
  1043. uint64_t bh = arg_info(op->args[5])->val;
  1044. TCGArg rl, rh;
  1045. TCGOp *op2;
  1046. if (ctx->type == TCG_TYPE_I32) {
  1047. uint64_t a = deposit64(al, 32, 32, ah);
  1048. uint64_t b = deposit64(bl, 32, 32, bh);
  1049. if (add) {
  1050. a += b;
  1051. } else {
  1052. a -= b;
  1053. }
  1054. al = sextract64(a, 0, 32);
  1055. ah = sextract64(a, 32, 32);
  1056. } else {
  1057. Int128 a = int128_make128(al, ah);
  1058. Int128 b = int128_make128(bl, bh);
  1059. if (add) {
  1060. a = int128_add(a, b);
  1061. } else {
  1062. a = int128_sub(a, b);
  1063. }
  1064. al = int128_getlo(a);
  1065. ah = int128_gethi(a);
  1066. }
  1067. rl = op->args[0];
  1068. rh = op->args[1];
  1069. /* The proper opcode is supplied by tcg_opt_gen_mov. */
  1070. op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2);
  1071. tcg_opt_gen_movi(ctx, op, rl, al);
  1072. tcg_opt_gen_movi(ctx, op2, rh, ah);
  1073. return true;
  1074. }
  1075. /* Fold sub2 r,x,i to add2 r,x,-i */
  1076. if (!add && b_const) {
  1077. uint64_t bl = arg_info(op->args[4])->val;
  1078. uint64_t bh = arg_info(op->args[5])->val;
  1079. /* Negate the two parts without assembling and disassembling. */
  1080. bl = -bl;
  1081. bh = ~bh + !bl;
  1082. op->opc = (ctx->type == TCG_TYPE_I32
  1083. ? INDEX_op_add2_i32 : INDEX_op_add2_i64);
  1084. op->args[4] = arg_new_constant(ctx, bl);
  1085. op->args[5] = arg_new_constant(ctx, bh);
  1086. }
  1087. return false;
  1088. }
  1089. static bool fold_add2(OptContext *ctx, TCGOp *op)
  1090. {
  1091. /* Note that the high and low parts may be independently swapped. */
  1092. swap_commutative(op->args[0], &op->args[2], &op->args[4]);
  1093. swap_commutative(op->args[1], &op->args[3], &op->args[5]);
  1094. return fold_addsub2(ctx, op, true);
  1095. }
  1096. static bool fold_and(OptContext *ctx, TCGOp *op)
  1097. {
  1098. uint64_t z1, z2;
  1099. if (fold_const2_commutative(ctx, op) ||
  1100. fold_xi_to_i(ctx, op, 0) ||
  1101. fold_xi_to_x(ctx, op, -1) ||
  1102. fold_xx_to_x(ctx, op)) {
  1103. return true;
  1104. }
  1105. z1 = arg_info(op->args[1])->z_mask;
  1106. z2 = arg_info(op->args[2])->z_mask;
  1107. ctx->z_mask = z1 & z2;
  1108. /*
  1109. * Sign repetitions are perforce all identical, whether they are 1 or 0.
  1110. * Bitwise operations preserve the relative quantity of the repetitions.
  1111. */
  1112. ctx->s_mask = arg_info(op->args[1])->s_mask
  1113. & arg_info(op->args[2])->s_mask;
  1114. /*
  1115. * Known-zeros does not imply known-ones. Therefore unless
  1116. * arg2 is constant, we can't infer affected bits from it.
  1117. */
  1118. if (arg_is_const(op->args[2])) {
  1119. ctx->a_mask = z1 & ~z2;
  1120. }
  1121. return fold_masks(ctx, op);
  1122. }
  1123. static bool fold_andc(OptContext *ctx, TCGOp *op)
  1124. {
  1125. uint64_t z1;
  1126. if (fold_const2(ctx, op) ||
  1127. fold_xx_to_i(ctx, op, 0) ||
  1128. fold_xi_to_x(ctx, op, 0) ||
  1129. fold_ix_to_not(ctx, op, -1)) {
  1130. return true;
  1131. }
  1132. z1 = arg_info(op->args[1])->z_mask;
  1133. /*
  1134. * Known-zeros does not imply known-ones. Therefore unless
  1135. * arg2 is constant, we can't infer anything from it.
  1136. */
  1137. if (arg_is_const(op->args[2])) {
  1138. uint64_t z2 = ~arg_info(op->args[2])->z_mask;
  1139. ctx->a_mask = z1 & ~z2;
  1140. z1 &= z2;
  1141. }
  1142. ctx->z_mask = z1;
  1143. ctx->s_mask = arg_info(op->args[1])->s_mask
  1144. & arg_info(op->args[2])->s_mask;
  1145. return fold_masks(ctx, op);
  1146. }
  1147. static bool fold_brcond(OptContext *ctx, TCGOp *op)
  1148. {
  1149. int i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[0],
  1150. &op->args[1], &op->args[2]);
  1151. if (i == 0) {
  1152. tcg_op_remove(ctx->tcg, op);
  1153. return true;
  1154. }
  1155. if (i > 0) {
  1156. op->opc = INDEX_op_br;
  1157. op->args[0] = op->args[3];
  1158. }
  1159. return false;
  1160. }
  1161. static bool fold_brcond2(OptContext *ctx, TCGOp *op)
  1162. {
  1163. TCGCond cond;
  1164. TCGArg label;
  1165. int i, inv = 0;
  1166. i = do_constant_folding_cond2(ctx, op, &op->args[0]);
  1167. cond = op->args[4];
  1168. label = op->args[5];
  1169. if (i >= 0) {
  1170. goto do_brcond_const;
  1171. }
  1172. switch (cond) {
  1173. case TCG_COND_LT:
  1174. case TCG_COND_GE:
  1175. /*
  1176. * Simplify LT/GE comparisons vs zero to a single compare
  1177. * vs the high word of the input.
  1178. */
  1179. if (arg_is_const_val(op->args[2], 0) &&
  1180. arg_is_const_val(op->args[3], 0)) {
  1181. goto do_brcond_high;
  1182. }
  1183. break;
  1184. case TCG_COND_NE:
  1185. inv = 1;
  1186. QEMU_FALLTHROUGH;
  1187. case TCG_COND_EQ:
  1188. /*
  1189. * Simplify EQ/NE comparisons where one of the pairs
  1190. * can be simplified.
  1191. */
  1192. i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0],
  1193. op->args[2], cond);
  1194. switch (i ^ inv) {
  1195. case 0:
  1196. goto do_brcond_const;
  1197. case 1:
  1198. goto do_brcond_high;
  1199. }
  1200. i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
  1201. op->args[3], cond);
  1202. switch (i ^ inv) {
  1203. case 0:
  1204. goto do_brcond_const;
  1205. case 1:
  1206. goto do_brcond_low;
  1207. }
  1208. break;
  1209. case TCG_COND_TSTEQ:
  1210. case TCG_COND_TSTNE:
  1211. if (arg_is_const_val(op->args[2], 0)) {
  1212. goto do_brcond_high;
  1213. }
  1214. if (arg_is_const_val(op->args[3], 0)) {
  1215. goto do_brcond_low;
  1216. }
  1217. break;
  1218. default:
  1219. break;
  1220. do_brcond_low:
  1221. op->opc = INDEX_op_brcond_i32;
  1222. op->args[1] = op->args[2];
  1223. op->args[2] = cond;
  1224. op->args[3] = label;
  1225. return fold_brcond(ctx, op);
  1226. do_brcond_high:
  1227. op->opc = INDEX_op_brcond_i32;
  1228. op->args[0] = op->args[1];
  1229. op->args[1] = op->args[3];
  1230. op->args[2] = cond;
  1231. op->args[3] = label;
  1232. return fold_brcond(ctx, op);
  1233. do_brcond_const:
  1234. if (i == 0) {
  1235. tcg_op_remove(ctx->tcg, op);
  1236. return true;
  1237. }
  1238. op->opc = INDEX_op_br;
  1239. op->args[0] = label;
  1240. break;
  1241. }
  1242. return false;
  1243. }
  1244. static bool fold_bswap(OptContext *ctx, TCGOp *op)
  1245. {
  1246. uint64_t z_mask, s_mask, sign;
  1247. if (arg_is_const(op->args[1])) {
  1248. uint64_t t = arg_info(op->args[1])->val;
  1249. t = do_constant_folding(op->opc, ctx->type, t, op->args[2]);
  1250. return tcg_opt_gen_movi(ctx, op, op->args[0], t);
  1251. }
  1252. z_mask = arg_info(op->args[1])->z_mask;
  1253. switch (op->opc) {
  1254. case INDEX_op_bswap16_i32:
  1255. case INDEX_op_bswap16_i64:
  1256. z_mask = bswap16(z_mask);
  1257. sign = INT16_MIN;
  1258. break;
  1259. case INDEX_op_bswap32_i32:
  1260. case INDEX_op_bswap32_i64:
  1261. z_mask = bswap32(z_mask);
  1262. sign = INT32_MIN;
  1263. break;
  1264. case INDEX_op_bswap64_i64:
  1265. z_mask = bswap64(z_mask);
  1266. sign = INT64_MIN;
  1267. break;
  1268. default:
  1269. g_assert_not_reached();
  1270. }
  1271. s_mask = smask_from_zmask(z_mask);
  1272. switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) {
  1273. case TCG_BSWAP_OZ:
  1274. break;
  1275. case TCG_BSWAP_OS:
  1276. /* If the sign bit may be 1, force all the bits above to 1. */
  1277. if (z_mask & sign) {
  1278. z_mask |= sign;
  1279. s_mask = sign << 1;
  1280. }
  1281. break;
  1282. default:
  1283. /* The high bits are undefined: force all bits above the sign to 1. */
  1284. z_mask |= sign << 1;
  1285. s_mask = 0;
  1286. break;
  1287. }
  1288. ctx->z_mask = z_mask;
  1289. ctx->s_mask = s_mask;
  1290. return fold_masks(ctx, op);
  1291. }
  1292. static bool fold_call(OptContext *ctx, TCGOp *op)
  1293. {
  1294. TCGContext *s = ctx->tcg;
  1295. int nb_oargs = TCGOP_CALLO(op);
  1296. int nb_iargs = TCGOP_CALLI(op);
  1297. int flags, i;
  1298. init_arguments(ctx, op, nb_oargs + nb_iargs);
  1299. copy_propagate(ctx, op, nb_oargs, nb_iargs);
  1300. /* If the function reads or writes globals, reset temp data. */
  1301. flags = tcg_call_flags(op);
  1302. if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
  1303. int nb_globals = s->nb_globals;
  1304. for (i = 0; i < nb_globals; i++) {
  1305. if (test_bit(i, ctx->temps_used.l)) {
  1306. reset_ts(ctx, &ctx->tcg->temps[i]);
  1307. }
  1308. }
  1309. }
  1310. /* If the function has side effects, reset mem data. */
  1311. if (!(flags & TCG_CALL_NO_SIDE_EFFECTS)) {
  1312. remove_mem_copy_all(ctx);
  1313. }
  1314. /* Reset temp data for outputs. */
  1315. for (i = 0; i < nb_oargs; i++) {
  1316. reset_temp(ctx, op->args[i]);
  1317. }
  1318. /* Stop optimizing MB across calls. */
  1319. ctx->prev_mb = NULL;
  1320. return true;
  1321. }
  1322. static bool fold_count_zeros(OptContext *ctx, TCGOp *op)
  1323. {
  1324. uint64_t z_mask;
  1325. if (arg_is_const(op->args[1])) {
  1326. uint64_t t = arg_info(op->args[1])->val;
  1327. if (t != 0) {
  1328. t = do_constant_folding(op->opc, ctx->type, t, 0);
  1329. return tcg_opt_gen_movi(ctx, op, op->args[0], t);
  1330. }
  1331. return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
  1332. }
  1333. switch (ctx->type) {
  1334. case TCG_TYPE_I32:
  1335. z_mask = 31;
  1336. break;
  1337. case TCG_TYPE_I64:
  1338. z_mask = 63;
  1339. break;
  1340. default:
  1341. g_assert_not_reached();
  1342. }
  1343. ctx->z_mask = arg_info(op->args[2])->z_mask | z_mask;
  1344. ctx->s_mask = smask_from_zmask(ctx->z_mask);
  1345. return false;
  1346. }
  1347. static bool fold_ctpop(OptContext *ctx, TCGOp *op)
  1348. {
  1349. if (fold_const1(ctx, op)) {
  1350. return true;
  1351. }
  1352. switch (ctx->type) {
  1353. case TCG_TYPE_I32:
  1354. ctx->z_mask = 32 | 31;
  1355. break;
  1356. case TCG_TYPE_I64:
  1357. ctx->z_mask = 64 | 63;
  1358. break;
  1359. default:
  1360. g_assert_not_reached();
  1361. }
  1362. ctx->s_mask = smask_from_zmask(ctx->z_mask);
  1363. return false;
  1364. }
  1365. static bool fold_deposit(OptContext *ctx, TCGOp *op)
  1366. {
  1367. TCGOpcode and_opc;
  1368. if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
  1369. uint64_t t1 = arg_info(op->args[1])->val;
  1370. uint64_t t2 = arg_info(op->args[2])->val;
  1371. t1 = deposit64(t1, op->args[3], op->args[4], t2);
  1372. return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
  1373. }
  1374. switch (ctx->type) {
  1375. case TCG_TYPE_I32:
  1376. and_opc = INDEX_op_and_i32;
  1377. break;
  1378. case TCG_TYPE_I64:
  1379. and_opc = INDEX_op_and_i64;
  1380. break;
  1381. default:
  1382. g_assert_not_reached();
  1383. }
  1384. /* Inserting a value into zero at offset 0. */
  1385. if (arg_is_const_val(op->args[1], 0) && op->args[3] == 0) {
  1386. uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]);
  1387. op->opc = and_opc;
  1388. op->args[1] = op->args[2];
  1389. op->args[2] = arg_new_constant(ctx, mask);
  1390. ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
  1391. return false;
  1392. }
  1393. /* Inserting zero into a value. */
  1394. if (arg_is_const_val(op->args[2], 0)) {
  1395. uint64_t mask = deposit64(-1, op->args[3], op->args[4], 0);
  1396. op->opc = and_opc;
  1397. op->args[2] = arg_new_constant(ctx, mask);
  1398. ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
  1399. return false;
  1400. }
  1401. ctx->z_mask = deposit64(arg_info(op->args[1])->z_mask,
  1402. op->args[3], op->args[4],
  1403. arg_info(op->args[2])->z_mask);
  1404. return false;
  1405. }
  1406. static bool fold_divide(OptContext *ctx, TCGOp *op)
  1407. {
  1408. if (fold_const2(ctx, op) ||
  1409. fold_xi_to_x(ctx, op, 1)) {
  1410. return true;
  1411. }
  1412. return false;
  1413. }
  1414. static bool fold_dup(OptContext *ctx, TCGOp *op)
  1415. {
  1416. if (arg_is_const(op->args[1])) {
  1417. uint64_t t = arg_info(op->args[1])->val;
  1418. t = dup_const(TCGOP_VECE(op), t);
  1419. return tcg_opt_gen_movi(ctx, op, op->args[0], t);
  1420. }
  1421. return false;
  1422. }
  1423. static bool fold_dup2(OptContext *ctx, TCGOp *op)
  1424. {
  1425. if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
  1426. uint64_t t = deposit64(arg_info(op->args[1])->val, 32, 32,
  1427. arg_info(op->args[2])->val);
  1428. return tcg_opt_gen_movi(ctx, op, op->args[0], t);
  1429. }
  1430. if (args_are_copies(op->args[1], op->args[2])) {
  1431. op->opc = INDEX_op_dup_vec;
  1432. TCGOP_VECE(op) = MO_32;
  1433. }
  1434. return false;
  1435. }
  1436. static bool fold_eqv(OptContext *ctx, TCGOp *op)
  1437. {
  1438. if (fold_const2_commutative(ctx, op) ||
  1439. fold_xi_to_x(ctx, op, -1) ||
  1440. fold_xi_to_not(ctx, op, 0)) {
  1441. return true;
  1442. }
  1443. ctx->s_mask = arg_info(op->args[1])->s_mask
  1444. & arg_info(op->args[2])->s_mask;
  1445. return false;
  1446. }
  1447. static bool fold_extract(OptContext *ctx, TCGOp *op)
  1448. {
  1449. uint64_t z_mask_old, z_mask;
  1450. int pos = op->args[2];
  1451. int len = op->args[3];
  1452. if (arg_is_const(op->args[1])) {
  1453. uint64_t t;
  1454. t = arg_info(op->args[1])->val;
  1455. t = extract64(t, pos, len);
  1456. return tcg_opt_gen_movi(ctx, op, op->args[0], t);
  1457. }
  1458. z_mask_old = arg_info(op->args[1])->z_mask;
  1459. z_mask = extract64(z_mask_old, pos, len);
  1460. if (pos == 0) {
  1461. ctx->a_mask = z_mask_old ^ z_mask;
  1462. }
  1463. ctx->z_mask = z_mask;
  1464. ctx->s_mask = smask_from_zmask(z_mask);
  1465. return fold_masks(ctx, op);
  1466. }
  1467. static bool fold_extract2(OptContext *ctx, TCGOp *op)
  1468. {
  1469. if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
  1470. uint64_t v1 = arg_info(op->args[1])->val;
  1471. uint64_t v2 = arg_info(op->args[2])->val;
  1472. int shr = op->args[3];
  1473. if (op->opc == INDEX_op_extract2_i64) {
  1474. v1 >>= shr;
  1475. v2 <<= 64 - shr;
  1476. } else {
  1477. v1 = (uint32_t)v1 >> shr;
  1478. v2 = (uint64_t)((int32_t)v2 << (32 - shr));
  1479. }
  1480. return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2);
  1481. }
  1482. return false;
  1483. }
  1484. static bool fold_exts(OptContext *ctx, TCGOp *op)
  1485. {
  1486. uint64_t s_mask_old, s_mask, z_mask, sign;
  1487. bool type_change = false;
  1488. if (fold_const1(ctx, op)) {
  1489. return true;
  1490. }
  1491. z_mask = arg_info(op->args[1])->z_mask;
  1492. s_mask = arg_info(op->args[1])->s_mask;
  1493. s_mask_old = s_mask;
  1494. switch (op->opc) {
  1495. CASE_OP_32_64(ext8s):
  1496. sign = INT8_MIN;
  1497. z_mask = (uint8_t)z_mask;
  1498. break;
  1499. CASE_OP_32_64(ext16s):
  1500. sign = INT16_MIN;
  1501. z_mask = (uint16_t)z_mask;
  1502. break;
  1503. case INDEX_op_ext_i32_i64:
  1504. type_change = true;
  1505. QEMU_FALLTHROUGH;
  1506. case INDEX_op_ext32s_i64:
  1507. sign = INT32_MIN;
  1508. z_mask = (uint32_t)z_mask;
  1509. break;
  1510. default:
  1511. g_assert_not_reached();
  1512. }
  1513. if (z_mask & sign) {
  1514. z_mask |= sign;
  1515. }
  1516. s_mask |= sign << 1;
  1517. ctx->z_mask = z_mask;
  1518. ctx->s_mask = s_mask;
  1519. if (!type_change) {
  1520. ctx->a_mask = s_mask & ~s_mask_old;
  1521. }
  1522. return fold_masks(ctx, op);
  1523. }
  1524. static bool fold_extu(OptContext *ctx, TCGOp *op)
  1525. {
  1526. uint64_t z_mask_old, z_mask;
  1527. bool type_change = false;
  1528. if (fold_const1(ctx, op)) {
  1529. return true;
  1530. }
  1531. z_mask_old = z_mask = arg_info(op->args[1])->z_mask;
  1532. switch (op->opc) {
  1533. CASE_OP_32_64(ext8u):
  1534. z_mask = (uint8_t)z_mask;
  1535. break;
  1536. CASE_OP_32_64(ext16u):
  1537. z_mask = (uint16_t)z_mask;
  1538. break;
  1539. case INDEX_op_extrl_i64_i32:
  1540. case INDEX_op_extu_i32_i64:
  1541. type_change = true;
  1542. QEMU_FALLTHROUGH;
  1543. case INDEX_op_ext32u_i64:
  1544. z_mask = (uint32_t)z_mask;
  1545. break;
  1546. case INDEX_op_extrh_i64_i32:
  1547. type_change = true;
  1548. z_mask >>= 32;
  1549. break;
  1550. default:
  1551. g_assert_not_reached();
  1552. }
  1553. ctx->z_mask = z_mask;
  1554. ctx->s_mask = smask_from_zmask(z_mask);
  1555. if (!type_change) {
  1556. ctx->a_mask = z_mask_old ^ z_mask;
  1557. }
  1558. return fold_masks(ctx, op);
  1559. }
  1560. static bool fold_mb(OptContext *ctx, TCGOp *op)
  1561. {
  1562. /* Eliminate duplicate and redundant fence instructions. */
  1563. if (ctx->prev_mb) {
  1564. /*
  1565. * Merge two barriers of the same type into one,
  1566. * or a weaker barrier into a stronger one,
  1567. * or two weaker barriers into a stronger one.
  1568. * mb X; mb Y => mb X|Y
  1569. * mb; strl => mb; st
  1570. * ldaq; mb => ld; mb
  1571. * ldaq; strl => ld; mb; st
  1572. * Other combinations are also merged into a strong
  1573. * barrier. This is stricter than specified but for
  1574. * the purposes of TCG is better than not optimizing.
  1575. */
  1576. ctx->prev_mb->args[0] |= op->args[0];
  1577. tcg_op_remove(ctx->tcg, op);
  1578. } else {
  1579. ctx->prev_mb = op;
  1580. }
  1581. return true;
  1582. }
  1583. static bool fold_mov(OptContext *ctx, TCGOp *op)
  1584. {
  1585. return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
  1586. }
  1587. static bool fold_movcond(OptContext *ctx, TCGOp *op)
  1588. {
  1589. int i;
  1590. /* If true and false values are the same, eliminate the cmp. */
  1591. if (args_are_copies(op->args[3], op->args[4])) {
  1592. return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]);
  1593. }
  1594. /*
  1595. * Canonicalize the "false" input reg to match the destination reg so
  1596. * that the tcg backend can implement a "move if true" operation.
  1597. */
  1598. if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) {
  1599. op->args[5] = tcg_invert_cond(op->args[5]);
  1600. }
  1601. i = do_constant_folding_cond1(ctx, op, NO_DEST, &op->args[1],
  1602. &op->args[2], &op->args[5]);
  1603. if (i >= 0) {
  1604. return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]);
  1605. }
  1606. ctx->z_mask = arg_info(op->args[3])->z_mask
  1607. | arg_info(op->args[4])->z_mask;
  1608. ctx->s_mask = arg_info(op->args[3])->s_mask
  1609. & arg_info(op->args[4])->s_mask;
  1610. if (arg_is_const(op->args[3]) && arg_is_const(op->args[4])) {
  1611. uint64_t tv = arg_info(op->args[3])->val;
  1612. uint64_t fv = arg_info(op->args[4])->val;
  1613. TCGOpcode opc, negopc = 0;
  1614. TCGCond cond = op->args[5];
  1615. switch (ctx->type) {
  1616. case TCG_TYPE_I32:
  1617. opc = INDEX_op_setcond_i32;
  1618. if (TCG_TARGET_HAS_negsetcond_i32) {
  1619. negopc = INDEX_op_negsetcond_i32;
  1620. }
  1621. tv = (int32_t)tv;
  1622. fv = (int32_t)fv;
  1623. break;
  1624. case TCG_TYPE_I64:
  1625. opc = INDEX_op_setcond_i64;
  1626. if (TCG_TARGET_HAS_negsetcond_i64) {
  1627. negopc = INDEX_op_negsetcond_i64;
  1628. }
  1629. break;
  1630. default:
  1631. g_assert_not_reached();
  1632. }
  1633. if (tv == 1 && fv == 0) {
  1634. op->opc = opc;
  1635. op->args[3] = cond;
  1636. } else if (fv == 1 && tv == 0) {
  1637. op->opc = opc;
  1638. op->args[3] = tcg_invert_cond(cond);
  1639. } else if (negopc) {
  1640. if (tv == -1 && fv == 0) {
  1641. op->opc = negopc;
  1642. op->args[3] = cond;
  1643. } else if (fv == -1 && tv == 0) {
  1644. op->opc = negopc;
  1645. op->args[3] = tcg_invert_cond(cond);
  1646. }
  1647. }
  1648. }
  1649. return false;
  1650. }
  1651. static bool fold_mul(OptContext *ctx, TCGOp *op)
  1652. {
  1653. if (fold_const2(ctx, op) ||
  1654. fold_xi_to_i(ctx, op, 0) ||
  1655. fold_xi_to_x(ctx, op, 1)) {
  1656. return true;
  1657. }
  1658. return false;
  1659. }
  1660. static bool fold_mul_highpart(OptContext *ctx, TCGOp *op)
  1661. {
  1662. if (fold_const2_commutative(ctx, op) ||
  1663. fold_xi_to_i(ctx, op, 0)) {
  1664. return true;
  1665. }
  1666. return false;
  1667. }
  1668. static bool fold_multiply2(OptContext *ctx, TCGOp *op)
  1669. {
  1670. swap_commutative(op->args[0], &op->args[2], &op->args[3]);
  1671. if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) {
  1672. uint64_t a = arg_info(op->args[2])->val;
  1673. uint64_t b = arg_info(op->args[3])->val;
  1674. uint64_t h, l;
  1675. TCGArg rl, rh;
  1676. TCGOp *op2;
  1677. switch (op->opc) {
  1678. case INDEX_op_mulu2_i32:
  1679. l = (uint64_t)(uint32_t)a * (uint32_t)b;
  1680. h = (int32_t)(l >> 32);
  1681. l = (int32_t)l;
  1682. break;
  1683. case INDEX_op_muls2_i32:
  1684. l = (int64_t)(int32_t)a * (int32_t)b;
  1685. h = l >> 32;
  1686. l = (int32_t)l;
  1687. break;
  1688. case INDEX_op_mulu2_i64:
  1689. mulu64(&l, &h, a, b);
  1690. break;
  1691. case INDEX_op_muls2_i64:
  1692. muls64(&l, &h, a, b);
  1693. break;
  1694. default:
  1695. g_assert_not_reached();
  1696. }
  1697. rl = op->args[0];
  1698. rh = op->args[1];
  1699. /* The proper opcode is supplied by tcg_opt_gen_mov. */
  1700. op2 = tcg_op_insert_before(ctx->tcg, op, 0, 2);
  1701. tcg_opt_gen_movi(ctx, op, rl, l);
  1702. tcg_opt_gen_movi(ctx, op2, rh, h);
  1703. return true;
  1704. }
  1705. return false;
  1706. }
  1707. static bool fold_nand(OptContext *ctx, TCGOp *op)
  1708. {
  1709. if (fold_const2_commutative(ctx, op) ||
  1710. fold_xi_to_not(ctx, op, -1)) {
  1711. return true;
  1712. }
  1713. ctx->s_mask = arg_info(op->args[1])->s_mask
  1714. & arg_info(op->args[2])->s_mask;
  1715. return false;
  1716. }
  1717. static bool fold_neg_no_const(OptContext *ctx, TCGOp *op)
  1718. {
  1719. /* Set to 1 all bits to the left of the rightmost. */
  1720. uint64_t z_mask = arg_info(op->args[1])->z_mask;
  1721. ctx->z_mask = -(z_mask & -z_mask);
  1722. /*
  1723. * Because of fold_sub_to_neg, we want to always return true,
  1724. * via finish_folding.
  1725. */
  1726. finish_folding(ctx, op);
  1727. return true;
  1728. }
  1729. static bool fold_neg(OptContext *ctx, TCGOp *op)
  1730. {
  1731. return fold_const1(ctx, op) || fold_neg_no_const(ctx, op);
  1732. }
  1733. static bool fold_nor(OptContext *ctx, TCGOp *op)
  1734. {
  1735. if (fold_const2_commutative(ctx, op) ||
  1736. fold_xi_to_not(ctx, op, 0)) {
  1737. return true;
  1738. }
  1739. ctx->s_mask = arg_info(op->args[1])->s_mask
  1740. & arg_info(op->args[2])->s_mask;
  1741. return false;
  1742. }
  1743. static bool fold_not(OptContext *ctx, TCGOp *op)
  1744. {
  1745. if (fold_const1(ctx, op)) {
  1746. return true;
  1747. }
  1748. ctx->s_mask = arg_info(op->args[1])->s_mask;
  1749. /* Because of fold_to_not, we want to always return true, via finish. */
  1750. finish_folding(ctx, op);
  1751. return true;
  1752. }
  1753. static bool fold_or(OptContext *ctx, TCGOp *op)
  1754. {
  1755. if (fold_const2_commutative(ctx, op) ||
  1756. fold_xi_to_x(ctx, op, 0) ||
  1757. fold_xx_to_x(ctx, op)) {
  1758. return true;
  1759. }
  1760. ctx->z_mask = arg_info(op->args[1])->z_mask
  1761. | arg_info(op->args[2])->z_mask;
  1762. ctx->s_mask = arg_info(op->args[1])->s_mask
  1763. & arg_info(op->args[2])->s_mask;
  1764. return fold_masks(ctx, op);
  1765. }
  1766. static bool fold_orc(OptContext *ctx, TCGOp *op)
  1767. {
  1768. if (fold_const2(ctx, op) ||
  1769. fold_xx_to_i(ctx, op, -1) ||
  1770. fold_xi_to_x(ctx, op, -1) ||
  1771. fold_ix_to_not(ctx, op, 0)) {
  1772. return true;
  1773. }
  1774. ctx->s_mask = arg_info(op->args[1])->s_mask
  1775. & arg_info(op->args[2])->s_mask;
  1776. return false;
  1777. }
  1778. static bool fold_qemu_ld(OptContext *ctx, TCGOp *op)
  1779. {
  1780. const TCGOpDef *def = &tcg_op_defs[op->opc];
  1781. MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs];
  1782. MemOp mop = get_memop(oi);
  1783. int width = 8 * memop_size(mop);
  1784. if (width < 64) {
  1785. ctx->s_mask = MAKE_64BIT_MASK(width, 64 - width);
  1786. if (!(mop & MO_SIGN)) {
  1787. ctx->z_mask = MAKE_64BIT_MASK(0, width);
  1788. ctx->s_mask <<= 1;
  1789. }
  1790. }
  1791. /* Opcodes that touch guest memory stop the mb optimization. */
  1792. ctx->prev_mb = NULL;
  1793. return false;
  1794. }
  1795. static bool fold_qemu_st(OptContext *ctx, TCGOp *op)
  1796. {
  1797. /* Opcodes that touch guest memory stop the mb optimization. */
  1798. ctx->prev_mb = NULL;
  1799. return false;
  1800. }
  1801. static bool fold_remainder(OptContext *ctx, TCGOp *op)
  1802. {
  1803. if (fold_const2(ctx, op) ||
  1804. fold_xx_to_i(ctx, op, 0)) {
  1805. return true;
  1806. }
  1807. return false;
  1808. }
  1809. static bool fold_setcond_zmask(OptContext *ctx, TCGOp *op, bool neg)
  1810. {
  1811. uint64_t a_zmask, b_val;
  1812. TCGCond cond;
  1813. if (!arg_is_const(op->args[2])) {
  1814. return false;
  1815. }
  1816. a_zmask = arg_info(op->args[1])->z_mask;
  1817. b_val = arg_info(op->args[2])->val;
  1818. cond = op->args[3];
  1819. if (ctx->type == TCG_TYPE_I32) {
  1820. a_zmask = (uint32_t)a_zmask;
  1821. b_val = (uint32_t)b_val;
  1822. }
  1823. /*
  1824. * A with only low bits set vs B with high bits set means that A < B.
  1825. */
  1826. if (a_zmask < b_val) {
  1827. bool inv = false;
  1828. switch (cond) {
  1829. case TCG_COND_NE:
  1830. case TCG_COND_LEU:
  1831. case TCG_COND_LTU:
  1832. inv = true;
  1833. /* fall through */
  1834. case TCG_COND_GTU:
  1835. case TCG_COND_GEU:
  1836. case TCG_COND_EQ:
  1837. return tcg_opt_gen_movi(ctx, op, op->args[0], neg ? -inv : inv);
  1838. default:
  1839. break;
  1840. }
  1841. }
  1842. /*
  1843. * A with only lsb set is already boolean.
  1844. */
  1845. if (a_zmask <= 1) {
  1846. bool convert = false;
  1847. bool inv = false;
  1848. switch (cond) {
  1849. case TCG_COND_EQ:
  1850. inv = true;
  1851. /* fall through */
  1852. case TCG_COND_NE:
  1853. convert = (b_val == 0);
  1854. break;
  1855. case TCG_COND_LTU:
  1856. case TCG_COND_TSTEQ:
  1857. inv = true;
  1858. /* fall through */
  1859. case TCG_COND_GEU:
  1860. case TCG_COND_TSTNE:
  1861. convert = (b_val == 1);
  1862. break;
  1863. default:
  1864. break;
  1865. }
  1866. if (convert) {
  1867. TCGOpcode add_opc, xor_opc, neg_opc;
  1868. if (!inv && !neg) {
  1869. return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
  1870. }
  1871. switch (ctx->type) {
  1872. case TCG_TYPE_I32:
  1873. add_opc = INDEX_op_add_i32;
  1874. neg_opc = INDEX_op_neg_i32;
  1875. xor_opc = INDEX_op_xor_i32;
  1876. break;
  1877. case TCG_TYPE_I64:
  1878. add_opc = INDEX_op_add_i64;
  1879. neg_opc = INDEX_op_neg_i64;
  1880. xor_opc = INDEX_op_xor_i64;
  1881. break;
  1882. default:
  1883. g_assert_not_reached();
  1884. }
  1885. if (!inv) {
  1886. op->opc = neg_opc;
  1887. } else if (neg) {
  1888. op->opc = add_opc;
  1889. op->args[2] = arg_new_constant(ctx, -1);
  1890. } else {
  1891. op->opc = xor_opc;
  1892. op->args[2] = arg_new_constant(ctx, 1);
  1893. }
  1894. return false;
  1895. }
  1896. }
  1897. return false;
  1898. }
  1899. static void fold_setcond_tst_pow2(OptContext *ctx, TCGOp *op, bool neg)
  1900. {
  1901. TCGOpcode and_opc, sub_opc, xor_opc, neg_opc, shr_opc;
  1902. TCGOpcode uext_opc = 0, sext_opc = 0;
  1903. TCGCond cond = op->args[3];
  1904. TCGArg ret, src1, src2;
  1905. TCGOp *op2;
  1906. uint64_t val;
  1907. int sh;
  1908. bool inv;
  1909. if (!is_tst_cond(cond) || !arg_is_const(op->args[2])) {
  1910. return;
  1911. }
  1912. src2 = op->args[2];
  1913. val = arg_info(src2)->val;
  1914. if (!is_power_of_2(val)) {
  1915. return;
  1916. }
  1917. sh = ctz64(val);
  1918. switch (ctx->type) {
  1919. case TCG_TYPE_I32:
  1920. and_opc = INDEX_op_and_i32;
  1921. sub_opc = INDEX_op_sub_i32;
  1922. xor_opc = INDEX_op_xor_i32;
  1923. shr_opc = INDEX_op_shr_i32;
  1924. neg_opc = INDEX_op_neg_i32;
  1925. if (TCG_TARGET_extract_i32_valid(sh, 1)) {
  1926. uext_opc = TCG_TARGET_HAS_extract_i32 ? INDEX_op_extract_i32 : 0;
  1927. sext_opc = TCG_TARGET_HAS_sextract_i32 ? INDEX_op_sextract_i32 : 0;
  1928. }
  1929. break;
  1930. case TCG_TYPE_I64:
  1931. and_opc = INDEX_op_and_i64;
  1932. sub_opc = INDEX_op_sub_i64;
  1933. xor_opc = INDEX_op_xor_i64;
  1934. shr_opc = INDEX_op_shr_i64;
  1935. neg_opc = INDEX_op_neg_i64;
  1936. if (TCG_TARGET_extract_i64_valid(sh, 1)) {
  1937. uext_opc = TCG_TARGET_HAS_extract_i64 ? INDEX_op_extract_i64 : 0;
  1938. sext_opc = TCG_TARGET_HAS_sextract_i64 ? INDEX_op_sextract_i64 : 0;
  1939. }
  1940. break;
  1941. default:
  1942. g_assert_not_reached();
  1943. }
  1944. ret = op->args[0];
  1945. src1 = op->args[1];
  1946. inv = cond == TCG_COND_TSTEQ;
  1947. if (sh && sext_opc && neg && !inv) {
  1948. op->opc = sext_opc;
  1949. op->args[1] = src1;
  1950. op->args[2] = sh;
  1951. op->args[3] = 1;
  1952. return;
  1953. } else if (sh && uext_opc) {
  1954. op->opc = uext_opc;
  1955. op->args[1] = src1;
  1956. op->args[2] = sh;
  1957. op->args[3] = 1;
  1958. } else {
  1959. if (sh) {
  1960. op2 = tcg_op_insert_before(ctx->tcg, op, shr_opc, 3);
  1961. op2->args[0] = ret;
  1962. op2->args[1] = src1;
  1963. op2->args[2] = arg_new_constant(ctx, sh);
  1964. src1 = ret;
  1965. }
  1966. op->opc = and_opc;
  1967. op->args[1] = src1;
  1968. op->args[2] = arg_new_constant(ctx, 1);
  1969. }
  1970. if (neg && inv) {
  1971. op2 = tcg_op_insert_after(ctx->tcg, op, sub_opc, 3);
  1972. op2->args[0] = ret;
  1973. op2->args[1] = ret;
  1974. op2->args[2] = arg_new_constant(ctx, 1);
  1975. } else if (inv) {
  1976. op2 = tcg_op_insert_after(ctx->tcg, op, xor_opc, 3);
  1977. op2->args[0] = ret;
  1978. op2->args[1] = ret;
  1979. op2->args[2] = arg_new_constant(ctx, 1);
  1980. } else if (neg) {
  1981. op2 = tcg_op_insert_after(ctx->tcg, op, neg_opc, 2);
  1982. op2->args[0] = ret;
  1983. op2->args[1] = ret;
  1984. }
  1985. }
  1986. static bool fold_setcond(OptContext *ctx, TCGOp *op)
  1987. {
  1988. int i = do_constant_folding_cond1(ctx, op, op->args[0], &op->args[1],
  1989. &op->args[2], &op->args[3]);
  1990. if (i >= 0) {
  1991. return tcg_opt_gen_movi(ctx, op, op->args[0], i);
  1992. }
  1993. if (fold_setcond_zmask(ctx, op, false)) {
  1994. return true;
  1995. }
  1996. fold_setcond_tst_pow2(ctx, op, false);
  1997. ctx->z_mask = 1;
  1998. ctx->s_mask = smask_from_zmask(1);
  1999. return false;
  2000. }
  2001. static bool fold_negsetcond(OptContext *ctx, TCGOp *op)
  2002. {
  2003. int i = do_constant_folding_cond1(ctx, op, op->args[0], &op->args[1],
  2004. &op->args[2], &op->args[3]);
  2005. if (i >= 0) {
  2006. return tcg_opt_gen_movi(ctx, op, op->args[0], -i);
  2007. }
  2008. if (fold_setcond_zmask(ctx, op, true)) {
  2009. return true;
  2010. }
  2011. fold_setcond_tst_pow2(ctx, op, true);
  2012. /* Value is {0,-1} so all bits are repetitions of the sign. */
  2013. ctx->s_mask = -1;
  2014. return false;
  2015. }
  2016. static bool fold_setcond2(OptContext *ctx, TCGOp *op)
  2017. {
  2018. TCGCond cond;
  2019. int i, inv = 0;
  2020. i = do_constant_folding_cond2(ctx, op, &op->args[1]);
  2021. cond = op->args[5];
  2022. if (i >= 0) {
  2023. goto do_setcond_const;
  2024. }
  2025. switch (cond) {
  2026. case TCG_COND_LT:
  2027. case TCG_COND_GE:
  2028. /*
  2029. * Simplify LT/GE comparisons vs zero to a single compare
  2030. * vs the high word of the input.
  2031. */
  2032. if (arg_is_const_val(op->args[3], 0) &&
  2033. arg_is_const_val(op->args[4], 0)) {
  2034. goto do_setcond_high;
  2035. }
  2036. break;
  2037. case TCG_COND_NE:
  2038. inv = 1;
  2039. QEMU_FALLTHROUGH;
  2040. case TCG_COND_EQ:
  2041. /*
  2042. * Simplify EQ/NE comparisons where one of the pairs
  2043. * can be simplified.
  2044. */
  2045. i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
  2046. op->args[3], cond);
  2047. switch (i ^ inv) {
  2048. case 0:
  2049. goto do_setcond_const;
  2050. case 1:
  2051. goto do_setcond_high;
  2052. }
  2053. i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2],
  2054. op->args[4], cond);
  2055. switch (i ^ inv) {
  2056. case 0:
  2057. goto do_setcond_const;
  2058. case 1:
  2059. goto do_setcond_low;
  2060. }
  2061. break;
  2062. case TCG_COND_TSTEQ:
  2063. case TCG_COND_TSTNE:
  2064. if (arg_is_const_val(op->args[3], 0)) {
  2065. goto do_setcond_high;
  2066. }
  2067. if (arg_is_const_val(op->args[4], 0)) {
  2068. goto do_setcond_low;
  2069. }
  2070. break;
  2071. default:
  2072. break;
  2073. do_setcond_low:
  2074. op->args[2] = op->args[3];
  2075. op->args[3] = cond;
  2076. op->opc = INDEX_op_setcond_i32;
  2077. return fold_setcond(ctx, op);
  2078. do_setcond_high:
  2079. op->args[1] = op->args[2];
  2080. op->args[2] = op->args[4];
  2081. op->args[3] = cond;
  2082. op->opc = INDEX_op_setcond_i32;
  2083. return fold_setcond(ctx, op);
  2084. }
  2085. ctx->z_mask = 1;
  2086. ctx->s_mask = smask_from_zmask(1);
  2087. return false;
  2088. do_setcond_const:
  2089. return tcg_opt_gen_movi(ctx, op, op->args[0], i);
  2090. }
  2091. static bool fold_cmp_vec(OptContext *ctx, TCGOp *op)
  2092. {
  2093. /* Canonicalize the comparison to put immediate second. */
  2094. if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) {
  2095. op->args[3] = tcg_swap_cond(op->args[3]);
  2096. }
  2097. return false;
  2098. }
  2099. static bool fold_cmpsel_vec(OptContext *ctx, TCGOp *op)
  2100. {
  2101. /* If true and false values are the same, eliminate the cmp. */
  2102. if (args_are_copies(op->args[3], op->args[4])) {
  2103. return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]);
  2104. }
  2105. /* Canonicalize the comparison to put immediate second. */
  2106. if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) {
  2107. op->args[5] = tcg_swap_cond(op->args[5]);
  2108. }
  2109. /*
  2110. * Canonicalize the "false" input reg to match the destination,
  2111. * so that the tcg backend can implement "move if true".
  2112. */
  2113. if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) {
  2114. op->args[5] = tcg_invert_cond(op->args[5]);
  2115. }
  2116. return false;
  2117. }
  2118. static bool fold_sextract(OptContext *ctx, TCGOp *op)
  2119. {
  2120. uint64_t z_mask, s_mask, s_mask_old;
  2121. int pos = op->args[2];
  2122. int len = op->args[3];
  2123. if (arg_is_const(op->args[1])) {
  2124. uint64_t t;
  2125. t = arg_info(op->args[1])->val;
  2126. t = sextract64(t, pos, len);
  2127. return tcg_opt_gen_movi(ctx, op, op->args[0], t);
  2128. }
  2129. z_mask = arg_info(op->args[1])->z_mask;
  2130. z_mask = sextract64(z_mask, pos, len);
  2131. ctx->z_mask = z_mask;
  2132. s_mask_old = arg_info(op->args[1])->s_mask;
  2133. s_mask = sextract64(s_mask_old, pos, len);
  2134. s_mask |= MAKE_64BIT_MASK(len, 64 - len);
  2135. ctx->s_mask = s_mask;
  2136. if (pos == 0) {
  2137. ctx->a_mask = s_mask & ~s_mask_old;
  2138. }
  2139. return fold_masks(ctx, op);
  2140. }
  2141. static bool fold_shift(OptContext *ctx, TCGOp *op)
  2142. {
  2143. uint64_t s_mask, z_mask, sign;
  2144. if (fold_const2(ctx, op) ||
  2145. fold_ix_to_i(ctx, op, 0) ||
  2146. fold_xi_to_x(ctx, op, 0)) {
  2147. return true;
  2148. }
  2149. s_mask = arg_info(op->args[1])->s_mask;
  2150. z_mask = arg_info(op->args[1])->z_mask;
  2151. if (arg_is_const(op->args[2])) {
  2152. int sh = arg_info(op->args[2])->val;
  2153. ctx->z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh);
  2154. s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
  2155. ctx->s_mask = smask_from_smask(s_mask);
  2156. return fold_masks(ctx, op);
  2157. }
  2158. switch (op->opc) {
  2159. CASE_OP_32_64(sar):
  2160. /*
  2161. * Arithmetic right shift will not reduce the number of
  2162. * input sign repetitions.
  2163. */
  2164. ctx->s_mask = s_mask;
  2165. break;
  2166. CASE_OP_32_64(shr):
  2167. /*
  2168. * If the sign bit is known zero, then logical right shift
  2169. * will not reduced the number of input sign repetitions.
  2170. */
  2171. sign = (s_mask & -s_mask) >> 1;
  2172. if (sign && !(z_mask & sign)) {
  2173. ctx->s_mask = s_mask;
  2174. }
  2175. break;
  2176. default:
  2177. break;
  2178. }
  2179. return false;
  2180. }
  2181. static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op)
  2182. {
  2183. TCGOpcode neg_op;
  2184. bool have_neg;
  2185. if (!arg_is_const(op->args[1]) || arg_info(op->args[1])->val != 0) {
  2186. return false;
  2187. }
  2188. switch (ctx->type) {
  2189. case TCG_TYPE_I32:
  2190. neg_op = INDEX_op_neg_i32;
  2191. have_neg = true;
  2192. break;
  2193. case TCG_TYPE_I64:
  2194. neg_op = INDEX_op_neg_i64;
  2195. have_neg = true;
  2196. break;
  2197. case TCG_TYPE_V64:
  2198. case TCG_TYPE_V128:
  2199. case TCG_TYPE_V256:
  2200. neg_op = INDEX_op_neg_vec;
  2201. have_neg = (TCG_TARGET_HAS_neg_vec &&
  2202. tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0);
  2203. break;
  2204. default:
  2205. g_assert_not_reached();
  2206. }
  2207. if (have_neg) {
  2208. op->opc = neg_op;
  2209. op->args[1] = op->args[2];
  2210. return fold_neg_no_const(ctx, op);
  2211. }
  2212. return false;
  2213. }
  2214. /* We cannot as yet do_constant_folding with vectors. */
  2215. static bool fold_sub_vec(OptContext *ctx, TCGOp *op)
  2216. {
  2217. if (fold_xx_to_i(ctx, op, 0) ||
  2218. fold_xi_to_x(ctx, op, 0) ||
  2219. fold_sub_to_neg(ctx, op)) {
  2220. return true;
  2221. }
  2222. return false;
  2223. }
  2224. static bool fold_sub(OptContext *ctx, TCGOp *op)
  2225. {
  2226. if (fold_const2(ctx, op) || fold_sub_vec(ctx, op)) {
  2227. return true;
  2228. }
  2229. /* Fold sub r,x,i to add r,x,-i */
  2230. if (arg_is_const(op->args[2])) {
  2231. uint64_t val = arg_info(op->args[2])->val;
  2232. op->opc = (ctx->type == TCG_TYPE_I32
  2233. ? INDEX_op_add_i32 : INDEX_op_add_i64);
  2234. op->args[2] = arg_new_constant(ctx, -val);
  2235. }
  2236. return false;
  2237. }
  2238. static bool fold_sub2(OptContext *ctx, TCGOp *op)
  2239. {
  2240. return fold_addsub2(ctx, op, false);
  2241. }
  2242. static bool fold_tcg_ld(OptContext *ctx, TCGOp *op)
  2243. {
  2244. /* We can't do any folding with a load, but we can record bits. */
  2245. switch (op->opc) {
  2246. CASE_OP_32_64(ld8s):
  2247. ctx->s_mask = MAKE_64BIT_MASK(8, 56);
  2248. break;
  2249. CASE_OP_32_64(ld8u):
  2250. ctx->z_mask = MAKE_64BIT_MASK(0, 8);
  2251. ctx->s_mask = MAKE_64BIT_MASK(9, 55);
  2252. break;
  2253. CASE_OP_32_64(ld16s):
  2254. ctx->s_mask = MAKE_64BIT_MASK(16, 48);
  2255. break;
  2256. CASE_OP_32_64(ld16u):
  2257. ctx->z_mask = MAKE_64BIT_MASK(0, 16);
  2258. ctx->s_mask = MAKE_64BIT_MASK(17, 47);
  2259. break;
  2260. case INDEX_op_ld32s_i64:
  2261. ctx->s_mask = MAKE_64BIT_MASK(32, 32);
  2262. break;
  2263. case INDEX_op_ld32u_i64:
  2264. ctx->z_mask = MAKE_64BIT_MASK(0, 32);
  2265. ctx->s_mask = MAKE_64BIT_MASK(33, 31);
  2266. break;
  2267. default:
  2268. g_assert_not_reached();
  2269. }
  2270. return false;
  2271. }
  2272. static bool fold_tcg_ld_memcopy(OptContext *ctx, TCGOp *op)
  2273. {
  2274. TCGTemp *dst, *src;
  2275. intptr_t ofs;
  2276. TCGType type;
  2277. if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
  2278. return false;
  2279. }
  2280. type = ctx->type;
  2281. ofs = op->args[2];
  2282. dst = arg_temp(op->args[0]);
  2283. src = find_mem_copy_for(ctx, type, ofs);
  2284. if (src && src->base_type == type) {
  2285. return tcg_opt_gen_mov(ctx, op, temp_arg(dst), temp_arg(src));
  2286. }
  2287. reset_ts(ctx, dst);
  2288. record_mem_copy(ctx, type, dst, ofs, ofs + tcg_type_size(type) - 1);
  2289. return true;
  2290. }
  2291. static bool fold_tcg_st(OptContext *ctx, TCGOp *op)
  2292. {
  2293. intptr_t ofs = op->args[2];
  2294. intptr_t lm1;
  2295. if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
  2296. remove_mem_copy_all(ctx);
  2297. return false;
  2298. }
  2299. switch (op->opc) {
  2300. CASE_OP_32_64(st8):
  2301. lm1 = 0;
  2302. break;
  2303. CASE_OP_32_64(st16):
  2304. lm1 = 1;
  2305. break;
  2306. case INDEX_op_st32_i64:
  2307. case INDEX_op_st_i32:
  2308. lm1 = 3;
  2309. break;
  2310. case INDEX_op_st_i64:
  2311. lm1 = 7;
  2312. break;
  2313. case INDEX_op_st_vec:
  2314. lm1 = tcg_type_size(ctx->type) - 1;
  2315. break;
  2316. default:
  2317. g_assert_not_reached();
  2318. }
  2319. remove_mem_copy_in(ctx, ofs, ofs + lm1);
  2320. return false;
  2321. }
  2322. static bool fold_tcg_st_memcopy(OptContext *ctx, TCGOp *op)
  2323. {
  2324. TCGTemp *src;
  2325. intptr_t ofs, last;
  2326. TCGType type;
  2327. if (op->args[1] != tcgv_ptr_arg(tcg_env)) {
  2328. fold_tcg_st(ctx, op);
  2329. return false;
  2330. }
  2331. src = arg_temp(op->args[0]);
  2332. ofs = op->args[2];
  2333. type = ctx->type;
  2334. /*
  2335. * Eliminate duplicate stores of a constant.
  2336. * This happens frequently when the target ISA zero-extends.
  2337. */
  2338. if (ts_is_const(src)) {
  2339. TCGTemp *prev = find_mem_copy_for(ctx, type, ofs);
  2340. if (src == prev) {
  2341. tcg_op_remove(ctx->tcg, op);
  2342. return true;
  2343. }
  2344. }
  2345. last = ofs + tcg_type_size(type) - 1;
  2346. remove_mem_copy_in(ctx, ofs, last);
  2347. record_mem_copy(ctx, type, src, ofs, last);
  2348. return false;
  2349. }
  2350. static bool fold_xor(OptContext *ctx, TCGOp *op)
  2351. {
  2352. if (fold_const2_commutative(ctx, op) ||
  2353. fold_xx_to_i(ctx, op, 0) ||
  2354. fold_xi_to_x(ctx, op, 0) ||
  2355. fold_xi_to_not(ctx, op, -1)) {
  2356. return true;
  2357. }
  2358. ctx->z_mask = arg_info(op->args[1])->z_mask
  2359. | arg_info(op->args[2])->z_mask;
  2360. ctx->s_mask = arg_info(op->args[1])->s_mask
  2361. & arg_info(op->args[2])->s_mask;
  2362. return fold_masks(ctx, op);
  2363. }
  2364. static bool fold_bitsel_vec(OptContext *ctx, TCGOp *op)
  2365. {
  2366. /* If true and false values are the same, eliminate the cmp. */
  2367. if (args_are_copies(op->args[2], op->args[3])) {
  2368. return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
  2369. }
  2370. if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) {
  2371. uint64_t tv = arg_info(op->args[2])->val;
  2372. uint64_t fv = arg_info(op->args[3])->val;
  2373. if (tv == -1 && fv == 0) {
  2374. return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
  2375. }
  2376. if (tv == 0 && fv == -1) {
  2377. if (TCG_TARGET_HAS_not_vec) {
  2378. op->opc = INDEX_op_not_vec;
  2379. return fold_not(ctx, op);
  2380. } else {
  2381. op->opc = INDEX_op_xor_vec;
  2382. op->args[2] = arg_new_constant(ctx, -1);
  2383. return fold_xor(ctx, op);
  2384. }
  2385. }
  2386. }
  2387. if (arg_is_const(op->args[2])) {
  2388. uint64_t tv = arg_info(op->args[2])->val;
  2389. if (tv == -1) {
  2390. op->opc = INDEX_op_or_vec;
  2391. op->args[2] = op->args[3];
  2392. return fold_or(ctx, op);
  2393. }
  2394. if (tv == 0 && TCG_TARGET_HAS_andc_vec) {
  2395. op->opc = INDEX_op_andc_vec;
  2396. op->args[2] = op->args[1];
  2397. op->args[1] = op->args[3];
  2398. return fold_andc(ctx, op);
  2399. }
  2400. }
  2401. if (arg_is_const(op->args[3])) {
  2402. uint64_t fv = arg_info(op->args[3])->val;
  2403. if (fv == 0) {
  2404. op->opc = INDEX_op_and_vec;
  2405. return fold_and(ctx, op);
  2406. }
  2407. if (fv == -1 && TCG_TARGET_HAS_orc_vec) {
  2408. op->opc = INDEX_op_orc_vec;
  2409. op->args[2] = op->args[1];
  2410. op->args[1] = op->args[3];
  2411. return fold_orc(ctx, op);
  2412. }
  2413. }
  2414. return false;
  2415. }
  2416. /* Propagate constants and copies, fold constant expressions. */
  2417. void tcg_optimize(TCGContext *s)
  2418. {
  2419. int nb_temps, i;
  2420. TCGOp *op, *op_next;
  2421. OptContext ctx = { .tcg = s };
  2422. QSIMPLEQ_INIT(&ctx.mem_free);
  2423. /* Array VALS has an element for each temp.
  2424. If this temp holds a constant then its value is kept in VALS' element.
  2425. If this temp is a copy of other ones then the other copies are
  2426. available through the doubly linked circular list. */
  2427. nb_temps = s->nb_temps;
  2428. for (i = 0; i < nb_temps; ++i) {
  2429. s->temps[i].state_ptr = NULL;
  2430. }
  2431. QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
  2432. TCGOpcode opc = op->opc;
  2433. const TCGOpDef *def;
  2434. bool done = false;
  2435. /* Calls are special. */
  2436. if (opc == INDEX_op_call) {
  2437. fold_call(&ctx, op);
  2438. continue;
  2439. }
  2440. def = &tcg_op_defs[opc];
  2441. init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs);
  2442. copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs);
  2443. /* Pre-compute the type of the operation. */
  2444. if (def->flags & TCG_OPF_VECTOR) {
  2445. ctx.type = TCG_TYPE_V64 + TCGOP_VECL(op);
  2446. } else if (def->flags & TCG_OPF_64BIT) {
  2447. ctx.type = TCG_TYPE_I64;
  2448. } else {
  2449. ctx.type = TCG_TYPE_I32;
  2450. }
  2451. /* Assume all bits affected, no bits known zero, no sign reps. */
  2452. ctx.a_mask = -1;
  2453. ctx.z_mask = -1;
  2454. ctx.s_mask = 0;
  2455. /*
  2456. * Process each opcode.
  2457. * Sorted alphabetically by opcode as much as possible.
  2458. */
  2459. switch (opc) {
  2460. CASE_OP_32_64(add):
  2461. done = fold_add(&ctx, op);
  2462. break;
  2463. case INDEX_op_add_vec:
  2464. done = fold_add_vec(&ctx, op);
  2465. break;
  2466. CASE_OP_32_64(add2):
  2467. done = fold_add2(&ctx, op);
  2468. break;
  2469. CASE_OP_32_64_VEC(and):
  2470. done = fold_and(&ctx, op);
  2471. break;
  2472. CASE_OP_32_64_VEC(andc):
  2473. done = fold_andc(&ctx, op);
  2474. break;
  2475. CASE_OP_32_64(brcond):
  2476. done = fold_brcond(&ctx, op);
  2477. break;
  2478. case INDEX_op_brcond2_i32:
  2479. done = fold_brcond2(&ctx, op);
  2480. break;
  2481. CASE_OP_32_64(bswap16):
  2482. CASE_OP_32_64(bswap32):
  2483. case INDEX_op_bswap64_i64:
  2484. done = fold_bswap(&ctx, op);
  2485. break;
  2486. CASE_OP_32_64(clz):
  2487. CASE_OP_32_64(ctz):
  2488. done = fold_count_zeros(&ctx, op);
  2489. break;
  2490. CASE_OP_32_64(ctpop):
  2491. done = fold_ctpop(&ctx, op);
  2492. break;
  2493. CASE_OP_32_64(deposit):
  2494. done = fold_deposit(&ctx, op);
  2495. break;
  2496. CASE_OP_32_64(div):
  2497. CASE_OP_32_64(divu):
  2498. done = fold_divide(&ctx, op);
  2499. break;
  2500. case INDEX_op_dup_vec:
  2501. done = fold_dup(&ctx, op);
  2502. break;
  2503. case INDEX_op_dup2_vec:
  2504. done = fold_dup2(&ctx, op);
  2505. break;
  2506. CASE_OP_32_64_VEC(eqv):
  2507. done = fold_eqv(&ctx, op);
  2508. break;
  2509. CASE_OP_32_64(extract):
  2510. done = fold_extract(&ctx, op);
  2511. break;
  2512. CASE_OP_32_64(extract2):
  2513. done = fold_extract2(&ctx, op);
  2514. break;
  2515. CASE_OP_32_64(ext8s):
  2516. CASE_OP_32_64(ext16s):
  2517. case INDEX_op_ext32s_i64:
  2518. case INDEX_op_ext_i32_i64:
  2519. done = fold_exts(&ctx, op);
  2520. break;
  2521. CASE_OP_32_64(ext8u):
  2522. CASE_OP_32_64(ext16u):
  2523. case INDEX_op_ext32u_i64:
  2524. case INDEX_op_extu_i32_i64:
  2525. case INDEX_op_extrl_i64_i32:
  2526. case INDEX_op_extrh_i64_i32:
  2527. done = fold_extu(&ctx, op);
  2528. break;
  2529. CASE_OP_32_64(ld8s):
  2530. CASE_OP_32_64(ld8u):
  2531. CASE_OP_32_64(ld16s):
  2532. CASE_OP_32_64(ld16u):
  2533. case INDEX_op_ld32s_i64:
  2534. case INDEX_op_ld32u_i64:
  2535. done = fold_tcg_ld(&ctx, op);
  2536. break;
  2537. case INDEX_op_ld_i32:
  2538. case INDEX_op_ld_i64:
  2539. case INDEX_op_ld_vec:
  2540. done = fold_tcg_ld_memcopy(&ctx, op);
  2541. break;
  2542. CASE_OP_32_64(st8):
  2543. CASE_OP_32_64(st16):
  2544. case INDEX_op_st32_i64:
  2545. done = fold_tcg_st(&ctx, op);
  2546. break;
  2547. case INDEX_op_st_i32:
  2548. case INDEX_op_st_i64:
  2549. case INDEX_op_st_vec:
  2550. done = fold_tcg_st_memcopy(&ctx, op);
  2551. break;
  2552. case INDEX_op_mb:
  2553. done = fold_mb(&ctx, op);
  2554. break;
  2555. CASE_OP_32_64_VEC(mov):
  2556. done = fold_mov(&ctx, op);
  2557. break;
  2558. CASE_OP_32_64(movcond):
  2559. done = fold_movcond(&ctx, op);
  2560. break;
  2561. CASE_OP_32_64(mul):
  2562. done = fold_mul(&ctx, op);
  2563. break;
  2564. CASE_OP_32_64(mulsh):
  2565. CASE_OP_32_64(muluh):
  2566. done = fold_mul_highpart(&ctx, op);
  2567. break;
  2568. CASE_OP_32_64(muls2):
  2569. CASE_OP_32_64(mulu2):
  2570. done = fold_multiply2(&ctx, op);
  2571. break;
  2572. CASE_OP_32_64_VEC(nand):
  2573. done = fold_nand(&ctx, op);
  2574. break;
  2575. CASE_OP_32_64(neg):
  2576. done = fold_neg(&ctx, op);
  2577. break;
  2578. CASE_OP_32_64_VEC(nor):
  2579. done = fold_nor(&ctx, op);
  2580. break;
  2581. CASE_OP_32_64_VEC(not):
  2582. done = fold_not(&ctx, op);
  2583. break;
  2584. CASE_OP_32_64_VEC(or):
  2585. done = fold_or(&ctx, op);
  2586. break;
  2587. CASE_OP_32_64_VEC(orc):
  2588. done = fold_orc(&ctx, op);
  2589. break;
  2590. case INDEX_op_qemu_ld_a32_i32:
  2591. case INDEX_op_qemu_ld_a64_i32:
  2592. case INDEX_op_qemu_ld_a32_i64:
  2593. case INDEX_op_qemu_ld_a64_i64:
  2594. case INDEX_op_qemu_ld_a32_i128:
  2595. case INDEX_op_qemu_ld_a64_i128:
  2596. done = fold_qemu_ld(&ctx, op);
  2597. break;
  2598. case INDEX_op_qemu_st8_a32_i32:
  2599. case INDEX_op_qemu_st8_a64_i32:
  2600. case INDEX_op_qemu_st_a32_i32:
  2601. case INDEX_op_qemu_st_a64_i32:
  2602. case INDEX_op_qemu_st_a32_i64:
  2603. case INDEX_op_qemu_st_a64_i64:
  2604. case INDEX_op_qemu_st_a32_i128:
  2605. case INDEX_op_qemu_st_a64_i128:
  2606. done = fold_qemu_st(&ctx, op);
  2607. break;
  2608. CASE_OP_32_64(rem):
  2609. CASE_OP_32_64(remu):
  2610. done = fold_remainder(&ctx, op);
  2611. break;
  2612. CASE_OP_32_64(rotl):
  2613. CASE_OP_32_64(rotr):
  2614. CASE_OP_32_64(sar):
  2615. CASE_OP_32_64(shl):
  2616. CASE_OP_32_64(shr):
  2617. done = fold_shift(&ctx, op);
  2618. break;
  2619. CASE_OP_32_64(setcond):
  2620. done = fold_setcond(&ctx, op);
  2621. break;
  2622. CASE_OP_32_64(negsetcond):
  2623. done = fold_negsetcond(&ctx, op);
  2624. break;
  2625. case INDEX_op_setcond2_i32:
  2626. done = fold_setcond2(&ctx, op);
  2627. break;
  2628. case INDEX_op_cmp_vec:
  2629. done = fold_cmp_vec(&ctx, op);
  2630. break;
  2631. case INDEX_op_cmpsel_vec:
  2632. done = fold_cmpsel_vec(&ctx, op);
  2633. break;
  2634. case INDEX_op_bitsel_vec:
  2635. done = fold_bitsel_vec(&ctx, op);
  2636. break;
  2637. CASE_OP_32_64(sextract):
  2638. done = fold_sextract(&ctx, op);
  2639. break;
  2640. CASE_OP_32_64(sub):
  2641. done = fold_sub(&ctx, op);
  2642. break;
  2643. case INDEX_op_sub_vec:
  2644. done = fold_sub_vec(&ctx, op);
  2645. break;
  2646. CASE_OP_32_64(sub2):
  2647. done = fold_sub2(&ctx, op);
  2648. break;
  2649. CASE_OP_32_64_VEC(xor):
  2650. done = fold_xor(&ctx, op);
  2651. break;
  2652. default:
  2653. break;
  2654. }
  2655. if (!done) {
  2656. finish_folding(&ctx, op);
  2657. }
  2658. }
  2659. }