auto-inc-dec.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. /* Discovery of auto-inc and auto-dec instructions.
  2. Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "rtl.h"
  31. #include "tm_p.h"
  32. #include "hard-reg-set.h"
  33. #include "predict.h"
  34. #include "hashtab.h"
  35. #include "function.h"
  36. #include "dominance.h"
  37. #include "cfg.h"
  38. #include "cfgrtl.h"
  39. #include "basic-block.h"
  40. #include "insn-config.h"
  41. #include "regs.h"
  42. #include "flags.h"
  43. #include "except.h"
  44. #include "diagnostic-core.h"
  45. #include "recog.h"
  46. #include "statistics.h"
  47. #include "real.h"
  48. #include "fixed-value.h"
  49. #include "expmed.h"
  50. #include "dojump.h"
  51. #include "explow.h"
  52. #include "calls.h"
  53. #include "emit-rtl.h"
  54. #include "varasm.h"
  55. #include "stmt.h"
  56. #include "expr.h"
  57. #include "tree-pass.h"
  58. #include "df.h"
  59. #include "dbgcnt.h"
  60. #include "target.h"
  61. /* This pass was originally removed from flow.c. However there is
  62. almost nothing that remains of that code.
  63. There are (4) basic forms that are matched:
  64. (1) FORM_PRE_ADD
  65. a <- b + c
  66. ...
  67. *a
  68. becomes
  69. a <- b
  70. ...
  71. *(a += c) pre
  72. (2) FORM_PRE_INC
  73. a += c
  74. ...
  75. *a
  76. becomes
  77. *(a += c) pre
  78. (3) FORM_POST_ADD
  79. *a
  80. ...
  81. b <- a + c
  82. (For this case to be true, b must not be assigned or used between
  83. the *a and the assignment to b. B must also be a Pmode reg.)
  84. becomes
  85. b <- a
  86. ...
  87. *(b += c) post
  88. (4) FORM_POST_INC
  89. *a
  90. ...
  91. a <- a + c
  92. becomes
  93. *(a += c) post
  94. There are three types of values of c.
  95. 1) c is a constant equal to the width of the value being accessed by
  96. the pointer. This is useful for machines that have
  97. HAVE_PRE_INCREMENT, HAVE_POST_INCREMENT, HAVE_PRE_DECREMENT or
  98. HAVE_POST_DECREMENT defined.
  99. 2) c is a constant not equal to the width of the value being accessed
  100. by the pointer. This is useful for machines that have
  101. HAVE_PRE_MODIFY_DISP, HAVE_POST_MODIFY_DISP defined.
  102. 3) c is a register. This is useful for machines that have
  103. HAVE_PRE_MODIFY_REG, HAVE_POST_MODIFY_REG
  104. The is one special case: if a already had an offset equal to it +-
  105. its width and that offset is equal to -c when the increment was
  106. before the ref or +c if the increment was after the ref, then if we
  107. can do the combination but switch the pre/post bit. */
  108. #ifdef AUTO_INC_DEC
  109. enum form
  110. {
  111. FORM_PRE_ADD,
  112. FORM_PRE_INC,
  113. FORM_POST_ADD,
  114. FORM_POST_INC,
  115. FORM_last
  116. };
  117. /* The states of the second operands of mem refs and inc insns. If no
  118. second operand of the mem_ref was found, it is assumed to just be
  119. ZERO. SIZE is the size of the mode accessed in the memref. The
  120. ANY is used for constants that are not +-size or 0. REG is used if
  121. the forms are reg1 + reg2. */
  122. enum inc_state
  123. {
  124. INC_ZERO, /* == 0 */
  125. INC_NEG_SIZE, /* == +size */
  126. INC_POS_SIZE, /* == -size */
  127. INC_NEG_ANY, /* == some -constant */
  128. INC_POS_ANY, /* == some +constant */
  129. INC_REG, /* == some register */
  130. INC_last
  131. };
  132. /* The eight forms that pre/post inc/dec can take. */
  133. enum gen_form
  134. {
  135. NOTHING,
  136. SIMPLE_PRE_INC, /* ++size */
  137. SIMPLE_POST_INC, /* size++ */
  138. SIMPLE_PRE_DEC, /* --size */
  139. SIMPLE_POST_DEC, /* size-- */
  140. DISP_PRE, /* ++con */
  141. DISP_POST, /* con++ */
  142. REG_PRE, /* ++reg */
  143. REG_POST /* reg++ */
  144. };
  145. /* Tmp mem rtx for use in cost modeling. */
  146. static rtx mem_tmp;
  147. static enum inc_state
  148. set_inc_state (HOST_WIDE_INT val, int size)
  149. {
  150. if (val == 0)
  151. return INC_ZERO;
  152. if (val < 0)
  153. return (val == -size) ? INC_NEG_SIZE : INC_NEG_ANY;
  154. else
  155. return (val == size) ? INC_POS_SIZE : INC_POS_ANY;
  156. }
  157. /* The DECISION_TABLE that describes what form, if any, the increment
  158. or decrement will take. It is a three dimensional table. The first
  159. index is the type of constant or register found as the second
  160. operand of the inc insn. The second index is the type of constant
  161. or register found as the second operand of the memory reference (if
  162. no second operand exists, 0 is used). The third index is the form
  163. and location (relative to the mem reference) of inc insn. */
  164. static bool initialized = false;
  165. static enum gen_form decision_table[INC_last][INC_last][FORM_last];
  166. static void
  167. init_decision_table (void)
  168. {
  169. enum gen_form value;
  170. if (HAVE_PRE_INCREMENT || HAVE_PRE_MODIFY_DISP)
  171. {
  172. /* Prefer the simple form if both are available. */
  173. value = (HAVE_PRE_INCREMENT) ? SIMPLE_PRE_INC : DISP_PRE;
  174. decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
  175. decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_INC] = value;
  176. decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_ADD] = value;
  177. decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_INC] = value;
  178. }
  179. if (HAVE_POST_INCREMENT || HAVE_POST_MODIFY_DISP)
  180. {
  181. /* Prefer the simple form if both are available. */
  182. value = (HAVE_POST_INCREMENT) ? SIMPLE_POST_INC : DISP_POST;
  183. decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_ADD] = value;
  184. decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_INC] = value;
  185. decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_ADD] = value;
  186. decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_INC] = value;
  187. }
  188. if (HAVE_PRE_DECREMENT || HAVE_PRE_MODIFY_DISP)
  189. {
  190. /* Prefer the simple form if both are available. */
  191. value = (HAVE_PRE_DECREMENT) ? SIMPLE_PRE_DEC : DISP_PRE;
  192. decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
  193. decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_INC] = value;
  194. decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_ADD] = value;
  195. decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_INC] = value;
  196. }
  197. if (HAVE_POST_DECREMENT || HAVE_POST_MODIFY_DISP)
  198. {
  199. /* Prefer the simple form if both are available. */
  200. value = (HAVE_POST_DECREMENT) ? SIMPLE_POST_DEC : DISP_POST;
  201. decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_ADD] = value;
  202. decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_INC] = value;
  203. decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_ADD] = value;
  204. decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_INC] = value;
  205. }
  206. if (HAVE_PRE_MODIFY_DISP)
  207. {
  208. decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
  209. decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
  210. decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_ADD] = DISP_PRE;
  211. decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_INC] = DISP_PRE;
  212. decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
  213. decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
  214. decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_ADD] = DISP_PRE;
  215. decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_INC] = DISP_PRE;
  216. }
  217. if (HAVE_POST_MODIFY_DISP)
  218. {
  219. decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
  220. decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
  221. decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_ADD] = DISP_POST;
  222. decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_INC] = DISP_POST;
  223. decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
  224. decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
  225. decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_ADD] = DISP_POST;
  226. decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_INC] = DISP_POST;
  227. }
  228. /* This is much simpler than the other cases because we do not look
  229. for the reg1-reg2 case. Note that we do not have a INC_POS_REG
  230. and INC_NEG_REG states. Most of the use of such states would be
  231. on a target that had an R1 - R2 update address form.
  232. There is the remote possibility that you could also catch a = a +
  233. b; *(a - b) as a postdecrement of (a + b). However, it is
  234. unclear if *(a - b) would ever be generated on a machine that did
  235. not have that kind of addressing mode. The IA-64 and RS6000 will
  236. not do this, and I cannot speak for any other. If any
  237. architecture does have an a-b update for, these cases should be
  238. added. */
  239. if (HAVE_PRE_MODIFY_REG)
  240. {
  241. decision_table[INC_REG][INC_ZERO][FORM_PRE_ADD] = REG_PRE;
  242. decision_table[INC_REG][INC_ZERO][FORM_PRE_INC] = REG_PRE;
  243. decision_table[INC_REG][INC_REG][FORM_POST_ADD] = REG_PRE;
  244. decision_table[INC_REG][INC_REG][FORM_POST_INC] = REG_PRE;
  245. }
  246. if (HAVE_POST_MODIFY_REG)
  247. {
  248. decision_table[INC_REG][INC_ZERO][FORM_POST_ADD] = REG_POST;
  249. decision_table[INC_REG][INC_ZERO][FORM_POST_INC] = REG_POST;
  250. }
  251. initialized = true;
  252. }
  253. /* Parsed fields of an inc insn of the form "reg_res = reg0+reg1" or
  254. "reg_res = reg0+c". */
  255. static struct inc_insn
  256. {
  257. rtx_insn *insn; /* The insn being parsed. */
  258. rtx pat; /* The pattern of the insn. */
  259. bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
  260. enum form form;
  261. rtx reg_res;
  262. rtx reg0;
  263. rtx reg1;
  264. enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
  265. HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
  266. } inc_insn;
  267. /* Dump the parsed inc insn to FILE. */
  268. static void
  269. dump_inc_insn (FILE *file)
  270. {
  271. const char *f = ((inc_insn.form == FORM_PRE_ADD)
  272. || (inc_insn.form == FORM_PRE_INC)) ? "pre" : "post";
  273. dump_insn_slim (file, inc_insn.insn);
  274. switch (inc_insn.form)
  275. {
  276. case FORM_PRE_ADD:
  277. case FORM_POST_ADD:
  278. if (inc_insn.reg1_is_const)
  279. fprintf (file, "found %s add(%d) r[%d]=r[%d]+%d\n",
  280. f, INSN_UID (inc_insn.insn),
  281. REGNO (inc_insn.reg_res),
  282. REGNO (inc_insn.reg0), (int) inc_insn.reg1_val);
  283. else
  284. fprintf (file, "found %s add(%d) r[%d]=r[%d]+r[%d]\n",
  285. f, INSN_UID (inc_insn.insn),
  286. REGNO (inc_insn.reg_res),
  287. REGNO (inc_insn.reg0), REGNO (inc_insn.reg1));
  288. break;
  289. case FORM_PRE_INC:
  290. case FORM_POST_INC:
  291. if (inc_insn.reg1_is_const)
  292. fprintf (file, "found %s inc(%d) r[%d]+=%d\n",
  293. f, INSN_UID (inc_insn.insn),
  294. REGNO (inc_insn.reg_res), (int) inc_insn.reg1_val);
  295. else
  296. fprintf (file, "found %s inc(%d) r[%d]+=r[%d]\n",
  297. f, INSN_UID (inc_insn.insn),
  298. REGNO (inc_insn.reg_res), REGNO (inc_insn.reg1));
  299. break;
  300. default:
  301. break;
  302. }
  303. }
  304. /* Parsed fields of a mem ref of the form "*(reg0+reg1)" or "*(reg0+c)". */
  305. static struct mem_insn
  306. {
  307. rtx_insn *insn; /* The insn being parsed. */
  308. rtx pat; /* The pattern of the insn. */
  309. rtx *mem_loc; /* The address of the field that holds the mem */
  310. /* that is to be replaced. */
  311. bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
  312. rtx reg0;
  313. rtx reg1; /* This is either a reg or a const depending on
  314. reg1_is_const. */
  315. enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
  316. HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
  317. } mem_insn;
  318. /* Dump the parsed mem insn to FILE. */
  319. static void
  320. dump_mem_insn (FILE *file)
  321. {
  322. dump_insn_slim (file, mem_insn.insn);
  323. if (mem_insn.reg1_is_const)
  324. fprintf (file, "found mem(%d) *(r[%d]+%d)\n",
  325. INSN_UID (mem_insn.insn),
  326. REGNO (mem_insn.reg0), (int) mem_insn.reg1_val);
  327. else
  328. fprintf (file, "found mem(%d) *(r[%d]+r[%d])\n",
  329. INSN_UID (mem_insn.insn),
  330. REGNO (mem_insn.reg0), REGNO (mem_insn.reg1));
  331. }
  332. /* The following three arrays contain pointers to instructions. They
  333. are indexed by REGNO. At any point in the basic block where we are
  334. looking these three arrays contain, respectively, the next insn
  335. that uses REGNO, the next inc or add insn that uses REGNO and the
  336. next insn that sets REGNO.
  337. The arrays are not cleared when we move from block to block so
  338. whenever an insn is retrieved from these arrays, it's block number
  339. must be compared with the current block.
  340. */
  341. static rtx_insn **reg_next_use = NULL;
  342. static rtx_insn **reg_next_inc_use = NULL;
  343. static rtx_insn **reg_next_def = NULL;
  344. /* Move dead note that match PATTERN to TO_INSN from FROM_INSN. We do
  345. not really care about moving any other notes from the inc or add
  346. insn. Moving the REG_EQUAL and REG_EQUIV is clearly wrong and it
  347. does not appear that there are any other kinds of relevant notes. */
  348. static void
  349. move_dead_notes (rtx_insn *to_insn, rtx_insn *from_insn, rtx pattern)
  350. {
  351. rtx note;
  352. rtx next_note;
  353. rtx prev_note = NULL;
  354. for (note = REG_NOTES (from_insn); note; note = next_note)
  355. {
  356. next_note = XEXP (note, 1);
  357. if ((REG_NOTE_KIND (note) == REG_DEAD)
  358. && pattern == XEXP (note, 0))
  359. {
  360. XEXP (note, 1) = REG_NOTES (to_insn);
  361. REG_NOTES (to_insn) = note;
  362. if (prev_note)
  363. XEXP (prev_note, 1) = next_note;
  364. else
  365. REG_NOTES (from_insn) = next_note;
  366. }
  367. else prev_note = note;
  368. }
  369. }
  370. /* Create a mov insn DEST_REG <- SRC_REG and insert it before
  371. NEXT_INSN. */
  372. static rtx_insn *
  373. insert_move_insn_before (rtx_insn *next_insn, rtx dest_reg, rtx src_reg)
  374. {
  375. rtx_insn *insns;
  376. start_sequence ();
  377. emit_move_insn (dest_reg, src_reg);
  378. insns = get_insns ();
  379. end_sequence ();
  380. emit_insn_before (insns, next_insn);
  381. return insns;
  382. }
  383. /* Change mem_insn.mem_loc so that uses NEW_ADDR which has an
  384. increment of INC_REG. To have reached this point, the change is a
  385. legitimate one from a dataflow point of view. The only questions
  386. are is this a valid change to the instruction and is this a
  387. profitable change to the instruction. */
  388. static bool
  389. attempt_change (rtx new_addr, rtx inc_reg)
  390. {
  391. /* There are four cases: For the two cases that involve an add
  392. instruction, we are going to have to delete the add and insert a
  393. mov. We are going to assume that the mov is free. This is
  394. fairly early in the backend and there are a lot of opportunities
  395. for removing that move later. In particular, there is the case
  396. where the move may be dead, this is what dead code elimination
  397. passes are for. The two cases where we have an inc insn will be
  398. handled mov free. */
  399. basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
  400. rtx_insn *mov_insn = NULL;
  401. int regno;
  402. rtx mem = *mem_insn.mem_loc;
  403. machine_mode mode = GET_MODE (mem);
  404. rtx new_mem;
  405. int old_cost = 0;
  406. int new_cost = 0;
  407. bool speed = optimize_bb_for_speed_p (bb);
  408. PUT_MODE (mem_tmp, mode);
  409. XEXP (mem_tmp, 0) = new_addr;
  410. old_cost = (set_src_cost (mem, speed)
  411. + set_rtx_cost (PATTERN (inc_insn.insn), speed));
  412. new_cost = set_src_cost (mem_tmp, speed);
  413. /* The first item of business is to see if this is profitable. */
  414. if (old_cost < new_cost)
  415. {
  416. if (dump_file)
  417. fprintf (dump_file, "cost failure old=%d new=%d\n", old_cost, new_cost);
  418. return false;
  419. }
  420. /* Jump through a lot of hoops to keep the attributes up to date. We
  421. do not want to call one of the change address variants that take
  422. an offset even though we know the offset in many cases. These
  423. assume you are changing where the address is pointing by the
  424. offset. */
  425. new_mem = replace_equiv_address_nv (mem, new_addr);
  426. if (! validate_change (mem_insn.insn, mem_insn.mem_loc, new_mem, 0))
  427. {
  428. if (dump_file)
  429. fprintf (dump_file, "validation failure\n");
  430. return false;
  431. }
  432. /* From here to the end of the function we are committed to the
  433. change, i.e. nothing fails. Generate any necessary movs, move
  434. any regnotes, and fix up the reg_next_{use,inc_use,def}. */
  435. switch (inc_insn.form)
  436. {
  437. case FORM_PRE_ADD:
  438. /* Replace the addition with a move. Do it at the location of
  439. the addition since the operand of the addition may change
  440. before the memory reference. */
  441. mov_insn = insert_move_insn_before (inc_insn.insn,
  442. inc_insn.reg_res, inc_insn.reg0);
  443. move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
  444. regno = REGNO (inc_insn.reg_res);
  445. reg_next_def[regno] = mov_insn;
  446. reg_next_use[regno] = NULL;
  447. regno = REGNO (inc_insn.reg0);
  448. reg_next_use[regno] = mov_insn;
  449. df_recompute_luids (bb);
  450. break;
  451. case FORM_POST_INC:
  452. regno = REGNO (inc_insn.reg_res);
  453. if (reg_next_use[regno] == reg_next_inc_use[regno])
  454. reg_next_inc_use[regno] = NULL;
  455. /* Fallthru. */
  456. case FORM_PRE_INC:
  457. regno = REGNO (inc_insn.reg_res);
  458. reg_next_def[regno] = mem_insn.insn;
  459. reg_next_use[regno] = NULL;
  460. break;
  461. case FORM_POST_ADD:
  462. mov_insn = insert_move_insn_before (mem_insn.insn,
  463. inc_insn.reg_res, inc_insn.reg0);
  464. move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
  465. /* Do not move anything to the mov insn because the instruction
  466. pointer for the main iteration has not yet hit that. It is
  467. still pointing to the mem insn. */
  468. regno = REGNO (inc_insn.reg_res);
  469. reg_next_def[regno] = mem_insn.insn;
  470. reg_next_use[regno] = NULL;
  471. regno = REGNO (inc_insn.reg0);
  472. reg_next_use[regno] = mem_insn.insn;
  473. if ((reg_next_use[regno] == reg_next_inc_use[regno])
  474. || (reg_next_inc_use[regno] == inc_insn.insn))
  475. reg_next_inc_use[regno] = NULL;
  476. df_recompute_luids (bb);
  477. break;
  478. case FORM_last:
  479. default:
  480. gcc_unreachable ();
  481. }
  482. if (!inc_insn.reg1_is_const)
  483. {
  484. regno = REGNO (inc_insn.reg1);
  485. reg_next_use[regno] = mem_insn.insn;
  486. if ((reg_next_use[regno] == reg_next_inc_use[regno])
  487. || (reg_next_inc_use[regno] == inc_insn.insn))
  488. reg_next_inc_use[regno] = NULL;
  489. }
  490. delete_insn (inc_insn.insn);
  491. if (dump_file && mov_insn)
  492. {
  493. fprintf (dump_file, "inserting mov ");
  494. dump_insn_slim (dump_file, mov_insn);
  495. }
  496. /* Record that this insn has an implicit side effect. */
  497. add_reg_note (mem_insn.insn, REG_INC, inc_reg);
  498. if (dump_file)
  499. {
  500. fprintf (dump_file, "****success ");
  501. dump_insn_slim (dump_file, mem_insn.insn);
  502. }
  503. return true;
  504. }
  505. /* Try to combine the instruction in INC_INSN with the instruction in
  506. MEM_INSN. First the form is determined using the DECISION_TABLE
  507. and the results of parsing the INC_INSN and the MEM_INSN.
  508. Assuming the form is ok, a prototype new address is built which is
  509. passed to ATTEMPT_CHANGE for final processing. */
  510. static bool
  511. try_merge (void)
  512. {
  513. enum gen_form gen_form;
  514. rtx mem = *mem_insn.mem_loc;
  515. rtx inc_reg = inc_insn.form == FORM_POST_ADD ?
  516. inc_insn.reg_res : mem_insn.reg0;
  517. /* The width of the mem being accessed. */
  518. int size = GET_MODE_SIZE (GET_MODE (mem));
  519. rtx_insn *last_insn = NULL;
  520. machine_mode reg_mode = GET_MODE (inc_reg);
  521. switch (inc_insn.form)
  522. {
  523. case FORM_PRE_ADD:
  524. case FORM_PRE_INC:
  525. last_insn = mem_insn.insn;
  526. break;
  527. case FORM_POST_INC:
  528. case FORM_POST_ADD:
  529. last_insn = inc_insn.insn;
  530. break;
  531. case FORM_last:
  532. default:
  533. gcc_unreachable ();
  534. }
  535. /* Cannot handle auto inc of the stack. */
  536. if (inc_reg == stack_pointer_rtx)
  537. {
  538. if (dump_file)
  539. fprintf (dump_file, "cannot inc stack %d failure\n", REGNO (inc_reg));
  540. return false;
  541. }
  542. /* Look to see if the inc register is dead after the memory
  543. reference. If it is, do not do the combination. */
  544. if (find_regno_note (last_insn, REG_DEAD, REGNO (inc_reg)))
  545. {
  546. if (dump_file)
  547. fprintf (dump_file, "dead failure %d\n", REGNO (inc_reg));
  548. return false;
  549. }
  550. mem_insn.reg1_state = (mem_insn.reg1_is_const)
  551. ? set_inc_state (mem_insn.reg1_val, size) : INC_REG;
  552. inc_insn.reg1_state = (inc_insn.reg1_is_const)
  553. ? set_inc_state (inc_insn.reg1_val, size) : INC_REG;
  554. /* Now get the form that we are generating. */
  555. gen_form = decision_table
  556. [inc_insn.reg1_state][mem_insn.reg1_state][inc_insn.form];
  557. if (dbg_cnt (auto_inc_dec) == false)
  558. return false;
  559. switch (gen_form)
  560. {
  561. default:
  562. case NOTHING:
  563. return false;
  564. case SIMPLE_PRE_INC: /* ++size */
  565. if (dump_file)
  566. fprintf (dump_file, "trying SIMPLE_PRE_INC\n");
  567. return attempt_change (gen_rtx_PRE_INC (reg_mode, inc_reg), inc_reg);
  568. break;
  569. case SIMPLE_POST_INC: /* size++ */
  570. if (dump_file)
  571. fprintf (dump_file, "trying SIMPLE_POST_INC\n");
  572. return attempt_change (gen_rtx_POST_INC (reg_mode, inc_reg), inc_reg);
  573. break;
  574. case SIMPLE_PRE_DEC: /* --size */
  575. if (dump_file)
  576. fprintf (dump_file, "trying SIMPLE_PRE_DEC\n");
  577. return attempt_change (gen_rtx_PRE_DEC (reg_mode, inc_reg), inc_reg);
  578. break;
  579. case SIMPLE_POST_DEC: /* size-- */
  580. if (dump_file)
  581. fprintf (dump_file, "trying SIMPLE_POST_DEC\n");
  582. return attempt_change (gen_rtx_POST_DEC (reg_mode, inc_reg), inc_reg);
  583. break;
  584. case DISP_PRE: /* ++con */
  585. if (dump_file)
  586. fprintf (dump_file, "trying DISP_PRE\n");
  587. return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
  588. inc_reg,
  589. gen_rtx_PLUS (reg_mode,
  590. inc_reg,
  591. inc_insn.reg1)),
  592. inc_reg);
  593. break;
  594. case DISP_POST: /* con++ */
  595. if (dump_file)
  596. fprintf (dump_file, "trying POST_DISP\n");
  597. return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
  598. inc_reg,
  599. gen_rtx_PLUS (reg_mode,
  600. inc_reg,
  601. inc_insn.reg1)),
  602. inc_reg);
  603. break;
  604. case REG_PRE: /* ++reg */
  605. if (dump_file)
  606. fprintf (dump_file, "trying PRE_REG\n");
  607. return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
  608. inc_reg,
  609. gen_rtx_PLUS (reg_mode,
  610. inc_reg,
  611. inc_insn.reg1)),
  612. inc_reg);
  613. break;
  614. case REG_POST: /* reg++ */
  615. if (dump_file)
  616. fprintf (dump_file, "trying POST_REG\n");
  617. return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
  618. inc_reg,
  619. gen_rtx_PLUS (reg_mode,
  620. inc_reg,
  621. inc_insn.reg1)),
  622. inc_reg);
  623. break;
  624. }
  625. }
  626. /* Return the next insn that uses (if reg_next_use is passed in
  627. NEXT_ARRAY) or defines (if reg_next_def is passed in NEXT_ARRAY)
  628. REGNO in BB. */
  629. static rtx_insn *
  630. get_next_ref (int regno, basic_block bb, rtx_insn **next_array)
  631. {
  632. rtx_insn *insn = next_array[regno];
  633. /* Lazy about cleaning out the next_arrays. */
  634. if (insn && BLOCK_FOR_INSN (insn) != bb)
  635. {
  636. next_array[regno] = NULL;
  637. insn = NULL;
  638. }
  639. return insn;
  640. }
  641. /* Reverse the operands in a mem insn. */
  642. static void
  643. reverse_mem (void)
  644. {
  645. rtx tmp = mem_insn.reg1;
  646. mem_insn.reg1 = mem_insn.reg0;
  647. mem_insn.reg0 = tmp;
  648. }
  649. /* Reverse the operands in a inc insn. */
  650. static void
  651. reverse_inc (void)
  652. {
  653. rtx tmp = inc_insn.reg1;
  654. inc_insn.reg1 = inc_insn.reg0;
  655. inc_insn.reg0 = tmp;
  656. }
  657. /* Return true if INSN is of a form "a = b op c" where a and b are
  658. regs. op is + if c is a reg and +|- if c is a const. Fill in
  659. INC_INSN with what is found.
  660. This function is called in two contexts, if BEFORE_MEM is true,
  661. this is called for each insn in the basic block. If BEFORE_MEM is
  662. false, it is called for the instruction in the block that uses the
  663. index register for some memory reference that is currently being
  664. processed. */
  665. static bool
  666. parse_add_or_inc (rtx_insn *insn, bool before_mem)
  667. {
  668. rtx pat = single_set (insn);
  669. if (!pat)
  670. return false;
  671. /* Result must be single reg. */
  672. if (!REG_P (SET_DEST (pat)))
  673. return false;
  674. if ((GET_CODE (SET_SRC (pat)) != PLUS)
  675. && (GET_CODE (SET_SRC (pat)) != MINUS))
  676. return false;
  677. if (!REG_P (XEXP (SET_SRC (pat), 0)))
  678. return false;
  679. inc_insn.insn = insn;
  680. inc_insn.pat = pat;
  681. inc_insn.reg_res = SET_DEST (pat);
  682. inc_insn.reg0 = XEXP (SET_SRC (pat), 0);
  683. if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg0))
  684. inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
  685. else
  686. inc_insn.form = before_mem ? FORM_PRE_ADD : FORM_POST_ADD;
  687. if (CONST_INT_P (XEXP (SET_SRC (pat), 1)))
  688. {
  689. /* Process a = b + c where c is a const. */
  690. inc_insn.reg1_is_const = true;
  691. if (GET_CODE (SET_SRC (pat)) == PLUS)
  692. {
  693. inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
  694. inc_insn.reg1_val = INTVAL (inc_insn.reg1);
  695. }
  696. else
  697. {
  698. inc_insn.reg1_val = -INTVAL (XEXP (SET_SRC (pat), 1));
  699. inc_insn.reg1 = GEN_INT (inc_insn.reg1_val);
  700. }
  701. return true;
  702. }
  703. else if ((HAVE_PRE_MODIFY_REG || HAVE_POST_MODIFY_REG)
  704. && (REG_P (XEXP (SET_SRC (pat), 1)))
  705. && GET_CODE (SET_SRC (pat)) == PLUS)
  706. {
  707. /* Process a = b + c where c is a reg. */
  708. inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
  709. inc_insn.reg1_is_const = false;
  710. if (inc_insn.form == FORM_PRE_INC
  711. || inc_insn.form == FORM_POST_INC)
  712. return true;
  713. else if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg1))
  714. {
  715. /* Reverse the two operands and turn *_ADD into *_INC since
  716. a = c + a. */
  717. reverse_inc ();
  718. inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
  719. return true;
  720. }
  721. else
  722. return true;
  723. }
  724. return false;
  725. }
  726. /* A recursive function that checks all of the mem uses in
  727. ADDRESS_OF_X to see if any single one of them is compatible with
  728. what has been found in inc_insn.
  729. -1 is returned for success. 0 is returned if nothing was found and
  730. 1 is returned for failure. */
  731. static int
  732. find_address (rtx *address_of_x)
  733. {
  734. rtx x = *address_of_x;
  735. enum rtx_code code = GET_CODE (x);
  736. const char *const fmt = GET_RTX_FORMAT (code);
  737. int i;
  738. int value = 0;
  739. int tem;
  740. if (code == MEM && rtx_equal_p (XEXP (x, 0), inc_insn.reg_res))
  741. {
  742. /* Match with *reg0. */
  743. mem_insn.mem_loc = address_of_x;
  744. mem_insn.reg0 = inc_insn.reg_res;
  745. mem_insn.reg1_is_const = true;
  746. mem_insn.reg1_val = 0;
  747. mem_insn.reg1 = GEN_INT (0);
  748. return -1;
  749. }
  750. if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
  751. && rtx_equal_p (XEXP (XEXP (x, 0), 0), inc_insn.reg_res))
  752. {
  753. rtx b = XEXP (XEXP (x, 0), 1);
  754. mem_insn.mem_loc = address_of_x;
  755. mem_insn.reg0 = inc_insn.reg_res;
  756. mem_insn.reg1 = b;
  757. mem_insn.reg1_is_const = inc_insn.reg1_is_const;
  758. if (CONST_INT_P (b))
  759. {
  760. /* Match with *(reg0 + reg1) where reg1 is a const. */
  761. HOST_WIDE_INT val = INTVAL (b);
  762. if (inc_insn.reg1_is_const
  763. && (inc_insn.reg1_val == val || inc_insn.reg1_val == -val))
  764. {
  765. mem_insn.reg1_val = val;
  766. return -1;
  767. }
  768. }
  769. else if (!inc_insn.reg1_is_const
  770. && rtx_equal_p (inc_insn.reg1, b))
  771. /* Match with *(reg0 + reg1). */
  772. return -1;
  773. }
  774. if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
  775. {
  776. /* If REG occurs inside a MEM used in a bit-field reference,
  777. that is unacceptable. */
  778. if (find_address (&XEXP (x, 0)))
  779. return 1;
  780. }
  781. if (x == inc_insn.reg_res)
  782. return 1;
  783. /* Time for some deep diving. */
  784. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  785. {
  786. if (fmt[i] == 'e')
  787. {
  788. tem = find_address (&XEXP (x, i));
  789. /* If this is the first use, let it go so the rest of the
  790. insn can be checked. */
  791. if (value == 0)
  792. value = tem;
  793. else if (tem != 0)
  794. /* More than one match was found. */
  795. return 1;
  796. }
  797. else if (fmt[i] == 'E')
  798. {
  799. int j;
  800. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  801. {
  802. tem = find_address (&XVECEXP (x, i, j));
  803. /* If this is the first use, let it go so the rest of
  804. the insn can be checked. */
  805. if (value == 0)
  806. value = tem;
  807. else if (tem != 0)
  808. /* More than one match was found. */
  809. return 1;
  810. }
  811. }
  812. }
  813. return value;
  814. }
  815. /* Once a suitable mem reference has been found and the MEM_INSN
  816. structure has been filled in, FIND_INC is called to see if there is
  817. a suitable add or inc insn that follows the mem reference and
  818. determine if it is suitable to merge.
  819. In the case where the MEM_INSN has two registers in the reference,
  820. this function may be called recursively. The first time looking
  821. for an add of the first register, and if that fails, looking for an
  822. add of the second register. The FIRST_TRY parameter is used to
  823. only allow the parameters to be reversed once. */
  824. static bool
  825. find_inc (bool first_try)
  826. {
  827. rtx_insn *insn;
  828. basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
  829. rtx_insn *other_insn;
  830. df_ref def;
  831. /* Make sure this reg appears only once in this insn. */
  832. if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg0, 1) != 1)
  833. {
  834. if (dump_file)
  835. fprintf (dump_file, "mem count failure\n");
  836. return false;
  837. }
  838. if (dump_file)
  839. dump_mem_insn (dump_file);
  840. /* Find the next use that is an inc. */
  841. insn = get_next_ref (REGNO (mem_insn.reg0),
  842. BLOCK_FOR_INSN (mem_insn.insn),
  843. reg_next_inc_use);
  844. if (!insn)
  845. return false;
  846. /* Even though we know the next use is an add or inc because it came
  847. from the reg_next_inc_use, we must still reparse. */
  848. if (!parse_add_or_inc (insn, false))
  849. {
  850. /* Next use was not an add. Look for one extra case. It could be
  851. that we have:
  852. *(a + b)
  853. ...= a;
  854. ...= b + a
  855. if we reverse the operands in the mem ref we would
  856. find this. Only try it once though. */
  857. if (first_try && !mem_insn.reg1_is_const)
  858. {
  859. reverse_mem ();
  860. return find_inc (false);
  861. }
  862. else
  863. return false;
  864. }
  865. /* Need to assure that none of the operands of the inc instruction are
  866. assigned to by the mem insn. */
  867. FOR_EACH_INSN_DEF (def, mem_insn.insn)
  868. {
  869. unsigned int regno = DF_REF_REGNO (def);
  870. if ((regno == REGNO (inc_insn.reg0))
  871. || (regno == REGNO (inc_insn.reg_res)))
  872. {
  873. if (dump_file)
  874. fprintf (dump_file, "inc conflicts with store failure.\n");
  875. return false;
  876. }
  877. if (!inc_insn.reg1_is_const && (regno == REGNO (inc_insn.reg1)))
  878. {
  879. if (dump_file)
  880. fprintf (dump_file, "inc conflicts with store failure.\n");
  881. return false;
  882. }
  883. }
  884. if (dump_file)
  885. dump_inc_insn (dump_file);
  886. if (inc_insn.form == FORM_POST_ADD)
  887. {
  888. /* Make sure that there is no insn that assigns to inc_insn.res
  889. between the mem_insn and the inc_insn. */
  890. rtx_insn *other_insn = get_next_ref (REGNO (inc_insn.reg_res),
  891. BLOCK_FOR_INSN (mem_insn.insn),
  892. reg_next_def);
  893. if (other_insn != inc_insn.insn)
  894. {
  895. if (dump_file)
  896. fprintf (dump_file,
  897. "result of add is assigned to between mem and inc insns.\n");
  898. return false;
  899. }
  900. other_insn = get_next_ref (REGNO (inc_insn.reg_res),
  901. BLOCK_FOR_INSN (mem_insn.insn),
  902. reg_next_use);
  903. if (other_insn
  904. && (other_insn != inc_insn.insn)
  905. && (DF_INSN_LUID (inc_insn.insn) > DF_INSN_LUID (other_insn)))
  906. {
  907. if (dump_file)
  908. fprintf (dump_file,
  909. "result of add is used between mem and inc insns.\n");
  910. return false;
  911. }
  912. /* For the post_add to work, the result_reg of the inc must not be
  913. used in the mem insn since this will become the new index
  914. register. */
  915. if (reg_overlap_mentioned_p (inc_insn.reg_res, PATTERN (mem_insn.insn)))
  916. {
  917. if (dump_file)
  918. fprintf (dump_file, "base reg replacement failure.\n");
  919. return false;
  920. }
  921. }
  922. if (mem_insn.reg1_is_const)
  923. {
  924. if (mem_insn.reg1_val == 0)
  925. {
  926. if (!inc_insn.reg1_is_const)
  927. {
  928. /* The mem looks like *r0 and the rhs of the add has two
  929. registers. */
  930. int luid = DF_INSN_LUID (inc_insn.insn);
  931. if (inc_insn.form == FORM_POST_ADD)
  932. {
  933. /* The trick is that we are not going to increment r0,
  934. we are going to increment the result of the add insn.
  935. For this trick to be correct, the result reg of
  936. the inc must be a valid addressing reg. */
  937. addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
  938. if (GET_MODE (inc_insn.reg_res)
  939. != targetm.addr_space.address_mode (as))
  940. {
  941. if (dump_file)
  942. fprintf (dump_file, "base reg mode failure.\n");
  943. return false;
  944. }
  945. /* We also need to make sure that the next use of
  946. inc result is after the inc. */
  947. other_insn
  948. = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
  949. if (other_insn && luid > DF_INSN_LUID (other_insn))
  950. return false;
  951. if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
  952. reverse_inc ();
  953. }
  954. other_insn
  955. = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
  956. if (other_insn && luid > DF_INSN_LUID (other_insn))
  957. return false;
  958. }
  959. }
  960. /* Both the inc/add and the mem have a constant. Need to check
  961. that the constants are ok. */
  962. else if ((mem_insn.reg1_val != inc_insn.reg1_val)
  963. && (mem_insn.reg1_val != -inc_insn.reg1_val))
  964. return false;
  965. }
  966. else
  967. {
  968. /* The mem insn is of the form *(a + b) where a and b are both
  969. regs. It may be that in order to match the add or inc we
  970. need to treat it as if it was *(b + a). It may also be that
  971. the add is of the form a + c where c does not match b and
  972. then we just abandon this. */
  973. int luid = DF_INSN_LUID (inc_insn.insn);
  974. rtx_insn *other_insn;
  975. /* Make sure this reg appears only once in this insn. */
  976. if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg1, 1) != 1)
  977. return false;
  978. if (inc_insn.form == FORM_POST_ADD)
  979. {
  980. /* For this trick to be correct, the result reg of the inc
  981. must be a valid addressing reg. */
  982. addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
  983. if (GET_MODE (inc_insn.reg_res)
  984. != targetm.addr_space.address_mode (as))
  985. {
  986. if (dump_file)
  987. fprintf (dump_file, "base reg mode failure.\n");
  988. return false;
  989. }
  990. if (rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
  991. {
  992. if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
  993. {
  994. /* See comment above on find_inc (false) call. */
  995. if (first_try)
  996. {
  997. reverse_mem ();
  998. return find_inc (false);
  999. }
  1000. else
  1001. return false;
  1002. }
  1003. /* Need to check that there are no assignments to b
  1004. before the add insn. */
  1005. other_insn
  1006. = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
  1007. if (other_insn && luid > DF_INSN_LUID (other_insn))
  1008. return false;
  1009. /* All ok for the next step. */
  1010. }
  1011. else
  1012. {
  1013. /* We know that mem_insn.reg0 must equal inc_insn.reg1
  1014. or else we would not have found the inc insn. */
  1015. reverse_mem ();
  1016. if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
  1017. {
  1018. /* See comment above on find_inc (false) call. */
  1019. if (first_try)
  1020. return find_inc (false);
  1021. else
  1022. return false;
  1023. }
  1024. /* To have gotten here know that.
  1025. *(b + a)
  1026. ... = (b + a)
  1027. We also know that the lhs of the inc is not b or a. We
  1028. need to make sure that there are no assignments to b
  1029. between the mem ref and the inc. */
  1030. other_insn
  1031. = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_def);
  1032. if (other_insn && luid > DF_INSN_LUID (other_insn))
  1033. return false;
  1034. }
  1035. /* Need to check that the next use of the add result is later than
  1036. add insn since this will be the reg incremented. */
  1037. other_insn
  1038. = get_next_ref (REGNO (inc_insn.reg_res), bb, reg_next_use);
  1039. if (other_insn && luid > DF_INSN_LUID (other_insn))
  1040. return false;
  1041. }
  1042. else /* FORM_POST_INC. There is less to check here because we
  1043. know that operands must line up. */
  1044. {
  1045. if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
  1046. /* See comment above on find_inc (false) call. */
  1047. {
  1048. if (first_try)
  1049. {
  1050. reverse_mem ();
  1051. return find_inc (false);
  1052. }
  1053. else
  1054. return false;
  1055. }
  1056. /* To have gotten here know that.
  1057. *(a + b)
  1058. ... = (a + b)
  1059. We also know that the lhs of the inc is not b. We need to make
  1060. sure that there are no assignments to b between the mem ref and
  1061. the inc. */
  1062. other_insn
  1063. = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
  1064. if (other_insn && luid > DF_INSN_LUID (other_insn))
  1065. return false;
  1066. }
  1067. }
  1068. if (inc_insn.form == FORM_POST_INC)
  1069. {
  1070. other_insn
  1071. = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_use);
  1072. /* When we found inc_insn, we were looking for the
  1073. next add or inc, not the next insn that used the
  1074. reg. Because we are going to increment the reg
  1075. in this form, we need to make sure that there
  1076. were no intervening uses of reg. */
  1077. if (inc_insn.insn != other_insn)
  1078. return false;
  1079. }
  1080. return try_merge ();
  1081. }
  1082. /* A recursive function that walks ADDRESS_OF_X to find all of the mem
  1083. uses in pat that could be used as an auto inc or dec. It then
  1084. calls FIND_INC for each one. */
  1085. static bool
  1086. find_mem (rtx *address_of_x)
  1087. {
  1088. rtx x = *address_of_x;
  1089. enum rtx_code code = GET_CODE (x);
  1090. const char *const fmt = GET_RTX_FORMAT (code);
  1091. int i;
  1092. if (code == MEM && REG_P (XEXP (x, 0)))
  1093. {
  1094. /* Match with *reg0. */
  1095. mem_insn.mem_loc = address_of_x;
  1096. mem_insn.reg0 = XEXP (x, 0);
  1097. mem_insn.reg1_is_const = true;
  1098. mem_insn.reg1_val = 0;
  1099. mem_insn.reg1 = GEN_INT (0);
  1100. if (find_inc (true))
  1101. return true;
  1102. }
  1103. if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
  1104. && REG_P (XEXP (XEXP (x, 0), 0)))
  1105. {
  1106. rtx reg1 = XEXP (XEXP (x, 0), 1);
  1107. mem_insn.mem_loc = address_of_x;
  1108. mem_insn.reg0 = XEXP (XEXP (x, 0), 0);
  1109. mem_insn.reg1 = reg1;
  1110. if (CONST_INT_P (reg1))
  1111. {
  1112. mem_insn.reg1_is_const = true;
  1113. /* Match with *(reg0 + c) where c is a const. */
  1114. mem_insn.reg1_val = INTVAL (reg1);
  1115. if (find_inc (true))
  1116. return true;
  1117. }
  1118. else if (REG_P (reg1))
  1119. {
  1120. /* Match with *(reg0 + reg1). */
  1121. mem_insn.reg1_is_const = false;
  1122. if (find_inc (true))
  1123. return true;
  1124. }
  1125. }
  1126. if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
  1127. {
  1128. /* If REG occurs inside a MEM used in a bit-field reference,
  1129. that is unacceptable. */
  1130. return false;
  1131. }
  1132. /* Time for some deep diving. */
  1133. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1134. {
  1135. if (fmt[i] == 'e')
  1136. {
  1137. if (find_mem (&XEXP (x, i)))
  1138. return true;
  1139. }
  1140. else if (fmt[i] == 'E')
  1141. {
  1142. int j;
  1143. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1144. if (find_mem (&XVECEXP (x, i, j)))
  1145. return true;
  1146. }
  1147. }
  1148. return false;
  1149. }
  1150. /* Try to combine all incs and decs by constant values with memory
  1151. references in BB. */
  1152. static void
  1153. merge_in_block (int max_reg, basic_block bb)
  1154. {
  1155. rtx_insn *insn;
  1156. rtx_insn *curr;
  1157. int success_in_block = 0;
  1158. if (dump_file)
  1159. fprintf (dump_file, "\n\nstarting bb %d\n", bb->index);
  1160. FOR_BB_INSNS_REVERSE_SAFE (bb, insn, curr)
  1161. {
  1162. bool insn_is_add_or_inc = true;
  1163. if (!NONDEBUG_INSN_P (insn))
  1164. continue;
  1165. /* This continue is deliberate. We do not want the uses of the
  1166. jump put into reg_next_use because it is not considered safe to
  1167. combine a preincrement with a jump. */
  1168. if (JUMP_P (insn))
  1169. continue;
  1170. if (dump_file)
  1171. dump_insn_slim (dump_file, insn);
  1172. /* Does this instruction increment or decrement a register? */
  1173. if (parse_add_or_inc (insn, true))
  1174. {
  1175. int regno = REGNO (inc_insn.reg_res);
  1176. /* Cannot handle case where there are three separate regs
  1177. before a mem ref. Too many moves would be needed to be
  1178. profitable. */
  1179. if ((inc_insn.form == FORM_PRE_INC) || inc_insn.reg1_is_const)
  1180. {
  1181. mem_insn.insn = get_next_ref (regno, bb, reg_next_use);
  1182. if (mem_insn.insn)
  1183. {
  1184. bool ok = true;
  1185. if (!inc_insn.reg1_is_const)
  1186. {
  1187. /* We are only here if we are going to try a
  1188. HAVE_*_MODIFY_REG type transformation. c is a
  1189. reg and we must sure that the path from the
  1190. inc_insn to the mem_insn.insn is both def and use
  1191. clear of c because the inc insn is going to move
  1192. into the mem_insn.insn. */
  1193. int luid = DF_INSN_LUID (mem_insn.insn);
  1194. rtx_insn *other_insn
  1195. = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
  1196. if (other_insn && luid > DF_INSN_LUID (other_insn))
  1197. ok = false;
  1198. other_insn
  1199. = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
  1200. if (other_insn && luid > DF_INSN_LUID (other_insn))
  1201. ok = false;
  1202. }
  1203. if (dump_file)
  1204. dump_inc_insn (dump_file);
  1205. if (ok && find_address (&PATTERN (mem_insn.insn)) == -1)
  1206. {
  1207. if (dump_file)
  1208. dump_mem_insn (dump_file);
  1209. if (try_merge ())
  1210. {
  1211. success_in_block++;
  1212. insn_is_add_or_inc = false;
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. else
  1219. {
  1220. insn_is_add_or_inc = false;
  1221. mem_insn.insn = insn;
  1222. if (find_mem (&PATTERN (insn)))
  1223. success_in_block++;
  1224. }
  1225. /* If the inc insn was merged with a mem, the inc insn is gone
  1226. and there is noting to update. */
  1227. if (df_insn_info *insn_info = DF_INSN_INFO_GET (insn))
  1228. {
  1229. df_ref def, use;
  1230. /* Need to update next use. */
  1231. FOR_EACH_INSN_INFO_DEF (def, insn_info)
  1232. {
  1233. reg_next_use[DF_REF_REGNO (def)] = NULL;
  1234. reg_next_inc_use[DF_REF_REGNO (def)] = NULL;
  1235. reg_next_def[DF_REF_REGNO (def)] = insn;
  1236. }
  1237. FOR_EACH_INSN_INFO_USE (use, insn_info)
  1238. {
  1239. reg_next_use[DF_REF_REGNO (use)] = insn;
  1240. if (insn_is_add_or_inc)
  1241. reg_next_inc_use[DF_REF_REGNO (use)] = insn;
  1242. else
  1243. reg_next_inc_use[DF_REF_REGNO (use)] = NULL;
  1244. }
  1245. }
  1246. else if (dump_file)
  1247. fprintf (dump_file, "skipping update of deleted insn %d\n",
  1248. INSN_UID (insn));
  1249. }
  1250. /* If we were successful, try again. There may have been several
  1251. opportunities that were interleaved. This is rare but
  1252. gcc.c-torture/compile/pr17273.c actually exhibits this. */
  1253. if (success_in_block)
  1254. {
  1255. /* In this case, we must clear these vectors since the trick of
  1256. testing if the stale insn in the block will not work. */
  1257. memset (reg_next_use, 0, max_reg * sizeof (rtx));
  1258. memset (reg_next_inc_use, 0, max_reg * sizeof (rtx));
  1259. memset (reg_next_def, 0, max_reg * sizeof (rtx));
  1260. df_recompute_luids (bb);
  1261. merge_in_block (max_reg, bb);
  1262. }
  1263. }
  1264. #endif
  1265. /* Discover auto-inc auto-dec instructions. */
  1266. namespace {
  1267. const pass_data pass_data_inc_dec =
  1268. {
  1269. RTL_PASS, /* type */
  1270. "auto_inc_dec", /* name */
  1271. OPTGROUP_NONE, /* optinfo_flags */
  1272. TV_AUTO_INC_DEC, /* tv_id */
  1273. 0, /* properties_required */
  1274. 0, /* properties_provided */
  1275. 0, /* properties_destroyed */
  1276. 0, /* todo_flags_start */
  1277. TODO_df_finish, /* todo_flags_finish */
  1278. };
  1279. class pass_inc_dec : public rtl_opt_pass
  1280. {
  1281. public:
  1282. pass_inc_dec (gcc::context *ctxt)
  1283. : rtl_opt_pass (pass_data_inc_dec, ctxt)
  1284. {}
  1285. /* opt_pass methods: */
  1286. virtual bool gate (function *)
  1287. {
  1288. #ifdef AUTO_INC_DEC
  1289. return (optimize > 0 && flag_auto_inc_dec);
  1290. #else
  1291. return false;
  1292. #endif
  1293. }
  1294. unsigned int execute (function *);
  1295. }; // class pass_inc_dec
  1296. unsigned int
  1297. pass_inc_dec::execute (function *fun ATTRIBUTE_UNUSED)
  1298. {
  1299. #ifdef AUTO_INC_DEC
  1300. basic_block bb;
  1301. int max_reg = max_reg_num ();
  1302. if (!initialized)
  1303. init_decision_table ();
  1304. mem_tmp = gen_rtx_MEM (Pmode, NULL_RTX);
  1305. df_note_add_problem ();
  1306. df_analyze ();
  1307. reg_next_use = XCNEWVEC (rtx_insn *, max_reg);
  1308. reg_next_inc_use = XCNEWVEC (rtx_insn *, max_reg);
  1309. reg_next_def = XCNEWVEC (rtx_insn *, max_reg);
  1310. FOR_EACH_BB_FN (bb, fun)
  1311. merge_in_block (max_reg, bb);
  1312. free (reg_next_use);
  1313. free (reg_next_inc_use);
  1314. free (reg_next_def);
  1315. mem_tmp = NULL;
  1316. #endif
  1317. return 0;
  1318. }
  1319. } // anon namespace
  1320. rtl_opt_pass *
  1321. make_pass_inc_dec (gcc::context *ctxt)
  1322. {
  1323. return new pass_inc_dec (ctxt);
  1324. }