reload1.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. /* Reload pseudo regs into hard regs for insns that require hard regs.
  2. Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. #include "config.h"
  18. #include "rtl.h"
  19. #include "flags.h"
  20. #include "regs.h"
  21. #include "hard-reg-set.h"
  22. #include "reload.h"
  23. #include "insn-config.h"
  24. #include "basic-block.h"
  25. #include <stdio.h>
  26. #define min(A,B) ((A) < (B) ? (A) : (B))
  27. /* This file contains the reload pass of the compiler, which is
  28. run after register allocation has been done. It checks that
  29. each insn is valid (operands required to be in registers really
  30. are in registers of the proper class) and fixes up invalid ones
  31. by copying values temporarily into registers for the insns
  32. that need them.
  33. The results of register allocation are described by the vector
  34. reg_renumber; the insns still contain pseudo regs, but reg_renumber
  35. can be used to find which hard reg, if any, a pseudo reg is in.
  36. The technique we always use is to free up a few hard regs that are
  37. called ``reload regs'', and for each place where a pseudo reg
  38. must be in a hard reg, copy it temporarily into one of the reload regs.
  39. All the pseudos that were formerly allocated to the hard regs that
  40. are now in use as reload regs must be ``spilled''. This means
  41. that they go to other hard regs, or to stack slots if no other
  42. available hard regs can be found. Spilling can invalidate more
  43. insns, requiring additional need for reloads, so we must keep checking
  44. until the process stabilizes.
  45. For machines with different classes of registers, we must keep track
  46. of the register class needed for each reload, and make sure that
  47. we allocate enough reload registers of each class.
  48. The file reload.c contains the code that checks one insn for
  49. validity and reports the reloads that it needs. This file
  50. is in charge of scanning the entire rtl code, accumulating the
  51. reload needs, spilling, assigning reload registers to use for
  52. fixing up each insn, and generating the new insns to copy values
  53. into the reload registers. */
  54. /* During reload_as_needed, element N contains a REG rtx for the hard reg
  55. into which pseudo reg N has been reloaded (perhaps for a previous insn). */
  56. static rtx *reg_last_reload_reg;
  57. /* Element N is the constant value to which pseudo reg N is equivalent,
  58. or zero if pseudo reg N is not equivalent to a constant.
  59. find_reloads looks at this in order to replace pseudo reg N
  60. with the constant it stands for. */
  61. rtx *reg_equiv_constant;
  62. /* Element N is the address of stack slot to which pseudo reg N is equivalent.
  63. This is used when the address is not valid as a memory address
  64. (because its displacement is too big for the machine.) */
  65. rtx *reg_equiv_address;
  66. /* Element N is the memory slot to which pseudo reg N is equivalent,
  67. or zero if pseudo reg N is not equivalent to a memory slot. */
  68. static rtx *reg_equiv_mem;
  69. /* Element N is the insn that initialized reg N from its equivalent
  70. constant or memory slot. */
  71. static rtx *reg_equiv_init;
  72. /* During reload_as_needed, element N contains the last pseudo regno
  73. reloaded into the Nth reload register. This vector is in parallel
  74. with spill_regs. */
  75. static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
  76. /* In parallel with spill_regs, contains REG rtx's for those regs.
  77. Holds the last rtx used for any given reg, or 0 if it has never
  78. been used for spilling yet. This rtx is reused, provided it has
  79. the proper mode. */
  80. static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
  81. /* In parallel with spill_regs, contains nonzero for a spill reg
  82. that was stored after the last time it was used.
  83. The precise value is the insn generated to do the store. */
  84. static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
  85. /* This table is the inverse mapping of spill_regs:
  86. indexed by hard reg number,
  87. it contains the position of that reg in spill_regs,
  88. or -1 for something that is not in spill_regs. */
  89. static short spill_reg_order[FIRST_PSEUDO_REGISTER];
  90. /* This table contains 1 for a register that may not be used
  91. for retrying global allocation, or -1 for a register that may be used.
  92. The registers that may not be used include all spill registers
  93. and the frame pointer (if we are using one). */
  94. static short forbidden_regs[FIRST_PSEUDO_REGISTER];
  95. /* Describes order of use of registers for reloading
  96. of spilled pseudo-registers. `spills' is the number of
  97. elements that are actually valid; new ones are added at the end. */
  98. static char spill_regs[FIRST_PSEUDO_REGISTER];
  99. /* Describes order of preference for putting regs into spill_regs.
  100. Contains the numbers of all the hard regs, in order most preferred first.
  101. This order is different for each function.
  102. It is set up by order_regs_for_reload.
  103. Empty elements at the end contain -1. */
  104. static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
  105. /* 1 for a hard register that appears explicitly in the rtl
  106. (for example, function value registers, special registers
  107. used by insns, structure value pointer registers). */
  108. static char regs_explicitly_used[FIRST_PSEUDO_REGISTER];
  109. /* Nonzero if spilling (REG n) does not require reloading it into
  110. a register in order to do (MEM (REG n)). */
  111. static char spill_indirect_ok;
  112. /* Indexed by basic block number, nonzero if there is any need
  113. for a spill register in that basic block.
  114. The pointer is 0 if we did stupid allocation and don't know
  115. the structure of basic blocks. */
  116. char *basic_block_needs;
  117. static void reload_as_needed ();
  118. static rtx alter_frame_pointer_addresses ();
  119. static void alter_reg ();
  120. void mark_home_live ();
  121. static int spill_hard_reg ();
  122. static void choose_reload_targets ();
  123. static void forget_old_reloads ();
  124. static void order_regs_for_reload ();
  125. static void eliminate_frame_pointer ();
  126. extern rtx adj_offsetable_operand ();
  127. /* Main entry point for the reload pass, and only entry point
  128. in this file.
  129. FIRST is the first insn of the function being compiled.
  130. GLOBAL nonzero means we were called from global_alloc
  131. and should attempt to reallocate any pseudoregs that we
  132. displace from hard regs we will use for reloads.
  133. If GLOBAL is zero, we do not have enough information to do that,
  134. so any pseudo reg that is spilled must go to the stack.
  135. DUMPFILE is the global-reg debugging dump file stream, or 0.
  136. If it is nonzero, messages are written to it to describe
  137. which registers are seized as reload regs, which pseudo regs
  138. are spilled from them, and where the pseudo regs are reallocated to. */
  139. void
  140. reload (first, global, dumpfile)
  141. rtx first;
  142. int global;
  143. FILE *dumpfile;
  144. {
  145. register int n_spills;
  146. register int class;
  147. register int i;
  148. register rtx insn;
  149. int something_changed;
  150. int something_needs_reloads;
  151. int new_basic_block_needs;
  152. /* The basic block number currently being processed for INSN. */
  153. int this_block;
  154. basic_block_needs = 0;
  155. /* Remember which hard regs appear explicitly
  156. before we merge into `regs_ever_live' the ones in which
  157. pseudo regs have been allocated. */
  158. bcopy (regs_ever_live, regs_explicitly_used, sizeof regs_ever_live);
  159. /* Compute which hard registers are now in use
  160. as homes for pseudo registers.
  161. This is done here rather than (eg) in global_alloc
  162. because this point is reached even if not optimizing. */
  163. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  164. mark_home_live (i);
  165. /* Find all the pseudo registers that didn't get hard regs
  166. but do have known equivalent constants or memory slots.
  167. These include parameters (known equivalent to parameter slots)
  168. and cse'd or loop-moved constant memory addresses.
  169. Record constant equivalents in reg_equiv_constant
  170. so they will be substituted by find_reloads.
  171. Record memory equivalents in reg_mem_equiv so they can
  172. be substituted eventually by altering the REG-rtx's. */
  173. reg_equiv_constant = (rtx *) alloca (max_regno * sizeof (rtx));
  174. bzero (reg_equiv_constant, max_regno * sizeof (rtx));
  175. reg_equiv_mem = (rtx *) alloca (max_regno * sizeof (rtx));
  176. bzero (reg_equiv_mem, max_regno * sizeof (rtx));
  177. reg_equiv_init = (rtx *) alloca (max_regno * sizeof (rtx));
  178. bzero (reg_equiv_init, max_regno * sizeof (rtx));
  179. reg_equiv_address = (rtx *) alloca (max_regno * sizeof (rtx));
  180. bzero (reg_equiv_address, max_regno * sizeof (rtx));
  181. for (insn = first; insn; insn = NEXT_INSN (insn))
  182. if (GET_CODE (insn) == INSN
  183. && GET_CODE (PATTERN (insn)) == SET
  184. && GET_CODE (SET_DEST (PATTERN (insn))) == REG)
  185. {
  186. rtx note = find_reg_note (insn, REG_EQUIV, 0);
  187. if (note)
  188. {
  189. rtx x = XEXP (note, 0);
  190. i = REGNO (SET_DEST (PATTERN (insn)));
  191. if (GET_CODE (x) == MEM)
  192. reg_equiv_mem[i] = x;
  193. else if (immediate_operand (x))
  194. reg_equiv_constant[i] = x;
  195. else
  196. continue;
  197. reg_equiv_init[i] = insn;
  198. }
  199. }
  200. /* Does this function require a frame pointer? */
  201. frame_pointer_needed
  202. |= (! global || FRAME_POINTER_REQUIRED);
  203. if (! frame_pointer_needed)
  204. frame_pointer_needed
  205. = check_frame_pointer_required (reg_equiv_constant, reg_equiv_mem);
  206. /* Alter each pseudo-reg rtx to contain its hard reg number.
  207. Delete initializations of pseudos that don't have hard regs
  208. and do have equivalents.
  209. Assign stack slots to the pseudos that lack hard regs or equivalents. */
  210. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  211. alter_reg (i);
  212. #ifndef REGISTER_CONSTRAINTS
  213. /* If all the pseudo regs have hard regs,
  214. except for those that are never referenced,
  215. we know that no reloads are needed. */
  216. /* But that is not true if there are register constraints, since
  217. in that case some pseudos might be in the wrong kind of hard reg. */
  218. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  219. if (reg_renumber[i] == -1 && reg_n_refs[i] != 0)
  220. break;
  221. if (i == max_regno && frame_pointer_needed)
  222. return;
  223. #endif
  224. /* Compute the order of preference for hard registers to spill.
  225. Store them by decreasing preference in potential_reload_regs. */
  226. order_regs_for_reload ();
  227. /* So far, no hard regs have been spilled. */
  228. n_spills = 0;
  229. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  230. {
  231. spill_reg_order[i] = -1;
  232. forbidden_regs[i] = -1;
  233. }
  234. if (frame_pointer_needed)
  235. {
  236. forbidden_regs[FRAME_POINTER_REGNUM] = 1;
  237. spill_hard_reg (FRAME_POINTER_REGNUM, global, dumpfile);
  238. }
  239. if (global)
  240. {
  241. basic_block_needs = (char *)alloca (n_basic_blocks);
  242. bzero (basic_block_needs, n_basic_blocks);
  243. }
  244. /* This loop scans the entire function each go-round
  245. and repeats until one repetition spills no additional hard regs. */
  246. /* This flag is set when a psuedo reg is spilled,
  247. to require another pass. Note that getting an additional reload
  248. reg does not necessarily imply any pseudo reg was spilled;
  249. sometimes we find a reload reg that no pseudo reg was allocated in. */
  250. something_changed = 1;
  251. /* This flag is set if there are any insns that require reloading. */
  252. something_needs_reloads = 0;
  253. while (something_changed)
  254. {
  255. /* For each class, number of reload regs needed in that class.
  256. This is the maximum over all insns of the needs in that class
  257. of the individual insn. */
  258. int max_needs[N_REG_CLASSES];
  259. /* For each class, size of group of consecutive regs
  260. that is needed for the reloads of this class. */
  261. int group_size[N_REG_CLASSES];
  262. /* For each class, max number of consecutive groups needed.
  263. (Each group contains max_needs_size[CLASS] consecutive registers.) */
  264. int max_groups[N_REG_CLASSES];
  265. /* For each class, max number needed of regs that don't belong
  266. to any of the groups. */
  267. int max_nongroups[N_REG_CLASSES];
  268. /* For each class, the machine mode which requires consecutive
  269. groups of regs of that class.
  270. If two different modes ever require groups of one class,
  271. we crash, not having the hair to deal with that case. */
  272. enum machine_mode group_mode[N_REG_CLASSES];
  273. /* For each register, 1 if it was counted against the need for
  274. groups. 0 means it can count against max_nongroup instead. */
  275. char counted_for_groups[FIRST_PSEUDO_REGISTER];
  276. something_changed = 0;
  277. bzero (max_needs, sizeof max_needs);
  278. bzero (max_groups, sizeof max_groups);
  279. bzero (max_nongroups, sizeof max_nongroups);
  280. bzero (group_size, sizeof group_size);
  281. for (i = 0; i < N_REG_CLASSES; i++)
  282. group_mode[i] = VOIDmode;
  283. /* Keep track of which basic blocks are needing the reloads. */
  284. this_block = 0;
  285. /* Remember whether any element of basic_block_needs
  286. changes from 0 to 1 in this pass. */
  287. new_basic_block_needs = 0;
  288. /* Compute the most additional registers needed by any instruction.
  289. Collect information separately for each class of regs. */
  290. for (insn = first; insn; insn = NEXT_INSN (insn))
  291. {
  292. if (global && insn == basic_block_head[this_block+1])
  293. ++this_block;
  294. if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
  295. || GET_CODE (insn) == CALL_INSN)
  296. {
  297. int insn_needs[N_REG_CLASSES];
  298. int insn_groups[N_REG_CLASSES];
  299. int insn_total_groups = 0;
  300. for (i = 0; i < N_REG_CLASSES; i++)
  301. insn_needs[i] = 0, insn_groups[i] = 0;
  302. #if 0
  303. /* Optimization: a bit-field instruction whose field
  304. happens to be a byte or halfword in memory
  305. can be changed to a move instruction. */
  306. if (GET_CODE (PATTERN (insn)) == SET)
  307. {
  308. rtx dest = SET_DEST (PATTERN (insn));
  309. rtx src = SET_SRC (PATTERN (insn));
  310. if (GET_CODE (dest) == ZERO_EXTRACT
  311. || GET_CODE (dest) == SIGN_EXTRACT)
  312. optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
  313. if (GET_CODE (src) == ZERO_EXTRACT
  314. || GET_CODE (src) == SIGN_EXTRACT)
  315. optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
  316. }
  317. #endif
  318. /* Analyze the instruction. */
  319. find_reloads (insn, 0, spill_indirect_ok, global, spill_reg_order);
  320. if (n_reloads == 0)
  321. continue;
  322. something_needs_reloads = 1;
  323. /* Count each reload once in every class
  324. containing the reload's own class. */
  325. for (i = 0; i < n_reloads; i++)
  326. {
  327. register enum reg_class *p;
  328. int size;
  329. enum machine_mode mode;
  330. /* Don't count the dummy reloads, for which one of the
  331. regs mentioned in the insn can be used for reloading.
  332. Don't count optional reloads.
  333. Don't count reloads that got combined with others. */
  334. if (reload_reg_rtx[i] != 0
  335. || reload_optional[i] != 0
  336. || (reload_out[i] == 0 && reload_in[i] == 0))
  337. continue;
  338. mode = reload_inmode[i];
  339. if (GET_MODE_SIZE (reload_outmode[i]) > GET_MODE_SIZE (mode))
  340. mode = reload_outmode[i];
  341. size = CLASS_MAX_NREGS (reload_reg_class[i], mode);
  342. if (size > 1)
  343. {
  344. /* Count number of groups needed separately from
  345. number of individual regs needed. */
  346. insn_groups[(int) reload_reg_class[i]] += size;
  347. p = reg_class_superclasses[(int) reload_reg_class[i]];
  348. while (*p != LIM_REG_CLASSES)
  349. insn_groups[(int) *p++] += size;
  350. insn_total_groups++;
  351. /* Record size of a group. */
  352. group_size[(int) reload_reg_class[i]] = size;
  353. /* If a group of consecutive regs are needed,
  354. record which machine mode needs them.
  355. Crash if two different machine modes both need
  356. groups of consecutive regs of the same class. */
  357. if ((group_mode[(int) reload_reg_class[i]] != VOIDmode)
  358. &&
  359. (group_mode[(int) reload_reg_class[i]] != mode))
  360. abort ();
  361. group_mode[(int) reload_reg_class[i]] = mode;
  362. }
  363. else if (size == 1)
  364. {
  365. insn_needs[(int) reload_reg_class[i]] += 1;
  366. p = reg_class_superclasses[(int) reload_reg_class[i]];
  367. while (*p != LIM_REG_CLASSES)
  368. insn_needs[(int) *p++] += 1;
  369. }
  370. else
  371. abort ();
  372. if (global)
  373. {
  374. if (! basic_block_needs[this_block])
  375. new_basic_block_needs = 1;
  376. basic_block_needs[this_block] = 1;
  377. }
  378. }
  379. /* Remember for later shortcuts which insns had any reloads. */
  380. PUT_MODE (insn, n_reloads ? QImode : VOIDmode);
  381. /* For each class, collect maximum need of any insn */
  382. for (i = 0; i < N_REG_CLASSES; i++)
  383. {
  384. if (max_needs[i] < insn_needs[i])
  385. max_needs[i] = insn_needs[i];
  386. if (max_groups[i] < insn_groups[i])
  387. max_groups[i] = insn_groups[i];
  388. if (insn_total_groups > 0)
  389. if (max_nongroups[i] < insn_needs[i])
  390. max_nongroups[i] = insn_needs[i];
  391. }
  392. }
  393. /* Note that there is a continue statement above. */
  394. }
  395. /* Now deduct from the needs for the registers already
  396. available (already spilled). */
  397. bzero (counted_for_groups, sizeof counted_for_groups);
  398. /* Find all consecutive groups of spilled registers
  399. and mark each group off against the need for such groups. */
  400. for (i = 0; i < N_REG_CLASSES; i++)
  401. if (group_size[i] > 1)
  402. {
  403. char regmask[FIRST_PSEUDO_REGISTER];
  404. int j;
  405. bzero (regmask, sizeof regmask);
  406. /* Make a mask of all the regs that are spill regs in class I. */
  407. for (j = 0; j < n_spills; j++)
  408. if (TEST_HARD_REG_BIT (reg_class_contents[i], spill_regs[j])
  409. && !counted_for_groups[spill_regs[i]])
  410. regmask[spill_regs[j]] = 1;
  411. /* Find each consecutive group of them. */
  412. for (j = 0; j < FIRST_PSEUDO_REGISTER && max_groups[i] > 0; j++)
  413. if (regmask[j] && j + group_size[i] <= FIRST_PSEUDO_REGISTER)
  414. {
  415. int k;
  416. for (k = 1; k < group_size[i]; k++)
  417. if (! regmask[j + k])
  418. break;
  419. if (k == group_size[i])
  420. {
  421. /* We found a group. Mark it off against this class's
  422. need for groups, and against each superclass too. */
  423. register enum reg_class *p;
  424. max_groups[i]--;
  425. p = reg_class_superclasses[i];
  426. while (*p != LIM_REG_CLASSES)
  427. max_groups[(int) *p++]--;
  428. /* Don't count these registers again. */
  429. counted_for_groups[j] = 1;
  430. for (k = 1; k < group_size[i]; k++)
  431. counted_for_groups[j + k] = 1;
  432. }
  433. j += k;
  434. }
  435. }
  436. /* Now count all remaining spill regs against the individual need.
  437. Those that weren't counted_for_groups in groups can also count against
  438. the not-in-group need. */
  439. for (i = 0; i < n_spills; i++)
  440. {
  441. register enum reg_class *p;
  442. class = (int) REGNO_REG_CLASS (spill_regs[i]);
  443. max_needs[class]--;
  444. p = reg_class_superclasses[class];
  445. while (*p != LIM_REG_CLASSES)
  446. max_needs[(int) *p++]--;
  447. if (! counted_for_groups[spill_regs[i]])
  448. {
  449. max_nongroups[class]--;
  450. p = reg_class_superclasses[class];
  451. while (*p != LIM_REG_CLASSES)
  452. max_nongroups[(int) *p++]--;
  453. }
  454. }
  455. /* If all needs are met, we win. */
  456. for (i = 0; i < N_REG_CLASSES; i++)
  457. if (max_needs[i] > 0 || max_groups[i] > 0 || max_nongroups[i] > 0)
  458. break;
  459. if (i == N_REG_CLASSES && !new_basic_block_needs)
  460. break;
  461. /* Not all needs are met; must spill more hard regs. */
  462. /* If any element of basic_block_needs changed from 0 to 1,
  463. re-spill all the regs already spilled. This may spill
  464. additional pseudos that didn't spill before. */
  465. if (new_basic_block_needs)
  466. for (i = 0; i < n_spills; i++)
  467. something_changed
  468. |= spill_hard_reg (spill_regs[i], global, dumpfile);
  469. /* Now find more reload regs to satisfy the remaining need.
  470. Count them in `spills', and add entries to
  471. `spill_regs' and `spill_reg_order'. */
  472. for (class = 0; class < N_REG_CLASSES; class++)
  473. while (max_needs[class] > 0 || max_groups[class] > 0)
  474. {
  475. register enum reg_class *p;
  476. int in_a_group = 0;
  477. /* First, if we need more groups of consecutive regs, get them.
  478. Either get a spill register that completes a group
  479. or, if that cannot be done, get one that starts a group.
  480. Here we do not yet handle groups of size > 2. */
  481. if (max_groups[class] > 0)
  482. {
  483. if (group_size[class] > 2)
  484. abort ();
  485. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  486. {
  487. int j = potential_reload_regs[i];
  488. if (j >= 0
  489. &&
  490. ((j > 0 && spill_reg_order[j - 1] >= 0
  491. && TEST_HARD_REG_BIT (reg_class_contents[class], j)
  492. && TEST_HARD_REG_BIT (reg_class_contents[class], j - 1)
  493. && HARD_REGNO_MODE_OK (j - 1, group_mode[class]))
  494. ||
  495. (j < FIRST_PSEUDO_REGISTER - 1
  496. && spill_reg_order[j + 1] >= 0
  497. && TEST_HARD_REG_BIT (reg_class_contents[class], j)
  498. && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
  499. && HARD_REGNO_MODE_OK (j, group_mode[class]))))
  500. {
  501. /* We have found one that will complete a group,
  502. so count off one group as provided. */
  503. max_groups[class]--;
  504. in_a_group = 1;
  505. break;
  506. }
  507. }
  508. /* We can't complete any group, so start one. */
  509. if (i == FIRST_PSEUDO_REGISTER)
  510. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  511. {
  512. int j = potential_reload_regs[i];
  513. if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
  514. && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
  515. && TEST_HARD_REG_BIT (reg_class_contents[class], j)
  516. && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
  517. && HARD_REGNO_MODE_OK (j, group_mode[class]))
  518. {
  519. in_a_group = 1;
  520. break;
  521. }
  522. }
  523. }
  524. else
  525. {
  526. /* We want a solitary register. */
  527. /* Consider the potential reload regs that aren't
  528. yet in use as reload regs, in order of preference.
  529. Find the most preferred one that's in this class. */
  530. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  531. if (potential_reload_regs[i] >= 0
  532. && TEST_HARD_REG_BIT (reg_class_contents[class],
  533. potential_reload_regs[i]))
  534. break;
  535. }
  536. /* I should be the index in potential_reload_regs
  537. of the new reload reg we have found. */
  538. if (i == FIRST_PSEUDO_REGISTER)
  539. abort (); /* No reload reg possible? */
  540. /* Make potential_reload_regs[i] an additional reload reg. */
  541. spill_regs[n_spills] = potential_reload_regs[i];
  542. spill_reg_order[potential_reload_regs[i]] = n_spills;
  543. forbidden_regs[potential_reload_regs[i]] = 1;
  544. potential_reload_regs[i] = -1;
  545. if (dumpfile)
  546. fprintf (dumpfile, "Spilling reg %d.\n", spill_regs[n_spills]);
  547. /* Clear off the needs we just satisfied. */
  548. max_needs[class]--;
  549. p = reg_class_superclasses[class];
  550. while (*p != LIM_REG_CLASSES)
  551. max_needs[(int) *p++]--;
  552. if (! in_a_group)
  553. {
  554. max_nongroups[class]--;
  555. p = reg_class_superclasses[class];
  556. while (*p != LIM_REG_CLASSES)
  557. max_nongroups[(int) *p++]--;
  558. }
  559. /* Spill every pseudo reg that was allocated to this reg
  560. or to something that overlaps this reg. */
  561. something_changed |= spill_hard_reg (spill_regs[n_spills],
  562. global, dumpfile);
  563. regs_ever_live[spill_regs[n_spills]] = 1;
  564. n_spills++;
  565. }
  566. }
  567. /* Use the reload registers where necessary
  568. by generating move instructions to move the must-be-register
  569. values into or out of the reload registers. */
  570. if (something_needs_reloads)
  571. reload_as_needed (first, n_spills, global);
  572. /* Now eliminate all pseudo regs by modifying them into
  573. their equivalent memory references.
  574. The REG-rtx's for the pseudos are modified in place,
  575. so all insns that used to refer to them now refer to memory.
  576. For a reg that has a reg_equiv_address, all those insns
  577. were changed by reloading so that no insns refer to it any longer;
  578. but the DECL_RTL of a variable decl may refer to it,
  579. and if so this causes the debugging info to mention the variable. */
  580. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  581. {
  582. rtx addr = 0;
  583. if (reg_equiv_mem[i])
  584. addr = XEXP (reg_equiv_mem[i], 0);
  585. if (reg_equiv_address[i])
  586. addr = reg_equiv_address[i];
  587. if (addr)
  588. {
  589. if (reg_renumber[i] < 0)
  590. {
  591. rtx reg = regno_reg_rtx[i];
  592. XEXP (reg, 0) = addr;
  593. PUT_CODE (reg, MEM);
  594. }
  595. else if (reg_equiv_mem[i])
  596. {
  597. if (! frame_pointer_needed)
  598. FIX_FRAME_POINTER_ADDRESS (addr, 0);
  599. XEXP (reg_equiv_mem[i], 0) = addr;
  600. }
  601. }
  602. }
  603. /* Sometimes the sole reference to a parameter has been combined into
  604. another instruction, eliminating the parameter's register.
  605. Find these memory references and fix their addresses. */
  606. if (! frame_pointer_needed)
  607. eliminate_frame_pointer (first);
  608. }
  609. /* Scan all insns, computing the stack depth, and convert all
  610. frame-pointer-relative references to stack-pointer-relative references. */
  611. static void
  612. eliminate_frame_pointer (first)
  613. rtx first;
  614. {
  615. int depth = 0;
  616. int max_uid = get_max_uid ();
  617. int *label_depth = (int *) alloca ((max_uid + 1) * sizeof (int));
  618. int i;
  619. rtx insn;
  620. for (i = 0; i <= max_uid; i++)
  621. label_depth[i] = -1;
  622. /* In this loop, for each forward branch we record the stack
  623. depth of the label it jumps to. We take advantage of the fact
  624. that the stack depth at a label reached by a backward branch
  625. is always, in GCC output, equal to the stack depth of the preceding
  626. unconditional jump, because it was either a loop statement or
  627. statement label. */
  628. for (insn = first; insn; insn = NEXT_INSN (insn))
  629. {
  630. rtx pattern = PATTERN (insn);
  631. switch (GET_CODE (insn))
  632. {
  633. case INSN:
  634. alter_frame_pointer_addresses (pattern, depth);
  635. #ifdef PUSH_ROUNDING
  636. /* Notice pushes and pops; update DEPTH. */
  637. if (GET_CODE (pattern) == SET)
  638. {
  639. if (push_operand (SET_DEST (pattern),
  640. GET_MODE (SET_DEST (pattern))))
  641. depth += PUSH_ROUNDING (GET_MODE_SIZE (GET_MODE (SET_DEST (pattern))));
  642. if (GET_CODE (SET_DEST (pattern)) == REG
  643. && REGNO (SET_DEST (pattern)) == STACK_POINTER_REGNUM)
  644. {
  645. int delta;
  646. if (GET_CODE (SET_SRC (pattern)) == PLUS
  647. && GET_CODE (XEXP (SET_SRC (pattern), 0)) == REG
  648. && REGNO (XEXP (SET_SRC (pattern), 0)) == STACK_POINTER_REGNUM)
  649. delta = INTVAL (XEXP (SET_SRC (pattern), 1));
  650. else if (GET_CODE (SET_SRC (pattern)) == MINUS
  651. && GET_CODE (XEXP (SET_SRC (pattern), 0)) == REG
  652. && REGNO (XEXP (SET_SRC (pattern), 0)) == STACK_POINTER_REGNUM)
  653. delta = -INTVAL (XEXP (SET_SRC (pattern), 1));
  654. else abort ();
  655. #ifdef STACK_GROWS_DOWNWARD
  656. depth -= delta;
  657. #else
  658. depth += delta;
  659. #endif
  660. }
  661. }
  662. #endif
  663. break;
  664. case JUMP_INSN:
  665. alter_frame_pointer_addresses (pattern, depth);
  666. if (GET_CODE (pattern) == ADDR_VEC)
  667. for (i = 0; i < XVECLEN (pattern, 0); i++)
  668. label_depth[INSN_UID (XEXP (XVECEXP (pattern, 0, i), 0))] = depth;
  669. else if (GET_CODE (pattern) == ADDR_DIFF_VEC)
  670. {
  671. label_depth[INSN_UID (XEXP (XEXP (pattern, 0), 0))] = depth;
  672. for (i = 0; i < XVECLEN (pattern, 1); i++)
  673. label_depth[INSN_UID (XEXP (XVECEXP (pattern, 1, i), 0))] = depth;
  674. }
  675. else if (JUMP_LABEL (insn))
  676. label_depth[INSN_UID (JUMP_LABEL (insn))] = depth;
  677. else
  678. break;
  679. case CODE_LABEL:
  680. if (label_depth [INSN_UID (insn)] >= 0)
  681. depth = label_depth [INSN_UID (insn)];
  682. break;
  683. case CALL_INSN:
  684. alter_frame_pointer_addresses (pattern, depth);
  685. break;
  686. }
  687. }
  688. }
  689. /* Walk the rtx X, converting all frame-pointer refs to stack-pointer refs
  690. on the assumption that the current temporary stack depth is DEPTH.
  691. (The size of saved registers must be added to DEPTH
  692. to get the actual offset between the logical frame-pointer and the
  693. stack pointer. FIX_FRAME_POINTER_ADDRESS takes care of that.) */
  694. static rtx
  695. alter_frame_pointer_addresses (x, depth)
  696. register rtx x;
  697. int depth;
  698. {
  699. register int i;
  700. register char *fmt;
  701. register enum rtx_code code = GET_CODE (x);
  702. switch (code)
  703. {
  704. case REG:
  705. case CONST_INT:
  706. case CONST:
  707. case SYMBOL_REF:
  708. case LABEL_REF:
  709. case CONST_DOUBLE:
  710. case CC0:
  711. case PC:
  712. return x;
  713. case MEM:
  714. {
  715. rtx addr = XEXP (x, 0);
  716. FIX_FRAME_POINTER_ADDRESS (addr, depth);
  717. XEXP (x, 0) = addr;
  718. }
  719. break;
  720. case PLUS:
  721. /* Handle addresses being loaded or pushed, etc.,
  722. rather than referenced. */
  723. FIX_FRAME_POINTER_ADDRESS (x, depth);
  724. break;
  725. }
  726. fmt = GET_RTX_FORMAT (code);
  727. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  728. {
  729. if (fmt[i] == 'e')
  730. XEXP (x, i) = alter_frame_pointer_addresses (XEXP (x, i), depth);
  731. else if (fmt[i] == 'E')
  732. {
  733. register int j;
  734. for (j = XVECLEN (x, i) - 1; j >=0; j--)
  735. XVECEXP (x, i, j)
  736. = alter_frame_pointer_addresses (XVECEXP (x, i, j), depth);
  737. }
  738. }
  739. return x;
  740. }
  741. static void
  742. alter_reg (i)
  743. register int i;
  744. {
  745. /* If the reg got changed to a MEM at rtl-generation time,
  746. ignore it. */
  747. if (GET_CODE (regno_reg_rtx[i]) != REG)
  748. return;
  749. /* Modify the reg-rtx to contain the new hard reg
  750. number or else to contain its pseudo reg number. */
  751. REGNO (regno_reg_rtx[i])
  752. = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
  753. if (reg_renumber[i] < 0 && reg_equiv_init[i])
  754. {
  755. /* Delete the insn that loads the pseudo register. */
  756. PUT_CODE (reg_equiv_init[i], NOTE);
  757. NOTE_LINE_NUMBER (reg_equiv_init[i])
  758. = NOTE_INSN_DELETED;
  759. NOTE_SOURCE_FILE (reg_equiv_init[i]) = 0;
  760. }
  761. /* If we have a pseudo that is needed but has no hard reg or equivalent,
  762. allocate a stack slot for it. */
  763. if (reg_renumber[i] < 0
  764. && reg_n_refs[i] > 0
  765. && reg_equiv_constant[i] == 0
  766. && reg_equiv_mem[i] == 0)
  767. {
  768. register rtx x = assign_stack_local (GET_MODE (regno_reg_rtx[i]),
  769. PSEUDO_REGNO_BYTES (i));
  770. register rtx addr = XEXP (x, 0);
  771. /* If the stack slot is directly addressable, substitute
  772. the MEM we just got directly for the old REG.
  773. Otherwise, record the address; we will generate hairy code
  774. to compute the address in a register each time it is needed. */
  775. if (memory_address_p (GET_MODE (regno_reg_rtx[i]), addr))
  776. reg_equiv_mem[i] = x;
  777. else
  778. reg_equiv_address[i] = XEXP (x, 0);
  779. }
  780. }
  781. /* Mark the slots in regs_ever_live for the hard regs
  782. used by pseudo-reg number REGNO. */
  783. void
  784. mark_home_live (regno)
  785. int regno;
  786. {
  787. register int i, lim;
  788. i = reg_renumber[regno];
  789. if (i < 0)
  790. return;
  791. lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
  792. while (i < lim)
  793. regs_ever_live[i++] = 1;
  794. }
  795. /* Kick all pseudos out of hard register REGNO.
  796. If GLOBAL is nonzero, try to find someplace else to put them.
  797. If DUMPFILE is nonzero, log actions taken on that file.
  798. Return nonzero if any pseudos needed to be kicked out. */
  799. static int
  800. spill_hard_reg (regno, global, dumpfile)
  801. register int regno;
  802. int global;
  803. FILE *dumpfile;
  804. {
  805. int something_changed = 0;
  806. register int i;
  807. /* Spill every pseudo reg that was allocated to this reg
  808. or to something that overlaps this reg. */
  809. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  810. if (reg_renumber[i] >= 0
  811. && reg_renumber[i] <= regno
  812. && (reg_renumber[i]
  813. + HARD_REGNO_NREGS (reg_renumber[i],
  814. PSEUDO_REGNO_MODE (i))
  815. > regno))
  816. {
  817. #if 1
  818. /* If this register belongs solely to a basic block
  819. which needed no spilling, leave it be. */
  820. if (basic_block_needs
  821. && reg_basic_block[i] >= 0
  822. && basic_block_needs[reg_basic_block[i]] == 0)
  823. continue;
  824. #endif
  825. /* Mark it as no longer having a hard register home. */
  826. reg_renumber[i] = -1;
  827. /* We will need to scan everything again. */
  828. something_changed = 1;
  829. if (global)
  830. {
  831. retry_global_alloc (i, forbidden_regs);
  832. /* Update regs_ever_live for new home (if any). */
  833. mark_home_live (i);
  834. /* If something gets spilled to the stack,
  835. we must have a frame pointer, so spill the frame pointer. */
  836. if (reg_renumber[i] == -1 && ! frame_pointer_needed)
  837. {
  838. frame_pointer_needed = 1;
  839. forbidden_regs[FRAME_POINTER_REGNUM] = 1;
  840. spill_hard_reg (FRAME_POINTER_REGNUM, global, dumpfile);
  841. }
  842. }
  843. alter_reg (i);
  844. if (dumpfile)
  845. {
  846. if (reg_renumber[i] == -1)
  847. fprintf (dumpfile, " Register %d now on stack.\n\n", i);
  848. else
  849. fprintf (dumpfile, " Register %d now in %d.\n\n",
  850. i, reg_renumber[i]);
  851. }
  852. }
  853. return something_changed;
  854. }
  855. struct hard_reg_n_uses { int regno; int uses; };
  856. static int
  857. hard_reg_use_compare (p1, p2)
  858. struct hard_reg_n_uses *p1, *p2;
  859. {
  860. return p1->uses - p2->uses;
  861. }
  862. /* Choose the order to consider regs for use as reload registers
  863. based on how much trouble would be caused by spilling one.
  864. Store them in order of decreasing preference in potential_reload_regs. */
  865. static void
  866. order_regs_for_reload ()
  867. {
  868. register int i;
  869. register int o = 0;
  870. int large = 0;
  871. struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
  872. /* Count number of uses of each hard reg by pseudo regs allocated to it
  873. and then order them by decreasing use. */
  874. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  875. {
  876. hard_reg_n_uses[i].uses = 0;
  877. hard_reg_n_uses[i].regno = i;
  878. }
  879. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  880. {
  881. if (reg_renumber[i] >= 0)
  882. hard_reg_n_uses[reg_renumber[i]].uses += reg_n_refs[i];
  883. large += reg_n_refs[i];
  884. }
  885. /* Now fixed registers (which cannot safely be used for reloading)
  886. get a very high use count so they will be considered least desirable.
  887. Likewise registers used explicitly in the rtl code. */
  888. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  889. if (fixed_regs[i] || regs_explicitly_used[i])
  890. hard_reg_n_uses[i].uses = large;
  891. hard_reg_n_uses[FRAME_POINTER_REGNUM].uses = large;
  892. qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
  893. sizeof hard_reg_n_uses[0], hard_reg_use_compare);
  894. /* Prefer registers not so far used, for use in temporary loading.
  895. Among them, prefer registers not preserved by calls. */
  896. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  897. if (regs_ever_live[i] == 0 && call_used_regs[i]
  898. && ! fixed_regs[i])
  899. potential_reload_regs[o++] = i;
  900. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  901. if (regs_ever_live[i] == 0 && ! call_used_regs[i]
  902. && i != FRAME_POINTER_REGNUM)
  903. potential_reload_regs[o++] = i;
  904. /* Now add the regs that are already used,
  905. preferring those used less often. */
  906. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  907. if (regs_ever_live[hard_reg_n_uses[i].regno] != 0)
  908. potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
  909. #if 0
  910. /* For regs that are used, don't prefer those not preserved by calls
  911. because those are likely to contain high priority things
  912. that are live for short periods of time. */
  913. for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
  914. if (regs_ever_live[i] != 0 && ! call_used_regs[i])
  915. potential_reload_regs[o++] = i;
  916. #endif
  917. }
  918. /* Reload pseudo-registers into hard regs around each insn as needed.
  919. Additional register load insns are output before the insn that needs it
  920. and perhaps store insns after insns that modify the reloaded pseudo reg.
  921. reg_last_reload_reg and reg_reloaded_contents keep track of
  922. which pseudo-registers are already available in reload registers.
  923. We update these for the reloads that we perform,
  924. as the insns are scanned. */
  925. static void
  926. reload_as_needed (first, n_spills, live_known)
  927. rtx first;
  928. int n_spills;
  929. int live_known;
  930. {
  931. register rtx insn;
  932. register int i;
  933. /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
  934. Set spill_indirect_ok if so. */
  935. register rtx tem
  936. = gen_rtx (MEM, SImode,
  937. gen_rtx (PLUS, Pmode,
  938. gen_rtx (REG, Pmode, FRAME_POINTER_REGNUM),
  939. gen_rtx (CONST_INT, VOIDmode, 4)));
  940. spill_indirect_ok = memory_address_p (QImode, tem);
  941. bzero (spill_reg_rtx, sizeof spill_reg_rtx);
  942. reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
  943. bzero (reg_last_reload_reg, max_regno * sizeof (rtx));
  944. for (i = 0; i < n_spills; i++)
  945. reg_reloaded_contents[i] = -1;
  946. for (insn = first; insn;)
  947. {
  948. register rtx next = NEXT_INSN (insn);
  949. if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
  950. || GET_CODE (insn) == CALL_INSN)
  951. {
  952. if (GET_MODE (insn) == VOIDmode)
  953. n_reloads = 0;
  954. /* First find the pseudo regs that must be reloaded for this insn.
  955. This info is returned in the tables reload_... (see reload.h).
  956. Also modify the body of INSN by substituting RELOAD
  957. rtx's for those pseudo regs. */
  958. else
  959. find_reloads (insn, 1, spill_indirect_ok, live_known, spill_reg_order);
  960. if (n_reloads > 0)
  961. {
  962. /* Now compute which reload regs to reload them into. Perhaps
  963. reusing reload regs from previous insns, or else output
  964. load insns to reload them. Maybe output store insns too.
  965. Record the choices of reload reg in reload_reg_rtx. */
  966. choose_reload_targets (insn, n_spills);
  967. /* Substitute the chosen reload regs from reload_reg_rtx
  968. into the insn's body (or perhaps into the bodies of other
  969. load and store insn that we just made for reloading
  970. and that we moved the structure into). */
  971. subst_reloads ();
  972. }
  973. /* Any previously reloaded spilled pseudo reg, stored in this insn,
  974. is no longer validly lying around to save a future reload.
  975. Note that this does not detect pseudos that were reloaded
  976. for this insn in order to be stored in
  977. (obeying register constraints). That is correct; such reload
  978. registers ARE still valid. */
  979. forget_old_reloads (PATTERN (insn));
  980. }
  981. /* A reload reg's contents are unknown after a label. */
  982. if (GET_CODE (insn) == CODE_LABEL)
  983. for (i = 0; i < n_spills; i++)
  984. reg_reloaded_contents[i] = -1;
  985. /* Don't assume a reload reg is still good after a call insn
  986. if it is a call-used reg. */
  987. if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == CALL_INSN)
  988. for (i = 0; i < n_spills; i++)
  989. if (call_used_regs[spill_regs[i]])
  990. reg_reloaded_contents[i] = -1;
  991. insn = next;
  992. }
  993. }
  994. /* If we see a pseudo-reg being stored into,
  995. don't try to reuse an old reload reg
  996. which previously contained a copy of it. */
  997. static void
  998. forget_old_reloads (x)
  999. rtx x;
  1000. {
  1001. if (GET_CODE (x) == SET && GET_CODE (SET_DEST (x)) == REG)
  1002. {
  1003. register int regno = REGNO (SET_DEST (x));
  1004. int nr;
  1005. if (regno >= FIRST_PSEUDO_REGISTER)
  1006. nr = 1;
  1007. else
  1008. nr = HARD_REGNO_NREGS (regno, GET_MODE (SET_DEST (x)));
  1009. while (nr-- > 0) reg_last_reload_reg[regno + nr] = 0;
  1010. }
  1011. else if (GET_CODE (x) == PARALLEL)
  1012. {
  1013. register int i;
  1014. for (i = 0; i < XVECLEN (x, 0); i++)
  1015. {
  1016. register rtx y = XVECEXP (x, 0, i);
  1017. if (GET_CODE (y) == SET && GET_CODE (SET_DEST (y)) == REG)
  1018. {
  1019. register int regno = REGNO (SET_DEST (y));
  1020. reg_last_reload_reg[regno] = 0;
  1021. }
  1022. }
  1023. }
  1024. }
  1025. /* Comparison function for qsort to decide which of two reloads
  1026. should be handled first. *P1 and *P2 are the reload numbers. */
  1027. static int
  1028. reload_reg_class_lower (p1, p2)
  1029. short *p1, *p2;
  1030. {
  1031. register int r1 = *p1, r2 = *p2;
  1032. register int t;
  1033. register enum machine_mode mode1, mode2;
  1034. /* Consider required reloads before optional ones. */
  1035. t = reload_optional[r1] - reload_optional[r2];
  1036. if (t != 0)
  1037. return t;
  1038. /* Consider reloads in order of increasing reg-class number. */
  1039. t = (int) reload_reg_class[r1] - (int) reload_reg_class[r2];
  1040. if (t != 0)
  1041. return t;
  1042. /* For a given reg-class number, consider multi-reg groups first. */
  1043. mode1 = (reload_inmode[r1] == VOIDmode ? reload_outmode[r1] : reload_inmode[r1]);
  1044. mode2 = (reload_inmode[r2] == VOIDmode ? reload_outmode[r2] : reload_inmode[r2]);
  1045. t = (CLASS_MAX_NREGS (reload_reg_class[r2], mode2)
  1046. - CLASS_MAX_NREGS (reload_reg_class[r1], mode1));
  1047. return t;
  1048. }
  1049. /* Assign hard reg targets for the pseudo-registers we must reload
  1050. into hard regs for this insn.
  1051. Also output the instructions to copy them in and out of the hard regs.
  1052. For machines with register classes, we are responsible for
  1053. finding a reload reg in the proper class. */
  1054. static void
  1055. choose_reload_targets (insn, n_spills)
  1056. rtx insn;
  1057. int n_spills;
  1058. {
  1059. register int j;
  1060. char reload_reg_in_use[FIRST_PSEUDO_REGISTER];
  1061. short reload_order[FIRST_PSEUDO_REGISTER];
  1062. char reload_inherited[FIRST_PSEUDO_REGISTER];
  1063. /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
  1064. for an output reload that stores into reg N. */
  1065. char *reg_has_output_reload;
  1066. int have_groups = 0;
  1067. /* For each reload, the index in spill_regs of the spill register used,
  1068. or -1 if we did not need one of the spill registers for this reload. */
  1069. int reload_spill_index[FIRST_PSEUDO_REGISTER];
  1070. bzero (reload_inherited, FIRST_PSEUDO_REGISTER);
  1071. bzero (reload_reg_in_use, FIRST_PSEUDO_REGISTER);
  1072. reg_has_output_reload = (char *) alloca (max_regno);
  1073. bzero (reg_has_output_reload, max_regno);
  1074. /* In order to be certain of getting the registers we need,
  1075. we must sort the reloads into order of increasing register class.
  1076. Then our grabbing of reload registers will parallel the process
  1077. that provided the reload registers. */
  1078. /* Also note whether any of the reloads wants a consecutive group of regs.
  1079. When that happens, we must when processing the non-group reloads
  1080. avoid (when possible) using a reload reg that would break up a group. */
  1081. /* This used to look for an existing reloaded home for all
  1082. of the reloads, and only then perform any new reloads.
  1083. But that could lose if the reloads were done out of reg-class order
  1084. because a later reload with a looser constraint might have an old
  1085. home in a register needed by an earlier reload with a tighter constraint.
  1086. It would be possible with even hairier code to detect such cases
  1087. and handle them, but it doesn't seem worth while yet. */
  1088. for (j = 0; j < n_reloads; j++)
  1089. {
  1090. enum machine_mode mode;
  1091. reload_order[j] = j;
  1092. reload_spill_index[j] = -1;
  1093. mode = (reload_inmode[j] == VOIDmode ? reload_outmode[j] : reload_inmode[j]);
  1094. if (CLASS_MAX_NREGS (reload_reg_class[j], mode) > 1)
  1095. have_groups = 1;
  1096. }
  1097. if (n_reloads > 1)
  1098. qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
  1099. for (j = 0; j < n_reloads; j++)
  1100. {
  1101. register int r = reload_order[j];
  1102. register int i;
  1103. register rtx new;
  1104. enum machine_mode reload_mode = reload_inmode[r];
  1105. if (GET_MODE_SIZE (reload_outmode[r]) > GET_MODE_SIZE (reload_mode))
  1106. reload_mode = reload_outmode[r];
  1107. if (reload_strict_low[r])
  1108. reload_mode = GET_MODE (SUBREG_REG (reload_out[r]));
  1109. /* Notice reloads that got mark inoperative. */
  1110. if (reload_out[r] == 0 && reload_in[r] == 0)
  1111. continue;
  1112. /* No need to find a reload-register if find_reloads chose one. */
  1113. if (reload_reg_rtx[r] != 0)
  1114. {
  1115. #if 0
  1116. /* But do see if the chosen reload-reg already contains
  1117. a copy of the desired value. */
  1118. if (reload_in[r] != 0)
  1119. {
  1120. register rtx equiv
  1121. = find_equiv_reg (reload_in[r], insn, 0,
  1122. REGNO (reload_reg_rtx[r]), 0, 0);
  1123. if (equiv != 0)
  1124. reload_inherited[r] = 1;
  1125. }
  1126. #endif
  1127. continue;
  1128. }
  1129. /* First see if this pseudo is already available as reloaded
  1130. for a previous insn.
  1131. This feature is disabled for multi-register groups
  1132. because we haven't yet any way to tell whether the entire
  1133. value is properly preserved.
  1134. It is also disabled when there are other reloads for mult-register
  1135. groups, lest the inherited reload reg break up a needed group. */
  1136. {
  1137. register int regno = -1;
  1138. if (reload_in[r] == 0)
  1139. ;
  1140. else if (GET_CODE (reload_in[r]) == REG)
  1141. regno = REGNO (reload_in[r]);
  1142. #if 0
  1143. /* This won't work, since REGNO can be a pseudo reg number.
  1144. Also, it takes much more hair to keep track of all the things
  1145. that can invalidate an inherited reload of part of a pseudoreg. */
  1146. else if (GET_CODE (reload_in[r]) == SUBREG
  1147. && GET_CODE (SUBREG_REG (reload_in[r])) == REG)
  1148. regno = REGNO (SUBREG_REG (reload_in[r])) + SUBREG_WORD (reload_in[r]);
  1149. #endif
  1150. if (regno >= 0
  1151. && GET_MODE_SIZE (reload_mode) <= UNITS_PER_WORD
  1152. && reg_last_reload_reg[regno] != 0
  1153. && ! have_groups)
  1154. {
  1155. i = spill_reg_order[REGNO (reg_last_reload_reg[regno])];
  1156. if (reg_reloaded_contents[i] == regno
  1157. && TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
  1158. spill_regs[i])
  1159. && ! reload_reg_in_use[spill_regs[i]])
  1160. {
  1161. /* Mark the reload register as in use for this insn. */
  1162. reload_reg_rtx[r] = reg_last_reload_reg[regno];
  1163. reload_reg_in_use[spill_regs[i]] = 1;
  1164. reload_inherited[r] = 1;
  1165. reload_spill_index[r] = i;
  1166. }
  1167. }
  1168. }
  1169. /* If this is not a pseudo, here's a different way to see
  1170. if it is already lying around. */
  1171. if (reload_in[r] != 0
  1172. && reload_out[r] == 0
  1173. && (CONSTANT_P (reload_in[r])
  1174. || GET_CODE (reload_in[r]) == PLUS
  1175. || GET_CODE (reload_in[r]) == MEM)
  1176. && ! have_groups)
  1177. {
  1178. register rtx equiv
  1179. = find_equiv_reg (reload_in[r], insn, reload_reg_class[r],
  1180. -1, (short *)1, 0);
  1181. /* If we found an equivalent reg, say no code need be generated
  1182. to load it, and use it as our reload reg. */
  1183. if (equiv != 0
  1184. && REGNO (equiv) != FRAME_POINTER_REGNUM)
  1185. {
  1186. reload_reg_rtx[r] = equiv;
  1187. reload_inherited[r] = 1;
  1188. /* If it is a spill reg,
  1189. mark the spill reg as in use for this insn. */
  1190. i = spill_reg_order[REGNO (equiv)];
  1191. if (i >= 0)
  1192. {
  1193. int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode);
  1194. while (nr > 0)
  1195. reload_reg_in_use[REGNO (equiv) + --nr] = 1;
  1196. }
  1197. }
  1198. }
  1199. /* If it isn't lying around, and isn't optional,
  1200. find a place to reload it into. */
  1201. if (reload_reg_rtx[r] != 0 || reload_optional[r] != 0)
  1202. continue;
  1203. /* Value not lying around; find a register to reload it into.
  1204. Here I is not a regno, it is an index into spill_regs. */
  1205. i = n_spills;
  1206. /* If we want just one reg, and other reloads want groups,
  1207. first try to find a reg that can't be part of a group. */
  1208. if (have_groups && HARD_REGNO_NREGS (spill_regs[i], reload_mode) == 1)
  1209. for (i = 0; i < n_spills; i++)
  1210. {
  1211. int regno = spill_regs[i];
  1212. int class = (int) reload_reg_class[r];
  1213. if (reload_reg_in_use[regno] == 0
  1214. && TEST_HARD_REG_BIT (reg_class_contents[class],
  1215. regno)
  1216. && !(regno + 1 < FIRST_PSEUDO_REGISTER
  1217. && spill_reg_order[regno + 1] >= 0
  1218. && reload_reg_in_use[regno + 1] == 0
  1219. && TEST_HARD_REG_BIT (reg_class_contents[class],
  1220. regno - 1))
  1221. && !(regno > 0
  1222. && spill_reg_order[regno - 1] >= 0
  1223. && reload_reg_in_use[regno - 1] == 0
  1224. && TEST_HARD_REG_BIT (reg_class_contents[class],
  1225. regno - 1)))
  1226. break;
  1227. }
  1228. /* If that didn't work, try to find a register that has only one
  1229. neighbor that could make a group with it. That way, if the
  1230. available registers are three consecutive ones, we avoid taking
  1231. the middle one (which would leave us with no possible groups). */
  1232. if (have_groups && HARD_REGNO_NREGS (spill_regs[i], reload_mode) == 1
  1233. && i == n_spills)
  1234. for (i = 0; i < n_spills; i++)
  1235. {
  1236. int regno = spill_regs[i];
  1237. int class = (int) reload_reg_class[r];
  1238. if (reload_reg_in_use[regno] == 0
  1239. && TEST_HARD_REG_BIT (reg_class_contents[class],
  1240. regno)
  1241. && (!(regno + 1 < FIRST_PSEUDO_REGISTER
  1242. && spill_reg_order[regno + 1] >= 0
  1243. && reload_reg_in_use[regno + 1] == 0
  1244. && TEST_HARD_REG_BIT (reg_class_contents[class],
  1245. regno - 1))
  1246. || !(regno > 0
  1247. && spill_reg_order[regno - 1] >= 0
  1248. && reload_reg_in_use[regno - 1] == 0
  1249. && TEST_HARD_REG_BIT (reg_class_contents[class],
  1250. regno - 1))))
  1251. break;
  1252. }
  1253. /* Now, if we want a single register and haven't yet found one,
  1254. take any reg in the right class and not in use.
  1255. If we want a consecutive group, here is where we look for it. */
  1256. if (i == n_spills)
  1257. for (i = 0; i < n_spills; i++)
  1258. {
  1259. int class = (int) reload_reg_class[r];
  1260. if (reload_reg_in_use[spill_regs[i]] == 0
  1261. && TEST_HARD_REG_BIT (reg_class_contents[class],
  1262. spill_regs[i]))
  1263. {
  1264. int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode);
  1265. /* If we need only one reg, we have already won. */
  1266. if (nr == 1)
  1267. break;
  1268. /* Otherwise check that as many consecutive regs as we need
  1269. are available here. */
  1270. if (HARD_REGNO_MODE_OK (spill_regs[i], reload_mode))
  1271. while (nr > 1)
  1272. {
  1273. if (!(TEST_HARD_REG_BIT (reg_class_contents[class],
  1274. spill_regs[i] + nr - 1)
  1275. && spill_reg_order[spill_regs[i] + nr - 1] >= 0
  1276. && reload_reg_in_use[spill_regs[i] + nr - 1] == 0))
  1277. break;
  1278. nr--;
  1279. }
  1280. if (nr == 1)
  1281. break;
  1282. }
  1283. }
  1284. /* We should have found a spill register by now. */
  1285. if (i == n_spills)
  1286. abort ();
  1287. /* Mark as in use for this insn the reload regs we use for this. */
  1288. {
  1289. int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode);
  1290. while (nr > 0)
  1291. reload_reg_in_use[spill_regs[i] + --nr] = 1;
  1292. }
  1293. new = spill_reg_rtx[i];
  1294. if (new == 0 || GET_MODE (new) != reload_mode)
  1295. spill_reg_rtx[i] = new = gen_rtx (REG, reload_mode, spill_regs[i]);
  1296. reload_reg_rtx[r] = new;
  1297. reload_spill_index[r] = i;
  1298. reg_reloaded_contents[i] = -1;
  1299. /* Detect when the reload reg can't hold the reload mode. */
  1300. if (! HARD_REGNO_MODE_OK (REGNO (reload_reg_rtx[r]), reload_mode))
  1301. {
  1302. if (! asm_noperands (PATTERN (insn)))
  1303. /* It's the compiler's fault. */
  1304. abort ();
  1305. /* It's the user's fault; the operand's mode and constraint
  1306. don't match. Disable this reload so we don't crash in final.
  1307. Maybe we should print an error message too?? */
  1308. reload_in[r] = 0;
  1309. reload_out[r] = 0;
  1310. reload_reg_rtx[r] = 0;
  1311. reload_optional[r] = 1;
  1312. }
  1313. }
  1314. /* For all the spill regs newly reloaded in this instruction,
  1315. record what they were reloaded from, so subsequent instructions
  1316. can inherit the reloads. */
  1317. for (j = 0; j < n_reloads; j++)
  1318. {
  1319. register int r = reload_order[j];
  1320. register int i = reload_spill_index[r];
  1321. /* I is nonneg if this reload used one of the spill regs.
  1322. If reload_reg_rtx[r] is 0, this is an optional reload
  1323. that we opted to ignore. */
  1324. if (i >= 0 && reload_reg_rtx[r] != 0)
  1325. {
  1326. /* Maybe the spill reg contains a copy of reload_out. */
  1327. if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
  1328. {
  1329. register int nregno = REGNO (reload_out[r]);
  1330. reg_last_reload_reg[nregno] = reload_reg_rtx[r];
  1331. reg_reloaded_contents[i] = nregno;
  1332. reg_has_output_reload[nregno] = 1;
  1333. }
  1334. /* Maybe the spill reg contains a copy of reload_in. */
  1335. else if (reload_out[r] == 0 && GET_CODE (reload_in[r]) == REG)
  1336. {
  1337. register int nregno = REGNO (reload_in[r]);
  1338. /* If there are two separate reloads (one in and one out)
  1339. for the same (hard or pseudo) reg, set reg_last_reload_reg
  1340. based on the output reload. */
  1341. if (!reg_has_output_reload[nregno])
  1342. {
  1343. reg_last_reload_reg[nregno] = reload_reg_rtx[r];
  1344. reg_reloaded_contents[i] = nregno;
  1345. }
  1346. }
  1347. /* Otherwise, the spill reg doesn't contain a copy of any reg.
  1348. Clear out its records, lest it be taken for a copy
  1349. of reload_in when that is no longer true. */
  1350. else
  1351. reg_reloaded_contents[i] = -1;
  1352. }
  1353. }
  1354. /* Now output the instructions to copy the data into and out of the
  1355. reload registers. Do these in the order that the reloads were reported,
  1356. since reloads of base and index registers precede reloads of operands
  1357. and the operands may need the base and index registers reloaded. */
  1358. for (j = 0; j < n_reloads; j++)
  1359. {
  1360. register rtx old;
  1361. rtx store_insn;
  1362. old = reload_in[j];
  1363. if (old != 0 && ! reload_inherited[j]
  1364. && reload_reg_rtx[j] != old
  1365. && reload_reg_rtx[j] != 0)
  1366. {
  1367. register rtx reloadreg = reload_reg_rtx[j];
  1368. rtx oldequiv = 0;
  1369. enum machine_mode mode;
  1370. /* Strip off of OLD any size-increasing SUBREGs such as
  1371. (SUBREG:SI foo:QI 0). */
  1372. while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
  1373. && (GET_MODE_SIZE (GET_MODE (old))
  1374. > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
  1375. old = SUBREG_REG (old);
  1376. /* If reloading from memory, see if there is a register
  1377. that already holds the same value. If so, reload from there.
  1378. We can pass 0 as the reload_reg_p argument because
  1379. any other reload has either already been emitted,
  1380. in which case find_equiv_reg will see the reload-insn,
  1381. or has yet to be emitted, in which case it doesn't matter
  1382. because we will use this equiv reg right away. */
  1383. if (GET_CODE (old) == MEM
  1384. || (GET_CODE (old) == REG
  1385. && REGNO (old) >= FIRST_PSEUDO_REGISTER
  1386. && reg_renumber[REGNO (old)] < 0))
  1387. oldequiv = find_equiv_reg (old, insn, GENERAL_REGS, -1, 0, 0);
  1388. if (oldequiv == 0)
  1389. oldequiv = old;
  1390. /* Determine the mode to reload in.
  1391. This is very tricky because we have three to choose from.
  1392. There is the mode the insn operand wants (reload_inmode[J]).
  1393. There is the mode of the reload register RELOADREG.
  1394. There is the intrinsic mode of the operand, revealed now
  1395. in OLD because we have stripped SUBREGs.
  1396. It turns out that RELOADREG's mode is irrelevant:
  1397. we can change that arbitrarily.
  1398. Neither of the other two is always right. For example, consider
  1399. (SUBREG:SI foo:QI)) appearing as an operand that must be SImode;
  1400. then suppose foo is in memory. This must be loaded in QImode
  1401. because we cannot fetch a byte as a word. In this case OLD's
  1402. mode is correct.
  1403. Then consider a one-word union which has SImode and one of its
  1404. members is a float, being fetched as (SUBREG:SF union:SI).
  1405. We must fetch that as SFmode because we could be loading into
  1406. a float-only register. In this case OLD's mode is also correct.
  1407. Consider an immediate integer: it has VOIDmode. Here we need
  1408. to get a mode from something else.
  1409. In some cases, there is a fourth mode, the operand's
  1410. containing mode. If the insn specifies a containing mode for
  1411. this operand, it overrides all others.
  1412. I am not sure whether the algorithm here is always right,
  1413. but it does the right things in those cases. */
  1414. mode = GET_MODE (old);
  1415. if (mode == VOIDmode)
  1416. mode = reload_inmode[j];
  1417. if (reload_strict_low[j])
  1418. mode = GET_MODE (SUBREG_REG (reload_in[j]));
  1419. /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
  1420. then load RELOADREG from OLDEQUIV. */
  1421. if (GET_MODE (reloadreg) != mode)
  1422. reloadreg = gen_rtx (SUBREG, mode, reloadreg, 0);
  1423. if (GET_MODE (oldequiv) != VOIDmode
  1424. && mode != GET_MODE (oldequiv))
  1425. oldequiv = gen_rtx (SUBREG, mode, oldequiv, 0);
  1426. /* If we are reloading a pseudo-register that was set by the previous
  1427. insn, see if we can get rid of that pseudo-register entirely
  1428. by redirecting the previous insn into our reload register. */
  1429. if (optimize && GET_CODE (old) == REG
  1430. && REGNO (old) >= FIRST_PSEUDO_REGISTER
  1431. && PREV_INSN (insn) && GET_CODE (PREV_INSN (insn)) == INSN
  1432. && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
  1433. && SET_DEST (PATTERN (PREV_INSN (insn))) == old
  1434. && dead_or_set_p (insn, old)
  1435. && reg_n_deaths[REGNO (old)] == 1
  1436. && reg_n_sets[REGNO (old)] == 1)
  1437. {
  1438. /* For the debugging info,
  1439. say the pseudo lives in this reload reg. */
  1440. reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
  1441. alter_reg (REGNO (old));
  1442. /* Store into the reload register instead of the pseudo. */
  1443. SET_DEST (PATTERN (PREV_INSN (insn))) = reloadreg;
  1444. }
  1445. else
  1446. /* We can't do that, so output an insn to load RELOADREG. */
  1447. emit_insn_before (gen_move_insn (reloadreg, oldequiv), insn);
  1448. /* If this reload wants reload_in[j] incremented by a constant,
  1449. output code to get this done before the insn reloaded for. */
  1450. if (reload_inc[j] != 0)
  1451. {
  1452. /* If reload_in[j] is a register, assume we can
  1453. output an insn to increment it directly. */
  1454. if (GET_CODE (old) == REG &&
  1455. (REGNO (old) < FIRST_PSEUDO_REGISTER
  1456. || reg_renumber[REGNO (old)] >= 0))
  1457. emit_insn_before (gen_add2_insn (old,
  1458. gen_rtx (CONST_INT, VOIDmode,
  1459. reload_inc[j])),
  1460. insn);
  1461. else
  1462. /* Else we must not assume we can increment reload_in[j]
  1463. (even though on many target machines we can);
  1464. increment the copy in the reload register,
  1465. save that back, then decrement the reload register
  1466. so it has its original contents. */
  1467. {
  1468. emit_insn_before (gen_add2_insn (reloadreg,
  1469. gen_rtx (CONST_INT, VOIDmode,
  1470. reload_inc[j])),
  1471. insn);
  1472. emit_insn_before (gen_move_insn (oldequiv, reloadreg), insn);
  1473. emit_insn_before (gen_sub2_insn (reloadreg,
  1474. gen_rtx (CONST_INT, VOIDmode,
  1475. reload_inc[j])),
  1476. insn);
  1477. }
  1478. }
  1479. }
  1480. /* If we are reloading a register that was recently stored in with an
  1481. output-reload, see if we can prove there was
  1482. actually no need to store the old value in it. */
  1483. if (optimize && reload_inherited[j] && reload_spill_index[j] >= 0
  1484. && GET_CODE (reload_in[j]) == REG
  1485. && spill_reg_store[reload_spill_index[j]] != 0
  1486. && dead_or_set_p (insn, reload_in[j]))
  1487. {
  1488. register rtx i1;
  1489. /* If the pseudo-reg we are reloading is no longer referenced
  1490. anywhere between the store into it and here,
  1491. and no jumps or labels intervene, then the value can get
  1492. here through the reload reg alone. */
  1493. for (i1 = NEXT_INSN (spill_reg_store[reload_spill_index[j]]);
  1494. i1 != insn; i1 = NEXT_INSN (i1))
  1495. {
  1496. if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
  1497. break;
  1498. if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
  1499. && reg_mentioned_p (reload_in[j], PATTERN (i1)))
  1500. break;
  1501. }
  1502. if (i1 == insn)
  1503. {
  1504. /* If this insn will store in the pseudo again,
  1505. the previous store can be removed. */
  1506. if (reload_out[j] == reload_in[j])
  1507. delete_insn (spill_reg_store[reload_spill_index[j]]);
  1508. /* See if the pseudo reg has been completely replaced
  1509. with reload regs. If so, delete the store insn
  1510. and forget we had a stack slot for the pseudo. */
  1511. if (reg_n_deaths[REGNO (reload_in[j])] == 1
  1512. && reg_basic_block[REGNO (reload_in[j])] >= 0)
  1513. {
  1514. /* We know that it was used only between here
  1515. and the beginning of the current basic block.
  1516. Search that range; see if any ref remains. */
  1517. for (i1 = PREV_INSN (insn); i1; i1 = PREV_INSN (i1))
  1518. {
  1519. if (GET_CODE (i1) == CODE_LABEL
  1520. || GET_CODE (i1) == JUMP_INSN)
  1521. break;
  1522. if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
  1523. && reg_mentioned_p (reload_in[j], PATTERN (i1)))
  1524. goto still_used;
  1525. }
  1526. /* For the debugging info,
  1527. say the pseudo lives in this reload reg. */
  1528. reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
  1529. alter_reg (REGNO (old));
  1530. delete_insn (spill_reg_store[reload_spill_index[j]]);
  1531. still_used: ;
  1532. }
  1533. }
  1534. }
  1535. /* Input-reloading is done. Now do output-reloading,
  1536. storing the value from the reload-register after the main insn
  1537. if reload_out[j] is nonzero. */
  1538. old = reload_out[j];
  1539. if (old != 0
  1540. && reload_reg_rtx[j] != old
  1541. && reload_reg_rtx[j] != 0)
  1542. {
  1543. register rtx reloadreg = reload_reg_rtx[j];
  1544. enum machine_mode mode;
  1545. /* Strip off of OLD any size-increasing SUBREGs such as
  1546. (SUBREG:SI foo:QI 0). */
  1547. while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
  1548. && (GET_MODE_SIZE (GET_MODE (old))
  1549. > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
  1550. old = SUBREG_REG (old);
  1551. /* Determine the mode to reload in.
  1552. See comments above (for input reloading). */
  1553. mode = GET_MODE (old);
  1554. if (mode == VOIDmode)
  1555. abort (); /* Should never happen for an output. */
  1556. #if 0
  1557. mode = reload_inmode[j];
  1558. #endif
  1559. if (reload_strict_low[j])
  1560. mode = GET_MODE (SUBREG_REG (reload_out[j]));
  1561. /* Encapsulate both RELOADREG and OLD into that mode,
  1562. then load RELOADREG from OLD. */
  1563. if (GET_MODE (reloadreg) != mode)
  1564. reloadreg = gen_rtx (SUBREG, mode, reloadreg, 0);
  1565. if (GET_MODE (old) != VOIDmode
  1566. && mode != GET_MODE (old))
  1567. old = gen_rtx (SUBREG, mode, old, 0);
  1568. store_insn = emit_insn_after (gen_move_insn (old, reloadreg), insn);
  1569. }
  1570. else store_insn = 0;
  1571. if (reload_spill_index[j] >= 0)
  1572. spill_reg_store[reload_spill_index[j]] = store_insn;
  1573. }
  1574. }