tree-ssa-phiopt.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287
  1. /* Optimization of PHI nodes by converting them into straightline code.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "hash-table.h"
  19. #include "tm.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "fold-const.h"
  31. #include "stor-layout.h"
  32. #include "flags.h"
  33. #include "tm_p.h"
  34. #include "predict.h"
  35. #include "hard-reg-set.h"
  36. #include "function.h"
  37. #include "dominance.h"
  38. #include "cfg.h"
  39. #include "cfganal.h"
  40. #include "basic-block.h"
  41. #include "tree-ssa-alias.h"
  42. #include "internal-fn.h"
  43. #include "gimple-expr.h"
  44. #include "is-a.h"
  45. #include "gimple.h"
  46. #include "gimplify.h"
  47. #include "gimple-iterator.h"
  48. #include "gimplify-me.h"
  49. #include "gimple-ssa.h"
  50. #include "tree-cfg.h"
  51. #include "tree-phinodes.h"
  52. #include "ssa-iterators.h"
  53. #include "stringpool.h"
  54. #include "tree-ssanames.h"
  55. #include "hashtab.h"
  56. #include "rtl.h"
  57. #include "statistics.h"
  58. #include "real.h"
  59. #include "fixed-value.h"
  60. #include "insn-config.h"
  61. #include "expmed.h"
  62. #include "dojump.h"
  63. #include "explow.h"
  64. #include "calls.h"
  65. #include "emit-rtl.h"
  66. #include "varasm.h"
  67. #include "stmt.h"
  68. #include "expr.h"
  69. #include "tree-dfa.h"
  70. #include "tree-pass.h"
  71. #include "langhooks.h"
  72. #include "domwalk.h"
  73. #include "cfgloop.h"
  74. #include "tree-data-ref.h"
  75. #include "gimple-pretty-print.h"
  76. #include "insn-codes.h"
  77. #include "optabs.h"
  78. #include "tree-scalar-evolution.h"
  79. #include "tree-inline.h"
  80. #ifndef HAVE_conditional_move
  81. #define HAVE_conditional_move (0)
  82. #endif
  83. static unsigned int tree_ssa_phiopt_worker (bool, bool);
  84. static bool conditional_replacement (basic_block, basic_block,
  85. edge, edge, gphi *, tree, tree);
  86. static int value_replacement (basic_block, basic_block,
  87. edge, edge, gimple, tree, tree);
  88. static bool minmax_replacement (basic_block, basic_block,
  89. edge, edge, gimple, tree, tree);
  90. static bool abs_replacement (basic_block, basic_block,
  91. edge, edge, gimple, tree, tree);
  92. static bool cond_store_replacement (basic_block, basic_block, edge, edge,
  93. hash_set<tree> *);
  94. static bool cond_if_else_store_replacement (basic_block, basic_block, basic_block);
  95. static hash_set<tree> * get_non_trapping ();
  96. static void replace_phi_edge_with_variable (basic_block, edge, gimple, tree);
  97. static void hoist_adjacent_loads (basic_block, basic_block,
  98. basic_block, basic_block);
  99. static bool gate_hoist_loads (void);
  100. /* This pass tries to transform conditional stores into unconditional
  101. ones, enabling further simplifications with the simpler then and else
  102. blocks. In particular it replaces this:
  103. bb0:
  104. if (cond) goto bb2; else goto bb1;
  105. bb1:
  106. *p = RHS;
  107. bb2:
  108. with
  109. bb0:
  110. if (cond) goto bb1; else goto bb2;
  111. bb1:
  112. condtmp' = *p;
  113. bb2:
  114. condtmp = PHI <RHS, condtmp'>
  115. *p = condtmp;
  116. This transformation can only be done under several constraints,
  117. documented below. It also replaces:
  118. bb0:
  119. if (cond) goto bb2; else goto bb1;
  120. bb1:
  121. *p = RHS1;
  122. goto bb3;
  123. bb2:
  124. *p = RHS2;
  125. bb3:
  126. with
  127. bb0:
  128. if (cond) goto bb3; else goto bb1;
  129. bb1:
  130. bb3:
  131. condtmp = PHI <RHS1, RHS2>
  132. *p = condtmp; */
  133. static unsigned int
  134. tree_ssa_cs_elim (void)
  135. {
  136. unsigned todo;
  137. /* ??? We are not interested in loop related info, but the following
  138. will create it, ICEing as we didn't init loops with pre-headers.
  139. An interfacing issue of find_data_references_in_bb. */
  140. loop_optimizer_init (LOOPS_NORMAL);
  141. scev_initialize ();
  142. todo = tree_ssa_phiopt_worker (true, false);
  143. scev_finalize ();
  144. loop_optimizer_finalize ();
  145. return todo;
  146. }
  147. /* Return the singleton PHI in the SEQ of PHIs for edges E0 and E1. */
  148. static gphi *
  149. single_non_singleton_phi_for_edges (gimple_seq seq, edge e0, edge e1)
  150. {
  151. gimple_stmt_iterator i;
  152. gphi *phi = NULL;
  153. if (gimple_seq_singleton_p (seq))
  154. return as_a <gphi *> (gsi_stmt (gsi_start (seq)));
  155. for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
  156. {
  157. gphi *p = as_a <gphi *> (gsi_stmt (i));
  158. /* If the PHI arguments are equal then we can skip this PHI. */
  159. if (operand_equal_for_phi_arg_p (gimple_phi_arg_def (p, e0->dest_idx),
  160. gimple_phi_arg_def (p, e1->dest_idx)))
  161. continue;
  162. /* If we already have a PHI that has the two edge arguments are
  163. different, then return it is not a singleton for these PHIs. */
  164. if (phi)
  165. return NULL;
  166. phi = p;
  167. }
  168. return phi;
  169. }
  170. /* The core routine of conditional store replacement and normal
  171. phi optimizations. Both share much of the infrastructure in how
  172. to match applicable basic block patterns. DO_STORE_ELIM is true
  173. when we want to do conditional store replacement, false otherwise.
  174. DO_HOIST_LOADS is true when we want to hoist adjacent loads out
  175. of diamond control flow patterns, false otherwise. */
  176. static unsigned int
  177. tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads)
  178. {
  179. basic_block bb;
  180. basic_block *bb_order;
  181. unsigned n, i;
  182. bool cfgchanged = false;
  183. hash_set<tree> *nontrap = 0;
  184. if (do_store_elim)
  185. /* Calculate the set of non-trapping memory accesses. */
  186. nontrap = get_non_trapping ();
  187. /* Search every basic block for COND_EXPR we may be able to optimize.
  188. We walk the blocks in order that guarantees that a block with
  189. a single predecessor is processed before the predecessor.
  190. This ensures that we collapse inner ifs before visiting the
  191. outer ones, and also that we do not try to visit a removed
  192. block. */
  193. bb_order = single_pred_before_succ_order ();
  194. n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
  195. for (i = 0; i < n; i++)
  196. {
  197. gimple cond_stmt;
  198. gphi *phi;
  199. basic_block bb1, bb2;
  200. edge e1, e2;
  201. tree arg0, arg1;
  202. bb = bb_order[i];
  203. cond_stmt = last_stmt (bb);
  204. /* Check to see if the last statement is a GIMPLE_COND. */
  205. if (!cond_stmt
  206. || gimple_code (cond_stmt) != GIMPLE_COND)
  207. continue;
  208. e1 = EDGE_SUCC (bb, 0);
  209. bb1 = e1->dest;
  210. e2 = EDGE_SUCC (bb, 1);
  211. bb2 = e2->dest;
  212. /* We cannot do the optimization on abnormal edges. */
  213. if ((e1->flags & EDGE_ABNORMAL) != 0
  214. || (e2->flags & EDGE_ABNORMAL) != 0)
  215. continue;
  216. /* If either bb1's succ or bb2 or bb2's succ is non NULL. */
  217. if (EDGE_COUNT (bb1->succs) == 0
  218. || bb2 == NULL
  219. || EDGE_COUNT (bb2->succs) == 0)
  220. continue;
  221. /* Find the bb which is the fall through to the other. */
  222. if (EDGE_SUCC (bb1, 0)->dest == bb2)
  223. ;
  224. else if (EDGE_SUCC (bb2, 0)->dest == bb1)
  225. {
  226. basic_block bb_tmp = bb1;
  227. edge e_tmp = e1;
  228. bb1 = bb2;
  229. bb2 = bb_tmp;
  230. e1 = e2;
  231. e2 = e_tmp;
  232. }
  233. else if (do_store_elim
  234. && EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest)
  235. {
  236. basic_block bb3 = EDGE_SUCC (bb1, 0)->dest;
  237. if (!single_succ_p (bb1)
  238. || (EDGE_SUCC (bb1, 0)->flags & EDGE_FALLTHRU) == 0
  239. || !single_succ_p (bb2)
  240. || (EDGE_SUCC (bb2, 0)->flags & EDGE_FALLTHRU) == 0
  241. || EDGE_COUNT (bb3->preds) != 2)
  242. continue;
  243. if (cond_if_else_store_replacement (bb1, bb2, bb3))
  244. cfgchanged = true;
  245. continue;
  246. }
  247. else if (do_hoist_loads
  248. && EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest)
  249. {
  250. basic_block bb3 = EDGE_SUCC (bb1, 0)->dest;
  251. if (!FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (cond_stmt)))
  252. && single_succ_p (bb1)
  253. && single_succ_p (bb2)
  254. && single_pred_p (bb1)
  255. && single_pred_p (bb2)
  256. && EDGE_COUNT (bb->succs) == 2
  257. && EDGE_COUNT (bb3->preds) == 2
  258. /* If one edge or the other is dominant, a conditional move
  259. is likely to perform worse than the well-predicted branch. */
  260. && !predictable_edge_p (EDGE_SUCC (bb, 0))
  261. && !predictable_edge_p (EDGE_SUCC (bb, 1)))
  262. hoist_adjacent_loads (bb, bb1, bb2, bb3);
  263. continue;
  264. }
  265. else
  266. continue;
  267. e1 = EDGE_SUCC (bb1, 0);
  268. /* Make sure that bb1 is just a fall through. */
  269. if (!single_succ_p (bb1)
  270. || (e1->flags & EDGE_FALLTHRU) == 0)
  271. continue;
  272. /* Also make sure that bb1 only have one predecessor and that it
  273. is bb. */
  274. if (!single_pred_p (bb1)
  275. || single_pred (bb1) != bb)
  276. continue;
  277. if (do_store_elim)
  278. {
  279. /* bb1 is the middle block, bb2 the join block, bb the split block,
  280. e1 the fallthrough edge from bb1 to bb2. We can't do the
  281. optimization if the join block has more than two predecessors. */
  282. if (EDGE_COUNT (bb2->preds) > 2)
  283. continue;
  284. if (cond_store_replacement (bb1, bb2, e1, e2, nontrap))
  285. cfgchanged = true;
  286. }
  287. else
  288. {
  289. gimple_seq phis = phi_nodes (bb2);
  290. gimple_stmt_iterator gsi;
  291. bool candorest = true;
  292. /* Value replacement can work with more than one PHI
  293. so try that first. */
  294. for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
  295. {
  296. phi = as_a <gphi *> (gsi_stmt (gsi));
  297. arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
  298. arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
  299. if (value_replacement (bb, bb1, e1, e2, phi, arg0, arg1) == 2)
  300. {
  301. candorest = false;
  302. cfgchanged = true;
  303. break;
  304. }
  305. }
  306. if (!candorest)
  307. continue;
  308. phi = single_non_singleton_phi_for_edges (phis, e1, e2);
  309. if (!phi)
  310. continue;
  311. arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
  312. arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
  313. /* Something is wrong if we cannot find the arguments in the PHI
  314. node. */
  315. gcc_assert (arg0 != NULL && arg1 != NULL);
  316. /* Do the replacement of conditional if it can be done. */
  317. if (conditional_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
  318. cfgchanged = true;
  319. else if (abs_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
  320. cfgchanged = true;
  321. else if (minmax_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
  322. cfgchanged = true;
  323. }
  324. }
  325. free (bb_order);
  326. if (do_store_elim)
  327. delete nontrap;
  328. /* If the CFG has changed, we should cleanup the CFG. */
  329. if (cfgchanged && do_store_elim)
  330. {
  331. /* In cond-store replacement we have added some loads on edges
  332. and new VOPS (as we moved the store, and created a load). */
  333. gsi_commit_edge_inserts ();
  334. return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
  335. }
  336. else if (cfgchanged)
  337. return TODO_cleanup_cfg;
  338. return 0;
  339. }
  340. /* Replace PHI node element whose edge is E in block BB with variable NEW.
  341. Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
  342. is known to have two edges, one of which must reach BB). */
  343. static void
  344. replace_phi_edge_with_variable (basic_block cond_block,
  345. edge e, gimple phi, tree new_tree)
  346. {
  347. basic_block bb = gimple_bb (phi);
  348. basic_block block_to_remove;
  349. gimple_stmt_iterator gsi;
  350. /* Change the PHI argument to new. */
  351. SET_USE (PHI_ARG_DEF_PTR (phi, e->dest_idx), new_tree);
  352. /* Remove the empty basic block. */
  353. if (EDGE_SUCC (cond_block, 0)->dest == bb)
  354. {
  355. EDGE_SUCC (cond_block, 0)->flags |= EDGE_FALLTHRU;
  356. EDGE_SUCC (cond_block, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
  357. EDGE_SUCC (cond_block, 0)->probability = REG_BR_PROB_BASE;
  358. EDGE_SUCC (cond_block, 0)->count += EDGE_SUCC (cond_block, 1)->count;
  359. block_to_remove = EDGE_SUCC (cond_block, 1)->dest;
  360. }
  361. else
  362. {
  363. EDGE_SUCC (cond_block, 1)->flags |= EDGE_FALLTHRU;
  364. EDGE_SUCC (cond_block, 1)->flags
  365. &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
  366. EDGE_SUCC (cond_block, 1)->probability = REG_BR_PROB_BASE;
  367. EDGE_SUCC (cond_block, 1)->count += EDGE_SUCC (cond_block, 0)->count;
  368. block_to_remove = EDGE_SUCC (cond_block, 0)->dest;
  369. }
  370. delete_basic_block (block_to_remove);
  371. /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
  372. gsi = gsi_last_bb (cond_block);
  373. gsi_remove (&gsi, true);
  374. if (dump_file && (dump_flags & TDF_DETAILS))
  375. fprintf (dump_file,
  376. "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
  377. cond_block->index,
  378. bb->index);
  379. }
  380. /* The function conditional_replacement does the main work of doing the
  381. conditional replacement. Return true if the replacement is done.
  382. Otherwise return false.
  383. BB is the basic block where the replacement is going to be done on. ARG0
  384. is argument 0 from PHI. Likewise for ARG1. */
  385. static bool
  386. conditional_replacement (basic_block cond_bb, basic_block middle_bb,
  387. edge e0, edge e1, gphi *phi,
  388. tree arg0, tree arg1)
  389. {
  390. tree result;
  391. gimple stmt;
  392. gassign *new_stmt;
  393. tree cond;
  394. gimple_stmt_iterator gsi;
  395. edge true_edge, false_edge;
  396. tree new_var, new_var2;
  397. bool neg;
  398. /* FIXME: Gimplification of complex type is too hard for now. */
  399. /* We aren't prepared to handle vectors either (and it is a question
  400. if it would be worthwhile anyway). */
  401. if (!(INTEGRAL_TYPE_P (TREE_TYPE (arg0))
  402. || POINTER_TYPE_P (TREE_TYPE (arg0)))
  403. || !(INTEGRAL_TYPE_P (TREE_TYPE (arg1))
  404. || POINTER_TYPE_P (TREE_TYPE (arg1))))
  405. return false;
  406. /* The PHI arguments have the constants 0 and 1, or 0 and -1, then
  407. convert it to the conditional. */
  408. if ((integer_zerop (arg0) && integer_onep (arg1))
  409. || (integer_zerop (arg1) && integer_onep (arg0)))
  410. neg = false;
  411. else if ((integer_zerop (arg0) && integer_all_onesp (arg1))
  412. || (integer_zerop (arg1) && integer_all_onesp (arg0)))
  413. neg = true;
  414. else
  415. return false;
  416. if (!empty_block_p (middle_bb))
  417. return false;
  418. /* At this point we know we have a GIMPLE_COND with two successors.
  419. One successor is BB, the other successor is an empty block which
  420. falls through into BB.
  421. There is a single PHI node at the join point (BB) and its arguments
  422. are constants (0, 1) or (0, -1).
  423. So, given the condition COND, and the two PHI arguments, we can
  424. rewrite this PHI into non-branching code:
  425. dest = (COND) or dest = COND'
  426. We use the condition as-is if the argument associated with the
  427. true edge has the value one or the argument associated with the
  428. false edge as the value zero. Note that those conditions are not
  429. the same since only one of the outgoing edges from the GIMPLE_COND
  430. will directly reach BB and thus be associated with an argument. */
  431. stmt = last_stmt (cond_bb);
  432. result = PHI_RESULT (phi);
  433. /* To handle special cases like floating point comparison, it is easier and
  434. less error-prone to build a tree and gimplify it on the fly though it is
  435. less efficient. */
  436. cond = fold_build2_loc (gimple_location (stmt),
  437. gimple_cond_code (stmt), boolean_type_node,
  438. gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
  439. /* We need to know which is the true edge and which is the false
  440. edge so that we know when to invert the condition below. */
  441. extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
  442. if ((e0 == true_edge && integer_zerop (arg0))
  443. || (e0 == false_edge && !integer_zerop (arg0))
  444. || (e1 == true_edge && integer_zerop (arg1))
  445. || (e1 == false_edge && !integer_zerop (arg1)))
  446. cond = fold_build1_loc (gimple_location (stmt),
  447. TRUTH_NOT_EXPR, TREE_TYPE (cond), cond);
  448. if (neg)
  449. {
  450. cond = fold_convert_loc (gimple_location (stmt),
  451. TREE_TYPE (result), cond);
  452. cond = fold_build1_loc (gimple_location (stmt),
  453. NEGATE_EXPR, TREE_TYPE (cond), cond);
  454. }
  455. /* Insert our new statements at the end of conditional block before the
  456. COND_STMT. */
  457. gsi = gsi_for_stmt (stmt);
  458. new_var = force_gimple_operand_gsi (&gsi, cond, true, NULL, true,
  459. GSI_SAME_STMT);
  460. if (!useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (new_var)))
  461. {
  462. source_location locus_0, locus_1;
  463. new_var2 = make_ssa_name (TREE_TYPE (result));
  464. new_stmt = gimple_build_assign (new_var2, CONVERT_EXPR, new_var);
  465. gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
  466. new_var = new_var2;
  467. /* Set the locus to the first argument, unless is doesn't have one. */
  468. locus_0 = gimple_phi_arg_location (phi, 0);
  469. locus_1 = gimple_phi_arg_location (phi, 1);
  470. if (locus_0 == UNKNOWN_LOCATION)
  471. locus_0 = locus_1;
  472. gimple_set_location (new_stmt, locus_0);
  473. }
  474. replace_phi_edge_with_variable (cond_bb, e1, phi, new_var);
  475. /* Note that we optimized this PHI. */
  476. return true;
  477. }
  478. /* Update *ARG which is defined in STMT so that it contains the
  479. computed value if that seems profitable. Return true if the
  480. statement is made dead by that rewriting. */
  481. static bool
  482. jump_function_from_stmt (tree *arg, gimple stmt)
  483. {
  484. enum tree_code code = gimple_assign_rhs_code (stmt);
  485. if (code == ADDR_EXPR)
  486. {
  487. /* For arg = &p->i transform it to p, if possible. */
  488. tree rhs1 = gimple_assign_rhs1 (stmt);
  489. HOST_WIDE_INT offset;
  490. tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs1, 0),
  491. &offset);
  492. if (tem
  493. && TREE_CODE (tem) == MEM_REF
  494. && (mem_ref_offset (tem) + offset) == 0)
  495. {
  496. *arg = TREE_OPERAND (tem, 0);
  497. return true;
  498. }
  499. }
  500. /* TODO: Much like IPA-CP jump-functions we want to handle constant
  501. additions symbolically here, and we'd need to update the comparison
  502. code that compares the arg + cst tuples in our caller. For now the
  503. code above exactly handles the VEC_BASE pattern from vec.h. */
  504. return false;
  505. }
  506. /* RHS is a source argument in a BIT_AND_EXPR which feeds a conditional
  507. of the form SSA_NAME NE 0.
  508. If RHS is fed by a simple EQ_EXPR comparison of two values, see if
  509. the two input values of the EQ_EXPR match arg0 and arg1.
  510. If so update *code and return TRUE. Otherwise return FALSE. */
  511. static bool
  512. rhs_is_fed_for_value_replacement (const_tree arg0, const_tree arg1,
  513. enum tree_code *code, const_tree rhs)
  514. {
  515. /* Obviously if RHS is not an SSA_NAME, we can't look at the defining
  516. statement. */
  517. if (TREE_CODE (rhs) == SSA_NAME)
  518. {
  519. gimple def1 = SSA_NAME_DEF_STMT (rhs);
  520. /* Verify the defining statement has an EQ_EXPR on the RHS. */
  521. if (is_gimple_assign (def1) && gimple_assign_rhs_code (def1) == EQ_EXPR)
  522. {
  523. /* Finally verify the source operands of the EQ_EXPR are equal
  524. to arg0 and arg1. */
  525. tree op0 = gimple_assign_rhs1 (def1);
  526. tree op1 = gimple_assign_rhs2 (def1);
  527. if ((operand_equal_for_phi_arg_p (arg0, op0)
  528. && operand_equal_for_phi_arg_p (arg1, op1))
  529. || (operand_equal_for_phi_arg_p (arg0, op1)
  530. && operand_equal_for_phi_arg_p (arg1, op0)))
  531. {
  532. /* We will perform the optimization. */
  533. *code = gimple_assign_rhs_code (def1);
  534. return true;
  535. }
  536. }
  537. }
  538. return false;
  539. }
  540. /* Return TRUE if arg0/arg1 are equal to the rhs/lhs or lhs/rhs of COND.
  541. Also return TRUE if arg0/arg1 are equal to the source arguments of a
  542. an EQ comparison feeding a BIT_AND_EXPR which feeds COND.
  543. Return FALSE otherwise. */
  544. static bool
  545. operand_equal_for_value_replacement (const_tree arg0, const_tree arg1,
  546. enum tree_code *code, gimple cond)
  547. {
  548. gimple def;
  549. tree lhs = gimple_cond_lhs (cond);
  550. tree rhs = gimple_cond_rhs (cond);
  551. if ((operand_equal_for_phi_arg_p (arg0, lhs)
  552. && operand_equal_for_phi_arg_p (arg1, rhs))
  553. || (operand_equal_for_phi_arg_p (arg1, lhs)
  554. && operand_equal_for_phi_arg_p (arg0, rhs)))
  555. return true;
  556. /* Now handle more complex case where we have an EQ comparison
  557. which feeds a BIT_AND_EXPR which feeds COND.
  558. First verify that COND is of the form SSA_NAME NE 0. */
  559. if (*code != NE_EXPR || !integer_zerop (rhs)
  560. || TREE_CODE (lhs) != SSA_NAME)
  561. return false;
  562. /* Now ensure that SSA_NAME is set by a BIT_AND_EXPR. */
  563. def = SSA_NAME_DEF_STMT (lhs);
  564. if (!is_gimple_assign (def) || gimple_assign_rhs_code (def) != BIT_AND_EXPR)
  565. return false;
  566. /* Now verify arg0/arg1 correspond to the source arguments of an
  567. EQ comparison feeding the BIT_AND_EXPR. */
  568. tree tmp = gimple_assign_rhs1 (def);
  569. if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp))
  570. return true;
  571. tmp = gimple_assign_rhs2 (def);
  572. if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp))
  573. return true;
  574. return false;
  575. }
  576. /* Returns true if ARG is a neutral element for operation CODE
  577. on the RIGHT side. */
  578. static bool
  579. neutral_element_p (tree_code code, tree arg, bool right)
  580. {
  581. switch (code)
  582. {
  583. case PLUS_EXPR:
  584. case BIT_IOR_EXPR:
  585. case BIT_XOR_EXPR:
  586. return integer_zerop (arg);
  587. case LROTATE_EXPR:
  588. case RROTATE_EXPR:
  589. case LSHIFT_EXPR:
  590. case RSHIFT_EXPR:
  591. case MINUS_EXPR:
  592. case POINTER_PLUS_EXPR:
  593. return right && integer_zerop (arg);
  594. case MULT_EXPR:
  595. return integer_onep (arg);
  596. case TRUNC_DIV_EXPR:
  597. case CEIL_DIV_EXPR:
  598. case FLOOR_DIV_EXPR:
  599. case ROUND_DIV_EXPR:
  600. case EXACT_DIV_EXPR:
  601. return right && integer_onep (arg);
  602. case BIT_AND_EXPR:
  603. return integer_all_onesp (arg);
  604. default:
  605. return false;
  606. }
  607. }
  608. /* Returns true if ARG is an absorbing element for operation CODE. */
  609. static bool
  610. absorbing_element_p (tree_code code, tree arg)
  611. {
  612. switch (code)
  613. {
  614. case BIT_IOR_EXPR:
  615. return integer_all_onesp (arg);
  616. case MULT_EXPR:
  617. case BIT_AND_EXPR:
  618. return integer_zerop (arg);
  619. default:
  620. return false;
  621. }
  622. }
  623. /* The function value_replacement does the main work of doing the value
  624. replacement. Return non-zero if the replacement is done. Otherwise return
  625. 0. If we remove the middle basic block, return 2.
  626. BB is the basic block where the replacement is going to be done on. ARG0
  627. is argument 0 from the PHI. Likewise for ARG1. */
  628. static int
  629. value_replacement (basic_block cond_bb, basic_block middle_bb,
  630. edge e0, edge e1, gimple phi,
  631. tree arg0, tree arg1)
  632. {
  633. gimple_stmt_iterator gsi;
  634. gimple cond;
  635. edge true_edge, false_edge;
  636. enum tree_code code;
  637. bool emtpy_or_with_defined_p = true;
  638. /* If the type says honor signed zeros we cannot do this
  639. optimization. */
  640. if (HONOR_SIGNED_ZEROS (arg1))
  641. return 0;
  642. /* If there is a statement in MIDDLE_BB that defines one of the PHI
  643. arguments, then adjust arg0 or arg1. */
  644. gsi = gsi_start_nondebug_after_labels_bb (middle_bb);
  645. while (!gsi_end_p (gsi))
  646. {
  647. gimple stmt = gsi_stmt (gsi);
  648. tree lhs;
  649. gsi_next_nondebug (&gsi);
  650. if (!is_gimple_assign (stmt))
  651. {
  652. emtpy_or_with_defined_p = false;
  653. continue;
  654. }
  655. /* Now try to adjust arg0 or arg1 according to the computation
  656. in the statement. */
  657. lhs = gimple_assign_lhs (stmt);
  658. if (!(lhs == arg0
  659. && jump_function_from_stmt (&arg0, stmt))
  660. || (lhs == arg1
  661. && jump_function_from_stmt (&arg1, stmt)))
  662. emtpy_or_with_defined_p = false;
  663. }
  664. cond = last_stmt (cond_bb);
  665. code = gimple_cond_code (cond);
  666. /* This transformation is only valid for equality comparisons. */
  667. if (code != NE_EXPR && code != EQ_EXPR)
  668. return 0;
  669. /* We need to know which is the true edge and which is the false
  670. edge so that we know if have abs or negative abs. */
  671. extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
  672. /* At this point we know we have a COND_EXPR with two successors.
  673. One successor is BB, the other successor is an empty block which
  674. falls through into BB.
  675. The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
  676. There is a single PHI node at the join point (BB) with two arguments.
  677. We now need to verify that the two arguments in the PHI node match
  678. the two arguments to the equality comparison. */
  679. if (operand_equal_for_value_replacement (arg0, arg1, &code, cond))
  680. {
  681. edge e;
  682. tree arg;
  683. /* For NE_EXPR, we want to build an assignment result = arg where
  684. arg is the PHI argument associated with the true edge. For
  685. EQ_EXPR we want the PHI argument associated with the false edge. */
  686. e = (code == NE_EXPR ? true_edge : false_edge);
  687. /* Unfortunately, E may not reach BB (it may instead have gone to
  688. OTHER_BLOCK). If that is the case, then we want the single outgoing
  689. edge from OTHER_BLOCK which reaches BB and represents the desired
  690. path from COND_BLOCK. */
  691. if (e->dest == middle_bb)
  692. e = single_succ_edge (e->dest);
  693. /* Now we know the incoming edge to BB that has the argument for the
  694. RHS of our new assignment statement. */
  695. if (e0 == e)
  696. arg = arg0;
  697. else
  698. arg = arg1;
  699. /* If the middle basic block was empty or is defining the
  700. PHI arguments and this is a single phi where the args are different
  701. for the edges e0 and e1 then we can remove the middle basic block. */
  702. if (emtpy_or_with_defined_p
  703. && single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)),
  704. e0, e1) == phi)
  705. {
  706. replace_phi_edge_with_variable (cond_bb, e1, phi, arg);
  707. /* Note that we optimized this PHI. */
  708. return 2;
  709. }
  710. else
  711. {
  712. /* Replace the PHI arguments with arg. */
  713. SET_PHI_ARG_DEF (phi, e0->dest_idx, arg);
  714. SET_PHI_ARG_DEF (phi, e1->dest_idx, arg);
  715. if (dump_file && (dump_flags & TDF_DETAILS))
  716. {
  717. fprintf (dump_file, "PHI ");
  718. print_generic_expr (dump_file, gimple_phi_result (phi), 0);
  719. fprintf (dump_file, " reduced for COND_EXPR in block %d to ",
  720. cond_bb->index);
  721. print_generic_expr (dump_file, arg, 0);
  722. fprintf (dump_file, ".\n");
  723. }
  724. return 1;
  725. }
  726. }
  727. /* Now optimize (x != 0) ? x + y : y to just y.
  728. The following condition is too restrictive, there can easily be another
  729. stmt in middle_bb, for instance a CONVERT_EXPR for the second argument. */
  730. gimple assign = last_and_only_stmt (middle_bb);
  731. if (!assign || gimple_code (assign) != GIMPLE_ASSIGN
  732. || gimple_assign_rhs_class (assign) != GIMPLE_BINARY_RHS
  733. || (!INTEGRAL_TYPE_P (TREE_TYPE (arg0))
  734. && !POINTER_TYPE_P (TREE_TYPE (arg0))))
  735. return 0;
  736. /* Punt if there are (degenerate) PHIs in middle_bb, there should not be. */
  737. if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
  738. return 0;
  739. /* Only transform if it removes the condition. */
  740. if (!single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)), e0, e1))
  741. return 0;
  742. /* Size-wise, this is always profitable. */
  743. if (optimize_bb_for_speed_p (cond_bb)
  744. /* The special case is useless if it has a low probability. */
  745. && profile_status_for_fn (cfun) != PROFILE_ABSENT
  746. && EDGE_PRED (middle_bb, 0)->probability < PROB_EVEN
  747. /* If assign is cheap, there is no point avoiding it. */
  748. && estimate_num_insns (assign, &eni_time_weights)
  749. >= 3 * estimate_num_insns (cond, &eni_time_weights))
  750. return 0;
  751. tree lhs = gimple_assign_lhs (assign);
  752. tree rhs1 = gimple_assign_rhs1 (assign);
  753. tree rhs2 = gimple_assign_rhs2 (assign);
  754. enum tree_code code_def = gimple_assign_rhs_code (assign);
  755. tree cond_lhs = gimple_cond_lhs (cond);
  756. tree cond_rhs = gimple_cond_rhs (cond);
  757. if (((code == NE_EXPR && e1 == false_edge)
  758. || (code == EQ_EXPR && e1 == true_edge))
  759. && arg0 == lhs
  760. && ((arg1 == rhs1
  761. && operand_equal_for_phi_arg_p (rhs2, cond_lhs)
  762. && neutral_element_p (code_def, cond_rhs, true))
  763. || (arg1 == rhs2
  764. && operand_equal_for_phi_arg_p (rhs1, cond_lhs)
  765. && neutral_element_p (code_def, cond_rhs, false))
  766. || (operand_equal_for_phi_arg_p (arg1, cond_rhs)
  767. && (operand_equal_for_phi_arg_p (rhs2, cond_lhs)
  768. || operand_equal_for_phi_arg_p (rhs1, cond_lhs))
  769. && absorbing_element_p (code_def, cond_rhs))))
  770. {
  771. gsi = gsi_for_stmt (cond);
  772. if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
  773. {
  774. /* Moving ASSIGN might change VR of lhs, e.g. when moving u_6
  775. def-stmt in:
  776. if (n_5 != 0)
  777. goto <bb 3>;
  778. else
  779. goto <bb 4>;
  780. <bb 3>:
  781. # RANGE [0, 4294967294]
  782. u_6 = n_5 + 4294967295;
  783. <bb 4>:
  784. # u_3 = PHI <u_6(3), 4294967295(2)> */
  785. SSA_NAME_RANGE_INFO (lhs) = NULL;
  786. SSA_NAME_ANTI_RANGE_P (lhs) = 0;
  787. /* If available, we can use VR of phi result at least. */
  788. tree phires = gimple_phi_result (phi);
  789. struct range_info_def *phires_range_info
  790. = SSA_NAME_RANGE_INFO (phires);
  791. if (phires_range_info)
  792. duplicate_ssa_name_range_info (lhs, SSA_NAME_RANGE_TYPE (phires),
  793. phires_range_info);
  794. }
  795. gimple_stmt_iterator gsi_from = gsi_for_stmt (assign);
  796. gsi_move_before (&gsi_from, &gsi);
  797. replace_phi_edge_with_variable (cond_bb, e1, phi, lhs);
  798. return 2;
  799. }
  800. return 0;
  801. }
  802. /* The function minmax_replacement does the main work of doing the minmax
  803. replacement. Return true if the replacement is done. Otherwise return
  804. false.
  805. BB is the basic block where the replacement is going to be done on. ARG0
  806. is argument 0 from the PHI. Likewise for ARG1. */
  807. static bool
  808. minmax_replacement (basic_block cond_bb, basic_block middle_bb,
  809. edge e0, edge e1, gimple phi,
  810. tree arg0, tree arg1)
  811. {
  812. tree result, type;
  813. gcond *cond;
  814. gassign *new_stmt;
  815. edge true_edge, false_edge;
  816. enum tree_code cmp, minmax, ass_code;
  817. tree smaller, larger, arg_true, arg_false;
  818. gimple_stmt_iterator gsi, gsi_from;
  819. type = TREE_TYPE (PHI_RESULT (phi));
  820. /* The optimization may be unsafe due to NaNs. */
  821. if (HONOR_NANS (type))
  822. return false;
  823. cond = as_a <gcond *> (last_stmt (cond_bb));
  824. cmp = gimple_cond_code (cond);
  825. /* This transformation is only valid for order comparisons. Record which
  826. operand is smaller/larger if the result of the comparison is true. */
  827. if (cmp == LT_EXPR || cmp == LE_EXPR)
  828. {
  829. smaller = gimple_cond_lhs (cond);
  830. larger = gimple_cond_rhs (cond);
  831. }
  832. else if (cmp == GT_EXPR || cmp == GE_EXPR)
  833. {
  834. smaller = gimple_cond_rhs (cond);
  835. larger = gimple_cond_lhs (cond);
  836. }
  837. else
  838. return false;
  839. /* We need to know which is the true edge and which is the false
  840. edge so that we know if have abs or negative abs. */
  841. extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
  842. /* Forward the edges over the middle basic block. */
  843. if (true_edge->dest == middle_bb)
  844. true_edge = EDGE_SUCC (true_edge->dest, 0);
  845. if (false_edge->dest == middle_bb)
  846. false_edge = EDGE_SUCC (false_edge->dest, 0);
  847. if (true_edge == e0)
  848. {
  849. gcc_assert (false_edge == e1);
  850. arg_true = arg0;
  851. arg_false = arg1;
  852. }
  853. else
  854. {
  855. gcc_assert (false_edge == e0);
  856. gcc_assert (true_edge == e1);
  857. arg_true = arg1;
  858. arg_false = arg0;
  859. }
  860. if (empty_block_p (middle_bb))
  861. {
  862. if (operand_equal_for_phi_arg_p (arg_true, smaller)
  863. && operand_equal_for_phi_arg_p (arg_false, larger))
  864. {
  865. /* Case
  866. if (smaller < larger)
  867. rslt = smaller;
  868. else
  869. rslt = larger; */
  870. minmax = MIN_EXPR;
  871. }
  872. else if (operand_equal_for_phi_arg_p (arg_false, smaller)
  873. && operand_equal_for_phi_arg_p (arg_true, larger))
  874. minmax = MAX_EXPR;
  875. else
  876. return false;
  877. }
  878. else
  879. {
  880. /* Recognize the following case, assuming d <= u:
  881. if (a <= u)
  882. b = MAX (a, d);
  883. x = PHI <b, u>
  884. This is equivalent to
  885. b = MAX (a, d);
  886. x = MIN (b, u); */
  887. gimple assign = last_and_only_stmt (middle_bb);
  888. tree lhs, op0, op1, bound;
  889. if (!assign
  890. || gimple_code (assign) != GIMPLE_ASSIGN)
  891. return false;
  892. lhs = gimple_assign_lhs (assign);
  893. ass_code = gimple_assign_rhs_code (assign);
  894. if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
  895. return false;
  896. op0 = gimple_assign_rhs1 (assign);
  897. op1 = gimple_assign_rhs2 (assign);
  898. if (true_edge->src == middle_bb)
  899. {
  900. /* We got here if the condition is true, i.e., SMALLER < LARGER. */
  901. if (!operand_equal_for_phi_arg_p (lhs, arg_true))
  902. return false;
  903. if (operand_equal_for_phi_arg_p (arg_false, larger))
  904. {
  905. /* Case
  906. if (smaller < larger)
  907. {
  908. r' = MAX_EXPR (smaller, bound)
  909. }
  910. r = PHI <r', larger> --> to be turned to MIN_EXPR. */
  911. if (ass_code != MAX_EXPR)
  912. return false;
  913. minmax = MIN_EXPR;
  914. if (operand_equal_for_phi_arg_p (op0, smaller))
  915. bound = op1;
  916. else if (operand_equal_for_phi_arg_p (op1, smaller))
  917. bound = op0;
  918. else
  919. return false;
  920. /* We need BOUND <= LARGER. */
  921. if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
  922. bound, larger)))
  923. return false;
  924. }
  925. else if (operand_equal_for_phi_arg_p (arg_false, smaller))
  926. {
  927. /* Case
  928. if (smaller < larger)
  929. {
  930. r' = MIN_EXPR (larger, bound)
  931. }
  932. r = PHI <r', smaller> --> to be turned to MAX_EXPR. */
  933. if (ass_code != MIN_EXPR)
  934. return false;
  935. minmax = MAX_EXPR;
  936. if (operand_equal_for_phi_arg_p (op0, larger))
  937. bound = op1;
  938. else if (operand_equal_for_phi_arg_p (op1, larger))
  939. bound = op0;
  940. else
  941. return false;
  942. /* We need BOUND >= SMALLER. */
  943. if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
  944. bound, smaller)))
  945. return false;
  946. }
  947. else
  948. return false;
  949. }
  950. else
  951. {
  952. /* We got here if the condition is false, i.e., SMALLER > LARGER. */
  953. if (!operand_equal_for_phi_arg_p (lhs, arg_false))
  954. return false;
  955. if (operand_equal_for_phi_arg_p (arg_true, larger))
  956. {
  957. /* Case
  958. if (smaller > larger)
  959. {
  960. r' = MIN_EXPR (smaller, bound)
  961. }
  962. r = PHI <r', larger> --> to be turned to MAX_EXPR. */
  963. if (ass_code != MIN_EXPR)
  964. return false;
  965. minmax = MAX_EXPR;
  966. if (operand_equal_for_phi_arg_p (op0, smaller))
  967. bound = op1;
  968. else if (operand_equal_for_phi_arg_p (op1, smaller))
  969. bound = op0;
  970. else
  971. return false;
  972. /* We need BOUND >= LARGER. */
  973. if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
  974. bound, larger)))
  975. return false;
  976. }
  977. else if (operand_equal_for_phi_arg_p (arg_true, smaller))
  978. {
  979. /* Case
  980. if (smaller > larger)
  981. {
  982. r' = MAX_EXPR (larger, bound)
  983. }
  984. r = PHI <r', smaller> --> to be turned to MIN_EXPR. */
  985. if (ass_code != MAX_EXPR)
  986. return false;
  987. minmax = MIN_EXPR;
  988. if (operand_equal_for_phi_arg_p (op0, larger))
  989. bound = op1;
  990. else if (operand_equal_for_phi_arg_p (op1, larger))
  991. bound = op0;
  992. else
  993. return false;
  994. /* We need BOUND <= SMALLER. */
  995. if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
  996. bound, smaller)))
  997. return false;
  998. }
  999. else
  1000. return false;
  1001. }
  1002. /* Move the statement from the middle block. */
  1003. gsi = gsi_last_bb (cond_bb);
  1004. gsi_from = gsi_last_nondebug_bb (middle_bb);
  1005. gsi_move_before (&gsi_from, &gsi);
  1006. }
  1007. /* Emit the statement to compute min/max. */
  1008. result = duplicate_ssa_name (PHI_RESULT (phi), NULL);
  1009. new_stmt = gimple_build_assign (result, minmax, arg0, arg1);
  1010. gsi = gsi_last_bb (cond_bb);
  1011. gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
  1012. replace_phi_edge_with_variable (cond_bb, e1, phi, result);
  1013. return true;
  1014. }
  1015. /* The function absolute_replacement does the main work of doing the absolute
  1016. replacement. Return true if the replacement is done. Otherwise return
  1017. false.
  1018. bb is the basic block where the replacement is going to be done on. arg0
  1019. is argument 0 from the phi. Likewise for arg1. */
  1020. static bool
  1021. abs_replacement (basic_block cond_bb, basic_block middle_bb,
  1022. edge e0 ATTRIBUTE_UNUSED, edge e1,
  1023. gimple phi, tree arg0, tree arg1)
  1024. {
  1025. tree result;
  1026. gassign *new_stmt;
  1027. gimple cond;
  1028. gimple_stmt_iterator gsi;
  1029. edge true_edge, false_edge;
  1030. gimple assign;
  1031. edge e;
  1032. tree rhs, lhs;
  1033. bool negate;
  1034. enum tree_code cond_code;
  1035. /* If the type says honor signed zeros we cannot do this
  1036. optimization. */
  1037. if (HONOR_SIGNED_ZEROS (arg1))
  1038. return false;
  1039. /* OTHER_BLOCK must have only one executable statement which must have the
  1040. form arg0 = -arg1 or arg1 = -arg0. */
  1041. assign = last_and_only_stmt (middle_bb);
  1042. /* If we did not find the proper negation assignment, then we can not
  1043. optimize. */
  1044. if (assign == NULL)
  1045. return false;
  1046. /* If we got here, then we have found the only executable statement
  1047. in OTHER_BLOCK. If it is anything other than arg = -arg1 or
  1048. arg1 = -arg0, then we can not optimize. */
  1049. if (gimple_code (assign) != GIMPLE_ASSIGN)
  1050. return false;
  1051. lhs = gimple_assign_lhs (assign);
  1052. if (gimple_assign_rhs_code (assign) != NEGATE_EXPR)
  1053. return false;
  1054. rhs = gimple_assign_rhs1 (assign);
  1055. /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
  1056. if (!(lhs == arg0 && rhs == arg1)
  1057. && !(lhs == arg1 && rhs == arg0))
  1058. return false;
  1059. cond = last_stmt (cond_bb);
  1060. result = PHI_RESULT (phi);
  1061. /* Only relationals comparing arg[01] against zero are interesting. */
  1062. cond_code = gimple_cond_code (cond);
  1063. if (cond_code != GT_EXPR && cond_code != GE_EXPR
  1064. && cond_code != LT_EXPR && cond_code != LE_EXPR)
  1065. return false;
  1066. /* Make sure the conditional is arg[01] OP y. */
  1067. if (gimple_cond_lhs (cond) != rhs)
  1068. return false;
  1069. if (FLOAT_TYPE_P (TREE_TYPE (gimple_cond_rhs (cond)))
  1070. ? real_zerop (gimple_cond_rhs (cond))
  1071. : integer_zerop (gimple_cond_rhs (cond)))
  1072. ;
  1073. else
  1074. return false;
  1075. /* We need to know which is the true edge and which is the false
  1076. edge so that we know if have abs or negative abs. */
  1077. extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
  1078. /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
  1079. will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
  1080. the false edge goes to OTHER_BLOCK. */
  1081. if (cond_code == GT_EXPR || cond_code == GE_EXPR)
  1082. e = true_edge;
  1083. else
  1084. e = false_edge;
  1085. if (e->dest == middle_bb)
  1086. negate = true;
  1087. else
  1088. negate = false;
  1089. result = duplicate_ssa_name (result, NULL);
  1090. if (negate)
  1091. lhs = make_ssa_name (TREE_TYPE (result));
  1092. else
  1093. lhs = result;
  1094. /* Build the modify expression with abs expression. */
  1095. new_stmt = gimple_build_assign (lhs, ABS_EXPR, rhs);
  1096. gsi = gsi_last_bb (cond_bb);
  1097. gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
  1098. if (negate)
  1099. {
  1100. /* Get the right GSI. We want to insert after the recently
  1101. added ABS_EXPR statement (which we know is the first statement
  1102. in the block. */
  1103. new_stmt = gimple_build_assign (result, NEGATE_EXPR, lhs);
  1104. gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
  1105. }
  1106. replace_phi_edge_with_variable (cond_bb, e1, phi, result);
  1107. /* Note that we optimized this PHI. */
  1108. return true;
  1109. }
  1110. /* Auxiliary functions to determine the set of memory accesses which
  1111. can't trap because they are preceded by accesses to the same memory
  1112. portion. We do that for MEM_REFs, so we only need to track
  1113. the SSA_NAME of the pointer indirectly referenced. The algorithm
  1114. simply is a walk over all instructions in dominator order. When
  1115. we see an MEM_REF we determine if we've already seen a same
  1116. ref anywhere up to the root of the dominator tree. If we do the
  1117. current access can't trap. If we don't see any dominating access
  1118. the current access might trap, but might also make later accesses
  1119. non-trapping, so we remember it. We need to be careful with loads
  1120. or stores, for instance a load might not trap, while a store would,
  1121. so if we see a dominating read access this doesn't mean that a later
  1122. write access would not trap. Hence we also need to differentiate the
  1123. type of access(es) seen.
  1124. ??? We currently are very conservative and assume that a load might
  1125. trap even if a store doesn't (write-only memory). This probably is
  1126. overly conservative. */
  1127. /* A hash-table of SSA_NAMEs, and in which basic block an MEM_REF
  1128. through it was seen, which would constitute a no-trap region for
  1129. same accesses. */
  1130. struct name_to_bb
  1131. {
  1132. unsigned int ssa_name_ver;
  1133. unsigned int phase;
  1134. bool store;
  1135. HOST_WIDE_INT offset, size;
  1136. basic_block bb;
  1137. };
  1138. /* Hashtable helpers. */
  1139. struct ssa_names_hasher : typed_free_remove <name_to_bb>
  1140. {
  1141. typedef name_to_bb value_type;
  1142. typedef name_to_bb compare_type;
  1143. static inline hashval_t hash (const value_type *);
  1144. static inline bool equal (const value_type *, const compare_type *);
  1145. };
  1146. /* Used for quick clearing of the hash-table when we see calls.
  1147. Hash entries with phase < nt_call_phase are invalid. */
  1148. static unsigned int nt_call_phase;
  1149. /* The hash function. */
  1150. inline hashval_t
  1151. ssa_names_hasher::hash (const value_type *n)
  1152. {
  1153. return n->ssa_name_ver ^ (((hashval_t) n->store) << 31)
  1154. ^ (n->offset << 6) ^ (n->size << 3);
  1155. }
  1156. /* The equality function of *P1 and *P2. */
  1157. inline bool
  1158. ssa_names_hasher::equal (const value_type *n1, const compare_type *n2)
  1159. {
  1160. return n1->ssa_name_ver == n2->ssa_name_ver
  1161. && n1->store == n2->store
  1162. && n1->offset == n2->offset
  1163. && n1->size == n2->size;
  1164. }
  1165. class nontrapping_dom_walker : public dom_walker
  1166. {
  1167. public:
  1168. nontrapping_dom_walker (cdi_direction direction, hash_set<tree> *ps)
  1169. : dom_walker (direction), m_nontrapping (ps), m_seen_ssa_names (128) {}
  1170. virtual void before_dom_children (basic_block);
  1171. virtual void after_dom_children (basic_block);
  1172. private:
  1173. /* We see the expression EXP in basic block BB. If it's an interesting
  1174. expression (an MEM_REF through an SSA_NAME) possibly insert the
  1175. expression into the set NONTRAP or the hash table of seen expressions.
  1176. STORE is true if this expression is on the LHS, otherwise it's on
  1177. the RHS. */
  1178. void add_or_mark_expr (basic_block, tree, bool);
  1179. hash_set<tree> *m_nontrapping;
  1180. /* The hash table for remembering what we've seen. */
  1181. hash_table<ssa_names_hasher> m_seen_ssa_names;
  1182. };
  1183. /* Called by walk_dominator_tree, when entering the block BB. */
  1184. void
  1185. nontrapping_dom_walker::before_dom_children (basic_block bb)
  1186. {
  1187. edge e;
  1188. edge_iterator ei;
  1189. gimple_stmt_iterator gsi;
  1190. /* If we haven't seen all our predecessors, clear the hash-table. */
  1191. FOR_EACH_EDGE (e, ei, bb->preds)
  1192. if ((((size_t)e->src->aux) & 2) == 0)
  1193. {
  1194. nt_call_phase++;
  1195. break;
  1196. }
  1197. /* Mark this BB as being on the path to dominator root and as visited. */
  1198. bb->aux = (void*)(1 | 2);
  1199. /* And walk the statements in order. */
  1200. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  1201. {
  1202. gimple stmt = gsi_stmt (gsi);
  1203. if (is_gimple_call (stmt) && !nonfreeing_call_p (stmt))
  1204. nt_call_phase++;
  1205. else if (gimple_assign_single_p (stmt) && !gimple_has_volatile_ops (stmt))
  1206. {
  1207. add_or_mark_expr (bb, gimple_assign_lhs (stmt), true);
  1208. add_or_mark_expr (bb, gimple_assign_rhs1 (stmt), false);
  1209. }
  1210. }
  1211. }
  1212. /* Called by walk_dominator_tree, when basic block BB is exited. */
  1213. void
  1214. nontrapping_dom_walker::after_dom_children (basic_block bb)
  1215. {
  1216. /* This BB isn't on the path to dominator root anymore. */
  1217. bb->aux = (void*)2;
  1218. }
  1219. /* We see the expression EXP in basic block BB. If it's an interesting
  1220. expression (an MEM_REF through an SSA_NAME) possibly insert the
  1221. expression into the set NONTRAP or the hash table of seen expressions.
  1222. STORE is true if this expression is on the LHS, otherwise it's on
  1223. the RHS. */
  1224. void
  1225. nontrapping_dom_walker::add_or_mark_expr (basic_block bb, tree exp, bool store)
  1226. {
  1227. HOST_WIDE_INT size;
  1228. if (TREE_CODE (exp) == MEM_REF
  1229. && TREE_CODE (TREE_OPERAND (exp, 0)) == SSA_NAME
  1230. && tree_fits_shwi_p (TREE_OPERAND (exp, 1))
  1231. && (size = int_size_in_bytes (TREE_TYPE (exp))) > 0)
  1232. {
  1233. tree name = TREE_OPERAND (exp, 0);
  1234. struct name_to_bb map;
  1235. name_to_bb **slot;
  1236. struct name_to_bb *n2bb;
  1237. basic_block found_bb = 0;
  1238. /* Try to find the last seen MEM_REF through the same
  1239. SSA_NAME, which can trap. */
  1240. map.ssa_name_ver = SSA_NAME_VERSION (name);
  1241. map.phase = 0;
  1242. map.bb = 0;
  1243. map.store = store;
  1244. map.offset = tree_to_shwi (TREE_OPERAND (exp, 1));
  1245. map.size = size;
  1246. slot = m_seen_ssa_names.find_slot (&map, INSERT);
  1247. n2bb = *slot;
  1248. if (n2bb && n2bb->phase >= nt_call_phase)
  1249. found_bb = n2bb->bb;
  1250. /* If we've found a trapping MEM_REF, _and_ it dominates EXP
  1251. (it's in a basic block on the path from us to the dominator root)
  1252. then we can't trap. */
  1253. if (found_bb && (((size_t)found_bb->aux) & 1) == 1)
  1254. {
  1255. m_nontrapping->add (exp);
  1256. }
  1257. else
  1258. {
  1259. /* EXP might trap, so insert it into the hash table. */
  1260. if (n2bb)
  1261. {
  1262. n2bb->phase = nt_call_phase;
  1263. n2bb->bb = bb;
  1264. }
  1265. else
  1266. {
  1267. n2bb = XNEW (struct name_to_bb);
  1268. n2bb->ssa_name_ver = SSA_NAME_VERSION (name);
  1269. n2bb->phase = nt_call_phase;
  1270. n2bb->bb = bb;
  1271. n2bb->store = store;
  1272. n2bb->offset = map.offset;
  1273. n2bb->size = size;
  1274. *slot = n2bb;
  1275. }
  1276. }
  1277. }
  1278. }
  1279. /* This is the entry point of gathering non trapping memory accesses.
  1280. It will do a dominator walk over the whole function, and it will
  1281. make use of the bb->aux pointers. It returns a set of trees
  1282. (the MEM_REFs itself) which can't trap. */
  1283. static hash_set<tree> *
  1284. get_non_trapping (void)
  1285. {
  1286. nt_call_phase = 0;
  1287. hash_set<tree> *nontrap = new hash_set<tree>;
  1288. /* We're going to do a dominator walk, so ensure that we have
  1289. dominance information. */
  1290. calculate_dominance_info (CDI_DOMINATORS);
  1291. nontrapping_dom_walker (CDI_DOMINATORS, nontrap)
  1292. .walk (cfun->cfg->x_entry_block_ptr);
  1293. clear_aux_for_blocks ();
  1294. return nontrap;
  1295. }
  1296. /* Do the main work of conditional store replacement. We already know
  1297. that the recognized pattern looks like so:
  1298. split:
  1299. if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
  1300. MIDDLE_BB:
  1301. something
  1302. fallthrough (edge E0)
  1303. JOIN_BB:
  1304. some more
  1305. We check that MIDDLE_BB contains only one store, that that store
  1306. doesn't trap (not via NOTRAP, but via checking if an access to the same
  1307. memory location dominates us) and that the store has a "simple" RHS. */
  1308. static bool
  1309. cond_store_replacement (basic_block middle_bb, basic_block join_bb,
  1310. edge e0, edge e1, hash_set<tree> *nontrap)
  1311. {
  1312. gimple assign = last_and_only_stmt (middle_bb);
  1313. tree lhs, rhs, name, name2;
  1314. gphi *newphi;
  1315. gassign *new_stmt;
  1316. gimple_stmt_iterator gsi;
  1317. source_location locus;
  1318. /* Check if middle_bb contains of only one store. */
  1319. if (!assign
  1320. || !gimple_assign_single_p (assign)
  1321. || gimple_has_volatile_ops (assign))
  1322. return false;
  1323. locus = gimple_location (assign);
  1324. lhs = gimple_assign_lhs (assign);
  1325. rhs = gimple_assign_rhs1 (assign);
  1326. if (TREE_CODE (lhs) != MEM_REF
  1327. || TREE_CODE (TREE_OPERAND (lhs, 0)) != SSA_NAME
  1328. || !is_gimple_reg_type (TREE_TYPE (lhs)))
  1329. return false;
  1330. /* Prove that we can move the store down. We could also check
  1331. TREE_THIS_NOTRAP here, but in that case we also could move stores,
  1332. whose value is not available readily, which we want to avoid. */
  1333. if (!nontrap->contains (lhs))
  1334. return false;
  1335. /* Now we've checked the constraints, so do the transformation:
  1336. 1) Remove the single store. */
  1337. gsi = gsi_for_stmt (assign);
  1338. unlink_stmt_vdef (assign);
  1339. gsi_remove (&gsi, true);
  1340. release_defs (assign);
  1341. /* 2) Insert a load from the memory of the store to the temporary
  1342. on the edge which did not contain the store. */
  1343. lhs = unshare_expr (lhs);
  1344. name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
  1345. new_stmt = gimple_build_assign (name, lhs);
  1346. gimple_set_location (new_stmt, locus);
  1347. gsi_insert_on_edge (e1, new_stmt);
  1348. /* 3) Create a PHI node at the join block, with one argument
  1349. holding the old RHS, and the other holding the temporary
  1350. where we stored the old memory contents. */
  1351. name2 = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
  1352. newphi = create_phi_node (name2, join_bb);
  1353. add_phi_arg (newphi, rhs, e0, locus);
  1354. add_phi_arg (newphi, name, e1, locus);
  1355. lhs = unshare_expr (lhs);
  1356. new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
  1357. /* 4) Insert that PHI node. */
  1358. gsi = gsi_after_labels (join_bb);
  1359. if (gsi_end_p (gsi))
  1360. {
  1361. gsi = gsi_last_bb (join_bb);
  1362. gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
  1363. }
  1364. else
  1365. gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
  1366. return true;
  1367. }
  1368. /* Do the main work of conditional store replacement. */
  1369. static bool
  1370. cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,
  1371. basic_block join_bb, gimple then_assign,
  1372. gimple else_assign)
  1373. {
  1374. tree lhs_base, lhs, then_rhs, else_rhs, name;
  1375. source_location then_locus, else_locus;
  1376. gimple_stmt_iterator gsi;
  1377. gphi *newphi;
  1378. gassign *new_stmt;
  1379. if (then_assign == NULL
  1380. || !gimple_assign_single_p (then_assign)
  1381. || gimple_clobber_p (then_assign)
  1382. || gimple_has_volatile_ops (then_assign)
  1383. || else_assign == NULL
  1384. || !gimple_assign_single_p (else_assign)
  1385. || gimple_clobber_p (else_assign)
  1386. || gimple_has_volatile_ops (else_assign))
  1387. return false;
  1388. lhs = gimple_assign_lhs (then_assign);
  1389. if (!is_gimple_reg_type (TREE_TYPE (lhs))
  1390. || !operand_equal_p (lhs, gimple_assign_lhs (else_assign), 0))
  1391. return false;
  1392. lhs_base = get_base_address (lhs);
  1393. if (lhs_base == NULL_TREE
  1394. || (!DECL_P (lhs_base) && TREE_CODE (lhs_base) != MEM_REF))
  1395. return false;
  1396. then_rhs = gimple_assign_rhs1 (then_assign);
  1397. else_rhs = gimple_assign_rhs1 (else_assign);
  1398. then_locus = gimple_location (then_assign);
  1399. else_locus = gimple_location (else_assign);
  1400. /* Now we've checked the constraints, so do the transformation:
  1401. 1) Remove the stores. */
  1402. gsi = gsi_for_stmt (then_assign);
  1403. unlink_stmt_vdef (then_assign);
  1404. gsi_remove (&gsi, true);
  1405. release_defs (then_assign);
  1406. gsi = gsi_for_stmt (else_assign);
  1407. unlink_stmt_vdef (else_assign);
  1408. gsi_remove (&gsi, true);
  1409. release_defs (else_assign);
  1410. /* 2) Create a PHI node at the join block, with one argument
  1411. holding the old RHS, and the other holding the temporary
  1412. where we stored the old memory contents. */
  1413. name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
  1414. newphi = create_phi_node (name, join_bb);
  1415. add_phi_arg (newphi, then_rhs, EDGE_SUCC (then_bb, 0), then_locus);
  1416. add_phi_arg (newphi, else_rhs, EDGE_SUCC (else_bb, 0), else_locus);
  1417. new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
  1418. /* 3) Insert that PHI node. */
  1419. gsi = gsi_after_labels (join_bb);
  1420. if (gsi_end_p (gsi))
  1421. {
  1422. gsi = gsi_last_bb (join_bb);
  1423. gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
  1424. }
  1425. else
  1426. gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
  1427. return true;
  1428. }
  1429. /* Conditional store replacement. We already know
  1430. that the recognized pattern looks like so:
  1431. split:
  1432. if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
  1433. THEN_BB:
  1434. ...
  1435. X = Y;
  1436. ...
  1437. goto JOIN_BB;
  1438. ELSE_BB:
  1439. ...
  1440. X = Z;
  1441. ...
  1442. fallthrough (edge E0)
  1443. JOIN_BB:
  1444. some more
  1445. We check that it is safe to sink the store to JOIN_BB by verifying that
  1446. there are no read-after-write or write-after-write dependencies in
  1447. THEN_BB and ELSE_BB. */
  1448. static bool
  1449. cond_if_else_store_replacement (basic_block then_bb, basic_block else_bb,
  1450. basic_block join_bb)
  1451. {
  1452. gimple then_assign = last_and_only_stmt (then_bb);
  1453. gimple else_assign = last_and_only_stmt (else_bb);
  1454. vec<data_reference_p> then_datarefs, else_datarefs;
  1455. vec<ddr_p> then_ddrs, else_ddrs;
  1456. gimple then_store, else_store;
  1457. bool found, ok = false, res;
  1458. struct data_dependence_relation *ddr;
  1459. data_reference_p then_dr, else_dr;
  1460. int i, j;
  1461. tree then_lhs, else_lhs;
  1462. basic_block blocks[3];
  1463. if (MAX_STORES_TO_SINK == 0)
  1464. return false;
  1465. /* Handle the case with single statement in THEN_BB and ELSE_BB. */
  1466. if (then_assign && else_assign)
  1467. return cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
  1468. then_assign, else_assign);
  1469. /* Find data references. */
  1470. then_datarefs.create (1);
  1471. else_datarefs.create (1);
  1472. if ((find_data_references_in_bb (NULL, then_bb, &then_datarefs)
  1473. == chrec_dont_know)
  1474. || !then_datarefs.length ()
  1475. || (find_data_references_in_bb (NULL, else_bb, &else_datarefs)
  1476. == chrec_dont_know)
  1477. || !else_datarefs.length ())
  1478. {
  1479. free_data_refs (then_datarefs);
  1480. free_data_refs (else_datarefs);
  1481. return false;
  1482. }
  1483. /* Find pairs of stores with equal LHS. */
  1484. auto_vec<gimple, 1> then_stores, else_stores;
  1485. FOR_EACH_VEC_ELT (then_datarefs, i, then_dr)
  1486. {
  1487. if (DR_IS_READ (then_dr))
  1488. continue;
  1489. then_store = DR_STMT (then_dr);
  1490. then_lhs = gimple_get_lhs (then_store);
  1491. if (then_lhs == NULL_TREE)
  1492. continue;
  1493. found = false;
  1494. FOR_EACH_VEC_ELT (else_datarefs, j, else_dr)
  1495. {
  1496. if (DR_IS_READ (else_dr))
  1497. continue;
  1498. else_store = DR_STMT (else_dr);
  1499. else_lhs = gimple_get_lhs (else_store);
  1500. if (else_lhs == NULL_TREE)
  1501. continue;
  1502. if (operand_equal_p (then_lhs, else_lhs, 0))
  1503. {
  1504. found = true;
  1505. break;
  1506. }
  1507. }
  1508. if (!found)
  1509. continue;
  1510. then_stores.safe_push (then_store);
  1511. else_stores.safe_push (else_store);
  1512. }
  1513. /* No pairs of stores found. */
  1514. if (!then_stores.length ()
  1515. || then_stores.length () > (unsigned) MAX_STORES_TO_SINK)
  1516. {
  1517. free_data_refs (then_datarefs);
  1518. free_data_refs (else_datarefs);
  1519. return false;
  1520. }
  1521. /* Compute and check data dependencies in both basic blocks. */
  1522. then_ddrs.create (1);
  1523. else_ddrs.create (1);
  1524. if (!compute_all_dependences (then_datarefs, &then_ddrs,
  1525. vNULL, false)
  1526. || !compute_all_dependences (else_datarefs, &else_ddrs,
  1527. vNULL, false))
  1528. {
  1529. free_dependence_relations (then_ddrs);
  1530. free_dependence_relations (else_ddrs);
  1531. free_data_refs (then_datarefs);
  1532. free_data_refs (else_datarefs);
  1533. return false;
  1534. }
  1535. blocks[0] = then_bb;
  1536. blocks[1] = else_bb;
  1537. blocks[2] = join_bb;
  1538. renumber_gimple_stmt_uids_in_blocks (blocks, 3);
  1539. /* Check that there are no read-after-write or write-after-write dependencies
  1540. in THEN_BB. */
  1541. FOR_EACH_VEC_ELT (then_ddrs, i, ddr)
  1542. {
  1543. struct data_reference *dra = DDR_A (ddr);
  1544. struct data_reference *drb = DDR_B (ddr);
  1545. if (DDR_ARE_DEPENDENT (ddr) != chrec_known
  1546. && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
  1547. && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
  1548. || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
  1549. && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
  1550. || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
  1551. {
  1552. free_dependence_relations (then_ddrs);
  1553. free_dependence_relations (else_ddrs);
  1554. free_data_refs (then_datarefs);
  1555. free_data_refs (else_datarefs);
  1556. return false;
  1557. }
  1558. }
  1559. /* Check that there are no read-after-write or write-after-write dependencies
  1560. in ELSE_BB. */
  1561. FOR_EACH_VEC_ELT (else_ddrs, i, ddr)
  1562. {
  1563. struct data_reference *dra = DDR_A (ddr);
  1564. struct data_reference *drb = DDR_B (ddr);
  1565. if (DDR_ARE_DEPENDENT (ddr) != chrec_known
  1566. && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
  1567. && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
  1568. || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
  1569. && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
  1570. || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
  1571. {
  1572. free_dependence_relations (then_ddrs);
  1573. free_dependence_relations (else_ddrs);
  1574. free_data_refs (then_datarefs);
  1575. free_data_refs (else_datarefs);
  1576. return false;
  1577. }
  1578. }
  1579. /* Sink stores with same LHS. */
  1580. FOR_EACH_VEC_ELT (then_stores, i, then_store)
  1581. {
  1582. else_store = else_stores[i];
  1583. res = cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
  1584. then_store, else_store);
  1585. ok = ok || res;
  1586. }
  1587. free_dependence_relations (then_ddrs);
  1588. free_dependence_relations (else_ddrs);
  1589. free_data_refs (then_datarefs);
  1590. free_data_refs (else_datarefs);
  1591. return ok;
  1592. }
  1593. /* Return TRUE if STMT has a VUSE whose corresponding VDEF is in BB. */
  1594. static bool
  1595. local_mem_dependence (gimple stmt, basic_block bb)
  1596. {
  1597. tree vuse = gimple_vuse (stmt);
  1598. gimple def;
  1599. if (!vuse)
  1600. return false;
  1601. def = SSA_NAME_DEF_STMT (vuse);
  1602. return (def && gimple_bb (def) == bb);
  1603. }
  1604. /* Given a "diamond" control-flow pattern where BB0 tests a condition,
  1605. BB1 and BB2 are "then" and "else" blocks dependent on this test,
  1606. and BB3 rejoins control flow following BB1 and BB2, look for
  1607. opportunities to hoist loads as follows. If BB3 contains a PHI of
  1608. two loads, one each occurring in BB1 and BB2, and the loads are
  1609. provably of adjacent fields in the same structure, then move both
  1610. loads into BB0. Of course this can only be done if there are no
  1611. dependencies preventing such motion.
  1612. One of the hoisted loads will always be speculative, so the
  1613. transformation is currently conservative:
  1614. - The fields must be strictly adjacent.
  1615. - The two fields must occupy a single memory block that is
  1616. guaranteed to not cross a page boundary.
  1617. The last is difficult to prove, as such memory blocks should be
  1618. aligned on the minimum of the stack alignment boundary and the
  1619. alignment guaranteed by heap allocation interfaces. Thus we rely
  1620. on a parameter for the alignment value.
  1621. Provided a good value is used for the last case, the first
  1622. restriction could possibly be relaxed. */
  1623. static void
  1624. hoist_adjacent_loads (basic_block bb0, basic_block bb1,
  1625. basic_block bb2, basic_block bb3)
  1626. {
  1627. int param_align = PARAM_VALUE (PARAM_L1_CACHE_LINE_SIZE);
  1628. unsigned param_align_bits = (unsigned) (param_align * BITS_PER_UNIT);
  1629. gphi_iterator gsi;
  1630. /* Walk the phis in bb3 looking for an opportunity. We are looking
  1631. for phis of two SSA names, one each of which is defined in bb1 and
  1632. bb2. */
  1633. for (gsi = gsi_start_phis (bb3); !gsi_end_p (gsi); gsi_next (&gsi))
  1634. {
  1635. gphi *phi_stmt = gsi.phi ();
  1636. gimple def1, def2, defswap;
  1637. tree arg1, arg2, ref1, ref2, field1, field2, fieldswap;
  1638. tree tree_offset1, tree_offset2, tree_size2, next;
  1639. int offset1, offset2, size2;
  1640. unsigned align1;
  1641. gimple_stmt_iterator gsi2;
  1642. basic_block bb_for_def1, bb_for_def2;
  1643. if (gimple_phi_num_args (phi_stmt) != 2
  1644. || virtual_operand_p (gimple_phi_result (phi_stmt)))
  1645. continue;
  1646. arg1 = gimple_phi_arg_def (phi_stmt, 0);
  1647. arg2 = gimple_phi_arg_def (phi_stmt, 1);
  1648. if (TREE_CODE (arg1) != SSA_NAME
  1649. || TREE_CODE (arg2) != SSA_NAME
  1650. || SSA_NAME_IS_DEFAULT_DEF (arg1)
  1651. || SSA_NAME_IS_DEFAULT_DEF (arg2))
  1652. continue;
  1653. def1 = SSA_NAME_DEF_STMT (arg1);
  1654. def2 = SSA_NAME_DEF_STMT (arg2);
  1655. if ((gimple_bb (def1) != bb1 || gimple_bb (def2) != bb2)
  1656. && (gimple_bb (def2) != bb1 || gimple_bb (def1) != bb2))
  1657. continue;
  1658. /* Check the mode of the arguments to be sure a conditional move
  1659. can be generated for it. */
  1660. if (optab_handler (movcc_optab, TYPE_MODE (TREE_TYPE (arg1)))
  1661. == CODE_FOR_nothing)
  1662. continue;
  1663. /* Both statements must be assignments whose RHS is a COMPONENT_REF. */
  1664. if (!gimple_assign_single_p (def1)
  1665. || !gimple_assign_single_p (def2)
  1666. || gimple_has_volatile_ops (def1)
  1667. || gimple_has_volatile_ops (def2))
  1668. continue;
  1669. ref1 = gimple_assign_rhs1 (def1);
  1670. ref2 = gimple_assign_rhs1 (def2);
  1671. if (TREE_CODE (ref1) != COMPONENT_REF
  1672. || TREE_CODE (ref2) != COMPONENT_REF)
  1673. continue;
  1674. /* The zeroth operand of the two component references must be
  1675. identical. It is not sufficient to compare get_base_address of
  1676. the two references, because this could allow for different
  1677. elements of the same array in the two trees. It is not safe to
  1678. assume that the existence of one array element implies the
  1679. existence of a different one. */
  1680. if (!operand_equal_p (TREE_OPERAND (ref1, 0), TREE_OPERAND (ref2, 0), 0))
  1681. continue;
  1682. field1 = TREE_OPERAND (ref1, 1);
  1683. field2 = TREE_OPERAND (ref2, 1);
  1684. /* Check for field adjacency, and ensure field1 comes first. */
  1685. for (next = DECL_CHAIN (field1);
  1686. next && TREE_CODE (next) != FIELD_DECL;
  1687. next = DECL_CHAIN (next))
  1688. ;
  1689. if (next != field2)
  1690. {
  1691. for (next = DECL_CHAIN (field2);
  1692. next && TREE_CODE (next) != FIELD_DECL;
  1693. next = DECL_CHAIN (next))
  1694. ;
  1695. if (next != field1)
  1696. continue;
  1697. fieldswap = field1;
  1698. field1 = field2;
  1699. field2 = fieldswap;
  1700. defswap = def1;
  1701. def1 = def2;
  1702. def2 = defswap;
  1703. }
  1704. bb_for_def1 = gimple_bb (def1);
  1705. bb_for_def2 = gimple_bb (def2);
  1706. /* Check for proper alignment of the first field. */
  1707. tree_offset1 = bit_position (field1);
  1708. tree_offset2 = bit_position (field2);
  1709. tree_size2 = DECL_SIZE (field2);
  1710. if (!tree_fits_uhwi_p (tree_offset1)
  1711. || !tree_fits_uhwi_p (tree_offset2)
  1712. || !tree_fits_uhwi_p (tree_size2))
  1713. continue;
  1714. offset1 = tree_to_uhwi (tree_offset1);
  1715. offset2 = tree_to_uhwi (tree_offset2);
  1716. size2 = tree_to_uhwi (tree_size2);
  1717. align1 = DECL_ALIGN (field1) % param_align_bits;
  1718. if (offset1 % BITS_PER_UNIT != 0)
  1719. continue;
  1720. /* For profitability, the two field references should fit within
  1721. a single cache line. */
  1722. if (align1 + offset2 - offset1 + size2 > param_align_bits)
  1723. continue;
  1724. /* The two expressions cannot be dependent upon vdefs defined
  1725. in bb1/bb2. */
  1726. if (local_mem_dependence (def1, bb_for_def1)
  1727. || local_mem_dependence (def2, bb_for_def2))
  1728. continue;
  1729. /* The conditions are satisfied; hoist the loads from bb1 and bb2 into
  1730. bb0. We hoist the first one first so that a cache miss is handled
  1731. efficiently regardless of hardware cache-fill policy. */
  1732. gsi2 = gsi_for_stmt (def1);
  1733. gsi_move_to_bb_end (&gsi2, bb0);
  1734. gsi2 = gsi_for_stmt (def2);
  1735. gsi_move_to_bb_end (&gsi2, bb0);
  1736. if (dump_file && (dump_flags & TDF_DETAILS))
  1737. {
  1738. fprintf (dump_file,
  1739. "\nHoisting adjacent loads from %d and %d into %d: \n",
  1740. bb_for_def1->index, bb_for_def2->index, bb0->index);
  1741. print_gimple_stmt (dump_file, def1, 0, TDF_VOPS|TDF_MEMSYMS);
  1742. print_gimple_stmt (dump_file, def2, 0, TDF_VOPS|TDF_MEMSYMS);
  1743. }
  1744. }
  1745. }
  1746. /* Determine whether we should attempt to hoist adjacent loads out of
  1747. diamond patterns in pass_phiopt. Always hoist loads if
  1748. -fhoist-adjacent-loads is specified and the target machine has
  1749. both a conditional move instruction and a defined cache line size. */
  1750. static bool
  1751. gate_hoist_loads (void)
  1752. {
  1753. return (flag_hoist_adjacent_loads == 1
  1754. && PARAM_VALUE (PARAM_L1_CACHE_LINE_SIZE)
  1755. && HAVE_conditional_move);
  1756. }
  1757. /* This pass tries to replaces an if-then-else block with an
  1758. assignment. We have four kinds of transformations. Some of these
  1759. transformations are also performed by the ifcvt RTL optimizer.
  1760. Conditional Replacement
  1761. -----------------------
  1762. This transformation, implemented in conditional_replacement,
  1763. replaces
  1764. bb0:
  1765. if (cond) goto bb2; else goto bb1;
  1766. bb1:
  1767. bb2:
  1768. x = PHI <0 (bb1), 1 (bb0), ...>;
  1769. with
  1770. bb0:
  1771. x' = cond;
  1772. goto bb2;
  1773. bb2:
  1774. x = PHI <x' (bb0), ...>;
  1775. We remove bb1 as it becomes unreachable. This occurs often due to
  1776. gimplification of conditionals.
  1777. Value Replacement
  1778. -----------------
  1779. This transformation, implemented in value_replacement, replaces
  1780. bb0:
  1781. if (a != b) goto bb2; else goto bb1;
  1782. bb1:
  1783. bb2:
  1784. x = PHI <a (bb1), b (bb0), ...>;
  1785. with
  1786. bb0:
  1787. bb2:
  1788. x = PHI <b (bb0), ...>;
  1789. This opportunity can sometimes occur as a result of other
  1790. optimizations.
  1791. Another case caught by value replacement looks like this:
  1792. bb0:
  1793. t1 = a == CONST;
  1794. t2 = b > c;
  1795. t3 = t1 & t2;
  1796. if (t3 != 0) goto bb1; else goto bb2;
  1797. bb1:
  1798. bb2:
  1799. x = PHI (CONST, a)
  1800. Gets replaced with:
  1801. bb0:
  1802. bb2:
  1803. t1 = a == CONST;
  1804. t2 = b > c;
  1805. t3 = t1 & t2;
  1806. x = a;
  1807. ABS Replacement
  1808. ---------------
  1809. This transformation, implemented in abs_replacement, replaces
  1810. bb0:
  1811. if (a >= 0) goto bb2; else goto bb1;
  1812. bb1:
  1813. x = -a;
  1814. bb2:
  1815. x = PHI <x (bb1), a (bb0), ...>;
  1816. with
  1817. bb0:
  1818. x' = ABS_EXPR< a >;
  1819. bb2:
  1820. x = PHI <x' (bb0), ...>;
  1821. MIN/MAX Replacement
  1822. -------------------
  1823. This transformation, minmax_replacement replaces
  1824. bb0:
  1825. if (a <= b) goto bb2; else goto bb1;
  1826. bb1:
  1827. bb2:
  1828. x = PHI <b (bb1), a (bb0), ...>;
  1829. with
  1830. bb0:
  1831. x' = MIN_EXPR (a, b)
  1832. bb2:
  1833. x = PHI <x' (bb0), ...>;
  1834. A similar transformation is done for MAX_EXPR.
  1835. This pass also performs a fifth transformation of a slightly different
  1836. flavor.
  1837. Adjacent Load Hoisting
  1838. ----------------------
  1839. This transformation replaces
  1840. bb0:
  1841. if (...) goto bb2; else goto bb1;
  1842. bb1:
  1843. x1 = (<expr>).field1;
  1844. goto bb3;
  1845. bb2:
  1846. x2 = (<expr>).field2;
  1847. bb3:
  1848. # x = PHI <x1, x2>;
  1849. with
  1850. bb0:
  1851. x1 = (<expr>).field1;
  1852. x2 = (<expr>).field2;
  1853. if (...) goto bb2; else goto bb1;
  1854. bb1:
  1855. goto bb3;
  1856. bb2:
  1857. bb3:
  1858. # x = PHI <x1, x2>;
  1859. The purpose of this transformation is to enable generation of conditional
  1860. move instructions such as Intel CMOVE or PowerPC ISEL. Because one of
  1861. the loads is speculative, the transformation is restricted to very
  1862. specific cases to avoid introducing a page fault. We are looking for
  1863. the common idiom:
  1864. if (...)
  1865. x = y->left;
  1866. else
  1867. x = y->right;
  1868. where left and right are typically adjacent pointers in a tree structure. */
  1869. namespace {
  1870. const pass_data pass_data_phiopt =
  1871. {
  1872. GIMPLE_PASS, /* type */
  1873. "phiopt", /* name */
  1874. OPTGROUP_NONE, /* optinfo_flags */
  1875. TV_TREE_PHIOPT, /* tv_id */
  1876. ( PROP_cfg | PROP_ssa ), /* properties_required */
  1877. 0, /* properties_provided */
  1878. 0, /* properties_destroyed */
  1879. 0, /* todo_flags_start */
  1880. 0, /* todo_flags_finish */
  1881. };
  1882. class pass_phiopt : public gimple_opt_pass
  1883. {
  1884. public:
  1885. pass_phiopt (gcc::context *ctxt)
  1886. : gimple_opt_pass (pass_data_phiopt, ctxt)
  1887. {}
  1888. /* opt_pass methods: */
  1889. opt_pass * clone () { return new pass_phiopt (m_ctxt); }
  1890. virtual bool gate (function *) { return flag_ssa_phiopt; }
  1891. virtual unsigned int execute (function *)
  1892. {
  1893. return tree_ssa_phiopt_worker (false, gate_hoist_loads ());
  1894. }
  1895. }; // class pass_phiopt
  1896. } // anon namespace
  1897. gimple_opt_pass *
  1898. make_pass_phiopt (gcc::context *ctxt)
  1899. {
  1900. return new pass_phiopt (ctxt);
  1901. }
  1902. namespace {
  1903. const pass_data pass_data_cselim =
  1904. {
  1905. GIMPLE_PASS, /* type */
  1906. "cselim", /* name */
  1907. OPTGROUP_NONE, /* optinfo_flags */
  1908. TV_TREE_PHIOPT, /* tv_id */
  1909. ( PROP_cfg | PROP_ssa ), /* properties_required */
  1910. 0, /* properties_provided */
  1911. 0, /* properties_destroyed */
  1912. 0, /* todo_flags_start */
  1913. 0, /* todo_flags_finish */
  1914. };
  1915. class pass_cselim : public gimple_opt_pass
  1916. {
  1917. public:
  1918. pass_cselim (gcc::context *ctxt)
  1919. : gimple_opt_pass (pass_data_cselim, ctxt)
  1920. {}
  1921. /* opt_pass methods: */
  1922. virtual bool gate (function *) { return flag_tree_cselim; }
  1923. virtual unsigned int execute (function *) { return tree_ssa_cs_elim (); }
  1924. }; // class pass_cselim
  1925. } // anon namespace
  1926. gimple_opt_pass *
  1927. make_pass_cselim (gcc::context *ctxt)
  1928. {
  1929. return new pass_cselim (ctxt);
  1930. }