tree-ssa-ccp.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842
  1. /* Conditional constant propagation pass for the GNU compiler.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. Adapted from original RTL SSA-CCP by Daniel Berlin <dberlin@dberlin.org>
  4. Adapted to GIMPLE trees by Diego Novillo <dnovillo@redhat.com>
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 3, or (at your option) any
  9. later version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. /* Conditional constant propagation (CCP) is based on the SSA
  18. propagation engine (tree-ssa-propagate.c). Constant assignments of
  19. the form VAR = CST are propagated from the assignments into uses of
  20. VAR, which in turn may generate new constants. The simulation uses
  21. a four level lattice to keep track of constant values associated
  22. with SSA names. Given an SSA name V_i, it may take one of the
  23. following values:
  24. UNINITIALIZED -> the initial state of the value. This value
  25. is replaced with a correct initial value
  26. the first time the value is used, so the
  27. rest of the pass does not need to care about
  28. it. Using this value simplifies initialization
  29. of the pass, and prevents us from needlessly
  30. scanning statements that are never reached.
  31. UNDEFINED -> V_i is a local variable whose definition
  32. has not been processed yet. Therefore we
  33. don't yet know if its value is a constant
  34. or not.
  35. CONSTANT -> V_i has been found to hold a constant
  36. value C.
  37. VARYING -> V_i cannot take a constant value, or if it
  38. does, it is not possible to determine it
  39. at compile time.
  40. The core of SSA-CCP is in ccp_visit_stmt and ccp_visit_phi_node:
  41. 1- In ccp_visit_stmt, we are interested in assignments whose RHS
  42. evaluates into a constant and conditional jumps whose predicate
  43. evaluates into a boolean true or false. When an assignment of
  44. the form V_i = CONST is found, V_i's lattice value is set to
  45. CONSTANT and CONST is associated with it. This causes the
  46. propagation engine to add all the SSA edges coming out the
  47. assignment into the worklists, so that statements that use V_i
  48. can be visited.
  49. If the statement is a conditional with a constant predicate, we
  50. mark the outgoing edges as executable or not executable
  51. depending on the predicate's value. This is then used when
  52. visiting PHI nodes to know when a PHI argument can be ignored.
  53. 2- In ccp_visit_phi_node, if all the PHI arguments evaluate to the
  54. same constant C, then the LHS of the PHI is set to C. This
  55. evaluation is known as the "meet operation". Since one of the
  56. goals of this evaluation is to optimistically return constant
  57. values as often as possible, it uses two main short cuts:
  58. - If an argument is flowing in through a non-executable edge, it
  59. is ignored. This is useful in cases like this:
  60. if (PRED)
  61. a_9 = 3;
  62. else
  63. a_10 = 100;
  64. a_11 = PHI (a_9, a_10)
  65. If PRED is known to always evaluate to false, then we can
  66. assume that a_11 will always take its value from a_10, meaning
  67. that instead of consider it VARYING (a_9 and a_10 have
  68. different values), we can consider it CONSTANT 100.
  69. - If an argument has an UNDEFINED value, then it does not affect
  70. the outcome of the meet operation. If a variable V_i has an
  71. UNDEFINED value, it means that either its defining statement
  72. hasn't been visited yet or V_i has no defining statement, in
  73. which case the original symbol 'V' is being used
  74. uninitialized. Since 'V' is a local variable, the compiler
  75. may assume any initial value for it.
  76. After propagation, every variable V_i that ends up with a lattice
  77. value of CONSTANT will have the associated constant value in the
  78. array CONST_VAL[i].VALUE. That is fed into substitute_and_fold for
  79. final substitution and folding.
  80. This algorithm uses wide-ints at the max precision of the target.
  81. This means that, with one uninteresting exception, variables with
  82. UNSIGNED types never go to VARYING because the bits above the
  83. precision of the type of the variable are always zero. The
  84. uninteresting case is a variable of UNSIGNED type that has the
  85. maximum precision of the target. Such variables can go to VARYING,
  86. but this causes no loss of infomation since these variables will
  87. never be extended.
  88. References:
  89. Constant propagation with conditional branches,
  90. Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
  91. Building an Optimizing Compiler,
  92. Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
  93. Advanced Compiler Design and Implementation,
  94. Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
  95. #include "config.h"
  96. #include "system.h"
  97. #include "coretypes.h"
  98. #include "tm.h"
  99. #include "hash-set.h"
  100. #include "machmode.h"
  101. #include "vec.h"
  102. #include "double-int.h"
  103. #include "input.h"
  104. #include "alias.h"
  105. #include "symtab.h"
  106. #include "wide-int.h"
  107. #include "inchash.h"
  108. #include "real.h"
  109. #include "tree.h"
  110. #include "fold-const.h"
  111. #include "stor-layout.h"
  112. #include "flags.h"
  113. #include "tm_p.h"
  114. #include "predict.h"
  115. #include "hard-reg-set.h"
  116. #include "input.h"
  117. #include "function.h"
  118. #include "dominance.h"
  119. #include "cfg.h"
  120. #include "basic-block.h"
  121. #include "gimple-pretty-print.h"
  122. #include "hash-table.h"
  123. #include "tree-ssa-alias.h"
  124. #include "internal-fn.h"
  125. #include "gimple-fold.h"
  126. #include "tree-eh.h"
  127. #include "gimple-expr.h"
  128. #include "is-a.h"
  129. #include "gimple.h"
  130. #include "gimplify.h"
  131. #include "gimple-iterator.h"
  132. #include "gimple-ssa.h"
  133. #include "tree-cfg.h"
  134. #include "tree-phinodes.h"
  135. #include "ssa-iterators.h"
  136. #include "stringpool.h"
  137. #include "tree-ssanames.h"
  138. #include "tree-pass.h"
  139. #include "tree-ssa-propagate.h"
  140. #include "value-prof.h"
  141. #include "langhooks.h"
  142. #include "target.h"
  143. #include "diagnostic-core.h"
  144. #include "dbgcnt.h"
  145. #include "params.h"
  146. #include "wide-int-print.h"
  147. #include "builtins.h"
  148. #include "tree-chkp.h"
  149. /* Possible lattice values. */
  150. typedef enum
  151. {
  152. UNINITIALIZED,
  153. UNDEFINED,
  154. CONSTANT,
  155. VARYING
  156. } ccp_lattice_t;
  157. struct ccp_prop_value_t {
  158. /* Lattice value. */
  159. ccp_lattice_t lattice_val;
  160. /* Propagated value. */
  161. tree value;
  162. /* Mask that applies to the propagated value during CCP. For X
  163. with a CONSTANT lattice value X & ~mask == value & ~mask. The
  164. zero bits in the mask cover constant values. The ones mean no
  165. information. */
  166. widest_int mask;
  167. };
  168. /* Array of propagated constant values. After propagation,
  169. CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
  170. the constant is held in an SSA name representing a memory store
  171. (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
  172. memory reference used to store (i.e., the LHS of the assignment
  173. doing the store). */
  174. static ccp_prop_value_t *const_val;
  175. static unsigned n_const_val;
  176. static void canonicalize_value (ccp_prop_value_t *);
  177. static bool ccp_fold_stmt (gimple_stmt_iterator *);
  178. /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
  179. static void
  180. dump_lattice_value (FILE *outf, const char *prefix, ccp_prop_value_t val)
  181. {
  182. switch (val.lattice_val)
  183. {
  184. case UNINITIALIZED:
  185. fprintf (outf, "%sUNINITIALIZED", prefix);
  186. break;
  187. case UNDEFINED:
  188. fprintf (outf, "%sUNDEFINED", prefix);
  189. break;
  190. case VARYING:
  191. fprintf (outf, "%sVARYING", prefix);
  192. break;
  193. case CONSTANT:
  194. if (TREE_CODE (val.value) != INTEGER_CST
  195. || val.mask == 0)
  196. {
  197. fprintf (outf, "%sCONSTANT ", prefix);
  198. print_generic_expr (outf, val.value, dump_flags);
  199. }
  200. else
  201. {
  202. widest_int cval = wi::bit_and_not (wi::to_widest (val.value),
  203. val.mask);
  204. fprintf (outf, "%sCONSTANT ", prefix);
  205. print_hex (cval, outf);
  206. fprintf (outf, " (");
  207. print_hex (val.mask, outf);
  208. fprintf (outf, ")");
  209. }
  210. break;
  211. default:
  212. gcc_unreachable ();
  213. }
  214. }
  215. /* Print lattice value VAL to stderr. */
  216. void debug_lattice_value (ccp_prop_value_t val);
  217. DEBUG_FUNCTION void
  218. debug_lattice_value (ccp_prop_value_t val)
  219. {
  220. dump_lattice_value (stderr, "", val);
  221. fprintf (stderr, "\n");
  222. }
  223. /* Extend NONZERO_BITS to a full mask, with the upper bits being set. */
  224. static widest_int
  225. extend_mask (const wide_int &nonzero_bits)
  226. {
  227. return (wi::mask <widest_int> (wi::get_precision (nonzero_bits), true)
  228. | widest_int::from (nonzero_bits, UNSIGNED));
  229. }
  230. /* Compute a default value for variable VAR and store it in the
  231. CONST_VAL array. The following rules are used to get default
  232. values:
  233. 1- Global and static variables that are declared constant are
  234. considered CONSTANT.
  235. 2- Any other value is considered UNDEFINED. This is useful when
  236. considering PHI nodes. PHI arguments that are undefined do not
  237. change the constant value of the PHI node, which allows for more
  238. constants to be propagated.
  239. 3- Variables defined by statements other than assignments and PHI
  240. nodes are considered VARYING.
  241. 4- Initial values of variables that are not GIMPLE registers are
  242. considered VARYING. */
  243. static ccp_prop_value_t
  244. get_default_value (tree var)
  245. {
  246. ccp_prop_value_t val = { UNINITIALIZED, NULL_TREE, 0 };
  247. gimple stmt;
  248. stmt = SSA_NAME_DEF_STMT (var);
  249. if (gimple_nop_p (stmt))
  250. {
  251. /* Variables defined by an empty statement are those used
  252. before being initialized. If VAR is a local variable, we
  253. can assume initially that it is UNDEFINED, otherwise we must
  254. consider it VARYING. */
  255. if (!virtual_operand_p (var)
  256. && TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
  257. val.lattice_val = UNDEFINED;
  258. else
  259. {
  260. val.lattice_val = VARYING;
  261. val.mask = -1;
  262. if (flag_tree_bit_ccp)
  263. {
  264. wide_int nonzero_bits = get_nonzero_bits (var);
  265. if (nonzero_bits != -1)
  266. {
  267. val.lattice_val = CONSTANT;
  268. val.value = build_zero_cst (TREE_TYPE (var));
  269. val.mask = extend_mask (nonzero_bits);
  270. }
  271. }
  272. }
  273. }
  274. else if (is_gimple_assign (stmt))
  275. {
  276. tree cst;
  277. if (gimple_assign_single_p (stmt)
  278. && DECL_P (gimple_assign_rhs1 (stmt))
  279. && (cst = get_symbol_constant_value (gimple_assign_rhs1 (stmt))))
  280. {
  281. val.lattice_val = CONSTANT;
  282. val.value = cst;
  283. }
  284. else
  285. {
  286. /* Any other variable defined by an assignment is considered
  287. UNDEFINED. */
  288. val.lattice_val = UNDEFINED;
  289. }
  290. }
  291. else if ((is_gimple_call (stmt)
  292. && gimple_call_lhs (stmt) != NULL_TREE)
  293. || gimple_code (stmt) == GIMPLE_PHI)
  294. {
  295. /* A variable defined by a call or a PHI node is considered
  296. UNDEFINED. */
  297. val.lattice_val = UNDEFINED;
  298. }
  299. else
  300. {
  301. /* Otherwise, VAR will never take on a constant value. */
  302. val.lattice_val = VARYING;
  303. val.mask = -1;
  304. }
  305. return val;
  306. }
  307. /* Get the constant value associated with variable VAR. */
  308. static inline ccp_prop_value_t *
  309. get_value (tree var)
  310. {
  311. ccp_prop_value_t *val;
  312. if (const_val == NULL
  313. || SSA_NAME_VERSION (var) >= n_const_val)
  314. return NULL;
  315. val = &const_val[SSA_NAME_VERSION (var)];
  316. if (val->lattice_val == UNINITIALIZED)
  317. *val = get_default_value (var);
  318. canonicalize_value (val);
  319. return val;
  320. }
  321. /* Return the constant tree value associated with VAR. */
  322. static inline tree
  323. get_constant_value (tree var)
  324. {
  325. ccp_prop_value_t *val;
  326. if (TREE_CODE (var) != SSA_NAME)
  327. {
  328. if (is_gimple_min_invariant (var))
  329. return var;
  330. return NULL_TREE;
  331. }
  332. val = get_value (var);
  333. if (val
  334. && val->lattice_val == CONSTANT
  335. && (TREE_CODE (val->value) != INTEGER_CST
  336. || val->mask == 0))
  337. return val->value;
  338. return NULL_TREE;
  339. }
  340. /* Sets the value associated with VAR to VARYING. */
  341. static inline void
  342. set_value_varying (tree var)
  343. {
  344. ccp_prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
  345. val->lattice_val = VARYING;
  346. val->value = NULL_TREE;
  347. val->mask = -1;
  348. }
  349. /* For integer constants, make sure to drop TREE_OVERFLOW. */
  350. static void
  351. canonicalize_value (ccp_prop_value_t *val)
  352. {
  353. if (val->lattice_val != CONSTANT)
  354. return;
  355. if (TREE_OVERFLOW_P (val->value))
  356. val->value = drop_tree_overflow (val->value);
  357. }
  358. /* Return whether the lattice transition is valid. */
  359. static bool
  360. valid_lattice_transition (ccp_prop_value_t old_val, ccp_prop_value_t new_val)
  361. {
  362. /* Lattice transitions must always be monotonically increasing in
  363. value. */
  364. if (old_val.lattice_val < new_val.lattice_val)
  365. return true;
  366. if (old_val.lattice_val != new_val.lattice_val)
  367. return false;
  368. if (!old_val.value && !new_val.value)
  369. return true;
  370. /* Now both lattice values are CONSTANT. */
  371. /* Allow transitioning from PHI <&x, not executable> == &x
  372. to PHI <&x, &y> == common alignment. */
  373. if (TREE_CODE (old_val.value) != INTEGER_CST
  374. && TREE_CODE (new_val.value) == INTEGER_CST)
  375. return true;
  376. /* Bit-lattices have to agree in the still valid bits. */
  377. if (TREE_CODE (old_val.value) == INTEGER_CST
  378. && TREE_CODE (new_val.value) == INTEGER_CST)
  379. return (wi::bit_and_not (wi::to_widest (old_val.value), new_val.mask)
  380. == wi::bit_and_not (wi::to_widest (new_val.value), new_val.mask));
  381. /* Otherwise constant values have to agree. */
  382. if (operand_equal_p (old_val.value, new_val.value, 0))
  383. return true;
  384. /* At least the kinds and types should agree now. */
  385. if (TREE_CODE (old_val.value) != TREE_CODE (new_val.value)
  386. || !types_compatible_p (TREE_TYPE (old_val.value),
  387. TREE_TYPE (new_val.value)))
  388. return false;
  389. /* For floats and !HONOR_NANS allow transitions from (partial) NaN
  390. to non-NaN. */
  391. tree type = TREE_TYPE (new_val.value);
  392. if (SCALAR_FLOAT_TYPE_P (type)
  393. && !HONOR_NANS (type))
  394. {
  395. if (REAL_VALUE_ISNAN (TREE_REAL_CST (old_val.value)))
  396. return true;
  397. }
  398. else if (VECTOR_FLOAT_TYPE_P (type)
  399. && !HONOR_NANS (type))
  400. {
  401. for (unsigned i = 0; i < VECTOR_CST_NELTS (old_val.value); ++i)
  402. if (!REAL_VALUE_ISNAN
  403. (TREE_REAL_CST (VECTOR_CST_ELT (old_val.value, i)))
  404. && !operand_equal_p (VECTOR_CST_ELT (old_val.value, i),
  405. VECTOR_CST_ELT (new_val.value, i), 0))
  406. return false;
  407. return true;
  408. }
  409. else if (COMPLEX_FLOAT_TYPE_P (type)
  410. && !HONOR_NANS (type))
  411. {
  412. if (!REAL_VALUE_ISNAN (TREE_REAL_CST (TREE_REALPART (old_val.value)))
  413. && !operand_equal_p (TREE_REALPART (old_val.value),
  414. TREE_REALPART (new_val.value), 0))
  415. return false;
  416. if (!REAL_VALUE_ISNAN (TREE_REAL_CST (TREE_IMAGPART (old_val.value)))
  417. && !operand_equal_p (TREE_IMAGPART (old_val.value),
  418. TREE_IMAGPART (new_val.value), 0))
  419. return false;
  420. return true;
  421. }
  422. return false;
  423. }
  424. /* Set the value for variable VAR to NEW_VAL. Return true if the new
  425. value is different from VAR's previous value. */
  426. static bool
  427. set_lattice_value (tree var, ccp_prop_value_t new_val)
  428. {
  429. /* We can deal with old UNINITIALIZED values just fine here. */
  430. ccp_prop_value_t *old_val = &const_val[SSA_NAME_VERSION (var)];
  431. canonicalize_value (&new_val);
  432. /* We have to be careful to not go up the bitwise lattice
  433. represented by the mask.
  434. ??? This doesn't seem to be the best place to enforce this. */
  435. if (new_val.lattice_val == CONSTANT
  436. && old_val->lattice_val == CONSTANT
  437. && TREE_CODE (new_val.value) == INTEGER_CST
  438. && TREE_CODE (old_val->value) == INTEGER_CST)
  439. {
  440. widest_int diff = (wi::to_widest (new_val.value)
  441. ^ wi::to_widest (old_val->value));
  442. new_val.mask = new_val.mask | old_val->mask | diff;
  443. }
  444. gcc_checking_assert (valid_lattice_transition (*old_val, new_val));
  445. /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
  446. caller that this was a non-transition. */
  447. if (old_val->lattice_val != new_val.lattice_val
  448. || (new_val.lattice_val == CONSTANT
  449. && TREE_CODE (new_val.value) == INTEGER_CST
  450. && (TREE_CODE (old_val->value) != INTEGER_CST
  451. || new_val.mask != old_val->mask)))
  452. {
  453. /* ??? We would like to delay creation of INTEGER_CSTs from
  454. partially constants here. */
  455. if (dump_file && (dump_flags & TDF_DETAILS))
  456. {
  457. dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
  458. fprintf (dump_file, ". Adding SSA edges to worklist.\n");
  459. }
  460. *old_val = new_val;
  461. gcc_assert (new_val.lattice_val != UNINITIALIZED);
  462. return true;
  463. }
  464. return false;
  465. }
  466. static ccp_prop_value_t get_value_for_expr (tree, bool);
  467. static ccp_prop_value_t bit_value_binop (enum tree_code, tree, tree, tree);
  468. static void bit_value_binop_1 (enum tree_code, tree, widest_int *, widest_int *,
  469. tree, const widest_int &, const widest_int &,
  470. tree, const widest_int &, const widest_int &);
  471. /* Return a widest_int that can be used for bitwise simplifications
  472. from VAL. */
  473. static widest_int
  474. value_to_wide_int (ccp_prop_value_t val)
  475. {
  476. if (val.value
  477. && TREE_CODE (val.value) == INTEGER_CST)
  478. return wi::to_widest (val.value);
  479. return 0;
  480. }
  481. /* Return the value for the address expression EXPR based on alignment
  482. information. */
  483. static ccp_prop_value_t
  484. get_value_from_alignment (tree expr)
  485. {
  486. tree type = TREE_TYPE (expr);
  487. ccp_prop_value_t val;
  488. unsigned HOST_WIDE_INT bitpos;
  489. unsigned int align;
  490. gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
  491. get_pointer_alignment_1 (expr, &align, &bitpos);
  492. val.mask = (POINTER_TYPE_P (type) || TYPE_UNSIGNED (type)
  493. ? wi::mask <widest_int> (TYPE_PRECISION (type), false)
  494. : -1).and_not (align / BITS_PER_UNIT - 1);
  495. val.lattice_val = val.mask == -1 ? VARYING : CONSTANT;
  496. if (val.lattice_val == CONSTANT)
  497. val.value = build_int_cstu (type, bitpos / BITS_PER_UNIT);
  498. else
  499. val.value = NULL_TREE;
  500. return val;
  501. }
  502. /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
  503. return constant bits extracted from alignment information for
  504. invariant addresses. */
  505. static ccp_prop_value_t
  506. get_value_for_expr (tree expr, bool for_bits_p)
  507. {
  508. ccp_prop_value_t val;
  509. if (TREE_CODE (expr) == SSA_NAME)
  510. {
  511. val = *get_value (expr);
  512. if (for_bits_p
  513. && val.lattice_val == CONSTANT
  514. && TREE_CODE (val.value) == ADDR_EXPR)
  515. val = get_value_from_alignment (val.value);
  516. }
  517. else if (is_gimple_min_invariant (expr)
  518. && (!for_bits_p || TREE_CODE (expr) != ADDR_EXPR))
  519. {
  520. val.lattice_val = CONSTANT;
  521. val.value = expr;
  522. val.mask = 0;
  523. canonicalize_value (&val);
  524. }
  525. else if (TREE_CODE (expr) == ADDR_EXPR)
  526. val = get_value_from_alignment (expr);
  527. else
  528. {
  529. val.lattice_val = VARYING;
  530. val.mask = -1;
  531. val.value = NULL_TREE;
  532. }
  533. return val;
  534. }
  535. /* Return the likely CCP lattice value for STMT.
  536. If STMT has no operands, then return CONSTANT.
  537. Else if undefinedness of operands of STMT cause its value to be
  538. undefined, then return UNDEFINED.
  539. Else if any operands of STMT are constants, then return CONSTANT.
  540. Else return VARYING. */
  541. static ccp_lattice_t
  542. likely_value (gimple stmt)
  543. {
  544. bool has_constant_operand, has_undefined_operand, all_undefined_operands;
  545. tree use;
  546. ssa_op_iter iter;
  547. unsigned i;
  548. enum gimple_code code = gimple_code (stmt);
  549. /* This function appears to be called only for assignments, calls,
  550. conditionals, and switches, due to the logic in visit_stmt. */
  551. gcc_assert (code == GIMPLE_ASSIGN
  552. || code == GIMPLE_CALL
  553. || code == GIMPLE_COND
  554. || code == GIMPLE_SWITCH);
  555. /* If the statement has volatile operands, it won't fold to a
  556. constant value. */
  557. if (gimple_has_volatile_ops (stmt))
  558. return VARYING;
  559. /* Arrive here for more complex cases. */
  560. has_constant_operand = false;
  561. has_undefined_operand = false;
  562. all_undefined_operands = true;
  563. FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
  564. {
  565. ccp_prop_value_t *val = get_value (use);
  566. if (val->lattice_val == UNDEFINED)
  567. has_undefined_operand = true;
  568. else
  569. all_undefined_operands = false;
  570. if (val->lattice_val == CONSTANT)
  571. has_constant_operand = true;
  572. }
  573. /* There may be constants in regular rhs operands. For calls we
  574. have to ignore lhs, fndecl and static chain, otherwise only
  575. the lhs. */
  576. for (i = (is_gimple_call (stmt) ? 2 : 0) + gimple_has_lhs (stmt);
  577. i < gimple_num_ops (stmt); ++i)
  578. {
  579. tree op = gimple_op (stmt, i);
  580. if (!op || TREE_CODE (op) == SSA_NAME)
  581. continue;
  582. if (is_gimple_min_invariant (op))
  583. has_constant_operand = true;
  584. }
  585. if (has_constant_operand)
  586. all_undefined_operands = false;
  587. if (has_undefined_operand
  588. && code == GIMPLE_CALL
  589. && gimple_call_internal_p (stmt))
  590. switch (gimple_call_internal_fn (stmt))
  591. {
  592. /* These 3 builtins use the first argument just as a magic
  593. way how to find out a decl uid. */
  594. case IFN_GOMP_SIMD_LANE:
  595. case IFN_GOMP_SIMD_VF:
  596. case IFN_GOMP_SIMD_LAST_LANE:
  597. has_undefined_operand = false;
  598. break;
  599. default:
  600. break;
  601. }
  602. /* If the operation combines operands like COMPLEX_EXPR make sure to
  603. not mark the result UNDEFINED if only one part of the result is
  604. undefined. */
  605. if (has_undefined_operand && all_undefined_operands)
  606. return UNDEFINED;
  607. else if (code == GIMPLE_ASSIGN && has_undefined_operand)
  608. {
  609. switch (gimple_assign_rhs_code (stmt))
  610. {
  611. /* Unary operators are handled with all_undefined_operands. */
  612. case PLUS_EXPR:
  613. case MINUS_EXPR:
  614. case POINTER_PLUS_EXPR:
  615. /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
  616. Not bitwise operators, one VARYING operand may specify the
  617. result completely. Not logical operators for the same reason.
  618. Not COMPLEX_EXPR as one VARYING operand makes the result partly
  619. not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
  620. the undefined operand may be promoted. */
  621. return UNDEFINED;
  622. case ADDR_EXPR:
  623. /* If any part of an address is UNDEFINED, like the index
  624. of an ARRAY_EXPR, then treat the result as UNDEFINED. */
  625. return UNDEFINED;
  626. default:
  627. ;
  628. }
  629. }
  630. /* If there was an UNDEFINED operand but the result may be not UNDEFINED
  631. fall back to CONSTANT. During iteration UNDEFINED may still drop
  632. to CONSTANT. */
  633. if (has_undefined_operand)
  634. return CONSTANT;
  635. /* We do not consider virtual operands here -- load from read-only
  636. memory may have only VARYING virtual operands, but still be
  637. constant. */
  638. if (has_constant_operand
  639. || gimple_references_memory_p (stmt))
  640. return CONSTANT;
  641. return VARYING;
  642. }
  643. /* Returns true if STMT cannot be constant. */
  644. static bool
  645. surely_varying_stmt_p (gimple stmt)
  646. {
  647. /* If the statement has operands that we cannot handle, it cannot be
  648. constant. */
  649. if (gimple_has_volatile_ops (stmt))
  650. return true;
  651. /* If it is a call and does not return a value or is not a
  652. builtin and not an indirect call or a call to function with
  653. assume_aligned/alloc_align attribute, it is varying. */
  654. if (is_gimple_call (stmt))
  655. {
  656. tree fndecl, fntype = gimple_call_fntype (stmt);
  657. if (!gimple_call_lhs (stmt)
  658. || ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
  659. && !DECL_BUILT_IN (fndecl)
  660. && !lookup_attribute ("assume_aligned",
  661. TYPE_ATTRIBUTES (fntype))
  662. && !lookup_attribute ("alloc_align",
  663. TYPE_ATTRIBUTES (fntype))))
  664. return true;
  665. }
  666. /* Any other store operation is not interesting. */
  667. else if (gimple_vdef (stmt))
  668. return true;
  669. /* Anything other than assignments and conditional jumps are not
  670. interesting for CCP. */
  671. if (gimple_code (stmt) != GIMPLE_ASSIGN
  672. && gimple_code (stmt) != GIMPLE_COND
  673. && gimple_code (stmt) != GIMPLE_SWITCH
  674. && gimple_code (stmt) != GIMPLE_CALL)
  675. return true;
  676. return false;
  677. }
  678. /* Initialize local data structures for CCP. */
  679. static void
  680. ccp_initialize (void)
  681. {
  682. basic_block bb;
  683. n_const_val = num_ssa_names;
  684. const_val = XCNEWVEC (ccp_prop_value_t, n_const_val);
  685. /* Initialize simulation flags for PHI nodes and statements. */
  686. FOR_EACH_BB_FN (bb, cfun)
  687. {
  688. gimple_stmt_iterator i;
  689. for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
  690. {
  691. gimple stmt = gsi_stmt (i);
  692. bool is_varying;
  693. /* If the statement is a control insn, then we do not
  694. want to avoid simulating the statement once. Failure
  695. to do so means that those edges will never get added. */
  696. if (stmt_ends_bb_p (stmt))
  697. is_varying = false;
  698. else
  699. is_varying = surely_varying_stmt_p (stmt);
  700. if (is_varying)
  701. {
  702. tree def;
  703. ssa_op_iter iter;
  704. /* If the statement will not produce a constant, mark
  705. all its outputs VARYING. */
  706. FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
  707. set_value_varying (def);
  708. }
  709. prop_set_simulate_again (stmt, !is_varying);
  710. }
  711. }
  712. /* Now process PHI nodes. We never clear the simulate_again flag on
  713. phi nodes, since we do not know which edges are executable yet,
  714. except for phi nodes for virtual operands when we do not do store ccp. */
  715. FOR_EACH_BB_FN (bb, cfun)
  716. {
  717. gphi_iterator i;
  718. for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
  719. {
  720. gphi *phi = i.phi ();
  721. if (virtual_operand_p (gimple_phi_result (phi)))
  722. prop_set_simulate_again (phi, false);
  723. else
  724. prop_set_simulate_again (phi, true);
  725. }
  726. }
  727. }
  728. /* Debug count support. Reset the values of ssa names
  729. VARYING when the total number ssa names analyzed is
  730. beyond the debug count specified. */
  731. static void
  732. do_dbg_cnt (void)
  733. {
  734. unsigned i;
  735. for (i = 0; i < num_ssa_names; i++)
  736. {
  737. if (!dbg_cnt (ccp))
  738. {
  739. const_val[i].lattice_val = VARYING;
  740. const_val[i].mask = -1;
  741. const_val[i].value = NULL_TREE;
  742. }
  743. }
  744. }
  745. /* Do final substitution of propagated values, cleanup the flowgraph and
  746. free allocated storage.
  747. Return TRUE when something was optimized. */
  748. static bool
  749. ccp_finalize (void)
  750. {
  751. bool something_changed;
  752. unsigned i;
  753. do_dbg_cnt ();
  754. /* Derive alignment and misalignment information from partially
  755. constant pointers in the lattice or nonzero bits from partially
  756. constant integers. */
  757. for (i = 1; i < num_ssa_names; ++i)
  758. {
  759. tree name = ssa_name (i);
  760. ccp_prop_value_t *val;
  761. unsigned int tem, align;
  762. if (!name
  763. || (!POINTER_TYPE_P (TREE_TYPE (name))
  764. && (!INTEGRAL_TYPE_P (TREE_TYPE (name))
  765. /* Don't record nonzero bits before IPA to avoid
  766. using too much memory. */
  767. || first_pass_instance)))
  768. continue;
  769. val = get_value (name);
  770. if (val->lattice_val != CONSTANT
  771. || TREE_CODE (val->value) != INTEGER_CST)
  772. continue;
  773. if (POINTER_TYPE_P (TREE_TYPE (name)))
  774. {
  775. /* Trailing mask bits specify the alignment, trailing value
  776. bits the misalignment. */
  777. tem = val->mask.to_uhwi ();
  778. align = (tem & -tem);
  779. if (align > 1)
  780. set_ptr_info_alignment (get_ptr_info (name), align,
  781. (TREE_INT_CST_LOW (val->value)
  782. & (align - 1)));
  783. }
  784. else
  785. {
  786. unsigned int precision = TYPE_PRECISION (TREE_TYPE (val->value));
  787. wide_int nonzero_bits = wide_int::from (val->mask, precision,
  788. UNSIGNED) | val->value;
  789. nonzero_bits &= get_nonzero_bits (name);
  790. set_nonzero_bits (name, nonzero_bits);
  791. }
  792. }
  793. /* Perform substitutions based on the known constant values. */
  794. something_changed = substitute_and_fold (get_constant_value,
  795. ccp_fold_stmt, true);
  796. free (const_val);
  797. const_val = NULL;
  798. return something_changed;;
  799. }
  800. /* Compute the meet operator between *VAL1 and *VAL2. Store the result
  801. in VAL1.
  802. any M UNDEFINED = any
  803. any M VARYING = VARYING
  804. Ci M Cj = Ci if (i == j)
  805. Ci M Cj = VARYING if (i != j)
  806. */
  807. static void
  808. ccp_lattice_meet (ccp_prop_value_t *val1, ccp_prop_value_t *val2)
  809. {
  810. if (val1->lattice_val == UNDEFINED)
  811. {
  812. /* UNDEFINED M any = any */
  813. *val1 = *val2;
  814. }
  815. else if (val2->lattice_val == UNDEFINED)
  816. {
  817. /* any M UNDEFINED = any
  818. Nothing to do. VAL1 already contains the value we want. */
  819. ;
  820. }
  821. else if (val1->lattice_val == VARYING
  822. || val2->lattice_val == VARYING)
  823. {
  824. /* any M VARYING = VARYING. */
  825. val1->lattice_val = VARYING;
  826. val1->mask = -1;
  827. val1->value = NULL_TREE;
  828. }
  829. else if (val1->lattice_val == CONSTANT
  830. && val2->lattice_val == CONSTANT
  831. && TREE_CODE (val1->value) == INTEGER_CST
  832. && TREE_CODE (val2->value) == INTEGER_CST)
  833. {
  834. /* Ci M Cj = Ci if (i == j)
  835. Ci M Cj = VARYING if (i != j)
  836. For INTEGER_CSTs mask unequal bits. If no equal bits remain,
  837. drop to varying. */
  838. val1->mask = (val1->mask | val2->mask
  839. | (wi::to_widest (val1->value)
  840. ^ wi::to_widest (val2->value)));
  841. if (val1->mask == -1)
  842. {
  843. val1->lattice_val = VARYING;
  844. val1->value = NULL_TREE;
  845. }
  846. }
  847. else if (val1->lattice_val == CONSTANT
  848. && val2->lattice_val == CONSTANT
  849. && simple_cst_equal (val1->value, val2->value) == 1)
  850. {
  851. /* Ci M Cj = Ci if (i == j)
  852. Ci M Cj = VARYING if (i != j)
  853. VAL1 already contains the value we want for equivalent values. */
  854. }
  855. else if (val1->lattice_val == CONSTANT
  856. && val2->lattice_val == CONSTANT
  857. && (TREE_CODE (val1->value) == ADDR_EXPR
  858. || TREE_CODE (val2->value) == ADDR_EXPR))
  859. {
  860. /* When not equal addresses are involved try meeting for
  861. alignment. */
  862. ccp_prop_value_t tem = *val2;
  863. if (TREE_CODE (val1->value) == ADDR_EXPR)
  864. *val1 = get_value_for_expr (val1->value, true);
  865. if (TREE_CODE (val2->value) == ADDR_EXPR)
  866. tem = get_value_for_expr (val2->value, true);
  867. ccp_lattice_meet (val1, &tem);
  868. }
  869. else
  870. {
  871. /* Any other combination is VARYING. */
  872. val1->lattice_val = VARYING;
  873. val1->mask = -1;
  874. val1->value = NULL_TREE;
  875. }
  876. }
  877. /* Loop through the PHI_NODE's parameters for BLOCK and compare their
  878. lattice values to determine PHI_NODE's lattice value. The value of a
  879. PHI node is determined calling ccp_lattice_meet with all the arguments
  880. of the PHI node that are incoming via executable edges. */
  881. static enum ssa_prop_result
  882. ccp_visit_phi_node (gphi *phi)
  883. {
  884. unsigned i;
  885. ccp_prop_value_t *old_val, new_val;
  886. if (dump_file && (dump_flags & TDF_DETAILS))
  887. {
  888. fprintf (dump_file, "\nVisiting PHI node: ");
  889. print_gimple_stmt (dump_file, phi, 0, dump_flags);
  890. }
  891. old_val = get_value (gimple_phi_result (phi));
  892. switch (old_val->lattice_val)
  893. {
  894. case VARYING:
  895. return SSA_PROP_VARYING;
  896. case CONSTANT:
  897. new_val = *old_val;
  898. break;
  899. case UNDEFINED:
  900. new_val.lattice_val = UNDEFINED;
  901. new_val.value = NULL_TREE;
  902. break;
  903. default:
  904. gcc_unreachable ();
  905. }
  906. for (i = 0; i < gimple_phi_num_args (phi); i++)
  907. {
  908. /* Compute the meet operator over all the PHI arguments flowing
  909. through executable edges. */
  910. edge e = gimple_phi_arg_edge (phi, i);
  911. if (dump_file && (dump_flags & TDF_DETAILS))
  912. {
  913. fprintf (dump_file,
  914. "\n Argument #%d (%d -> %d %sexecutable)\n",
  915. i, e->src->index, e->dest->index,
  916. (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
  917. }
  918. /* If the incoming edge is executable, Compute the meet operator for
  919. the existing value of the PHI node and the current PHI argument. */
  920. if (e->flags & EDGE_EXECUTABLE)
  921. {
  922. tree arg = gimple_phi_arg (phi, i)->def;
  923. ccp_prop_value_t arg_val = get_value_for_expr (arg, false);
  924. ccp_lattice_meet (&new_val, &arg_val);
  925. if (dump_file && (dump_flags & TDF_DETAILS))
  926. {
  927. fprintf (dump_file, "\t");
  928. print_generic_expr (dump_file, arg, dump_flags);
  929. dump_lattice_value (dump_file, "\tValue: ", arg_val);
  930. fprintf (dump_file, "\n");
  931. }
  932. if (new_val.lattice_val == VARYING)
  933. break;
  934. }
  935. }
  936. if (dump_file && (dump_flags & TDF_DETAILS))
  937. {
  938. dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
  939. fprintf (dump_file, "\n\n");
  940. }
  941. /* Make the transition to the new value. */
  942. if (set_lattice_value (gimple_phi_result (phi), new_val))
  943. {
  944. if (new_val.lattice_val == VARYING)
  945. return SSA_PROP_VARYING;
  946. else
  947. return SSA_PROP_INTERESTING;
  948. }
  949. else
  950. return SSA_PROP_NOT_INTERESTING;
  951. }
  952. /* Return the constant value for OP or OP otherwise. */
  953. static tree
  954. valueize_op (tree op)
  955. {
  956. if (TREE_CODE (op) == SSA_NAME)
  957. {
  958. tree tem = get_constant_value (op);
  959. if (tem)
  960. return tem;
  961. }
  962. return op;
  963. }
  964. /* Return the constant value for OP, but signal to not follow SSA
  965. edges if the definition may be simulated again. */
  966. static tree
  967. valueize_op_1 (tree op)
  968. {
  969. if (TREE_CODE (op) == SSA_NAME)
  970. {
  971. /* If the definition may be simulated again we cannot follow
  972. this SSA edge as the SSA propagator does not necessarily
  973. re-visit the use. */
  974. gimple def_stmt = SSA_NAME_DEF_STMT (op);
  975. if (!gimple_nop_p (def_stmt)
  976. && prop_simulate_again_p (def_stmt))
  977. return NULL_TREE;
  978. tree tem = get_constant_value (op);
  979. if (tem)
  980. return tem;
  981. }
  982. return op;
  983. }
  984. /* CCP specific front-end to the non-destructive constant folding
  985. routines.
  986. Attempt to simplify the RHS of STMT knowing that one or more
  987. operands are constants.
  988. If simplification is possible, return the simplified RHS,
  989. otherwise return the original RHS or NULL_TREE. */
  990. static tree
  991. ccp_fold (gimple stmt)
  992. {
  993. location_t loc = gimple_location (stmt);
  994. switch (gimple_code (stmt))
  995. {
  996. case GIMPLE_COND:
  997. {
  998. /* Handle comparison operators that can appear in GIMPLE form. */
  999. tree op0 = valueize_op (gimple_cond_lhs (stmt));
  1000. tree op1 = valueize_op (gimple_cond_rhs (stmt));
  1001. enum tree_code code = gimple_cond_code (stmt);
  1002. return fold_binary_loc (loc, code, boolean_type_node, op0, op1);
  1003. }
  1004. case GIMPLE_SWITCH:
  1005. {
  1006. /* Return the constant switch index. */
  1007. return valueize_op (gimple_switch_index (as_a <gswitch *> (stmt)));
  1008. }
  1009. case GIMPLE_ASSIGN:
  1010. case GIMPLE_CALL:
  1011. return gimple_fold_stmt_to_constant_1 (stmt,
  1012. valueize_op, valueize_op_1);
  1013. default:
  1014. gcc_unreachable ();
  1015. }
  1016. }
  1017. /* Apply the operation CODE in type TYPE to the value, mask pair
  1018. RVAL and RMASK representing a value of type RTYPE and set
  1019. the value, mask pair *VAL and *MASK to the result. */
  1020. static void
  1021. bit_value_unop_1 (enum tree_code code, tree type,
  1022. widest_int *val, widest_int *mask,
  1023. tree rtype, const widest_int &rval, const widest_int &rmask)
  1024. {
  1025. switch (code)
  1026. {
  1027. case BIT_NOT_EXPR:
  1028. *mask = rmask;
  1029. *val = ~rval;
  1030. break;
  1031. case NEGATE_EXPR:
  1032. {
  1033. widest_int temv, temm;
  1034. /* Return ~rval + 1. */
  1035. bit_value_unop_1 (BIT_NOT_EXPR, type, &temv, &temm, type, rval, rmask);
  1036. bit_value_binop_1 (PLUS_EXPR, type, val, mask,
  1037. type, temv, temm, type, 1, 0);
  1038. break;
  1039. }
  1040. CASE_CONVERT:
  1041. {
  1042. signop sgn;
  1043. /* First extend mask and value according to the original type. */
  1044. sgn = TYPE_SIGN (rtype);
  1045. *mask = wi::ext (rmask, TYPE_PRECISION (rtype), sgn);
  1046. *val = wi::ext (rval, TYPE_PRECISION (rtype), sgn);
  1047. /* Then extend mask and value according to the target type. */
  1048. sgn = TYPE_SIGN (type);
  1049. *mask = wi::ext (*mask, TYPE_PRECISION (type), sgn);
  1050. *val = wi::ext (*val, TYPE_PRECISION (type), sgn);
  1051. break;
  1052. }
  1053. default:
  1054. *mask = -1;
  1055. break;
  1056. }
  1057. }
  1058. /* Apply the operation CODE in type TYPE to the value, mask pairs
  1059. R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
  1060. and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
  1061. static void
  1062. bit_value_binop_1 (enum tree_code code, tree type,
  1063. widest_int *val, widest_int *mask,
  1064. tree r1type, const widest_int &r1val,
  1065. const widest_int &r1mask, tree r2type,
  1066. const widest_int &r2val, const widest_int &r2mask)
  1067. {
  1068. signop sgn = TYPE_SIGN (type);
  1069. int width = TYPE_PRECISION (type);
  1070. bool swap_p = false;
  1071. /* Assume we'll get a constant result. Use an initial non varying
  1072. value, we fall back to varying in the end if necessary. */
  1073. *mask = -1;
  1074. switch (code)
  1075. {
  1076. case BIT_AND_EXPR:
  1077. /* The mask is constant where there is a known not
  1078. set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
  1079. *mask = (r1mask | r2mask) & (r1val | r1mask) & (r2val | r2mask);
  1080. *val = r1val & r2val;
  1081. break;
  1082. case BIT_IOR_EXPR:
  1083. /* The mask is constant where there is a known
  1084. set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
  1085. *mask = (r1mask | r2mask)
  1086. .and_not (r1val.and_not (r1mask) | r2val.and_not (r2mask));
  1087. *val = r1val | r2val;
  1088. break;
  1089. case BIT_XOR_EXPR:
  1090. /* m1 | m2 */
  1091. *mask = r1mask | r2mask;
  1092. *val = r1val ^ r2val;
  1093. break;
  1094. case LROTATE_EXPR:
  1095. case RROTATE_EXPR:
  1096. if (r2mask == 0)
  1097. {
  1098. widest_int shift = r2val;
  1099. if (shift == 0)
  1100. {
  1101. *mask = r1mask;
  1102. *val = r1val;
  1103. }
  1104. else
  1105. {
  1106. if (wi::neg_p (shift))
  1107. {
  1108. shift = -shift;
  1109. if (code == RROTATE_EXPR)
  1110. code = LROTATE_EXPR;
  1111. else
  1112. code = RROTATE_EXPR;
  1113. }
  1114. if (code == RROTATE_EXPR)
  1115. {
  1116. *mask = wi::rrotate (r1mask, shift, width);
  1117. *val = wi::rrotate (r1val, shift, width);
  1118. }
  1119. else
  1120. {
  1121. *mask = wi::lrotate (r1mask, shift, width);
  1122. *val = wi::lrotate (r1val, shift, width);
  1123. }
  1124. }
  1125. }
  1126. break;
  1127. case LSHIFT_EXPR:
  1128. case RSHIFT_EXPR:
  1129. /* ??? We can handle partially known shift counts if we know
  1130. its sign. That way we can tell that (x << (y | 8)) & 255
  1131. is zero. */
  1132. if (r2mask == 0)
  1133. {
  1134. widest_int shift = r2val;
  1135. if (shift == 0)
  1136. {
  1137. *mask = r1mask;
  1138. *val = r1val;
  1139. }
  1140. else
  1141. {
  1142. if (wi::neg_p (shift))
  1143. {
  1144. shift = -shift;
  1145. if (code == RSHIFT_EXPR)
  1146. code = LSHIFT_EXPR;
  1147. else
  1148. code = RSHIFT_EXPR;
  1149. }
  1150. if (code == RSHIFT_EXPR)
  1151. {
  1152. *mask = wi::rshift (wi::ext (r1mask, width, sgn), shift, sgn);
  1153. *val = wi::rshift (wi::ext (r1val, width, sgn), shift, sgn);
  1154. }
  1155. else
  1156. {
  1157. *mask = wi::ext (wi::lshift (r1mask, shift), width, sgn);
  1158. *val = wi::ext (wi::lshift (r1val, shift), width, sgn);
  1159. }
  1160. }
  1161. }
  1162. break;
  1163. case PLUS_EXPR:
  1164. case POINTER_PLUS_EXPR:
  1165. {
  1166. /* Do the addition with unknown bits set to zero, to give carry-ins of
  1167. zero wherever possible. */
  1168. widest_int lo = r1val.and_not (r1mask) + r2val.and_not (r2mask);
  1169. lo = wi::ext (lo, width, sgn);
  1170. /* Do the addition with unknown bits set to one, to give carry-ins of
  1171. one wherever possible. */
  1172. widest_int hi = (r1val | r1mask) + (r2val | r2mask);
  1173. hi = wi::ext (hi, width, sgn);
  1174. /* Each bit in the result is known if (a) the corresponding bits in
  1175. both inputs are known, and (b) the carry-in to that bit position
  1176. is known. We can check condition (b) by seeing if we got the same
  1177. result with minimised carries as with maximised carries. */
  1178. *mask = r1mask | r2mask | (lo ^ hi);
  1179. *mask = wi::ext (*mask, width, sgn);
  1180. /* It shouldn't matter whether we choose lo or hi here. */
  1181. *val = lo;
  1182. break;
  1183. }
  1184. case MINUS_EXPR:
  1185. {
  1186. widest_int temv, temm;
  1187. bit_value_unop_1 (NEGATE_EXPR, r2type, &temv, &temm,
  1188. r2type, r2val, r2mask);
  1189. bit_value_binop_1 (PLUS_EXPR, type, val, mask,
  1190. r1type, r1val, r1mask,
  1191. r2type, temv, temm);
  1192. break;
  1193. }
  1194. case MULT_EXPR:
  1195. {
  1196. /* Just track trailing zeros in both operands and transfer
  1197. them to the other. */
  1198. int r1tz = wi::ctz (r1val | r1mask);
  1199. int r2tz = wi::ctz (r2val | r2mask);
  1200. if (r1tz + r2tz >= width)
  1201. {
  1202. *mask = 0;
  1203. *val = 0;
  1204. }
  1205. else if (r1tz + r2tz > 0)
  1206. {
  1207. *mask = wi::ext (wi::mask <widest_int> (r1tz + r2tz, true),
  1208. width, sgn);
  1209. *val = 0;
  1210. }
  1211. break;
  1212. }
  1213. case EQ_EXPR:
  1214. case NE_EXPR:
  1215. {
  1216. widest_int m = r1mask | r2mask;
  1217. if (r1val.and_not (m) != r2val.and_not (m))
  1218. {
  1219. *mask = 0;
  1220. *val = ((code == EQ_EXPR) ? 0 : 1);
  1221. }
  1222. else
  1223. {
  1224. /* We know the result of a comparison is always one or zero. */
  1225. *mask = 1;
  1226. *val = 0;
  1227. }
  1228. break;
  1229. }
  1230. case GE_EXPR:
  1231. case GT_EXPR:
  1232. swap_p = true;
  1233. code = swap_tree_comparison (code);
  1234. /* Fall through. */
  1235. case LT_EXPR:
  1236. case LE_EXPR:
  1237. {
  1238. int minmax, maxmin;
  1239. const widest_int &o1val = swap_p ? r2val : r1val;
  1240. const widest_int &o1mask = swap_p ? r2mask : r1mask;
  1241. const widest_int &o2val = swap_p ? r1val : r2val;
  1242. const widest_int &o2mask = swap_p ? r1mask : r2mask;
  1243. /* If the most significant bits are not known we know nothing. */
  1244. if (wi::neg_p (o1mask) || wi::neg_p (o2mask))
  1245. break;
  1246. /* For comparisons the signedness is in the comparison operands. */
  1247. sgn = TYPE_SIGN (r1type);
  1248. /* If we know the most significant bits we know the values
  1249. value ranges by means of treating varying bits as zero
  1250. or one. Do a cross comparison of the max/min pairs. */
  1251. maxmin = wi::cmp (o1val | o1mask, o2val.and_not (o2mask), sgn);
  1252. minmax = wi::cmp (o1val.and_not (o1mask), o2val | o2mask, sgn);
  1253. if (maxmin < 0) /* o1 is less than o2. */
  1254. {
  1255. *mask = 0;
  1256. *val = 1;
  1257. }
  1258. else if (minmax > 0) /* o1 is not less or equal to o2. */
  1259. {
  1260. *mask = 0;
  1261. *val = 0;
  1262. }
  1263. else if (maxmin == minmax) /* o1 and o2 are equal. */
  1264. {
  1265. /* This probably should never happen as we'd have
  1266. folded the thing during fully constant value folding. */
  1267. *mask = 0;
  1268. *val = (code == LE_EXPR ? 1 : 0);
  1269. }
  1270. else
  1271. {
  1272. /* We know the result of a comparison is always one or zero. */
  1273. *mask = 1;
  1274. *val = 0;
  1275. }
  1276. break;
  1277. }
  1278. default:;
  1279. }
  1280. }
  1281. /* Return the propagation value when applying the operation CODE to
  1282. the value RHS yielding type TYPE. */
  1283. static ccp_prop_value_t
  1284. bit_value_unop (enum tree_code code, tree type, tree rhs)
  1285. {
  1286. ccp_prop_value_t rval = get_value_for_expr (rhs, true);
  1287. widest_int value, mask;
  1288. ccp_prop_value_t val;
  1289. if (rval.lattice_val == UNDEFINED)
  1290. return rval;
  1291. gcc_assert ((rval.lattice_val == CONSTANT
  1292. && TREE_CODE (rval.value) == INTEGER_CST)
  1293. || rval.mask == -1);
  1294. bit_value_unop_1 (code, type, &value, &mask,
  1295. TREE_TYPE (rhs), value_to_wide_int (rval), rval.mask);
  1296. if (mask != -1)
  1297. {
  1298. val.lattice_val = CONSTANT;
  1299. val.mask = mask;
  1300. /* ??? Delay building trees here. */
  1301. val.value = wide_int_to_tree (type, value);
  1302. }
  1303. else
  1304. {
  1305. val.lattice_val = VARYING;
  1306. val.value = NULL_TREE;
  1307. val.mask = -1;
  1308. }
  1309. return val;
  1310. }
  1311. /* Return the propagation value when applying the operation CODE to
  1312. the values RHS1 and RHS2 yielding type TYPE. */
  1313. static ccp_prop_value_t
  1314. bit_value_binop (enum tree_code code, tree type, tree rhs1, tree rhs2)
  1315. {
  1316. ccp_prop_value_t r1val = get_value_for_expr (rhs1, true);
  1317. ccp_prop_value_t r2val = get_value_for_expr (rhs2, true);
  1318. widest_int value, mask;
  1319. ccp_prop_value_t val;
  1320. if (r1val.lattice_val == UNDEFINED
  1321. || r2val.lattice_val == UNDEFINED)
  1322. {
  1323. val.lattice_val = VARYING;
  1324. val.value = NULL_TREE;
  1325. val.mask = -1;
  1326. return val;
  1327. }
  1328. gcc_assert ((r1val.lattice_val == CONSTANT
  1329. && TREE_CODE (r1val.value) == INTEGER_CST)
  1330. || r1val.mask == -1);
  1331. gcc_assert ((r2val.lattice_val == CONSTANT
  1332. && TREE_CODE (r2val.value) == INTEGER_CST)
  1333. || r2val.mask == -1);
  1334. bit_value_binop_1 (code, type, &value, &mask,
  1335. TREE_TYPE (rhs1), value_to_wide_int (r1val), r1val.mask,
  1336. TREE_TYPE (rhs2), value_to_wide_int (r2val), r2val.mask);
  1337. if (mask != -1)
  1338. {
  1339. val.lattice_val = CONSTANT;
  1340. val.mask = mask;
  1341. /* ??? Delay building trees here. */
  1342. val.value = wide_int_to_tree (type, value);
  1343. }
  1344. else
  1345. {
  1346. val.lattice_val = VARYING;
  1347. val.value = NULL_TREE;
  1348. val.mask = -1;
  1349. }
  1350. return val;
  1351. }
  1352. /* Return the propagation value for __builtin_assume_aligned
  1353. and functions with assume_aligned or alloc_aligned attribute.
  1354. For __builtin_assume_aligned, ATTR is NULL_TREE,
  1355. for assume_aligned attribute ATTR is non-NULL and ALLOC_ALIGNED
  1356. is false, for alloc_aligned attribute ATTR is non-NULL and
  1357. ALLOC_ALIGNED is true. */
  1358. static ccp_prop_value_t
  1359. bit_value_assume_aligned (gimple stmt, tree attr, ccp_prop_value_t ptrval,
  1360. bool alloc_aligned)
  1361. {
  1362. tree align, misalign = NULL_TREE, type;
  1363. unsigned HOST_WIDE_INT aligni, misaligni = 0;
  1364. ccp_prop_value_t alignval;
  1365. widest_int value, mask;
  1366. ccp_prop_value_t val;
  1367. if (attr == NULL_TREE)
  1368. {
  1369. tree ptr = gimple_call_arg (stmt, 0);
  1370. type = TREE_TYPE (ptr);
  1371. ptrval = get_value_for_expr (ptr, true);
  1372. }
  1373. else
  1374. {
  1375. tree lhs = gimple_call_lhs (stmt);
  1376. type = TREE_TYPE (lhs);
  1377. }
  1378. if (ptrval.lattice_val == UNDEFINED)
  1379. return ptrval;
  1380. gcc_assert ((ptrval.lattice_val == CONSTANT
  1381. && TREE_CODE (ptrval.value) == INTEGER_CST)
  1382. || ptrval.mask == -1);
  1383. if (attr == NULL_TREE)
  1384. {
  1385. /* Get aligni and misaligni from __builtin_assume_aligned. */
  1386. align = gimple_call_arg (stmt, 1);
  1387. if (!tree_fits_uhwi_p (align))
  1388. return ptrval;
  1389. aligni = tree_to_uhwi (align);
  1390. if (gimple_call_num_args (stmt) > 2)
  1391. {
  1392. misalign = gimple_call_arg (stmt, 2);
  1393. if (!tree_fits_uhwi_p (misalign))
  1394. return ptrval;
  1395. misaligni = tree_to_uhwi (misalign);
  1396. }
  1397. }
  1398. else
  1399. {
  1400. /* Get aligni and misaligni from assume_aligned or
  1401. alloc_align attributes. */
  1402. if (TREE_VALUE (attr) == NULL_TREE)
  1403. return ptrval;
  1404. attr = TREE_VALUE (attr);
  1405. align = TREE_VALUE (attr);
  1406. if (!tree_fits_uhwi_p (align))
  1407. return ptrval;
  1408. aligni = tree_to_uhwi (align);
  1409. if (alloc_aligned)
  1410. {
  1411. if (aligni == 0 || aligni > gimple_call_num_args (stmt))
  1412. return ptrval;
  1413. align = gimple_call_arg (stmt, aligni - 1);
  1414. if (!tree_fits_uhwi_p (align))
  1415. return ptrval;
  1416. aligni = tree_to_uhwi (align);
  1417. }
  1418. else if (TREE_CHAIN (attr) && TREE_VALUE (TREE_CHAIN (attr)))
  1419. {
  1420. misalign = TREE_VALUE (TREE_CHAIN (attr));
  1421. if (!tree_fits_uhwi_p (misalign))
  1422. return ptrval;
  1423. misaligni = tree_to_uhwi (misalign);
  1424. }
  1425. }
  1426. if (aligni <= 1 || (aligni & (aligni - 1)) != 0 || misaligni >= aligni)
  1427. return ptrval;
  1428. align = build_int_cst_type (type, -aligni);
  1429. alignval = get_value_for_expr (align, true);
  1430. bit_value_binop_1 (BIT_AND_EXPR, type, &value, &mask,
  1431. type, value_to_wide_int (ptrval), ptrval.mask,
  1432. type, value_to_wide_int (alignval), alignval.mask);
  1433. if (mask != -1)
  1434. {
  1435. val.lattice_val = CONSTANT;
  1436. val.mask = mask;
  1437. gcc_assert ((mask.to_uhwi () & (aligni - 1)) == 0);
  1438. gcc_assert ((value.to_uhwi () & (aligni - 1)) == 0);
  1439. value |= misaligni;
  1440. /* ??? Delay building trees here. */
  1441. val.value = wide_int_to_tree (type, value);
  1442. }
  1443. else
  1444. {
  1445. val.lattice_val = VARYING;
  1446. val.value = NULL_TREE;
  1447. val.mask = -1;
  1448. }
  1449. return val;
  1450. }
  1451. /* Evaluate statement STMT.
  1452. Valid only for assignments, calls, conditionals, and switches. */
  1453. static ccp_prop_value_t
  1454. evaluate_stmt (gimple stmt)
  1455. {
  1456. ccp_prop_value_t val;
  1457. tree simplified = NULL_TREE;
  1458. ccp_lattice_t likelyvalue = likely_value (stmt);
  1459. bool is_constant = false;
  1460. unsigned int align;
  1461. if (dump_file && (dump_flags & TDF_DETAILS))
  1462. {
  1463. fprintf (dump_file, "which is likely ");
  1464. switch (likelyvalue)
  1465. {
  1466. case CONSTANT:
  1467. fprintf (dump_file, "CONSTANT");
  1468. break;
  1469. case UNDEFINED:
  1470. fprintf (dump_file, "UNDEFINED");
  1471. break;
  1472. case VARYING:
  1473. fprintf (dump_file, "VARYING");
  1474. break;
  1475. default:;
  1476. }
  1477. fprintf (dump_file, "\n");
  1478. }
  1479. /* If the statement is likely to have a CONSTANT result, then try
  1480. to fold the statement to determine the constant value. */
  1481. /* FIXME. This is the only place that we call ccp_fold.
  1482. Since likely_value never returns CONSTANT for calls, we will
  1483. not attempt to fold them, including builtins that may profit. */
  1484. if (likelyvalue == CONSTANT)
  1485. {
  1486. fold_defer_overflow_warnings ();
  1487. simplified = ccp_fold (stmt);
  1488. is_constant = simplified && is_gimple_min_invariant (simplified);
  1489. fold_undefer_overflow_warnings (is_constant, stmt, 0);
  1490. if (is_constant)
  1491. {
  1492. /* The statement produced a constant value. */
  1493. val.lattice_val = CONSTANT;
  1494. val.value = simplified;
  1495. val.mask = 0;
  1496. }
  1497. }
  1498. /* If the statement is likely to have a VARYING result, then do not
  1499. bother folding the statement. */
  1500. else if (likelyvalue == VARYING)
  1501. {
  1502. enum gimple_code code = gimple_code (stmt);
  1503. if (code == GIMPLE_ASSIGN)
  1504. {
  1505. enum tree_code subcode = gimple_assign_rhs_code (stmt);
  1506. /* Other cases cannot satisfy is_gimple_min_invariant
  1507. without folding. */
  1508. if (get_gimple_rhs_class (subcode) == GIMPLE_SINGLE_RHS)
  1509. simplified = gimple_assign_rhs1 (stmt);
  1510. }
  1511. else if (code == GIMPLE_SWITCH)
  1512. simplified = gimple_switch_index (as_a <gswitch *> (stmt));
  1513. else
  1514. /* These cannot satisfy is_gimple_min_invariant without folding. */
  1515. gcc_assert (code == GIMPLE_CALL || code == GIMPLE_COND);
  1516. is_constant = simplified && is_gimple_min_invariant (simplified);
  1517. if (is_constant)
  1518. {
  1519. /* The statement produced a constant value. */
  1520. val.lattice_val = CONSTANT;
  1521. val.value = simplified;
  1522. val.mask = 0;
  1523. }
  1524. }
  1525. /* Resort to simplification for bitwise tracking. */
  1526. if (flag_tree_bit_ccp
  1527. && (likelyvalue == CONSTANT || is_gimple_call (stmt))
  1528. && !is_constant)
  1529. {
  1530. enum gimple_code code = gimple_code (stmt);
  1531. val.lattice_val = VARYING;
  1532. val.value = NULL_TREE;
  1533. val.mask = -1;
  1534. if (code == GIMPLE_ASSIGN)
  1535. {
  1536. enum tree_code subcode = gimple_assign_rhs_code (stmt);
  1537. tree rhs1 = gimple_assign_rhs1 (stmt);
  1538. switch (get_gimple_rhs_class (subcode))
  1539. {
  1540. case GIMPLE_SINGLE_RHS:
  1541. if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
  1542. || POINTER_TYPE_P (TREE_TYPE (rhs1)))
  1543. val = get_value_for_expr (rhs1, true);
  1544. break;
  1545. case GIMPLE_UNARY_RHS:
  1546. if ((INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
  1547. || POINTER_TYPE_P (TREE_TYPE (rhs1)))
  1548. && (INTEGRAL_TYPE_P (gimple_expr_type (stmt))
  1549. || POINTER_TYPE_P (gimple_expr_type (stmt))))
  1550. val = bit_value_unop (subcode, gimple_expr_type (stmt), rhs1);
  1551. break;
  1552. case GIMPLE_BINARY_RHS:
  1553. if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
  1554. || POINTER_TYPE_P (TREE_TYPE (rhs1)))
  1555. {
  1556. tree lhs = gimple_assign_lhs (stmt);
  1557. tree rhs2 = gimple_assign_rhs2 (stmt);
  1558. val = bit_value_binop (subcode,
  1559. TREE_TYPE (lhs), rhs1, rhs2);
  1560. }
  1561. break;
  1562. default:;
  1563. }
  1564. }
  1565. else if (code == GIMPLE_COND)
  1566. {
  1567. enum tree_code code = gimple_cond_code (stmt);
  1568. tree rhs1 = gimple_cond_lhs (stmt);
  1569. tree rhs2 = gimple_cond_rhs (stmt);
  1570. if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
  1571. || POINTER_TYPE_P (TREE_TYPE (rhs1)))
  1572. val = bit_value_binop (code, TREE_TYPE (rhs1), rhs1, rhs2);
  1573. }
  1574. else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
  1575. {
  1576. tree fndecl = gimple_call_fndecl (stmt);
  1577. switch (DECL_FUNCTION_CODE (fndecl))
  1578. {
  1579. case BUILT_IN_MALLOC:
  1580. case BUILT_IN_REALLOC:
  1581. case BUILT_IN_CALLOC:
  1582. case BUILT_IN_STRDUP:
  1583. case BUILT_IN_STRNDUP:
  1584. val.lattice_val = CONSTANT;
  1585. val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
  1586. val.mask = ~((HOST_WIDE_INT) MALLOC_ABI_ALIGNMENT
  1587. / BITS_PER_UNIT - 1);
  1588. break;
  1589. case BUILT_IN_ALLOCA:
  1590. case BUILT_IN_ALLOCA_WITH_ALIGN:
  1591. align = (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN
  1592. ? TREE_INT_CST_LOW (gimple_call_arg (stmt, 1))
  1593. : BIGGEST_ALIGNMENT);
  1594. val.lattice_val = CONSTANT;
  1595. val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
  1596. val.mask = ~((HOST_WIDE_INT) align / BITS_PER_UNIT - 1);
  1597. break;
  1598. /* These builtins return their first argument, unmodified. */
  1599. case BUILT_IN_MEMCPY:
  1600. case BUILT_IN_MEMMOVE:
  1601. case BUILT_IN_MEMSET:
  1602. case BUILT_IN_STRCPY:
  1603. case BUILT_IN_STRNCPY:
  1604. case BUILT_IN_MEMCPY_CHK:
  1605. case BUILT_IN_MEMMOVE_CHK:
  1606. case BUILT_IN_MEMSET_CHK:
  1607. case BUILT_IN_STRCPY_CHK:
  1608. case BUILT_IN_STRNCPY_CHK:
  1609. val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
  1610. break;
  1611. case BUILT_IN_ASSUME_ALIGNED:
  1612. val = bit_value_assume_aligned (stmt, NULL_TREE, val, false);
  1613. break;
  1614. case BUILT_IN_ALIGNED_ALLOC:
  1615. {
  1616. tree align = get_constant_value (gimple_call_arg (stmt, 0));
  1617. if (align
  1618. && tree_fits_uhwi_p (align))
  1619. {
  1620. unsigned HOST_WIDE_INT aligni = tree_to_uhwi (align);
  1621. if (aligni > 1
  1622. /* align must be power-of-two */
  1623. && (aligni & (aligni - 1)) == 0)
  1624. {
  1625. val.lattice_val = CONSTANT;
  1626. val.value = build_int_cst (ptr_type_node, 0);
  1627. val.mask = -aligni;
  1628. }
  1629. }
  1630. break;
  1631. }
  1632. default:;
  1633. }
  1634. }
  1635. if (is_gimple_call (stmt) && gimple_call_lhs (stmt))
  1636. {
  1637. tree fntype = gimple_call_fntype (stmt);
  1638. if (fntype)
  1639. {
  1640. tree attrs = lookup_attribute ("assume_aligned",
  1641. TYPE_ATTRIBUTES (fntype));
  1642. if (attrs)
  1643. val = bit_value_assume_aligned (stmt, attrs, val, false);
  1644. attrs = lookup_attribute ("alloc_align",
  1645. TYPE_ATTRIBUTES (fntype));
  1646. if (attrs)
  1647. val = bit_value_assume_aligned (stmt, attrs, val, true);
  1648. }
  1649. }
  1650. is_constant = (val.lattice_val == CONSTANT);
  1651. }
  1652. if (flag_tree_bit_ccp
  1653. && ((is_constant && TREE_CODE (val.value) == INTEGER_CST)
  1654. || (!is_constant && likelyvalue != UNDEFINED))
  1655. && gimple_get_lhs (stmt)
  1656. && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME)
  1657. {
  1658. tree lhs = gimple_get_lhs (stmt);
  1659. wide_int nonzero_bits = get_nonzero_bits (lhs);
  1660. if (nonzero_bits != -1)
  1661. {
  1662. if (!is_constant)
  1663. {
  1664. val.lattice_val = CONSTANT;
  1665. val.value = build_zero_cst (TREE_TYPE (lhs));
  1666. val.mask = extend_mask (nonzero_bits);
  1667. is_constant = true;
  1668. }
  1669. else
  1670. {
  1671. if (wi::bit_and_not (val.value, nonzero_bits) != 0)
  1672. val.value = wide_int_to_tree (TREE_TYPE (lhs),
  1673. nonzero_bits & val.value);
  1674. if (nonzero_bits == 0)
  1675. val.mask = 0;
  1676. else
  1677. val.mask = val.mask & extend_mask (nonzero_bits);
  1678. }
  1679. }
  1680. }
  1681. if (!is_constant)
  1682. {
  1683. /* The statement produced a nonconstant value. If the statement
  1684. had UNDEFINED operands, then the result of the statement
  1685. should be UNDEFINED. Otherwise, the statement is VARYING. */
  1686. if (likelyvalue == UNDEFINED)
  1687. {
  1688. val.lattice_val = likelyvalue;
  1689. val.mask = 0;
  1690. }
  1691. else
  1692. {
  1693. val.lattice_val = VARYING;
  1694. val.mask = -1;
  1695. }
  1696. val.value = NULL_TREE;
  1697. }
  1698. return val;
  1699. }
  1700. typedef hash_table<pointer_hash<gimple_statement_base> > gimple_htab;
  1701. /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
  1702. each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
  1703. static void
  1704. insert_clobber_before_stack_restore (tree saved_val, tree var,
  1705. gimple_htab **visited)
  1706. {
  1707. gimple stmt;
  1708. gassign *clobber_stmt;
  1709. tree clobber;
  1710. imm_use_iterator iter;
  1711. gimple_stmt_iterator i;
  1712. gimple *slot;
  1713. FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
  1714. if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
  1715. {
  1716. clobber = build_constructor (TREE_TYPE (var),
  1717. NULL);
  1718. TREE_THIS_VOLATILE (clobber) = 1;
  1719. clobber_stmt = gimple_build_assign (var, clobber);
  1720. i = gsi_for_stmt (stmt);
  1721. gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
  1722. }
  1723. else if (gimple_code (stmt) == GIMPLE_PHI)
  1724. {
  1725. if (!*visited)
  1726. *visited = new gimple_htab (10);
  1727. slot = (*visited)->find_slot (stmt, INSERT);
  1728. if (*slot != NULL)
  1729. continue;
  1730. *slot = stmt;
  1731. insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
  1732. visited);
  1733. }
  1734. else if (gimple_assign_ssa_name_copy_p (stmt))
  1735. insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
  1736. visited);
  1737. else if (chkp_gimple_call_builtin_p (stmt, BUILT_IN_CHKP_BNDRET))
  1738. continue;
  1739. else
  1740. gcc_assert (is_gimple_debug (stmt));
  1741. }
  1742. /* Advance the iterator to the previous non-debug gimple statement in the same
  1743. or dominating basic block. */
  1744. static inline void
  1745. gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
  1746. {
  1747. basic_block dom;
  1748. gsi_prev_nondebug (i);
  1749. while (gsi_end_p (*i))
  1750. {
  1751. dom = get_immediate_dominator (CDI_DOMINATORS, i->bb);
  1752. if (dom == NULL || dom == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1753. return;
  1754. *i = gsi_last_bb (dom);
  1755. }
  1756. }
  1757. /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
  1758. a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
  1759. It is possible that BUILT_IN_STACK_SAVE cannot be find in a dominator when a
  1760. previous pass (such as DOM) duplicated it along multiple paths to a BB. In
  1761. that case the function gives up without inserting the clobbers. */
  1762. static void
  1763. insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
  1764. {
  1765. gimple stmt;
  1766. tree saved_val;
  1767. gimple_htab *visited = NULL;
  1768. for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
  1769. {
  1770. stmt = gsi_stmt (i);
  1771. if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
  1772. continue;
  1773. saved_val = gimple_call_lhs (stmt);
  1774. if (saved_val == NULL_TREE)
  1775. continue;
  1776. insert_clobber_before_stack_restore (saved_val, var, &visited);
  1777. break;
  1778. }
  1779. delete visited;
  1780. }
  1781. /* Detects a __builtin_alloca_with_align with constant size argument. Declares
  1782. fixed-size array and returns the address, if found, otherwise returns
  1783. NULL_TREE. */
  1784. static tree
  1785. fold_builtin_alloca_with_align (gimple stmt)
  1786. {
  1787. unsigned HOST_WIDE_INT size, threshold, n_elem;
  1788. tree lhs, arg, block, var, elem_type, array_type;
  1789. /* Get lhs. */
  1790. lhs = gimple_call_lhs (stmt);
  1791. if (lhs == NULL_TREE)
  1792. return NULL_TREE;
  1793. /* Detect constant argument. */
  1794. arg = get_constant_value (gimple_call_arg (stmt, 0));
  1795. if (arg == NULL_TREE
  1796. || TREE_CODE (arg) != INTEGER_CST
  1797. || !tree_fits_uhwi_p (arg))
  1798. return NULL_TREE;
  1799. size = tree_to_uhwi (arg);
  1800. /* Heuristic: don't fold large allocas. */
  1801. threshold = (unsigned HOST_WIDE_INT)PARAM_VALUE (PARAM_LARGE_STACK_FRAME);
  1802. /* In case the alloca is located at function entry, it has the same lifetime
  1803. as a declared array, so we allow a larger size. */
  1804. block = gimple_block (stmt);
  1805. if (!(cfun->after_inlining
  1806. && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
  1807. threshold /= 10;
  1808. if (size > threshold)
  1809. return NULL_TREE;
  1810. /* Declare array. */
  1811. elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
  1812. n_elem = size * 8 / BITS_PER_UNIT;
  1813. array_type = build_array_type_nelts (elem_type, n_elem);
  1814. var = create_tmp_var (array_type);
  1815. DECL_ALIGN (var) = TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
  1816. {
  1817. struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
  1818. if (pi != NULL && !pi->pt.anything)
  1819. {
  1820. bool singleton_p;
  1821. unsigned uid;
  1822. singleton_p = pt_solution_singleton_p (&pi->pt, &uid);
  1823. gcc_assert (singleton_p);
  1824. SET_DECL_PT_UID (var, uid);
  1825. }
  1826. }
  1827. /* Fold alloca to the address of the array. */
  1828. return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
  1829. }
  1830. /* Fold the stmt at *GSI with CCP specific information that propagating
  1831. and regular folding does not catch. */
  1832. static bool
  1833. ccp_fold_stmt (gimple_stmt_iterator *gsi)
  1834. {
  1835. gimple stmt = gsi_stmt (*gsi);
  1836. switch (gimple_code (stmt))
  1837. {
  1838. case GIMPLE_COND:
  1839. {
  1840. gcond *cond_stmt = as_a <gcond *> (stmt);
  1841. ccp_prop_value_t val;
  1842. /* Statement evaluation will handle type mismatches in constants
  1843. more gracefully than the final propagation. This allows us to
  1844. fold more conditionals here. */
  1845. val = evaluate_stmt (stmt);
  1846. if (val.lattice_val != CONSTANT
  1847. || val.mask != 0)
  1848. return false;
  1849. if (dump_file)
  1850. {
  1851. fprintf (dump_file, "Folding predicate ");
  1852. print_gimple_expr (dump_file, stmt, 0, 0);
  1853. fprintf (dump_file, " to ");
  1854. print_generic_expr (dump_file, val.value, 0);
  1855. fprintf (dump_file, "\n");
  1856. }
  1857. if (integer_zerop (val.value))
  1858. gimple_cond_make_false (cond_stmt);
  1859. else
  1860. gimple_cond_make_true (cond_stmt);
  1861. return true;
  1862. }
  1863. case GIMPLE_CALL:
  1864. {
  1865. tree lhs = gimple_call_lhs (stmt);
  1866. int flags = gimple_call_flags (stmt);
  1867. tree val;
  1868. tree argt;
  1869. bool changed = false;
  1870. unsigned i;
  1871. /* If the call was folded into a constant make sure it goes
  1872. away even if we cannot propagate into all uses because of
  1873. type issues. */
  1874. if (lhs
  1875. && TREE_CODE (lhs) == SSA_NAME
  1876. && (val = get_constant_value (lhs))
  1877. /* Don't optimize away calls that have side-effects. */
  1878. && (flags & (ECF_CONST|ECF_PURE)) != 0
  1879. && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
  1880. {
  1881. tree new_rhs = unshare_expr (val);
  1882. bool res;
  1883. if (!useless_type_conversion_p (TREE_TYPE (lhs),
  1884. TREE_TYPE (new_rhs)))
  1885. new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
  1886. res = update_call_from_tree (gsi, new_rhs);
  1887. gcc_assert (res);
  1888. return true;
  1889. }
  1890. /* Internal calls provide no argument types, so the extra laxity
  1891. for normal calls does not apply. */
  1892. if (gimple_call_internal_p (stmt))
  1893. return false;
  1894. /* The heuristic of fold_builtin_alloca_with_align differs before and
  1895. after inlining, so we don't require the arg to be changed into a
  1896. constant for folding, but just to be constant. */
  1897. if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
  1898. {
  1899. tree new_rhs = fold_builtin_alloca_with_align (stmt);
  1900. if (new_rhs)
  1901. {
  1902. bool res = update_call_from_tree (gsi, new_rhs);
  1903. tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
  1904. gcc_assert (res);
  1905. insert_clobbers_for_var (*gsi, var);
  1906. return true;
  1907. }
  1908. }
  1909. /* Propagate into the call arguments. Compared to replace_uses_in
  1910. this can use the argument slot types for type verification
  1911. instead of the current argument type. We also can safely
  1912. drop qualifiers here as we are dealing with constants anyway. */
  1913. argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
  1914. for (i = 0; i < gimple_call_num_args (stmt) && argt;
  1915. ++i, argt = TREE_CHAIN (argt))
  1916. {
  1917. tree arg = gimple_call_arg (stmt, i);
  1918. if (TREE_CODE (arg) == SSA_NAME
  1919. && (val = get_constant_value (arg))
  1920. && useless_type_conversion_p
  1921. (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
  1922. TYPE_MAIN_VARIANT (TREE_TYPE (val))))
  1923. {
  1924. gimple_call_set_arg (stmt, i, unshare_expr (val));
  1925. changed = true;
  1926. }
  1927. }
  1928. return changed;
  1929. }
  1930. case GIMPLE_ASSIGN:
  1931. {
  1932. tree lhs = gimple_assign_lhs (stmt);
  1933. tree val;
  1934. /* If we have a load that turned out to be constant replace it
  1935. as we cannot propagate into all uses in all cases. */
  1936. if (gimple_assign_single_p (stmt)
  1937. && TREE_CODE (lhs) == SSA_NAME
  1938. && (val = get_constant_value (lhs)))
  1939. {
  1940. tree rhs = unshare_expr (val);
  1941. if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
  1942. rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
  1943. gimple_assign_set_rhs_from_tree (gsi, rhs);
  1944. return true;
  1945. }
  1946. return false;
  1947. }
  1948. default:
  1949. return false;
  1950. }
  1951. }
  1952. /* Visit the assignment statement STMT. Set the value of its LHS to the
  1953. value computed by the RHS and store LHS in *OUTPUT_P. If STMT
  1954. creates virtual definitions, set the value of each new name to that
  1955. of the RHS (if we can derive a constant out of the RHS).
  1956. Value-returning call statements also perform an assignment, and
  1957. are handled here. */
  1958. static enum ssa_prop_result
  1959. visit_assignment (gimple stmt, tree *output_p)
  1960. {
  1961. ccp_prop_value_t val;
  1962. enum ssa_prop_result retval;
  1963. tree lhs = gimple_get_lhs (stmt);
  1964. gcc_assert (gimple_code (stmt) != GIMPLE_CALL
  1965. || gimple_call_lhs (stmt) != NULL_TREE);
  1966. if (gimple_assign_single_p (stmt)
  1967. && gimple_assign_rhs_code (stmt) == SSA_NAME)
  1968. /* For a simple copy operation, we copy the lattice values. */
  1969. val = *get_value (gimple_assign_rhs1 (stmt));
  1970. else
  1971. /* Evaluate the statement, which could be
  1972. either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
  1973. val = evaluate_stmt (stmt);
  1974. retval = SSA_PROP_NOT_INTERESTING;
  1975. /* Set the lattice value of the statement's output. */
  1976. if (TREE_CODE (lhs) == SSA_NAME)
  1977. {
  1978. /* If STMT is an assignment to an SSA_NAME, we only have one
  1979. value to set. */
  1980. if (set_lattice_value (lhs, val))
  1981. {
  1982. *output_p = lhs;
  1983. if (val.lattice_val == VARYING)
  1984. retval = SSA_PROP_VARYING;
  1985. else
  1986. retval = SSA_PROP_INTERESTING;
  1987. }
  1988. }
  1989. return retval;
  1990. }
  1991. /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
  1992. if it can determine which edge will be taken. Otherwise, return
  1993. SSA_PROP_VARYING. */
  1994. static enum ssa_prop_result
  1995. visit_cond_stmt (gimple stmt, edge *taken_edge_p)
  1996. {
  1997. ccp_prop_value_t val;
  1998. basic_block block;
  1999. block = gimple_bb (stmt);
  2000. val = evaluate_stmt (stmt);
  2001. if (val.lattice_val != CONSTANT
  2002. || val.mask != 0)
  2003. return SSA_PROP_VARYING;
  2004. /* Find which edge out of the conditional block will be taken and add it
  2005. to the worklist. If no single edge can be determined statically,
  2006. return SSA_PROP_VARYING to feed all the outgoing edges to the
  2007. propagation engine. */
  2008. *taken_edge_p = find_taken_edge (block, val.value);
  2009. if (*taken_edge_p)
  2010. return SSA_PROP_INTERESTING;
  2011. else
  2012. return SSA_PROP_VARYING;
  2013. }
  2014. /* Evaluate statement STMT. If the statement produces an output value and
  2015. its evaluation changes the lattice value of its output, return
  2016. SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
  2017. output value.
  2018. If STMT is a conditional branch and we can determine its truth
  2019. value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
  2020. value, return SSA_PROP_VARYING. */
  2021. static enum ssa_prop_result
  2022. ccp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
  2023. {
  2024. tree def;
  2025. ssa_op_iter iter;
  2026. if (dump_file && (dump_flags & TDF_DETAILS))
  2027. {
  2028. fprintf (dump_file, "\nVisiting statement:\n");
  2029. print_gimple_stmt (dump_file, stmt, 0, dump_flags);
  2030. }
  2031. switch (gimple_code (stmt))
  2032. {
  2033. case GIMPLE_ASSIGN:
  2034. /* If the statement is an assignment that produces a single
  2035. output value, evaluate its RHS to see if the lattice value of
  2036. its output has changed. */
  2037. return visit_assignment (stmt, output_p);
  2038. case GIMPLE_CALL:
  2039. /* A value-returning call also performs an assignment. */
  2040. if (gimple_call_lhs (stmt) != NULL_TREE)
  2041. return visit_assignment (stmt, output_p);
  2042. break;
  2043. case GIMPLE_COND:
  2044. case GIMPLE_SWITCH:
  2045. /* If STMT is a conditional branch, see if we can determine
  2046. which branch will be taken. */
  2047. /* FIXME. It appears that we should be able to optimize
  2048. computed GOTOs here as well. */
  2049. return visit_cond_stmt (stmt, taken_edge_p);
  2050. default:
  2051. break;
  2052. }
  2053. /* Any other kind of statement is not interesting for constant
  2054. propagation and, therefore, not worth simulating. */
  2055. if (dump_file && (dump_flags & TDF_DETAILS))
  2056. fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
  2057. /* Definitions made by statements other than assignments to
  2058. SSA_NAMEs represent unknown modifications to their outputs.
  2059. Mark them VARYING. */
  2060. FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
  2061. {
  2062. ccp_prop_value_t v = { VARYING, NULL_TREE, -1 };
  2063. set_lattice_value (def, v);
  2064. }
  2065. return SSA_PROP_VARYING;
  2066. }
  2067. /* Main entry point for SSA Conditional Constant Propagation. */
  2068. static unsigned int
  2069. do_ssa_ccp (void)
  2070. {
  2071. unsigned int todo = 0;
  2072. calculate_dominance_info (CDI_DOMINATORS);
  2073. ccp_initialize ();
  2074. ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
  2075. if (ccp_finalize ())
  2076. todo = (TODO_cleanup_cfg | TODO_update_ssa);
  2077. free_dominance_info (CDI_DOMINATORS);
  2078. return todo;
  2079. }
  2080. namespace {
  2081. const pass_data pass_data_ccp =
  2082. {
  2083. GIMPLE_PASS, /* type */
  2084. "ccp", /* name */
  2085. OPTGROUP_NONE, /* optinfo_flags */
  2086. TV_TREE_CCP, /* tv_id */
  2087. ( PROP_cfg | PROP_ssa ), /* properties_required */
  2088. 0, /* properties_provided */
  2089. 0, /* properties_destroyed */
  2090. 0, /* todo_flags_start */
  2091. TODO_update_address_taken, /* todo_flags_finish */
  2092. };
  2093. class pass_ccp : public gimple_opt_pass
  2094. {
  2095. public:
  2096. pass_ccp (gcc::context *ctxt)
  2097. : gimple_opt_pass (pass_data_ccp, ctxt)
  2098. {}
  2099. /* opt_pass methods: */
  2100. opt_pass * clone () { return new pass_ccp (m_ctxt); }
  2101. virtual bool gate (function *) { return flag_tree_ccp != 0; }
  2102. virtual unsigned int execute (function *) { return do_ssa_ccp (); }
  2103. }; // class pass_ccp
  2104. } // anon namespace
  2105. gimple_opt_pass *
  2106. make_pass_ccp (gcc::context *ctxt)
  2107. {
  2108. return new pass_ccp (ctxt);
  2109. }
  2110. /* Try to optimize out __builtin_stack_restore. Optimize it out
  2111. if there is another __builtin_stack_restore in the same basic
  2112. block and no calls or ASM_EXPRs are in between, or if this block's
  2113. only outgoing edge is to EXIT_BLOCK and there are no calls or
  2114. ASM_EXPRs after this __builtin_stack_restore. */
  2115. static tree
  2116. optimize_stack_restore (gimple_stmt_iterator i)
  2117. {
  2118. tree callee;
  2119. gimple stmt;
  2120. basic_block bb = gsi_bb (i);
  2121. gimple call = gsi_stmt (i);
  2122. if (gimple_code (call) != GIMPLE_CALL
  2123. || gimple_call_num_args (call) != 1
  2124. || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
  2125. || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
  2126. return NULL_TREE;
  2127. for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
  2128. {
  2129. stmt = gsi_stmt (i);
  2130. if (gimple_code (stmt) == GIMPLE_ASM)
  2131. return NULL_TREE;
  2132. if (gimple_code (stmt) != GIMPLE_CALL)
  2133. continue;
  2134. callee = gimple_call_fndecl (stmt);
  2135. if (!callee
  2136. || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
  2137. /* All regular builtins are ok, just obviously not alloca. */
  2138. || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
  2139. || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA_WITH_ALIGN)
  2140. return NULL_TREE;
  2141. if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE)
  2142. goto second_stack_restore;
  2143. }
  2144. if (!gsi_end_p (i))
  2145. return NULL_TREE;
  2146. /* Allow one successor of the exit block, or zero successors. */
  2147. switch (EDGE_COUNT (bb->succs))
  2148. {
  2149. case 0:
  2150. break;
  2151. case 1:
  2152. if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  2153. return NULL_TREE;
  2154. break;
  2155. default:
  2156. return NULL_TREE;
  2157. }
  2158. second_stack_restore:
  2159. /* If there's exactly one use, then zap the call to __builtin_stack_save.
  2160. If there are multiple uses, then the last one should remove the call.
  2161. In any case, whether the call to __builtin_stack_save can be removed
  2162. or not is irrelevant to removing the call to __builtin_stack_restore. */
  2163. if (has_single_use (gimple_call_arg (call, 0)))
  2164. {
  2165. gimple stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
  2166. if (is_gimple_call (stack_save))
  2167. {
  2168. callee = gimple_call_fndecl (stack_save);
  2169. if (callee
  2170. && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
  2171. && DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE)
  2172. {
  2173. gimple_stmt_iterator stack_save_gsi;
  2174. tree rhs;
  2175. stack_save_gsi = gsi_for_stmt (stack_save);
  2176. rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
  2177. update_call_from_tree (&stack_save_gsi, rhs);
  2178. }
  2179. }
  2180. }
  2181. /* No effect, so the statement will be deleted. */
  2182. return integer_zero_node;
  2183. }
  2184. /* If va_list type is a simple pointer and nothing special is needed,
  2185. optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
  2186. __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
  2187. pointer assignment. */
  2188. static tree
  2189. optimize_stdarg_builtin (gimple call)
  2190. {
  2191. tree callee, lhs, rhs, cfun_va_list;
  2192. bool va_list_simple_ptr;
  2193. location_t loc = gimple_location (call);
  2194. if (gimple_code (call) != GIMPLE_CALL)
  2195. return NULL_TREE;
  2196. callee = gimple_call_fndecl (call);
  2197. cfun_va_list = targetm.fn_abi_va_list (callee);
  2198. va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
  2199. && (TREE_TYPE (cfun_va_list) == void_type_node
  2200. || TREE_TYPE (cfun_va_list) == char_type_node);
  2201. switch (DECL_FUNCTION_CODE (callee))
  2202. {
  2203. case BUILT_IN_VA_START:
  2204. if (!va_list_simple_ptr
  2205. || targetm.expand_builtin_va_start != NULL
  2206. || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
  2207. return NULL_TREE;
  2208. if (gimple_call_num_args (call) != 2)
  2209. return NULL_TREE;
  2210. lhs = gimple_call_arg (call, 0);
  2211. if (!POINTER_TYPE_P (TREE_TYPE (lhs))
  2212. || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
  2213. != TYPE_MAIN_VARIANT (cfun_va_list))
  2214. return NULL_TREE;
  2215. lhs = build_fold_indirect_ref_loc (loc, lhs);
  2216. rhs = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_NEXT_ARG),
  2217. 1, integer_zero_node);
  2218. rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
  2219. return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
  2220. case BUILT_IN_VA_COPY:
  2221. if (!va_list_simple_ptr)
  2222. return NULL_TREE;
  2223. if (gimple_call_num_args (call) != 2)
  2224. return NULL_TREE;
  2225. lhs = gimple_call_arg (call, 0);
  2226. if (!POINTER_TYPE_P (TREE_TYPE (lhs))
  2227. || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
  2228. != TYPE_MAIN_VARIANT (cfun_va_list))
  2229. return NULL_TREE;
  2230. lhs = build_fold_indirect_ref_loc (loc, lhs);
  2231. rhs = gimple_call_arg (call, 1);
  2232. if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
  2233. != TYPE_MAIN_VARIANT (cfun_va_list))
  2234. return NULL_TREE;
  2235. rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
  2236. return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
  2237. case BUILT_IN_VA_END:
  2238. /* No effect, so the statement will be deleted. */
  2239. return integer_zero_node;
  2240. default:
  2241. gcc_unreachable ();
  2242. }
  2243. }
  2244. /* Attemp to make the block of __builtin_unreachable I unreachable by changing
  2245. the incoming jumps. Return true if at least one jump was changed. */
  2246. static bool
  2247. optimize_unreachable (gimple_stmt_iterator i)
  2248. {
  2249. basic_block bb = gsi_bb (i);
  2250. gimple_stmt_iterator gsi;
  2251. gimple stmt;
  2252. edge_iterator ei;
  2253. edge e;
  2254. bool ret;
  2255. if (flag_sanitize & SANITIZE_UNREACHABLE)
  2256. return false;
  2257. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  2258. {
  2259. stmt = gsi_stmt (gsi);
  2260. if (is_gimple_debug (stmt))
  2261. continue;
  2262. if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
  2263. {
  2264. /* Verify we do not need to preserve the label. */
  2265. if (FORCED_LABEL (gimple_label_label (label_stmt)))
  2266. return false;
  2267. continue;
  2268. }
  2269. /* Only handle the case that __builtin_unreachable is the first statement
  2270. in the block. We rely on DCE to remove stmts without side-effects
  2271. before __builtin_unreachable. */
  2272. if (gsi_stmt (gsi) != gsi_stmt (i))
  2273. return false;
  2274. }
  2275. ret = false;
  2276. FOR_EACH_EDGE (e, ei, bb->preds)
  2277. {
  2278. gsi = gsi_last_bb (e->src);
  2279. if (gsi_end_p (gsi))
  2280. continue;
  2281. stmt = gsi_stmt (gsi);
  2282. if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
  2283. {
  2284. if (e->flags & EDGE_TRUE_VALUE)
  2285. gimple_cond_make_false (cond_stmt);
  2286. else if (e->flags & EDGE_FALSE_VALUE)
  2287. gimple_cond_make_true (cond_stmt);
  2288. else
  2289. gcc_unreachable ();
  2290. update_stmt (cond_stmt);
  2291. }
  2292. else
  2293. {
  2294. /* Todo: handle other cases, f.i. switch statement. */
  2295. continue;
  2296. }
  2297. ret = true;
  2298. }
  2299. return ret;
  2300. }
  2301. /* A simple pass that attempts to fold all builtin functions. This pass
  2302. is run after we've propagated as many constants as we can. */
  2303. namespace {
  2304. const pass_data pass_data_fold_builtins =
  2305. {
  2306. GIMPLE_PASS, /* type */
  2307. "fab", /* name */
  2308. OPTGROUP_NONE, /* optinfo_flags */
  2309. TV_NONE, /* tv_id */
  2310. ( PROP_cfg | PROP_ssa ), /* properties_required */
  2311. 0, /* properties_provided */
  2312. 0, /* properties_destroyed */
  2313. 0, /* todo_flags_start */
  2314. TODO_update_ssa, /* todo_flags_finish */
  2315. };
  2316. class pass_fold_builtins : public gimple_opt_pass
  2317. {
  2318. public:
  2319. pass_fold_builtins (gcc::context *ctxt)
  2320. : gimple_opt_pass (pass_data_fold_builtins, ctxt)
  2321. {}
  2322. /* opt_pass methods: */
  2323. opt_pass * clone () { return new pass_fold_builtins (m_ctxt); }
  2324. virtual unsigned int execute (function *);
  2325. }; // class pass_fold_builtins
  2326. unsigned int
  2327. pass_fold_builtins::execute (function *fun)
  2328. {
  2329. bool cfg_changed = false;
  2330. basic_block bb;
  2331. unsigned int todoflags = 0;
  2332. FOR_EACH_BB_FN (bb, fun)
  2333. {
  2334. gimple_stmt_iterator i;
  2335. for (i = gsi_start_bb (bb); !gsi_end_p (i); )
  2336. {
  2337. gimple stmt, old_stmt;
  2338. tree callee;
  2339. enum built_in_function fcode;
  2340. stmt = gsi_stmt (i);
  2341. if (gimple_code (stmt) != GIMPLE_CALL)
  2342. {
  2343. /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
  2344. after the last GIMPLE DSE they aren't needed and might
  2345. unnecessarily keep the SSA_NAMEs live. */
  2346. if (gimple_clobber_p (stmt))
  2347. {
  2348. tree lhs = gimple_assign_lhs (stmt);
  2349. if (TREE_CODE (lhs) == MEM_REF
  2350. && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
  2351. {
  2352. unlink_stmt_vdef (stmt);
  2353. gsi_remove (&i, true);
  2354. release_defs (stmt);
  2355. continue;
  2356. }
  2357. }
  2358. gsi_next (&i);
  2359. continue;
  2360. }
  2361. callee = gimple_call_fndecl (stmt);
  2362. if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
  2363. {
  2364. gsi_next (&i);
  2365. continue;
  2366. }
  2367. fcode = DECL_FUNCTION_CODE (callee);
  2368. if (fold_stmt (&i))
  2369. ;
  2370. else
  2371. {
  2372. tree result = NULL_TREE;
  2373. switch (DECL_FUNCTION_CODE (callee))
  2374. {
  2375. case BUILT_IN_CONSTANT_P:
  2376. /* Resolve __builtin_constant_p. If it hasn't been
  2377. folded to integer_one_node by now, it's fairly
  2378. certain that the value simply isn't constant. */
  2379. result = integer_zero_node;
  2380. break;
  2381. case BUILT_IN_ASSUME_ALIGNED:
  2382. /* Remove __builtin_assume_aligned. */
  2383. result = gimple_call_arg (stmt, 0);
  2384. break;
  2385. case BUILT_IN_STACK_RESTORE:
  2386. result = optimize_stack_restore (i);
  2387. if (result)
  2388. break;
  2389. gsi_next (&i);
  2390. continue;
  2391. case BUILT_IN_UNREACHABLE:
  2392. if (optimize_unreachable (i))
  2393. cfg_changed = true;
  2394. break;
  2395. case BUILT_IN_VA_START:
  2396. case BUILT_IN_VA_END:
  2397. case BUILT_IN_VA_COPY:
  2398. /* These shouldn't be folded before pass_stdarg. */
  2399. result = optimize_stdarg_builtin (stmt);
  2400. if (result)
  2401. break;
  2402. /* FALLTHRU */
  2403. default:;
  2404. }
  2405. if (!result)
  2406. {
  2407. gsi_next (&i);
  2408. continue;
  2409. }
  2410. if (!update_call_from_tree (&i, result))
  2411. gimplify_and_update_call_from_tree (&i, result);
  2412. }
  2413. todoflags |= TODO_update_address_taken;
  2414. if (dump_file && (dump_flags & TDF_DETAILS))
  2415. {
  2416. fprintf (dump_file, "Simplified\n ");
  2417. print_gimple_stmt (dump_file, stmt, 0, dump_flags);
  2418. }
  2419. old_stmt = stmt;
  2420. stmt = gsi_stmt (i);
  2421. update_stmt (stmt);
  2422. if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)
  2423. && gimple_purge_dead_eh_edges (bb))
  2424. cfg_changed = true;
  2425. if (dump_file && (dump_flags & TDF_DETAILS))
  2426. {
  2427. fprintf (dump_file, "to\n ");
  2428. print_gimple_stmt (dump_file, stmt, 0, dump_flags);
  2429. fprintf (dump_file, "\n");
  2430. }
  2431. /* Retry the same statement if it changed into another
  2432. builtin, there might be new opportunities now. */
  2433. if (gimple_code (stmt) != GIMPLE_CALL)
  2434. {
  2435. gsi_next (&i);
  2436. continue;
  2437. }
  2438. callee = gimple_call_fndecl (stmt);
  2439. if (!callee
  2440. || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
  2441. || DECL_FUNCTION_CODE (callee) == fcode)
  2442. gsi_next (&i);
  2443. }
  2444. }
  2445. /* Delete unreachable blocks. */
  2446. if (cfg_changed)
  2447. todoflags |= TODO_cleanup_cfg;
  2448. return todoflags;
  2449. }
  2450. } // anon namespace
  2451. gimple_opt_pass *
  2452. make_pass_fold_builtins (gcc::context *ctxt)
  2453. {
  2454. return new pass_fold_builtins (ctxt);
  2455. }