xtensa.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310
  1. /* Subroutines for insn-output.c for Tensilica's Xtensa architecture.
  2. Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. Contributed by Bob Wilson (bwilson@tensilica.com) at Tensilica.
  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 "rtl.h"
  21. #include "regs.h"
  22. #include "hard-reg-set.h"
  23. #include "predict.h"
  24. #include "vec.h"
  25. #include "hashtab.h"
  26. #include "hash-set.h"
  27. #include "machmode.h"
  28. #include "input.h"
  29. #include "function.h"
  30. #include "dominance.h"
  31. #include "cfg.h"
  32. #include "cfgrtl.h"
  33. #include "cfganal.h"
  34. #include "lcm.h"
  35. #include "cfgbuild.h"
  36. #include "cfgcleanup.h"
  37. #include "basic-block.h"
  38. #include "insn-config.h"
  39. #include "conditions.h"
  40. #include "insn-flags.h"
  41. #include "insn-attr.h"
  42. #include "insn-codes.h"
  43. #include "recog.h"
  44. #include "output.h"
  45. #include "symtab.h"
  46. #include "wide-int.h"
  47. #include "inchash.h"
  48. #include "tree.h"
  49. #include "fold-const.h"
  50. #include "stringpool.h"
  51. #include "stor-layout.h"
  52. #include "calls.h"
  53. #include "varasm.h"
  54. #include "flags.h"
  55. #include "statistics.h"
  56. #include "double-int.h"
  57. #include "real.h"
  58. #include "fixed-value.h"
  59. #include "alias.h"
  60. #include "expmed.h"
  61. #include "dojump.h"
  62. #include "explow.h"
  63. #include "emit-rtl.h"
  64. #include "stmt.h"
  65. #include "expr.h"
  66. #include "reload.h"
  67. #include "tm_p.h"
  68. #include "diagnostic-core.h"
  69. #include "optabs.h"
  70. #include "libfuncs.h"
  71. #include "ggc.h"
  72. #include "target.h"
  73. #include "target-def.h"
  74. #include "langhooks.h"
  75. #include "hash-table.h"
  76. #include "tree-ssa-alias.h"
  77. #include "internal-fn.h"
  78. #include "gimple-fold.h"
  79. #include "tree-eh.h"
  80. #include "gimple-expr.h"
  81. #include "is-a.h"
  82. #include "gimple.h"
  83. #include "gimplify.h"
  84. #include "df.h"
  85. #include "builtins.h"
  86. #include "dumpfile.h"
  87. #include "hw-doloop.h"
  88. #include "rtl-iter.h"
  89. /* Enumeration for all of the relational tests, so that we can build
  90. arrays indexed by the test type, and not worry about the order
  91. of EQ, NE, etc. */
  92. enum internal_test
  93. {
  94. ITEST_EQ,
  95. ITEST_NE,
  96. ITEST_GT,
  97. ITEST_GE,
  98. ITEST_LT,
  99. ITEST_LE,
  100. ITEST_GTU,
  101. ITEST_GEU,
  102. ITEST_LTU,
  103. ITEST_LEU,
  104. ITEST_MAX
  105. };
  106. /* Array giving truth value on whether or not a given hard register
  107. can support a given mode. */
  108. char xtensa_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
  109. /* Current frame size calculated by compute_frame_size. */
  110. unsigned xtensa_current_frame_size;
  111. /* Callee-save area size in the current frame calculated by compute_frame_size. */
  112. int xtensa_callee_save_size;
  113. /* Largest block move to handle in-line. */
  114. #define LARGEST_MOVE_RATIO 15
  115. /* Define the structure for the machine field in struct function. */
  116. struct GTY(()) machine_function
  117. {
  118. int accesses_prev_frame;
  119. bool need_a7_copy;
  120. bool vararg_a7;
  121. rtx vararg_a7_copy;
  122. rtx_insn *set_frame_ptr_insn;
  123. };
  124. /* Vector, indexed by hard register number, which contains 1 for a
  125. register that is allowable in a candidate for leaf function
  126. treatment. */
  127. const char xtensa_leaf_regs[FIRST_PSEUDO_REGISTER] =
  128. {
  129. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  130. 1, 1, 1,
  131. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  132. 1
  133. };
  134. static void xtensa_option_override (void);
  135. static enum internal_test map_test_to_internal_test (enum rtx_code);
  136. static rtx gen_int_relational (enum rtx_code, rtx, rtx, int *);
  137. static rtx gen_float_relational (enum rtx_code, rtx, rtx);
  138. static rtx gen_conditional_move (enum rtx_code, machine_mode, rtx, rtx);
  139. static rtx fixup_subreg_mem (rtx);
  140. static struct machine_function * xtensa_init_machine_status (void);
  141. static rtx xtensa_legitimize_tls_address (rtx);
  142. static rtx xtensa_legitimize_address (rtx, rtx, machine_mode);
  143. static bool xtensa_mode_dependent_address_p (const_rtx, addr_space_t);
  144. static bool xtensa_return_in_msb (const_tree);
  145. static void printx (FILE *, signed int);
  146. static rtx xtensa_builtin_saveregs (void);
  147. static bool xtensa_legitimate_address_p (machine_mode, rtx, bool);
  148. static unsigned int xtensa_multibss_section_type_flags (tree, const char *,
  149. int) ATTRIBUTE_UNUSED;
  150. static section *xtensa_select_rtx_section (machine_mode, rtx,
  151. unsigned HOST_WIDE_INT);
  152. static bool xtensa_rtx_costs (rtx, int, int, int, int *, bool);
  153. static int xtensa_register_move_cost (machine_mode, reg_class_t,
  154. reg_class_t);
  155. static int xtensa_memory_move_cost (machine_mode, reg_class_t, bool);
  156. static tree xtensa_build_builtin_va_list (void);
  157. static bool xtensa_return_in_memory (const_tree, const_tree);
  158. static tree xtensa_gimplify_va_arg_expr (tree, tree, gimple_seq *,
  159. gimple_seq *);
  160. static void xtensa_function_arg_advance (cumulative_args_t, machine_mode,
  161. const_tree, bool);
  162. static rtx xtensa_function_arg (cumulative_args_t, machine_mode,
  163. const_tree, bool);
  164. static rtx xtensa_function_incoming_arg (cumulative_args_t,
  165. machine_mode, const_tree, bool);
  166. static rtx xtensa_function_value (const_tree, const_tree, bool);
  167. static rtx xtensa_libcall_value (machine_mode, const_rtx);
  168. static bool xtensa_function_value_regno_p (const unsigned int);
  169. static unsigned int xtensa_function_arg_boundary (machine_mode,
  170. const_tree);
  171. static void xtensa_init_builtins (void);
  172. static tree xtensa_fold_builtin (tree, int, tree *, bool);
  173. static rtx xtensa_expand_builtin (tree, rtx, rtx, machine_mode, int);
  174. static void xtensa_va_start (tree, rtx);
  175. static bool xtensa_frame_pointer_required (void);
  176. static rtx xtensa_static_chain (const_tree, bool);
  177. static void xtensa_asm_trampoline_template (FILE *);
  178. static void xtensa_trampoline_init (rtx, tree, rtx);
  179. static bool xtensa_output_addr_const_extra (FILE *, rtx);
  180. static bool xtensa_cannot_force_const_mem (machine_mode, rtx);
  181. static reg_class_t xtensa_preferred_reload_class (rtx, reg_class_t);
  182. static reg_class_t xtensa_preferred_output_reload_class (rtx, reg_class_t);
  183. static reg_class_t xtensa_secondary_reload (bool, rtx, reg_class_t,
  184. machine_mode,
  185. struct secondary_reload_info *);
  186. static bool constantpool_address_p (const_rtx addr);
  187. static bool xtensa_legitimate_constant_p (machine_mode, rtx);
  188. static void xtensa_reorg (void);
  189. static bool xtensa_can_use_doloop_p (const widest_int &, const widest_int &,
  190. unsigned int, bool);
  191. static const char *xtensa_invalid_within_doloop (const rtx_insn *);
  192. static bool xtensa_member_type_forces_blk (const_tree,
  193. machine_mode mode);
  194. static void xtensa_conditional_register_usage (void);
  195. /* These hooks specify assembly directives for creating certain kinds
  196. of integer object. */
  197. #undef TARGET_ASM_ALIGNED_SI_OP
  198. #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
  199. #undef TARGET_ASM_SELECT_RTX_SECTION
  200. #define TARGET_ASM_SELECT_RTX_SECTION xtensa_select_rtx_section
  201. #undef TARGET_LEGITIMIZE_ADDRESS
  202. #define TARGET_LEGITIMIZE_ADDRESS xtensa_legitimize_address
  203. #undef TARGET_MODE_DEPENDENT_ADDRESS_P
  204. #define TARGET_MODE_DEPENDENT_ADDRESS_P xtensa_mode_dependent_address_p
  205. #undef TARGET_REGISTER_MOVE_COST
  206. #define TARGET_REGISTER_MOVE_COST xtensa_register_move_cost
  207. #undef TARGET_MEMORY_MOVE_COST
  208. #define TARGET_MEMORY_MOVE_COST xtensa_memory_move_cost
  209. #undef TARGET_RTX_COSTS
  210. #define TARGET_RTX_COSTS xtensa_rtx_costs
  211. #undef TARGET_ADDRESS_COST
  212. #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
  213. #undef TARGET_MEMBER_TYPE_FORCES_BLK
  214. #define TARGET_MEMBER_TYPE_FORCES_BLK xtensa_member_type_forces_blk
  215. #undef TARGET_BUILD_BUILTIN_VA_LIST
  216. #define TARGET_BUILD_BUILTIN_VA_LIST xtensa_build_builtin_va_list
  217. #undef TARGET_EXPAND_BUILTIN_VA_START
  218. #define TARGET_EXPAND_BUILTIN_VA_START xtensa_va_start
  219. #undef TARGET_PROMOTE_FUNCTION_MODE
  220. #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
  221. #undef TARGET_PROMOTE_PROTOTYPES
  222. #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
  223. #undef TARGET_RETURN_IN_MEMORY
  224. #define TARGET_RETURN_IN_MEMORY xtensa_return_in_memory
  225. #undef TARGET_FUNCTION_VALUE
  226. #define TARGET_FUNCTION_VALUE xtensa_function_value
  227. #undef TARGET_LIBCALL_VALUE
  228. #define TARGET_LIBCALL_VALUE xtensa_libcall_value
  229. #undef TARGET_FUNCTION_VALUE_REGNO_P
  230. #define TARGET_FUNCTION_VALUE_REGNO_P xtensa_function_value_regno_p
  231. #undef TARGET_SPLIT_COMPLEX_ARG
  232. #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
  233. #undef TARGET_MUST_PASS_IN_STACK
  234. #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
  235. #undef TARGET_FUNCTION_ARG_ADVANCE
  236. #define TARGET_FUNCTION_ARG_ADVANCE xtensa_function_arg_advance
  237. #undef TARGET_FUNCTION_ARG
  238. #define TARGET_FUNCTION_ARG xtensa_function_arg
  239. #undef TARGET_FUNCTION_INCOMING_ARG
  240. #define TARGET_FUNCTION_INCOMING_ARG xtensa_function_incoming_arg
  241. #undef TARGET_FUNCTION_ARG_BOUNDARY
  242. #define TARGET_FUNCTION_ARG_BOUNDARY xtensa_function_arg_boundary
  243. #undef TARGET_EXPAND_BUILTIN_SAVEREGS
  244. #define TARGET_EXPAND_BUILTIN_SAVEREGS xtensa_builtin_saveregs
  245. #undef TARGET_GIMPLIFY_VA_ARG_EXPR
  246. #define TARGET_GIMPLIFY_VA_ARG_EXPR xtensa_gimplify_va_arg_expr
  247. #undef TARGET_RETURN_IN_MSB
  248. #define TARGET_RETURN_IN_MSB xtensa_return_in_msb
  249. #undef TARGET_INIT_BUILTINS
  250. #define TARGET_INIT_BUILTINS xtensa_init_builtins
  251. #undef TARGET_FOLD_BUILTIN
  252. #define TARGET_FOLD_BUILTIN xtensa_fold_builtin
  253. #undef TARGET_EXPAND_BUILTIN
  254. #define TARGET_EXPAND_BUILTIN xtensa_expand_builtin
  255. #undef TARGET_PREFERRED_RELOAD_CLASS
  256. #define TARGET_PREFERRED_RELOAD_CLASS xtensa_preferred_reload_class
  257. #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
  258. #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS xtensa_preferred_output_reload_class
  259. #undef TARGET_SECONDARY_RELOAD
  260. #define TARGET_SECONDARY_RELOAD xtensa_secondary_reload
  261. #undef TARGET_HAVE_TLS
  262. #define TARGET_HAVE_TLS (TARGET_THREADPTR && HAVE_AS_TLS)
  263. #undef TARGET_CANNOT_FORCE_CONST_MEM
  264. #define TARGET_CANNOT_FORCE_CONST_MEM xtensa_cannot_force_const_mem
  265. #undef TARGET_LEGITIMATE_ADDRESS_P
  266. #define TARGET_LEGITIMATE_ADDRESS_P xtensa_legitimate_address_p
  267. #undef TARGET_FRAME_POINTER_REQUIRED
  268. #define TARGET_FRAME_POINTER_REQUIRED xtensa_frame_pointer_required
  269. #undef TARGET_STATIC_CHAIN
  270. #define TARGET_STATIC_CHAIN xtensa_static_chain
  271. #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
  272. #define TARGET_ASM_TRAMPOLINE_TEMPLATE xtensa_asm_trampoline_template
  273. #undef TARGET_TRAMPOLINE_INIT
  274. #define TARGET_TRAMPOLINE_INIT xtensa_trampoline_init
  275. #undef TARGET_OPTION_OVERRIDE
  276. #define TARGET_OPTION_OVERRIDE xtensa_option_override
  277. #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
  278. #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA xtensa_output_addr_const_extra
  279. #undef TARGET_LEGITIMATE_CONSTANT_P
  280. #define TARGET_LEGITIMATE_CONSTANT_P xtensa_legitimate_constant_p
  281. #undef TARGET_MACHINE_DEPENDENT_REORG
  282. #define TARGET_MACHINE_DEPENDENT_REORG xtensa_reorg
  283. #undef TARGET_CAN_USE_DOLOOP_P
  284. #define TARGET_CAN_USE_DOLOOP_P xtensa_can_use_doloop_p
  285. #undef TARGET_INVALID_WITHIN_DOLOOP
  286. #define TARGET_INVALID_WITHIN_DOLOOP xtensa_invalid_within_doloop
  287. #undef TARGET_CONDITIONAL_REGISTER_USAGE
  288. #define TARGET_CONDITIONAL_REGISTER_USAGE xtensa_conditional_register_usage
  289. struct gcc_target targetm = TARGET_INITIALIZER;
  290. /* Functions to test Xtensa immediate operand validity. */
  291. bool
  292. xtensa_simm8 (HOST_WIDE_INT v)
  293. {
  294. return v >= -128 && v <= 127;
  295. }
  296. bool
  297. xtensa_simm8x256 (HOST_WIDE_INT v)
  298. {
  299. return (v & 255) == 0 && (v >= -32768 && v <= 32512);
  300. }
  301. bool
  302. xtensa_simm12b (HOST_WIDE_INT v)
  303. {
  304. return v >= -2048 && v <= 2047;
  305. }
  306. static bool
  307. xtensa_uimm8 (HOST_WIDE_INT v)
  308. {
  309. return v >= 0 && v <= 255;
  310. }
  311. static bool
  312. xtensa_uimm8x2 (HOST_WIDE_INT v)
  313. {
  314. return (v & 1) == 0 && (v >= 0 && v <= 510);
  315. }
  316. static bool
  317. xtensa_uimm8x4 (HOST_WIDE_INT v)
  318. {
  319. return (v & 3) == 0 && (v >= 0 && v <= 1020);
  320. }
  321. static bool
  322. xtensa_b4const (HOST_WIDE_INT v)
  323. {
  324. switch (v)
  325. {
  326. case -1:
  327. case 1:
  328. case 2:
  329. case 3:
  330. case 4:
  331. case 5:
  332. case 6:
  333. case 7:
  334. case 8:
  335. case 10:
  336. case 12:
  337. case 16:
  338. case 32:
  339. case 64:
  340. case 128:
  341. case 256:
  342. return true;
  343. }
  344. return false;
  345. }
  346. bool
  347. xtensa_b4const_or_zero (HOST_WIDE_INT v)
  348. {
  349. if (v == 0)
  350. return true;
  351. return xtensa_b4const (v);
  352. }
  353. bool
  354. xtensa_b4constu (HOST_WIDE_INT v)
  355. {
  356. switch (v)
  357. {
  358. case 32768:
  359. case 65536:
  360. case 2:
  361. case 3:
  362. case 4:
  363. case 5:
  364. case 6:
  365. case 7:
  366. case 8:
  367. case 10:
  368. case 12:
  369. case 16:
  370. case 32:
  371. case 64:
  372. case 128:
  373. case 256:
  374. return true;
  375. }
  376. return false;
  377. }
  378. bool
  379. xtensa_mask_immediate (HOST_WIDE_INT v)
  380. {
  381. #define MAX_MASK_SIZE 16
  382. int mask_size;
  383. for (mask_size = 1; mask_size <= MAX_MASK_SIZE; mask_size++)
  384. {
  385. if ((v & 1) == 0)
  386. return false;
  387. v = v >> 1;
  388. if (v == 0)
  389. return true;
  390. }
  391. return false;
  392. }
  393. /* This is just like the standard true_regnum() function except that it
  394. works even when reg_renumber is not initialized. */
  395. int
  396. xt_true_regnum (rtx x)
  397. {
  398. if (GET_CODE (x) == REG)
  399. {
  400. if (reg_renumber
  401. && REGNO (x) >= FIRST_PSEUDO_REGISTER
  402. && reg_renumber[REGNO (x)] >= 0)
  403. return reg_renumber[REGNO (x)];
  404. return REGNO (x);
  405. }
  406. if (GET_CODE (x) == SUBREG)
  407. {
  408. int base = xt_true_regnum (SUBREG_REG (x));
  409. if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
  410. return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
  411. GET_MODE (SUBREG_REG (x)),
  412. SUBREG_BYTE (x), GET_MODE (x));
  413. }
  414. return -1;
  415. }
  416. int
  417. xtensa_valid_move (machine_mode mode, rtx *operands)
  418. {
  419. /* Either the destination or source must be a register, and the
  420. MAC16 accumulator doesn't count. */
  421. if (register_operand (operands[0], mode))
  422. {
  423. int dst_regnum = xt_true_regnum (operands[0]);
  424. /* The stack pointer can only be assigned with a MOVSP opcode. */
  425. if (dst_regnum == STACK_POINTER_REGNUM)
  426. return !TARGET_WINDOWED_ABI
  427. || (mode == SImode
  428. && register_operand (operands[1], mode)
  429. && !ACC_REG_P (xt_true_regnum (operands[1])));
  430. if (!ACC_REG_P (dst_regnum))
  431. return true;
  432. }
  433. if (register_operand (operands[1], mode))
  434. {
  435. int src_regnum = xt_true_regnum (operands[1]);
  436. if (!ACC_REG_P (src_regnum))
  437. return true;
  438. }
  439. return FALSE;
  440. }
  441. int
  442. smalloffset_mem_p (rtx op)
  443. {
  444. if (GET_CODE (op) == MEM)
  445. {
  446. rtx addr = XEXP (op, 0);
  447. if (GET_CODE (addr) == REG)
  448. return BASE_REG_P (addr, 0);
  449. if (GET_CODE (addr) == PLUS)
  450. {
  451. rtx offset = XEXP (addr, 0);
  452. HOST_WIDE_INT val;
  453. if (GET_CODE (offset) != CONST_INT)
  454. offset = XEXP (addr, 1);
  455. if (GET_CODE (offset) != CONST_INT)
  456. return FALSE;
  457. val = INTVAL (offset);
  458. return (val & 3) == 0 && (val >= 0 && val <= 60);
  459. }
  460. }
  461. return FALSE;
  462. }
  463. static bool
  464. constantpool_address_p (const_rtx addr)
  465. {
  466. const_rtx sym = addr;
  467. if (GET_CODE (addr) == CONST)
  468. {
  469. rtx offset;
  470. /* Only handle (PLUS (SYM, OFFSET)) form. */
  471. addr = XEXP (addr, 0);
  472. if (GET_CODE (addr) != PLUS)
  473. return false;
  474. /* Make sure the address is word aligned. */
  475. offset = XEXP (addr, 1);
  476. if ((!CONST_INT_P (offset))
  477. || ((INTVAL (offset) & 3) != 0))
  478. return false;
  479. sym = XEXP (addr, 0);
  480. }
  481. if ((GET_CODE (sym) == SYMBOL_REF)
  482. && CONSTANT_POOL_ADDRESS_P (sym))
  483. return true;
  484. return false;
  485. }
  486. int
  487. constantpool_mem_p (rtx op)
  488. {
  489. if (GET_CODE (op) == SUBREG)
  490. op = SUBREG_REG (op);
  491. if (GET_CODE (op) == MEM)
  492. return constantpool_address_p (XEXP (op, 0));
  493. return FALSE;
  494. }
  495. /* Return TRUE if X is a thread-local symbol. */
  496. static bool
  497. xtensa_tls_symbol_p (rtx x)
  498. {
  499. if (! TARGET_HAVE_TLS)
  500. return false;
  501. return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
  502. }
  503. void
  504. xtensa_extend_reg (rtx dst, rtx src)
  505. {
  506. rtx temp = gen_reg_rtx (SImode);
  507. rtx shift = GEN_INT (BITS_PER_WORD - GET_MODE_BITSIZE (GET_MODE (src)));
  508. /* Generate paradoxical subregs as needed so that the modes match. */
  509. src = simplify_gen_subreg (SImode, src, GET_MODE (src), 0);
  510. dst = simplify_gen_subreg (SImode, dst, GET_MODE (dst), 0);
  511. emit_insn (gen_ashlsi3 (temp, src, shift));
  512. emit_insn (gen_ashrsi3 (dst, temp, shift));
  513. }
  514. bool
  515. xtensa_mem_offset (unsigned v, machine_mode mode)
  516. {
  517. switch (mode)
  518. {
  519. case BLKmode:
  520. /* Handle the worst case for block moves. See xtensa_expand_block_move
  521. where we emit an optimized block move operation if the block can be
  522. moved in < "move_ratio" pieces. The worst case is when the block is
  523. aligned but has a size of (3 mod 4) (does this happen?) so that the
  524. last piece requires a byte load/store. */
  525. return (xtensa_uimm8 (v)
  526. && xtensa_uimm8 (v + MOVE_MAX * LARGEST_MOVE_RATIO));
  527. case QImode:
  528. return xtensa_uimm8 (v);
  529. case HImode:
  530. return xtensa_uimm8x2 (v);
  531. case DFmode:
  532. return (xtensa_uimm8x4 (v) && xtensa_uimm8x4 (v + 4));
  533. default:
  534. break;
  535. }
  536. return xtensa_uimm8x4 (v);
  537. }
  538. /* Make normal rtx_code into something we can index from an array. */
  539. static enum internal_test
  540. map_test_to_internal_test (enum rtx_code test_code)
  541. {
  542. enum internal_test test = ITEST_MAX;
  543. switch (test_code)
  544. {
  545. default: break;
  546. case EQ: test = ITEST_EQ; break;
  547. case NE: test = ITEST_NE; break;
  548. case GT: test = ITEST_GT; break;
  549. case GE: test = ITEST_GE; break;
  550. case LT: test = ITEST_LT; break;
  551. case LE: test = ITEST_LE; break;
  552. case GTU: test = ITEST_GTU; break;
  553. case GEU: test = ITEST_GEU; break;
  554. case LTU: test = ITEST_LTU; break;
  555. case LEU: test = ITEST_LEU; break;
  556. }
  557. return test;
  558. }
  559. /* Generate the code to compare two integer values. The return value is
  560. the comparison expression. */
  561. static rtx
  562. gen_int_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
  563. rtx cmp0, /* first operand to compare */
  564. rtx cmp1, /* second operand to compare */
  565. int *p_invert /* whether branch needs to reverse test */)
  566. {
  567. struct cmp_info
  568. {
  569. enum rtx_code test_code; /* test code to use in insn */
  570. bool (*const_range_p) (HOST_WIDE_INT); /* range check function */
  571. int const_add; /* constant to add (convert LE -> LT) */
  572. int reverse_regs; /* reverse registers in test */
  573. int invert_const; /* != 0 if invert value if cmp1 is constant */
  574. int invert_reg; /* != 0 if invert value if cmp1 is register */
  575. int unsignedp; /* != 0 for unsigned comparisons. */
  576. };
  577. static struct cmp_info info[ (int)ITEST_MAX ] = {
  578. { EQ, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* EQ */
  579. { NE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* NE */
  580. { LT, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* GT */
  581. { GE, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* GE */
  582. { LT, xtensa_b4const_or_zero, 0, 0, 0, 0, 0 }, /* LT */
  583. { GE, xtensa_b4const_or_zero, 1, 1, 1, 0, 0 }, /* LE */
  584. { LTU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* GTU */
  585. { GEU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* GEU */
  586. { LTU, xtensa_b4constu, 0, 0, 0, 0, 1 }, /* LTU */
  587. { GEU, xtensa_b4constu, 1, 1, 1, 0, 1 }, /* LEU */
  588. };
  589. enum internal_test test;
  590. machine_mode mode;
  591. struct cmp_info *p_info;
  592. test = map_test_to_internal_test (test_code);
  593. gcc_assert (test != ITEST_MAX);
  594. p_info = &info[ (int)test ];
  595. mode = GET_MODE (cmp0);
  596. if (mode == VOIDmode)
  597. mode = GET_MODE (cmp1);
  598. /* Make sure we can handle any constants given to us. */
  599. if (GET_CODE (cmp1) == CONST_INT)
  600. {
  601. HOST_WIDE_INT value = INTVAL (cmp1);
  602. unsigned HOST_WIDE_INT uvalue = (unsigned HOST_WIDE_INT)value;
  603. /* if the immediate overflows or does not fit in the immediate field,
  604. spill it to a register */
  605. if ((p_info->unsignedp ?
  606. (uvalue + p_info->const_add > uvalue) :
  607. (value + p_info->const_add > value)) != (p_info->const_add > 0))
  608. {
  609. cmp1 = force_reg (mode, cmp1);
  610. }
  611. else if (!(p_info->const_range_p) (value + p_info->const_add))
  612. {
  613. cmp1 = force_reg (mode, cmp1);
  614. }
  615. }
  616. else if ((GET_CODE (cmp1) != REG) && (GET_CODE (cmp1) != SUBREG))
  617. {
  618. cmp1 = force_reg (mode, cmp1);
  619. }
  620. /* See if we need to invert the result. */
  621. *p_invert = ((GET_CODE (cmp1) == CONST_INT)
  622. ? p_info->invert_const
  623. : p_info->invert_reg);
  624. /* Comparison to constants, may involve adding 1 to change a LT into LE.
  625. Comparison between two registers, may involve switching operands. */
  626. if (GET_CODE (cmp1) == CONST_INT)
  627. {
  628. if (p_info->const_add != 0)
  629. cmp1 = GEN_INT (INTVAL (cmp1) + p_info->const_add);
  630. }
  631. else if (p_info->reverse_regs)
  632. {
  633. rtx temp = cmp0;
  634. cmp0 = cmp1;
  635. cmp1 = temp;
  636. }
  637. return gen_rtx_fmt_ee (p_info->test_code, VOIDmode, cmp0, cmp1);
  638. }
  639. /* Generate the code to compare two float values. The return value is
  640. the comparison expression. */
  641. static rtx
  642. gen_float_relational (enum rtx_code test_code, /* relational test (EQ, etc) */
  643. rtx cmp0, /* first operand to compare */
  644. rtx cmp1 /* second operand to compare */)
  645. {
  646. rtx (*gen_fn) (rtx, rtx, rtx);
  647. rtx brtmp;
  648. int reverse_regs, invert;
  649. switch (test_code)
  650. {
  651. case EQ: reverse_regs = 0; invert = 0; gen_fn = gen_seq_sf; break;
  652. case NE: reverse_regs = 0; invert = 1; gen_fn = gen_seq_sf; break;
  653. case LE: reverse_regs = 0; invert = 0; gen_fn = gen_sle_sf; break;
  654. case GT: reverse_regs = 1; invert = 0; gen_fn = gen_slt_sf; break;
  655. case LT: reverse_regs = 0; invert = 0; gen_fn = gen_slt_sf; break;
  656. case GE: reverse_regs = 1; invert = 0; gen_fn = gen_sle_sf; break;
  657. case UNEQ: reverse_regs = 0; invert = 0; gen_fn = gen_suneq_sf; break;
  658. case LTGT: reverse_regs = 0; invert = 1; gen_fn = gen_suneq_sf; break;
  659. case UNLE: reverse_regs = 0; invert = 0; gen_fn = gen_sunle_sf; break;
  660. case UNGT: reverse_regs = 1; invert = 0; gen_fn = gen_sunlt_sf; break;
  661. case UNLT: reverse_regs = 0; invert = 0; gen_fn = gen_sunlt_sf; break;
  662. case UNGE: reverse_regs = 1; invert = 0; gen_fn = gen_sunle_sf; break;
  663. case UNORDERED:
  664. reverse_regs = 0; invert = 0; gen_fn = gen_sunordered_sf; break;
  665. case ORDERED:
  666. reverse_regs = 0; invert = 1; gen_fn = gen_sunordered_sf; break;
  667. default:
  668. fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
  669. reverse_regs = 0; invert = 0; gen_fn = 0; /* avoid compiler warnings */
  670. }
  671. if (reverse_regs)
  672. {
  673. rtx temp = cmp0;
  674. cmp0 = cmp1;
  675. cmp1 = temp;
  676. }
  677. brtmp = gen_rtx_REG (CCmode, FPCC_REGNUM);
  678. emit_insn (gen_fn (brtmp, cmp0, cmp1));
  679. return gen_rtx_fmt_ee (invert ? EQ : NE, VOIDmode, brtmp, const0_rtx);
  680. }
  681. void
  682. xtensa_expand_conditional_branch (rtx *operands, machine_mode mode)
  683. {
  684. enum rtx_code test_code = GET_CODE (operands[0]);
  685. rtx cmp0 = operands[1];
  686. rtx cmp1 = operands[2];
  687. rtx cmp;
  688. int invert;
  689. rtx label1, label2;
  690. switch (mode)
  691. {
  692. case DFmode:
  693. default:
  694. fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode, cmp0, cmp1));
  695. case SImode:
  696. invert = FALSE;
  697. cmp = gen_int_relational (test_code, cmp0, cmp1, &invert);
  698. break;
  699. case SFmode:
  700. if (!TARGET_HARD_FLOAT)
  701. fatal_insn ("bad test", gen_rtx_fmt_ee (test_code, VOIDmode,
  702. cmp0, cmp1));
  703. invert = FALSE;
  704. cmp = gen_float_relational (test_code, cmp0, cmp1);
  705. break;
  706. }
  707. /* Generate the branch. */
  708. label1 = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
  709. label2 = pc_rtx;
  710. if (invert)
  711. {
  712. label2 = label1;
  713. label1 = pc_rtx;
  714. }
  715. emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
  716. gen_rtx_IF_THEN_ELSE (VOIDmode, cmp,
  717. label1,
  718. label2)));
  719. }
  720. static rtx
  721. gen_conditional_move (enum rtx_code code, machine_mode mode,
  722. rtx op0, rtx op1)
  723. {
  724. if (mode == SImode)
  725. {
  726. rtx cmp;
  727. /* Jump optimization calls get_condition() which canonicalizes
  728. comparisons like (GE x <const>) to (GT x <const-1>).
  729. Transform those comparisons back to GE, since that is the
  730. comparison supported in Xtensa. We shouldn't have to
  731. transform <LE x const> comparisons, because neither
  732. xtensa_expand_conditional_branch() nor get_condition() will
  733. produce them. */
  734. if ((code == GT) && (op1 == constm1_rtx))
  735. {
  736. code = GE;
  737. op1 = const0_rtx;
  738. }
  739. cmp = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
  740. if (boolean_operator (cmp, VOIDmode))
  741. {
  742. /* Swap the operands to make const0 second. */
  743. if (op0 == const0_rtx)
  744. {
  745. op0 = op1;
  746. op1 = const0_rtx;
  747. }
  748. /* If not comparing against zero, emit a comparison (subtract). */
  749. if (op1 != const0_rtx)
  750. {
  751. op0 = expand_binop (SImode, sub_optab, op0, op1,
  752. 0, 0, OPTAB_LIB_WIDEN);
  753. op1 = const0_rtx;
  754. }
  755. }
  756. else if (branch_operator (cmp, VOIDmode))
  757. {
  758. /* Swap the operands to make const0 second. */
  759. if (op0 == const0_rtx)
  760. {
  761. op0 = op1;
  762. op1 = const0_rtx;
  763. switch (code)
  764. {
  765. case LT: code = GE; break;
  766. case GE: code = LT; break;
  767. default: gcc_unreachable ();
  768. }
  769. }
  770. if (op1 != const0_rtx)
  771. return 0;
  772. }
  773. else
  774. return 0;
  775. return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
  776. }
  777. if (TARGET_HARD_FLOAT && mode == SFmode)
  778. return gen_float_relational (code, op0, op1);
  779. return 0;
  780. }
  781. int
  782. xtensa_expand_conditional_move (rtx *operands, int isflt)
  783. {
  784. rtx dest = operands[0];
  785. rtx cmp = operands[1];
  786. machine_mode cmp_mode = GET_MODE (XEXP (cmp, 0));
  787. rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
  788. if (!(cmp = gen_conditional_move (GET_CODE (cmp), cmp_mode,
  789. XEXP (cmp, 0), XEXP (cmp, 1))))
  790. return 0;
  791. if (isflt)
  792. gen_fn = (cmp_mode == SImode
  793. ? gen_movsfcc_internal0
  794. : gen_movsfcc_internal1);
  795. else
  796. gen_fn = (cmp_mode == SImode
  797. ? gen_movsicc_internal0
  798. : gen_movsicc_internal1);
  799. emit_insn (gen_fn (dest, XEXP (cmp, 0), operands[2], operands[3], cmp));
  800. return 1;
  801. }
  802. int
  803. xtensa_expand_scc (rtx operands[4], machine_mode cmp_mode)
  804. {
  805. rtx dest = operands[0];
  806. rtx cmp;
  807. rtx one_tmp, zero_tmp;
  808. rtx (*gen_fn) (rtx, rtx, rtx, rtx, rtx);
  809. if (!(cmp = gen_conditional_move (GET_CODE (operands[1]), cmp_mode,
  810. operands[2], operands[3])))
  811. return 0;
  812. one_tmp = gen_reg_rtx (SImode);
  813. zero_tmp = gen_reg_rtx (SImode);
  814. emit_insn (gen_movsi (one_tmp, const_true_rtx));
  815. emit_insn (gen_movsi (zero_tmp, const0_rtx));
  816. gen_fn = (cmp_mode == SImode
  817. ? gen_movsicc_internal0
  818. : gen_movsicc_internal1);
  819. emit_insn (gen_fn (dest, XEXP (cmp, 0), one_tmp, zero_tmp, cmp));
  820. return 1;
  821. }
  822. /* Split OP[1] into OP[2,3] and likewise for OP[0] into OP[0,1]. MODE is
  823. for the output, i.e., the input operands are twice as big as MODE. */
  824. void
  825. xtensa_split_operand_pair (rtx operands[4], machine_mode mode)
  826. {
  827. switch (GET_CODE (operands[1]))
  828. {
  829. case REG:
  830. operands[3] = gen_rtx_REG (mode, REGNO (operands[1]) + 1);
  831. operands[2] = gen_rtx_REG (mode, REGNO (operands[1]));
  832. break;
  833. case MEM:
  834. operands[3] = adjust_address (operands[1], mode, GET_MODE_SIZE (mode));
  835. operands[2] = adjust_address (operands[1], mode, 0);
  836. break;
  837. case CONST_INT:
  838. case CONST_DOUBLE:
  839. split_double (operands[1], &operands[2], &operands[3]);
  840. break;
  841. default:
  842. gcc_unreachable ();
  843. }
  844. switch (GET_CODE (operands[0]))
  845. {
  846. case REG:
  847. operands[1] = gen_rtx_REG (mode, REGNO (operands[0]) + 1);
  848. operands[0] = gen_rtx_REG (mode, REGNO (operands[0]));
  849. break;
  850. case MEM:
  851. operands[1] = adjust_address (operands[0], mode, GET_MODE_SIZE (mode));
  852. operands[0] = adjust_address (operands[0], mode, 0);
  853. break;
  854. default:
  855. gcc_unreachable ();
  856. }
  857. }
  858. /* Emit insns to move operands[1] into operands[0].
  859. Return 1 if we have written out everything that needs to be done to
  860. do the move. Otherwise, return 0 and the caller will emit the move
  861. normally. */
  862. int
  863. xtensa_emit_move_sequence (rtx *operands, machine_mode mode)
  864. {
  865. rtx src = operands[1];
  866. if (CONSTANT_P (src)
  867. && (GET_CODE (src) != CONST_INT || ! xtensa_simm12b (INTVAL (src))))
  868. {
  869. rtx dst = operands[0];
  870. if (xtensa_tls_referenced_p (src))
  871. {
  872. rtx addend = NULL;
  873. if (GET_CODE (src) == CONST && GET_CODE (XEXP (src, 0)) == PLUS)
  874. {
  875. addend = XEXP (XEXP (src, 0), 1);
  876. src = XEXP (XEXP (src, 0), 0);
  877. }
  878. src = xtensa_legitimize_tls_address (src);
  879. if (addend)
  880. {
  881. src = gen_rtx_PLUS (mode, src, addend);
  882. src = force_operand (src, dst);
  883. }
  884. emit_move_insn (dst, src);
  885. return 1;
  886. }
  887. if (! TARGET_CONST16)
  888. {
  889. src = force_const_mem (SImode, src);
  890. operands[1] = src;
  891. }
  892. /* PC-relative loads are always SImode, and CONST16 is only
  893. supported in the movsi pattern, so add a SUBREG for any other
  894. (smaller) mode. */
  895. if (mode != SImode)
  896. {
  897. if (register_operand (dst, mode))
  898. {
  899. emit_move_insn (simplify_gen_subreg (SImode, dst, mode, 0), src);
  900. return 1;
  901. }
  902. else
  903. {
  904. src = force_reg (SImode, src);
  905. src = gen_lowpart_SUBREG (mode, src);
  906. operands[1] = src;
  907. }
  908. }
  909. }
  910. if (!(reload_in_progress | reload_completed)
  911. && !xtensa_valid_move (mode, operands))
  912. operands[1] = force_reg (mode, operands[1]);
  913. operands[1] = xtensa_copy_incoming_a7 (operands[1]);
  914. /* During reload we don't want to emit (subreg:X (mem:Y)) since that
  915. instruction won't be recognized after reload, so we remove the
  916. subreg and adjust mem accordingly. */
  917. if (reload_in_progress)
  918. {
  919. operands[0] = fixup_subreg_mem (operands[0]);
  920. operands[1] = fixup_subreg_mem (operands[1]);
  921. }
  922. return 0;
  923. }
  924. static rtx
  925. fixup_subreg_mem (rtx x)
  926. {
  927. if (GET_CODE (x) == SUBREG
  928. && GET_CODE (SUBREG_REG (x)) == REG
  929. && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER)
  930. {
  931. rtx temp =
  932. gen_rtx_SUBREG (GET_MODE (x),
  933. reg_equiv_mem (REGNO (SUBREG_REG (x))),
  934. SUBREG_BYTE (x));
  935. x = alter_subreg (&temp, true);
  936. }
  937. return x;
  938. }
  939. /* Check if an incoming argument in a7 is expected to be used soon and
  940. if OPND is a register or register pair that includes a7. If so,
  941. create a new pseudo and copy a7 into that pseudo at the very
  942. beginning of the function, followed by the special "set_frame_ptr"
  943. unspec_volatile insn. The return value is either the original
  944. operand, if it is not a7, or the new pseudo containing a copy of
  945. the incoming argument. This is necessary because the register
  946. allocator will ignore conflicts with a7 and may either assign some
  947. other pseudo to a7 or use a7 as the hard_frame_pointer, clobbering
  948. the incoming argument in a7. By copying the argument out of a7 as
  949. the very first thing, and then immediately following that with an
  950. unspec_volatile to keep the scheduler away, we should avoid any
  951. problems. Putting the set_frame_ptr insn at the beginning, with
  952. only the a7 copy before it, also makes it easier for the prologue
  953. expander to initialize the frame pointer after the a7 copy and to
  954. fix up the a7 copy to use the stack pointer instead of the frame
  955. pointer. */
  956. rtx
  957. xtensa_copy_incoming_a7 (rtx opnd)
  958. {
  959. rtx entry_insns = 0;
  960. rtx reg, tmp;
  961. machine_mode mode;
  962. if (!cfun->machine->need_a7_copy)
  963. return opnd;
  964. /* This function should never be called again once a7 has been copied. */
  965. gcc_assert (!cfun->machine->set_frame_ptr_insn);
  966. mode = GET_MODE (opnd);
  967. /* The operand using a7 may come in a later instruction, so just return
  968. the original operand if it doesn't use a7. */
  969. reg = opnd;
  970. if (GET_CODE (reg) == SUBREG)
  971. {
  972. gcc_assert (SUBREG_BYTE (reg) == 0);
  973. reg = SUBREG_REG (reg);
  974. }
  975. if (GET_CODE (reg) != REG
  976. || REGNO (reg) > A7_REG
  977. || REGNO (reg) + HARD_REGNO_NREGS (A7_REG, mode) <= A7_REG)
  978. return opnd;
  979. /* 1-word args will always be in a7; 2-word args in a6/a7. */
  980. gcc_assert (REGNO (reg) + HARD_REGNO_NREGS (A7_REG, mode) - 1 == A7_REG);
  981. cfun->machine->need_a7_copy = false;
  982. /* Copy a7 to a new pseudo at the function entry. Use gen_raw_REG to
  983. create the REG for a7 so that hard_frame_pointer_rtx is not used. */
  984. start_sequence ();
  985. tmp = gen_reg_rtx (mode);
  986. switch (mode)
  987. {
  988. case DFmode:
  989. case DImode:
  990. /* Copy the value out of A7 here but keep the first word in A6 until
  991. after the set_frame_ptr insn. Otherwise, the register allocator
  992. may decide to put "subreg (tmp, 0)" in A7 and clobber the incoming
  993. value. */
  994. emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 4),
  995. gen_raw_REG (SImode, A7_REG)));
  996. break;
  997. case SFmode:
  998. emit_insn (gen_movsf_internal (tmp, gen_raw_REG (mode, A7_REG)));
  999. break;
  1000. case SImode:
  1001. emit_insn (gen_movsi_internal (tmp, gen_raw_REG (mode, A7_REG)));
  1002. break;
  1003. case HImode:
  1004. emit_insn (gen_movhi_internal (tmp, gen_raw_REG (mode, A7_REG)));
  1005. break;
  1006. case QImode:
  1007. emit_insn (gen_movqi_internal (tmp, gen_raw_REG (mode, A7_REG)));
  1008. break;
  1009. default:
  1010. gcc_unreachable ();
  1011. }
  1012. cfun->machine->set_frame_ptr_insn = emit_insn (gen_set_frame_ptr ());
  1013. /* For DF and DI mode arguments, copy the incoming value in A6 now. */
  1014. if (mode == DFmode || mode == DImode)
  1015. emit_insn (gen_movsi_internal (gen_rtx_SUBREG (SImode, tmp, 0),
  1016. gen_rtx_REG (SImode, A7_REG - 1)));
  1017. entry_insns = get_insns ();
  1018. end_sequence ();
  1019. if (cfun->machine->vararg_a7)
  1020. {
  1021. /* This is called from within builtin_saveregs, which will insert the
  1022. saveregs code at the function entry, ahead of anything placed at
  1023. the function entry now. Instead, save the sequence to be inserted
  1024. at the beginning of the saveregs code. */
  1025. cfun->machine->vararg_a7_copy = entry_insns;
  1026. }
  1027. else
  1028. {
  1029. /* Put entry_insns after the NOTE that starts the function. If
  1030. this is inside a start_sequence, make the outer-level insn
  1031. chain current, so the code is placed at the start of the
  1032. function. */
  1033. push_topmost_sequence ();
  1034. /* Do not use entry_of_function() here. This is called from within
  1035. expand_function_start, when the CFG still holds GIMPLE. */
  1036. emit_insn_after (entry_insns, get_insns ());
  1037. pop_topmost_sequence ();
  1038. }
  1039. return tmp;
  1040. }
  1041. /* Try to expand a block move operation to a sequence of RTL move
  1042. instructions. If not optimizing, or if the block size is not a
  1043. constant, or if the block is too large, the expansion fails and GCC
  1044. falls back to calling memcpy().
  1045. operands[0] is the destination
  1046. operands[1] is the source
  1047. operands[2] is the length
  1048. operands[3] is the alignment */
  1049. int
  1050. xtensa_expand_block_move (rtx *operands)
  1051. {
  1052. static const machine_mode mode_from_align[] =
  1053. {
  1054. VOIDmode, QImode, HImode, VOIDmode, SImode,
  1055. };
  1056. rtx dst_mem = operands[0];
  1057. rtx src_mem = operands[1];
  1058. HOST_WIDE_INT bytes, align;
  1059. int num_pieces, move_ratio;
  1060. rtx temp[2];
  1061. machine_mode mode[2];
  1062. int amount[2];
  1063. bool active[2];
  1064. int phase = 0;
  1065. int next;
  1066. int offset_ld = 0;
  1067. int offset_st = 0;
  1068. rtx x;
  1069. /* If this is not a fixed size move, just call memcpy. */
  1070. if (!optimize || (GET_CODE (operands[2]) != CONST_INT))
  1071. return 0;
  1072. bytes = INTVAL (operands[2]);
  1073. align = INTVAL (operands[3]);
  1074. /* Anything to move? */
  1075. if (bytes <= 0)
  1076. return 0;
  1077. if (align > MOVE_MAX)
  1078. align = MOVE_MAX;
  1079. /* Decide whether to expand inline based on the optimization level. */
  1080. move_ratio = 4;
  1081. if (optimize > 2)
  1082. move_ratio = LARGEST_MOVE_RATIO;
  1083. num_pieces = (bytes / align) + (bytes % align); /* Close enough anyway. */
  1084. if (num_pieces > move_ratio)
  1085. return 0;
  1086. x = XEXP (dst_mem, 0);
  1087. if (!REG_P (x))
  1088. {
  1089. x = force_reg (Pmode, x);
  1090. dst_mem = replace_equiv_address (dst_mem, x);
  1091. }
  1092. x = XEXP (src_mem, 0);
  1093. if (!REG_P (x))
  1094. {
  1095. x = force_reg (Pmode, x);
  1096. src_mem = replace_equiv_address (src_mem, x);
  1097. }
  1098. active[0] = active[1] = false;
  1099. do
  1100. {
  1101. next = phase;
  1102. phase ^= 1;
  1103. if (bytes > 0)
  1104. {
  1105. int next_amount;
  1106. next_amount = (bytes >= 4 ? 4 : (bytes >= 2 ? 2 : 1));
  1107. next_amount = MIN (next_amount, align);
  1108. amount[next] = next_amount;
  1109. mode[next] = mode_from_align[next_amount];
  1110. temp[next] = gen_reg_rtx (mode[next]);
  1111. x = adjust_address (src_mem, mode[next], offset_ld);
  1112. emit_insn (gen_rtx_SET (VOIDmode, temp[next], x));
  1113. offset_ld += next_amount;
  1114. bytes -= next_amount;
  1115. active[next] = true;
  1116. }
  1117. if (active[phase])
  1118. {
  1119. active[phase] = false;
  1120. x = adjust_address (dst_mem, mode[phase], offset_st);
  1121. emit_insn (gen_rtx_SET (VOIDmode, x, temp[phase]));
  1122. offset_st += amount[phase];
  1123. }
  1124. }
  1125. while (active[next]);
  1126. return 1;
  1127. }
  1128. void
  1129. xtensa_expand_nonlocal_goto (rtx *operands)
  1130. {
  1131. rtx goto_handler = operands[1];
  1132. rtx containing_fp = operands[3];
  1133. /* Generate a call to "__xtensa_nonlocal_goto" (in libgcc); the code
  1134. is too big to generate in-line. */
  1135. if (GET_CODE (containing_fp) != REG)
  1136. containing_fp = force_reg (Pmode, containing_fp);
  1137. emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_nonlocal_goto"),
  1138. LCT_NORMAL, VOIDmode, 2,
  1139. containing_fp, Pmode,
  1140. goto_handler, Pmode);
  1141. }
  1142. static struct machine_function *
  1143. xtensa_init_machine_status (void)
  1144. {
  1145. return ggc_cleared_alloc<machine_function> ();
  1146. }
  1147. /* Shift VAL of mode MODE left by COUNT bits. */
  1148. static inline rtx
  1149. xtensa_expand_mask_and_shift (rtx val, machine_mode mode, rtx count)
  1150. {
  1151. val = expand_simple_binop (SImode, AND, val, GEN_INT (GET_MODE_MASK (mode)),
  1152. NULL_RTX, 1, OPTAB_DIRECT);
  1153. return expand_simple_binop (SImode, ASHIFT, val, count,
  1154. NULL_RTX, 1, OPTAB_DIRECT);
  1155. }
  1156. /* Structure to hold the initial parameters for a compare_and_swap operation
  1157. in HImode and QImode. */
  1158. struct alignment_context
  1159. {
  1160. rtx memsi; /* SI aligned memory location. */
  1161. rtx shift; /* Bit offset with regard to lsb. */
  1162. rtx modemask; /* Mask of the HQImode shifted by SHIFT bits. */
  1163. rtx modemaski; /* ~modemask */
  1164. };
  1165. /* Initialize structure AC for word access to HI and QI mode memory. */
  1166. static void
  1167. init_alignment_context (struct alignment_context *ac, rtx mem)
  1168. {
  1169. machine_mode mode = GET_MODE (mem);
  1170. rtx byteoffset = NULL_RTX;
  1171. bool aligned = (MEM_ALIGN (mem) >= GET_MODE_BITSIZE (SImode));
  1172. if (aligned)
  1173. ac->memsi = adjust_address (mem, SImode, 0); /* Memory is aligned. */
  1174. else
  1175. {
  1176. /* Alignment is unknown. */
  1177. rtx addr, align;
  1178. /* Force the address into a register. */
  1179. addr = force_reg (Pmode, XEXP (mem, 0));
  1180. /* Align it to SImode. */
  1181. align = expand_simple_binop (Pmode, AND, addr,
  1182. GEN_INT (-GET_MODE_SIZE (SImode)),
  1183. NULL_RTX, 1, OPTAB_DIRECT);
  1184. /* Generate MEM. */
  1185. ac->memsi = gen_rtx_MEM (SImode, align);
  1186. MEM_VOLATILE_P (ac->memsi) = MEM_VOLATILE_P (mem);
  1187. set_mem_alias_set (ac->memsi, ALIAS_SET_MEMORY_BARRIER);
  1188. set_mem_align (ac->memsi, GET_MODE_BITSIZE (SImode));
  1189. byteoffset = expand_simple_binop (Pmode, AND, addr,
  1190. GEN_INT (GET_MODE_SIZE (SImode) - 1),
  1191. NULL_RTX, 1, OPTAB_DIRECT);
  1192. }
  1193. /* Calculate shiftcount. */
  1194. if (TARGET_BIG_ENDIAN)
  1195. {
  1196. ac->shift = GEN_INT (GET_MODE_SIZE (SImode) - GET_MODE_SIZE (mode));
  1197. if (!aligned)
  1198. ac->shift = expand_simple_binop (SImode, MINUS, ac->shift, byteoffset,
  1199. NULL_RTX, 1, OPTAB_DIRECT);
  1200. }
  1201. else
  1202. {
  1203. if (aligned)
  1204. ac->shift = NULL_RTX;
  1205. else
  1206. ac->shift = byteoffset;
  1207. }
  1208. if (ac->shift != NULL_RTX)
  1209. {
  1210. /* Shift is the byte count, but we need the bitcount. */
  1211. ac->shift = expand_simple_binop (SImode, MULT, ac->shift,
  1212. GEN_INT (BITS_PER_UNIT),
  1213. NULL_RTX, 1, OPTAB_DIRECT);
  1214. ac->modemask = expand_simple_binop (SImode, ASHIFT,
  1215. GEN_INT (GET_MODE_MASK (mode)),
  1216. ac->shift,
  1217. NULL_RTX, 1, OPTAB_DIRECT);
  1218. }
  1219. else
  1220. ac->modemask = GEN_INT (GET_MODE_MASK (mode));
  1221. ac->modemaski = expand_simple_unop (SImode, NOT, ac->modemask, NULL_RTX, 1);
  1222. }
  1223. /* Expand an atomic compare and swap operation for HImode and QImode.
  1224. MEM is the memory location, CMP the old value to compare MEM with
  1225. and NEW_RTX the value to set if CMP == MEM. */
  1226. void
  1227. xtensa_expand_compare_and_swap (rtx target, rtx mem, rtx cmp, rtx new_rtx)
  1228. {
  1229. machine_mode mode = GET_MODE (mem);
  1230. struct alignment_context ac;
  1231. rtx tmp, cmpv, newv, val;
  1232. rtx oldval = gen_reg_rtx (SImode);
  1233. rtx res = gen_reg_rtx (SImode);
  1234. rtx_code_label *csloop = gen_label_rtx ();
  1235. rtx_code_label *csend = gen_label_rtx ();
  1236. init_alignment_context (&ac, mem);
  1237. if (ac.shift != NULL_RTX)
  1238. {
  1239. cmp = xtensa_expand_mask_and_shift (cmp, mode, ac.shift);
  1240. new_rtx = xtensa_expand_mask_and_shift (new_rtx, mode, ac.shift);
  1241. }
  1242. /* Load the surrounding word into VAL with the MEM value masked out. */
  1243. val = force_reg (SImode, expand_simple_binop (SImode, AND, ac.memsi,
  1244. ac.modemaski, NULL_RTX, 1,
  1245. OPTAB_DIRECT));
  1246. emit_label (csloop);
  1247. /* Patch CMP and NEW_RTX into VAL at correct position. */
  1248. cmpv = force_reg (SImode, expand_simple_binop (SImode, IOR, cmp, val,
  1249. NULL_RTX, 1, OPTAB_DIRECT));
  1250. newv = force_reg (SImode, expand_simple_binop (SImode, IOR, new_rtx, val,
  1251. NULL_RTX, 1, OPTAB_DIRECT));
  1252. /* Jump to end if we're done. */
  1253. emit_insn (gen_sync_compare_and_swapsi (res, ac.memsi, cmpv, newv));
  1254. emit_cmp_and_jump_insns (res, cmpv, EQ, const0_rtx, SImode, true, csend);
  1255. /* Check for changes outside mode. */
  1256. emit_move_insn (oldval, val);
  1257. tmp = expand_simple_binop (SImode, AND, res, ac.modemaski,
  1258. val, 1, OPTAB_DIRECT);
  1259. if (tmp != val)
  1260. emit_move_insn (val, tmp);
  1261. /* Loop internal if so. */
  1262. emit_cmp_and_jump_insns (oldval, val, NE, const0_rtx, SImode, true, csloop);
  1263. emit_label (csend);
  1264. /* Return the correct part of the bitfield. */
  1265. convert_move (target,
  1266. (ac.shift == NULL_RTX ? res
  1267. : expand_simple_binop (SImode, LSHIFTRT, res, ac.shift,
  1268. NULL_RTX, 1, OPTAB_DIRECT)),
  1269. 1);
  1270. }
  1271. /* Expand an atomic operation CODE of mode MODE (either HImode or QImode --
  1272. the default expansion works fine for SImode). MEM is the memory location
  1273. and VAL the value to play with. If AFTER is true then store the value
  1274. MEM holds after the operation, if AFTER is false then store the value MEM
  1275. holds before the operation. If TARGET is zero then discard that value, else
  1276. store it to TARGET. */
  1277. void
  1278. xtensa_expand_atomic (enum rtx_code code, rtx target, rtx mem, rtx val,
  1279. bool after)
  1280. {
  1281. machine_mode mode = GET_MODE (mem);
  1282. struct alignment_context ac;
  1283. rtx_code_label *csloop = gen_label_rtx ();
  1284. rtx cmp, tmp;
  1285. rtx old = gen_reg_rtx (SImode);
  1286. rtx new_rtx = gen_reg_rtx (SImode);
  1287. rtx orig = NULL_RTX;
  1288. init_alignment_context (&ac, mem);
  1289. /* Prepare values before the compare-and-swap loop. */
  1290. if (ac.shift != NULL_RTX)
  1291. val = xtensa_expand_mask_and_shift (val, mode, ac.shift);
  1292. switch (code)
  1293. {
  1294. case PLUS:
  1295. case MINUS:
  1296. orig = gen_reg_rtx (SImode);
  1297. convert_move (orig, val, 1);
  1298. break;
  1299. case SET:
  1300. case IOR:
  1301. case XOR:
  1302. break;
  1303. case MULT: /* NAND */
  1304. case AND:
  1305. /* val = "11..1<val>11..1" */
  1306. val = expand_simple_binop (SImode, XOR, val, ac.modemaski,
  1307. NULL_RTX, 1, OPTAB_DIRECT);
  1308. break;
  1309. default:
  1310. gcc_unreachable ();
  1311. }
  1312. /* Load full word. Subsequent loads are performed by S32C1I. */
  1313. cmp = force_reg (SImode, ac.memsi);
  1314. emit_label (csloop);
  1315. emit_move_insn (old, cmp);
  1316. switch (code)
  1317. {
  1318. case PLUS:
  1319. case MINUS:
  1320. val = expand_simple_binop (SImode, code, old, orig,
  1321. NULL_RTX, 1, OPTAB_DIRECT);
  1322. val = expand_simple_binop (SImode, AND, val, ac.modemask,
  1323. NULL_RTX, 1, OPTAB_DIRECT);
  1324. /* FALLTHRU */
  1325. case SET:
  1326. tmp = expand_simple_binop (SImode, AND, old, ac.modemaski,
  1327. NULL_RTX, 1, OPTAB_DIRECT);
  1328. tmp = expand_simple_binop (SImode, IOR, tmp, val,
  1329. new_rtx, 1, OPTAB_DIRECT);
  1330. break;
  1331. case AND:
  1332. case IOR:
  1333. case XOR:
  1334. tmp = expand_simple_binop (SImode, code, old, val,
  1335. new_rtx, 1, OPTAB_DIRECT);
  1336. break;
  1337. case MULT: /* NAND */
  1338. tmp = expand_simple_binop (SImode, XOR, old, ac.modemask,
  1339. NULL_RTX, 1, OPTAB_DIRECT);
  1340. tmp = expand_simple_binop (SImode, AND, tmp, val,
  1341. new_rtx, 1, OPTAB_DIRECT);
  1342. break;
  1343. default:
  1344. gcc_unreachable ();
  1345. }
  1346. if (tmp != new_rtx)
  1347. emit_move_insn (new_rtx, tmp);
  1348. emit_insn (gen_sync_compare_and_swapsi (cmp, ac.memsi, old, new_rtx));
  1349. emit_cmp_and_jump_insns (cmp, old, NE, const0_rtx, SImode, true, csloop);
  1350. if (target)
  1351. {
  1352. tmp = (after ? new_rtx : cmp);
  1353. convert_move (target,
  1354. (ac.shift == NULL_RTX ? tmp
  1355. : expand_simple_binop (SImode, LSHIFTRT, tmp, ac.shift,
  1356. NULL_RTX, 1, OPTAB_DIRECT)),
  1357. 1);
  1358. }
  1359. }
  1360. void
  1361. xtensa_setup_frame_addresses (void)
  1362. {
  1363. /* Set flag to cause TARGET_FRAME_POINTER_REQUIRED to return true. */
  1364. cfun->machine->accesses_prev_frame = 1;
  1365. if (TARGET_WINDOWED_ABI)
  1366. emit_library_call
  1367. (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_libgcc_window_spill"),
  1368. LCT_NORMAL, VOIDmode, 0);
  1369. }
  1370. /* Emit the assembly for the end of a zero-cost loop. Normally we just emit
  1371. a comment showing where the end of the loop is. However, if there is a
  1372. label or a branch at the end of the loop then we need to place a nop
  1373. there. If the loop ends with a label we need the nop so that branches
  1374. targeting that label will target the nop (and thus remain in the loop),
  1375. instead of targeting the instruction after the loop (and thus exiting
  1376. the loop). If the loop ends with a branch, we need the nop in case the
  1377. branch is targeting a location inside the loop. When the branch
  1378. executes it will cause the loop count to be decremented even if it is
  1379. taken (because it is the last instruction in the loop), so we need to
  1380. nop after the branch to prevent the loop count from being decremented
  1381. when the branch is taken. */
  1382. void
  1383. xtensa_emit_loop_end (rtx_insn *insn, rtx *operands)
  1384. {
  1385. char done = 0;
  1386. for (insn = PREV_INSN (insn); insn && !done; insn = PREV_INSN (insn))
  1387. {
  1388. switch (GET_CODE (insn))
  1389. {
  1390. case NOTE:
  1391. case BARRIER:
  1392. break;
  1393. case CODE_LABEL:
  1394. output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
  1395. done = 1;
  1396. break;
  1397. default:
  1398. {
  1399. rtx body = PATTERN (insn);
  1400. if (JUMP_P (body))
  1401. {
  1402. output_asm_insn (TARGET_DENSITY ? "nop.n" : "nop", operands);
  1403. done = 1;
  1404. }
  1405. else if ((GET_CODE (body) != USE)
  1406. && (GET_CODE (body) != CLOBBER))
  1407. done = 1;
  1408. }
  1409. break;
  1410. }
  1411. }
  1412. output_asm_insn ("%1_LEND:", operands);
  1413. }
  1414. char *
  1415. xtensa_emit_branch (bool inverted, bool immed, rtx *operands)
  1416. {
  1417. static char result[64];
  1418. enum rtx_code code;
  1419. const char *op;
  1420. code = GET_CODE (operands[3]);
  1421. switch (code)
  1422. {
  1423. case EQ: op = inverted ? "ne" : "eq"; break;
  1424. case NE: op = inverted ? "eq" : "ne"; break;
  1425. case LT: op = inverted ? "ge" : "lt"; break;
  1426. case GE: op = inverted ? "lt" : "ge"; break;
  1427. case LTU: op = inverted ? "geu" : "ltu"; break;
  1428. case GEU: op = inverted ? "ltu" : "geu"; break;
  1429. default: gcc_unreachable ();
  1430. }
  1431. if (immed)
  1432. {
  1433. if (INTVAL (operands[1]) == 0)
  1434. sprintf (result, "b%sz%s\t%%0, %%2", op,
  1435. (TARGET_DENSITY && (code == EQ || code == NE)) ? ".n" : "");
  1436. else
  1437. sprintf (result, "b%si\t%%0, %%d1, %%2", op);
  1438. }
  1439. else
  1440. sprintf (result, "b%s\t%%0, %%1, %%2", op);
  1441. return result;
  1442. }
  1443. char *
  1444. xtensa_emit_bit_branch (bool inverted, bool immed, rtx *operands)
  1445. {
  1446. static char result[64];
  1447. const char *op;
  1448. switch (GET_CODE (operands[3]))
  1449. {
  1450. case EQ: op = inverted ? "bs" : "bc"; break;
  1451. case NE: op = inverted ? "bc" : "bs"; break;
  1452. default: gcc_unreachable ();
  1453. }
  1454. if (immed)
  1455. {
  1456. unsigned bitnum = INTVAL (operands[1]) & 0x1f;
  1457. operands[1] = GEN_INT (bitnum);
  1458. sprintf (result, "b%si\t%%0, %%d1, %%2", op);
  1459. }
  1460. else
  1461. sprintf (result, "b%s\t%%0, %%1, %%2", op);
  1462. return result;
  1463. }
  1464. char *
  1465. xtensa_emit_movcc (bool inverted, bool isfp, bool isbool, rtx *operands)
  1466. {
  1467. static char result[64];
  1468. enum rtx_code code;
  1469. const char *op;
  1470. code = GET_CODE (operands[4]);
  1471. if (isbool)
  1472. {
  1473. switch (code)
  1474. {
  1475. case EQ: op = inverted ? "t" : "f"; break;
  1476. case NE: op = inverted ? "f" : "t"; break;
  1477. default: gcc_unreachable ();
  1478. }
  1479. }
  1480. else
  1481. {
  1482. switch (code)
  1483. {
  1484. case EQ: op = inverted ? "nez" : "eqz"; break;
  1485. case NE: op = inverted ? "eqz" : "nez"; break;
  1486. case LT: op = inverted ? "gez" : "ltz"; break;
  1487. case GE: op = inverted ? "ltz" : "gez"; break;
  1488. default: gcc_unreachable ();
  1489. }
  1490. }
  1491. sprintf (result, "mov%s%s\t%%0, %%%d, %%1",
  1492. op, isfp ? ".s" : "", inverted ? 3 : 2);
  1493. return result;
  1494. }
  1495. char *
  1496. xtensa_emit_call (int callop, rtx *operands)
  1497. {
  1498. static char result[64];
  1499. rtx tgt = operands[callop];
  1500. if (GET_CODE (tgt) == CONST_INT)
  1501. sprintf (result, "call%d\t0x%lx", WINDOW_SIZE, INTVAL (tgt));
  1502. else if (register_operand (tgt, VOIDmode))
  1503. sprintf (result, "callx%d\t%%%d", WINDOW_SIZE, callop);
  1504. else
  1505. sprintf (result, "call%d\t%%%d", WINDOW_SIZE, callop);
  1506. return result;
  1507. }
  1508. bool
  1509. xtensa_legitimate_address_p (machine_mode mode, rtx addr, bool strict)
  1510. {
  1511. /* Allow constant pool addresses. */
  1512. if (mode != BLKmode && GET_MODE_SIZE (mode) >= UNITS_PER_WORD
  1513. && ! TARGET_CONST16 && constantpool_address_p (addr)
  1514. && ! xtensa_tls_referenced_p (addr))
  1515. return true;
  1516. while (GET_CODE (addr) == SUBREG)
  1517. addr = SUBREG_REG (addr);
  1518. /* Allow base registers. */
  1519. if (GET_CODE (addr) == REG && BASE_REG_P (addr, strict))
  1520. return true;
  1521. /* Check for "register + offset" addressing. */
  1522. if (GET_CODE (addr) == PLUS)
  1523. {
  1524. rtx xplus0 = XEXP (addr, 0);
  1525. rtx xplus1 = XEXP (addr, 1);
  1526. enum rtx_code code0;
  1527. enum rtx_code code1;
  1528. while (GET_CODE (xplus0) == SUBREG)
  1529. xplus0 = SUBREG_REG (xplus0);
  1530. code0 = GET_CODE (xplus0);
  1531. while (GET_CODE (xplus1) == SUBREG)
  1532. xplus1 = SUBREG_REG (xplus1);
  1533. code1 = GET_CODE (xplus1);
  1534. /* Swap operands if necessary so the register is first. */
  1535. if (code0 != REG && code1 == REG)
  1536. {
  1537. xplus0 = XEXP (addr, 1);
  1538. xplus1 = XEXP (addr, 0);
  1539. code0 = GET_CODE (xplus0);
  1540. code1 = GET_CODE (xplus1);
  1541. }
  1542. if (code0 == REG && BASE_REG_P (xplus0, strict)
  1543. && code1 == CONST_INT
  1544. && xtensa_mem_offset (INTVAL (xplus1), mode))
  1545. return true;
  1546. }
  1547. return false;
  1548. }
  1549. /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol. */
  1550. static GTY(()) rtx xtensa_tls_module_base_symbol;
  1551. static rtx
  1552. xtensa_tls_module_base (void)
  1553. {
  1554. if (! xtensa_tls_module_base_symbol)
  1555. {
  1556. xtensa_tls_module_base_symbol =
  1557. gen_rtx_SYMBOL_REF (Pmode, "_TLS_MODULE_BASE_");
  1558. SYMBOL_REF_FLAGS (xtensa_tls_module_base_symbol)
  1559. |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
  1560. }
  1561. return xtensa_tls_module_base_symbol;
  1562. }
  1563. static rtx_insn *
  1564. xtensa_call_tls_desc (rtx sym, rtx *retp)
  1565. {
  1566. rtx fn, arg, a10;
  1567. rtx_insn *call_insn, *insns;
  1568. start_sequence ();
  1569. fn = gen_reg_rtx (Pmode);
  1570. arg = gen_reg_rtx (Pmode);
  1571. a10 = gen_rtx_REG (Pmode, 10);
  1572. emit_insn (gen_tls_func (fn, sym));
  1573. emit_insn (gen_tls_arg (arg, sym));
  1574. emit_move_insn (a10, arg);
  1575. call_insn = emit_call_insn (gen_tls_call (a10, fn, sym, const1_rtx));
  1576. use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn), a10);
  1577. insns = get_insns ();
  1578. end_sequence ();
  1579. *retp = a10;
  1580. return insns;
  1581. }
  1582. static rtx
  1583. xtensa_legitimize_tls_address (rtx x)
  1584. {
  1585. unsigned int model = SYMBOL_REF_TLS_MODEL (x);
  1586. rtx dest, tp, ret, modbase, base, addend;
  1587. rtx_insn *insns;
  1588. dest = gen_reg_rtx (Pmode);
  1589. switch (model)
  1590. {
  1591. case TLS_MODEL_GLOBAL_DYNAMIC:
  1592. insns = xtensa_call_tls_desc (x, &ret);
  1593. emit_libcall_block (insns, dest, ret, x);
  1594. break;
  1595. case TLS_MODEL_LOCAL_DYNAMIC:
  1596. base = gen_reg_rtx (Pmode);
  1597. modbase = xtensa_tls_module_base ();
  1598. insns = xtensa_call_tls_desc (modbase, &ret);
  1599. emit_libcall_block (insns, base, ret, modbase);
  1600. addend = force_reg (SImode, gen_sym_DTPOFF (x));
  1601. emit_insn (gen_addsi3 (dest, base, addend));
  1602. break;
  1603. case TLS_MODEL_INITIAL_EXEC:
  1604. case TLS_MODEL_LOCAL_EXEC:
  1605. tp = gen_reg_rtx (SImode);
  1606. emit_insn (gen_get_thread_pointersi (tp));
  1607. addend = force_reg (SImode, gen_sym_TPOFF (x));
  1608. emit_insn (gen_addsi3 (dest, tp, addend));
  1609. break;
  1610. default:
  1611. gcc_unreachable ();
  1612. }
  1613. return dest;
  1614. }
  1615. rtx
  1616. xtensa_legitimize_address (rtx x,
  1617. rtx oldx ATTRIBUTE_UNUSED,
  1618. machine_mode mode)
  1619. {
  1620. if (xtensa_tls_symbol_p (x))
  1621. return xtensa_legitimize_tls_address (x);
  1622. if (GET_CODE (x) == PLUS)
  1623. {
  1624. rtx plus0 = XEXP (x, 0);
  1625. rtx plus1 = XEXP (x, 1);
  1626. if (GET_CODE (plus0) != REG && GET_CODE (plus1) == REG)
  1627. {
  1628. plus0 = XEXP (x, 1);
  1629. plus1 = XEXP (x, 0);
  1630. }
  1631. /* Try to split up the offset to use an ADDMI instruction. */
  1632. if (GET_CODE (plus0) == REG
  1633. && GET_CODE (plus1) == CONST_INT
  1634. && !xtensa_mem_offset (INTVAL (plus1), mode)
  1635. && !xtensa_simm8 (INTVAL (plus1))
  1636. && xtensa_mem_offset (INTVAL (plus1) & 0xff, mode)
  1637. && xtensa_simm8x256 (INTVAL (plus1) & ~0xff))
  1638. {
  1639. rtx temp = gen_reg_rtx (Pmode);
  1640. rtx addmi_offset = GEN_INT (INTVAL (plus1) & ~0xff);
  1641. emit_insn (gen_rtx_SET (Pmode, temp,
  1642. gen_rtx_PLUS (Pmode, plus0, addmi_offset)));
  1643. return gen_rtx_PLUS (Pmode, temp, GEN_INT (INTVAL (plus1) & 0xff));
  1644. }
  1645. }
  1646. return x;
  1647. }
  1648. /* Worker function for TARGET_MODE_DEPENDENT_ADDRESS_P.
  1649. Treat constant-pool references as "mode dependent" since they can
  1650. only be accessed with SImode loads. This works around a bug in the
  1651. combiner where a constant pool reference is temporarily converted
  1652. to an HImode load, which is then assumed to zero-extend based on
  1653. our definition of LOAD_EXTEND_OP. This is wrong because the high
  1654. bits of a 16-bit value in the constant pool are now sign-extended
  1655. by default. */
  1656. static bool
  1657. xtensa_mode_dependent_address_p (const_rtx addr,
  1658. addr_space_t as ATTRIBUTE_UNUSED)
  1659. {
  1660. return constantpool_address_p (addr);
  1661. }
  1662. /* Return TRUE if X contains any TLS symbol references. */
  1663. bool
  1664. xtensa_tls_referenced_p (rtx x)
  1665. {
  1666. if (! TARGET_HAVE_TLS)
  1667. return false;
  1668. subrtx_iterator::array_type array;
  1669. FOR_EACH_SUBRTX (iter, array, x, ALL)
  1670. {
  1671. const_rtx x = *iter;
  1672. if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0)
  1673. return true;
  1674. /* Ignore TLS references that have already been legitimized. */
  1675. if (GET_CODE (x) == UNSPEC)
  1676. switch (XINT (x, 1))
  1677. {
  1678. case UNSPEC_TPOFF:
  1679. case UNSPEC_DTPOFF:
  1680. case UNSPEC_TLS_FUNC:
  1681. case UNSPEC_TLS_ARG:
  1682. case UNSPEC_TLS_CALL:
  1683. iter.skip_subrtxes ();
  1684. break;
  1685. default:
  1686. break;
  1687. }
  1688. }
  1689. return false;
  1690. }
  1691. /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
  1692. static bool
  1693. xtensa_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
  1694. {
  1695. return xtensa_tls_referenced_p (x);
  1696. }
  1697. /* Return the debugger register number to use for 'regno'. */
  1698. int
  1699. xtensa_dbx_register_number (int regno)
  1700. {
  1701. int first = -1;
  1702. if (GP_REG_P (regno))
  1703. {
  1704. regno -= GP_REG_FIRST;
  1705. first = 0;
  1706. }
  1707. else if (BR_REG_P (regno))
  1708. {
  1709. regno -= BR_REG_FIRST;
  1710. first = 16;
  1711. }
  1712. else if (FP_REG_P (regno))
  1713. {
  1714. regno -= FP_REG_FIRST;
  1715. first = 48;
  1716. }
  1717. else if (ACC_REG_P (regno))
  1718. {
  1719. first = 0x200; /* Start of Xtensa special registers. */
  1720. regno = 16; /* ACCLO is special register 16. */
  1721. }
  1722. /* When optimizing, we sometimes get asked about pseudo-registers
  1723. that don't represent hard registers. Return 0 for these. */
  1724. if (first == -1)
  1725. return 0;
  1726. return first + regno;
  1727. }
  1728. /* Argument support functions. */
  1729. /* Initialize CUMULATIVE_ARGS for a function. */
  1730. void
  1731. init_cumulative_args (CUMULATIVE_ARGS *cum, int incoming)
  1732. {
  1733. cum->arg_words = 0;
  1734. cum->incoming = incoming;
  1735. }
  1736. /* Advance the argument to the next argument position. */
  1737. static void
  1738. xtensa_function_arg_advance (cumulative_args_t cum, machine_mode mode,
  1739. const_tree type, bool named ATTRIBUTE_UNUSED)
  1740. {
  1741. int words, max;
  1742. int *arg_words;
  1743. arg_words = &get_cumulative_args (cum)->arg_words;
  1744. max = MAX_ARGS_IN_REGISTERS;
  1745. words = (((mode != BLKmode)
  1746. ? (int) GET_MODE_SIZE (mode)
  1747. : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
  1748. if (*arg_words < max
  1749. && (targetm.calls.must_pass_in_stack (mode, type)
  1750. || *arg_words + words > max))
  1751. *arg_words = max;
  1752. *arg_words += words;
  1753. }
  1754. /* Return an RTL expression containing the register for the given mode,
  1755. or 0 if the argument is to be passed on the stack. INCOMING_P is nonzero
  1756. if this is an incoming argument to the current function. */
  1757. static rtx
  1758. xtensa_function_arg_1 (cumulative_args_t cum_v, machine_mode mode,
  1759. const_tree type, bool incoming_p)
  1760. {
  1761. CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
  1762. int regbase, words, max;
  1763. int *arg_words;
  1764. int regno;
  1765. arg_words = &cum->arg_words;
  1766. regbase = (incoming_p ? GP_ARG_FIRST : GP_OUTGOING_ARG_FIRST);
  1767. max = MAX_ARGS_IN_REGISTERS;
  1768. words = (((mode != BLKmode)
  1769. ? (int) GET_MODE_SIZE (mode)
  1770. : int_size_in_bytes (type)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
  1771. if (type && (TYPE_ALIGN (type) > BITS_PER_WORD))
  1772. {
  1773. int align = MIN (TYPE_ALIGN (type), STACK_BOUNDARY) / BITS_PER_WORD;
  1774. *arg_words = (*arg_words + align - 1) & -align;
  1775. }
  1776. if (*arg_words + words > max)
  1777. return (rtx)0;
  1778. regno = regbase + *arg_words;
  1779. if (cum->incoming && regno <= A7_REG && regno + words > A7_REG)
  1780. cfun->machine->need_a7_copy = TARGET_WINDOWED_ABI;
  1781. return gen_rtx_REG (mode, regno);
  1782. }
  1783. /* Implement TARGET_FUNCTION_ARG. */
  1784. static rtx
  1785. xtensa_function_arg (cumulative_args_t cum, machine_mode mode,
  1786. const_tree type, bool named ATTRIBUTE_UNUSED)
  1787. {
  1788. return xtensa_function_arg_1 (cum, mode, type, false);
  1789. }
  1790. /* Implement TARGET_FUNCTION_INCOMING_ARG. */
  1791. static rtx
  1792. xtensa_function_incoming_arg (cumulative_args_t cum, machine_mode mode,
  1793. const_tree type, bool named ATTRIBUTE_UNUSED)
  1794. {
  1795. return xtensa_function_arg_1 (cum, mode, type, true);
  1796. }
  1797. static unsigned int
  1798. xtensa_function_arg_boundary (machine_mode mode, const_tree type)
  1799. {
  1800. unsigned int alignment;
  1801. alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
  1802. if (alignment < PARM_BOUNDARY)
  1803. alignment = PARM_BOUNDARY;
  1804. if (alignment > STACK_BOUNDARY)
  1805. alignment = STACK_BOUNDARY;
  1806. return alignment;
  1807. }
  1808. static bool
  1809. xtensa_return_in_msb (const_tree valtype)
  1810. {
  1811. return (TARGET_BIG_ENDIAN
  1812. && AGGREGATE_TYPE_P (valtype)
  1813. && int_size_in_bytes (valtype) >= UNITS_PER_WORD);
  1814. }
  1815. static void
  1816. xtensa_option_override (void)
  1817. {
  1818. int regno;
  1819. machine_mode mode;
  1820. if (!TARGET_BOOLEANS && TARGET_HARD_FLOAT)
  1821. error ("boolean registers required for the floating-point option");
  1822. /* Set up array giving whether a given register can hold a given mode. */
  1823. for (mode = VOIDmode;
  1824. mode != MAX_MACHINE_MODE;
  1825. mode = (machine_mode) ((int) mode + 1))
  1826. {
  1827. int size = GET_MODE_SIZE (mode);
  1828. enum mode_class mclass = GET_MODE_CLASS (mode);
  1829. for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
  1830. {
  1831. int temp;
  1832. if (ACC_REG_P (regno))
  1833. temp = (TARGET_MAC16
  1834. && (mclass == MODE_INT) && (size <= UNITS_PER_WORD));
  1835. else if (GP_REG_P (regno))
  1836. temp = ((regno & 1) == 0 || (size <= UNITS_PER_WORD));
  1837. else if (FP_REG_P (regno))
  1838. temp = (TARGET_HARD_FLOAT && (mode == SFmode));
  1839. else if (BR_REG_P (regno))
  1840. temp = (TARGET_BOOLEANS && (mode == CCmode));
  1841. else
  1842. temp = FALSE;
  1843. xtensa_hard_regno_mode_ok[(int) mode][regno] = temp;
  1844. }
  1845. }
  1846. init_machine_status = xtensa_init_machine_status;
  1847. /* Check PIC settings. PIC is only supported when using L32R
  1848. instructions, and some targets need to always use PIC. */
  1849. if (flag_pic && TARGET_CONST16)
  1850. error ("-f%s is not supported with CONST16 instructions",
  1851. (flag_pic > 1 ? "PIC" : "pic"));
  1852. else if (TARGET_FORCE_NO_PIC)
  1853. flag_pic = 0;
  1854. else if (XTENSA_ALWAYS_PIC)
  1855. {
  1856. if (TARGET_CONST16)
  1857. error ("PIC is required but not supported with CONST16 instructions");
  1858. flag_pic = 1;
  1859. }
  1860. /* There's no need for -fPIC (as opposed to -fpic) on Xtensa. */
  1861. if (flag_pic > 1)
  1862. flag_pic = 1;
  1863. if (flag_pic && !flag_pie)
  1864. flag_shlib = 1;
  1865. /* Hot/cold partitioning does not work on this architecture, because of
  1866. constant pools (the load instruction cannot necessarily reach that far).
  1867. Therefore disable it on this architecture. */
  1868. if (flag_reorder_blocks_and_partition)
  1869. {
  1870. flag_reorder_blocks_and_partition = 0;
  1871. flag_reorder_blocks = 1;
  1872. }
  1873. }
  1874. /* A C compound statement to output to stdio stream STREAM the
  1875. assembler syntax for an instruction operand X. X is an RTL
  1876. expression.
  1877. CODE is a value that can be used to specify one of several ways
  1878. of printing the operand. It is used when identical operands
  1879. must be printed differently depending on the context. CODE
  1880. comes from the '%' specification that was used to request
  1881. printing of the operand. If the specification was just '%DIGIT'
  1882. then CODE is 0; if the specification was '%LTR DIGIT' then CODE
  1883. is the ASCII code for LTR.
  1884. If X is a register, this macro should print the register's name.
  1885. The names can be found in an array 'reg_names' whose type is
  1886. 'char *[]'. 'reg_names' is initialized from 'REGISTER_NAMES'.
  1887. When the machine description has a specification '%PUNCT' (a '%'
  1888. followed by a punctuation character), this macro is called with
  1889. a null pointer for X and the punctuation character for CODE.
  1890. 'a', 'c', 'l', and 'n' are reserved.
  1891. The Xtensa specific codes are:
  1892. 'd' CONST_INT, print as signed decimal
  1893. 'x' CONST_INT, print as signed hexadecimal
  1894. 'K' CONST_INT, print number of bits in mask for EXTUI
  1895. 'R' CONST_INT, print (X & 0x1f)
  1896. 'L' CONST_INT, print ((32 - X) & 0x1f)
  1897. 'D' REG, print second register of double-word register operand
  1898. 'N' MEM, print address of next word following a memory operand
  1899. 'v' MEM, if memory reference is volatile, output a MEMW before it
  1900. 't' any constant, add "@h" suffix for top 16 bits
  1901. 'b' any constant, add "@l" suffix for bottom 16 bits
  1902. */
  1903. static void
  1904. printx (FILE *file, signed int val)
  1905. {
  1906. /* Print a hexadecimal value in a nice way. */
  1907. if ((val > -0xa) && (val < 0xa))
  1908. fprintf (file, "%d", val);
  1909. else if (val < 0)
  1910. fprintf (file, "-0x%x", -val);
  1911. else
  1912. fprintf (file, "0x%x", val);
  1913. }
  1914. void
  1915. print_operand (FILE *file, rtx x, int letter)
  1916. {
  1917. if (!x)
  1918. error ("PRINT_OPERAND null pointer");
  1919. switch (letter)
  1920. {
  1921. case 'D':
  1922. if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
  1923. fprintf (file, "%s", reg_names[xt_true_regnum (x) + 1]);
  1924. else
  1925. output_operand_lossage ("invalid %%D value");
  1926. break;
  1927. case 'v':
  1928. if (GET_CODE (x) == MEM)
  1929. {
  1930. /* For a volatile memory reference, emit a MEMW before the
  1931. load or store. */
  1932. if (MEM_VOLATILE_P (x) && TARGET_SERIALIZE_VOLATILE)
  1933. fprintf (file, "memw\n\t");
  1934. }
  1935. else
  1936. output_operand_lossage ("invalid %%v value");
  1937. break;
  1938. case 'N':
  1939. if (GET_CODE (x) == MEM
  1940. && (GET_MODE (x) == DFmode || GET_MODE (x) == DImode))
  1941. {
  1942. x = adjust_address (x, GET_MODE (x) == DFmode ? SFmode : SImode, 4);
  1943. output_address (XEXP (x, 0));
  1944. }
  1945. else
  1946. output_operand_lossage ("invalid %%N value");
  1947. break;
  1948. case 'K':
  1949. if (GET_CODE (x) == CONST_INT)
  1950. {
  1951. int num_bits = 0;
  1952. unsigned val = INTVAL (x);
  1953. while (val & 1)
  1954. {
  1955. num_bits += 1;
  1956. val = val >> 1;
  1957. }
  1958. if ((val != 0) || (num_bits == 0) || (num_bits > 16))
  1959. fatal_insn ("invalid mask", x);
  1960. fprintf (file, "%d", num_bits);
  1961. }
  1962. else
  1963. output_operand_lossage ("invalid %%K value");
  1964. break;
  1965. case 'L':
  1966. if (GET_CODE (x) == CONST_INT)
  1967. fprintf (file, "%ld", (32 - INTVAL (x)) & 0x1f);
  1968. else
  1969. output_operand_lossage ("invalid %%L value");
  1970. break;
  1971. case 'R':
  1972. if (GET_CODE (x) == CONST_INT)
  1973. fprintf (file, "%ld", INTVAL (x) & 0x1f);
  1974. else
  1975. output_operand_lossage ("invalid %%R value");
  1976. break;
  1977. case 'x':
  1978. if (GET_CODE (x) == CONST_INT)
  1979. printx (file, INTVAL (x));
  1980. else
  1981. output_operand_lossage ("invalid %%x value");
  1982. break;
  1983. case 'd':
  1984. if (GET_CODE (x) == CONST_INT)
  1985. fprintf (file, "%ld", INTVAL (x));
  1986. else
  1987. output_operand_lossage ("invalid %%d value");
  1988. break;
  1989. case 't':
  1990. case 'b':
  1991. if (GET_CODE (x) == CONST_INT)
  1992. {
  1993. printx (file, INTVAL (x));
  1994. fputs (letter == 't' ? "@h" : "@l", file);
  1995. }
  1996. else if (GET_CODE (x) == CONST_DOUBLE)
  1997. {
  1998. REAL_VALUE_TYPE r;
  1999. REAL_VALUE_FROM_CONST_DOUBLE (r, x);
  2000. if (GET_MODE (x) == SFmode)
  2001. {
  2002. long l;
  2003. REAL_VALUE_TO_TARGET_SINGLE (r, l);
  2004. fprintf (file, "0x%08lx@%c", l, letter == 't' ? 'h' : 'l');
  2005. }
  2006. else
  2007. output_operand_lossage ("invalid %%t/%%b value");
  2008. }
  2009. else if (GET_CODE (x) == CONST)
  2010. {
  2011. /* X must be a symbolic constant on ELF. Write an expression
  2012. suitable for 'const16' that sets the high or low 16 bits. */
  2013. if (GET_CODE (XEXP (x, 0)) != PLUS
  2014. || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
  2015. && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
  2016. || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
  2017. output_operand_lossage ("invalid %%t/%%b value");
  2018. print_operand (file, XEXP (XEXP (x, 0), 0), 0);
  2019. fputs (letter == 't' ? "@h" : "@l", file);
  2020. /* There must be a non-alphanumeric character between 'h' or 'l'
  2021. and the number. The '-' is added by print_operand() already. */
  2022. if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
  2023. fputs ("+", file);
  2024. print_operand (file, XEXP (XEXP (x, 0), 1), 0);
  2025. }
  2026. else
  2027. {
  2028. output_addr_const (file, x);
  2029. fputs (letter == 't' ? "@h" : "@l", file);
  2030. }
  2031. break;
  2032. default:
  2033. if (GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
  2034. fprintf (file, "%s", reg_names[xt_true_regnum (x)]);
  2035. else if (GET_CODE (x) == MEM)
  2036. output_address (XEXP (x, 0));
  2037. else if (GET_CODE (x) == CONST_INT)
  2038. fprintf (file, "%ld", INTVAL (x));
  2039. else
  2040. output_addr_const (file, x);
  2041. }
  2042. }
  2043. /* A C compound statement to output to stdio stream STREAM the
  2044. assembler syntax for an instruction operand that is a memory
  2045. reference whose address is ADDR. ADDR is an RTL expression. */
  2046. void
  2047. print_operand_address (FILE *file, rtx addr)
  2048. {
  2049. if (!addr)
  2050. error ("PRINT_OPERAND_ADDRESS, null pointer");
  2051. switch (GET_CODE (addr))
  2052. {
  2053. default:
  2054. fatal_insn ("invalid address", addr);
  2055. break;
  2056. case REG:
  2057. fprintf (file, "%s, 0", reg_names [REGNO (addr)]);
  2058. break;
  2059. case PLUS:
  2060. {
  2061. rtx reg = (rtx)0;
  2062. rtx offset = (rtx)0;
  2063. rtx arg0 = XEXP (addr, 0);
  2064. rtx arg1 = XEXP (addr, 1);
  2065. if (GET_CODE (arg0) == REG)
  2066. {
  2067. reg = arg0;
  2068. offset = arg1;
  2069. }
  2070. else if (GET_CODE (arg1) == REG)
  2071. {
  2072. reg = arg1;
  2073. offset = arg0;
  2074. }
  2075. else
  2076. fatal_insn ("no register in address", addr);
  2077. if (CONSTANT_P (offset))
  2078. {
  2079. fprintf (file, "%s, ", reg_names [REGNO (reg)]);
  2080. output_addr_const (file, offset);
  2081. }
  2082. else
  2083. fatal_insn ("address offset not a constant", addr);
  2084. }
  2085. break;
  2086. case LABEL_REF:
  2087. case SYMBOL_REF:
  2088. case CONST_INT:
  2089. case CONST:
  2090. output_addr_const (file, addr);
  2091. break;
  2092. }
  2093. }
  2094. /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA. */
  2095. static bool
  2096. xtensa_output_addr_const_extra (FILE *fp, rtx x)
  2097. {
  2098. if (GET_CODE (x) == UNSPEC && XVECLEN (x, 0) == 1)
  2099. {
  2100. switch (XINT (x, 1))
  2101. {
  2102. case UNSPEC_TPOFF:
  2103. output_addr_const (fp, XVECEXP (x, 0, 0));
  2104. fputs ("@TPOFF", fp);
  2105. return true;
  2106. case UNSPEC_DTPOFF:
  2107. output_addr_const (fp, XVECEXP (x, 0, 0));
  2108. fputs ("@DTPOFF", fp);
  2109. return true;
  2110. case UNSPEC_PLT:
  2111. if (flag_pic)
  2112. {
  2113. output_addr_const (fp, XVECEXP (x, 0, 0));
  2114. fputs ("@PLT", fp);
  2115. return true;
  2116. }
  2117. break;
  2118. default:
  2119. break;
  2120. }
  2121. }
  2122. return false;
  2123. }
  2124. void
  2125. xtensa_output_literal (FILE *file, rtx x, machine_mode mode, int labelno)
  2126. {
  2127. long value_long[2];
  2128. REAL_VALUE_TYPE r;
  2129. int size;
  2130. rtx first, second;
  2131. fprintf (file, "\t.literal .LC%u, ", (unsigned) labelno);
  2132. switch (GET_MODE_CLASS (mode))
  2133. {
  2134. case MODE_FLOAT:
  2135. gcc_assert (GET_CODE (x) == CONST_DOUBLE);
  2136. REAL_VALUE_FROM_CONST_DOUBLE (r, x);
  2137. switch (mode)
  2138. {
  2139. case SFmode:
  2140. REAL_VALUE_TO_TARGET_SINGLE (r, value_long[0]);
  2141. if (HOST_BITS_PER_LONG > 32)
  2142. value_long[0] &= 0xffffffff;
  2143. fprintf (file, "0x%08lx\n", value_long[0]);
  2144. break;
  2145. case DFmode:
  2146. REAL_VALUE_TO_TARGET_DOUBLE (r, value_long);
  2147. if (HOST_BITS_PER_LONG > 32)
  2148. {
  2149. value_long[0] &= 0xffffffff;
  2150. value_long[1] &= 0xffffffff;
  2151. }
  2152. fprintf (file, "0x%08lx, 0x%08lx\n",
  2153. value_long[0], value_long[1]);
  2154. break;
  2155. default:
  2156. gcc_unreachable ();
  2157. }
  2158. break;
  2159. case MODE_INT:
  2160. case MODE_PARTIAL_INT:
  2161. size = GET_MODE_SIZE (mode);
  2162. switch (size)
  2163. {
  2164. case 4:
  2165. output_addr_const (file, x);
  2166. fputs ("\n", file);
  2167. break;
  2168. case 8:
  2169. split_double (x, &first, &second);
  2170. output_addr_const (file, first);
  2171. fputs (", ", file);
  2172. output_addr_const (file, second);
  2173. fputs ("\n", file);
  2174. break;
  2175. default:
  2176. gcc_unreachable ();
  2177. }
  2178. break;
  2179. default:
  2180. gcc_unreachable ();
  2181. }
  2182. }
  2183. static bool
  2184. xtensa_call_save_reg(int regno)
  2185. {
  2186. if (TARGET_WINDOWED_ABI)
  2187. return false;
  2188. if (regno == A0_REG)
  2189. return crtl->profile || !crtl->is_leaf || crtl->calls_eh_return ||
  2190. df_regs_ever_live_p (regno);
  2191. if (crtl->calls_eh_return && regno >= 2 && regno < 4)
  2192. return true;
  2193. return !fixed_regs[regno] && !call_used_regs[regno] &&
  2194. df_regs_ever_live_p (regno);
  2195. }
  2196. /* Return the bytes needed to compute the frame pointer from the current
  2197. stack pointer. */
  2198. #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
  2199. #define XTENSA_STACK_ALIGN(LOC) (((LOC) + STACK_BYTES-1) & ~(STACK_BYTES-1))
  2200. long
  2201. compute_frame_size (int size)
  2202. {
  2203. int regno;
  2204. /* Add space for the incoming static chain value. */
  2205. if (cfun->static_chain_decl != NULL)
  2206. size += (1 * UNITS_PER_WORD);
  2207. xtensa_callee_save_size = 0;
  2208. for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
  2209. {
  2210. if (xtensa_call_save_reg(regno))
  2211. xtensa_callee_save_size += UNITS_PER_WORD;
  2212. }
  2213. xtensa_current_frame_size =
  2214. XTENSA_STACK_ALIGN (size
  2215. + xtensa_callee_save_size
  2216. + crtl->outgoing_args_size
  2217. + (WINDOW_SIZE * UNITS_PER_WORD));
  2218. xtensa_callee_save_size = XTENSA_STACK_ALIGN (xtensa_callee_save_size);
  2219. return xtensa_current_frame_size;
  2220. }
  2221. bool
  2222. xtensa_frame_pointer_required (void)
  2223. {
  2224. /* The code to expand builtin_frame_addr and builtin_return_addr
  2225. currently uses the hard_frame_pointer instead of frame_pointer.
  2226. This seems wrong but maybe it's necessary for other architectures.
  2227. This function is derived from the i386 code. */
  2228. if (cfun->machine->accesses_prev_frame)
  2229. return true;
  2230. return false;
  2231. }
  2232. /* minimum frame = reg save area (4 words) plus static chain (1 word)
  2233. and the total number of words must be a multiple of 128 bits. */
  2234. #define MIN_FRAME_SIZE (8 * UNITS_PER_WORD)
  2235. void
  2236. xtensa_expand_prologue (void)
  2237. {
  2238. HOST_WIDE_INT total_size;
  2239. rtx_insn *insn = NULL;
  2240. rtx note_rtx;
  2241. total_size = compute_frame_size (get_frame_size ());
  2242. if (TARGET_WINDOWED_ABI)
  2243. {
  2244. if (total_size < (1 << (12+3)))
  2245. insn = emit_insn (gen_entry (GEN_INT (total_size)));
  2246. else
  2247. {
  2248. /* Use a8 as a temporary since a0-a7 may be live. */
  2249. rtx tmp_reg = gen_rtx_REG (Pmode, A8_REG);
  2250. emit_insn (gen_entry (GEN_INT (MIN_FRAME_SIZE)));
  2251. emit_move_insn (tmp_reg, GEN_INT (total_size - MIN_FRAME_SIZE));
  2252. emit_insn (gen_subsi3 (tmp_reg, stack_pointer_rtx, tmp_reg));
  2253. insn = emit_insn (gen_movsi (stack_pointer_rtx, tmp_reg));
  2254. }
  2255. }
  2256. else
  2257. {
  2258. int regno;
  2259. HOST_WIDE_INT offset = 0;
  2260. /* -128 is a limit of single addi instruction. */
  2261. if (total_size > 0 && total_size <= 128)
  2262. {
  2263. insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
  2264. GEN_INT (-total_size)));
  2265. RTX_FRAME_RELATED_P (insn) = 1;
  2266. note_rtx = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
  2267. plus_constant (Pmode, stack_pointer_rtx,
  2268. -total_size));
  2269. add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
  2270. offset = total_size - UNITS_PER_WORD;
  2271. }
  2272. else if (xtensa_callee_save_size)
  2273. {
  2274. /* 1020 is maximal s32i offset, if the frame is bigger than that
  2275. * we move sp to the end of callee-saved save area, save and then
  2276. * move it to its final location. */
  2277. if (total_size > 1024)
  2278. {
  2279. insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
  2280. GEN_INT (-xtensa_callee_save_size)));
  2281. RTX_FRAME_RELATED_P (insn) = 1;
  2282. note_rtx = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
  2283. plus_constant (Pmode, stack_pointer_rtx,
  2284. -xtensa_callee_save_size));
  2285. add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
  2286. offset = xtensa_callee_save_size - UNITS_PER_WORD;
  2287. }
  2288. else
  2289. {
  2290. rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
  2291. emit_move_insn (tmp_reg, GEN_INT (total_size));
  2292. insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
  2293. stack_pointer_rtx, tmp_reg));
  2294. RTX_FRAME_RELATED_P (insn) = 1;
  2295. note_rtx = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
  2296. plus_constant (Pmode, stack_pointer_rtx,
  2297. -total_size));
  2298. add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
  2299. offset = total_size - UNITS_PER_WORD;
  2300. }
  2301. }
  2302. for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
  2303. {
  2304. if (xtensa_call_save_reg(regno))
  2305. {
  2306. rtx x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
  2307. rtx mem = gen_frame_mem (SImode, x);
  2308. rtx reg = gen_rtx_REG (SImode, regno);
  2309. offset -= UNITS_PER_WORD;
  2310. insn = emit_move_insn (mem, reg);
  2311. RTX_FRAME_RELATED_P (insn) = 1;
  2312. add_reg_note (insn, REG_FRAME_RELATED_EXPR,
  2313. gen_rtx_SET (VOIDmode, mem, reg));
  2314. }
  2315. }
  2316. if (total_size > 1024)
  2317. {
  2318. rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
  2319. emit_move_insn (tmp_reg, GEN_INT (total_size -
  2320. xtensa_callee_save_size));
  2321. insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
  2322. stack_pointer_rtx, tmp_reg));
  2323. RTX_FRAME_RELATED_P (insn) = 1;
  2324. note_rtx = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
  2325. plus_constant (Pmode, stack_pointer_rtx,
  2326. xtensa_callee_save_size -
  2327. total_size));
  2328. add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
  2329. }
  2330. }
  2331. if (frame_pointer_needed)
  2332. {
  2333. if (cfun->machine->set_frame_ptr_insn)
  2334. {
  2335. rtx_insn *first;
  2336. push_topmost_sequence ();
  2337. first = get_insns ();
  2338. pop_topmost_sequence ();
  2339. /* For all instructions prior to set_frame_ptr_insn, replace
  2340. hard_frame_pointer references with stack_pointer. */
  2341. for (insn = first;
  2342. insn != cfun->machine->set_frame_ptr_insn;
  2343. insn = NEXT_INSN (insn))
  2344. {
  2345. if (INSN_P (insn))
  2346. {
  2347. PATTERN (insn) = replace_rtx (copy_rtx (PATTERN (insn)),
  2348. hard_frame_pointer_rtx,
  2349. stack_pointer_rtx);
  2350. df_insn_rescan (insn);
  2351. }
  2352. }
  2353. }
  2354. else
  2355. {
  2356. insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
  2357. stack_pointer_rtx));
  2358. if (!TARGET_WINDOWED_ABI)
  2359. {
  2360. note_rtx = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
  2361. stack_pointer_rtx);
  2362. RTX_FRAME_RELATED_P (insn) = 1;
  2363. add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
  2364. }
  2365. }
  2366. }
  2367. if (TARGET_WINDOWED_ABI)
  2368. {
  2369. /* Create a note to describe the CFA. Because this is only used to set
  2370. DW_AT_frame_base for debug info, don't bother tracking changes through
  2371. each instruction in the prologue. It just takes up space. */
  2372. note_rtx = gen_rtx_SET (VOIDmode, (frame_pointer_needed
  2373. ? hard_frame_pointer_rtx
  2374. : stack_pointer_rtx),
  2375. plus_constant (Pmode, stack_pointer_rtx,
  2376. -total_size));
  2377. RTX_FRAME_RELATED_P (insn) = 1;
  2378. add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx);
  2379. }
  2380. }
  2381. void
  2382. xtensa_expand_epilogue (void)
  2383. {
  2384. if (!TARGET_WINDOWED_ABI)
  2385. {
  2386. int regno;
  2387. HOST_WIDE_INT offset;
  2388. if (xtensa_current_frame_size > (frame_pointer_needed ? 127 : 1024))
  2389. {
  2390. rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
  2391. emit_move_insn (tmp_reg, GEN_INT (xtensa_current_frame_size -
  2392. xtensa_callee_save_size));
  2393. emit_insn (gen_addsi3 (stack_pointer_rtx, frame_pointer_needed ?
  2394. hard_frame_pointer_rtx : stack_pointer_rtx,
  2395. tmp_reg));
  2396. offset = xtensa_callee_save_size - UNITS_PER_WORD;
  2397. }
  2398. else
  2399. {
  2400. if (frame_pointer_needed)
  2401. emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
  2402. offset = xtensa_current_frame_size - UNITS_PER_WORD;
  2403. }
  2404. /* Prevent reordering of saved a0 update and loading it back from
  2405. the save area. */
  2406. if (crtl->calls_eh_return)
  2407. emit_insn (gen_blockage ());
  2408. for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
  2409. {
  2410. if (xtensa_call_save_reg(regno))
  2411. {
  2412. rtx x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
  2413. offset -= UNITS_PER_WORD;
  2414. emit_move_insn (gen_rtx_REG (SImode, regno),
  2415. gen_frame_mem (SImode, x));
  2416. }
  2417. }
  2418. if (xtensa_current_frame_size > 0)
  2419. {
  2420. if (frame_pointer_needed || /* always reachable with addi */
  2421. xtensa_current_frame_size > 1024 ||
  2422. xtensa_current_frame_size <= 127)
  2423. {
  2424. if (xtensa_current_frame_size <= 127)
  2425. offset = xtensa_current_frame_size;
  2426. else
  2427. offset = xtensa_callee_save_size;
  2428. emit_insn (gen_addsi3 (stack_pointer_rtx,
  2429. stack_pointer_rtx,
  2430. GEN_INT (offset)));
  2431. }
  2432. else
  2433. {
  2434. rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG);
  2435. emit_move_insn (tmp_reg, GEN_INT (xtensa_current_frame_size));
  2436. emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
  2437. tmp_reg));
  2438. }
  2439. }
  2440. if (crtl->calls_eh_return)
  2441. emit_insn (gen_add3_insn (stack_pointer_rtx,
  2442. stack_pointer_rtx,
  2443. EH_RETURN_STACKADJ_RTX));
  2444. }
  2445. xtensa_current_frame_size = 0;
  2446. xtensa_callee_save_size = 0;
  2447. emit_jump_insn (gen_return ());
  2448. }
  2449. void
  2450. xtensa_set_return_address (rtx address, rtx scratch)
  2451. {
  2452. HOST_WIDE_INT total_size = compute_frame_size (get_frame_size ());
  2453. rtx frame = frame_pointer_needed ?
  2454. hard_frame_pointer_rtx : stack_pointer_rtx;
  2455. rtx a0_addr = plus_constant (Pmode, frame,
  2456. total_size - UNITS_PER_WORD);
  2457. rtx note = gen_rtx_SET (VOIDmode,
  2458. gen_frame_mem (SImode, a0_addr),
  2459. gen_rtx_REG (SImode, A0_REG));
  2460. rtx insn;
  2461. if (total_size > 1024) {
  2462. emit_move_insn (scratch, GEN_INT (total_size - UNITS_PER_WORD));
  2463. emit_insn (gen_addsi3 (scratch, frame, scratch));
  2464. a0_addr = scratch;
  2465. }
  2466. insn = emit_move_insn (gen_frame_mem (SImode, a0_addr), address);
  2467. RTX_FRAME_RELATED_P (insn) = 1;
  2468. add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
  2469. }
  2470. rtx
  2471. xtensa_return_addr (int count, rtx frame)
  2472. {
  2473. rtx result, retaddr, curaddr, label;
  2474. if (!TARGET_WINDOWED_ABI)
  2475. {
  2476. if (count != 0)
  2477. return const0_rtx;
  2478. return get_hard_reg_initial_val (Pmode, A0_REG);
  2479. }
  2480. if (count == -1)
  2481. retaddr = gen_rtx_REG (Pmode, A0_REG);
  2482. else
  2483. {
  2484. rtx addr = plus_constant (Pmode, frame, -4 * UNITS_PER_WORD);
  2485. addr = memory_address (Pmode, addr);
  2486. retaddr = gen_reg_rtx (Pmode);
  2487. emit_move_insn (retaddr, gen_rtx_MEM (Pmode, addr));
  2488. }
  2489. /* The 2 most-significant bits of the return address on Xtensa hold
  2490. the register window size. To get the real return address, these
  2491. bits must be replaced with the high bits from some address in the
  2492. code. */
  2493. /* Get the 2 high bits of a local label in the code. */
  2494. curaddr = gen_reg_rtx (Pmode);
  2495. label = gen_label_rtx ();
  2496. emit_label (label);
  2497. LABEL_PRESERVE_P (label) = 1;
  2498. emit_move_insn (curaddr, gen_rtx_LABEL_REF (Pmode, label));
  2499. emit_insn (gen_lshrsi3 (curaddr, curaddr, GEN_INT (30)));
  2500. emit_insn (gen_ashlsi3 (curaddr, curaddr, GEN_INT (30)));
  2501. /* Clear the 2 high bits of the return address. */
  2502. result = gen_reg_rtx (Pmode);
  2503. emit_insn (gen_ashlsi3 (result, retaddr, GEN_INT (2)));
  2504. emit_insn (gen_lshrsi3 (result, result, GEN_INT (2)));
  2505. /* Combine them to get the result. */
  2506. emit_insn (gen_iorsi3 (result, result, curaddr));
  2507. return result;
  2508. }
  2509. /* Disable the use of word-sized or smaller complex modes for structures,
  2510. and for function arguments in particular, where they cause problems with
  2511. register a7. The xtensa_copy_incoming_a7 function assumes that there is
  2512. a single reference to an argument in a7, but with small complex modes the
  2513. real and imaginary components may be extracted separately, leading to two
  2514. uses of the register, only one of which would be replaced. */
  2515. static bool
  2516. xtensa_member_type_forces_blk (const_tree, machine_mode mode)
  2517. {
  2518. return mode == CQImode || mode == CHImode;
  2519. }
  2520. /* Create the va_list data type.
  2521. This structure is set up by __builtin_saveregs. The __va_reg field
  2522. points to a stack-allocated region holding the contents of the
  2523. incoming argument registers. The __va_ndx field is an index
  2524. initialized to the position of the first unnamed (variable)
  2525. argument. This same index is also used to address the arguments
  2526. passed in memory. Thus, the __va_stk field is initialized to point
  2527. to the position of the first argument in memory offset to account
  2528. for the arguments passed in registers and to account for the size
  2529. of the argument registers not being 16-byte aligned. E.G., there
  2530. are 6 argument registers of 4 bytes each, but we want the __va_ndx
  2531. for the first stack argument to have the maximal alignment of 16
  2532. bytes, so we offset the __va_stk address by 32 bytes so that
  2533. __va_stk[32] references the first argument on the stack. */
  2534. static tree
  2535. xtensa_build_builtin_va_list (void)
  2536. {
  2537. tree f_stk, f_reg, f_ndx, record, type_decl;
  2538. record = (*lang_hooks.types.make_type) (RECORD_TYPE);
  2539. type_decl = build_decl (BUILTINS_LOCATION,
  2540. TYPE_DECL, get_identifier ("__va_list_tag"), record);
  2541. f_stk = build_decl (BUILTINS_LOCATION,
  2542. FIELD_DECL, get_identifier ("__va_stk"),
  2543. ptr_type_node);
  2544. f_reg = build_decl (BUILTINS_LOCATION,
  2545. FIELD_DECL, get_identifier ("__va_reg"),
  2546. ptr_type_node);
  2547. f_ndx = build_decl (BUILTINS_LOCATION,
  2548. FIELD_DECL, get_identifier ("__va_ndx"),
  2549. integer_type_node);
  2550. DECL_FIELD_CONTEXT (f_stk) = record;
  2551. DECL_FIELD_CONTEXT (f_reg) = record;
  2552. DECL_FIELD_CONTEXT (f_ndx) = record;
  2553. TYPE_STUB_DECL (record) = type_decl;
  2554. TYPE_NAME (record) = type_decl;
  2555. TYPE_FIELDS (record) = f_stk;
  2556. DECL_CHAIN (f_stk) = f_reg;
  2557. DECL_CHAIN (f_reg) = f_ndx;
  2558. layout_type (record);
  2559. return record;
  2560. }
  2561. /* Save the incoming argument registers on the stack. Returns the
  2562. address of the saved registers. */
  2563. static rtx
  2564. xtensa_builtin_saveregs (void)
  2565. {
  2566. rtx gp_regs;
  2567. int arg_words = crtl->args.info.arg_words;
  2568. int gp_left = MAX_ARGS_IN_REGISTERS - arg_words;
  2569. if (gp_left <= 0)
  2570. return const0_rtx;
  2571. /* Allocate the general-purpose register space. */
  2572. gp_regs = assign_stack_local
  2573. (BLKmode, MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD, -1);
  2574. set_mem_alias_set (gp_regs, get_varargs_alias_set ());
  2575. /* Now store the incoming registers. */
  2576. cfun->machine->need_a7_copy = TARGET_WINDOWED_ABI;
  2577. cfun->machine->vararg_a7 = true;
  2578. move_block_from_reg (GP_ARG_FIRST + arg_words,
  2579. adjust_address (gp_regs, BLKmode,
  2580. arg_words * UNITS_PER_WORD),
  2581. gp_left);
  2582. if (cfun->machine->vararg_a7_copy != 0)
  2583. emit_insn_before (cfun->machine->vararg_a7_copy, get_insns ());
  2584. return XEXP (gp_regs, 0);
  2585. }
  2586. /* Implement `va_start' for varargs and stdarg. We look at the
  2587. current function to fill in an initial va_list. */
  2588. static void
  2589. xtensa_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
  2590. {
  2591. tree f_stk, stk;
  2592. tree f_reg, reg;
  2593. tree f_ndx, ndx;
  2594. tree t, u;
  2595. int arg_words;
  2596. arg_words = crtl->args.info.arg_words;
  2597. f_stk = TYPE_FIELDS (va_list_type_node);
  2598. f_reg = DECL_CHAIN (f_stk);
  2599. f_ndx = DECL_CHAIN (f_reg);
  2600. stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist, f_stk, NULL_TREE);
  2601. reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
  2602. f_reg, NULL_TREE);
  2603. ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
  2604. f_ndx, NULL_TREE);
  2605. /* Call __builtin_saveregs; save the result in __va_reg */
  2606. u = make_tree (sizetype, expand_builtin_saveregs ());
  2607. u = fold_convert (ptr_type_node, u);
  2608. t = build2 (MODIFY_EXPR, ptr_type_node, reg, u);
  2609. TREE_SIDE_EFFECTS (t) = 1;
  2610. expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
  2611. /* Set the __va_stk member to ($arg_ptr - 32). */
  2612. u = make_tree (ptr_type_node, virtual_incoming_args_rtx);
  2613. u = fold_build_pointer_plus_hwi (u, -32);
  2614. t = build2 (MODIFY_EXPR, ptr_type_node, stk, u);
  2615. TREE_SIDE_EFFECTS (t) = 1;
  2616. expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
  2617. /* Set the __va_ndx member. If the first variable argument is on
  2618. the stack, adjust __va_ndx by 2 words to account for the extra
  2619. alignment offset for __va_stk. */
  2620. if (arg_words >= MAX_ARGS_IN_REGISTERS)
  2621. arg_words += 2;
  2622. t = build2 (MODIFY_EXPR, integer_type_node, ndx,
  2623. build_int_cst (integer_type_node, arg_words * UNITS_PER_WORD));
  2624. TREE_SIDE_EFFECTS (t) = 1;
  2625. expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
  2626. }
  2627. /* Implement `va_arg'. */
  2628. static tree
  2629. xtensa_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
  2630. gimple_seq *post_p ATTRIBUTE_UNUSED)
  2631. {
  2632. tree f_stk, stk;
  2633. tree f_reg, reg;
  2634. tree f_ndx, ndx;
  2635. tree type_size, array, orig_ndx, addr, size, va_size, t;
  2636. tree lab_false, lab_over, lab_false2;
  2637. bool indirect;
  2638. indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
  2639. if (indirect)
  2640. type = build_pointer_type (type);
  2641. /* Handle complex values as separate real and imaginary parts. */
  2642. if (TREE_CODE (type) == COMPLEX_TYPE)
  2643. {
  2644. tree real_part, imag_part;
  2645. real_part = xtensa_gimplify_va_arg_expr (valist, TREE_TYPE (type),
  2646. pre_p, NULL);
  2647. real_part = get_initialized_tmp_var (real_part, pre_p, NULL);
  2648. imag_part = xtensa_gimplify_va_arg_expr (unshare_expr (valist),
  2649. TREE_TYPE (type),
  2650. pre_p, NULL);
  2651. imag_part = get_initialized_tmp_var (imag_part, pre_p, NULL);
  2652. return build2 (COMPLEX_EXPR, type, real_part, imag_part);
  2653. }
  2654. f_stk = TYPE_FIELDS (va_list_type_node);
  2655. f_reg = DECL_CHAIN (f_stk);
  2656. f_ndx = DECL_CHAIN (f_reg);
  2657. stk = build3 (COMPONENT_REF, TREE_TYPE (f_stk), valist,
  2658. f_stk, NULL_TREE);
  2659. reg = build3 (COMPONENT_REF, TREE_TYPE (f_reg), unshare_expr (valist),
  2660. f_reg, NULL_TREE);
  2661. ndx = build3 (COMPONENT_REF, TREE_TYPE (f_ndx), unshare_expr (valist),
  2662. f_ndx, NULL_TREE);
  2663. type_size = size_in_bytes (type);
  2664. va_size = round_up (type_size, UNITS_PER_WORD);
  2665. gimplify_expr (&va_size, pre_p, NULL, is_gimple_val, fb_rvalue);
  2666. /* First align __va_ndx if necessary for this arg:
  2667. orig_ndx = (AP).__va_ndx;
  2668. if (__alignof__ (TYPE) > 4 )
  2669. orig_ndx = ((orig_ndx + __alignof__ (TYPE) - 1)
  2670. & -__alignof__ (TYPE)); */
  2671. orig_ndx = get_initialized_tmp_var (ndx, pre_p, NULL);
  2672. if (TYPE_ALIGN (type) > BITS_PER_WORD)
  2673. {
  2674. int align = MIN (TYPE_ALIGN (type), STACK_BOUNDARY) / BITS_PER_UNIT;
  2675. t = build2 (PLUS_EXPR, integer_type_node, unshare_expr (orig_ndx),
  2676. build_int_cst (integer_type_node, align - 1));
  2677. t = build2 (BIT_AND_EXPR, integer_type_node, t,
  2678. build_int_cst (integer_type_node, -align));
  2679. gimplify_assign (unshare_expr (orig_ndx), t, pre_p);
  2680. }
  2681. /* Increment __va_ndx to point past the argument:
  2682. (AP).__va_ndx = orig_ndx + __va_size (TYPE); */
  2683. t = fold_convert (integer_type_node, va_size);
  2684. t = build2 (PLUS_EXPR, integer_type_node, orig_ndx, t);
  2685. gimplify_assign (unshare_expr (ndx), t, pre_p);
  2686. /* Check if the argument is in registers:
  2687. if ((AP).__va_ndx <= __MAX_ARGS_IN_REGISTERS * 4
  2688. && !must_pass_in_stack (type))
  2689. __array = (AP).__va_reg; */
  2690. array = create_tmp_var (ptr_type_node);
  2691. lab_over = NULL;
  2692. if (!targetm.calls.must_pass_in_stack (TYPE_MODE (type), type))
  2693. {
  2694. lab_false = create_artificial_label (UNKNOWN_LOCATION);
  2695. lab_over = create_artificial_label (UNKNOWN_LOCATION);
  2696. t = build2 (GT_EXPR, boolean_type_node, unshare_expr (ndx),
  2697. build_int_cst (integer_type_node,
  2698. MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
  2699. t = build3 (COND_EXPR, void_type_node, t,
  2700. build1 (GOTO_EXPR, void_type_node, lab_false),
  2701. NULL_TREE);
  2702. gimplify_and_add (t, pre_p);
  2703. gimplify_assign (unshare_expr (array), reg, pre_p);
  2704. t = build1 (GOTO_EXPR, void_type_node, lab_over);
  2705. gimplify_and_add (t, pre_p);
  2706. t = build1 (LABEL_EXPR, void_type_node, lab_false);
  2707. gimplify_and_add (t, pre_p);
  2708. }
  2709. /* ...otherwise, the argument is on the stack (never split between
  2710. registers and the stack -- change __va_ndx if necessary):
  2711. else
  2712. {
  2713. if (orig_ndx <= __MAX_ARGS_IN_REGISTERS * 4)
  2714. (AP).__va_ndx = 32 + __va_size (TYPE);
  2715. __array = (AP).__va_stk;
  2716. } */
  2717. lab_false2 = create_artificial_label (UNKNOWN_LOCATION);
  2718. t = build2 (GT_EXPR, boolean_type_node, unshare_expr (orig_ndx),
  2719. build_int_cst (integer_type_node,
  2720. MAX_ARGS_IN_REGISTERS * UNITS_PER_WORD));
  2721. t = build3 (COND_EXPR, void_type_node, t,
  2722. build1 (GOTO_EXPR, void_type_node, lab_false2),
  2723. NULL_TREE);
  2724. gimplify_and_add (t, pre_p);
  2725. t = size_binop (PLUS_EXPR, unshare_expr (va_size), size_int (32));
  2726. t = fold_convert (integer_type_node, t);
  2727. gimplify_assign (unshare_expr (ndx), t, pre_p);
  2728. t = build1 (LABEL_EXPR, void_type_node, lab_false2);
  2729. gimplify_and_add (t, pre_p);
  2730. gimplify_assign (array, stk, pre_p);
  2731. if (lab_over)
  2732. {
  2733. t = build1 (LABEL_EXPR, void_type_node, lab_over);
  2734. gimplify_and_add (t, pre_p);
  2735. }
  2736. /* Given the base array pointer (__array) and index to the subsequent
  2737. argument (__va_ndx), find the address:
  2738. __array + (AP).__va_ndx - (BYTES_BIG_ENDIAN && sizeof (TYPE) < 4
  2739. ? sizeof (TYPE)
  2740. : __va_size (TYPE))
  2741. The results are endian-dependent because values smaller than one word
  2742. are aligned differently. */
  2743. if (BYTES_BIG_ENDIAN && TREE_CODE (type_size) == INTEGER_CST)
  2744. {
  2745. t = fold_build2 (GE_EXPR, boolean_type_node, unshare_expr (type_size),
  2746. size_int (PARM_BOUNDARY / BITS_PER_UNIT));
  2747. t = fold_build3 (COND_EXPR, sizetype, t, unshare_expr (va_size),
  2748. unshare_expr (type_size));
  2749. size = t;
  2750. }
  2751. else
  2752. size = unshare_expr (va_size);
  2753. t = fold_convert (sizetype, unshare_expr (ndx));
  2754. t = build2 (MINUS_EXPR, sizetype, t, size);
  2755. addr = fold_build_pointer_plus (unshare_expr (array), t);
  2756. addr = fold_convert (build_pointer_type (type), addr);
  2757. if (indirect)
  2758. addr = build_va_arg_indirect_ref (addr);
  2759. return build_va_arg_indirect_ref (addr);
  2760. }
  2761. /* Builtins. */
  2762. enum xtensa_builtin
  2763. {
  2764. XTENSA_BUILTIN_UMULSIDI3,
  2765. XTENSA_BUILTIN_max
  2766. };
  2767. static void
  2768. xtensa_init_builtins (void)
  2769. {
  2770. tree ftype, decl;
  2771. ftype = build_function_type_list (unsigned_intDI_type_node,
  2772. unsigned_intSI_type_node,
  2773. unsigned_intSI_type_node, NULL_TREE);
  2774. decl = add_builtin_function ("__builtin_umulsidi3", ftype,
  2775. XTENSA_BUILTIN_UMULSIDI3, BUILT_IN_MD,
  2776. "__umulsidi3", NULL_TREE);
  2777. TREE_NOTHROW (decl) = 1;
  2778. TREE_READONLY (decl) = 1;
  2779. }
  2780. static tree
  2781. xtensa_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED, tree *args,
  2782. bool ignore ATTRIBUTE_UNUSED)
  2783. {
  2784. unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
  2785. tree arg0, arg1;
  2786. switch (fcode)
  2787. {
  2788. case XTENSA_BUILTIN_UMULSIDI3:
  2789. arg0 = args[0];
  2790. arg1 = args[1];
  2791. if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
  2792. || TARGET_MUL32_HIGH)
  2793. return fold_build2 (MULT_EXPR, unsigned_intDI_type_node,
  2794. fold_convert (unsigned_intDI_type_node, arg0),
  2795. fold_convert (unsigned_intDI_type_node, arg1));
  2796. break;
  2797. default:
  2798. internal_error ("bad builtin code");
  2799. break;
  2800. }
  2801. return NULL;
  2802. }
  2803. static rtx
  2804. xtensa_expand_builtin (tree exp, rtx target,
  2805. rtx subtarget ATTRIBUTE_UNUSED,
  2806. machine_mode mode ATTRIBUTE_UNUSED,
  2807. int ignore)
  2808. {
  2809. tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
  2810. unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
  2811. switch (fcode)
  2812. {
  2813. case XTENSA_BUILTIN_UMULSIDI3:
  2814. /* The umulsidi3 builtin is just a mechanism to avoid calling the real
  2815. __umulsidi3 function when the Xtensa configuration can directly
  2816. implement it. If not, just call the function. */
  2817. return expand_call (exp, target, ignore);
  2818. default:
  2819. internal_error ("bad builtin code");
  2820. }
  2821. return NULL_RTX;
  2822. }
  2823. /* Worker function for TARGET_PREFERRED_RELOAD_CLASS. */
  2824. static reg_class_t
  2825. xtensa_preferred_reload_class (rtx x, reg_class_t rclass)
  2826. {
  2827. if (CONSTANT_P (x) && CONST_DOUBLE_P (x))
  2828. return NO_REGS;
  2829. /* Don't use the stack pointer or hard frame pointer for reloads!
  2830. The hard frame pointer would normally be OK except that it may
  2831. briefly hold an incoming argument in the prologue, and reload
  2832. won't know that it is live because the hard frame pointer is
  2833. treated specially. */
  2834. if (rclass == AR_REGS || rclass == GR_REGS)
  2835. return RL_REGS;
  2836. return rclass;
  2837. }
  2838. /* Worker function for TARGET_PREFERRED_OUTPUT_RELOAD_CLASS. */
  2839. static reg_class_t
  2840. xtensa_preferred_output_reload_class (rtx x ATTRIBUTE_UNUSED,
  2841. reg_class_t rclass)
  2842. {
  2843. /* Don't use the stack pointer or hard frame pointer for reloads!
  2844. The hard frame pointer would normally be OK except that it may
  2845. briefly hold an incoming argument in the prologue, and reload
  2846. won't know that it is live because the hard frame pointer is
  2847. treated specially. */
  2848. if (rclass == AR_REGS || rclass == GR_REGS)
  2849. return RL_REGS;
  2850. return rclass;
  2851. }
  2852. /* Worker function for TARGET_SECONDARY_RELOAD. */
  2853. static reg_class_t
  2854. xtensa_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
  2855. machine_mode mode, secondary_reload_info *sri)
  2856. {
  2857. int regno;
  2858. if (in_p && constantpool_mem_p (x))
  2859. {
  2860. if (rclass == FP_REGS)
  2861. return RL_REGS;
  2862. if (mode == QImode)
  2863. sri->icode = CODE_FOR_reloadqi_literal;
  2864. else if (mode == HImode)
  2865. sri->icode = CODE_FOR_reloadhi_literal;
  2866. }
  2867. regno = xt_true_regnum (x);
  2868. if (ACC_REG_P (regno))
  2869. return ((rclass == GR_REGS || rclass == RL_REGS) ? NO_REGS : RL_REGS);
  2870. if (rclass == ACC_REG)
  2871. return (GP_REG_P (regno) ? NO_REGS : RL_REGS);
  2872. return NO_REGS;
  2873. }
  2874. void
  2875. order_regs_for_local_alloc (void)
  2876. {
  2877. if (!leaf_function_p ())
  2878. {
  2879. static const int reg_nonleaf_alloc_order[FIRST_PSEUDO_REGISTER] =
  2880. REG_ALLOC_ORDER;
  2881. static const int reg_nonleaf_alloc_order_call0[FIRST_PSEUDO_REGISTER] =
  2882. {
  2883. 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 12, 13, 14, 15,
  2884. 18,
  2885. 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
  2886. 0, 1, 16, 17,
  2887. 35,
  2888. };
  2889. memcpy (reg_alloc_order, TARGET_WINDOWED_ABI ?
  2890. reg_nonleaf_alloc_order : reg_nonleaf_alloc_order_call0,
  2891. FIRST_PSEUDO_REGISTER * sizeof (int));
  2892. }
  2893. else
  2894. {
  2895. int i, num_arg_regs;
  2896. int nxt = 0;
  2897. /* Use the AR registers in increasing order (skipping a0 and a1)
  2898. but save the incoming argument registers for a last resort. */
  2899. num_arg_regs = crtl->args.info.arg_words;
  2900. if (num_arg_regs > MAX_ARGS_IN_REGISTERS)
  2901. num_arg_regs = MAX_ARGS_IN_REGISTERS;
  2902. for (i = GP_ARG_FIRST; i < 16 - num_arg_regs; i++)
  2903. reg_alloc_order[nxt++] = i + num_arg_regs;
  2904. for (i = 0; i < num_arg_regs; i++)
  2905. reg_alloc_order[nxt++] = GP_ARG_FIRST + i;
  2906. /* List the coprocessor registers in order. */
  2907. for (i = 0; i < BR_REG_NUM; i++)
  2908. reg_alloc_order[nxt++] = BR_REG_FIRST + i;
  2909. /* List the FP registers in order for now. */
  2910. for (i = 0; i < 16; i++)
  2911. reg_alloc_order[nxt++] = FP_REG_FIRST + i;
  2912. /* GCC requires that we list *all* the registers.... */
  2913. reg_alloc_order[nxt++] = 0; /* a0 = return address */
  2914. reg_alloc_order[nxt++] = 1; /* a1 = stack pointer */
  2915. reg_alloc_order[nxt++] = 16; /* pseudo frame pointer */
  2916. reg_alloc_order[nxt++] = 17; /* pseudo arg pointer */
  2917. reg_alloc_order[nxt++] = ACC_REG_FIRST; /* MAC16 accumulator */
  2918. }
  2919. }
  2920. /* Some Xtensa targets support multiple bss sections. If the section
  2921. name ends with ".bss", add SECTION_BSS to the flags. */
  2922. static unsigned int
  2923. xtensa_multibss_section_type_flags (tree decl, const char *name, int reloc)
  2924. {
  2925. unsigned int flags = default_section_type_flags (decl, name, reloc);
  2926. const char *suffix;
  2927. suffix = strrchr (name, '.');
  2928. if (suffix && strcmp (suffix, ".bss") == 0)
  2929. {
  2930. if (!decl || (TREE_CODE (decl) == VAR_DECL
  2931. && DECL_INITIAL (decl) == NULL_TREE))
  2932. flags |= SECTION_BSS; /* @nobits */
  2933. else
  2934. warning (0, "only uninitialized variables can be placed in a "
  2935. ".bss section");
  2936. }
  2937. return flags;
  2938. }
  2939. /* The literal pool stays with the function. */
  2940. static section *
  2941. xtensa_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
  2942. rtx x ATTRIBUTE_UNUSED,
  2943. unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
  2944. {
  2945. return function_section (current_function_decl);
  2946. }
  2947. /* Worker function for TARGET_REGISTER_MOVE_COST. */
  2948. static int
  2949. xtensa_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
  2950. reg_class_t from, reg_class_t to)
  2951. {
  2952. if (from == to && from != BR_REGS && to != BR_REGS)
  2953. return 2;
  2954. else if (reg_class_subset_p (from, AR_REGS)
  2955. && reg_class_subset_p (to, AR_REGS))
  2956. return 2;
  2957. else if (reg_class_subset_p (from, AR_REGS) && to == ACC_REG)
  2958. return 3;
  2959. else if (from == ACC_REG && reg_class_subset_p (to, AR_REGS))
  2960. return 3;
  2961. else
  2962. return 10;
  2963. }
  2964. /* Worker function for TARGET_MEMORY_MOVE_COST. */
  2965. static int
  2966. xtensa_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
  2967. reg_class_t rclass ATTRIBUTE_UNUSED,
  2968. bool in ATTRIBUTE_UNUSED)
  2969. {
  2970. return 4;
  2971. }
  2972. /* Compute a (partial) cost for rtx X. Return true if the complete
  2973. cost has been computed, and false if subexpressions should be
  2974. scanned. In either case, *TOTAL contains the cost result. */
  2975. static bool
  2976. xtensa_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
  2977. int *total, bool speed ATTRIBUTE_UNUSED)
  2978. {
  2979. switch (code)
  2980. {
  2981. case CONST_INT:
  2982. switch (outer_code)
  2983. {
  2984. case SET:
  2985. if (xtensa_simm12b (INTVAL (x)))
  2986. {
  2987. *total = 4;
  2988. return true;
  2989. }
  2990. break;
  2991. case PLUS:
  2992. if (xtensa_simm8 (INTVAL (x))
  2993. || xtensa_simm8x256 (INTVAL (x)))
  2994. {
  2995. *total = 0;
  2996. return true;
  2997. }
  2998. break;
  2999. case AND:
  3000. if (xtensa_mask_immediate (INTVAL (x)))
  3001. {
  3002. *total = 0;
  3003. return true;
  3004. }
  3005. break;
  3006. case COMPARE:
  3007. if ((INTVAL (x) == 0) || xtensa_b4const (INTVAL (x)))
  3008. {
  3009. *total = 0;
  3010. return true;
  3011. }
  3012. break;
  3013. case ASHIFT:
  3014. case ASHIFTRT:
  3015. case LSHIFTRT:
  3016. case ROTATE:
  3017. case ROTATERT:
  3018. /* No way to tell if X is the 2nd operand so be conservative. */
  3019. default: break;
  3020. }
  3021. if (xtensa_simm12b (INTVAL (x)))
  3022. *total = 5;
  3023. else if (TARGET_CONST16)
  3024. *total = COSTS_N_INSNS (2);
  3025. else
  3026. *total = 6;
  3027. return true;
  3028. case CONST:
  3029. case LABEL_REF:
  3030. case SYMBOL_REF:
  3031. if (TARGET_CONST16)
  3032. *total = COSTS_N_INSNS (2);
  3033. else
  3034. *total = 5;
  3035. return true;
  3036. case CONST_DOUBLE:
  3037. if (TARGET_CONST16)
  3038. *total = COSTS_N_INSNS (4);
  3039. else
  3040. *total = 7;
  3041. return true;
  3042. case MEM:
  3043. {
  3044. int num_words =
  3045. (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD) ? 2 : 1;
  3046. if (memory_address_p (GET_MODE (x), XEXP ((x), 0)))
  3047. *total = COSTS_N_INSNS (num_words);
  3048. else
  3049. *total = COSTS_N_INSNS (2*num_words);
  3050. return true;
  3051. }
  3052. case FFS:
  3053. case CTZ:
  3054. *total = COSTS_N_INSNS (TARGET_NSA ? 5 : 50);
  3055. return true;
  3056. case CLZ:
  3057. *total = COSTS_N_INSNS (TARGET_NSA ? 1 : 50);
  3058. return true;
  3059. case NOT:
  3060. *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 3 : 2);
  3061. return true;
  3062. case AND:
  3063. case IOR:
  3064. case XOR:
  3065. if (GET_MODE (x) == DImode)
  3066. *total = COSTS_N_INSNS (2);
  3067. else
  3068. *total = COSTS_N_INSNS (1);
  3069. return true;
  3070. case ASHIFT:
  3071. case ASHIFTRT:
  3072. case LSHIFTRT:
  3073. if (GET_MODE (x) == DImode)
  3074. *total = COSTS_N_INSNS (50);
  3075. else
  3076. *total = COSTS_N_INSNS (1);
  3077. return true;
  3078. case ABS:
  3079. {
  3080. machine_mode xmode = GET_MODE (x);
  3081. if (xmode == SFmode)
  3082. *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
  3083. else if (xmode == DFmode)
  3084. *total = COSTS_N_INSNS (50);
  3085. else
  3086. *total = COSTS_N_INSNS (4);
  3087. return true;
  3088. }
  3089. case PLUS:
  3090. case MINUS:
  3091. {
  3092. machine_mode xmode = GET_MODE (x);
  3093. if (xmode == SFmode)
  3094. *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 1 : 50);
  3095. else if (xmode == DFmode || xmode == DImode)
  3096. *total = COSTS_N_INSNS (50);
  3097. else
  3098. *total = COSTS_N_INSNS (1);
  3099. return true;
  3100. }
  3101. case NEG:
  3102. *total = COSTS_N_INSNS ((GET_MODE (x) == DImode) ? 4 : 2);
  3103. return true;
  3104. case MULT:
  3105. {
  3106. machine_mode xmode = GET_MODE (x);
  3107. if (xmode == SFmode)
  3108. *total = COSTS_N_INSNS (TARGET_HARD_FLOAT ? 4 : 50);
  3109. else if (xmode == DFmode)
  3110. *total = COSTS_N_INSNS (50);
  3111. else if (xmode == DImode)
  3112. *total = COSTS_N_INSNS (TARGET_MUL32_HIGH ? 10 : 50);
  3113. else if (TARGET_MUL32)
  3114. *total = COSTS_N_INSNS (4);
  3115. else if (TARGET_MAC16)
  3116. *total = COSTS_N_INSNS (16);
  3117. else if (TARGET_MUL16)
  3118. *total = COSTS_N_INSNS (12);
  3119. else
  3120. *total = COSTS_N_INSNS (50);
  3121. return true;
  3122. }
  3123. case DIV:
  3124. case MOD:
  3125. {
  3126. machine_mode xmode = GET_MODE (x);
  3127. if (xmode == SFmode)
  3128. {
  3129. *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_DIV ? 8 : 50);
  3130. return true;
  3131. }
  3132. else if (xmode == DFmode)
  3133. {
  3134. *total = COSTS_N_INSNS (50);
  3135. return true;
  3136. }
  3137. }
  3138. /* Fall through. */
  3139. case UDIV:
  3140. case UMOD:
  3141. {
  3142. machine_mode xmode = GET_MODE (x);
  3143. if (xmode == DImode)
  3144. *total = COSTS_N_INSNS (50);
  3145. else if (TARGET_DIV32)
  3146. *total = COSTS_N_INSNS (32);
  3147. else
  3148. *total = COSTS_N_INSNS (50);
  3149. return true;
  3150. }
  3151. case SQRT:
  3152. if (GET_MODE (x) == SFmode)
  3153. *total = COSTS_N_INSNS (TARGET_HARD_FLOAT_SQRT ? 8 : 50);
  3154. else
  3155. *total = COSTS_N_INSNS (50);
  3156. return true;
  3157. case SMIN:
  3158. case UMIN:
  3159. case SMAX:
  3160. case UMAX:
  3161. *total = COSTS_N_INSNS (TARGET_MINMAX ? 1 : 50);
  3162. return true;
  3163. case SIGN_EXTRACT:
  3164. case SIGN_EXTEND:
  3165. *total = COSTS_N_INSNS (TARGET_SEXT ? 1 : 2);
  3166. return true;
  3167. case ZERO_EXTRACT:
  3168. case ZERO_EXTEND:
  3169. *total = COSTS_N_INSNS (1);
  3170. return true;
  3171. default:
  3172. return false;
  3173. }
  3174. }
  3175. /* Worker function for TARGET_RETURN_IN_MEMORY. */
  3176. static bool
  3177. xtensa_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
  3178. {
  3179. return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
  3180. > 4 * UNITS_PER_WORD);
  3181. }
  3182. /* Worker function for TARGET_FUNCTION_VALUE. */
  3183. rtx
  3184. xtensa_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED,
  3185. bool outgoing)
  3186. {
  3187. return gen_rtx_REG ((INTEGRAL_TYPE_P (valtype)
  3188. && TYPE_PRECISION (valtype) < BITS_PER_WORD)
  3189. ? SImode : TYPE_MODE (valtype),
  3190. outgoing ? GP_OUTGOING_RETURN : GP_RETURN);
  3191. }
  3192. /* Worker function for TARGET_LIBCALL_VALUE. */
  3193. static rtx
  3194. xtensa_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
  3195. {
  3196. return gen_rtx_REG ((GET_MODE_CLASS (mode) == MODE_INT
  3197. && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
  3198. ? SImode : mode, GP_RETURN);
  3199. }
  3200. /* Worker function TARGET_FUNCTION_VALUE_REGNO_P. */
  3201. static bool
  3202. xtensa_function_value_regno_p (const unsigned int regno)
  3203. {
  3204. return (regno == GP_RETURN);
  3205. }
  3206. /* The static chain is passed in memory. Provide rtx giving 'mem'
  3207. expressions that denote where they are stored. */
  3208. static rtx
  3209. xtensa_static_chain (const_tree ARG_UNUSED (fndecl_or_type), bool incoming_p)
  3210. {
  3211. if (TARGET_WINDOWED_ABI)
  3212. {
  3213. rtx base = incoming_p ? arg_pointer_rtx : stack_pointer_rtx;
  3214. return gen_frame_mem (Pmode, plus_constant (Pmode, base,
  3215. -5 * UNITS_PER_WORD));
  3216. }
  3217. else
  3218. return gen_rtx_REG (Pmode, A8_REG);
  3219. }
  3220. /* TRAMPOLINE_TEMPLATE: For Xtensa, the trampoline must perform an ENTRY
  3221. instruction with a minimal stack frame in order to get some free
  3222. registers. Once the actual call target is known, the proper stack frame
  3223. size is extracted from the ENTRY instruction at the target and the
  3224. current frame is adjusted to match. The trampoline then transfers
  3225. control to the instruction following the ENTRY at the target. Note:
  3226. this assumes that the target begins with an ENTRY instruction. */
  3227. static void
  3228. xtensa_asm_trampoline_template (FILE *stream)
  3229. {
  3230. bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
  3231. fprintf (stream, "\t.begin no-transform\n");
  3232. if (TARGET_WINDOWED_ABI)
  3233. {
  3234. fprintf (stream, "\tentry\tsp, %d\n", MIN_FRAME_SIZE);
  3235. if (use_call0)
  3236. {
  3237. /* Save the return address. */
  3238. fprintf (stream, "\tmov\ta10, a0\n");
  3239. /* Use a CALL0 instruction to skip past the constants and in the
  3240. process get the PC into A0. This allows PC-relative access to
  3241. the constants without relying on L32R. */
  3242. fprintf (stream, "\tcall0\t.Lskipconsts\n");
  3243. }
  3244. else
  3245. fprintf (stream, "\tj\t.Lskipconsts\n");
  3246. fprintf (stream, "\t.align\t4\n");
  3247. fprintf (stream, ".Lchainval:%s0\n", integer_asm_op (4, TRUE));
  3248. fprintf (stream, ".Lfnaddr:%s0\n", integer_asm_op (4, TRUE));
  3249. fprintf (stream, ".Lskipconsts:\n");
  3250. /* Load the static chain and function address from the trampoline. */
  3251. if (use_call0)
  3252. {
  3253. fprintf (stream, "\taddi\ta0, a0, 3\n");
  3254. fprintf (stream, "\tl32i\ta9, a0, 0\n");
  3255. fprintf (stream, "\tl32i\ta8, a0, 4\n");
  3256. }
  3257. else
  3258. {
  3259. fprintf (stream, "\tl32r\ta9, .Lchainval\n");
  3260. fprintf (stream, "\tl32r\ta8, .Lfnaddr\n");
  3261. }
  3262. /* Store the static chain. */
  3263. fprintf (stream, "\ts32i\ta9, sp, %d\n", MIN_FRAME_SIZE - 20);
  3264. /* Set the proper stack pointer value. */
  3265. fprintf (stream, "\tl32i\ta9, a8, 0\n");
  3266. fprintf (stream, "\textui\ta9, a9, %d, 12\n",
  3267. TARGET_BIG_ENDIAN ? 8 : 12);
  3268. fprintf (stream, "\tslli\ta9, a9, 3\n");
  3269. fprintf (stream, "\taddi\ta9, a9, %d\n", -MIN_FRAME_SIZE);
  3270. fprintf (stream, "\tsub\ta9, sp, a9\n");
  3271. fprintf (stream, "\tmovsp\tsp, a9\n");
  3272. if (use_call0)
  3273. /* Restore the return address. */
  3274. fprintf (stream, "\tmov\ta0, a10\n");
  3275. /* Jump to the instruction following the ENTRY. */
  3276. fprintf (stream, "\taddi\ta8, a8, 3\n");
  3277. fprintf (stream, "\tjx\ta8\n");
  3278. /* Pad size to a multiple of TRAMPOLINE_ALIGNMENT. */
  3279. if (use_call0)
  3280. fprintf (stream, "\t.byte\t0\n");
  3281. else
  3282. fprintf (stream, "\tnop\n");
  3283. }
  3284. else
  3285. {
  3286. if (use_call0)
  3287. {
  3288. /* Save the return address. */
  3289. fprintf (stream, "\tmov\ta10, a0\n");
  3290. /* Use a CALL0 instruction to skip past the constants and in the
  3291. process get the PC into A0. This allows PC-relative access to
  3292. the constants without relying on L32R. */
  3293. fprintf (stream, "\tcall0\t.Lskipconsts\n");
  3294. }
  3295. else
  3296. fprintf (stream, "\tj\t.Lskipconsts\n");
  3297. fprintf (stream, "\t.align\t4\n");
  3298. fprintf (stream, ".Lchainval:%s0\n", integer_asm_op (4, TRUE));
  3299. fprintf (stream, ".Lfnaddr:%s0\n", integer_asm_op (4, TRUE));
  3300. fprintf (stream, ".Lskipconsts:\n");
  3301. /* Load the static chain and function address from the trampoline. */
  3302. if (use_call0)
  3303. {
  3304. fprintf (stream, "\taddi\ta0, a0, 3\n");
  3305. fprintf (stream, "\tl32i\ta8, a0, 0\n");
  3306. fprintf (stream, "\tl32i\ta9, a0, 4\n");
  3307. fprintf (stream, "\tmov\ta0, a10\n");
  3308. }
  3309. else
  3310. {
  3311. fprintf (stream, "\tl32r\ta8, .Lchainval\n");
  3312. fprintf (stream, "\tl32r\ta9, .Lfnaddr\n");
  3313. }
  3314. fprintf (stream, "\tjx\ta9\n");
  3315. /* Pad size to a multiple of TRAMPOLINE_ALIGNMENT. */
  3316. if (use_call0)
  3317. fprintf (stream, "\t.byte\t0\n");
  3318. else
  3319. fprintf (stream, "\tnop\n");
  3320. }
  3321. fprintf (stream, "\t.end no-transform\n");
  3322. }
  3323. static void
  3324. xtensa_trampoline_init (rtx m_tramp, tree fndecl, rtx chain)
  3325. {
  3326. rtx func = XEXP (DECL_RTL (fndecl), 0);
  3327. bool use_call0 = (TARGET_CONST16 || TARGET_ABSOLUTE_LITERALS);
  3328. int chain_off;
  3329. int func_off;
  3330. if (TARGET_WINDOWED_ABI)
  3331. {
  3332. chain_off = use_call0 ? 12 : 8;
  3333. func_off = use_call0 ? 16 : 12;
  3334. }
  3335. else
  3336. {
  3337. chain_off = use_call0 ? 8 : 4;
  3338. func_off = use_call0 ? 12 : 8;
  3339. }
  3340. emit_block_move (m_tramp, assemble_trampoline_template (),
  3341. GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
  3342. emit_move_insn (adjust_address (m_tramp, SImode, chain_off), chain);
  3343. emit_move_insn (adjust_address (m_tramp, SImode, func_off), func);
  3344. emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__xtensa_sync_caches"),
  3345. LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
  3346. }
  3347. /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
  3348. static bool
  3349. xtensa_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
  3350. {
  3351. return !xtensa_tls_referenced_p (x);
  3352. }
  3353. /* Implement TARGET_CAN_USE_DOLOOP_P. */
  3354. static bool
  3355. xtensa_can_use_doloop_p (const widest_int &, const widest_int &,
  3356. unsigned int loop_depth, bool entered_at_top)
  3357. {
  3358. /* Considering limitations in the hardware, only use doloop
  3359. for innermost loops which must be entered from the top. */
  3360. if (loop_depth > 1 || !entered_at_top)
  3361. return false;
  3362. return true;
  3363. }
  3364. /* NULL if INSN insn is valid within a low-overhead loop.
  3365. Otherwise return why doloop cannot be applied. */
  3366. static const char *
  3367. xtensa_invalid_within_doloop (const rtx_insn *insn)
  3368. {
  3369. if (CALL_P (insn))
  3370. return "Function call in the loop.";
  3371. if (JUMP_P (insn) && INSN_CODE (insn) == CODE_FOR_return)
  3372. return "Return from a call instruction in the loop.";
  3373. return NULL;
  3374. }
  3375. /* Optimize LOOP. */
  3376. #if TARGET_LOOPS
  3377. static bool
  3378. hwloop_optimize (hwloop_info loop)
  3379. {
  3380. int i;
  3381. edge entry_edge;
  3382. basic_block entry_bb;
  3383. rtx iter_reg;
  3384. rtx_insn *insn, *seq, *entry_after;
  3385. if (loop->depth > 1)
  3386. {
  3387. if (dump_file)
  3388. fprintf (dump_file, ";; loop %d is not innermost\n",
  3389. loop->loop_no);
  3390. return false;
  3391. }
  3392. if (!loop->incoming_dest)
  3393. {
  3394. if (dump_file)
  3395. fprintf (dump_file, ";; loop %d has more than one entry\n",
  3396. loop->loop_no);
  3397. return false;
  3398. }
  3399. if (loop->incoming_dest != loop->head)
  3400. {
  3401. if (dump_file)
  3402. fprintf (dump_file, ";; loop %d is not entered from head\n",
  3403. loop->loop_no);
  3404. return false;
  3405. }
  3406. if (loop->has_call || loop->has_asm)
  3407. {
  3408. if (dump_file)
  3409. fprintf (dump_file, ";; loop %d has invalid insn\n",
  3410. loop->loop_no);
  3411. return false;
  3412. }
  3413. /* Scan all the blocks to make sure they don't use iter_reg. */
  3414. if (loop->iter_reg_used || loop->iter_reg_used_outside)
  3415. {
  3416. if (dump_file)
  3417. fprintf (dump_file, ";; loop %d uses iterator\n",
  3418. loop->loop_no);
  3419. return false;
  3420. }
  3421. /* Check if start_label appears before doloop_end. */
  3422. insn = loop->start_label;
  3423. while (insn && insn != loop->loop_end)
  3424. insn = NEXT_INSN (insn);
  3425. if (!insn)
  3426. {
  3427. if (dump_file)
  3428. fprintf (dump_file, ";; loop %d start_label not before loop_end\n",
  3429. loop->loop_no);
  3430. return false;
  3431. }
  3432. /* Get the loop iteration register. */
  3433. iter_reg = loop->iter_reg;
  3434. gcc_assert (REG_P (iter_reg));
  3435. entry_edge = NULL;
  3436. FOR_EACH_VEC_SAFE_ELT (loop->incoming, i, entry_edge)
  3437. if (entry_edge->flags & EDGE_FALLTHRU)
  3438. break;
  3439. if (entry_edge == NULL)
  3440. return false;
  3441. /* Place the zero_cost_loop_start instruction before the loop. */
  3442. entry_bb = entry_edge->src;
  3443. start_sequence ();
  3444. insn = emit_insn (gen_zero_cost_loop_start (loop->iter_reg,
  3445. loop->start_label,
  3446. loop->iter_reg));
  3447. seq = get_insns ();
  3448. if (!single_succ_p (entry_bb) || vec_safe_length (loop->incoming) > 1)
  3449. {
  3450. basic_block new_bb;
  3451. edge e;
  3452. edge_iterator ei;
  3453. emit_insn_before (seq, BB_HEAD (loop->head));
  3454. seq = emit_label_before (gen_label_rtx (), seq);
  3455. new_bb = create_basic_block (seq, insn, entry_bb);
  3456. FOR_EACH_EDGE (e, ei, loop->incoming)
  3457. {
  3458. if (!(e->flags & EDGE_FALLTHRU))
  3459. redirect_edge_and_branch_force (e, new_bb);
  3460. else
  3461. redirect_edge_succ (e, new_bb);
  3462. }
  3463. make_edge (new_bb, loop->head, 0);
  3464. }
  3465. else
  3466. {
  3467. entry_after = BB_END (entry_bb);
  3468. while (DEBUG_INSN_P (entry_after)
  3469. || (NOTE_P (entry_after)
  3470. && NOTE_KIND (entry_after) != NOTE_INSN_BASIC_BLOCK))
  3471. entry_after = PREV_INSN (entry_after);
  3472. emit_insn_after (seq, entry_after);
  3473. }
  3474. end_sequence ();
  3475. return true;
  3476. }
  3477. /* A callback for the hw-doloop pass. Called when a loop we have discovered
  3478. turns out not to be optimizable; we have to split the loop_end pattern into
  3479. a subtract and a test. */
  3480. static void
  3481. hwloop_fail (hwloop_info loop)
  3482. {
  3483. rtx test;
  3484. rtx_insn *insn = loop->loop_end;
  3485. emit_insn_before (gen_addsi3 (loop->iter_reg,
  3486. loop->iter_reg,
  3487. constm1_rtx),
  3488. loop->loop_end);
  3489. test = gen_rtx_NE (VOIDmode, loop->iter_reg, const0_rtx);
  3490. insn = emit_jump_insn_before (gen_cbranchsi4 (test,
  3491. loop->iter_reg, const0_rtx,
  3492. loop->start_label),
  3493. loop->loop_end);
  3494. JUMP_LABEL (insn) = loop->start_label;
  3495. LABEL_NUSES (loop->start_label)++;
  3496. delete_insn (loop->loop_end);
  3497. }
  3498. /* A callback for the hw-doloop pass. This function examines INSN; if
  3499. it is a doloop_end pattern we recognize, return the reg rtx for the
  3500. loop counter. Otherwise, return NULL_RTX. */
  3501. static rtx
  3502. hwloop_pattern_reg (rtx_insn *insn)
  3503. {
  3504. rtx reg;
  3505. if (!JUMP_P (insn) || recog_memoized (insn) != CODE_FOR_loop_end)
  3506. return NULL_RTX;
  3507. reg = SET_DEST (XVECEXP (PATTERN (insn), 0, 1));
  3508. if (!REG_P (reg))
  3509. return NULL_RTX;
  3510. return reg;
  3511. }
  3512. static struct hw_doloop_hooks xtensa_doloop_hooks =
  3513. {
  3514. hwloop_pattern_reg,
  3515. hwloop_optimize,
  3516. hwloop_fail
  3517. };
  3518. /* Run from machine_dependent_reorg, this pass looks for doloop_end insns
  3519. and tries to rewrite the RTL of these loops so that proper Xtensa
  3520. hardware loops are generated. */
  3521. static void
  3522. xtensa_reorg_loops (void)
  3523. {
  3524. reorg_loops (false, &xtensa_doloop_hooks);
  3525. }
  3526. #else
  3527. static inline void
  3528. xtensa_reorg_loops (void)
  3529. {
  3530. }
  3531. #endif
  3532. /* Implement the TARGET_MACHINE_DEPENDENT_REORG pass. */
  3533. static void
  3534. xtensa_reorg (void)
  3535. {
  3536. /* We are freeing block_for_insn in the toplev to keep compatibility
  3537. with old MDEP_REORGS that are not CFG based. Recompute it now. */
  3538. compute_bb_for_insn ();
  3539. df_analyze ();
  3540. /* Doloop optimization. */
  3541. xtensa_reorg_loops ();
  3542. }
  3543. /* Update register usage after having seen the compiler flags. */
  3544. static void
  3545. xtensa_conditional_register_usage (void)
  3546. {
  3547. unsigned i, c_mask;
  3548. c_mask = TARGET_WINDOWED_ABI ? (1 << 1) : (1 << 2);
  3549. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  3550. {
  3551. /* Set/reset conditionally defined registers from
  3552. CALL_USED_REGISTERS initializer. */
  3553. if (call_used_regs[i] > 1)
  3554. call_used_regs[i] = !!(call_used_regs[i] & c_mask);
  3555. }
  3556. /* Remove hard FP register from the preferred reload registers set. */
  3557. CLEAR_HARD_REG_BIT (reg_class_contents[(int)RL_REGS],
  3558. HARD_FRAME_POINTER_REGNUM);
  3559. }
  3560. /* Map hard register number to register class */
  3561. enum reg_class xtensa_regno_to_class (int regno)
  3562. {
  3563. static const enum reg_class regno_to_class[FIRST_PSEUDO_REGISTER] =
  3564. {
  3565. RL_REGS, SP_REG, RL_REGS, RL_REGS,
  3566. RL_REGS, RL_REGS, RL_REGS, RL_REGS,
  3567. RL_REGS, RL_REGS, RL_REGS, RL_REGS,
  3568. RL_REGS, RL_REGS, RL_REGS, RL_REGS,
  3569. AR_REGS, AR_REGS, BR_REGS,
  3570. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  3571. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  3572. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  3573. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  3574. ACC_REG,
  3575. };
  3576. if (regno == HARD_FRAME_POINTER_REGNUM)
  3577. return GR_REGS;
  3578. else
  3579. return regno_to_class[regno];
  3580. }
  3581. #include "gt-xtensa.h"