combine.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. /* Optimize by combining instructions for GNU compiler.
  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. /* This module is essentially the "combiner" phase of the U. of Arizona
  18. Portable Optimizer, but redone to work on our list-structured
  19. representation for RTL instead of their string representation.
  20. The LOG_LINKS of each insn identify the most recent assignment
  21. to each REG used in the insn. It is a list of previous insns,
  22. each of which contains a SET for a REG that is used in this insn
  23. and not used or set in between. LOG_LINKs never cross basic blocks.
  24. They were set up by the preceding pass (lifetime analysis).
  25. We try to combine each pair of insns joined by a logical link.
  26. We also try to combine triples of insns A, B and C when
  27. C has a link back to B and B has a link back to A.
  28. LOG_LINKS does not have links for use of the CC0. They don't
  29. need to, because the insn that sets the CC0 is always immediately
  30. before the insn that tests it. So we always regard a branch
  31. insn as having a logical link to the preceding insn.
  32. We check (with use_crosses_set_p) to avoid combining in such a way
  33. as to move a computation to a place where its value would be different.
  34. Combination is done by mathematically substituting the previous
  35. insn(s) values for the regs they set into the expressions in
  36. the later insns that refer to these regs. If the result is a valid insn
  37. for our target machine, according to the machine description,
  38. we install it, delete the earlier insns, and update the data flow
  39. information (LOG_LINKS and REG_NOTES) for what we did.
  40. To simplify substitution, we combine only when the earlier insn(s)
  41. consist of only a single assignment. To simplify updating afterward,
  42. we never combine when a subroutine call appears in the middle.
  43. Since we do not represent assignments to CC0 explicitly except when that
  44. is all an insn does, there is no LOG_LINKS entry in an insn that uses
  45. the condition code for the insn that set the condition code.
  46. Fortunately, these two insns must be consecutive.
  47. Therefore, every JUMP_INSN is taken to have an implicit logical link
  48. to the preceding insn. This is not quite right, since non-jumps can
  49. also use the condition code; but in practice such insns would not
  50. combine anyway. */
  51. #include "config.h"
  52. #include "rtl.h"
  53. #include "flags.h"
  54. #include "regs.h"
  55. #include "basic-block.h"
  56. #include "insn-config.h"
  57. #include "recog.h"
  58. #define max(A,B) ((A) > (B) ? (A) : (B))
  59. #define min(A,B) ((A) < (B) ? (A) : (B))
  60. /* It is not safe to use ordinary gen_lowpart in combine.
  61. Use gen_lowpart_for_combine instead. See comments there. */
  62. #define gen_lowpart dont_use_gen_lowpart_you_dummy
  63. /* Number of attempts to combine instructions in this function. */
  64. static int combine_attempts;
  65. /* Number of attempts that got as far as substitution in this function. */
  66. static int combine_merges;
  67. /* Number of instructions combined with added SETs in this function. */
  68. static int combine_extras;
  69. /* Number of instructions combined in this function. */
  70. static int combine_successes;
  71. /* Totals over entire compilation. */
  72. static int total_attempts, total_merges, total_extras, total_successes;
  73. /* Vector mapping INSN_UIDs to cuids.
  74. The cuids are like uids but increase monononically always.
  75. Combine always uses cuids so that it can compare them.
  76. But actually renumbering the uids, which we used to do,
  77. proves to be a bad idea because it makes it hard to compare
  78. the dumps produced by earlier passes with those from later passes. */
  79. static short *uid_cuid;
  80. /* Get the cuid of an insn. */
  81. #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
  82. /* Record last point of death of (hard or pseudo) register n. */
  83. static rtx *reg_last_death;
  84. /* Record last point of modification of (hard or pseudo) register n. */
  85. static rtx *reg_last_set;
  86. /* Record the cuid of the last insn that invalidated memory
  87. (anything that writes memory, and subroutine calls). */
  88. static int mem_last_set;
  89. /* Record the cuid of the last CALL_INSN
  90. so we can tell whether a potential combination crosses any calls. */
  91. static int last_call_cuid;
  92. /* When `subst' is called, this is the insn that is being modified
  93. (by combining in a previous insn). The PATTERN of this insn
  94. is still the old pattern partially modified and it should not be
  95. looked at, but this may be used to examine the successors of the insn
  96. to judge whether a simplification is valid. */
  97. static rtx subst_insn;
  98. /* Record one modification to rtl structure
  99. to be undone by storing old_contents into *where. */
  100. struct undo
  101. {
  102. rtx *where;
  103. rtx old_contents;
  104. };
  105. /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
  106. num_undo says how many are currently recorded.
  107. storage is nonzero if we must undo the allocation of new storage.
  108. The value of storage is what to pass to obfree. */
  109. #define MAX_UNDO 10
  110. struct undobuf
  111. {
  112. int num_undo;
  113. char *storage;
  114. struct undo undo[MAX_UNDO];
  115. };
  116. static struct undobuf undobuf;
  117. /* Number of times the pseudo being substituted for
  118. was found and replaced. */
  119. static int n_occurrences;
  120. static void move_deaths ();
  121. static void remove_death ();
  122. static void record_dead_and_set_regs ();
  123. int regno_dead_p ();
  124. static int use_crosses_set_p ();
  125. static rtx subst ();
  126. static void undo_all ();
  127. static void copy_substitutions ();
  128. static void add_links ();
  129. static void add_incs ();
  130. static int insn_has_inc_p ();
  131. static int adjacent_insns_p ();
  132. static rtx simplify_and_const_int ();
  133. static rtx gen_lowpart_for_combine ();
  134. static void simplify_set_cc0_and ();
  135. /* Main entry point for combiner. F is the first insn of the function.
  136. NREGS is the first unused pseudo-reg number. */
  137. void
  138. combine_instructions (f, nregs)
  139. rtx f;
  140. int nregs;
  141. {
  142. register rtx insn;
  143. register int i;
  144. register rtx links, nextlinks;
  145. rtx prev;
  146. combine_attempts = 0;
  147. combine_merges = 0;
  148. combine_extras = 0;
  149. combine_successes = 0;
  150. reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
  151. reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
  152. bzero (reg_last_death, nregs * sizeof (rtx));
  153. bzero (reg_last_set, nregs * sizeof (rtx));
  154. init_recog ();
  155. /* Compute maximum uid value so uid_cuid can be allocated. */
  156. for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  157. if (INSN_UID (insn) > i)
  158. i = INSN_UID (insn);
  159. uid_cuid = (short *) alloca ((i + 1) * sizeof (short));
  160. /* Compute the mapping from uids to cuids.
  161. Cuids are numbers assigned to insns, like uids,
  162. except that cuids increase monotonically through the code. */
  163. for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  164. INSN_CUID (insn) = ++i;
  165. /* Now scan all the insns in forward order. */
  166. last_call_cuid = 0;
  167. mem_last_set = 0;
  168. prev = 0;
  169. for (insn = f; insn; insn = NEXT_INSN (insn))
  170. {
  171. if (GET_CODE (insn) == INSN
  172. || GET_CODE (insn) == CALL_INSN
  173. || GET_CODE (insn) == JUMP_INSN)
  174. {
  175. retry:
  176. /* Try this insn with each insn it links back to. */
  177. for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  178. if (try_combine (insn, XEXP (links, 0), 0))
  179. goto retry;
  180. /* Try each sequence of three linked insns ending with this one. */
  181. for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  182. if (GET_CODE (XEXP (links, 0)) != NOTE)
  183. for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
  184. nextlinks = XEXP (nextlinks, 1))
  185. if (try_combine (insn, XEXP (links, 0), XEXP (nextlinks, 0)))
  186. goto retry;
  187. /* Try to combine a jump insn that uses CC0
  188. with a preceding insn that sets CC0, and maybe with its
  189. logical predecessor as well.
  190. This is how we make decrement-and-branch insns.
  191. We need this special code because data flow connections
  192. via CC0 do not get entered in LOG_LINKS. */
  193. if (GET_CODE (insn) == JUMP_INSN
  194. && prev != 0
  195. && GET_CODE (prev) == INSN
  196. && GET_CODE (PATTERN (prev)) == SET
  197. && GET_CODE (SET_DEST (PATTERN (prev))) == CC0)
  198. {
  199. if (try_combine (insn, prev, 0))
  200. goto retry;
  201. if (GET_CODE (prev) != NOTE)
  202. for (nextlinks = LOG_LINKS (prev); nextlinks;
  203. nextlinks = XEXP (nextlinks, 1))
  204. if (try_combine (insn, prev, XEXP (nextlinks, 0)))
  205. goto retry;
  206. }
  207. #if 0
  208. /* Turned off because on 68020 it takes four insns to make
  209. something like (a[b / 32] & (1 << (31 - (b % 32)))) != 0
  210. that could actually be optimized, and that's an unlikely piece of code. */
  211. /* If an insn gets or sets a bit field, try combining it
  212. with two different insns whose results it uses. */
  213. if (GET_CODE (insn) == INSN
  214. && GET_CODE (PATTERN (insn)) == SET
  215. && (GET_CODE (SET_DEST (PATTERN (insn))) == ZERO_EXTRACT
  216. || GET_CODE (SET_DEST (PATTERN (insn))) == SIGN_EXTRACT
  217. || GET_CODE (SET_SRC (PATTERN (insn))) == ZERO_EXTRACT
  218. || GET_CODE (SET_SRC (PATTERN (insn))) == SIGN_EXTRACT))
  219. {
  220. for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  221. if (GET_CODE (XEXP (links, 0)) != NOTE)
  222. for (nextlinks = XEXP (links, 1); nextlinks;
  223. nextlinks = XEXP (nextlinks, 1))
  224. if (try_combine (insn, XEXP (links, 0), XEXP (nextlinks, 0)))
  225. goto retry;
  226. }
  227. #endif
  228. record_dead_and_set_regs (insn);
  229. prev = insn;
  230. }
  231. else if (GET_CODE (insn) != NOTE)
  232. prev = 0;
  233. }
  234. total_attempts += combine_attempts;
  235. total_merges += combine_merges;
  236. total_extras += combine_extras;
  237. total_successes += combine_successes;
  238. }
  239. /* Try to combine the insns I1 and I2 into I3.
  240. Here I1 appears earlier than I2, which is earlier than I3.
  241. I1 can be zero; then we combine just I2 into I3.
  242. Return 1 if successful; if that happens, I1 and I2 are pseudo-deleted
  243. by turning them into NOTEs, and I3 is modified.
  244. Return 0 if the combination does not work. Then nothing is changed. */
  245. static int
  246. try_combine (i3, i2, i1)
  247. register rtx i3, i2, i1;
  248. {
  249. register rtx newpat;
  250. int added_sets_1 = 0;
  251. int added_sets_2 = 0;
  252. int total_sets;
  253. int i2_is_used;
  254. register rtx link;
  255. int insn_code_number;
  256. int recog_flags = 0;
  257. rtx i2dest, i2src;
  258. rtx i1dest, i1src;
  259. int maxreg;
  260. combine_attempts++;
  261. /* Don't combine with something already used up by combination. */
  262. if (GET_CODE (i2) == NOTE
  263. || (i1 && GET_CODE (i1) == NOTE))
  264. return 0;
  265. /* Don't combine across a CALL_INSN, because that would possibly
  266. change whether the life span of some REGs crosses calls or not,
  267. and it is a pain to update that information. */
  268. if (INSN_CUID (i2) < last_call_cuid
  269. || (i1 && INSN_CUID (i1) < last_call_cuid))
  270. return 0;
  271. /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
  272. That REG must be either set or dead by the final instruction
  273. (so that we can safely forget about setting it).
  274. Also test use_crosses_set_p to make sure that the value
  275. that is to be substituted for the register
  276. does not use any registers whose values alter in between.
  277. Do not try combining with moves from one register to another
  278. since it is better to let them be tied by register allocation.
  279. (There is a switch to permit such combination; except the insns
  280. that copy a function value into another register are never combined
  281. because moving that too far away from the function call could cause
  282. something else to be stored in that register in the interim.)
  283. A set of a SUBREG is considered as if it were a set from
  284. SUBREG. Thus, (SET (SUBREG:X (REG:Y...)) (something:X...))
  285. is handled by substituting (SUBREG:Y (something:X...)) for (REG:Y...). */
  286. if (GET_CODE (PATTERN (i2)) != SET)
  287. return 0;
  288. i2dest = SET_DEST (PATTERN (i2));
  289. i2src = SET_SRC (PATTERN (i2));
  290. if (GET_CODE (i2dest) == SUBREG)
  291. {
  292. i2dest = SUBREG_REG (i2dest);
  293. i2src = gen_rtx (SUBREG, GET_MODE (i2dest), i2src, 0);
  294. }
  295. if (GET_CODE (i2dest) != CC0
  296. && (GET_CODE (i2dest) != REG
  297. || (GET_CODE (i2src) == REG
  298. && (!flag_combine_regs
  299. || FUNCTION_VALUE_REGNO_P (REGNO (i2src))))
  300. || GET_CODE (i2src) == CALL
  301. || use_crosses_set_p (i2src, INSN_CUID (i2))))
  302. return 0;
  303. if (i1 != 0)
  304. {
  305. if (GET_CODE (PATTERN (i1)) != SET)
  306. return 0;
  307. i1dest = SET_DEST (PATTERN (i1));
  308. i1src = SET_SRC (PATTERN (i1));
  309. if (GET_CODE (i1dest) == SUBREG)
  310. {
  311. i1dest = SUBREG_REG (i1dest);
  312. i1src = gen_rtx (SUBREG, GET_MODE (i1dest), i1src, 0);
  313. }
  314. if (GET_CODE (i1dest) != CC0
  315. && (GET_CODE (i1dest) != REG
  316. || (GET_CODE (i1src) == REG
  317. && (!flag_combine_regs
  318. || FUNCTION_VALUE_REGNO_P (REGNO (i1src))))
  319. || GET_CODE (i1src) == CALL
  320. || use_crosses_set_p (i1src, INSN_CUID (i1))))
  321. return 0;
  322. }
  323. /* If I1 or I2 contains an autoincrement or autodecrement,
  324. make sure that register is not used between there and I3.
  325. Also insist that I3 not be a jump; if it were one
  326. and the incremented register were spilled, we would lose. */
  327. if ((link = find_reg_note (i2, REG_INC, 0)) != 0
  328. && (GET_CODE (i3) == JUMP_INSN
  329. || reg_used_between_p (XEXP (link, 0), i2, i3)))
  330. return 0;
  331. if (i1 && (link = find_reg_note (i1, REG_INC, 0)) != 0
  332. && (GET_CODE (i3) == JUMP_INSN
  333. || reg_used_between_p (XEXP (link, 0), i1, i3)))
  334. return 0;
  335. /* See if the SETs in i1 or i2 need to be kept around in the merged
  336. instruction: whenever the value set there is still needed past i3. */
  337. added_sets_2 = (GET_CODE (i2dest) != CC0
  338. && ! dead_or_set_p (i3, i2dest));
  339. if (i1)
  340. added_sets_1 = ! (dead_or_set_p (i3, i1dest)
  341. || dead_or_set_p (i2, i1dest));
  342. combine_merges++;
  343. undobuf.num_undo = 0;
  344. undobuf.storage = 0;
  345. /* Substitute in the latest insn for the regs set by the earlier ones. */
  346. maxreg = max_reg_num ();
  347. subst_insn = i3;
  348. n_occurrences = 0; /* `subst' counts here */
  349. newpat = subst (PATTERN (i3), i2dest, i2src);
  350. /* Record whether i2's body now appears within i3's body. */
  351. i2_is_used = n_occurrences;
  352. if (i1)
  353. {
  354. n_occurrences = 0;
  355. newpat = subst (newpat, i1dest, i1src);
  356. }
  357. if (GET_CODE (PATTERN (i3)) == SET
  358. && SET_DEST (PATTERN (i3)) == cc0_rtx
  359. && (GET_CODE (SET_SRC (PATTERN (i3))) == AND
  360. || GET_CODE (SET_SRC (PATTERN (i3))) == LSHIFTRT)
  361. && next_insn_tests_no_inequality (i3))
  362. simplify_set_cc0_and (i3);
  363. if (max_reg_num () != maxreg)
  364. abort ();
  365. /* If the actions of the earler insns must be kept
  366. in addition to substituting them into the latest one,
  367. we must make a new PARALLEL for the latest insn
  368. to hold additional the SETs. */
  369. if (added_sets_1 || added_sets_2)
  370. {
  371. combine_extras++;
  372. /* Arrange to free later what we allocate now
  373. if we don't accept this combination. */
  374. if (!undobuf.storage)
  375. undobuf.storage = (char *) oballoc (0);
  376. if (GET_CODE (newpat) == PARALLEL)
  377. {
  378. rtvec old = XVEC (newpat, 0);
  379. total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
  380. newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
  381. bcopy (&old->elem[0], &XVECEXP (newpat, 0, 0),
  382. sizeof (old->elem[0]) * old->num_elem);
  383. }
  384. else
  385. {
  386. rtx old = newpat;
  387. total_sets = 1 + added_sets_1 + added_sets_2;
  388. newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
  389. XVECEXP (newpat, 0, 0) = old;
  390. }
  391. if (added_sets_1)
  392. {
  393. XVECEXP (newpat, 0, --total_sets) = PATTERN (i1);
  394. }
  395. if (added_sets_2)
  396. {
  397. /* If there is no I1, use I2's body as is. */
  398. if (i1 == 0
  399. /* If I2 was stuck into I3, then anything within it has
  400. already had I1 substituted into it when that was done to I3. */
  401. || i2_is_used)
  402. {
  403. XVECEXP (newpat, 0, --total_sets) = PATTERN (i2);
  404. }
  405. else
  406. XVECEXP (newpat, 0, --total_sets)
  407. = subst (PATTERN (i2), i1dest, i1src);
  408. }
  409. }
  410. /* Fail if an autoincrement side-effect has been duplicated. */
  411. if ((i2_is_used > 1 && find_reg_note (i2, REG_INC, 0) != 0)
  412. || (i1 != 0 && n_occurrences > 1 && find_reg_note (i1, REG_INC, 0) != 0))
  413. {
  414. undo_all ();
  415. return 0;
  416. }
  417. /* Is the result of combination a valid instruction? */
  418. insn_code_number = recog (newpat, i3);
  419. if (insn_code_number >= 0)
  420. {
  421. /* Yes. Install it. */
  422. register int regno;
  423. INSN_CODE (i3) = insn_code_number;
  424. PATTERN (i3) = newpat;
  425. /* If anything was substituted more than once,
  426. copy it to avoid invalid shared rtl structure. */
  427. copy_substitutions ();
  428. /* The data flowing into I2 now flows into I3.
  429. But we cannot always move all of I2's LOG_LINKS into I3,
  430. since they must go to a setting of a REG from the
  431. first use following. If I2 was the first use following a set,
  432. I3 is now a use, but it is not the first use
  433. if some instruction between I2 and I3 is also a use.
  434. Here, for simplicity, we move all the links only if
  435. there are no real insns between I2 and I3.
  436. Otherwise, we move only links that correspond to regs
  437. that used to die in I2. They are always safe to move. */
  438. add_links (i3, i2, adjacent_insns_p (i2, i3));
  439. /* Most REGs that previously died in I2 now die in I3. */
  440. move_deaths (i2src, INSN_CUID (i2), i3);
  441. if (GET_CODE (i2dest) == REG)
  442. {
  443. /* If the reg formerly set in I2 died only once and that was in I3,
  444. zero its use count so it won't make `reload' do any work. */
  445. regno = REGNO (i2dest);
  446. if (! added_sets_2)
  447. {
  448. reg_n_sets[regno]--;
  449. /* Used to check && regno_dead_p (regno, i3) also here. */
  450. if (reg_n_sets[regno] == 0
  451. && ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
  452. & (1 << (regno % HOST_BITS_PER_INT))))
  453. reg_n_refs[regno] = 0;
  454. }
  455. /* If a ref to REGNO was substituted into I3 from I2,
  456. then it still dies there if it previously did.
  457. Otherwise either REGNO never did die in I3 so remove_death is safe
  458. or this entire life of REGNO is gone so remove its death. */
  459. if (!added_sets_2
  460. && ! reg_mentioned_p (i2dest, PATTERN (i3)))
  461. remove_death (regno, i3);
  462. }
  463. /* Any registers previously autoincremented in I2
  464. are now incremented in I3. */
  465. add_incs (i3, REG_NOTES (i2));
  466. if (i1)
  467. {
  468. /* Likewise, merge the info from I1 and get rid of it. */
  469. add_links (i3, i1,
  470. adjacent_insns_p (i1, i2) && adjacent_insns_p (i2, i3));
  471. move_deaths (i1src, INSN_CUID (i1), i3);
  472. if (GET_CODE (i1dest) == REG)
  473. {
  474. regno = REGNO (i1dest);
  475. if (! added_sets_1)
  476. {
  477. reg_n_sets[regno]--;
  478. /* Used to also check && regno_dead_p (regno, i3) here. */
  479. if (reg_n_sets[regno] == 0
  480. && ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
  481. & (1 << (regno % HOST_BITS_PER_INT))))
  482. reg_n_refs[regno] = 0;
  483. }
  484. /* If a ref to REGNO was substituted into I3 from I1,
  485. then it still dies there if it previously did.
  486. Else either REGNO never did die in I3 so remove_death is safe
  487. or this entire life of REGNO is gone so remove its death. */
  488. if (! added_sets_1
  489. && ! reg_mentioned_p (i1dest, PATTERN (i3)))
  490. remove_death (regno, i3);
  491. }
  492. add_incs (i3, REG_NOTES (i1));
  493. LOG_LINKS (i1) = 0;
  494. PUT_CODE (i1, NOTE);
  495. NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
  496. NOTE_SOURCE_FILE (i1) = 0;
  497. }
  498. /* Get rid of I2. */
  499. LOG_LINKS (i2) = 0;
  500. PUT_CODE (i2, NOTE);
  501. NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
  502. NOTE_SOURCE_FILE (i2) = 0;
  503. combine_successes++;
  504. return 1;
  505. }
  506. /* Failure: change I3 back the way it was. */
  507. undo_all ();
  508. return 0;
  509. }
  510. /* Undo all the modifications recorded in undobuf. */
  511. static void
  512. undo_all ()
  513. {
  514. register int i;
  515. if (undobuf.num_undo > MAX_UNDO)
  516. undobuf.num_undo = MAX_UNDO;
  517. for (i = undobuf.num_undo - 1; i >= 0; i--)
  518. *undobuf.undo[i].where = undobuf.undo[i].old_contents;
  519. if (undobuf.storage)
  520. obfree (undobuf.storage);
  521. undobuf.num_undo = 0;
  522. undobuf.storage = 0;
  523. }
  524. /* If this insn had more than one substitution,
  525. copy all but one, so that no invalid shared substructure is introduced. */
  526. static void
  527. copy_substitutions ()
  528. {
  529. register int i;
  530. if (undobuf.num_undo > 1)
  531. {
  532. for (i = undobuf.num_undo - 1; i >= 1; i--)
  533. *undobuf.undo[i].where = copy_rtx (*undobuf.undo[i].where);
  534. }
  535. }
  536. /* Throughout X, replace FROM with TO, and return the result.
  537. The result is TO if X is FROM;
  538. otherwise the result is X, but its contents may have been modified.
  539. If they were modified, a record was made in undobuf so that
  540. undo_all will (among other things) return X to its original state.
  541. If the number of changes necessary is too much to record to undo,
  542. the excess changes are not made, so the result is invalid.
  543. The changes already made can still be undone.
  544. undobuf.num_undo is incremented for such changes, so by testing that
  545. the caller can tell whether the result is valid.
  546. `n_occurrences' is incremented each time FROM is replaced. */
  547. static rtx
  548. subst (x, from, to)
  549. register rtx x, from, to;
  550. {
  551. register char *fmt;
  552. register int len, i;
  553. register enum rtx_code code;
  554. char was_replaced[2];
  555. #define SUBST(INTO, NEWVAL) \
  556. do { if (undobuf.num_undo < MAX_UNDO) \
  557. { \
  558. undobuf.undo[undobuf.num_undo].where = &INTO; \
  559. undobuf.undo[undobuf.num_undo].old_contents = INTO; \
  560. INTO = NEWVAL; \
  561. } \
  562. undobuf.num_undo++; } while (0)
  563. /* FAKE_EXTEND_SAFE_P (MODE, FROM) is 1 if (subreg:MODE FROM 0) is a safe
  564. replacement for (zero_extend:MODE FROM) or (sign_extend:MODE FROM).
  565. If it is 0, that cannot be done because it might cause a badly aligned
  566. memory reference. */
  567. #ifndef STRICT_ALIGNMENT
  568. #define FAKE_EXTEND_SAFE_P(MODE, FROM) \
  569. (GET_CODE (FROM) == REG || \
  570. (GET_CODE (FROM) == MEM \
  571. && offsetable_address_p ((MODE), XEXP ((FROM), 0)) \
  572. && ! mode_dependent_address_p ((XEXP ((FROM), 0)))))
  573. #else
  574. #define FAKE_EXTEND_SAFE_P(MODE, FROM) (GET_CODE (FROM) == REG)
  575. #endif
  576. if (x == from)
  577. return to;
  578. /* It is possible to have a subexpression appear twice in the insn.
  579. Suppose that FROM is a register that appears within TO.
  580. Then, after that subexpression has been scanned once by `subst',
  581. the second time it is scanned, TO may be found. If we were
  582. to scan TO here, we would find FROM within it and create a
  583. self-referent rtl structure which is completely wrong. */
  584. if (x == to)
  585. return to;
  586. code = GET_CODE (x);
  587. /* A little bit of algebraic simplification here. */
  588. switch (code)
  589. {
  590. /* This case has no effect except to speed things up. */
  591. case REG:
  592. case CONST_INT:
  593. case CONST:
  594. case SYMBOL_REF:
  595. case LABEL_REF:
  596. case PC:
  597. case CC0:
  598. return x;
  599. }
  600. was_replaced[0] = 0;
  601. was_replaced[1] = 0;
  602. len = GET_RTX_LENGTH (code);
  603. fmt = GET_RTX_FORMAT (code);
  604. /* Don't replace FROM where it is being stored in rather than used. */
  605. if (code == SET && SET_DEST (x) == from)
  606. fmt = "ie";
  607. if (code == SET && GET_CODE (SET_DEST (x)) == SUBREG
  608. && SUBREG_REG (SET_DEST (x)) == from)
  609. fmt = "ie";
  610. for (i = 0; i < len; i++)
  611. {
  612. if (fmt[i] == 'E')
  613. {
  614. register int j;
  615. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  616. {
  617. register rtx new;
  618. if (XVECEXP (x, i, j) == from)
  619. new = to, n_occurrences++;
  620. else
  621. new = subst (XVECEXP (x, i, j), from, to);
  622. if (new != XVECEXP (x, i, j))
  623. SUBST (XVECEXP (x, i, j), new);
  624. }
  625. }
  626. else if (fmt[i] == 'e')
  627. {
  628. register rtx new;
  629. if (XEXP (x, i) == from)
  630. {
  631. new = to;
  632. n_occurrences++;
  633. if (i < 2)
  634. was_replaced[i] = 1;
  635. }
  636. else
  637. new = subst (XEXP (x, i), from, to);
  638. if (new != XEXP (x, i))
  639. SUBST (XEXP (x, i), new);
  640. }
  641. }
  642. /* A little bit of algebraic simplification here. */
  643. switch (code)
  644. {
  645. case SUBREG:
  646. /* Changing mode twice with SUBREG => just change it once,
  647. or not at all if changing back to starting mode. */
  648. if (SUBREG_REG (x) == to
  649. && GET_CODE (to) == SUBREG
  650. && SUBREG_WORD (x) == 0
  651. && SUBREG_WORD (to) == 0)
  652. {
  653. if (GET_MODE (x) == GET_MODE (SUBREG_REG (to)))
  654. return SUBREG_REG (to);
  655. SUBST (SUBREG_REG (x), SUBREG_REG (to));
  656. }
  657. break;
  658. case NOT:
  659. case NEG:
  660. /* Don't let substitution introduce double-negatives. */
  661. if (was_replaced[0]
  662. && GET_CODE (to) == code)
  663. return XEXP (to, 0);
  664. break;
  665. case FLOAT_TRUNCATE:
  666. /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
  667. if (was_replaced[0]
  668. && GET_CODE (to) == FLOAT_EXTEND
  669. && GET_MODE (XEXP (to, 0)) == GET_MODE (x))
  670. return XEXP (to, 0);
  671. break;
  672. case PLUS:
  673. /* In (plus <foo> (ashift <bar> <n>))
  674. change the shift to a multiply so we can recognize
  675. scaled indexed addresses. */
  676. if ((was_replaced[0]
  677. || was_replaced[1])
  678. && GET_CODE (to) == ASHIFT
  679. && GET_CODE (XEXP (to, 1)) == CONST_INT
  680. && INTVAL (XEXP (to, 1)) < HOST_BITS_PER_INT)
  681. {
  682. rtx temp;
  683. if (!undobuf.storage)
  684. undobuf.storage = (char *) oballoc (0);
  685. temp = gen_rtx (MULT, GET_MODE (to),
  686. XEXP (to, 0),
  687. gen_rtx (CONST_INT, VOIDmode,
  688. 1 << INTVAL (XEXP (to, 1))));
  689. if (was_replaced[0])
  690. SUBST (XEXP (x, 0), temp);
  691. else
  692. SUBST (XEXP (x, 1), temp);
  693. }
  694. /* (plus X (neg Y)) becomes (minus X Y). */
  695. if (GET_CODE (XEXP (x, 1)) == NEG)
  696. {
  697. if (!undobuf.storage)
  698. undobuf.storage = (char *) oballoc (0);
  699. return gen_rtx (MINUS, GET_MODE (x),
  700. XEXP (x, 0), XEXP (XEXP (x, 1), 0));
  701. }
  702. /* (plus (neg X) Y) becomes (minus Y X). */
  703. if (GET_CODE (XEXP (x, 0)) == NEG)
  704. {
  705. if (!undobuf.storage)
  706. undobuf.storage = (char *) oballoc (0);
  707. return gen_rtx (MINUS, GET_MODE (x),
  708. XEXP (x, 1), XEXP (XEXP (x, 0), 0));
  709. }
  710. /* (plus (plus x c1) c2) => (plus x c1+c2) */
  711. if (GET_CODE (XEXP (x, 1)) == CONST_INT
  712. && GET_CODE (XEXP (x, 0)) == PLUS
  713. && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
  714. {
  715. int sum = (INTVAL (XEXP (x, 1))
  716. + INTVAL (XEXP (XEXP (x, 0), 1)));
  717. if (sum == 0)
  718. return XEXP (XEXP (x, 0), 0);
  719. if (!undobuf.storage)
  720. undobuf.storage = (char *) oballoc (0);
  721. SUBST (XEXP (x, 1), gen_rtx (CONST_INT, VOIDmode, sum));
  722. SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  723. break;
  724. }
  725. /* If we have something (putative index) being added to a sum,
  726. associate it so that any constant term is outermost.
  727. That's because that's the way indexed addresses are
  728. now supposed to appear. */
  729. if (((was_replaced[0] && GET_CODE (XEXP (x, 1)) == PLUS)
  730. || (was_replaced[1] && GET_CODE (XEXP (x, 0)) == PLUS))
  731. ||
  732. ((was_replaced[0] || was_replaced[1])
  733. && GET_CODE (to) == PLUS))
  734. {
  735. rtx offset = 0, base, index;
  736. if (GET_CODE (to) != PLUS)
  737. {
  738. index = to;
  739. base = was_replaced[0] ? XEXP (x, 1) : XEXP (x, 0);
  740. }
  741. else
  742. {
  743. index = was_replaced[0] ? XEXP (x, 1) : XEXP (x, 0);
  744. base = to;
  745. }
  746. if (CONSTANT_ADDRESS_P (XEXP (base, 0)))
  747. {
  748. offset = XEXP (base, 0);
  749. base = XEXP (base, 1);
  750. }
  751. else if (CONSTANT_ADDRESS_P (XEXP (base, 1)))
  752. {
  753. offset = XEXP (base, 1);
  754. base = XEXP (base, 0);
  755. }
  756. if (offset != 0)
  757. {
  758. if (!undobuf.storage)
  759. undobuf.storage = (char *) oballoc (0);
  760. if (GET_CODE (offset) == CONST_INT)
  761. return plus_constant (gen_rtx (PLUS, GET_MODE (index),
  762. base, index),
  763. INTVAL (offset));
  764. if (GET_CODE (index) == CONST_INT)
  765. return plus_constant (gen_rtx (PLUS, GET_MODE (offset),
  766. base, offset),
  767. INTVAL (index));
  768. return gen_rtx (PLUS, GET_MODE (index),
  769. gen_rtx (PLUS, GET_MODE (index),
  770. base, index),
  771. offset);
  772. }
  773. }
  774. break;
  775. case MINUS:
  776. /* Can simplify (minus:VOIDmode (zero/sign_extend FOO) CONST)
  777. (which is a compare instruction, not a subtract instruction)
  778. to (minus FOO CONST) if CONST fits in FOO's mode
  779. and we are only testing equality.
  780. In fact, this is valid for zero_extend if what follows is an
  781. unsigned comparison, and for sign_extend with a signed comparison. */
  782. if (GET_MODE (x) == VOIDmode
  783. && was_replaced[0]
  784. && (GET_CODE (to) == ZERO_EXTEND || GET_CODE (to) == SIGN_EXTEND)
  785. && next_insn_tests_no_inequality (subst_insn)
  786. && GET_CODE (XEXP (x, 1)) == CONST_INT
  787. /* This is overly cautious by one bit, but saves worrying about
  788. whether it is zero-extension or sign extension. */
  789. && ((unsigned) INTVAL (XEXP (x, 1))
  790. < (1 << (GET_MODE_BITSIZE (GET_MODE (XEXP (to, 0))) - 1))))
  791. SUBST (XEXP (x, 0), XEXP (to, 0));
  792. break;
  793. case EQ:
  794. case NE:
  795. /* If comparing a subreg against zero, discard the subreg. */
  796. if (was_replaced[0]
  797. && GET_CODE (to) == SUBREG
  798. && SUBREG_WORD (to) == 0
  799. && XEXP (x, 1) == const0_rtx)
  800. SUBST (XEXP (x, 0), SUBREG_REG (to));
  801. /* If comparing a ZERO_EXTRACT against zero,
  802. canonicalize to a SIGN_EXTRACT,
  803. since the two are equivalent here. */
  804. if (was_replaced[0]
  805. && GET_CODE (to) == ZERO_EXTRACT
  806. && XEXP (x, 1) == const0_rtx)
  807. {
  808. if (!undobuf.storage)
  809. undobuf.storage = (char *) oballoc (0);
  810. SUBST (XEXP (x, 0),
  811. gen_rtx (SIGN_EXTRACT, GET_MODE (to),
  812. XEXP (to, 0), XEXP (to, 1),
  813. XEXP (to, 2)));
  814. }
  815. /* If we are putting (ASHIFT 1 x) into (EQ (AND ... y) 0),
  816. arrange to return (EQ (SIGN_EXTRACT y 1 x) 0),
  817. which is what jump-on-bit instructions are written with. */
  818. else if (XEXP (x, 1) == const0_rtx
  819. && GET_CODE (XEXP (x, 0)) == AND
  820. && (XEXP (XEXP (x, 0), 0) == to
  821. || XEXP (XEXP (x, 0), 1) == to)
  822. && GET_CODE (to) == ASHIFT
  823. && XEXP (to, 0) == const1_rtx)
  824. {
  825. register rtx y = XEXP (XEXP (x, 0),
  826. XEXP (XEXP (x, 0), 0) == to);
  827. if (!undobuf.storage)
  828. undobuf.storage = (char *) oballoc (0);
  829. SUBST (XEXP (x, 0),
  830. gen_rtx (SIGN_EXTRACT, GET_MODE (to),
  831. y,
  832. const1_rtx, XEXP (to, 1)));
  833. }
  834. break;
  835. case ZERO_EXTEND:
  836. if (was_replaced[0]
  837. && GET_CODE (to) == ZERO_EXTEND)
  838. SUBST (XEXP (x, 0), XEXP (to, 0));
  839. /* Zero-extending the result of an and with a constant can be done
  840. with a wider and. */
  841. if (was_replaced[0]
  842. && GET_CODE (to) == AND
  843. && GET_CODE (XEXP (to, 1)) == CONST_INT
  844. && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  845. /* Avoid getting wrong result if the constant has high bits set
  846. that are irrelevant in the narrow mode where it is being used. */
  847. && 0 == (INTVAL (XEXP (to, 1))
  848. & ~ GET_MODE_MASK (GET_MODE (to))))
  849. {
  850. if (!undobuf.storage)
  851. undobuf.storage = (char *) oballoc (0);
  852. return gen_rtx (AND, GET_MODE (x),
  853. gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  854. XEXP (to, 1));
  855. }
  856. /* Change (zero_extend:M (subreg:N (zero_extract:M ...) 0))
  857. to (zero_extract:M ...) if the field extracted fits in mode N. */
  858. if (GET_CODE (XEXP (x, 0)) == SUBREG
  859. && GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTRACT
  860. && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
  861. && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
  862. <= GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
  863. {
  864. return XEXP (XEXP (x, 0), 0);
  865. }
  866. break;
  867. case SIGN_EXTEND:
  868. if (was_replaced[0]
  869. && GET_CODE (to) == SIGN_EXTEND)
  870. SUBST (XEXP (x, 0), XEXP (to, 0));
  871. /* Sign-extending the result of an and with a constant can be done
  872. with a wider and, provided the high bit of the constant is 0. */
  873. if (was_replaced[0]
  874. && GET_CODE (to) == AND
  875. && GET_CODE (XEXP (to, 1)) == CONST_INT
  876. && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  877. && ((INTVAL (XEXP (to, 1))
  878. & (-1 << (GET_MODE_BITSIZE (GET_MODE (to)) - 1)))
  879. == 0))
  880. {
  881. if (!undobuf.storage)
  882. undobuf.storage = (char *) oballoc (0);
  883. return gen_rtx (AND, GET_MODE (x),
  884. gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  885. XEXP (to, 1));
  886. }
  887. break;
  888. case SET:
  889. /* In (set (zero-extract <x> <n> <y>) (and <foo> <(2**n-1) | anything>))
  890. the `and' can be deleted. This can happen when storing a bit
  891. that came from a set-flag insn followed by masking to one bit. */
  892. if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  893. && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  894. && was_replaced[1]
  895. && GET_CODE (to) == AND
  896. && GET_CODE (XEXP (to, 1)) == CONST_INT
  897. && 0 == (((1 << INTVAL (XEXP (XEXP (x, 0), 1))) - 1)
  898. & ~ INTVAL (XEXP (to, 1))))
  899. {
  900. SUBST (XEXP (x, 1), XEXP (to, 0));
  901. }
  902. /* In (set (zero-extract <x> <n> <y>)
  903. (subreg (and <foo> <(2**n-1) | anything>)))
  904. the `and' can be deleted. */
  905. if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  906. && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  907. && GET_CODE (XEXP (x, 1)) == SUBREG
  908. && SUBREG_WORD (XEXP (x, 1)) == 0
  909. && GET_CODE (SUBREG_REG (XEXP (x, 1))) == AND
  910. && GET_CODE (XEXP (SUBREG_REG (XEXP (x, 1)), 1)) == CONST_INT
  911. && 0 == (((1 << INTVAL (XEXP (XEXP (x, 0), 1))) - 1)
  912. & ~ INTVAL (XEXP (SUBREG_REG (XEXP (x, 1)), 1))))
  913. {
  914. SUBST (SUBREG_REG (XEXP (x, 1)), XEXP (SUBREG_REG (XEXP (x, 1)), 0));
  915. }
  916. /* (set (zero_extract ...) (and/or/xor (zero_extract ...) const)),
  917. if both zero_extracts have the byte size and position,
  918. can be changed to avoid the byte extracts. */
  919. if ((GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  920. || GET_CODE (XEXP (x, 0)) == SIGN_EXTRACT)
  921. && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  922. && (GET_CODE (XEXP (x, 1)) == AND
  923. || GET_CODE (XEXP (x, 1)) == IOR
  924. || GET_CODE (XEXP (x, 1)) == XOR)
  925. && (GET_CODE (XEXP (XEXP (x, 1), 0)) == ZERO_EXTRACT
  926. || GET_CODE (XEXP (XEXP (x, 1), 0)) == SIGN_EXTRACT)
  927. && rtx_equal_p (XEXP (XEXP (XEXP (x, 1), 0), 1),
  928. XEXP (XEXP (x, 0), 1))
  929. && rtx_equal_p (XEXP (XEXP (XEXP (x, 1), 0), 2),
  930. XEXP (XEXP (x, 0), 2))
  931. && GET_CODE (XEXP (XEXP (x, 1), 0)) == GET_CODE (XEXP (x, 0))
  932. && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
  933. {
  934. #ifdef BITS_BIG_ENDIAN
  935. int shiftcount
  936. = GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
  937. - INTVAL (XEXP (XEXP (x, 0), 1)) - INTVAL (XEXP (XEXP (x, 0), 2));
  938. #else
  939. int shiftcount
  940. = INTVAL (XEXP (XEXP (x, 0), 2));
  941. #endif
  942. if (!undobuf.storage)
  943. undobuf.storage = (char *) oballoc (0);
  944. return
  945. gen_rtx (SET, VOIDmode,
  946. XEXP (XEXP (x, 0), 0),
  947. gen_rtx (GET_CODE (XEXP (x, 1)),
  948. GET_MODE (XEXP (XEXP (x, 0), 0)),
  949. XEXP (XEXP (XEXP (x, 1), 0), 0),
  950. gen_rtx (CONST_INT, VOIDmode,
  951. (INTVAL (XEXP (XEXP (x, 1), 1))
  952. << shiftcount)
  953. + (GET_CODE (XEXP (x, 1)) == AND
  954. ? (1 << shiftcount) - 1
  955. : 0))));
  956. }
  957. break;
  958. case AND:
  959. if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  960. {
  961. rtx tem = simplify_and_const_int (x, to);
  962. if (tem)
  963. return tem;
  964. }
  965. break;
  966. case FLOAT:
  967. /* (float (sign_extend <X>)) = (float <X>). */
  968. if (was_replaced[0]
  969. && GET_CODE (to) == SIGN_EXTEND)
  970. SUBST (XEXP (x, 0), XEXP (to, 0));
  971. break;
  972. case ZERO_EXTRACT:
  973. /* (ZERO_EXTRACT (TRUNCATE x)...)
  974. can become (ZERO_EXTRACT x ...). */
  975. if (was_replaced[0]
  976. && GET_CODE (to) == TRUNCATE)
  977. {
  978. #ifdef BITS_BIG_ENDIAN
  979. if (GET_CODE (XEXP (x, 2)) == CONST_INT)
  980. {
  981. if (!undobuf.storage)
  982. undobuf.storage = (char *) oballoc (0);
  983. /* On a big-endian machine, must increment the bit-number
  984. since sign bit is farther away in the pre-truncated value. */
  985. return gen_rtx (ZERO_EXTRACT, GET_MODE (x),
  986. XEXP (to, 0),
  987. XEXP (x, 1),
  988. gen_rtx (CONST_INT, VOIDmode,
  989. (INTVAL (XEXP (x, 2))
  990. + GET_MODE_BITSIZE (GET_MODE (XEXP (to, 0)))
  991. - GET_MODE_BITSIZE (GET_MODE (to)))));
  992. }
  993. #else
  994. SUBST (XEXP (x, 0), XEXP (to, 0));
  995. #endif
  996. }
  997. /* Extracting a single bit from the result of a shift:
  998. see which bit it was before the shift and extract that directly. */
  999. if (was_replaced[0]
  1000. && (GET_CODE (to) == ASHIFTRT || GET_CODE (to) == LSHIFTRT
  1001. || GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1002. && GET_CODE (XEXP (to, 1)) == CONST_INT
  1003. && XEXP (x, 1) == const1_rtx
  1004. && GET_CODE (XEXP (x, 2)) == CONST_INT)
  1005. {
  1006. int shift = INTVAL (XEXP (to, 1));
  1007. int newpos;
  1008. if (GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1009. shift = - shift;
  1010. #ifdef BITS_BIG_ENDIAN
  1011. shift = - shift;
  1012. #endif
  1013. newpos = INTVAL (XEXP (x, 2)) + shift;
  1014. if (newpos >= 0 &&
  1015. newpos < GET_MODE_BITSIZE (GET_MODE (to)))
  1016. {
  1017. if (!undobuf.storage)
  1018. undobuf.storage = (char *) oballoc (0);
  1019. return gen_rtx (ZERO_EXTRACT, GET_MODE (x),
  1020. XEXP (to, 0), const1_rtx,
  1021. gen_rtx (CONST_INT, VOIDmode, newpos));
  1022. }
  1023. }
  1024. break;
  1025. case LSHIFTRT:
  1026. case ASHIFTRT:
  1027. case ROTATE:
  1028. case ROTATERT:
  1029. #ifdef SHIFT_COUNT_TRUNCATED
  1030. /* (lshift <X> (sign_extend <Y>)) = (lshift <X> <Y>) (most machines).
  1031. True for all kinds of shifts and also for zero_extend. */
  1032. if (was_replaced[1]
  1033. && (GET_CODE (to) == SIGN_EXTEND
  1034. || GET_CODE (to) == ZERO_EXTEND)
  1035. && FAKE_EXTEND_SAFE_P (GET_MODE (to), XEXP (to, 0)))
  1036. {
  1037. if (!undobuf.storage)
  1038. undobuf.storage = (char *) oballoc (0);
  1039. SUBST (XEXP (x, 1),
  1040. /* This is a perverse SUBREG, wider than its base. */
  1041. gen_lowpart_for_combine (GET_MODE (to), XEXP (to, 0)));
  1042. }
  1043. #endif
  1044. /* Two shifts in a row of same kind
  1045. in same direction with constant counts
  1046. may be combined. */
  1047. if (was_replaced[0]
  1048. && GET_CODE (to) == GET_CODE (x)
  1049. && GET_CODE (XEXP (x, 1)) == CONST_INT
  1050. && GET_CODE (XEXP (to, 1)) == CONST_INT
  1051. && INTVAL (XEXP (to, 1)) > 0
  1052. && INTVAL (XEXP (x, 1)) > 0
  1053. && (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (to, 1))
  1054. < GET_MODE_BITSIZE (GET_MODE (x))))
  1055. {
  1056. if (!undobuf.storage)
  1057. undobuf.storage = (char *) oballoc (0);
  1058. return gen_rtx (GET_CODE (x), GET_MODE (x),
  1059. XEXP (to, 0),
  1060. gen_rtx (CONST_INT, VOIDmode,
  1061. INTVAL (XEXP (x, 1))
  1062. + INTVAL (XEXP (to, 1))));
  1063. }
  1064. break;
  1065. case LSHIFT:
  1066. case ASHIFT:
  1067. #ifdef SHIFT_COUNT_TRUNCATED
  1068. /* (lshift <X> (sign_extend <Y>)) = (lshift <X> <Y>) (most machines).
  1069. True for all kinds of shifts and also for zero_extend. */
  1070. if (was_replaced[1]
  1071. && (GET_CODE (to) == SIGN_EXTEND
  1072. || GET_CODE (to) == ZERO_EXTEND))
  1073. {
  1074. if (!undobuf.storage)
  1075. undobuf.storage = (char *) oballoc (0);
  1076. SUBST (XEXP (x, 1), gen_rtx (SUBREG, GET_MODE (to), XEXP (to, 0), 0));
  1077. }
  1078. #endif
  1079. /* (lshift (and (lshiftrt <foo> <X>) <Y>) <X>)
  1080. happens copying between bit fields in similar structures.
  1081. It can be replaced by one and instruction.
  1082. It does not matter whether the shifts are logical or arithmetic. */
  1083. if (GET_CODE (XEXP (x, 0)) == AND
  1084. && GET_CODE (XEXP (x, 1)) == CONST_INT
  1085. && INTVAL (XEXP (x, 1)) > 0
  1086. && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1087. && XEXP (XEXP (x, 0), 0) == to
  1088. && (GET_CODE (to) == LSHIFTRT
  1089. || GET_CODE (to) == ASHIFTRT)
  1090. #if 0
  1091. /* I now believe this restriction is unnecessary.
  1092. The outer shift will discard those bits in any case, right? */
  1093. /* If inner shift is arithmetic, either it shifts left or
  1094. the bits it shifts the sign into are zeroed by the and. */
  1095. && (INTVAL (XEXP (x, 1)) < 0
  1096. || ((unsigned) INTVAL (XEXP (XEXP (x, 0), 1))
  1097. < 1 << (GET_MODE_BITSIZE (GET_MODE (x))
  1098. - INTVAL (XEXP (x, 0)))))
  1099. #endif
  1100. && GET_CODE (XEXP (to, 1)) == CONST_INT
  1101. && INTVAL (XEXP (x, 1)) == INTVAL (XEXP (to, 1)))
  1102. {
  1103. if (!undobuf.storage)
  1104. undobuf.storage = (char *) oballoc (0);
  1105. /* The constant in the new `and' is <Y> << <X>
  1106. but clear out all bits that don't belong in our mode. */
  1107. return gen_rtx (AND, GET_MODE (x), XEXP (to, 0),
  1108. gen_rtx (CONST_INT, VOIDmode,
  1109. (GET_MODE_MASK (GET_MODE (x))
  1110. & ((GET_MODE_MASK (GET_MODE (x))
  1111. & INTVAL (XEXP (XEXP (x, 0), 1)))
  1112. << INTVAL (XEXP (x, 1))))));
  1113. }
  1114. /* Two shifts in a row in same direction with constant counts
  1115. may be combined. */
  1116. if (was_replaced[0]
  1117. && (GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1118. && GET_CODE (XEXP (x, 1)) == CONST_INT
  1119. && GET_CODE (XEXP (to, 1)) == CONST_INT
  1120. && INTVAL (XEXP (to, 1)) > 0
  1121. && INTVAL (XEXP (x, 1)) > 0
  1122. && (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (to, 1))
  1123. < GET_MODE_BITSIZE (GET_MODE (x))))
  1124. {
  1125. if (!undobuf.storage)
  1126. undobuf.storage = (char *) oballoc (0);
  1127. return gen_rtx (GET_CODE (x), GET_MODE (x),
  1128. XEXP (to, 0),
  1129. gen_rtx (CONST_INT, VOIDmode,
  1130. INTVAL (XEXP (x, 1))
  1131. + INTVAL (XEXP (to, 1))));
  1132. }
  1133. /* (ashift (ashiftrt <foo> <X>) <X>)
  1134. (or, on some machines, (ashift (ashift <foo> <-X>) <X>) instead)
  1135. happens if you divide by 2**N and then multiply by 2**N.
  1136. It can be replaced by one `and' instruction.
  1137. It does not matter whether the shifts are logical or arithmetic. */
  1138. if (GET_CODE (XEXP (x, 1)) == CONST_INT
  1139. && INTVAL (XEXP (x, 1)) > 0
  1140. && was_replaced[0]
  1141. && (((GET_CODE (to) == LSHIFTRT || GET_CODE (to) == ASHIFTRT)
  1142. && GET_CODE (XEXP (to, 1)) == CONST_INT
  1143. && INTVAL (XEXP (x, 1)) == INTVAL (XEXP (to, 1)))
  1144. ||
  1145. ((GET_CODE (to) == LSHIFT || GET_CODE (to) == ASHIFT)
  1146. && GET_CODE (XEXP (to, 1)) == CONST_INT
  1147. && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (to, 1)))))
  1148. {
  1149. if (!undobuf.storage)
  1150. undobuf.storage = (char *) oballoc (0);
  1151. /* The constant in the new `and' is -1 << <X>
  1152. but clear out all bits that don't belong in our mode. */
  1153. return gen_rtx (AND, GET_MODE (x), XEXP (to, 0),
  1154. gen_rtx (CONST_INT, VOIDmode,
  1155. (GET_MODE_MASK (GET_MODE (x))
  1156. & (GET_MODE_MASK (GET_MODE (x))
  1157. << INTVAL (XEXP (x, 1))))));
  1158. }
  1159. }
  1160. return x;
  1161. }
  1162. /* This is the AND case of the function subst. */
  1163. static rtx
  1164. simplify_and_const_int (x, to)
  1165. rtx x, to;
  1166. {
  1167. register rtx varop = XEXP (x, 0);
  1168. register int constop = INTVAL (XEXP (x, 1));
  1169. /* (and (subreg (and <foo> <constant>) 0) <constant>)
  1170. results from an andsi followed by an andqi,
  1171. which happens frequently when storing bit-fields
  1172. on something whose result comes from an andsi. */
  1173. if (GET_CODE (varop) == SUBREG
  1174. && XEXP (varop, 0) == to
  1175. && subreg_lowpart_p (varop)
  1176. && GET_CODE (to) == AND
  1177. && GET_CODE (XEXP (to, 1)) == CONST_INT
  1178. /* Verify that the result of the outer `and'
  1179. is not affected by any bits not defined in the inner `and'.
  1180. True if the outer mode is narrower, or if the outer constant
  1181. masks to zero all the bits that the inner mode doesn't have. */
  1182. && (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (GET_MODE (to))
  1183. || (constop & ~ GET_MODE_MASK (GET_MODE (to))) == 0))
  1184. {
  1185. if (!undobuf.storage)
  1186. undobuf.storage = (char *) oballoc (0);
  1187. return gen_rtx (AND, GET_MODE (x),
  1188. gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1189. gen_rtx (CONST_INT, VOIDmode,
  1190. constop
  1191. /* Remember that the bits outside that mode
  1192. are not being changed, so the effect
  1193. is as if they were all 1. */
  1194. & INTVAL (XEXP (to, 1))));
  1195. }
  1196. /* (and:SI (zero_extract:SI ...) <constant>)
  1197. results from an andsi following a byte-fetch on risc machines.
  1198. When the constant includes all bits extracted, eliminate the `and'. */
  1199. if (GET_CODE (varop) == ZERO_EXTRACT
  1200. && GET_CODE (XEXP (varop, 1)) == CONST_INT
  1201. /* The `and' must not clear any bits that the extract can give. */
  1202. && (~ constop & ((1 << INTVAL (XEXP (varop, 1))) - 1)) == 0)
  1203. return varop;
  1204. /* (and (zero_extend <foo>) <constant>)
  1205. often results from storing in a bit-field something
  1206. that was calculated as a short. Replace with a single `and'
  1207. in whose constant all bits not in <foo>'s mode are zero. */
  1208. if (varop == to
  1209. && GET_CODE (to) == ZERO_EXTEND
  1210. && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
  1211. {
  1212. if (!undobuf.storage)
  1213. undobuf.storage = (char *) oballoc (0);
  1214. return gen_rtx (AND, GET_MODE (x),
  1215. /* This is a perverse SUBREG, wider than its base. */
  1216. gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1217. gen_rtx (CONST_INT, VOIDmode,
  1218. constop & GET_MODE_MASK (GET_MODE (XEXP (to, 0)))));
  1219. }
  1220. /* (and (sign_extend <foo>) <constant>)
  1221. can be replaced with (and (subreg <foo>) <constant>)
  1222. if <constant> is narrower than <foo>'s mode,
  1223. or with (zero_extend <foo>) if <constant> is a mask for that mode. */
  1224. if (varop == to
  1225. && GET_CODE (to) == SIGN_EXTEND
  1226. && ((unsigned) constop <= GET_MODE_MASK (GET_MODE (XEXP (to, 0))))
  1227. && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
  1228. {
  1229. if (!undobuf.storage)
  1230. undobuf.storage = (char *) oballoc (0);
  1231. if (constop == GET_MODE_MASK (GET_MODE (XEXP (to, 0))))
  1232. return gen_rtx (ZERO_EXTEND, GET_MODE (x), XEXP (to, 0));
  1233. return gen_rtx (AND, GET_MODE (x),
  1234. /* This is a perverse SUBREG, wider than its base. */
  1235. gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1236. XEXP (x, 1));
  1237. }
  1238. /* (and (and <foo> <constant>) <constant>)
  1239. comes from two and instructions in a row. */
  1240. if (varop == to
  1241. && GET_CODE (to) == AND
  1242. && GET_CODE (XEXP (to, 1)) == CONST_INT)
  1243. {
  1244. if (!undobuf.storage)
  1245. undobuf.storage = (char *) oballoc (0);
  1246. return gen_rtx (AND, GET_MODE (x),
  1247. XEXP (to, 0),
  1248. gen_rtx (CONST_INT, VOIDmode,
  1249. constop
  1250. & INTVAL (XEXP (to, 1))));
  1251. }
  1252. /* (and (ashiftrt (ashift FOO N) N) CONST)
  1253. may be simplified to (and FOO CONST) if CONST masks off the bits
  1254. changed by the two shifts. */
  1255. if (GET_CODE (varop) == ASHIFTRT
  1256. && GET_CODE (XEXP (varop, 1)) == CONST_INT
  1257. && XEXP (varop, 0) == to
  1258. && GET_CODE (to) == ASHIFT
  1259. && GET_CODE (XEXP (to, 1)) == CONST_INT
  1260. && INTVAL (XEXP (varop, 1)) == INTVAL (XEXP (to, 1))
  1261. && ((unsigned) constop >> INTVAL (XEXP (varop, 1))) == 0)
  1262. {
  1263. if (!undobuf.storage)
  1264. undobuf.storage = (char *) oballoc (0);
  1265. /* If CONST is a mask for the low byte,
  1266. change this into a zero-extend instruction
  1267. from just the low byte of FOO. */
  1268. if (constop == GET_MODE_MASK (QImode))
  1269. {
  1270. rtx temp = gen_lowpart_for_combine (QImode, XEXP (to, 0));
  1271. if (GET_CODE (temp) != CLOBBER)
  1272. return gen_rtx (ZERO_EXTEND, GET_MODE (x), temp);
  1273. }
  1274. return gen_rtx (AND, GET_MODE (x),
  1275. XEXP (to, 0), XEXP (x, 1));
  1276. }
  1277. /* (and x const) may be converted to (zero_extend (subreg x 0)). */
  1278. if (constop == GET_MODE_MASK (QImode))
  1279. {
  1280. if (!undobuf.storage)
  1281. undobuf.storage = (char *) oballoc (0);
  1282. return gen_rtx (ZERO_EXTEND, GET_MODE (x),
  1283. gen_rtx (SUBREG, QImode, varop, 0));
  1284. }
  1285. if (constop == GET_MODE_MASK (HImode))
  1286. {
  1287. if (!undobuf.storage)
  1288. undobuf.storage = (char *) oballoc (0);
  1289. return gen_rtx (ZERO_EXTEND, GET_MODE (x),
  1290. gen_rtx (SUBREG, HImode, varop, 0));
  1291. }
  1292. /* No simplification applies. */
  1293. return 0;
  1294. }
  1295. /* Like gen_lowpart but for use by combine. In combine it is not possible
  1296. to create any new pseudoregs. However, it is safe to create
  1297. invalid memory addresses, because combine will try to recognize
  1298. them and all they will do is make the combine attempt fail.
  1299. If for some reason this cannot do its job, an rtx
  1300. (clobber (const_int 0)) is returned.
  1301. An insn containing that will not be recognized. */
  1302. #undef gen_lowpart
  1303. static rtx
  1304. gen_lowpart_for_combine (mode, x)
  1305. enum machine_mode mode;
  1306. register rtx x;
  1307. {
  1308. if (GET_CODE (x) == SUBREG || GET_CODE (x) == REG)
  1309. return gen_lowpart (mode, x);
  1310. if (GET_MODE (x) == mode || x->volatil)
  1311. return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1312. if (GET_CODE (x) == MEM)
  1313. {
  1314. register int offset = 0;
  1315. #ifdef WORDS_BIG_ENDIAN
  1316. offset = (max (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
  1317. - max (GET_MODE_SIZE (mode), UNITS_PER_WORD));
  1318. #endif
  1319. #ifdef BYTES_BIG_ENDIAN
  1320. /* Adjust the address so that the address-after-the-data
  1321. is unchanged. */
  1322. offset -= (min (UNITS_PER_WORD, GET_MODE_SIZE (mode))
  1323. - min (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
  1324. #endif
  1325. return gen_rtx (MEM, mode, plus_constant (XEXP (x, 0),
  1326. offset));
  1327. }
  1328. else
  1329. return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1330. }
  1331. /* After substitution, if the resulting pattern looks like
  1332. (set (cc0) (and ...)) or (set (cc0) (lshiftrt ...)),
  1333. this function is called to simplify the
  1334. pattern into a bit-field operation if possible. */
  1335. static void
  1336. simplify_set_cc0_and (insn)
  1337. rtx insn;
  1338. {
  1339. register rtx value = XEXP (PATTERN (insn), 1);
  1340. register rtx op0 = XEXP (value, 0);
  1341. register rtx op1 = XEXP (value, 1);
  1342. int offset = 0;
  1343. rtx var = 0;
  1344. rtx bitnum = 0;
  1345. int temp;
  1346. int unit;
  1347. rtx newpat;
  1348. if (GET_CODE (value) == AND)
  1349. {
  1350. op0 = XEXP (value, 0);
  1351. op1 = XEXP (value, 1);
  1352. }
  1353. else if (GET_CODE (value) == LSHIFTRT)
  1354. {
  1355. /* If there is no AND, but there is a shift that discards
  1356. all but the sign bit, we can pretend that the shift result
  1357. is ANDed with 1. Otherwise we cannot handle just a shift. */
  1358. if (GET_CODE (XEXP (value, 1)) == CONST_INT
  1359. && (INTVAL (XEXP (value, 1))
  1360. == GET_MODE_BITSIZE (GET_MODE (value)) - 1))
  1361. {
  1362. op0 = value;
  1363. op1 = const1_rtx;
  1364. }
  1365. else
  1366. return;
  1367. }
  1368. else
  1369. abort ();
  1370. /* Look for a constant power of 2 or a shifted 1
  1371. on either side of the AND. Set VAR to the other side.
  1372. Set BITNUM to the shift count of the 1 (as an rtx).
  1373. Or, if bit number is constant, set OFFSET to the bit number. */
  1374. switch (GET_CODE (op0))
  1375. {
  1376. case CONST_INT:
  1377. temp = exact_log2 (INTVAL (op0));
  1378. if (temp < 0)
  1379. return;
  1380. offset = temp;
  1381. var = op1;
  1382. break;
  1383. case ASHIFT:
  1384. case LSHIFT:
  1385. if (XEXP (op0, 0) == const1_rtx)
  1386. {
  1387. bitnum = XEXP (op0, 1);
  1388. var = op1;
  1389. }
  1390. }
  1391. if (var == 0)
  1392. switch (GET_CODE (op1))
  1393. {
  1394. case CONST_INT:
  1395. temp = exact_log2 (INTVAL (op1));
  1396. if (temp < 0)
  1397. return;
  1398. offset = temp;
  1399. var = op0;
  1400. break;
  1401. case ASHIFT:
  1402. case LSHIFT:
  1403. if (XEXP (op1, 0) == const1_rtx)
  1404. {
  1405. bitnum = XEXP (op1, 1);
  1406. var = op0;
  1407. }
  1408. }
  1409. /* If VAR is 0, we didn't find something recognizable. */
  1410. if (var == 0)
  1411. return;
  1412. if (!undobuf.storage)
  1413. undobuf.storage = (char *) oballoc (0);
  1414. /* If the bit position is currently exactly 0,
  1415. extract a right-shift from the variable portion. */
  1416. if (offset == 0
  1417. && (GET_CODE (var) == ASHIFTRT || GET_CODE (var) == LSHIFTRT))
  1418. {
  1419. bitnum = XEXP (var, 1);
  1420. var = XEXP (var, 0);
  1421. }
  1422. if (GET_CODE (var) == SUBREG && SUBREG_WORD (var) == 0)
  1423. var = SUBREG_REG (var);
  1424. /* Note that BITNUM and OFFSET are always little-endian thru here
  1425. even on a big-endian machine. */
  1426. #ifdef BITS_BIG_ENDIAN
  1427. unit = GET_MODE_BITSIZE (GET_MODE (var)) - 1;
  1428. if (bitnum != 0)
  1429. bitnum = gen_rtx (MINUS, SImode,
  1430. gen_rtx (CONST_INT, VOIDmode, unit), bitnum);
  1431. else
  1432. offset = unit - offset;
  1433. #endif
  1434. if (bitnum == 0)
  1435. bitnum = gen_rtx (CONST_INT, VOIDmode, offset);
  1436. newpat = gen_rtx (SET, VOIDmode, cc0_rtx,
  1437. gen_rtx (ZERO_EXTRACT, VOIDmode, var, const1_rtx, bitnum));
  1438. if (recog (newpat, insn) >= 0)
  1439. {
  1440. if (undobuf.num_undo < MAX_UNDO)
  1441. {
  1442. undobuf.undo[undobuf.num_undo].where = &XEXP (PATTERN (insn), 1);
  1443. undobuf.undo[undobuf.num_undo].old_contents = value;
  1444. XEXP (PATTERN (insn), 1) = XEXP (newpat, 1);
  1445. }
  1446. undobuf.num_undo++;
  1447. }
  1448. }
  1449. /* Update the records of when each REG was most recently set or killed
  1450. for the things done by INSN. This is the last thing done in processing
  1451. INSN in the combiner loop.
  1452. We update reg_last_set, reg_last_death, and also the similar information
  1453. mem_last_set (which insn most recently modified memory)
  1454. and last_call_cuid (which insn was the most recent subroutine call). */
  1455. static void
  1456. record_dead_and_set_regs (insn)
  1457. rtx insn;
  1458. {
  1459. register rtx link;
  1460. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1461. {
  1462. if (REG_NOTE_KIND (link) == REG_DEAD)
  1463. reg_last_death[REGNO (XEXP (link, 0))] = insn;
  1464. else if (REG_NOTE_KIND (link) == REG_INC)
  1465. reg_last_set[REGNO (XEXP (link, 0))] = insn;
  1466. }
  1467. if (GET_CODE (insn) == CALL_INSN)
  1468. last_call_cuid = mem_last_set = INSN_CUID (insn);
  1469. if (GET_CODE (PATTERN (insn)) == PARALLEL)
  1470. {
  1471. register int i;
  1472. for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  1473. {
  1474. register rtx elt = XVECEXP (PATTERN (insn), 0, i);
  1475. register enum rtx_code code = GET_CODE (elt);
  1476. if (code == SET || code == CLOBBER)
  1477. {
  1478. if (GET_CODE (XEXP (elt, 0)) == REG)
  1479. reg_last_set[REGNO (XEXP (elt, 0))] = insn;
  1480. if (GET_CODE (XEXP (elt, 0)) == SUBREG
  1481. && GET_CODE (SUBREG_REG (XEXP (elt, 0))) == REG)
  1482. reg_last_set[REGNO (SUBREG_REG (XEXP (elt, 0)))] = insn;
  1483. else if (GET_CODE (XEXP (elt, 0)) == MEM)
  1484. mem_last_set = INSN_CUID (insn);
  1485. }
  1486. }
  1487. }
  1488. else if (GET_CODE (PATTERN (insn)) == SET
  1489. || GET_CODE (PATTERN (insn)) == CLOBBER)
  1490. {
  1491. register rtx x = XEXP (PATTERN (insn), 0);
  1492. if (GET_CODE (x) == REG)
  1493. reg_last_set[REGNO (x)] = insn;
  1494. if (GET_CODE (x) == SUBREG
  1495. && GET_CODE (SUBREG_REG (x)) == REG)
  1496. reg_last_set[REGNO (SUBREG_REG (x))] = insn;
  1497. else if (GET_CODE (x) == MEM)
  1498. mem_last_set = INSN_CUID (insn);
  1499. }
  1500. }
  1501. /* Return nonzero if expression X refers to a REG or to memory
  1502. that is set in an instruction more recent than FROM_CUID. */
  1503. static int
  1504. use_crosses_set_p (x, from_cuid)
  1505. register rtx x;
  1506. int from_cuid;
  1507. {
  1508. register char *fmt;
  1509. register int i;
  1510. register enum rtx_code code = GET_CODE (x);
  1511. if (code == REG)
  1512. {
  1513. register int regno = REGNO (x);
  1514. return (reg_last_set[regno]
  1515. && INSN_CUID (reg_last_set[regno]) > from_cuid);
  1516. }
  1517. if (code == MEM && mem_last_set > from_cuid)
  1518. return 1;
  1519. fmt = GET_RTX_FORMAT (code);
  1520. for (i = GET_RTX_LENGTH (code); i >= 0; i--)
  1521. {
  1522. if (fmt[i] == 'E')
  1523. {
  1524. register int j;
  1525. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1526. if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
  1527. return 1;
  1528. }
  1529. else if (fmt[i] == 'e'
  1530. && use_crosses_set_p (XEXP (x, i), from_cuid))
  1531. return 1;
  1532. }
  1533. return 0;
  1534. }
  1535. /* Return nonzero if reg REGNO is marked as dying in INSN. */
  1536. int
  1537. regno_dead_p (regno, insn)
  1538. int regno;
  1539. rtx insn;
  1540. {
  1541. register rtx link;
  1542. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1543. if ((REG_NOTE_KIND (link) == REG_DEAD
  1544. || REG_NOTE_KIND (link) == REG_INC)
  1545. && REGNO (XEXP (link, 0)) == regno)
  1546. return 1;
  1547. return 0;
  1548. }
  1549. /* Remove register number REGNO from the dead registers list of INSN. */
  1550. static void
  1551. remove_death (regno, insn)
  1552. int regno;
  1553. rtx insn;
  1554. {
  1555. register rtx link, next;
  1556. while ((link = REG_NOTES (insn))
  1557. && REG_NOTE_KIND (link) == REG_DEAD
  1558. && REGNO (XEXP (link, 0)) == regno)
  1559. REG_NOTES (insn) = XEXP (link, 1);
  1560. if (link)
  1561. while (next = XEXP (link, 1))
  1562. {
  1563. if (REG_NOTE_KIND (next) == REG_DEAD
  1564. && REGNO (XEXP (next, 0)) == regno)
  1565. XEXP (link, 1) = XEXP (next, 1);
  1566. else
  1567. link = next;
  1568. }
  1569. }
  1570. /* Return nonzero if J is the first insn following I,
  1571. not counting labels, line numbers, etc.
  1572. We assume that J follows I. */
  1573. static int
  1574. adjacent_insns_p (i, j)
  1575. rtx i, j;
  1576. {
  1577. register rtx insn;
  1578. for (insn = NEXT_INSN (i); insn != j; insn = NEXT_INSN (insn))
  1579. if (GET_CODE (insn) == INSN
  1580. || GET_CODE (insn) == CALL_INSN
  1581. || GET_CODE (insn) == JUMP_INSN)
  1582. return 0;
  1583. return 1;
  1584. }
  1585. /* Concatenate the list of logical links of OINSN
  1586. into INSN's list of logical links.
  1587. Modifies OINSN destructively.
  1588. If ALL_LINKS is nonzero, move all the links that OINSN has.
  1589. Otherwise, move only those that point to insns that set regs
  1590. that die in the insn OINSN.
  1591. Other links are clobbered so that they are no longer effective. */
  1592. static void
  1593. add_links (insn, oinsn, all_links)
  1594. rtx insn, oinsn;
  1595. int all_links;
  1596. {
  1597. register rtx links = LOG_LINKS (oinsn);
  1598. if (! all_links)
  1599. {
  1600. rtx tail;
  1601. for (tail = links; tail; tail = XEXP (tail, 1))
  1602. {
  1603. rtx target = XEXP (tail, 0);
  1604. if (GET_CODE (target) != INSN
  1605. || GET_CODE (PATTERN (target)) != SET
  1606. || GET_CODE (SET_DEST (PATTERN (target))) != REG
  1607. || ! dead_or_set_p (oinsn, SET_DEST (PATTERN (target))))
  1608. /* OINSN is going to become a NOTE
  1609. so a link pointing there will have no effect. */
  1610. XEXP (tail, 0) = oinsn;
  1611. }
  1612. }
  1613. if (LOG_LINKS (insn) == 0)
  1614. LOG_LINKS (insn) = links;
  1615. else
  1616. {
  1617. register rtx next, prev = LOG_LINKS (insn);
  1618. while (next = XEXP (prev, 1))
  1619. prev = next;
  1620. XEXP (prev, 1) = links;
  1621. }
  1622. }
  1623. /* Concatenate the any elements of the list of reg-notes INCS
  1624. which are of type REG_INC
  1625. into INSN's list of reg-notes. */
  1626. static void
  1627. add_incs (insn, incs)
  1628. rtx insn, incs;
  1629. {
  1630. register rtx tail;
  1631. for (tail = incs; tail; tail = XEXP (tail, 1))
  1632. if (REG_NOTE_KIND (tail) == REG_INC)
  1633. REG_NOTES (insn)
  1634. = gen_rtx (EXPR_LIST, REG_INC, XEXP (tail, 0), REG_NOTES (insn));
  1635. }
  1636. /* For each register (hardware or pseudo) used within expression X,
  1637. if its death is in an instruction with cuid
  1638. between FROM_CUID (inclusive) and TO_INSN (exclusive),
  1639. mark it as dead in TO_INSN instead.
  1640. This is done when X is being merged by combination into TO_INSN. */
  1641. static void
  1642. move_deaths (x, from_cuid, to_insn)
  1643. rtx x;
  1644. int from_cuid;
  1645. rtx to_insn;
  1646. {
  1647. register char *fmt;
  1648. register int len, i;
  1649. register enum rtx_code code = GET_CODE (x);
  1650. if (code == REG)
  1651. {
  1652. register rtx where_dead = reg_last_death[REGNO (x)];
  1653. if (where_dead && INSN_CUID (where_dead) >= from_cuid
  1654. && INSN_CUID (where_dead) < INSN_CUID (to_insn))
  1655. {
  1656. remove_death (REGNO (x), reg_last_death[REGNO (x)]);
  1657. if (! dead_or_set_p (to_insn, x))
  1658. REG_NOTES (to_insn)
  1659. = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (to_insn));
  1660. }
  1661. return;
  1662. }
  1663. len = GET_RTX_LENGTH (code);
  1664. fmt = GET_RTX_FORMAT (code);
  1665. for (i = 0; i < len; i++)
  1666. {
  1667. if (fmt[i] == 'E')
  1668. {
  1669. register int j;
  1670. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1671. move_deaths (XVECEXP (x, i, j), from_cuid, to_insn);
  1672. }
  1673. else if (fmt[i] == 'e')
  1674. move_deaths (XEXP (x, i), from_cuid, to_insn);
  1675. }
  1676. }
  1677. void
  1678. dump_combine_stats (file)
  1679. char *file;
  1680. {
  1681. fprintf
  1682. (file,
  1683. ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n"
  1684. , combine_attempts, combine_merges, combine_extras, combine_successes);
  1685. }
  1686. void
  1687. dump_combine_total_stats (file)
  1688. char *file;
  1689. {
  1690. fprintf
  1691. (file,
  1692. "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
  1693. total_attempts, total_merges, total_extras, total_successes);
  1694. }