ipa-icf-gimple.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /* Interprocedural Identical Code Folding pass
  2. Copyright (C) 2014-2015 Free Software Foundation, Inc.
  3. Contributed by Jan Hubicka <hubicka@ucw.cz> and Martin Liska <mliska@suse.cz>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. 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 "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "options.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "fold-const.h"
  31. #include "predict.h"
  32. #include "tm.h"
  33. #include "hard-reg-set.h"
  34. #include "function.h"
  35. #include "basic-block.h"
  36. #include "tree-ssa-alias.h"
  37. #include "internal-fn.h"
  38. #include "gimple-expr.h"
  39. #include "is-a.h"
  40. #include "gimple.h"
  41. #include "hashtab.h"
  42. #include "rtl.h"
  43. #include "flags.h"
  44. #include "statistics.h"
  45. #include "real.h"
  46. #include "fixed-value.h"
  47. #include "insn-config.h"
  48. #include "expmed.h"
  49. #include "dojump.h"
  50. #include "explow.h"
  51. #include "calls.h"
  52. #include "emit-rtl.h"
  53. #include "varasm.h"
  54. #include "stmt.h"
  55. #include "expr.h"
  56. #include "gimple-iterator.h"
  57. #include "gimple-ssa.h"
  58. #include "tree-cfg.h"
  59. #include "stringpool.h"
  60. #include "tree-dfa.h"
  61. #include "tree-pass.h"
  62. #include "gimple-pretty-print.h"
  63. #include "cfgloop.h"
  64. #include "except.h"
  65. #include "hash-map.h"
  66. #include "plugin-api.h"
  67. #include "ipa-ref.h"
  68. #include "cgraph.h"
  69. #include "data-streamer.h"
  70. #include "ipa-utils.h"
  71. #include <list>
  72. #include "tree-ssanames.h"
  73. #include "tree-eh.h"
  74. #include "builtins.h"
  75. #include "ipa-icf-gimple.h"
  76. #include "ipa-icf.h"
  77. namespace ipa_icf_gimple {
  78. /* Initialize internal structures for a given SOURCE_FUNC_DECL and
  79. TARGET_FUNC_DECL. Strict polymorphic comparison is processed if
  80. an option COMPARE_POLYMORPHIC is true. For special cases, one can
  81. set IGNORE_LABELS to skip label comparison.
  82. Similarly, IGNORE_SOURCE_DECLS and IGNORE_TARGET_DECLS are sets
  83. of declarations that can be skipped. */
  84. func_checker::func_checker (tree source_func_decl, tree target_func_decl,
  85. bool compare_polymorphic,
  86. bool ignore_labels,
  87. hash_set<symtab_node *> *ignored_source_nodes,
  88. hash_set<symtab_node *> *ignored_target_nodes)
  89. : m_source_func_decl (source_func_decl), m_target_func_decl (target_func_decl),
  90. m_ignored_source_nodes (ignored_source_nodes),
  91. m_ignored_target_nodes (ignored_target_nodes),
  92. m_compare_polymorphic (compare_polymorphic),
  93. m_ignore_labels (ignore_labels)
  94. {
  95. function *source_func = DECL_STRUCT_FUNCTION (source_func_decl);
  96. function *target_func = DECL_STRUCT_FUNCTION (target_func_decl);
  97. unsigned ssa_source = SSANAMES (source_func)->length ();
  98. unsigned ssa_target = SSANAMES (target_func)->length ();
  99. m_source_ssa_names.create (ssa_source);
  100. m_target_ssa_names.create (ssa_target);
  101. for (unsigned i = 0; i < ssa_source; i++)
  102. m_source_ssa_names.safe_push (-1);
  103. for (unsigned i = 0; i < ssa_target; i++)
  104. m_target_ssa_names.safe_push (-1);
  105. }
  106. /* Memory release routine. */
  107. func_checker::~func_checker ()
  108. {
  109. m_source_ssa_names.release();
  110. m_target_ssa_names.release();
  111. }
  112. /* Verifies that trees T1 and T2 are equivalent from perspective of ICF. */
  113. bool
  114. func_checker::compare_ssa_name (tree t1, tree t2)
  115. {
  116. gcc_assert (TREE_CODE (t1) == SSA_NAME);
  117. gcc_assert (TREE_CODE (t2) == SSA_NAME);
  118. unsigned i1 = SSA_NAME_VERSION (t1);
  119. unsigned i2 = SSA_NAME_VERSION (t2);
  120. if (m_source_ssa_names[i1] == -1)
  121. m_source_ssa_names[i1] = i2;
  122. else if (m_source_ssa_names[i1] != (int) i2)
  123. return false;
  124. if(m_target_ssa_names[i2] == -1)
  125. m_target_ssa_names[i2] = i1;
  126. else if (m_target_ssa_names[i2] != (int) i1)
  127. return false;
  128. if (SSA_NAME_IS_DEFAULT_DEF (t1))
  129. {
  130. tree b1 = SSA_NAME_VAR (t1);
  131. tree b2 = SSA_NAME_VAR (t2);
  132. if (b1 == NULL && b2 == NULL)
  133. return true;
  134. if (b1 == NULL || b2 == NULL || TREE_CODE (b1) != TREE_CODE (b2))
  135. return return_false ();
  136. return compare_cst_or_decl (b1, b2);
  137. }
  138. return true;
  139. }
  140. /* Verification function for edges E1 and E2. */
  141. bool
  142. func_checker::compare_edge (edge e1, edge e2)
  143. {
  144. if (e1->flags != e2->flags)
  145. return false;
  146. bool existed_p;
  147. edge &slot = m_edge_map.get_or_insert (e1, &existed_p);
  148. if (existed_p)
  149. return return_with_debug (slot == e2);
  150. else
  151. slot = e2;
  152. /* TODO: filter edge probabilities for profile feedback match. */
  153. return true;
  154. }
  155. /* Verification function for declaration trees T1 and T2 that
  156. come from functions FUNC1 and FUNC2. */
  157. bool
  158. func_checker::compare_decl (tree t1, tree t2)
  159. {
  160. if (!auto_var_in_fn_p (t1, m_source_func_decl)
  161. || !auto_var_in_fn_p (t2, m_target_func_decl))
  162. return return_with_debug (t1 == t2);
  163. tree_code t = TREE_CODE (t1);
  164. if ((t == VAR_DECL || t == PARM_DECL || t == RESULT_DECL)
  165. && DECL_BY_REFERENCE (t1) != DECL_BY_REFERENCE (t2))
  166. return return_false_with_msg ("DECL_BY_REFERENCE flags are different");
  167. if (!compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2)))
  168. return return_false ();
  169. /* TODO: we are actually too strict here. We only need to compare if
  170. T1 can be used in polymorphic call. */
  171. if (TREE_ADDRESSABLE (t1)
  172. && m_compare_polymorphic
  173. && !compatible_polymorphic_types_p (TREE_TYPE (t1), TREE_TYPE (t2),
  174. false))
  175. return return_false ();
  176. if ((t == VAR_DECL || t == PARM_DECL || t == RESULT_DECL)
  177. && DECL_BY_REFERENCE (t1)
  178. && m_compare_polymorphic
  179. && !compatible_polymorphic_types_p (TREE_TYPE (t1), TREE_TYPE (t2),
  180. true))
  181. return return_false ();
  182. bool existed_p;
  183. tree &slot = m_decl_map.get_or_insert (t1, &existed_p);
  184. if (existed_p)
  185. return return_with_debug (slot == t2);
  186. else
  187. slot = t2;
  188. return true;
  189. }
  190. /* Return true if T1 and T2 are same for purposes of ipa-polymorphic-call
  191. analysis. COMPARE_PTR indicates if types of pointers needs to be
  192. considered. */
  193. bool
  194. func_checker::compatible_polymorphic_types_p (tree t1, tree t2,
  195. bool compare_ptr)
  196. {
  197. gcc_assert (TREE_CODE (t1) != FUNCTION_TYPE && TREE_CODE (t1) != METHOD_TYPE);
  198. /* Pointer types generally give no information. */
  199. if (POINTER_TYPE_P (t1))
  200. {
  201. if (!compare_ptr)
  202. return true;
  203. return func_checker::compatible_polymorphic_types_p (TREE_TYPE (t1),
  204. TREE_TYPE (t2),
  205. false);
  206. }
  207. /* If types contain a polymorphic types, match them. */
  208. bool c1 = contains_polymorphic_type_p (t1);
  209. bool c2 = contains_polymorphic_type_p (t2);
  210. if (!c1 && !c2)
  211. return true;
  212. if (!c1 || !c2)
  213. return return_false_with_msg ("one type is not polymorphic");
  214. if (!types_must_be_same_for_odr (t1, t2))
  215. return return_false_with_msg ("types are not same for ODR");
  216. return true;
  217. }
  218. /* Return true if types are compatible from perspective of ICF. */
  219. bool
  220. func_checker::compatible_types_p (tree t1, tree t2)
  221. {
  222. if (TREE_CODE (t1) != TREE_CODE (t2))
  223. return return_false_with_msg ("different tree types");
  224. if (TYPE_RESTRICT (t1) != TYPE_RESTRICT (t2))
  225. return return_false_with_msg ("restrict flags are different");
  226. if (!types_compatible_p (t1, t2))
  227. return return_false_with_msg ("types are not compatible");
  228. if (get_alias_set (t1) != get_alias_set (t2))
  229. return return_false_with_msg ("alias sets are different");
  230. return true;
  231. }
  232. /* Function compare for equality given memory operands T1 and T2. */
  233. bool
  234. func_checker::compare_memory_operand (tree t1, tree t2)
  235. {
  236. if (!t1 && !t2)
  237. return true;
  238. else if (!t1 || !t2)
  239. return false;
  240. ao_ref r1, r2;
  241. ao_ref_init (&r1, t1);
  242. ao_ref_init (&r2, t2);
  243. tree b1 = ao_ref_base (&r1);
  244. tree b2 = ao_ref_base (&r2);
  245. bool source_is_memop = DECL_P (b1) || INDIRECT_REF_P (b1)
  246. || TREE_CODE (b1) == MEM_REF
  247. || TREE_CODE (b1) == TARGET_MEM_REF;
  248. bool target_is_memop = DECL_P (b2) || INDIRECT_REF_P (b2)
  249. || TREE_CODE (b2) == MEM_REF
  250. || TREE_CODE (b2) == TARGET_MEM_REF;
  251. /* Compare alias sets for memory operands. */
  252. if (source_is_memop && target_is_memop)
  253. {
  254. if (TREE_THIS_VOLATILE (t1) != TREE_THIS_VOLATILE (t2))
  255. return return_false_with_msg ("different operand volatility");
  256. if (ao_ref_alias_set (&r1) != ao_ref_alias_set (&r2)
  257. || ao_ref_base_alias_set (&r1) != ao_ref_base_alias_set (&r2))
  258. return return_false_with_msg ("ao alias sets are different");
  259. /* We can't simply use get_object_alignment_1 on the full
  260. reference as for accesses with variable indexes this reports
  261. too conservative alignment. We also can't use the ao_ref_base
  262. base objects as ao_ref_base happily strips MEM_REFs around
  263. decls even though that may carry alignment info. */
  264. b1 = t1;
  265. while (handled_component_p (b1))
  266. b1 = TREE_OPERAND (b1, 0);
  267. b2 = t2;
  268. while (handled_component_p (b2))
  269. b2 = TREE_OPERAND (b2, 0);
  270. unsigned int align1, align2;
  271. unsigned HOST_WIDE_INT tem;
  272. get_object_alignment_1 (b1, &align1, &tem);
  273. get_object_alignment_1 (b2, &align2, &tem);
  274. if (align1 != align2)
  275. return return_false_with_msg ("different access alignment");
  276. /* Similarly we have to compare dependence info where equality
  277. tells us we are safe (even some unequal values would be safe
  278. but then we have to maintain a map of bases and cliques). */
  279. unsigned short clique1 = 0, base1 = 0, clique2 = 0, base2 = 0;
  280. if (TREE_CODE (b1) == MEM_REF)
  281. {
  282. clique1 = MR_DEPENDENCE_CLIQUE (b1);
  283. base1 = MR_DEPENDENCE_BASE (b1);
  284. }
  285. if (TREE_CODE (b2) == MEM_REF)
  286. {
  287. clique2 = MR_DEPENDENCE_CLIQUE (b2);
  288. base2 = MR_DEPENDENCE_BASE (b2);
  289. }
  290. if (clique1 != clique2 || base1 != base2)
  291. return return_false_with_msg ("different dependence info");
  292. }
  293. return compare_operand (t1, t2);
  294. }
  295. /* Function compare for equality given trees T1 and T2 which
  296. can be either a constant or a declaration type. */
  297. bool
  298. func_checker::compare_cst_or_decl (tree t1, tree t2)
  299. {
  300. bool ret;
  301. switch (TREE_CODE (t1))
  302. {
  303. case INTEGER_CST:
  304. case COMPLEX_CST:
  305. case VECTOR_CST:
  306. case STRING_CST:
  307. case REAL_CST:
  308. {
  309. ret = compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2))
  310. && operand_equal_p (t1, t2, OEP_ONLY_CONST);
  311. return return_with_debug (ret);
  312. }
  313. case FUNCTION_DECL:
  314. /* All function decls are in the symbol table and known to match
  315. before we start comparing bodies. */
  316. return true;
  317. case VAR_DECL:
  318. return return_with_debug (compare_variable_decl (t1, t2));
  319. case FIELD_DECL:
  320. {
  321. tree offset1 = DECL_FIELD_OFFSET (t1);
  322. tree offset2 = DECL_FIELD_OFFSET (t2);
  323. tree bit_offset1 = DECL_FIELD_BIT_OFFSET (t1);
  324. tree bit_offset2 = DECL_FIELD_BIT_OFFSET (t2);
  325. ret = compare_operand (offset1, offset2)
  326. && compare_operand (bit_offset1, bit_offset2);
  327. return return_with_debug (ret);
  328. }
  329. case LABEL_DECL:
  330. {
  331. int *bb1 = m_label_bb_map.get (t1);
  332. int *bb2 = m_label_bb_map.get (t2);
  333. return return_with_debug (*bb1 == *bb2);
  334. }
  335. case PARM_DECL:
  336. case RESULT_DECL:
  337. case CONST_DECL:
  338. {
  339. ret = compare_decl (t1, t2);
  340. return return_with_debug (ret);
  341. }
  342. default:
  343. gcc_unreachable ();
  344. }
  345. }
  346. /* Function responsible for comparison of various operands T1 and T2.
  347. If these components, from functions FUNC1 and FUNC2, are equal, true
  348. is returned. */
  349. bool
  350. func_checker::compare_operand (tree t1, tree t2)
  351. {
  352. tree x1, x2, y1, y2, z1, z2;
  353. bool ret;
  354. if (!t1 && !t2)
  355. return true;
  356. else if (!t1 || !t2)
  357. return false;
  358. tree tt1 = TREE_TYPE (t1);
  359. tree tt2 = TREE_TYPE (t2);
  360. if (!func_checker::compatible_types_p (tt1, tt2))
  361. return false;
  362. if (TREE_CODE (t1) != TREE_CODE (t2))
  363. return return_false ();
  364. switch (TREE_CODE (t1))
  365. {
  366. case CONSTRUCTOR:
  367. {
  368. unsigned length1 = vec_safe_length (CONSTRUCTOR_ELTS (t1));
  369. unsigned length2 = vec_safe_length (CONSTRUCTOR_ELTS (t2));
  370. if (length1 != length2)
  371. return return_false ();
  372. for (unsigned i = 0; i < length1; i++)
  373. if (!compare_operand (CONSTRUCTOR_ELT (t1, i)->value,
  374. CONSTRUCTOR_ELT (t2, i)->value))
  375. return return_false();
  376. return true;
  377. }
  378. case ARRAY_REF:
  379. case ARRAY_RANGE_REF:
  380. /* First argument is the array, second is the index. */
  381. x1 = TREE_OPERAND (t1, 0);
  382. x2 = TREE_OPERAND (t2, 0);
  383. y1 = TREE_OPERAND (t1, 1);
  384. y2 = TREE_OPERAND (t2, 1);
  385. if (!compare_operand (array_ref_low_bound (t1),
  386. array_ref_low_bound (t2)))
  387. return return_false_with_msg ("");
  388. if (!compare_operand (array_ref_element_size (t1),
  389. array_ref_element_size (t2)))
  390. return return_false_with_msg ("");
  391. if (!compare_operand (x1, x2))
  392. return return_false_with_msg ("");
  393. return compare_operand (y1, y2);
  394. case MEM_REF:
  395. {
  396. x1 = TREE_OPERAND (t1, 0);
  397. x2 = TREE_OPERAND (t2, 0);
  398. y1 = TREE_OPERAND (t1, 1);
  399. y2 = TREE_OPERAND (t2, 1);
  400. /* See if operand is an memory access (the test originate from
  401. gimple_load_p).
  402. In this case the alias set of the function being replaced must
  403. be subset of the alias set of the other function. At the moment
  404. we seek for equivalency classes, so simply require inclussion in
  405. both directions. */
  406. if (!func_checker::compatible_types_p (TREE_TYPE (x1), TREE_TYPE (x2)))
  407. return return_false ();
  408. if (!compare_operand (x1, x2))
  409. return return_false_with_msg ("");
  410. /* Type of the offset on MEM_REF does not matter. */
  411. return wi::to_offset (y1) == wi::to_offset (y2);
  412. }
  413. case COMPONENT_REF:
  414. {
  415. x1 = TREE_OPERAND (t1, 0);
  416. x2 = TREE_OPERAND (t2, 0);
  417. y1 = TREE_OPERAND (t1, 1);
  418. y2 = TREE_OPERAND (t2, 1);
  419. ret = compare_operand (x1, x2)
  420. && compare_cst_or_decl (y1, y2);
  421. return return_with_debug (ret);
  422. }
  423. /* Virtual table call. */
  424. case OBJ_TYPE_REF:
  425. {
  426. if (!compare_ssa_name (OBJ_TYPE_REF_EXPR (t1), OBJ_TYPE_REF_EXPR (t2)))
  427. return return_false ();
  428. if (opt_for_fn (m_source_func_decl, flag_devirtualize)
  429. && virtual_method_call_p (t1))
  430. {
  431. if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (t1))
  432. != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (t2)))
  433. return return_false_with_msg ("OBJ_TYPE_REF token mismatch");
  434. if (!types_same_for_odr (obj_type_ref_class (t1),
  435. obj_type_ref_class (t2)))
  436. return return_false_with_msg ("OBJ_TYPE_REF OTR type mismatch");
  437. if (!compare_operand (OBJ_TYPE_REF_OBJECT (t1),
  438. OBJ_TYPE_REF_OBJECT (t2)))
  439. return return_false_with_msg ("OBJ_TYPE_REF object mismatch");
  440. }
  441. return return_with_debug (true);
  442. }
  443. case IMAGPART_EXPR:
  444. case REALPART_EXPR:
  445. case ADDR_EXPR:
  446. {
  447. x1 = TREE_OPERAND (t1, 0);
  448. x2 = TREE_OPERAND (t2, 0);
  449. ret = compare_operand (x1, x2);
  450. return return_with_debug (ret);
  451. }
  452. case BIT_FIELD_REF:
  453. {
  454. x1 = TREE_OPERAND (t1, 0);
  455. x2 = TREE_OPERAND (t2, 0);
  456. y1 = TREE_OPERAND (t1, 1);
  457. y2 = TREE_OPERAND (t2, 1);
  458. z1 = TREE_OPERAND (t1, 2);
  459. z2 = TREE_OPERAND (t2, 2);
  460. ret = compare_operand (x1, x2)
  461. && compare_cst_or_decl (y1, y2)
  462. && compare_cst_or_decl (z1, z2);
  463. return return_with_debug (ret);
  464. }
  465. case SSA_NAME:
  466. return compare_ssa_name (t1, t2);
  467. case INTEGER_CST:
  468. case COMPLEX_CST:
  469. case VECTOR_CST:
  470. case STRING_CST:
  471. case REAL_CST:
  472. case FUNCTION_DECL:
  473. case VAR_DECL:
  474. case FIELD_DECL:
  475. case LABEL_DECL:
  476. case PARM_DECL:
  477. case RESULT_DECL:
  478. case CONST_DECL:
  479. return compare_cst_or_decl (t1, t2);
  480. default:
  481. return return_false_with_msg ("Unknown TREE code reached");
  482. }
  483. }
  484. /* Compares two tree list operands T1 and T2 and returns true if these
  485. two trees are semantically equivalent. */
  486. bool
  487. func_checker::compare_tree_list_operand (tree t1, tree t2)
  488. {
  489. gcc_assert (TREE_CODE (t1) == TREE_LIST);
  490. gcc_assert (TREE_CODE (t2) == TREE_LIST);
  491. for (; t1; t1 = TREE_CHAIN (t1))
  492. {
  493. if (!t2)
  494. return false;
  495. if (!compare_operand (TREE_VALUE (t1), TREE_VALUE (t2)))
  496. return return_false ();
  497. t2 = TREE_CHAIN (t2);
  498. }
  499. if (t2)
  500. return return_false ();
  501. return true;
  502. }
  503. /* Verifies that trees T1 and T2 do correspond. */
  504. bool
  505. func_checker::compare_variable_decl (tree t1, tree t2)
  506. {
  507. bool ret = false;
  508. if (t1 == t2)
  509. return true;
  510. if (DECL_ALIGN (t1) != DECL_ALIGN (t2))
  511. return return_false_with_msg ("alignments are different");
  512. if (DECL_HARD_REGISTER (t1) != DECL_HARD_REGISTER (t2))
  513. return return_false_with_msg ("DECL_HARD_REGISTER are different");
  514. if (DECL_HARD_REGISTER (t1)
  515. && DECL_ASSEMBLER_NAME (t1) != DECL_ASSEMBLER_NAME (t2))
  516. return return_false_with_msg ("HARD REGISTERS are different");
  517. /* Symbol table variables are known to match before we start comparing
  518. bodies. */
  519. if (decl_in_symtab_p (t1))
  520. return decl_in_symtab_p (t2);
  521. ret = compare_decl (t1, t2);
  522. return return_with_debug (ret);
  523. }
  524. /* Function visits all gimple labels and creates corresponding
  525. mapping between basic blocks and labels. */
  526. void
  527. func_checker::parse_labels (sem_bb *bb)
  528. {
  529. for (gimple_stmt_iterator gsi = gsi_start_bb (bb->bb); !gsi_end_p (gsi);
  530. gsi_next (&gsi))
  531. {
  532. gimple stmt = gsi_stmt (gsi);
  533. if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
  534. {
  535. tree t = gimple_label_label (label_stmt);
  536. gcc_assert (TREE_CODE (t) == LABEL_DECL);
  537. m_label_bb_map.put (t, bb->bb->index);
  538. }
  539. }
  540. }
  541. /* Basic block equivalence comparison function that returns true if
  542. basic blocks BB1 and BB2 (from functions FUNC1 and FUNC2) correspond.
  543. In general, a collection of equivalence dictionaries is built for types
  544. like SSA names, declarations (VAR_DECL, PARM_DECL, ..). This infrastructure
  545. is utilized by every statement-by-statement comparison function. */
  546. bool
  547. func_checker::compare_bb (sem_bb *bb1, sem_bb *bb2)
  548. {
  549. gimple_stmt_iterator gsi1, gsi2;
  550. gimple s1, s2;
  551. gsi1 = gsi_start_bb_nondebug (bb1->bb);
  552. gsi2 = gsi_start_bb_nondebug (bb2->bb);
  553. while (!gsi_end_p (gsi1))
  554. {
  555. if (gsi_end_p (gsi2))
  556. return return_false ();
  557. s1 = gsi_stmt (gsi1);
  558. s2 = gsi_stmt (gsi2);
  559. int eh1 = lookup_stmt_eh_lp_fn
  560. (DECL_STRUCT_FUNCTION (m_source_func_decl), s1);
  561. int eh2 = lookup_stmt_eh_lp_fn
  562. (DECL_STRUCT_FUNCTION (m_target_func_decl), s2);
  563. if (eh1 != eh2)
  564. return return_false_with_msg ("EH regions are different");
  565. if (gimple_code (s1) != gimple_code (s2))
  566. return return_false_with_msg ("gimple codes are different");
  567. switch (gimple_code (s1))
  568. {
  569. case GIMPLE_CALL:
  570. if (!compare_gimple_call (as_a <gcall *> (s1),
  571. as_a <gcall *> (s2)))
  572. return return_different_stmts (s1, s2, "GIMPLE_CALL");
  573. break;
  574. case GIMPLE_ASSIGN:
  575. if (!compare_gimple_assign (s1, s2))
  576. return return_different_stmts (s1, s2, "GIMPLE_ASSIGN");
  577. break;
  578. case GIMPLE_COND:
  579. if (!compare_gimple_cond (s1, s2))
  580. return return_different_stmts (s1, s2, "GIMPLE_COND");
  581. break;
  582. case GIMPLE_SWITCH:
  583. if (!compare_gimple_switch (as_a <gswitch *> (s1),
  584. as_a <gswitch *> (s2)))
  585. return return_different_stmts (s1, s2, "GIMPLE_SWITCH");
  586. break;
  587. case GIMPLE_DEBUG:
  588. break;
  589. case GIMPLE_EH_DISPATCH:
  590. if (gimple_eh_dispatch_region (as_a <geh_dispatch *> (s1))
  591. != gimple_eh_dispatch_region (as_a <geh_dispatch *> (s2)))
  592. return return_different_stmts (s1, s2, "GIMPLE_EH_DISPATCH");
  593. break;
  594. case GIMPLE_RESX:
  595. if (!compare_gimple_resx (as_a <gresx *> (s1),
  596. as_a <gresx *> (s2)))
  597. return return_different_stmts (s1, s2, "GIMPLE_RESX");
  598. break;
  599. case GIMPLE_LABEL:
  600. if (!compare_gimple_label (as_a <glabel *> (s1),
  601. as_a <glabel *> (s2)))
  602. return return_different_stmts (s1, s2, "GIMPLE_LABEL");
  603. break;
  604. case GIMPLE_RETURN:
  605. if (!compare_gimple_return (as_a <greturn *> (s1),
  606. as_a <greturn *> (s2)))
  607. return return_different_stmts (s1, s2, "GIMPLE_RETURN");
  608. break;
  609. case GIMPLE_GOTO:
  610. if (!compare_gimple_goto (s1, s2))
  611. return return_different_stmts (s1, s2, "GIMPLE_GOTO");
  612. break;
  613. case GIMPLE_ASM:
  614. if (!compare_gimple_asm (as_a <gasm *> (s1),
  615. as_a <gasm *> (s2)))
  616. return return_different_stmts (s1, s2, "GIMPLE_ASM");
  617. break;
  618. case GIMPLE_PREDICT:
  619. case GIMPLE_NOP:
  620. break;
  621. default:
  622. return return_false_with_msg ("Unknown GIMPLE code reached");
  623. }
  624. gsi_next_nondebug (&gsi1);
  625. gsi_next_nondebug (&gsi2);
  626. }
  627. if (!gsi_end_p (gsi2))
  628. return return_false ();
  629. return true;
  630. }
  631. /* Verifies for given GIMPLEs S1 and S2 that
  632. call statements are semantically equivalent. */
  633. bool
  634. func_checker::compare_gimple_call (gcall *s1, gcall *s2)
  635. {
  636. unsigned i;
  637. tree t1, t2;
  638. if (gimple_call_num_args (s1) != gimple_call_num_args (s2))
  639. return false;
  640. t1 = gimple_call_fn (s1);
  641. t2 = gimple_call_fn (s2);
  642. if (!compare_operand (t1, t2))
  643. return return_false ();
  644. /* Compare flags. */
  645. if (gimple_call_internal_p (s1) != gimple_call_internal_p (s2)
  646. || gimple_call_ctrl_altering_p (s1) != gimple_call_ctrl_altering_p (s2)
  647. || gimple_call_tail_p (s1) != gimple_call_tail_p (s2)
  648. || gimple_call_return_slot_opt_p (s1) != gimple_call_return_slot_opt_p (s2)
  649. || gimple_call_from_thunk_p (s1) != gimple_call_from_thunk_p (s2)
  650. || gimple_call_va_arg_pack_p (s1) != gimple_call_va_arg_pack_p (s2)
  651. || gimple_call_alloca_for_var_p (s1) != gimple_call_alloca_for_var_p (s2)
  652. || gimple_call_with_bounds_p (s1) != gimple_call_with_bounds_p (s2))
  653. return false;
  654. if (gimple_call_internal_p (s1)
  655. && gimple_call_internal_fn (s1) != gimple_call_internal_fn (s2))
  656. return false;
  657. tree fntype1 = gimple_call_fntype (s1);
  658. tree fntype2 = gimple_call_fntype (s2);
  659. if ((fntype1 && !fntype2)
  660. || (!fntype1 && fntype2)
  661. || (fntype1 && !types_compatible_p (fntype1, fntype2)))
  662. return return_false_with_msg ("call function types are not compatible");
  663. tree chain1 = gimple_call_chain (s1);
  664. tree chain2 = gimple_call_chain (s2);
  665. if ((chain1 && !chain2)
  666. || (!chain1 && chain2)
  667. || !compare_operand (chain1, chain2))
  668. return return_false_with_msg ("static call chains are different");
  669. /* Checking of argument. */
  670. for (i = 0; i < gimple_call_num_args (s1); ++i)
  671. {
  672. t1 = gimple_call_arg (s1, i);
  673. t2 = gimple_call_arg (s2, i);
  674. if (!compare_memory_operand (t1, t2))
  675. return return_false_with_msg ("memory operands are different");
  676. }
  677. /* Return value checking. */
  678. t1 = gimple_get_lhs (s1);
  679. t2 = gimple_get_lhs (s2);
  680. return compare_memory_operand (t1, t2);
  681. }
  682. /* Verifies for given GIMPLEs S1 and S2 that
  683. assignment statements are semantically equivalent. */
  684. bool
  685. func_checker::compare_gimple_assign (gimple s1, gimple s2)
  686. {
  687. tree arg1, arg2;
  688. tree_code code1, code2;
  689. unsigned i;
  690. code1 = gimple_expr_code (s1);
  691. code2 = gimple_expr_code (s2);
  692. if (code1 != code2)
  693. return false;
  694. code1 = gimple_assign_rhs_code (s1);
  695. code2 = gimple_assign_rhs_code (s2);
  696. if (code1 != code2)
  697. return false;
  698. for (i = 0; i < gimple_num_ops (s1); i++)
  699. {
  700. arg1 = gimple_op (s1, i);
  701. arg2 = gimple_op (s2, i);
  702. if (!compare_memory_operand (arg1, arg2))
  703. return return_false_with_msg ("memory operands are different");
  704. }
  705. return true;
  706. }
  707. /* Verifies for given GIMPLEs S1 and S2 that
  708. condition statements are semantically equivalent. */
  709. bool
  710. func_checker::compare_gimple_cond (gimple s1, gimple s2)
  711. {
  712. tree t1, t2;
  713. tree_code code1, code2;
  714. code1 = gimple_expr_code (s1);
  715. code2 = gimple_expr_code (s2);
  716. if (code1 != code2)
  717. return false;
  718. t1 = gimple_cond_lhs (s1);
  719. t2 = gimple_cond_lhs (s2);
  720. if (!compare_operand (t1, t2))
  721. return false;
  722. t1 = gimple_cond_rhs (s1);
  723. t2 = gimple_cond_rhs (s2);
  724. return compare_operand (t1, t2);
  725. }
  726. /* Verifies that tree labels T1 and T2 correspond in FUNC1 and FUNC2. */
  727. bool
  728. func_checker::compare_tree_ssa_label (tree t1, tree t2)
  729. {
  730. return compare_operand (t1, t2);
  731. }
  732. /* Verifies for given GIMPLE_LABEL stmts S1 and S2 that
  733. label statements are semantically equivalent. */
  734. bool
  735. func_checker::compare_gimple_label (const glabel *g1, const glabel *g2)
  736. {
  737. if (m_ignore_labels)
  738. return true;
  739. tree t1 = gimple_label_label (g1);
  740. tree t2 = gimple_label_label (g2);
  741. if (FORCED_LABEL (t1) || FORCED_LABEL (t2))
  742. return return_false_with_msg ("FORCED_LABEL");
  743. /* As the pass build BB to label mapping, no further check is needed. */
  744. return true;
  745. }
  746. /* Verifies for given GIMPLE_SWITCH stmts S1 and S2 that
  747. switch statements are semantically equivalent. */
  748. bool
  749. func_checker::compare_gimple_switch (const gswitch *g1, const gswitch *g2)
  750. {
  751. unsigned lsize1, lsize2, i;
  752. lsize1 = gimple_switch_num_labels (g1);
  753. lsize2 = gimple_switch_num_labels (g2);
  754. if (lsize1 != lsize2)
  755. return false;
  756. tree t1 = gimple_switch_index (g1);
  757. tree t2 = gimple_switch_index (g2);
  758. if (!compare_operand (t1, t2))
  759. return false;
  760. for (i = 0; i < lsize1; i++)
  761. {
  762. tree label1 = gimple_switch_label (g1, i);
  763. tree label2 = gimple_switch_label (g2, i);
  764. /* Label LOW and HIGH comparison. */
  765. tree low1 = CASE_LOW (label1);
  766. tree low2 = CASE_LOW (label2);
  767. if (!tree_int_cst_equal (low1, low2))
  768. return return_false_with_msg ("case low values are different");
  769. tree high1 = CASE_HIGH (label1);
  770. tree high2 = CASE_HIGH (label2);
  771. if (!tree_int_cst_equal (high1, high2))
  772. return return_false_with_msg ("case high values are different");
  773. if (TREE_CODE (label1) == CASE_LABEL_EXPR
  774. && TREE_CODE (label2) == CASE_LABEL_EXPR)
  775. {
  776. label1 = CASE_LABEL (label1);
  777. label2 = CASE_LABEL (label2);
  778. if (!compare_operand (label1, label2))
  779. return return_false_with_msg ("switch label_exprs are different");
  780. }
  781. else if (!tree_int_cst_equal (label1, label2))
  782. return return_false_with_msg ("switch labels are different");
  783. }
  784. return true;
  785. }
  786. /* Verifies for given GIMPLE_RETURN stmts S1 and S2 that
  787. return statements are semantically equivalent. */
  788. bool
  789. func_checker::compare_gimple_return (const greturn *g1, const greturn *g2)
  790. {
  791. tree t1, t2;
  792. t1 = gimple_return_retval (g1);
  793. t2 = gimple_return_retval (g2);
  794. /* Void return type. */
  795. if (t1 == NULL && t2 == NULL)
  796. return true;
  797. else
  798. return compare_operand (t1, t2);
  799. }
  800. /* Verifies for given GIMPLEs S1 and S2 that
  801. goto statements are semantically equivalent. */
  802. bool
  803. func_checker::compare_gimple_goto (gimple g1, gimple g2)
  804. {
  805. tree dest1, dest2;
  806. dest1 = gimple_goto_dest (g1);
  807. dest2 = gimple_goto_dest (g2);
  808. if (TREE_CODE (dest1) != TREE_CODE (dest2) || TREE_CODE (dest1) != SSA_NAME)
  809. return false;
  810. return compare_operand (dest1, dest2);
  811. }
  812. /* Verifies for given GIMPLE_RESX stmts S1 and S2 that
  813. resx statements are semantically equivalent. */
  814. bool
  815. func_checker::compare_gimple_resx (const gresx *g1, const gresx *g2)
  816. {
  817. return gimple_resx_region (g1) == gimple_resx_region (g2);
  818. }
  819. /* Verifies for given GIMPLEs S1 and S2 that ASM statements are equivalent.
  820. For the beginning, the pass only supports equality for
  821. '__asm__ __volatile__ ("", "", "", "memory")'. */
  822. bool
  823. func_checker::compare_gimple_asm (const gasm *g1, const gasm *g2)
  824. {
  825. if (gimple_asm_volatile_p (g1) != gimple_asm_volatile_p (g2))
  826. return false;
  827. if (gimple_asm_ninputs (g1) != gimple_asm_ninputs (g2))
  828. return false;
  829. if (gimple_asm_noutputs (g1) != gimple_asm_noutputs (g2))
  830. return false;
  831. /* We do not suppport goto ASM statement comparison. */
  832. if (gimple_asm_nlabels (g1) || gimple_asm_nlabels (g2))
  833. return false;
  834. if (gimple_asm_nclobbers (g1) != gimple_asm_nclobbers (g2))
  835. return false;
  836. if (strcmp (gimple_asm_string (g1), gimple_asm_string (g2)) != 0)
  837. return return_false_with_msg ("ASM strings are different");
  838. for (unsigned i = 0; i < gimple_asm_ninputs (g1); i++)
  839. {
  840. tree input1 = gimple_asm_input_op (g1, i);
  841. tree input2 = gimple_asm_input_op (g2, i);
  842. if (!compare_tree_list_operand (input1, input2))
  843. return return_false_with_msg ("ASM input is different");
  844. }
  845. for (unsigned i = 0; i < gimple_asm_noutputs (g1); i++)
  846. {
  847. tree output1 = gimple_asm_output_op (g1, i);
  848. tree output2 = gimple_asm_output_op (g2, i);
  849. if (!compare_tree_list_operand (output1, output2))
  850. return return_false_with_msg ("ASM output is different");
  851. }
  852. for (unsigned i = 0; i < gimple_asm_nclobbers (g1); i++)
  853. {
  854. tree clobber1 = gimple_asm_clobber_op (g1, i);
  855. tree clobber2 = gimple_asm_clobber_op (g2, i);
  856. if (!operand_equal_p (TREE_VALUE (clobber1), TREE_VALUE (clobber2),
  857. OEP_ONLY_CONST))
  858. return return_false_with_msg ("ASM clobber is different");
  859. }
  860. return true;
  861. }
  862. } // ipa_icf_gimple namespace