tree-outof-ssa.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. /* Convert a program in SSA form into Normal form.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. Contributed by Andrew Macleod <amacleod@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 "stor-layout.h"
  32. #include "predict.h"
  33. #include "hard-reg-set.h"
  34. #include "function.h"
  35. #include "dominance.h"
  36. #include "cfg.h"
  37. #include "cfgrtl.h"
  38. #include "cfganal.h"
  39. #include "basic-block.h"
  40. #include "gimple-pretty-print.h"
  41. #include "bitmap.h"
  42. #include "sbitmap.h"
  43. #include "tree-ssa-alias.h"
  44. #include "internal-fn.h"
  45. #include "tree-eh.h"
  46. #include "gimple-expr.h"
  47. #include "is-a.h"
  48. #include "gimple.h"
  49. #include "gimple-iterator.h"
  50. #include "gimple-ssa.h"
  51. #include "tree-cfg.h"
  52. #include "tree-phinodes.h"
  53. #include "ssa-iterators.h"
  54. #include "stringpool.h"
  55. #include "tree-ssanames.h"
  56. #include "dumpfile.h"
  57. #include "diagnostic-core.h"
  58. #include "tree-ssa-live.h"
  59. #include "tree-ssa-ter.h"
  60. #include "tree-ssa-coalesce.h"
  61. #include "tree-outof-ssa.h"
  62. /* FIXME: A lot of code here deals with expanding to RTL. All that code
  63. should be in cfgexpand.c. */
  64. #include "hashtab.h"
  65. #include "rtl.h"
  66. #include "flags.h"
  67. #include "statistics.h"
  68. #include "real.h"
  69. #include "fixed-value.h"
  70. #include "insn-config.h"
  71. #include "expmed.h"
  72. #include "dojump.h"
  73. #include "explow.h"
  74. #include "calls.h"
  75. #include "emit-rtl.h"
  76. #include "varasm.h"
  77. #include "stmt.h"
  78. #include "expr.h"
  79. /* Return TRUE if expression STMT is suitable for replacement. */
  80. bool
  81. ssa_is_replaceable_p (gimple stmt)
  82. {
  83. use_operand_p use_p;
  84. tree def;
  85. gimple use_stmt;
  86. /* Only consider modify stmts. */
  87. if (!is_gimple_assign (stmt))
  88. return false;
  89. /* If the statement may throw an exception, it cannot be replaced. */
  90. if (stmt_could_throw_p (stmt))
  91. return false;
  92. /* Punt if there is more than 1 def. */
  93. def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
  94. if (!def)
  95. return false;
  96. /* Only consider definitions which have a single use. */
  97. if (!single_imm_use (def, &use_p, &use_stmt))
  98. return false;
  99. /* Used in this block, but at the TOP of the block, not the end. */
  100. if (gimple_code (use_stmt) == GIMPLE_PHI)
  101. return false;
  102. /* There must be no VDEFs. */
  103. if (gimple_vdef (stmt))
  104. return false;
  105. /* Float expressions must go through memory if float-store is on. */
  106. if (flag_float_store
  107. && FLOAT_TYPE_P (gimple_expr_type (stmt)))
  108. return false;
  109. /* An assignment with a register variable on the RHS is not
  110. replaceable. */
  111. if (gimple_assign_rhs_code (stmt) == VAR_DECL
  112. && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
  113. return false;
  114. /* No function calls can be replaced. */
  115. if (is_gimple_call (stmt))
  116. return false;
  117. /* Leave any stmt with volatile operands alone as well. */
  118. if (gimple_has_volatile_ops (stmt))
  119. return false;
  120. return true;
  121. }
  122. /* Used to hold all the components required to do SSA PHI elimination.
  123. The node and pred/succ list is a simple linear list of nodes and
  124. edges represented as pairs of nodes.
  125. The predecessor and successor list: Nodes are entered in pairs, where
  126. [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
  127. predecessors, all the odd elements are successors.
  128. Rationale:
  129. When implemented as bitmaps, very large programs SSA->Normal times were
  130. being dominated by clearing the interference graph.
  131. Typically this list of edges is extremely small since it only includes
  132. PHI results and uses from a single edge which have not coalesced with
  133. each other. This means that no virtual PHI nodes are included, and
  134. empirical evidence suggests that the number of edges rarely exceed
  135. 3, and in a bootstrap of GCC, the maximum size encountered was 7.
  136. This also limits the number of possible nodes that are involved to
  137. rarely more than 6, and in the bootstrap of gcc, the maximum number
  138. of nodes encountered was 12. */
  139. typedef struct _elim_graph {
  140. /* Size of the elimination vectors. */
  141. int size;
  142. /* List of nodes in the elimination graph. */
  143. vec<int> nodes;
  144. /* The predecessor and successor edge list. */
  145. vec<int> edge_list;
  146. /* Source locus on each edge */
  147. vec<source_location> edge_locus;
  148. /* Visited vector. */
  149. sbitmap visited;
  150. /* Stack for visited nodes. */
  151. vec<int> stack;
  152. /* The variable partition map. */
  153. var_map map;
  154. /* Edge being eliminated by this graph. */
  155. edge e;
  156. /* List of constant copies to emit. These are pushed on in pairs. */
  157. vec<int> const_dests;
  158. vec<tree> const_copies;
  159. /* Source locations for any constant copies. */
  160. vec<source_location> copy_locus;
  161. } *elim_graph;
  162. /* For an edge E find out a good source location to associate with
  163. instructions inserted on edge E. If E has an implicit goto set,
  164. use its location. Otherwise search instructions in predecessors
  165. of E for a location, and use that one. That makes sense because
  166. we insert on edges for PHI nodes, and effects of PHIs happen on
  167. the end of the predecessor conceptually. */
  168. static void
  169. set_location_for_edge (edge e)
  170. {
  171. if (e->goto_locus)
  172. {
  173. set_curr_insn_location (e->goto_locus);
  174. }
  175. else
  176. {
  177. basic_block bb = e->src;
  178. gimple_stmt_iterator gsi;
  179. do
  180. {
  181. for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
  182. {
  183. gimple stmt = gsi_stmt (gsi);
  184. if (is_gimple_debug (stmt))
  185. continue;
  186. if (gimple_has_location (stmt) || gimple_block (stmt))
  187. {
  188. set_curr_insn_location (gimple_location (stmt));
  189. return;
  190. }
  191. }
  192. /* Nothing found in this basic block. Make a half-assed attempt
  193. to continue with another block. */
  194. if (single_pred_p (bb))
  195. bb = single_pred (bb);
  196. else
  197. bb = e->src;
  198. }
  199. while (bb != e->src);
  200. }
  201. }
  202. /* Emit insns to copy SRC into DEST converting SRC if necessary. As
  203. SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
  204. which we deduce the size to copy in that case. */
  205. static inline rtx
  206. emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
  207. {
  208. rtx seq;
  209. start_sequence ();
  210. if (GET_MODE (src) != VOIDmode && GET_MODE (src) != GET_MODE (dest))
  211. src = convert_to_mode (GET_MODE (dest), src, unsignedsrcp);
  212. if (GET_MODE (src) == BLKmode)
  213. {
  214. gcc_assert (GET_MODE (dest) == BLKmode);
  215. emit_block_move (dest, src, expr_size (sizeexp), BLOCK_OP_NORMAL);
  216. }
  217. else
  218. emit_move_insn (dest, src);
  219. seq = get_insns ();
  220. end_sequence ();
  221. return seq;
  222. }
  223. /* Insert a copy instruction from partition SRC to DEST onto edge E. */
  224. static void
  225. insert_partition_copy_on_edge (edge e, int dest, int src, source_location locus)
  226. {
  227. tree var;
  228. rtx seq;
  229. if (dump_file && (dump_flags & TDF_DETAILS))
  230. {
  231. fprintf (dump_file,
  232. "Inserting a partition copy on edge BB%d->BB%d :"
  233. "PART.%d = PART.%d",
  234. e->src->index,
  235. e->dest->index, dest, src);
  236. fprintf (dump_file, "\n");
  237. }
  238. gcc_assert (SA.partition_to_pseudo[dest]);
  239. gcc_assert (SA.partition_to_pseudo[src]);
  240. set_location_for_edge (e);
  241. /* If a locus is provided, override the default. */
  242. if (locus)
  243. set_curr_insn_location (locus);
  244. var = partition_to_var (SA.map, src);
  245. seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
  246. copy_rtx (SA.partition_to_pseudo[src]),
  247. TYPE_UNSIGNED (TREE_TYPE (var)),
  248. var);
  249. insert_insn_on_edge (seq, e);
  250. }
  251. /* Insert a copy instruction from expression SRC to partition DEST
  252. onto edge E. */
  253. static void
  254. insert_value_copy_on_edge (edge e, int dest, tree src, source_location locus)
  255. {
  256. rtx dest_rtx, seq, x;
  257. machine_mode dest_mode, src_mode;
  258. int unsignedp;
  259. tree var;
  260. if (dump_file && (dump_flags & TDF_DETAILS))
  261. {
  262. fprintf (dump_file,
  263. "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
  264. e->src->index,
  265. e->dest->index, dest);
  266. print_generic_expr (dump_file, src, TDF_SLIM);
  267. fprintf (dump_file, "\n");
  268. }
  269. dest_rtx = copy_rtx (SA.partition_to_pseudo[dest]);
  270. gcc_assert (dest_rtx);
  271. set_location_for_edge (e);
  272. /* If a locus is provided, override the default. */
  273. if (locus)
  274. set_curr_insn_location (locus);
  275. start_sequence ();
  276. var = SSA_NAME_VAR (partition_to_var (SA.map, dest));
  277. src_mode = TYPE_MODE (TREE_TYPE (src));
  278. dest_mode = GET_MODE (dest_rtx);
  279. gcc_assert (src_mode == TYPE_MODE (TREE_TYPE (var)));
  280. gcc_assert (!REG_P (dest_rtx)
  281. || dest_mode == promote_decl_mode (var, &unsignedp));
  282. if (src_mode != dest_mode)
  283. {
  284. x = expand_expr (src, NULL, src_mode, EXPAND_NORMAL);
  285. x = convert_modes (dest_mode, src_mode, x, unsignedp);
  286. }
  287. else if (src_mode == BLKmode)
  288. {
  289. x = dest_rtx;
  290. store_expr (src, x, 0, false);
  291. }
  292. else
  293. x = expand_expr (src, dest_rtx, dest_mode, EXPAND_NORMAL);
  294. if (x != dest_rtx)
  295. emit_move_insn (dest_rtx, x);
  296. seq = get_insns ();
  297. end_sequence ();
  298. insert_insn_on_edge (seq, e);
  299. }
  300. /* Insert a copy instruction from RTL expression SRC to partition DEST
  301. onto edge E. */
  302. static void
  303. insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
  304. source_location locus)
  305. {
  306. rtx seq;
  307. if (dump_file && (dump_flags & TDF_DETAILS))
  308. {
  309. fprintf (dump_file,
  310. "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
  311. e->src->index,
  312. e->dest->index, dest);
  313. print_simple_rtl (dump_file, src);
  314. fprintf (dump_file, "\n");
  315. }
  316. gcc_assert (SA.partition_to_pseudo[dest]);
  317. set_location_for_edge (e);
  318. /* If a locus is provided, override the default. */
  319. if (locus)
  320. set_curr_insn_location (locus);
  321. /* We give the destination as sizeexp in case src/dest are BLKmode
  322. mems. Usually we give the source. As we result from SSA names
  323. the left and right size should be the same (and no WITH_SIZE_EXPR
  324. involved), so it doesn't matter. */
  325. seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
  326. src, unsignedsrcp,
  327. partition_to_var (SA.map, dest));
  328. insert_insn_on_edge (seq, e);
  329. }
  330. /* Insert a copy instruction from partition SRC to RTL lvalue DEST
  331. onto edge E. */
  332. static void
  333. insert_part_to_rtx_on_edge (edge e, rtx dest, int src, source_location locus)
  334. {
  335. tree var;
  336. rtx seq;
  337. if (dump_file && (dump_flags & TDF_DETAILS))
  338. {
  339. fprintf (dump_file,
  340. "Inserting a temp copy on edge BB%d->BB%d : ",
  341. e->src->index,
  342. e->dest->index);
  343. print_simple_rtl (dump_file, dest);
  344. fprintf (dump_file, "= PART.%d\n", src);
  345. }
  346. gcc_assert (SA.partition_to_pseudo[src]);
  347. set_location_for_edge (e);
  348. /* If a locus is provided, override the default. */
  349. if (locus)
  350. set_curr_insn_location (locus);
  351. var = partition_to_var (SA.map, src);
  352. seq = emit_partition_copy (dest,
  353. copy_rtx (SA.partition_to_pseudo[src]),
  354. TYPE_UNSIGNED (TREE_TYPE (var)),
  355. var);
  356. insert_insn_on_edge (seq, e);
  357. }
  358. /* Create an elimination graph with SIZE nodes and associated data
  359. structures. */
  360. static elim_graph
  361. new_elim_graph (int size)
  362. {
  363. elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
  364. g->nodes.create (30);
  365. g->const_dests.create (20);
  366. g->const_copies.create (20);
  367. g->copy_locus.create (10);
  368. g->edge_list.create (20);
  369. g->edge_locus.create (10);
  370. g->stack.create (30);
  371. g->visited = sbitmap_alloc (size);
  372. return g;
  373. }
  374. /* Empty elimination graph G. */
  375. static inline void
  376. clear_elim_graph (elim_graph g)
  377. {
  378. g->nodes.truncate (0);
  379. g->edge_list.truncate (0);
  380. g->edge_locus.truncate (0);
  381. }
  382. /* Delete elimination graph G. */
  383. static inline void
  384. delete_elim_graph (elim_graph g)
  385. {
  386. sbitmap_free (g->visited);
  387. g->stack.release ();
  388. g->edge_list.release ();
  389. g->const_copies.release ();
  390. g->const_dests.release ();
  391. g->nodes.release ();
  392. g->copy_locus.release ();
  393. g->edge_locus.release ();
  394. free (g);
  395. }
  396. /* Return the number of nodes in graph G. */
  397. static inline int
  398. elim_graph_size (elim_graph g)
  399. {
  400. return g->nodes.length ();
  401. }
  402. /* Add NODE to graph G, if it doesn't exist already. */
  403. static inline void
  404. elim_graph_add_node (elim_graph g, int node)
  405. {
  406. int x;
  407. int t;
  408. FOR_EACH_VEC_ELT (g->nodes, x, t)
  409. if (t == node)
  410. return;
  411. g->nodes.safe_push (node);
  412. }
  413. /* Add the edge PRED->SUCC to graph G. */
  414. static inline void
  415. elim_graph_add_edge (elim_graph g, int pred, int succ, source_location locus)
  416. {
  417. g->edge_list.safe_push (pred);
  418. g->edge_list.safe_push (succ);
  419. g->edge_locus.safe_push (locus);
  420. }
  421. /* Remove an edge from graph G for which NODE is the predecessor, and
  422. return the successor node. -1 is returned if there is no such edge. */
  423. static inline int
  424. elim_graph_remove_succ_edge (elim_graph g, int node, source_location *locus)
  425. {
  426. int y;
  427. unsigned x;
  428. for (x = 0; x < g->edge_list.length (); x += 2)
  429. if (g->edge_list[x] == node)
  430. {
  431. g->edge_list[x] = -1;
  432. y = g->edge_list[x + 1];
  433. g->edge_list[x + 1] = -1;
  434. *locus = g->edge_locus[x / 2];
  435. g->edge_locus[x / 2] = UNKNOWN_LOCATION;
  436. return y;
  437. }
  438. *locus = UNKNOWN_LOCATION;
  439. return -1;
  440. }
  441. /* Find all the nodes in GRAPH which are successors to NODE in the
  442. edge list. VAR will hold the partition number found. CODE is the
  443. code fragment executed for every node found. */
  444. #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE) \
  445. do { \
  446. unsigned x_; \
  447. int y_; \
  448. for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
  449. { \
  450. y_ = (GRAPH)->edge_list[x_]; \
  451. if (y_ != (NODE)) \
  452. continue; \
  453. (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]); \
  454. (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
  455. CODE; \
  456. } \
  457. } while (0)
  458. /* Find all the nodes which are predecessors of NODE in the edge list for
  459. GRAPH. VAR will hold the partition number found. CODE is the
  460. code fragment executed for every node found. */
  461. #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE) \
  462. do { \
  463. unsigned x_; \
  464. int y_; \
  465. for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
  466. { \
  467. y_ = (GRAPH)->edge_list[x_ + 1]; \
  468. if (y_ != (NODE)) \
  469. continue; \
  470. (void) ((VAR) = (GRAPH)->edge_list[x_]); \
  471. (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
  472. CODE; \
  473. } \
  474. } while (0)
  475. /* Add T to elimination graph G. */
  476. static inline void
  477. eliminate_name (elim_graph g, int T)
  478. {
  479. elim_graph_add_node (g, T);
  480. }
  481. /* Return true if this phi argument T should have a copy queued when using
  482. var_map MAP. PHI nodes should contain only ssa_names and invariants. A
  483. test for ssa_name is definitely simpler, but don't let invalid contents
  484. slip through in the meantime. */
  485. static inline bool
  486. queue_phi_copy_p (var_map map, tree t)
  487. {
  488. if (TREE_CODE (t) == SSA_NAME)
  489. {
  490. if (var_to_partition (map, t) == NO_PARTITION)
  491. return true;
  492. return false;
  493. }
  494. gcc_checking_assert (is_gimple_min_invariant (t));
  495. return true;
  496. }
  497. /* Build elimination graph G for basic block BB on incoming PHI edge
  498. G->e. */
  499. static void
  500. eliminate_build (elim_graph g)
  501. {
  502. tree Ti;
  503. int p0, pi;
  504. gphi_iterator gsi;
  505. clear_elim_graph (g);
  506. for (gsi = gsi_start_phis (g->e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
  507. {
  508. gphi *phi = gsi.phi ();
  509. source_location locus;
  510. p0 = var_to_partition (g->map, gimple_phi_result (phi));
  511. /* Ignore results which are not in partitions. */
  512. if (p0 == NO_PARTITION)
  513. continue;
  514. Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
  515. locus = gimple_phi_arg_location_from_edge (phi, g->e);
  516. /* If this argument is a constant, or a SSA_NAME which is being
  517. left in SSA form, just queue a copy to be emitted on this
  518. edge. */
  519. if (queue_phi_copy_p (g->map, Ti))
  520. {
  521. /* Save constant copies until all other copies have been emitted
  522. on this edge. */
  523. g->const_dests.safe_push (p0);
  524. g->const_copies.safe_push (Ti);
  525. g->copy_locus.safe_push (locus);
  526. }
  527. else
  528. {
  529. pi = var_to_partition (g->map, Ti);
  530. if (p0 != pi)
  531. {
  532. eliminate_name (g, p0);
  533. eliminate_name (g, pi);
  534. elim_graph_add_edge (g, p0, pi, locus);
  535. }
  536. }
  537. }
  538. }
  539. /* Push successors of T onto the elimination stack for G. */
  540. static void
  541. elim_forward (elim_graph g, int T)
  542. {
  543. int S;
  544. source_location locus;
  545. bitmap_set_bit (g->visited, T);
  546. FOR_EACH_ELIM_GRAPH_SUCC (g, T, S, locus,
  547. {
  548. if (!bitmap_bit_p (g->visited, S))
  549. elim_forward (g, S);
  550. });
  551. g->stack.safe_push (T);
  552. }
  553. /* Return 1 if there unvisited predecessors of T in graph G. */
  554. static int
  555. elim_unvisited_predecessor (elim_graph g, int T)
  556. {
  557. int P;
  558. source_location locus;
  559. FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
  560. {
  561. if (!bitmap_bit_p (g->visited, P))
  562. return 1;
  563. });
  564. return 0;
  565. }
  566. /* Process predecessors first, and insert a copy. */
  567. static void
  568. elim_backward (elim_graph g, int T)
  569. {
  570. int P;
  571. source_location locus;
  572. bitmap_set_bit (g->visited, T);
  573. FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
  574. {
  575. if (!bitmap_bit_p (g->visited, P))
  576. {
  577. elim_backward (g, P);
  578. insert_partition_copy_on_edge (g->e, P, T, locus);
  579. }
  580. });
  581. }
  582. /* Allocate a new pseudo register usable for storing values sitting
  583. in NAME (a decl or SSA name), i.e. with matching mode and attributes. */
  584. static rtx
  585. get_temp_reg (tree name)
  586. {
  587. tree var = TREE_CODE (name) == SSA_NAME ? SSA_NAME_VAR (name) : name;
  588. tree type = TREE_TYPE (var);
  589. int unsignedp;
  590. machine_mode reg_mode = promote_decl_mode (var, &unsignedp);
  591. rtx x = gen_reg_rtx (reg_mode);
  592. if (POINTER_TYPE_P (type))
  593. mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
  594. return x;
  595. }
  596. /* Insert required copies for T in graph G. Check for a strongly connected
  597. region, and create a temporary to break the cycle if one is found. */
  598. static void
  599. elim_create (elim_graph g, int T)
  600. {
  601. int P, S;
  602. source_location locus;
  603. if (elim_unvisited_predecessor (g, T))
  604. {
  605. tree var = partition_to_var (g->map, T);
  606. rtx U = get_temp_reg (var);
  607. int unsignedsrcp = TYPE_UNSIGNED (TREE_TYPE (var));
  608. insert_part_to_rtx_on_edge (g->e, U, T, UNKNOWN_LOCATION);
  609. FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
  610. {
  611. if (!bitmap_bit_p (g->visited, P))
  612. {
  613. elim_backward (g, P);
  614. insert_rtx_to_part_on_edge (g->e, P, U, unsignedsrcp, locus);
  615. }
  616. });
  617. }
  618. else
  619. {
  620. S = elim_graph_remove_succ_edge (g, T, &locus);
  621. if (S != -1)
  622. {
  623. bitmap_set_bit (g->visited, T);
  624. insert_partition_copy_on_edge (g->e, T, S, locus);
  625. }
  626. }
  627. }
  628. /* Eliminate all the phi nodes on edge E in graph G. */
  629. static void
  630. eliminate_phi (edge e, elim_graph g)
  631. {
  632. int x;
  633. gcc_assert (g->const_copies.length () == 0);
  634. gcc_assert (g->copy_locus.length () == 0);
  635. /* Abnormal edges already have everything coalesced. */
  636. if (e->flags & EDGE_ABNORMAL)
  637. return;
  638. g->e = e;
  639. eliminate_build (g);
  640. if (elim_graph_size (g) != 0)
  641. {
  642. int part;
  643. bitmap_clear (g->visited);
  644. g->stack.truncate (0);
  645. FOR_EACH_VEC_ELT (g->nodes, x, part)
  646. {
  647. if (!bitmap_bit_p (g->visited, part))
  648. elim_forward (g, part);
  649. }
  650. bitmap_clear (g->visited);
  651. while (g->stack.length () > 0)
  652. {
  653. x = g->stack.pop ();
  654. if (!bitmap_bit_p (g->visited, x))
  655. elim_create (g, x);
  656. }
  657. }
  658. /* If there are any pending constant copies, issue them now. */
  659. while (g->const_copies.length () > 0)
  660. {
  661. int dest;
  662. tree src;
  663. source_location locus;
  664. src = g->const_copies.pop ();
  665. dest = g->const_dests.pop ();
  666. locus = g->copy_locus.pop ();
  667. insert_value_copy_on_edge (e, dest, src, locus);
  668. }
  669. }
  670. /* Remove each argument from PHI. If an arg was the last use of an SSA_NAME,
  671. check to see if this allows another PHI node to be removed. */
  672. static void
  673. remove_gimple_phi_args (gphi *phi)
  674. {
  675. use_operand_p arg_p;
  676. ssa_op_iter iter;
  677. if (dump_file && (dump_flags & TDF_DETAILS))
  678. {
  679. fprintf (dump_file, "Removing Dead PHI definition: ");
  680. print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
  681. }
  682. FOR_EACH_PHI_ARG (arg_p, phi, iter, SSA_OP_USE)
  683. {
  684. tree arg = USE_FROM_PTR (arg_p);
  685. if (TREE_CODE (arg) == SSA_NAME)
  686. {
  687. /* Remove the reference to the existing argument. */
  688. SET_USE (arg_p, NULL_TREE);
  689. if (has_zero_uses (arg))
  690. {
  691. gimple stmt;
  692. gimple_stmt_iterator gsi;
  693. stmt = SSA_NAME_DEF_STMT (arg);
  694. /* Also remove the def if it is a PHI node. */
  695. if (gimple_code (stmt) == GIMPLE_PHI)
  696. {
  697. remove_gimple_phi_args (as_a <gphi *> (stmt));
  698. gsi = gsi_for_stmt (stmt);
  699. remove_phi_node (&gsi, true);
  700. }
  701. }
  702. }
  703. }
  704. }
  705. /* Remove any PHI node which is a virtual PHI, or a PHI with no uses. */
  706. static void
  707. eliminate_useless_phis (void)
  708. {
  709. basic_block bb;
  710. gphi_iterator gsi;
  711. tree result;
  712. FOR_EACH_BB_FN (bb, cfun)
  713. {
  714. for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
  715. {
  716. gphi *phi = gsi.phi ();
  717. result = gimple_phi_result (phi);
  718. if (virtual_operand_p (result))
  719. {
  720. #ifdef ENABLE_CHECKING
  721. size_t i;
  722. /* There should be no arguments which are not virtual, or the
  723. results will be incorrect. */
  724. for (i = 0; i < gimple_phi_num_args (phi); i++)
  725. {
  726. tree arg = PHI_ARG_DEF (phi, i);
  727. if (TREE_CODE (arg) == SSA_NAME
  728. && !virtual_operand_p (arg))
  729. {
  730. fprintf (stderr, "Argument of PHI is not virtual (");
  731. print_generic_expr (stderr, arg, TDF_SLIM);
  732. fprintf (stderr, "), but the result is :");
  733. print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
  734. internal_error ("SSA corruption");
  735. }
  736. }
  737. #endif
  738. remove_phi_node (&gsi, true);
  739. }
  740. else
  741. {
  742. /* Also remove real PHIs with no uses. */
  743. if (has_zero_uses (result))
  744. {
  745. remove_gimple_phi_args (phi);
  746. remove_phi_node (&gsi, true);
  747. }
  748. else
  749. gsi_next (&gsi);
  750. }
  751. }
  752. }
  753. }
  754. /* This function will rewrite the current program using the variable mapping
  755. found in MAP. If the replacement vector VALUES is provided, any
  756. occurrences of partitions with non-null entries in the vector will be
  757. replaced with the expression in the vector instead of its mapped
  758. variable. */
  759. static void
  760. rewrite_trees (var_map map ATTRIBUTE_UNUSED)
  761. {
  762. #ifdef ENABLE_CHECKING
  763. basic_block bb;
  764. /* Search for PHIs where the destination has no partition, but one
  765. or more arguments has a partition. This should not happen and can
  766. create incorrect code. */
  767. FOR_EACH_BB_FN (bb, cfun)
  768. {
  769. gphi_iterator gsi;
  770. for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  771. {
  772. gphi *phi = gsi.phi ();
  773. tree T0 = var_to_partition_to_var (map, gimple_phi_result (phi));
  774. if (T0 == NULL_TREE)
  775. {
  776. size_t i;
  777. for (i = 0; i < gimple_phi_num_args (phi); i++)
  778. {
  779. tree arg = PHI_ARG_DEF (phi, i);
  780. if (TREE_CODE (arg) == SSA_NAME
  781. && var_to_partition (map, arg) != NO_PARTITION)
  782. {
  783. fprintf (stderr, "Argument of PHI is in a partition :(");
  784. print_generic_expr (stderr, arg, TDF_SLIM);
  785. fprintf (stderr, "), but the result is not :");
  786. print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
  787. internal_error ("SSA corruption");
  788. }
  789. }
  790. }
  791. }
  792. }
  793. #endif
  794. }
  795. /* Given the out-of-ssa info object SA (with prepared partitions)
  796. eliminate all phi nodes in all basic blocks. Afterwards no
  797. basic block will have phi nodes anymore and there are possibly
  798. some RTL instructions inserted on edges. */
  799. void
  800. expand_phi_nodes (struct ssaexpand *sa)
  801. {
  802. basic_block bb;
  803. elim_graph g = new_elim_graph (sa->map->num_partitions);
  804. g->map = sa->map;
  805. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
  806. EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
  807. if (!gimple_seq_empty_p (phi_nodes (bb)))
  808. {
  809. edge e;
  810. edge_iterator ei;
  811. FOR_EACH_EDGE (e, ei, bb->preds)
  812. eliminate_phi (e, g);
  813. set_phi_nodes (bb, NULL);
  814. /* We can't redirect EH edges in RTL land, so we need to do this
  815. here. Redirection happens only when splitting is necessary,
  816. which it is only for critical edges, normally. For EH edges
  817. it might also be necessary when the successor has more than
  818. one predecessor. In that case the edge is either required to
  819. be fallthru (which EH edges aren't), or the predecessor needs
  820. to end with a jump (which again, isn't the case with EH edges).
  821. Hence, split all EH edges on which we inserted instructions
  822. and whose successor has multiple predecessors. */
  823. for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
  824. {
  825. if (e->insns.r && (e->flags & EDGE_EH)
  826. && !single_pred_p (e->dest))
  827. {
  828. rtx_insn *insns = e->insns.r;
  829. basic_block bb;
  830. e->insns.r = NULL;
  831. bb = split_edge (e);
  832. single_pred_edge (bb)->insns.r = insns;
  833. }
  834. else
  835. ei_next (&ei);
  836. }
  837. }
  838. delete_elim_graph (g);
  839. }
  840. /* Remove the ssa-names in the current function and translate them into normal
  841. compiler variables. PERFORM_TER is true if Temporary Expression Replacement
  842. should also be used. */
  843. static void
  844. remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
  845. {
  846. bitmap values = NULL;
  847. var_map map;
  848. unsigned i;
  849. map = coalesce_ssa_name ();
  850. /* Return to viewing the variable list as just all reference variables after
  851. coalescing has been performed. */
  852. partition_view_normal (map, false);
  853. if (dump_file && (dump_flags & TDF_DETAILS))
  854. {
  855. fprintf (dump_file, "After Coalescing:\n");
  856. dump_var_map (dump_file, map);
  857. }
  858. if (perform_ter)
  859. {
  860. values = find_replaceable_exprs (map);
  861. if (values && dump_file && (dump_flags & TDF_DETAILS))
  862. dump_replaceable_exprs (dump_file, values);
  863. }
  864. rewrite_trees (map);
  865. sa->map = map;
  866. sa->values = values;
  867. sa->partition_has_default_def = BITMAP_ALLOC (NULL);
  868. for (i = 1; i < num_ssa_names; i++)
  869. {
  870. tree t = ssa_name (i);
  871. if (t && SSA_NAME_IS_DEFAULT_DEF (t))
  872. {
  873. int p = var_to_partition (map, t);
  874. if (p != NO_PARTITION)
  875. bitmap_set_bit (sa->partition_has_default_def, p);
  876. }
  877. }
  878. }
  879. /* If not already done so for basic block BB, assign increasing uids
  880. to each of its instructions. */
  881. static void
  882. maybe_renumber_stmts_bb (basic_block bb)
  883. {
  884. unsigned i = 0;
  885. gimple_stmt_iterator gsi;
  886. if (!bb->aux)
  887. return;
  888. bb->aux = NULL;
  889. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  890. {
  891. gimple stmt = gsi_stmt (gsi);
  892. gimple_set_uid (stmt, i);
  893. i++;
  894. }
  895. }
  896. /* Return true if we can determine that the SSA_NAMEs RESULT (a result
  897. of a PHI node) and ARG (one of its arguments) conflict. Return false
  898. otherwise, also when we simply aren't sure. */
  899. static bool
  900. trivially_conflicts_p (basic_block bb, tree result, tree arg)
  901. {
  902. use_operand_p use;
  903. imm_use_iterator imm_iter;
  904. gimple defa = SSA_NAME_DEF_STMT (arg);
  905. /* If ARG isn't defined in the same block it's too complicated for
  906. our little mind. */
  907. if (gimple_bb (defa) != bb)
  908. return false;
  909. FOR_EACH_IMM_USE_FAST (use, imm_iter, result)
  910. {
  911. gimple use_stmt = USE_STMT (use);
  912. if (is_gimple_debug (use_stmt))
  913. continue;
  914. /* Now, if there's a use of RESULT that lies outside this basic block,
  915. then there surely is a conflict with ARG. */
  916. if (gimple_bb (use_stmt) != bb)
  917. return true;
  918. if (gimple_code (use_stmt) == GIMPLE_PHI)
  919. continue;
  920. /* The use now is in a real stmt of BB, so if ARG was defined
  921. in a PHI node (like RESULT) both conflict. */
  922. if (gimple_code (defa) == GIMPLE_PHI)
  923. return true;
  924. maybe_renumber_stmts_bb (bb);
  925. /* If the use of RESULT occurs after the definition of ARG,
  926. the two conflict too. */
  927. if (gimple_uid (defa) < gimple_uid (use_stmt))
  928. return true;
  929. }
  930. return false;
  931. }
  932. /* Search every PHI node for arguments associated with backedges which
  933. we can trivially determine will need a copy (the argument is either
  934. not an SSA_NAME or the argument has a different underlying variable
  935. than the PHI result).
  936. Insert a copy from the PHI argument to a new destination at the
  937. end of the block with the backedge to the top of the loop. Update
  938. the PHI argument to reference this new destination. */
  939. static void
  940. insert_backedge_copies (void)
  941. {
  942. basic_block bb;
  943. gphi_iterator gsi;
  944. mark_dfs_back_edges ();
  945. FOR_EACH_BB_FN (bb, cfun)
  946. {
  947. /* Mark block as possibly needing calculation of UIDs. */
  948. bb->aux = &bb->aux;
  949. for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  950. {
  951. gphi *phi = gsi.phi ();
  952. tree result = gimple_phi_result (phi);
  953. size_t i;
  954. if (virtual_operand_p (result))
  955. continue;
  956. for (i = 0; i < gimple_phi_num_args (phi); i++)
  957. {
  958. tree arg = gimple_phi_arg_def (phi, i);
  959. edge e = gimple_phi_arg_edge (phi, i);
  960. /* If the argument is not an SSA_NAME, then we will need a
  961. constant initialization. If the argument is an SSA_NAME with
  962. a different underlying variable then a copy statement will be
  963. needed. */
  964. if ((e->flags & EDGE_DFS_BACK)
  965. && (TREE_CODE (arg) != SSA_NAME
  966. || SSA_NAME_VAR (arg) != SSA_NAME_VAR (result)
  967. || trivially_conflicts_p (bb, result, arg)))
  968. {
  969. tree name;
  970. gassign *stmt;
  971. gimple last = NULL;
  972. gimple_stmt_iterator gsi2;
  973. gsi2 = gsi_last_bb (gimple_phi_arg_edge (phi, i)->src);
  974. if (!gsi_end_p (gsi2))
  975. last = gsi_stmt (gsi2);
  976. /* In theory the only way we ought to get back to the
  977. start of a loop should be with a COND_EXPR or GOTO_EXPR.
  978. However, better safe than sorry.
  979. If the block ends with a control statement or
  980. something that might throw, then we have to
  981. insert this assignment before the last
  982. statement. Else insert it after the last statement. */
  983. if (last && stmt_ends_bb_p (last))
  984. {
  985. /* If the last statement in the block is the definition
  986. site of the PHI argument, then we can't insert
  987. anything after it. */
  988. if (TREE_CODE (arg) == SSA_NAME
  989. && SSA_NAME_DEF_STMT (arg) == last)
  990. continue;
  991. }
  992. /* Create a new instance of the underlying variable of the
  993. PHI result. */
  994. name = copy_ssa_name (result);
  995. stmt = gimple_build_assign (name,
  996. gimple_phi_arg_def (phi, i));
  997. /* copy location if present. */
  998. if (gimple_phi_arg_has_location (phi, i))
  999. gimple_set_location (stmt,
  1000. gimple_phi_arg_location (phi, i));
  1001. /* Insert the new statement into the block and update
  1002. the PHI node. */
  1003. if (last && stmt_ends_bb_p (last))
  1004. gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
  1005. else
  1006. gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
  1007. SET_PHI_ARG_DEF (phi, i, name);
  1008. }
  1009. }
  1010. }
  1011. /* Unmark this block again. */
  1012. bb->aux = NULL;
  1013. }
  1014. }
  1015. /* Free all memory associated with going out of SSA form. SA is
  1016. the outof-SSA info object. */
  1017. void
  1018. finish_out_of_ssa (struct ssaexpand *sa)
  1019. {
  1020. free (sa->partition_to_pseudo);
  1021. if (sa->values)
  1022. BITMAP_FREE (sa->values);
  1023. delete_var_map (sa->map);
  1024. BITMAP_FREE (sa->partition_has_default_def);
  1025. memset (sa, 0, sizeof *sa);
  1026. }
  1027. /* Take the current function out of SSA form, translating PHIs as described in
  1028. R. Morgan, ``Building an Optimizing Compiler'',
  1029. Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
  1030. unsigned int
  1031. rewrite_out_of_ssa (struct ssaexpand *sa)
  1032. {
  1033. /* If elimination of a PHI requires inserting a copy on a backedge,
  1034. then we will have to split the backedge which has numerous
  1035. undesirable performance effects.
  1036. A significant number of such cases can be handled here by inserting
  1037. copies into the loop itself. */
  1038. insert_backedge_copies ();
  1039. /* Eliminate PHIs which are of no use, such as virtual or dead phis. */
  1040. eliminate_useless_phis ();
  1041. if (dump_file && (dump_flags & TDF_DETAILS))
  1042. gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
  1043. remove_ssa_form (flag_tree_ter, sa);
  1044. if (dump_file && (dump_flags & TDF_DETAILS))
  1045. gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
  1046. return 0;
  1047. }