ssa-iterators.h 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /* Header file for SSA iterators.
  2. Copyright (C) 2013-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_SSA_ITERATORS_H
  16. #define GCC_SSA_ITERATORS_H
  17. /* Immediate use lists are used to directly access all uses for an SSA
  18. name and get pointers to the statement for each use.
  19. The structure ssa_use_operand_t consists of PREV and NEXT pointers
  20. to maintain the list. A USE pointer, which points to address where
  21. the use is located and a LOC pointer which can point to the
  22. statement where the use is located, or, in the case of the root
  23. node, it points to the SSA name itself.
  24. The list is anchored by an occurrence of ssa_operand_d *in* the
  25. ssa_name node itself (named 'imm_uses'). This node is uniquely
  26. identified by having a NULL USE pointer. and the LOC pointer
  27. pointing back to the ssa_name node itself. This node forms the
  28. base for a circular list, and initially this is the only node in
  29. the list.
  30. Fast iteration allows each use to be examined, but does not allow
  31. any modifications to the uses or stmts.
  32. Normal iteration allows insertion, deletion, and modification. the
  33. iterator manages this by inserting a marker node into the list
  34. immediately before the node currently being examined in the list.
  35. this marker node is uniquely identified by having null stmt *and* a
  36. null use pointer.
  37. When iterating to the next use, the iteration routines check to see
  38. if the node after the marker has changed. if it has, then the node
  39. following the marker is now the next one to be visited. if not, the
  40. marker node is moved past that node in the list (visualize it as
  41. bumping the marker node through the list). this continues until
  42. the marker node is moved to the original anchor position. the
  43. marker node is then removed from the list.
  44. If iteration is halted early, the marker node must be removed from
  45. the list before continuing. */
  46. struct imm_use_iterator
  47. {
  48. /* This is the current use the iterator is processing. */
  49. ssa_use_operand_t *imm_use;
  50. /* This marks the last use in the list (use node from SSA_NAME) */
  51. ssa_use_operand_t *end_p;
  52. /* This node is inserted and used to mark the end of the uses for a stmt. */
  53. ssa_use_operand_t iter_node;
  54. /* This is the next ssa_name to visit. IMM_USE may get removed before
  55. the next one is traversed to, so it must be cached early. */
  56. ssa_use_operand_t *next_imm_name;
  57. };
  58. /* Use this iterator when simply looking at stmts. Adding, deleting or
  59. modifying stmts will cause this iterator to malfunction. */
  60. #define FOR_EACH_IMM_USE_FAST(DEST, ITER, SSAVAR) \
  61. for ((DEST) = first_readonly_imm_use (&(ITER), (SSAVAR)); \
  62. !end_readonly_imm_use_p (&(ITER)); \
  63. (void) ((DEST) = next_readonly_imm_use (&(ITER))))
  64. /* Use this iterator to visit each stmt which has a use of SSAVAR. */
  65. #define FOR_EACH_IMM_USE_STMT(STMT, ITER, SSAVAR) \
  66. for ((STMT) = first_imm_use_stmt (&(ITER), (SSAVAR)); \
  67. !end_imm_use_stmt_p (&(ITER)); \
  68. (void) ((STMT) = next_imm_use_stmt (&(ITER))))
  69. /* Use this to terminate the FOR_EACH_IMM_USE_STMT loop early. Failure to
  70. do so will result in leaving a iterator marker node in the immediate
  71. use list, and nothing good will come from that. */
  72. #define BREAK_FROM_IMM_USE_STMT(ITER) \
  73. { \
  74. end_imm_use_stmt_traverse (&(ITER)); \
  75. break; \
  76. }
  77. /* Use this iterator in combination with FOR_EACH_IMM_USE_STMT to
  78. get access to each occurrence of ssavar on the stmt returned by
  79. that iterator.. for instance:
  80. FOR_EACH_IMM_USE_STMT (stmt, iter, ssavar)
  81. {
  82. FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
  83. {
  84. SET_USE (use_p, blah);
  85. }
  86. update_stmt (stmt);
  87. } */
  88. #define FOR_EACH_IMM_USE_ON_STMT(DEST, ITER) \
  89. for ((DEST) = first_imm_use_on_stmt (&(ITER)); \
  90. !end_imm_use_on_stmt_p (&(ITER)); \
  91. (void) ((DEST) = next_imm_use_on_stmt (&(ITER))))
  92. extern bool has_zero_uses_1 (const ssa_use_operand_t *head);
  93. extern bool single_imm_use_1 (const ssa_use_operand_t *head,
  94. use_operand_p *use_p, gimple *stmt);
  95. enum ssa_op_iter_type {
  96. ssa_op_iter_none = 0,
  97. ssa_op_iter_tree,
  98. ssa_op_iter_use,
  99. ssa_op_iter_def
  100. };
  101. /* This structure is used in the operand iterator loops. It contains the
  102. items required to determine which operand is retrieved next. During
  103. optimization, this structure is scalarized, and any unused fields are
  104. optimized away, resulting in little overhead. */
  105. struct ssa_op_iter
  106. {
  107. enum ssa_op_iter_type iter_type;
  108. bool done;
  109. int flags;
  110. unsigned i;
  111. unsigned numops;
  112. use_optype_p uses;
  113. gimple stmt;
  114. };
  115. /* NOTE: Keep these in sync with doc/tree-ssa.texi. */
  116. /* These flags are used to determine which operands are returned during
  117. execution of the loop. */
  118. #define SSA_OP_USE 0x01 /* Real USE operands. */
  119. #define SSA_OP_DEF 0x02 /* Real DEF operands. */
  120. #define SSA_OP_VUSE 0x04 /* VUSE operands. */
  121. #define SSA_OP_VDEF 0x08 /* VDEF operands. */
  122. /* These are commonly grouped operand flags. */
  123. #define SSA_OP_VIRTUAL_USES (SSA_OP_VUSE)
  124. #define SSA_OP_VIRTUAL_DEFS (SSA_OP_VDEF)
  125. #define SSA_OP_ALL_VIRTUALS (SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_DEFS)
  126. #define SSA_OP_ALL_USES (SSA_OP_VIRTUAL_USES | SSA_OP_USE)
  127. #define SSA_OP_ALL_DEFS (SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF)
  128. #define SSA_OP_ALL_OPERANDS (SSA_OP_ALL_USES | SSA_OP_ALL_DEFS)
  129. /* This macro executes a loop over the operands of STMT specified in FLAG,
  130. returning each operand as a 'tree' in the variable TREEVAR. ITER is an
  131. ssa_op_iter structure used to control the loop. */
  132. #define FOR_EACH_SSA_TREE_OPERAND(TREEVAR, STMT, ITER, FLAGS) \
  133. for (TREEVAR = op_iter_init_tree (&(ITER), STMT, FLAGS); \
  134. !op_iter_done (&(ITER)); \
  135. (void) (TREEVAR = op_iter_next_tree (&(ITER))))
  136. /* This macro executes a loop over the operands of STMT specified in FLAG,
  137. returning each operand as a 'use_operand_p' in the variable USEVAR.
  138. ITER is an ssa_op_iter structure used to control the loop. */
  139. #define FOR_EACH_SSA_USE_OPERAND(USEVAR, STMT, ITER, FLAGS) \
  140. for (USEVAR = op_iter_init_use (&(ITER), STMT, FLAGS); \
  141. !op_iter_done (&(ITER)); \
  142. USEVAR = op_iter_next_use (&(ITER)))
  143. /* This macro executes a loop over the operands of STMT specified in FLAG,
  144. returning each operand as a 'def_operand_p' in the variable DEFVAR.
  145. ITER is an ssa_op_iter structure used to control the loop. */
  146. #define FOR_EACH_SSA_DEF_OPERAND(DEFVAR, STMT, ITER, FLAGS) \
  147. for (DEFVAR = op_iter_init_def (&(ITER), STMT, FLAGS); \
  148. !op_iter_done (&(ITER)); \
  149. DEFVAR = op_iter_next_def (&(ITER)))
  150. /* This macro will execute a loop over all the arguments of a PHI which
  151. match FLAGS. A use_operand_p is always returned via USEVAR. FLAGS
  152. can be either SSA_OP_USE or SSA_OP_VIRTUAL_USES or SSA_OP_ALL_USES. */
  153. #define FOR_EACH_PHI_ARG(USEVAR, STMT, ITER, FLAGS) \
  154. for ((USEVAR) = op_iter_init_phiuse (&(ITER), STMT, FLAGS); \
  155. !op_iter_done (&(ITER)); \
  156. (USEVAR) = op_iter_next_use (&(ITER)))
  157. /* This macro will execute a loop over a stmt, regardless of whether it is
  158. a real stmt or a PHI node, looking at the USE nodes matching FLAGS. */
  159. #define FOR_EACH_PHI_OR_STMT_USE(USEVAR, STMT, ITER, FLAGS) \
  160. for ((USEVAR) = (gimple_code (STMT) == GIMPLE_PHI \
  161. ? op_iter_init_phiuse (&(ITER), \
  162. as_a <gphi *> (STMT), \
  163. FLAGS) \
  164. : op_iter_init_use (&(ITER), STMT, FLAGS)); \
  165. !op_iter_done (&(ITER)); \
  166. (USEVAR) = op_iter_next_use (&(ITER)))
  167. /* This macro will execute a loop over a stmt, regardless of whether it is
  168. a real stmt or a PHI node, looking at the DEF nodes matching FLAGS. */
  169. #define FOR_EACH_PHI_OR_STMT_DEF(DEFVAR, STMT, ITER, FLAGS) \
  170. for ((DEFVAR) = (gimple_code (STMT) == GIMPLE_PHI \
  171. ? op_iter_init_phidef (&(ITER), \
  172. as_a <gphi *> (STMT), \
  173. FLAGS) \
  174. : op_iter_init_def (&(ITER), STMT, FLAGS)); \
  175. !op_iter_done (&(ITER)); \
  176. (DEFVAR) = op_iter_next_def (&(ITER)))
  177. /* This macro returns an operand in STMT as a tree if it is the ONLY
  178. operand matching FLAGS. If there are 0 or more than 1 operand matching
  179. FLAGS, then NULL_TREE is returned. */
  180. #define SINGLE_SSA_TREE_OPERAND(STMT, FLAGS) \
  181. single_ssa_tree_operand (STMT, FLAGS)
  182. /* This macro returns an operand in STMT as a use_operand_p if it is the ONLY
  183. operand matching FLAGS. If there are 0 or more than 1 operand matching
  184. FLAGS, then NULL_USE_OPERAND_P is returned. */
  185. #define SINGLE_SSA_USE_OPERAND(STMT, FLAGS) \
  186. single_ssa_use_operand (STMT, FLAGS)
  187. /* This macro returns an operand in STMT as a def_operand_p if it is the ONLY
  188. operand matching FLAGS. If there are 0 or more than 1 operand matching
  189. FLAGS, then NULL_DEF_OPERAND_P is returned. */
  190. #define SINGLE_SSA_DEF_OPERAND(STMT, FLAGS) \
  191. single_ssa_def_operand (STMT, FLAGS)
  192. /* This macro returns TRUE if there are no operands matching FLAGS in STMT. */
  193. #define ZERO_SSA_OPERANDS(STMT, FLAGS) zero_ssa_operands (STMT, FLAGS)
  194. /* This macro counts the number of operands in STMT matching FLAGS. */
  195. #define NUM_SSA_OPERANDS(STMT, FLAGS) num_ssa_operands (STMT, FLAGS)
  196. /* Delink an immediate_uses node from its chain. */
  197. static inline void
  198. delink_imm_use (ssa_use_operand_t *linknode)
  199. {
  200. /* Return if this node is not in a list. */
  201. if (linknode->prev == NULL)
  202. return;
  203. linknode->prev->next = linknode->next;
  204. linknode->next->prev = linknode->prev;
  205. linknode->prev = NULL;
  206. linknode->next = NULL;
  207. }
  208. /* Link ssa_imm_use node LINKNODE into the chain for LIST. */
  209. static inline void
  210. link_imm_use_to_list (ssa_use_operand_t *linknode, ssa_use_operand_t *list)
  211. {
  212. /* Link the new node at the head of the list. If we are in the process of
  213. traversing the list, we won't visit any new nodes added to it. */
  214. linknode->prev = list;
  215. linknode->next = list->next;
  216. list->next->prev = linknode;
  217. list->next = linknode;
  218. }
  219. /* Link ssa_imm_use node LINKNODE into the chain for DEF. */
  220. static inline void
  221. link_imm_use (ssa_use_operand_t *linknode, tree def)
  222. {
  223. ssa_use_operand_t *root;
  224. if (!def || TREE_CODE (def) != SSA_NAME)
  225. linknode->prev = NULL;
  226. else
  227. {
  228. root = &(SSA_NAME_IMM_USE_NODE (def));
  229. if (linknode->use)
  230. gcc_checking_assert (*(linknode->use) == def);
  231. link_imm_use_to_list (linknode, root);
  232. }
  233. }
  234. /* Set the value of a use pointed to by USE to VAL. */
  235. static inline void
  236. set_ssa_use_from_ptr (use_operand_p use, tree val)
  237. {
  238. delink_imm_use (use);
  239. *(use->use) = val;
  240. link_imm_use (use, val);
  241. }
  242. /* Link ssa_imm_use node LINKNODE into the chain for DEF, with use occurring
  243. in STMT. */
  244. static inline void
  245. link_imm_use_stmt (ssa_use_operand_t *linknode, tree def, gimple stmt)
  246. {
  247. if (stmt)
  248. link_imm_use (linknode, def);
  249. else
  250. link_imm_use (linknode, NULL);
  251. linknode->loc.stmt = stmt;
  252. }
  253. /* Relink a new node in place of an old node in the list. */
  254. static inline void
  255. relink_imm_use (ssa_use_operand_t *node, ssa_use_operand_t *old)
  256. {
  257. /* The node one had better be in the same list. */
  258. gcc_checking_assert (*(old->use) == *(node->use));
  259. node->prev = old->prev;
  260. node->next = old->next;
  261. if (old->prev)
  262. {
  263. old->prev->next = node;
  264. old->next->prev = node;
  265. /* Remove the old node from the list. */
  266. old->prev = NULL;
  267. }
  268. }
  269. /* Relink ssa_imm_use node LINKNODE into the chain for OLD, with use occurring
  270. in STMT. */
  271. static inline void
  272. relink_imm_use_stmt (ssa_use_operand_t *linknode, ssa_use_operand_t *old,
  273. gimple stmt)
  274. {
  275. if (stmt)
  276. relink_imm_use (linknode, old);
  277. else
  278. link_imm_use (linknode, NULL);
  279. linknode->loc.stmt = stmt;
  280. }
  281. /* Return true is IMM has reached the end of the immediate use list. */
  282. static inline bool
  283. end_readonly_imm_use_p (const imm_use_iterator *imm)
  284. {
  285. return (imm->imm_use == imm->end_p);
  286. }
  287. /* Initialize iterator IMM to process the list for VAR. */
  288. static inline use_operand_p
  289. first_readonly_imm_use (imm_use_iterator *imm, tree var)
  290. {
  291. imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
  292. imm->imm_use = imm->end_p->next;
  293. #ifdef ENABLE_CHECKING
  294. imm->iter_node.next = imm->imm_use->next;
  295. #endif
  296. if (end_readonly_imm_use_p (imm))
  297. return NULL_USE_OPERAND_P;
  298. return imm->imm_use;
  299. }
  300. /* Bump IMM to the next use in the list. */
  301. static inline use_operand_p
  302. next_readonly_imm_use (imm_use_iterator *imm)
  303. {
  304. use_operand_p old = imm->imm_use;
  305. #ifdef ENABLE_CHECKING
  306. /* If this assertion fails, it indicates the 'next' pointer has changed
  307. since the last bump. This indicates that the list is being modified
  308. via stmt changes, or SET_USE, or somesuch thing, and you need to be
  309. using the SAFE version of the iterator. */
  310. gcc_assert (imm->iter_node.next == old->next);
  311. imm->iter_node.next = old->next->next;
  312. #endif
  313. imm->imm_use = old->next;
  314. if (end_readonly_imm_use_p (imm))
  315. return NULL_USE_OPERAND_P;
  316. return imm->imm_use;
  317. }
  318. /* Return true if VAR has no nondebug uses. */
  319. static inline bool
  320. has_zero_uses (const_tree var)
  321. {
  322. const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
  323. /* A single use_operand means there is no items in the list. */
  324. if (ptr == ptr->next)
  325. return true;
  326. /* If there are debug stmts, we have to look at each use and see
  327. whether there are any nondebug uses. */
  328. if (!MAY_HAVE_DEBUG_STMTS)
  329. return false;
  330. return has_zero_uses_1 (ptr);
  331. }
  332. /* Return true if VAR has a single nondebug use. */
  333. static inline bool
  334. has_single_use (const_tree var)
  335. {
  336. const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
  337. /* If there aren't any uses whatsoever, we're done. */
  338. if (ptr == ptr->next)
  339. return false;
  340. /* If there's a single use, check that it's not a debug stmt. */
  341. if (ptr == ptr->next->next)
  342. return !is_gimple_debug (USE_STMT (ptr->next));
  343. /* If there are debug stmts, we have to look at each of them. */
  344. if (!MAY_HAVE_DEBUG_STMTS)
  345. return false;
  346. return single_imm_use_1 (ptr, NULL, NULL);
  347. }
  348. /* If VAR has only a single immediate nondebug use, return true, and
  349. set USE_P and STMT to the use pointer and stmt of occurrence. */
  350. static inline bool
  351. single_imm_use (const_tree var, use_operand_p *use_p, gimple *stmt)
  352. {
  353. const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
  354. /* If there aren't any uses whatsoever, we're done. */
  355. if (ptr == ptr->next)
  356. {
  357. return_false:
  358. *use_p = NULL_USE_OPERAND_P;
  359. *stmt = NULL;
  360. return false;
  361. }
  362. /* If there's a single use, check that it's not a debug stmt. */
  363. if (ptr == ptr->next->next)
  364. {
  365. if (!is_gimple_debug (USE_STMT (ptr->next)))
  366. {
  367. *use_p = ptr->next;
  368. *stmt = ptr->next->loc.stmt;
  369. return true;
  370. }
  371. else
  372. goto return_false;
  373. }
  374. /* If there are debug stmts, we have to look at each of them. */
  375. if (!MAY_HAVE_DEBUG_STMTS)
  376. goto return_false;
  377. return single_imm_use_1 (ptr, use_p, stmt);
  378. }
  379. /* Return the number of nondebug immediate uses of VAR. */
  380. static inline unsigned int
  381. num_imm_uses (const_tree var)
  382. {
  383. const ssa_use_operand_t *const start = &(SSA_NAME_IMM_USE_NODE (var));
  384. const ssa_use_operand_t *ptr;
  385. unsigned int num = 0;
  386. if (!MAY_HAVE_DEBUG_STMTS)
  387. for (ptr = start->next; ptr != start; ptr = ptr->next)
  388. num++;
  389. else
  390. for (ptr = start->next; ptr != start; ptr = ptr->next)
  391. if (!is_gimple_debug (USE_STMT (ptr)))
  392. num++;
  393. return num;
  394. }
  395. /* ----------------------------------------------------------------------- */
  396. /* The following set of routines are used to iterator over various type of
  397. SSA operands. */
  398. /* Return true if PTR is finished iterating. */
  399. static inline bool
  400. op_iter_done (const ssa_op_iter *ptr)
  401. {
  402. return ptr->done;
  403. }
  404. /* Get the next iterator use value for PTR. */
  405. static inline use_operand_p
  406. op_iter_next_use (ssa_op_iter *ptr)
  407. {
  408. use_operand_p use_p;
  409. gcc_checking_assert (ptr->iter_type == ssa_op_iter_use);
  410. if (ptr->uses)
  411. {
  412. use_p = USE_OP_PTR (ptr->uses);
  413. ptr->uses = ptr->uses->next;
  414. return use_p;
  415. }
  416. if (ptr->i < ptr->numops)
  417. {
  418. return PHI_ARG_DEF_PTR (ptr->stmt, (ptr->i)++);
  419. }
  420. ptr->done = true;
  421. return NULL_USE_OPERAND_P;
  422. }
  423. /* Get the next iterator def value for PTR. */
  424. static inline def_operand_p
  425. op_iter_next_def (ssa_op_iter *ptr)
  426. {
  427. gcc_checking_assert (ptr->iter_type == ssa_op_iter_def);
  428. if (ptr->flags & SSA_OP_VDEF)
  429. {
  430. tree *p;
  431. ptr->flags &= ~SSA_OP_VDEF;
  432. p = gimple_vdef_ptr (ptr->stmt);
  433. if (p && *p)
  434. return p;
  435. }
  436. if (ptr->flags & SSA_OP_DEF)
  437. {
  438. while (ptr->i < ptr->numops)
  439. {
  440. tree *val = gimple_op_ptr (ptr->stmt, ptr->i);
  441. ptr->i++;
  442. if (*val)
  443. {
  444. if (TREE_CODE (*val) == TREE_LIST)
  445. val = &TREE_VALUE (*val);
  446. if (TREE_CODE (*val) == SSA_NAME
  447. || is_gimple_reg (*val))
  448. return val;
  449. }
  450. }
  451. ptr->flags &= ~SSA_OP_DEF;
  452. }
  453. ptr->done = true;
  454. return NULL_DEF_OPERAND_P;
  455. }
  456. /* Get the next iterator tree value for PTR. */
  457. static inline tree
  458. op_iter_next_tree (ssa_op_iter *ptr)
  459. {
  460. tree val;
  461. gcc_checking_assert (ptr->iter_type == ssa_op_iter_tree);
  462. if (ptr->uses)
  463. {
  464. val = USE_OP (ptr->uses);
  465. ptr->uses = ptr->uses->next;
  466. return val;
  467. }
  468. if (ptr->flags & SSA_OP_VDEF)
  469. {
  470. ptr->flags &= ~SSA_OP_VDEF;
  471. if ((val = gimple_vdef (ptr->stmt)))
  472. return val;
  473. }
  474. if (ptr->flags & SSA_OP_DEF)
  475. {
  476. while (ptr->i < ptr->numops)
  477. {
  478. val = gimple_op (ptr->stmt, ptr->i);
  479. ptr->i++;
  480. if (val)
  481. {
  482. if (TREE_CODE (val) == TREE_LIST)
  483. val = TREE_VALUE (val);
  484. if (TREE_CODE (val) == SSA_NAME
  485. || is_gimple_reg (val))
  486. return val;
  487. }
  488. }
  489. ptr->flags &= ~SSA_OP_DEF;
  490. }
  491. ptr->done = true;
  492. return NULL_TREE;
  493. }
  494. /* This functions clears the iterator PTR, and marks it done. This is normally
  495. used to prevent warnings in the compile about might be uninitialized
  496. components. */
  497. static inline void
  498. clear_and_done_ssa_iter (ssa_op_iter *ptr)
  499. {
  500. ptr->i = 0;
  501. ptr->numops = 0;
  502. ptr->uses = NULL;
  503. ptr->iter_type = ssa_op_iter_none;
  504. ptr->stmt = NULL;
  505. ptr->done = true;
  506. ptr->flags = 0;
  507. }
  508. /* Initialize the iterator PTR to the virtual defs in STMT. */
  509. static inline void
  510. op_iter_init (ssa_op_iter *ptr, gimple stmt, int flags)
  511. {
  512. /* PHI nodes require a different iterator initialization path. We
  513. do not support iterating over virtual defs or uses without
  514. iterating over defs or uses at the same time. */
  515. gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI
  516. && (!(flags & SSA_OP_VDEF) || (flags & SSA_OP_DEF))
  517. && (!(flags & SSA_OP_VUSE) || (flags & SSA_OP_USE)));
  518. ptr->numops = 0;
  519. if (flags & (SSA_OP_DEF | SSA_OP_VDEF))
  520. {
  521. switch (gimple_code (stmt))
  522. {
  523. case GIMPLE_ASSIGN:
  524. case GIMPLE_CALL:
  525. ptr->numops = 1;
  526. break;
  527. case GIMPLE_ASM:
  528. ptr->numops = gimple_asm_noutputs (as_a <gasm *> (stmt));
  529. break;
  530. default:
  531. ptr->numops = 0;
  532. flags &= ~(SSA_OP_DEF | SSA_OP_VDEF);
  533. break;
  534. }
  535. }
  536. ptr->uses = (flags & (SSA_OP_USE|SSA_OP_VUSE)) ? gimple_use_ops (stmt) : NULL;
  537. if (!(flags & SSA_OP_VUSE)
  538. && ptr->uses
  539. && gimple_vuse (stmt) != NULL_TREE)
  540. ptr->uses = ptr->uses->next;
  541. ptr->done = false;
  542. ptr->i = 0;
  543. ptr->stmt = stmt;
  544. ptr->flags = flags;
  545. }
  546. /* Initialize iterator PTR to the use operands in STMT based on FLAGS. Return
  547. the first use. */
  548. static inline use_operand_p
  549. op_iter_init_use (ssa_op_iter *ptr, gimple stmt, int flags)
  550. {
  551. gcc_checking_assert ((flags & SSA_OP_ALL_DEFS) == 0
  552. && (flags & SSA_OP_USE));
  553. op_iter_init (ptr, stmt, flags);
  554. ptr->iter_type = ssa_op_iter_use;
  555. return op_iter_next_use (ptr);
  556. }
  557. /* Initialize iterator PTR to the def operands in STMT based on FLAGS. Return
  558. the first def. */
  559. static inline def_operand_p
  560. op_iter_init_def (ssa_op_iter *ptr, gimple stmt, int flags)
  561. {
  562. gcc_checking_assert ((flags & SSA_OP_ALL_USES) == 0
  563. && (flags & SSA_OP_DEF));
  564. op_iter_init (ptr, stmt, flags);
  565. ptr->iter_type = ssa_op_iter_def;
  566. return op_iter_next_def (ptr);
  567. }
  568. /* Initialize iterator PTR to the operands in STMT based on FLAGS. Return
  569. the first operand as a tree. */
  570. static inline tree
  571. op_iter_init_tree (ssa_op_iter *ptr, gimple stmt, int flags)
  572. {
  573. op_iter_init (ptr, stmt, flags);
  574. ptr->iter_type = ssa_op_iter_tree;
  575. return op_iter_next_tree (ptr);
  576. }
  577. /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
  578. return NULL. */
  579. static inline tree
  580. single_ssa_tree_operand (gimple stmt, int flags)
  581. {
  582. tree var;
  583. ssa_op_iter iter;
  584. var = op_iter_init_tree (&iter, stmt, flags);
  585. if (op_iter_done (&iter))
  586. return NULL_TREE;
  587. op_iter_next_tree (&iter);
  588. if (op_iter_done (&iter))
  589. return var;
  590. return NULL_TREE;
  591. }
  592. /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
  593. return NULL. */
  594. static inline use_operand_p
  595. single_ssa_use_operand (gimple stmt, int flags)
  596. {
  597. use_operand_p var;
  598. ssa_op_iter iter;
  599. var = op_iter_init_use (&iter, stmt, flags);
  600. if (op_iter_done (&iter))
  601. return NULL_USE_OPERAND_P;
  602. op_iter_next_use (&iter);
  603. if (op_iter_done (&iter))
  604. return var;
  605. return NULL_USE_OPERAND_P;
  606. }
  607. /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
  608. return NULL. */
  609. static inline def_operand_p
  610. single_ssa_def_operand (gimple stmt, int flags)
  611. {
  612. def_operand_p var;
  613. ssa_op_iter iter;
  614. var = op_iter_init_def (&iter, stmt, flags);
  615. if (op_iter_done (&iter))
  616. return NULL_DEF_OPERAND_P;
  617. op_iter_next_def (&iter);
  618. if (op_iter_done (&iter))
  619. return var;
  620. return NULL_DEF_OPERAND_P;
  621. }
  622. /* Return true if there are zero operands in STMT matching the type
  623. given in FLAGS. */
  624. static inline bool
  625. zero_ssa_operands (gimple stmt, int flags)
  626. {
  627. ssa_op_iter iter;
  628. op_iter_init_tree (&iter, stmt, flags);
  629. return op_iter_done (&iter);
  630. }
  631. /* Return the number of operands matching FLAGS in STMT. */
  632. static inline int
  633. num_ssa_operands (gimple stmt, int flags)
  634. {
  635. ssa_op_iter iter;
  636. tree t;
  637. int num = 0;
  638. gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI);
  639. FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, flags)
  640. num++;
  641. return num;
  642. }
  643. /* If there is a single DEF in the PHI node which matches FLAG, return it.
  644. Otherwise return NULL_DEF_OPERAND_P. */
  645. static inline tree
  646. single_phi_def (gphi *stmt, int flags)
  647. {
  648. tree def = PHI_RESULT (stmt);
  649. if ((flags & SSA_OP_DEF) && is_gimple_reg (def))
  650. return def;
  651. if ((flags & SSA_OP_VIRTUAL_DEFS) && !is_gimple_reg (def))
  652. return def;
  653. return NULL_TREE;
  654. }
  655. /* Initialize the iterator PTR for uses matching FLAGS in PHI. FLAGS should
  656. be either SSA_OP_USES or SSA_OP_VIRTUAL_USES. */
  657. static inline use_operand_p
  658. op_iter_init_phiuse (ssa_op_iter *ptr, gphi *phi, int flags)
  659. {
  660. tree phi_def = gimple_phi_result (phi);
  661. int comp;
  662. clear_and_done_ssa_iter (ptr);
  663. ptr->done = false;
  664. gcc_checking_assert ((flags & (SSA_OP_USE | SSA_OP_VIRTUAL_USES)) != 0);
  665. comp = (is_gimple_reg (phi_def) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
  666. /* If the PHI node doesn't the operand type we care about, we're done. */
  667. if ((flags & comp) == 0)
  668. {
  669. ptr->done = true;
  670. return NULL_USE_OPERAND_P;
  671. }
  672. ptr->stmt = phi;
  673. ptr->numops = gimple_phi_num_args (phi);
  674. ptr->iter_type = ssa_op_iter_use;
  675. ptr->flags = flags;
  676. return op_iter_next_use (ptr);
  677. }
  678. /* Start an iterator for a PHI definition. */
  679. static inline def_operand_p
  680. op_iter_init_phidef (ssa_op_iter *ptr, gphi *phi, int flags)
  681. {
  682. tree phi_def = PHI_RESULT (phi);
  683. int comp;
  684. clear_and_done_ssa_iter (ptr);
  685. ptr->done = false;
  686. gcc_checking_assert ((flags & (SSA_OP_DEF | SSA_OP_VIRTUAL_DEFS)) != 0);
  687. comp = (is_gimple_reg (phi_def) ? SSA_OP_DEF : SSA_OP_VIRTUAL_DEFS);
  688. /* If the PHI node doesn't have the operand type we care about,
  689. we're done. */
  690. if ((flags & comp) == 0)
  691. {
  692. ptr->done = true;
  693. return NULL_DEF_OPERAND_P;
  694. }
  695. ptr->iter_type = ssa_op_iter_def;
  696. /* The first call to op_iter_next_def will terminate the iterator since
  697. all the fields are NULL. Simply return the result here as the first and
  698. therefore only result. */
  699. return PHI_RESULT_PTR (phi);
  700. }
  701. /* Return true is IMM has reached the end of the immediate use stmt list. */
  702. static inline bool
  703. end_imm_use_stmt_p (const imm_use_iterator *imm)
  704. {
  705. return (imm->imm_use == imm->end_p);
  706. }
  707. /* Finished the traverse of an immediate use stmt list IMM by removing the
  708. placeholder node from the list. */
  709. static inline void
  710. end_imm_use_stmt_traverse (imm_use_iterator *imm)
  711. {
  712. delink_imm_use (&(imm->iter_node));
  713. }
  714. /* Immediate use traversal of uses within a stmt require that all the
  715. uses on a stmt be sequentially listed. This routine is used to build up
  716. this sequential list by adding USE_P to the end of the current list
  717. currently delimited by HEAD and LAST_P. The new LAST_P value is
  718. returned. */
  719. static inline use_operand_p
  720. move_use_after_head (use_operand_p use_p, use_operand_p head,
  721. use_operand_p last_p)
  722. {
  723. gcc_checking_assert (USE_FROM_PTR (use_p) == USE_FROM_PTR (head));
  724. /* Skip head when we find it. */
  725. if (use_p != head)
  726. {
  727. /* If use_p is already linked in after last_p, continue. */
  728. if (last_p->next == use_p)
  729. last_p = use_p;
  730. else
  731. {
  732. /* Delink from current location, and link in at last_p. */
  733. delink_imm_use (use_p);
  734. link_imm_use_to_list (use_p, last_p);
  735. last_p = use_p;
  736. }
  737. }
  738. return last_p;
  739. }
  740. /* This routine will relink all uses with the same stmt as HEAD into the list
  741. immediately following HEAD for iterator IMM. */
  742. static inline void
  743. link_use_stmts_after (use_operand_p head, imm_use_iterator *imm)
  744. {
  745. use_operand_p use_p;
  746. use_operand_p last_p = head;
  747. gimple head_stmt = USE_STMT (head);
  748. tree use = USE_FROM_PTR (head);
  749. ssa_op_iter op_iter;
  750. int flag;
  751. /* Only look at virtual or real uses, depending on the type of HEAD. */
  752. flag = (is_gimple_reg (use) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
  753. if (gphi *phi = dyn_cast <gphi *> (head_stmt))
  754. {
  755. FOR_EACH_PHI_ARG (use_p, phi, op_iter, flag)
  756. if (USE_FROM_PTR (use_p) == use)
  757. last_p = move_use_after_head (use_p, head, last_p);
  758. }
  759. else
  760. {
  761. if (flag == SSA_OP_USE)
  762. {
  763. FOR_EACH_SSA_USE_OPERAND (use_p, head_stmt, op_iter, flag)
  764. if (USE_FROM_PTR (use_p) == use)
  765. last_p = move_use_after_head (use_p, head, last_p);
  766. }
  767. else if ((use_p = gimple_vuse_op (head_stmt)) != NULL_USE_OPERAND_P)
  768. {
  769. if (USE_FROM_PTR (use_p) == use)
  770. last_p = move_use_after_head (use_p, head, last_p);
  771. }
  772. }
  773. /* Link iter node in after last_p. */
  774. if (imm->iter_node.prev != NULL)
  775. delink_imm_use (&imm->iter_node);
  776. link_imm_use_to_list (&(imm->iter_node), last_p);
  777. }
  778. /* Initialize IMM to traverse over uses of VAR. Return the first statement. */
  779. static inline gimple
  780. first_imm_use_stmt (imm_use_iterator *imm, tree var)
  781. {
  782. imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
  783. imm->imm_use = imm->end_p->next;
  784. imm->next_imm_name = NULL_USE_OPERAND_P;
  785. /* iter_node is used as a marker within the immediate use list to indicate
  786. where the end of the current stmt's uses are. Initialize it to NULL
  787. stmt and use, which indicates a marker node. */
  788. imm->iter_node.prev = NULL_USE_OPERAND_P;
  789. imm->iter_node.next = NULL_USE_OPERAND_P;
  790. imm->iter_node.loc.stmt = NULL;
  791. imm->iter_node.use = NULL;
  792. if (end_imm_use_stmt_p (imm))
  793. return NULL;
  794. link_use_stmts_after (imm->imm_use, imm);
  795. return USE_STMT (imm->imm_use);
  796. }
  797. /* Bump IMM to the next stmt which has a use of var. */
  798. static inline gimple
  799. next_imm_use_stmt (imm_use_iterator *imm)
  800. {
  801. imm->imm_use = imm->iter_node.next;
  802. if (end_imm_use_stmt_p (imm))
  803. {
  804. if (imm->iter_node.prev != NULL)
  805. delink_imm_use (&imm->iter_node);
  806. return NULL;
  807. }
  808. link_use_stmts_after (imm->imm_use, imm);
  809. return USE_STMT (imm->imm_use);
  810. }
  811. /* This routine will return the first use on the stmt IMM currently refers
  812. to. */
  813. static inline use_operand_p
  814. first_imm_use_on_stmt (imm_use_iterator *imm)
  815. {
  816. imm->next_imm_name = imm->imm_use->next;
  817. return imm->imm_use;
  818. }
  819. /* Return TRUE if the last use on the stmt IMM refers to has been visited. */
  820. static inline bool
  821. end_imm_use_on_stmt_p (const imm_use_iterator *imm)
  822. {
  823. return (imm->imm_use == &(imm->iter_node));
  824. }
  825. /* Bump to the next use on the stmt IMM refers to, return NULL if done. */
  826. static inline use_operand_p
  827. next_imm_use_on_stmt (imm_use_iterator *imm)
  828. {
  829. imm->imm_use = imm->next_imm_name;
  830. if (end_imm_use_on_stmt_p (imm))
  831. return NULL_USE_OPERAND_P;
  832. else
  833. {
  834. imm->next_imm_name = imm->imm_use->next;
  835. return imm->imm_use;
  836. }
  837. }
  838. /* Delink all immediate_use information for STMT. */
  839. static inline void
  840. delink_stmt_imm_use (gimple stmt)
  841. {
  842. ssa_op_iter iter;
  843. use_operand_p use_p;
  844. if (ssa_operands_active (cfun))
  845. FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_ALL_USES)
  846. delink_imm_use (use_p);
  847. }
  848. #endif /* GCC_TREE_SSA_ITERATORS_H */