tree-ssa-threadedge.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. /* SSA Jump Threading
  2. Copyright (C) 2005-2015 Free Software Foundation, Inc.
  3. Contributed by Jeff Law <law@redhat.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.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 "flags.h"
  32. #include "tm_p.h"
  33. #include "predict.h"
  34. #include "hard-reg-set.h"
  35. #include "input.h"
  36. #include "function.h"
  37. #include "dominance.h"
  38. #include "basic-block.h"
  39. #include "cfgloop.h"
  40. #include "timevar.h"
  41. #include "dumpfile.h"
  42. #include "tree-ssa-alias.h"
  43. #include "internal-fn.h"
  44. #include "gimple-expr.h"
  45. #include "is-a.h"
  46. #include "gimple.h"
  47. #include "gimple-iterator.h"
  48. #include "gimple-ssa.h"
  49. #include "tree-cfg.h"
  50. #include "tree-phinodes.h"
  51. #include "ssa-iterators.h"
  52. #include "stringpool.h"
  53. #include "tree-ssanames.h"
  54. #include "tree-ssa-propagate.h"
  55. #include "tree-ssa-threadupdate.h"
  56. #include "langhooks.h"
  57. #include "params.h"
  58. #include "tree-ssa-threadedge.h"
  59. #include "tree-ssa-loop.h"
  60. #include "builtins.h"
  61. #include "cfg.h"
  62. #include "cfganal.h"
  63. /* To avoid code explosion due to jump threading, we limit the
  64. number of statements we are going to copy. This variable
  65. holds the number of statements currently seen that we'll have
  66. to copy as part of the jump threading process. */
  67. static int stmt_count;
  68. /* Array to record value-handles per SSA_NAME. */
  69. vec<tree> ssa_name_values;
  70. /* Set the value for the SSA name NAME to VALUE. */
  71. void
  72. set_ssa_name_value (tree name, tree value)
  73. {
  74. if (SSA_NAME_VERSION (name) >= ssa_name_values.length ())
  75. ssa_name_values.safe_grow_cleared (SSA_NAME_VERSION (name) + 1);
  76. if (value && TREE_OVERFLOW_P (value))
  77. value = drop_tree_overflow (value);
  78. ssa_name_values[SSA_NAME_VERSION (name)] = value;
  79. }
  80. /* Initialize the per SSA_NAME value-handles array. Returns it. */
  81. void
  82. threadedge_initialize_values (void)
  83. {
  84. gcc_assert (!ssa_name_values.exists ());
  85. ssa_name_values.create (num_ssa_names);
  86. }
  87. /* Free the per SSA_NAME value-handle array. */
  88. void
  89. threadedge_finalize_values (void)
  90. {
  91. ssa_name_values.release ();
  92. }
  93. /* Return TRUE if we may be able to thread an incoming edge into
  94. BB to an outgoing edge from BB. Return FALSE otherwise. */
  95. bool
  96. potentially_threadable_block (basic_block bb)
  97. {
  98. gimple_stmt_iterator gsi;
  99. /* Special case. We can get blocks that are forwarders, but are
  100. not optimized away because they forward from outside a loop
  101. to the loop header. We want to thread through them as we can
  102. sometimes thread to the loop exit, which is obviously profitable.
  103. the interesting case here is when the block has PHIs. */
  104. if (gsi_end_p (gsi_start_nondebug_bb (bb))
  105. && !gsi_end_p (gsi_start_phis (bb)))
  106. return true;
  107. /* If BB has a single successor or a single predecessor, then
  108. there is no threading opportunity. */
  109. if (single_succ_p (bb) || single_pred_p (bb))
  110. return false;
  111. /* If BB does not end with a conditional, switch or computed goto,
  112. then there is no threading opportunity. */
  113. gsi = gsi_last_bb (bb);
  114. if (gsi_end_p (gsi)
  115. || ! gsi_stmt (gsi)
  116. || (gimple_code (gsi_stmt (gsi)) != GIMPLE_COND
  117. && gimple_code (gsi_stmt (gsi)) != GIMPLE_GOTO
  118. && gimple_code (gsi_stmt (gsi)) != GIMPLE_SWITCH))
  119. return false;
  120. return true;
  121. }
  122. /* Return the LHS of any ASSERT_EXPR where OP appears as the first
  123. argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
  124. BB. If no such ASSERT_EXPR is found, return OP. */
  125. static tree
  126. lhs_of_dominating_assert (tree op, basic_block bb, gimple stmt)
  127. {
  128. imm_use_iterator imm_iter;
  129. gimple use_stmt;
  130. use_operand_p use_p;
  131. FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
  132. {
  133. use_stmt = USE_STMT (use_p);
  134. if (use_stmt != stmt
  135. && gimple_assign_single_p (use_stmt)
  136. && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
  137. && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
  138. && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
  139. {
  140. return gimple_assign_lhs (use_stmt);
  141. }
  142. }
  143. return op;
  144. }
  145. /* We record temporary equivalences created by PHI nodes or
  146. statements within the target block. Doing so allows us to
  147. identify more jump threading opportunities, even in blocks
  148. with side effects.
  149. We keep track of those temporary equivalences in a stack
  150. structure so that we can unwind them when we're done processing
  151. a particular edge. This routine handles unwinding the data
  152. structures. */
  153. static void
  154. remove_temporary_equivalences (vec<tree> *stack)
  155. {
  156. while (stack->length () > 0)
  157. {
  158. tree prev_value, dest;
  159. dest = stack->pop ();
  160. /* A NULL value indicates we should stop unwinding, otherwise
  161. pop off the next entry as they're recorded in pairs. */
  162. if (dest == NULL)
  163. break;
  164. prev_value = stack->pop ();
  165. set_ssa_name_value (dest, prev_value);
  166. }
  167. }
  168. /* Record a temporary equivalence, saving enough information so that
  169. we can restore the state of recorded equivalences when we're
  170. done processing the current edge. */
  171. static void
  172. record_temporary_equivalence (tree x, tree y, vec<tree> *stack)
  173. {
  174. tree prev_x = SSA_NAME_VALUE (x);
  175. /* Y may be NULL if we are invalidating entries in the table. */
  176. if (y && TREE_CODE (y) == SSA_NAME)
  177. {
  178. tree tmp = SSA_NAME_VALUE (y);
  179. y = tmp ? tmp : y;
  180. }
  181. set_ssa_name_value (x, y);
  182. stack->reserve (2);
  183. stack->quick_push (prev_x);
  184. stack->quick_push (x);
  185. }
  186. /* Record temporary equivalences created by PHIs at the target of the
  187. edge E. Record unwind information for the equivalences onto STACK.
  188. If a PHI which prevents threading is encountered, then return FALSE
  189. indicating we should not thread this edge, else return TRUE.
  190. If SRC_MAP/DST_MAP exist, then mark the source and destination SSA_NAMEs
  191. of any equivalences recorded. We use this to make invalidation after
  192. traversing back edges less painful. */
  193. static bool
  194. record_temporary_equivalences_from_phis (edge e, vec<tree> *stack)
  195. {
  196. gphi_iterator gsi;
  197. /* Each PHI creates a temporary equivalence, record them.
  198. These are context sensitive equivalences and will be removed
  199. later. */
  200. for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
  201. {
  202. gphi *phi = gsi.phi ();
  203. tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
  204. tree dst = gimple_phi_result (phi);
  205. /* If the desired argument is not the same as this PHI's result
  206. and it is set by a PHI in E->dest, then we can not thread
  207. through E->dest. */
  208. if (src != dst
  209. && TREE_CODE (src) == SSA_NAME
  210. && gimple_code (SSA_NAME_DEF_STMT (src)) == GIMPLE_PHI
  211. && gimple_bb (SSA_NAME_DEF_STMT (src)) == e->dest)
  212. return false;
  213. /* We consider any non-virtual PHI as a statement since it
  214. count result in a constant assignment or copy operation. */
  215. if (!virtual_operand_p (dst))
  216. stmt_count++;
  217. record_temporary_equivalence (dst, src, stack);
  218. }
  219. return true;
  220. }
  221. /* Fold the RHS of an assignment statement and return it as a tree.
  222. May return NULL_TREE if no simplification is possible. */
  223. static tree
  224. fold_assignment_stmt (gimple stmt)
  225. {
  226. enum tree_code subcode = gimple_assign_rhs_code (stmt);
  227. switch (get_gimple_rhs_class (subcode))
  228. {
  229. case GIMPLE_SINGLE_RHS:
  230. return fold (gimple_assign_rhs1 (stmt));
  231. case GIMPLE_UNARY_RHS:
  232. {
  233. tree lhs = gimple_assign_lhs (stmt);
  234. tree op0 = gimple_assign_rhs1 (stmt);
  235. return fold_unary (subcode, TREE_TYPE (lhs), op0);
  236. }
  237. case GIMPLE_BINARY_RHS:
  238. {
  239. tree lhs = gimple_assign_lhs (stmt);
  240. tree op0 = gimple_assign_rhs1 (stmt);
  241. tree op1 = gimple_assign_rhs2 (stmt);
  242. return fold_binary (subcode, TREE_TYPE (lhs), op0, op1);
  243. }
  244. case GIMPLE_TERNARY_RHS:
  245. {
  246. tree lhs = gimple_assign_lhs (stmt);
  247. tree op0 = gimple_assign_rhs1 (stmt);
  248. tree op1 = gimple_assign_rhs2 (stmt);
  249. tree op2 = gimple_assign_rhs3 (stmt);
  250. /* Sadly, we have to handle conditional assignments specially
  251. here, because fold expects all the operands of an expression
  252. to be folded before the expression itself is folded, but we
  253. can't just substitute the folded condition here. */
  254. if (gimple_assign_rhs_code (stmt) == COND_EXPR)
  255. op0 = fold (op0);
  256. return fold_ternary (subcode, TREE_TYPE (lhs), op0, op1, op2);
  257. }
  258. default:
  259. gcc_unreachable ();
  260. }
  261. }
  262. /* A new value has been assigned to LHS. If necessary, invalidate any
  263. equivalences that are no longer valid. This includes invaliding
  264. LHS and any objects that are currently equivalent to LHS.
  265. Finding the objects that are currently marked as equivalent to LHS
  266. is a bit tricky. We could walk the ssa names and see if any have
  267. SSA_NAME_VALUE that is the same as LHS. That's expensive.
  268. However, it's far more efficient to look at the unwinding stack as
  269. that will have all context sensitive equivalences which are the only
  270. ones that we really have to worry about here. */
  271. static void
  272. invalidate_equivalences (tree lhs, vec<tree> *stack)
  273. {
  274. /* The stack is an unwinding stack. If the current element is NULL
  275. then it's a "stop unwinding" marker. Else the current marker is
  276. the SSA_NAME with an equivalence and the prior entry in the stack
  277. is what the current element is equivalent to. */
  278. for (int i = stack->length() - 1; i >= 0; i--)
  279. {
  280. /* Ignore the stop unwinding markers. */
  281. if ((*stack)[i] == NULL)
  282. continue;
  283. /* We want to check the current value of stack[i] to see if
  284. it matches LHS. If so, then invalidate. */
  285. if (SSA_NAME_VALUE ((*stack)[i]) == lhs)
  286. record_temporary_equivalence ((*stack)[i], NULL_TREE, stack);
  287. /* Remember, we're dealing with two elements in this case. */
  288. i--;
  289. }
  290. /* And invalidate any known value for LHS itself. */
  291. if (SSA_NAME_VALUE (lhs))
  292. record_temporary_equivalence (lhs, NULL_TREE, stack);
  293. }
  294. /* Try to simplify each statement in E->dest, ultimately leading to
  295. a simplification of the COND_EXPR at the end of E->dest.
  296. Record unwind information for temporary equivalences onto STACK.
  297. Use SIMPLIFY (a pointer to a callback function) to further simplify
  298. statements using pass specific information.
  299. We might consider marking just those statements which ultimately
  300. feed the COND_EXPR. It's not clear if the overhead of bookkeeping
  301. would be recovered by trying to simplify fewer statements.
  302. If we are able to simplify a statement into the form
  303. SSA_NAME = (SSA_NAME | gimple invariant), then we can record
  304. a context sensitive equivalence which may help us simplify
  305. later statements in E->dest. */
  306. static gimple
  307. record_temporary_equivalences_from_stmts_at_dest (edge e,
  308. vec<tree> *stack,
  309. tree (*simplify) (gimple,
  310. gimple),
  311. bool backedge_seen)
  312. {
  313. gimple stmt = NULL;
  314. gimple_stmt_iterator gsi;
  315. int max_stmt_count;
  316. max_stmt_count = PARAM_VALUE (PARAM_MAX_JUMP_THREAD_DUPLICATION_STMTS);
  317. /* Walk through each statement in the block recording equivalences
  318. we discover. Note any equivalences we discover are context
  319. sensitive (ie, are dependent on traversing E) and must be unwound
  320. when we're finished processing E. */
  321. for (gsi = gsi_start_bb (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
  322. {
  323. tree cached_lhs = NULL;
  324. stmt = gsi_stmt (gsi);
  325. /* Ignore empty statements and labels. */
  326. if (gimple_code (stmt) == GIMPLE_NOP
  327. || gimple_code (stmt) == GIMPLE_LABEL
  328. || is_gimple_debug (stmt))
  329. continue;
  330. /* If the statement has volatile operands, then we assume we
  331. can not thread through this block. This is overly
  332. conservative in some ways. */
  333. if (gimple_code (stmt) == GIMPLE_ASM
  334. && gimple_asm_volatile_p (as_a <gasm *> (stmt)))
  335. return NULL;
  336. /* If duplicating this block is going to cause too much code
  337. expansion, then do not thread through this block. */
  338. stmt_count++;
  339. if (stmt_count > max_stmt_count)
  340. return NULL;
  341. /* If this is not a statement that sets an SSA_NAME to a new
  342. value, then do not try to simplify this statement as it will
  343. not simplify in any way that is helpful for jump threading. */
  344. if ((gimple_code (stmt) != GIMPLE_ASSIGN
  345. || TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
  346. && (gimple_code (stmt) != GIMPLE_CALL
  347. || gimple_call_lhs (stmt) == NULL_TREE
  348. || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME))
  349. {
  350. /* STMT might still have DEFS and we need to invalidate any known
  351. equivalences for them.
  352. Consider if STMT is a GIMPLE_ASM with one or more outputs that
  353. feeds a conditional inside a loop. We might derive an equivalence
  354. due to the conditional. */
  355. tree op;
  356. ssa_op_iter iter;
  357. if (backedge_seen)
  358. FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
  359. invalidate_equivalences (op, stack);
  360. continue;
  361. }
  362. /* The result of __builtin_object_size depends on all the arguments
  363. of a phi node. Temporarily using only one edge produces invalid
  364. results. For example
  365. if (x < 6)
  366. goto l;
  367. else
  368. goto l;
  369. l:
  370. r = PHI <&w[2].a[1](2), &a.a[6](3)>
  371. __builtin_object_size (r, 0)
  372. The result of __builtin_object_size is defined to be the maximum of
  373. remaining bytes. If we use only one edge on the phi, the result will
  374. change to be the remaining bytes for the corresponding phi argument.
  375. Similarly for __builtin_constant_p:
  376. r = PHI <1(2), 2(3)>
  377. __builtin_constant_p (r)
  378. Both PHI arguments are constant, but x ? 1 : 2 is still not
  379. constant. */
  380. if (is_gimple_call (stmt))
  381. {
  382. tree fndecl = gimple_call_fndecl (stmt);
  383. if (fndecl
  384. && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_OBJECT_SIZE
  385. || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P))
  386. {
  387. if (backedge_seen)
  388. {
  389. tree lhs = gimple_get_lhs (stmt);
  390. invalidate_equivalences (lhs, stack);
  391. }
  392. continue;
  393. }
  394. }
  395. /* At this point we have a statement which assigns an RHS to an
  396. SSA_VAR on the LHS. We want to try and simplify this statement
  397. to expose more context sensitive equivalences which in turn may
  398. allow us to simplify the condition at the end of the loop.
  399. Handle simple copy operations as well as implied copies from
  400. ASSERT_EXPRs. */
  401. if (gimple_assign_single_p (stmt)
  402. && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
  403. cached_lhs = gimple_assign_rhs1 (stmt);
  404. else if (gimple_assign_single_p (stmt)
  405. && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
  406. cached_lhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
  407. else
  408. {
  409. /* A statement that is not a trivial copy or ASSERT_EXPR.
  410. We're going to temporarily copy propagate the operands
  411. and see if that allows us to simplify this statement. */
  412. tree *copy;
  413. ssa_op_iter iter;
  414. use_operand_p use_p;
  415. unsigned int num, i = 0;
  416. num = NUM_SSA_OPERANDS (stmt, (SSA_OP_USE | SSA_OP_VUSE));
  417. copy = XCNEWVEC (tree, num);
  418. /* Make a copy of the uses & vuses into USES_COPY, then cprop into
  419. the operands. */
  420. FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
  421. {
  422. tree tmp = NULL;
  423. tree use = USE_FROM_PTR (use_p);
  424. copy[i++] = use;
  425. if (TREE_CODE (use) == SSA_NAME)
  426. tmp = SSA_NAME_VALUE (use);
  427. if (tmp)
  428. SET_USE (use_p, tmp);
  429. }
  430. /* Try to fold/lookup the new expression. Inserting the
  431. expression into the hash table is unlikely to help. */
  432. if (is_gimple_call (stmt))
  433. cached_lhs = fold_call_stmt (as_a <gcall *> (stmt), false);
  434. else
  435. cached_lhs = fold_assignment_stmt (stmt);
  436. if (!cached_lhs
  437. || (TREE_CODE (cached_lhs) != SSA_NAME
  438. && !is_gimple_min_invariant (cached_lhs)))
  439. cached_lhs = (*simplify) (stmt, stmt);
  440. /* Restore the statement's original uses/defs. */
  441. i = 0;
  442. FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
  443. SET_USE (use_p, copy[i++]);
  444. free (copy);
  445. }
  446. /* Record the context sensitive equivalence if we were able
  447. to simplify this statement.
  448. If we have traversed a backedge at some point during threading,
  449. then always enter something here. Either a real equivalence,
  450. or a NULL_TREE equivalence which is effectively invalidation of
  451. prior equivalences. */
  452. if (cached_lhs
  453. && (TREE_CODE (cached_lhs) == SSA_NAME
  454. || is_gimple_min_invariant (cached_lhs)))
  455. record_temporary_equivalence (gimple_get_lhs (stmt), cached_lhs, stack);
  456. else if (backedge_seen)
  457. invalidate_equivalences (gimple_get_lhs (stmt), stack);
  458. }
  459. return stmt;
  460. }
  461. /* Once we have passed a backedge in the CFG when threading, we do not want to
  462. utilize edge equivalences for simplification purpose. They are no longer
  463. necessarily valid. We use this callback rather than the ones provided by
  464. DOM/VRP to achieve that effect. */
  465. static tree
  466. dummy_simplify (gimple stmt1 ATTRIBUTE_UNUSED, gimple stmt2 ATTRIBUTE_UNUSED)
  467. {
  468. return NULL_TREE;
  469. }
  470. /* Simplify the control statement at the end of the block E->dest.
  471. To avoid allocating memory unnecessarily, a scratch GIMPLE_COND
  472. is available to use/clobber in DUMMY_COND.
  473. Use SIMPLIFY (a pointer to a callback function) to further simplify
  474. a condition using pass specific information.
  475. Return the simplified condition or NULL if simplification could
  476. not be performed. */
  477. static tree
  478. simplify_control_stmt_condition (edge e,
  479. gimple stmt,
  480. gcond *dummy_cond,
  481. tree (*simplify) (gimple, gimple),
  482. bool handle_dominating_asserts)
  483. {
  484. tree cond, cached_lhs;
  485. enum gimple_code code = gimple_code (stmt);
  486. /* For comparisons, we have to update both operands, then try
  487. to simplify the comparison. */
  488. if (code == GIMPLE_COND)
  489. {
  490. tree op0, op1;
  491. enum tree_code cond_code;
  492. op0 = gimple_cond_lhs (stmt);
  493. op1 = gimple_cond_rhs (stmt);
  494. cond_code = gimple_cond_code (stmt);
  495. /* Get the current value of both operands. */
  496. if (TREE_CODE (op0) == SSA_NAME)
  497. {
  498. for (int i = 0; i < 2; i++)
  499. {
  500. if (TREE_CODE (op0) == SSA_NAME
  501. && SSA_NAME_VALUE (op0))
  502. op0 = SSA_NAME_VALUE (op0);
  503. else
  504. break;
  505. }
  506. }
  507. if (TREE_CODE (op1) == SSA_NAME)
  508. {
  509. for (int i = 0; i < 2; i++)
  510. {
  511. if (TREE_CODE (op1) == SSA_NAME
  512. && SSA_NAME_VALUE (op1))
  513. op1 = SSA_NAME_VALUE (op1);
  514. else
  515. break;
  516. }
  517. }
  518. if (handle_dominating_asserts)
  519. {
  520. /* Now see if the operand was consumed by an ASSERT_EXPR
  521. which dominates E->src. If so, we want to replace the
  522. operand with the LHS of the ASSERT_EXPR. */
  523. if (TREE_CODE (op0) == SSA_NAME)
  524. op0 = lhs_of_dominating_assert (op0, e->src, stmt);
  525. if (TREE_CODE (op1) == SSA_NAME)
  526. op1 = lhs_of_dominating_assert (op1, e->src, stmt);
  527. }
  528. /* We may need to canonicalize the comparison. For
  529. example, op0 might be a constant while op1 is an
  530. SSA_NAME. Failure to canonicalize will cause us to
  531. miss threading opportunities. */
  532. if (tree_swap_operands_p (op0, op1, false))
  533. {
  534. tree tmp;
  535. cond_code = swap_tree_comparison (cond_code);
  536. tmp = op0;
  537. op0 = op1;
  538. op1 = tmp;
  539. }
  540. /* Stuff the operator and operands into our dummy conditional
  541. expression. */
  542. gimple_cond_set_code (dummy_cond, cond_code);
  543. gimple_cond_set_lhs (dummy_cond, op0);
  544. gimple_cond_set_rhs (dummy_cond, op1);
  545. /* We absolutely do not care about any type conversions
  546. we only care about a zero/nonzero value. */
  547. fold_defer_overflow_warnings ();
  548. cached_lhs = fold_binary (cond_code, boolean_type_node, op0, op1);
  549. if (cached_lhs)
  550. while (CONVERT_EXPR_P (cached_lhs))
  551. cached_lhs = TREE_OPERAND (cached_lhs, 0);
  552. fold_undefer_overflow_warnings ((cached_lhs
  553. && is_gimple_min_invariant (cached_lhs)),
  554. stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
  555. /* If we have not simplified the condition down to an invariant,
  556. then use the pass specific callback to simplify the condition. */
  557. if (!cached_lhs
  558. || !is_gimple_min_invariant (cached_lhs))
  559. cached_lhs = (*simplify) (dummy_cond, stmt);
  560. return cached_lhs;
  561. }
  562. if (code == GIMPLE_SWITCH)
  563. cond = gimple_switch_index (as_a <gswitch *> (stmt));
  564. else if (code == GIMPLE_GOTO)
  565. cond = gimple_goto_dest (stmt);
  566. else
  567. gcc_unreachable ();
  568. /* We can have conditionals which just test the state of a variable
  569. rather than use a relational operator. These are simpler to handle. */
  570. if (TREE_CODE (cond) == SSA_NAME)
  571. {
  572. tree original_lhs = cond;
  573. cached_lhs = cond;
  574. /* Get the variable's current value from the equivalence chains.
  575. It is possible to get loops in the SSA_NAME_VALUE chains
  576. (consider threading the backedge of a loop where we have
  577. a loop invariant SSA_NAME used in the condition. */
  578. if (cached_lhs)
  579. {
  580. for (int i = 0; i < 2; i++)
  581. {
  582. if (TREE_CODE (cached_lhs) == SSA_NAME
  583. && SSA_NAME_VALUE (cached_lhs))
  584. cached_lhs = SSA_NAME_VALUE (cached_lhs);
  585. else
  586. break;
  587. }
  588. }
  589. /* If we're dominated by a suitable ASSERT_EXPR, then
  590. update CACHED_LHS appropriately. */
  591. if (handle_dominating_asserts && TREE_CODE (cached_lhs) == SSA_NAME)
  592. cached_lhs = lhs_of_dominating_assert (cached_lhs, e->src, stmt);
  593. /* If we haven't simplified to an invariant yet, then use the
  594. pass specific callback to try and simplify it further. */
  595. if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
  596. cached_lhs = (*simplify) (stmt, stmt);
  597. /* We couldn't find an invariant. But, callers of this
  598. function may be able to do something useful with the
  599. unmodified destination. */
  600. if (!cached_lhs)
  601. cached_lhs = original_lhs;
  602. }
  603. else
  604. cached_lhs = NULL;
  605. return cached_lhs;
  606. }
  607. /* Copy debug stmts from DEST's chain of single predecessors up to
  608. SRC, so that we don't lose the bindings as PHI nodes are introduced
  609. when DEST gains new predecessors. */
  610. void
  611. propagate_threaded_block_debug_into (basic_block dest, basic_block src)
  612. {
  613. if (!MAY_HAVE_DEBUG_STMTS)
  614. return;
  615. if (!single_pred_p (dest))
  616. return;
  617. gcc_checking_assert (dest != src);
  618. gimple_stmt_iterator gsi = gsi_after_labels (dest);
  619. int i = 0;
  620. const int alloc_count = 16; // ?? Should this be a PARAM?
  621. /* Estimate the number of debug vars overridden in the beginning of
  622. DEST, to tell how many we're going to need to begin with. */
  623. for (gimple_stmt_iterator si = gsi;
  624. i * 4 <= alloc_count * 3 && !gsi_end_p (si); gsi_next (&si))
  625. {
  626. gimple stmt = gsi_stmt (si);
  627. if (!is_gimple_debug (stmt))
  628. break;
  629. i++;
  630. }
  631. auto_vec<tree, alloc_count> fewvars;
  632. hash_set<tree> *vars = NULL;
  633. /* If we're already starting with 3/4 of alloc_count, go for a
  634. hash_set, otherwise start with an unordered stack-allocated
  635. VEC. */
  636. if (i * 4 > alloc_count * 3)
  637. vars = new hash_set<tree>;
  638. /* Now go through the initial debug stmts in DEST again, this time
  639. actually inserting in VARS or FEWVARS. Don't bother checking for
  640. duplicates in FEWVARS. */
  641. for (gimple_stmt_iterator si = gsi; !gsi_end_p (si); gsi_next (&si))
  642. {
  643. gimple stmt = gsi_stmt (si);
  644. if (!is_gimple_debug (stmt))
  645. break;
  646. tree var;
  647. if (gimple_debug_bind_p (stmt))
  648. var = gimple_debug_bind_get_var (stmt);
  649. else if (gimple_debug_source_bind_p (stmt))
  650. var = gimple_debug_source_bind_get_var (stmt);
  651. else
  652. gcc_unreachable ();
  653. if (vars)
  654. vars->add (var);
  655. else
  656. fewvars.quick_push (var);
  657. }
  658. basic_block bb = dest;
  659. do
  660. {
  661. bb = single_pred (bb);
  662. for (gimple_stmt_iterator si = gsi_last_bb (bb);
  663. !gsi_end_p (si); gsi_prev (&si))
  664. {
  665. gimple stmt = gsi_stmt (si);
  666. if (!is_gimple_debug (stmt))
  667. continue;
  668. tree var;
  669. if (gimple_debug_bind_p (stmt))
  670. var = gimple_debug_bind_get_var (stmt);
  671. else if (gimple_debug_source_bind_p (stmt))
  672. var = gimple_debug_source_bind_get_var (stmt);
  673. else
  674. gcc_unreachable ();
  675. /* Discard debug bind overlaps. ??? Unlike stmts from src,
  676. copied into a new block that will precede BB, debug bind
  677. stmts in bypassed BBs may actually be discarded if
  678. they're overwritten by subsequent debug bind stmts, which
  679. might be a problem once we introduce stmt frontier notes
  680. or somesuch. Adding `&& bb == src' to the condition
  681. below will preserve all potentially relevant debug
  682. notes. */
  683. if (vars && vars->add (var))
  684. continue;
  685. else if (!vars)
  686. {
  687. int i = fewvars.length ();
  688. while (i--)
  689. if (fewvars[i] == var)
  690. break;
  691. if (i >= 0)
  692. continue;
  693. if (fewvars.length () < (unsigned) alloc_count)
  694. fewvars.quick_push (var);
  695. else
  696. {
  697. vars = new hash_set<tree>;
  698. for (i = 0; i < alloc_count; i++)
  699. vars->add (fewvars[i]);
  700. fewvars.release ();
  701. vars->add (var);
  702. }
  703. }
  704. stmt = gimple_copy (stmt);
  705. /* ??? Should we drop the location of the copy to denote
  706. they're artificial bindings? */
  707. gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
  708. }
  709. }
  710. while (bb != src && single_pred_p (bb));
  711. if (vars)
  712. delete vars;
  713. else if (fewvars.exists ())
  714. fewvars.release ();
  715. }
  716. /* See if TAKEN_EDGE->dest is a threadable block with no side effecs (ie, it
  717. need not be duplicated as part of the CFG/SSA updating process).
  718. If it is threadable, add it to PATH and VISITED and recurse, ultimately
  719. returning TRUE from the toplevel call. Otherwise do nothing and
  720. return false.
  721. DUMMY_COND, HANDLE_DOMINATING_ASSERTS and SIMPLIFY are used to
  722. try and simplify the condition at the end of TAKEN_EDGE->dest. */
  723. static bool
  724. thread_around_empty_blocks (edge taken_edge,
  725. gcond *dummy_cond,
  726. bool handle_dominating_asserts,
  727. tree (*simplify) (gimple, gimple),
  728. bitmap visited,
  729. vec<jump_thread_edge *> *path,
  730. bool *backedge_seen_p)
  731. {
  732. basic_block bb = taken_edge->dest;
  733. gimple_stmt_iterator gsi;
  734. gimple stmt;
  735. tree cond;
  736. /* The key property of these blocks is that they need not be duplicated
  737. when threading. Thus they can not have visible side effects such
  738. as PHI nodes. */
  739. if (!gsi_end_p (gsi_start_phis (bb)))
  740. return false;
  741. /* Skip over DEBUG statements at the start of the block. */
  742. gsi = gsi_start_nondebug_bb (bb);
  743. /* If the block has no statements, but does have a single successor, then
  744. it's just a forwarding block and we can thread through it trivially.
  745. However, note that just threading through empty blocks with single
  746. successors is not inherently profitable. For the jump thread to
  747. be profitable, we must avoid a runtime conditional.
  748. By taking the return value from the recursive call, we get the
  749. desired effect of returning TRUE when we found a profitable jump
  750. threading opportunity and FALSE otherwise.
  751. This is particularly important when this routine is called after
  752. processing a joiner block. Returning TRUE too aggressively in
  753. that case results in pointless duplication of the joiner block. */
  754. if (gsi_end_p (gsi))
  755. {
  756. if (single_succ_p (bb))
  757. {
  758. taken_edge = single_succ_edge (bb);
  759. if (!bitmap_bit_p (visited, taken_edge->dest->index))
  760. {
  761. jump_thread_edge *x
  762. = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
  763. path->safe_push (x);
  764. bitmap_set_bit (visited, taken_edge->dest->index);
  765. *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
  766. if (*backedge_seen_p)
  767. simplify = dummy_simplify;
  768. return thread_around_empty_blocks (taken_edge,
  769. dummy_cond,
  770. handle_dominating_asserts,
  771. simplify,
  772. visited,
  773. path,
  774. backedge_seen_p);
  775. }
  776. }
  777. /* We have a block with no statements, but multiple successors? */
  778. return false;
  779. }
  780. /* The only real statements this block can have are a control
  781. flow altering statement. Anything else stops the thread. */
  782. stmt = gsi_stmt (gsi);
  783. if (gimple_code (stmt) != GIMPLE_COND
  784. && gimple_code (stmt) != GIMPLE_GOTO
  785. && gimple_code (stmt) != GIMPLE_SWITCH)
  786. return false;
  787. /* If we have traversed a backedge, then we do not want to look
  788. at certain expressions in the table that can not be relied upon.
  789. Luckily the only code that looked at those expressions is the
  790. SIMPLIFY callback, which we replace if we can no longer use it. */
  791. if (*backedge_seen_p)
  792. simplify = dummy_simplify;
  793. /* Extract and simplify the condition. */
  794. cond = simplify_control_stmt_condition (taken_edge, stmt, dummy_cond,
  795. simplify, handle_dominating_asserts);
  796. /* If the condition can be statically computed and we have not already
  797. visited the destination edge, then add the taken edge to our thread
  798. path. */
  799. if (cond && is_gimple_min_invariant (cond))
  800. {
  801. taken_edge = find_taken_edge (bb, cond);
  802. if (bitmap_bit_p (visited, taken_edge->dest->index))
  803. return false;
  804. bitmap_set_bit (visited, taken_edge->dest->index);
  805. jump_thread_edge *x
  806. = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
  807. path->safe_push (x);
  808. *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
  809. if (*backedge_seen_p)
  810. simplify = dummy_simplify;
  811. thread_around_empty_blocks (taken_edge,
  812. dummy_cond,
  813. handle_dominating_asserts,
  814. simplify,
  815. visited,
  816. path,
  817. backedge_seen_p);
  818. return true;
  819. }
  820. return false;
  821. }
  822. /* Return true if the CFG contains at least one path from START_BB to END_BB.
  823. When a path is found, record in PATH the blocks from END_BB to START_BB.
  824. VISITED_BBS is used to make sure we don't fall into an infinite loop. Bound
  825. the recursion to basic blocks belonging to LOOP. */
  826. static bool
  827. fsm_find_thread_path (basic_block start_bb, basic_block end_bb,
  828. vec<basic_block, va_gc> *&path,
  829. hash_set<basic_block> *visited_bbs, loop_p loop)
  830. {
  831. if (loop != start_bb->loop_father)
  832. return false;
  833. if (start_bb == end_bb)
  834. {
  835. vec_safe_push (path, start_bb);
  836. return true;
  837. }
  838. if (!visited_bbs->add (start_bb))
  839. {
  840. edge e;
  841. edge_iterator ei;
  842. FOR_EACH_EDGE (e, ei, start_bb->succs)
  843. if (fsm_find_thread_path (e->dest, end_bb, path, visited_bbs, loop))
  844. {
  845. vec_safe_push (path, start_bb);
  846. return true;
  847. }
  848. }
  849. return false;
  850. }
  851. static int max_threaded_paths;
  852. /* We trace the value of the variable EXPR back through any phi nodes looking
  853. for places where it gets a constant value and save the path. Stop after
  854. having recorded MAX_PATHS jump threading paths. */
  855. static void
  856. fsm_find_control_statement_thread_paths (tree expr,
  857. hash_set<basic_block> *visited_bbs,
  858. vec<basic_block, va_gc> *&path,
  859. bool seen_loop_phi)
  860. {
  861. tree var = SSA_NAME_VAR (expr);
  862. gimple def_stmt = SSA_NAME_DEF_STMT (expr);
  863. basic_block var_bb = gimple_bb (def_stmt);
  864. if (var == NULL || var_bb == NULL)
  865. return;
  866. /* For the moment we assume that an SSA chain only contains phi nodes, and
  867. eventually one of the phi arguments will be an integer constant. In the
  868. future, this could be extended to also handle simple assignments of
  869. arithmetic operations. */
  870. if (gimple_code (def_stmt) != GIMPLE_PHI)
  871. return;
  872. /* Avoid infinite recursion. */
  873. if (visited_bbs->add (var_bb))
  874. return;
  875. gphi *phi = as_a <gphi *> (def_stmt);
  876. int next_path_length = 0;
  877. basic_block last_bb_in_path = path->last ();
  878. if (loop_containing_stmt (phi)->header == gimple_bb (phi))
  879. {
  880. /* Do not walk through more than one loop PHI node. */
  881. if (seen_loop_phi)
  882. return;
  883. seen_loop_phi = true;
  884. }
  885. /* Following the chain of SSA_NAME definitions, we jumped from a definition in
  886. LAST_BB_IN_PATH to a definition in VAR_BB. When these basic blocks are
  887. different, append to PATH the blocks from LAST_BB_IN_PATH to VAR_BB. */
  888. if (var_bb != last_bb_in_path)
  889. {
  890. edge e;
  891. int e_count = 0;
  892. edge_iterator ei;
  893. vec<basic_block, va_gc> *next_path;
  894. vec_alloc (next_path, n_basic_blocks_for_fn (cfun));
  895. FOR_EACH_EDGE (e, ei, last_bb_in_path->preds)
  896. {
  897. hash_set<basic_block> *visited_bbs = new hash_set<basic_block>;
  898. if (fsm_find_thread_path (var_bb, e->src, next_path, visited_bbs,
  899. e->src->loop_father))
  900. ++e_count;
  901. delete visited_bbs;
  902. /* If there is more than one path, stop. */
  903. if (e_count > 1)
  904. {
  905. vec_free (next_path);
  906. return;
  907. }
  908. }
  909. /* Stop if we have not found a path: this could occur when the recursion
  910. is stopped by one of the bounds. */
  911. if (e_count == 0)
  912. {
  913. vec_free (next_path);
  914. return;
  915. }
  916. /* Append all the nodes from NEXT_PATH to PATH. */
  917. vec_safe_splice (path, next_path);
  918. next_path_length = next_path->length ();
  919. vec_free (next_path);
  920. }
  921. gcc_assert (path->last () == var_bb);
  922. /* Iterate over the arguments of PHI. */
  923. unsigned int i;
  924. for (i = 0; i < gimple_phi_num_args (phi); i++)
  925. {
  926. tree arg = gimple_phi_arg_def (phi, i);
  927. basic_block bbi = gimple_phi_arg_edge (phi, i)->src;
  928. /* Skip edges pointing outside the current loop. */
  929. if (!arg || var_bb->loop_father != bbi->loop_father)
  930. continue;
  931. if (TREE_CODE (arg) == SSA_NAME)
  932. {
  933. vec_safe_push (path, bbi);
  934. /* Recursively follow SSA_NAMEs looking for a constant definition. */
  935. fsm_find_control_statement_thread_paths (arg, visited_bbs, path,
  936. seen_loop_phi);
  937. path->pop ();
  938. continue;
  939. }
  940. if (TREE_CODE (arg) != INTEGER_CST)
  941. continue;
  942. int path_length = path->length ();
  943. /* A path with less than 2 basic blocks should not be jump-threaded. */
  944. if (path_length < 2)
  945. continue;
  946. if (path_length > PARAM_VALUE (PARAM_MAX_FSM_THREAD_LENGTH))
  947. {
  948. if (dump_file && (dump_flags & TDF_DETAILS))
  949. fprintf (dump_file, "FSM jump-thread path not considered: "
  950. "the number of basic blocks on the path "
  951. "exceeds PARAM_MAX_FSM_THREAD_LENGTH.\n");
  952. continue;
  953. }
  954. if (max_threaded_paths <= 0)
  955. {
  956. if (dump_file && (dump_flags & TDF_DETAILS))
  957. fprintf (dump_file, "FSM jump-thread path not considered: "
  958. "the number of previously recorded FSM paths to thread "
  959. "exceeds PARAM_MAX_FSM_THREAD_PATHS.\n");
  960. continue;
  961. }
  962. /* Add BBI to the path. */
  963. vec_safe_push (path, bbi);
  964. ++path_length;
  965. int n_insns = 0;
  966. gimple_stmt_iterator gsi;
  967. int j;
  968. loop_p loop = (*path)[0]->loop_father;
  969. bool path_crosses_loops = false;
  970. /* Count the number of instructions on the path: as these instructions
  971. will have to be duplicated, we will not record the path if there are
  972. too many instructions on the path. Also check that all the blocks in
  973. the path belong to a single loop. */
  974. for (j = 1; j < path_length - 1; j++)
  975. {
  976. basic_block bb = (*path)[j];
  977. if (bb->loop_father != loop)
  978. {
  979. path_crosses_loops = true;
  980. break;
  981. }
  982. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  983. {
  984. gimple stmt = gsi_stmt (gsi);
  985. /* Do not count empty statements and labels. */
  986. if (gimple_code (stmt) != GIMPLE_NOP
  987. && gimple_code (stmt) != GIMPLE_LABEL
  988. && !is_gimple_debug (stmt))
  989. ++n_insns;
  990. }
  991. }
  992. if (path_crosses_loops)
  993. {
  994. if (dump_file && (dump_flags & TDF_DETAILS))
  995. fprintf (dump_file, "FSM jump-thread path not considered: "
  996. "the path crosses loops.\n");
  997. path->pop ();
  998. continue;
  999. }
  1000. if (n_insns >= PARAM_VALUE (PARAM_MAX_FSM_THREAD_PATH_INSNS))
  1001. {
  1002. if (dump_file && (dump_flags & TDF_DETAILS))
  1003. fprintf (dump_file, "FSM jump-thread path not considered: "
  1004. "the number of instructions on the path "
  1005. "exceeds PARAM_MAX_FSM_THREAD_PATH_INSNS.\n");
  1006. path->pop ();
  1007. continue;
  1008. }
  1009. vec<jump_thread_edge *> *jump_thread_path
  1010. = new vec<jump_thread_edge *> ();
  1011. /* Record the edges between the blocks in PATH. */
  1012. for (j = 0; j < path_length - 1; j++)
  1013. {
  1014. edge e = find_edge ((*path)[path_length - j - 1],
  1015. (*path)[path_length - j - 2]);
  1016. gcc_assert (e);
  1017. jump_thread_edge *x = new jump_thread_edge (e, EDGE_FSM_THREAD);
  1018. jump_thread_path->safe_push (x);
  1019. }
  1020. /* Add the edge taken when the control variable has value ARG. */
  1021. edge taken_edge = find_taken_edge ((*path)[0], arg);
  1022. jump_thread_edge *x
  1023. = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
  1024. jump_thread_path->safe_push (x);
  1025. register_jump_thread (jump_thread_path);
  1026. --max_threaded_paths;
  1027. /* Remove BBI from the path. */
  1028. path->pop ();
  1029. }
  1030. /* Remove all the nodes that we added from NEXT_PATH. */
  1031. if (next_path_length)
  1032. vec_safe_truncate (path, (path->length () - next_path_length));
  1033. }
  1034. /* We are exiting E->src, see if E->dest ends with a conditional
  1035. jump which has a known value when reached via E.
  1036. E->dest can have arbitrary side effects which, if threading is
  1037. successful, will be maintained.
  1038. Special care is necessary if E is a back edge in the CFG as we
  1039. may have already recorded equivalences for E->dest into our
  1040. various tables, including the result of the conditional at
  1041. the end of E->dest. Threading opportunities are severely
  1042. limited in that case to avoid short-circuiting the loop
  1043. incorrectly.
  1044. DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
  1045. to avoid allocating memory.
  1046. HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
  1047. the simplified condition with left-hand sides of ASSERT_EXPRs they are
  1048. used in.
  1049. STACK is used to undo temporary equivalences created during the walk of
  1050. E->dest.
  1051. SIMPLIFY is a pass-specific function used to simplify statements.
  1052. Our caller is responsible for restoring the state of the expression
  1053. and const_and_copies stacks.
  1054. Positive return value is success. Zero return value is failure, but
  1055. the block can still be duplicated as a joiner in a jump thread path,
  1056. negative indicates the block should not be duplicated and thus is not
  1057. suitable for a joiner in a jump threading path. */
  1058. static int
  1059. thread_through_normal_block (edge e,
  1060. gcond *dummy_cond,
  1061. bool handle_dominating_asserts,
  1062. vec<tree> *stack,
  1063. tree (*simplify) (gimple, gimple),
  1064. vec<jump_thread_edge *> *path,
  1065. bitmap visited,
  1066. bool *backedge_seen_p)
  1067. {
  1068. /* If we have traversed a backedge, then we do not want to look
  1069. at certain expressions in the table that can not be relied upon.
  1070. Luckily the only code that looked at those expressions is the
  1071. SIMPLIFY callback, which we replace if we can no longer use it. */
  1072. if (*backedge_seen_p)
  1073. simplify = dummy_simplify;
  1074. /* PHIs create temporary equivalences.
  1075. Note that if we found a PHI that made the block non-threadable, then
  1076. we need to bubble that up to our caller in the same manner we do
  1077. when we prematurely stop processing statements below. */
  1078. if (!record_temporary_equivalences_from_phis (e, stack))
  1079. return -1;
  1080. /* Now walk each statement recording any context sensitive
  1081. temporary equivalences we can detect. */
  1082. gimple stmt
  1083. = record_temporary_equivalences_from_stmts_at_dest (e, stack, simplify,
  1084. *backedge_seen_p);
  1085. /* There's two reasons STMT might be null, and distinguishing
  1086. between them is important.
  1087. First the block may not have had any statements. For example, it
  1088. might have some PHIs and unconditionally transfer control elsewhere.
  1089. Such blocks are suitable for jump threading, particularly as a
  1090. joiner block.
  1091. The second reason would be if we did not process all the statements
  1092. in the block (because there were too many to make duplicating the
  1093. block profitable. If we did not look at all the statements, then
  1094. we may not have invalidated everything needing invalidation. Thus
  1095. we must signal to our caller that this block is not suitable for
  1096. use as a joiner in a threading path. */
  1097. if (!stmt)
  1098. {
  1099. /* First case. The statement simply doesn't have any instructions, but
  1100. does have PHIs. */
  1101. if (gsi_end_p (gsi_start_nondebug_bb (e->dest))
  1102. && !gsi_end_p (gsi_start_phis (e->dest)))
  1103. return 0;
  1104. /* Second case. */
  1105. return -1;
  1106. }
  1107. /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
  1108. will be taken. */
  1109. if (gimple_code (stmt) == GIMPLE_COND
  1110. || gimple_code (stmt) == GIMPLE_GOTO
  1111. || gimple_code (stmt) == GIMPLE_SWITCH)
  1112. {
  1113. tree cond;
  1114. /* Extract and simplify the condition. */
  1115. cond = simplify_control_stmt_condition (e, stmt, dummy_cond, simplify,
  1116. handle_dominating_asserts);
  1117. if (!cond)
  1118. return 0;
  1119. if (is_gimple_min_invariant (cond))
  1120. {
  1121. edge taken_edge = find_taken_edge (e->dest, cond);
  1122. basic_block dest = (taken_edge ? taken_edge->dest : NULL);
  1123. /* DEST could be NULL for a computed jump to an absolute
  1124. address. */
  1125. if (dest == NULL
  1126. || dest == e->dest
  1127. || bitmap_bit_p (visited, dest->index))
  1128. return 0;
  1129. /* Only push the EDGE_START_JUMP_THREAD marker if this is
  1130. first edge on the path. */
  1131. if (path->length () == 0)
  1132. {
  1133. jump_thread_edge *x
  1134. = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
  1135. path->safe_push (x);
  1136. *backedge_seen_p |= ((e->flags & EDGE_DFS_BACK) != 0);
  1137. }
  1138. jump_thread_edge *x
  1139. = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_BLOCK);
  1140. path->safe_push (x);
  1141. *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
  1142. if (*backedge_seen_p)
  1143. simplify = dummy_simplify;
  1144. /* See if we can thread through DEST as well, this helps capture
  1145. secondary effects of threading without having to re-run DOM or
  1146. VRP.
  1147. We don't want to thread back to a block we have already
  1148. visited. This may be overly conservative. */
  1149. bitmap_set_bit (visited, dest->index);
  1150. bitmap_set_bit (visited, e->dest->index);
  1151. thread_around_empty_blocks (taken_edge,
  1152. dummy_cond,
  1153. handle_dominating_asserts,
  1154. simplify,
  1155. visited,
  1156. path,
  1157. backedge_seen_p);
  1158. return 1;
  1159. }
  1160. if (!flag_expensive_optimizations
  1161. || optimize_function_for_size_p (cfun)
  1162. || TREE_CODE (cond) != SSA_NAME
  1163. || e->dest->loop_father != e->src->loop_father
  1164. || loop_depth (e->dest->loop_father) == 0)
  1165. return 0;
  1166. /* When COND cannot be simplified, try to find paths from a control
  1167. statement back through the PHI nodes which would affect that control
  1168. statement. */
  1169. vec<basic_block, va_gc> *bb_path;
  1170. vec_alloc (bb_path, n_basic_blocks_for_fn (cfun));
  1171. vec_safe_push (bb_path, e->dest);
  1172. hash_set<basic_block> *visited_bbs = new hash_set<basic_block>;
  1173. max_threaded_paths = PARAM_VALUE (PARAM_MAX_FSM_THREAD_PATHS);
  1174. fsm_find_control_statement_thread_paths (cond, visited_bbs, bb_path,
  1175. false);
  1176. delete visited_bbs;
  1177. vec_free (bb_path);
  1178. }
  1179. return 0;
  1180. }
  1181. /* We are exiting E->src, see if E->dest ends with a conditional
  1182. jump which has a known value when reached via E.
  1183. Special care is necessary if E is a back edge in the CFG as we
  1184. may have already recorded equivalences for E->dest into our
  1185. various tables, including the result of the conditional at
  1186. the end of E->dest. Threading opportunities are severely
  1187. limited in that case to avoid short-circuiting the loop
  1188. incorrectly.
  1189. Note it is quite common for the first block inside a loop to
  1190. end with a conditional which is either always true or always
  1191. false when reached via the loop backedge. Thus we do not want
  1192. to blindly disable threading across a loop backedge.
  1193. DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
  1194. to avoid allocating memory.
  1195. HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
  1196. the simplified condition with left-hand sides of ASSERT_EXPRs they are
  1197. used in.
  1198. STACK is used to undo temporary equivalences created during the walk of
  1199. E->dest.
  1200. SIMPLIFY is a pass-specific function used to simplify statements. */
  1201. void
  1202. thread_across_edge (gcond *dummy_cond,
  1203. edge e,
  1204. bool handle_dominating_asserts,
  1205. vec<tree> *stack,
  1206. tree (*simplify) (gimple, gimple))
  1207. {
  1208. bitmap visited = BITMAP_ALLOC (NULL);
  1209. bool backedge_seen;
  1210. stmt_count = 0;
  1211. vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
  1212. bitmap_clear (visited);
  1213. bitmap_set_bit (visited, e->src->index);
  1214. bitmap_set_bit (visited, e->dest->index);
  1215. backedge_seen = ((e->flags & EDGE_DFS_BACK) != 0);
  1216. if (backedge_seen)
  1217. simplify = dummy_simplify;
  1218. int threaded = thread_through_normal_block (e, dummy_cond,
  1219. handle_dominating_asserts,
  1220. stack, simplify, path,
  1221. visited, &backedge_seen);
  1222. if (threaded > 0)
  1223. {
  1224. propagate_threaded_block_debug_into (path->last ()->e->dest,
  1225. e->dest);
  1226. remove_temporary_equivalences (stack);
  1227. BITMAP_FREE (visited);
  1228. register_jump_thread (path);
  1229. return;
  1230. }
  1231. else
  1232. {
  1233. /* Negative and zero return values indicate no threading was possible,
  1234. thus there should be no edges on the thread path and no need to walk
  1235. through the vector entries. */
  1236. gcc_assert (path->length () == 0);
  1237. path->release ();
  1238. delete path;
  1239. /* A negative status indicates the target block was deemed too big to
  1240. duplicate. Just quit now rather than trying to use the block as
  1241. a joiner in a jump threading path.
  1242. This prevents unnecessary code growth, but more importantly if we
  1243. do not look at all the statements in the block, then we may have
  1244. missed some invalidations if we had traversed a backedge! */
  1245. if (threaded < 0)
  1246. {
  1247. BITMAP_FREE (visited);
  1248. remove_temporary_equivalences (stack);
  1249. return;
  1250. }
  1251. }
  1252. /* We were unable to determine what out edge from E->dest is taken. However,
  1253. we might still be able to thread through successors of E->dest. This
  1254. often occurs when E->dest is a joiner block which then fans back out
  1255. based on redundant tests.
  1256. If so, we'll copy E->dest and redirect the appropriate predecessor to
  1257. the copy. Within the copy of E->dest, we'll thread one or more edges
  1258. to points deeper in the CFG.
  1259. This is a stopgap until we have a more structured approach to path
  1260. isolation. */
  1261. {
  1262. edge taken_edge;
  1263. edge_iterator ei;
  1264. bool found;
  1265. /* If E->dest has abnormal outgoing edges, then there's no guarantee
  1266. we can safely redirect any of the edges. Just punt those cases. */
  1267. FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
  1268. if (taken_edge->flags & EDGE_ABNORMAL)
  1269. {
  1270. remove_temporary_equivalences (stack);
  1271. BITMAP_FREE (visited);
  1272. return;
  1273. }
  1274. /* Look at each successor of E->dest to see if we can thread through it. */
  1275. FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
  1276. {
  1277. /* Push a fresh marker so we can unwind the equivalences created
  1278. for each of E->dest's successors. */
  1279. stack->safe_push (NULL_TREE);
  1280. /* Avoid threading to any block we have already visited. */
  1281. bitmap_clear (visited);
  1282. bitmap_set_bit (visited, e->src->index);
  1283. bitmap_set_bit (visited, e->dest->index);
  1284. bitmap_set_bit (visited, taken_edge->dest->index);
  1285. vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
  1286. /* Record whether or not we were able to thread through a successor
  1287. of E->dest. */
  1288. jump_thread_edge *x = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
  1289. path->safe_push (x);
  1290. x = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
  1291. path->safe_push (x);
  1292. found = false;
  1293. backedge_seen = ((e->flags & EDGE_DFS_BACK) != 0);
  1294. backedge_seen |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
  1295. if (backedge_seen)
  1296. simplify = dummy_simplify;
  1297. found = thread_around_empty_blocks (taken_edge,
  1298. dummy_cond,
  1299. handle_dominating_asserts,
  1300. simplify,
  1301. visited,
  1302. path,
  1303. &backedge_seen);
  1304. if (backedge_seen)
  1305. simplify = dummy_simplify;
  1306. if (!found)
  1307. found = thread_through_normal_block (path->last ()->e, dummy_cond,
  1308. handle_dominating_asserts,
  1309. stack, simplify, path, visited,
  1310. &backedge_seen) > 0;
  1311. /* If we were able to thread through a successor of E->dest, then
  1312. record the jump threading opportunity. */
  1313. if (found)
  1314. {
  1315. propagate_threaded_block_debug_into (path->last ()->e->dest,
  1316. taken_edge->dest);
  1317. register_jump_thread (path);
  1318. }
  1319. else
  1320. {
  1321. delete_jump_thread_path (path);
  1322. }
  1323. /* And unwind the equivalence table. */
  1324. remove_temporary_equivalences (stack);
  1325. }
  1326. BITMAP_FREE (visited);
  1327. }
  1328. remove_temporary_equivalences (stack);
  1329. }