lower-subreg.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  1. /* Decompose multiword subregs.
  2. Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. Contributed by Richard Henderson <rth@redhat.com>
  4. Ian Lance Taylor <iant@google.com>
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. 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. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "machmode.h"
  21. #include "tm.h"
  22. #include "hash-set.h"
  23. #include "vec.h"
  24. #include "double-int.h"
  25. #include "input.h"
  26. #include "alias.h"
  27. #include "symtab.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "rtl.h"
  32. #include "tm_p.h"
  33. #include "flags.h"
  34. #include "insn-config.h"
  35. #include "obstack.h"
  36. #include "predict.h"
  37. #include "hard-reg-set.h"
  38. #include "function.h"
  39. #include "dominance.h"
  40. #include "cfg.h"
  41. #include "cfgrtl.h"
  42. #include "cfgbuild.h"
  43. #include "basic-block.h"
  44. #include "recog.h"
  45. #include "bitmap.h"
  46. #include "dce.h"
  47. #include "hashtab.h"
  48. #include "statistics.h"
  49. #include "real.h"
  50. #include "fixed-value.h"
  51. #include "expmed.h"
  52. #include "dojump.h"
  53. #include "explow.h"
  54. #include "calls.h"
  55. #include "emit-rtl.h"
  56. #include "varasm.h"
  57. #include "stmt.h"
  58. #include "expr.h"
  59. #include "except.h"
  60. #include "regs.h"
  61. #include "tree-pass.h"
  62. #include "df.h"
  63. #include "lower-subreg.h"
  64. #include "rtl-iter.h"
  65. #ifdef STACK_GROWS_DOWNWARD
  66. # undef STACK_GROWS_DOWNWARD
  67. # define STACK_GROWS_DOWNWARD 1
  68. #else
  69. # define STACK_GROWS_DOWNWARD 0
  70. #endif
  71. /* Decompose multi-word pseudo-registers into individual
  72. pseudo-registers when possible and profitable. This is possible
  73. when all the uses of a multi-word register are via SUBREG, or are
  74. copies of the register to another location. Breaking apart the
  75. register permits more CSE and permits better register allocation.
  76. This is profitable if the machine does not have move instructions
  77. to do this.
  78. This pass only splits moves with modes that are wider than
  79. word_mode and ASHIFTs, LSHIFTRTs, ASHIFTRTs and ZERO_EXTENDs with
  80. integer modes that are twice the width of word_mode. The latter
  81. could be generalized if there was a need to do this, but the trend in
  82. architectures is to not need this.
  83. There are two useful preprocessor defines for use by maintainers:
  84. #define LOG_COSTS 1
  85. if you wish to see the actual cost estimates that are being used
  86. for each mode wider than word mode and the cost estimates for zero
  87. extension and the shifts. This can be useful when port maintainers
  88. are tuning insn rtx costs.
  89. #define FORCE_LOWERING 1
  90. if you wish to test the pass with all the transformation forced on.
  91. This can be useful for finding bugs in the transformations. */
  92. #define LOG_COSTS 0
  93. #define FORCE_LOWERING 0
  94. /* Bit N in this bitmap is set if regno N is used in a context in
  95. which we can decompose it. */
  96. static bitmap decomposable_context;
  97. /* Bit N in this bitmap is set if regno N is used in a context in
  98. which it can not be decomposed. */
  99. static bitmap non_decomposable_context;
  100. /* Bit N in this bitmap is set if regno N is used in a subreg
  101. which changes the mode but not the size. This typically happens
  102. when the register accessed as a floating-point value; we want to
  103. avoid generating accesses to its subwords in integer modes. */
  104. static bitmap subreg_context;
  105. /* Bit N in the bitmap in element M of this array is set if there is a
  106. copy from reg M to reg N. */
  107. static vec<bitmap> reg_copy_graph;
  108. struct target_lower_subreg default_target_lower_subreg;
  109. #if SWITCHABLE_TARGET
  110. struct target_lower_subreg *this_target_lower_subreg
  111. = &default_target_lower_subreg;
  112. #endif
  113. #define twice_word_mode \
  114. this_target_lower_subreg->x_twice_word_mode
  115. #define choices \
  116. this_target_lower_subreg->x_choices
  117. /* RTXes used while computing costs. */
  118. struct cost_rtxes {
  119. /* Source and target registers. */
  120. rtx source;
  121. rtx target;
  122. /* A twice_word_mode ZERO_EXTEND of SOURCE. */
  123. rtx zext;
  124. /* A shift of SOURCE. */
  125. rtx shift;
  126. /* A SET of TARGET. */
  127. rtx set;
  128. };
  129. /* Return the cost of a CODE shift in mode MODE by OP1 bits, using the
  130. rtxes in RTXES. SPEED_P selects between the speed and size cost. */
  131. static int
  132. shift_cost (bool speed_p, struct cost_rtxes *rtxes, enum rtx_code code,
  133. machine_mode mode, int op1)
  134. {
  135. PUT_CODE (rtxes->shift, code);
  136. PUT_MODE (rtxes->shift, mode);
  137. PUT_MODE (rtxes->source, mode);
  138. XEXP (rtxes->shift, 1) = GEN_INT (op1);
  139. return set_src_cost (rtxes->shift, speed_p);
  140. }
  141. /* For each X in the range [0, BITS_PER_WORD), set SPLITTING[X]
  142. to true if it is profitable to split a double-word CODE shift
  143. of X + BITS_PER_WORD bits. SPEED_P says whether we are testing
  144. for speed or size profitability.
  145. Use the rtxes in RTXES to calculate costs. WORD_MOVE_ZERO_COST is
  146. the cost of moving zero into a word-mode register. WORD_MOVE_COST
  147. is the cost of moving between word registers. */
  148. static void
  149. compute_splitting_shift (bool speed_p, struct cost_rtxes *rtxes,
  150. bool *splitting, enum rtx_code code,
  151. int word_move_zero_cost, int word_move_cost)
  152. {
  153. int wide_cost, narrow_cost, upper_cost, i;
  154. for (i = 0; i < BITS_PER_WORD; i++)
  155. {
  156. wide_cost = shift_cost (speed_p, rtxes, code, twice_word_mode,
  157. i + BITS_PER_WORD);
  158. if (i == 0)
  159. narrow_cost = word_move_cost;
  160. else
  161. narrow_cost = shift_cost (speed_p, rtxes, code, word_mode, i);
  162. if (code != ASHIFTRT)
  163. upper_cost = word_move_zero_cost;
  164. else if (i == BITS_PER_WORD - 1)
  165. upper_cost = word_move_cost;
  166. else
  167. upper_cost = shift_cost (speed_p, rtxes, code, word_mode,
  168. BITS_PER_WORD - 1);
  169. if (LOG_COSTS)
  170. fprintf (stderr, "%s %s by %d: original cost %d, split cost %d + %d\n",
  171. GET_MODE_NAME (twice_word_mode), GET_RTX_NAME (code),
  172. i + BITS_PER_WORD, wide_cost, narrow_cost, upper_cost);
  173. if (FORCE_LOWERING || wide_cost >= narrow_cost + upper_cost)
  174. splitting[i] = true;
  175. }
  176. }
  177. /* Compute what we should do when optimizing for speed or size; SPEED_P
  178. selects which. Use RTXES for computing costs. */
  179. static void
  180. compute_costs (bool speed_p, struct cost_rtxes *rtxes)
  181. {
  182. unsigned int i;
  183. int word_move_zero_cost, word_move_cost;
  184. PUT_MODE (rtxes->target, word_mode);
  185. SET_SRC (rtxes->set) = CONST0_RTX (word_mode);
  186. word_move_zero_cost = set_rtx_cost (rtxes->set, speed_p);
  187. SET_SRC (rtxes->set) = rtxes->source;
  188. word_move_cost = set_rtx_cost (rtxes->set, speed_p);
  189. if (LOG_COSTS)
  190. fprintf (stderr, "%s move: from zero cost %d, from reg cost %d\n",
  191. GET_MODE_NAME (word_mode), word_move_zero_cost, word_move_cost);
  192. for (i = 0; i < MAX_MACHINE_MODE; i++)
  193. {
  194. machine_mode mode = (machine_mode) i;
  195. int factor = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
  196. if (factor > 1)
  197. {
  198. int mode_move_cost;
  199. PUT_MODE (rtxes->target, mode);
  200. PUT_MODE (rtxes->source, mode);
  201. mode_move_cost = set_rtx_cost (rtxes->set, speed_p);
  202. if (LOG_COSTS)
  203. fprintf (stderr, "%s move: original cost %d, split cost %d * %d\n",
  204. GET_MODE_NAME (mode), mode_move_cost,
  205. word_move_cost, factor);
  206. if (FORCE_LOWERING || mode_move_cost >= word_move_cost * factor)
  207. {
  208. choices[speed_p].move_modes_to_split[i] = true;
  209. choices[speed_p].something_to_do = true;
  210. }
  211. }
  212. }
  213. /* For the moves and shifts, the only case that is checked is one
  214. where the mode of the target is an integer mode twice the width
  215. of the word_mode.
  216. If it is not profitable to split a double word move then do not
  217. even consider the shifts or the zero extension. */
  218. if (choices[speed_p].move_modes_to_split[(int) twice_word_mode])
  219. {
  220. int zext_cost;
  221. /* The only case here to check to see if moving the upper part with a
  222. zero is cheaper than doing the zext itself. */
  223. PUT_MODE (rtxes->source, word_mode);
  224. zext_cost = set_src_cost (rtxes->zext, speed_p);
  225. if (LOG_COSTS)
  226. fprintf (stderr, "%s %s: original cost %d, split cost %d + %d\n",
  227. GET_MODE_NAME (twice_word_mode), GET_RTX_NAME (ZERO_EXTEND),
  228. zext_cost, word_move_cost, word_move_zero_cost);
  229. if (FORCE_LOWERING || zext_cost >= word_move_cost + word_move_zero_cost)
  230. choices[speed_p].splitting_zext = true;
  231. compute_splitting_shift (speed_p, rtxes,
  232. choices[speed_p].splitting_ashift, ASHIFT,
  233. word_move_zero_cost, word_move_cost);
  234. compute_splitting_shift (speed_p, rtxes,
  235. choices[speed_p].splitting_lshiftrt, LSHIFTRT,
  236. word_move_zero_cost, word_move_cost);
  237. compute_splitting_shift (speed_p, rtxes,
  238. choices[speed_p].splitting_ashiftrt, ASHIFTRT,
  239. word_move_zero_cost, word_move_cost);
  240. }
  241. }
  242. /* Do one-per-target initialisation. This involves determining
  243. which operations on the machine are profitable. If none are found,
  244. then the pass just returns when called. */
  245. void
  246. init_lower_subreg (void)
  247. {
  248. struct cost_rtxes rtxes;
  249. memset (this_target_lower_subreg, 0, sizeof (*this_target_lower_subreg));
  250. twice_word_mode = GET_MODE_2XWIDER_MODE (word_mode);
  251. rtxes.target = gen_rtx_REG (word_mode, FIRST_PSEUDO_REGISTER);
  252. rtxes.source = gen_rtx_REG (word_mode, FIRST_PSEUDO_REGISTER + 1);
  253. rtxes.set = gen_rtx_SET (VOIDmode, rtxes.target, rtxes.source);
  254. rtxes.zext = gen_rtx_ZERO_EXTEND (twice_word_mode, rtxes.source);
  255. rtxes.shift = gen_rtx_ASHIFT (twice_word_mode, rtxes.source, const0_rtx);
  256. if (LOG_COSTS)
  257. fprintf (stderr, "\nSize costs\n==========\n\n");
  258. compute_costs (false, &rtxes);
  259. if (LOG_COSTS)
  260. fprintf (stderr, "\nSpeed costs\n===========\n\n");
  261. compute_costs (true, &rtxes);
  262. }
  263. static bool
  264. simple_move_operand (rtx x)
  265. {
  266. if (GET_CODE (x) == SUBREG)
  267. x = SUBREG_REG (x);
  268. if (!OBJECT_P (x))
  269. return false;
  270. if (GET_CODE (x) == LABEL_REF
  271. || GET_CODE (x) == SYMBOL_REF
  272. || GET_CODE (x) == HIGH
  273. || GET_CODE (x) == CONST)
  274. return false;
  275. if (MEM_P (x)
  276. && (MEM_VOLATILE_P (x)
  277. || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x))))
  278. return false;
  279. return true;
  280. }
  281. /* If INSN is a single set between two objects that we want to split,
  282. return the single set. SPEED_P says whether we are optimizing
  283. INSN for speed or size.
  284. INSN should have been passed to recog and extract_insn before this
  285. is called. */
  286. static rtx
  287. simple_move (rtx_insn *insn, bool speed_p)
  288. {
  289. rtx x;
  290. rtx set;
  291. machine_mode mode;
  292. if (recog_data.n_operands != 2)
  293. return NULL_RTX;
  294. set = single_set (insn);
  295. if (!set)
  296. return NULL_RTX;
  297. x = SET_DEST (set);
  298. if (x != recog_data.operand[0] && x != recog_data.operand[1])
  299. return NULL_RTX;
  300. if (!simple_move_operand (x))
  301. return NULL_RTX;
  302. x = SET_SRC (set);
  303. if (x != recog_data.operand[0] && x != recog_data.operand[1])
  304. return NULL_RTX;
  305. /* For the src we can handle ASM_OPERANDS, and it is beneficial for
  306. things like x86 rdtsc which returns a DImode value. */
  307. if (GET_CODE (x) != ASM_OPERANDS
  308. && !simple_move_operand (x))
  309. return NULL_RTX;
  310. /* We try to decompose in integer modes, to avoid generating
  311. inefficient code copying between integer and floating point
  312. registers. That means that we can't decompose if this is a
  313. non-integer mode for which there is no integer mode of the same
  314. size. */
  315. mode = GET_MODE (SET_DEST (set));
  316. if (!SCALAR_INT_MODE_P (mode)
  317. && (mode_for_size (GET_MODE_SIZE (mode) * BITS_PER_UNIT, MODE_INT, 0)
  318. == BLKmode))
  319. return NULL_RTX;
  320. /* Reject PARTIAL_INT modes. They are used for processor specific
  321. purposes and it's probably best not to tamper with them. */
  322. if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
  323. return NULL_RTX;
  324. if (!choices[speed_p].move_modes_to_split[(int) mode])
  325. return NULL_RTX;
  326. return set;
  327. }
  328. /* If SET is a copy from one multi-word pseudo-register to another,
  329. record that in reg_copy_graph. Return whether it is such a
  330. copy. */
  331. static bool
  332. find_pseudo_copy (rtx set)
  333. {
  334. rtx dest = SET_DEST (set);
  335. rtx src = SET_SRC (set);
  336. unsigned int rd, rs;
  337. bitmap b;
  338. if (!REG_P (dest) || !REG_P (src))
  339. return false;
  340. rd = REGNO (dest);
  341. rs = REGNO (src);
  342. if (HARD_REGISTER_NUM_P (rd) || HARD_REGISTER_NUM_P (rs))
  343. return false;
  344. b = reg_copy_graph[rs];
  345. if (b == NULL)
  346. {
  347. b = BITMAP_ALLOC (NULL);
  348. reg_copy_graph[rs] = b;
  349. }
  350. bitmap_set_bit (b, rd);
  351. return true;
  352. }
  353. /* Look through the registers in DECOMPOSABLE_CONTEXT. For each case
  354. where they are copied to another register, add the register to
  355. which they are copied to DECOMPOSABLE_CONTEXT. Use
  356. NON_DECOMPOSABLE_CONTEXT to limit this--we don't bother to track
  357. copies of registers which are in NON_DECOMPOSABLE_CONTEXT. */
  358. static void
  359. propagate_pseudo_copies (void)
  360. {
  361. bitmap queue, propagate;
  362. queue = BITMAP_ALLOC (NULL);
  363. propagate = BITMAP_ALLOC (NULL);
  364. bitmap_copy (queue, decomposable_context);
  365. do
  366. {
  367. bitmap_iterator iter;
  368. unsigned int i;
  369. bitmap_clear (propagate);
  370. EXECUTE_IF_SET_IN_BITMAP (queue, 0, i, iter)
  371. {
  372. bitmap b = reg_copy_graph[i];
  373. if (b)
  374. bitmap_ior_and_compl_into (propagate, b, non_decomposable_context);
  375. }
  376. bitmap_and_compl (queue, propagate, decomposable_context);
  377. bitmap_ior_into (decomposable_context, propagate);
  378. }
  379. while (!bitmap_empty_p (queue));
  380. BITMAP_FREE (queue);
  381. BITMAP_FREE (propagate);
  382. }
  383. /* A pointer to one of these values is passed to
  384. find_decomposable_subregs. */
  385. enum classify_move_insn
  386. {
  387. /* Not a simple move from one location to another. */
  388. NOT_SIMPLE_MOVE,
  389. /* A simple move we want to decompose. */
  390. DECOMPOSABLE_SIMPLE_MOVE,
  391. /* Any other simple move. */
  392. SIMPLE_MOVE
  393. };
  394. /* If we find a SUBREG in *LOC which we could use to decompose a
  395. pseudo-register, set a bit in DECOMPOSABLE_CONTEXT. If we find an
  396. unadorned register which is not a simple pseudo-register copy,
  397. DATA will point at the type of move, and we set a bit in
  398. DECOMPOSABLE_CONTEXT or NON_DECOMPOSABLE_CONTEXT as appropriate. */
  399. static void
  400. find_decomposable_subregs (rtx *loc, enum classify_move_insn *pcmi)
  401. {
  402. subrtx_var_iterator::array_type array;
  403. FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
  404. {
  405. rtx x = *iter;
  406. if (GET_CODE (x) == SUBREG)
  407. {
  408. rtx inner = SUBREG_REG (x);
  409. unsigned int regno, outer_size, inner_size, outer_words, inner_words;
  410. if (!REG_P (inner))
  411. continue;
  412. regno = REGNO (inner);
  413. if (HARD_REGISTER_NUM_P (regno))
  414. {
  415. iter.skip_subrtxes ();
  416. continue;
  417. }
  418. outer_size = GET_MODE_SIZE (GET_MODE (x));
  419. inner_size = GET_MODE_SIZE (GET_MODE (inner));
  420. outer_words = (outer_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
  421. inner_words = (inner_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
  422. /* We only try to decompose single word subregs of multi-word
  423. registers. When we find one, we return -1 to avoid iterating
  424. over the inner register.
  425. ??? This doesn't allow, e.g., DImode subregs of TImode values
  426. on 32-bit targets. We would need to record the way the
  427. pseudo-register was used, and only decompose if all the uses
  428. were the same number and size of pieces. Hopefully this
  429. doesn't happen much. */
  430. if (outer_words == 1 && inner_words > 1)
  431. {
  432. bitmap_set_bit (decomposable_context, regno);
  433. iter.skip_subrtxes ();
  434. continue;
  435. }
  436. /* If this is a cast from one mode to another, where the modes
  437. have the same size, and they are not tieable, then mark this
  438. register as non-decomposable. If we decompose it we are
  439. likely to mess up whatever the backend is trying to do. */
  440. if (outer_words > 1
  441. && outer_size == inner_size
  442. && !MODES_TIEABLE_P (GET_MODE (x), GET_MODE (inner)))
  443. {
  444. bitmap_set_bit (non_decomposable_context, regno);
  445. bitmap_set_bit (subreg_context, regno);
  446. iter.skip_subrtxes ();
  447. continue;
  448. }
  449. }
  450. else if (REG_P (x))
  451. {
  452. unsigned int regno;
  453. /* We will see an outer SUBREG before we see the inner REG, so
  454. when we see a plain REG here it means a direct reference to
  455. the register.
  456. If this is not a simple copy from one location to another,
  457. then we can not decompose this register. If this is a simple
  458. copy we want to decompose, and the mode is right,
  459. then we mark the register as decomposable.
  460. Otherwise we don't say anything about this register --
  461. it could be decomposed, but whether that would be
  462. profitable depends upon how it is used elsewhere.
  463. We only set bits in the bitmap for multi-word
  464. pseudo-registers, since those are the only ones we care about
  465. and it keeps the size of the bitmaps down. */
  466. regno = REGNO (x);
  467. if (!HARD_REGISTER_NUM_P (regno)
  468. && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
  469. {
  470. switch (*pcmi)
  471. {
  472. case NOT_SIMPLE_MOVE:
  473. bitmap_set_bit (non_decomposable_context, regno);
  474. break;
  475. case DECOMPOSABLE_SIMPLE_MOVE:
  476. if (MODES_TIEABLE_P (GET_MODE (x), word_mode))
  477. bitmap_set_bit (decomposable_context, regno);
  478. break;
  479. case SIMPLE_MOVE:
  480. break;
  481. default:
  482. gcc_unreachable ();
  483. }
  484. }
  485. }
  486. else if (MEM_P (x))
  487. {
  488. enum classify_move_insn cmi_mem = NOT_SIMPLE_MOVE;
  489. /* Any registers used in a MEM do not participate in a
  490. SIMPLE_MOVE or DECOMPOSABLE_SIMPLE_MOVE. Do our own recursion
  491. here, and return -1 to block the parent's recursion. */
  492. find_decomposable_subregs (&XEXP (x, 0), &cmi_mem);
  493. iter.skip_subrtxes ();
  494. }
  495. }
  496. }
  497. /* Decompose REGNO into word-sized components. We smash the REG node
  498. in place. This ensures that (1) something goes wrong quickly if we
  499. fail to make some replacement, and (2) the debug information inside
  500. the symbol table is automatically kept up to date. */
  501. static void
  502. decompose_register (unsigned int regno)
  503. {
  504. rtx reg;
  505. unsigned int words, i;
  506. rtvec v;
  507. reg = regno_reg_rtx[regno];
  508. regno_reg_rtx[regno] = NULL_RTX;
  509. words = GET_MODE_SIZE (GET_MODE (reg));
  510. words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
  511. v = rtvec_alloc (words);
  512. for (i = 0; i < words; ++i)
  513. RTVEC_ELT (v, i) = gen_reg_rtx_offset (reg, word_mode, i * UNITS_PER_WORD);
  514. PUT_CODE (reg, CONCATN);
  515. XVEC (reg, 0) = v;
  516. if (dump_file)
  517. {
  518. fprintf (dump_file, "; Splitting reg %u ->", regno);
  519. for (i = 0; i < words; ++i)
  520. fprintf (dump_file, " %u", REGNO (XVECEXP (reg, 0, i)));
  521. fputc ('\n', dump_file);
  522. }
  523. }
  524. /* Get a SUBREG of a CONCATN. */
  525. static rtx
  526. simplify_subreg_concatn (machine_mode outermode, rtx op,
  527. unsigned int byte)
  528. {
  529. unsigned int inner_size;
  530. machine_mode innermode, partmode;
  531. rtx part;
  532. unsigned int final_offset;
  533. gcc_assert (GET_CODE (op) == CONCATN);
  534. gcc_assert (byte % GET_MODE_SIZE (outermode) == 0);
  535. innermode = GET_MODE (op);
  536. gcc_assert (byte < GET_MODE_SIZE (innermode));
  537. gcc_assert (GET_MODE_SIZE (outermode) <= GET_MODE_SIZE (innermode));
  538. inner_size = GET_MODE_SIZE (innermode) / XVECLEN (op, 0);
  539. part = XVECEXP (op, 0, byte / inner_size);
  540. partmode = GET_MODE (part);
  541. /* VECTOR_CSTs in debug expressions are expanded into CONCATN instead of
  542. regular CONST_VECTORs. They have vector or integer modes, depending
  543. on the capabilities of the target. Cope with them. */
  544. if (partmode == VOIDmode && VECTOR_MODE_P (innermode))
  545. partmode = GET_MODE_INNER (innermode);
  546. else if (partmode == VOIDmode)
  547. {
  548. enum mode_class mclass = GET_MODE_CLASS (innermode);
  549. partmode = mode_for_size (inner_size * BITS_PER_UNIT, mclass, 0);
  550. }
  551. final_offset = byte % inner_size;
  552. if (final_offset + GET_MODE_SIZE (outermode) > inner_size)
  553. return NULL_RTX;
  554. return simplify_gen_subreg (outermode, part, partmode, final_offset);
  555. }
  556. /* Wrapper around simplify_gen_subreg which handles CONCATN. */
  557. static rtx
  558. simplify_gen_subreg_concatn (machine_mode outermode, rtx op,
  559. machine_mode innermode, unsigned int byte)
  560. {
  561. rtx ret;
  562. /* We have to handle generating a SUBREG of a SUBREG of a CONCATN.
  563. If OP is a SUBREG of a CONCATN, then it must be a simple mode
  564. change with the same size and offset 0, or it must extract a
  565. part. We shouldn't see anything else here. */
  566. if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == CONCATN)
  567. {
  568. rtx op2;
  569. if ((GET_MODE_SIZE (GET_MODE (op))
  570. == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))))
  571. && SUBREG_BYTE (op) == 0)
  572. return simplify_gen_subreg_concatn (outermode, SUBREG_REG (op),
  573. GET_MODE (SUBREG_REG (op)), byte);
  574. op2 = simplify_subreg_concatn (GET_MODE (op), SUBREG_REG (op),
  575. SUBREG_BYTE (op));
  576. if (op2 == NULL_RTX)
  577. {
  578. /* We don't handle paradoxical subregs here. */
  579. gcc_assert (GET_MODE_SIZE (outermode)
  580. <= GET_MODE_SIZE (GET_MODE (op)));
  581. gcc_assert (GET_MODE_SIZE (GET_MODE (op))
  582. <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))));
  583. op2 = simplify_subreg_concatn (outermode, SUBREG_REG (op),
  584. byte + SUBREG_BYTE (op));
  585. gcc_assert (op2 != NULL_RTX);
  586. return op2;
  587. }
  588. op = op2;
  589. gcc_assert (op != NULL_RTX);
  590. gcc_assert (innermode == GET_MODE (op));
  591. }
  592. if (GET_CODE (op) == CONCATN)
  593. return simplify_subreg_concatn (outermode, op, byte);
  594. ret = simplify_gen_subreg (outermode, op, innermode, byte);
  595. /* If we see an insn like (set (reg:DI) (subreg:DI (reg:SI) 0)) then
  596. resolve_simple_move will ask for the high part of the paradoxical
  597. subreg, which does not have a value. Just return a zero. */
  598. if (ret == NULL_RTX
  599. && GET_CODE (op) == SUBREG
  600. && SUBREG_BYTE (op) == 0
  601. && (GET_MODE_SIZE (innermode)
  602. > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op)))))
  603. return CONST0_RTX (outermode);
  604. gcc_assert (ret != NULL_RTX);
  605. return ret;
  606. }
  607. /* Return whether we should resolve X into the registers into which it
  608. was decomposed. */
  609. static bool
  610. resolve_reg_p (rtx x)
  611. {
  612. return GET_CODE (x) == CONCATN;
  613. }
  614. /* Return whether X is a SUBREG of a register which we need to
  615. resolve. */
  616. static bool
  617. resolve_subreg_p (rtx x)
  618. {
  619. if (GET_CODE (x) != SUBREG)
  620. return false;
  621. return resolve_reg_p (SUBREG_REG (x));
  622. }
  623. /* Look for SUBREGs in *LOC which need to be decomposed. */
  624. static bool
  625. resolve_subreg_use (rtx *loc, rtx insn)
  626. {
  627. subrtx_ptr_iterator::array_type array;
  628. FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
  629. {
  630. rtx *loc = *iter;
  631. rtx x = *loc;
  632. if (resolve_subreg_p (x))
  633. {
  634. x = simplify_subreg_concatn (GET_MODE (x), SUBREG_REG (x),
  635. SUBREG_BYTE (x));
  636. /* It is possible for a note to contain a reference which we can
  637. decompose. In this case, return 1 to the caller to indicate
  638. that the note must be removed. */
  639. if (!x)
  640. {
  641. gcc_assert (!insn);
  642. return true;
  643. }
  644. validate_change (insn, loc, x, 1);
  645. iter.skip_subrtxes ();
  646. }
  647. else if (resolve_reg_p (x))
  648. /* Return 1 to the caller to indicate that we found a direct
  649. reference to a register which is being decomposed. This can
  650. happen inside notes, multiword shift or zero-extend
  651. instructions. */
  652. return true;
  653. }
  654. return false;
  655. }
  656. /* Resolve any decomposed registers which appear in register notes on
  657. INSN. */
  658. static void
  659. resolve_reg_notes (rtx_insn *insn)
  660. {
  661. rtx *pnote, note;
  662. note = find_reg_equal_equiv_note (insn);
  663. if (note)
  664. {
  665. int old_count = num_validated_changes ();
  666. if (resolve_subreg_use (&XEXP (note, 0), NULL_RTX))
  667. remove_note (insn, note);
  668. else
  669. if (old_count != num_validated_changes ())
  670. df_notes_rescan (insn);
  671. }
  672. pnote = &REG_NOTES (insn);
  673. while (*pnote != NULL_RTX)
  674. {
  675. bool del = false;
  676. note = *pnote;
  677. switch (REG_NOTE_KIND (note))
  678. {
  679. case REG_DEAD:
  680. case REG_UNUSED:
  681. if (resolve_reg_p (XEXP (note, 0)))
  682. del = true;
  683. break;
  684. default:
  685. break;
  686. }
  687. if (del)
  688. *pnote = XEXP (note, 1);
  689. else
  690. pnote = &XEXP (note, 1);
  691. }
  692. }
  693. /* Return whether X can be decomposed into subwords. */
  694. static bool
  695. can_decompose_p (rtx x)
  696. {
  697. if (REG_P (x))
  698. {
  699. unsigned int regno = REGNO (x);
  700. if (HARD_REGISTER_NUM_P (regno))
  701. {
  702. unsigned int byte, num_bytes;
  703. num_bytes = GET_MODE_SIZE (GET_MODE (x));
  704. for (byte = 0; byte < num_bytes; byte += UNITS_PER_WORD)
  705. if (simplify_subreg_regno (regno, GET_MODE (x), byte, word_mode) < 0)
  706. return false;
  707. return true;
  708. }
  709. else
  710. return !bitmap_bit_p (subreg_context, regno);
  711. }
  712. return true;
  713. }
  714. /* Decompose the registers used in a simple move SET within INSN. If
  715. we don't change anything, return INSN, otherwise return the start
  716. of the sequence of moves. */
  717. static rtx_insn *
  718. resolve_simple_move (rtx set, rtx_insn *insn)
  719. {
  720. rtx src, dest, real_dest;
  721. rtx_insn *insns;
  722. machine_mode orig_mode, dest_mode;
  723. unsigned int words;
  724. bool pushing;
  725. src = SET_SRC (set);
  726. dest = SET_DEST (set);
  727. orig_mode = GET_MODE (dest);
  728. words = (GET_MODE_SIZE (orig_mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
  729. gcc_assert (words > 1);
  730. start_sequence ();
  731. /* We have to handle copying from a SUBREG of a decomposed reg where
  732. the SUBREG is larger than word size. Rather than assume that we
  733. can take a word_mode SUBREG of the destination, we copy to a new
  734. register and then copy that to the destination. */
  735. real_dest = NULL_RTX;
  736. if (GET_CODE (src) == SUBREG
  737. && resolve_reg_p (SUBREG_REG (src))
  738. && (SUBREG_BYTE (src) != 0
  739. || (GET_MODE_SIZE (orig_mode)
  740. != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))))
  741. {
  742. real_dest = dest;
  743. dest = gen_reg_rtx (orig_mode);
  744. if (REG_P (real_dest))
  745. REG_ATTRS (dest) = REG_ATTRS (real_dest);
  746. }
  747. /* Similarly if we are copying to a SUBREG of a decomposed reg where
  748. the SUBREG is larger than word size. */
  749. if (GET_CODE (dest) == SUBREG
  750. && resolve_reg_p (SUBREG_REG (dest))
  751. && (SUBREG_BYTE (dest) != 0
  752. || (GET_MODE_SIZE (orig_mode)
  753. != GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))))
  754. {
  755. rtx reg, smove;
  756. rtx_insn *minsn;
  757. reg = gen_reg_rtx (orig_mode);
  758. minsn = emit_move_insn (reg, src);
  759. smove = single_set (minsn);
  760. gcc_assert (smove != NULL_RTX);
  761. resolve_simple_move (smove, minsn);
  762. src = reg;
  763. }
  764. /* If we didn't have any big SUBREGS of decomposed registers, and
  765. neither side of the move is a register we are decomposing, then
  766. we don't have to do anything here. */
  767. if (src == SET_SRC (set)
  768. && dest == SET_DEST (set)
  769. && !resolve_reg_p (src)
  770. && !resolve_subreg_p (src)
  771. && !resolve_reg_p (dest)
  772. && !resolve_subreg_p (dest))
  773. {
  774. end_sequence ();
  775. return insn;
  776. }
  777. /* It's possible for the code to use a subreg of a decomposed
  778. register while forming an address. We need to handle that before
  779. passing the address to emit_move_insn. We pass NULL_RTX as the
  780. insn parameter to resolve_subreg_use because we can not validate
  781. the insn yet. */
  782. if (MEM_P (src) || MEM_P (dest))
  783. {
  784. int acg;
  785. if (MEM_P (src))
  786. resolve_subreg_use (&XEXP (src, 0), NULL_RTX);
  787. if (MEM_P (dest))
  788. resolve_subreg_use (&XEXP (dest, 0), NULL_RTX);
  789. acg = apply_change_group ();
  790. gcc_assert (acg);
  791. }
  792. /* If SRC is a register which we can't decompose, or has side
  793. effects, we need to move via a temporary register. */
  794. if (!can_decompose_p (src)
  795. || side_effects_p (src)
  796. || GET_CODE (src) == ASM_OPERANDS)
  797. {
  798. rtx reg;
  799. reg = gen_reg_rtx (orig_mode);
  800. #ifdef AUTO_INC_DEC
  801. {
  802. rtx move = emit_move_insn (reg, src);
  803. if (MEM_P (src))
  804. {
  805. rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
  806. if (note)
  807. add_reg_note (move, REG_INC, XEXP (note, 0));
  808. }
  809. }
  810. #else
  811. emit_move_insn (reg, src);
  812. #endif
  813. src = reg;
  814. }
  815. /* If DEST is a register which we can't decompose, or has side
  816. effects, we need to first move to a temporary register. We
  817. handle the common case of pushing an operand directly. We also
  818. go through a temporary register if it holds a floating point
  819. value. This gives us better code on systems which can't move
  820. data easily between integer and floating point registers. */
  821. dest_mode = orig_mode;
  822. pushing = push_operand (dest, dest_mode);
  823. if (!can_decompose_p (dest)
  824. || (side_effects_p (dest) && !pushing)
  825. || (!SCALAR_INT_MODE_P (dest_mode)
  826. && !resolve_reg_p (dest)
  827. && !resolve_subreg_p (dest)))
  828. {
  829. if (real_dest == NULL_RTX)
  830. real_dest = dest;
  831. if (!SCALAR_INT_MODE_P (dest_mode))
  832. {
  833. dest_mode = mode_for_size (GET_MODE_SIZE (dest_mode) * BITS_PER_UNIT,
  834. MODE_INT, 0);
  835. gcc_assert (dest_mode != BLKmode);
  836. }
  837. dest = gen_reg_rtx (dest_mode);
  838. if (REG_P (real_dest))
  839. REG_ATTRS (dest) = REG_ATTRS (real_dest);
  840. }
  841. if (pushing)
  842. {
  843. unsigned int i, j, jinc;
  844. gcc_assert (GET_MODE_SIZE (orig_mode) % UNITS_PER_WORD == 0);
  845. gcc_assert (GET_CODE (XEXP (dest, 0)) != PRE_MODIFY);
  846. gcc_assert (GET_CODE (XEXP (dest, 0)) != POST_MODIFY);
  847. if (WORDS_BIG_ENDIAN == STACK_GROWS_DOWNWARD)
  848. {
  849. j = 0;
  850. jinc = 1;
  851. }
  852. else
  853. {
  854. j = words - 1;
  855. jinc = -1;
  856. }
  857. for (i = 0; i < words; ++i, j += jinc)
  858. {
  859. rtx temp;
  860. temp = copy_rtx (XEXP (dest, 0));
  861. temp = adjust_automodify_address_nv (dest, word_mode, temp,
  862. j * UNITS_PER_WORD);
  863. emit_move_insn (temp,
  864. simplify_gen_subreg_concatn (word_mode, src,
  865. orig_mode,
  866. j * UNITS_PER_WORD));
  867. }
  868. }
  869. else
  870. {
  871. unsigned int i;
  872. if (REG_P (dest) && !HARD_REGISTER_NUM_P (REGNO (dest)))
  873. emit_clobber (dest);
  874. for (i = 0; i < words; ++i)
  875. emit_move_insn (simplify_gen_subreg_concatn (word_mode, dest,
  876. dest_mode,
  877. i * UNITS_PER_WORD),
  878. simplify_gen_subreg_concatn (word_mode, src,
  879. orig_mode,
  880. i * UNITS_PER_WORD));
  881. }
  882. if (real_dest != NULL_RTX)
  883. {
  884. rtx mdest, smove;
  885. rtx_insn *minsn;
  886. if (dest_mode == orig_mode)
  887. mdest = dest;
  888. else
  889. mdest = simplify_gen_subreg (orig_mode, dest, GET_MODE (dest), 0);
  890. minsn = emit_move_insn (real_dest, mdest);
  891. #ifdef AUTO_INC_DEC
  892. if (MEM_P (real_dest)
  893. && !(resolve_reg_p (real_dest) || resolve_subreg_p (real_dest)))
  894. {
  895. rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
  896. if (note)
  897. add_reg_note (minsn, REG_INC, XEXP (note, 0));
  898. }
  899. #endif
  900. smove = single_set (minsn);
  901. gcc_assert (smove != NULL_RTX);
  902. resolve_simple_move (smove, minsn);
  903. }
  904. insns = get_insns ();
  905. end_sequence ();
  906. copy_reg_eh_region_note_forward (insn, insns, NULL_RTX);
  907. emit_insn_before (insns, insn);
  908. /* If we get here via self-recursion, then INSN is not yet in the insns
  909. chain and delete_insn will fail. We only want to remove INSN from the
  910. current sequence. See PR56738. */
  911. if (in_sequence_p ())
  912. remove_insn (insn);
  913. else
  914. delete_insn (insn);
  915. return insns;
  916. }
  917. /* Change a CLOBBER of a decomposed register into a CLOBBER of the
  918. component registers. Return whether we changed something. */
  919. static bool
  920. resolve_clobber (rtx pat, rtx_insn *insn)
  921. {
  922. rtx reg;
  923. machine_mode orig_mode;
  924. unsigned int words, i;
  925. int ret;
  926. reg = XEXP (pat, 0);
  927. if (!resolve_reg_p (reg) && !resolve_subreg_p (reg))
  928. return false;
  929. orig_mode = GET_MODE (reg);
  930. words = GET_MODE_SIZE (orig_mode);
  931. words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
  932. ret = validate_change (NULL_RTX, &XEXP (pat, 0),
  933. simplify_gen_subreg_concatn (word_mode, reg,
  934. orig_mode, 0),
  935. 0);
  936. df_insn_rescan (insn);
  937. gcc_assert (ret != 0);
  938. for (i = words - 1; i > 0; --i)
  939. {
  940. rtx x;
  941. x = simplify_gen_subreg_concatn (word_mode, reg, orig_mode,
  942. i * UNITS_PER_WORD);
  943. x = gen_rtx_CLOBBER (VOIDmode, x);
  944. emit_insn_after (x, insn);
  945. }
  946. resolve_reg_notes (insn);
  947. return true;
  948. }
  949. /* A USE of a decomposed register is no longer meaningful. Return
  950. whether we changed something. */
  951. static bool
  952. resolve_use (rtx pat, rtx_insn *insn)
  953. {
  954. if (resolve_reg_p (XEXP (pat, 0)) || resolve_subreg_p (XEXP (pat, 0)))
  955. {
  956. delete_insn (insn);
  957. return true;
  958. }
  959. resolve_reg_notes (insn);
  960. return false;
  961. }
  962. /* A VAR_LOCATION can be simplified. */
  963. static void
  964. resolve_debug (rtx_insn *insn)
  965. {
  966. subrtx_ptr_iterator::array_type array;
  967. FOR_EACH_SUBRTX_PTR (iter, array, &PATTERN (insn), NONCONST)
  968. {
  969. rtx *loc = *iter;
  970. rtx x = *loc;
  971. if (resolve_subreg_p (x))
  972. {
  973. x = simplify_subreg_concatn (GET_MODE (x), SUBREG_REG (x),
  974. SUBREG_BYTE (x));
  975. if (x)
  976. *loc = x;
  977. else
  978. x = copy_rtx (*loc);
  979. }
  980. if (resolve_reg_p (x))
  981. *loc = copy_rtx (x);
  982. }
  983. df_insn_rescan (insn);
  984. resolve_reg_notes (insn);
  985. }
  986. /* Check if INSN is a decomposable multiword-shift or zero-extend and
  987. set the decomposable_context bitmap accordingly. SPEED_P is true
  988. if we are optimizing INSN for speed rather than size. Return true
  989. if INSN is decomposable. */
  990. static bool
  991. find_decomposable_shift_zext (rtx_insn *insn, bool speed_p)
  992. {
  993. rtx set;
  994. rtx op;
  995. rtx op_operand;
  996. set = single_set (insn);
  997. if (!set)
  998. return false;
  999. op = SET_SRC (set);
  1000. if (GET_CODE (op) != ASHIFT
  1001. && GET_CODE (op) != LSHIFTRT
  1002. && GET_CODE (op) != ASHIFTRT
  1003. && GET_CODE (op) != ZERO_EXTEND)
  1004. return false;
  1005. op_operand = XEXP (op, 0);
  1006. if (!REG_P (SET_DEST (set)) || !REG_P (op_operand)
  1007. || HARD_REGISTER_NUM_P (REGNO (SET_DEST (set)))
  1008. || HARD_REGISTER_NUM_P (REGNO (op_operand))
  1009. || GET_MODE (op) != twice_word_mode)
  1010. return false;
  1011. if (GET_CODE (op) == ZERO_EXTEND)
  1012. {
  1013. if (GET_MODE (op_operand) != word_mode
  1014. || !choices[speed_p].splitting_zext)
  1015. return false;
  1016. }
  1017. else /* left or right shift */
  1018. {
  1019. bool *splitting = (GET_CODE (op) == ASHIFT
  1020. ? choices[speed_p].splitting_ashift
  1021. : GET_CODE (op) == ASHIFTRT
  1022. ? choices[speed_p].splitting_ashiftrt
  1023. : choices[speed_p].splitting_lshiftrt);
  1024. if (!CONST_INT_P (XEXP (op, 1))
  1025. || !IN_RANGE (INTVAL (XEXP (op, 1)), BITS_PER_WORD,
  1026. 2 * BITS_PER_WORD - 1)
  1027. || !splitting[INTVAL (XEXP (op, 1)) - BITS_PER_WORD])
  1028. return false;
  1029. bitmap_set_bit (decomposable_context, REGNO (op_operand));
  1030. }
  1031. bitmap_set_bit (decomposable_context, REGNO (SET_DEST (set)));
  1032. return true;
  1033. }
  1034. /* Decompose a more than word wide shift (in INSN) of a multiword
  1035. pseudo or a multiword zero-extend of a wordmode pseudo into a move
  1036. and 'set to zero' insn. Return a pointer to the new insn when a
  1037. replacement was done. */
  1038. static rtx_insn *
  1039. resolve_shift_zext (rtx_insn *insn)
  1040. {
  1041. rtx set;
  1042. rtx op;
  1043. rtx op_operand;
  1044. rtx_insn *insns;
  1045. rtx src_reg, dest_reg, dest_upper, upper_src = NULL_RTX;
  1046. int src_reg_num, dest_reg_num, offset1, offset2, src_offset;
  1047. set = single_set (insn);
  1048. if (!set)
  1049. return NULL;
  1050. op = SET_SRC (set);
  1051. if (GET_CODE (op) != ASHIFT
  1052. && GET_CODE (op) != LSHIFTRT
  1053. && GET_CODE (op) != ASHIFTRT
  1054. && GET_CODE (op) != ZERO_EXTEND)
  1055. return NULL;
  1056. op_operand = XEXP (op, 0);
  1057. /* We can tear this operation apart only if the regs were already
  1058. torn apart. */
  1059. if (!resolve_reg_p (SET_DEST (set)) && !resolve_reg_p (op_operand))
  1060. return NULL;
  1061. /* src_reg_num is the number of the word mode register which we
  1062. are operating on. For a left shift and a zero_extend on little
  1063. endian machines this is register 0. */
  1064. src_reg_num = (GET_CODE (op) == LSHIFTRT || GET_CODE (op) == ASHIFTRT)
  1065. ? 1 : 0;
  1066. if (WORDS_BIG_ENDIAN
  1067. && GET_MODE_SIZE (GET_MODE (op_operand)) > UNITS_PER_WORD)
  1068. src_reg_num = 1 - src_reg_num;
  1069. if (GET_CODE (op) == ZERO_EXTEND)
  1070. dest_reg_num = WORDS_BIG_ENDIAN ? 1 : 0;
  1071. else
  1072. dest_reg_num = 1 - src_reg_num;
  1073. offset1 = UNITS_PER_WORD * dest_reg_num;
  1074. offset2 = UNITS_PER_WORD * (1 - dest_reg_num);
  1075. src_offset = UNITS_PER_WORD * src_reg_num;
  1076. start_sequence ();
  1077. dest_reg = simplify_gen_subreg_concatn (word_mode, SET_DEST (set),
  1078. GET_MODE (SET_DEST (set)),
  1079. offset1);
  1080. dest_upper = simplify_gen_subreg_concatn (word_mode, SET_DEST (set),
  1081. GET_MODE (SET_DEST (set)),
  1082. offset2);
  1083. src_reg = simplify_gen_subreg_concatn (word_mode, op_operand,
  1084. GET_MODE (op_operand),
  1085. src_offset);
  1086. if (GET_CODE (op) == ASHIFTRT
  1087. && INTVAL (XEXP (op, 1)) != 2 * BITS_PER_WORD - 1)
  1088. upper_src = expand_shift (RSHIFT_EXPR, word_mode, copy_rtx (src_reg),
  1089. BITS_PER_WORD - 1, NULL_RTX, 0);
  1090. if (GET_CODE (op) != ZERO_EXTEND)
  1091. {
  1092. int shift_count = INTVAL (XEXP (op, 1));
  1093. if (shift_count > BITS_PER_WORD)
  1094. src_reg = expand_shift (GET_CODE (op) == ASHIFT ?
  1095. LSHIFT_EXPR : RSHIFT_EXPR,
  1096. word_mode, src_reg,
  1097. shift_count - BITS_PER_WORD,
  1098. dest_reg, GET_CODE (op) != ASHIFTRT);
  1099. }
  1100. if (dest_reg != src_reg)
  1101. emit_move_insn (dest_reg, src_reg);
  1102. if (GET_CODE (op) != ASHIFTRT)
  1103. emit_move_insn (dest_upper, CONST0_RTX (word_mode));
  1104. else if (INTVAL (XEXP (op, 1)) == 2 * BITS_PER_WORD - 1)
  1105. emit_move_insn (dest_upper, copy_rtx (src_reg));
  1106. else
  1107. emit_move_insn (dest_upper, upper_src);
  1108. insns = get_insns ();
  1109. end_sequence ();
  1110. emit_insn_before (insns, insn);
  1111. if (dump_file)
  1112. {
  1113. rtx_insn *in;
  1114. fprintf (dump_file, "; Replacing insn: %d with insns: ", INSN_UID (insn));
  1115. for (in = insns; in != insn; in = NEXT_INSN (in))
  1116. fprintf (dump_file, "%d ", INSN_UID (in));
  1117. fprintf (dump_file, "\n");
  1118. }
  1119. delete_insn (insn);
  1120. return insns;
  1121. }
  1122. /* Print to dump_file a description of what we're doing with shift code CODE.
  1123. SPLITTING[X] is true if we are splitting shifts by X + BITS_PER_WORD. */
  1124. static void
  1125. dump_shift_choices (enum rtx_code code, bool *splitting)
  1126. {
  1127. int i;
  1128. const char *sep;
  1129. fprintf (dump_file,
  1130. " Splitting mode %s for %s lowering with shift amounts = ",
  1131. GET_MODE_NAME (twice_word_mode), GET_RTX_NAME (code));
  1132. sep = "";
  1133. for (i = 0; i < BITS_PER_WORD; i++)
  1134. if (splitting[i])
  1135. {
  1136. fprintf (dump_file, "%s%d", sep, i + BITS_PER_WORD);
  1137. sep = ",";
  1138. }
  1139. fprintf (dump_file, "\n");
  1140. }
  1141. /* Print to dump_file a description of what we're doing when optimizing
  1142. for speed or size; SPEED_P says which. DESCRIPTION is a description
  1143. of the SPEED_P choice. */
  1144. static void
  1145. dump_choices (bool speed_p, const char *description)
  1146. {
  1147. unsigned int i;
  1148. fprintf (dump_file, "Choices when optimizing for %s:\n", description);
  1149. for (i = 0; i < MAX_MACHINE_MODE; i++)
  1150. if (GET_MODE_SIZE ((machine_mode) i) > UNITS_PER_WORD)
  1151. fprintf (dump_file, " %s mode %s for copy lowering.\n",
  1152. choices[speed_p].move_modes_to_split[i]
  1153. ? "Splitting"
  1154. : "Skipping",
  1155. GET_MODE_NAME ((machine_mode) i));
  1156. fprintf (dump_file, " %s mode %s for zero_extend lowering.\n",
  1157. choices[speed_p].splitting_zext ? "Splitting" : "Skipping",
  1158. GET_MODE_NAME (twice_word_mode));
  1159. dump_shift_choices (ASHIFT, choices[speed_p].splitting_ashift);
  1160. dump_shift_choices (LSHIFTRT, choices[speed_p].splitting_lshiftrt);
  1161. dump_shift_choices (ASHIFTRT, choices[speed_p].splitting_ashiftrt);
  1162. fprintf (dump_file, "\n");
  1163. }
  1164. /* Look for registers which are always accessed via word-sized SUBREGs
  1165. or -if DECOMPOSE_COPIES is true- via copies. Decompose these
  1166. registers into several word-sized pseudo-registers. */
  1167. static void
  1168. decompose_multiword_subregs (bool decompose_copies)
  1169. {
  1170. unsigned int max;
  1171. basic_block bb;
  1172. bool speed_p;
  1173. if (dump_file)
  1174. {
  1175. dump_choices (false, "size");
  1176. dump_choices (true, "speed");
  1177. }
  1178. /* Check if this target even has any modes to consider lowering. */
  1179. if (!choices[false].something_to_do && !choices[true].something_to_do)
  1180. {
  1181. if (dump_file)
  1182. fprintf (dump_file, "Nothing to do!\n");
  1183. return;
  1184. }
  1185. max = max_reg_num ();
  1186. /* First see if there are any multi-word pseudo-registers. If there
  1187. aren't, there is nothing we can do. This should speed up this
  1188. pass in the normal case, since it should be faster than scanning
  1189. all the insns. */
  1190. {
  1191. unsigned int i;
  1192. bool useful_modes_seen = false;
  1193. for (i = FIRST_PSEUDO_REGISTER; i < max; ++i)
  1194. if (regno_reg_rtx[i] != NULL)
  1195. {
  1196. machine_mode mode = GET_MODE (regno_reg_rtx[i]);
  1197. if (choices[false].move_modes_to_split[(int) mode]
  1198. || choices[true].move_modes_to_split[(int) mode])
  1199. {
  1200. useful_modes_seen = true;
  1201. break;
  1202. }
  1203. }
  1204. if (!useful_modes_seen)
  1205. {
  1206. if (dump_file)
  1207. fprintf (dump_file, "Nothing to lower in this function.\n");
  1208. return;
  1209. }
  1210. }
  1211. if (df)
  1212. {
  1213. df_set_flags (DF_DEFER_INSN_RESCAN);
  1214. run_word_dce ();
  1215. }
  1216. /* FIXME: It may be possible to change this code to look for each
  1217. multi-word pseudo-register and to find each insn which sets or
  1218. uses that register. That should be faster than scanning all the
  1219. insns. */
  1220. decomposable_context = BITMAP_ALLOC (NULL);
  1221. non_decomposable_context = BITMAP_ALLOC (NULL);
  1222. subreg_context = BITMAP_ALLOC (NULL);
  1223. reg_copy_graph.create (max);
  1224. reg_copy_graph.safe_grow_cleared (max);
  1225. memset (reg_copy_graph.address (), 0, sizeof (bitmap) * max);
  1226. speed_p = optimize_function_for_speed_p (cfun);
  1227. FOR_EACH_BB_FN (bb, cfun)
  1228. {
  1229. rtx_insn *insn;
  1230. FOR_BB_INSNS (bb, insn)
  1231. {
  1232. rtx set;
  1233. enum classify_move_insn cmi;
  1234. int i, n;
  1235. if (!INSN_P (insn)
  1236. || GET_CODE (PATTERN (insn)) == CLOBBER
  1237. || GET_CODE (PATTERN (insn)) == USE)
  1238. continue;
  1239. recog_memoized (insn);
  1240. if (find_decomposable_shift_zext (insn, speed_p))
  1241. continue;
  1242. extract_insn (insn);
  1243. set = simple_move (insn, speed_p);
  1244. if (!set)
  1245. cmi = NOT_SIMPLE_MOVE;
  1246. else
  1247. {
  1248. /* We mark pseudo-to-pseudo copies as decomposable during the
  1249. second pass only. The first pass is so early that there is
  1250. good chance such moves will be optimized away completely by
  1251. subsequent optimizations anyway.
  1252. However, we call find_pseudo_copy even during the first pass
  1253. so as to properly set up the reg_copy_graph. */
  1254. if (find_pseudo_copy (set))
  1255. cmi = decompose_copies? DECOMPOSABLE_SIMPLE_MOVE : SIMPLE_MOVE;
  1256. else
  1257. cmi = SIMPLE_MOVE;
  1258. }
  1259. n = recog_data.n_operands;
  1260. for (i = 0; i < n; ++i)
  1261. {
  1262. find_decomposable_subregs (&recog_data.operand[i], &cmi);
  1263. /* We handle ASM_OPERANDS as a special case to support
  1264. things like x86 rdtsc which returns a DImode value.
  1265. We can decompose the output, which will certainly be
  1266. operand 0, but not the inputs. */
  1267. if (cmi == SIMPLE_MOVE
  1268. && GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
  1269. {
  1270. gcc_assert (i == 0);
  1271. cmi = NOT_SIMPLE_MOVE;
  1272. }
  1273. }
  1274. }
  1275. }
  1276. bitmap_and_compl_into (decomposable_context, non_decomposable_context);
  1277. if (!bitmap_empty_p (decomposable_context))
  1278. {
  1279. sbitmap sub_blocks;
  1280. unsigned int i;
  1281. sbitmap_iterator sbi;
  1282. bitmap_iterator iter;
  1283. unsigned int regno;
  1284. propagate_pseudo_copies ();
  1285. sub_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
  1286. bitmap_clear (sub_blocks);
  1287. EXECUTE_IF_SET_IN_BITMAP (decomposable_context, 0, regno, iter)
  1288. decompose_register (regno);
  1289. FOR_EACH_BB_FN (bb, cfun)
  1290. {
  1291. rtx_insn *insn;
  1292. FOR_BB_INSNS (bb, insn)
  1293. {
  1294. rtx pat;
  1295. if (!INSN_P (insn))
  1296. continue;
  1297. pat = PATTERN (insn);
  1298. if (GET_CODE (pat) == CLOBBER)
  1299. resolve_clobber (pat, insn);
  1300. else if (GET_CODE (pat) == USE)
  1301. resolve_use (pat, insn);
  1302. else if (DEBUG_INSN_P (insn))
  1303. resolve_debug (insn);
  1304. else
  1305. {
  1306. rtx set;
  1307. int i;
  1308. recog_memoized (insn);
  1309. extract_insn (insn);
  1310. set = simple_move (insn, speed_p);
  1311. if (set)
  1312. {
  1313. rtx_insn *orig_insn = insn;
  1314. bool cfi = control_flow_insn_p (insn);
  1315. /* We can end up splitting loads to multi-word pseudos
  1316. into separate loads to machine word size pseudos.
  1317. When this happens, we first had one load that can
  1318. throw, and after resolve_simple_move we'll have a
  1319. bunch of loads (at least two). All those loads may
  1320. trap if we can have non-call exceptions, so they
  1321. all will end the current basic block. We split the
  1322. block after the outer loop over all insns, but we
  1323. make sure here that we will be able to split the
  1324. basic block and still produce the correct control
  1325. flow graph for it. */
  1326. gcc_assert (!cfi
  1327. || (cfun->can_throw_non_call_exceptions
  1328. && can_throw_internal (insn)));
  1329. insn = resolve_simple_move (set, insn);
  1330. if (insn != orig_insn)
  1331. {
  1332. recog_memoized (insn);
  1333. extract_insn (insn);
  1334. if (cfi)
  1335. bitmap_set_bit (sub_blocks, bb->index);
  1336. }
  1337. }
  1338. else
  1339. {
  1340. rtx_insn *decomposed_shift;
  1341. decomposed_shift = resolve_shift_zext (insn);
  1342. if (decomposed_shift != NULL_RTX)
  1343. {
  1344. insn = decomposed_shift;
  1345. recog_memoized (insn);
  1346. extract_insn (insn);
  1347. }
  1348. }
  1349. for (i = recog_data.n_operands - 1; i >= 0; --i)
  1350. resolve_subreg_use (recog_data.operand_loc[i], insn);
  1351. resolve_reg_notes (insn);
  1352. if (num_validated_changes () > 0)
  1353. {
  1354. for (i = recog_data.n_dups - 1; i >= 0; --i)
  1355. {
  1356. rtx *pl = recog_data.dup_loc[i];
  1357. int dup_num = recog_data.dup_num[i];
  1358. rtx *px = recog_data.operand_loc[dup_num];
  1359. validate_unshare_change (insn, pl, *px, 1);
  1360. }
  1361. i = apply_change_group ();
  1362. gcc_assert (i);
  1363. }
  1364. }
  1365. }
  1366. }
  1367. /* If we had insns to split that caused control flow insns in the middle
  1368. of a basic block, split those blocks now. Note that we only handle
  1369. the case where splitting a load has caused multiple possibly trapping
  1370. loads to appear. */
  1371. EXECUTE_IF_SET_IN_BITMAP (sub_blocks, 0, i, sbi)
  1372. {
  1373. rtx_insn *insn, *end;
  1374. edge fallthru;
  1375. bb = BASIC_BLOCK_FOR_FN (cfun, i);
  1376. insn = BB_HEAD (bb);
  1377. end = BB_END (bb);
  1378. while (insn != end)
  1379. {
  1380. if (control_flow_insn_p (insn))
  1381. {
  1382. /* Split the block after insn. There will be a fallthru
  1383. edge, which is OK so we keep it. We have to create the
  1384. exception edges ourselves. */
  1385. fallthru = split_block (bb, insn);
  1386. rtl_make_eh_edge (NULL, bb, BB_END (bb));
  1387. bb = fallthru->dest;
  1388. insn = BB_HEAD (bb);
  1389. }
  1390. else
  1391. insn = NEXT_INSN (insn);
  1392. }
  1393. }
  1394. sbitmap_free (sub_blocks);
  1395. }
  1396. {
  1397. unsigned int i;
  1398. bitmap b;
  1399. FOR_EACH_VEC_ELT (reg_copy_graph, i, b)
  1400. if (b)
  1401. BITMAP_FREE (b);
  1402. }
  1403. reg_copy_graph.release ();
  1404. BITMAP_FREE (decomposable_context);
  1405. BITMAP_FREE (non_decomposable_context);
  1406. BITMAP_FREE (subreg_context);
  1407. }
  1408. /* Implement first lower subreg pass. */
  1409. namespace {
  1410. const pass_data pass_data_lower_subreg =
  1411. {
  1412. RTL_PASS, /* type */
  1413. "subreg1", /* name */
  1414. OPTGROUP_NONE, /* optinfo_flags */
  1415. TV_LOWER_SUBREG, /* tv_id */
  1416. 0, /* properties_required */
  1417. 0, /* properties_provided */
  1418. 0, /* properties_destroyed */
  1419. 0, /* todo_flags_start */
  1420. 0, /* todo_flags_finish */
  1421. };
  1422. class pass_lower_subreg : public rtl_opt_pass
  1423. {
  1424. public:
  1425. pass_lower_subreg (gcc::context *ctxt)
  1426. : rtl_opt_pass (pass_data_lower_subreg, ctxt)
  1427. {}
  1428. /* opt_pass methods: */
  1429. virtual bool gate (function *) { return flag_split_wide_types != 0; }
  1430. virtual unsigned int execute (function *)
  1431. {
  1432. decompose_multiword_subregs (false);
  1433. return 0;
  1434. }
  1435. }; // class pass_lower_subreg
  1436. } // anon namespace
  1437. rtl_opt_pass *
  1438. make_pass_lower_subreg (gcc::context *ctxt)
  1439. {
  1440. return new pass_lower_subreg (ctxt);
  1441. }
  1442. /* Implement second lower subreg pass. */
  1443. namespace {
  1444. const pass_data pass_data_lower_subreg2 =
  1445. {
  1446. RTL_PASS, /* type */
  1447. "subreg2", /* name */
  1448. OPTGROUP_NONE, /* optinfo_flags */
  1449. TV_LOWER_SUBREG, /* tv_id */
  1450. 0, /* properties_required */
  1451. 0, /* properties_provided */
  1452. 0, /* properties_destroyed */
  1453. 0, /* todo_flags_start */
  1454. TODO_df_finish, /* todo_flags_finish */
  1455. };
  1456. class pass_lower_subreg2 : public rtl_opt_pass
  1457. {
  1458. public:
  1459. pass_lower_subreg2 (gcc::context *ctxt)
  1460. : rtl_opt_pass (pass_data_lower_subreg2, ctxt)
  1461. {}
  1462. /* opt_pass methods: */
  1463. virtual bool gate (function *) { return flag_split_wide_types != 0; }
  1464. virtual unsigned int execute (function *)
  1465. {
  1466. decompose_multiword_subregs (true);
  1467. return 0;
  1468. }
  1469. }; // class pass_lower_subreg2
  1470. } // anon namespace
  1471. rtl_opt_pass *
  1472. make_pass_lower_subreg2 (gcc::context *ctxt)
  1473. {
  1474. return new pass_lower_subreg2 (ctxt);
  1475. }