gensupport.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987
  1. /* Support routines for the various generation passes.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "bconfig.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "rtl.h"
  20. #include "obstack.h"
  21. #include "errors.h"
  22. #include "hashtab.h"
  23. #include "read-md.h"
  24. #include "gensupport.h"
  25. #define MAX_OPERANDS 40
  26. static rtx operand_data[MAX_OPERANDS];
  27. static rtx match_operand_entries_in_pattern[MAX_OPERANDS];
  28. static char used_operands_numbers[MAX_OPERANDS];
  29. /* In case some macros used by files we include need it, define this here. */
  30. int target_flags;
  31. int insn_elision = 1;
  32. static struct obstack obstack;
  33. struct obstack *rtl_obstack = &obstack;
  34. /* Counter for patterns that generate code: define_insn, define_expand,
  35. define_split, define_peephole, and define_peephole2. See read_md_rtx().
  36. Any define_insn_and_splits are already in separate queues so that the
  37. insn and the splitter get a unique number also. */
  38. static int sequence_num;
  39. static int predicable_default;
  40. static const char *predicable_true;
  41. static const char *predicable_false;
  42. static const char *subst_true = "yes";
  43. static const char *subst_false = "no";
  44. static htab_t condition_table;
  45. /* We initially queue all patterns, process the define_insn,
  46. define_cond_exec and define_subst patterns, then return
  47. them one at a time. */
  48. struct queue_elem
  49. {
  50. rtx data;
  51. const char *filename;
  52. int lineno;
  53. struct queue_elem *next;
  54. /* In a DEFINE_INSN that came from a DEFINE_INSN_AND_SPLIT, SPLIT
  55. points to the generated DEFINE_SPLIT. */
  56. struct queue_elem *split;
  57. };
  58. #define MNEMONIC_ATTR_NAME "mnemonic"
  59. #define MNEMONIC_HTAB_SIZE 1024
  60. static struct queue_elem *define_attr_queue;
  61. static struct queue_elem **define_attr_tail = &define_attr_queue;
  62. static struct queue_elem *define_pred_queue;
  63. static struct queue_elem **define_pred_tail = &define_pred_queue;
  64. static struct queue_elem *define_insn_queue;
  65. static struct queue_elem **define_insn_tail = &define_insn_queue;
  66. static struct queue_elem *define_cond_exec_queue;
  67. static struct queue_elem **define_cond_exec_tail = &define_cond_exec_queue;
  68. static struct queue_elem *define_subst_queue;
  69. static struct queue_elem **define_subst_tail = &define_subst_queue;
  70. static struct queue_elem *other_queue;
  71. static struct queue_elem **other_tail = &other_queue;
  72. static struct queue_elem *define_subst_attr_queue;
  73. static struct queue_elem **define_subst_attr_tail = &define_subst_attr_queue;
  74. static struct queue_elem *queue_pattern (rtx, struct queue_elem ***,
  75. const char *, int);
  76. static void remove_constraints (rtx);
  77. static void process_rtx (rtx, int);
  78. static int is_predicable (struct queue_elem *);
  79. static void identify_predicable_attribute (void);
  80. static int n_alternatives (const char *);
  81. static void collect_insn_data (rtx, int *, int *);
  82. static rtx alter_predicate_for_insn (rtx, int, int, int);
  83. static const char *alter_test_for_insn (struct queue_elem *,
  84. struct queue_elem *);
  85. static char *shift_output_template (char *, const char *, int);
  86. static const char *alter_output_for_insn (struct queue_elem *,
  87. struct queue_elem *,
  88. int, int);
  89. static void process_one_cond_exec (struct queue_elem *);
  90. static void process_define_cond_exec (void);
  91. static void init_predicate_table (void);
  92. static void record_insn_name (int, const char *);
  93. static bool has_subst_attribute (struct queue_elem *, struct queue_elem *);
  94. static bool subst_pattern_match (rtx, rtx, int);
  95. static int get_alternatives_number (rtx, int *, int);
  96. static const char * alter_output_for_subst_insn (rtx, int);
  97. static void alter_attrs_for_subst_insn (struct queue_elem *, int);
  98. static void process_substs_on_one_elem (struct queue_elem *,
  99. struct queue_elem *);
  100. static rtx subst_dup (rtx, int, int);
  101. static void process_define_subst (void);
  102. static const char * duplicate_alternatives (const char *, int);
  103. static const char * duplicate_each_alternative (const char * str, int n_dup);
  104. typedef const char * (*constraints_handler_t) (const char *, int);
  105. static rtx alter_constraints (rtx, int, constraints_handler_t);
  106. static rtx adjust_operands_numbers (rtx);
  107. static rtx replace_duplicating_operands_in_pattern (rtx);
  108. /* Make a version of gen_rtx_CONST_INT so that GEN_INT can be used in
  109. the gensupport programs. */
  110. rtx
  111. gen_rtx_CONST_INT (machine_mode ARG_UNUSED (mode),
  112. HOST_WIDE_INT arg)
  113. {
  114. rtx rt = rtx_alloc (CONST_INT);
  115. XWINT (rt, 0) = arg;
  116. return rt;
  117. }
  118. /* Predicate handling.
  119. We construct from the machine description a table mapping each
  120. predicate to a list of the rtl codes it can possibly match. The
  121. function 'maybe_both_true' uses it to deduce that there are no
  122. expressions that can be matches by certain pairs of tree nodes.
  123. Also, if a predicate can match only one code, we can hardwire that
  124. code into the node testing the predicate.
  125. Some predicates are flagged as special. validate_pattern will not
  126. warn about modeless match_operand expressions if they have a
  127. special predicate. Predicates that allow only constants are also
  128. treated as special, for this purpose.
  129. validate_pattern will warn about predicates that allow non-lvalues
  130. when they appear in destination operands.
  131. Calculating the set of rtx codes that can possibly be accepted by a
  132. predicate expression EXP requires a three-state logic: any given
  133. subexpression may definitively accept a code C (Y), definitively
  134. reject a code C (N), or may have an indeterminate effect (I). N
  135. and I is N; Y or I is Y; Y and I, N or I are both I. Here are full
  136. truth tables.
  137. a b a&b a|b
  138. Y Y Y Y
  139. N Y N Y
  140. N N N N
  141. I Y I Y
  142. I N N I
  143. I I I I
  144. We represent Y with 1, N with 0, I with 2. If any code is left in
  145. an I state by the complete expression, we must assume that that
  146. code can be accepted. */
  147. #define N 0
  148. #define Y 1
  149. #define I 2
  150. #define TRISTATE_AND(a,b) \
  151. ((a) == I ? ((b) == N ? N : I) : \
  152. (b) == I ? ((a) == N ? N : I) : \
  153. (a) && (b))
  154. #define TRISTATE_OR(a,b) \
  155. ((a) == I ? ((b) == Y ? Y : I) : \
  156. (b) == I ? ((a) == Y ? Y : I) : \
  157. (a) || (b))
  158. #define TRISTATE_NOT(a) \
  159. ((a) == I ? I : !(a))
  160. /* 0 means no warning about that code yet, 1 means warned. */
  161. static char did_you_mean_codes[NUM_RTX_CODE];
  162. /* Recursively calculate the set of rtx codes accepted by the
  163. predicate expression EXP, writing the result to CODES. LINENO is
  164. the line number on which the directive containing EXP appeared. */
  165. static void
  166. compute_predicate_codes (rtx exp, int lineno, char codes[NUM_RTX_CODE])
  167. {
  168. char op0_codes[NUM_RTX_CODE];
  169. char op1_codes[NUM_RTX_CODE];
  170. char op2_codes[NUM_RTX_CODE];
  171. int i;
  172. switch (GET_CODE (exp))
  173. {
  174. case AND:
  175. compute_predicate_codes (XEXP (exp, 0), lineno, op0_codes);
  176. compute_predicate_codes (XEXP (exp, 1), lineno, op1_codes);
  177. for (i = 0; i < NUM_RTX_CODE; i++)
  178. codes[i] = TRISTATE_AND (op0_codes[i], op1_codes[i]);
  179. break;
  180. case IOR:
  181. compute_predicate_codes (XEXP (exp, 0), lineno, op0_codes);
  182. compute_predicate_codes (XEXP (exp, 1), lineno, op1_codes);
  183. for (i = 0; i < NUM_RTX_CODE; i++)
  184. codes[i] = TRISTATE_OR (op0_codes[i], op1_codes[i]);
  185. break;
  186. case NOT:
  187. compute_predicate_codes (XEXP (exp, 0), lineno, op0_codes);
  188. for (i = 0; i < NUM_RTX_CODE; i++)
  189. codes[i] = TRISTATE_NOT (op0_codes[i]);
  190. break;
  191. case IF_THEN_ELSE:
  192. /* a ? b : c accepts the same codes as (a & b) | (!a & c). */
  193. compute_predicate_codes (XEXP (exp, 0), lineno, op0_codes);
  194. compute_predicate_codes (XEXP (exp, 1), lineno, op1_codes);
  195. compute_predicate_codes (XEXP (exp, 2), lineno, op2_codes);
  196. for (i = 0; i < NUM_RTX_CODE; i++)
  197. codes[i] = TRISTATE_OR (TRISTATE_AND (op0_codes[i], op1_codes[i]),
  198. TRISTATE_AND (TRISTATE_NOT (op0_codes[i]),
  199. op2_codes[i]));
  200. break;
  201. case MATCH_CODE:
  202. /* MATCH_CODE allows a specified list of codes. However, if it
  203. does not apply to the top level of the expression, it does not
  204. constrain the set of codes for the top level. */
  205. if (XSTR (exp, 1)[0] != '\0')
  206. {
  207. memset (codes, Y, NUM_RTX_CODE);
  208. break;
  209. }
  210. memset (codes, N, NUM_RTX_CODE);
  211. {
  212. const char *next_code = XSTR (exp, 0);
  213. const char *code;
  214. if (*next_code == '\0')
  215. {
  216. error_with_line (lineno, "empty match_code expression");
  217. break;
  218. }
  219. while ((code = scan_comma_elt (&next_code)) != 0)
  220. {
  221. size_t n = next_code - code;
  222. int found_it = 0;
  223. for (i = 0; i < NUM_RTX_CODE; i++)
  224. if (!strncmp (code, GET_RTX_NAME (i), n)
  225. && GET_RTX_NAME (i)[n] == '\0')
  226. {
  227. codes[i] = Y;
  228. found_it = 1;
  229. break;
  230. }
  231. if (!found_it)
  232. {
  233. error_with_line (lineno,
  234. "match_code \"%.*s\" matches nothing",
  235. (int) n, code);
  236. for (i = 0; i < NUM_RTX_CODE; i++)
  237. if (!strncasecmp (code, GET_RTX_NAME (i), n)
  238. && GET_RTX_NAME (i)[n] == '\0'
  239. && !did_you_mean_codes[i])
  240. {
  241. did_you_mean_codes[i] = 1;
  242. message_with_line (lineno, "(did you mean \"%s\"?)",
  243. GET_RTX_NAME (i));
  244. }
  245. }
  246. }
  247. }
  248. break;
  249. case MATCH_OPERAND:
  250. /* MATCH_OPERAND disallows the set of codes that the named predicate
  251. disallows, and is indeterminate for the codes that it does allow. */
  252. {
  253. struct pred_data *p = lookup_predicate (XSTR (exp, 1));
  254. if (!p)
  255. {
  256. error_with_line (lineno, "reference to unknown predicate '%s'",
  257. XSTR (exp, 1));
  258. break;
  259. }
  260. for (i = 0; i < NUM_RTX_CODE; i++)
  261. codes[i] = p->codes[i] ? I : N;
  262. }
  263. break;
  264. case MATCH_TEST:
  265. /* (match_test WHATEVER) is completely indeterminate. */
  266. memset (codes, I, NUM_RTX_CODE);
  267. break;
  268. default:
  269. error_with_line (lineno,
  270. "'%s' cannot be used in a define_predicate expression",
  271. GET_RTX_NAME (GET_CODE (exp)));
  272. memset (codes, I, NUM_RTX_CODE);
  273. break;
  274. }
  275. }
  276. #undef TRISTATE_OR
  277. #undef TRISTATE_AND
  278. #undef TRISTATE_NOT
  279. /* Return true if NAME is a valid predicate name. */
  280. static bool
  281. valid_predicate_name_p (const char *name)
  282. {
  283. const char *p;
  284. if (!ISALPHA (name[0]) && name[0] != '_')
  285. return false;
  286. for (p = name + 1; *p; p++)
  287. if (!ISALNUM (*p) && *p != '_')
  288. return false;
  289. return true;
  290. }
  291. /* Process define_predicate directive DESC, which appears on line number
  292. LINENO. Compute the set of codes that can be matched, and record this
  293. as a known predicate. */
  294. static void
  295. process_define_predicate (rtx desc, int lineno)
  296. {
  297. struct pred_data *pred;
  298. char codes[NUM_RTX_CODE];
  299. int i;
  300. if (!valid_predicate_name_p (XSTR (desc, 0)))
  301. {
  302. error_with_line (lineno,
  303. "%s: predicate name must be a valid C function name",
  304. XSTR (desc, 0));
  305. return;
  306. }
  307. pred = XCNEW (struct pred_data);
  308. pred->name = XSTR (desc, 0);
  309. pred->exp = XEXP (desc, 1);
  310. pred->c_block = XSTR (desc, 2);
  311. if (GET_CODE (desc) == DEFINE_SPECIAL_PREDICATE)
  312. pred->special = true;
  313. compute_predicate_codes (XEXP (desc, 1), lineno, codes);
  314. for (i = 0; i < NUM_RTX_CODE; i++)
  315. if (codes[i] != N)
  316. add_predicate_code (pred, (enum rtx_code) i);
  317. add_predicate (pred);
  318. }
  319. #undef I
  320. #undef N
  321. #undef Y
  322. /* Queue PATTERN on LIST_TAIL. Return the address of the new queue
  323. element. */
  324. static struct queue_elem *
  325. queue_pattern (rtx pattern, struct queue_elem ***list_tail,
  326. const char *filename, int lineno)
  327. {
  328. struct queue_elem *e = XNEW (struct queue_elem);
  329. e->data = pattern;
  330. e->filename = filename;
  331. e->lineno = lineno;
  332. e->next = NULL;
  333. e->split = NULL;
  334. **list_tail = e;
  335. *list_tail = &e->next;
  336. return e;
  337. }
  338. /* Remove element ELEM from QUEUE. */
  339. static void
  340. remove_from_queue (struct queue_elem *elem, struct queue_elem **queue)
  341. {
  342. struct queue_elem *prev, *e;
  343. prev = NULL;
  344. for (e = *queue; e ; e = e->next)
  345. {
  346. if (e == elem)
  347. break;
  348. prev = e;
  349. }
  350. if (e == NULL)
  351. return;
  352. if (prev)
  353. prev->next = elem->next;
  354. else
  355. *queue = elem->next;
  356. }
  357. /* Build a define_attr for an binary attribute with name NAME and
  358. possible values "yes" and "no", and queue it. */
  359. static void
  360. add_define_attr (const char *name)
  361. {
  362. struct queue_elem *e = XNEW (struct queue_elem);
  363. rtx t1 = rtx_alloc (DEFINE_ATTR);
  364. XSTR (t1, 0) = name;
  365. XSTR (t1, 1) = "no,yes";
  366. XEXP (t1, 2) = rtx_alloc (CONST_STRING);
  367. XSTR (XEXP (t1, 2), 0) = "yes";
  368. e->data = t1;
  369. e->filename = "built-in";
  370. e->lineno = -1;
  371. e->next = define_attr_queue;
  372. define_attr_queue = e;
  373. }
  374. /* Recursively remove constraints from an rtx. */
  375. static void
  376. remove_constraints (rtx part)
  377. {
  378. int i, j;
  379. const char *format_ptr;
  380. if (part == 0)
  381. return;
  382. if (GET_CODE (part) == MATCH_OPERAND)
  383. XSTR (part, 2) = "";
  384. else if (GET_CODE (part) == MATCH_SCRATCH)
  385. XSTR (part, 1) = "";
  386. format_ptr = GET_RTX_FORMAT (GET_CODE (part));
  387. for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
  388. switch (*format_ptr++)
  389. {
  390. case 'e':
  391. case 'u':
  392. remove_constraints (XEXP (part, i));
  393. break;
  394. case 'E':
  395. if (XVEC (part, i) != NULL)
  396. for (j = 0; j < XVECLEN (part, i); j++)
  397. remove_constraints (XVECEXP (part, i, j));
  398. break;
  399. }
  400. }
  401. /* Process a top level rtx in some way, queuing as appropriate. */
  402. static void
  403. process_rtx (rtx desc, int lineno)
  404. {
  405. switch (GET_CODE (desc))
  406. {
  407. case DEFINE_INSN:
  408. queue_pattern (desc, &define_insn_tail, read_md_filename, lineno);
  409. break;
  410. case DEFINE_COND_EXEC:
  411. queue_pattern (desc, &define_cond_exec_tail, read_md_filename, lineno);
  412. break;
  413. case DEFINE_SUBST:
  414. queue_pattern (desc, &define_subst_tail, read_md_filename, lineno);
  415. break;
  416. case DEFINE_SUBST_ATTR:
  417. queue_pattern (desc, &define_subst_attr_tail, read_md_filename, lineno);
  418. break;
  419. case DEFINE_ATTR:
  420. case DEFINE_ENUM_ATTR:
  421. queue_pattern (desc, &define_attr_tail, read_md_filename, lineno);
  422. break;
  423. case DEFINE_PREDICATE:
  424. case DEFINE_SPECIAL_PREDICATE:
  425. process_define_predicate (desc, lineno);
  426. /* Fall through. */
  427. case DEFINE_CONSTRAINT:
  428. case DEFINE_REGISTER_CONSTRAINT:
  429. case DEFINE_MEMORY_CONSTRAINT:
  430. case DEFINE_ADDRESS_CONSTRAINT:
  431. queue_pattern (desc, &define_pred_tail, read_md_filename, lineno);
  432. break;
  433. case DEFINE_INSN_AND_SPLIT:
  434. {
  435. const char *split_cond;
  436. rtx split;
  437. rtvec attr;
  438. int i;
  439. struct queue_elem *insn_elem;
  440. struct queue_elem *split_elem;
  441. /* Create a split with values from the insn_and_split. */
  442. split = rtx_alloc (DEFINE_SPLIT);
  443. i = XVECLEN (desc, 1);
  444. XVEC (split, 0) = rtvec_alloc (i);
  445. while (--i >= 0)
  446. {
  447. XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
  448. remove_constraints (XVECEXP (split, 0, i));
  449. }
  450. /* If the split condition starts with "&&", append it to the
  451. insn condition to create the new split condition. */
  452. split_cond = XSTR (desc, 4);
  453. if (split_cond[0] == '&' && split_cond[1] == '&')
  454. {
  455. copy_md_ptr_loc (split_cond + 2, split_cond);
  456. split_cond = join_c_conditions (XSTR (desc, 2), split_cond + 2);
  457. }
  458. XSTR (split, 1) = split_cond;
  459. XVEC (split, 2) = XVEC (desc, 5);
  460. XSTR (split, 3) = XSTR (desc, 6);
  461. /* Fix up the DEFINE_INSN. */
  462. attr = XVEC (desc, 7);
  463. PUT_CODE (desc, DEFINE_INSN);
  464. XVEC (desc, 4) = attr;
  465. /* Queue them. */
  466. insn_elem
  467. = queue_pattern (desc, &define_insn_tail, read_md_filename,
  468. lineno);
  469. split_elem
  470. = queue_pattern (split, &other_tail, read_md_filename, lineno);
  471. insn_elem->split = split_elem;
  472. break;
  473. }
  474. default:
  475. queue_pattern (desc, &other_tail, read_md_filename, lineno);
  476. break;
  477. }
  478. }
  479. /* Return true if attribute PREDICABLE is true for ELEM, which holds
  480. a DEFINE_INSN. */
  481. static int
  482. is_predicable (struct queue_elem *elem)
  483. {
  484. rtvec vec = XVEC (elem->data, 4);
  485. const char *value;
  486. int i;
  487. if (! vec)
  488. return predicable_default;
  489. for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
  490. {
  491. rtx sub = RTVEC_ELT (vec, i);
  492. switch (GET_CODE (sub))
  493. {
  494. case SET_ATTR:
  495. if (strcmp (XSTR (sub, 0), "predicable") == 0)
  496. {
  497. value = XSTR (sub, 1);
  498. goto found;
  499. }
  500. break;
  501. case SET_ATTR_ALTERNATIVE:
  502. if (strcmp (XSTR (sub, 0), "predicable") == 0)
  503. {
  504. error_with_line (elem->lineno,
  505. "multiple alternatives for `predicable'");
  506. return 0;
  507. }
  508. break;
  509. case SET:
  510. if (GET_CODE (SET_DEST (sub)) != ATTR
  511. || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
  512. break;
  513. sub = SET_SRC (sub);
  514. if (GET_CODE (sub) == CONST_STRING)
  515. {
  516. value = XSTR (sub, 0);
  517. goto found;
  518. }
  519. /* ??? It would be possible to handle this if we really tried.
  520. It's not easy though, and I'm not going to bother until it
  521. really proves necessary. */
  522. error_with_line (elem->lineno,
  523. "non-constant value for `predicable'");
  524. return 0;
  525. default:
  526. gcc_unreachable ();
  527. }
  528. }
  529. return predicable_default;
  530. found:
  531. /* Find out which value we're looking at. Multiple alternatives means at
  532. least one is predicable. */
  533. if (strchr (value, ',') != NULL)
  534. return 1;
  535. if (strcmp (value, predicable_true) == 0)
  536. return 1;
  537. if (strcmp (value, predicable_false) == 0)
  538. return 0;
  539. error_with_line (elem->lineno,
  540. "unknown value `%s' for `predicable' attribute", value);
  541. return 0;
  542. }
  543. /* Find attribute SUBST in ELEM and assign NEW_VALUE to it. */
  544. static void
  545. change_subst_attribute (struct queue_elem *elem,
  546. struct queue_elem *subst_elem,
  547. const char *new_value)
  548. {
  549. rtvec attrs_vec = XVEC (elem->data, 4);
  550. const char *subst_name = XSTR (subst_elem->data, 0);
  551. int i;
  552. if (! attrs_vec)
  553. return;
  554. for (i = GET_NUM_ELEM (attrs_vec) - 1; i >= 0; --i)
  555. {
  556. rtx cur_attr = RTVEC_ELT (attrs_vec, i);
  557. if (GET_CODE (cur_attr) != SET_ATTR)
  558. continue;
  559. if (strcmp (XSTR (cur_attr, 0), subst_name) == 0)
  560. {
  561. XSTR (cur_attr, 1) = new_value;
  562. return;
  563. }
  564. }
  565. }
  566. /* Return true if ELEM has the attribute with the name of DEFINE_SUBST
  567. represented by SUBST_ELEM and this attribute has value SUBST_TRUE.
  568. DEFINE_SUBST isn't applied to patterns without such attribute. In other
  569. words, we suppose the default value of the attribute to be 'no' since it is
  570. always generated automaticaly in read-rtl.c. */
  571. static bool
  572. has_subst_attribute (struct queue_elem *elem, struct queue_elem *subst_elem)
  573. {
  574. rtvec attrs_vec = XVEC (elem->data, 4);
  575. const char *value, *subst_name = XSTR (subst_elem->data, 0);
  576. int i;
  577. if (! attrs_vec)
  578. return false;
  579. for (i = GET_NUM_ELEM (attrs_vec) - 1; i >= 0; --i)
  580. {
  581. rtx cur_attr = RTVEC_ELT (attrs_vec, i);
  582. switch (GET_CODE (cur_attr))
  583. {
  584. case SET_ATTR:
  585. if (strcmp (XSTR (cur_attr, 0), subst_name) == 0)
  586. {
  587. value = XSTR (cur_attr, 1);
  588. goto found;
  589. }
  590. break;
  591. case SET:
  592. if (GET_CODE (SET_DEST (cur_attr)) != ATTR
  593. || strcmp (XSTR (SET_DEST (cur_attr), 0), subst_name) != 0)
  594. break;
  595. cur_attr = SET_SRC (cur_attr);
  596. if (GET_CODE (cur_attr) == CONST_STRING)
  597. {
  598. value = XSTR (cur_attr, 0);
  599. goto found;
  600. }
  601. /* Only (set_attr "subst" "yes/no") and
  602. (set (attr "subst" (const_string "yes/no")))
  603. are currently allowed. */
  604. error_with_line (elem->lineno,
  605. "unsupported value for `%s'", subst_name);
  606. return false;
  607. case SET_ATTR_ALTERNATIVE:
  608. error_with_line (elem->lineno,
  609. "%s: `set_attr_alternative' is unsupported by "
  610. "`define_subst'",
  611. XSTR (elem->data, 0));
  612. return false;
  613. default:
  614. gcc_unreachable ();
  615. }
  616. }
  617. return false;
  618. found:
  619. if (strcmp (value, subst_true) == 0)
  620. return true;
  621. if (strcmp (value, subst_false) == 0)
  622. return false;
  623. error_with_line (elem->lineno,
  624. "unknown value `%s' for `%s' attribute", value, subst_name);
  625. return false;
  626. }
  627. /* Compare RTL-template of original define_insn X to input RTL-template of
  628. define_subst PT. Return 1 if the templates match, 0 otherwise.
  629. During the comparison, the routine also fills global_array OPERAND_DATA. */
  630. static bool
  631. subst_pattern_match (rtx x, rtx pt, int lineno)
  632. {
  633. RTX_CODE code, code_pt;
  634. int i, j, len;
  635. const char *fmt, *pred_name;
  636. code = GET_CODE (x);
  637. code_pt = GET_CODE (pt);
  638. if (code_pt == MATCH_OPERAND)
  639. {
  640. /* MATCH_DUP, and MATCH_OP_DUP don't have a specified mode, so we
  641. always accept them. */
  642. if (GET_MODE (pt) != VOIDmode && GET_MODE (x) != GET_MODE (pt)
  643. && (code != MATCH_DUP && code != MATCH_OP_DUP))
  644. return false; /* Modes don't match. */
  645. if (code == MATCH_OPERAND)
  646. {
  647. pred_name = XSTR (pt, 1);
  648. if (pred_name[0] != 0)
  649. {
  650. const struct pred_data *pred_pt = lookup_predicate (pred_name);
  651. if (!pred_pt || pred_pt != lookup_predicate (XSTR (x, 1)))
  652. return false; /* Predicates don't match. */
  653. }
  654. }
  655. gcc_assert (XINT (pt, 0) >= 0 && XINT (pt, 0) < MAX_OPERANDS);
  656. operand_data[XINT (pt, 0)] = x;
  657. return true;
  658. }
  659. if (code_pt == MATCH_OPERATOR)
  660. {
  661. int x_vecexp_pos = -1;
  662. /* Compare modes. */
  663. if (GET_MODE (pt) != VOIDmode && GET_MODE (x) != GET_MODE (pt))
  664. return false;
  665. /* In case X is also match_operator, compare predicates. */
  666. if (code == MATCH_OPERATOR)
  667. {
  668. pred_name = XSTR (pt, 1);
  669. if (pred_name[0] != 0)
  670. {
  671. const struct pred_data *pred_pt = lookup_predicate (pred_name);
  672. if (!pred_pt || pred_pt != lookup_predicate (XSTR (x, 1)))
  673. return false;
  674. }
  675. }
  676. /* Compare operands.
  677. MATCH_OPERATOR in input template could match in original template
  678. either 1) MATCH_OPERAND, 2) UNSPEC, 3) ordinary operation (like PLUS).
  679. In the first case operands are at (XVECEXP (x, 2, j)), in the second
  680. - at (XVECEXP (x, 0, j)), in the last one - (XEXP (x, j)).
  681. X_VECEXP_POS variable shows, where to look for these operands. */
  682. if (code == UNSPEC
  683. || code == UNSPEC_VOLATILE)
  684. x_vecexp_pos = 0;
  685. else if (code == MATCH_OPERATOR)
  686. x_vecexp_pos = 2;
  687. else
  688. x_vecexp_pos = -1;
  689. /* MATCH_OPERATOR or UNSPEC case. */
  690. if (x_vecexp_pos >= 0)
  691. {
  692. /* Compare operands number in X and PT. */
  693. if (XVECLEN (x, x_vecexp_pos) != XVECLEN (pt, 2))
  694. return false;
  695. for (j = 0; j < XVECLEN (pt, 2); j++)
  696. if (!subst_pattern_match (XVECEXP (x, x_vecexp_pos, j),
  697. XVECEXP (pt, 2, j), lineno))
  698. return false;
  699. }
  700. /* Ordinary operator. */
  701. else
  702. {
  703. /* Compare operands number in X and PT.
  704. We count operands differently for X and PT since we compare
  705. an operator (with operands directly in RTX) and MATCH_OPERATOR
  706. (that has a vector with operands). */
  707. if (GET_RTX_LENGTH (code) != XVECLEN (pt, 2))
  708. return false;
  709. for (j = 0; j < XVECLEN (pt, 2); j++)
  710. if (!subst_pattern_match (XEXP (x, j), XVECEXP (pt, 2, j), lineno))
  711. return false;
  712. }
  713. /* Store the operand to OPERAND_DATA array. */
  714. gcc_assert (XINT (pt, 0) >= 0 && XINT (pt, 0) < MAX_OPERANDS);
  715. operand_data[XINT (pt, 0)] = x;
  716. return true;
  717. }
  718. if (code_pt == MATCH_PAR_DUP
  719. || code_pt == MATCH_DUP
  720. || code_pt == MATCH_OP_DUP
  721. || code_pt == MATCH_SCRATCH
  722. || code_pt == MATCH_PARALLEL)
  723. {
  724. /* Currently interface for these constructions isn't defined -
  725. probably they aren't needed in input template of define_subst at all.
  726. So, for now their usage in define_subst is forbidden. */
  727. error_with_line (lineno, "%s cannot be used in define_subst",
  728. GET_RTX_NAME (code_pt));
  729. }
  730. gcc_assert (code != MATCH_PAR_DUP
  731. && code_pt != MATCH_DUP
  732. && code_pt != MATCH_OP_DUP
  733. && code_pt != MATCH_SCRATCH
  734. && code_pt != MATCH_PARALLEL
  735. && code_pt != MATCH_OPERAND
  736. && code_pt != MATCH_OPERATOR);
  737. /* If PT is none of the handled above, then we match only expressions with
  738. the same code in X. */
  739. if (code != code_pt)
  740. return false;
  741. fmt = GET_RTX_FORMAT (code_pt);
  742. len = GET_RTX_LENGTH (code_pt);
  743. for (i = 0; i < len; i++)
  744. {
  745. if (fmt[i] == '0')
  746. break;
  747. switch (fmt[i])
  748. {
  749. case 'i': case 'w': case 's':
  750. continue;
  751. case 'e': case 'u':
  752. if (!subst_pattern_match (XEXP (x, i), XEXP (pt, i), lineno))
  753. return false;
  754. break;
  755. case 'E':
  756. {
  757. if (XVECLEN (x, i) != XVECLEN (pt, i))
  758. return false;
  759. for (j = 0; j < XVECLEN (pt, i); j++)
  760. if (!subst_pattern_match (XVECEXP (x, i, j), XVECEXP (pt, i, j),
  761. lineno))
  762. return false;
  763. break;
  764. }
  765. default:
  766. gcc_unreachable ();
  767. }
  768. }
  769. return true;
  770. }
  771. /* Examine the attribute "predicable"; discover its boolean values
  772. and its default. */
  773. static void
  774. identify_predicable_attribute (void)
  775. {
  776. struct queue_elem *elem;
  777. char *p_true, *p_false;
  778. const char *value;
  779. /* Look for the DEFINE_ATTR for `predicable', which must exist. */
  780. for (elem = define_attr_queue; elem ; elem = elem->next)
  781. if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
  782. goto found;
  783. error_with_line (define_cond_exec_queue->lineno,
  784. "attribute `predicable' not defined");
  785. return;
  786. found:
  787. value = XSTR (elem->data, 1);
  788. p_false = xstrdup (value);
  789. p_true = strchr (p_false, ',');
  790. if (p_true == NULL || strchr (++p_true, ',') != NULL)
  791. {
  792. error_with_line (elem->lineno, "attribute `predicable' is not a boolean");
  793. free (p_false);
  794. return;
  795. }
  796. p_true[-1] = '\0';
  797. predicable_true = p_true;
  798. predicable_false = p_false;
  799. switch (GET_CODE (XEXP (elem->data, 2)))
  800. {
  801. case CONST_STRING:
  802. value = XSTR (XEXP (elem->data, 2), 0);
  803. break;
  804. case CONST:
  805. error_with_line (elem->lineno, "attribute `predicable' cannot be const");
  806. free (p_false);
  807. return;
  808. default:
  809. error_with_line (elem->lineno,
  810. "attribute `predicable' must have a constant default");
  811. free (p_false);
  812. return;
  813. }
  814. if (strcmp (value, p_true) == 0)
  815. predicable_default = 1;
  816. else if (strcmp (value, p_false) == 0)
  817. predicable_default = 0;
  818. else
  819. {
  820. error_with_line (elem->lineno,
  821. "unknown value `%s' for `predicable' attribute", value);
  822. free (p_false);
  823. }
  824. }
  825. /* Return the number of alternatives in constraint S. */
  826. static int
  827. n_alternatives (const char *s)
  828. {
  829. int n = 1;
  830. if (s)
  831. while (*s)
  832. n += (*s++ == ',');
  833. return n;
  834. }
  835. /* The routine scans rtl PATTERN, find match_operand in it and counts
  836. number of alternatives. If PATTERN contains several match_operands
  837. with different number of alternatives, error is emitted, and the
  838. routine returns 0. If all match_operands in PATTERN have the same
  839. number of alternatives, it's stored in N_ALT, and the routine returns 1.
  840. Argument LINENO is used in when the error is emitted. */
  841. static int
  842. get_alternatives_number (rtx pattern, int *n_alt, int lineno)
  843. {
  844. const char *fmt;
  845. enum rtx_code code;
  846. int i, j, len;
  847. if (!n_alt)
  848. return 0;
  849. code = GET_CODE (pattern);
  850. switch (code)
  851. {
  852. case MATCH_OPERAND:
  853. i = n_alternatives (XSTR (pattern, 2));
  854. /* n_alternatives returns 1 if constraint string is empty -
  855. here we fix it up. */
  856. if (!*(XSTR (pattern, 2)))
  857. i = 0;
  858. if (*n_alt <= 0)
  859. *n_alt = i;
  860. else if (i && i != *n_alt)
  861. {
  862. error_with_line (lineno,
  863. "wrong number of alternatives in operand %d",
  864. XINT (pattern, 0));
  865. return 0;
  866. }
  867. default:
  868. break;
  869. }
  870. fmt = GET_RTX_FORMAT (code);
  871. len = GET_RTX_LENGTH (code);
  872. for (i = 0; i < len; i++)
  873. {
  874. switch (fmt[i])
  875. {
  876. case 'e': case 'u':
  877. if (!get_alternatives_number (XEXP (pattern, i), n_alt, lineno))
  878. return 0;
  879. break;
  880. case 'V':
  881. if (XVEC (pattern, i) == NULL)
  882. break;
  883. case 'E':
  884. for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
  885. if (!get_alternatives_number (XVECEXP (pattern, i, j),
  886. n_alt, lineno))
  887. return 0;
  888. break;
  889. case 'i': case 'w': case '0': case 's': case 'S': case 'T':
  890. break;
  891. default:
  892. gcc_unreachable ();
  893. }
  894. }
  895. return 1;
  896. }
  897. /* Determine how many alternatives there are in INSN, and how many
  898. operands. */
  899. static void
  900. collect_insn_data (rtx pattern, int *palt, int *pmax)
  901. {
  902. const char *fmt;
  903. enum rtx_code code;
  904. int i, j, len;
  905. code = GET_CODE (pattern);
  906. switch (code)
  907. {
  908. case MATCH_OPERAND:
  909. i = n_alternatives (XSTR (pattern, 2));
  910. *palt = (i > *palt ? i : *palt);
  911. /* Fall through. */
  912. case MATCH_OPERATOR:
  913. case MATCH_SCRATCH:
  914. case MATCH_PARALLEL:
  915. i = XINT (pattern, 0);
  916. if (i > *pmax)
  917. *pmax = i;
  918. break;
  919. default:
  920. break;
  921. }
  922. fmt = GET_RTX_FORMAT (code);
  923. len = GET_RTX_LENGTH (code);
  924. for (i = 0; i < len; i++)
  925. {
  926. switch (fmt[i])
  927. {
  928. case 'e': case 'u':
  929. collect_insn_data (XEXP (pattern, i), palt, pmax);
  930. break;
  931. case 'V':
  932. if (XVEC (pattern, i) == NULL)
  933. break;
  934. /* Fall through. */
  935. case 'E':
  936. for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
  937. collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
  938. break;
  939. case 'i': case 'w': case '0': case 's': case 'S': case 'T':
  940. break;
  941. default:
  942. gcc_unreachable ();
  943. }
  944. }
  945. }
  946. static rtx
  947. alter_predicate_for_insn (rtx pattern, int alt, int max_op, int lineno)
  948. {
  949. const char *fmt;
  950. enum rtx_code code;
  951. int i, j, len;
  952. code = GET_CODE (pattern);
  953. switch (code)
  954. {
  955. case MATCH_OPERAND:
  956. {
  957. const char *c = XSTR (pattern, 2);
  958. if (n_alternatives (c) != 1)
  959. {
  960. error_with_line (lineno, "too many alternatives for operand %d",
  961. XINT (pattern, 0));
  962. return NULL;
  963. }
  964. /* Replicate C as needed to fill out ALT alternatives. */
  965. if (c && *c && alt > 1)
  966. {
  967. size_t c_len = strlen (c);
  968. size_t len = alt * (c_len + 1);
  969. char *new_c = XNEWVEC (char, len);
  970. memcpy (new_c, c, c_len);
  971. for (i = 1; i < alt; ++i)
  972. {
  973. new_c[i * (c_len + 1) - 1] = ',';
  974. memcpy (&new_c[i * (c_len + 1)], c, c_len);
  975. }
  976. new_c[len - 1] = '\0';
  977. XSTR (pattern, 2) = new_c;
  978. }
  979. }
  980. /* Fall through. */
  981. case MATCH_OPERATOR:
  982. case MATCH_SCRATCH:
  983. case MATCH_PARALLEL:
  984. XINT (pattern, 0) += max_op;
  985. break;
  986. default:
  987. break;
  988. }
  989. fmt = GET_RTX_FORMAT (code);
  990. len = GET_RTX_LENGTH (code);
  991. for (i = 0; i < len; i++)
  992. {
  993. rtx r;
  994. switch (fmt[i])
  995. {
  996. case 'e': case 'u':
  997. r = alter_predicate_for_insn (XEXP (pattern, i), alt,
  998. max_op, lineno);
  999. if (r == NULL)
  1000. return r;
  1001. break;
  1002. case 'E':
  1003. for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
  1004. {
  1005. r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
  1006. alt, max_op, lineno);
  1007. if (r == NULL)
  1008. return r;
  1009. }
  1010. break;
  1011. case 'i': case 'w': case '0': case 's':
  1012. break;
  1013. default:
  1014. gcc_unreachable ();
  1015. }
  1016. }
  1017. return pattern;
  1018. }
  1019. /* Duplicate constraints in PATTERN. If pattern is from original
  1020. rtl-template, we need to duplicate each alternative - for that we
  1021. need to use duplicate_each_alternative () as a functor ALTER.
  1022. If pattern is from output-pattern of define_subst, we need to
  1023. duplicate constraints in another way - with duplicate_alternatives ().
  1024. N_DUP is multiplication factor. */
  1025. static rtx
  1026. alter_constraints (rtx pattern, int n_dup, constraints_handler_t alter)
  1027. {
  1028. const char *fmt;
  1029. enum rtx_code code;
  1030. int i, j, len;
  1031. code = GET_CODE (pattern);
  1032. switch (code)
  1033. {
  1034. case MATCH_OPERAND:
  1035. XSTR (pattern, 2) = alter (XSTR (pattern, 2), n_dup);
  1036. break;
  1037. default:
  1038. break;
  1039. }
  1040. fmt = GET_RTX_FORMAT (code);
  1041. len = GET_RTX_LENGTH (code);
  1042. for (i = 0; i < len; i++)
  1043. {
  1044. rtx r;
  1045. switch (fmt[i])
  1046. {
  1047. case 'e': case 'u':
  1048. r = alter_constraints (XEXP (pattern, i), n_dup, alter);
  1049. if (r == NULL)
  1050. return r;
  1051. break;
  1052. case 'E':
  1053. for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
  1054. {
  1055. r = alter_constraints (XVECEXP (pattern, i, j), n_dup, alter);
  1056. if (r == NULL)
  1057. return r;
  1058. }
  1059. break;
  1060. case 'i': case 'w': case '0': case 's':
  1061. break;
  1062. default:
  1063. break;
  1064. }
  1065. }
  1066. return pattern;
  1067. }
  1068. static const char *
  1069. alter_test_for_insn (struct queue_elem *ce_elem,
  1070. struct queue_elem *insn_elem)
  1071. {
  1072. return join_c_conditions (XSTR (ce_elem->data, 1),
  1073. XSTR (insn_elem->data, 2));
  1074. }
  1075. /* Modify VAL, which is an attribute expression for the "enabled" attribute,
  1076. to take "ce_enabled" into account. Return the new expression. */
  1077. static rtx
  1078. modify_attr_enabled_ce (rtx val)
  1079. {
  1080. rtx eq_attr, str;
  1081. rtx ite;
  1082. eq_attr = rtx_alloc (EQ_ATTR);
  1083. ite = rtx_alloc (IF_THEN_ELSE);
  1084. str = rtx_alloc (CONST_STRING);
  1085. XSTR (eq_attr, 0) = "ce_enabled";
  1086. XSTR (eq_attr, 1) = "yes";
  1087. XSTR (str, 0) = "no";
  1088. XEXP (ite, 0) = eq_attr;
  1089. XEXP (ite, 1) = val;
  1090. XEXP (ite, 2) = str;
  1091. return ite;
  1092. }
  1093. /* Alter the attribute vector of INSN, which is a COND_EXEC variant created
  1094. from a define_insn pattern. We must modify the "predicable" attribute
  1095. to be named "ce_enabled", and also change any "enabled" attribute that's
  1096. present so that it takes ce_enabled into account.
  1097. We rely on the fact that INSN was created with copy_rtx, and modify data
  1098. in-place. */
  1099. static void
  1100. alter_attrs_for_insn (rtx insn)
  1101. {
  1102. static bool global_changes_made = false;
  1103. rtvec vec = XVEC (insn, 4);
  1104. rtvec new_vec;
  1105. rtx val, set;
  1106. int num_elem;
  1107. int predicable_idx = -1;
  1108. int enabled_idx = -1;
  1109. int i;
  1110. if (! vec)
  1111. return;
  1112. num_elem = GET_NUM_ELEM (vec);
  1113. for (i = num_elem - 1; i >= 0; --i)
  1114. {
  1115. rtx sub = RTVEC_ELT (vec, i);
  1116. switch (GET_CODE (sub))
  1117. {
  1118. case SET_ATTR:
  1119. if (strcmp (XSTR (sub, 0), "predicable") == 0)
  1120. {
  1121. predicable_idx = i;
  1122. XSTR (sub, 0) = "ce_enabled";
  1123. }
  1124. else if (strcmp (XSTR (sub, 0), "enabled") == 0)
  1125. {
  1126. enabled_idx = i;
  1127. XSTR (sub, 0) = "nonce_enabled";
  1128. }
  1129. break;
  1130. case SET_ATTR_ALTERNATIVE:
  1131. if (strcmp (XSTR (sub, 0), "predicable") == 0)
  1132. /* We already give an error elsewhere. */
  1133. return;
  1134. else if (strcmp (XSTR (sub, 0), "enabled") == 0)
  1135. {
  1136. enabled_idx = i;
  1137. XSTR (sub, 0) = "nonce_enabled";
  1138. }
  1139. break;
  1140. case SET:
  1141. if (GET_CODE (SET_DEST (sub)) != ATTR)
  1142. break;
  1143. if (strcmp (XSTR (SET_DEST (sub), 0), "predicable") == 0)
  1144. {
  1145. sub = SET_SRC (sub);
  1146. if (GET_CODE (sub) == CONST_STRING)
  1147. {
  1148. predicable_idx = i;
  1149. XSTR (sub, 0) = "ce_enabled";
  1150. }
  1151. else
  1152. /* We already give an error elsewhere. */
  1153. return;
  1154. break;
  1155. }
  1156. if (strcmp (XSTR (SET_DEST (sub), 0), "enabled") == 0)
  1157. {
  1158. enabled_idx = i;
  1159. XSTR (SET_DEST (sub), 0) = "nonce_enabled";
  1160. }
  1161. break;
  1162. default:
  1163. gcc_unreachable ();
  1164. }
  1165. }
  1166. if (predicable_idx == -1)
  1167. return;
  1168. if (!global_changes_made)
  1169. {
  1170. struct queue_elem *elem;
  1171. global_changes_made = true;
  1172. add_define_attr ("ce_enabled");
  1173. add_define_attr ("nonce_enabled");
  1174. for (elem = define_attr_queue; elem ; elem = elem->next)
  1175. if (strcmp (XSTR (elem->data, 0), "enabled") == 0)
  1176. {
  1177. XEXP (elem->data, 2)
  1178. = modify_attr_enabled_ce (XEXP (elem->data, 2));
  1179. }
  1180. }
  1181. if (enabled_idx == -1)
  1182. return;
  1183. new_vec = rtvec_alloc (num_elem + 1);
  1184. for (i = 0; i < num_elem; i++)
  1185. RTVEC_ELT (new_vec, i) = RTVEC_ELT (vec, i);
  1186. val = rtx_alloc (IF_THEN_ELSE);
  1187. XEXP (val, 0) = rtx_alloc (EQ_ATTR);
  1188. XEXP (val, 1) = rtx_alloc (CONST_STRING);
  1189. XEXP (val, 2) = rtx_alloc (CONST_STRING);
  1190. XSTR (XEXP (val, 0), 0) = "nonce_enabled";
  1191. XSTR (XEXP (val, 0), 1) = "yes";
  1192. XSTR (XEXP (val, 1), 0) = "yes";
  1193. XSTR (XEXP (val, 2), 0) = "no";
  1194. set = rtx_alloc (SET);
  1195. SET_DEST (set) = rtx_alloc (ATTR);
  1196. XSTR (SET_DEST (set), 0) = "enabled";
  1197. SET_SRC (set) = modify_attr_enabled_ce (val);
  1198. RTVEC_ELT (new_vec, i) = set;
  1199. XVEC (insn, 4) = new_vec;
  1200. }
  1201. /* As number of constraints is changed after define_subst, we need to
  1202. process attributes as well - we need to duplicate them the same way
  1203. that we duplicated constraints in original pattern
  1204. ELEM is a queue element, containing our rtl-template,
  1205. N_DUP - multiplication factor. */
  1206. static void
  1207. alter_attrs_for_subst_insn (struct queue_elem * elem, int n_dup)
  1208. {
  1209. rtvec vec = XVEC (elem->data, 4);
  1210. int num_elem;
  1211. int i;
  1212. if (n_dup < 2 || ! vec)
  1213. return;
  1214. num_elem = GET_NUM_ELEM (vec);
  1215. for (i = num_elem - 1; i >= 0; --i)
  1216. {
  1217. rtx sub = RTVEC_ELT (vec, i);
  1218. switch (GET_CODE (sub))
  1219. {
  1220. case SET_ATTR:
  1221. if (strchr (XSTR (sub, 1), ',') != NULL)
  1222. XSTR (sub, 1) = duplicate_alternatives (XSTR (sub, 1), n_dup);
  1223. break;
  1224. case SET_ATTR_ALTERNATIVE:
  1225. case SET:
  1226. error_with_line (elem->lineno,
  1227. "%s: `define_subst' does not support attributes "
  1228. "assigned by `set' and `set_attr_alternative'",
  1229. XSTR (elem->data, 0));
  1230. return;
  1231. default:
  1232. gcc_unreachable ();
  1233. }
  1234. }
  1235. }
  1236. /* Adjust all of the operand numbers in SRC to match the shift they'll
  1237. get from an operand displacement of DISP. Return a pointer after the
  1238. adjusted string. */
  1239. static char *
  1240. shift_output_template (char *dest, const char *src, int disp)
  1241. {
  1242. while (*src)
  1243. {
  1244. char c = *src++;
  1245. *dest++ = c;
  1246. if (c == '%')
  1247. {
  1248. c = *src++;
  1249. if (ISDIGIT ((unsigned char) c))
  1250. c += disp;
  1251. else if (ISALPHA (c))
  1252. {
  1253. *dest++ = c;
  1254. c = *src++ + disp;
  1255. }
  1256. *dest++ = c;
  1257. }
  1258. }
  1259. return dest;
  1260. }
  1261. static const char *
  1262. alter_output_for_insn (struct queue_elem *ce_elem,
  1263. struct queue_elem *insn_elem,
  1264. int alt, int max_op)
  1265. {
  1266. const char *ce_out, *insn_out;
  1267. char *result, *p;
  1268. size_t len, ce_len, insn_len;
  1269. /* ??? Could coordinate with genoutput to not duplicate code here. */
  1270. ce_out = XSTR (ce_elem->data, 2);
  1271. insn_out = XTMPL (insn_elem->data, 3);
  1272. if (!ce_out || *ce_out == '\0')
  1273. return insn_out;
  1274. ce_len = strlen (ce_out);
  1275. insn_len = strlen (insn_out);
  1276. if (*insn_out == '*')
  1277. /* You must take care of the predicate yourself. */
  1278. return insn_out;
  1279. if (*insn_out == '@')
  1280. {
  1281. len = (ce_len + 1) * alt + insn_len + 1;
  1282. p = result = XNEWVEC (char, len);
  1283. do
  1284. {
  1285. do
  1286. *p++ = *insn_out++;
  1287. while (ISSPACE ((unsigned char) *insn_out));
  1288. if (*insn_out != '#')
  1289. {
  1290. p = shift_output_template (p, ce_out, max_op);
  1291. *p++ = ' ';
  1292. }
  1293. do
  1294. *p++ = *insn_out++;
  1295. while (*insn_out && *insn_out != '\n');
  1296. }
  1297. while (*insn_out);
  1298. *p = '\0';
  1299. }
  1300. else
  1301. {
  1302. len = ce_len + 1 + insn_len + 1;
  1303. result = XNEWVEC (char, len);
  1304. p = shift_output_template (result, ce_out, max_op);
  1305. *p++ = ' ';
  1306. memcpy (p, insn_out, insn_len + 1);
  1307. }
  1308. return result;
  1309. }
  1310. /* From string STR "a,b,c" produce "a,b,c,a,b,c,a,b,c", i.e. original
  1311. string, duplicated N_DUP times. */
  1312. static const char *
  1313. duplicate_alternatives (const char * str, int n_dup)
  1314. {
  1315. int i, len, new_len;
  1316. char *result, *sp;
  1317. const char *cp;
  1318. if (n_dup < 2)
  1319. return str;
  1320. while (ISSPACE (*str))
  1321. str++;
  1322. if (*str == '\0')
  1323. return str;
  1324. cp = str;
  1325. len = strlen (str);
  1326. new_len = (len + 1) * n_dup;
  1327. sp = result = XNEWVEC (char, new_len);
  1328. /* Global modifier characters mustn't be duplicated: skip if found. */
  1329. if (*cp == '=' || *cp == '+' || *cp == '%')
  1330. {
  1331. *sp++ = *cp++;
  1332. len--;
  1333. }
  1334. /* Copy original constraints N_DUP times. */
  1335. for (i = 0; i < n_dup; i++, sp += len+1)
  1336. {
  1337. memcpy (sp, cp, len);
  1338. *(sp+len) = (i == n_dup - 1) ? '\0' : ',';
  1339. }
  1340. return result;
  1341. }
  1342. /* From string STR "a,b,c" produce "a,a,a,b,b,b,c,c,c", i.e. string where
  1343. each alternative from the original string is duplicated N_DUP times. */
  1344. static const char *
  1345. duplicate_each_alternative (const char * str, int n_dup)
  1346. {
  1347. int i, len, new_len;
  1348. char *result, *sp, *ep, *cp;
  1349. if (n_dup < 2)
  1350. return str;
  1351. while (ISSPACE (*str))
  1352. str++;
  1353. if (*str == '\0')
  1354. return str;
  1355. cp = xstrdup (str);
  1356. new_len = (strlen (cp) + 1) * n_dup;
  1357. sp = result = XNEWVEC (char, new_len);
  1358. /* Global modifier characters mustn't be duplicated: skip if found. */
  1359. if (*cp == '=' || *cp == '+' || *cp == '%')
  1360. *sp++ = *cp++;
  1361. do
  1362. {
  1363. if ((ep = strchr (cp, ',')) != NULL)
  1364. *ep++ = '\0';
  1365. len = strlen (cp);
  1366. /* Copy a constraint N_DUP times. */
  1367. for (i = 0; i < n_dup; i++, sp += len + 1)
  1368. {
  1369. memcpy (sp, cp, len);
  1370. *(sp+len) = (ep == NULL && i == n_dup - 1) ? '\0' : ',';
  1371. }
  1372. cp = ep;
  1373. }
  1374. while (cp != NULL);
  1375. return result;
  1376. }
  1377. /* Alter the output of INSN whose pattern was modified by
  1378. DEFINE_SUBST. We must replicate output strings according
  1379. to the new number of alternatives ALT in substituted pattern.
  1380. If ALT equals 1, output has one alternative or defined by C
  1381. code, then output is returned without any changes. */
  1382. static const char *
  1383. alter_output_for_subst_insn (rtx insn, int alt)
  1384. {
  1385. const char *insn_out, *sp ;
  1386. char *old_out, *new_out, *cp;
  1387. int i, j, new_len;
  1388. insn_out = XTMPL (insn, 3);
  1389. if (alt < 2 || *insn_out == '*' || *insn_out != '@')
  1390. return insn_out;
  1391. old_out = XNEWVEC (char, strlen (insn_out)),
  1392. sp = insn_out;
  1393. while (ISSPACE (*sp) || *sp == '@')
  1394. sp++;
  1395. for (i = 0; *sp;)
  1396. old_out[i++] = *sp++;
  1397. new_len = alt * (i + 1) + 1;
  1398. new_out = XNEWVEC (char, new_len);
  1399. new_out[0] = '@';
  1400. for (j = 0, cp = new_out + 1; j < alt; j++, cp += i + 1)
  1401. {
  1402. memcpy (cp, old_out, i);
  1403. *(cp+i) = (j == alt - 1) ? '\0' : '\n';
  1404. }
  1405. return new_out;
  1406. }
  1407. /* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */
  1408. static void
  1409. process_one_cond_exec (struct queue_elem *ce_elem)
  1410. {
  1411. struct queue_elem *insn_elem;
  1412. for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
  1413. {
  1414. int alternatives, max_operand;
  1415. rtx pred, insn, pattern, split;
  1416. char *new_name;
  1417. int i;
  1418. if (! is_predicable (insn_elem))
  1419. continue;
  1420. alternatives = 1;
  1421. max_operand = -1;
  1422. collect_insn_data (insn_elem->data, &alternatives, &max_operand);
  1423. max_operand += 1;
  1424. if (XVECLEN (ce_elem->data, 0) != 1)
  1425. {
  1426. error_with_line (ce_elem->lineno, "too many patterns in predicate");
  1427. return;
  1428. }
  1429. pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
  1430. pred = alter_predicate_for_insn (pred, alternatives, max_operand,
  1431. ce_elem->lineno);
  1432. if (pred == NULL)
  1433. return;
  1434. /* Construct a new pattern for the new insn. */
  1435. insn = copy_rtx (insn_elem->data);
  1436. new_name = XNEWVAR (char, strlen XSTR (insn_elem->data, 0) + 4);
  1437. sprintf (new_name, "*p %s", XSTR (insn_elem->data, 0));
  1438. XSTR (insn, 0) = new_name;
  1439. pattern = rtx_alloc (COND_EXEC);
  1440. XEXP (pattern, 0) = pred;
  1441. if (XVECLEN (insn, 1) == 1)
  1442. {
  1443. XEXP (pattern, 1) = XVECEXP (insn, 1, 0);
  1444. XVECEXP (insn, 1, 0) = pattern;
  1445. PUT_NUM_ELEM (XVEC (insn, 1), 1);
  1446. }
  1447. else
  1448. {
  1449. XEXP (pattern, 1) = rtx_alloc (PARALLEL);
  1450. XVEC (XEXP (pattern, 1), 0) = XVEC (insn, 1);
  1451. XVEC (insn, 1) = rtvec_alloc (1);
  1452. XVECEXP (insn, 1, 0) = pattern;
  1453. }
  1454. if (XVEC (ce_elem->data, 3) != NULL)
  1455. {
  1456. rtvec attributes = rtvec_alloc (XVECLEN (insn, 4)
  1457. + XVECLEN (ce_elem->data, 3));
  1458. int i = 0;
  1459. int j = 0;
  1460. for (i = 0; i < XVECLEN (insn, 4); i++)
  1461. RTVEC_ELT (attributes, i) = XVECEXP (insn, 4, i);
  1462. for (j = 0; j < XVECLEN (ce_elem->data, 3); j++, i++)
  1463. RTVEC_ELT (attributes, i) = XVECEXP (ce_elem->data, 3, j);
  1464. XVEC (insn, 4) = attributes;
  1465. }
  1466. XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
  1467. XTMPL (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
  1468. alternatives, max_operand);
  1469. alter_attrs_for_insn (insn);
  1470. /* Put the new pattern on the `other' list so that it
  1471. (a) is not reprocessed by other define_cond_exec patterns
  1472. (b) appears after all normal define_insn patterns.
  1473. ??? B is debatable. If one has normal insns that match
  1474. cond_exec patterns, they will be preferred over these
  1475. generated patterns. Whether this matters in practice, or if
  1476. it's a good thing, or whether we should thread these new
  1477. patterns into the define_insn chain just after their generator
  1478. is something we'll have to experiment with. */
  1479. queue_pattern (insn, &other_tail, insn_elem->filename,
  1480. insn_elem->lineno);
  1481. if (!insn_elem->split)
  1482. continue;
  1483. /* If the original insn came from a define_insn_and_split,
  1484. generate a new split to handle the predicated insn. */
  1485. split = copy_rtx (insn_elem->split->data);
  1486. /* Predicate the pattern matched by the split. */
  1487. pattern = rtx_alloc (COND_EXEC);
  1488. XEXP (pattern, 0) = pred;
  1489. if (XVECLEN (split, 0) == 1)
  1490. {
  1491. XEXP (pattern, 1) = XVECEXP (split, 0, 0);
  1492. XVECEXP (split, 0, 0) = pattern;
  1493. PUT_NUM_ELEM (XVEC (split, 0), 1);
  1494. }
  1495. else
  1496. {
  1497. XEXP (pattern, 1) = rtx_alloc (PARALLEL);
  1498. XVEC (XEXP (pattern, 1), 0) = XVEC (split, 0);
  1499. XVEC (split, 0) = rtvec_alloc (1);
  1500. XVECEXP (split, 0, 0) = pattern;
  1501. }
  1502. /* Predicate all of the insns generated by the split. */
  1503. for (i = 0; i < XVECLEN (split, 2); i++)
  1504. {
  1505. pattern = rtx_alloc (COND_EXEC);
  1506. XEXP (pattern, 0) = pred;
  1507. XEXP (pattern, 1) = XVECEXP (split, 2, i);
  1508. XVECEXP (split, 2, i) = pattern;
  1509. }
  1510. /* Add the new split to the queue. */
  1511. queue_pattern (split, &other_tail, read_md_filename,
  1512. insn_elem->split->lineno);
  1513. }
  1514. }
  1515. /* Try to apply define_substs to the given ELEM.
  1516. Only define_substs, specified via attributes would be applied.
  1517. If attribute, requiring define_subst, is set, but no define_subst
  1518. was applied, ELEM would be deleted. */
  1519. static void
  1520. process_substs_on_one_elem (struct queue_elem *elem,
  1521. struct queue_elem *queue)
  1522. {
  1523. struct queue_elem *subst_elem;
  1524. int i, j, patterns_match;
  1525. for (subst_elem = define_subst_queue;
  1526. subst_elem; subst_elem = subst_elem->next)
  1527. {
  1528. int alternatives, alternatives_subst;
  1529. rtx subst_pattern;
  1530. rtvec subst_pattern_vec;
  1531. if (!has_subst_attribute (elem, subst_elem))
  1532. continue;
  1533. /* Compare original rtl-pattern from define_insn with input
  1534. pattern from define_subst.
  1535. Also, check if numbers of alternatives are the same in all
  1536. match_operands. */
  1537. if (XVECLEN (elem->data, 1) != XVECLEN (subst_elem->data, 1))
  1538. continue;
  1539. patterns_match = 1;
  1540. alternatives = -1;
  1541. alternatives_subst = -1;
  1542. for (j = 0; j < XVECLEN (elem->data, 1); j++)
  1543. {
  1544. if (!subst_pattern_match (XVECEXP (elem->data, 1, j),
  1545. XVECEXP (subst_elem->data, 1, j),
  1546. subst_elem->lineno))
  1547. {
  1548. patterns_match = 0;
  1549. break;
  1550. }
  1551. if (!get_alternatives_number (XVECEXP (elem->data, 1, j),
  1552. &alternatives, subst_elem->lineno))
  1553. {
  1554. patterns_match = 0;
  1555. break;
  1556. }
  1557. }
  1558. /* Check if numbers of alternatives are the same in all
  1559. match_operands in output template of define_subst. */
  1560. for (j = 0; j < XVECLEN (subst_elem->data, 3); j++)
  1561. {
  1562. if (!get_alternatives_number (XVECEXP (subst_elem->data, 3, j),
  1563. &alternatives_subst,
  1564. subst_elem->lineno))
  1565. {
  1566. patterns_match = 0;
  1567. break;
  1568. }
  1569. }
  1570. if (!patterns_match)
  1571. continue;
  1572. /* Clear array in which we save occupied indexes of operands. */
  1573. memset (used_operands_numbers, 0, sizeof (used_operands_numbers));
  1574. /* Create a pattern, based on the output one from define_subst. */
  1575. subst_pattern_vec = rtvec_alloc (XVECLEN (subst_elem->data, 3));
  1576. for (j = 0; j < XVECLEN (subst_elem->data, 3); j++)
  1577. {
  1578. subst_pattern = copy_rtx (XVECEXP (subst_elem->data, 3, j));
  1579. /* Duplicate constraints in substitute-pattern. */
  1580. subst_pattern = alter_constraints (subst_pattern, alternatives,
  1581. duplicate_each_alternative);
  1582. subst_pattern = adjust_operands_numbers (subst_pattern);
  1583. /* Substitute match_dup and match_op_dup in the new pattern and
  1584. duplicate constraints. */
  1585. subst_pattern = subst_dup (subst_pattern, alternatives,
  1586. alternatives_subst);
  1587. replace_duplicating_operands_in_pattern (subst_pattern);
  1588. /* We don't need any constraints in DEFINE_EXPAND. */
  1589. if (GET_CODE (elem->data) == DEFINE_EXPAND)
  1590. remove_constraints (subst_pattern);
  1591. RTVEC_ELT (subst_pattern_vec, j) = subst_pattern;
  1592. }
  1593. XVEC (elem->data, 1) = subst_pattern_vec;
  1594. for (i = 0; i < MAX_OPERANDS; i++)
  1595. match_operand_entries_in_pattern[i] = NULL;
  1596. if (GET_CODE (elem->data) == DEFINE_INSN)
  1597. {
  1598. XTMPL (elem->data, 3) =
  1599. alter_output_for_subst_insn (elem->data, alternatives_subst);
  1600. alter_attrs_for_subst_insn (elem, alternatives_subst);
  1601. }
  1602. /* Recalculate condition, joining conditions from original and
  1603. DEFINE_SUBST input patterns. */
  1604. XSTR (elem->data, 2) = join_c_conditions (XSTR (subst_elem->data, 2),
  1605. XSTR (elem->data, 2));
  1606. /* Mark that subst was applied by changing attribute from "yes"
  1607. to "no". */
  1608. change_subst_attribute (elem, subst_elem, subst_false);
  1609. }
  1610. /* If ELEM contains a subst attribute with value "yes", then we
  1611. expected that a subst would be applied, but it wasn't - so,
  1612. we need to remove that elementto avoid duplicating. */
  1613. for (subst_elem = define_subst_queue;
  1614. subst_elem; subst_elem = subst_elem->next)
  1615. {
  1616. if (has_subst_attribute (elem, subst_elem))
  1617. {
  1618. remove_from_queue (elem, &queue);
  1619. return;
  1620. }
  1621. }
  1622. }
  1623. /* This is a subroutine of mark_operands_used_in_match_dup.
  1624. This routine is marks all MATCH_OPERANDs inside PATTERN as occupied. */
  1625. static void
  1626. mark_operands_from_match_dup (rtx pattern)
  1627. {
  1628. const char *fmt;
  1629. int i, j, len, opno;
  1630. if (GET_CODE (pattern) == MATCH_OPERAND
  1631. || GET_CODE (pattern) == MATCH_OPERATOR
  1632. || GET_CODE (pattern) == MATCH_PARALLEL)
  1633. {
  1634. opno = XINT (pattern, 0);
  1635. gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
  1636. used_operands_numbers [opno] = 1;
  1637. }
  1638. fmt = GET_RTX_FORMAT (GET_CODE (pattern));
  1639. len = GET_RTX_LENGTH (GET_CODE (pattern));
  1640. for (i = 0; i < len; i++)
  1641. {
  1642. switch (fmt[i])
  1643. {
  1644. case 'e': case 'u':
  1645. mark_operands_from_match_dup (XEXP (pattern, i));
  1646. break;
  1647. case 'E':
  1648. for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
  1649. mark_operands_from_match_dup (XVECEXP (pattern, i, j));
  1650. break;
  1651. }
  1652. }
  1653. }
  1654. /* This is a subroutine of adjust_operands_numbers.
  1655. It goes through all expressions in PATTERN and when MATCH_DUP is
  1656. met, all MATCH_OPERANDs inside it is marked as occupied. The
  1657. process of marking is done by routin mark_operands_from_match_dup. */
  1658. static void
  1659. mark_operands_used_in_match_dup (rtx pattern)
  1660. {
  1661. const char *fmt;
  1662. int i, j, len, opno;
  1663. if (GET_CODE (pattern) == MATCH_DUP)
  1664. {
  1665. opno = XINT (pattern, 0);
  1666. gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
  1667. mark_operands_from_match_dup (operand_data[opno]);
  1668. return;
  1669. }
  1670. fmt = GET_RTX_FORMAT (GET_CODE (pattern));
  1671. len = GET_RTX_LENGTH (GET_CODE (pattern));
  1672. for (i = 0; i < len; i++)
  1673. {
  1674. switch (fmt[i])
  1675. {
  1676. case 'e': case 'u':
  1677. mark_operands_used_in_match_dup (XEXP (pattern, i));
  1678. break;
  1679. case 'E':
  1680. for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
  1681. mark_operands_used_in_match_dup (XVECEXP (pattern, i, j));
  1682. break;
  1683. }
  1684. }
  1685. }
  1686. /* This is subroutine of renumerate_operands_in_pattern.
  1687. It finds first not-occupied operand-index. */
  1688. static int
  1689. find_first_unused_number_of_operand ()
  1690. {
  1691. int i;
  1692. for (i = 0; i < MAX_OPERANDS; i++)
  1693. if (!used_operands_numbers[i])
  1694. return i;
  1695. return MAX_OPERANDS;
  1696. }
  1697. /* This is subroutine of adjust_operands_numbers.
  1698. It visits all expressions in PATTERN and assigns not-occupied
  1699. operand indexes to MATCH_OPERANDs and MATCH_OPERATORs of this
  1700. PATTERN. */
  1701. static void
  1702. renumerate_operands_in_pattern (rtx pattern)
  1703. {
  1704. const char *fmt;
  1705. enum rtx_code code;
  1706. int i, j, len, new_opno;
  1707. code = GET_CODE (pattern);
  1708. if (code == MATCH_OPERAND
  1709. || code == MATCH_OPERATOR)
  1710. {
  1711. new_opno = find_first_unused_number_of_operand ();
  1712. gcc_assert (new_opno >= 0 && new_opno < MAX_OPERANDS);
  1713. XINT (pattern, 0) = new_opno;
  1714. used_operands_numbers [new_opno] = 1;
  1715. }
  1716. fmt = GET_RTX_FORMAT (GET_CODE (pattern));
  1717. len = GET_RTX_LENGTH (GET_CODE (pattern));
  1718. for (i = 0; i < len; i++)
  1719. {
  1720. switch (fmt[i])
  1721. {
  1722. case 'e': case 'u':
  1723. renumerate_operands_in_pattern (XEXP (pattern, i));
  1724. break;
  1725. case 'E':
  1726. for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
  1727. renumerate_operands_in_pattern (XVECEXP (pattern, i, j));
  1728. break;
  1729. }
  1730. }
  1731. }
  1732. /* If output pattern of define_subst contains MATCH_DUP, then this
  1733. expression would be replaced with the pattern, matched with
  1734. MATCH_OPERAND from input pattern. This pattern could contain any
  1735. number of MATCH_OPERANDs, MATCH_OPERATORs etc., so it's possible
  1736. that a MATCH_OPERAND from output_pattern (if any) would have the
  1737. same number, as MATCH_OPERAND from copied pattern. To avoid such
  1738. indexes overlapping, we assign new indexes to MATCH_OPERANDs,
  1739. laying in the output pattern outside of MATCH_DUPs. */
  1740. static rtx
  1741. adjust_operands_numbers (rtx pattern)
  1742. {
  1743. mark_operands_used_in_match_dup (pattern);
  1744. renumerate_operands_in_pattern (pattern);
  1745. return pattern;
  1746. }
  1747. /* Generate RTL expression
  1748. (match_dup OPNO)
  1749. */
  1750. static rtx
  1751. generate_match_dup (int opno)
  1752. {
  1753. rtx return_rtx = rtx_alloc (MATCH_DUP);
  1754. PUT_CODE (return_rtx, MATCH_DUP);
  1755. XINT (return_rtx, 0) = opno;
  1756. return return_rtx;
  1757. }
  1758. /* This routine checks all match_operands in PATTERN and if some of
  1759. have the same index, it replaces all of them except the first one to
  1760. match_dup.
  1761. Usually, match_operands with the same indexes are forbidden, but
  1762. after define_subst copy an RTL-expression from original template,
  1763. indexes of existed and just-copied match_operands could coincide.
  1764. To fix it, we replace one of them with match_dup. */
  1765. static rtx
  1766. replace_duplicating_operands_in_pattern (rtx pattern)
  1767. {
  1768. const char *fmt;
  1769. int i, j, len, opno;
  1770. rtx mdup;
  1771. if (GET_CODE (pattern) == MATCH_OPERAND)
  1772. {
  1773. opno = XINT (pattern, 0);
  1774. gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
  1775. if (match_operand_entries_in_pattern[opno] == NULL)
  1776. {
  1777. match_operand_entries_in_pattern[opno] = pattern;
  1778. return NULL;
  1779. }
  1780. else
  1781. {
  1782. /* Compare predicates before replacing with match_dup. */
  1783. if (strcmp (XSTR (pattern, 1),
  1784. XSTR (match_operand_entries_in_pattern[opno], 1)))
  1785. {
  1786. error ("duplicated match_operands with different predicates were"
  1787. " found.");
  1788. return NULL;
  1789. }
  1790. return generate_match_dup (opno);
  1791. }
  1792. }
  1793. fmt = GET_RTX_FORMAT (GET_CODE (pattern));
  1794. len = GET_RTX_LENGTH (GET_CODE (pattern));
  1795. for (i = 0; i < len; i++)
  1796. {
  1797. switch (fmt[i])
  1798. {
  1799. case 'e': case 'u':
  1800. mdup = replace_duplicating_operands_in_pattern (XEXP (pattern, i));
  1801. if (mdup)
  1802. XEXP (pattern, i) = mdup;
  1803. break;
  1804. case 'E':
  1805. for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
  1806. {
  1807. mdup =
  1808. replace_duplicating_operands_in_pattern (XVECEXP
  1809. (pattern, i, j));
  1810. if (mdup)
  1811. XVECEXP (pattern, i, j) = mdup;
  1812. }
  1813. break;
  1814. }
  1815. }
  1816. return NULL;
  1817. }
  1818. /* The routine modifies given input PATTERN of define_subst, replacing
  1819. MATCH_DUP and MATCH_OP_DUP with operands from define_insn original
  1820. pattern, whose operands are stored in OPERAND_DATA array.
  1821. It also duplicates constraints in operands - constraints from
  1822. define_insn operands are duplicated N_SUBST_ALT times, constraints
  1823. from define_subst operands are duplicated N_ALT times.
  1824. After the duplication, returned output rtl-pattern contains every
  1825. combination of input constraints Vs constraints from define_subst
  1826. output. */
  1827. static rtx
  1828. subst_dup (rtx pattern, int n_alt, int n_subst_alt)
  1829. {
  1830. const char *fmt;
  1831. enum rtx_code code;
  1832. int i, j, len, opno;
  1833. code = GET_CODE (pattern);
  1834. switch (code)
  1835. {
  1836. case MATCH_DUP:
  1837. case MATCH_OP_DUP:
  1838. opno = XINT (pattern, 0);
  1839. gcc_assert (opno >= 0 && opno < MAX_OPERANDS);
  1840. if (operand_data[opno])
  1841. {
  1842. pattern = copy_rtx (operand_data[opno]);
  1843. /* Duplicate constraints. */
  1844. pattern = alter_constraints (pattern, n_subst_alt,
  1845. duplicate_alternatives);
  1846. }
  1847. break;
  1848. default:
  1849. break;
  1850. }
  1851. fmt = GET_RTX_FORMAT (GET_CODE (pattern));
  1852. len = GET_RTX_LENGTH (GET_CODE (pattern));
  1853. for (i = 0; i < len; i++)
  1854. {
  1855. switch (fmt[i])
  1856. {
  1857. case 'e': case 'u':
  1858. if (code != MATCH_DUP && code != MATCH_OP_DUP)
  1859. XEXP (pattern, i) = subst_dup (XEXP (pattern, i),
  1860. n_alt, n_subst_alt);
  1861. break;
  1862. case 'V':
  1863. if (XVEC (pattern, i) == NULL)
  1864. break;
  1865. case 'E':
  1866. if (code != MATCH_DUP && code != MATCH_OP_DUP)
  1867. for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
  1868. XVECEXP (pattern, i, j) = subst_dup (XVECEXP (pattern, i, j),
  1869. n_alt, n_subst_alt);
  1870. break;
  1871. case 'i': case 'w': case '0': case 's': case 'S': case 'T':
  1872. break;
  1873. default:
  1874. gcc_unreachable ();
  1875. }
  1876. }
  1877. return pattern;
  1878. }
  1879. /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
  1880. patterns appropriately. */
  1881. static void
  1882. process_define_cond_exec (void)
  1883. {
  1884. struct queue_elem *elem;
  1885. identify_predicable_attribute ();
  1886. if (have_error)
  1887. return;
  1888. for (elem = define_cond_exec_queue; elem ; elem = elem->next)
  1889. process_one_cond_exec (elem);
  1890. }
  1891. /* If we have any DEFINE_SUBST patterns, expand DEFINE_INSN and
  1892. DEFINE_EXPAND patterns appropriately. */
  1893. static void
  1894. process_define_subst (void)
  1895. {
  1896. struct queue_elem *elem, *elem_attr;
  1897. /* Check if each define_subst has corresponding define_subst_attr. */
  1898. for (elem = define_subst_queue; elem ; elem = elem->next)
  1899. {
  1900. for (elem_attr = define_subst_attr_queue;
  1901. elem_attr;
  1902. elem_attr = elem_attr->next)
  1903. if (strcmp (XSTR (elem->data, 0), XSTR (elem_attr->data, 1)) == 0)
  1904. goto found;
  1905. error_with_line (elem->lineno,
  1906. "%s: `define_subst' must have at least one "
  1907. "corresponding `define_subst_attr'",
  1908. XSTR (elem->data, 0));
  1909. return;
  1910. found:
  1911. continue;
  1912. }
  1913. for (elem = define_insn_queue; elem ; elem = elem->next)
  1914. process_substs_on_one_elem (elem, define_insn_queue);
  1915. for (elem = other_queue; elem ; elem = elem->next)
  1916. {
  1917. if (GET_CODE (elem->data) != DEFINE_EXPAND)
  1918. continue;
  1919. process_substs_on_one_elem (elem, other_queue);
  1920. }
  1921. }
  1922. /* A read_md_files callback for reading an rtx. */
  1923. static void
  1924. rtx_handle_directive (int lineno, const char *rtx_name)
  1925. {
  1926. rtx queue, x;
  1927. if (read_rtx (rtx_name, &queue))
  1928. for (x = queue; x; x = XEXP (x, 1))
  1929. process_rtx (XEXP (x, 0), lineno);
  1930. }
  1931. /* Comparison function for the mnemonic hash table. */
  1932. static int
  1933. htab_eq_string (const void *s1, const void *s2)
  1934. {
  1935. return strcmp ((const char*)s1, (const char*)s2) == 0;
  1936. }
  1937. /* Add mnemonic STR with length LEN to the mnemonic hash table
  1938. MNEMONIC_HTAB. A trailing zero end character is appendend to STR
  1939. and a permanent heap copy of STR is created. */
  1940. static void
  1941. add_mnemonic_string (htab_t mnemonic_htab, const char *str, int len)
  1942. {
  1943. char *new_str;
  1944. void **slot;
  1945. char *str_zero = (char*)alloca (len + 1);
  1946. memcpy (str_zero, str, len);
  1947. str_zero[len] = '\0';
  1948. slot = htab_find_slot (mnemonic_htab, str_zero, INSERT);
  1949. if (*slot)
  1950. return;
  1951. /* Not found; create a permanent copy and add it to the hash table. */
  1952. new_str = XNEWVAR (char, len + 1);
  1953. memcpy (new_str, str_zero, len + 1);
  1954. *slot = new_str;
  1955. }
  1956. /* Scan INSN for mnemonic strings and add them to the mnemonic hash
  1957. table in MNEMONIC_HTAB.
  1958. The mnemonics cannot be found if they are emitted using C code.
  1959. If a mnemonic string contains ';' or a newline the string assumed
  1960. to consist of more than a single instruction. The attribute value
  1961. will then be set to the user defined default value. */
  1962. static void
  1963. gen_mnemonic_setattr (htab_t mnemonic_htab, rtx insn)
  1964. {
  1965. const char *template_code, *cp;
  1966. int i;
  1967. int vec_len;
  1968. rtx set_attr;
  1969. char *attr_name;
  1970. rtvec new_vec;
  1971. template_code = XTMPL (insn, 3);
  1972. /* Skip patterns which use C code to emit the template. */
  1973. if (template_code[0] == '*')
  1974. return;
  1975. if (template_code[0] == '@')
  1976. cp = &template_code[1];
  1977. else
  1978. cp = &template_code[0];
  1979. for (i = 0; *cp; )
  1980. {
  1981. const char *ep, *sp;
  1982. int size = 0;
  1983. while (ISSPACE (*cp))
  1984. cp++;
  1985. for (ep = sp = cp; !IS_VSPACE (*ep) && *ep != '\0'; ++ep)
  1986. if (!ISSPACE (*ep))
  1987. sp = ep + 1;
  1988. if (i > 0)
  1989. obstack_1grow (&string_obstack, ',');
  1990. while (cp < sp && ((*cp >= '0' && *cp <= '9')
  1991. || (*cp >= 'a' && *cp <= 'z')))
  1992. {
  1993. obstack_1grow (&string_obstack, *cp);
  1994. cp++;
  1995. size++;
  1996. }
  1997. while (cp < sp)
  1998. {
  1999. if (*cp == ';' || (*cp == '\\' && cp[1] == 'n'))
  2000. {
  2001. /* Don't set a value if there are more than one
  2002. instruction in the string. */
  2003. obstack_next_free (&string_obstack) =
  2004. obstack_next_free (&string_obstack) - size;
  2005. size = 0;
  2006. cp = sp;
  2007. break;
  2008. }
  2009. cp++;
  2010. }
  2011. if (size == 0)
  2012. obstack_1grow (&string_obstack, '*');
  2013. else
  2014. add_mnemonic_string (mnemonic_htab,
  2015. obstack_next_free (&string_obstack) - size,
  2016. size);
  2017. i++;
  2018. }
  2019. /* An insn definition might emit an empty string. */
  2020. if (obstack_object_size (&string_obstack) == 0)
  2021. return;
  2022. obstack_1grow (&string_obstack, '\0');
  2023. set_attr = rtx_alloc (SET_ATTR);
  2024. XSTR (set_attr, 1) = XOBFINISH (&string_obstack, char *);
  2025. attr_name = XNEWVAR (char, strlen (MNEMONIC_ATTR_NAME) + 1);
  2026. strcpy (attr_name, MNEMONIC_ATTR_NAME);
  2027. XSTR (set_attr, 0) = attr_name;
  2028. if (!XVEC (insn, 4))
  2029. vec_len = 0;
  2030. else
  2031. vec_len = XVECLEN (insn, 4);
  2032. new_vec = rtvec_alloc (vec_len + 1);
  2033. for (i = 0; i < vec_len; i++)
  2034. RTVEC_ELT (new_vec, i) = XVECEXP (insn, 4, i);
  2035. RTVEC_ELT (new_vec, vec_len) = set_attr;
  2036. XVEC (insn, 4) = new_vec;
  2037. }
  2038. /* This function is called for the elements in the mnemonic hashtable
  2039. and generates a comma separated list of the mnemonics. */
  2040. static int
  2041. mnemonic_htab_callback (void **slot, void *info ATTRIBUTE_UNUSED)
  2042. {
  2043. obstack_grow (&string_obstack, (char*)*slot, strlen ((char*)*slot));
  2044. obstack_1grow (&string_obstack, ',');
  2045. return 1;
  2046. }
  2047. /* Generate (set_attr "mnemonic" "..") RTXs and append them to every
  2048. insn definition in case the back end requests it by defining the
  2049. mnemonic attribute. The values for the attribute will be extracted
  2050. from the output patterns of the insn definitions as far as
  2051. possible. */
  2052. static void
  2053. gen_mnemonic_attr (void)
  2054. {
  2055. struct queue_elem *elem;
  2056. rtx mnemonic_attr = NULL;
  2057. htab_t mnemonic_htab;
  2058. const char *str, *p;
  2059. int i;
  2060. if (have_error)
  2061. return;
  2062. /* Look for the DEFINE_ATTR for `mnemonic'. */
  2063. for (elem = define_attr_queue; elem != *define_attr_tail; elem = elem->next)
  2064. if (GET_CODE (elem->data) == DEFINE_ATTR
  2065. && strcmp (XSTR (elem->data, 0), MNEMONIC_ATTR_NAME) == 0)
  2066. {
  2067. mnemonic_attr = elem->data;
  2068. break;
  2069. }
  2070. /* A (define_attr "mnemonic" "...") indicates that the back-end
  2071. wants a mnemonic attribute to be generated. */
  2072. if (!mnemonic_attr)
  2073. return;
  2074. mnemonic_htab = htab_create_alloc (MNEMONIC_HTAB_SIZE, htab_hash_string,
  2075. htab_eq_string, 0, xcalloc, free);
  2076. for (elem = define_insn_queue; elem; elem = elem->next)
  2077. {
  2078. rtx insn = elem->data;
  2079. bool found = false;
  2080. /* Check if the insn definition already has
  2081. (set_attr "mnemonic" ...) or (set (attr "mnemonic") ...). */
  2082. if (XVEC (insn, 4))
  2083. for (i = 0; i < XVECLEN (insn, 4); i++)
  2084. {
  2085. rtx set_attr = XVECEXP (insn, 4, i);
  2086. switch (GET_CODE (set_attr))
  2087. {
  2088. case SET_ATTR:
  2089. case SET_ATTR_ALTERNATIVE:
  2090. if (strcmp (XSTR (set_attr, 0), MNEMONIC_ATTR_NAME) == 0)
  2091. found = true;
  2092. break;
  2093. case SET:
  2094. if (GET_CODE (SET_DEST (set_attr)) == ATTR
  2095. && strcmp (XSTR (SET_DEST (set_attr), 0),
  2096. MNEMONIC_ATTR_NAME) == 0)
  2097. found = true;
  2098. break;
  2099. default:
  2100. break;
  2101. }
  2102. }
  2103. if (!found)
  2104. gen_mnemonic_setattr (mnemonic_htab, insn);
  2105. }
  2106. /* Add the user defined values to the hash table. */
  2107. str = XSTR (mnemonic_attr, 1);
  2108. while ((p = scan_comma_elt (&str)) != NULL)
  2109. add_mnemonic_string (mnemonic_htab, p, str - p);
  2110. htab_traverse (mnemonic_htab, mnemonic_htab_callback, NULL);
  2111. /* Replace the last ',' with the zero end character. */
  2112. *((char *)obstack_next_free (&string_obstack) - 1) = '\0';
  2113. XSTR (mnemonic_attr, 1) = XOBFINISH (&string_obstack, char *);
  2114. }
  2115. /* Check if there are DEFINE_ATTRs with the same name. */
  2116. static void
  2117. check_define_attr_duplicates ()
  2118. {
  2119. struct queue_elem *elem;
  2120. htab_t attr_htab;
  2121. char * attr_name;
  2122. void **slot;
  2123. attr_htab = htab_create (500, htab_hash_string, htab_eq_string, NULL);
  2124. for (elem = define_attr_queue; elem; elem = elem->next)
  2125. {
  2126. attr_name = xstrdup (XSTR (elem->data, 0));
  2127. slot = htab_find_slot (attr_htab, attr_name, INSERT);
  2128. /* Duplicate. */
  2129. if (*slot)
  2130. {
  2131. error_with_line (elem->lineno, "redefinition of attribute '%s'",
  2132. attr_name);
  2133. htab_delete (attr_htab);
  2134. return;
  2135. }
  2136. *slot = attr_name;
  2137. }
  2138. htab_delete (attr_htab);
  2139. }
  2140. /* The entry point for initializing the reader. */
  2141. bool
  2142. init_rtx_reader_args_cb (int argc, char **argv,
  2143. bool (*parse_opt) (const char *))
  2144. {
  2145. /* Prepare to read input. */
  2146. condition_table = htab_create (500, hash_c_test, cmp_c_test, NULL);
  2147. init_predicate_table ();
  2148. obstack_init (rtl_obstack);
  2149. /* Start at 1, to make 0 available for CODE_FOR_nothing. */
  2150. sequence_num = 1;
  2151. read_md_files (argc, argv, parse_opt, rtx_handle_directive);
  2152. if (define_attr_queue != NULL)
  2153. check_define_attr_duplicates ();
  2154. /* Process define_cond_exec patterns. */
  2155. if (define_cond_exec_queue != NULL)
  2156. process_define_cond_exec ();
  2157. /* Process define_subst patterns. */
  2158. if (define_subst_queue != NULL)
  2159. process_define_subst ();
  2160. if (define_attr_queue != NULL)
  2161. gen_mnemonic_attr ();
  2162. return !have_error;
  2163. }
  2164. /* Programs that don't have their own options can use this entry point
  2165. instead. */
  2166. bool
  2167. init_rtx_reader_args (int argc, char **argv)
  2168. {
  2169. return init_rtx_reader_args_cb (argc, argv, 0);
  2170. }
  2171. /* The entry point for reading a single rtx from an md file. Return
  2172. the rtx, or NULL if the md file has been fully processed.
  2173. Return the line where the rtx was found in LINENO.
  2174. Return the number of code generating rtx'en read since the start
  2175. of the md file in SEQNR. */
  2176. rtx
  2177. read_md_rtx (int *lineno, int *seqnr)
  2178. {
  2179. struct queue_elem **queue, *elem;
  2180. rtx desc;
  2181. discard:
  2182. /* Read all patterns from a given queue before moving on to the next. */
  2183. if (define_attr_queue != NULL)
  2184. queue = &define_attr_queue;
  2185. else if (define_pred_queue != NULL)
  2186. queue = &define_pred_queue;
  2187. else if (define_insn_queue != NULL)
  2188. queue = &define_insn_queue;
  2189. else if (other_queue != NULL)
  2190. queue = &other_queue;
  2191. else
  2192. return NULL_RTX;
  2193. elem = *queue;
  2194. *queue = elem->next;
  2195. desc = elem->data;
  2196. read_md_filename = elem->filename;
  2197. *lineno = elem->lineno;
  2198. *seqnr = sequence_num;
  2199. free (elem);
  2200. /* Discard insn patterns which we know can never match (because
  2201. their C test is provably always false). If insn_elision is
  2202. false, our caller needs to see all the patterns. Note that the
  2203. elided patterns are never counted by the sequence numbering; it
  2204. is the caller's responsibility, when insn_elision is false, not
  2205. to use elided pattern numbers for anything. */
  2206. switch (GET_CODE (desc))
  2207. {
  2208. case DEFINE_INSN:
  2209. case DEFINE_EXPAND:
  2210. case DEFINE_SUBST:
  2211. if (maybe_eval_c_test (XSTR (desc, 2)) != 0)
  2212. sequence_num++;
  2213. else if (insn_elision)
  2214. goto discard;
  2215. /* *seqnr is used here so the name table will match caller's
  2216. idea of insn numbering, whether or not elision is active. */
  2217. record_insn_name (*seqnr, XSTR (desc, 0));
  2218. break;
  2219. case DEFINE_SPLIT:
  2220. case DEFINE_PEEPHOLE:
  2221. case DEFINE_PEEPHOLE2:
  2222. if (maybe_eval_c_test (XSTR (desc, 1)) != 0)
  2223. sequence_num++;
  2224. else if (insn_elision)
  2225. goto discard;
  2226. break;
  2227. default:
  2228. break;
  2229. }
  2230. return desc;
  2231. }
  2232. /* Helper functions for insn elision. */
  2233. /* Compute a hash function of a c_test structure, which is keyed
  2234. by its ->expr field. */
  2235. hashval_t
  2236. hash_c_test (const void *x)
  2237. {
  2238. const struct c_test *a = (const struct c_test *) x;
  2239. const unsigned char *base, *s = (const unsigned char *) a->expr;
  2240. hashval_t hash;
  2241. unsigned char c;
  2242. unsigned int len;
  2243. base = s;
  2244. hash = 0;
  2245. while ((c = *s++) != '\0')
  2246. {
  2247. hash += c + (c << 17);
  2248. hash ^= hash >> 2;
  2249. }
  2250. len = s - base;
  2251. hash += len + (len << 17);
  2252. hash ^= hash >> 2;
  2253. return hash;
  2254. }
  2255. /* Compare two c_test expression structures. */
  2256. int
  2257. cmp_c_test (const void *x, const void *y)
  2258. {
  2259. const struct c_test *a = (const struct c_test *) x;
  2260. const struct c_test *b = (const struct c_test *) y;
  2261. return !strcmp (a->expr, b->expr);
  2262. }
  2263. /* Given a string representing a C test expression, look it up in the
  2264. condition_table and report whether or not its value is known
  2265. at compile time. Returns a tristate: 1 for known true, 0 for
  2266. known false, -1 for unknown. */
  2267. int
  2268. maybe_eval_c_test (const char *expr)
  2269. {
  2270. const struct c_test *test;
  2271. struct c_test dummy;
  2272. if (expr[0] == 0)
  2273. return 1;
  2274. dummy.expr = expr;
  2275. test = (const struct c_test *)htab_find (condition_table, &dummy);
  2276. if (!test)
  2277. return -1;
  2278. return test->value;
  2279. }
  2280. /* Record the C test expression EXPR in the condition_table, with
  2281. value VAL. Duplicates clobber previous entries. */
  2282. void
  2283. add_c_test (const char *expr, int value)
  2284. {
  2285. struct c_test *test;
  2286. if (expr[0] == 0)
  2287. return;
  2288. test = XNEW (struct c_test);
  2289. test->expr = expr;
  2290. test->value = value;
  2291. *(htab_find_slot (condition_table, test, INSERT)) = test;
  2292. }
  2293. /* For every C test, call CALLBACK with two arguments: a pointer to
  2294. the condition structure and INFO. Stops when CALLBACK returns zero. */
  2295. void
  2296. traverse_c_tests (htab_trav callback, void *info)
  2297. {
  2298. if (condition_table)
  2299. htab_traverse (condition_table, callback, info);
  2300. }
  2301. /* Helper functions for define_predicate and define_special_predicate
  2302. processing. Shared between genrecog.c and genpreds.c. */
  2303. static htab_t predicate_table;
  2304. struct pred_data *first_predicate;
  2305. static struct pred_data **last_predicate = &first_predicate;
  2306. static hashval_t
  2307. hash_struct_pred_data (const void *ptr)
  2308. {
  2309. return htab_hash_string (((const struct pred_data *)ptr)->name);
  2310. }
  2311. static int
  2312. eq_struct_pred_data (const void *a, const void *b)
  2313. {
  2314. return !strcmp (((const struct pred_data *)a)->name,
  2315. ((const struct pred_data *)b)->name);
  2316. }
  2317. struct pred_data *
  2318. lookup_predicate (const char *name)
  2319. {
  2320. struct pred_data key;
  2321. key.name = name;
  2322. return (struct pred_data *) htab_find (predicate_table, &key);
  2323. }
  2324. /* Record that predicate PRED can accept CODE. */
  2325. void
  2326. add_predicate_code (struct pred_data *pred, enum rtx_code code)
  2327. {
  2328. if (!pred->codes[code])
  2329. {
  2330. pred->num_codes++;
  2331. pred->codes[code] = true;
  2332. if (GET_RTX_CLASS (code) != RTX_CONST_OBJ)
  2333. pred->allows_non_const = true;
  2334. if (code != REG
  2335. && code != SUBREG
  2336. && code != MEM
  2337. && code != CONCAT
  2338. && code != PARALLEL
  2339. && code != STRICT_LOW_PART
  2340. && code != SCRATCH)
  2341. pred->allows_non_lvalue = true;
  2342. if (pred->num_codes == 1)
  2343. pred->singleton = code;
  2344. else if (pred->num_codes == 2)
  2345. pred->singleton = UNKNOWN;
  2346. }
  2347. }
  2348. void
  2349. add_predicate (struct pred_data *pred)
  2350. {
  2351. void **slot = htab_find_slot (predicate_table, pred, INSERT);
  2352. if (*slot)
  2353. {
  2354. error ("duplicate predicate definition for '%s'", pred->name);
  2355. return;
  2356. }
  2357. *slot = pred;
  2358. *last_predicate = pred;
  2359. last_predicate = &pred->next;
  2360. }
  2361. /* This array gives the initial content of the predicate table. It
  2362. has entries for all predicates defined in recog.c. */
  2363. struct std_pred_table
  2364. {
  2365. const char *name;
  2366. bool special;
  2367. bool allows_const_p;
  2368. RTX_CODE codes[NUM_RTX_CODE];
  2369. };
  2370. static const struct std_pred_table std_preds[] = {
  2371. {"general_operand", false, true, {SUBREG, REG, MEM}},
  2372. {"address_operand", true, true, {SUBREG, REG, MEM, PLUS, MINUS, MULT}},
  2373. {"register_operand", false, false, {SUBREG, REG}},
  2374. {"pmode_register_operand", true, false, {SUBREG, REG}},
  2375. {"scratch_operand", false, false, {SCRATCH, REG}},
  2376. {"immediate_operand", false, true, {UNKNOWN}},
  2377. {"const_int_operand", false, false, {CONST_INT}},
  2378. #if TARGET_SUPPORTS_WIDE_INT
  2379. {"const_scalar_int_operand", false, false, {CONST_INT, CONST_WIDE_INT}},
  2380. {"const_double_operand", false, false, {CONST_DOUBLE}},
  2381. #else
  2382. {"const_double_operand", false, false, {CONST_INT, CONST_DOUBLE}},
  2383. #endif
  2384. {"nonimmediate_operand", false, false, {SUBREG, REG, MEM}},
  2385. {"nonmemory_operand", false, true, {SUBREG, REG}},
  2386. {"push_operand", false, false, {MEM}},
  2387. {"pop_operand", false, false, {MEM}},
  2388. {"memory_operand", false, false, {SUBREG, MEM}},
  2389. {"indirect_operand", false, false, {SUBREG, MEM}},
  2390. {"ordered_comparison_operator", false, false, {EQ, NE,
  2391. LE, LT, GE, GT,
  2392. LEU, LTU, GEU, GTU}},
  2393. {"comparison_operator", false, false, {EQ, NE,
  2394. LE, LT, GE, GT,
  2395. LEU, LTU, GEU, GTU,
  2396. UNORDERED, ORDERED,
  2397. UNEQ, UNGE, UNGT,
  2398. UNLE, UNLT, LTGT}}
  2399. };
  2400. #define NUM_KNOWN_STD_PREDS ARRAY_SIZE (std_preds)
  2401. /* Initialize the table of predicate definitions, starting with
  2402. the information we have on generic predicates. */
  2403. static void
  2404. init_predicate_table (void)
  2405. {
  2406. size_t i, j;
  2407. struct pred_data *pred;
  2408. predicate_table = htab_create_alloc (37, hash_struct_pred_data,
  2409. eq_struct_pred_data, 0,
  2410. xcalloc, free);
  2411. for (i = 0; i < NUM_KNOWN_STD_PREDS; i++)
  2412. {
  2413. pred = XCNEW (struct pred_data);
  2414. pred->name = std_preds[i].name;
  2415. pred->special = std_preds[i].special;
  2416. for (j = 0; std_preds[i].codes[j] != 0; j++)
  2417. add_predicate_code (pred, std_preds[i].codes[j]);
  2418. if (std_preds[i].allows_const_p)
  2419. for (j = 0; j < NUM_RTX_CODE; j++)
  2420. if (GET_RTX_CLASS (j) == RTX_CONST_OBJ)
  2421. add_predicate_code (pred, (enum rtx_code) j);
  2422. add_predicate (pred);
  2423. }
  2424. }
  2425. /* These functions allow linkage with print-rtl.c. Also, some generators
  2426. like to annotate their output with insn names. */
  2427. /* Holds an array of names indexed by insn_code_number. */
  2428. static char **insn_name_ptr = 0;
  2429. static int insn_name_ptr_size = 0;
  2430. const char *
  2431. get_insn_name (int code)
  2432. {
  2433. if (code < insn_name_ptr_size)
  2434. return insn_name_ptr[code];
  2435. else
  2436. return NULL;
  2437. }
  2438. static void
  2439. record_insn_name (int code, const char *name)
  2440. {
  2441. static const char *last_real_name = "insn";
  2442. static int last_real_code = 0;
  2443. char *new_name;
  2444. if (insn_name_ptr_size <= code)
  2445. {
  2446. int new_size;
  2447. new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
  2448. insn_name_ptr = XRESIZEVEC (char *, insn_name_ptr, new_size);
  2449. memset (insn_name_ptr + insn_name_ptr_size, 0,
  2450. sizeof (char *) * (new_size - insn_name_ptr_size));
  2451. insn_name_ptr_size = new_size;
  2452. }
  2453. if (!name || name[0] == '\0')
  2454. {
  2455. new_name = XNEWVAR (char, strlen (last_real_name) + 10);
  2456. sprintf (new_name, "%s+%d", last_real_name, code - last_real_code);
  2457. }
  2458. else
  2459. {
  2460. last_real_name = new_name = xstrdup (name);
  2461. last_real_code = code;
  2462. }
  2463. insn_name_ptr[code] = new_name;
  2464. }
  2465. /* Make STATS describe the operands that appear in rtx X. */
  2466. static void
  2467. get_pattern_stats_1 (struct pattern_stats *stats, rtx x)
  2468. {
  2469. RTX_CODE code;
  2470. int i;
  2471. int len;
  2472. const char *fmt;
  2473. if (x == NULL_RTX)
  2474. return;
  2475. code = GET_CODE (x);
  2476. switch (code)
  2477. {
  2478. case MATCH_OPERAND:
  2479. case MATCH_OPERATOR:
  2480. case MATCH_PARALLEL:
  2481. stats->max_opno = MAX (stats->max_opno, XINT (x, 0));
  2482. break;
  2483. case MATCH_DUP:
  2484. case MATCH_OP_DUP:
  2485. case MATCH_PAR_DUP:
  2486. stats->num_dups++;
  2487. stats->max_dup_opno = MAX (stats->max_dup_opno, XINT (x, 0));
  2488. break;
  2489. case MATCH_SCRATCH:
  2490. stats->max_scratch_opno = MAX (stats->max_scratch_opno, XINT (x, 0));
  2491. break;
  2492. default:
  2493. break;
  2494. }
  2495. fmt = GET_RTX_FORMAT (code);
  2496. len = GET_RTX_LENGTH (code);
  2497. for (i = 0; i < len; i++)
  2498. {
  2499. if (fmt[i] == 'e' || fmt[i] == 'u')
  2500. get_pattern_stats_1 (stats, XEXP (x, i));
  2501. else if (fmt[i] == 'E')
  2502. {
  2503. int j;
  2504. for (j = 0; j < XVECLEN (x, i); j++)
  2505. get_pattern_stats_1 (stats, XVECEXP (x, i, j));
  2506. }
  2507. }
  2508. }
  2509. /* Make STATS describe the operands that appear in instruction pattern
  2510. PATTERN. */
  2511. void
  2512. get_pattern_stats (struct pattern_stats *stats, rtvec pattern)
  2513. {
  2514. int i, len;
  2515. stats->max_opno = -1;
  2516. stats->max_dup_opno = -1;
  2517. stats->max_scratch_opno = -1;
  2518. stats->num_dups = 0;
  2519. len = GET_NUM_ELEM (pattern);
  2520. for (i = 0; i < len; i++)
  2521. get_pattern_stats_1 (stats, RTVEC_ELT (pattern, i));
  2522. stats->num_generator_args = stats->max_opno + 1;
  2523. stats->num_insn_operands = MAX (stats->max_opno,
  2524. stats->max_scratch_opno) + 1;
  2525. stats->num_operand_vars = MAX (stats->max_opno,
  2526. MAX (stats->max_dup_opno,
  2527. stats->max_scratch_opno)) + 1;
  2528. }