tree-ssa-loop-im.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630
  1. /* Loop invariant motion.
  2. Copyright (C) 2003-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. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "tree.h"
  29. #include "fold-const.h"
  30. #include "tm_p.h"
  31. #include "predict.h"
  32. #include "hard-reg-set.h"
  33. #include "input.h"
  34. #include "function.h"
  35. #include "dominance.h"
  36. #include "cfg.h"
  37. #include "cfganal.h"
  38. #include "basic-block.h"
  39. #include "gimple-pretty-print.h"
  40. #include "hash-map.h"
  41. #include "hash-table.h"
  42. #include "tree-ssa-alias.h"
  43. #include "internal-fn.h"
  44. #include "tree-eh.h"
  45. #include "gimple-expr.h"
  46. #include "is-a.h"
  47. #include "gimple.h"
  48. #include "gimplify.h"
  49. #include "gimple-iterator.h"
  50. #include "gimple-ssa.h"
  51. #include "tree-cfg.h"
  52. #include "tree-phinodes.h"
  53. #include "ssa-iterators.h"
  54. #include "stringpool.h"
  55. #include "tree-ssanames.h"
  56. #include "tree-ssa-loop-manip.h"
  57. #include "tree-ssa-loop.h"
  58. #include "tree-into-ssa.h"
  59. #include "cfgloop.h"
  60. #include "domwalk.h"
  61. #include "params.h"
  62. #include "tree-pass.h"
  63. #include "flags.h"
  64. #include "tree-affine.h"
  65. #include "tree-ssa-propagate.h"
  66. #include "trans-mem.h"
  67. #include "gimple-fold.h"
  68. /* TODO: Support for predicated code motion. I.e.
  69. while (1)
  70. {
  71. if (cond)
  72. {
  73. a = inv;
  74. something;
  75. }
  76. }
  77. Where COND and INV are invariants, but evaluating INV may trap or be
  78. invalid from some other reason if !COND. This may be transformed to
  79. if (cond)
  80. a = inv;
  81. while (1)
  82. {
  83. if (cond)
  84. something;
  85. } */
  86. /* The auxiliary data kept for each statement. */
  87. struct lim_aux_data
  88. {
  89. struct loop *max_loop; /* The outermost loop in that the statement
  90. is invariant. */
  91. struct loop *tgt_loop; /* The loop out of that we want to move the
  92. invariant. */
  93. struct loop *always_executed_in;
  94. /* The outermost loop for that we are sure
  95. the statement is executed if the loop
  96. is entered. */
  97. unsigned cost; /* Cost of the computation performed by the
  98. statement. */
  99. vec<gimple> depends; /* Vector of statements that must be also
  100. hoisted out of the loop when this statement
  101. is hoisted; i.e. those that define the
  102. operands of the statement and are inside of
  103. the MAX_LOOP loop. */
  104. };
  105. /* Maps statements to their lim_aux_data. */
  106. static hash_map<gimple, lim_aux_data *> *lim_aux_data_map;
  107. /* Description of a memory reference location. */
  108. typedef struct mem_ref_loc
  109. {
  110. tree *ref; /* The reference itself. */
  111. gimple stmt; /* The statement in that it occurs. */
  112. } *mem_ref_loc_p;
  113. /* Description of a memory reference. */
  114. typedef struct im_mem_ref
  115. {
  116. unsigned id; /* ID assigned to the memory reference
  117. (its index in memory_accesses.refs_list) */
  118. hashval_t hash; /* Its hash value. */
  119. /* The memory access itself and associated caching of alias-oracle
  120. query meta-data. */
  121. ao_ref mem;
  122. bitmap stored; /* The set of loops in that this memory location
  123. is stored to. */
  124. vec<mem_ref_loc> accesses_in_loop;
  125. /* The locations of the accesses. Vector
  126. indexed by the loop number. */
  127. /* The following sets are computed on demand. We keep both set and
  128. its complement, so that we know whether the information was
  129. already computed or not. */
  130. bitmap_head indep_loop; /* The set of loops in that the memory
  131. reference is independent, meaning:
  132. If it is stored in the loop, this store
  133. is independent on all other loads and
  134. stores.
  135. If it is only loaded, then it is independent
  136. on all stores in the loop. */
  137. bitmap_head dep_loop; /* The complement of INDEP_LOOP. */
  138. } *mem_ref_p;
  139. /* We use two bits per loop in the ref->{in,}dep_loop bitmaps, the first
  140. to record (in)dependence against stores in the loop and its subloops, the
  141. second to record (in)dependence against all references in the loop
  142. and its subloops. */
  143. #define LOOP_DEP_BIT(loopnum, storedp) (2 * (loopnum) + (storedp ? 1 : 0))
  144. /* Mem_ref hashtable helpers. */
  145. struct mem_ref_hasher : typed_noop_remove <im_mem_ref>
  146. {
  147. typedef im_mem_ref value_type;
  148. typedef tree_node compare_type;
  149. static inline hashval_t hash (const value_type *);
  150. static inline bool equal (const value_type *, const compare_type *);
  151. };
  152. /* A hash function for struct im_mem_ref object OBJ. */
  153. inline hashval_t
  154. mem_ref_hasher::hash (const value_type *mem)
  155. {
  156. return mem->hash;
  157. }
  158. /* An equality function for struct im_mem_ref object MEM1 with
  159. memory reference OBJ2. */
  160. inline bool
  161. mem_ref_hasher::equal (const value_type *mem1, const compare_type *obj2)
  162. {
  163. return operand_equal_p (mem1->mem.ref, (const_tree) obj2, 0);
  164. }
  165. /* Description of memory accesses in loops. */
  166. static struct
  167. {
  168. /* The hash table of memory references accessed in loops. */
  169. hash_table<mem_ref_hasher> *refs;
  170. /* The list of memory references. */
  171. vec<mem_ref_p> refs_list;
  172. /* The set of memory references accessed in each loop. */
  173. vec<bitmap_head> refs_in_loop;
  174. /* The set of memory references stored in each loop. */
  175. vec<bitmap_head> refs_stored_in_loop;
  176. /* The set of memory references stored in each loop, including subloops . */
  177. vec<bitmap_head> all_refs_stored_in_loop;
  178. /* Cache for expanding memory addresses. */
  179. hash_map<tree, name_expansion *> *ttae_cache;
  180. } memory_accesses;
  181. /* Obstack for the bitmaps in the above data structures. */
  182. static bitmap_obstack lim_bitmap_obstack;
  183. static obstack mem_ref_obstack;
  184. static bool ref_indep_loop_p (struct loop *, mem_ref_p);
  185. /* Minimum cost of an expensive expression. */
  186. #define LIM_EXPENSIVE ((unsigned) PARAM_VALUE (PARAM_LIM_EXPENSIVE))
  187. /* The outermost loop for which execution of the header guarantees that the
  188. block will be executed. */
  189. #define ALWAYS_EXECUTED_IN(BB) ((struct loop *) (BB)->aux)
  190. #define SET_ALWAYS_EXECUTED_IN(BB, VAL) ((BB)->aux = (void *) (VAL))
  191. /* ID of the shared unanalyzable mem. */
  192. #define UNANALYZABLE_MEM_ID 0
  193. /* Whether the reference was analyzable. */
  194. #define MEM_ANALYZABLE(REF) ((REF)->id != UNANALYZABLE_MEM_ID)
  195. static struct lim_aux_data *
  196. init_lim_data (gimple stmt)
  197. {
  198. lim_aux_data *p = XCNEW (struct lim_aux_data);
  199. lim_aux_data_map->put (stmt, p);
  200. return p;
  201. }
  202. static struct lim_aux_data *
  203. get_lim_data (gimple stmt)
  204. {
  205. lim_aux_data **p = lim_aux_data_map->get (stmt);
  206. if (!p)
  207. return NULL;
  208. return *p;
  209. }
  210. /* Releases the memory occupied by DATA. */
  211. static void
  212. free_lim_aux_data (struct lim_aux_data *data)
  213. {
  214. data->depends.release ();
  215. free (data);
  216. }
  217. static void
  218. clear_lim_data (gimple stmt)
  219. {
  220. lim_aux_data **p = lim_aux_data_map->get (stmt);
  221. if (!p)
  222. return;
  223. free_lim_aux_data (*p);
  224. *p = NULL;
  225. }
  226. /* The possibilities of statement movement. */
  227. enum move_pos
  228. {
  229. MOVE_IMPOSSIBLE, /* No movement -- side effect expression. */
  230. MOVE_PRESERVE_EXECUTION, /* Must not cause the non-executed statement
  231. become executed -- memory accesses, ... */
  232. MOVE_POSSIBLE /* Unlimited movement. */
  233. };
  234. /* If it is possible to hoist the statement STMT unconditionally,
  235. returns MOVE_POSSIBLE.
  236. If it is possible to hoist the statement STMT, but we must avoid making
  237. it executed if it would not be executed in the original program (e.g.
  238. because it may trap), return MOVE_PRESERVE_EXECUTION.
  239. Otherwise return MOVE_IMPOSSIBLE. */
  240. enum move_pos
  241. movement_possibility (gimple stmt)
  242. {
  243. tree lhs;
  244. enum move_pos ret = MOVE_POSSIBLE;
  245. if (flag_unswitch_loops
  246. && gimple_code (stmt) == GIMPLE_COND)
  247. {
  248. /* If we perform unswitching, force the operands of the invariant
  249. condition to be moved out of the loop. */
  250. return MOVE_POSSIBLE;
  251. }
  252. if (gimple_code (stmt) == GIMPLE_PHI
  253. && gimple_phi_num_args (stmt) <= 2
  254. && !virtual_operand_p (gimple_phi_result (stmt))
  255. && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt)))
  256. return MOVE_POSSIBLE;
  257. if (gimple_get_lhs (stmt) == NULL_TREE)
  258. return MOVE_IMPOSSIBLE;
  259. if (gimple_vdef (stmt))
  260. return MOVE_IMPOSSIBLE;
  261. if (stmt_ends_bb_p (stmt)
  262. || gimple_has_volatile_ops (stmt)
  263. || gimple_has_side_effects (stmt)
  264. || stmt_could_throw_p (stmt))
  265. return MOVE_IMPOSSIBLE;
  266. if (is_gimple_call (stmt))
  267. {
  268. /* While pure or const call is guaranteed to have no side effects, we
  269. cannot move it arbitrarily. Consider code like
  270. char *s = something ();
  271. while (1)
  272. {
  273. if (s)
  274. t = strlen (s);
  275. else
  276. t = 0;
  277. }
  278. Here the strlen call cannot be moved out of the loop, even though
  279. s is invariant. In addition to possibly creating a call with
  280. invalid arguments, moving out a function call that is not executed
  281. may cause performance regressions in case the call is costly and
  282. not executed at all. */
  283. ret = MOVE_PRESERVE_EXECUTION;
  284. lhs = gimple_call_lhs (stmt);
  285. }
  286. else if (is_gimple_assign (stmt))
  287. lhs = gimple_assign_lhs (stmt);
  288. else
  289. return MOVE_IMPOSSIBLE;
  290. if (TREE_CODE (lhs) == SSA_NAME
  291. && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
  292. return MOVE_IMPOSSIBLE;
  293. if (TREE_CODE (lhs) != SSA_NAME
  294. || gimple_could_trap_p (stmt))
  295. return MOVE_PRESERVE_EXECUTION;
  296. /* Non local loads in a transaction cannot be hoisted out. Well,
  297. unless the load happens on every path out of the loop, but we
  298. don't take this into account yet. */
  299. if (flag_tm
  300. && gimple_in_transaction (stmt)
  301. && gimple_assign_single_p (stmt))
  302. {
  303. tree rhs = gimple_assign_rhs1 (stmt);
  304. if (DECL_P (rhs) && is_global_var (rhs))
  305. {
  306. if (dump_file)
  307. {
  308. fprintf (dump_file, "Cannot hoist conditional load of ");
  309. print_generic_expr (dump_file, rhs, TDF_SLIM);
  310. fprintf (dump_file, " because it is in a transaction.\n");
  311. }
  312. return MOVE_IMPOSSIBLE;
  313. }
  314. }
  315. return ret;
  316. }
  317. /* Suppose that operand DEF is used inside the LOOP. Returns the outermost
  318. loop to that we could move the expression using DEF if it did not have
  319. other operands, i.e. the outermost loop enclosing LOOP in that the value
  320. of DEF is invariant. */
  321. static struct loop *
  322. outermost_invariant_loop (tree def, struct loop *loop)
  323. {
  324. gimple def_stmt;
  325. basic_block def_bb;
  326. struct loop *max_loop;
  327. struct lim_aux_data *lim_data;
  328. if (!def)
  329. return superloop_at_depth (loop, 1);
  330. if (TREE_CODE (def) != SSA_NAME)
  331. {
  332. gcc_assert (is_gimple_min_invariant (def));
  333. return superloop_at_depth (loop, 1);
  334. }
  335. def_stmt = SSA_NAME_DEF_STMT (def);
  336. def_bb = gimple_bb (def_stmt);
  337. if (!def_bb)
  338. return superloop_at_depth (loop, 1);
  339. max_loop = find_common_loop (loop, def_bb->loop_father);
  340. lim_data = get_lim_data (def_stmt);
  341. if (lim_data != NULL && lim_data->max_loop != NULL)
  342. max_loop = find_common_loop (max_loop,
  343. loop_outer (lim_data->max_loop));
  344. if (max_loop == loop)
  345. return NULL;
  346. max_loop = superloop_at_depth (loop, loop_depth (max_loop) + 1);
  347. return max_loop;
  348. }
  349. /* DATA is a structure containing information associated with a statement
  350. inside LOOP. DEF is one of the operands of this statement.
  351. Find the outermost loop enclosing LOOP in that value of DEF is invariant
  352. and record this in DATA->max_loop field. If DEF itself is defined inside
  353. this loop as well (i.e. we need to hoist it out of the loop if we want
  354. to hoist the statement represented by DATA), record the statement in that
  355. DEF is defined to the DATA->depends list. Additionally if ADD_COST is true,
  356. add the cost of the computation of DEF to the DATA->cost.
  357. If DEF is not invariant in LOOP, return false. Otherwise return TRUE. */
  358. static bool
  359. add_dependency (tree def, struct lim_aux_data *data, struct loop *loop,
  360. bool add_cost)
  361. {
  362. gimple def_stmt = SSA_NAME_DEF_STMT (def);
  363. basic_block def_bb = gimple_bb (def_stmt);
  364. struct loop *max_loop;
  365. struct lim_aux_data *def_data;
  366. if (!def_bb)
  367. return true;
  368. max_loop = outermost_invariant_loop (def, loop);
  369. if (!max_loop)
  370. return false;
  371. if (flow_loop_nested_p (data->max_loop, max_loop))
  372. data->max_loop = max_loop;
  373. def_data = get_lim_data (def_stmt);
  374. if (!def_data)
  375. return true;
  376. if (add_cost
  377. /* Only add the cost if the statement defining DEF is inside LOOP,
  378. i.e. if it is likely that by moving the invariants dependent
  379. on it, we will be able to avoid creating a new register for
  380. it (since it will be only used in these dependent invariants). */
  381. && def_bb->loop_father == loop)
  382. data->cost += def_data->cost;
  383. data->depends.safe_push (def_stmt);
  384. return true;
  385. }
  386. /* Returns an estimate for a cost of statement STMT. The values here
  387. are just ad-hoc constants, similar to costs for inlining. */
  388. static unsigned
  389. stmt_cost (gimple stmt)
  390. {
  391. /* Always try to create possibilities for unswitching. */
  392. if (gimple_code (stmt) == GIMPLE_COND
  393. || gimple_code (stmt) == GIMPLE_PHI)
  394. return LIM_EXPENSIVE;
  395. /* We should be hoisting calls if possible. */
  396. if (is_gimple_call (stmt))
  397. {
  398. tree fndecl;
  399. /* Unless the call is a builtin_constant_p; this always folds to a
  400. constant, so moving it is useless. */
  401. fndecl = gimple_call_fndecl (stmt);
  402. if (fndecl
  403. && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
  404. && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
  405. return 0;
  406. return LIM_EXPENSIVE;
  407. }
  408. /* Hoisting memory references out should almost surely be a win. */
  409. if (gimple_references_memory_p (stmt))
  410. return LIM_EXPENSIVE;
  411. if (gimple_code (stmt) != GIMPLE_ASSIGN)
  412. return 1;
  413. switch (gimple_assign_rhs_code (stmt))
  414. {
  415. case MULT_EXPR:
  416. case WIDEN_MULT_EXPR:
  417. case WIDEN_MULT_PLUS_EXPR:
  418. case WIDEN_MULT_MINUS_EXPR:
  419. case DOT_PROD_EXPR:
  420. case FMA_EXPR:
  421. case TRUNC_DIV_EXPR:
  422. case CEIL_DIV_EXPR:
  423. case FLOOR_DIV_EXPR:
  424. case ROUND_DIV_EXPR:
  425. case EXACT_DIV_EXPR:
  426. case CEIL_MOD_EXPR:
  427. case FLOOR_MOD_EXPR:
  428. case ROUND_MOD_EXPR:
  429. case TRUNC_MOD_EXPR:
  430. case RDIV_EXPR:
  431. /* Division and multiplication are usually expensive. */
  432. return LIM_EXPENSIVE;
  433. case LSHIFT_EXPR:
  434. case RSHIFT_EXPR:
  435. case WIDEN_LSHIFT_EXPR:
  436. case LROTATE_EXPR:
  437. case RROTATE_EXPR:
  438. /* Shifts and rotates are usually expensive. */
  439. return LIM_EXPENSIVE;
  440. case CONSTRUCTOR:
  441. /* Make vector construction cost proportional to the number
  442. of elements. */
  443. return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
  444. case SSA_NAME:
  445. case PAREN_EXPR:
  446. /* Whether or not something is wrapped inside a PAREN_EXPR
  447. should not change move cost. Nor should an intermediate
  448. unpropagated SSA name copy. */
  449. return 0;
  450. default:
  451. return 1;
  452. }
  453. }
  454. /* Finds the outermost loop between OUTER and LOOP in that the memory reference
  455. REF is independent. If REF is not independent in LOOP, NULL is returned
  456. instead. */
  457. static struct loop *
  458. outermost_indep_loop (struct loop *outer, struct loop *loop, mem_ref_p ref)
  459. {
  460. struct loop *aloop;
  461. if (ref->stored && bitmap_bit_p (ref->stored, loop->num))
  462. return NULL;
  463. for (aloop = outer;
  464. aloop != loop;
  465. aloop = superloop_at_depth (loop, loop_depth (aloop) + 1))
  466. if ((!ref->stored || !bitmap_bit_p (ref->stored, aloop->num))
  467. && ref_indep_loop_p (aloop, ref))
  468. return aloop;
  469. if (ref_indep_loop_p (loop, ref))
  470. return loop;
  471. else
  472. return NULL;
  473. }
  474. /* If there is a simple load or store to a memory reference in STMT, returns
  475. the location of the memory reference, and sets IS_STORE according to whether
  476. it is a store or load. Otherwise, returns NULL. */
  477. static tree *
  478. simple_mem_ref_in_stmt (gimple stmt, bool *is_store)
  479. {
  480. tree *lhs, *rhs;
  481. /* Recognize SSA_NAME = MEM and MEM = (SSA_NAME | invariant) patterns. */
  482. if (!gimple_assign_single_p (stmt))
  483. return NULL;
  484. lhs = gimple_assign_lhs_ptr (stmt);
  485. rhs = gimple_assign_rhs1_ptr (stmt);
  486. if (TREE_CODE (*lhs) == SSA_NAME && gimple_vuse (stmt))
  487. {
  488. *is_store = false;
  489. return rhs;
  490. }
  491. else if (gimple_vdef (stmt)
  492. && (TREE_CODE (*rhs) == SSA_NAME || is_gimple_min_invariant (*rhs)))
  493. {
  494. *is_store = true;
  495. return lhs;
  496. }
  497. else
  498. return NULL;
  499. }
  500. /* Returns the memory reference contained in STMT. */
  501. static mem_ref_p
  502. mem_ref_in_stmt (gimple stmt)
  503. {
  504. bool store;
  505. tree *mem = simple_mem_ref_in_stmt (stmt, &store);
  506. hashval_t hash;
  507. mem_ref_p ref;
  508. if (!mem)
  509. return NULL;
  510. gcc_assert (!store);
  511. hash = iterative_hash_expr (*mem, 0);
  512. ref = memory_accesses.refs->find_with_hash (*mem, hash);
  513. gcc_assert (ref != NULL);
  514. return ref;
  515. }
  516. /* From a controlling predicate in DOM determine the arguments from
  517. the PHI node PHI that are chosen if the predicate evaluates to
  518. true and false and store them to *TRUE_ARG_P and *FALSE_ARG_P if
  519. they are non-NULL. Returns true if the arguments can be determined,
  520. else return false. */
  521. static bool
  522. extract_true_false_args_from_phi (basic_block dom, gphi *phi,
  523. tree *true_arg_p, tree *false_arg_p)
  524. {
  525. basic_block bb = gimple_bb (phi);
  526. edge true_edge, false_edge, tem;
  527. tree arg0 = NULL_TREE, arg1 = NULL_TREE;
  528. /* We have to verify that one edge into the PHI node is dominated
  529. by the true edge of the predicate block and the other edge
  530. dominated by the false edge. This ensures that the PHI argument
  531. we are going to take is completely determined by the path we
  532. take from the predicate block.
  533. We can only use BB dominance checks below if the destination of
  534. the true/false edges are dominated by their edge, thus only
  535. have a single predecessor. */
  536. extract_true_false_edges_from_block (dom, &true_edge, &false_edge);
  537. tem = EDGE_PRED (bb, 0);
  538. if (tem == true_edge
  539. || (single_pred_p (true_edge->dest)
  540. && (tem->src == true_edge->dest
  541. || dominated_by_p (CDI_DOMINATORS,
  542. tem->src, true_edge->dest))))
  543. arg0 = PHI_ARG_DEF (phi, tem->dest_idx);
  544. else if (tem == false_edge
  545. || (single_pred_p (false_edge->dest)
  546. && (tem->src == false_edge->dest
  547. || dominated_by_p (CDI_DOMINATORS,
  548. tem->src, false_edge->dest))))
  549. arg1 = PHI_ARG_DEF (phi, tem->dest_idx);
  550. else
  551. return false;
  552. tem = EDGE_PRED (bb, 1);
  553. if (tem == true_edge
  554. || (single_pred_p (true_edge->dest)
  555. && (tem->src == true_edge->dest
  556. || dominated_by_p (CDI_DOMINATORS,
  557. tem->src, true_edge->dest))))
  558. arg0 = PHI_ARG_DEF (phi, tem->dest_idx);
  559. else if (tem == false_edge
  560. || (single_pred_p (false_edge->dest)
  561. && (tem->src == false_edge->dest
  562. || dominated_by_p (CDI_DOMINATORS,
  563. tem->src, false_edge->dest))))
  564. arg1 = PHI_ARG_DEF (phi, tem->dest_idx);
  565. else
  566. return false;
  567. if (!arg0 || !arg1)
  568. return false;
  569. if (true_arg_p)
  570. *true_arg_p = arg0;
  571. if (false_arg_p)
  572. *false_arg_p = arg1;
  573. return true;
  574. }
  575. /* Determine the outermost loop to that it is possible to hoist a statement
  576. STMT and store it to LIM_DATA (STMT)->max_loop. To do this we determine
  577. the outermost loop in that the value computed by STMT is invariant.
  578. If MUST_PRESERVE_EXEC is true, additionally choose such a loop that
  579. we preserve the fact whether STMT is executed. It also fills other related
  580. information to LIM_DATA (STMT).
  581. The function returns false if STMT cannot be hoisted outside of the loop it
  582. is defined in, and true otherwise. */
  583. static bool
  584. determine_max_movement (gimple stmt, bool must_preserve_exec)
  585. {
  586. basic_block bb = gimple_bb (stmt);
  587. struct loop *loop = bb->loop_father;
  588. struct loop *level;
  589. struct lim_aux_data *lim_data = get_lim_data (stmt);
  590. tree val;
  591. ssa_op_iter iter;
  592. if (must_preserve_exec)
  593. level = ALWAYS_EXECUTED_IN (bb);
  594. else
  595. level = superloop_at_depth (loop, 1);
  596. lim_data->max_loop = level;
  597. if (gphi *phi = dyn_cast <gphi *> (stmt))
  598. {
  599. use_operand_p use_p;
  600. unsigned min_cost = UINT_MAX;
  601. unsigned total_cost = 0;
  602. struct lim_aux_data *def_data;
  603. /* We will end up promoting dependencies to be unconditionally
  604. evaluated. For this reason the PHI cost (and thus the
  605. cost we remove from the loop by doing the invariant motion)
  606. is that of the cheapest PHI argument dependency chain. */
  607. FOR_EACH_PHI_ARG (use_p, phi, iter, SSA_OP_USE)
  608. {
  609. val = USE_FROM_PTR (use_p);
  610. if (TREE_CODE (val) != SSA_NAME)
  611. {
  612. /* Assign const 1 to constants. */
  613. min_cost = MIN (min_cost, 1);
  614. total_cost += 1;
  615. continue;
  616. }
  617. if (!add_dependency (val, lim_data, loop, false))
  618. return false;
  619. gimple def_stmt = SSA_NAME_DEF_STMT (val);
  620. if (gimple_bb (def_stmt)
  621. && gimple_bb (def_stmt)->loop_father == loop)
  622. {
  623. def_data = get_lim_data (def_stmt);
  624. if (def_data)
  625. {
  626. min_cost = MIN (min_cost, def_data->cost);
  627. total_cost += def_data->cost;
  628. }
  629. }
  630. }
  631. min_cost = MIN (min_cost, total_cost);
  632. lim_data->cost += min_cost;
  633. if (gimple_phi_num_args (phi) > 1)
  634. {
  635. basic_block dom = get_immediate_dominator (CDI_DOMINATORS, bb);
  636. gimple cond;
  637. if (gsi_end_p (gsi_last_bb (dom)))
  638. return false;
  639. cond = gsi_stmt (gsi_last_bb (dom));
  640. if (gimple_code (cond) != GIMPLE_COND)
  641. return false;
  642. /* Verify that this is an extended form of a diamond and
  643. the PHI arguments are completely controlled by the
  644. predicate in DOM. */
  645. if (!extract_true_false_args_from_phi (dom, phi, NULL, NULL))
  646. return false;
  647. /* Fold in dependencies and cost of the condition. */
  648. FOR_EACH_SSA_TREE_OPERAND (val, cond, iter, SSA_OP_USE)
  649. {
  650. if (!add_dependency (val, lim_data, loop, false))
  651. return false;
  652. def_data = get_lim_data (SSA_NAME_DEF_STMT (val));
  653. if (def_data)
  654. total_cost += def_data->cost;
  655. }
  656. /* We want to avoid unconditionally executing very expensive
  657. operations. As costs for our dependencies cannot be
  658. negative just claim we are not invariand for this case.
  659. We also are not sure whether the control-flow inside the
  660. loop will vanish. */
  661. if (total_cost - min_cost >= 2 * LIM_EXPENSIVE
  662. && !(min_cost != 0
  663. && total_cost / min_cost <= 2))
  664. return false;
  665. /* Assume that the control-flow in the loop will vanish.
  666. ??? We should verify this and not artificially increase
  667. the cost if that is not the case. */
  668. lim_data->cost += stmt_cost (stmt);
  669. }
  670. return true;
  671. }
  672. else
  673. FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_USE)
  674. if (!add_dependency (val, lim_data, loop, true))
  675. return false;
  676. if (gimple_vuse (stmt))
  677. {
  678. mem_ref_p ref = mem_ref_in_stmt (stmt);
  679. if (ref)
  680. {
  681. lim_data->max_loop
  682. = outermost_indep_loop (lim_data->max_loop, loop, ref);
  683. if (!lim_data->max_loop)
  684. return false;
  685. }
  686. else
  687. {
  688. if ((val = gimple_vuse (stmt)) != NULL_TREE)
  689. {
  690. if (!add_dependency (val, lim_data, loop, false))
  691. return false;
  692. }
  693. }
  694. }
  695. lim_data->cost += stmt_cost (stmt);
  696. return true;
  697. }
  698. /* Suppose that some statement in ORIG_LOOP is hoisted to the loop LEVEL,
  699. and that one of the operands of this statement is computed by STMT.
  700. Ensure that STMT (together with all the statements that define its
  701. operands) is hoisted at least out of the loop LEVEL. */
  702. static void
  703. set_level (gimple stmt, struct loop *orig_loop, struct loop *level)
  704. {
  705. struct loop *stmt_loop = gimple_bb (stmt)->loop_father;
  706. struct lim_aux_data *lim_data;
  707. gimple dep_stmt;
  708. unsigned i;
  709. stmt_loop = find_common_loop (orig_loop, stmt_loop);
  710. lim_data = get_lim_data (stmt);
  711. if (lim_data != NULL && lim_data->tgt_loop != NULL)
  712. stmt_loop = find_common_loop (stmt_loop,
  713. loop_outer (lim_data->tgt_loop));
  714. if (flow_loop_nested_p (stmt_loop, level))
  715. return;
  716. gcc_assert (level == lim_data->max_loop
  717. || flow_loop_nested_p (lim_data->max_loop, level));
  718. lim_data->tgt_loop = level;
  719. FOR_EACH_VEC_ELT (lim_data->depends, i, dep_stmt)
  720. set_level (dep_stmt, orig_loop, level);
  721. }
  722. /* Determines an outermost loop from that we want to hoist the statement STMT.
  723. For now we chose the outermost possible loop. TODO -- use profiling
  724. information to set it more sanely. */
  725. static void
  726. set_profitable_level (gimple stmt)
  727. {
  728. set_level (stmt, gimple_bb (stmt)->loop_father, get_lim_data (stmt)->max_loop);
  729. }
  730. /* Returns true if STMT is a call that has side effects. */
  731. static bool
  732. nonpure_call_p (gimple stmt)
  733. {
  734. if (gimple_code (stmt) != GIMPLE_CALL)
  735. return false;
  736. return gimple_has_side_effects (stmt);
  737. }
  738. /* Rewrite a/b to a*(1/b). Return the invariant stmt to process. */
  739. static gimple
  740. rewrite_reciprocal (gimple_stmt_iterator *bsi)
  741. {
  742. gassign *stmt, *stmt1, *stmt2;
  743. tree name, lhs, type;
  744. tree real_one;
  745. gimple_stmt_iterator gsi;
  746. stmt = as_a <gassign *> (gsi_stmt (*bsi));
  747. lhs = gimple_assign_lhs (stmt);
  748. type = TREE_TYPE (lhs);
  749. real_one = build_one_cst (type);
  750. name = make_temp_ssa_name (type, NULL, "reciptmp");
  751. stmt1 = gimple_build_assign (name, RDIV_EXPR, real_one,
  752. gimple_assign_rhs2 (stmt));
  753. stmt2 = gimple_build_assign (lhs, MULT_EXPR, name,
  754. gimple_assign_rhs1 (stmt));
  755. /* Replace division stmt with reciprocal and multiply stmts.
  756. The multiply stmt is not invariant, so update iterator
  757. and avoid rescanning. */
  758. gsi = *bsi;
  759. gsi_insert_before (bsi, stmt1, GSI_NEW_STMT);
  760. gsi_replace (&gsi, stmt2, true);
  761. /* Continue processing with invariant reciprocal statement. */
  762. return stmt1;
  763. }
  764. /* Check if the pattern at *BSI is a bittest of the form
  765. (A >> B) & 1 != 0 and in this case rewrite it to A & (1 << B) != 0. */
  766. static gimple
  767. rewrite_bittest (gimple_stmt_iterator *bsi)
  768. {
  769. gassign *stmt;
  770. gimple stmt1;
  771. gassign *stmt2;
  772. gimple use_stmt;
  773. gcond *cond_stmt;
  774. tree lhs, name, t, a, b;
  775. use_operand_p use;
  776. stmt = as_a <gassign *> (gsi_stmt (*bsi));
  777. lhs = gimple_assign_lhs (stmt);
  778. /* Verify that the single use of lhs is a comparison against zero. */
  779. if (TREE_CODE (lhs) != SSA_NAME
  780. || !single_imm_use (lhs, &use, &use_stmt))
  781. return stmt;
  782. cond_stmt = dyn_cast <gcond *> (use_stmt);
  783. if (!cond_stmt)
  784. return stmt;
  785. if (gimple_cond_lhs (cond_stmt) != lhs
  786. || (gimple_cond_code (cond_stmt) != NE_EXPR
  787. && gimple_cond_code (cond_stmt) != EQ_EXPR)
  788. || !integer_zerop (gimple_cond_rhs (cond_stmt)))
  789. return stmt;
  790. /* Get at the operands of the shift. The rhs is TMP1 & 1. */
  791. stmt1 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
  792. if (gimple_code (stmt1) != GIMPLE_ASSIGN)
  793. return stmt;
  794. /* There is a conversion in between possibly inserted by fold. */
  795. if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt1)))
  796. {
  797. t = gimple_assign_rhs1 (stmt1);
  798. if (TREE_CODE (t) != SSA_NAME
  799. || !has_single_use (t))
  800. return stmt;
  801. stmt1 = SSA_NAME_DEF_STMT (t);
  802. if (gimple_code (stmt1) != GIMPLE_ASSIGN)
  803. return stmt;
  804. }
  805. /* Verify that B is loop invariant but A is not. Verify that with
  806. all the stmt walking we are still in the same loop. */
  807. if (gimple_assign_rhs_code (stmt1) != RSHIFT_EXPR
  808. || loop_containing_stmt (stmt1) != loop_containing_stmt (stmt))
  809. return stmt;
  810. a = gimple_assign_rhs1 (stmt1);
  811. b = gimple_assign_rhs2 (stmt1);
  812. if (outermost_invariant_loop (b, loop_containing_stmt (stmt1)) != NULL
  813. && outermost_invariant_loop (a, loop_containing_stmt (stmt1)) == NULL)
  814. {
  815. gimple_stmt_iterator rsi;
  816. /* 1 << B */
  817. t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (a),
  818. build_int_cst (TREE_TYPE (a), 1), b);
  819. name = make_temp_ssa_name (TREE_TYPE (a), NULL, "shifttmp");
  820. stmt1 = gimple_build_assign (name, t);
  821. /* A & (1 << B) */
  822. t = fold_build2 (BIT_AND_EXPR, TREE_TYPE (a), a, name);
  823. name = make_temp_ssa_name (TREE_TYPE (a), NULL, "shifttmp");
  824. stmt2 = gimple_build_assign (name, t);
  825. /* Replace the SSA_NAME we compare against zero. Adjust
  826. the type of zero accordingly. */
  827. SET_USE (use, name);
  828. gimple_cond_set_rhs (cond_stmt,
  829. build_int_cst_type (TREE_TYPE (name),
  830. 0));
  831. /* Don't use gsi_replace here, none of the new assignments sets
  832. the variable originally set in stmt. Move bsi to stmt1, and
  833. then remove the original stmt, so that we get a chance to
  834. retain debug info for it. */
  835. rsi = *bsi;
  836. gsi_insert_before (bsi, stmt1, GSI_NEW_STMT);
  837. gsi_insert_before (&rsi, stmt2, GSI_SAME_STMT);
  838. gsi_remove (&rsi, true);
  839. return stmt1;
  840. }
  841. return stmt;
  842. }
  843. /* For each statement determines the outermost loop in that it is invariant,
  844. - statements on whose motion it depends and the cost of the computation.
  845. - This information is stored to the LIM_DATA structure associated with
  846. - each statement. */
  847. class invariantness_dom_walker : public dom_walker
  848. {
  849. public:
  850. invariantness_dom_walker (cdi_direction direction)
  851. : dom_walker (direction) {}
  852. virtual void before_dom_children (basic_block);
  853. };
  854. /* Determine the outermost loops in that statements in basic block BB are
  855. invariant, and record them to the LIM_DATA associated with the statements.
  856. Callback for dom_walker. */
  857. void
  858. invariantness_dom_walker::before_dom_children (basic_block bb)
  859. {
  860. enum move_pos pos;
  861. gimple_stmt_iterator bsi;
  862. gimple stmt;
  863. bool maybe_never = ALWAYS_EXECUTED_IN (bb) == NULL;
  864. struct loop *outermost = ALWAYS_EXECUTED_IN (bb);
  865. struct lim_aux_data *lim_data;
  866. if (!loop_outer (bb->loop_father))
  867. return;
  868. if (dump_file && (dump_flags & TDF_DETAILS))
  869. fprintf (dump_file, "Basic block %d (loop %d -- depth %d):\n\n",
  870. bb->index, bb->loop_father->num, loop_depth (bb->loop_father));
  871. /* Look at PHI nodes, but only if there is at most two.
  872. ??? We could relax this further by post-processing the inserted
  873. code and transforming adjacent cond-exprs with the same predicate
  874. to control flow again. */
  875. bsi = gsi_start_phis (bb);
  876. if (!gsi_end_p (bsi)
  877. && ((gsi_next (&bsi), gsi_end_p (bsi))
  878. || (gsi_next (&bsi), gsi_end_p (bsi))))
  879. for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  880. {
  881. stmt = gsi_stmt (bsi);
  882. pos = movement_possibility (stmt);
  883. if (pos == MOVE_IMPOSSIBLE)
  884. continue;
  885. lim_data = init_lim_data (stmt);
  886. lim_data->always_executed_in = outermost;
  887. if (!determine_max_movement (stmt, false))
  888. {
  889. lim_data->max_loop = NULL;
  890. continue;
  891. }
  892. if (dump_file && (dump_flags & TDF_DETAILS))
  893. {
  894. print_gimple_stmt (dump_file, stmt, 2, 0);
  895. fprintf (dump_file, " invariant up to level %d, cost %d.\n\n",
  896. loop_depth (lim_data->max_loop),
  897. lim_data->cost);
  898. }
  899. if (lim_data->cost >= LIM_EXPENSIVE)
  900. set_profitable_level (stmt);
  901. }
  902. for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  903. {
  904. stmt = gsi_stmt (bsi);
  905. pos = movement_possibility (stmt);
  906. if (pos == MOVE_IMPOSSIBLE)
  907. {
  908. if (nonpure_call_p (stmt))
  909. {
  910. maybe_never = true;
  911. outermost = NULL;
  912. }
  913. /* Make sure to note always_executed_in for stores to make
  914. store-motion work. */
  915. else if (stmt_makes_single_store (stmt))
  916. {
  917. struct lim_aux_data *lim_data = init_lim_data (stmt);
  918. lim_data->always_executed_in = outermost;
  919. }
  920. continue;
  921. }
  922. if (is_gimple_assign (stmt)
  923. && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
  924. == GIMPLE_BINARY_RHS))
  925. {
  926. tree op0 = gimple_assign_rhs1 (stmt);
  927. tree op1 = gimple_assign_rhs2 (stmt);
  928. struct loop *ol1 = outermost_invariant_loop (op1,
  929. loop_containing_stmt (stmt));
  930. /* If divisor is invariant, convert a/b to a*(1/b), allowing reciprocal
  931. to be hoisted out of loop, saving expensive divide. */
  932. if (pos == MOVE_POSSIBLE
  933. && gimple_assign_rhs_code (stmt) == RDIV_EXPR
  934. && flag_unsafe_math_optimizations
  935. && !flag_trapping_math
  936. && ol1 != NULL
  937. && outermost_invariant_loop (op0, ol1) == NULL)
  938. stmt = rewrite_reciprocal (&bsi);
  939. /* If the shift count is invariant, convert (A >> B) & 1 to
  940. A & (1 << B) allowing the bit mask to be hoisted out of the loop
  941. saving an expensive shift. */
  942. if (pos == MOVE_POSSIBLE
  943. && gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
  944. && integer_onep (op1)
  945. && TREE_CODE (op0) == SSA_NAME
  946. && has_single_use (op0))
  947. stmt = rewrite_bittest (&bsi);
  948. }
  949. lim_data = init_lim_data (stmt);
  950. lim_data->always_executed_in = outermost;
  951. if (maybe_never && pos == MOVE_PRESERVE_EXECUTION)
  952. continue;
  953. if (!determine_max_movement (stmt, pos == MOVE_PRESERVE_EXECUTION))
  954. {
  955. lim_data->max_loop = NULL;
  956. continue;
  957. }
  958. if (dump_file && (dump_flags & TDF_DETAILS))
  959. {
  960. print_gimple_stmt (dump_file, stmt, 2, 0);
  961. fprintf (dump_file, " invariant up to level %d, cost %d.\n\n",
  962. loop_depth (lim_data->max_loop),
  963. lim_data->cost);
  964. }
  965. if (lim_data->cost >= LIM_EXPENSIVE)
  966. set_profitable_level (stmt);
  967. }
  968. }
  969. class move_computations_dom_walker : public dom_walker
  970. {
  971. public:
  972. move_computations_dom_walker (cdi_direction direction)
  973. : dom_walker (direction), todo_ (0) {}
  974. virtual void before_dom_children (basic_block);
  975. unsigned int todo_;
  976. };
  977. /* Hoist the statements in basic block BB out of the loops prescribed by
  978. data stored in LIM_DATA structures associated with each statement. Callback
  979. for walk_dominator_tree. */
  980. void
  981. move_computations_dom_walker::before_dom_children (basic_block bb)
  982. {
  983. struct loop *level;
  984. unsigned cost = 0;
  985. struct lim_aux_data *lim_data;
  986. if (!loop_outer (bb->loop_father))
  987. return;
  988. for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi); )
  989. {
  990. gassign *new_stmt;
  991. gphi *stmt = bsi.phi ();
  992. lim_data = get_lim_data (stmt);
  993. if (lim_data == NULL)
  994. {
  995. gsi_next (&bsi);
  996. continue;
  997. }
  998. cost = lim_data->cost;
  999. level = lim_data->tgt_loop;
  1000. clear_lim_data (stmt);
  1001. if (!level)
  1002. {
  1003. gsi_next (&bsi);
  1004. continue;
  1005. }
  1006. if (dump_file && (dump_flags & TDF_DETAILS))
  1007. {
  1008. fprintf (dump_file, "Moving PHI node\n");
  1009. print_gimple_stmt (dump_file, stmt, 0, 0);
  1010. fprintf (dump_file, "(cost %u) out of loop %d.\n\n",
  1011. cost, level->num);
  1012. }
  1013. if (gimple_phi_num_args (stmt) == 1)
  1014. {
  1015. tree arg = PHI_ARG_DEF (stmt, 0);
  1016. new_stmt = gimple_build_assign (gimple_phi_result (stmt),
  1017. TREE_CODE (arg), arg);
  1018. }
  1019. else
  1020. {
  1021. basic_block dom = get_immediate_dominator (CDI_DOMINATORS, bb);
  1022. gimple cond = gsi_stmt (gsi_last_bb (dom));
  1023. tree arg0 = NULL_TREE, arg1 = NULL_TREE, t;
  1024. /* Get the PHI arguments corresponding to the true and false
  1025. edges of COND. */
  1026. extract_true_false_args_from_phi (dom, stmt, &arg0, &arg1);
  1027. gcc_assert (arg0 && arg1);
  1028. t = build2 (gimple_cond_code (cond), boolean_type_node,
  1029. gimple_cond_lhs (cond), gimple_cond_rhs (cond));
  1030. new_stmt = gimple_build_assign (gimple_phi_result (stmt),
  1031. COND_EXPR, t, arg0, arg1);
  1032. todo_ |= TODO_cleanup_cfg;
  1033. }
  1034. if (INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (new_stmt)))
  1035. && (!ALWAYS_EXECUTED_IN (bb)
  1036. || (ALWAYS_EXECUTED_IN (bb) != level
  1037. && !flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb), level))))
  1038. {
  1039. tree lhs = gimple_assign_lhs (new_stmt);
  1040. SSA_NAME_RANGE_INFO (lhs) = NULL;
  1041. SSA_NAME_ANTI_RANGE_P (lhs) = 0;
  1042. }
  1043. gsi_insert_on_edge (loop_preheader_edge (level), new_stmt);
  1044. remove_phi_node (&bsi, false);
  1045. }
  1046. for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi); )
  1047. {
  1048. edge e;
  1049. gimple stmt = gsi_stmt (bsi);
  1050. lim_data = get_lim_data (stmt);
  1051. if (lim_data == NULL)
  1052. {
  1053. gsi_next (&bsi);
  1054. continue;
  1055. }
  1056. cost = lim_data->cost;
  1057. level = lim_data->tgt_loop;
  1058. clear_lim_data (stmt);
  1059. if (!level)
  1060. {
  1061. gsi_next (&bsi);
  1062. continue;
  1063. }
  1064. /* We do not really want to move conditionals out of the loop; we just
  1065. placed it here to force its operands to be moved if necessary. */
  1066. if (gimple_code (stmt) == GIMPLE_COND)
  1067. continue;
  1068. if (dump_file && (dump_flags & TDF_DETAILS))
  1069. {
  1070. fprintf (dump_file, "Moving statement\n");
  1071. print_gimple_stmt (dump_file, stmt, 0, 0);
  1072. fprintf (dump_file, "(cost %u) out of loop %d.\n\n",
  1073. cost, level->num);
  1074. }
  1075. e = loop_preheader_edge (level);
  1076. gcc_assert (!gimple_vdef (stmt));
  1077. if (gimple_vuse (stmt))
  1078. {
  1079. /* The new VUSE is the one from the virtual PHI in the loop
  1080. header or the one already present. */
  1081. gphi_iterator gsi2;
  1082. for (gsi2 = gsi_start_phis (e->dest);
  1083. !gsi_end_p (gsi2); gsi_next (&gsi2))
  1084. {
  1085. gphi *phi = gsi2.phi ();
  1086. if (virtual_operand_p (gimple_phi_result (phi)))
  1087. {
  1088. gimple_set_vuse (stmt, PHI_ARG_DEF_FROM_EDGE (phi, e));
  1089. break;
  1090. }
  1091. }
  1092. }
  1093. gsi_remove (&bsi, false);
  1094. if (gimple_has_lhs (stmt)
  1095. && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME
  1096. && INTEGRAL_TYPE_P (TREE_TYPE (gimple_get_lhs (stmt)))
  1097. && (!ALWAYS_EXECUTED_IN (bb)
  1098. || !(ALWAYS_EXECUTED_IN (bb) == level
  1099. || flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb), level))))
  1100. {
  1101. tree lhs = gimple_get_lhs (stmt);
  1102. SSA_NAME_RANGE_INFO (lhs) = NULL;
  1103. SSA_NAME_ANTI_RANGE_P (lhs) = 0;
  1104. }
  1105. /* In case this is a stmt that is not unconditionally executed
  1106. when the target loop header is executed and the stmt may
  1107. invoke undefined integer or pointer overflow rewrite it to
  1108. unsigned arithmetic. */
  1109. if (is_gimple_assign (stmt)
  1110. && INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt)))
  1111. && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (gimple_assign_lhs (stmt)))
  1112. && arith_code_with_undefined_signed_overflow
  1113. (gimple_assign_rhs_code (stmt))
  1114. && (!ALWAYS_EXECUTED_IN (bb)
  1115. || !(ALWAYS_EXECUTED_IN (bb) == level
  1116. || flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb), level))))
  1117. gsi_insert_seq_on_edge (e, rewrite_to_defined_overflow (stmt));
  1118. else
  1119. gsi_insert_on_edge (e, stmt);
  1120. }
  1121. }
  1122. /* Hoist the statements out of the loops prescribed by data stored in
  1123. LIM_DATA structures associated with each statement.*/
  1124. static unsigned int
  1125. move_computations (void)
  1126. {
  1127. move_computations_dom_walker walker (CDI_DOMINATORS);
  1128. walker.walk (cfun->cfg->x_entry_block_ptr);
  1129. gsi_commit_edge_inserts ();
  1130. if (need_ssa_update_p (cfun))
  1131. rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
  1132. return walker.todo_;
  1133. }
  1134. /* Checks whether the statement defining variable *INDEX can be hoisted
  1135. out of the loop passed in DATA. Callback for for_each_index. */
  1136. static bool
  1137. may_move_till (tree ref, tree *index, void *data)
  1138. {
  1139. struct loop *loop = (struct loop *) data, *max_loop;
  1140. /* If REF is an array reference, check also that the step and the lower
  1141. bound is invariant in LOOP. */
  1142. if (TREE_CODE (ref) == ARRAY_REF)
  1143. {
  1144. tree step = TREE_OPERAND (ref, 3);
  1145. tree lbound = TREE_OPERAND (ref, 2);
  1146. max_loop = outermost_invariant_loop (step, loop);
  1147. if (!max_loop)
  1148. return false;
  1149. max_loop = outermost_invariant_loop (lbound, loop);
  1150. if (!max_loop)
  1151. return false;
  1152. }
  1153. max_loop = outermost_invariant_loop (*index, loop);
  1154. if (!max_loop)
  1155. return false;
  1156. return true;
  1157. }
  1158. /* If OP is SSA NAME, force the statement that defines it to be
  1159. moved out of the LOOP. ORIG_LOOP is the loop in that EXPR is used. */
  1160. static void
  1161. force_move_till_op (tree op, struct loop *orig_loop, struct loop *loop)
  1162. {
  1163. gimple stmt;
  1164. if (!op
  1165. || is_gimple_min_invariant (op))
  1166. return;
  1167. gcc_assert (TREE_CODE (op) == SSA_NAME);
  1168. stmt = SSA_NAME_DEF_STMT (op);
  1169. if (gimple_nop_p (stmt))
  1170. return;
  1171. set_level (stmt, orig_loop, loop);
  1172. }
  1173. /* Forces statement defining invariants in REF (and *INDEX) to be moved out of
  1174. the LOOP. The reference REF is used in the loop ORIG_LOOP. Callback for
  1175. for_each_index. */
  1176. struct fmt_data
  1177. {
  1178. struct loop *loop;
  1179. struct loop *orig_loop;
  1180. };
  1181. static bool
  1182. force_move_till (tree ref, tree *index, void *data)
  1183. {
  1184. struct fmt_data *fmt_data = (struct fmt_data *) data;
  1185. if (TREE_CODE (ref) == ARRAY_REF)
  1186. {
  1187. tree step = TREE_OPERAND (ref, 3);
  1188. tree lbound = TREE_OPERAND (ref, 2);
  1189. force_move_till_op (step, fmt_data->orig_loop, fmt_data->loop);
  1190. force_move_till_op (lbound, fmt_data->orig_loop, fmt_data->loop);
  1191. }
  1192. force_move_till_op (*index, fmt_data->orig_loop, fmt_data->loop);
  1193. return true;
  1194. }
  1195. /* A function to free the mem_ref object OBJ. */
  1196. static void
  1197. memref_free (struct im_mem_ref *mem)
  1198. {
  1199. mem->accesses_in_loop.release ();
  1200. }
  1201. /* Allocates and returns a memory reference description for MEM whose hash
  1202. value is HASH and id is ID. */
  1203. static mem_ref_p
  1204. mem_ref_alloc (tree mem, unsigned hash, unsigned id)
  1205. {
  1206. mem_ref_p ref = XOBNEW (&mem_ref_obstack, struct im_mem_ref);
  1207. ao_ref_init (&ref->mem, mem);
  1208. ref->id = id;
  1209. ref->hash = hash;
  1210. ref->stored = NULL;
  1211. bitmap_initialize (&ref->indep_loop, &lim_bitmap_obstack);
  1212. bitmap_initialize (&ref->dep_loop, &lim_bitmap_obstack);
  1213. ref->accesses_in_loop.create (1);
  1214. return ref;
  1215. }
  1216. /* Records memory reference location *LOC in LOOP to the memory reference
  1217. description REF. The reference occurs in statement STMT. */
  1218. static void
  1219. record_mem_ref_loc (mem_ref_p ref, gimple stmt, tree *loc)
  1220. {
  1221. mem_ref_loc aref;
  1222. aref.stmt = stmt;
  1223. aref.ref = loc;
  1224. ref->accesses_in_loop.safe_push (aref);
  1225. }
  1226. /* Set the LOOP bit in REF stored bitmap and allocate that if
  1227. necessary. Return whether a bit was changed. */
  1228. static bool
  1229. set_ref_stored_in_loop (mem_ref_p ref, struct loop *loop)
  1230. {
  1231. if (!ref->stored)
  1232. ref->stored = BITMAP_ALLOC (&lim_bitmap_obstack);
  1233. return bitmap_set_bit (ref->stored, loop->num);
  1234. }
  1235. /* Marks reference REF as stored in LOOP. */
  1236. static void
  1237. mark_ref_stored (mem_ref_p ref, struct loop *loop)
  1238. {
  1239. while (loop != current_loops->tree_root
  1240. && set_ref_stored_in_loop (ref, loop))
  1241. loop = loop_outer (loop);
  1242. }
  1243. /* Gathers memory references in statement STMT in LOOP, storing the
  1244. information about them in the memory_accesses structure. Marks
  1245. the vops accessed through unrecognized statements there as
  1246. well. */
  1247. static void
  1248. gather_mem_refs_stmt (struct loop *loop, gimple stmt)
  1249. {
  1250. tree *mem = NULL;
  1251. hashval_t hash;
  1252. im_mem_ref **slot;
  1253. mem_ref_p ref;
  1254. bool is_stored;
  1255. unsigned id;
  1256. if (!gimple_vuse (stmt))
  1257. return;
  1258. mem = simple_mem_ref_in_stmt (stmt, &is_stored);
  1259. if (!mem)
  1260. {
  1261. /* We use the shared mem_ref for all unanalyzable refs. */
  1262. id = UNANALYZABLE_MEM_ID;
  1263. ref = memory_accesses.refs_list[id];
  1264. if (dump_file && (dump_flags & TDF_DETAILS))
  1265. {
  1266. fprintf (dump_file, "Unanalyzed memory reference %u: ", id);
  1267. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1268. }
  1269. is_stored = gimple_vdef (stmt);
  1270. }
  1271. else
  1272. {
  1273. hash = iterative_hash_expr (*mem, 0);
  1274. slot = memory_accesses.refs->find_slot_with_hash (*mem, hash, INSERT);
  1275. if (*slot)
  1276. {
  1277. ref = (mem_ref_p) *slot;
  1278. id = ref->id;
  1279. }
  1280. else
  1281. {
  1282. id = memory_accesses.refs_list.length ();
  1283. ref = mem_ref_alloc (*mem, hash, id);
  1284. memory_accesses.refs_list.safe_push (ref);
  1285. *slot = ref;
  1286. if (dump_file && (dump_flags & TDF_DETAILS))
  1287. {
  1288. fprintf (dump_file, "Memory reference %u: ", id);
  1289. print_generic_expr (dump_file, ref->mem.ref, TDF_SLIM);
  1290. fprintf (dump_file, "\n");
  1291. }
  1292. }
  1293. record_mem_ref_loc (ref, stmt, mem);
  1294. }
  1295. bitmap_set_bit (&memory_accesses.refs_in_loop[loop->num], ref->id);
  1296. if (is_stored)
  1297. {
  1298. bitmap_set_bit (&memory_accesses.refs_stored_in_loop[loop->num], ref->id);
  1299. mark_ref_stored (ref, loop);
  1300. }
  1301. return;
  1302. }
  1303. static unsigned *bb_loop_postorder;
  1304. /* qsort sort function to sort blocks after their loop fathers postorder. */
  1305. static int
  1306. sort_bbs_in_loop_postorder_cmp (const void *bb1_, const void *bb2_)
  1307. {
  1308. basic_block bb1 = *(basic_block *)const_cast<void *>(bb1_);
  1309. basic_block bb2 = *(basic_block *)const_cast<void *>(bb2_);
  1310. struct loop *loop1 = bb1->loop_father;
  1311. struct loop *loop2 = bb2->loop_father;
  1312. if (loop1->num == loop2->num)
  1313. return 0;
  1314. return bb_loop_postorder[loop1->num] < bb_loop_postorder[loop2->num] ? -1 : 1;
  1315. }
  1316. /* qsort sort function to sort ref locs after their loop fathers postorder. */
  1317. static int
  1318. sort_locs_in_loop_postorder_cmp (const void *loc1_, const void *loc2_)
  1319. {
  1320. mem_ref_loc *loc1 = (mem_ref_loc *)const_cast<void *>(loc1_);
  1321. mem_ref_loc *loc2 = (mem_ref_loc *)const_cast<void *>(loc2_);
  1322. struct loop *loop1 = gimple_bb (loc1->stmt)->loop_father;
  1323. struct loop *loop2 = gimple_bb (loc2->stmt)->loop_father;
  1324. if (loop1->num == loop2->num)
  1325. return 0;
  1326. return bb_loop_postorder[loop1->num] < bb_loop_postorder[loop2->num] ? -1 : 1;
  1327. }
  1328. /* Gathers memory references in loops. */
  1329. static void
  1330. analyze_memory_references (void)
  1331. {
  1332. gimple_stmt_iterator bsi;
  1333. basic_block bb, *bbs;
  1334. struct loop *loop, *outer;
  1335. unsigned i, n;
  1336. /* Collect all basic-blocks in loops and sort them after their
  1337. loops postorder. */
  1338. i = 0;
  1339. bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
  1340. FOR_EACH_BB_FN (bb, cfun)
  1341. if (bb->loop_father != current_loops->tree_root)
  1342. bbs[i++] = bb;
  1343. n = i;
  1344. qsort (bbs, n, sizeof (basic_block), sort_bbs_in_loop_postorder_cmp);
  1345. /* Visit blocks in loop postorder and assign mem-ref IDs in that order.
  1346. That results in better locality for all the bitmaps. */
  1347. for (i = 0; i < n; ++i)
  1348. {
  1349. basic_block bb = bbs[i];
  1350. for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  1351. gather_mem_refs_stmt (bb->loop_father, gsi_stmt (bsi));
  1352. }
  1353. /* Sort the location list of gathered memory references after their
  1354. loop postorder number. */
  1355. im_mem_ref *ref;
  1356. FOR_EACH_VEC_ELT (memory_accesses.refs_list, i, ref)
  1357. ref->accesses_in_loop.qsort (sort_locs_in_loop_postorder_cmp);
  1358. free (bbs);
  1359. // free (bb_loop_postorder);
  1360. /* Propagate the information about accessed memory references up
  1361. the loop hierarchy. */
  1362. FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
  1363. {
  1364. /* Finalize the overall touched references (including subloops). */
  1365. bitmap_ior_into (&memory_accesses.all_refs_stored_in_loop[loop->num],
  1366. &memory_accesses.refs_stored_in_loop[loop->num]);
  1367. /* Propagate the information about accessed memory references up
  1368. the loop hierarchy. */
  1369. outer = loop_outer (loop);
  1370. if (outer == current_loops->tree_root)
  1371. continue;
  1372. bitmap_ior_into (&memory_accesses.all_refs_stored_in_loop[outer->num],
  1373. &memory_accesses.all_refs_stored_in_loop[loop->num]);
  1374. }
  1375. }
  1376. /* Returns true if MEM1 and MEM2 may alias. TTAE_CACHE is used as a cache in
  1377. tree_to_aff_combination_expand. */
  1378. static bool
  1379. mem_refs_may_alias_p (mem_ref_p mem1, mem_ref_p mem2,
  1380. hash_map<tree, name_expansion *> **ttae_cache)
  1381. {
  1382. /* Perform BASE + OFFSET analysis -- if MEM1 and MEM2 are based on the same
  1383. object and their offset differ in such a way that the locations cannot
  1384. overlap, then they cannot alias. */
  1385. widest_int size1, size2;
  1386. aff_tree off1, off2;
  1387. /* Perform basic offset and type-based disambiguation. */
  1388. if (!refs_may_alias_p_1 (&mem1->mem, &mem2->mem, true))
  1389. return false;
  1390. /* The expansion of addresses may be a bit expensive, thus we only do
  1391. the check at -O2 and higher optimization levels. */
  1392. if (optimize < 2)
  1393. return true;
  1394. get_inner_reference_aff (mem1->mem.ref, &off1, &size1);
  1395. get_inner_reference_aff (mem2->mem.ref, &off2, &size2);
  1396. aff_combination_expand (&off1, ttae_cache);
  1397. aff_combination_expand (&off2, ttae_cache);
  1398. aff_combination_scale (&off1, -1);
  1399. aff_combination_add (&off2, &off1);
  1400. if (aff_comb_cannot_overlap_p (&off2, size1, size2))
  1401. return false;
  1402. return true;
  1403. }
  1404. /* Compare function for bsearch searching for reference locations
  1405. in a loop. */
  1406. static int
  1407. find_ref_loc_in_loop_cmp (const void *loop_, const void *loc_)
  1408. {
  1409. struct loop *loop = (struct loop *)const_cast<void *>(loop_);
  1410. mem_ref_loc *loc = (mem_ref_loc *)const_cast<void *>(loc_);
  1411. struct loop *loc_loop = gimple_bb (loc->stmt)->loop_father;
  1412. if (loop->num == loc_loop->num
  1413. || flow_loop_nested_p (loop, loc_loop))
  1414. return 0;
  1415. return (bb_loop_postorder[loop->num] < bb_loop_postorder[loc_loop->num]
  1416. ? -1 : 1);
  1417. }
  1418. /* Iterates over all locations of REF in LOOP and its subloops calling
  1419. fn.operator() with the location as argument. When that operator
  1420. returns true the iteration is stopped and true is returned.
  1421. Otherwise false is returned. */
  1422. template <typename FN>
  1423. static bool
  1424. for_all_locs_in_loop (struct loop *loop, mem_ref_p ref, FN fn)
  1425. {
  1426. unsigned i;
  1427. mem_ref_loc_p loc;
  1428. /* Search for the cluster of locs in the accesses_in_loop vector
  1429. which is sorted after postorder index of the loop father. */
  1430. loc = ref->accesses_in_loop.bsearch (loop, find_ref_loc_in_loop_cmp);
  1431. if (!loc)
  1432. return false;
  1433. /* We have found one location inside loop or its sub-loops. Iterate
  1434. both forward and backward to cover the whole cluster. */
  1435. i = loc - ref->accesses_in_loop.address ();
  1436. while (i > 0)
  1437. {
  1438. --i;
  1439. mem_ref_loc_p l = &ref->accesses_in_loop[i];
  1440. if (!flow_bb_inside_loop_p (loop, gimple_bb (l->stmt)))
  1441. break;
  1442. if (fn (l))
  1443. return true;
  1444. }
  1445. for (i = loc - ref->accesses_in_loop.address ();
  1446. i < ref->accesses_in_loop.length (); ++i)
  1447. {
  1448. mem_ref_loc_p l = &ref->accesses_in_loop[i];
  1449. if (!flow_bb_inside_loop_p (loop, gimple_bb (l->stmt)))
  1450. break;
  1451. if (fn (l))
  1452. return true;
  1453. }
  1454. return false;
  1455. }
  1456. /* Rewrites location LOC by TMP_VAR. */
  1457. struct rewrite_mem_ref_loc
  1458. {
  1459. rewrite_mem_ref_loc (tree tmp_var_) : tmp_var (tmp_var_) {}
  1460. bool operator () (mem_ref_loc_p loc);
  1461. tree tmp_var;
  1462. };
  1463. bool
  1464. rewrite_mem_ref_loc::operator () (mem_ref_loc_p loc)
  1465. {
  1466. *loc->ref = tmp_var;
  1467. update_stmt (loc->stmt);
  1468. return false;
  1469. }
  1470. /* Rewrites all references to REF in LOOP by variable TMP_VAR. */
  1471. static void
  1472. rewrite_mem_refs (struct loop *loop, mem_ref_p ref, tree tmp_var)
  1473. {
  1474. for_all_locs_in_loop (loop, ref, rewrite_mem_ref_loc (tmp_var));
  1475. }
  1476. /* Stores the first reference location in LOCP. */
  1477. struct first_mem_ref_loc_1
  1478. {
  1479. first_mem_ref_loc_1 (mem_ref_loc_p *locp_) : locp (locp_) {}
  1480. bool operator () (mem_ref_loc_p loc);
  1481. mem_ref_loc_p *locp;
  1482. };
  1483. bool
  1484. first_mem_ref_loc_1::operator () (mem_ref_loc_p loc)
  1485. {
  1486. *locp = loc;
  1487. return true;
  1488. }
  1489. /* Returns the first reference location to REF in LOOP. */
  1490. static mem_ref_loc_p
  1491. first_mem_ref_loc (struct loop *loop, mem_ref_p ref)
  1492. {
  1493. mem_ref_loc_p locp = NULL;
  1494. for_all_locs_in_loop (loop, ref, first_mem_ref_loc_1 (&locp));
  1495. return locp;
  1496. }
  1497. struct prev_flag_edges {
  1498. /* Edge to insert new flag comparison code. */
  1499. edge append_cond_position;
  1500. /* Edge for fall through from previous flag comparison. */
  1501. edge last_cond_fallthru;
  1502. };
  1503. /* Helper function for execute_sm. Emit code to store TMP_VAR into
  1504. MEM along edge EX.
  1505. The store is only done if MEM has changed. We do this so no
  1506. changes to MEM occur on code paths that did not originally store
  1507. into it.
  1508. The common case for execute_sm will transform:
  1509. for (...) {
  1510. if (foo)
  1511. stuff;
  1512. else
  1513. MEM = TMP_VAR;
  1514. }
  1515. into:
  1516. lsm = MEM;
  1517. for (...) {
  1518. if (foo)
  1519. stuff;
  1520. else
  1521. lsm = TMP_VAR;
  1522. }
  1523. MEM = lsm;
  1524. This function will generate:
  1525. lsm = MEM;
  1526. lsm_flag = false;
  1527. ...
  1528. for (...) {
  1529. if (foo)
  1530. stuff;
  1531. else {
  1532. lsm = TMP_VAR;
  1533. lsm_flag = true;
  1534. }
  1535. }
  1536. if (lsm_flag) <--
  1537. MEM = lsm; <--
  1538. */
  1539. static void
  1540. execute_sm_if_changed (edge ex, tree mem, tree tmp_var, tree flag)
  1541. {
  1542. basic_block new_bb, then_bb, old_dest;
  1543. bool loop_has_only_one_exit;
  1544. edge then_old_edge, orig_ex = ex;
  1545. gimple_stmt_iterator gsi;
  1546. gimple stmt;
  1547. struct prev_flag_edges *prev_edges = (struct prev_flag_edges *) ex->aux;
  1548. bool irr = ex->flags & EDGE_IRREDUCIBLE_LOOP;
  1549. /* ?? Insert store after previous store if applicable. See note
  1550. below. */
  1551. if (prev_edges)
  1552. ex = prev_edges->append_cond_position;
  1553. loop_has_only_one_exit = single_pred_p (ex->dest);
  1554. if (loop_has_only_one_exit)
  1555. ex = split_block_after_labels (ex->dest);
  1556. old_dest = ex->dest;
  1557. new_bb = split_edge (ex);
  1558. then_bb = create_empty_bb (new_bb);
  1559. if (irr)
  1560. then_bb->flags = BB_IRREDUCIBLE_LOOP;
  1561. add_bb_to_loop (then_bb, new_bb->loop_father);
  1562. gsi = gsi_start_bb (new_bb);
  1563. stmt = gimple_build_cond (NE_EXPR, flag, boolean_false_node,
  1564. NULL_TREE, NULL_TREE);
  1565. gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
  1566. gsi = gsi_start_bb (then_bb);
  1567. /* Insert actual store. */
  1568. stmt = gimple_build_assign (unshare_expr (mem), tmp_var);
  1569. gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
  1570. make_edge (new_bb, then_bb,
  1571. EDGE_TRUE_VALUE | (irr ? EDGE_IRREDUCIBLE_LOOP : 0));
  1572. make_edge (new_bb, old_dest,
  1573. EDGE_FALSE_VALUE | (irr ? EDGE_IRREDUCIBLE_LOOP : 0));
  1574. then_old_edge = make_edge (then_bb, old_dest,
  1575. EDGE_FALLTHRU | (irr ? EDGE_IRREDUCIBLE_LOOP : 0));
  1576. set_immediate_dominator (CDI_DOMINATORS, then_bb, new_bb);
  1577. if (prev_edges)
  1578. {
  1579. basic_block prevbb = prev_edges->last_cond_fallthru->src;
  1580. redirect_edge_succ (prev_edges->last_cond_fallthru, new_bb);
  1581. set_immediate_dominator (CDI_DOMINATORS, new_bb, prevbb);
  1582. set_immediate_dominator (CDI_DOMINATORS, old_dest,
  1583. recompute_dominator (CDI_DOMINATORS, old_dest));
  1584. }
  1585. /* ?? Because stores may alias, they must happen in the exact
  1586. sequence they originally happened. Save the position right after
  1587. the (_lsm) store we just created so we can continue appending after
  1588. it and maintain the original order. */
  1589. {
  1590. struct prev_flag_edges *p;
  1591. if (orig_ex->aux)
  1592. orig_ex->aux = NULL;
  1593. alloc_aux_for_edge (orig_ex, sizeof (struct prev_flag_edges));
  1594. p = (struct prev_flag_edges *) orig_ex->aux;
  1595. p->append_cond_position = then_old_edge;
  1596. p->last_cond_fallthru = find_edge (new_bb, old_dest);
  1597. orig_ex->aux = (void *) p;
  1598. }
  1599. if (!loop_has_only_one_exit)
  1600. for (gphi_iterator gpi = gsi_start_phis (old_dest);
  1601. !gsi_end_p (gpi); gsi_next (&gpi))
  1602. {
  1603. gphi *phi = gpi.phi ();
  1604. unsigned i;
  1605. for (i = 0; i < gimple_phi_num_args (phi); i++)
  1606. if (gimple_phi_arg_edge (phi, i)->src == new_bb)
  1607. {
  1608. tree arg = gimple_phi_arg_def (phi, i);
  1609. add_phi_arg (phi, arg, then_old_edge, UNKNOWN_LOCATION);
  1610. update_stmt (phi);
  1611. }
  1612. }
  1613. /* Remove the original fall through edge. This was the
  1614. single_succ_edge (new_bb). */
  1615. EDGE_SUCC (new_bb, 0)->flags &= ~EDGE_FALLTHRU;
  1616. }
  1617. /* When REF is set on the location, set flag indicating the store. */
  1618. struct sm_set_flag_if_changed
  1619. {
  1620. sm_set_flag_if_changed (tree flag_) : flag (flag_) {}
  1621. bool operator () (mem_ref_loc_p loc);
  1622. tree flag;
  1623. };
  1624. bool
  1625. sm_set_flag_if_changed::operator () (mem_ref_loc_p loc)
  1626. {
  1627. /* Only set the flag for writes. */
  1628. if (is_gimple_assign (loc->stmt)
  1629. && gimple_assign_lhs_ptr (loc->stmt) == loc->ref)
  1630. {
  1631. gimple_stmt_iterator gsi = gsi_for_stmt (loc->stmt);
  1632. gimple stmt = gimple_build_assign (flag, boolean_true_node);
  1633. gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
  1634. }
  1635. return false;
  1636. }
  1637. /* Helper function for execute_sm. On every location where REF is
  1638. set, set an appropriate flag indicating the store. */
  1639. static tree
  1640. execute_sm_if_changed_flag_set (struct loop *loop, mem_ref_p ref)
  1641. {
  1642. tree flag;
  1643. char *str = get_lsm_tmp_name (ref->mem.ref, ~0, "_flag");
  1644. flag = create_tmp_reg (boolean_type_node, str);
  1645. for_all_locs_in_loop (loop, ref, sm_set_flag_if_changed (flag));
  1646. return flag;
  1647. }
  1648. /* Executes store motion of memory reference REF from LOOP.
  1649. Exits from the LOOP are stored in EXITS. The initialization of the
  1650. temporary variable is put to the preheader of the loop, and assignments
  1651. to the reference from the temporary variable are emitted to exits. */
  1652. static void
  1653. execute_sm (struct loop *loop, vec<edge> exits, mem_ref_p ref)
  1654. {
  1655. tree tmp_var, store_flag = NULL_TREE;
  1656. unsigned i;
  1657. gassign *load;
  1658. struct fmt_data fmt_data;
  1659. edge ex;
  1660. struct lim_aux_data *lim_data;
  1661. bool multi_threaded_model_p = false;
  1662. gimple_stmt_iterator gsi;
  1663. if (dump_file && (dump_flags & TDF_DETAILS))
  1664. {
  1665. fprintf (dump_file, "Executing store motion of ");
  1666. print_generic_expr (dump_file, ref->mem.ref, 0);
  1667. fprintf (dump_file, " from loop %d\n", loop->num);
  1668. }
  1669. tmp_var = create_tmp_reg (TREE_TYPE (ref->mem.ref),
  1670. get_lsm_tmp_name (ref->mem.ref, ~0));
  1671. fmt_data.loop = loop;
  1672. fmt_data.orig_loop = loop;
  1673. for_each_index (&ref->mem.ref, force_move_till, &fmt_data);
  1674. if (bb_in_transaction (loop_preheader_edge (loop)->src)
  1675. || !PARAM_VALUE (PARAM_ALLOW_STORE_DATA_RACES))
  1676. multi_threaded_model_p = true;
  1677. if (multi_threaded_model_p)
  1678. store_flag = execute_sm_if_changed_flag_set (loop, ref);
  1679. rewrite_mem_refs (loop, ref, tmp_var);
  1680. /* Emit the load code on a random exit edge or into the latch if
  1681. the loop does not exit, so that we are sure it will be processed
  1682. by move_computations after all dependencies. */
  1683. gsi = gsi_for_stmt (first_mem_ref_loc (loop, ref)->stmt);
  1684. /* FIXME/TODO: For the multi-threaded variant, we could avoid this
  1685. load altogether, since the store is predicated by a flag. We
  1686. could, do the load only if it was originally in the loop. */
  1687. load = gimple_build_assign (tmp_var, unshare_expr (ref->mem.ref));
  1688. lim_data = init_lim_data (load);
  1689. lim_data->max_loop = loop;
  1690. lim_data->tgt_loop = loop;
  1691. gsi_insert_before (&gsi, load, GSI_SAME_STMT);
  1692. if (multi_threaded_model_p)
  1693. {
  1694. load = gimple_build_assign (store_flag, boolean_false_node);
  1695. lim_data = init_lim_data (load);
  1696. lim_data->max_loop = loop;
  1697. lim_data->tgt_loop = loop;
  1698. gsi_insert_before (&gsi, load, GSI_SAME_STMT);
  1699. }
  1700. /* Sink the store to every exit from the loop. */
  1701. FOR_EACH_VEC_ELT (exits, i, ex)
  1702. if (!multi_threaded_model_p)
  1703. {
  1704. gassign *store;
  1705. store = gimple_build_assign (unshare_expr (ref->mem.ref), tmp_var);
  1706. gsi_insert_on_edge (ex, store);
  1707. }
  1708. else
  1709. execute_sm_if_changed (ex, ref->mem.ref, tmp_var, store_flag);
  1710. }
  1711. /* Hoists memory references MEM_REFS out of LOOP. EXITS is the list of exit
  1712. edges of the LOOP. */
  1713. static void
  1714. hoist_memory_references (struct loop *loop, bitmap mem_refs,
  1715. vec<edge> exits)
  1716. {
  1717. mem_ref_p ref;
  1718. unsigned i;
  1719. bitmap_iterator bi;
  1720. EXECUTE_IF_SET_IN_BITMAP (mem_refs, 0, i, bi)
  1721. {
  1722. ref = memory_accesses.refs_list[i];
  1723. execute_sm (loop, exits, ref);
  1724. }
  1725. }
  1726. struct ref_always_accessed
  1727. {
  1728. ref_always_accessed (struct loop *loop_, bool stored_p_)
  1729. : loop (loop_), stored_p (stored_p_) {}
  1730. bool operator () (mem_ref_loc_p loc);
  1731. struct loop *loop;
  1732. bool stored_p;
  1733. };
  1734. bool
  1735. ref_always_accessed::operator () (mem_ref_loc_p loc)
  1736. {
  1737. struct loop *must_exec;
  1738. if (!get_lim_data (loc->stmt))
  1739. return false;
  1740. /* If we require an always executed store make sure the statement
  1741. stores to the reference. */
  1742. if (stored_p)
  1743. {
  1744. tree lhs = gimple_get_lhs (loc->stmt);
  1745. if (!lhs
  1746. || lhs != *loc->ref)
  1747. return false;
  1748. }
  1749. must_exec = get_lim_data (loc->stmt)->always_executed_in;
  1750. if (!must_exec)
  1751. return false;
  1752. if (must_exec == loop
  1753. || flow_loop_nested_p (must_exec, loop))
  1754. return true;
  1755. return false;
  1756. }
  1757. /* Returns true if REF is always accessed in LOOP. If STORED_P is true
  1758. make sure REF is always stored to in LOOP. */
  1759. static bool
  1760. ref_always_accessed_p (struct loop *loop, mem_ref_p ref, bool stored_p)
  1761. {
  1762. return for_all_locs_in_loop (loop, ref,
  1763. ref_always_accessed (loop, stored_p));
  1764. }
  1765. /* Returns true if REF1 and REF2 are independent. */
  1766. static bool
  1767. refs_independent_p (mem_ref_p ref1, mem_ref_p ref2)
  1768. {
  1769. if (ref1 == ref2)
  1770. return true;
  1771. if (dump_file && (dump_flags & TDF_DETAILS))
  1772. fprintf (dump_file, "Querying dependency of refs %u and %u: ",
  1773. ref1->id, ref2->id);
  1774. if (mem_refs_may_alias_p (ref1, ref2, &memory_accesses.ttae_cache))
  1775. {
  1776. if (dump_file && (dump_flags & TDF_DETAILS))
  1777. fprintf (dump_file, "dependent.\n");
  1778. return false;
  1779. }
  1780. else
  1781. {
  1782. if (dump_file && (dump_flags & TDF_DETAILS))
  1783. fprintf (dump_file, "independent.\n");
  1784. return true;
  1785. }
  1786. }
  1787. /* Mark REF dependent on stores or loads (according to STORED_P) in LOOP
  1788. and its super-loops. */
  1789. static void
  1790. record_dep_loop (struct loop *loop, mem_ref_p ref, bool stored_p)
  1791. {
  1792. /* We can propagate dependent-in-loop bits up the loop
  1793. hierarchy to all outer loops. */
  1794. while (loop != current_loops->tree_root
  1795. && bitmap_set_bit (&ref->dep_loop, LOOP_DEP_BIT (loop->num, stored_p)))
  1796. loop = loop_outer (loop);
  1797. }
  1798. /* Returns true if REF is independent on all other memory references in
  1799. LOOP. */
  1800. static bool
  1801. ref_indep_loop_p_1 (struct loop *loop, mem_ref_p ref, bool stored_p)
  1802. {
  1803. bitmap refs_to_check;
  1804. unsigned i;
  1805. bitmap_iterator bi;
  1806. mem_ref_p aref;
  1807. if (stored_p)
  1808. refs_to_check = &memory_accesses.refs_in_loop[loop->num];
  1809. else
  1810. refs_to_check = &memory_accesses.refs_stored_in_loop[loop->num];
  1811. if (bitmap_bit_p (refs_to_check, UNANALYZABLE_MEM_ID))
  1812. return false;
  1813. EXECUTE_IF_SET_IN_BITMAP (refs_to_check, 0, i, bi)
  1814. {
  1815. aref = memory_accesses.refs_list[i];
  1816. if (!refs_independent_p (ref, aref))
  1817. return false;
  1818. }
  1819. return true;
  1820. }
  1821. /* Returns true if REF is independent on all other memory references in
  1822. LOOP. Wrapper over ref_indep_loop_p_1, caching its results. */
  1823. static bool
  1824. ref_indep_loop_p_2 (struct loop *loop, mem_ref_p ref, bool stored_p)
  1825. {
  1826. stored_p |= (ref->stored && bitmap_bit_p (ref->stored, loop->num));
  1827. if (bitmap_bit_p (&ref->indep_loop, LOOP_DEP_BIT (loop->num, stored_p)))
  1828. return true;
  1829. if (bitmap_bit_p (&ref->dep_loop, LOOP_DEP_BIT (loop->num, stored_p)))
  1830. return false;
  1831. struct loop *inner = loop->inner;
  1832. while (inner)
  1833. {
  1834. if (!ref_indep_loop_p_2 (inner, ref, stored_p))
  1835. return false;
  1836. inner = inner->next;
  1837. }
  1838. bool indep_p = ref_indep_loop_p_1 (loop, ref, stored_p);
  1839. if (dump_file && (dump_flags & TDF_DETAILS))
  1840. fprintf (dump_file, "Querying dependencies of ref %u in loop %d: %s\n",
  1841. ref->id, loop->num, indep_p ? "independent" : "dependent");
  1842. /* Record the computed result in the cache. */
  1843. if (indep_p)
  1844. {
  1845. if (bitmap_set_bit (&ref->indep_loop, LOOP_DEP_BIT (loop->num, stored_p))
  1846. && stored_p)
  1847. {
  1848. /* If it's independend against all refs then it's independent
  1849. against stores, too. */
  1850. bitmap_set_bit (&ref->indep_loop, LOOP_DEP_BIT (loop->num, false));
  1851. }
  1852. }
  1853. else
  1854. {
  1855. record_dep_loop (loop, ref, stored_p);
  1856. if (!stored_p)
  1857. {
  1858. /* If it's dependent against stores it's dependent against
  1859. all refs, too. */
  1860. record_dep_loop (loop, ref, true);
  1861. }
  1862. }
  1863. return indep_p;
  1864. }
  1865. /* Returns true if REF is independent on all other memory references in
  1866. LOOP. */
  1867. static bool
  1868. ref_indep_loop_p (struct loop *loop, mem_ref_p ref)
  1869. {
  1870. gcc_checking_assert (MEM_ANALYZABLE (ref));
  1871. return ref_indep_loop_p_2 (loop, ref, false);
  1872. }
  1873. /* Returns true if we can perform store motion of REF from LOOP. */
  1874. static bool
  1875. can_sm_ref_p (struct loop *loop, mem_ref_p ref)
  1876. {
  1877. tree base;
  1878. /* Can't hoist unanalyzable refs. */
  1879. if (!MEM_ANALYZABLE (ref))
  1880. return false;
  1881. /* It should be movable. */
  1882. if (!is_gimple_reg_type (TREE_TYPE (ref->mem.ref))
  1883. || TREE_THIS_VOLATILE (ref->mem.ref)
  1884. || !for_each_index (&ref->mem.ref, may_move_till, loop))
  1885. return false;
  1886. /* If it can throw fail, we do not properly update EH info. */
  1887. if (tree_could_throw_p (ref->mem.ref))
  1888. return false;
  1889. /* If it can trap, it must be always executed in LOOP.
  1890. Readonly memory locations may trap when storing to them, but
  1891. tree_could_trap_p is a predicate for rvalues, so check that
  1892. explicitly. */
  1893. base = get_base_address (ref->mem.ref);
  1894. if ((tree_could_trap_p (ref->mem.ref)
  1895. || (DECL_P (base) && TREE_READONLY (base)))
  1896. && !ref_always_accessed_p (loop, ref, true))
  1897. return false;
  1898. /* And it must be independent on all other memory references
  1899. in LOOP. */
  1900. if (!ref_indep_loop_p (loop, ref))
  1901. return false;
  1902. return true;
  1903. }
  1904. /* Marks the references in LOOP for that store motion should be performed
  1905. in REFS_TO_SM. SM_EXECUTED is the set of references for that store
  1906. motion was performed in one of the outer loops. */
  1907. static void
  1908. find_refs_for_sm (struct loop *loop, bitmap sm_executed, bitmap refs_to_sm)
  1909. {
  1910. bitmap refs = &memory_accesses.all_refs_stored_in_loop[loop->num];
  1911. unsigned i;
  1912. bitmap_iterator bi;
  1913. mem_ref_p ref;
  1914. EXECUTE_IF_AND_COMPL_IN_BITMAP (refs, sm_executed, 0, i, bi)
  1915. {
  1916. ref = memory_accesses.refs_list[i];
  1917. if (can_sm_ref_p (loop, ref))
  1918. bitmap_set_bit (refs_to_sm, i);
  1919. }
  1920. }
  1921. /* Checks whether LOOP (with exits stored in EXITS array) is suitable
  1922. for a store motion optimization (i.e. whether we can insert statement
  1923. on its exits). */
  1924. static bool
  1925. loop_suitable_for_sm (struct loop *loop ATTRIBUTE_UNUSED,
  1926. vec<edge> exits)
  1927. {
  1928. unsigned i;
  1929. edge ex;
  1930. FOR_EACH_VEC_ELT (exits, i, ex)
  1931. if (ex->flags & (EDGE_ABNORMAL | EDGE_EH))
  1932. return false;
  1933. return true;
  1934. }
  1935. /* Try to perform store motion for all memory references modified inside
  1936. LOOP. SM_EXECUTED is the bitmap of the memory references for that
  1937. store motion was executed in one of the outer loops. */
  1938. static void
  1939. store_motion_loop (struct loop *loop, bitmap sm_executed)
  1940. {
  1941. vec<edge> exits = get_loop_exit_edges (loop);
  1942. struct loop *subloop;
  1943. bitmap sm_in_loop = BITMAP_ALLOC (&lim_bitmap_obstack);
  1944. if (loop_suitable_for_sm (loop, exits))
  1945. {
  1946. find_refs_for_sm (loop, sm_executed, sm_in_loop);
  1947. hoist_memory_references (loop, sm_in_loop, exits);
  1948. }
  1949. exits.release ();
  1950. bitmap_ior_into (sm_executed, sm_in_loop);
  1951. for (subloop = loop->inner; subloop != NULL; subloop = subloop->next)
  1952. store_motion_loop (subloop, sm_executed);
  1953. bitmap_and_compl_into (sm_executed, sm_in_loop);
  1954. BITMAP_FREE (sm_in_loop);
  1955. }
  1956. /* Try to perform store motion for all memory references modified inside
  1957. loops. */
  1958. static void
  1959. store_motion (void)
  1960. {
  1961. struct loop *loop;
  1962. bitmap sm_executed = BITMAP_ALLOC (&lim_bitmap_obstack);
  1963. for (loop = current_loops->tree_root->inner; loop != NULL; loop = loop->next)
  1964. store_motion_loop (loop, sm_executed);
  1965. BITMAP_FREE (sm_executed);
  1966. gsi_commit_edge_inserts ();
  1967. }
  1968. /* Fills ALWAYS_EXECUTED_IN information for basic blocks of LOOP, i.e.
  1969. for each such basic block bb records the outermost loop for that execution
  1970. of its header implies execution of bb. CONTAINS_CALL is the bitmap of
  1971. blocks that contain a nonpure call. */
  1972. static void
  1973. fill_always_executed_in_1 (struct loop *loop, sbitmap contains_call)
  1974. {
  1975. basic_block bb = NULL, *bbs, last = NULL;
  1976. unsigned i;
  1977. edge e;
  1978. struct loop *inn_loop = loop;
  1979. if (ALWAYS_EXECUTED_IN (loop->header) == NULL)
  1980. {
  1981. bbs = get_loop_body_in_dom_order (loop);
  1982. for (i = 0; i < loop->num_nodes; i++)
  1983. {
  1984. edge_iterator ei;
  1985. bb = bbs[i];
  1986. if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
  1987. last = bb;
  1988. if (bitmap_bit_p (contains_call, bb->index))
  1989. break;
  1990. FOR_EACH_EDGE (e, ei, bb->succs)
  1991. if (!flow_bb_inside_loop_p (loop, e->dest))
  1992. break;
  1993. if (e)
  1994. break;
  1995. /* A loop might be infinite (TODO use simple loop analysis
  1996. to disprove this if possible). */
  1997. if (bb->flags & BB_IRREDUCIBLE_LOOP)
  1998. break;
  1999. if (!flow_bb_inside_loop_p (inn_loop, bb))
  2000. break;
  2001. if (bb->loop_father->header == bb)
  2002. {
  2003. if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
  2004. break;
  2005. /* In a loop that is always entered we may proceed anyway.
  2006. But record that we entered it and stop once we leave it. */
  2007. inn_loop = bb->loop_father;
  2008. }
  2009. }
  2010. while (1)
  2011. {
  2012. SET_ALWAYS_EXECUTED_IN (last, loop);
  2013. if (last == loop->header)
  2014. break;
  2015. last = get_immediate_dominator (CDI_DOMINATORS, last);
  2016. }
  2017. free (bbs);
  2018. }
  2019. for (loop = loop->inner; loop; loop = loop->next)
  2020. fill_always_executed_in_1 (loop, contains_call);
  2021. }
  2022. /* Fills ALWAYS_EXECUTED_IN information for basic blocks, i.e.
  2023. for each such basic block bb records the outermost loop for that execution
  2024. of its header implies execution of bb. */
  2025. static void
  2026. fill_always_executed_in (void)
  2027. {
  2028. sbitmap contains_call = sbitmap_alloc (last_basic_block_for_fn (cfun));
  2029. basic_block bb;
  2030. struct loop *loop;
  2031. bitmap_clear (contains_call);
  2032. FOR_EACH_BB_FN (bb, cfun)
  2033. {
  2034. gimple_stmt_iterator gsi;
  2035. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  2036. {
  2037. if (nonpure_call_p (gsi_stmt (gsi)))
  2038. break;
  2039. }
  2040. if (!gsi_end_p (gsi))
  2041. bitmap_set_bit (contains_call, bb->index);
  2042. }
  2043. for (loop = current_loops->tree_root->inner; loop; loop = loop->next)
  2044. fill_always_executed_in_1 (loop, contains_call);
  2045. sbitmap_free (contains_call);
  2046. }
  2047. /* Compute the global information needed by the loop invariant motion pass. */
  2048. static void
  2049. tree_ssa_lim_initialize (void)
  2050. {
  2051. struct loop *loop;
  2052. unsigned i;
  2053. bitmap_obstack_initialize (&lim_bitmap_obstack);
  2054. gcc_obstack_init (&mem_ref_obstack);
  2055. lim_aux_data_map = new hash_map<gimple, lim_aux_data *>;
  2056. if (flag_tm)
  2057. compute_transaction_bits ();
  2058. alloc_aux_for_edges (0);
  2059. memory_accesses.refs = new hash_table<mem_ref_hasher> (100);
  2060. memory_accesses.refs_list.create (100);
  2061. /* Allocate a special, unanalyzable mem-ref with ID zero. */
  2062. memory_accesses.refs_list.quick_push
  2063. (mem_ref_alloc (error_mark_node, 0, UNANALYZABLE_MEM_ID));
  2064. memory_accesses.refs_in_loop.create (number_of_loops (cfun));
  2065. memory_accesses.refs_in_loop.quick_grow (number_of_loops (cfun));
  2066. memory_accesses.refs_stored_in_loop.create (number_of_loops (cfun));
  2067. memory_accesses.refs_stored_in_loop.quick_grow (number_of_loops (cfun));
  2068. memory_accesses.all_refs_stored_in_loop.create (number_of_loops (cfun));
  2069. memory_accesses.all_refs_stored_in_loop.quick_grow (number_of_loops (cfun));
  2070. for (i = 0; i < number_of_loops (cfun); i++)
  2071. {
  2072. bitmap_initialize (&memory_accesses.refs_in_loop[i],
  2073. &lim_bitmap_obstack);
  2074. bitmap_initialize (&memory_accesses.refs_stored_in_loop[i],
  2075. &lim_bitmap_obstack);
  2076. bitmap_initialize (&memory_accesses.all_refs_stored_in_loop[i],
  2077. &lim_bitmap_obstack);
  2078. }
  2079. memory_accesses.ttae_cache = NULL;
  2080. /* Initialize bb_loop_postorder with a mapping from loop->num to
  2081. its postorder index. */
  2082. i = 0;
  2083. bb_loop_postorder = XNEWVEC (unsigned, number_of_loops (cfun));
  2084. FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
  2085. bb_loop_postorder[loop->num] = i++;
  2086. }
  2087. /* Cleans up after the invariant motion pass. */
  2088. static void
  2089. tree_ssa_lim_finalize (void)
  2090. {
  2091. basic_block bb;
  2092. unsigned i;
  2093. mem_ref_p ref;
  2094. free_aux_for_edges ();
  2095. FOR_EACH_BB_FN (bb, cfun)
  2096. SET_ALWAYS_EXECUTED_IN (bb, NULL);
  2097. bitmap_obstack_release (&lim_bitmap_obstack);
  2098. delete lim_aux_data_map;
  2099. delete memory_accesses.refs;
  2100. memory_accesses.refs = NULL;
  2101. FOR_EACH_VEC_ELT (memory_accesses.refs_list, i, ref)
  2102. memref_free (ref);
  2103. memory_accesses.refs_list.release ();
  2104. obstack_free (&mem_ref_obstack, NULL);
  2105. memory_accesses.refs_in_loop.release ();
  2106. memory_accesses.refs_stored_in_loop.release ();
  2107. memory_accesses.all_refs_stored_in_loop.release ();
  2108. if (memory_accesses.ttae_cache)
  2109. free_affine_expand_cache (&memory_accesses.ttae_cache);
  2110. free (bb_loop_postorder);
  2111. }
  2112. /* Moves invariants from loops. Only "expensive" invariants are moved out --
  2113. i.e. those that are likely to be win regardless of the register pressure. */
  2114. unsigned int
  2115. tree_ssa_lim (void)
  2116. {
  2117. unsigned int todo;
  2118. tree_ssa_lim_initialize ();
  2119. /* Gathers information about memory accesses in the loops. */
  2120. analyze_memory_references ();
  2121. /* Fills ALWAYS_EXECUTED_IN information for basic blocks. */
  2122. fill_always_executed_in ();
  2123. /* For each statement determine the outermost loop in that it is
  2124. invariant and cost for computing the invariant. */
  2125. invariantness_dom_walker (CDI_DOMINATORS)
  2126. .walk (cfun->cfg->x_entry_block_ptr);
  2127. /* Execute store motion. Force the necessary invariants to be moved
  2128. out of the loops as well. */
  2129. store_motion ();
  2130. /* Move the expressions that are expensive enough. */
  2131. todo = move_computations ();
  2132. tree_ssa_lim_finalize ();
  2133. return todo;
  2134. }
  2135. /* Loop invariant motion pass. */
  2136. namespace {
  2137. const pass_data pass_data_lim =
  2138. {
  2139. GIMPLE_PASS, /* type */
  2140. "lim", /* name */
  2141. OPTGROUP_LOOP, /* optinfo_flags */
  2142. TV_LIM, /* tv_id */
  2143. PROP_cfg, /* properties_required */
  2144. 0, /* properties_provided */
  2145. 0, /* properties_destroyed */
  2146. 0, /* todo_flags_start */
  2147. 0, /* todo_flags_finish */
  2148. };
  2149. class pass_lim : public gimple_opt_pass
  2150. {
  2151. public:
  2152. pass_lim (gcc::context *ctxt)
  2153. : gimple_opt_pass (pass_data_lim, ctxt)
  2154. {}
  2155. /* opt_pass methods: */
  2156. opt_pass * clone () { return new pass_lim (m_ctxt); }
  2157. virtual bool gate (function *) { return flag_tree_loop_im != 0; }
  2158. virtual unsigned int execute (function *);
  2159. }; // class pass_lim
  2160. unsigned int
  2161. pass_lim::execute (function *fun)
  2162. {
  2163. if (number_of_loops (fun) <= 1)
  2164. return 0;
  2165. return tree_ssa_lim ();
  2166. }
  2167. } // anon namespace
  2168. gimple_opt_pass *
  2169. make_pass_lim (gcc::context *ctxt)
  2170. {
  2171. return new pass_lim (ctxt);
  2172. }