tree-predcom.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676
  1. /* Predictive commoning.
  2. Copyright (C) 2005-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This file implements the predictive commoning optimization. Predictive
  16. commoning can be viewed as CSE around a loop, and with some improvements,
  17. as generalized strength reduction-- i.e., reusing values computed in
  18. earlier iterations of a loop in the later ones. So far, the pass only
  19. handles the most useful case, that is, reusing values of memory references.
  20. If you think this is all just a special case of PRE, you are sort of right;
  21. however, concentrating on loops is simpler, and makes it possible to
  22. incorporate data dependence analysis to detect the opportunities, perform
  23. loop unrolling to avoid copies together with renaming immediately,
  24. and if needed, we could also take register pressure into account.
  25. Let us demonstrate what is done on an example:
  26. for (i = 0; i < 100; i++)
  27. {
  28. a[i+2] = a[i] + a[i+1];
  29. b[10] = b[10] + i;
  30. c[i] = c[99 - i];
  31. d[i] = d[i + 1];
  32. }
  33. 1) We find data references in the loop, and split them to mutually
  34. independent groups (i.e., we find components of a data dependence
  35. graph). We ignore read-read dependences whose distance is not constant.
  36. (TODO -- we could also ignore antidependences). In this example, we
  37. find the following groups:
  38. a[i]{read}, a[i+1]{read}, a[i+2]{write}
  39. b[10]{read}, b[10]{write}
  40. c[99 - i]{read}, c[i]{write}
  41. d[i + 1]{read}, d[i]{write}
  42. 2) Inside each of the group, we verify several conditions:
  43. a) all the references must differ in indices only, and the indices
  44. must all have the same step
  45. b) the references must dominate loop latch (and thus, they must be
  46. ordered by dominance relation).
  47. c) the distance of the indices must be a small multiple of the step
  48. We are then able to compute the difference of the references (# of
  49. iterations before they point to the same place as the first of them).
  50. Also, in case there are writes in the loop, we split the groups into
  51. chains whose head is the write whose values are used by the reads in
  52. the same chain. The chains are then processed independently,
  53. making the further transformations simpler. Also, the shorter chains
  54. need the same number of registers, but may require lower unrolling
  55. factor in order to get rid of the copies on the loop latch.
  56. In our example, we get the following chains (the chain for c is invalid).
  57. a[i]{read,+0}, a[i+1]{read,-1}, a[i+2]{write,-2}
  58. b[10]{read,+0}, b[10]{write,+0}
  59. d[i + 1]{read,+0}, d[i]{write,+1}
  60. 3) For each read, we determine the read or write whose value it reuses,
  61. together with the distance of this reuse. I.e. we take the last
  62. reference before it with distance 0, or the last of the references
  63. with the smallest positive distance to the read. Then, we remove
  64. the references that are not used in any of these chains, discard the
  65. empty groups, and propagate all the links so that they point to the
  66. single root reference of the chain (adjusting their distance
  67. appropriately). Some extra care needs to be taken for references with
  68. step 0. In our example (the numbers indicate the distance of the
  69. reuse),
  70. a[i] --> (*) 2, a[i+1] --> (*) 1, a[i+2] (*)
  71. b[10] --> (*) 1, b[10] (*)
  72. 4) The chains are combined together if possible. If the corresponding
  73. elements of two chains are always combined together with the same
  74. operator, we remember just the result of this combination, instead
  75. of remembering the values separately. We may need to perform
  76. reassociation to enable combining, for example
  77. e[i] + f[i+1] + e[i+1] + f[i]
  78. can be reassociated as
  79. (e[i] + f[i]) + (e[i+1] + f[i+1])
  80. and we can combine the chains for e and f into one chain.
  81. 5) For each root reference (end of the chain) R, let N be maximum distance
  82. of a reference reusing its value. Variables R0 up to RN are created,
  83. together with phi nodes that transfer values from R1 .. RN to
  84. R0 .. R(N-1).
  85. Initial values are loaded to R0..R(N-1) (in case not all references
  86. must necessarily be accessed and they may trap, we may fail here;
  87. TODO sometimes, the loads could be guarded by a check for the number
  88. of iterations). Values loaded/stored in roots are also copied to
  89. RN. Other reads are replaced with the appropriate variable Ri.
  90. Everything is put to SSA form.
  91. As a small improvement, if R0 is dead after the root (i.e., all uses of
  92. the value with the maximum distance dominate the root), we can avoid
  93. creating RN and use R0 instead of it.
  94. In our example, we get (only the parts concerning a and b are shown):
  95. for (i = 0; i < 100; i++)
  96. {
  97. f = phi (a[0], s);
  98. s = phi (a[1], f);
  99. x = phi (b[10], x);
  100. f = f + s;
  101. a[i+2] = f;
  102. x = x + i;
  103. b[10] = x;
  104. }
  105. 6) Factor F for unrolling is determined as the smallest common multiple of
  106. (N + 1) for each root reference (N for references for that we avoided
  107. creating RN). If F and the loop is small enough, loop is unrolled F
  108. times. The stores to RN (R0) in the copies of the loop body are
  109. periodically replaced with R0, R1, ... (R1, R2, ...), so that they can
  110. be coalesced and the copies can be eliminated.
  111. TODO -- copy propagation and other optimizations may change the live
  112. ranges of the temporary registers and prevent them from being coalesced;
  113. this may increase the register pressure.
  114. In our case, F = 2 and the (main loop of the) result is
  115. for (i = 0; i < ...; i += 2)
  116. {
  117. f = phi (a[0], f);
  118. s = phi (a[1], s);
  119. x = phi (b[10], x);
  120. f = f + s;
  121. a[i+2] = f;
  122. x = x + i;
  123. b[10] = x;
  124. s = s + f;
  125. a[i+3] = s;
  126. x = x + i;
  127. b[10] = x;
  128. }
  129. TODO -- stores killing other stores can be taken into account, e.g.,
  130. for (i = 0; i < n; i++)
  131. {
  132. a[i] = 1;
  133. a[i+2] = 2;
  134. }
  135. can be replaced with
  136. t0 = a[0];
  137. t1 = a[1];
  138. for (i = 0; i < n; i++)
  139. {
  140. a[i] = 1;
  141. t2 = 2;
  142. t0 = t1;
  143. t1 = t2;
  144. }
  145. a[n] = t0;
  146. a[n+1] = t1;
  147. The interesting part is that this would generalize store motion; still, since
  148. sm is performed elsewhere, it does not seem that important.
  149. Predictive commoning can be generalized for arbitrary computations (not
  150. just memory loads), and also nontrivial transfer functions (e.g., replacing
  151. i * i with ii_last + 2 * i + 1), to generalize strength reduction. */
  152. #include "config.h"
  153. #include "system.h"
  154. #include "coretypes.h"
  155. #include "tm.h"
  156. #include "hash-set.h"
  157. #include "machmode.h"
  158. #include "vec.h"
  159. #include "double-int.h"
  160. #include "input.h"
  161. #include "alias.h"
  162. #include "symtab.h"
  163. #include "wide-int.h"
  164. #include "inchash.h"
  165. #include "tree.h"
  166. #include "fold-const.h"
  167. #include "tm_p.h"
  168. #include "cfgloop.h"
  169. #include "predict.h"
  170. #include "hard-reg-set.h"
  171. #include "function.h"
  172. #include "dominance.h"
  173. #include "cfg.h"
  174. #include "basic-block.h"
  175. #include "tree-ssa-alias.h"
  176. #include "internal-fn.h"
  177. #include "tree-eh.h"
  178. #include "gimple-expr.h"
  179. #include "is-a.h"
  180. #include "gimple.h"
  181. #include "gimplify.h"
  182. #include "gimple-iterator.h"
  183. #include "gimplify-me.h"
  184. #include "gimple-ssa.h"
  185. #include "tree-phinodes.h"
  186. #include "ssa-iterators.h"
  187. #include "stringpool.h"
  188. #include "tree-ssanames.h"
  189. #include "tree-ssa-loop-ivopts.h"
  190. #include "tree-ssa-loop-manip.h"
  191. #include "tree-ssa-loop-niter.h"
  192. #include "tree-ssa-loop.h"
  193. #include "tree-into-ssa.h"
  194. #include "hashtab.h"
  195. #include "rtl.h"
  196. #include "flags.h"
  197. #include "statistics.h"
  198. #include "real.h"
  199. #include "fixed-value.h"
  200. #include "insn-config.h"
  201. #include "expmed.h"
  202. #include "dojump.h"
  203. #include "explow.h"
  204. #include "calls.h"
  205. #include "emit-rtl.h"
  206. #include "varasm.h"
  207. #include "stmt.h"
  208. #include "expr.h"
  209. #include "tree-dfa.h"
  210. #include "tree-ssa.h"
  211. #include "tree-data-ref.h"
  212. #include "tree-scalar-evolution.h"
  213. #include "tree-chrec.h"
  214. #include "params.h"
  215. #include "gimple-pretty-print.h"
  216. #include "tree-pass.h"
  217. #include "tree-affine.h"
  218. #include "tree-inline.h"
  219. #include "wide-int-print.h"
  220. /* The maximum number of iterations between the considered memory
  221. references. */
  222. #define MAX_DISTANCE (target_avail_regs < 16 ? 4 : 8)
  223. /* Data references (or phi nodes that carry data reference values across
  224. loop iterations). */
  225. typedef struct dref_d
  226. {
  227. /* The reference itself. */
  228. struct data_reference *ref;
  229. /* The statement in that the reference appears. */
  230. gimple stmt;
  231. /* In case that STMT is a phi node, this field is set to the SSA name
  232. defined by it in replace_phis_by_defined_names (in order to avoid
  233. pointing to phi node that got reallocated in the meantime). */
  234. tree name_defined_by_phi;
  235. /* Distance of the reference from the root of the chain (in number of
  236. iterations of the loop). */
  237. unsigned distance;
  238. /* Number of iterations offset from the first reference in the component. */
  239. widest_int offset;
  240. /* Number of the reference in a component, in dominance ordering. */
  241. unsigned pos;
  242. /* True if the memory reference is always accessed when the loop is
  243. entered. */
  244. unsigned always_accessed : 1;
  245. } *dref;
  246. /* Type of the chain of the references. */
  247. enum chain_type
  248. {
  249. /* The addresses of the references in the chain are constant. */
  250. CT_INVARIANT,
  251. /* There are only loads in the chain. */
  252. CT_LOAD,
  253. /* Root of the chain is store, the rest are loads. */
  254. CT_STORE_LOAD,
  255. /* A combination of two chains. */
  256. CT_COMBINATION
  257. };
  258. /* Chains of data references. */
  259. typedef struct chain
  260. {
  261. /* Type of the chain. */
  262. enum chain_type type;
  263. /* For combination chains, the operator and the two chains that are
  264. combined, and the type of the result. */
  265. enum tree_code op;
  266. tree rslt_type;
  267. struct chain *ch1, *ch2;
  268. /* The references in the chain. */
  269. vec<dref> refs;
  270. /* The maximum distance of the reference in the chain from the root. */
  271. unsigned length;
  272. /* The variables used to copy the value throughout iterations. */
  273. vec<tree> vars;
  274. /* Initializers for the variables. */
  275. vec<tree> inits;
  276. /* True if there is a use of a variable with the maximal distance
  277. that comes after the root in the loop. */
  278. unsigned has_max_use_after : 1;
  279. /* True if all the memory references in the chain are always accessed. */
  280. unsigned all_always_accessed : 1;
  281. /* True if this chain was combined together with some other chain. */
  282. unsigned combined : 1;
  283. } *chain_p;
  284. /* Describes the knowledge about the step of the memory references in
  285. the component. */
  286. enum ref_step_type
  287. {
  288. /* The step is zero. */
  289. RS_INVARIANT,
  290. /* The step is nonzero. */
  291. RS_NONZERO,
  292. /* The step may or may not be nonzero. */
  293. RS_ANY
  294. };
  295. /* Components of the data dependence graph. */
  296. struct component
  297. {
  298. /* The references in the component. */
  299. vec<dref> refs;
  300. /* What we know about the step of the references in the component. */
  301. enum ref_step_type comp_step;
  302. /* Next component in the list. */
  303. struct component *next;
  304. };
  305. /* Bitmap of ssa names defined by looparound phi nodes covered by chains. */
  306. static bitmap looparound_phis;
  307. /* Cache used by tree_to_aff_combination_expand. */
  308. static hash_map<tree, name_expansion *> *name_expansions;
  309. /* Dumps data reference REF to FILE. */
  310. extern void dump_dref (FILE *, dref);
  311. void
  312. dump_dref (FILE *file, dref ref)
  313. {
  314. if (ref->ref)
  315. {
  316. fprintf (file, " ");
  317. print_generic_expr (file, DR_REF (ref->ref), TDF_SLIM);
  318. fprintf (file, " (id %u%s)\n", ref->pos,
  319. DR_IS_READ (ref->ref) ? "" : ", write");
  320. fprintf (file, " offset ");
  321. print_decs (ref->offset, file);
  322. fprintf (file, "\n");
  323. fprintf (file, " distance %u\n", ref->distance);
  324. }
  325. else
  326. {
  327. if (gimple_code (ref->stmt) == GIMPLE_PHI)
  328. fprintf (file, " looparound ref\n");
  329. else
  330. fprintf (file, " combination ref\n");
  331. fprintf (file, " in statement ");
  332. print_gimple_stmt (file, ref->stmt, 0, TDF_SLIM);
  333. fprintf (file, "\n");
  334. fprintf (file, " distance %u\n", ref->distance);
  335. }
  336. }
  337. /* Dumps CHAIN to FILE. */
  338. extern void dump_chain (FILE *, chain_p);
  339. void
  340. dump_chain (FILE *file, chain_p chain)
  341. {
  342. dref a;
  343. const char *chain_type;
  344. unsigned i;
  345. tree var;
  346. switch (chain->type)
  347. {
  348. case CT_INVARIANT:
  349. chain_type = "Load motion";
  350. break;
  351. case CT_LOAD:
  352. chain_type = "Loads-only";
  353. break;
  354. case CT_STORE_LOAD:
  355. chain_type = "Store-loads";
  356. break;
  357. case CT_COMBINATION:
  358. chain_type = "Combination";
  359. break;
  360. default:
  361. gcc_unreachable ();
  362. }
  363. fprintf (file, "%s chain %p%s\n", chain_type, (void *) chain,
  364. chain->combined ? " (combined)" : "");
  365. if (chain->type != CT_INVARIANT)
  366. fprintf (file, " max distance %u%s\n", chain->length,
  367. chain->has_max_use_after ? "" : ", may reuse first");
  368. if (chain->type == CT_COMBINATION)
  369. {
  370. fprintf (file, " equal to %p %s %p in type ",
  371. (void *) chain->ch1, op_symbol_code (chain->op),
  372. (void *) chain->ch2);
  373. print_generic_expr (file, chain->rslt_type, TDF_SLIM);
  374. fprintf (file, "\n");
  375. }
  376. if (chain->vars.exists ())
  377. {
  378. fprintf (file, " vars");
  379. FOR_EACH_VEC_ELT (chain->vars, i, var)
  380. {
  381. fprintf (file, " ");
  382. print_generic_expr (file, var, TDF_SLIM);
  383. }
  384. fprintf (file, "\n");
  385. }
  386. if (chain->inits.exists ())
  387. {
  388. fprintf (file, " inits");
  389. FOR_EACH_VEC_ELT (chain->inits, i, var)
  390. {
  391. fprintf (file, " ");
  392. print_generic_expr (file, var, TDF_SLIM);
  393. }
  394. fprintf (file, "\n");
  395. }
  396. fprintf (file, " references:\n");
  397. FOR_EACH_VEC_ELT (chain->refs, i, a)
  398. dump_dref (file, a);
  399. fprintf (file, "\n");
  400. }
  401. /* Dumps CHAINS to FILE. */
  402. extern void dump_chains (FILE *, vec<chain_p> );
  403. void
  404. dump_chains (FILE *file, vec<chain_p> chains)
  405. {
  406. chain_p chain;
  407. unsigned i;
  408. FOR_EACH_VEC_ELT (chains, i, chain)
  409. dump_chain (file, chain);
  410. }
  411. /* Dumps COMP to FILE. */
  412. extern void dump_component (FILE *, struct component *);
  413. void
  414. dump_component (FILE *file, struct component *comp)
  415. {
  416. dref a;
  417. unsigned i;
  418. fprintf (file, "Component%s:\n",
  419. comp->comp_step == RS_INVARIANT ? " (invariant)" : "");
  420. FOR_EACH_VEC_ELT (comp->refs, i, a)
  421. dump_dref (file, a);
  422. fprintf (file, "\n");
  423. }
  424. /* Dumps COMPS to FILE. */
  425. extern void dump_components (FILE *, struct component *);
  426. void
  427. dump_components (FILE *file, struct component *comps)
  428. {
  429. struct component *comp;
  430. for (comp = comps; comp; comp = comp->next)
  431. dump_component (file, comp);
  432. }
  433. /* Frees a chain CHAIN. */
  434. static void
  435. release_chain (chain_p chain)
  436. {
  437. dref ref;
  438. unsigned i;
  439. if (chain == NULL)
  440. return;
  441. FOR_EACH_VEC_ELT (chain->refs, i, ref)
  442. free (ref);
  443. chain->refs.release ();
  444. chain->vars.release ();
  445. chain->inits.release ();
  446. free (chain);
  447. }
  448. /* Frees CHAINS. */
  449. static void
  450. release_chains (vec<chain_p> chains)
  451. {
  452. unsigned i;
  453. chain_p chain;
  454. FOR_EACH_VEC_ELT (chains, i, chain)
  455. release_chain (chain);
  456. chains.release ();
  457. }
  458. /* Frees a component COMP. */
  459. static void
  460. release_component (struct component *comp)
  461. {
  462. comp->refs.release ();
  463. free (comp);
  464. }
  465. /* Frees list of components COMPS. */
  466. static void
  467. release_components (struct component *comps)
  468. {
  469. struct component *act, *next;
  470. for (act = comps; act; act = next)
  471. {
  472. next = act->next;
  473. release_component (act);
  474. }
  475. }
  476. /* Finds a root of tree given by FATHERS containing A, and performs path
  477. shortening. */
  478. static unsigned
  479. component_of (unsigned fathers[], unsigned a)
  480. {
  481. unsigned root, n;
  482. for (root = a; root != fathers[root]; root = fathers[root])
  483. continue;
  484. for (; a != root; a = n)
  485. {
  486. n = fathers[a];
  487. fathers[a] = root;
  488. }
  489. return root;
  490. }
  491. /* Join operation for DFU. FATHERS gives the tree, SIZES are sizes of the
  492. components, A and B are components to merge. */
  493. static void
  494. merge_comps (unsigned fathers[], unsigned sizes[], unsigned a, unsigned b)
  495. {
  496. unsigned ca = component_of (fathers, a);
  497. unsigned cb = component_of (fathers, b);
  498. if (ca == cb)
  499. return;
  500. if (sizes[ca] < sizes[cb])
  501. {
  502. sizes[cb] += sizes[ca];
  503. fathers[ca] = cb;
  504. }
  505. else
  506. {
  507. sizes[ca] += sizes[cb];
  508. fathers[cb] = ca;
  509. }
  510. }
  511. /* Returns true if A is a reference that is suitable for predictive commoning
  512. in the innermost loop that contains it. REF_STEP is set according to the
  513. step of the reference A. */
  514. static bool
  515. suitable_reference_p (struct data_reference *a, enum ref_step_type *ref_step)
  516. {
  517. tree ref = DR_REF (a), step = DR_STEP (a);
  518. if (!step
  519. || TREE_THIS_VOLATILE (ref)
  520. || !is_gimple_reg_type (TREE_TYPE (ref))
  521. || tree_could_throw_p (ref))
  522. return false;
  523. if (integer_zerop (step))
  524. *ref_step = RS_INVARIANT;
  525. else if (integer_nonzerop (step))
  526. *ref_step = RS_NONZERO;
  527. else
  528. *ref_step = RS_ANY;
  529. return true;
  530. }
  531. /* Stores DR_OFFSET (DR) + DR_INIT (DR) to OFFSET. */
  532. static void
  533. aff_combination_dr_offset (struct data_reference *dr, aff_tree *offset)
  534. {
  535. tree type = TREE_TYPE (DR_OFFSET (dr));
  536. aff_tree delta;
  537. tree_to_aff_combination_expand (DR_OFFSET (dr), type, offset,
  538. &name_expansions);
  539. aff_combination_const (&delta, type, wi::to_widest (DR_INIT (dr)));
  540. aff_combination_add (offset, &delta);
  541. }
  542. /* Determines number of iterations of the innermost enclosing loop before B
  543. refers to exactly the same location as A and stores it to OFF. If A and
  544. B do not have the same step, they never meet, or anything else fails,
  545. returns false, otherwise returns true. Both A and B are assumed to
  546. satisfy suitable_reference_p. */
  547. static bool
  548. determine_offset (struct data_reference *a, struct data_reference *b,
  549. widest_int *off)
  550. {
  551. aff_tree diff, baseb, step;
  552. tree typea, typeb;
  553. /* Check that both the references access the location in the same type. */
  554. typea = TREE_TYPE (DR_REF (a));
  555. typeb = TREE_TYPE (DR_REF (b));
  556. if (!useless_type_conversion_p (typeb, typea))
  557. return false;
  558. /* Check whether the base address and the step of both references is the
  559. same. */
  560. if (!operand_equal_p (DR_STEP (a), DR_STEP (b), 0)
  561. || !operand_equal_p (DR_BASE_ADDRESS (a), DR_BASE_ADDRESS (b), 0))
  562. return false;
  563. if (integer_zerop (DR_STEP (a)))
  564. {
  565. /* If the references have loop invariant address, check that they access
  566. exactly the same location. */
  567. *off = 0;
  568. return (operand_equal_p (DR_OFFSET (a), DR_OFFSET (b), 0)
  569. && operand_equal_p (DR_INIT (a), DR_INIT (b), 0));
  570. }
  571. /* Compare the offsets of the addresses, and check whether the difference
  572. is a multiple of step. */
  573. aff_combination_dr_offset (a, &diff);
  574. aff_combination_dr_offset (b, &baseb);
  575. aff_combination_scale (&baseb, -1);
  576. aff_combination_add (&diff, &baseb);
  577. tree_to_aff_combination_expand (DR_STEP (a), TREE_TYPE (DR_STEP (a)),
  578. &step, &name_expansions);
  579. return aff_combination_constant_multiple_p (&diff, &step, off);
  580. }
  581. /* Returns the last basic block in LOOP for that we are sure that
  582. it is executed whenever the loop is entered. */
  583. static basic_block
  584. last_always_executed_block (struct loop *loop)
  585. {
  586. unsigned i;
  587. vec<edge> exits = get_loop_exit_edges (loop);
  588. edge ex;
  589. basic_block last = loop->latch;
  590. FOR_EACH_VEC_ELT (exits, i, ex)
  591. last = nearest_common_dominator (CDI_DOMINATORS, last, ex->src);
  592. exits.release ();
  593. return last;
  594. }
  595. /* Splits dependence graph on DATAREFS described by DEPENDS to components. */
  596. static struct component *
  597. split_data_refs_to_components (struct loop *loop,
  598. vec<data_reference_p> datarefs,
  599. vec<ddr_p> depends)
  600. {
  601. unsigned i, n = datarefs.length ();
  602. unsigned ca, ia, ib, bad;
  603. unsigned *comp_father = XNEWVEC (unsigned, n + 1);
  604. unsigned *comp_size = XNEWVEC (unsigned, n + 1);
  605. struct component **comps;
  606. struct data_reference *dr, *dra, *drb;
  607. struct data_dependence_relation *ddr;
  608. struct component *comp_list = NULL, *comp;
  609. dref dataref;
  610. basic_block last_always_executed = last_always_executed_block (loop);
  611. FOR_EACH_VEC_ELT (datarefs, i, dr)
  612. {
  613. if (!DR_REF (dr))
  614. {
  615. /* A fake reference for call or asm_expr that may clobber memory;
  616. just fail. */
  617. goto end;
  618. }
  619. /* predcom pass isn't prepared to handle calls with data references. */
  620. if (is_gimple_call (DR_STMT (dr)))
  621. goto end;
  622. dr->aux = (void *) (size_t) i;
  623. comp_father[i] = i;
  624. comp_size[i] = 1;
  625. }
  626. /* A component reserved for the "bad" data references. */
  627. comp_father[n] = n;
  628. comp_size[n] = 1;
  629. FOR_EACH_VEC_ELT (datarefs, i, dr)
  630. {
  631. enum ref_step_type dummy;
  632. if (!suitable_reference_p (dr, &dummy))
  633. {
  634. ia = (unsigned) (size_t) dr->aux;
  635. merge_comps (comp_father, comp_size, n, ia);
  636. }
  637. }
  638. FOR_EACH_VEC_ELT (depends, i, ddr)
  639. {
  640. widest_int dummy_off;
  641. if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
  642. continue;
  643. dra = DDR_A (ddr);
  644. drb = DDR_B (ddr);
  645. ia = component_of (comp_father, (unsigned) (size_t) dra->aux);
  646. ib = component_of (comp_father, (unsigned) (size_t) drb->aux);
  647. if (ia == ib)
  648. continue;
  649. bad = component_of (comp_father, n);
  650. /* If both A and B are reads, we may ignore unsuitable dependences. */
  651. if (DR_IS_READ (dra) && DR_IS_READ (drb))
  652. {
  653. if (ia == bad || ib == bad
  654. || !determine_offset (dra, drb, &dummy_off))
  655. continue;
  656. }
  657. /* If A is read and B write or vice versa and there is unsuitable
  658. dependence, instead of merging both components into a component
  659. that will certainly not pass suitable_component_p, just put the
  660. read into bad component, perhaps at least the write together with
  661. all the other data refs in it's component will be optimizable. */
  662. else if (DR_IS_READ (dra) && ib != bad)
  663. {
  664. if (ia == bad)
  665. continue;
  666. else if (!determine_offset (dra, drb, &dummy_off))
  667. {
  668. merge_comps (comp_father, comp_size, bad, ia);
  669. continue;
  670. }
  671. }
  672. else if (DR_IS_READ (drb) && ia != bad)
  673. {
  674. if (ib == bad)
  675. continue;
  676. else if (!determine_offset (dra, drb, &dummy_off))
  677. {
  678. merge_comps (comp_father, comp_size, bad, ib);
  679. continue;
  680. }
  681. }
  682. merge_comps (comp_father, comp_size, ia, ib);
  683. }
  684. comps = XCNEWVEC (struct component *, n);
  685. bad = component_of (comp_father, n);
  686. FOR_EACH_VEC_ELT (datarefs, i, dr)
  687. {
  688. ia = (unsigned) (size_t) dr->aux;
  689. ca = component_of (comp_father, ia);
  690. if (ca == bad)
  691. continue;
  692. comp = comps[ca];
  693. if (!comp)
  694. {
  695. comp = XCNEW (struct component);
  696. comp->refs.create (comp_size[ca]);
  697. comps[ca] = comp;
  698. }
  699. dataref = XCNEW (struct dref_d);
  700. dataref->ref = dr;
  701. dataref->stmt = DR_STMT (dr);
  702. dataref->offset = 0;
  703. dataref->distance = 0;
  704. dataref->always_accessed
  705. = dominated_by_p (CDI_DOMINATORS, last_always_executed,
  706. gimple_bb (dataref->stmt));
  707. dataref->pos = comp->refs.length ();
  708. comp->refs.quick_push (dataref);
  709. }
  710. for (i = 0; i < n; i++)
  711. {
  712. comp = comps[i];
  713. if (comp)
  714. {
  715. comp->next = comp_list;
  716. comp_list = comp;
  717. }
  718. }
  719. free (comps);
  720. end:
  721. free (comp_father);
  722. free (comp_size);
  723. return comp_list;
  724. }
  725. /* Returns true if the component COMP satisfies the conditions
  726. described in 2) at the beginning of this file. LOOP is the current
  727. loop. */
  728. static bool
  729. suitable_component_p (struct loop *loop, struct component *comp)
  730. {
  731. unsigned i;
  732. dref a, first;
  733. basic_block ba, bp = loop->header;
  734. bool ok, has_write = false;
  735. FOR_EACH_VEC_ELT (comp->refs, i, a)
  736. {
  737. ba = gimple_bb (a->stmt);
  738. if (!just_once_each_iteration_p (loop, ba))
  739. return false;
  740. gcc_assert (dominated_by_p (CDI_DOMINATORS, ba, bp));
  741. bp = ba;
  742. if (DR_IS_WRITE (a->ref))
  743. has_write = true;
  744. }
  745. first = comp->refs[0];
  746. ok = suitable_reference_p (first->ref, &comp->comp_step);
  747. gcc_assert (ok);
  748. first->offset = 0;
  749. for (i = 1; comp->refs.iterate (i, &a); i++)
  750. {
  751. if (!determine_offset (first->ref, a->ref, &a->offset))
  752. return false;
  753. #ifdef ENABLE_CHECKING
  754. {
  755. enum ref_step_type a_step;
  756. ok = suitable_reference_p (a->ref, &a_step);
  757. gcc_assert (ok && a_step == comp->comp_step);
  758. }
  759. #endif
  760. }
  761. /* If there is a write inside the component, we must know whether the
  762. step is nonzero or not -- we would not otherwise be able to recognize
  763. whether the value accessed by reads comes from the OFFSET-th iteration
  764. or the previous one. */
  765. if (has_write && comp->comp_step == RS_ANY)
  766. return false;
  767. return true;
  768. }
  769. /* Check the conditions on references inside each of components COMPS,
  770. and remove the unsuitable components from the list. The new list
  771. of components is returned. The conditions are described in 2) at
  772. the beginning of this file. LOOP is the current loop. */
  773. static struct component *
  774. filter_suitable_components (struct loop *loop, struct component *comps)
  775. {
  776. struct component **comp, *act;
  777. for (comp = &comps; *comp; )
  778. {
  779. act = *comp;
  780. if (suitable_component_p (loop, act))
  781. comp = &act->next;
  782. else
  783. {
  784. dref ref;
  785. unsigned i;
  786. *comp = act->next;
  787. FOR_EACH_VEC_ELT (act->refs, i, ref)
  788. free (ref);
  789. release_component (act);
  790. }
  791. }
  792. return comps;
  793. }
  794. /* Compares two drefs A and B by their offset and position. Callback for
  795. qsort. */
  796. static int
  797. order_drefs (const void *a, const void *b)
  798. {
  799. const dref *const da = (const dref *) a;
  800. const dref *const db = (const dref *) b;
  801. int offcmp = wi::cmps ((*da)->offset, (*db)->offset);
  802. if (offcmp != 0)
  803. return offcmp;
  804. return (*da)->pos - (*db)->pos;
  805. }
  806. /* Returns root of the CHAIN. */
  807. static inline dref
  808. get_chain_root (chain_p chain)
  809. {
  810. return chain->refs[0];
  811. }
  812. /* Adds REF to the chain CHAIN. */
  813. static void
  814. add_ref_to_chain (chain_p chain, dref ref)
  815. {
  816. dref root = get_chain_root (chain);
  817. gcc_assert (wi::les_p (root->offset, ref->offset));
  818. widest_int dist = ref->offset - root->offset;
  819. if (wi::leu_p (MAX_DISTANCE, dist))
  820. {
  821. free (ref);
  822. return;
  823. }
  824. gcc_assert (wi::fits_uhwi_p (dist));
  825. chain->refs.safe_push (ref);
  826. ref->distance = dist.to_uhwi ();
  827. if (ref->distance >= chain->length)
  828. {
  829. chain->length = ref->distance;
  830. chain->has_max_use_after = false;
  831. }
  832. if (ref->distance == chain->length
  833. && ref->pos > root->pos)
  834. chain->has_max_use_after = true;
  835. chain->all_always_accessed &= ref->always_accessed;
  836. }
  837. /* Returns the chain for invariant component COMP. */
  838. static chain_p
  839. make_invariant_chain (struct component *comp)
  840. {
  841. chain_p chain = XCNEW (struct chain);
  842. unsigned i;
  843. dref ref;
  844. chain->type = CT_INVARIANT;
  845. chain->all_always_accessed = true;
  846. FOR_EACH_VEC_ELT (comp->refs, i, ref)
  847. {
  848. chain->refs.safe_push (ref);
  849. chain->all_always_accessed &= ref->always_accessed;
  850. }
  851. return chain;
  852. }
  853. /* Make a new chain rooted at REF. */
  854. static chain_p
  855. make_rooted_chain (dref ref)
  856. {
  857. chain_p chain = XCNEW (struct chain);
  858. chain->type = DR_IS_READ (ref->ref) ? CT_LOAD : CT_STORE_LOAD;
  859. chain->refs.safe_push (ref);
  860. chain->all_always_accessed = ref->always_accessed;
  861. ref->distance = 0;
  862. return chain;
  863. }
  864. /* Returns true if CHAIN is not trivial. */
  865. static bool
  866. nontrivial_chain_p (chain_p chain)
  867. {
  868. return chain != NULL && chain->refs.length () > 1;
  869. }
  870. /* Returns the ssa name that contains the value of REF, or NULL_TREE if there
  871. is no such name. */
  872. static tree
  873. name_for_ref (dref ref)
  874. {
  875. tree name;
  876. if (is_gimple_assign (ref->stmt))
  877. {
  878. if (!ref->ref || DR_IS_READ (ref->ref))
  879. name = gimple_assign_lhs (ref->stmt);
  880. else
  881. name = gimple_assign_rhs1 (ref->stmt);
  882. }
  883. else
  884. name = PHI_RESULT (ref->stmt);
  885. return (TREE_CODE (name) == SSA_NAME ? name : NULL_TREE);
  886. }
  887. /* Returns true if REF is a valid initializer for ROOT with given DISTANCE (in
  888. iterations of the innermost enclosing loop). */
  889. static bool
  890. valid_initializer_p (struct data_reference *ref,
  891. unsigned distance, struct data_reference *root)
  892. {
  893. aff_tree diff, base, step;
  894. widest_int off;
  895. /* Both REF and ROOT must be accessing the same object. */
  896. if (!operand_equal_p (DR_BASE_ADDRESS (ref), DR_BASE_ADDRESS (root), 0))
  897. return false;
  898. /* The initializer is defined outside of loop, hence its address must be
  899. invariant inside the loop. */
  900. gcc_assert (integer_zerop (DR_STEP (ref)));
  901. /* If the address of the reference is invariant, initializer must access
  902. exactly the same location. */
  903. if (integer_zerop (DR_STEP (root)))
  904. return (operand_equal_p (DR_OFFSET (ref), DR_OFFSET (root), 0)
  905. && operand_equal_p (DR_INIT (ref), DR_INIT (root), 0));
  906. /* Verify that this index of REF is equal to the root's index at
  907. -DISTANCE-th iteration. */
  908. aff_combination_dr_offset (root, &diff);
  909. aff_combination_dr_offset (ref, &base);
  910. aff_combination_scale (&base, -1);
  911. aff_combination_add (&diff, &base);
  912. tree_to_aff_combination_expand (DR_STEP (root), TREE_TYPE (DR_STEP (root)),
  913. &step, &name_expansions);
  914. if (!aff_combination_constant_multiple_p (&diff, &step, &off))
  915. return false;
  916. if (off != distance)
  917. return false;
  918. return true;
  919. }
  920. /* Finds looparound phi node of LOOP that copies the value of REF, and if its
  921. initial value is correct (equal to initial value of REF shifted by one
  922. iteration), returns the phi node. Otherwise, NULL_TREE is returned. ROOT
  923. is the root of the current chain. */
  924. static gphi *
  925. find_looparound_phi (struct loop *loop, dref ref, dref root)
  926. {
  927. tree name, init, init_ref;
  928. gphi *phi = NULL;
  929. gimple init_stmt;
  930. edge latch = loop_latch_edge (loop);
  931. struct data_reference init_dr;
  932. gphi_iterator psi;
  933. if (is_gimple_assign (ref->stmt))
  934. {
  935. if (DR_IS_READ (ref->ref))
  936. name = gimple_assign_lhs (ref->stmt);
  937. else
  938. name = gimple_assign_rhs1 (ref->stmt);
  939. }
  940. else
  941. name = PHI_RESULT (ref->stmt);
  942. if (!name)
  943. return NULL;
  944. for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
  945. {
  946. phi = psi.phi ();
  947. if (PHI_ARG_DEF_FROM_EDGE (phi, latch) == name)
  948. break;
  949. }
  950. if (gsi_end_p (psi))
  951. return NULL;
  952. init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
  953. if (TREE_CODE (init) != SSA_NAME)
  954. return NULL;
  955. init_stmt = SSA_NAME_DEF_STMT (init);
  956. if (gimple_code (init_stmt) != GIMPLE_ASSIGN)
  957. return NULL;
  958. gcc_assert (gimple_assign_lhs (init_stmt) == init);
  959. init_ref = gimple_assign_rhs1 (init_stmt);
  960. if (!REFERENCE_CLASS_P (init_ref)
  961. && !DECL_P (init_ref))
  962. return NULL;
  963. /* Analyze the behavior of INIT_REF with respect to LOOP (innermost
  964. loop enclosing PHI). */
  965. memset (&init_dr, 0, sizeof (struct data_reference));
  966. DR_REF (&init_dr) = init_ref;
  967. DR_STMT (&init_dr) = phi;
  968. if (!dr_analyze_innermost (&init_dr, loop))
  969. return NULL;
  970. if (!valid_initializer_p (&init_dr, ref->distance + 1, root->ref))
  971. return NULL;
  972. return phi;
  973. }
  974. /* Adds a reference for the looparound copy of REF in PHI to CHAIN. */
  975. static void
  976. insert_looparound_copy (chain_p chain, dref ref, gphi *phi)
  977. {
  978. dref nw = XCNEW (struct dref_d), aref;
  979. unsigned i;
  980. nw->stmt = phi;
  981. nw->distance = ref->distance + 1;
  982. nw->always_accessed = 1;
  983. FOR_EACH_VEC_ELT (chain->refs, i, aref)
  984. if (aref->distance >= nw->distance)
  985. break;
  986. chain->refs.safe_insert (i, nw);
  987. if (nw->distance > chain->length)
  988. {
  989. chain->length = nw->distance;
  990. chain->has_max_use_after = false;
  991. }
  992. }
  993. /* For references in CHAIN that are copied around the LOOP (created previously
  994. by PRE, or by user), add the results of such copies to the chain. This
  995. enables us to remove the copies by unrolling, and may need less registers
  996. (also, it may allow us to combine chains together). */
  997. static void
  998. add_looparound_copies (struct loop *loop, chain_p chain)
  999. {
  1000. unsigned i;
  1001. dref ref, root = get_chain_root (chain);
  1002. gphi *phi;
  1003. FOR_EACH_VEC_ELT (chain->refs, i, ref)
  1004. {
  1005. phi = find_looparound_phi (loop, ref, root);
  1006. if (!phi)
  1007. continue;
  1008. bitmap_set_bit (looparound_phis, SSA_NAME_VERSION (PHI_RESULT (phi)));
  1009. insert_looparound_copy (chain, ref, phi);
  1010. }
  1011. }
  1012. /* Find roots of the values and determine distances in the component COMP.
  1013. The references are redistributed into CHAINS. LOOP is the current
  1014. loop. */
  1015. static void
  1016. determine_roots_comp (struct loop *loop,
  1017. struct component *comp,
  1018. vec<chain_p> *chains)
  1019. {
  1020. unsigned i;
  1021. dref a;
  1022. chain_p chain = NULL;
  1023. widest_int last_ofs = 0;
  1024. /* Invariants are handled specially. */
  1025. if (comp->comp_step == RS_INVARIANT)
  1026. {
  1027. chain = make_invariant_chain (comp);
  1028. chains->safe_push (chain);
  1029. return;
  1030. }
  1031. comp->refs.qsort (order_drefs);
  1032. FOR_EACH_VEC_ELT (comp->refs, i, a)
  1033. {
  1034. if (!chain || DR_IS_WRITE (a->ref)
  1035. || wi::leu_p (MAX_DISTANCE, a->offset - last_ofs))
  1036. {
  1037. if (nontrivial_chain_p (chain))
  1038. {
  1039. add_looparound_copies (loop, chain);
  1040. chains->safe_push (chain);
  1041. }
  1042. else
  1043. release_chain (chain);
  1044. chain = make_rooted_chain (a);
  1045. last_ofs = a->offset;
  1046. continue;
  1047. }
  1048. add_ref_to_chain (chain, a);
  1049. }
  1050. if (nontrivial_chain_p (chain))
  1051. {
  1052. add_looparound_copies (loop, chain);
  1053. chains->safe_push (chain);
  1054. }
  1055. else
  1056. release_chain (chain);
  1057. }
  1058. /* Find roots of the values and determine distances in components COMPS, and
  1059. separates the references to CHAINS. LOOP is the current loop. */
  1060. static void
  1061. determine_roots (struct loop *loop,
  1062. struct component *comps, vec<chain_p> *chains)
  1063. {
  1064. struct component *comp;
  1065. for (comp = comps; comp; comp = comp->next)
  1066. determine_roots_comp (loop, comp, chains);
  1067. }
  1068. /* Replace the reference in statement STMT with temporary variable
  1069. NEW_TREE. If SET is true, NEW_TREE is instead initialized to the value of
  1070. the reference in the statement. IN_LHS is true if the reference
  1071. is in the lhs of STMT, false if it is in rhs. */
  1072. static void
  1073. replace_ref_with (gimple stmt, tree new_tree, bool set, bool in_lhs)
  1074. {
  1075. tree val;
  1076. gassign *new_stmt;
  1077. gimple_stmt_iterator bsi, psi;
  1078. if (gimple_code (stmt) == GIMPLE_PHI)
  1079. {
  1080. gcc_assert (!in_lhs && !set);
  1081. val = PHI_RESULT (stmt);
  1082. bsi = gsi_after_labels (gimple_bb (stmt));
  1083. psi = gsi_for_stmt (stmt);
  1084. remove_phi_node (&psi, false);
  1085. /* Turn the phi node into GIMPLE_ASSIGN. */
  1086. new_stmt = gimple_build_assign (val, new_tree);
  1087. gsi_insert_before (&bsi, new_stmt, GSI_NEW_STMT);
  1088. return;
  1089. }
  1090. /* Since the reference is of gimple_reg type, it should only
  1091. appear as lhs or rhs of modify statement. */
  1092. gcc_assert (is_gimple_assign (stmt));
  1093. bsi = gsi_for_stmt (stmt);
  1094. /* If we do not need to initialize NEW_TREE, just replace the use of OLD. */
  1095. if (!set)
  1096. {
  1097. gcc_assert (!in_lhs);
  1098. gimple_assign_set_rhs_from_tree (&bsi, new_tree);
  1099. stmt = gsi_stmt (bsi);
  1100. update_stmt (stmt);
  1101. return;
  1102. }
  1103. if (in_lhs)
  1104. {
  1105. /* We have statement
  1106. OLD = VAL
  1107. If OLD is a memory reference, then VAL is gimple_val, and we transform
  1108. this to
  1109. OLD = VAL
  1110. NEW = VAL
  1111. Otherwise, we are replacing a combination chain,
  1112. VAL is the expression that performs the combination, and OLD is an
  1113. SSA name. In this case, we transform the assignment to
  1114. OLD = VAL
  1115. NEW = OLD
  1116. */
  1117. val = gimple_assign_lhs (stmt);
  1118. if (TREE_CODE (val) != SSA_NAME)
  1119. {
  1120. val = gimple_assign_rhs1 (stmt);
  1121. gcc_assert (gimple_assign_single_p (stmt));
  1122. if (TREE_CLOBBER_P (val))
  1123. val = get_or_create_ssa_default_def (cfun, SSA_NAME_VAR (new_tree));
  1124. else
  1125. gcc_assert (gimple_assign_copy_p (stmt));
  1126. }
  1127. }
  1128. else
  1129. {
  1130. /* VAL = OLD
  1131. is transformed to
  1132. VAL = OLD
  1133. NEW = VAL */
  1134. val = gimple_assign_lhs (stmt);
  1135. }
  1136. new_stmt = gimple_build_assign (new_tree, unshare_expr (val));
  1137. gsi_insert_after (&bsi, new_stmt, GSI_NEW_STMT);
  1138. }
  1139. /* Returns a memory reference to DR in the ITER-th iteration of
  1140. the loop it was analyzed in. Append init stmts to STMTS. */
  1141. static tree
  1142. ref_at_iteration (data_reference_p dr, int iter, gimple_seq *stmts)
  1143. {
  1144. tree off = DR_OFFSET (dr);
  1145. tree coff = DR_INIT (dr);
  1146. if (iter == 0)
  1147. ;
  1148. else if (TREE_CODE (DR_STEP (dr)) == INTEGER_CST)
  1149. coff = size_binop (PLUS_EXPR, coff,
  1150. size_binop (MULT_EXPR, DR_STEP (dr), ssize_int (iter)));
  1151. else
  1152. off = size_binop (PLUS_EXPR, off,
  1153. size_binop (MULT_EXPR, DR_STEP (dr), ssize_int (iter)));
  1154. tree addr = fold_build_pointer_plus (DR_BASE_ADDRESS (dr), off);
  1155. addr = force_gimple_operand_1 (unshare_expr (addr), stmts,
  1156. is_gimple_mem_ref_addr, NULL_TREE);
  1157. tree alias_ptr = fold_convert (reference_alias_ptr_type (DR_REF (dr)), coff);
  1158. /* While data-ref analysis punts on bit offsets it still handles
  1159. bitfield accesses at byte boundaries. Cope with that. Note that
  1160. we cannot simply re-apply the outer COMPONENT_REF because the
  1161. byte-granular portion of it is already applied via DR_INIT and
  1162. DR_OFFSET, so simply build a BIT_FIELD_REF knowing that the bits
  1163. start at offset zero. */
  1164. if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
  1165. && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
  1166. {
  1167. tree field = TREE_OPERAND (DR_REF (dr), 1);
  1168. return build3 (BIT_FIELD_REF, TREE_TYPE (DR_REF (dr)),
  1169. build2 (MEM_REF, DECL_BIT_FIELD_TYPE (field),
  1170. addr, alias_ptr),
  1171. DECL_SIZE (field), bitsize_zero_node);
  1172. }
  1173. else
  1174. return fold_build2 (MEM_REF, TREE_TYPE (DR_REF (dr)), addr, alias_ptr);
  1175. }
  1176. /* Get the initialization expression for the INDEX-th temporary variable
  1177. of CHAIN. */
  1178. static tree
  1179. get_init_expr (chain_p chain, unsigned index)
  1180. {
  1181. if (chain->type == CT_COMBINATION)
  1182. {
  1183. tree e1 = get_init_expr (chain->ch1, index);
  1184. tree e2 = get_init_expr (chain->ch2, index);
  1185. return fold_build2 (chain->op, chain->rslt_type, e1, e2);
  1186. }
  1187. else
  1188. return chain->inits[index];
  1189. }
  1190. /* Returns a new temporary variable used for the I-th variable carrying
  1191. value of REF. The variable's uid is marked in TMP_VARS. */
  1192. static tree
  1193. predcom_tmp_var (tree ref, unsigned i, bitmap tmp_vars)
  1194. {
  1195. tree type = TREE_TYPE (ref);
  1196. /* We never access the components of the temporary variable in predictive
  1197. commoning. */
  1198. tree var = create_tmp_reg (type, get_lsm_tmp_name (ref, i));
  1199. bitmap_set_bit (tmp_vars, DECL_UID (var));
  1200. return var;
  1201. }
  1202. /* Creates the variables for CHAIN, as well as phi nodes for them and
  1203. initialization on entry to LOOP. Uids of the newly created
  1204. temporary variables are marked in TMP_VARS. */
  1205. static void
  1206. initialize_root_vars (struct loop *loop, chain_p chain, bitmap tmp_vars)
  1207. {
  1208. unsigned i;
  1209. unsigned n = chain->length;
  1210. dref root = get_chain_root (chain);
  1211. bool reuse_first = !chain->has_max_use_after;
  1212. tree ref, init, var, next;
  1213. gphi *phi;
  1214. gimple_seq stmts;
  1215. edge entry = loop_preheader_edge (loop), latch = loop_latch_edge (loop);
  1216. /* If N == 0, then all the references are within the single iteration. And
  1217. since this is an nonempty chain, reuse_first cannot be true. */
  1218. gcc_assert (n > 0 || !reuse_first);
  1219. chain->vars.create (n + 1);
  1220. if (chain->type == CT_COMBINATION)
  1221. ref = gimple_assign_lhs (root->stmt);
  1222. else
  1223. ref = DR_REF (root->ref);
  1224. for (i = 0; i < n + (reuse_first ? 0 : 1); i++)
  1225. {
  1226. var = predcom_tmp_var (ref, i, tmp_vars);
  1227. chain->vars.quick_push (var);
  1228. }
  1229. if (reuse_first)
  1230. chain->vars.quick_push (chain->vars[0]);
  1231. FOR_EACH_VEC_ELT (chain->vars, i, var)
  1232. chain->vars[i] = make_ssa_name (var);
  1233. for (i = 0; i < n; i++)
  1234. {
  1235. var = chain->vars[i];
  1236. next = chain->vars[i + 1];
  1237. init = get_init_expr (chain, i);
  1238. init = force_gimple_operand (init, &stmts, true, NULL_TREE);
  1239. if (stmts)
  1240. gsi_insert_seq_on_edge_immediate (entry, stmts);
  1241. phi = create_phi_node (var, loop->header);
  1242. add_phi_arg (phi, init, entry, UNKNOWN_LOCATION);
  1243. add_phi_arg (phi, next, latch, UNKNOWN_LOCATION);
  1244. }
  1245. }
  1246. /* Create the variables and initialization statement for root of chain
  1247. CHAIN. Uids of the newly created temporary variables are marked
  1248. in TMP_VARS. */
  1249. static void
  1250. initialize_root (struct loop *loop, chain_p chain, bitmap tmp_vars)
  1251. {
  1252. dref root = get_chain_root (chain);
  1253. bool in_lhs = (chain->type == CT_STORE_LOAD
  1254. || chain->type == CT_COMBINATION);
  1255. initialize_root_vars (loop, chain, tmp_vars);
  1256. replace_ref_with (root->stmt,
  1257. chain->vars[chain->length],
  1258. true, in_lhs);
  1259. }
  1260. /* Initializes a variable for load motion for ROOT and prepares phi nodes and
  1261. initialization on entry to LOOP if necessary. The ssa name for the variable
  1262. is stored in VARS. If WRITTEN is true, also a phi node to copy its value
  1263. around the loop is created. Uid of the newly created temporary variable
  1264. is marked in TMP_VARS. INITS is the list containing the (single)
  1265. initializer. */
  1266. static void
  1267. initialize_root_vars_lm (struct loop *loop, dref root, bool written,
  1268. vec<tree> *vars, vec<tree> inits,
  1269. bitmap tmp_vars)
  1270. {
  1271. unsigned i;
  1272. tree ref = DR_REF (root->ref), init, var, next;
  1273. gimple_seq stmts;
  1274. gphi *phi;
  1275. edge entry = loop_preheader_edge (loop), latch = loop_latch_edge (loop);
  1276. /* Find the initializer for the variable, and check that it cannot
  1277. trap. */
  1278. init = inits[0];
  1279. vars->create (written ? 2 : 1);
  1280. var = predcom_tmp_var (ref, 0, tmp_vars);
  1281. vars->quick_push (var);
  1282. if (written)
  1283. vars->quick_push ((*vars)[0]);
  1284. FOR_EACH_VEC_ELT (*vars, i, var)
  1285. (*vars)[i] = make_ssa_name (var);
  1286. var = (*vars)[0];
  1287. init = force_gimple_operand (init, &stmts, written, NULL_TREE);
  1288. if (stmts)
  1289. gsi_insert_seq_on_edge_immediate (entry, stmts);
  1290. if (written)
  1291. {
  1292. next = (*vars)[1];
  1293. phi = create_phi_node (var, loop->header);
  1294. add_phi_arg (phi, init, entry, UNKNOWN_LOCATION);
  1295. add_phi_arg (phi, next, latch, UNKNOWN_LOCATION);
  1296. }
  1297. else
  1298. {
  1299. gassign *init_stmt = gimple_build_assign (var, init);
  1300. gsi_insert_on_edge_immediate (entry, init_stmt);
  1301. }
  1302. }
  1303. /* Execute load motion for references in chain CHAIN. Uids of the newly
  1304. created temporary variables are marked in TMP_VARS. */
  1305. static void
  1306. execute_load_motion (struct loop *loop, chain_p chain, bitmap tmp_vars)
  1307. {
  1308. auto_vec<tree> vars;
  1309. dref a;
  1310. unsigned n_writes = 0, ridx, i;
  1311. tree var;
  1312. gcc_assert (chain->type == CT_INVARIANT);
  1313. gcc_assert (!chain->combined);
  1314. FOR_EACH_VEC_ELT (chain->refs, i, a)
  1315. if (DR_IS_WRITE (a->ref))
  1316. n_writes++;
  1317. /* If there are no reads in the loop, there is nothing to do. */
  1318. if (n_writes == chain->refs.length ())
  1319. return;
  1320. initialize_root_vars_lm (loop, get_chain_root (chain), n_writes > 0,
  1321. &vars, chain->inits, tmp_vars);
  1322. ridx = 0;
  1323. FOR_EACH_VEC_ELT (chain->refs, i, a)
  1324. {
  1325. bool is_read = DR_IS_READ (a->ref);
  1326. if (DR_IS_WRITE (a->ref))
  1327. {
  1328. n_writes--;
  1329. if (n_writes)
  1330. {
  1331. var = vars[0];
  1332. var = make_ssa_name (SSA_NAME_VAR (var));
  1333. vars[0] = var;
  1334. }
  1335. else
  1336. ridx = 1;
  1337. }
  1338. replace_ref_with (a->stmt, vars[ridx],
  1339. !is_read, !is_read);
  1340. }
  1341. }
  1342. /* Returns the single statement in that NAME is used, excepting
  1343. the looparound phi nodes contained in one of the chains. If there is no
  1344. such statement, or more statements, NULL is returned. */
  1345. static gimple
  1346. single_nonlooparound_use (tree name)
  1347. {
  1348. use_operand_p use;
  1349. imm_use_iterator it;
  1350. gimple stmt, ret = NULL;
  1351. FOR_EACH_IMM_USE_FAST (use, it, name)
  1352. {
  1353. stmt = USE_STMT (use);
  1354. if (gimple_code (stmt) == GIMPLE_PHI)
  1355. {
  1356. /* Ignore uses in looparound phi nodes. Uses in other phi nodes
  1357. could not be processed anyway, so just fail for them. */
  1358. if (bitmap_bit_p (looparound_phis,
  1359. SSA_NAME_VERSION (PHI_RESULT (stmt))))
  1360. continue;
  1361. return NULL;
  1362. }
  1363. else if (is_gimple_debug (stmt))
  1364. continue;
  1365. else if (ret != NULL)
  1366. return NULL;
  1367. else
  1368. ret = stmt;
  1369. }
  1370. return ret;
  1371. }
  1372. /* Remove statement STMT, as well as the chain of assignments in that it is
  1373. used. */
  1374. static void
  1375. remove_stmt (gimple stmt)
  1376. {
  1377. tree name;
  1378. gimple next;
  1379. gimple_stmt_iterator psi;
  1380. if (gimple_code (stmt) == GIMPLE_PHI)
  1381. {
  1382. name = PHI_RESULT (stmt);
  1383. next = single_nonlooparound_use (name);
  1384. reset_debug_uses (stmt);
  1385. psi = gsi_for_stmt (stmt);
  1386. remove_phi_node (&psi, true);
  1387. if (!next
  1388. || !gimple_assign_ssa_name_copy_p (next)
  1389. || gimple_assign_rhs1 (next) != name)
  1390. return;
  1391. stmt = next;
  1392. }
  1393. while (1)
  1394. {
  1395. gimple_stmt_iterator bsi;
  1396. bsi = gsi_for_stmt (stmt);
  1397. name = gimple_assign_lhs (stmt);
  1398. gcc_assert (TREE_CODE (name) == SSA_NAME);
  1399. next = single_nonlooparound_use (name);
  1400. reset_debug_uses (stmt);
  1401. unlink_stmt_vdef (stmt);
  1402. gsi_remove (&bsi, true);
  1403. release_defs (stmt);
  1404. if (!next
  1405. || !gimple_assign_ssa_name_copy_p (next)
  1406. || gimple_assign_rhs1 (next) != name)
  1407. return;
  1408. stmt = next;
  1409. }
  1410. }
  1411. /* Perform the predictive commoning optimization for a chain CHAIN.
  1412. Uids of the newly created temporary variables are marked in TMP_VARS.*/
  1413. static void
  1414. execute_pred_commoning_chain (struct loop *loop, chain_p chain,
  1415. bitmap tmp_vars)
  1416. {
  1417. unsigned i;
  1418. dref a;
  1419. tree var;
  1420. if (chain->combined)
  1421. {
  1422. /* For combined chains, just remove the statements that are used to
  1423. compute the values of the expression (except for the root one).
  1424. We delay this until after all chains are processed. */
  1425. }
  1426. else
  1427. {
  1428. /* For non-combined chains, set up the variables that hold its value,
  1429. and replace the uses of the original references by these
  1430. variables. */
  1431. initialize_root (loop, chain, tmp_vars);
  1432. for (i = 1; chain->refs.iterate (i, &a); i++)
  1433. {
  1434. var = chain->vars[chain->length - a->distance];
  1435. replace_ref_with (a->stmt, var, false, false);
  1436. }
  1437. }
  1438. }
  1439. /* Determines the unroll factor necessary to remove as many temporary variable
  1440. copies as possible. CHAINS is the list of chains that will be
  1441. optimized. */
  1442. static unsigned
  1443. determine_unroll_factor (vec<chain_p> chains)
  1444. {
  1445. chain_p chain;
  1446. unsigned factor = 1, af, nfactor, i;
  1447. unsigned max = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
  1448. FOR_EACH_VEC_ELT (chains, i, chain)
  1449. {
  1450. if (chain->type == CT_INVARIANT)
  1451. continue;
  1452. if (chain->combined)
  1453. {
  1454. /* For combined chains, we can't handle unrolling if we replace
  1455. looparound PHIs. */
  1456. dref a;
  1457. unsigned j;
  1458. for (j = 1; chain->refs.iterate (j, &a); j++)
  1459. if (gimple_code (a->stmt) == GIMPLE_PHI)
  1460. return 1;
  1461. continue;
  1462. }
  1463. /* The best unroll factor for this chain is equal to the number of
  1464. temporary variables that we create for it. */
  1465. af = chain->length;
  1466. if (chain->has_max_use_after)
  1467. af++;
  1468. nfactor = factor * af / gcd (factor, af);
  1469. if (nfactor <= max)
  1470. factor = nfactor;
  1471. }
  1472. return factor;
  1473. }
  1474. /* Perform the predictive commoning optimization for CHAINS.
  1475. Uids of the newly created temporary variables are marked in TMP_VARS. */
  1476. static void
  1477. execute_pred_commoning (struct loop *loop, vec<chain_p> chains,
  1478. bitmap tmp_vars)
  1479. {
  1480. chain_p chain;
  1481. unsigned i;
  1482. FOR_EACH_VEC_ELT (chains, i, chain)
  1483. {
  1484. if (chain->type == CT_INVARIANT)
  1485. execute_load_motion (loop, chain, tmp_vars);
  1486. else
  1487. execute_pred_commoning_chain (loop, chain, tmp_vars);
  1488. }
  1489. FOR_EACH_VEC_ELT (chains, i, chain)
  1490. {
  1491. if (chain->type == CT_INVARIANT)
  1492. ;
  1493. else if (chain->combined)
  1494. {
  1495. /* For combined chains, just remove the statements that are used to
  1496. compute the values of the expression (except for the root one). */
  1497. dref a;
  1498. unsigned j;
  1499. for (j = 1; chain->refs.iterate (j, &a); j++)
  1500. remove_stmt (a->stmt);
  1501. }
  1502. }
  1503. update_ssa (TODO_update_ssa_only_virtuals);
  1504. }
  1505. /* For each reference in CHAINS, if its defining statement is
  1506. phi node, record the ssa name that is defined by it. */
  1507. static void
  1508. replace_phis_by_defined_names (vec<chain_p> chains)
  1509. {
  1510. chain_p chain;
  1511. dref a;
  1512. unsigned i, j;
  1513. FOR_EACH_VEC_ELT (chains, i, chain)
  1514. FOR_EACH_VEC_ELT (chain->refs, j, a)
  1515. {
  1516. if (gimple_code (a->stmt) == GIMPLE_PHI)
  1517. {
  1518. a->name_defined_by_phi = PHI_RESULT (a->stmt);
  1519. a->stmt = NULL;
  1520. }
  1521. }
  1522. }
  1523. /* For each reference in CHAINS, if name_defined_by_phi is not
  1524. NULL, use it to set the stmt field. */
  1525. static void
  1526. replace_names_by_phis (vec<chain_p> chains)
  1527. {
  1528. chain_p chain;
  1529. dref a;
  1530. unsigned i, j;
  1531. FOR_EACH_VEC_ELT (chains, i, chain)
  1532. FOR_EACH_VEC_ELT (chain->refs, j, a)
  1533. if (a->stmt == NULL)
  1534. {
  1535. a->stmt = SSA_NAME_DEF_STMT (a->name_defined_by_phi);
  1536. gcc_assert (gimple_code (a->stmt) == GIMPLE_PHI);
  1537. a->name_defined_by_phi = NULL_TREE;
  1538. }
  1539. }
  1540. /* Wrapper over execute_pred_commoning, to pass it as a callback
  1541. to tree_transform_and_unroll_loop. */
  1542. struct epcc_data
  1543. {
  1544. vec<chain_p> chains;
  1545. bitmap tmp_vars;
  1546. };
  1547. static void
  1548. execute_pred_commoning_cbck (struct loop *loop, void *data)
  1549. {
  1550. struct epcc_data *const dta = (struct epcc_data *) data;
  1551. /* Restore phi nodes that were replaced by ssa names before
  1552. tree_transform_and_unroll_loop (see detailed description in
  1553. tree_predictive_commoning_loop). */
  1554. replace_names_by_phis (dta->chains);
  1555. execute_pred_commoning (loop, dta->chains, dta->tmp_vars);
  1556. }
  1557. /* Base NAME and all the names in the chain of phi nodes that use it
  1558. on variable VAR. The phi nodes are recognized by being in the copies of
  1559. the header of the LOOP. */
  1560. static void
  1561. base_names_in_chain_on (struct loop *loop, tree name, tree var)
  1562. {
  1563. gimple stmt, phi;
  1564. imm_use_iterator iter;
  1565. replace_ssa_name_symbol (name, var);
  1566. while (1)
  1567. {
  1568. phi = NULL;
  1569. FOR_EACH_IMM_USE_STMT (stmt, iter, name)
  1570. {
  1571. if (gimple_code (stmt) == GIMPLE_PHI
  1572. && flow_bb_inside_loop_p (loop, gimple_bb (stmt)))
  1573. {
  1574. phi = stmt;
  1575. BREAK_FROM_IMM_USE_STMT (iter);
  1576. }
  1577. }
  1578. if (!phi)
  1579. return;
  1580. name = PHI_RESULT (phi);
  1581. replace_ssa_name_symbol (name, var);
  1582. }
  1583. }
  1584. /* Given an unrolled LOOP after predictive commoning, remove the
  1585. register copies arising from phi nodes by changing the base
  1586. variables of SSA names. TMP_VARS is the set of the temporary variables
  1587. for those we want to perform this. */
  1588. static void
  1589. eliminate_temp_copies (struct loop *loop, bitmap tmp_vars)
  1590. {
  1591. edge e;
  1592. gphi *phi;
  1593. gimple stmt;
  1594. tree name, use, var;
  1595. gphi_iterator psi;
  1596. e = loop_latch_edge (loop);
  1597. for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
  1598. {
  1599. phi = psi.phi ();
  1600. name = PHI_RESULT (phi);
  1601. var = SSA_NAME_VAR (name);
  1602. if (!var || !bitmap_bit_p (tmp_vars, DECL_UID (var)))
  1603. continue;
  1604. use = PHI_ARG_DEF_FROM_EDGE (phi, e);
  1605. gcc_assert (TREE_CODE (use) == SSA_NAME);
  1606. /* Base all the ssa names in the ud and du chain of NAME on VAR. */
  1607. stmt = SSA_NAME_DEF_STMT (use);
  1608. while (gimple_code (stmt) == GIMPLE_PHI
  1609. /* In case we could not unroll the loop enough to eliminate
  1610. all copies, we may reach the loop header before the defining
  1611. statement (in that case, some register copies will be present
  1612. in loop latch in the final code, corresponding to the newly
  1613. created looparound phi nodes). */
  1614. && gimple_bb (stmt) != loop->header)
  1615. {
  1616. gcc_assert (single_pred_p (gimple_bb (stmt)));
  1617. use = PHI_ARG_DEF (stmt, 0);
  1618. stmt = SSA_NAME_DEF_STMT (use);
  1619. }
  1620. base_names_in_chain_on (loop, use, var);
  1621. }
  1622. }
  1623. /* Returns true if CHAIN is suitable to be combined. */
  1624. static bool
  1625. chain_can_be_combined_p (chain_p chain)
  1626. {
  1627. return (!chain->combined
  1628. && (chain->type == CT_LOAD || chain->type == CT_COMBINATION));
  1629. }
  1630. /* Returns the modify statement that uses NAME. Skips over assignment
  1631. statements, NAME is replaced with the actual name used in the returned
  1632. statement. */
  1633. static gimple
  1634. find_use_stmt (tree *name)
  1635. {
  1636. gimple stmt;
  1637. tree rhs, lhs;
  1638. /* Skip over assignments. */
  1639. while (1)
  1640. {
  1641. stmt = single_nonlooparound_use (*name);
  1642. if (!stmt)
  1643. return NULL;
  1644. if (gimple_code (stmt) != GIMPLE_ASSIGN)
  1645. return NULL;
  1646. lhs = gimple_assign_lhs (stmt);
  1647. if (TREE_CODE (lhs) != SSA_NAME)
  1648. return NULL;
  1649. if (gimple_assign_copy_p (stmt))
  1650. {
  1651. rhs = gimple_assign_rhs1 (stmt);
  1652. if (rhs != *name)
  1653. return NULL;
  1654. *name = lhs;
  1655. }
  1656. else if (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
  1657. == GIMPLE_BINARY_RHS)
  1658. return stmt;
  1659. else
  1660. return NULL;
  1661. }
  1662. }
  1663. /* Returns true if we may perform reassociation for operation CODE in TYPE. */
  1664. static bool
  1665. may_reassociate_p (tree type, enum tree_code code)
  1666. {
  1667. if (FLOAT_TYPE_P (type)
  1668. && !flag_unsafe_math_optimizations)
  1669. return false;
  1670. return (commutative_tree_code (code)
  1671. && associative_tree_code (code));
  1672. }
  1673. /* If the operation used in STMT is associative and commutative, go through the
  1674. tree of the same operations and returns its root. Distance to the root
  1675. is stored in DISTANCE. */
  1676. static gimple
  1677. find_associative_operation_root (gimple stmt, unsigned *distance)
  1678. {
  1679. tree lhs;
  1680. gimple next;
  1681. enum tree_code code = gimple_assign_rhs_code (stmt);
  1682. tree type = TREE_TYPE (gimple_assign_lhs (stmt));
  1683. unsigned dist = 0;
  1684. if (!may_reassociate_p (type, code))
  1685. return NULL;
  1686. while (1)
  1687. {
  1688. lhs = gimple_assign_lhs (stmt);
  1689. gcc_assert (TREE_CODE (lhs) == SSA_NAME);
  1690. next = find_use_stmt (&lhs);
  1691. if (!next
  1692. || gimple_assign_rhs_code (next) != code)
  1693. break;
  1694. stmt = next;
  1695. dist++;
  1696. }
  1697. if (distance)
  1698. *distance = dist;
  1699. return stmt;
  1700. }
  1701. /* Returns the common statement in that NAME1 and NAME2 have a use. If there
  1702. is no such statement, returns NULL_TREE. In case the operation used on
  1703. NAME1 and NAME2 is associative and commutative, returns the root of the
  1704. tree formed by this operation instead of the statement that uses NAME1 or
  1705. NAME2. */
  1706. static gimple
  1707. find_common_use_stmt (tree *name1, tree *name2)
  1708. {
  1709. gimple stmt1, stmt2;
  1710. stmt1 = find_use_stmt (name1);
  1711. if (!stmt1)
  1712. return NULL;
  1713. stmt2 = find_use_stmt (name2);
  1714. if (!stmt2)
  1715. return NULL;
  1716. if (stmt1 == stmt2)
  1717. return stmt1;
  1718. stmt1 = find_associative_operation_root (stmt1, NULL);
  1719. if (!stmt1)
  1720. return NULL;
  1721. stmt2 = find_associative_operation_root (stmt2, NULL);
  1722. if (!stmt2)
  1723. return NULL;
  1724. return (stmt1 == stmt2 ? stmt1 : NULL);
  1725. }
  1726. /* Checks whether R1 and R2 are combined together using CODE, with the result
  1727. in RSLT_TYPE, in order R1 CODE R2 if SWAP is false and in order R2 CODE R1
  1728. if it is true. If CODE is ERROR_MARK, set these values instead. */
  1729. static bool
  1730. combinable_refs_p (dref r1, dref r2,
  1731. enum tree_code *code, bool *swap, tree *rslt_type)
  1732. {
  1733. enum tree_code acode;
  1734. bool aswap;
  1735. tree atype;
  1736. tree name1, name2;
  1737. gimple stmt;
  1738. name1 = name_for_ref (r1);
  1739. name2 = name_for_ref (r2);
  1740. gcc_assert (name1 != NULL_TREE && name2 != NULL_TREE);
  1741. stmt = find_common_use_stmt (&name1, &name2);
  1742. if (!stmt
  1743. /* A simple post-dominance check - make sure the combination
  1744. is executed under the same condition as the references. */
  1745. || (gimple_bb (stmt) != gimple_bb (r1->stmt)
  1746. && gimple_bb (stmt) != gimple_bb (r2->stmt)))
  1747. return false;
  1748. acode = gimple_assign_rhs_code (stmt);
  1749. aswap = (!commutative_tree_code (acode)
  1750. && gimple_assign_rhs1 (stmt) != name1);
  1751. atype = TREE_TYPE (gimple_assign_lhs (stmt));
  1752. if (*code == ERROR_MARK)
  1753. {
  1754. *code = acode;
  1755. *swap = aswap;
  1756. *rslt_type = atype;
  1757. return true;
  1758. }
  1759. return (*code == acode
  1760. && *swap == aswap
  1761. && *rslt_type == atype);
  1762. }
  1763. /* Remove OP from the operation on rhs of STMT, and replace STMT with
  1764. an assignment of the remaining operand. */
  1765. static void
  1766. remove_name_from_operation (gimple stmt, tree op)
  1767. {
  1768. tree other_op;
  1769. gimple_stmt_iterator si;
  1770. gcc_assert (is_gimple_assign (stmt));
  1771. if (gimple_assign_rhs1 (stmt) == op)
  1772. other_op = gimple_assign_rhs2 (stmt);
  1773. else
  1774. other_op = gimple_assign_rhs1 (stmt);
  1775. si = gsi_for_stmt (stmt);
  1776. gimple_assign_set_rhs_from_tree (&si, other_op);
  1777. /* We should not have reallocated STMT. */
  1778. gcc_assert (gsi_stmt (si) == stmt);
  1779. update_stmt (stmt);
  1780. }
  1781. /* Reassociates the expression in that NAME1 and NAME2 are used so that they
  1782. are combined in a single statement, and returns this statement. */
  1783. static gimple
  1784. reassociate_to_the_same_stmt (tree name1, tree name2)
  1785. {
  1786. gimple stmt1, stmt2, root1, root2, s1, s2;
  1787. gassign *new_stmt, *tmp_stmt;
  1788. tree new_name, tmp_name, var, r1, r2;
  1789. unsigned dist1, dist2;
  1790. enum tree_code code;
  1791. tree type = TREE_TYPE (name1);
  1792. gimple_stmt_iterator bsi;
  1793. stmt1 = find_use_stmt (&name1);
  1794. stmt2 = find_use_stmt (&name2);
  1795. root1 = find_associative_operation_root (stmt1, &dist1);
  1796. root2 = find_associative_operation_root (stmt2, &dist2);
  1797. code = gimple_assign_rhs_code (stmt1);
  1798. gcc_assert (root1 && root2 && root1 == root2
  1799. && code == gimple_assign_rhs_code (stmt2));
  1800. /* Find the root of the nearest expression in that both NAME1 and NAME2
  1801. are used. */
  1802. r1 = name1;
  1803. s1 = stmt1;
  1804. r2 = name2;
  1805. s2 = stmt2;
  1806. while (dist1 > dist2)
  1807. {
  1808. s1 = find_use_stmt (&r1);
  1809. r1 = gimple_assign_lhs (s1);
  1810. dist1--;
  1811. }
  1812. while (dist2 > dist1)
  1813. {
  1814. s2 = find_use_stmt (&r2);
  1815. r2 = gimple_assign_lhs (s2);
  1816. dist2--;
  1817. }
  1818. while (s1 != s2)
  1819. {
  1820. s1 = find_use_stmt (&r1);
  1821. r1 = gimple_assign_lhs (s1);
  1822. s2 = find_use_stmt (&r2);
  1823. r2 = gimple_assign_lhs (s2);
  1824. }
  1825. /* Remove NAME1 and NAME2 from the statements in that they are used
  1826. currently. */
  1827. remove_name_from_operation (stmt1, name1);
  1828. remove_name_from_operation (stmt2, name2);
  1829. /* Insert the new statement combining NAME1 and NAME2 before S1, and
  1830. combine it with the rhs of S1. */
  1831. var = create_tmp_reg (type, "predreastmp");
  1832. new_name = make_ssa_name (var);
  1833. new_stmt = gimple_build_assign (new_name, code, name1, name2);
  1834. var = create_tmp_reg (type, "predreastmp");
  1835. tmp_name = make_ssa_name (var);
  1836. /* Rhs of S1 may now be either a binary expression with operation
  1837. CODE, or gimple_val (in case that stmt1 == s1 or stmt2 == s1,
  1838. so that name1 or name2 was removed from it). */
  1839. tmp_stmt = gimple_build_assign (tmp_name, gimple_assign_rhs_code (s1),
  1840. gimple_assign_rhs1 (s1),
  1841. gimple_assign_rhs2 (s1));
  1842. bsi = gsi_for_stmt (s1);
  1843. gimple_assign_set_rhs_with_ops (&bsi, code, new_name, tmp_name);
  1844. s1 = gsi_stmt (bsi);
  1845. update_stmt (s1);
  1846. gsi_insert_before (&bsi, new_stmt, GSI_SAME_STMT);
  1847. gsi_insert_before (&bsi, tmp_stmt, GSI_SAME_STMT);
  1848. return new_stmt;
  1849. }
  1850. /* Returns the statement that combines references R1 and R2. In case R1
  1851. and R2 are not used in the same statement, but they are used with an
  1852. associative and commutative operation in the same expression, reassociate
  1853. the expression so that they are used in the same statement. */
  1854. static gimple
  1855. stmt_combining_refs (dref r1, dref r2)
  1856. {
  1857. gimple stmt1, stmt2;
  1858. tree name1 = name_for_ref (r1);
  1859. tree name2 = name_for_ref (r2);
  1860. stmt1 = find_use_stmt (&name1);
  1861. stmt2 = find_use_stmt (&name2);
  1862. if (stmt1 == stmt2)
  1863. return stmt1;
  1864. return reassociate_to_the_same_stmt (name1, name2);
  1865. }
  1866. /* Tries to combine chains CH1 and CH2 together. If this succeeds, the
  1867. description of the new chain is returned, otherwise we return NULL. */
  1868. static chain_p
  1869. combine_chains (chain_p ch1, chain_p ch2)
  1870. {
  1871. dref r1, r2, nw;
  1872. enum tree_code op = ERROR_MARK;
  1873. bool swap = false;
  1874. chain_p new_chain;
  1875. unsigned i;
  1876. gimple root_stmt;
  1877. tree rslt_type = NULL_TREE;
  1878. if (ch1 == ch2)
  1879. return NULL;
  1880. if (ch1->length != ch2->length)
  1881. return NULL;
  1882. if (ch1->refs.length () != ch2->refs.length ())
  1883. return NULL;
  1884. for (i = 0; (ch1->refs.iterate (i, &r1)
  1885. && ch2->refs.iterate (i, &r2)); i++)
  1886. {
  1887. if (r1->distance != r2->distance)
  1888. return NULL;
  1889. if (!combinable_refs_p (r1, r2, &op, &swap, &rslt_type))
  1890. return NULL;
  1891. }
  1892. if (swap)
  1893. {
  1894. chain_p tmp = ch1;
  1895. ch1 = ch2;
  1896. ch2 = tmp;
  1897. }
  1898. new_chain = XCNEW (struct chain);
  1899. new_chain->type = CT_COMBINATION;
  1900. new_chain->op = op;
  1901. new_chain->ch1 = ch1;
  1902. new_chain->ch2 = ch2;
  1903. new_chain->rslt_type = rslt_type;
  1904. new_chain->length = ch1->length;
  1905. for (i = 0; (ch1->refs.iterate (i, &r1)
  1906. && ch2->refs.iterate (i, &r2)); i++)
  1907. {
  1908. nw = XCNEW (struct dref_d);
  1909. nw->stmt = stmt_combining_refs (r1, r2);
  1910. nw->distance = r1->distance;
  1911. new_chain->refs.safe_push (nw);
  1912. }
  1913. new_chain->has_max_use_after = false;
  1914. root_stmt = get_chain_root (new_chain)->stmt;
  1915. for (i = 1; new_chain->refs.iterate (i, &nw); i++)
  1916. {
  1917. if (nw->distance == new_chain->length
  1918. && !stmt_dominates_stmt_p (nw->stmt, root_stmt))
  1919. {
  1920. new_chain->has_max_use_after = true;
  1921. break;
  1922. }
  1923. }
  1924. ch1->combined = true;
  1925. ch2->combined = true;
  1926. return new_chain;
  1927. }
  1928. /* Try to combine the CHAINS. */
  1929. static void
  1930. try_combine_chains (vec<chain_p> *chains)
  1931. {
  1932. unsigned i, j;
  1933. chain_p ch1, ch2, cch;
  1934. auto_vec<chain_p> worklist;
  1935. FOR_EACH_VEC_ELT (*chains, i, ch1)
  1936. if (chain_can_be_combined_p (ch1))
  1937. worklist.safe_push (ch1);
  1938. while (!worklist.is_empty ())
  1939. {
  1940. ch1 = worklist.pop ();
  1941. if (!chain_can_be_combined_p (ch1))
  1942. continue;
  1943. FOR_EACH_VEC_ELT (*chains, j, ch2)
  1944. {
  1945. if (!chain_can_be_combined_p (ch2))
  1946. continue;
  1947. cch = combine_chains (ch1, ch2);
  1948. if (cch)
  1949. {
  1950. worklist.safe_push (cch);
  1951. chains->safe_push (cch);
  1952. break;
  1953. }
  1954. }
  1955. }
  1956. }
  1957. /* Prepare initializers for CHAIN in LOOP. Returns false if this is
  1958. impossible because one of these initializers may trap, true otherwise. */
  1959. static bool
  1960. prepare_initializers_chain (struct loop *loop, chain_p chain)
  1961. {
  1962. unsigned i, n = (chain->type == CT_INVARIANT) ? 1 : chain->length;
  1963. struct data_reference *dr = get_chain_root (chain)->ref;
  1964. tree init;
  1965. dref laref;
  1966. edge entry = loop_preheader_edge (loop);
  1967. /* Find the initializers for the variables, and check that they cannot
  1968. trap. */
  1969. chain->inits.create (n);
  1970. for (i = 0; i < n; i++)
  1971. chain->inits.quick_push (NULL_TREE);
  1972. /* If we have replaced some looparound phi nodes, use their initializers
  1973. instead of creating our own. */
  1974. FOR_EACH_VEC_ELT (chain->refs, i, laref)
  1975. {
  1976. if (gimple_code (laref->stmt) != GIMPLE_PHI)
  1977. continue;
  1978. gcc_assert (laref->distance > 0);
  1979. chain->inits[n - laref->distance]
  1980. = PHI_ARG_DEF_FROM_EDGE (laref->stmt, entry);
  1981. }
  1982. for (i = 0; i < n; i++)
  1983. {
  1984. gimple_seq stmts = NULL;
  1985. if (chain->inits[i] != NULL_TREE)
  1986. continue;
  1987. init = ref_at_iteration (dr, (int) i - n, &stmts);
  1988. if (!chain->all_always_accessed && tree_could_trap_p (init))
  1989. {
  1990. gimple_seq_discard (stmts);
  1991. return false;
  1992. }
  1993. if (stmts)
  1994. gsi_insert_seq_on_edge_immediate (entry, stmts);
  1995. chain->inits[i] = init;
  1996. }
  1997. return true;
  1998. }
  1999. /* Prepare initializers for CHAINS in LOOP, and free chains that cannot
  2000. be used because the initializers might trap. */
  2001. static void
  2002. prepare_initializers (struct loop *loop, vec<chain_p> chains)
  2003. {
  2004. chain_p chain;
  2005. unsigned i;
  2006. for (i = 0; i < chains.length (); )
  2007. {
  2008. chain = chains[i];
  2009. if (prepare_initializers_chain (loop, chain))
  2010. i++;
  2011. else
  2012. {
  2013. release_chain (chain);
  2014. chains.unordered_remove (i);
  2015. }
  2016. }
  2017. }
  2018. /* Performs predictive commoning for LOOP. Returns true if LOOP was
  2019. unrolled. */
  2020. static bool
  2021. tree_predictive_commoning_loop (struct loop *loop)
  2022. {
  2023. vec<data_reference_p> datarefs;
  2024. vec<ddr_p> dependences;
  2025. struct component *components;
  2026. vec<chain_p> chains = vNULL;
  2027. unsigned unroll_factor;
  2028. struct tree_niter_desc desc;
  2029. bool unroll = false;
  2030. edge exit;
  2031. bitmap tmp_vars;
  2032. if (dump_file && (dump_flags & TDF_DETAILS))
  2033. fprintf (dump_file, "Processing loop %d\n", loop->num);
  2034. /* Find the data references and split them into components according to their
  2035. dependence relations. */
  2036. auto_vec<loop_p, 3> loop_nest;
  2037. dependences.create (10);
  2038. datarefs.create (10);
  2039. if (! compute_data_dependences_for_loop (loop, true, &loop_nest, &datarefs,
  2040. &dependences))
  2041. {
  2042. if (dump_file && (dump_flags & TDF_DETAILS))
  2043. fprintf (dump_file, "Cannot analyze data dependencies\n");
  2044. free_data_refs (datarefs);
  2045. free_dependence_relations (dependences);
  2046. return false;
  2047. }
  2048. if (dump_file && (dump_flags & TDF_DETAILS))
  2049. dump_data_dependence_relations (dump_file, dependences);
  2050. components = split_data_refs_to_components (loop, datarefs, dependences);
  2051. loop_nest.release ();
  2052. free_dependence_relations (dependences);
  2053. if (!components)
  2054. {
  2055. free_data_refs (datarefs);
  2056. free_affine_expand_cache (&name_expansions);
  2057. return false;
  2058. }
  2059. if (dump_file && (dump_flags & TDF_DETAILS))
  2060. {
  2061. fprintf (dump_file, "Initial state:\n\n");
  2062. dump_components (dump_file, components);
  2063. }
  2064. /* Find the suitable components and split them into chains. */
  2065. components = filter_suitable_components (loop, components);
  2066. tmp_vars = BITMAP_ALLOC (NULL);
  2067. looparound_phis = BITMAP_ALLOC (NULL);
  2068. determine_roots (loop, components, &chains);
  2069. release_components (components);
  2070. if (!chains.exists ())
  2071. {
  2072. if (dump_file && (dump_flags & TDF_DETAILS))
  2073. fprintf (dump_file,
  2074. "Predictive commoning failed: no suitable chains\n");
  2075. goto end;
  2076. }
  2077. prepare_initializers (loop, chains);
  2078. /* Try to combine the chains that are always worked with together. */
  2079. try_combine_chains (&chains);
  2080. if (dump_file && (dump_flags & TDF_DETAILS))
  2081. {
  2082. fprintf (dump_file, "Before commoning:\n\n");
  2083. dump_chains (dump_file, chains);
  2084. }
  2085. /* Determine the unroll factor, and if the loop should be unrolled, ensure
  2086. that its number of iterations is divisible by the factor. */
  2087. unroll_factor = determine_unroll_factor (chains);
  2088. scev_reset ();
  2089. unroll = (unroll_factor > 1
  2090. && can_unroll_loop_p (loop, unroll_factor, &desc));
  2091. exit = single_dom_exit (loop);
  2092. /* Execute the predictive commoning transformations, and possibly unroll the
  2093. loop. */
  2094. if (unroll)
  2095. {
  2096. struct epcc_data dta;
  2097. if (dump_file && (dump_flags & TDF_DETAILS))
  2098. fprintf (dump_file, "Unrolling %u times.\n", unroll_factor);
  2099. dta.chains = chains;
  2100. dta.tmp_vars = tmp_vars;
  2101. update_ssa (TODO_update_ssa_only_virtuals);
  2102. /* Cfg manipulations performed in tree_transform_and_unroll_loop before
  2103. execute_pred_commoning_cbck is called may cause phi nodes to be
  2104. reallocated, which is a problem since CHAINS may point to these
  2105. statements. To fix this, we store the ssa names defined by the
  2106. phi nodes here instead of the phi nodes themselves, and restore
  2107. the phi nodes in execute_pred_commoning_cbck. A bit hacky. */
  2108. replace_phis_by_defined_names (chains);
  2109. tree_transform_and_unroll_loop (loop, unroll_factor, exit, &desc,
  2110. execute_pred_commoning_cbck, &dta);
  2111. eliminate_temp_copies (loop, tmp_vars);
  2112. }
  2113. else
  2114. {
  2115. if (dump_file && (dump_flags & TDF_DETAILS))
  2116. fprintf (dump_file,
  2117. "Executing predictive commoning without unrolling.\n");
  2118. execute_pred_commoning (loop, chains, tmp_vars);
  2119. }
  2120. end: ;
  2121. release_chains (chains);
  2122. free_data_refs (datarefs);
  2123. BITMAP_FREE (tmp_vars);
  2124. BITMAP_FREE (looparound_phis);
  2125. free_affine_expand_cache (&name_expansions);
  2126. return unroll;
  2127. }
  2128. /* Runs predictive commoning. */
  2129. unsigned
  2130. tree_predictive_commoning (void)
  2131. {
  2132. bool unrolled = false;
  2133. struct loop *loop;
  2134. unsigned ret = 0;
  2135. initialize_original_copy_tables ();
  2136. FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
  2137. if (optimize_loop_for_speed_p (loop))
  2138. {
  2139. unrolled |= tree_predictive_commoning_loop (loop);
  2140. }
  2141. if (unrolled)
  2142. {
  2143. scev_reset ();
  2144. ret = TODO_cleanup_cfg;
  2145. }
  2146. free_original_copy_tables ();
  2147. return ret;
  2148. }
  2149. /* Predictive commoning Pass. */
  2150. static unsigned
  2151. run_tree_predictive_commoning (struct function *fun)
  2152. {
  2153. if (number_of_loops (fun) <= 1)
  2154. return 0;
  2155. return tree_predictive_commoning ();
  2156. }
  2157. namespace {
  2158. const pass_data pass_data_predcom =
  2159. {
  2160. GIMPLE_PASS, /* type */
  2161. "pcom", /* name */
  2162. OPTGROUP_LOOP, /* optinfo_flags */
  2163. TV_PREDCOM, /* tv_id */
  2164. PROP_cfg, /* properties_required */
  2165. 0, /* properties_provided */
  2166. 0, /* properties_destroyed */
  2167. 0, /* todo_flags_start */
  2168. TODO_update_ssa_only_virtuals, /* todo_flags_finish */
  2169. };
  2170. class pass_predcom : public gimple_opt_pass
  2171. {
  2172. public:
  2173. pass_predcom (gcc::context *ctxt)
  2174. : gimple_opt_pass (pass_data_predcom, ctxt)
  2175. {}
  2176. /* opt_pass methods: */
  2177. virtual bool gate (function *) { return flag_predictive_commoning != 0; }
  2178. virtual unsigned int execute (function *fun)
  2179. {
  2180. return run_tree_predictive_commoning (fun);
  2181. }
  2182. }; // class pass_predcom
  2183. } // anon namespace
  2184. gimple_opt_pass *
  2185. make_pass_predcom (gcc::context *ctxt)
  2186. {
  2187. return new pass_predcom (ctxt);
  2188. }