loop-iv.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137
  1. /* Rtl-level induction variable analysis.
  2. Copyright (C) 2004-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 is a simple analysis of induction variables of the loop. The major use
  16. is for determining the number of iterations of a loop for loop unrolling,
  17. doloop optimization and branch prediction. The iv information is computed
  18. on demand.
  19. Induction variables are analyzed by walking the use-def chains. When
  20. a basic induction variable (biv) is found, it is cached in the bivs
  21. hash table. When register is proved to be a biv, its description
  22. is stored to DF_REF_DATA of the def reference.
  23. The analysis works always with one loop -- you must call
  24. iv_analysis_loop_init (loop) for it. All the other functions then work with
  25. this loop. When you need to work with another loop, just call
  26. iv_analysis_loop_init for it. When you no longer need iv analysis, call
  27. iv_analysis_done () to clean up the memory.
  28. The available functions are:
  29. iv_analyze (insn, reg, iv): Stores the description of the induction variable
  30. corresponding to the use of register REG in INSN to IV. Returns true if
  31. REG is an induction variable in INSN. false otherwise.
  32. If use of REG is not found in INSN, following insns are scanned (so that
  33. we may call this function on insn returned by get_condition).
  34. iv_analyze_result (insn, def, iv): Stores to IV the description of the iv
  35. corresponding to DEF, which is a register defined in INSN.
  36. iv_analyze_expr (insn, rhs, mode, iv): Stores to IV the description of iv
  37. corresponding to expression EXPR evaluated at INSN. All registers used bu
  38. EXPR must also be used in INSN.
  39. */
  40. #include "config.h"
  41. #include "system.h"
  42. #include "coretypes.h"
  43. #include "tm.h"
  44. #include "rtl.h"
  45. #include "hard-reg-set.h"
  46. #include "obstack.h"
  47. #include "predict.h"
  48. #include "vec.h"
  49. #include "hashtab.h"
  50. #include "hash-set.h"
  51. #include "machmode.h"
  52. #include "input.h"
  53. #include "function.h"
  54. #include "dominance.h"
  55. #include "cfg.h"
  56. #include "basic-block.h"
  57. #include "cfgloop.h"
  58. #include "symtab.h"
  59. #include "flags.h"
  60. #include "statistics.h"
  61. #include "double-int.h"
  62. #include "real.h"
  63. #include "fixed-value.h"
  64. #include "alias.h"
  65. #include "wide-int.h"
  66. #include "inchash.h"
  67. #include "tree.h"
  68. #include "insn-config.h"
  69. #include "expmed.h"
  70. #include "dojump.h"
  71. #include "explow.h"
  72. #include "calls.h"
  73. #include "emit-rtl.h"
  74. #include "varasm.h"
  75. #include "stmt.h"
  76. #include "expr.h"
  77. #include "intl.h"
  78. #include "diagnostic-core.h"
  79. #include "df.h"
  80. #include "hash-table.h"
  81. #include "dumpfile.h"
  82. #include "rtl-iter.h"
  83. /* Possible return values of iv_get_reaching_def. */
  84. enum iv_grd_result
  85. {
  86. /* More than one reaching def, or reaching def that does not
  87. dominate the use. */
  88. GRD_INVALID,
  89. /* The use is trivial invariant of the loop, i.e. is not changed
  90. inside the loop. */
  91. GRD_INVARIANT,
  92. /* The use is reached by initial value and a value from the
  93. previous iteration. */
  94. GRD_MAYBE_BIV,
  95. /* The use has single dominating def. */
  96. GRD_SINGLE_DOM
  97. };
  98. /* Information about a biv. */
  99. struct biv_entry
  100. {
  101. unsigned regno; /* The register of the biv. */
  102. struct rtx_iv iv; /* Value of the biv. */
  103. };
  104. static bool clean_slate = true;
  105. static unsigned int iv_ref_table_size = 0;
  106. /* Table of rtx_ivs indexed by the df_ref uid field. */
  107. static struct rtx_iv ** iv_ref_table;
  108. /* Induction variable stored at the reference. */
  109. #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID (REF)]
  110. #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID (REF)] = (IV)
  111. /* The current loop. */
  112. static struct loop *current_loop;
  113. /* Hashtable helper. */
  114. struct biv_entry_hasher : typed_free_remove <biv_entry>
  115. {
  116. typedef biv_entry value_type;
  117. typedef rtx_def compare_type;
  118. static inline hashval_t hash (const value_type *);
  119. static inline bool equal (const value_type *, const compare_type *);
  120. };
  121. /* Returns hash value for biv B. */
  122. inline hashval_t
  123. biv_entry_hasher::hash (const value_type *b)
  124. {
  125. return b->regno;
  126. }
  127. /* Compares biv B and register R. */
  128. inline bool
  129. biv_entry_hasher::equal (const value_type *b, const compare_type *r)
  130. {
  131. return b->regno == REGNO (r);
  132. }
  133. /* Bivs of the current loop. */
  134. static hash_table<biv_entry_hasher> *bivs;
  135. static bool iv_analyze_op (rtx_insn *, rtx, struct rtx_iv *);
  136. /* Return the RTX code corresponding to the IV extend code EXTEND. */
  137. static inline enum rtx_code
  138. iv_extend_to_rtx_code (enum iv_extend_code extend)
  139. {
  140. switch (extend)
  141. {
  142. case IV_SIGN_EXTEND:
  143. return SIGN_EXTEND;
  144. case IV_ZERO_EXTEND:
  145. return ZERO_EXTEND;
  146. case IV_UNKNOWN_EXTEND:
  147. return UNKNOWN;
  148. }
  149. gcc_unreachable ();
  150. }
  151. /* Dumps information about IV to FILE. */
  152. extern void dump_iv_info (FILE *, struct rtx_iv *);
  153. void
  154. dump_iv_info (FILE *file, struct rtx_iv *iv)
  155. {
  156. if (!iv->base)
  157. {
  158. fprintf (file, "not simple");
  159. return;
  160. }
  161. if (iv->step == const0_rtx
  162. && !iv->first_special)
  163. fprintf (file, "invariant ");
  164. print_rtl (file, iv->base);
  165. if (iv->step != const0_rtx)
  166. {
  167. fprintf (file, " + ");
  168. print_rtl (file, iv->step);
  169. fprintf (file, " * iteration");
  170. }
  171. fprintf (file, " (in %s)", GET_MODE_NAME (iv->mode));
  172. if (iv->mode != iv->extend_mode)
  173. fprintf (file, " %s to %s",
  174. rtx_name[iv_extend_to_rtx_code (iv->extend)],
  175. GET_MODE_NAME (iv->extend_mode));
  176. if (iv->mult != const1_rtx)
  177. {
  178. fprintf (file, " * ");
  179. print_rtl (file, iv->mult);
  180. }
  181. if (iv->delta != const0_rtx)
  182. {
  183. fprintf (file, " + ");
  184. print_rtl (file, iv->delta);
  185. }
  186. if (iv->first_special)
  187. fprintf (file, " (first special)");
  188. }
  189. /* Generates a subreg to get the least significant part of EXPR (in mode
  190. INNER_MODE) to OUTER_MODE. */
  191. rtx
  192. lowpart_subreg (machine_mode outer_mode, rtx expr,
  193. machine_mode inner_mode)
  194. {
  195. return simplify_gen_subreg (outer_mode, expr, inner_mode,
  196. subreg_lowpart_offset (outer_mode, inner_mode));
  197. }
  198. static void
  199. check_iv_ref_table_size (void)
  200. {
  201. if (iv_ref_table_size < DF_DEFS_TABLE_SIZE ())
  202. {
  203. unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
  204. iv_ref_table = XRESIZEVEC (struct rtx_iv *, iv_ref_table, new_size);
  205. memset (&iv_ref_table[iv_ref_table_size], 0,
  206. (new_size - iv_ref_table_size) * sizeof (struct rtx_iv *));
  207. iv_ref_table_size = new_size;
  208. }
  209. }
  210. /* Checks whether REG is a well-behaved register. */
  211. static bool
  212. simple_reg_p (rtx reg)
  213. {
  214. unsigned r;
  215. if (GET_CODE (reg) == SUBREG)
  216. {
  217. if (!subreg_lowpart_p (reg))
  218. return false;
  219. reg = SUBREG_REG (reg);
  220. }
  221. if (!REG_P (reg))
  222. return false;
  223. r = REGNO (reg);
  224. if (HARD_REGISTER_NUM_P (r))
  225. return false;
  226. if (GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
  227. return false;
  228. return true;
  229. }
  230. /* Clears the information about ivs stored in df. */
  231. static void
  232. clear_iv_info (void)
  233. {
  234. unsigned i, n_defs = DF_DEFS_TABLE_SIZE ();
  235. struct rtx_iv *iv;
  236. check_iv_ref_table_size ();
  237. for (i = 0; i < n_defs; i++)
  238. {
  239. iv = iv_ref_table[i];
  240. if (iv)
  241. {
  242. free (iv);
  243. iv_ref_table[i] = NULL;
  244. }
  245. }
  246. bivs->empty ();
  247. }
  248. /* Prepare the data for an induction variable analysis of a LOOP. */
  249. void
  250. iv_analysis_loop_init (struct loop *loop)
  251. {
  252. current_loop = loop;
  253. /* Clear the information from the analysis of the previous loop. */
  254. if (clean_slate)
  255. {
  256. df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
  257. bivs = new hash_table<biv_entry_hasher> (10);
  258. clean_slate = false;
  259. }
  260. else
  261. clear_iv_info ();
  262. /* Get rid of the ud chains before processing the rescans. Then add
  263. the problem back. */
  264. df_remove_problem (df_chain);
  265. df_process_deferred_rescans ();
  266. df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
  267. df_chain_add_problem (DF_UD_CHAIN);
  268. df_note_add_problem ();
  269. df_analyze_loop (loop);
  270. if (dump_file)
  271. df_dump_region (dump_file);
  272. check_iv_ref_table_size ();
  273. }
  274. /* Finds the definition of REG that dominates loop latch and stores
  275. it to DEF. Returns false if there is not a single definition
  276. dominating the latch. If REG has no definition in loop, DEF
  277. is set to NULL and true is returned. */
  278. static bool
  279. latch_dominating_def (rtx reg, df_ref *def)
  280. {
  281. df_ref single_rd = NULL, adef;
  282. unsigned regno = REGNO (reg);
  283. struct df_rd_bb_info *bb_info = DF_RD_BB_INFO (current_loop->latch);
  284. for (adef = DF_REG_DEF_CHAIN (regno); adef; adef = DF_REF_NEXT_REG (adef))
  285. {
  286. if (!bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (adef))
  287. || !bitmap_bit_p (&bb_info->out, DF_REF_ID (adef)))
  288. continue;
  289. /* More than one reaching definition. */
  290. if (single_rd)
  291. return false;
  292. if (!just_once_each_iteration_p (current_loop, DF_REF_BB (adef)))
  293. return false;
  294. single_rd = adef;
  295. }
  296. *def = single_rd;
  297. return true;
  298. }
  299. /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
  300. static enum iv_grd_result
  301. iv_get_reaching_def (rtx_insn *insn, rtx reg, df_ref *def)
  302. {
  303. df_ref use, adef;
  304. basic_block def_bb, use_bb;
  305. rtx_insn *def_insn;
  306. bool dom_p;
  307. *def = NULL;
  308. if (!simple_reg_p (reg))
  309. return GRD_INVALID;
  310. if (GET_CODE (reg) == SUBREG)
  311. reg = SUBREG_REG (reg);
  312. gcc_assert (REG_P (reg));
  313. use = df_find_use (insn, reg);
  314. gcc_assert (use != NULL);
  315. if (!DF_REF_CHAIN (use))
  316. return GRD_INVARIANT;
  317. /* More than one reaching def. */
  318. if (DF_REF_CHAIN (use)->next)
  319. return GRD_INVALID;
  320. adef = DF_REF_CHAIN (use)->ref;
  321. /* We do not handle setting only part of the register. */
  322. if (DF_REF_FLAGS (adef) & DF_REF_READ_WRITE)
  323. return GRD_INVALID;
  324. def_insn = DF_REF_INSN (adef);
  325. def_bb = DF_REF_BB (adef);
  326. use_bb = BLOCK_FOR_INSN (insn);
  327. if (use_bb == def_bb)
  328. dom_p = (DF_INSN_LUID (def_insn) < DF_INSN_LUID (insn));
  329. else
  330. dom_p = dominated_by_p (CDI_DOMINATORS, use_bb, def_bb);
  331. if (dom_p)
  332. {
  333. *def = adef;
  334. return GRD_SINGLE_DOM;
  335. }
  336. /* The definition does not dominate the use. This is still OK if
  337. this may be a use of a biv, i.e. if the def_bb dominates loop
  338. latch. */
  339. if (just_once_each_iteration_p (current_loop, def_bb))
  340. return GRD_MAYBE_BIV;
  341. return GRD_INVALID;
  342. }
  343. /* Sets IV to invariant CST in MODE. Always returns true (just for
  344. consistency with other iv manipulation functions that may fail). */
  345. static bool
  346. iv_constant (struct rtx_iv *iv, rtx cst, machine_mode mode)
  347. {
  348. if (mode == VOIDmode)
  349. mode = GET_MODE (cst);
  350. iv->mode = mode;
  351. iv->base = cst;
  352. iv->step = const0_rtx;
  353. iv->first_special = false;
  354. iv->extend = IV_UNKNOWN_EXTEND;
  355. iv->extend_mode = iv->mode;
  356. iv->delta = const0_rtx;
  357. iv->mult = const1_rtx;
  358. return true;
  359. }
  360. /* Evaluates application of subreg to MODE on IV. */
  361. static bool
  362. iv_subreg (struct rtx_iv *iv, machine_mode mode)
  363. {
  364. /* If iv is invariant, just calculate the new value. */
  365. if (iv->step == const0_rtx
  366. && !iv->first_special)
  367. {
  368. rtx val = get_iv_value (iv, const0_rtx);
  369. val = lowpart_subreg (mode, val,
  370. iv->extend == IV_UNKNOWN_EXTEND
  371. ? iv->mode : iv->extend_mode);
  372. iv->base = val;
  373. iv->extend = IV_UNKNOWN_EXTEND;
  374. iv->mode = iv->extend_mode = mode;
  375. iv->delta = const0_rtx;
  376. iv->mult = const1_rtx;
  377. return true;
  378. }
  379. if (iv->extend_mode == mode)
  380. return true;
  381. if (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (iv->mode))
  382. return false;
  383. iv->extend = IV_UNKNOWN_EXTEND;
  384. iv->mode = mode;
  385. iv->base = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
  386. simplify_gen_binary (MULT, iv->extend_mode,
  387. iv->base, iv->mult));
  388. iv->step = simplify_gen_binary (MULT, iv->extend_mode, iv->step, iv->mult);
  389. iv->mult = const1_rtx;
  390. iv->delta = const0_rtx;
  391. iv->first_special = false;
  392. return true;
  393. }
  394. /* Evaluates application of EXTEND to MODE on IV. */
  395. static bool
  396. iv_extend (struct rtx_iv *iv, enum iv_extend_code extend, machine_mode mode)
  397. {
  398. /* If iv is invariant, just calculate the new value. */
  399. if (iv->step == const0_rtx
  400. && !iv->first_special)
  401. {
  402. rtx val = get_iv_value (iv, const0_rtx);
  403. if (iv->extend_mode != iv->mode
  404. && iv->extend != IV_UNKNOWN_EXTEND
  405. && iv->extend != extend)
  406. val = lowpart_subreg (iv->mode, val, iv->extend_mode);
  407. val = simplify_gen_unary (iv_extend_to_rtx_code (extend), mode,
  408. val,
  409. iv->extend == extend
  410. ? iv->extend_mode : iv->mode);
  411. iv->base = val;
  412. iv->extend = IV_UNKNOWN_EXTEND;
  413. iv->mode = iv->extend_mode = mode;
  414. iv->delta = const0_rtx;
  415. iv->mult = const1_rtx;
  416. return true;
  417. }
  418. if (mode != iv->extend_mode)
  419. return false;
  420. if (iv->extend != IV_UNKNOWN_EXTEND
  421. && iv->extend != extend)
  422. return false;
  423. iv->extend = extend;
  424. return true;
  425. }
  426. /* Evaluates negation of IV. */
  427. static bool
  428. iv_neg (struct rtx_iv *iv)
  429. {
  430. if (iv->extend == IV_UNKNOWN_EXTEND)
  431. {
  432. iv->base = simplify_gen_unary (NEG, iv->extend_mode,
  433. iv->base, iv->extend_mode);
  434. iv->step = simplify_gen_unary (NEG, iv->extend_mode,
  435. iv->step, iv->extend_mode);
  436. }
  437. else
  438. {
  439. iv->delta = simplify_gen_unary (NEG, iv->extend_mode,
  440. iv->delta, iv->extend_mode);
  441. iv->mult = simplify_gen_unary (NEG, iv->extend_mode,
  442. iv->mult, iv->extend_mode);
  443. }
  444. return true;
  445. }
  446. /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
  447. static bool
  448. iv_add (struct rtx_iv *iv0, struct rtx_iv *iv1, enum rtx_code op)
  449. {
  450. machine_mode mode;
  451. rtx arg;
  452. /* Extend the constant to extend_mode of the other operand if necessary. */
  453. if (iv0->extend == IV_UNKNOWN_EXTEND
  454. && iv0->mode == iv0->extend_mode
  455. && iv0->step == const0_rtx
  456. && GET_MODE_SIZE (iv0->extend_mode) < GET_MODE_SIZE (iv1->extend_mode))
  457. {
  458. iv0->extend_mode = iv1->extend_mode;
  459. iv0->base = simplify_gen_unary (ZERO_EXTEND, iv0->extend_mode,
  460. iv0->base, iv0->mode);
  461. }
  462. if (iv1->extend == IV_UNKNOWN_EXTEND
  463. && iv1->mode == iv1->extend_mode
  464. && iv1->step == const0_rtx
  465. && GET_MODE_SIZE (iv1->extend_mode) < GET_MODE_SIZE (iv0->extend_mode))
  466. {
  467. iv1->extend_mode = iv0->extend_mode;
  468. iv1->base = simplify_gen_unary (ZERO_EXTEND, iv1->extend_mode,
  469. iv1->base, iv1->mode);
  470. }
  471. mode = iv0->extend_mode;
  472. if (mode != iv1->extend_mode)
  473. return false;
  474. if (iv0->extend == IV_UNKNOWN_EXTEND
  475. && iv1->extend == IV_UNKNOWN_EXTEND)
  476. {
  477. if (iv0->mode != iv1->mode)
  478. return false;
  479. iv0->base = simplify_gen_binary (op, mode, iv0->base, iv1->base);
  480. iv0->step = simplify_gen_binary (op, mode, iv0->step, iv1->step);
  481. return true;
  482. }
  483. /* Handle addition of constant. */
  484. if (iv1->extend == IV_UNKNOWN_EXTEND
  485. && iv1->mode == mode
  486. && iv1->step == const0_rtx)
  487. {
  488. iv0->delta = simplify_gen_binary (op, mode, iv0->delta, iv1->base);
  489. return true;
  490. }
  491. if (iv0->extend == IV_UNKNOWN_EXTEND
  492. && iv0->mode == mode
  493. && iv0->step == const0_rtx)
  494. {
  495. arg = iv0->base;
  496. *iv0 = *iv1;
  497. if (op == MINUS
  498. && !iv_neg (iv0))
  499. return false;
  500. iv0->delta = simplify_gen_binary (PLUS, mode, iv0->delta, arg);
  501. return true;
  502. }
  503. return false;
  504. }
  505. /* Evaluates multiplication of IV by constant CST. */
  506. static bool
  507. iv_mult (struct rtx_iv *iv, rtx mby)
  508. {
  509. machine_mode mode = iv->extend_mode;
  510. if (GET_MODE (mby) != VOIDmode
  511. && GET_MODE (mby) != mode)
  512. return false;
  513. if (iv->extend == IV_UNKNOWN_EXTEND)
  514. {
  515. iv->base = simplify_gen_binary (MULT, mode, iv->base, mby);
  516. iv->step = simplify_gen_binary (MULT, mode, iv->step, mby);
  517. }
  518. else
  519. {
  520. iv->delta = simplify_gen_binary (MULT, mode, iv->delta, mby);
  521. iv->mult = simplify_gen_binary (MULT, mode, iv->mult, mby);
  522. }
  523. return true;
  524. }
  525. /* Evaluates shift of IV by constant CST. */
  526. static bool
  527. iv_shift (struct rtx_iv *iv, rtx mby)
  528. {
  529. machine_mode mode = iv->extend_mode;
  530. if (GET_MODE (mby) != VOIDmode
  531. && GET_MODE (mby) != mode)
  532. return false;
  533. if (iv->extend == IV_UNKNOWN_EXTEND)
  534. {
  535. iv->base = simplify_gen_binary (ASHIFT, mode, iv->base, mby);
  536. iv->step = simplify_gen_binary (ASHIFT, mode, iv->step, mby);
  537. }
  538. else
  539. {
  540. iv->delta = simplify_gen_binary (ASHIFT, mode, iv->delta, mby);
  541. iv->mult = simplify_gen_binary (ASHIFT, mode, iv->mult, mby);
  542. }
  543. return true;
  544. }
  545. /* The recursive part of get_biv_step. Gets the value of the single value
  546. defined by DEF wrto initial value of REG inside loop, in shape described
  547. at get_biv_step. */
  548. static bool
  549. get_biv_step_1 (df_ref def, rtx reg,
  550. rtx *inner_step, machine_mode *inner_mode,
  551. enum iv_extend_code *extend, machine_mode outer_mode,
  552. rtx *outer_step)
  553. {
  554. rtx set, rhs, op0 = NULL_RTX, op1 = NULL_RTX;
  555. rtx next, nextr, tmp;
  556. enum rtx_code code;
  557. rtx_insn *insn = DF_REF_INSN (def);
  558. df_ref next_def;
  559. enum iv_grd_result res;
  560. set = single_set (insn);
  561. if (!set)
  562. return false;
  563. rhs = find_reg_equal_equiv_note (insn);
  564. if (rhs)
  565. rhs = XEXP (rhs, 0);
  566. else
  567. rhs = SET_SRC (set);
  568. code = GET_CODE (rhs);
  569. switch (code)
  570. {
  571. case SUBREG:
  572. case REG:
  573. next = rhs;
  574. break;
  575. case PLUS:
  576. case MINUS:
  577. op0 = XEXP (rhs, 0);
  578. op1 = XEXP (rhs, 1);
  579. if (code == PLUS && CONSTANT_P (op0))
  580. {
  581. tmp = op0; op0 = op1; op1 = tmp;
  582. }
  583. if (!simple_reg_p (op0)
  584. || !CONSTANT_P (op1))
  585. return false;
  586. if (GET_MODE (rhs) != outer_mode)
  587. {
  588. /* ppc64 uses expressions like
  589. (set x:SI (plus:SI (subreg:SI y:DI) 1)).
  590. this is equivalent to
  591. (set x':DI (plus:DI y:DI 1))
  592. (set x:SI (subreg:SI (x':DI)). */
  593. if (GET_CODE (op0) != SUBREG)
  594. return false;
  595. if (GET_MODE (SUBREG_REG (op0)) != outer_mode)
  596. return false;
  597. }
  598. next = op0;
  599. break;
  600. case SIGN_EXTEND:
  601. case ZERO_EXTEND:
  602. if (GET_MODE (rhs) != outer_mode)
  603. return false;
  604. op0 = XEXP (rhs, 0);
  605. if (!simple_reg_p (op0))
  606. return false;
  607. next = op0;
  608. break;
  609. default:
  610. return false;
  611. }
  612. if (GET_CODE (next) == SUBREG)
  613. {
  614. if (!subreg_lowpart_p (next))
  615. return false;
  616. nextr = SUBREG_REG (next);
  617. if (GET_MODE (nextr) != outer_mode)
  618. return false;
  619. }
  620. else
  621. nextr = next;
  622. res = iv_get_reaching_def (insn, nextr, &next_def);
  623. if (res == GRD_INVALID || res == GRD_INVARIANT)
  624. return false;
  625. if (res == GRD_MAYBE_BIV)
  626. {
  627. if (!rtx_equal_p (nextr, reg))
  628. return false;
  629. *inner_step = const0_rtx;
  630. *extend = IV_UNKNOWN_EXTEND;
  631. *inner_mode = outer_mode;
  632. *outer_step = const0_rtx;
  633. }
  634. else if (!get_biv_step_1 (next_def, reg,
  635. inner_step, inner_mode, extend, outer_mode,
  636. outer_step))
  637. return false;
  638. if (GET_CODE (next) == SUBREG)
  639. {
  640. machine_mode amode = GET_MODE (next);
  641. if (GET_MODE_SIZE (amode) > GET_MODE_SIZE (*inner_mode))
  642. return false;
  643. *inner_mode = amode;
  644. *inner_step = simplify_gen_binary (PLUS, outer_mode,
  645. *inner_step, *outer_step);
  646. *outer_step = const0_rtx;
  647. *extend = IV_UNKNOWN_EXTEND;
  648. }
  649. switch (code)
  650. {
  651. case REG:
  652. case SUBREG:
  653. break;
  654. case PLUS:
  655. case MINUS:
  656. if (*inner_mode == outer_mode
  657. /* See comment in previous switch. */
  658. || GET_MODE (rhs) != outer_mode)
  659. *inner_step = simplify_gen_binary (code, outer_mode,
  660. *inner_step, op1);
  661. else
  662. *outer_step = simplify_gen_binary (code, outer_mode,
  663. *outer_step, op1);
  664. break;
  665. case SIGN_EXTEND:
  666. case ZERO_EXTEND:
  667. gcc_assert (GET_MODE (op0) == *inner_mode
  668. && *extend == IV_UNKNOWN_EXTEND
  669. && *outer_step == const0_rtx);
  670. *extend = (code == SIGN_EXTEND) ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
  671. break;
  672. default:
  673. return false;
  674. }
  675. return true;
  676. }
  677. /* Gets the operation on register REG inside loop, in shape
  678. OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
  679. If the operation cannot be described in this shape, return false.
  680. LAST_DEF is the definition of REG that dominates loop latch. */
  681. static bool
  682. get_biv_step (df_ref last_def, rtx reg, rtx *inner_step,
  683. machine_mode *inner_mode, enum iv_extend_code *extend,
  684. machine_mode *outer_mode, rtx *outer_step)
  685. {
  686. *outer_mode = GET_MODE (reg);
  687. if (!get_biv_step_1 (last_def, reg,
  688. inner_step, inner_mode, extend, *outer_mode,
  689. outer_step))
  690. return false;
  691. gcc_assert ((*inner_mode == *outer_mode) != (*extend != IV_UNKNOWN_EXTEND));
  692. gcc_assert (*inner_mode != *outer_mode || *outer_step == const0_rtx);
  693. return true;
  694. }
  695. /* Records information that DEF is induction variable IV. */
  696. static void
  697. record_iv (df_ref def, struct rtx_iv *iv)
  698. {
  699. struct rtx_iv *recorded_iv = XNEW (struct rtx_iv);
  700. *recorded_iv = *iv;
  701. check_iv_ref_table_size ();
  702. DF_REF_IV_SET (def, recorded_iv);
  703. }
  704. /* If DEF was already analyzed for bivness, store the description of the biv to
  705. IV and return true. Otherwise return false. */
  706. static bool
  707. analyzed_for_bivness_p (rtx def, struct rtx_iv *iv)
  708. {
  709. struct biv_entry *biv = bivs->find_with_hash (def, REGNO (def));
  710. if (!biv)
  711. return false;
  712. *iv = biv->iv;
  713. return true;
  714. }
  715. static void
  716. record_biv (rtx def, struct rtx_iv *iv)
  717. {
  718. struct biv_entry *biv = XNEW (struct biv_entry);
  719. biv_entry **slot = bivs->find_slot_with_hash (def, REGNO (def), INSERT);
  720. biv->regno = REGNO (def);
  721. biv->iv = *iv;
  722. gcc_assert (!*slot);
  723. *slot = biv;
  724. }
  725. /* Determines whether DEF is a biv and if so, stores its description
  726. to *IV. */
  727. static bool
  728. iv_analyze_biv (rtx def, struct rtx_iv *iv)
  729. {
  730. rtx inner_step, outer_step;
  731. machine_mode inner_mode, outer_mode;
  732. enum iv_extend_code extend;
  733. df_ref last_def;
  734. if (dump_file)
  735. {
  736. fprintf (dump_file, "Analyzing ");
  737. print_rtl (dump_file, def);
  738. fprintf (dump_file, " for bivness.\n");
  739. }
  740. if (!REG_P (def))
  741. {
  742. if (!CONSTANT_P (def))
  743. return false;
  744. return iv_constant (iv, def, VOIDmode);
  745. }
  746. if (!latch_dominating_def (def, &last_def))
  747. {
  748. if (dump_file)
  749. fprintf (dump_file, " not simple.\n");
  750. return false;
  751. }
  752. if (!last_def)
  753. return iv_constant (iv, def, VOIDmode);
  754. if (analyzed_for_bivness_p (def, iv))
  755. {
  756. if (dump_file)
  757. fprintf (dump_file, " already analysed.\n");
  758. return iv->base != NULL_RTX;
  759. }
  760. if (!get_biv_step (last_def, def, &inner_step, &inner_mode, &extend,
  761. &outer_mode, &outer_step))
  762. {
  763. iv->base = NULL_RTX;
  764. goto end;
  765. }
  766. /* Loop transforms base to es (base + inner_step) + outer_step,
  767. where es means extend of subreg between inner_mode and outer_mode.
  768. The corresponding induction variable is
  769. es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
  770. iv->base = simplify_gen_binary (MINUS, outer_mode, def, outer_step);
  771. iv->step = simplify_gen_binary (PLUS, outer_mode, inner_step, outer_step);
  772. iv->mode = inner_mode;
  773. iv->extend_mode = outer_mode;
  774. iv->extend = extend;
  775. iv->mult = const1_rtx;
  776. iv->delta = outer_step;
  777. iv->first_special = inner_mode != outer_mode;
  778. end:
  779. if (dump_file)
  780. {
  781. fprintf (dump_file, " ");
  782. dump_iv_info (dump_file, iv);
  783. fprintf (dump_file, "\n");
  784. }
  785. record_biv (def, iv);
  786. return iv->base != NULL_RTX;
  787. }
  788. /* Analyzes expression RHS used at INSN and stores the result to *IV.
  789. The mode of the induction variable is MODE. */
  790. bool
  791. iv_analyze_expr (rtx_insn *insn, rtx rhs, machine_mode mode,
  792. struct rtx_iv *iv)
  793. {
  794. rtx mby = NULL_RTX, tmp;
  795. rtx op0 = NULL_RTX, op1 = NULL_RTX;
  796. struct rtx_iv iv0, iv1;
  797. enum rtx_code code = GET_CODE (rhs);
  798. machine_mode omode = mode;
  799. iv->mode = VOIDmode;
  800. iv->base = NULL_RTX;
  801. iv->step = NULL_RTX;
  802. gcc_assert (GET_MODE (rhs) == mode || GET_MODE (rhs) == VOIDmode);
  803. if (CONSTANT_P (rhs)
  804. || REG_P (rhs)
  805. || code == SUBREG)
  806. {
  807. if (!iv_analyze_op (insn, rhs, iv))
  808. return false;
  809. if (iv->mode == VOIDmode)
  810. {
  811. iv->mode = mode;
  812. iv->extend_mode = mode;
  813. }
  814. return true;
  815. }
  816. switch (code)
  817. {
  818. case REG:
  819. op0 = rhs;
  820. break;
  821. case SIGN_EXTEND:
  822. case ZERO_EXTEND:
  823. case NEG:
  824. op0 = XEXP (rhs, 0);
  825. omode = GET_MODE (op0);
  826. break;
  827. case PLUS:
  828. case MINUS:
  829. op0 = XEXP (rhs, 0);
  830. op1 = XEXP (rhs, 1);
  831. break;
  832. case MULT:
  833. op0 = XEXP (rhs, 0);
  834. mby = XEXP (rhs, 1);
  835. if (!CONSTANT_P (mby))
  836. {
  837. tmp = op0;
  838. op0 = mby;
  839. mby = tmp;
  840. }
  841. if (!CONSTANT_P (mby))
  842. return false;
  843. break;
  844. case ASHIFT:
  845. op0 = XEXP (rhs, 0);
  846. mby = XEXP (rhs, 1);
  847. if (!CONSTANT_P (mby))
  848. return false;
  849. break;
  850. default:
  851. return false;
  852. }
  853. if (op0
  854. && !iv_analyze_expr (insn, op0, omode, &iv0))
  855. return false;
  856. if (op1
  857. && !iv_analyze_expr (insn, op1, omode, &iv1))
  858. return false;
  859. switch (code)
  860. {
  861. case SIGN_EXTEND:
  862. if (!iv_extend (&iv0, IV_SIGN_EXTEND, mode))
  863. return false;
  864. break;
  865. case ZERO_EXTEND:
  866. if (!iv_extend (&iv0, IV_ZERO_EXTEND, mode))
  867. return false;
  868. break;
  869. case NEG:
  870. if (!iv_neg (&iv0))
  871. return false;
  872. break;
  873. case PLUS:
  874. case MINUS:
  875. if (!iv_add (&iv0, &iv1, code))
  876. return false;
  877. break;
  878. case MULT:
  879. if (!iv_mult (&iv0, mby))
  880. return false;
  881. break;
  882. case ASHIFT:
  883. if (!iv_shift (&iv0, mby))
  884. return false;
  885. break;
  886. default:
  887. break;
  888. }
  889. *iv = iv0;
  890. return iv->base != NULL_RTX;
  891. }
  892. /* Analyzes iv DEF and stores the result to *IV. */
  893. static bool
  894. iv_analyze_def (df_ref def, struct rtx_iv *iv)
  895. {
  896. rtx_insn *insn = DF_REF_INSN (def);
  897. rtx reg = DF_REF_REG (def);
  898. rtx set, rhs;
  899. if (dump_file)
  900. {
  901. fprintf (dump_file, "Analyzing def of ");
  902. print_rtl (dump_file, reg);
  903. fprintf (dump_file, " in insn ");
  904. print_rtl_single (dump_file, insn);
  905. }
  906. check_iv_ref_table_size ();
  907. if (DF_REF_IV (def))
  908. {
  909. if (dump_file)
  910. fprintf (dump_file, " already analysed.\n");
  911. *iv = *DF_REF_IV (def);
  912. return iv->base != NULL_RTX;
  913. }
  914. iv->mode = VOIDmode;
  915. iv->base = NULL_RTX;
  916. iv->step = NULL_RTX;
  917. if (!REG_P (reg))
  918. return false;
  919. set = single_set (insn);
  920. if (!set)
  921. return false;
  922. if (!REG_P (SET_DEST (set)))
  923. return false;
  924. gcc_assert (SET_DEST (set) == reg);
  925. rhs = find_reg_equal_equiv_note (insn);
  926. if (rhs)
  927. rhs = XEXP (rhs, 0);
  928. else
  929. rhs = SET_SRC (set);
  930. iv_analyze_expr (insn, rhs, GET_MODE (reg), iv);
  931. record_iv (def, iv);
  932. if (dump_file)
  933. {
  934. print_rtl (dump_file, reg);
  935. fprintf (dump_file, " in insn ");
  936. print_rtl_single (dump_file, insn);
  937. fprintf (dump_file, " is ");
  938. dump_iv_info (dump_file, iv);
  939. fprintf (dump_file, "\n");
  940. }
  941. return iv->base != NULL_RTX;
  942. }
  943. /* Analyzes operand OP of INSN and stores the result to *IV. */
  944. static bool
  945. iv_analyze_op (rtx_insn *insn, rtx op, struct rtx_iv *iv)
  946. {
  947. df_ref def = NULL;
  948. enum iv_grd_result res;
  949. if (dump_file)
  950. {
  951. fprintf (dump_file, "Analyzing operand ");
  952. print_rtl (dump_file, op);
  953. fprintf (dump_file, " of insn ");
  954. print_rtl_single (dump_file, insn);
  955. }
  956. if (function_invariant_p (op))
  957. res = GRD_INVARIANT;
  958. else if (GET_CODE (op) == SUBREG)
  959. {
  960. if (!subreg_lowpart_p (op))
  961. return false;
  962. if (!iv_analyze_op (insn, SUBREG_REG (op), iv))
  963. return false;
  964. return iv_subreg (iv, GET_MODE (op));
  965. }
  966. else
  967. {
  968. res = iv_get_reaching_def (insn, op, &def);
  969. if (res == GRD_INVALID)
  970. {
  971. if (dump_file)
  972. fprintf (dump_file, " not simple.\n");
  973. return false;
  974. }
  975. }
  976. if (res == GRD_INVARIANT)
  977. {
  978. iv_constant (iv, op, VOIDmode);
  979. if (dump_file)
  980. {
  981. fprintf (dump_file, " ");
  982. dump_iv_info (dump_file, iv);
  983. fprintf (dump_file, "\n");
  984. }
  985. return true;
  986. }
  987. if (res == GRD_MAYBE_BIV)
  988. return iv_analyze_biv (op, iv);
  989. return iv_analyze_def (def, iv);
  990. }
  991. /* Analyzes value VAL at INSN and stores the result to *IV. */
  992. bool
  993. iv_analyze (rtx_insn *insn, rtx val, struct rtx_iv *iv)
  994. {
  995. rtx reg;
  996. /* We must find the insn in that val is used, so that we get to UD chains.
  997. Since the function is sometimes called on result of get_condition,
  998. this does not necessarily have to be directly INSN; scan also the
  999. following insns. */
  1000. if (simple_reg_p (val))
  1001. {
  1002. if (GET_CODE (val) == SUBREG)
  1003. reg = SUBREG_REG (val);
  1004. else
  1005. reg = val;
  1006. while (!df_find_use (insn, reg))
  1007. insn = NEXT_INSN (insn);
  1008. }
  1009. return iv_analyze_op (insn, val, iv);
  1010. }
  1011. /* Analyzes definition of DEF in INSN and stores the result to IV. */
  1012. bool
  1013. iv_analyze_result (rtx_insn *insn, rtx def, struct rtx_iv *iv)
  1014. {
  1015. df_ref adef;
  1016. adef = df_find_def (insn, def);
  1017. if (!adef)
  1018. return false;
  1019. return iv_analyze_def (adef, iv);
  1020. }
  1021. /* Checks whether definition of register REG in INSN is a basic induction
  1022. variable. IV analysis must have been initialized (via a call to
  1023. iv_analysis_loop_init) for this function to produce a result. */
  1024. bool
  1025. biv_p (rtx_insn *insn, rtx reg)
  1026. {
  1027. struct rtx_iv iv;
  1028. df_ref def, last_def;
  1029. if (!simple_reg_p (reg))
  1030. return false;
  1031. def = df_find_def (insn, reg);
  1032. gcc_assert (def != NULL);
  1033. if (!latch_dominating_def (reg, &last_def))
  1034. return false;
  1035. if (last_def != def)
  1036. return false;
  1037. if (!iv_analyze_biv (reg, &iv))
  1038. return false;
  1039. return iv.step != const0_rtx;
  1040. }
  1041. /* Calculates value of IV at ITERATION-th iteration. */
  1042. rtx
  1043. get_iv_value (struct rtx_iv *iv, rtx iteration)
  1044. {
  1045. rtx val;
  1046. /* We would need to generate some if_then_else patterns, and so far
  1047. it is not needed anywhere. */
  1048. gcc_assert (!iv->first_special);
  1049. if (iv->step != const0_rtx && iteration != const0_rtx)
  1050. val = simplify_gen_binary (PLUS, iv->extend_mode, iv->base,
  1051. simplify_gen_binary (MULT, iv->extend_mode,
  1052. iv->step, iteration));
  1053. else
  1054. val = iv->base;
  1055. if (iv->extend_mode == iv->mode)
  1056. return val;
  1057. val = lowpart_subreg (iv->mode, val, iv->extend_mode);
  1058. if (iv->extend == IV_UNKNOWN_EXTEND)
  1059. return val;
  1060. val = simplify_gen_unary (iv_extend_to_rtx_code (iv->extend),
  1061. iv->extend_mode, val, iv->mode);
  1062. val = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
  1063. simplify_gen_binary (MULT, iv->extend_mode,
  1064. iv->mult, val));
  1065. return val;
  1066. }
  1067. /* Free the data for an induction variable analysis. */
  1068. void
  1069. iv_analysis_done (void)
  1070. {
  1071. if (!clean_slate)
  1072. {
  1073. clear_iv_info ();
  1074. clean_slate = true;
  1075. df_finish_pass (true);
  1076. delete bivs;
  1077. bivs = NULL;
  1078. free (iv_ref_table);
  1079. iv_ref_table = NULL;
  1080. iv_ref_table_size = 0;
  1081. }
  1082. }
  1083. /* Computes inverse to X modulo (1 << MOD). */
  1084. static uint64_t
  1085. inverse (uint64_t x, int mod)
  1086. {
  1087. uint64_t mask =
  1088. ((uint64_t) 1 << (mod - 1) << 1) - 1;
  1089. uint64_t rslt = 1;
  1090. int i;
  1091. for (i = 0; i < mod - 1; i++)
  1092. {
  1093. rslt = (rslt * x) & mask;
  1094. x = (x * x) & mask;
  1095. }
  1096. return rslt;
  1097. }
  1098. /* Checks whether any register in X is in set ALT. */
  1099. static bool
  1100. altered_reg_used (const_rtx x, bitmap alt)
  1101. {
  1102. subrtx_iterator::array_type array;
  1103. FOR_EACH_SUBRTX (iter, array, x, NONCONST)
  1104. {
  1105. const_rtx x = *iter;
  1106. if (REG_P (x) && REGNO_REG_SET_P (alt, REGNO (x)))
  1107. return true;
  1108. }
  1109. return false;
  1110. }
  1111. /* Marks registers altered by EXPR in set ALT. */
  1112. static void
  1113. mark_altered (rtx expr, const_rtx by ATTRIBUTE_UNUSED, void *alt)
  1114. {
  1115. if (GET_CODE (expr) == SUBREG)
  1116. expr = SUBREG_REG (expr);
  1117. if (!REG_P (expr))
  1118. return;
  1119. SET_REGNO_REG_SET ((bitmap) alt, REGNO (expr));
  1120. }
  1121. /* Checks whether RHS is simple enough to process. */
  1122. static bool
  1123. simple_rhs_p (rtx rhs)
  1124. {
  1125. rtx op0, op1;
  1126. if (function_invariant_p (rhs)
  1127. || (REG_P (rhs) && !HARD_REGISTER_P (rhs)))
  1128. return true;
  1129. switch (GET_CODE (rhs))
  1130. {
  1131. case PLUS:
  1132. case MINUS:
  1133. case AND:
  1134. op0 = XEXP (rhs, 0);
  1135. op1 = XEXP (rhs, 1);
  1136. /* Allow reg OP const and reg OP reg. */
  1137. if (!(REG_P (op0) && !HARD_REGISTER_P (op0))
  1138. && !function_invariant_p (op0))
  1139. return false;
  1140. if (!(REG_P (op1) && !HARD_REGISTER_P (op1))
  1141. && !function_invariant_p (op1))
  1142. return false;
  1143. return true;
  1144. case ASHIFT:
  1145. case ASHIFTRT:
  1146. case LSHIFTRT:
  1147. case MULT:
  1148. op0 = XEXP (rhs, 0);
  1149. op1 = XEXP (rhs, 1);
  1150. /* Allow reg OP const. */
  1151. if (!(REG_P (op0) && !HARD_REGISTER_P (op0)))
  1152. return false;
  1153. if (!function_invariant_p (op1))
  1154. return false;
  1155. return true;
  1156. default:
  1157. return false;
  1158. }
  1159. }
  1160. /* If REGNO has a single definition, return its known value, otherwise return
  1161. null. */
  1162. static rtx
  1163. find_single_def_src (unsigned int regno)
  1164. {
  1165. df_ref adef;
  1166. rtx set, src;
  1167. for (;;)
  1168. {
  1169. rtx note;
  1170. adef = DF_REG_DEF_CHAIN (regno);
  1171. if (adef == NULL || DF_REF_NEXT_REG (adef) != NULL
  1172. || DF_REF_IS_ARTIFICIAL (adef))
  1173. return NULL_RTX;
  1174. set = single_set (DF_REF_INSN (adef));
  1175. if (set == NULL || !REG_P (SET_DEST (set))
  1176. || REGNO (SET_DEST (set)) != regno)
  1177. return NULL_RTX;
  1178. note = find_reg_equal_equiv_note (DF_REF_INSN (adef));
  1179. if (note && function_invariant_p (XEXP (note, 0)))
  1180. {
  1181. src = XEXP (note, 0);
  1182. break;
  1183. }
  1184. src = SET_SRC (set);
  1185. if (REG_P (src))
  1186. {
  1187. regno = REGNO (src);
  1188. continue;
  1189. }
  1190. break;
  1191. }
  1192. if (!function_invariant_p (src))
  1193. return NULL_RTX;
  1194. return src;
  1195. }
  1196. /* If any registers in *EXPR that have a single definition, try to replace
  1197. them with the known-equivalent values. */
  1198. static void
  1199. replace_single_def_regs (rtx *expr)
  1200. {
  1201. subrtx_var_iterator::array_type array;
  1202. repeat:
  1203. FOR_EACH_SUBRTX_VAR (iter, array, *expr, NONCONST)
  1204. {
  1205. rtx x = *iter;
  1206. if (REG_P (x))
  1207. if (rtx new_x = find_single_def_src (REGNO (x)))
  1208. {
  1209. *expr = simplify_replace_rtx (*expr, x, new_x);
  1210. goto repeat;
  1211. }
  1212. }
  1213. }
  1214. /* A subroutine of simplify_using_initial_values, this function examines INSN
  1215. to see if it contains a suitable set that we can use to make a replacement.
  1216. If it is suitable, return true and set DEST and SRC to the lhs and rhs of
  1217. the set; return false otherwise. */
  1218. static bool
  1219. suitable_set_for_replacement (rtx_insn *insn, rtx *dest, rtx *src)
  1220. {
  1221. rtx set = single_set (insn);
  1222. rtx lhs = NULL_RTX, rhs;
  1223. if (!set)
  1224. return false;
  1225. lhs = SET_DEST (set);
  1226. if (!REG_P (lhs))
  1227. return false;
  1228. rhs = find_reg_equal_equiv_note (insn);
  1229. if (rhs)
  1230. rhs = XEXP (rhs, 0);
  1231. else
  1232. rhs = SET_SRC (set);
  1233. if (!simple_rhs_p (rhs))
  1234. return false;
  1235. *dest = lhs;
  1236. *src = rhs;
  1237. return true;
  1238. }
  1239. /* Using the data returned by suitable_set_for_replacement, replace DEST
  1240. with SRC in *EXPR and return the new expression. Also call
  1241. replace_single_def_regs if the replacement changed something. */
  1242. static void
  1243. replace_in_expr (rtx *expr, rtx dest, rtx src)
  1244. {
  1245. rtx old = *expr;
  1246. *expr = simplify_replace_rtx (*expr, dest, src);
  1247. if (old == *expr)
  1248. return;
  1249. replace_single_def_regs (expr);
  1250. }
  1251. /* Checks whether A implies B. */
  1252. static bool
  1253. implies_p (rtx a, rtx b)
  1254. {
  1255. rtx op0, op1, opb0, opb1, r;
  1256. machine_mode mode;
  1257. if (rtx_equal_p (a, b))
  1258. return true;
  1259. if (GET_CODE (a) == EQ)
  1260. {
  1261. op0 = XEXP (a, 0);
  1262. op1 = XEXP (a, 1);
  1263. if (REG_P (op0)
  1264. || (GET_CODE (op0) == SUBREG
  1265. && REG_P (SUBREG_REG (op0))))
  1266. {
  1267. r = simplify_replace_rtx (b, op0, op1);
  1268. if (r == const_true_rtx)
  1269. return true;
  1270. }
  1271. if (REG_P (op1)
  1272. || (GET_CODE (op1) == SUBREG
  1273. && REG_P (SUBREG_REG (op1))))
  1274. {
  1275. r = simplify_replace_rtx (b, op1, op0);
  1276. if (r == const_true_rtx)
  1277. return true;
  1278. }
  1279. }
  1280. if (b == const_true_rtx)
  1281. return true;
  1282. if ((GET_RTX_CLASS (GET_CODE (a)) != RTX_COMM_COMPARE
  1283. && GET_RTX_CLASS (GET_CODE (a)) != RTX_COMPARE)
  1284. || (GET_RTX_CLASS (GET_CODE (b)) != RTX_COMM_COMPARE
  1285. && GET_RTX_CLASS (GET_CODE (b)) != RTX_COMPARE))
  1286. return false;
  1287. op0 = XEXP (a, 0);
  1288. op1 = XEXP (a, 1);
  1289. opb0 = XEXP (b, 0);
  1290. opb1 = XEXP (b, 1);
  1291. mode = GET_MODE (op0);
  1292. if (mode != GET_MODE (opb0))
  1293. mode = VOIDmode;
  1294. else if (mode == VOIDmode)
  1295. {
  1296. mode = GET_MODE (op1);
  1297. if (mode != GET_MODE (opb1))
  1298. mode = VOIDmode;
  1299. }
  1300. /* A < B implies A + 1 <= B. */
  1301. if ((GET_CODE (a) == GT || GET_CODE (a) == LT)
  1302. && (GET_CODE (b) == GE || GET_CODE (b) == LE))
  1303. {
  1304. if (GET_CODE (a) == GT)
  1305. {
  1306. r = op0;
  1307. op0 = op1;
  1308. op1 = r;
  1309. }
  1310. if (GET_CODE (b) == GE)
  1311. {
  1312. r = opb0;
  1313. opb0 = opb1;
  1314. opb1 = r;
  1315. }
  1316. if (SCALAR_INT_MODE_P (mode)
  1317. && rtx_equal_p (op1, opb1)
  1318. && simplify_gen_binary (MINUS, mode, opb0, op0) == const1_rtx)
  1319. return true;
  1320. return false;
  1321. }
  1322. /* A < B or A > B imply A != B. TODO: Likewise
  1323. A + n < B implies A != B + n if neither wraps. */
  1324. if (GET_CODE (b) == NE
  1325. && (GET_CODE (a) == GT || GET_CODE (a) == GTU
  1326. || GET_CODE (a) == LT || GET_CODE (a) == LTU))
  1327. {
  1328. if (rtx_equal_p (op0, opb0)
  1329. && rtx_equal_p (op1, opb1))
  1330. return true;
  1331. }
  1332. /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
  1333. if (GET_CODE (a) == NE
  1334. && op1 == const0_rtx)
  1335. {
  1336. if ((GET_CODE (b) == GTU
  1337. && opb1 == const0_rtx)
  1338. || (GET_CODE (b) == GEU
  1339. && opb1 == const1_rtx))
  1340. return rtx_equal_p (op0, opb0);
  1341. }
  1342. /* A != N is equivalent to A - (N + 1) <u -1. */
  1343. if (GET_CODE (a) == NE
  1344. && CONST_INT_P (op1)
  1345. && GET_CODE (b) == LTU
  1346. && opb1 == constm1_rtx
  1347. && GET_CODE (opb0) == PLUS
  1348. && CONST_INT_P (XEXP (opb0, 1))
  1349. /* Avoid overflows. */
  1350. && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
  1351. != ((unsigned HOST_WIDE_INT)1
  1352. << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
  1353. && INTVAL (XEXP (opb0, 1)) + 1 == -INTVAL (op1))
  1354. return rtx_equal_p (op0, XEXP (opb0, 0));
  1355. /* Likewise, A != N implies A - N > 0. */
  1356. if (GET_CODE (a) == NE
  1357. && CONST_INT_P (op1))
  1358. {
  1359. if (GET_CODE (b) == GTU
  1360. && GET_CODE (opb0) == PLUS
  1361. && opb1 == const0_rtx
  1362. && CONST_INT_P (XEXP (opb0, 1))
  1363. /* Avoid overflows. */
  1364. && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
  1365. != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
  1366. && rtx_equal_p (XEXP (opb0, 0), op0))
  1367. return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
  1368. if (GET_CODE (b) == GEU
  1369. && GET_CODE (opb0) == PLUS
  1370. && opb1 == const1_rtx
  1371. && CONST_INT_P (XEXP (opb0, 1))
  1372. /* Avoid overflows. */
  1373. && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
  1374. != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
  1375. && rtx_equal_p (XEXP (opb0, 0), op0))
  1376. return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
  1377. }
  1378. /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
  1379. if ((GET_CODE (a) == GT || GET_CODE (a) == GE)
  1380. && CONST_INT_P (op1)
  1381. && ((GET_CODE (a) == GT && op1 == constm1_rtx)
  1382. || INTVAL (op1) >= 0)
  1383. && GET_CODE (b) == LTU
  1384. && CONST_INT_P (opb1)
  1385. && rtx_equal_p (op0, opb0))
  1386. return INTVAL (opb1) < 0;
  1387. return false;
  1388. }
  1389. /* Canonicalizes COND so that
  1390. (1) Ensure that operands are ordered according to
  1391. swap_commutative_operands_p.
  1392. (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
  1393. for GE, GEU, and LEU. */
  1394. rtx
  1395. canon_condition (rtx cond)
  1396. {
  1397. rtx tem;
  1398. rtx op0, op1;
  1399. enum rtx_code code;
  1400. machine_mode mode;
  1401. code = GET_CODE (cond);
  1402. op0 = XEXP (cond, 0);
  1403. op1 = XEXP (cond, 1);
  1404. if (swap_commutative_operands_p (op0, op1))
  1405. {
  1406. code = swap_condition (code);
  1407. tem = op0;
  1408. op0 = op1;
  1409. op1 = tem;
  1410. }
  1411. mode = GET_MODE (op0);
  1412. if (mode == VOIDmode)
  1413. mode = GET_MODE (op1);
  1414. gcc_assert (mode != VOIDmode);
  1415. if (CONST_INT_P (op1)
  1416. && GET_MODE_CLASS (mode) != MODE_CC
  1417. && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
  1418. {
  1419. HOST_WIDE_INT const_val = INTVAL (op1);
  1420. unsigned HOST_WIDE_INT uconst_val = const_val;
  1421. unsigned HOST_WIDE_INT max_val
  1422. = (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode);
  1423. switch (code)
  1424. {
  1425. case LE:
  1426. if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
  1427. code = LT, op1 = gen_int_mode (const_val + 1, GET_MODE (op0));
  1428. break;
  1429. /* When cross-compiling, const_val might be sign-extended from
  1430. BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
  1431. case GE:
  1432. if ((HOST_WIDE_INT) (const_val & max_val)
  1433. != (((HOST_WIDE_INT) 1
  1434. << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
  1435. code = GT, op1 = gen_int_mode (const_val - 1, mode);
  1436. break;
  1437. case LEU:
  1438. if (uconst_val < max_val)
  1439. code = LTU, op1 = gen_int_mode (uconst_val + 1, mode);
  1440. break;
  1441. case GEU:
  1442. if (uconst_val != 0)
  1443. code = GTU, op1 = gen_int_mode (uconst_val - 1, mode);
  1444. break;
  1445. default:
  1446. break;
  1447. }
  1448. }
  1449. if (op0 != XEXP (cond, 0)
  1450. || op1 != XEXP (cond, 1)
  1451. || code != GET_CODE (cond)
  1452. || GET_MODE (cond) != SImode)
  1453. cond = gen_rtx_fmt_ee (code, SImode, op0, op1);
  1454. return cond;
  1455. }
  1456. /* Reverses CONDition; returns NULL if we cannot. */
  1457. static rtx
  1458. reversed_condition (rtx cond)
  1459. {
  1460. enum rtx_code reversed;
  1461. reversed = reversed_comparison_code (cond, NULL);
  1462. if (reversed == UNKNOWN)
  1463. return NULL_RTX;
  1464. else
  1465. return gen_rtx_fmt_ee (reversed,
  1466. GET_MODE (cond), XEXP (cond, 0),
  1467. XEXP (cond, 1));
  1468. }
  1469. /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
  1470. set of altered regs. */
  1471. void
  1472. simplify_using_condition (rtx cond, rtx *expr, regset altered)
  1473. {
  1474. rtx rev, reve, exp = *expr;
  1475. /* If some register gets altered later, we do not really speak about its
  1476. value at the time of comparison. */
  1477. if (altered && altered_reg_used (cond, altered))
  1478. return;
  1479. if (GET_CODE (cond) == EQ
  1480. && REG_P (XEXP (cond, 0)) && CONSTANT_P (XEXP (cond, 1)))
  1481. {
  1482. *expr = simplify_replace_rtx (*expr, XEXP (cond, 0), XEXP (cond, 1));
  1483. return;
  1484. }
  1485. if (!COMPARISON_P (exp))
  1486. return;
  1487. rev = reversed_condition (cond);
  1488. reve = reversed_condition (exp);
  1489. cond = canon_condition (cond);
  1490. exp = canon_condition (exp);
  1491. if (rev)
  1492. rev = canon_condition (rev);
  1493. if (reve)
  1494. reve = canon_condition (reve);
  1495. if (rtx_equal_p (exp, cond))
  1496. {
  1497. *expr = const_true_rtx;
  1498. return;
  1499. }
  1500. if (rev && rtx_equal_p (exp, rev))
  1501. {
  1502. *expr = const0_rtx;
  1503. return;
  1504. }
  1505. if (implies_p (cond, exp))
  1506. {
  1507. *expr = const_true_rtx;
  1508. return;
  1509. }
  1510. if (reve && implies_p (cond, reve))
  1511. {
  1512. *expr = const0_rtx;
  1513. return;
  1514. }
  1515. /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
  1516. be false. */
  1517. if (rev && implies_p (exp, rev))
  1518. {
  1519. *expr = const0_rtx;
  1520. return;
  1521. }
  1522. /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
  1523. if (rev && reve && implies_p (reve, rev))
  1524. {
  1525. *expr = const_true_rtx;
  1526. return;
  1527. }
  1528. /* We would like to have some other tests here. TODO. */
  1529. return;
  1530. }
  1531. /* Use relationship between A and *B to eventually eliminate *B.
  1532. OP is the operation we consider. */
  1533. static void
  1534. eliminate_implied_condition (enum rtx_code op, rtx a, rtx *b)
  1535. {
  1536. switch (op)
  1537. {
  1538. case AND:
  1539. /* If A implies *B, we may replace *B by true. */
  1540. if (implies_p (a, *b))
  1541. *b = const_true_rtx;
  1542. break;
  1543. case IOR:
  1544. /* If *B implies A, we may replace *B by false. */
  1545. if (implies_p (*b, a))
  1546. *b = const0_rtx;
  1547. break;
  1548. default:
  1549. gcc_unreachable ();
  1550. }
  1551. }
  1552. /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
  1553. operation we consider. */
  1554. static void
  1555. eliminate_implied_conditions (enum rtx_code op, rtx *head, rtx tail)
  1556. {
  1557. rtx elt;
  1558. for (elt = tail; elt; elt = XEXP (elt, 1))
  1559. eliminate_implied_condition (op, *head, &XEXP (elt, 0));
  1560. for (elt = tail; elt; elt = XEXP (elt, 1))
  1561. eliminate_implied_condition (op, XEXP (elt, 0), head);
  1562. }
  1563. /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
  1564. is a list, its elements are assumed to be combined using OP. */
  1565. static void
  1566. simplify_using_initial_values (struct loop *loop, enum rtx_code op, rtx *expr)
  1567. {
  1568. bool expression_valid;
  1569. rtx head, tail, last_valid_expr;
  1570. rtx_expr_list *cond_list;
  1571. rtx_insn *insn;
  1572. rtx neutral, aggr;
  1573. regset altered, this_altered;
  1574. edge e;
  1575. if (!*expr)
  1576. return;
  1577. if (CONSTANT_P (*expr))
  1578. return;
  1579. if (GET_CODE (*expr) == EXPR_LIST)
  1580. {
  1581. head = XEXP (*expr, 0);
  1582. tail = XEXP (*expr, 1);
  1583. eliminate_implied_conditions (op, &head, tail);
  1584. switch (op)
  1585. {
  1586. case AND:
  1587. neutral = const_true_rtx;
  1588. aggr = const0_rtx;
  1589. break;
  1590. case IOR:
  1591. neutral = const0_rtx;
  1592. aggr = const_true_rtx;
  1593. break;
  1594. default:
  1595. gcc_unreachable ();
  1596. }
  1597. simplify_using_initial_values (loop, UNKNOWN, &head);
  1598. if (head == aggr)
  1599. {
  1600. XEXP (*expr, 0) = aggr;
  1601. XEXP (*expr, 1) = NULL_RTX;
  1602. return;
  1603. }
  1604. else if (head == neutral)
  1605. {
  1606. *expr = tail;
  1607. simplify_using_initial_values (loop, op, expr);
  1608. return;
  1609. }
  1610. simplify_using_initial_values (loop, op, &tail);
  1611. if (tail && XEXP (tail, 0) == aggr)
  1612. {
  1613. *expr = tail;
  1614. return;
  1615. }
  1616. XEXP (*expr, 0) = head;
  1617. XEXP (*expr, 1) = tail;
  1618. return;
  1619. }
  1620. gcc_assert (op == UNKNOWN);
  1621. replace_single_def_regs (expr);
  1622. if (CONSTANT_P (*expr))
  1623. return;
  1624. e = loop_preheader_edge (loop);
  1625. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1626. return;
  1627. altered = ALLOC_REG_SET (&reg_obstack);
  1628. this_altered = ALLOC_REG_SET (&reg_obstack);
  1629. expression_valid = true;
  1630. last_valid_expr = *expr;
  1631. cond_list = NULL;
  1632. while (1)
  1633. {
  1634. insn = BB_END (e->src);
  1635. if (any_condjump_p (insn))
  1636. {
  1637. rtx cond = get_condition (BB_END (e->src), NULL, false, true);
  1638. if (cond && (e->flags & EDGE_FALLTHRU))
  1639. cond = reversed_condition (cond);
  1640. if (cond)
  1641. {
  1642. rtx old = *expr;
  1643. simplify_using_condition (cond, expr, altered);
  1644. if (old != *expr)
  1645. {
  1646. rtx note;
  1647. if (CONSTANT_P (*expr))
  1648. goto out;
  1649. for (note = cond_list; note; note = XEXP (note, 1))
  1650. {
  1651. simplify_using_condition (XEXP (note, 0), expr, altered);
  1652. if (CONSTANT_P (*expr))
  1653. goto out;
  1654. }
  1655. }
  1656. cond_list = alloc_EXPR_LIST (0, cond, cond_list);
  1657. }
  1658. }
  1659. FOR_BB_INSNS_REVERSE (e->src, insn)
  1660. {
  1661. rtx src, dest;
  1662. rtx old = *expr;
  1663. if (!INSN_P (insn))
  1664. continue;
  1665. CLEAR_REG_SET (this_altered);
  1666. note_stores (PATTERN (insn), mark_altered, this_altered);
  1667. if (CALL_P (insn))
  1668. {
  1669. /* Kill all call clobbered registers. */
  1670. unsigned int i;
  1671. hard_reg_set_iterator hrsi;
  1672. EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call,
  1673. 0, i, hrsi)
  1674. SET_REGNO_REG_SET (this_altered, i);
  1675. }
  1676. if (suitable_set_for_replacement (insn, &dest, &src))
  1677. {
  1678. rtx_expr_list **pnote, **pnote_next;
  1679. replace_in_expr (expr, dest, src);
  1680. if (CONSTANT_P (*expr))
  1681. goto out;
  1682. for (pnote = &cond_list; *pnote; pnote = pnote_next)
  1683. {
  1684. rtx note = *pnote;
  1685. rtx old_cond = XEXP (note, 0);
  1686. pnote_next = (rtx_expr_list **)&XEXP (note, 1);
  1687. replace_in_expr (&XEXP (note, 0), dest, src);
  1688. /* We can no longer use a condition that has been simplified
  1689. to a constant, and simplify_using_condition will abort if
  1690. we try. */
  1691. if (CONSTANT_P (XEXP (note, 0)))
  1692. {
  1693. *pnote = *pnote_next;
  1694. pnote_next = pnote;
  1695. free_EXPR_LIST_node (note);
  1696. }
  1697. /* Retry simplifications with this condition if either the
  1698. expression or the condition changed. */
  1699. else if (old_cond != XEXP (note, 0) || old != *expr)
  1700. simplify_using_condition (XEXP (note, 0), expr, altered);
  1701. }
  1702. }
  1703. else
  1704. {
  1705. rtx_expr_list **pnote, **pnote_next;
  1706. /* If we did not use this insn to make a replacement, any overlap
  1707. between stores in this insn and our expression will cause the
  1708. expression to become invalid. */
  1709. if (altered_reg_used (*expr, this_altered))
  1710. goto out;
  1711. /* Likewise for the conditions. */
  1712. for (pnote = &cond_list; *pnote; pnote = pnote_next)
  1713. {
  1714. rtx note = *pnote;
  1715. rtx old_cond = XEXP (note, 0);
  1716. pnote_next = (rtx_expr_list **)&XEXP (note, 1);
  1717. if (altered_reg_used (old_cond, this_altered))
  1718. {
  1719. *pnote = *pnote_next;
  1720. pnote_next = pnote;
  1721. free_EXPR_LIST_node (note);
  1722. }
  1723. }
  1724. }
  1725. if (CONSTANT_P (*expr))
  1726. goto out;
  1727. IOR_REG_SET (altered, this_altered);
  1728. /* If the expression now contains regs that have been altered, we
  1729. can't return it to the caller. However, it is still valid for
  1730. further simplification, so keep searching to see if we can
  1731. eventually turn it into a constant. */
  1732. if (altered_reg_used (*expr, altered))
  1733. expression_valid = false;
  1734. if (expression_valid)
  1735. last_valid_expr = *expr;
  1736. }
  1737. if (!single_pred_p (e->src)
  1738. || single_pred (e->src) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1739. break;
  1740. e = single_pred_edge (e->src);
  1741. }
  1742. out:
  1743. free_EXPR_LIST_list (&cond_list);
  1744. if (!CONSTANT_P (*expr))
  1745. *expr = last_valid_expr;
  1746. FREE_REG_SET (altered);
  1747. FREE_REG_SET (this_altered);
  1748. }
  1749. /* Transforms invariant IV into MODE. Adds assumptions based on the fact
  1750. that IV occurs as left operands of comparison COND and its signedness
  1751. is SIGNED_P to DESC. */
  1752. static void
  1753. shorten_into_mode (struct rtx_iv *iv, machine_mode mode,
  1754. enum rtx_code cond, bool signed_p, struct niter_desc *desc)
  1755. {
  1756. rtx mmin, mmax, cond_over, cond_under;
  1757. get_mode_bounds (mode, signed_p, iv->extend_mode, &mmin, &mmax);
  1758. cond_under = simplify_gen_relational (LT, SImode, iv->extend_mode,
  1759. iv->base, mmin);
  1760. cond_over = simplify_gen_relational (GT, SImode, iv->extend_mode,
  1761. iv->base, mmax);
  1762. switch (cond)
  1763. {
  1764. case LE:
  1765. case LT:
  1766. case LEU:
  1767. case LTU:
  1768. if (cond_under != const0_rtx)
  1769. desc->infinite =
  1770. alloc_EXPR_LIST (0, cond_under, desc->infinite);
  1771. if (cond_over != const0_rtx)
  1772. desc->noloop_assumptions =
  1773. alloc_EXPR_LIST (0, cond_over, desc->noloop_assumptions);
  1774. break;
  1775. case GE:
  1776. case GT:
  1777. case GEU:
  1778. case GTU:
  1779. if (cond_over != const0_rtx)
  1780. desc->infinite =
  1781. alloc_EXPR_LIST (0, cond_over, desc->infinite);
  1782. if (cond_under != const0_rtx)
  1783. desc->noloop_assumptions =
  1784. alloc_EXPR_LIST (0, cond_under, desc->noloop_assumptions);
  1785. break;
  1786. case NE:
  1787. if (cond_over != const0_rtx)
  1788. desc->infinite =
  1789. alloc_EXPR_LIST (0, cond_over, desc->infinite);
  1790. if (cond_under != const0_rtx)
  1791. desc->infinite =
  1792. alloc_EXPR_LIST (0, cond_under, desc->infinite);
  1793. break;
  1794. default:
  1795. gcc_unreachable ();
  1796. }
  1797. iv->mode = mode;
  1798. iv->extend = signed_p ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
  1799. }
  1800. /* Transforms IV0 and IV1 compared by COND so that they are both compared as
  1801. subregs of the same mode if possible (sometimes it is necessary to add
  1802. some assumptions to DESC). */
  1803. static bool
  1804. canonicalize_iv_subregs (struct rtx_iv *iv0, struct rtx_iv *iv1,
  1805. enum rtx_code cond, struct niter_desc *desc)
  1806. {
  1807. machine_mode comp_mode;
  1808. bool signed_p;
  1809. /* If the ivs behave specially in the first iteration, or are
  1810. added/multiplied after extending, we ignore them. */
  1811. if (iv0->first_special || iv0->mult != const1_rtx || iv0->delta != const0_rtx)
  1812. return false;
  1813. if (iv1->first_special || iv1->mult != const1_rtx || iv1->delta != const0_rtx)
  1814. return false;
  1815. /* If there is some extend, it must match signedness of the comparison. */
  1816. switch (cond)
  1817. {
  1818. case LE:
  1819. case LT:
  1820. if (iv0->extend == IV_ZERO_EXTEND
  1821. || iv1->extend == IV_ZERO_EXTEND)
  1822. return false;
  1823. signed_p = true;
  1824. break;
  1825. case LEU:
  1826. case LTU:
  1827. if (iv0->extend == IV_SIGN_EXTEND
  1828. || iv1->extend == IV_SIGN_EXTEND)
  1829. return false;
  1830. signed_p = false;
  1831. break;
  1832. case NE:
  1833. if (iv0->extend != IV_UNKNOWN_EXTEND
  1834. && iv1->extend != IV_UNKNOWN_EXTEND
  1835. && iv0->extend != iv1->extend)
  1836. return false;
  1837. signed_p = false;
  1838. if (iv0->extend != IV_UNKNOWN_EXTEND)
  1839. signed_p = iv0->extend == IV_SIGN_EXTEND;
  1840. if (iv1->extend != IV_UNKNOWN_EXTEND)
  1841. signed_p = iv1->extend == IV_SIGN_EXTEND;
  1842. break;
  1843. default:
  1844. gcc_unreachable ();
  1845. }
  1846. /* Values of both variables should be computed in the same mode. These
  1847. might indeed be different, if we have comparison like
  1848. (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
  1849. and iv0 and iv1 are both ivs iterating in SI mode, but calculated
  1850. in different modes. This does not seem impossible to handle, but
  1851. it hardly ever occurs in practice.
  1852. The only exception is the case when one of operands is invariant.
  1853. For example pentium 3 generates comparisons like
  1854. (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
  1855. definitely do not want this prevent the optimization. */
  1856. comp_mode = iv0->extend_mode;
  1857. if (GET_MODE_BITSIZE (comp_mode) < GET_MODE_BITSIZE (iv1->extend_mode))
  1858. comp_mode = iv1->extend_mode;
  1859. if (iv0->extend_mode != comp_mode)
  1860. {
  1861. if (iv0->mode != iv0->extend_mode
  1862. || iv0->step != const0_rtx)
  1863. return false;
  1864. iv0->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
  1865. comp_mode, iv0->base, iv0->mode);
  1866. iv0->extend_mode = comp_mode;
  1867. }
  1868. if (iv1->extend_mode != comp_mode)
  1869. {
  1870. if (iv1->mode != iv1->extend_mode
  1871. || iv1->step != const0_rtx)
  1872. return false;
  1873. iv1->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
  1874. comp_mode, iv1->base, iv1->mode);
  1875. iv1->extend_mode = comp_mode;
  1876. }
  1877. /* Check that both ivs belong to a range of a single mode. If one of the
  1878. operands is an invariant, we may need to shorten it into the common
  1879. mode. */
  1880. if (iv0->mode == iv0->extend_mode
  1881. && iv0->step == const0_rtx
  1882. && iv0->mode != iv1->mode)
  1883. shorten_into_mode (iv0, iv1->mode, cond, signed_p, desc);
  1884. if (iv1->mode == iv1->extend_mode
  1885. && iv1->step == const0_rtx
  1886. && iv0->mode != iv1->mode)
  1887. shorten_into_mode (iv1, iv0->mode, swap_condition (cond), signed_p, desc);
  1888. if (iv0->mode != iv1->mode)
  1889. return false;
  1890. desc->mode = iv0->mode;
  1891. desc->signed_p = signed_p;
  1892. return true;
  1893. }
  1894. /* Tries to estimate the maximum number of iterations in LOOP, and return the
  1895. result. This function is called from iv_number_of_iterations with
  1896. a number of fields in DESC already filled in. OLD_NITER is the original
  1897. expression for the number of iterations, before we tried to simplify it. */
  1898. static uint64_t
  1899. determine_max_iter (struct loop *loop, struct niter_desc *desc, rtx old_niter)
  1900. {
  1901. rtx niter = desc->niter_expr;
  1902. rtx mmin, mmax, cmp;
  1903. uint64_t nmax, inc;
  1904. uint64_t andmax = 0;
  1905. /* We used to look for constant operand 0 of AND,
  1906. but canonicalization should always make this impossible. */
  1907. gcc_checking_assert (GET_CODE (niter) != AND
  1908. || !CONST_INT_P (XEXP (niter, 0)));
  1909. if (GET_CODE (niter) == AND
  1910. && CONST_INT_P (XEXP (niter, 1)))
  1911. {
  1912. andmax = UINTVAL (XEXP (niter, 1));
  1913. niter = XEXP (niter, 0);
  1914. }
  1915. get_mode_bounds (desc->mode, desc->signed_p, desc->mode, &mmin, &mmax);
  1916. nmax = UINTVAL (mmax) - UINTVAL (mmin);
  1917. if (GET_CODE (niter) == UDIV)
  1918. {
  1919. if (!CONST_INT_P (XEXP (niter, 1)))
  1920. return nmax;
  1921. inc = INTVAL (XEXP (niter, 1));
  1922. niter = XEXP (niter, 0);
  1923. }
  1924. else
  1925. inc = 1;
  1926. /* We could use a binary search here, but for now improving the upper
  1927. bound by just one eliminates one important corner case. */
  1928. cmp = simplify_gen_relational (desc->signed_p ? LT : LTU, VOIDmode,
  1929. desc->mode, old_niter, mmax);
  1930. simplify_using_initial_values (loop, UNKNOWN, &cmp);
  1931. if (cmp == const_true_rtx)
  1932. {
  1933. nmax--;
  1934. if (dump_file)
  1935. fprintf (dump_file, ";; improved upper bound by one.\n");
  1936. }
  1937. nmax /= inc;
  1938. if (andmax)
  1939. nmax = MIN (nmax, andmax);
  1940. if (dump_file)
  1941. fprintf (dump_file, ";; Determined upper bound %"PRId64".\n",
  1942. nmax);
  1943. return nmax;
  1944. }
  1945. /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
  1946. the result into DESC. Very similar to determine_number_of_iterations
  1947. (basically its rtl version), complicated by things like subregs. */
  1948. static void
  1949. iv_number_of_iterations (struct loop *loop, rtx_insn *insn, rtx condition,
  1950. struct niter_desc *desc)
  1951. {
  1952. rtx op0, op1, delta, step, bound, may_xform, tmp, tmp0, tmp1;
  1953. struct rtx_iv iv0, iv1, tmp_iv;
  1954. rtx assumption, may_not_xform;
  1955. enum rtx_code cond;
  1956. machine_mode mode, comp_mode;
  1957. rtx mmin, mmax, mode_mmin, mode_mmax;
  1958. uint64_t s, size, d, inv, max;
  1959. int64_t up, down, inc, step_val;
  1960. int was_sharp = false;
  1961. rtx old_niter;
  1962. bool step_is_pow2;
  1963. /* The meaning of these assumptions is this:
  1964. if !assumptions
  1965. then the rest of information does not have to be valid
  1966. if noloop_assumptions then the loop does not roll
  1967. if infinite then this exit is never used */
  1968. desc->assumptions = NULL_RTX;
  1969. desc->noloop_assumptions = NULL_RTX;
  1970. desc->infinite = NULL_RTX;
  1971. desc->simple_p = true;
  1972. desc->const_iter = false;
  1973. desc->niter_expr = NULL_RTX;
  1974. cond = GET_CODE (condition);
  1975. gcc_assert (COMPARISON_P (condition));
  1976. mode = GET_MODE (XEXP (condition, 0));
  1977. if (mode == VOIDmode)
  1978. mode = GET_MODE (XEXP (condition, 1));
  1979. /* The constant comparisons should be folded. */
  1980. gcc_assert (mode != VOIDmode);
  1981. /* We only handle integers or pointers. */
  1982. if (GET_MODE_CLASS (mode) != MODE_INT
  1983. && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
  1984. goto fail;
  1985. op0 = XEXP (condition, 0);
  1986. if (!iv_analyze (insn, op0, &iv0))
  1987. goto fail;
  1988. if (iv0.extend_mode == VOIDmode)
  1989. iv0.mode = iv0.extend_mode = mode;
  1990. op1 = XEXP (condition, 1);
  1991. if (!iv_analyze (insn, op1, &iv1))
  1992. goto fail;
  1993. if (iv1.extend_mode == VOIDmode)
  1994. iv1.mode = iv1.extend_mode = mode;
  1995. if (GET_MODE_BITSIZE (iv0.extend_mode) > HOST_BITS_PER_WIDE_INT
  1996. || GET_MODE_BITSIZE (iv1.extend_mode) > HOST_BITS_PER_WIDE_INT)
  1997. goto fail;
  1998. /* Check condition and normalize it. */
  1999. switch (cond)
  2000. {
  2001. case GE:
  2002. case GT:
  2003. case GEU:
  2004. case GTU:
  2005. tmp_iv = iv0; iv0 = iv1; iv1 = tmp_iv;
  2006. cond = swap_condition (cond);
  2007. break;
  2008. case NE:
  2009. case LE:
  2010. case LEU:
  2011. case LT:
  2012. case LTU:
  2013. break;
  2014. default:
  2015. goto fail;
  2016. }
  2017. /* Handle extends. This is relatively nontrivial, so we only try in some
  2018. easy cases, when we can canonicalize the ivs (possibly by adding some
  2019. assumptions) to shape subreg (base + i * step). This function also fills
  2020. in desc->mode and desc->signed_p. */
  2021. if (!canonicalize_iv_subregs (&iv0, &iv1, cond, desc))
  2022. goto fail;
  2023. comp_mode = iv0.extend_mode;
  2024. mode = iv0.mode;
  2025. size = GET_MODE_PRECISION (mode);
  2026. get_mode_bounds (mode, (cond == LE || cond == LT), comp_mode, &mmin, &mmax);
  2027. mode_mmin = lowpart_subreg (mode, mmin, comp_mode);
  2028. mode_mmax = lowpart_subreg (mode, mmax, comp_mode);
  2029. if (!CONST_INT_P (iv0.step) || !CONST_INT_P (iv1.step))
  2030. goto fail;
  2031. /* We can take care of the case of two induction variables chasing each other
  2032. if the test is NE. I have never seen a loop using it, but still it is
  2033. cool. */
  2034. if (iv0.step != const0_rtx && iv1.step != const0_rtx)
  2035. {
  2036. if (cond != NE)
  2037. goto fail;
  2038. iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
  2039. iv1.step = const0_rtx;
  2040. }
  2041. iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
  2042. iv1.step = lowpart_subreg (mode, iv1.step, comp_mode);
  2043. /* This is either infinite loop or the one that ends immediately, depending
  2044. on initial values. Unswitching should remove this kind of conditions. */
  2045. if (iv0.step == const0_rtx && iv1.step == const0_rtx)
  2046. goto fail;
  2047. if (cond != NE)
  2048. {
  2049. if (iv0.step == const0_rtx)
  2050. step_val = -INTVAL (iv1.step);
  2051. else
  2052. step_val = INTVAL (iv0.step);
  2053. /* Ignore loops of while (i-- < 10) type. */
  2054. if (step_val < 0)
  2055. goto fail;
  2056. step_is_pow2 = !(step_val & (step_val - 1));
  2057. }
  2058. else
  2059. {
  2060. /* We do not care about whether the step is power of two in this
  2061. case. */
  2062. step_is_pow2 = false;
  2063. step_val = 0;
  2064. }
  2065. /* Some more condition normalization. We must record some assumptions
  2066. due to overflows. */
  2067. switch (cond)
  2068. {
  2069. case LT:
  2070. case LTU:
  2071. /* We want to take care only of non-sharp relationals; this is easy,
  2072. as in cases the overflow would make the transformation unsafe
  2073. the loop does not roll. Seemingly it would make more sense to want
  2074. to take care of sharp relationals instead, as NE is more similar to
  2075. them, but the problem is that here the transformation would be more
  2076. difficult due to possibly infinite loops. */
  2077. if (iv0.step == const0_rtx)
  2078. {
  2079. tmp = lowpart_subreg (mode, iv0.base, comp_mode);
  2080. assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
  2081. mode_mmax);
  2082. if (assumption == const_true_rtx)
  2083. goto zero_iter_simplify;
  2084. iv0.base = simplify_gen_binary (PLUS, comp_mode,
  2085. iv0.base, const1_rtx);
  2086. }
  2087. else
  2088. {
  2089. tmp = lowpart_subreg (mode, iv1.base, comp_mode);
  2090. assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
  2091. mode_mmin);
  2092. if (assumption == const_true_rtx)
  2093. goto zero_iter_simplify;
  2094. iv1.base = simplify_gen_binary (PLUS, comp_mode,
  2095. iv1.base, constm1_rtx);
  2096. }
  2097. if (assumption != const0_rtx)
  2098. desc->noloop_assumptions =
  2099. alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
  2100. cond = (cond == LT) ? LE : LEU;
  2101. /* It will be useful to be able to tell the difference once more in
  2102. LE -> NE reduction. */
  2103. was_sharp = true;
  2104. break;
  2105. default: ;
  2106. }
  2107. /* Take care of trivially infinite loops. */
  2108. if (cond != NE)
  2109. {
  2110. if (iv0.step == const0_rtx)
  2111. {
  2112. tmp = lowpart_subreg (mode, iv0.base, comp_mode);
  2113. if (rtx_equal_p (tmp, mode_mmin))
  2114. {
  2115. desc->infinite =
  2116. alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
  2117. /* Fill in the remaining fields somehow. */
  2118. goto zero_iter_simplify;
  2119. }
  2120. }
  2121. else
  2122. {
  2123. tmp = lowpart_subreg (mode, iv1.base, comp_mode);
  2124. if (rtx_equal_p (tmp, mode_mmax))
  2125. {
  2126. desc->infinite =
  2127. alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
  2128. /* Fill in the remaining fields somehow. */
  2129. goto zero_iter_simplify;
  2130. }
  2131. }
  2132. }
  2133. /* If we can we want to take care of NE conditions instead of size
  2134. comparisons, as they are much more friendly (most importantly
  2135. this takes care of special handling of loops with step 1). We can
  2136. do it if we first check that upper bound is greater or equal to
  2137. lower bound, their difference is constant c modulo step and that
  2138. there is not an overflow. */
  2139. if (cond != NE)
  2140. {
  2141. if (iv0.step == const0_rtx)
  2142. step = simplify_gen_unary (NEG, comp_mode, iv1.step, comp_mode);
  2143. else
  2144. step = iv0.step;
  2145. step = lowpart_subreg (mode, step, comp_mode);
  2146. delta = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
  2147. delta = lowpart_subreg (mode, delta, comp_mode);
  2148. delta = simplify_gen_binary (UMOD, mode, delta, step);
  2149. may_xform = const0_rtx;
  2150. may_not_xform = const_true_rtx;
  2151. if (CONST_INT_P (delta))
  2152. {
  2153. if (was_sharp && INTVAL (delta) == INTVAL (step) - 1)
  2154. {
  2155. /* A special case. We have transformed condition of type
  2156. for (i = 0; i < 4; i += 4)
  2157. into
  2158. for (i = 0; i <= 3; i += 4)
  2159. obviously if the test for overflow during that transformation
  2160. passed, we cannot overflow here. Most importantly any
  2161. loop with sharp end condition and step 1 falls into this
  2162. category, so handling this case specially is definitely
  2163. worth the troubles. */
  2164. may_xform = const_true_rtx;
  2165. }
  2166. else if (iv0.step == const0_rtx)
  2167. {
  2168. bound = simplify_gen_binary (PLUS, comp_mode, mmin, step);
  2169. bound = simplify_gen_binary (MINUS, comp_mode, bound, delta);
  2170. bound = lowpart_subreg (mode, bound, comp_mode);
  2171. tmp = lowpart_subreg (mode, iv0.base, comp_mode);
  2172. may_xform = simplify_gen_relational (cond, SImode, mode,
  2173. bound, tmp);
  2174. may_not_xform = simplify_gen_relational (reverse_condition (cond),
  2175. SImode, mode,
  2176. bound, tmp);
  2177. }
  2178. else
  2179. {
  2180. bound = simplify_gen_binary (MINUS, comp_mode, mmax, step);
  2181. bound = simplify_gen_binary (PLUS, comp_mode, bound, delta);
  2182. bound = lowpart_subreg (mode, bound, comp_mode);
  2183. tmp = lowpart_subreg (mode, iv1.base, comp_mode);
  2184. may_xform = simplify_gen_relational (cond, SImode, mode,
  2185. tmp, bound);
  2186. may_not_xform = simplify_gen_relational (reverse_condition (cond),
  2187. SImode, mode,
  2188. tmp, bound);
  2189. }
  2190. }
  2191. if (may_xform != const0_rtx)
  2192. {
  2193. /* We perform the transformation always provided that it is not
  2194. completely senseless. This is OK, as we would need this assumption
  2195. to determine the number of iterations anyway. */
  2196. if (may_xform != const_true_rtx)
  2197. {
  2198. /* If the step is a power of two and the final value we have
  2199. computed overflows, the cycle is infinite. Otherwise it
  2200. is nontrivial to compute the number of iterations. */
  2201. if (step_is_pow2)
  2202. desc->infinite = alloc_EXPR_LIST (0, may_not_xform,
  2203. desc->infinite);
  2204. else
  2205. desc->assumptions = alloc_EXPR_LIST (0, may_xform,
  2206. desc->assumptions);
  2207. }
  2208. /* We are going to lose some information about upper bound on
  2209. number of iterations in this step, so record the information
  2210. here. */
  2211. inc = INTVAL (iv0.step) - INTVAL (iv1.step);
  2212. if (CONST_INT_P (iv1.base))
  2213. up = INTVAL (iv1.base);
  2214. else
  2215. up = INTVAL (mode_mmax) - inc;
  2216. down = INTVAL (CONST_INT_P (iv0.base)
  2217. ? iv0.base
  2218. : mode_mmin);
  2219. max = (uint64_t) (up - down) / inc + 1;
  2220. if (!desc->infinite
  2221. && !desc->assumptions)
  2222. record_niter_bound (loop, max, false, true);
  2223. if (iv0.step == const0_rtx)
  2224. {
  2225. iv0.base = simplify_gen_binary (PLUS, comp_mode, iv0.base, delta);
  2226. iv0.base = simplify_gen_binary (MINUS, comp_mode, iv0.base, step);
  2227. }
  2228. else
  2229. {
  2230. iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, delta);
  2231. iv1.base = simplify_gen_binary (PLUS, comp_mode, iv1.base, step);
  2232. }
  2233. tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
  2234. tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
  2235. assumption = simplify_gen_relational (reverse_condition (cond),
  2236. SImode, mode, tmp0, tmp1);
  2237. if (assumption == const_true_rtx)
  2238. goto zero_iter_simplify;
  2239. else if (assumption != const0_rtx)
  2240. desc->noloop_assumptions =
  2241. alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
  2242. cond = NE;
  2243. }
  2244. }
  2245. /* Count the number of iterations. */
  2246. if (cond == NE)
  2247. {
  2248. /* Everything we do here is just arithmetics modulo size of mode. This
  2249. makes us able to do more involved computations of number of iterations
  2250. than in other cases. First transform the condition into shape
  2251. s * i <> c, with s positive. */
  2252. iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
  2253. iv0.base = const0_rtx;
  2254. iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
  2255. iv1.step = const0_rtx;
  2256. if (INTVAL (iv0.step) < 0)
  2257. {
  2258. iv0.step = simplify_gen_unary (NEG, comp_mode, iv0.step, comp_mode);
  2259. iv1.base = simplify_gen_unary (NEG, comp_mode, iv1.base, comp_mode);
  2260. }
  2261. iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
  2262. /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
  2263. is infinite. Otherwise, the number of iterations is
  2264. (inverse(s/d) * (c/d)) mod (size of mode/d). */
  2265. s = INTVAL (iv0.step); d = 1;
  2266. while (s % 2 != 1)
  2267. {
  2268. s /= 2;
  2269. d *= 2;
  2270. size--;
  2271. }
  2272. bound = GEN_INT (((uint64_t) 1 << (size - 1 ) << 1) - 1);
  2273. tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
  2274. tmp = simplify_gen_binary (UMOD, mode, tmp1, gen_int_mode (d, mode));
  2275. assumption = simplify_gen_relational (NE, SImode, mode, tmp, const0_rtx);
  2276. desc->infinite = alloc_EXPR_LIST (0, assumption, desc->infinite);
  2277. tmp = simplify_gen_binary (UDIV, mode, tmp1, gen_int_mode (d, mode));
  2278. inv = inverse (s, size);
  2279. tmp = simplify_gen_binary (MULT, mode, tmp, gen_int_mode (inv, mode));
  2280. desc->niter_expr = simplify_gen_binary (AND, mode, tmp, bound);
  2281. }
  2282. else
  2283. {
  2284. if (iv1.step == const0_rtx)
  2285. /* Condition in shape a + s * i <= b
  2286. We must know that b + s does not overflow and a <= b + s and then we
  2287. can compute number of iterations as (b + s - a) / s. (It might
  2288. seem that we in fact could be more clever about testing the b + s
  2289. overflow condition using some information about b - a mod s,
  2290. but it was already taken into account during LE -> NE transform). */
  2291. {
  2292. step = iv0.step;
  2293. tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
  2294. tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
  2295. bound = simplify_gen_binary (MINUS, mode, mode_mmax,
  2296. lowpart_subreg (mode, step,
  2297. comp_mode));
  2298. if (step_is_pow2)
  2299. {
  2300. rtx t0, t1;
  2301. /* If s is power of 2, we know that the loop is infinite if
  2302. a % s <= b % s and b + s overflows. */
  2303. assumption = simplify_gen_relational (reverse_condition (cond),
  2304. SImode, mode,
  2305. tmp1, bound);
  2306. t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
  2307. t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
  2308. tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
  2309. assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
  2310. desc->infinite =
  2311. alloc_EXPR_LIST (0, assumption, desc->infinite);
  2312. }
  2313. else
  2314. {
  2315. assumption = simplify_gen_relational (cond, SImode, mode,
  2316. tmp1, bound);
  2317. desc->assumptions =
  2318. alloc_EXPR_LIST (0, assumption, desc->assumptions);
  2319. }
  2320. tmp = simplify_gen_binary (PLUS, comp_mode, iv1.base, iv0.step);
  2321. tmp = lowpart_subreg (mode, tmp, comp_mode);
  2322. assumption = simplify_gen_relational (reverse_condition (cond),
  2323. SImode, mode, tmp0, tmp);
  2324. delta = simplify_gen_binary (PLUS, mode, tmp1, step);
  2325. delta = simplify_gen_binary (MINUS, mode, delta, tmp0);
  2326. }
  2327. else
  2328. {
  2329. /* Condition in shape a <= b - s * i
  2330. We must know that a - s does not overflow and a - s <= b and then
  2331. we can again compute number of iterations as (b - (a - s)) / s. */
  2332. step = simplify_gen_unary (NEG, mode, iv1.step, mode);
  2333. tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
  2334. tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
  2335. bound = simplify_gen_binary (PLUS, mode, mode_mmin,
  2336. lowpart_subreg (mode, step, comp_mode));
  2337. if (step_is_pow2)
  2338. {
  2339. rtx t0, t1;
  2340. /* If s is power of 2, we know that the loop is infinite if
  2341. a % s <= b % s and a - s overflows. */
  2342. assumption = simplify_gen_relational (reverse_condition (cond),
  2343. SImode, mode,
  2344. bound, tmp0);
  2345. t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
  2346. t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
  2347. tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
  2348. assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
  2349. desc->infinite =
  2350. alloc_EXPR_LIST (0, assumption, desc->infinite);
  2351. }
  2352. else
  2353. {
  2354. assumption = simplify_gen_relational (cond, SImode, mode,
  2355. bound, tmp0);
  2356. desc->assumptions =
  2357. alloc_EXPR_LIST (0, assumption, desc->assumptions);
  2358. }
  2359. tmp = simplify_gen_binary (PLUS, comp_mode, iv0.base, iv1.step);
  2360. tmp = lowpart_subreg (mode, tmp, comp_mode);
  2361. assumption = simplify_gen_relational (reverse_condition (cond),
  2362. SImode, mode,
  2363. tmp, tmp1);
  2364. delta = simplify_gen_binary (MINUS, mode, tmp0, step);
  2365. delta = simplify_gen_binary (MINUS, mode, tmp1, delta);
  2366. }
  2367. if (assumption == const_true_rtx)
  2368. goto zero_iter_simplify;
  2369. else if (assumption != const0_rtx)
  2370. desc->noloop_assumptions =
  2371. alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
  2372. delta = simplify_gen_binary (UDIV, mode, delta, step);
  2373. desc->niter_expr = delta;
  2374. }
  2375. old_niter = desc->niter_expr;
  2376. simplify_using_initial_values (loop, AND, &desc->assumptions);
  2377. if (desc->assumptions
  2378. && XEXP (desc->assumptions, 0) == const0_rtx)
  2379. goto fail;
  2380. simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
  2381. simplify_using_initial_values (loop, IOR, &desc->infinite);
  2382. simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
  2383. /* Rerun the simplification. Consider code (created by copying loop headers)
  2384. i = 0;
  2385. if (0 < n)
  2386. {
  2387. do
  2388. {
  2389. i++;
  2390. } while (i < n);
  2391. }
  2392. The first pass determines that i = 0, the second pass uses it to eliminate
  2393. noloop assumption. */
  2394. simplify_using_initial_values (loop, AND, &desc->assumptions);
  2395. if (desc->assumptions
  2396. && XEXP (desc->assumptions, 0) == const0_rtx)
  2397. goto fail;
  2398. simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
  2399. simplify_using_initial_values (loop, IOR, &desc->infinite);
  2400. simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
  2401. if (desc->noloop_assumptions
  2402. && XEXP (desc->noloop_assumptions, 0) == const_true_rtx)
  2403. goto zero_iter;
  2404. if (CONST_INT_P (desc->niter_expr))
  2405. {
  2406. uint64_t val = INTVAL (desc->niter_expr);
  2407. desc->const_iter = true;
  2408. desc->niter = val & GET_MODE_MASK (desc->mode);
  2409. if (!desc->infinite
  2410. && !desc->assumptions)
  2411. record_niter_bound (loop, desc->niter, false, true);
  2412. }
  2413. else
  2414. {
  2415. max = determine_max_iter (loop, desc, old_niter);
  2416. if (!max)
  2417. goto zero_iter_simplify;
  2418. if (!desc->infinite
  2419. && !desc->assumptions)
  2420. record_niter_bound (loop, max, false, true);
  2421. /* simplify_using_initial_values does a copy propagation on the registers
  2422. in the expression for the number of iterations. This prolongs life
  2423. ranges of registers and increases register pressure, and usually
  2424. brings no gain (and if it happens to do, the cse pass will take care
  2425. of it anyway). So prevent this behavior, unless it enabled us to
  2426. derive that the number of iterations is a constant. */
  2427. desc->niter_expr = old_niter;
  2428. }
  2429. return;
  2430. zero_iter_simplify:
  2431. /* Simplify the assumptions. */
  2432. simplify_using_initial_values (loop, AND, &desc->assumptions);
  2433. if (desc->assumptions
  2434. && XEXP (desc->assumptions, 0) == const0_rtx)
  2435. goto fail;
  2436. simplify_using_initial_values (loop, IOR, &desc->infinite);
  2437. /* Fallthru. */
  2438. zero_iter:
  2439. desc->const_iter = true;
  2440. desc->niter = 0;
  2441. record_niter_bound (loop, 0, true, true);
  2442. desc->noloop_assumptions = NULL_RTX;
  2443. desc->niter_expr = const0_rtx;
  2444. return;
  2445. fail:
  2446. desc->simple_p = false;
  2447. return;
  2448. }
  2449. /* Checks whether E is a simple exit from LOOP and stores its description
  2450. into DESC. */
  2451. static void
  2452. check_simple_exit (struct loop *loop, edge e, struct niter_desc *desc)
  2453. {
  2454. basic_block exit_bb;
  2455. rtx condition;
  2456. rtx_insn *at;
  2457. edge ein;
  2458. exit_bb = e->src;
  2459. desc->simple_p = false;
  2460. /* It must belong directly to the loop. */
  2461. if (exit_bb->loop_father != loop)
  2462. return;
  2463. /* It must be tested (at least) once during any iteration. */
  2464. if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit_bb))
  2465. return;
  2466. /* It must end in a simple conditional jump. */
  2467. if (!any_condjump_p (BB_END (exit_bb)))
  2468. return;
  2469. ein = EDGE_SUCC (exit_bb, 0);
  2470. if (ein == e)
  2471. ein = EDGE_SUCC (exit_bb, 1);
  2472. desc->out_edge = e;
  2473. desc->in_edge = ein;
  2474. /* Test whether the condition is suitable. */
  2475. if (!(condition = get_condition (BB_END (ein->src), &at, false, false)))
  2476. return;
  2477. if (ein->flags & EDGE_FALLTHRU)
  2478. {
  2479. condition = reversed_condition (condition);
  2480. if (!condition)
  2481. return;
  2482. }
  2483. /* Check that we are able to determine number of iterations and fill
  2484. in information about it. */
  2485. iv_number_of_iterations (loop, at, condition, desc);
  2486. }
  2487. /* Finds a simple exit of LOOP and stores its description into DESC. */
  2488. void
  2489. find_simple_exit (struct loop *loop, struct niter_desc *desc)
  2490. {
  2491. unsigned i;
  2492. basic_block *body;
  2493. edge e;
  2494. struct niter_desc act;
  2495. bool any = false;
  2496. edge_iterator ei;
  2497. desc->simple_p = false;
  2498. body = get_loop_body (loop);
  2499. for (i = 0; i < loop->num_nodes; i++)
  2500. {
  2501. FOR_EACH_EDGE (e, ei, body[i]->succs)
  2502. {
  2503. if (flow_bb_inside_loop_p (loop, e->dest))
  2504. continue;
  2505. check_simple_exit (loop, e, &act);
  2506. if (!act.simple_p)
  2507. continue;
  2508. if (!any)
  2509. any = true;
  2510. else
  2511. {
  2512. /* Prefer constant iterations; the less the better. */
  2513. if (!act.const_iter
  2514. || (desc->const_iter && act.niter >= desc->niter))
  2515. continue;
  2516. /* Also if the actual exit may be infinite, while the old one
  2517. not, prefer the old one. */
  2518. if (act.infinite && !desc->infinite)
  2519. continue;
  2520. }
  2521. *desc = act;
  2522. }
  2523. }
  2524. if (dump_file)
  2525. {
  2526. if (desc->simple_p)
  2527. {
  2528. fprintf (dump_file, "Loop %d is simple:\n", loop->num);
  2529. fprintf (dump_file, " simple exit %d -> %d\n",
  2530. desc->out_edge->src->index,
  2531. desc->out_edge->dest->index);
  2532. if (desc->assumptions)
  2533. {
  2534. fprintf (dump_file, " assumptions: ");
  2535. print_rtl (dump_file, desc->assumptions);
  2536. fprintf (dump_file, "\n");
  2537. }
  2538. if (desc->noloop_assumptions)
  2539. {
  2540. fprintf (dump_file, " does not roll if: ");
  2541. print_rtl (dump_file, desc->noloop_assumptions);
  2542. fprintf (dump_file, "\n");
  2543. }
  2544. if (desc->infinite)
  2545. {
  2546. fprintf (dump_file, " infinite if: ");
  2547. print_rtl (dump_file, desc->infinite);
  2548. fprintf (dump_file, "\n");
  2549. }
  2550. fprintf (dump_file, " number of iterations: ");
  2551. print_rtl (dump_file, desc->niter_expr);
  2552. fprintf (dump_file, "\n");
  2553. fprintf (dump_file, " upper bound: %li\n",
  2554. (long)get_max_loop_iterations_int (loop));
  2555. fprintf (dump_file, " realistic bound: %li\n",
  2556. (long)get_estimated_loop_iterations_int (loop));
  2557. }
  2558. else
  2559. fprintf (dump_file, "Loop %d is not simple.\n", loop->num);
  2560. }
  2561. free (body);
  2562. }
  2563. /* Creates a simple loop description of LOOP if it was not computed
  2564. already. */
  2565. struct niter_desc *
  2566. get_simple_loop_desc (struct loop *loop)
  2567. {
  2568. struct niter_desc *desc = simple_loop_desc (loop);
  2569. if (desc)
  2570. return desc;
  2571. /* At least desc->infinite is not always initialized by
  2572. find_simple_loop_exit. */
  2573. desc = ggc_cleared_alloc<niter_desc> ();
  2574. iv_analysis_loop_init (loop);
  2575. find_simple_exit (loop, desc);
  2576. loop->simple_loop_desc = desc;
  2577. if (desc->simple_p && (desc->assumptions || desc->infinite))
  2578. {
  2579. const char *wording;
  2580. /* Assume that no overflow happens and that the loop is finite.
  2581. We already warned at the tree level if we ran optimizations there. */
  2582. if (!flag_tree_loop_optimize && warn_unsafe_loop_optimizations)
  2583. {
  2584. if (desc->infinite)
  2585. {
  2586. wording =
  2587. flag_unsafe_loop_optimizations
  2588. ? N_("assuming that the loop is not infinite")
  2589. : N_("cannot optimize possibly infinite loops");
  2590. warning (OPT_Wunsafe_loop_optimizations, "%s",
  2591. gettext (wording));
  2592. }
  2593. if (desc->assumptions)
  2594. {
  2595. wording =
  2596. flag_unsafe_loop_optimizations
  2597. ? N_("assuming that the loop counter does not overflow")
  2598. : N_("cannot optimize loop, the loop counter may overflow");
  2599. warning (OPT_Wunsafe_loop_optimizations, "%s",
  2600. gettext (wording));
  2601. }
  2602. }
  2603. if (flag_unsafe_loop_optimizations)
  2604. {
  2605. desc->assumptions = NULL_RTX;
  2606. desc->infinite = NULL_RTX;
  2607. }
  2608. }
  2609. return desc;
  2610. }
  2611. /* Releases simple loop description for LOOP. */
  2612. void
  2613. free_simple_loop_desc (struct loop *loop)
  2614. {
  2615. struct niter_desc *desc = simple_loop_desc (loop);
  2616. if (!desc)
  2617. return;
  2618. ggc_free (desc);
  2619. loop->simple_loop_desc = NULL;
  2620. }