bt-load.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. /* Perform branch target register load optimizations.
  2. Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "rtl.h"
  20. #include "hard-reg-set.h"
  21. #include "regs.h"
  22. #include "target.h"
  23. #include "symtab.h"
  24. #include "hashtab.h"
  25. #include "hash-set.h"
  26. #include "vec.h"
  27. #include "machmode.h"
  28. #include "input.h"
  29. #include "function.h"
  30. #include "flags.h"
  31. #include "statistics.h"
  32. #include "double-int.h"
  33. #include "real.h"
  34. #include "fixed-value.h"
  35. #include "alias.h"
  36. #include "wide-int.h"
  37. #include "inchash.h"
  38. #include "tree.h"
  39. #include "insn-config.h"
  40. #include "expmed.h"
  41. #include "dojump.h"
  42. #include "explow.h"
  43. #include "calls.h"
  44. #include "emit-rtl.h"
  45. #include "varasm.h"
  46. #include "stmt.h"
  47. #include "expr.h"
  48. #include "insn-attr.h"
  49. #include "except.h"
  50. #include "tm_p.h"
  51. #include "diagnostic-core.h"
  52. #include "tree-pass.h"
  53. #include "recog.h"
  54. #include "dominance.h"
  55. #include "cfg.h"
  56. #include "cfgrtl.h"
  57. #include "cfganal.h"
  58. #include "cfgcleanup.h"
  59. #include "predict.h"
  60. #include "basic-block.h"
  61. #include "df.h"
  62. #include "cfgloop.h"
  63. #include "rtl-iter.h"
  64. #include "fibonacci_heap.h"
  65. /* Target register optimizations - these are performed after reload. */
  66. typedef struct btr_def_group_s
  67. {
  68. struct btr_def_group_s *next;
  69. rtx src;
  70. struct btr_def_s *members;
  71. } *btr_def_group;
  72. typedef struct btr_user_s
  73. {
  74. struct btr_user_s *next;
  75. basic_block bb;
  76. int luid;
  77. rtx_insn *insn;
  78. /* If INSN has a single use of a single branch register, then
  79. USE points to it within INSN. If there is more than
  80. one branch register use, or the use is in some way ambiguous,
  81. then USE is NULL. */
  82. rtx use;
  83. int n_reaching_defs;
  84. int first_reaching_def;
  85. char other_use_this_block;
  86. } *btr_user;
  87. /* btr_def structs appear on three lists:
  88. 1. A list of all btr_def structures (head is
  89. ALL_BTR_DEFS, linked by the NEXT field).
  90. 2. A list of branch reg definitions per basic block (head is
  91. BB_BTR_DEFS[i], linked by the NEXT_THIS_BB field).
  92. 3. A list of all branch reg definitions belonging to the same
  93. group (head is in a BTR_DEF_GROUP struct, linked by
  94. NEXT_THIS_GROUP field). */
  95. typedef struct btr_def_s
  96. {
  97. struct btr_def_s *next_this_bb;
  98. struct btr_def_s *next_this_group;
  99. basic_block bb;
  100. int luid;
  101. rtx_insn *insn;
  102. int btr;
  103. int cost;
  104. /* For a branch register setting insn that has a constant
  105. source (i.e. a label), group links together all the
  106. insns with the same source. For other branch register
  107. setting insns, group is NULL. */
  108. btr_def_group group;
  109. btr_user uses;
  110. /* If this def has a reaching use which is not a simple use
  111. in a branch instruction, then has_ambiguous_use will be true,
  112. and we will not attempt to migrate this definition. */
  113. char has_ambiguous_use;
  114. /* live_range is an approximation to the true live range for this
  115. def/use web, because it records the set of blocks that contain
  116. the live range. There could be other live ranges for the same
  117. branch register in that set of blocks, either in the block
  118. containing the def (before the def), or in a block containing
  119. a use (after the use). If there are such other live ranges, then
  120. other_btr_uses_before_def or other_btr_uses_after_use must be set true
  121. as appropriate. */
  122. char other_btr_uses_before_def;
  123. char other_btr_uses_after_use;
  124. /* We set own_end when we have moved a definition into a dominator.
  125. Thus, when a later combination removes this definition again, we know
  126. to clear out trs_live_at_end again. */
  127. char own_end;
  128. bitmap live_range;
  129. } *btr_def;
  130. typedef fibonacci_heap <long, btr_def_s> btr_heap_t;
  131. typedef fibonacci_node <long, btr_def_s> btr_heap_node_t;
  132. static int issue_rate;
  133. static int basic_block_freq (const_basic_block);
  134. static int insn_sets_btr_p (const rtx_insn *, int, int *);
  135. static void find_btr_def_group (btr_def_group *, btr_def);
  136. static btr_def add_btr_def (btr_heap_t *, basic_block, int, rtx_insn *,
  137. unsigned int, int, btr_def_group *);
  138. static btr_user new_btr_user (basic_block, int, rtx_insn *);
  139. static void dump_hard_reg_set (HARD_REG_SET);
  140. static void dump_btrs_live (int);
  141. static void note_other_use_this_block (unsigned int, btr_user);
  142. static void compute_defs_uses_and_gen (btr_heap_t *, btr_def *,btr_user *,
  143. sbitmap *, sbitmap *, HARD_REG_SET *);
  144. static void compute_kill (sbitmap *, sbitmap *, HARD_REG_SET *);
  145. static void compute_out (sbitmap *bb_out, sbitmap *, sbitmap *, int);
  146. static void link_btr_uses (btr_def *, btr_user *, sbitmap *, sbitmap *, int);
  147. static void build_btr_def_use_webs (btr_heap_t *);
  148. static int block_at_edge_of_live_range_p (int, btr_def);
  149. static void clear_btr_from_live_range (btr_def def);
  150. static void add_btr_to_live_range (btr_def, int);
  151. static void augment_live_range (bitmap, HARD_REG_SET *, basic_block,
  152. basic_block, int);
  153. static int choose_btr (HARD_REG_SET);
  154. static void combine_btr_defs (btr_def, HARD_REG_SET *);
  155. static void btr_def_live_range (btr_def, HARD_REG_SET *);
  156. static void move_btr_def (basic_block, int, btr_def, bitmap, HARD_REG_SET *);
  157. static int migrate_btr_def (btr_def, int);
  158. static void migrate_btr_defs (enum reg_class, int);
  159. static int can_move_up (const_basic_block, const rtx_insn *, int);
  160. static void note_btr_set (rtx, const_rtx, void *);
  161. /* The following code performs code motion of target load instructions
  162. (instructions that set branch target registers), to move them
  163. forward away from the branch instructions and out of loops (or,
  164. more generally, from a more frequently executed place to a less
  165. frequently executed place).
  166. Moving target load instructions further in front of the branch
  167. instruction that uses the target register value means that the hardware
  168. has a better chance of preloading the instructions at the branch
  169. target by the time the branch is reached. This avoids bubbles
  170. when a taken branch needs to flush out the pipeline.
  171. Moving target load instructions out of loops means they are executed
  172. less frequently. */
  173. /* An obstack to hold the def-use web data structures built up for
  174. migrating branch target load instructions. */
  175. static struct obstack migrate_btrl_obstack;
  176. /* Array indexed by basic block number, giving the set of registers
  177. live in that block. */
  178. static HARD_REG_SET *btrs_live;
  179. /* Array indexed by basic block number, giving the set of registers live at
  180. the end of that block, including any uses by a final jump insn, if any. */
  181. static HARD_REG_SET *btrs_live_at_end;
  182. /* Set of all target registers that we are willing to allocate. */
  183. static HARD_REG_SET all_btrs;
  184. /* Provide lower and upper bounds for target register numbers, so that
  185. we don't need to search through all the hard registers all the time. */
  186. static int first_btr, last_btr;
  187. /* Return an estimate of the frequency of execution of block bb. */
  188. static int
  189. basic_block_freq (const_basic_block bb)
  190. {
  191. return bb->frequency;
  192. }
  193. /* If X references (sets or reads) any branch target register, return one
  194. such register. If EXCLUDEP is set, disregard any references within
  195. that location. */
  196. static rtx *
  197. find_btr_use (rtx x, rtx *excludep = 0)
  198. {
  199. subrtx_ptr_iterator::array_type array;
  200. FOR_EACH_SUBRTX_PTR (iter, array, &x, NONCONST)
  201. {
  202. rtx *loc = *iter;
  203. if (loc == excludep)
  204. iter.skip_subrtxes ();
  205. else
  206. {
  207. const_rtx x = *loc;
  208. if (REG_P (x)
  209. && overlaps_hard_reg_set_p (all_btrs, GET_MODE (x), REGNO (x)))
  210. return loc;
  211. }
  212. }
  213. return 0;
  214. }
  215. /* Return true if insn is an instruction that sets a target register.
  216. if CHECK_CONST is true, only return true if the source is constant.
  217. If such a set is found and REGNO is nonzero, assign the register number
  218. of the destination register to *REGNO. */
  219. static int
  220. insn_sets_btr_p (const rtx_insn *insn, int check_const, int *regno)
  221. {
  222. rtx set;
  223. if (NONJUMP_INSN_P (insn)
  224. && (set = single_set (insn)))
  225. {
  226. rtx dest = SET_DEST (set);
  227. rtx src = SET_SRC (set);
  228. if (GET_CODE (dest) == SUBREG)
  229. dest = XEXP (dest, 0);
  230. if (REG_P (dest)
  231. && TEST_HARD_REG_BIT (all_btrs, REGNO (dest)))
  232. {
  233. gcc_assert (!find_btr_use (src));
  234. if (!check_const || CONSTANT_P (src))
  235. {
  236. if (regno)
  237. *regno = REGNO (dest);
  238. return 1;
  239. }
  240. }
  241. }
  242. return 0;
  243. }
  244. /* Find the group that the target register definition DEF belongs
  245. to in the list starting with *ALL_BTR_DEF_GROUPS. If no such
  246. group exists, create one. Add def to the group. */
  247. static void
  248. find_btr_def_group (btr_def_group *all_btr_def_groups, btr_def def)
  249. {
  250. if (insn_sets_btr_p (def->insn, 1, NULL))
  251. {
  252. btr_def_group this_group;
  253. rtx def_src = SET_SRC (single_set (def->insn));
  254. /* ?? This linear search is an efficiency concern, particularly
  255. as the search will almost always fail to find a match. */
  256. for (this_group = *all_btr_def_groups;
  257. this_group != NULL;
  258. this_group = this_group->next)
  259. if (rtx_equal_p (def_src, this_group->src))
  260. break;
  261. if (!this_group)
  262. {
  263. this_group = XOBNEW (&migrate_btrl_obstack, struct btr_def_group_s);
  264. this_group->src = def_src;
  265. this_group->members = NULL;
  266. this_group->next = *all_btr_def_groups;
  267. *all_btr_def_groups = this_group;
  268. }
  269. def->group = this_group;
  270. def->next_this_group = this_group->members;
  271. this_group->members = def;
  272. }
  273. else
  274. def->group = NULL;
  275. }
  276. /* Create a new target register definition structure, for a definition in
  277. block BB, instruction INSN, and insert it into ALL_BTR_DEFS. Return
  278. the new definition. */
  279. static btr_def
  280. add_btr_def (btr_heap_t *all_btr_defs, basic_block bb, int insn_luid,
  281. rtx_insn *insn,
  282. unsigned int dest_reg, int other_btr_uses_before_def,
  283. btr_def_group *all_btr_def_groups)
  284. {
  285. btr_def this_def = XOBNEW (&migrate_btrl_obstack, struct btr_def_s);
  286. this_def->bb = bb;
  287. this_def->luid = insn_luid;
  288. this_def->insn = insn;
  289. this_def->btr = dest_reg;
  290. this_def->cost = basic_block_freq (bb);
  291. this_def->has_ambiguous_use = 0;
  292. this_def->other_btr_uses_before_def = other_btr_uses_before_def;
  293. this_def->other_btr_uses_after_use = 0;
  294. this_def->next_this_bb = NULL;
  295. this_def->next_this_group = NULL;
  296. this_def->uses = NULL;
  297. this_def->live_range = NULL;
  298. find_btr_def_group (all_btr_def_groups, this_def);
  299. all_btr_defs->insert (-this_def->cost, this_def);
  300. if (dump_file)
  301. fprintf (dump_file,
  302. "Found target reg definition: sets %u { bb %d, insn %d }%s priority %d\n",
  303. dest_reg, bb->index, INSN_UID (insn),
  304. (this_def->group ? "" : ":not const"), this_def->cost);
  305. return this_def;
  306. }
  307. /* Create a new target register user structure, for a use in block BB,
  308. instruction INSN. Return the new user. */
  309. static btr_user
  310. new_btr_user (basic_block bb, int insn_luid, rtx_insn *insn)
  311. {
  312. /* This instruction reads target registers. We need
  313. to decide whether we can replace all target register
  314. uses easily.
  315. */
  316. rtx *usep = find_btr_use (PATTERN (insn));
  317. rtx use;
  318. btr_user user = NULL;
  319. if (usep)
  320. {
  321. int unambiguous_single_use;
  322. /* We want to ensure that USE is the only use of a target
  323. register in INSN, so that we know that to rewrite INSN to use
  324. a different target register, all we have to do is replace USE. */
  325. unambiguous_single_use = !find_btr_use (PATTERN (insn), usep);
  326. if (!unambiguous_single_use)
  327. usep = NULL;
  328. }
  329. use = usep ? *usep : NULL_RTX;
  330. user = XOBNEW (&migrate_btrl_obstack, struct btr_user_s);
  331. user->bb = bb;
  332. user->luid = insn_luid;
  333. user->insn = insn;
  334. user->use = use;
  335. user->other_use_this_block = 0;
  336. user->next = NULL;
  337. user->n_reaching_defs = 0;
  338. user->first_reaching_def = -1;
  339. if (dump_file)
  340. {
  341. fprintf (dump_file, "Uses target reg: { bb %d, insn %d }",
  342. bb->index, INSN_UID (insn));
  343. if (user->use)
  344. fprintf (dump_file, ": unambiguous use of reg %d\n",
  345. REGNO (user->use));
  346. }
  347. return user;
  348. }
  349. /* Write the contents of S to the dump file. */
  350. static void
  351. dump_hard_reg_set (HARD_REG_SET s)
  352. {
  353. int reg;
  354. for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
  355. if (TEST_HARD_REG_BIT (s, reg))
  356. fprintf (dump_file, " %d", reg);
  357. }
  358. /* Write the set of target regs live in block BB to the dump file. */
  359. static void
  360. dump_btrs_live (int bb)
  361. {
  362. fprintf (dump_file, "BB%d live:", bb);
  363. dump_hard_reg_set (btrs_live[bb]);
  364. fprintf (dump_file, "\n");
  365. }
  366. /* REGNO is the number of a branch target register that is being used or
  367. set. USERS_THIS_BB is a list of preceding branch target register users;
  368. If any of them use the same register, set their other_use_this_block
  369. flag. */
  370. static void
  371. note_other_use_this_block (unsigned int regno, btr_user users_this_bb)
  372. {
  373. btr_user user;
  374. for (user = users_this_bb; user != NULL; user = user->next)
  375. if (user->use && REGNO (user->use) == regno)
  376. user->other_use_this_block = 1;
  377. }
  378. typedef struct {
  379. btr_user users_this_bb;
  380. HARD_REG_SET btrs_written_in_block;
  381. HARD_REG_SET btrs_live_in_block;
  382. sbitmap bb_gen;
  383. sbitmap *btr_defset;
  384. } defs_uses_info;
  385. /* Called via note_stores or directly to register stores into /
  386. clobbers of a branch target register DEST that are not recognized as
  387. straightforward definitions. DATA points to information about the
  388. current basic block that needs updating. */
  389. static void
  390. note_btr_set (rtx dest, const_rtx set ATTRIBUTE_UNUSED, void *data)
  391. {
  392. defs_uses_info *info = (defs_uses_info *) data;
  393. int regno, end_regno;
  394. if (!REG_P (dest))
  395. return;
  396. regno = REGNO (dest);
  397. end_regno = END_HARD_REGNO (dest);
  398. for (; regno < end_regno; regno++)
  399. if (TEST_HARD_REG_BIT (all_btrs, regno))
  400. {
  401. note_other_use_this_block (regno, info->users_this_bb);
  402. SET_HARD_REG_BIT (info->btrs_written_in_block, regno);
  403. SET_HARD_REG_BIT (info->btrs_live_in_block, regno);
  404. bitmap_and_compl (info->bb_gen, info->bb_gen,
  405. info->btr_defset[regno - first_btr]);
  406. }
  407. }
  408. static void
  409. compute_defs_uses_and_gen (btr_heap_t *all_btr_defs, btr_def *def_array,
  410. btr_user *use_array, sbitmap *btr_defset,
  411. sbitmap *bb_gen, HARD_REG_SET *btrs_written)
  412. {
  413. /* Scan the code building up the set of all defs and all uses.
  414. For each target register, build the set of defs of that register.
  415. For each block, calculate the set of target registers
  416. written in that block.
  417. Also calculate the set of btrs ever live in that block.
  418. */
  419. int i;
  420. int insn_luid = 0;
  421. btr_def_group all_btr_def_groups = NULL;
  422. defs_uses_info info;
  423. bitmap_vector_clear (bb_gen, last_basic_block_for_fn (cfun));
  424. for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
  425. {
  426. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
  427. int reg;
  428. btr_def defs_this_bb = NULL;
  429. rtx_insn *insn;
  430. rtx_insn *last;
  431. int can_throw = 0;
  432. info.users_this_bb = NULL;
  433. info.bb_gen = bb_gen[i];
  434. info.btr_defset = btr_defset;
  435. CLEAR_HARD_REG_SET (info.btrs_live_in_block);
  436. CLEAR_HARD_REG_SET (info.btrs_written_in_block);
  437. for (reg = first_btr; reg <= last_btr; reg++)
  438. if (TEST_HARD_REG_BIT (all_btrs, reg)
  439. && REGNO_REG_SET_P (df_get_live_in (bb), reg))
  440. SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
  441. for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
  442. insn != last;
  443. insn = NEXT_INSN (insn), insn_luid++)
  444. {
  445. if (INSN_P (insn))
  446. {
  447. int regno;
  448. int insn_uid = INSN_UID (insn);
  449. if (insn_sets_btr_p (insn, 0, &regno))
  450. {
  451. btr_def def = add_btr_def (
  452. all_btr_defs, bb, insn_luid, insn, regno,
  453. TEST_HARD_REG_BIT (info.btrs_live_in_block, regno),
  454. &all_btr_def_groups);
  455. def_array[insn_uid] = def;
  456. SET_HARD_REG_BIT (info.btrs_written_in_block, regno);
  457. SET_HARD_REG_BIT (info.btrs_live_in_block, regno);
  458. bitmap_and_compl (bb_gen[i], bb_gen[i],
  459. btr_defset[regno - first_btr]);
  460. bitmap_set_bit (bb_gen[i], insn_uid);
  461. def->next_this_bb = defs_this_bb;
  462. defs_this_bb = def;
  463. bitmap_set_bit (btr_defset[regno - first_btr], insn_uid);
  464. note_other_use_this_block (regno, info.users_this_bb);
  465. }
  466. /* Check for the blockage emitted by expand_nl_goto_receiver. */
  467. else if (cfun->has_nonlocal_label
  468. && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE)
  469. {
  470. btr_user user;
  471. /* Do the equivalent of calling note_other_use_this_block
  472. for every target register. */
  473. for (user = info.users_this_bb; user != NULL;
  474. user = user->next)
  475. if (user->use)
  476. user->other_use_this_block = 1;
  477. IOR_HARD_REG_SET (info.btrs_written_in_block, all_btrs);
  478. IOR_HARD_REG_SET (info.btrs_live_in_block, all_btrs);
  479. bitmap_clear (info.bb_gen);
  480. }
  481. else
  482. {
  483. if (find_btr_use (PATTERN (insn)))
  484. {
  485. btr_user user = new_btr_user (bb, insn_luid, insn);
  486. use_array[insn_uid] = user;
  487. if (user->use)
  488. SET_HARD_REG_BIT (info.btrs_live_in_block,
  489. REGNO (user->use));
  490. else
  491. {
  492. int reg;
  493. for (reg = first_btr; reg <= last_btr; reg++)
  494. if (TEST_HARD_REG_BIT (all_btrs, reg)
  495. && refers_to_regno_p (reg, user->insn))
  496. {
  497. note_other_use_this_block (reg,
  498. info.users_this_bb);
  499. SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
  500. }
  501. note_stores (PATTERN (insn), note_btr_set, &info);
  502. }
  503. user->next = info.users_this_bb;
  504. info.users_this_bb = user;
  505. }
  506. if (CALL_P (insn))
  507. {
  508. HARD_REG_SET *clobbered = &call_used_reg_set;
  509. HARD_REG_SET call_saved;
  510. rtx pat = PATTERN (insn);
  511. int i;
  512. /* Check for sibcall. */
  513. if (GET_CODE (pat) == PARALLEL)
  514. for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
  515. if (ANY_RETURN_P (XVECEXP (pat, 0, i)))
  516. {
  517. COMPL_HARD_REG_SET (call_saved,
  518. call_used_reg_set);
  519. clobbered = &call_saved;
  520. }
  521. for (regno = first_btr; regno <= last_btr; regno++)
  522. if (TEST_HARD_REG_BIT (*clobbered, regno))
  523. note_btr_set (regno_reg_rtx[regno], NULL_RTX, &info);
  524. }
  525. }
  526. }
  527. }
  528. COPY_HARD_REG_SET (btrs_live[i], info.btrs_live_in_block);
  529. COPY_HARD_REG_SET (btrs_written[i], info.btrs_written_in_block);
  530. REG_SET_TO_HARD_REG_SET (btrs_live_at_end[i], df_get_live_out (bb));
  531. /* If this block ends in a jump insn, add any uses or even clobbers
  532. of branch target registers that it might have. */
  533. for (insn = BB_END (bb); insn != BB_HEAD (bb) && ! INSN_P (insn); )
  534. insn = PREV_INSN (insn);
  535. /* ??? for the fall-through edge, it would make sense to insert the
  536. btr set on the edge, but that would require to split the block
  537. early on so that we can distinguish between dominance from the fall
  538. through edge - which can use the call-clobbered registers - from
  539. dominance by the throw edge. */
  540. if (can_throw_internal (insn))
  541. {
  542. HARD_REG_SET tmp;
  543. COPY_HARD_REG_SET (tmp, call_used_reg_set);
  544. AND_HARD_REG_SET (tmp, all_btrs);
  545. IOR_HARD_REG_SET (btrs_live_at_end[i], tmp);
  546. can_throw = 1;
  547. }
  548. if (can_throw || JUMP_P (insn))
  549. {
  550. int regno;
  551. for (regno = first_btr; regno <= last_btr; regno++)
  552. if (refers_to_regno_p (regno, insn))
  553. SET_HARD_REG_BIT (btrs_live_at_end[i], regno);
  554. }
  555. if (dump_file)
  556. dump_btrs_live (i);
  557. }
  558. }
  559. static void
  560. compute_kill (sbitmap *bb_kill, sbitmap *btr_defset,
  561. HARD_REG_SET *btrs_written)
  562. {
  563. int i;
  564. int regno;
  565. /* For each basic block, form the set BB_KILL - the set
  566. of definitions that the block kills. */
  567. bitmap_vector_clear (bb_kill, last_basic_block_for_fn (cfun));
  568. for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
  569. {
  570. for (regno = first_btr; regno <= last_btr; regno++)
  571. if (TEST_HARD_REG_BIT (all_btrs, regno)
  572. && TEST_HARD_REG_BIT (btrs_written[i], regno))
  573. bitmap_ior (bb_kill[i], bb_kill[i],
  574. btr_defset[regno - first_btr]);
  575. }
  576. }
  577. static void
  578. compute_out (sbitmap *bb_out, sbitmap *bb_gen, sbitmap *bb_kill, int max_uid)
  579. {
  580. /* Perform iterative dataflow:
  581. Initially, for all blocks, BB_OUT = BB_GEN.
  582. For each block,
  583. BB_IN = union over predecessors of BB_OUT(pred)
  584. BB_OUT = (BB_IN - BB_KILL) + BB_GEN
  585. Iterate until the bb_out sets stop growing. */
  586. int i;
  587. int changed;
  588. sbitmap bb_in = sbitmap_alloc (max_uid);
  589. for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
  590. bitmap_copy (bb_out[i], bb_gen[i]);
  591. changed = 1;
  592. while (changed)
  593. {
  594. changed = 0;
  595. for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
  596. {
  597. bitmap_union_of_preds (bb_in, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
  598. changed |= bitmap_ior_and_compl (bb_out[i], bb_gen[i],
  599. bb_in, bb_kill[i]);
  600. }
  601. }
  602. sbitmap_free (bb_in);
  603. }
  604. static void
  605. link_btr_uses (btr_def *def_array, btr_user *use_array, sbitmap *bb_out,
  606. sbitmap *btr_defset, int max_uid)
  607. {
  608. int i;
  609. sbitmap reaching_defs = sbitmap_alloc (max_uid);
  610. /* Link uses to the uses lists of all of their reaching defs.
  611. Count up the number of reaching defs of each use. */
  612. for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
  613. {
  614. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
  615. rtx_insn *insn;
  616. rtx_insn *last;
  617. bitmap_union_of_preds (reaching_defs, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
  618. for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
  619. insn != last;
  620. insn = NEXT_INSN (insn))
  621. {
  622. if (INSN_P (insn))
  623. {
  624. int insn_uid = INSN_UID (insn);
  625. btr_def def = def_array[insn_uid];
  626. btr_user user = use_array[insn_uid];
  627. if (def != NULL)
  628. {
  629. /* Remove all reaching defs of regno except
  630. for this one. */
  631. bitmap_and_compl (reaching_defs, reaching_defs,
  632. btr_defset[def->btr - first_btr]);
  633. bitmap_set_bit (reaching_defs, insn_uid);
  634. }
  635. if (user != NULL)
  636. {
  637. /* Find all the reaching defs for this use. */
  638. sbitmap reaching_defs_of_reg = sbitmap_alloc (max_uid);
  639. unsigned int uid = 0;
  640. sbitmap_iterator sbi;
  641. if (user->use)
  642. bitmap_and (
  643. reaching_defs_of_reg,
  644. reaching_defs,
  645. btr_defset[REGNO (user->use) - first_btr]);
  646. else
  647. {
  648. int reg;
  649. bitmap_clear (reaching_defs_of_reg);
  650. for (reg = first_btr; reg <= last_btr; reg++)
  651. if (TEST_HARD_REG_BIT (all_btrs, reg)
  652. && refers_to_regno_p (reg, user->insn))
  653. bitmap_or_and (reaching_defs_of_reg,
  654. reaching_defs_of_reg,
  655. reaching_defs,
  656. btr_defset[reg - first_btr]);
  657. }
  658. EXECUTE_IF_SET_IN_BITMAP (reaching_defs_of_reg, 0, uid, sbi)
  659. {
  660. btr_def def = def_array[uid];
  661. /* We now know that def reaches user. */
  662. if (dump_file)
  663. fprintf (dump_file,
  664. "Def in insn %d reaches use in insn %d\n",
  665. uid, insn_uid);
  666. user->n_reaching_defs++;
  667. if (!user->use)
  668. def->has_ambiguous_use = 1;
  669. if (user->first_reaching_def != -1)
  670. { /* There is more than one reaching def. This is
  671. a rare case, so just give up on this def/use
  672. web when it occurs. */
  673. def->has_ambiguous_use = 1;
  674. def_array[user->first_reaching_def]
  675. ->has_ambiguous_use = 1;
  676. if (dump_file)
  677. fprintf (dump_file,
  678. "(use %d has multiple reaching defs)\n",
  679. insn_uid);
  680. }
  681. else
  682. user->first_reaching_def = uid;
  683. if (user->other_use_this_block)
  684. def->other_btr_uses_after_use = 1;
  685. user->next = def->uses;
  686. def->uses = user;
  687. }
  688. sbitmap_free (reaching_defs_of_reg);
  689. }
  690. if (CALL_P (insn))
  691. {
  692. int regno;
  693. for (regno = first_btr; regno <= last_btr; regno++)
  694. if (TEST_HARD_REG_BIT (all_btrs, regno)
  695. && TEST_HARD_REG_BIT (call_used_reg_set, regno))
  696. bitmap_and_compl (reaching_defs, reaching_defs,
  697. btr_defset[regno - first_btr]);
  698. }
  699. }
  700. }
  701. }
  702. sbitmap_free (reaching_defs);
  703. }
  704. static void
  705. build_btr_def_use_webs (btr_heap_t *all_btr_defs)
  706. {
  707. const int max_uid = get_max_uid ();
  708. btr_def *def_array = XCNEWVEC (btr_def, max_uid);
  709. btr_user *use_array = XCNEWVEC (btr_user, max_uid);
  710. sbitmap *btr_defset = sbitmap_vector_alloc (
  711. (last_btr - first_btr) + 1, max_uid);
  712. sbitmap *bb_gen = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
  713. max_uid);
  714. HARD_REG_SET *btrs_written = XCNEWVEC (HARD_REG_SET,
  715. last_basic_block_for_fn (cfun));
  716. sbitmap *bb_kill;
  717. sbitmap *bb_out;
  718. bitmap_vector_clear (btr_defset, (last_btr - first_btr) + 1);
  719. compute_defs_uses_and_gen (all_btr_defs, def_array, use_array, btr_defset,
  720. bb_gen, btrs_written);
  721. bb_kill = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), max_uid);
  722. compute_kill (bb_kill, btr_defset, btrs_written);
  723. free (btrs_written);
  724. bb_out = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), max_uid);
  725. compute_out (bb_out, bb_gen, bb_kill, max_uid);
  726. sbitmap_vector_free (bb_gen);
  727. sbitmap_vector_free (bb_kill);
  728. link_btr_uses (def_array, use_array, bb_out, btr_defset, max_uid);
  729. sbitmap_vector_free (bb_out);
  730. sbitmap_vector_free (btr_defset);
  731. free (use_array);
  732. free (def_array);
  733. }
  734. /* Return true if basic block BB contains the start or end of the
  735. live range of the definition DEF, AND there are other live
  736. ranges of the same target register that include BB. */
  737. static int
  738. block_at_edge_of_live_range_p (int bb, btr_def def)
  739. {
  740. if (def->other_btr_uses_before_def
  741. && BASIC_BLOCK_FOR_FN (cfun, bb) == def->bb)
  742. return 1;
  743. else if (def->other_btr_uses_after_use)
  744. {
  745. btr_user user;
  746. for (user = def->uses; user != NULL; user = user->next)
  747. if (BASIC_BLOCK_FOR_FN (cfun, bb) == user->bb)
  748. return 1;
  749. }
  750. return 0;
  751. }
  752. /* We are removing the def/use web DEF. The target register
  753. used in this web is therefore no longer live in the live range
  754. of this web, so remove it from the live set of all basic blocks
  755. in the live range of the web.
  756. Blocks at the boundary of the live range may contain other live
  757. ranges for the same target register, so we have to be careful
  758. to remove the target register from the live set of these blocks
  759. only if they do not contain other live ranges for the same register. */
  760. static void
  761. clear_btr_from_live_range (btr_def def)
  762. {
  763. unsigned bb;
  764. bitmap_iterator bi;
  765. EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
  766. {
  767. if ((!def->other_btr_uses_before_def
  768. && !def->other_btr_uses_after_use)
  769. || !block_at_edge_of_live_range_p (bb, def))
  770. {
  771. CLEAR_HARD_REG_BIT (btrs_live[bb], def->btr);
  772. CLEAR_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
  773. if (dump_file)
  774. dump_btrs_live (bb);
  775. }
  776. }
  777. if (def->own_end)
  778. CLEAR_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
  779. }
  780. /* We are adding the def/use web DEF. Add the target register used
  781. in this web to the live set of all of the basic blocks that contain
  782. the live range of the web.
  783. If OWN_END is set, also show that the register is live from our
  784. definitions at the end of the basic block where it is defined. */
  785. static void
  786. add_btr_to_live_range (btr_def def, int own_end)
  787. {
  788. unsigned bb;
  789. bitmap_iterator bi;
  790. EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
  791. {
  792. SET_HARD_REG_BIT (btrs_live[bb], def->btr);
  793. SET_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
  794. if (dump_file)
  795. dump_btrs_live (bb);
  796. }
  797. if (own_end)
  798. {
  799. SET_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
  800. def->own_end = 1;
  801. }
  802. }
  803. /* Update a live range to contain the basic block NEW_BLOCK, and all
  804. blocks on paths between the existing live range and NEW_BLOCK.
  805. HEAD is a block contained in the existing live range that dominates
  806. all other blocks in the existing live range.
  807. Also add to the set BTRS_LIVE_IN_RANGE all target registers that
  808. are live in the blocks that we add to the live range.
  809. If FULL_RANGE is set, include the full live range of NEW_BB;
  810. otherwise, if NEW_BB dominates HEAD_BB, only add registers that
  811. are life at the end of NEW_BB for NEW_BB itself.
  812. It is a precondition that either NEW_BLOCK dominates HEAD,or
  813. HEAD dom NEW_BLOCK. This is used to speed up the
  814. implementation of this function. */
  815. static void
  816. augment_live_range (bitmap live_range, HARD_REG_SET *btrs_live_in_range,
  817. basic_block head_bb, basic_block new_bb, int full_range)
  818. {
  819. basic_block *worklist, *tos;
  820. tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
  821. if (dominated_by_p (CDI_DOMINATORS, new_bb, head_bb))
  822. {
  823. if (new_bb == head_bb)
  824. {
  825. if (full_range)
  826. IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_bb->index]);
  827. free (tos);
  828. return;
  829. }
  830. *tos++ = new_bb;
  831. }
  832. else
  833. {
  834. edge e;
  835. edge_iterator ei;
  836. int new_block = new_bb->index;
  837. gcc_assert (dominated_by_p (CDI_DOMINATORS, head_bb, new_bb));
  838. IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[head_bb->index]);
  839. bitmap_set_bit (live_range, new_block);
  840. /* A previous btr migration could have caused a register to be
  841. live just at the end of new_block which we need in full, so
  842. use trs_live_at_end even if full_range is set. */
  843. IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live_at_end[new_block]);
  844. if (full_range)
  845. IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_block]);
  846. if (dump_file)
  847. {
  848. fprintf (dump_file,
  849. "Adding end of block %d and rest of %d to live range\n",
  850. new_block, head_bb->index);
  851. fprintf (dump_file,"Now live btrs are ");
  852. dump_hard_reg_set (*btrs_live_in_range);
  853. fprintf (dump_file, "\n");
  854. }
  855. FOR_EACH_EDGE (e, ei, head_bb->preds)
  856. *tos++ = e->src;
  857. }
  858. while (tos != worklist)
  859. {
  860. basic_block bb = *--tos;
  861. if (!bitmap_bit_p (live_range, bb->index))
  862. {
  863. edge e;
  864. edge_iterator ei;
  865. bitmap_set_bit (live_range, bb->index);
  866. IOR_HARD_REG_SET (*btrs_live_in_range,
  867. btrs_live[bb->index]);
  868. /* A previous btr migration could have caused a register to be
  869. live just at the end of a block which we need in full. */
  870. IOR_HARD_REG_SET (*btrs_live_in_range,
  871. btrs_live_at_end[bb->index]);
  872. if (dump_file)
  873. {
  874. fprintf (dump_file,
  875. "Adding block %d to live range\n", bb->index);
  876. fprintf (dump_file,"Now live btrs are ");
  877. dump_hard_reg_set (*btrs_live_in_range);
  878. fprintf (dump_file, "\n");
  879. }
  880. FOR_EACH_EDGE (e, ei, bb->preds)
  881. {
  882. basic_block pred = e->src;
  883. if (!bitmap_bit_p (live_range, pred->index))
  884. *tos++ = pred;
  885. }
  886. }
  887. }
  888. free (worklist);
  889. }
  890. /* Return the most desirable target register that is not in
  891. the set USED_BTRS. */
  892. static int
  893. choose_btr (HARD_REG_SET used_btrs)
  894. {
  895. int i;
  896. if (!hard_reg_set_subset_p (all_btrs, used_btrs))
  897. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  898. {
  899. #ifdef REG_ALLOC_ORDER
  900. int regno = reg_alloc_order[i];
  901. #else
  902. int regno = i;
  903. #endif
  904. if (TEST_HARD_REG_BIT (all_btrs, regno)
  905. && !TEST_HARD_REG_BIT (used_btrs, regno))
  906. return regno;
  907. }
  908. return -1;
  909. }
  910. /* Calculate the set of basic blocks that contain the live range of
  911. the def/use web DEF.
  912. Also calculate the set of target registers that are live at time
  913. in this live range, but ignore the live range represented by DEF
  914. when calculating this set. */
  915. static void
  916. btr_def_live_range (btr_def def, HARD_REG_SET *btrs_live_in_range)
  917. {
  918. if (!def->live_range)
  919. {
  920. btr_user user;
  921. def->live_range = BITMAP_ALLOC (NULL);
  922. bitmap_set_bit (def->live_range, def->bb->index);
  923. COPY_HARD_REG_SET (*btrs_live_in_range,
  924. (flag_btr_bb_exclusive
  925. ? btrs_live : btrs_live_at_end)[def->bb->index]);
  926. for (user = def->uses; user != NULL; user = user->next)
  927. augment_live_range (def->live_range, btrs_live_in_range,
  928. def->bb, user->bb,
  929. (flag_btr_bb_exclusive
  930. || user->insn != BB_END (def->bb)
  931. || !JUMP_P (user->insn)));
  932. }
  933. else
  934. {
  935. /* def->live_range is accurate, but we need to recompute
  936. the set of target registers live over it, because migration
  937. of other PT instructions may have affected it.
  938. */
  939. unsigned bb;
  940. unsigned def_bb = flag_btr_bb_exclusive ? -1 : def->bb->index;
  941. bitmap_iterator bi;
  942. CLEAR_HARD_REG_SET (*btrs_live_in_range);
  943. EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
  944. {
  945. IOR_HARD_REG_SET (*btrs_live_in_range,
  946. (def_bb == bb
  947. ? btrs_live_at_end : btrs_live) [bb]);
  948. }
  949. }
  950. if (!def->other_btr_uses_before_def &&
  951. !def->other_btr_uses_after_use)
  952. CLEAR_HARD_REG_BIT (*btrs_live_in_range, def->btr);
  953. }
  954. /* Merge into the def/use web DEF any other def/use webs in the same
  955. group that are dominated by DEF, provided that there is a target
  956. register available to allocate to the merged web. */
  957. static void
  958. combine_btr_defs (btr_def def, HARD_REG_SET *btrs_live_in_range)
  959. {
  960. btr_def other_def;
  961. for (other_def = def->group->members;
  962. other_def != NULL;
  963. other_def = other_def->next_this_group)
  964. {
  965. if (other_def != def
  966. && other_def->uses != NULL
  967. && ! other_def->has_ambiguous_use
  968. && dominated_by_p (CDI_DOMINATORS, other_def->bb, def->bb))
  969. {
  970. /* def->bb dominates the other def, so def and other_def could
  971. be combined. */
  972. /* Merge their live ranges, and get the set of
  973. target registers live over the merged range. */
  974. int btr;
  975. HARD_REG_SET combined_btrs_live;
  976. bitmap combined_live_range = BITMAP_ALLOC (NULL);
  977. btr_user user;
  978. if (other_def->live_range == NULL)
  979. {
  980. HARD_REG_SET dummy_btrs_live_in_range;
  981. btr_def_live_range (other_def, &dummy_btrs_live_in_range);
  982. }
  983. COPY_HARD_REG_SET (combined_btrs_live, *btrs_live_in_range);
  984. bitmap_copy (combined_live_range, def->live_range);
  985. for (user = other_def->uses; user != NULL; user = user->next)
  986. augment_live_range (combined_live_range, &combined_btrs_live,
  987. def->bb, user->bb,
  988. (flag_btr_bb_exclusive
  989. || user->insn != BB_END (def->bb)
  990. || !JUMP_P (user->insn)));
  991. btr = choose_btr (combined_btrs_live);
  992. if (btr != -1)
  993. {
  994. /* We can combine them. */
  995. if (dump_file)
  996. fprintf (dump_file,
  997. "Combining def in insn %d with def in insn %d\n",
  998. INSN_UID (other_def->insn), INSN_UID (def->insn));
  999. def->btr = btr;
  1000. user = other_def->uses;
  1001. while (user != NULL)
  1002. {
  1003. btr_user next = user->next;
  1004. user->next = def->uses;
  1005. def->uses = user;
  1006. user = next;
  1007. }
  1008. /* Combining def/use webs can make target registers live
  1009. after uses where they previously were not. This means
  1010. some REG_DEAD notes may no longer be correct. We could
  1011. be more precise about this if we looked at the combined
  1012. live range, but here I just delete any REG_DEAD notes
  1013. in case they are no longer correct. */
  1014. for (user = def->uses; user != NULL; user = user->next)
  1015. remove_note (user->insn,
  1016. find_regno_note (user->insn, REG_DEAD,
  1017. REGNO (user->use)));
  1018. clear_btr_from_live_range (other_def);
  1019. other_def->uses = NULL;
  1020. bitmap_copy (def->live_range, combined_live_range);
  1021. if (other_def->btr == btr && other_def->other_btr_uses_after_use)
  1022. def->other_btr_uses_after_use = 1;
  1023. COPY_HARD_REG_SET (*btrs_live_in_range, combined_btrs_live);
  1024. /* Delete the old target register initialization. */
  1025. delete_insn (other_def->insn);
  1026. }
  1027. BITMAP_FREE (combined_live_range);
  1028. }
  1029. }
  1030. }
  1031. /* Move the definition DEF from its current position to basic
  1032. block NEW_DEF_BB, and modify it to use branch target register BTR.
  1033. Delete the old defining insn, and insert a new one in NEW_DEF_BB.
  1034. Update all reaching uses of DEF in the RTL to use BTR.
  1035. If this new position means that other defs in the
  1036. same group can be combined with DEF then combine them. */
  1037. static void
  1038. move_btr_def (basic_block new_def_bb, int btr, btr_def def, bitmap live_range,
  1039. HARD_REG_SET *btrs_live_in_range)
  1040. {
  1041. /* We can move the instruction.
  1042. Set a target register in block NEW_DEF_BB to the value
  1043. needed for this target register definition.
  1044. Replace all uses of the old target register definition by
  1045. uses of the new definition. Delete the old definition. */
  1046. basic_block b = new_def_bb;
  1047. rtx_insn *insp = BB_HEAD (b);
  1048. rtx_insn *old_insn = def->insn;
  1049. rtx src;
  1050. rtx btr_rtx;
  1051. rtx_insn *new_insn;
  1052. machine_mode btr_mode;
  1053. btr_user user;
  1054. rtx set;
  1055. if (dump_file)
  1056. fprintf(dump_file, "migrating to basic block %d, using reg %d\n",
  1057. new_def_bb->index, btr);
  1058. clear_btr_from_live_range (def);
  1059. def->btr = btr;
  1060. def->bb = new_def_bb;
  1061. def->luid = 0;
  1062. def->cost = basic_block_freq (new_def_bb);
  1063. bitmap_copy (def->live_range, live_range);
  1064. combine_btr_defs (def, btrs_live_in_range);
  1065. btr = def->btr;
  1066. def->other_btr_uses_before_def
  1067. = TEST_HARD_REG_BIT (btrs_live[b->index], btr) ? 1 : 0;
  1068. add_btr_to_live_range (def, 1);
  1069. if (LABEL_P (insp))
  1070. insp = NEXT_INSN (insp);
  1071. /* N.B.: insp is expected to be NOTE_INSN_BASIC_BLOCK now. Some
  1072. optimizations can result in insp being both first and last insn of
  1073. its basic block. */
  1074. /* ?? some assertions to check that insp is sensible? */
  1075. if (def->other_btr_uses_before_def)
  1076. {
  1077. insp = BB_END (b);
  1078. for (insp = BB_END (b); ! INSN_P (insp); insp = PREV_INSN (insp))
  1079. gcc_assert (insp != BB_HEAD (b));
  1080. if (JUMP_P (insp) || can_throw_internal (insp))
  1081. insp = PREV_INSN (insp);
  1082. }
  1083. set = single_set (old_insn);
  1084. src = SET_SRC (set);
  1085. btr_mode = GET_MODE (SET_DEST (set));
  1086. btr_rtx = gen_rtx_REG (btr_mode, btr);
  1087. new_insn = as_a <rtx_insn *> (gen_move_insn (btr_rtx, src));
  1088. /* Insert target register initialization at head of basic block. */
  1089. def->insn = emit_insn_after (new_insn, insp);
  1090. df_set_regs_ever_live (btr, true);
  1091. if (dump_file)
  1092. fprintf (dump_file, "New pt is insn %d, inserted after insn %d\n",
  1093. INSN_UID (def->insn), INSN_UID (insp));
  1094. /* Delete the old target register initialization. */
  1095. delete_insn (old_insn);
  1096. /* Replace each use of the old target register by a use of the new target
  1097. register. */
  1098. for (user = def->uses; user != NULL; user = user->next)
  1099. {
  1100. /* Some extra work here to ensure consistent modes, because
  1101. it seems that a target register REG rtx can be given a different
  1102. mode depending on the context (surely that should not be
  1103. the case?). */
  1104. rtx replacement_rtx;
  1105. if (GET_MODE (user->use) == GET_MODE (btr_rtx)
  1106. || GET_MODE (user->use) == VOIDmode)
  1107. replacement_rtx = btr_rtx;
  1108. else
  1109. replacement_rtx = gen_rtx_REG (GET_MODE (user->use), btr);
  1110. validate_replace_rtx (user->use, replacement_rtx, user->insn);
  1111. user->use = replacement_rtx;
  1112. }
  1113. }
  1114. /* We anticipate intra-block scheduling to be done. See if INSN could move
  1115. up within BB by N_INSNS. */
  1116. static int
  1117. can_move_up (const_basic_block bb, const rtx_insn *insn, int n_insns)
  1118. {
  1119. while (insn != BB_HEAD (bb) && n_insns > 0)
  1120. {
  1121. insn = PREV_INSN (insn);
  1122. /* ??? What if we have an anti-dependency that actually prevents the
  1123. scheduler from doing the move? We'd like to re-allocate the register,
  1124. but not necessarily put the load into another basic block. */
  1125. if (INSN_P (insn))
  1126. n_insns--;
  1127. }
  1128. return n_insns <= 0;
  1129. }
  1130. /* Attempt to migrate the target register definition DEF to an
  1131. earlier point in the flowgraph.
  1132. It is a precondition of this function that DEF is migratable:
  1133. i.e. it has a constant source, and all uses are unambiguous.
  1134. Only migrations that reduce the cost of DEF will be made.
  1135. MIN_COST is the lower bound on the cost of the DEF after migration.
  1136. If we migrate DEF so that its cost falls below MIN_COST,
  1137. then we do not attempt to migrate further. The idea is that
  1138. we migrate definitions in a priority order based on their cost,
  1139. when the cost of this definition falls below MIN_COST, then
  1140. there is another definition with cost == MIN_COST which now
  1141. has a higher priority than this definition.
  1142. Return nonzero if there may be benefit from attempting to
  1143. migrate this DEF further (i.e. we have reduced the cost below
  1144. MIN_COST, but we may be able to reduce it further).
  1145. Return zero if no further migration is possible. */
  1146. static int
  1147. migrate_btr_def (btr_def def, int min_cost)
  1148. {
  1149. bitmap live_range;
  1150. HARD_REG_SET btrs_live_in_range;
  1151. int btr_used_near_def = 0;
  1152. int def_basic_block_freq;
  1153. basic_block attempt;
  1154. int give_up = 0;
  1155. int def_moved = 0;
  1156. btr_user user;
  1157. int def_latency;
  1158. if (dump_file)
  1159. fprintf (dump_file,
  1160. "Attempting to migrate pt from insn %d (cost = %d, min_cost = %d) ... ",
  1161. INSN_UID (def->insn), def->cost, min_cost);
  1162. if (!def->group || def->has_ambiguous_use)
  1163. /* These defs are not migratable. */
  1164. {
  1165. if (dump_file)
  1166. fprintf (dump_file, "it's not migratable\n");
  1167. return 0;
  1168. }
  1169. if (!def->uses)
  1170. /* We have combined this def with another in the same group, so
  1171. no need to consider it further.
  1172. */
  1173. {
  1174. if (dump_file)
  1175. fprintf (dump_file, "it's already combined with another pt\n");
  1176. return 0;
  1177. }
  1178. btr_def_live_range (def, &btrs_live_in_range);
  1179. live_range = BITMAP_ALLOC (NULL);
  1180. bitmap_copy (live_range, def->live_range);
  1181. #ifdef INSN_SCHEDULING
  1182. def_latency = insn_default_latency (def->insn) * issue_rate;
  1183. #else
  1184. def_latency = issue_rate;
  1185. #endif
  1186. for (user = def->uses; user != NULL; user = user->next)
  1187. {
  1188. if (user->bb == def->bb
  1189. && user->luid > def->luid
  1190. && (def->luid + def_latency) > user->luid
  1191. && ! can_move_up (def->bb, def->insn,
  1192. (def->luid + def_latency) - user->luid))
  1193. {
  1194. btr_used_near_def = 1;
  1195. break;
  1196. }
  1197. }
  1198. def_basic_block_freq = basic_block_freq (def->bb);
  1199. for (attempt = get_immediate_dominator (CDI_DOMINATORS, def->bb);
  1200. !give_up && attempt && attempt != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  1201. && def->cost >= min_cost;
  1202. attempt = get_immediate_dominator (CDI_DOMINATORS, attempt))
  1203. {
  1204. /* Try to move the instruction that sets the target register into
  1205. basic block ATTEMPT. */
  1206. int try_freq = basic_block_freq (attempt);
  1207. edge_iterator ei;
  1208. edge e;
  1209. /* If ATTEMPT has abnormal edges, skip it. */
  1210. FOR_EACH_EDGE (e, ei, attempt->succs)
  1211. if (e->flags & EDGE_COMPLEX)
  1212. break;
  1213. if (e)
  1214. continue;
  1215. if (dump_file)
  1216. fprintf (dump_file, "trying block %d ...", attempt->index);
  1217. if (try_freq < def_basic_block_freq
  1218. || (try_freq == def_basic_block_freq && btr_used_near_def))
  1219. {
  1220. int btr;
  1221. augment_live_range (live_range, &btrs_live_in_range, def->bb, attempt,
  1222. flag_btr_bb_exclusive);
  1223. if (dump_file)
  1224. {
  1225. fprintf (dump_file, "Now btrs live in range are: ");
  1226. dump_hard_reg_set (btrs_live_in_range);
  1227. fprintf (dump_file, "\n");
  1228. }
  1229. btr = choose_btr (btrs_live_in_range);
  1230. if (btr != -1)
  1231. {
  1232. move_btr_def (attempt, btr, def, live_range, &btrs_live_in_range);
  1233. bitmap_copy (live_range, def->live_range);
  1234. btr_used_near_def = 0;
  1235. def_moved = 1;
  1236. def_basic_block_freq = basic_block_freq (def->bb);
  1237. }
  1238. else
  1239. {
  1240. /* There are no free target registers available to move
  1241. this far forward, so give up */
  1242. give_up = 1;
  1243. if (dump_file)
  1244. fprintf (dump_file,
  1245. "giving up because there are no free target registers\n");
  1246. }
  1247. }
  1248. }
  1249. if (!def_moved)
  1250. {
  1251. give_up = 1;
  1252. if (dump_file)
  1253. fprintf (dump_file, "failed to move\n");
  1254. }
  1255. BITMAP_FREE (live_range);
  1256. return !give_up;
  1257. }
  1258. /* Attempt to move instructions that set target registers earlier
  1259. in the flowgraph, away from their corresponding uses. */
  1260. static void
  1261. migrate_btr_defs (enum reg_class btr_class, int allow_callee_save)
  1262. {
  1263. btr_heap_t all_btr_defs (LONG_MIN);
  1264. int reg;
  1265. gcc_obstack_init (&migrate_btrl_obstack);
  1266. if (dump_file)
  1267. {
  1268. int i;
  1269. for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
  1270. {
  1271. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
  1272. fprintf (dump_file,
  1273. "Basic block %d: count = %" PRId64
  1274. " loop-depth = %d idom = %d\n",
  1275. i, (int64_t) bb->count, bb_loop_depth (bb),
  1276. get_immediate_dominator (CDI_DOMINATORS, bb)->index);
  1277. }
  1278. }
  1279. CLEAR_HARD_REG_SET (all_btrs);
  1280. for (first_btr = -1, reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
  1281. if (TEST_HARD_REG_BIT (reg_class_contents[(int) btr_class], reg)
  1282. && (allow_callee_save || call_used_regs[reg]
  1283. || df_regs_ever_live_p (reg)))
  1284. {
  1285. SET_HARD_REG_BIT (all_btrs, reg);
  1286. last_btr = reg;
  1287. if (first_btr < 0)
  1288. first_btr = reg;
  1289. }
  1290. btrs_live = XCNEWVEC (HARD_REG_SET, last_basic_block_for_fn (cfun));
  1291. btrs_live_at_end = XCNEWVEC (HARD_REG_SET, last_basic_block_for_fn (cfun));
  1292. build_btr_def_use_webs (&all_btr_defs);
  1293. while (!all_btr_defs.empty ())
  1294. {
  1295. int min_cost = -all_btr_defs.min_key ();
  1296. btr_def def = all_btr_defs.extract_min ();
  1297. if (migrate_btr_def (def, min_cost))
  1298. {
  1299. all_btr_defs.insert (-def->cost, def);
  1300. if (dump_file)
  1301. {
  1302. fprintf (dump_file,
  1303. "Putting insn %d back on queue with priority %d\n",
  1304. INSN_UID (def->insn), def->cost);
  1305. }
  1306. }
  1307. else
  1308. BITMAP_FREE (def->live_range);
  1309. }
  1310. free (btrs_live);
  1311. free (btrs_live_at_end);
  1312. obstack_free (&migrate_btrl_obstack, NULL);
  1313. }
  1314. static void
  1315. branch_target_load_optimize (bool after_prologue_epilogue_gen)
  1316. {
  1317. enum reg_class klass
  1318. = (enum reg_class) targetm.branch_target_register_class ();
  1319. if (klass != NO_REGS)
  1320. {
  1321. /* Initialize issue_rate. */
  1322. if (targetm.sched.issue_rate)
  1323. issue_rate = targetm.sched.issue_rate ();
  1324. else
  1325. issue_rate = 1;
  1326. if (!after_prologue_epilogue_gen)
  1327. {
  1328. /* Build the CFG for migrate_btr_defs. */
  1329. #if 1
  1330. /* This may or may not be needed, depending on where we
  1331. run this phase. */
  1332. cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
  1333. #endif
  1334. }
  1335. df_analyze ();
  1336. /* Dominator info is also needed for migrate_btr_def. */
  1337. calculate_dominance_info (CDI_DOMINATORS);
  1338. migrate_btr_defs (klass,
  1339. (targetm.branch_target_register_callee_saved
  1340. (after_prologue_epilogue_gen)));
  1341. free_dominance_info (CDI_DOMINATORS);
  1342. }
  1343. }
  1344. namespace {
  1345. const pass_data pass_data_branch_target_load_optimize1 =
  1346. {
  1347. RTL_PASS, /* type */
  1348. "btl1", /* name */
  1349. OPTGROUP_NONE, /* optinfo_flags */
  1350. TV_NONE, /* tv_id */
  1351. 0, /* properties_required */
  1352. 0, /* properties_provided */
  1353. 0, /* properties_destroyed */
  1354. 0, /* todo_flags_start */
  1355. 0, /* todo_flags_finish */
  1356. };
  1357. class pass_branch_target_load_optimize1 : public rtl_opt_pass
  1358. {
  1359. public:
  1360. pass_branch_target_load_optimize1 (gcc::context *ctxt)
  1361. : rtl_opt_pass (pass_data_branch_target_load_optimize1, ctxt)
  1362. {}
  1363. /* opt_pass methods: */
  1364. virtual bool gate (function *) { return flag_branch_target_load_optimize; }
  1365. virtual unsigned int execute (function *)
  1366. {
  1367. branch_target_load_optimize (epilogue_completed);
  1368. return 0;
  1369. }
  1370. }; // class pass_branch_target_load_optimize1
  1371. } // anon namespace
  1372. rtl_opt_pass *
  1373. make_pass_branch_target_load_optimize1 (gcc::context *ctxt)
  1374. {
  1375. return new pass_branch_target_load_optimize1 (ctxt);
  1376. }
  1377. namespace {
  1378. const pass_data pass_data_branch_target_load_optimize2 =
  1379. {
  1380. RTL_PASS, /* type */
  1381. "btl2", /* name */
  1382. OPTGROUP_NONE, /* optinfo_flags */
  1383. TV_NONE, /* tv_id */
  1384. 0, /* properties_required */
  1385. 0, /* properties_provided */
  1386. 0, /* properties_destroyed */
  1387. 0, /* todo_flags_start */
  1388. 0, /* todo_flags_finish */
  1389. };
  1390. class pass_branch_target_load_optimize2 : public rtl_opt_pass
  1391. {
  1392. public:
  1393. pass_branch_target_load_optimize2 (gcc::context *ctxt)
  1394. : rtl_opt_pass (pass_data_branch_target_load_optimize2, ctxt)
  1395. {}
  1396. /* opt_pass methods: */
  1397. virtual bool gate (function *)
  1398. {
  1399. return (optimize > 0 && flag_branch_target_load_optimize2);
  1400. }
  1401. virtual unsigned int execute (function *);
  1402. }; // class pass_branch_target_load_optimize2
  1403. unsigned int
  1404. pass_branch_target_load_optimize2::execute (function *)
  1405. {
  1406. static int warned = 0;
  1407. /* Leave this a warning for now so that it is possible to experiment
  1408. with running this pass twice. In 3.6, we should either make this
  1409. an error, or use separate dump files. */
  1410. if (flag_branch_target_load_optimize
  1411. && flag_branch_target_load_optimize2
  1412. && !warned)
  1413. {
  1414. warning (0, "branch target register load optimization is not intended "
  1415. "to be run twice");
  1416. warned = 1;
  1417. }
  1418. branch_target_load_optimize (epilogue_completed);
  1419. return 0;
  1420. }
  1421. } // anon namespace
  1422. rtl_opt_pass *
  1423. make_pass_branch_target_load_optimize2 (gcc::context *ctxt)
  1424. {
  1425. return new pass_branch_target_load_optimize2 (ctxt);
  1426. }