modulo-sched.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424
  1. /* Swing Modulo Scheduling implementation.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. Contributed by Ayal Zaks and Mustafa Hagog <zaks,mustafa@il.ibm.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 "diagnostic-core.h"
  21. #include "rtl.h"
  22. #include "tm_p.h"
  23. #include "hard-reg-set.h"
  24. #include "regs.h"
  25. #include "hashtab.h"
  26. #include "hash-set.h"
  27. #include "vec.h"
  28. #include "machmode.h"
  29. #include "input.h"
  30. #include "function.h"
  31. #include "profile.h"
  32. #include "flags.h"
  33. #include "insn-config.h"
  34. #include "insn-attr.h"
  35. #include "except.h"
  36. #include "recog.h"
  37. #include "dominance.h"
  38. #include "cfg.h"
  39. #include "cfgrtl.h"
  40. #include "predict.h"
  41. #include "basic-block.h"
  42. #include "sched-int.h"
  43. #include "target.h"
  44. #include "cfgloop.h"
  45. #include "double-int.h"
  46. #include "alias.h"
  47. #include "symtab.h"
  48. #include "wide-int.h"
  49. #include "inchash.h"
  50. #include "tree.h"
  51. #include "insn-codes.h"
  52. #include "optabs.h"
  53. #include "statistics.h"
  54. #include "real.h"
  55. #include "fixed-value.h"
  56. #include "expmed.h"
  57. #include "dojump.h"
  58. #include "explow.h"
  59. #include "calls.h"
  60. #include "emit-rtl.h"
  61. #include "varasm.h"
  62. #include "stmt.h"
  63. #include "expr.h"
  64. #include "params.h"
  65. #include "gcov-io.h"
  66. #include "sbitmap.h"
  67. #include "df.h"
  68. #include "ddg.h"
  69. #include "tree-pass.h"
  70. #include "dbgcnt.h"
  71. #include "loop-unroll.h"
  72. #ifdef INSN_SCHEDULING
  73. /* This file contains the implementation of the Swing Modulo Scheduler,
  74. described in the following references:
  75. [1] J. Llosa, A. Gonzalez, E. Ayguade, M. Valero., and J. Eckhardt.
  76. Lifetime--sensitive modulo scheduling in a production environment.
  77. IEEE Trans. on Comps., 50(3), March 2001
  78. [2] J. Llosa, A. Gonzalez, E. Ayguade, and M. Valero.
  79. Swing Modulo Scheduling: A Lifetime Sensitive Approach.
  80. PACT '96 , pages 80-87, October 1996 (Boston - Massachusetts - USA).
  81. The basic structure is:
  82. 1. Build a data-dependence graph (DDG) for each loop.
  83. 2. Use the DDG to order the insns of a loop (not in topological order
  84. necessarily, but rather) trying to place each insn after all its
  85. predecessors _or_ after all its successors.
  86. 3. Compute MII: a lower bound on the number of cycles to schedule the loop.
  87. 4. Use the ordering to perform list-scheduling of the loop:
  88. 1. Set II = MII. We will try to schedule the loop within II cycles.
  89. 2. Try to schedule the insns one by one according to the ordering.
  90. For each insn compute an interval of cycles by considering already-
  91. scheduled preds and succs (and associated latencies); try to place
  92. the insn in the cycles of this window checking for potential
  93. resource conflicts (using the DFA interface).
  94. Note: this is different from the cycle-scheduling of schedule_insns;
  95. here the insns are not scheduled monotonically top-down (nor bottom-
  96. up).
  97. 3. If failed in scheduling all insns - bump II++ and try again, unless
  98. II reaches an upper bound MaxII, in which case report failure.
  99. 5. If we succeeded in scheduling the loop within II cycles, we now
  100. generate prolog and epilog, decrease the counter of the loop, and
  101. perform modulo variable expansion for live ranges that span more than
  102. II cycles (i.e. use register copies to prevent a def from overwriting
  103. itself before reaching the use).
  104. SMS works with countable loops (1) whose control part can be easily
  105. decoupled from the rest of the loop and (2) whose loop count can
  106. be easily adjusted. This is because we peel a constant number of
  107. iterations into a prologue and epilogue for which we want to avoid
  108. emitting the control part, and a kernel which is to iterate that
  109. constant number of iterations less than the original loop. So the
  110. control part should be a set of insns clearly identified and having
  111. its own iv, not otherwise used in the loop (at-least for now), which
  112. initializes a register before the loop to the number of iterations.
  113. Currently SMS relies on the do-loop pattern to recognize such loops,
  114. where (1) the control part comprises of all insns defining and/or
  115. using a certain 'count' register and (2) the loop count can be
  116. adjusted by modifying this register prior to the loop.
  117. TODO: Rely on cfgloop analysis instead. */
  118. /* This page defines partial-schedule structures and functions for
  119. modulo scheduling. */
  120. typedef struct partial_schedule *partial_schedule_ptr;
  121. typedef struct ps_insn *ps_insn_ptr;
  122. /* The minimum (absolute) cycle that a node of ps was scheduled in. */
  123. #define PS_MIN_CYCLE(ps) (((partial_schedule_ptr)(ps))->min_cycle)
  124. /* The maximum (absolute) cycle that a node of ps was scheduled in. */
  125. #define PS_MAX_CYCLE(ps) (((partial_schedule_ptr)(ps))->max_cycle)
  126. /* Perform signed modulo, always returning a non-negative value. */
  127. #define SMODULO(x,y) ((x) % (y) < 0 ? ((x) % (y) + (y)) : (x) % (y))
  128. /* The number of different iterations the nodes in ps span, assuming
  129. the stage boundaries are placed efficiently. */
  130. #define CALC_STAGE_COUNT(max_cycle,min_cycle,ii) ((max_cycle - min_cycle \
  131. + 1 + ii - 1) / ii)
  132. /* The stage count of ps. */
  133. #define PS_STAGE_COUNT(ps) (((partial_schedule_ptr)(ps))->stage_count)
  134. /* A single instruction in the partial schedule. */
  135. struct ps_insn
  136. {
  137. /* Identifies the instruction to be scheduled. Values smaller than
  138. the ddg's num_nodes refer directly to ddg nodes. A value of
  139. X - num_nodes refers to register move X. */
  140. int id;
  141. /* The (absolute) cycle in which the PS instruction is scheduled.
  142. Same as SCHED_TIME (node). */
  143. int cycle;
  144. /* The next/prev PS_INSN in the same row. */
  145. ps_insn_ptr next_in_row,
  146. prev_in_row;
  147. };
  148. /* Information about a register move that has been added to a partial
  149. schedule. */
  150. struct ps_reg_move_info
  151. {
  152. /* The source of the move is defined by the ps_insn with id DEF.
  153. The destination is used by the ps_insns with the ids in USES. */
  154. int def;
  155. sbitmap uses;
  156. /* The original form of USES' instructions used OLD_REG, but they
  157. should now use NEW_REG. */
  158. rtx old_reg;
  159. rtx new_reg;
  160. /* The number of consecutive stages that the move occupies. */
  161. int num_consecutive_stages;
  162. /* An instruction that sets NEW_REG to the correct value. The first
  163. move associated with DEF will have an rhs of OLD_REG; later moves
  164. use the result of the previous move. */
  165. rtx_insn *insn;
  166. };
  167. typedef struct ps_reg_move_info ps_reg_move_info;
  168. /* Holds the partial schedule as an array of II rows. Each entry of the
  169. array points to a linked list of PS_INSNs, which represents the
  170. instructions that are scheduled for that row. */
  171. struct partial_schedule
  172. {
  173. int ii; /* Number of rows in the partial schedule. */
  174. int history; /* Threshold for conflict checking using DFA. */
  175. /* rows[i] points to linked list of insns scheduled in row i (0<=i<ii). */
  176. ps_insn_ptr *rows;
  177. /* All the moves added for this partial schedule. Index X has
  178. a ps_insn id of X + g->num_nodes. */
  179. vec<ps_reg_move_info> reg_moves;
  180. /* rows_length[i] holds the number of instructions in the row.
  181. It is used only (as an optimization) to back off quickly from
  182. trying to schedule a node in a full row; that is, to avoid running
  183. through futile DFA state transitions. */
  184. int *rows_length;
  185. /* The earliest absolute cycle of an insn in the partial schedule. */
  186. int min_cycle;
  187. /* The latest absolute cycle of an insn in the partial schedule. */
  188. int max_cycle;
  189. ddg_ptr g; /* The DDG of the insns in the partial schedule. */
  190. int stage_count; /* The stage count of the partial schedule. */
  191. };
  192. static partial_schedule_ptr create_partial_schedule (int ii, ddg_ptr, int history);
  193. static void free_partial_schedule (partial_schedule_ptr);
  194. static void reset_partial_schedule (partial_schedule_ptr, int new_ii);
  195. void print_partial_schedule (partial_schedule_ptr, FILE *);
  196. static void verify_partial_schedule (partial_schedule_ptr, sbitmap);
  197. static ps_insn_ptr ps_add_node_check_conflicts (partial_schedule_ptr,
  198. int, int, sbitmap, sbitmap);
  199. static void rotate_partial_schedule (partial_schedule_ptr, int);
  200. void set_row_column_for_ps (partial_schedule_ptr);
  201. static void ps_insert_empty_row (partial_schedule_ptr, int, sbitmap);
  202. static int compute_split_row (sbitmap, int, int, int, ddg_node_ptr);
  203. /* This page defines constants and structures for the modulo scheduling
  204. driver. */
  205. static int sms_order_nodes (ddg_ptr, int, int *, int *);
  206. static void set_node_sched_params (ddg_ptr);
  207. static partial_schedule_ptr sms_schedule_by_order (ddg_ptr, int, int, int *);
  208. static void permute_partial_schedule (partial_schedule_ptr, rtx_insn *);
  209. static void generate_prolog_epilog (partial_schedule_ptr, struct loop *,
  210. rtx, rtx);
  211. static int calculate_stage_count (partial_schedule_ptr, int);
  212. static void calculate_must_precede_follow (ddg_node_ptr, int, int,
  213. int, int, sbitmap, sbitmap, sbitmap);
  214. static int get_sched_window (partial_schedule_ptr, ddg_node_ptr,
  215. sbitmap, int, int *, int *, int *);
  216. static bool try_scheduling_node_in_cycle (partial_schedule_ptr, int, int,
  217. sbitmap, int *, sbitmap, sbitmap);
  218. static void remove_node_from_ps (partial_schedule_ptr, ps_insn_ptr);
  219. #define NODE_ASAP(node) ((node)->aux.count)
  220. #define SCHED_PARAMS(x) (&node_sched_param_vec[x])
  221. #define SCHED_TIME(x) (SCHED_PARAMS (x)->time)
  222. #define SCHED_ROW(x) (SCHED_PARAMS (x)->row)
  223. #define SCHED_STAGE(x) (SCHED_PARAMS (x)->stage)
  224. #define SCHED_COLUMN(x) (SCHED_PARAMS (x)->column)
  225. /* The scheduling parameters held for each node. */
  226. typedef struct node_sched_params
  227. {
  228. int time; /* The absolute scheduling cycle. */
  229. int row; /* Holds time % ii. */
  230. int stage; /* Holds time / ii. */
  231. /* The column of a node inside the ps. If nodes u, v are on the same row,
  232. u will precede v if column (u) < column (v). */
  233. int column;
  234. } *node_sched_params_ptr;
  235. typedef struct node_sched_params node_sched_params;
  236. /* The following three functions are copied from the current scheduler
  237. code in order to use sched_analyze() for computing the dependencies.
  238. They are used when initializing the sched_info structure. */
  239. static const char *
  240. sms_print_insn (const rtx_insn *insn, int aligned ATTRIBUTE_UNUSED)
  241. {
  242. static char tmp[80];
  243. sprintf (tmp, "i%4d", INSN_UID (insn));
  244. return tmp;
  245. }
  246. static void
  247. compute_jump_reg_dependencies (rtx insn ATTRIBUTE_UNUSED,
  248. regset used ATTRIBUTE_UNUSED)
  249. {
  250. }
  251. static struct common_sched_info_def sms_common_sched_info;
  252. static struct sched_deps_info_def sms_sched_deps_info =
  253. {
  254. compute_jump_reg_dependencies,
  255. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  256. NULL,
  257. 0, 0, 0
  258. };
  259. static struct haifa_sched_info sms_sched_info =
  260. {
  261. NULL,
  262. NULL,
  263. NULL,
  264. NULL,
  265. NULL,
  266. sms_print_insn,
  267. NULL,
  268. NULL, /* insn_finishes_block_p */
  269. NULL, NULL,
  270. NULL, NULL,
  271. 0, 0,
  272. NULL, NULL, NULL, NULL,
  273. NULL, NULL,
  274. 0
  275. };
  276. /* Partial schedule instruction ID in PS is a register move. Return
  277. information about it. */
  278. static struct ps_reg_move_info *
  279. ps_reg_move (partial_schedule_ptr ps, int id)
  280. {
  281. gcc_checking_assert (id >= ps->g->num_nodes);
  282. return &ps->reg_moves[id - ps->g->num_nodes];
  283. }
  284. /* Return the rtl instruction that is being scheduled by partial schedule
  285. instruction ID, which belongs to schedule PS. */
  286. static rtx_insn *
  287. ps_rtl_insn (partial_schedule_ptr ps, int id)
  288. {
  289. if (id < ps->g->num_nodes)
  290. return ps->g->nodes[id].insn;
  291. else
  292. return ps_reg_move (ps, id)->insn;
  293. }
  294. /* Partial schedule instruction ID, which belongs to PS, occurred in
  295. the original (unscheduled) loop. Return the first instruction
  296. in the loop that was associated with ps_rtl_insn (PS, ID).
  297. If the instruction had some notes before it, this is the first
  298. of those notes. */
  299. static rtx_insn *
  300. ps_first_note (partial_schedule_ptr ps, int id)
  301. {
  302. gcc_assert (id < ps->g->num_nodes);
  303. return ps->g->nodes[id].first_note;
  304. }
  305. /* Return the number of consecutive stages that are occupied by
  306. partial schedule instruction ID in PS. */
  307. static int
  308. ps_num_consecutive_stages (partial_schedule_ptr ps, int id)
  309. {
  310. if (id < ps->g->num_nodes)
  311. return 1;
  312. else
  313. return ps_reg_move (ps, id)->num_consecutive_stages;
  314. }
  315. /* Given HEAD and TAIL which are the first and last insns in a loop;
  316. return the register which controls the loop. Return zero if it has
  317. more than one occurrence in the loop besides the control part or the
  318. do-loop pattern is not of the form we expect. */
  319. static rtx
  320. doloop_register_get (rtx_insn *head ATTRIBUTE_UNUSED, rtx_insn *tail ATTRIBUTE_UNUSED)
  321. {
  322. #ifdef HAVE_doloop_end
  323. rtx reg, condition;
  324. rtx_insn *insn, *first_insn_not_to_check;
  325. if (!JUMP_P (tail))
  326. return NULL_RTX;
  327. /* TODO: Free SMS's dependence on doloop_condition_get. */
  328. condition = doloop_condition_get (tail);
  329. if (! condition)
  330. return NULL_RTX;
  331. if (REG_P (XEXP (condition, 0)))
  332. reg = XEXP (condition, 0);
  333. else if (GET_CODE (XEXP (condition, 0)) == PLUS
  334. && REG_P (XEXP (XEXP (condition, 0), 0)))
  335. reg = XEXP (XEXP (condition, 0), 0);
  336. else
  337. gcc_unreachable ();
  338. /* Check that the COUNT_REG has no other occurrences in the loop
  339. until the decrement. We assume the control part consists of
  340. either a single (parallel) branch-on-count or a (non-parallel)
  341. branch immediately preceded by a single (decrement) insn. */
  342. first_insn_not_to_check = (GET_CODE (PATTERN (tail)) == PARALLEL ? tail
  343. : prev_nondebug_insn (tail));
  344. for (insn = head; insn != first_insn_not_to_check; insn = NEXT_INSN (insn))
  345. if (!DEBUG_INSN_P (insn) && reg_mentioned_p (reg, insn))
  346. {
  347. if (dump_file)
  348. {
  349. fprintf (dump_file, "SMS count_reg found ");
  350. print_rtl_single (dump_file, reg);
  351. fprintf (dump_file, " outside control in insn:\n");
  352. print_rtl_single (dump_file, insn);
  353. }
  354. return NULL_RTX;
  355. }
  356. return reg;
  357. #else
  358. return NULL_RTX;
  359. #endif
  360. }
  361. /* Check if COUNT_REG is set to a constant in the PRE_HEADER block, so
  362. that the number of iterations is a compile-time constant. If so,
  363. return the rtx_insn that sets COUNT_REG to a constant, and set COUNT to
  364. this constant. Otherwise return 0. */
  365. static rtx_insn *
  366. const_iteration_count (rtx count_reg, basic_block pre_header,
  367. int64_t * count)
  368. {
  369. rtx_insn *insn;
  370. rtx_insn *head, *tail;
  371. if (! pre_header)
  372. return NULL;
  373. get_ebb_head_tail (pre_header, pre_header, &head, &tail);
  374. for (insn = tail; insn != PREV_INSN (head); insn = PREV_INSN (insn))
  375. if (NONDEBUG_INSN_P (insn) && single_set (insn) &&
  376. rtx_equal_p (count_reg, SET_DEST (single_set (insn))))
  377. {
  378. rtx pat = single_set (insn);
  379. if (CONST_INT_P (SET_SRC (pat)))
  380. {
  381. *count = INTVAL (SET_SRC (pat));
  382. return insn;
  383. }
  384. return NULL;
  385. }
  386. return NULL;
  387. }
  388. /* A very simple resource-based lower bound on the initiation interval.
  389. ??? Improve the accuracy of this bound by considering the
  390. utilization of various units. */
  391. static int
  392. res_MII (ddg_ptr g)
  393. {
  394. if (targetm.sched.sms_res_mii)
  395. return targetm.sched.sms_res_mii (g);
  396. return ((g->num_nodes - g->num_debug) / issue_rate);
  397. }
  398. /* A vector that contains the sched data for each ps_insn. */
  399. static vec<node_sched_params> node_sched_param_vec;
  400. /* Allocate sched_params for each node and initialize it. */
  401. static void
  402. set_node_sched_params (ddg_ptr g)
  403. {
  404. node_sched_param_vec.truncate (0);
  405. node_sched_param_vec.safe_grow_cleared (g->num_nodes);
  406. }
  407. /* Make sure that node_sched_param_vec has an entry for every move in PS. */
  408. static void
  409. extend_node_sched_params (partial_schedule_ptr ps)
  410. {
  411. node_sched_param_vec.safe_grow_cleared (ps->g->num_nodes
  412. + ps->reg_moves.length ());
  413. }
  414. /* Update the sched_params (time, row and stage) for node U using the II,
  415. the CYCLE of U and MIN_CYCLE.
  416. We're not simply taking the following
  417. SCHED_STAGE (u) = CALC_STAGE_COUNT (SCHED_TIME (u), min_cycle, ii);
  418. because the stages may not be aligned on cycle 0. */
  419. static void
  420. update_node_sched_params (int u, int ii, int cycle, int min_cycle)
  421. {
  422. int sc_until_cycle_zero;
  423. int stage;
  424. SCHED_TIME (u) = cycle;
  425. SCHED_ROW (u) = SMODULO (cycle, ii);
  426. /* The calculation of stage count is done adding the number
  427. of stages before cycle zero and after cycle zero. */
  428. sc_until_cycle_zero = CALC_STAGE_COUNT (-1, min_cycle, ii);
  429. if (SCHED_TIME (u) < 0)
  430. {
  431. stage = CALC_STAGE_COUNT (-1, SCHED_TIME (u), ii);
  432. SCHED_STAGE (u) = sc_until_cycle_zero - stage;
  433. }
  434. else
  435. {
  436. stage = CALC_STAGE_COUNT (SCHED_TIME (u), 0, ii);
  437. SCHED_STAGE (u) = sc_until_cycle_zero + stage - 1;
  438. }
  439. }
  440. static void
  441. print_node_sched_params (FILE *file, int num_nodes, partial_schedule_ptr ps)
  442. {
  443. int i;
  444. if (! file)
  445. return;
  446. for (i = 0; i < num_nodes; i++)
  447. {
  448. node_sched_params_ptr nsp = SCHED_PARAMS (i);
  449. fprintf (file, "Node = %d; INSN = %d\n", i,
  450. INSN_UID (ps_rtl_insn (ps, i)));
  451. fprintf (file, " asap = %d:\n", NODE_ASAP (&ps->g->nodes[i]));
  452. fprintf (file, " time = %d:\n", nsp->time);
  453. fprintf (file, " stage = %d:\n", nsp->stage);
  454. }
  455. }
  456. /* Set SCHED_COLUMN for each instruction in row ROW of PS. */
  457. static void
  458. set_columns_for_row (partial_schedule_ptr ps, int row)
  459. {
  460. ps_insn_ptr cur_insn;
  461. int column;
  462. column = 0;
  463. for (cur_insn = ps->rows[row]; cur_insn; cur_insn = cur_insn->next_in_row)
  464. SCHED_COLUMN (cur_insn->id) = column++;
  465. }
  466. /* Set SCHED_COLUMN for each instruction in PS. */
  467. static void
  468. set_columns_for_ps (partial_schedule_ptr ps)
  469. {
  470. int row;
  471. for (row = 0; row < ps->ii; row++)
  472. set_columns_for_row (ps, row);
  473. }
  474. /* Try to schedule the move with ps_insn identifier I_REG_MOVE in PS.
  475. Its single predecessor has already been scheduled, as has its
  476. ddg node successors. (The move may have also another move as its
  477. successor, in which case that successor will be scheduled later.)
  478. The move is part of a chain that satisfies register dependencies
  479. between a producing ddg node and various consuming ddg nodes.
  480. If some of these dependencies have a distance of 1 (meaning that
  481. the use is upward-exposed) then DISTANCE1_USES is nonnull and
  482. contains the set of uses with distance-1 dependencies.
  483. DISTANCE1_USES is null otherwise.
  484. MUST_FOLLOW is a scratch bitmap that is big enough to hold
  485. all current ps_insn ids.
  486. Return true on success. */
  487. static bool
  488. schedule_reg_move (partial_schedule_ptr ps, int i_reg_move,
  489. sbitmap distance1_uses, sbitmap must_follow)
  490. {
  491. unsigned int u;
  492. int this_time, this_distance, this_start, this_end, this_latency;
  493. int start, end, c, ii;
  494. sbitmap_iterator sbi;
  495. ps_reg_move_info *move;
  496. rtx_insn *this_insn;
  497. ps_insn_ptr psi;
  498. move = ps_reg_move (ps, i_reg_move);
  499. ii = ps->ii;
  500. if (dump_file)
  501. {
  502. fprintf (dump_file, "Scheduling register move INSN %d; ii = %d"
  503. ", min cycle = %d\n\n", INSN_UID (move->insn), ii,
  504. PS_MIN_CYCLE (ps));
  505. print_rtl_single (dump_file, move->insn);
  506. fprintf (dump_file, "\n%11s %11s %5s\n", "start", "end", "time");
  507. fprintf (dump_file, "=========== =========== =====\n");
  508. }
  509. start = INT_MIN;
  510. end = INT_MAX;
  511. /* For dependencies of distance 1 between a producer ddg node A
  512. and consumer ddg node B, we have a chain of dependencies:
  513. A --(T,L1,1)--> M1 --(T,L2,0)--> M2 ... --(T,Ln,0)--> B
  514. where Mi is the ith move. For dependencies of distance 0 between
  515. a producer ddg node A and consumer ddg node C, we have a chain of
  516. dependencies:
  517. A --(T,L1',0)--> M1' --(T,L2',0)--> M2' ... --(T,Ln',0)--> C
  518. where Mi' occupies the same position as Mi but occurs a stage later.
  519. We can only schedule each move once, so if we have both types of
  520. chain, we model the second as:
  521. A --(T,L1',1)--> M1 --(T,L2',0)--> M2 ... --(T,Ln',-1)--> C
  522. First handle the dependencies between the previously-scheduled
  523. predecessor and the move. */
  524. this_insn = ps_rtl_insn (ps, move->def);
  525. this_latency = insn_latency (this_insn, move->insn);
  526. this_distance = distance1_uses && move->def < ps->g->num_nodes ? 1 : 0;
  527. this_time = SCHED_TIME (move->def) - this_distance * ii;
  528. this_start = this_time + this_latency;
  529. this_end = this_time + ii;
  530. if (dump_file)
  531. fprintf (dump_file, "%11d %11d %5d %d --(T,%d,%d)--> %d\n",
  532. this_start, this_end, SCHED_TIME (move->def),
  533. INSN_UID (this_insn), this_latency, this_distance,
  534. INSN_UID (move->insn));
  535. if (start < this_start)
  536. start = this_start;
  537. if (end > this_end)
  538. end = this_end;
  539. /* Handle the dependencies between the move and previously-scheduled
  540. successors. */
  541. EXECUTE_IF_SET_IN_BITMAP (move->uses, 0, u, sbi)
  542. {
  543. this_insn = ps_rtl_insn (ps, u);
  544. this_latency = insn_latency (move->insn, this_insn);
  545. if (distance1_uses && !bitmap_bit_p (distance1_uses, u))
  546. this_distance = -1;
  547. else
  548. this_distance = 0;
  549. this_time = SCHED_TIME (u) + this_distance * ii;
  550. this_start = this_time - ii;
  551. this_end = this_time - this_latency;
  552. if (dump_file)
  553. fprintf (dump_file, "%11d %11d %5d %d --(T,%d,%d)--> %d\n",
  554. this_start, this_end, SCHED_TIME (u), INSN_UID (move->insn),
  555. this_latency, this_distance, INSN_UID (this_insn));
  556. if (start < this_start)
  557. start = this_start;
  558. if (end > this_end)
  559. end = this_end;
  560. }
  561. if (dump_file)
  562. {
  563. fprintf (dump_file, "----------- ----------- -----\n");
  564. fprintf (dump_file, "%11d %11d %5s %s\n", start, end, "", "(max, min)");
  565. }
  566. bitmap_clear (must_follow);
  567. bitmap_set_bit (must_follow, move->def);
  568. start = MAX (start, end - (ii - 1));
  569. for (c = end; c >= start; c--)
  570. {
  571. psi = ps_add_node_check_conflicts (ps, i_reg_move, c,
  572. move->uses, must_follow);
  573. if (psi)
  574. {
  575. update_node_sched_params (i_reg_move, ii, c, PS_MIN_CYCLE (ps));
  576. if (dump_file)
  577. fprintf (dump_file, "\nScheduled register move INSN %d at"
  578. " time %d, row %d\n\n", INSN_UID (move->insn), c,
  579. SCHED_ROW (i_reg_move));
  580. return true;
  581. }
  582. }
  583. if (dump_file)
  584. fprintf (dump_file, "\nNo available slot\n\n");
  585. return false;
  586. }
  587. /*
  588. Breaking intra-loop register anti-dependences:
  589. Each intra-loop register anti-dependence implies a cross-iteration true
  590. dependence of distance 1. Therefore, we can remove such false dependencies
  591. and figure out if the partial schedule broke them by checking if (for a
  592. true-dependence of distance 1): SCHED_TIME (def) < SCHED_TIME (use) and
  593. if so generate a register move. The number of such moves is equal to:
  594. SCHED_TIME (use) - SCHED_TIME (def) { 0 broken
  595. nreg_moves = ----------------------------------- + 1 - { dependence.
  596. ii { 1 if not.
  597. */
  598. static bool
  599. schedule_reg_moves (partial_schedule_ptr ps)
  600. {
  601. ddg_ptr g = ps->g;
  602. int ii = ps->ii;
  603. int i;
  604. for (i = 0; i < g->num_nodes; i++)
  605. {
  606. ddg_node_ptr u = &g->nodes[i];
  607. ddg_edge_ptr e;
  608. int nreg_moves = 0, i_reg_move;
  609. rtx prev_reg, old_reg;
  610. int first_move;
  611. int distances[2];
  612. sbitmap must_follow;
  613. sbitmap distance1_uses;
  614. rtx set = single_set (u->insn);
  615. /* Skip instructions that do not set a register. */
  616. if ((set && !REG_P (SET_DEST (set))))
  617. continue;
  618. /* Compute the number of reg_moves needed for u, by looking at life
  619. ranges started at u (excluding self-loops). */
  620. distances[0] = distances[1] = false;
  621. for (e = u->out; e; e = e->next_out)
  622. if (e->type == TRUE_DEP && e->dest != e->src)
  623. {
  624. int nreg_moves4e = (SCHED_TIME (e->dest->cuid)
  625. - SCHED_TIME (e->src->cuid)) / ii;
  626. if (e->distance == 1)
  627. nreg_moves4e = (SCHED_TIME (e->dest->cuid)
  628. - SCHED_TIME (e->src->cuid) + ii) / ii;
  629. /* If dest precedes src in the schedule of the kernel, then dest
  630. will read before src writes and we can save one reg_copy. */
  631. if (SCHED_ROW (e->dest->cuid) == SCHED_ROW (e->src->cuid)
  632. && SCHED_COLUMN (e->dest->cuid) < SCHED_COLUMN (e->src->cuid))
  633. nreg_moves4e--;
  634. if (nreg_moves4e >= 1)
  635. {
  636. /* !single_set instructions are not supported yet and
  637. thus we do not except to encounter them in the loop
  638. except from the doloop part. For the latter case
  639. we assume no regmoves are generated as the doloop
  640. instructions are tied to the branch with an edge. */
  641. gcc_assert (set);
  642. /* If the instruction contains auto-inc register then
  643. validate that the regmov is being generated for the
  644. target regsiter rather then the inc'ed register. */
  645. gcc_assert (!autoinc_var_is_used_p (u->insn, e->dest->insn));
  646. }
  647. if (nreg_moves4e)
  648. {
  649. gcc_assert (e->distance < 2);
  650. distances[e->distance] = true;
  651. }
  652. nreg_moves = MAX (nreg_moves, nreg_moves4e);
  653. }
  654. if (nreg_moves == 0)
  655. continue;
  656. /* Create NREG_MOVES register moves. */
  657. first_move = ps->reg_moves.length ();
  658. ps->reg_moves.safe_grow_cleared (first_move + nreg_moves);
  659. extend_node_sched_params (ps);
  660. /* Record the moves associated with this node. */
  661. first_move += ps->g->num_nodes;
  662. /* Generate each move. */
  663. old_reg = prev_reg = SET_DEST (single_set (u->insn));
  664. for (i_reg_move = 0; i_reg_move < nreg_moves; i_reg_move++)
  665. {
  666. ps_reg_move_info *move = ps_reg_move (ps, first_move + i_reg_move);
  667. move->def = i_reg_move > 0 ? first_move + i_reg_move - 1 : i;
  668. move->uses = sbitmap_alloc (first_move + nreg_moves);
  669. move->old_reg = old_reg;
  670. move->new_reg = gen_reg_rtx (GET_MODE (prev_reg));
  671. move->num_consecutive_stages = distances[0] && distances[1] ? 2 : 1;
  672. move->insn = as_a <rtx_insn *> (gen_move_insn (move->new_reg,
  673. copy_rtx (prev_reg)));
  674. bitmap_clear (move->uses);
  675. prev_reg = move->new_reg;
  676. }
  677. distance1_uses = distances[1] ? sbitmap_alloc (g->num_nodes) : NULL;
  678. if (distance1_uses)
  679. bitmap_clear (distance1_uses);
  680. /* Every use of the register defined by node may require a different
  681. copy of this register, depending on the time the use is scheduled.
  682. Record which uses require which move results. */
  683. for (e = u->out; e; e = e->next_out)
  684. if (e->type == TRUE_DEP && e->dest != e->src)
  685. {
  686. int dest_copy = (SCHED_TIME (e->dest->cuid)
  687. - SCHED_TIME (e->src->cuid)) / ii;
  688. if (e->distance == 1)
  689. dest_copy = (SCHED_TIME (e->dest->cuid)
  690. - SCHED_TIME (e->src->cuid) + ii) / ii;
  691. if (SCHED_ROW (e->dest->cuid) == SCHED_ROW (e->src->cuid)
  692. && SCHED_COLUMN (e->dest->cuid) < SCHED_COLUMN (e->src->cuid))
  693. dest_copy--;
  694. if (dest_copy)
  695. {
  696. ps_reg_move_info *move;
  697. move = ps_reg_move (ps, first_move + dest_copy - 1);
  698. bitmap_set_bit (move->uses, e->dest->cuid);
  699. if (e->distance == 1)
  700. bitmap_set_bit (distance1_uses, e->dest->cuid);
  701. }
  702. }
  703. must_follow = sbitmap_alloc (first_move + nreg_moves);
  704. for (i_reg_move = 0; i_reg_move < nreg_moves; i_reg_move++)
  705. if (!schedule_reg_move (ps, first_move + i_reg_move,
  706. distance1_uses, must_follow))
  707. break;
  708. sbitmap_free (must_follow);
  709. if (distance1_uses)
  710. sbitmap_free (distance1_uses);
  711. if (i_reg_move < nreg_moves)
  712. return false;
  713. }
  714. return true;
  715. }
  716. /* Emit the moves associatied with PS. Apply the substitutions
  717. associated with them. */
  718. static void
  719. apply_reg_moves (partial_schedule_ptr ps)
  720. {
  721. ps_reg_move_info *move;
  722. int i;
  723. FOR_EACH_VEC_ELT (ps->reg_moves, i, move)
  724. {
  725. unsigned int i_use;
  726. sbitmap_iterator sbi;
  727. EXECUTE_IF_SET_IN_BITMAP (move->uses, 0, i_use, sbi)
  728. {
  729. replace_rtx (ps->g->nodes[i_use].insn, move->old_reg, move->new_reg);
  730. df_insn_rescan (ps->g->nodes[i_use].insn);
  731. }
  732. }
  733. }
  734. /* Bump the SCHED_TIMEs of all nodes by AMOUNT. Set the values of
  735. SCHED_ROW and SCHED_STAGE. Instruction scheduled on cycle AMOUNT
  736. will move to cycle zero. */
  737. static void
  738. reset_sched_times (partial_schedule_ptr ps, int amount)
  739. {
  740. int row;
  741. int ii = ps->ii;
  742. ps_insn_ptr crr_insn;
  743. for (row = 0; row < ii; row++)
  744. for (crr_insn = ps->rows[row]; crr_insn; crr_insn = crr_insn->next_in_row)
  745. {
  746. int u = crr_insn->id;
  747. int normalized_time = SCHED_TIME (u) - amount;
  748. int new_min_cycle = PS_MIN_CYCLE (ps) - amount;
  749. if (dump_file)
  750. {
  751. /* Print the scheduling times after the rotation. */
  752. rtx_insn *insn = ps_rtl_insn (ps, u);
  753. fprintf (dump_file, "crr_insn->node=%d (insn id %d), "
  754. "crr_insn->cycle=%d, min_cycle=%d", u,
  755. INSN_UID (insn), normalized_time, new_min_cycle);
  756. if (JUMP_P (insn))
  757. fprintf (dump_file, " (branch)");
  758. fprintf (dump_file, "\n");
  759. }
  760. gcc_assert (SCHED_TIME (u) >= ps->min_cycle);
  761. gcc_assert (SCHED_TIME (u) <= ps->max_cycle);
  762. crr_insn->cycle = normalized_time;
  763. update_node_sched_params (u, ii, normalized_time, new_min_cycle);
  764. }
  765. }
  766. /* Permute the insns according to their order in PS, from row 0 to
  767. row ii-1, and position them right before LAST. This schedules
  768. the insns of the loop kernel. */
  769. static void
  770. permute_partial_schedule (partial_schedule_ptr ps, rtx_insn *last)
  771. {
  772. int ii = ps->ii;
  773. int row;
  774. ps_insn_ptr ps_ij;
  775. for (row = 0; row < ii ; row++)
  776. for (ps_ij = ps->rows[row]; ps_ij; ps_ij = ps_ij->next_in_row)
  777. {
  778. rtx_insn *insn = ps_rtl_insn (ps, ps_ij->id);
  779. if (PREV_INSN (last) != insn)
  780. {
  781. if (ps_ij->id < ps->g->num_nodes)
  782. reorder_insns_nobb (ps_first_note (ps, ps_ij->id), insn,
  783. PREV_INSN (last));
  784. else
  785. add_insn_before (insn, last, NULL);
  786. }
  787. }
  788. }
  789. /* Set bitmaps TMP_FOLLOW and TMP_PRECEDE to MUST_FOLLOW and MUST_PRECEDE
  790. respectively only if cycle C falls on the border of the scheduling
  791. window boundaries marked by START and END cycles. STEP is the
  792. direction of the window. */
  793. static inline void
  794. set_must_precede_follow (sbitmap *tmp_follow, sbitmap must_follow,
  795. sbitmap *tmp_precede, sbitmap must_precede, int c,
  796. int start, int end, int step)
  797. {
  798. *tmp_precede = NULL;
  799. *tmp_follow = NULL;
  800. if (c == start)
  801. {
  802. if (step == 1)
  803. *tmp_precede = must_precede;
  804. else /* step == -1. */
  805. *tmp_follow = must_follow;
  806. }
  807. if (c == end - step)
  808. {
  809. if (step == 1)
  810. *tmp_follow = must_follow;
  811. else /* step == -1. */
  812. *tmp_precede = must_precede;
  813. }
  814. }
  815. /* Return True if the branch can be moved to row ii-1 while
  816. normalizing the partial schedule PS to start from cycle zero and thus
  817. optimize the SC. Otherwise return False. */
  818. static bool
  819. optimize_sc (partial_schedule_ptr ps, ddg_ptr g)
  820. {
  821. int amount = PS_MIN_CYCLE (ps);
  822. sbitmap sched_nodes = sbitmap_alloc (g->num_nodes);
  823. int start, end, step;
  824. int ii = ps->ii;
  825. bool ok = false;
  826. int stage_count, stage_count_curr;
  827. /* Compare the SC after normalization and SC after bringing the branch
  828. to row ii-1. If they are equal just bail out. */
  829. stage_count = calculate_stage_count (ps, amount);
  830. stage_count_curr =
  831. calculate_stage_count (ps, SCHED_TIME (g->closing_branch->cuid) - (ii - 1));
  832. if (stage_count == stage_count_curr)
  833. {
  834. if (dump_file)
  835. fprintf (dump_file, "SMS SC already optimized.\n");
  836. ok = false;
  837. goto clear;
  838. }
  839. if (dump_file)
  840. {
  841. fprintf (dump_file, "SMS Trying to optimize branch location\n");
  842. fprintf (dump_file, "SMS partial schedule before trial:\n");
  843. print_partial_schedule (ps, dump_file);
  844. }
  845. /* First, normalize the partial scheduling. */
  846. reset_sched_times (ps, amount);
  847. rotate_partial_schedule (ps, amount);
  848. if (dump_file)
  849. {
  850. fprintf (dump_file,
  851. "SMS partial schedule after normalization (ii, %d, SC %d):\n",
  852. ii, stage_count);
  853. print_partial_schedule (ps, dump_file);
  854. }
  855. if (SMODULO (SCHED_TIME (g->closing_branch->cuid), ii) == ii - 1)
  856. {
  857. ok = true;
  858. goto clear;
  859. }
  860. bitmap_ones (sched_nodes);
  861. /* Calculate the new placement of the branch. It should be in row
  862. ii-1 and fall into it's scheduling window. */
  863. if (get_sched_window (ps, g->closing_branch, sched_nodes, ii, &start,
  864. &step, &end) == 0)
  865. {
  866. bool success;
  867. ps_insn_ptr next_ps_i;
  868. int branch_cycle = SCHED_TIME (g->closing_branch->cuid);
  869. int row = SMODULO (branch_cycle, ps->ii);
  870. int num_splits = 0;
  871. sbitmap must_precede, must_follow, tmp_precede, tmp_follow;
  872. int c;
  873. if (dump_file)
  874. fprintf (dump_file, "\nTrying to schedule node %d "
  875. "INSN = %d in (%d .. %d) step %d\n",
  876. g->closing_branch->cuid,
  877. (INSN_UID (g->closing_branch->insn)), start, end, step);
  878. gcc_assert ((step > 0 && start < end) || (step < 0 && start > end));
  879. if (step == 1)
  880. {
  881. c = start + ii - SMODULO (start, ii) - 1;
  882. gcc_assert (c >= start);
  883. if (c >= end)
  884. {
  885. ok = false;
  886. if (dump_file)
  887. fprintf (dump_file,
  888. "SMS failed to schedule branch at cycle: %d\n", c);
  889. goto clear;
  890. }
  891. }
  892. else
  893. {
  894. c = start - SMODULO (start, ii) - 1;
  895. gcc_assert (c <= start);
  896. if (c <= end)
  897. {
  898. if (dump_file)
  899. fprintf (dump_file,
  900. "SMS failed to schedule branch at cycle: %d\n", c);
  901. ok = false;
  902. goto clear;
  903. }
  904. }
  905. must_precede = sbitmap_alloc (g->num_nodes);
  906. must_follow = sbitmap_alloc (g->num_nodes);
  907. /* Try to schedule the branch is it's new cycle. */
  908. calculate_must_precede_follow (g->closing_branch, start, end,
  909. step, ii, sched_nodes,
  910. must_precede, must_follow);
  911. set_must_precede_follow (&tmp_follow, must_follow, &tmp_precede,
  912. must_precede, c, start, end, step);
  913. /* Find the element in the partial schedule related to the closing
  914. branch so we can remove it from it's current cycle. */
  915. for (next_ps_i = ps->rows[row];
  916. next_ps_i; next_ps_i = next_ps_i->next_in_row)
  917. if (next_ps_i->id == g->closing_branch->cuid)
  918. break;
  919. remove_node_from_ps (ps, next_ps_i);
  920. success =
  921. try_scheduling_node_in_cycle (ps, g->closing_branch->cuid, c,
  922. sched_nodes, &num_splits,
  923. tmp_precede, tmp_follow);
  924. gcc_assert (num_splits == 0);
  925. if (!success)
  926. {
  927. if (dump_file)
  928. fprintf (dump_file,
  929. "SMS failed to schedule branch at cycle: %d, "
  930. "bringing it back to cycle %d\n", c, branch_cycle);
  931. /* The branch was failed to be placed in row ii - 1.
  932. Put it back in it's original place in the partial
  933. schedualing. */
  934. set_must_precede_follow (&tmp_follow, must_follow, &tmp_precede,
  935. must_precede, branch_cycle, start, end,
  936. step);
  937. success =
  938. try_scheduling_node_in_cycle (ps, g->closing_branch->cuid,
  939. branch_cycle, sched_nodes,
  940. &num_splits, tmp_precede,
  941. tmp_follow);
  942. gcc_assert (success && (num_splits == 0));
  943. ok = false;
  944. }
  945. else
  946. {
  947. /* The branch is placed in row ii - 1. */
  948. if (dump_file)
  949. fprintf (dump_file,
  950. "SMS success in moving branch to cycle %d\n", c);
  951. update_node_sched_params (g->closing_branch->cuid, ii, c,
  952. PS_MIN_CYCLE (ps));
  953. ok = true;
  954. }
  955. free (must_precede);
  956. free (must_follow);
  957. }
  958. clear:
  959. free (sched_nodes);
  960. return ok;
  961. }
  962. static void
  963. duplicate_insns_of_cycles (partial_schedule_ptr ps, int from_stage,
  964. int to_stage, rtx count_reg)
  965. {
  966. int row;
  967. ps_insn_ptr ps_ij;
  968. for (row = 0; row < ps->ii; row++)
  969. for (ps_ij = ps->rows[row]; ps_ij; ps_ij = ps_ij->next_in_row)
  970. {
  971. int u = ps_ij->id;
  972. int first_u, last_u;
  973. rtx_insn *u_insn;
  974. /* Do not duplicate any insn which refers to count_reg as it
  975. belongs to the control part.
  976. The closing branch is scheduled as well and thus should
  977. be ignored.
  978. TODO: This should be done by analyzing the control part of
  979. the loop. */
  980. u_insn = ps_rtl_insn (ps, u);
  981. if (reg_mentioned_p (count_reg, u_insn)
  982. || JUMP_P (u_insn))
  983. continue;
  984. first_u = SCHED_STAGE (u);
  985. last_u = first_u + ps_num_consecutive_stages (ps, u) - 1;
  986. if (from_stage <= last_u && to_stage >= first_u)
  987. {
  988. if (u < ps->g->num_nodes)
  989. duplicate_insn_chain (ps_first_note (ps, u), u_insn);
  990. else
  991. emit_insn (copy_rtx (PATTERN (u_insn)));
  992. }
  993. }
  994. }
  995. /* Generate the instructions (including reg_moves) for prolog & epilog. */
  996. static void
  997. generate_prolog_epilog (partial_schedule_ptr ps, struct loop *loop,
  998. rtx count_reg, rtx count_init)
  999. {
  1000. int i;
  1001. int last_stage = PS_STAGE_COUNT (ps) - 1;
  1002. edge e;
  1003. /* Generate the prolog, inserting its insns on the loop-entry edge. */
  1004. start_sequence ();
  1005. if (!count_init)
  1006. {
  1007. /* Generate instructions at the beginning of the prolog to
  1008. adjust the loop count by STAGE_COUNT. If loop count is constant
  1009. (count_init), this constant is adjusted by STAGE_COUNT in
  1010. generate_prolog_epilog function. */
  1011. rtx sub_reg = NULL_RTX;
  1012. sub_reg = expand_simple_binop (GET_MODE (count_reg), MINUS, count_reg,
  1013. gen_int_mode (last_stage,
  1014. GET_MODE (count_reg)),
  1015. count_reg, 1, OPTAB_DIRECT);
  1016. gcc_assert (REG_P (sub_reg));
  1017. if (REGNO (sub_reg) != REGNO (count_reg))
  1018. emit_move_insn (count_reg, sub_reg);
  1019. }
  1020. for (i = 0; i < last_stage; i++)
  1021. duplicate_insns_of_cycles (ps, 0, i, count_reg);
  1022. /* Put the prolog on the entry edge. */
  1023. e = loop_preheader_edge (loop);
  1024. split_edge_and_insert (e, get_insns ());
  1025. if (!flag_resched_modulo_sched)
  1026. e->dest->flags |= BB_DISABLE_SCHEDULE;
  1027. end_sequence ();
  1028. /* Generate the epilog, inserting its insns on the loop-exit edge. */
  1029. start_sequence ();
  1030. for (i = 0; i < last_stage; i++)
  1031. duplicate_insns_of_cycles (ps, i + 1, last_stage, count_reg);
  1032. /* Put the epilogue on the exit edge. */
  1033. gcc_assert (single_exit (loop));
  1034. e = single_exit (loop);
  1035. split_edge_and_insert (e, get_insns ());
  1036. if (!flag_resched_modulo_sched)
  1037. e->dest->flags |= BB_DISABLE_SCHEDULE;
  1038. end_sequence ();
  1039. }
  1040. /* Mark LOOP as software pipelined so the later
  1041. scheduling passes don't touch it. */
  1042. static void
  1043. mark_loop_unsched (struct loop *loop)
  1044. {
  1045. unsigned i;
  1046. basic_block *bbs = get_loop_body (loop);
  1047. for (i = 0; i < loop->num_nodes; i++)
  1048. bbs[i]->flags |= BB_DISABLE_SCHEDULE;
  1049. free (bbs);
  1050. }
  1051. /* Return true if all the BBs of the loop are empty except the
  1052. loop header. */
  1053. static bool
  1054. loop_single_full_bb_p (struct loop *loop)
  1055. {
  1056. unsigned i;
  1057. basic_block *bbs = get_loop_body (loop);
  1058. for (i = 0; i < loop->num_nodes ; i++)
  1059. {
  1060. rtx_insn *head, *tail;
  1061. bool empty_bb = true;
  1062. if (bbs[i] == loop->header)
  1063. continue;
  1064. /* Make sure that basic blocks other than the header
  1065. have only notes labels or jumps. */
  1066. get_ebb_head_tail (bbs[i], bbs[i], &head, &tail);
  1067. for (; head != NEXT_INSN (tail); head = NEXT_INSN (head))
  1068. {
  1069. if (NOTE_P (head) || LABEL_P (head)
  1070. || (INSN_P (head) && (DEBUG_INSN_P (head) || JUMP_P (head))))
  1071. continue;
  1072. empty_bb = false;
  1073. break;
  1074. }
  1075. if (! empty_bb)
  1076. {
  1077. free (bbs);
  1078. return false;
  1079. }
  1080. }
  1081. free (bbs);
  1082. return true;
  1083. }
  1084. /* Dump file:line from INSN's location info to dump_file. */
  1085. static void
  1086. dump_insn_location (rtx_insn *insn)
  1087. {
  1088. if (dump_file && INSN_HAS_LOCATION (insn))
  1089. {
  1090. expanded_location xloc = insn_location (insn);
  1091. fprintf (dump_file, " %s:%i", xloc.file, xloc.line);
  1092. }
  1093. }
  1094. /* A simple loop from SMS point of view; it is a loop that is composed of
  1095. either a single basic block or two BBs - a header and a latch. */
  1096. #define SIMPLE_SMS_LOOP_P(loop) ((loop->num_nodes < 3 ) \
  1097. && (EDGE_COUNT (loop->latch->preds) == 1) \
  1098. && (EDGE_COUNT (loop->latch->succs) == 1))
  1099. /* Return true if the loop is in its canonical form and false if not.
  1100. i.e. SIMPLE_SMS_LOOP_P and have one preheader block, and single exit. */
  1101. static bool
  1102. loop_canon_p (struct loop *loop)
  1103. {
  1104. if (loop->inner || !loop_outer (loop))
  1105. {
  1106. if (dump_file)
  1107. fprintf (dump_file, "SMS loop inner or !loop_outer\n");
  1108. return false;
  1109. }
  1110. if (!single_exit (loop))
  1111. {
  1112. if (dump_file)
  1113. {
  1114. rtx_insn *insn = BB_END (loop->header);
  1115. fprintf (dump_file, "SMS loop many exits");
  1116. dump_insn_location (insn);
  1117. fprintf (dump_file, "\n");
  1118. }
  1119. return false;
  1120. }
  1121. if (! SIMPLE_SMS_LOOP_P (loop) && ! loop_single_full_bb_p (loop))
  1122. {
  1123. if (dump_file)
  1124. {
  1125. rtx_insn *insn = BB_END (loop->header);
  1126. fprintf (dump_file, "SMS loop many BBs.");
  1127. dump_insn_location (insn);
  1128. fprintf (dump_file, "\n");
  1129. }
  1130. return false;
  1131. }
  1132. return true;
  1133. }
  1134. /* If there are more than one entry for the loop,
  1135. make it one by splitting the first entry edge and
  1136. redirecting the others to the new BB. */
  1137. static void
  1138. canon_loop (struct loop *loop)
  1139. {
  1140. edge e;
  1141. edge_iterator i;
  1142. /* Avoid annoying special cases of edges going to exit
  1143. block. */
  1144. FOR_EACH_EDGE (e, i, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
  1145. if ((e->flags & EDGE_FALLTHRU) && (EDGE_COUNT (e->src->succs) > 1))
  1146. split_edge (e);
  1147. if (loop->latch == loop->header
  1148. || EDGE_COUNT (loop->latch->succs) > 1)
  1149. {
  1150. FOR_EACH_EDGE (e, i, loop->header->preds)
  1151. if (e->src == loop->latch)
  1152. break;
  1153. split_edge (e);
  1154. }
  1155. }
  1156. /* Setup infos. */
  1157. static void
  1158. setup_sched_infos (void)
  1159. {
  1160. memcpy (&sms_common_sched_info, &haifa_common_sched_info,
  1161. sizeof (sms_common_sched_info));
  1162. sms_common_sched_info.sched_pass_id = SCHED_SMS_PASS;
  1163. common_sched_info = &sms_common_sched_info;
  1164. sched_deps_info = &sms_sched_deps_info;
  1165. current_sched_info = &sms_sched_info;
  1166. }
  1167. /* Probability in % that the sms-ed loop rolls enough so that optimized
  1168. version may be entered. Just a guess. */
  1169. #define PROB_SMS_ENOUGH_ITERATIONS 80
  1170. /* Used to calculate the upper bound of ii. */
  1171. #define MAXII_FACTOR 2
  1172. /* Main entry point, perform SMS scheduling on the loops of the function
  1173. that consist of single basic blocks. */
  1174. static void
  1175. sms_schedule (void)
  1176. {
  1177. rtx_insn *insn;
  1178. ddg_ptr *g_arr, g;
  1179. int * node_order;
  1180. int maxii, max_asap;
  1181. partial_schedule_ptr ps;
  1182. basic_block bb = NULL;
  1183. struct loop *loop;
  1184. basic_block condition_bb = NULL;
  1185. edge latch_edge;
  1186. gcov_type trip_count = 0;
  1187. loop_optimizer_init (LOOPS_HAVE_PREHEADERS
  1188. | LOOPS_HAVE_RECORDED_EXITS);
  1189. if (number_of_loops (cfun) <= 1)
  1190. {
  1191. loop_optimizer_finalize ();
  1192. return; /* There are no loops to schedule. */
  1193. }
  1194. /* Initialize issue_rate. */
  1195. if (targetm.sched.issue_rate)
  1196. {
  1197. int temp = reload_completed;
  1198. reload_completed = 1;
  1199. issue_rate = targetm.sched.issue_rate ();
  1200. reload_completed = temp;
  1201. }
  1202. else
  1203. issue_rate = 1;
  1204. /* Initialize the scheduler. */
  1205. setup_sched_infos ();
  1206. haifa_sched_init ();
  1207. /* Allocate memory to hold the DDG array one entry for each loop.
  1208. We use loop->num as index into this array. */
  1209. g_arr = XCNEWVEC (ddg_ptr, number_of_loops (cfun));
  1210. if (dump_file)
  1211. {
  1212. fprintf (dump_file, "\n\nSMS analysis phase\n");
  1213. fprintf (dump_file, "===================\n\n");
  1214. }
  1215. /* Build DDGs for all the relevant loops and hold them in G_ARR
  1216. indexed by the loop index. */
  1217. FOR_EACH_LOOP (loop, 0)
  1218. {
  1219. rtx_insn *head, *tail;
  1220. rtx count_reg;
  1221. /* For debugging. */
  1222. if (dbg_cnt (sms_sched_loop) == false)
  1223. {
  1224. if (dump_file)
  1225. fprintf (dump_file, "SMS reached max limit... \n");
  1226. break;
  1227. }
  1228. if (dump_file)
  1229. {
  1230. rtx_insn *insn = BB_END (loop->header);
  1231. fprintf (dump_file, "SMS loop num: %d", loop->num);
  1232. dump_insn_location (insn);
  1233. fprintf (dump_file, "\n");
  1234. }
  1235. if (! loop_canon_p (loop))
  1236. continue;
  1237. if (! loop_single_full_bb_p (loop))
  1238. {
  1239. if (dump_file)
  1240. fprintf (dump_file, "SMS not loop_single_full_bb_p\n");
  1241. continue;
  1242. }
  1243. bb = loop->header;
  1244. get_ebb_head_tail (bb, bb, &head, &tail);
  1245. latch_edge = loop_latch_edge (loop);
  1246. gcc_assert (single_exit (loop));
  1247. if (single_exit (loop)->count)
  1248. trip_count = latch_edge->count / single_exit (loop)->count;
  1249. /* Perform SMS only on loops that their average count is above threshold. */
  1250. if ( latch_edge->count
  1251. && (latch_edge->count < single_exit (loop)->count * SMS_LOOP_AVERAGE_COUNT_THRESHOLD))
  1252. {
  1253. if (dump_file)
  1254. {
  1255. dump_insn_location (tail);
  1256. fprintf (dump_file, "\nSMS single-bb-loop\n");
  1257. if (profile_info && flag_branch_probabilities)
  1258. {
  1259. fprintf (dump_file, "SMS loop-count ");
  1260. fprintf (dump_file, "%"PRId64,
  1261. (int64_t) bb->count);
  1262. fprintf (dump_file, "\n");
  1263. fprintf (dump_file, "SMS trip-count ");
  1264. fprintf (dump_file, "%"PRId64,
  1265. (int64_t) trip_count);
  1266. fprintf (dump_file, "\n");
  1267. fprintf (dump_file, "SMS profile-sum-max ");
  1268. fprintf (dump_file, "%"PRId64,
  1269. (int64_t) profile_info->sum_max);
  1270. fprintf (dump_file, "\n");
  1271. }
  1272. }
  1273. continue;
  1274. }
  1275. /* Make sure this is a doloop. */
  1276. if ( !(count_reg = doloop_register_get (head, tail)))
  1277. {
  1278. if (dump_file)
  1279. fprintf (dump_file, "SMS doloop_register_get failed\n");
  1280. continue;
  1281. }
  1282. /* Don't handle BBs with calls or barriers
  1283. or !single_set with the exception of instructions that include
  1284. count_reg---these instructions are part of the control part
  1285. that do-loop recognizes.
  1286. ??? Should handle insns defining subregs. */
  1287. for (insn = head; insn != NEXT_INSN (tail); insn = NEXT_INSN (insn))
  1288. {
  1289. rtx set;
  1290. if (CALL_P (insn)
  1291. || BARRIER_P (insn)
  1292. || (NONDEBUG_INSN_P (insn) && !JUMP_P (insn)
  1293. && !single_set (insn) && GET_CODE (PATTERN (insn)) != USE
  1294. && !reg_mentioned_p (count_reg, insn))
  1295. || (INSN_P (insn) && (set = single_set (insn))
  1296. && GET_CODE (SET_DEST (set)) == SUBREG))
  1297. break;
  1298. }
  1299. if (insn != NEXT_INSN (tail))
  1300. {
  1301. if (dump_file)
  1302. {
  1303. if (CALL_P (insn))
  1304. fprintf (dump_file, "SMS loop-with-call\n");
  1305. else if (BARRIER_P (insn))
  1306. fprintf (dump_file, "SMS loop-with-barrier\n");
  1307. else if ((NONDEBUG_INSN_P (insn) && !JUMP_P (insn)
  1308. && !single_set (insn) && GET_CODE (PATTERN (insn)) != USE))
  1309. fprintf (dump_file, "SMS loop-with-not-single-set\n");
  1310. else
  1311. fprintf (dump_file, "SMS loop with subreg in lhs\n");
  1312. print_rtl_single (dump_file, insn);
  1313. }
  1314. continue;
  1315. }
  1316. /* Always schedule the closing branch with the rest of the
  1317. instructions. The branch is rotated to be in row ii-1 at the
  1318. end of the scheduling procedure to make sure it's the last
  1319. instruction in the iteration. */
  1320. if (! (g = create_ddg (bb, 1)))
  1321. {
  1322. if (dump_file)
  1323. fprintf (dump_file, "SMS create_ddg failed\n");
  1324. continue;
  1325. }
  1326. g_arr[loop->num] = g;
  1327. if (dump_file)
  1328. fprintf (dump_file, "...OK\n");
  1329. }
  1330. if (dump_file)
  1331. {
  1332. fprintf (dump_file, "\nSMS transformation phase\n");
  1333. fprintf (dump_file, "=========================\n\n");
  1334. }
  1335. /* We don't want to perform SMS on new loops - created by versioning. */
  1336. FOR_EACH_LOOP (loop, 0)
  1337. {
  1338. rtx_insn *head, *tail;
  1339. rtx count_reg;
  1340. rtx_insn *count_init;
  1341. int mii, rec_mii, stage_count, min_cycle;
  1342. int64_t loop_count = 0;
  1343. bool opt_sc_p;
  1344. if (! (g = g_arr[loop->num]))
  1345. continue;
  1346. if (dump_file)
  1347. {
  1348. rtx_insn *insn = BB_END (loop->header);
  1349. fprintf (dump_file, "SMS loop num: %d", loop->num);
  1350. dump_insn_location (insn);
  1351. fprintf (dump_file, "\n");
  1352. print_ddg (dump_file, g);
  1353. }
  1354. get_ebb_head_tail (loop->header, loop->header, &head, &tail);
  1355. latch_edge = loop_latch_edge (loop);
  1356. gcc_assert (single_exit (loop));
  1357. if (single_exit (loop)->count)
  1358. trip_count = latch_edge->count / single_exit (loop)->count;
  1359. if (dump_file)
  1360. {
  1361. dump_insn_location (tail);
  1362. fprintf (dump_file, "\nSMS single-bb-loop\n");
  1363. if (profile_info && flag_branch_probabilities)
  1364. {
  1365. fprintf (dump_file, "SMS loop-count ");
  1366. fprintf (dump_file, "%"PRId64,
  1367. (int64_t) bb->count);
  1368. fprintf (dump_file, "\n");
  1369. fprintf (dump_file, "SMS profile-sum-max ");
  1370. fprintf (dump_file, "%"PRId64,
  1371. (int64_t) profile_info->sum_max);
  1372. fprintf (dump_file, "\n");
  1373. }
  1374. fprintf (dump_file, "SMS doloop\n");
  1375. fprintf (dump_file, "SMS built-ddg %d\n", g->num_nodes);
  1376. fprintf (dump_file, "SMS num-loads %d\n", g->num_loads);
  1377. fprintf (dump_file, "SMS num-stores %d\n", g->num_stores);
  1378. }
  1379. /* In case of th loop have doloop register it gets special
  1380. handling. */
  1381. count_init = NULL;
  1382. if ((count_reg = doloop_register_get (head, tail)))
  1383. {
  1384. basic_block pre_header;
  1385. pre_header = loop_preheader_edge (loop)->src;
  1386. count_init = const_iteration_count (count_reg, pre_header,
  1387. &loop_count);
  1388. }
  1389. gcc_assert (count_reg);
  1390. if (dump_file && count_init)
  1391. {
  1392. fprintf (dump_file, "SMS const-doloop ");
  1393. fprintf (dump_file, "%"PRId64,
  1394. loop_count);
  1395. fprintf (dump_file, "\n");
  1396. }
  1397. node_order = XNEWVEC (int, g->num_nodes);
  1398. mii = 1; /* Need to pass some estimate of mii. */
  1399. rec_mii = sms_order_nodes (g, mii, node_order, &max_asap);
  1400. mii = MAX (res_MII (g), rec_mii);
  1401. maxii = MAX (max_asap, MAXII_FACTOR * mii);
  1402. if (dump_file)
  1403. fprintf (dump_file, "SMS iis %d %d %d (rec_mii, mii, maxii)\n",
  1404. rec_mii, mii, maxii);
  1405. for (;;)
  1406. {
  1407. set_node_sched_params (g);
  1408. stage_count = 0;
  1409. opt_sc_p = false;
  1410. ps = sms_schedule_by_order (g, mii, maxii, node_order);
  1411. if (ps)
  1412. {
  1413. /* Try to achieve optimized SC by normalizing the partial
  1414. schedule (having the cycles start from cycle zero).
  1415. The branch location must be placed in row ii-1 in the
  1416. final scheduling. If failed, shift all instructions to
  1417. position the branch in row ii-1. */
  1418. opt_sc_p = optimize_sc (ps, g);
  1419. if (opt_sc_p)
  1420. stage_count = calculate_stage_count (ps, 0);
  1421. else
  1422. {
  1423. /* Bring the branch to cycle ii-1. */
  1424. int amount = (SCHED_TIME (g->closing_branch->cuid)
  1425. - (ps->ii - 1));
  1426. if (dump_file)
  1427. fprintf (dump_file, "SMS schedule branch at cycle ii-1\n");
  1428. stage_count = calculate_stage_count (ps, amount);
  1429. }
  1430. gcc_assert (stage_count >= 1);
  1431. }
  1432. /* The default value of PARAM_SMS_MIN_SC is 2 as stage count of
  1433. 1 means that there is no interleaving between iterations thus
  1434. we let the scheduling passes do the job in this case. */
  1435. if (stage_count < PARAM_VALUE (PARAM_SMS_MIN_SC)
  1436. || (count_init && (loop_count <= stage_count))
  1437. || (flag_branch_probabilities && (trip_count <= stage_count)))
  1438. {
  1439. if (dump_file)
  1440. {
  1441. fprintf (dump_file, "SMS failed... \n");
  1442. fprintf (dump_file, "SMS sched-failed (stage-count=%d,"
  1443. " loop-count=", stage_count);
  1444. fprintf (dump_file, "%"PRId64, loop_count);
  1445. fprintf (dump_file, ", trip-count=");
  1446. fprintf (dump_file, "%"PRId64, trip_count);
  1447. fprintf (dump_file, ")\n");
  1448. }
  1449. break;
  1450. }
  1451. if (!opt_sc_p)
  1452. {
  1453. /* Rotate the partial schedule to have the branch in row ii-1. */
  1454. int amount = SCHED_TIME (g->closing_branch->cuid) - (ps->ii - 1);
  1455. reset_sched_times (ps, amount);
  1456. rotate_partial_schedule (ps, amount);
  1457. }
  1458. set_columns_for_ps (ps);
  1459. min_cycle = PS_MIN_CYCLE (ps) - SMODULO (PS_MIN_CYCLE (ps), ps->ii);
  1460. if (!schedule_reg_moves (ps))
  1461. {
  1462. mii = ps->ii + 1;
  1463. free_partial_schedule (ps);
  1464. continue;
  1465. }
  1466. /* Moves that handle incoming values might have been added
  1467. to a new first stage. Bump the stage count if so.
  1468. ??? Perhaps we could consider rotating the schedule here
  1469. instead? */
  1470. if (PS_MIN_CYCLE (ps) < min_cycle)
  1471. {
  1472. reset_sched_times (ps, 0);
  1473. stage_count++;
  1474. }
  1475. /* The stage count should now be correct without rotation. */
  1476. gcc_checking_assert (stage_count == calculate_stage_count (ps, 0));
  1477. PS_STAGE_COUNT (ps) = stage_count;
  1478. canon_loop (loop);
  1479. if (dump_file)
  1480. {
  1481. dump_insn_location (tail);
  1482. fprintf (dump_file, " SMS succeeded %d %d (with ii, sc)\n",
  1483. ps->ii, stage_count);
  1484. print_partial_schedule (ps, dump_file);
  1485. }
  1486. /* case the BCT count is not known , Do loop-versioning */
  1487. if (count_reg && ! count_init)
  1488. {
  1489. rtx comp_rtx = gen_rtx_GT (VOIDmode, count_reg,
  1490. gen_int_mode (stage_count,
  1491. GET_MODE (count_reg)));
  1492. unsigned prob = (PROB_SMS_ENOUGH_ITERATIONS
  1493. * REG_BR_PROB_BASE) / 100;
  1494. loop_version (loop, comp_rtx, &condition_bb,
  1495. prob, prob, REG_BR_PROB_BASE - prob,
  1496. true);
  1497. }
  1498. /* Set new iteration count of loop kernel. */
  1499. if (count_reg && count_init)
  1500. SET_SRC (single_set (count_init)) = GEN_INT (loop_count
  1501. - stage_count + 1);
  1502. /* Now apply the scheduled kernel to the RTL of the loop. */
  1503. permute_partial_schedule (ps, g->closing_branch->first_note);
  1504. /* Mark this loop as software pipelined so the later
  1505. scheduling passes don't touch it. */
  1506. if (! flag_resched_modulo_sched)
  1507. mark_loop_unsched (loop);
  1508. /* The life-info is not valid any more. */
  1509. df_set_bb_dirty (g->bb);
  1510. apply_reg_moves (ps);
  1511. if (dump_file)
  1512. print_node_sched_params (dump_file, g->num_nodes, ps);
  1513. /* Generate prolog and epilog. */
  1514. generate_prolog_epilog (ps, loop, count_reg, count_init);
  1515. break;
  1516. }
  1517. free_partial_schedule (ps);
  1518. node_sched_param_vec.release ();
  1519. free (node_order);
  1520. free_ddg (g);
  1521. }
  1522. free (g_arr);
  1523. /* Release scheduler data, needed until now because of DFA. */
  1524. haifa_sched_finish ();
  1525. loop_optimizer_finalize ();
  1526. }
  1527. /* The SMS scheduling algorithm itself
  1528. -----------------------------------
  1529. Input: 'O' an ordered list of insns of a loop.
  1530. Output: A scheduling of the loop - kernel, prolog, and epilogue.
  1531. 'Q' is the empty Set
  1532. 'PS' is the partial schedule; it holds the currently scheduled nodes with
  1533. their cycle/slot.
  1534. 'PSP' previously scheduled predecessors.
  1535. 'PSS' previously scheduled successors.
  1536. 't(u)' the cycle where u is scheduled.
  1537. 'l(u)' is the latency of u.
  1538. 'd(v,u)' is the dependence distance from v to u.
  1539. 'ASAP(u)' the earliest time at which u could be scheduled as computed in
  1540. the node ordering phase.
  1541. 'check_hardware_resources_conflicts(u, PS, c)'
  1542. run a trace around cycle/slot through DFA model
  1543. to check resource conflicts involving instruction u
  1544. at cycle c given the partial schedule PS.
  1545. 'add_to_partial_schedule_at_time(u, PS, c)'
  1546. Add the node/instruction u to the partial schedule
  1547. PS at time c.
  1548. 'calculate_register_pressure(PS)'
  1549. Given a schedule of instructions, calculate the register
  1550. pressure it implies. One implementation could be the
  1551. maximum number of overlapping live ranges.
  1552. 'maxRP' The maximum allowed register pressure, it is usually derived from the number
  1553. registers available in the hardware.
  1554. 1. II = MII.
  1555. 2. PS = empty list
  1556. 3. for each node u in O in pre-computed order
  1557. 4. if (PSP(u) != Q && PSS(u) == Q) then
  1558. 5. Early_start(u) = max ( t(v) + l(v) - d(v,u)*II ) over all every v in PSP(u).
  1559. 6. start = Early_start; end = Early_start + II - 1; step = 1
  1560. 11. else if (PSP(u) == Q && PSS(u) != Q) then
  1561. 12. Late_start(u) = min ( t(v) - l(v) + d(v,u)*II ) over all every v in PSS(u).
  1562. 13. start = Late_start; end = Late_start - II + 1; step = -1
  1563. 14. else if (PSP(u) != Q && PSS(u) != Q) then
  1564. 15. Early_start(u) = max ( t(v) + l(v) - d(v,u)*II ) over all every v in PSP(u).
  1565. 16. Late_start(u) = min ( t(v) - l(v) + d(v,u)*II ) over all every v in PSS(u).
  1566. 17. start = Early_start;
  1567. 18. end = min(Early_start + II - 1 , Late_start);
  1568. 19. step = 1
  1569. 20. else "if (PSP(u) == Q && PSS(u) == Q)"
  1570. 21. start = ASAP(u); end = start + II - 1; step = 1
  1571. 22. endif
  1572. 23. success = false
  1573. 24. for (c = start ; c != end ; c += step)
  1574. 25. if check_hardware_resources_conflicts(u, PS, c) then
  1575. 26. add_to_partial_schedule_at_time(u, PS, c)
  1576. 27. success = true
  1577. 28. break
  1578. 29. endif
  1579. 30. endfor
  1580. 31. if (success == false) then
  1581. 32. II = II + 1
  1582. 33. if (II > maxII) then
  1583. 34. finish - failed to schedule
  1584. 35. endif
  1585. 36. goto 2.
  1586. 37. endif
  1587. 38. endfor
  1588. 39. if (calculate_register_pressure(PS) > maxRP) then
  1589. 40. goto 32.
  1590. 41. endif
  1591. 42. compute epilogue & prologue
  1592. 43. finish - succeeded to schedule
  1593. ??? The algorithm restricts the scheduling window to II cycles.
  1594. In rare cases, it may be better to allow windows of II+1 cycles.
  1595. The window would then start and end on the same row, but with
  1596. different "must precede" and "must follow" requirements. */
  1597. /* A limit on the number of cycles that resource conflicts can span. ??? Should
  1598. be provided by DFA, and be dependent on the type of insn scheduled. Currently
  1599. set to 0 to save compile time. */
  1600. #define DFA_HISTORY SMS_DFA_HISTORY
  1601. /* A threshold for the number of repeated unsuccessful attempts to insert
  1602. an empty row, before we flush the partial schedule and start over. */
  1603. #define MAX_SPLIT_NUM 10
  1604. /* Given the partial schedule PS, this function calculates and returns the
  1605. cycles in which we can schedule the node with the given index I.
  1606. NOTE: Here we do the backtracking in SMS, in some special cases. We have
  1607. noticed that there are several cases in which we fail to SMS the loop
  1608. because the sched window of a node is empty due to tight data-deps. In
  1609. such cases we want to unschedule some of the predecessors/successors
  1610. until we get non-empty scheduling window. It returns -1 if the
  1611. scheduling window is empty and zero otherwise. */
  1612. static int
  1613. get_sched_window (partial_schedule_ptr ps, ddg_node_ptr u_node,
  1614. sbitmap sched_nodes, int ii, int *start_p, int *step_p,
  1615. int *end_p)
  1616. {
  1617. int start, step, end;
  1618. int early_start, late_start;
  1619. ddg_edge_ptr e;
  1620. sbitmap psp = sbitmap_alloc (ps->g->num_nodes);
  1621. sbitmap pss = sbitmap_alloc (ps->g->num_nodes);
  1622. sbitmap u_node_preds = NODE_PREDECESSORS (u_node);
  1623. sbitmap u_node_succs = NODE_SUCCESSORS (u_node);
  1624. int psp_not_empty;
  1625. int pss_not_empty;
  1626. int count_preds;
  1627. int count_succs;
  1628. /* 1. compute sched window for u (start, end, step). */
  1629. bitmap_clear (psp);
  1630. bitmap_clear (pss);
  1631. psp_not_empty = bitmap_and (psp, u_node_preds, sched_nodes);
  1632. pss_not_empty = bitmap_and (pss, u_node_succs, sched_nodes);
  1633. /* We first compute a forward range (start <= end), then decide whether
  1634. to reverse it. */
  1635. early_start = INT_MIN;
  1636. late_start = INT_MAX;
  1637. start = INT_MIN;
  1638. end = INT_MAX;
  1639. step = 1;
  1640. count_preds = 0;
  1641. count_succs = 0;
  1642. if (dump_file && (psp_not_empty || pss_not_empty))
  1643. {
  1644. fprintf (dump_file, "\nAnalyzing dependencies for node %d (INSN %d)"
  1645. "; ii = %d\n\n", u_node->cuid, INSN_UID (u_node->insn), ii);
  1646. fprintf (dump_file, "%11s %11s %11s %11s %5s\n",
  1647. "start", "early start", "late start", "end", "time");
  1648. fprintf (dump_file, "=========== =========== =========== ==========="
  1649. " =====\n");
  1650. }
  1651. /* Calculate early_start and limit end. Both bounds are inclusive. */
  1652. if (psp_not_empty)
  1653. for (e = u_node->in; e != 0; e = e->next_in)
  1654. {
  1655. int v = e->src->cuid;
  1656. if (bitmap_bit_p (sched_nodes, v))
  1657. {
  1658. int p_st = SCHED_TIME (v);
  1659. int earliest = p_st + e->latency - (e->distance * ii);
  1660. int latest = (e->data_type == MEM_DEP ? p_st + ii - 1 : INT_MAX);
  1661. if (dump_file)
  1662. {
  1663. fprintf (dump_file, "%11s %11d %11s %11d %5d",
  1664. "", earliest, "", latest, p_st);
  1665. print_ddg_edge (dump_file, e);
  1666. fprintf (dump_file, "\n");
  1667. }
  1668. early_start = MAX (early_start, earliest);
  1669. end = MIN (end, latest);
  1670. if (e->type == TRUE_DEP && e->data_type == REG_DEP)
  1671. count_preds++;
  1672. }
  1673. }
  1674. /* Calculate late_start and limit start. Both bounds are inclusive. */
  1675. if (pss_not_empty)
  1676. for (e = u_node->out; e != 0; e = e->next_out)
  1677. {
  1678. int v = e->dest->cuid;
  1679. if (bitmap_bit_p (sched_nodes, v))
  1680. {
  1681. int s_st = SCHED_TIME (v);
  1682. int earliest = (e->data_type == MEM_DEP ? s_st - ii + 1 : INT_MIN);
  1683. int latest = s_st - e->latency + (e->distance * ii);
  1684. if (dump_file)
  1685. {
  1686. fprintf (dump_file, "%11d %11s %11d %11s %5d",
  1687. earliest, "", latest, "", s_st);
  1688. print_ddg_edge (dump_file, e);
  1689. fprintf (dump_file, "\n");
  1690. }
  1691. start = MAX (start, earliest);
  1692. late_start = MIN (late_start, latest);
  1693. if (e->type == TRUE_DEP && e->data_type == REG_DEP)
  1694. count_succs++;
  1695. }
  1696. }
  1697. if (dump_file && (psp_not_empty || pss_not_empty))
  1698. {
  1699. fprintf (dump_file, "----------- ----------- ----------- -----------"
  1700. " -----\n");
  1701. fprintf (dump_file, "%11d %11d %11d %11d %5s %s\n",
  1702. start, early_start, late_start, end, "",
  1703. "(max, max, min, min)");
  1704. }
  1705. /* Get a target scheduling window no bigger than ii. */
  1706. if (early_start == INT_MIN && late_start == INT_MAX)
  1707. early_start = NODE_ASAP (u_node);
  1708. else if (early_start == INT_MIN)
  1709. early_start = late_start - (ii - 1);
  1710. late_start = MIN (late_start, early_start + (ii - 1));
  1711. /* Apply memory dependence limits. */
  1712. start = MAX (start, early_start);
  1713. end = MIN (end, late_start);
  1714. if (dump_file && (psp_not_empty || pss_not_empty))
  1715. fprintf (dump_file, "%11s %11d %11d %11s %5s final window\n",
  1716. "", start, end, "", "");
  1717. /* If there are at least as many successors as predecessors, schedule the
  1718. node close to its successors. */
  1719. if (pss_not_empty && count_succs >= count_preds)
  1720. {
  1721. int tmp = end;
  1722. end = start;
  1723. start = tmp;
  1724. step = -1;
  1725. }
  1726. /* Now that we've finalized the window, make END an exclusive rather
  1727. than an inclusive bound. */
  1728. end += step;
  1729. *start_p = start;
  1730. *step_p = step;
  1731. *end_p = end;
  1732. sbitmap_free (psp);
  1733. sbitmap_free (pss);
  1734. if ((start >= end && step == 1) || (start <= end && step == -1))
  1735. {
  1736. if (dump_file)
  1737. fprintf (dump_file, "\nEmpty window: start=%d, end=%d, step=%d\n",
  1738. start, end, step);
  1739. return -1;
  1740. }
  1741. return 0;
  1742. }
  1743. /* Calculate MUST_PRECEDE/MUST_FOLLOW bitmaps of U_NODE; which is the
  1744. node currently been scheduled. At the end of the calculation
  1745. MUST_PRECEDE/MUST_FOLLOW contains all predecessors/successors of
  1746. U_NODE which are (1) already scheduled in the first/last row of
  1747. U_NODE's scheduling window, (2) whose dependence inequality with U
  1748. becomes an equality when U is scheduled in this same row, and (3)
  1749. whose dependence latency is zero.
  1750. The first and last rows are calculated using the following parameters:
  1751. START/END rows - The cycles that begins/ends the traversal on the window;
  1752. searching for an empty cycle to schedule U_NODE.
  1753. STEP - The direction in which we traverse the window.
  1754. II - The initiation interval. */
  1755. static void
  1756. calculate_must_precede_follow (ddg_node_ptr u_node, int start, int end,
  1757. int step, int ii, sbitmap sched_nodes,
  1758. sbitmap must_precede, sbitmap must_follow)
  1759. {
  1760. ddg_edge_ptr e;
  1761. int first_cycle_in_window, last_cycle_in_window;
  1762. gcc_assert (must_precede && must_follow);
  1763. /* Consider the following scheduling window:
  1764. {first_cycle_in_window, first_cycle_in_window+1, ...,
  1765. last_cycle_in_window}. If step is 1 then the following will be
  1766. the order we traverse the window: {start=first_cycle_in_window,
  1767. first_cycle_in_window+1, ..., end=last_cycle_in_window+1},
  1768. or {start=last_cycle_in_window, last_cycle_in_window-1, ...,
  1769. end=first_cycle_in_window-1} if step is -1. */
  1770. first_cycle_in_window = (step == 1) ? start : end - step;
  1771. last_cycle_in_window = (step == 1) ? end - step : start;
  1772. bitmap_clear (must_precede);
  1773. bitmap_clear (must_follow);
  1774. if (dump_file)
  1775. fprintf (dump_file, "\nmust_precede: ");
  1776. /* Instead of checking if:
  1777. (SMODULO (SCHED_TIME (e->src), ii) == first_row_in_window)
  1778. && ((SCHED_TIME (e->src) + e->latency - (e->distance * ii)) ==
  1779. first_cycle_in_window)
  1780. && e->latency == 0
  1781. we use the fact that latency is non-negative:
  1782. SCHED_TIME (e->src) - (e->distance * ii) <=
  1783. SCHED_TIME (e->src) + e->latency - (e->distance * ii)) <=
  1784. first_cycle_in_window
  1785. and check only if
  1786. SCHED_TIME (e->src) - (e->distance * ii) == first_cycle_in_window */
  1787. for (e = u_node->in; e != 0; e = e->next_in)
  1788. if (bitmap_bit_p (sched_nodes, e->src->cuid)
  1789. && ((SCHED_TIME (e->src->cuid) - (e->distance * ii)) ==
  1790. first_cycle_in_window))
  1791. {
  1792. if (dump_file)
  1793. fprintf (dump_file, "%d ", e->src->cuid);
  1794. bitmap_set_bit (must_precede, e->src->cuid);
  1795. }
  1796. if (dump_file)
  1797. fprintf (dump_file, "\nmust_follow: ");
  1798. /* Instead of checking if:
  1799. (SMODULO (SCHED_TIME (e->dest), ii) == last_row_in_window)
  1800. && ((SCHED_TIME (e->dest) - e->latency + (e->distance * ii)) ==
  1801. last_cycle_in_window)
  1802. && e->latency == 0
  1803. we use the fact that latency is non-negative:
  1804. SCHED_TIME (e->dest) + (e->distance * ii) >=
  1805. SCHED_TIME (e->dest) - e->latency + (e->distance * ii)) >=
  1806. last_cycle_in_window
  1807. and check only if
  1808. SCHED_TIME (e->dest) + (e->distance * ii) == last_cycle_in_window */
  1809. for (e = u_node->out; e != 0; e = e->next_out)
  1810. if (bitmap_bit_p (sched_nodes, e->dest->cuid)
  1811. && ((SCHED_TIME (e->dest->cuid) + (e->distance * ii)) ==
  1812. last_cycle_in_window))
  1813. {
  1814. if (dump_file)
  1815. fprintf (dump_file, "%d ", e->dest->cuid);
  1816. bitmap_set_bit (must_follow, e->dest->cuid);
  1817. }
  1818. if (dump_file)
  1819. fprintf (dump_file, "\n");
  1820. }
  1821. /* Return 1 if U_NODE can be scheduled in CYCLE. Use the following
  1822. parameters to decide if that's possible:
  1823. PS - The partial schedule.
  1824. U - The serial number of U_NODE.
  1825. NUM_SPLITS - The number of row splits made so far.
  1826. MUST_PRECEDE - The nodes that must precede U_NODE. (only valid at
  1827. the first row of the scheduling window)
  1828. MUST_FOLLOW - The nodes that must follow U_NODE. (only valid at the
  1829. last row of the scheduling window) */
  1830. static bool
  1831. try_scheduling_node_in_cycle (partial_schedule_ptr ps,
  1832. int u, int cycle, sbitmap sched_nodes,
  1833. int *num_splits, sbitmap must_precede,
  1834. sbitmap must_follow)
  1835. {
  1836. ps_insn_ptr psi;
  1837. bool success = 0;
  1838. verify_partial_schedule (ps, sched_nodes);
  1839. psi = ps_add_node_check_conflicts (ps, u, cycle, must_precede, must_follow);
  1840. if (psi)
  1841. {
  1842. SCHED_TIME (u) = cycle;
  1843. bitmap_set_bit (sched_nodes, u);
  1844. success = 1;
  1845. *num_splits = 0;
  1846. if (dump_file)
  1847. fprintf (dump_file, "Scheduled w/o split in %d\n", cycle);
  1848. }
  1849. return success;
  1850. }
  1851. /* This function implements the scheduling algorithm for SMS according to the
  1852. above algorithm. */
  1853. static partial_schedule_ptr
  1854. sms_schedule_by_order (ddg_ptr g, int mii, int maxii, int *nodes_order)
  1855. {
  1856. int ii = mii;
  1857. int i, c, success, num_splits = 0;
  1858. int flush_and_start_over = true;
  1859. int num_nodes = g->num_nodes;
  1860. int start, end, step; /* Place together into one struct? */
  1861. sbitmap sched_nodes = sbitmap_alloc (num_nodes);
  1862. sbitmap must_precede = sbitmap_alloc (num_nodes);
  1863. sbitmap must_follow = sbitmap_alloc (num_nodes);
  1864. sbitmap tobe_scheduled = sbitmap_alloc (num_nodes);
  1865. partial_schedule_ptr ps = create_partial_schedule (ii, g, DFA_HISTORY);
  1866. bitmap_ones (tobe_scheduled);
  1867. bitmap_clear (sched_nodes);
  1868. while (flush_and_start_over && (ii < maxii))
  1869. {
  1870. if (dump_file)
  1871. fprintf (dump_file, "Starting with ii=%d\n", ii);
  1872. flush_and_start_over = false;
  1873. bitmap_clear (sched_nodes);
  1874. for (i = 0; i < num_nodes; i++)
  1875. {
  1876. int u = nodes_order[i];
  1877. ddg_node_ptr u_node = &ps->g->nodes[u];
  1878. rtx insn = u_node->insn;
  1879. if (!NONDEBUG_INSN_P (insn))
  1880. {
  1881. bitmap_clear_bit (tobe_scheduled, u);
  1882. continue;
  1883. }
  1884. if (bitmap_bit_p (sched_nodes, u))
  1885. continue;
  1886. /* Try to get non-empty scheduling window. */
  1887. success = 0;
  1888. if (get_sched_window (ps, u_node, sched_nodes, ii, &start,
  1889. &step, &end) == 0)
  1890. {
  1891. if (dump_file)
  1892. fprintf (dump_file, "\nTrying to schedule node %d "
  1893. "INSN = %d in (%d .. %d) step %d\n", u, (INSN_UID
  1894. (g->nodes[u].insn)), start, end, step);
  1895. gcc_assert ((step > 0 && start < end)
  1896. || (step < 0 && start > end));
  1897. calculate_must_precede_follow (u_node, start, end, step, ii,
  1898. sched_nodes, must_precede,
  1899. must_follow);
  1900. for (c = start; c != end; c += step)
  1901. {
  1902. sbitmap tmp_precede, tmp_follow;
  1903. set_must_precede_follow (&tmp_follow, must_follow,
  1904. &tmp_precede, must_precede,
  1905. c, start, end, step);
  1906. success =
  1907. try_scheduling_node_in_cycle (ps, u, c,
  1908. sched_nodes,
  1909. &num_splits, tmp_precede,
  1910. tmp_follow);
  1911. if (success)
  1912. break;
  1913. }
  1914. verify_partial_schedule (ps, sched_nodes);
  1915. }
  1916. if (!success)
  1917. {
  1918. int split_row;
  1919. if (ii++ == maxii)
  1920. break;
  1921. if (num_splits >= MAX_SPLIT_NUM)
  1922. {
  1923. num_splits = 0;
  1924. flush_and_start_over = true;
  1925. verify_partial_schedule (ps, sched_nodes);
  1926. reset_partial_schedule (ps, ii);
  1927. verify_partial_schedule (ps, sched_nodes);
  1928. break;
  1929. }
  1930. num_splits++;
  1931. /* The scheduling window is exclusive of 'end'
  1932. whereas compute_split_window() expects an inclusive,
  1933. ordered range. */
  1934. if (step == 1)
  1935. split_row = compute_split_row (sched_nodes, start, end - 1,
  1936. ps->ii, u_node);
  1937. else
  1938. split_row = compute_split_row (sched_nodes, end + 1, start,
  1939. ps->ii, u_node);
  1940. ps_insert_empty_row (ps, split_row, sched_nodes);
  1941. i--; /* Go back and retry node i. */
  1942. if (dump_file)
  1943. fprintf (dump_file, "num_splits=%d\n", num_splits);
  1944. }
  1945. /* ??? If (success), check register pressure estimates. */
  1946. } /* Continue with next node. */
  1947. } /* While flush_and_start_over. */
  1948. if (ii >= maxii)
  1949. {
  1950. free_partial_schedule (ps);
  1951. ps = NULL;
  1952. }
  1953. else
  1954. gcc_assert (bitmap_equal_p (tobe_scheduled, sched_nodes));
  1955. sbitmap_free (sched_nodes);
  1956. sbitmap_free (must_precede);
  1957. sbitmap_free (must_follow);
  1958. sbitmap_free (tobe_scheduled);
  1959. return ps;
  1960. }
  1961. /* This function inserts a new empty row into PS at the position
  1962. according to SPLITROW, keeping all already scheduled instructions
  1963. intact and updating their SCHED_TIME and cycle accordingly. */
  1964. static void
  1965. ps_insert_empty_row (partial_schedule_ptr ps, int split_row,
  1966. sbitmap sched_nodes)
  1967. {
  1968. ps_insn_ptr crr_insn;
  1969. ps_insn_ptr *rows_new;
  1970. int ii = ps->ii;
  1971. int new_ii = ii + 1;
  1972. int row;
  1973. int *rows_length_new;
  1974. verify_partial_schedule (ps, sched_nodes);
  1975. /* We normalize sched_time and rotate ps to have only non-negative sched
  1976. times, for simplicity of updating cycles after inserting new row. */
  1977. split_row -= ps->min_cycle;
  1978. split_row = SMODULO (split_row, ii);
  1979. if (dump_file)
  1980. fprintf (dump_file, "split_row=%d\n", split_row);
  1981. reset_sched_times (ps, PS_MIN_CYCLE (ps));
  1982. rotate_partial_schedule (ps, PS_MIN_CYCLE (ps));
  1983. rows_new = (ps_insn_ptr *) xcalloc (new_ii, sizeof (ps_insn_ptr));
  1984. rows_length_new = (int *) xcalloc (new_ii, sizeof (int));
  1985. for (row = 0; row < split_row; row++)
  1986. {
  1987. rows_new[row] = ps->rows[row];
  1988. rows_length_new[row] = ps->rows_length[row];
  1989. ps->rows[row] = NULL;
  1990. for (crr_insn = rows_new[row];
  1991. crr_insn; crr_insn = crr_insn->next_in_row)
  1992. {
  1993. int u = crr_insn->id;
  1994. int new_time = SCHED_TIME (u) + (SCHED_TIME (u) / ii);
  1995. SCHED_TIME (u) = new_time;
  1996. crr_insn->cycle = new_time;
  1997. SCHED_ROW (u) = new_time % new_ii;
  1998. SCHED_STAGE (u) = new_time / new_ii;
  1999. }
  2000. }
  2001. rows_new[split_row] = NULL;
  2002. for (row = split_row; row < ii; row++)
  2003. {
  2004. rows_new[row + 1] = ps->rows[row];
  2005. rows_length_new[row + 1] = ps->rows_length[row];
  2006. ps->rows[row] = NULL;
  2007. for (crr_insn = rows_new[row + 1];
  2008. crr_insn; crr_insn = crr_insn->next_in_row)
  2009. {
  2010. int u = crr_insn->id;
  2011. int new_time = SCHED_TIME (u) + (SCHED_TIME (u) / ii) + 1;
  2012. SCHED_TIME (u) = new_time;
  2013. crr_insn->cycle = new_time;
  2014. SCHED_ROW (u) = new_time % new_ii;
  2015. SCHED_STAGE (u) = new_time / new_ii;
  2016. }
  2017. }
  2018. /* Updating ps. */
  2019. ps->min_cycle = ps->min_cycle + ps->min_cycle / ii
  2020. + (SMODULO (ps->min_cycle, ii) >= split_row ? 1 : 0);
  2021. ps->max_cycle = ps->max_cycle + ps->max_cycle / ii
  2022. + (SMODULO (ps->max_cycle, ii) >= split_row ? 1 : 0);
  2023. free (ps->rows);
  2024. ps->rows = rows_new;
  2025. free (ps->rows_length);
  2026. ps->rows_length = rows_length_new;
  2027. ps->ii = new_ii;
  2028. gcc_assert (ps->min_cycle >= 0);
  2029. verify_partial_schedule (ps, sched_nodes);
  2030. if (dump_file)
  2031. fprintf (dump_file, "min_cycle=%d, max_cycle=%d\n", ps->min_cycle,
  2032. ps->max_cycle);
  2033. }
  2034. /* Given U_NODE which is the node that failed to be scheduled; LOW and
  2035. UP which are the boundaries of it's scheduling window; compute using
  2036. SCHED_NODES and II a row in the partial schedule that can be split
  2037. which will separate a critical predecessor from a critical successor
  2038. thereby expanding the window, and return it. */
  2039. static int
  2040. compute_split_row (sbitmap sched_nodes, int low, int up, int ii,
  2041. ddg_node_ptr u_node)
  2042. {
  2043. ddg_edge_ptr e;
  2044. int lower = INT_MIN, upper = INT_MAX;
  2045. int crit_pred = -1;
  2046. int crit_succ = -1;
  2047. int crit_cycle;
  2048. for (e = u_node->in; e != 0; e = e->next_in)
  2049. {
  2050. int v = e->src->cuid;
  2051. if (bitmap_bit_p (sched_nodes, v)
  2052. && (low == SCHED_TIME (v) + e->latency - (e->distance * ii)))
  2053. if (SCHED_TIME (v) > lower)
  2054. {
  2055. crit_pred = v;
  2056. lower = SCHED_TIME (v);
  2057. }
  2058. }
  2059. if (crit_pred >= 0)
  2060. {
  2061. crit_cycle = SCHED_TIME (crit_pred) + 1;
  2062. return SMODULO (crit_cycle, ii);
  2063. }
  2064. for (e = u_node->out; e != 0; e = e->next_out)
  2065. {
  2066. int v = e->dest->cuid;
  2067. if (bitmap_bit_p (sched_nodes, v)
  2068. && (up == SCHED_TIME (v) - e->latency + (e->distance * ii)))
  2069. if (SCHED_TIME (v) < upper)
  2070. {
  2071. crit_succ = v;
  2072. upper = SCHED_TIME (v);
  2073. }
  2074. }
  2075. if (crit_succ >= 0)
  2076. {
  2077. crit_cycle = SCHED_TIME (crit_succ);
  2078. return SMODULO (crit_cycle, ii);
  2079. }
  2080. if (dump_file)
  2081. fprintf (dump_file, "Both crit_pred and crit_succ are NULL\n");
  2082. return SMODULO ((low + up + 1) / 2, ii);
  2083. }
  2084. static void
  2085. verify_partial_schedule (partial_schedule_ptr ps, sbitmap sched_nodes)
  2086. {
  2087. int row;
  2088. ps_insn_ptr crr_insn;
  2089. for (row = 0; row < ps->ii; row++)
  2090. {
  2091. int length = 0;
  2092. for (crr_insn = ps->rows[row]; crr_insn; crr_insn = crr_insn->next_in_row)
  2093. {
  2094. int u = crr_insn->id;
  2095. length++;
  2096. gcc_assert (bitmap_bit_p (sched_nodes, u));
  2097. /* ??? Test also that all nodes of sched_nodes are in ps, perhaps by
  2098. popcount (sched_nodes) == number of insns in ps. */
  2099. gcc_assert (SCHED_TIME (u) >= ps->min_cycle);
  2100. gcc_assert (SCHED_TIME (u) <= ps->max_cycle);
  2101. }
  2102. gcc_assert (ps->rows_length[row] == length);
  2103. }
  2104. }
  2105. /* This page implements the algorithm for ordering the nodes of a DDG
  2106. for modulo scheduling, activated through the
  2107. "int sms_order_nodes (ddg_ptr, int mii, int * result)" API. */
  2108. #define ORDER_PARAMS(x) ((struct node_order_params *) (x)->aux.info)
  2109. #define ASAP(x) (ORDER_PARAMS ((x))->asap)
  2110. #define ALAP(x) (ORDER_PARAMS ((x))->alap)
  2111. #define HEIGHT(x) (ORDER_PARAMS ((x))->height)
  2112. #define MOB(x) (ALAP ((x)) - ASAP ((x)))
  2113. #define DEPTH(x) (ASAP ((x)))
  2114. typedef struct node_order_params * nopa;
  2115. static void order_nodes_of_sccs (ddg_all_sccs_ptr, int * result);
  2116. static int order_nodes_in_scc (ddg_ptr, sbitmap, sbitmap, int*, int);
  2117. static nopa calculate_order_params (ddg_ptr, int, int *);
  2118. static int find_max_asap (ddg_ptr, sbitmap);
  2119. static int find_max_hv_min_mob (ddg_ptr, sbitmap);
  2120. static int find_max_dv_min_mob (ddg_ptr, sbitmap);
  2121. enum sms_direction {BOTTOMUP, TOPDOWN};
  2122. struct node_order_params
  2123. {
  2124. int asap;
  2125. int alap;
  2126. int height;
  2127. };
  2128. /* Check if NODE_ORDER contains a permutation of 0 .. NUM_NODES-1. */
  2129. static void
  2130. check_nodes_order (int *node_order, int num_nodes)
  2131. {
  2132. int i;
  2133. sbitmap tmp = sbitmap_alloc (num_nodes);
  2134. bitmap_clear (tmp);
  2135. if (dump_file)
  2136. fprintf (dump_file, "SMS final nodes order: \n");
  2137. for (i = 0; i < num_nodes; i++)
  2138. {
  2139. int u = node_order[i];
  2140. if (dump_file)
  2141. fprintf (dump_file, "%d ", u);
  2142. gcc_assert (u < num_nodes && u >= 0 && !bitmap_bit_p (tmp, u));
  2143. bitmap_set_bit (tmp, u);
  2144. }
  2145. if (dump_file)
  2146. fprintf (dump_file, "\n");
  2147. sbitmap_free (tmp);
  2148. }
  2149. /* Order the nodes of G for scheduling and pass the result in
  2150. NODE_ORDER. Also set aux.count of each node to ASAP.
  2151. Put maximal ASAP to PMAX_ASAP. Return the recMII for the given DDG. */
  2152. static int
  2153. sms_order_nodes (ddg_ptr g, int mii, int * node_order, int *pmax_asap)
  2154. {
  2155. int i;
  2156. int rec_mii = 0;
  2157. ddg_all_sccs_ptr sccs = create_ddg_all_sccs (g);
  2158. nopa nops = calculate_order_params (g, mii, pmax_asap);
  2159. if (dump_file)
  2160. print_sccs (dump_file, sccs, g);
  2161. order_nodes_of_sccs (sccs, node_order);
  2162. if (sccs->num_sccs > 0)
  2163. /* First SCC has the largest recurrence_length. */
  2164. rec_mii = sccs->sccs[0]->recurrence_length;
  2165. /* Save ASAP before destroying node_order_params. */
  2166. for (i = 0; i < g->num_nodes; i++)
  2167. {
  2168. ddg_node_ptr v = &g->nodes[i];
  2169. v->aux.count = ASAP (v);
  2170. }
  2171. free (nops);
  2172. free_ddg_all_sccs (sccs);
  2173. check_nodes_order (node_order, g->num_nodes);
  2174. return rec_mii;
  2175. }
  2176. static void
  2177. order_nodes_of_sccs (ddg_all_sccs_ptr all_sccs, int * node_order)
  2178. {
  2179. int i, pos = 0;
  2180. ddg_ptr g = all_sccs->ddg;
  2181. int num_nodes = g->num_nodes;
  2182. sbitmap prev_sccs = sbitmap_alloc (num_nodes);
  2183. sbitmap on_path = sbitmap_alloc (num_nodes);
  2184. sbitmap tmp = sbitmap_alloc (num_nodes);
  2185. sbitmap ones = sbitmap_alloc (num_nodes);
  2186. bitmap_clear (prev_sccs);
  2187. bitmap_ones (ones);
  2188. /* Perform the node ordering starting from the SCC with the highest recMII.
  2189. For each SCC order the nodes according to their ASAP/ALAP/HEIGHT etc. */
  2190. for (i = 0; i < all_sccs->num_sccs; i++)
  2191. {
  2192. ddg_scc_ptr scc = all_sccs->sccs[i];
  2193. /* Add nodes on paths from previous SCCs to the current SCC. */
  2194. find_nodes_on_paths (on_path, g, prev_sccs, scc->nodes);
  2195. bitmap_ior (tmp, scc->nodes, on_path);
  2196. /* Add nodes on paths from the current SCC to previous SCCs. */
  2197. find_nodes_on_paths (on_path, g, scc->nodes, prev_sccs);
  2198. bitmap_ior (tmp, tmp, on_path);
  2199. /* Remove nodes of previous SCCs from current extended SCC. */
  2200. bitmap_and_compl (tmp, tmp, prev_sccs);
  2201. pos = order_nodes_in_scc (g, prev_sccs, tmp, node_order, pos);
  2202. /* Above call to order_nodes_in_scc updated prev_sccs |= tmp. */
  2203. }
  2204. /* Handle the remaining nodes that do not belong to any scc. Each call
  2205. to order_nodes_in_scc handles a single connected component. */
  2206. while (pos < g->num_nodes)
  2207. {
  2208. bitmap_and_compl (tmp, ones, prev_sccs);
  2209. pos = order_nodes_in_scc (g, prev_sccs, tmp, node_order, pos);
  2210. }
  2211. sbitmap_free (prev_sccs);
  2212. sbitmap_free (on_path);
  2213. sbitmap_free (tmp);
  2214. sbitmap_free (ones);
  2215. }
  2216. /* MII is needed if we consider backarcs (that do not close recursive cycles). */
  2217. static struct node_order_params *
  2218. calculate_order_params (ddg_ptr g, int mii ATTRIBUTE_UNUSED, int *pmax_asap)
  2219. {
  2220. int u;
  2221. int max_asap;
  2222. int num_nodes = g->num_nodes;
  2223. ddg_edge_ptr e;
  2224. /* Allocate a place to hold ordering params for each node in the DDG. */
  2225. nopa node_order_params_arr;
  2226. /* Initialize of ASAP/ALAP/HEIGHT to zero. */
  2227. node_order_params_arr = (nopa) xcalloc (num_nodes,
  2228. sizeof (struct node_order_params));
  2229. /* Set the aux pointer of each node to point to its order_params structure. */
  2230. for (u = 0; u < num_nodes; u++)
  2231. g->nodes[u].aux.info = &node_order_params_arr[u];
  2232. /* Disregarding a backarc from each recursive cycle to obtain a DAG,
  2233. calculate ASAP, ALAP, mobility, distance, and height for each node
  2234. in the dependence (direct acyclic) graph. */
  2235. /* We assume that the nodes in the array are in topological order. */
  2236. max_asap = 0;
  2237. for (u = 0; u < num_nodes; u++)
  2238. {
  2239. ddg_node_ptr u_node = &g->nodes[u];
  2240. ASAP (u_node) = 0;
  2241. for (e = u_node->in; e; e = e->next_in)
  2242. if (e->distance == 0)
  2243. ASAP (u_node) = MAX (ASAP (u_node),
  2244. ASAP (e->src) + e->latency);
  2245. max_asap = MAX (max_asap, ASAP (u_node));
  2246. }
  2247. for (u = num_nodes - 1; u > -1; u--)
  2248. {
  2249. ddg_node_ptr u_node = &g->nodes[u];
  2250. ALAP (u_node) = max_asap;
  2251. HEIGHT (u_node) = 0;
  2252. for (e = u_node->out; e; e = e->next_out)
  2253. if (e->distance == 0)
  2254. {
  2255. ALAP (u_node) = MIN (ALAP (u_node),
  2256. ALAP (e->dest) - e->latency);
  2257. HEIGHT (u_node) = MAX (HEIGHT (u_node),
  2258. HEIGHT (e->dest) + e->latency);
  2259. }
  2260. }
  2261. if (dump_file)
  2262. {
  2263. fprintf (dump_file, "\nOrder params\n");
  2264. for (u = 0; u < num_nodes; u++)
  2265. {
  2266. ddg_node_ptr u_node = &g->nodes[u];
  2267. fprintf (dump_file, "node %d, ASAP: %d, ALAP: %d, HEIGHT: %d\n", u,
  2268. ASAP (u_node), ALAP (u_node), HEIGHT (u_node));
  2269. }
  2270. }
  2271. *pmax_asap = max_asap;
  2272. return node_order_params_arr;
  2273. }
  2274. static int
  2275. find_max_asap (ddg_ptr g, sbitmap nodes)
  2276. {
  2277. unsigned int u = 0;
  2278. int max_asap = -1;
  2279. int result = -1;
  2280. sbitmap_iterator sbi;
  2281. EXECUTE_IF_SET_IN_BITMAP (nodes, 0, u, sbi)
  2282. {
  2283. ddg_node_ptr u_node = &g->nodes[u];
  2284. if (max_asap < ASAP (u_node))
  2285. {
  2286. max_asap = ASAP (u_node);
  2287. result = u;
  2288. }
  2289. }
  2290. return result;
  2291. }
  2292. static int
  2293. find_max_hv_min_mob (ddg_ptr g, sbitmap nodes)
  2294. {
  2295. unsigned int u = 0;
  2296. int max_hv = -1;
  2297. int min_mob = INT_MAX;
  2298. int result = -1;
  2299. sbitmap_iterator sbi;
  2300. EXECUTE_IF_SET_IN_BITMAP (nodes, 0, u, sbi)
  2301. {
  2302. ddg_node_ptr u_node = &g->nodes[u];
  2303. if (max_hv < HEIGHT (u_node))
  2304. {
  2305. max_hv = HEIGHT (u_node);
  2306. min_mob = MOB (u_node);
  2307. result = u;
  2308. }
  2309. else if ((max_hv == HEIGHT (u_node))
  2310. && (min_mob > MOB (u_node)))
  2311. {
  2312. min_mob = MOB (u_node);
  2313. result = u;
  2314. }
  2315. }
  2316. return result;
  2317. }
  2318. static int
  2319. find_max_dv_min_mob (ddg_ptr g, sbitmap nodes)
  2320. {
  2321. unsigned int u = 0;
  2322. int max_dv = -1;
  2323. int min_mob = INT_MAX;
  2324. int result = -1;
  2325. sbitmap_iterator sbi;
  2326. EXECUTE_IF_SET_IN_BITMAP (nodes, 0, u, sbi)
  2327. {
  2328. ddg_node_ptr u_node = &g->nodes[u];
  2329. if (max_dv < DEPTH (u_node))
  2330. {
  2331. max_dv = DEPTH (u_node);
  2332. min_mob = MOB (u_node);
  2333. result = u;
  2334. }
  2335. else if ((max_dv == DEPTH (u_node))
  2336. && (min_mob > MOB (u_node)))
  2337. {
  2338. min_mob = MOB (u_node);
  2339. result = u;
  2340. }
  2341. }
  2342. return result;
  2343. }
  2344. /* Places the nodes of SCC into the NODE_ORDER array starting
  2345. at position POS, according to the SMS ordering algorithm.
  2346. NODES_ORDERED (in&out parameter) holds the bitset of all nodes in
  2347. the NODE_ORDER array, starting from position zero. */
  2348. static int
  2349. order_nodes_in_scc (ddg_ptr g, sbitmap nodes_ordered, sbitmap scc,
  2350. int * node_order, int pos)
  2351. {
  2352. enum sms_direction dir;
  2353. int num_nodes = g->num_nodes;
  2354. sbitmap workset = sbitmap_alloc (num_nodes);
  2355. sbitmap tmp = sbitmap_alloc (num_nodes);
  2356. sbitmap zero_bitmap = sbitmap_alloc (num_nodes);
  2357. sbitmap predecessors = sbitmap_alloc (num_nodes);
  2358. sbitmap successors = sbitmap_alloc (num_nodes);
  2359. bitmap_clear (predecessors);
  2360. find_predecessors (predecessors, g, nodes_ordered);
  2361. bitmap_clear (successors);
  2362. find_successors (successors, g, nodes_ordered);
  2363. bitmap_clear (tmp);
  2364. if (bitmap_and (tmp, predecessors, scc))
  2365. {
  2366. bitmap_copy (workset, tmp);
  2367. dir = BOTTOMUP;
  2368. }
  2369. else if (bitmap_and (tmp, successors, scc))
  2370. {
  2371. bitmap_copy (workset, tmp);
  2372. dir = TOPDOWN;
  2373. }
  2374. else
  2375. {
  2376. int u;
  2377. bitmap_clear (workset);
  2378. if ((u = find_max_asap (g, scc)) >= 0)
  2379. bitmap_set_bit (workset, u);
  2380. dir = BOTTOMUP;
  2381. }
  2382. bitmap_clear (zero_bitmap);
  2383. while (!bitmap_equal_p (workset, zero_bitmap))
  2384. {
  2385. int v;
  2386. ddg_node_ptr v_node;
  2387. sbitmap v_node_preds;
  2388. sbitmap v_node_succs;
  2389. if (dir == TOPDOWN)
  2390. {
  2391. while (!bitmap_equal_p (workset, zero_bitmap))
  2392. {
  2393. v = find_max_hv_min_mob (g, workset);
  2394. v_node = &g->nodes[v];
  2395. node_order[pos++] = v;
  2396. v_node_succs = NODE_SUCCESSORS (v_node);
  2397. bitmap_and (tmp, v_node_succs, scc);
  2398. /* Don't consider the already ordered successors again. */
  2399. bitmap_and_compl (tmp, tmp, nodes_ordered);
  2400. bitmap_ior (workset, workset, tmp);
  2401. bitmap_clear_bit (workset, v);
  2402. bitmap_set_bit (nodes_ordered, v);
  2403. }
  2404. dir = BOTTOMUP;
  2405. bitmap_clear (predecessors);
  2406. find_predecessors (predecessors, g, nodes_ordered);
  2407. bitmap_and (workset, predecessors, scc);
  2408. }
  2409. else
  2410. {
  2411. while (!bitmap_equal_p (workset, zero_bitmap))
  2412. {
  2413. v = find_max_dv_min_mob (g, workset);
  2414. v_node = &g->nodes[v];
  2415. node_order[pos++] = v;
  2416. v_node_preds = NODE_PREDECESSORS (v_node);
  2417. bitmap_and (tmp, v_node_preds, scc);
  2418. /* Don't consider the already ordered predecessors again. */
  2419. bitmap_and_compl (tmp, tmp, nodes_ordered);
  2420. bitmap_ior (workset, workset, tmp);
  2421. bitmap_clear_bit (workset, v);
  2422. bitmap_set_bit (nodes_ordered, v);
  2423. }
  2424. dir = TOPDOWN;
  2425. bitmap_clear (successors);
  2426. find_successors (successors, g, nodes_ordered);
  2427. bitmap_and (workset, successors, scc);
  2428. }
  2429. }
  2430. sbitmap_free (tmp);
  2431. sbitmap_free (workset);
  2432. sbitmap_free (zero_bitmap);
  2433. sbitmap_free (predecessors);
  2434. sbitmap_free (successors);
  2435. return pos;
  2436. }
  2437. /* This page contains functions for manipulating partial-schedules during
  2438. modulo scheduling. */
  2439. /* Create a partial schedule and allocate a memory to hold II rows. */
  2440. static partial_schedule_ptr
  2441. create_partial_schedule (int ii, ddg_ptr g, int history)
  2442. {
  2443. partial_schedule_ptr ps = XNEW (struct partial_schedule);
  2444. ps->rows = (ps_insn_ptr *) xcalloc (ii, sizeof (ps_insn_ptr));
  2445. ps->rows_length = (int *) xcalloc (ii, sizeof (int));
  2446. ps->reg_moves.create (0);
  2447. ps->ii = ii;
  2448. ps->history = history;
  2449. ps->min_cycle = INT_MAX;
  2450. ps->max_cycle = INT_MIN;
  2451. ps->g = g;
  2452. return ps;
  2453. }
  2454. /* Free the PS_INSNs in rows array of the given partial schedule.
  2455. ??? Consider caching the PS_INSN's. */
  2456. static void
  2457. free_ps_insns (partial_schedule_ptr ps)
  2458. {
  2459. int i;
  2460. for (i = 0; i < ps->ii; i++)
  2461. {
  2462. while (ps->rows[i])
  2463. {
  2464. ps_insn_ptr ps_insn = ps->rows[i]->next_in_row;
  2465. free (ps->rows[i]);
  2466. ps->rows[i] = ps_insn;
  2467. }
  2468. ps->rows[i] = NULL;
  2469. }
  2470. }
  2471. /* Free all the memory allocated to the partial schedule. */
  2472. static void
  2473. free_partial_schedule (partial_schedule_ptr ps)
  2474. {
  2475. ps_reg_move_info *move;
  2476. unsigned int i;
  2477. if (!ps)
  2478. return;
  2479. FOR_EACH_VEC_ELT (ps->reg_moves, i, move)
  2480. sbitmap_free (move->uses);
  2481. ps->reg_moves.release ();
  2482. free_ps_insns (ps);
  2483. free (ps->rows);
  2484. free (ps->rows_length);
  2485. free (ps);
  2486. }
  2487. /* Clear the rows array with its PS_INSNs, and create a new one with
  2488. NEW_II rows. */
  2489. static void
  2490. reset_partial_schedule (partial_schedule_ptr ps, int new_ii)
  2491. {
  2492. if (!ps)
  2493. return;
  2494. free_ps_insns (ps);
  2495. if (new_ii == ps->ii)
  2496. return;
  2497. ps->rows = (ps_insn_ptr *) xrealloc (ps->rows, new_ii
  2498. * sizeof (ps_insn_ptr));
  2499. memset (ps->rows, 0, new_ii * sizeof (ps_insn_ptr));
  2500. ps->rows_length = (int *) xrealloc (ps->rows_length, new_ii * sizeof (int));
  2501. memset (ps->rows_length, 0, new_ii * sizeof (int));
  2502. ps->ii = new_ii;
  2503. ps->min_cycle = INT_MAX;
  2504. ps->max_cycle = INT_MIN;
  2505. }
  2506. /* Prints the partial schedule as an ii rows array, for each rows
  2507. print the ids of the insns in it. */
  2508. void
  2509. print_partial_schedule (partial_schedule_ptr ps, FILE *dump)
  2510. {
  2511. int i;
  2512. for (i = 0; i < ps->ii; i++)
  2513. {
  2514. ps_insn_ptr ps_i = ps->rows[i];
  2515. fprintf (dump, "\n[ROW %d ]: ", i);
  2516. while (ps_i)
  2517. {
  2518. rtx_insn *insn = ps_rtl_insn (ps, ps_i->id);
  2519. if (JUMP_P (insn))
  2520. fprintf (dump, "%d (branch), ", INSN_UID (insn));
  2521. else
  2522. fprintf (dump, "%d, ", INSN_UID (insn));
  2523. ps_i = ps_i->next_in_row;
  2524. }
  2525. }
  2526. }
  2527. /* Creates an object of PS_INSN and initializes it to the given parameters. */
  2528. static ps_insn_ptr
  2529. create_ps_insn (int id, int cycle)
  2530. {
  2531. ps_insn_ptr ps_i = XNEW (struct ps_insn);
  2532. ps_i->id = id;
  2533. ps_i->next_in_row = NULL;
  2534. ps_i->prev_in_row = NULL;
  2535. ps_i->cycle = cycle;
  2536. return ps_i;
  2537. }
  2538. /* Removes the given PS_INSN from the partial schedule. */
  2539. static void
  2540. remove_node_from_ps (partial_schedule_ptr ps, ps_insn_ptr ps_i)
  2541. {
  2542. int row;
  2543. gcc_assert (ps && ps_i);
  2544. row = SMODULO (ps_i->cycle, ps->ii);
  2545. if (! ps_i->prev_in_row)
  2546. {
  2547. gcc_assert (ps_i == ps->rows[row]);
  2548. ps->rows[row] = ps_i->next_in_row;
  2549. if (ps->rows[row])
  2550. ps->rows[row]->prev_in_row = NULL;
  2551. }
  2552. else
  2553. {
  2554. ps_i->prev_in_row->next_in_row = ps_i->next_in_row;
  2555. if (ps_i->next_in_row)
  2556. ps_i->next_in_row->prev_in_row = ps_i->prev_in_row;
  2557. }
  2558. ps->rows_length[row] -= 1;
  2559. free (ps_i);
  2560. return;
  2561. }
  2562. /* Unlike what literature describes for modulo scheduling (which focuses
  2563. on VLIW machines) the order of the instructions inside a cycle is
  2564. important. Given the bitmaps MUST_FOLLOW and MUST_PRECEDE we know
  2565. where the current instruction should go relative to the already
  2566. scheduled instructions in the given cycle. Go over these
  2567. instructions and find the first possible column to put it in. */
  2568. static bool
  2569. ps_insn_find_column (partial_schedule_ptr ps, ps_insn_ptr ps_i,
  2570. sbitmap must_precede, sbitmap must_follow)
  2571. {
  2572. ps_insn_ptr next_ps_i;
  2573. ps_insn_ptr first_must_follow = NULL;
  2574. ps_insn_ptr last_must_precede = NULL;
  2575. ps_insn_ptr last_in_row = NULL;
  2576. int row;
  2577. if (! ps_i)
  2578. return false;
  2579. row = SMODULO (ps_i->cycle, ps->ii);
  2580. /* Find the first must follow and the last must precede
  2581. and insert the node immediately after the must precede
  2582. but make sure that it there is no must follow after it. */
  2583. for (next_ps_i = ps->rows[row];
  2584. next_ps_i;
  2585. next_ps_i = next_ps_i->next_in_row)
  2586. {
  2587. if (must_follow
  2588. && bitmap_bit_p (must_follow, next_ps_i->id)
  2589. && ! first_must_follow)
  2590. first_must_follow = next_ps_i;
  2591. if (must_precede && bitmap_bit_p (must_precede, next_ps_i->id))
  2592. {
  2593. /* If we have already met a node that must follow, then
  2594. there is no possible column. */
  2595. if (first_must_follow)
  2596. return false;
  2597. else
  2598. last_must_precede = next_ps_i;
  2599. }
  2600. /* The closing branch must be the last in the row. */
  2601. if (must_precede
  2602. && bitmap_bit_p (must_precede, next_ps_i->id)
  2603. && JUMP_P (ps_rtl_insn (ps, next_ps_i->id)))
  2604. return false;
  2605. last_in_row = next_ps_i;
  2606. }
  2607. /* The closing branch is scheduled as well. Make sure there is no
  2608. dependent instruction after it as the branch should be the last
  2609. instruction in the row. */
  2610. if (JUMP_P (ps_rtl_insn (ps, ps_i->id)))
  2611. {
  2612. if (first_must_follow)
  2613. return false;
  2614. if (last_in_row)
  2615. {
  2616. /* Make the branch the last in the row. New instructions
  2617. will be inserted at the beginning of the row or after the
  2618. last must_precede instruction thus the branch is guaranteed
  2619. to remain the last instruction in the row. */
  2620. last_in_row->next_in_row = ps_i;
  2621. ps_i->prev_in_row = last_in_row;
  2622. ps_i->next_in_row = NULL;
  2623. }
  2624. else
  2625. ps->rows[row] = ps_i;
  2626. return true;
  2627. }
  2628. /* Now insert the node after INSERT_AFTER_PSI. */
  2629. if (! last_must_precede)
  2630. {
  2631. ps_i->next_in_row = ps->rows[row];
  2632. ps_i->prev_in_row = NULL;
  2633. if (ps_i->next_in_row)
  2634. ps_i->next_in_row->prev_in_row = ps_i;
  2635. ps->rows[row] = ps_i;
  2636. }
  2637. else
  2638. {
  2639. ps_i->next_in_row = last_must_precede->next_in_row;
  2640. last_must_precede->next_in_row = ps_i;
  2641. ps_i->prev_in_row = last_must_precede;
  2642. if (ps_i->next_in_row)
  2643. ps_i->next_in_row->prev_in_row = ps_i;
  2644. }
  2645. return true;
  2646. }
  2647. /* Advances the PS_INSN one column in its current row; returns false
  2648. in failure and true in success. Bit N is set in MUST_FOLLOW if
  2649. the node with cuid N must be come after the node pointed to by
  2650. PS_I when scheduled in the same cycle. */
  2651. static int
  2652. ps_insn_advance_column (partial_schedule_ptr ps, ps_insn_ptr ps_i,
  2653. sbitmap must_follow)
  2654. {
  2655. ps_insn_ptr prev, next;
  2656. int row;
  2657. if (!ps || !ps_i)
  2658. return false;
  2659. row = SMODULO (ps_i->cycle, ps->ii);
  2660. if (! ps_i->next_in_row)
  2661. return false;
  2662. /* Check if next_in_row is dependent on ps_i, both having same sched
  2663. times (typically ANTI_DEP). If so, ps_i cannot skip over it. */
  2664. if (must_follow && bitmap_bit_p (must_follow, ps_i->next_in_row->id))
  2665. return false;
  2666. /* Advance PS_I over its next_in_row in the doubly linked list. */
  2667. prev = ps_i->prev_in_row;
  2668. next = ps_i->next_in_row;
  2669. if (ps_i == ps->rows[row])
  2670. ps->rows[row] = next;
  2671. ps_i->next_in_row = next->next_in_row;
  2672. if (next->next_in_row)
  2673. next->next_in_row->prev_in_row = ps_i;
  2674. next->next_in_row = ps_i;
  2675. ps_i->prev_in_row = next;
  2676. next->prev_in_row = prev;
  2677. if (prev)
  2678. prev->next_in_row = next;
  2679. return true;
  2680. }
  2681. /* Inserts a DDG_NODE to the given partial schedule at the given cycle.
  2682. Returns 0 if this is not possible and a PS_INSN otherwise. Bit N is
  2683. set in MUST_PRECEDE/MUST_FOLLOW if the node with cuid N must be come
  2684. before/after (respectively) the node pointed to by PS_I when scheduled
  2685. in the same cycle. */
  2686. static ps_insn_ptr
  2687. add_node_to_ps (partial_schedule_ptr ps, int id, int cycle,
  2688. sbitmap must_precede, sbitmap must_follow)
  2689. {
  2690. ps_insn_ptr ps_i;
  2691. int row = SMODULO (cycle, ps->ii);
  2692. if (ps->rows_length[row] >= issue_rate)
  2693. return NULL;
  2694. ps_i = create_ps_insn (id, cycle);
  2695. /* Finds and inserts PS_I according to MUST_FOLLOW and
  2696. MUST_PRECEDE. */
  2697. if (! ps_insn_find_column (ps, ps_i, must_precede, must_follow))
  2698. {
  2699. free (ps_i);
  2700. return NULL;
  2701. }
  2702. ps->rows_length[row] += 1;
  2703. return ps_i;
  2704. }
  2705. /* Advance time one cycle. Assumes DFA is being used. */
  2706. static void
  2707. advance_one_cycle (void)
  2708. {
  2709. if (targetm.sched.dfa_pre_cycle_insn)
  2710. state_transition (curr_state,
  2711. targetm.sched.dfa_pre_cycle_insn ());
  2712. state_transition (curr_state, NULL);
  2713. if (targetm.sched.dfa_post_cycle_insn)
  2714. state_transition (curr_state,
  2715. targetm.sched.dfa_post_cycle_insn ());
  2716. }
  2717. /* Checks if PS has resource conflicts according to DFA, starting from
  2718. FROM cycle to TO cycle; returns true if there are conflicts and false
  2719. if there are no conflicts. Assumes DFA is being used. */
  2720. static int
  2721. ps_has_conflicts (partial_schedule_ptr ps, int from, int to)
  2722. {
  2723. int cycle;
  2724. state_reset (curr_state);
  2725. for (cycle = from; cycle <= to; cycle++)
  2726. {
  2727. ps_insn_ptr crr_insn;
  2728. /* Holds the remaining issue slots in the current row. */
  2729. int can_issue_more = issue_rate;
  2730. /* Walk through the DFA for the current row. */
  2731. for (crr_insn = ps->rows[SMODULO (cycle, ps->ii)];
  2732. crr_insn;
  2733. crr_insn = crr_insn->next_in_row)
  2734. {
  2735. rtx_insn *insn = ps_rtl_insn (ps, crr_insn->id);
  2736. if (!NONDEBUG_INSN_P (insn))
  2737. continue;
  2738. /* Check if there is room for the current insn. */
  2739. if (!can_issue_more || state_dead_lock_p (curr_state))
  2740. return true;
  2741. /* Update the DFA state and return with failure if the DFA found
  2742. resource conflicts. */
  2743. if (state_transition (curr_state, insn) >= 0)
  2744. return true;
  2745. if (targetm.sched.variable_issue)
  2746. can_issue_more =
  2747. targetm.sched.variable_issue (sched_dump, sched_verbose,
  2748. insn, can_issue_more);
  2749. /* A naked CLOBBER or USE generates no instruction, so don't
  2750. let them consume issue slots. */
  2751. else if (GET_CODE (PATTERN (insn)) != USE
  2752. && GET_CODE (PATTERN (insn)) != CLOBBER)
  2753. can_issue_more--;
  2754. }
  2755. /* Advance the DFA to the next cycle. */
  2756. advance_one_cycle ();
  2757. }
  2758. return false;
  2759. }
  2760. /* Checks if the given node causes resource conflicts when added to PS at
  2761. cycle C. If not the node is added to PS and returned; otherwise zero
  2762. is returned. Bit N is set in MUST_PRECEDE/MUST_FOLLOW if the node with
  2763. cuid N must be come before/after (respectively) the node pointed to by
  2764. PS_I when scheduled in the same cycle. */
  2765. ps_insn_ptr
  2766. ps_add_node_check_conflicts (partial_schedule_ptr ps, int n,
  2767. int c, sbitmap must_precede,
  2768. sbitmap must_follow)
  2769. {
  2770. int has_conflicts = 0;
  2771. ps_insn_ptr ps_i;
  2772. /* First add the node to the PS, if this succeeds check for
  2773. conflicts, trying different issue slots in the same row. */
  2774. if (! (ps_i = add_node_to_ps (ps, n, c, must_precede, must_follow)))
  2775. return NULL; /* Failed to insert the node at the given cycle. */
  2776. has_conflicts = ps_has_conflicts (ps, c, c)
  2777. || (ps->history > 0
  2778. && ps_has_conflicts (ps,
  2779. c - ps->history,
  2780. c + ps->history));
  2781. /* Try different issue slots to find one that the given node can be
  2782. scheduled in without conflicts. */
  2783. while (has_conflicts)
  2784. {
  2785. if (! ps_insn_advance_column (ps, ps_i, must_follow))
  2786. break;
  2787. has_conflicts = ps_has_conflicts (ps, c, c)
  2788. || (ps->history > 0
  2789. && ps_has_conflicts (ps,
  2790. c - ps->history,
  2791. c + ps->history));
  2792. }
  2793. if (has_conflicts)
  2794. {
  2795. remove_node_from_ps (ps, ps_i);
  2796. return NULL;
  2797. }
  2798. ps->min_cycle = MIN (ps->min_cycle, c);
  2799. ps->max_cycle = MAX (ps->max_cycle, c);
  2800. return ps_i;
  2801. }
  2802. /* Calculate the stage count of the partial schedule PS. The calculation
  2803. takes into account the rotation amount passed in ROTATION_AMOUNT. */
  2804. int
  2805. calculate_stage_count (partial_schedule_ptr ps, int rotation_amount)
  2806. {
  2807. int new_min_cycle = PS_MIN_CYCLE (ps) - rotation_amount;
  2808. int new_max_cycle = PS_MAX_CYCLE (ps) - rotation_amount;
  2809. int stage_count = CALC_STAGE_COUNT (-1, new_min_cycle, ps->ii);
  2810. /* The calculation of stage count is done adding the number of stages
  2811. before cycle zero and after cycle zero. */
  2812. stage_count += CALC_STAGE_COUNT (new_max_cycle, 0, ps->ii);
  2813. return stage_count;
  2814. }
  2815. /* Rotate the rows of PS such that insns scheduled at time
  2816. START_CYCLE will appear in row 0. Updates max/min_cycles. */
  2817. void
  2818. rotate_partial_schedule (partial_schedule_ptr ps, int start_cycle)
  2819. {
  2820. int i, row, backward_rotates;
  2821. int last_row = ps->ii - 1;
  2822. if (start_cycle == 0)
  2823. return;
  2824. backward_rotates = SMODULO (start_cycle, ps->ii);
  2825. /* Revisit later and optimize this into a single loop. */
  2826. for (i = 0; i < backward_rotates; i++)
  2827. {
  2828. ps_insn_ptr first_row = ps->rows[0];
  2829. int first_row_length = ps->rows_length[0];
  2830. for (row = 0; row < last_row; row++)
  2831. {
  2832. ps->rows[row] = ps->rows[row + 1];
  2833. ps->rows_length[row] = ps->rows_length[row + 1];
  2834. }
  2835. ps->rows[last_row] = first_row;
  2836. ps->rows_length[last_row] = first_row_length;
  2837. }
  2838. ps->max_cycle -= start_cycle;
  2839. ps->min_cycle -= start_cycle;
  2840. }
  2841. #endif /* INSN_SCHEDULING */
  2842. /* Run instruction scheduler. */
  2843. /* Perform SMS module scheduling. */
  2844. namespace {
  2845. const pass_data pass_data_sms =
  2846. {
  2847. RTL_PASS, /* type */
  2848. "sms", /* name */
  2849. OPTGROUP_NONE, /* optinfo_flags */
  2850. TV_SMS, /* tv_id */
  2851. 0, /* properties_required */
  2852. 0, /* properties_provided */
  2853. 0, /* properties_destroyed */
  2854. 0, /* todo_flags_start */
  2855. TODO_df_finish, /* todo_flags_finish */
  2856. };
  2857. class pass_sms : public rtl_opt_pass
  2858. {
  2859. public:
  2860. pass_sms (gcc::context *ctxt)
  2861. : rtl_opt_pass (pass_data_sms, ctxt)
  2862. {}
  2863. /* opt_pass methods: */
  2864. virtual bool gate (function *)
  2865. {
  2866. return (optimize > 0 && flag_modulo_sched);
  2867. }
  2868. virtual unsigned int execute (function *);
  2869. }; // class pass_sms
  2870. unsigned int
  2871. pass_sms::execute (function *fun ATTRIBUTE_UNUSED)
  2872. {
  2873. #ifdef INSN_SCHEDULING
  2874. basic_block bb;
  2875. /* Collect loop information to be used in SMS. */
  2876. cfg_layout_initialize (0);
  2877. sms_schedule ();
  2878. /* Update the life information, because we add pseudos. */
  2879. max_regno = max_reg_num ();
  2880. /* Finalize layout changes. */
  2881. FOR_EACH_BB_FN (bb, fun)
  2882. if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (fun))
  2883. bb->aux = bb->next_bb;
  2884. free_dominance_info (CDI_DOMINATORS);
  2885. cfg_layout_finalize ();
  2886. #endif /* INSN_SCHEDULING */
  2887. return 0;
  2888. }
  2889. } // anon namespace
  2890. rtl_opt_pass *
  2891. make_pass_sms (gcc::context *ctxt)
  2892. {
  2893. return new pass_sms (ctxt);
  2894. }