genmatch.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713
  1. /* Generate pattern matching and transform code shared between
  2. GENERIC and GIMPLE folding code from match-and-simplify description.
  3. Copyright (C) 2014-2015 Free Software Foundation, Inc.
  4. Contributed by Richard Biener <rguenther@suse.de>
  5. and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
  6. This file is part of GCC.
  7. GCC is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 3, or (at your option) any later
  10. version.
  11. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with GCC; see the file COPYING3. If not see
  17. <http://www.gnu.org/licenses/>. */
  18. #include "bconfig.h"
  19. #include <new>
  20. #include "system.h"
  21. #include "coretypes.h"
  22. #include "ggc.h"
  23. #include <cpplib.h>
  24. #include "errors.h"
  25. #include "hashtab.h"
  26. #include "hash-table.h"
  27. #include "hash-map.h"
  28. #include "hash-set.h"
  29. #include "vec.h"
  30. #include "is-a.h"
  31. /* Stubs for GGC referenced through instantiations triggered by hash-map. */
  32. void *ggc_internal_cleared_alloc (size_t, void (*)(void *),
  33. size_t, size_t MEM_STAT_DECL)
  34. {
  35. return NULL;
  36. }
  37. void ggc_free (void *)
  38. {
  39. }
  40. /* libccp helpers. */
  41. static struct line_maps *line_table;
  42. static bool
  43. #if GCC_VERSION >= 4001
  44. __attribute__((format (printf, 6, 0)))
  45. #endif
  46. error_cb (cpp_reader *, int errtype, int, source_location location,
  47. unsigned int, const char *msg, va_list *ap)
  48. {
  49. const line_map *map;
  50. linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
  51. expanded_location loc = linemap_expand_location (line_table, map, location);
  52. fprintf (stderr, "%s:%d:%d %s: ", loc.file, loc.line, loc.column,
  53. (errtype == CPP_DL_WARNING) ? "warning" : "error");
  54. vfprintf (stderr, msg, *ap);
  55. fprintf (stderr, "\n");
  56. FILE *f = fopen (loc.file, "r");
  57. if (f)
  58. {
  59. char buf[128];
  60. while (loc.line > 0)
  61. {
  62. if (!fgets (buf, 128, f))
  63. goto notfound;
  64. if (buf[strlen (buf) - 1] != '\n')
  65. {
  66. if (loc.line > 1)
  67. loc.line++;
  68. }
  69. loc.line--;
  70. }
  71. fprintf (stderr, "%s", buf);
  72. for (int i = 0; i < loc.column - 1; ++i)
  73. fputc (' ', stderr);
  74. fputc ('^', stderr);
  75. fputc ('\n', stderr);
  76. notfound:
  77. fclose (f);
  78. }
  79. if (errtype == CPP_DL_FATAL)
  80. exit (1);
  81. return false;
  82. }
  83. static void
  84. #if GCC_VERSION >= 4001
  85. __attribute__((format (printf, 2, 3)))
  86. #endif
  87. fatal_at (const cpp_token *tk, const char *msg, ...)
  88. {
  89. va_list ap;
  90. va_start (ap, msg);
  91. error_cb (NULL, CPP_DL_FATAL, 0, tk->src_loc, 0, msg, &ap);
  92. va_end (ap);
  93. }
  94. static void
  95. #if GCC_VERSION >= 4001
  96. __attribute__((format (printf, 2, 3)))
  97. #endif
  98. fatal_at (source_location loc, const char *msg, ...)
  99. {
  100. va_list ap;
  101. va_start (ap, msg);
  102. error_cb (NULL, CPP_DL_FATAL, 0, loc, 0, msg, &ap);
  103. va_end (ap);
  104. }
  105. static void
  106. #if GCC_VERSION >= 4001
  107. __attribute__((format (printf, 2, 3)))
  108. #endif
  109. warning_at (const cpp_token *tk, const char *msg, ...)
  110. {
  111. va_list ap;
  112. va_start (ap, msg);
  113. error_cb (NULL, CPP_DL_WARNING, 0, tk->src_loc, 0, msg, &ap);
  114. va_end (ap);
  115. }
  116. static void
  117. output_line_directive (FILE *f, source_location location,
  118. bool dumpfile = false)
  119. {
  120. const line_map *map;
  121. linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
  122. expanded_location loc = linemap_expand_location (line_table, map, location);
  123. if (dumpfile)
  124. {
  125. /* When writing to a dumpfile only dump the filename. */
  126. const char *file = strrchr (loc.file, DIR_SEPARATOR);
  127. if (!file)
  128. file = loc.file;
  129. else
  130. ++file;
  131. fprintf (f, "%s:%d", file, loc.line);
  132. }
  133. else
  134. /* Other gen programs really output line directives here, at least for
  135. development it's right now more convenient to have line information
  136. from the generated file. Still keep the directives as comment for now
  137. to easily back-point to the meta-description. */
  138. fprintf (f, "/* #line %d \"%s\" */\n", loc.line, loc.file);
  139. }
  140. /* Pull in tree codes and builtin function codes from their
  141. definition files. */
  142. #define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
  143. enum tree_code {
  144. #include "tree.def"
  145. CONVERT0,
  146. CONVERT1,
  147. CONVERT2,
  148. MAX_TREE_CODES
  149. };
  150. #undef DEFTREECODE
  151. #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM,
  152. enum built_in_function {
  153. #include "builtins.def"
  154. END_BUILTINS
  155. };
  156. #undef DEF_BUILTIN
  157. /* Base class for all identifiers the parser knows. */
  158. struct id_base : typed_noop_remove<id_base>
  159. {
  160. enum id_kind { CODE, FN, PREDICATE, USER } kind;
  161. id_base (id_kind, const char *, int = -1);
  162. hashval_t hashval;
  163. int nargs;
  164. const char *id;
  165. /* hash_table support. */
  166. typedef id_base value_type;
  167. typedef id_base compare_type;
  168. static inline hashval_t hash (const value_type *);
  169. static inline int equal (const value_type *, const compare_type *);
  170. };
  171. inline hashval_t
  172. id_base::hash (const value_type *op)
  173. {
  174. return op->hashval;
  175. }
  176. inline int
  177. id_base::equal (const value_type *op1,
  178. const compare_type *op2)
  179. {
  180. return (op1->hashval == op2->hashval
  181. && strcmp (op1->id, op2->id) == 0);
  182. }
  183. /* Hashtable of known pattern operators. This is pre-seeded from
  184. all known tree codes and all known builtin function ids. */
  185. static hash_table<id_base> *operators;
  186. id_base::id_base (id_kind kind_, const char *id_, int nargs_)
  187. {
  188. kind = kind_;
  189. id = id_;
  190. nargs = nargs_;
  191. hashval = htab_hash_string (id);
  192. }
  193. /* Identifier that maps to a tree code. */
  194. struct operator_id : public id_base
  195. {
  196. operator_id (enum tree_code code_, const char *id_, unsigned nargs_,
  197. const char *tcc_)
  198. : id_base (id_base::CODE, id_, nargs_), code (code_), tcc (tcc_) {}
  199. enum tree_code code;
  200. const char *tcc;
  201. };
  202. /* Identifier that maps to a builtin function code. */
  203. struct fn_id : public id_base
  204. {
  205. fn_id (enum built_in_function fn_, const char *id_)
  206. : id_base (id_base::FN, id_), fn (fn_) {}
  207. enum built_in_function fn;
  208. };
  209. struct simplify;
  210. /* Identifier that maps to a user-defined predicate. */
  211. struct predicate_id : public id_base
  212. {
  213. predicate_id (const char *id_)
  214. : id_base (id_base::PREDICATE, id_), matchers (vNULL) {}
  215. vec<simplify *> matchers;
  216. };
  217. /* Identifier that maps to a operator defined by a 'for' directive. */
  218. struct user_id : public id_base
  219. {
  220. user_id (const char *id_, bool is_oper_list_ = false)
  221. : id_base (id_base::USER, id_), substitutes (vNULL),
  222. used (false), is_oper_list (is_oper_list_) {}
  223. vec<id_base *> substitutes;
  224. bool used;
  225. bool is_oper_list;
  226. };
  227. template<>
  228. template<>
  229. inline bool
  230. is_a_helper <fn_id *>::test (id_base *id)
  231. {
  232. return id->kind == id_base::FN;
  233. }
  234. template<>
  235. template<>
  236. inline bool
  237. is_a_helper <operator_id *>::test (id_base *id)
  238. {
  239. return id->kind == id_base::CODE;
  240. }
  241. template<>
  242. template<>
  243. inline bool
  244. is_a_helper <predicate_id *>::test (id_base *id)
  245. {
  246. return id->kind == id_base::PREDICATE;
  247. }
  248. template<>
  249. template<>
  250. inline bool
  251. is_a_helper <user_id *>::test (id_base *id)
  252. {
  253. return id->kind == id_base::USER;
  254. }
  255. /* Add a predicate identifier to the hash. */
  256. static predicate_id *
  257. add_predicate (const char *id)
  258. {
  259. predicate_id *p = new predicate_id (id);
  260. id_base **slot = operators->find_slot_with_hash (p, p->hashval, INSERT);
  261. if (*slot)
  262. fatal ("duplicate id definition");
  263. *slot = p;
  264. return p;
  265. }
  266. /* Add a tree code identifier to the hash. */
  267. static void
  268. add_operator (enum tree_code code, const char *id,
  269. const char *tcc, unsigned nargs)
  270. {
  271. if (strcmp (tcc, "tcc_unary") != 0
  272. && strcmp (tcc, "tcc_binary") != 0
  273. && strcmp (tcc, "tcc_comparison") != 0
  274. && strcmp (tcc, "tcc_expression") != 0
  275. /* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */
  276. && strcmp (tcc, "tcc_reference") != 0
  277. /* To have INTEGER_CST and friends as "predicate operators". */
  278. && strcmp (tcc, "tcc_constant") != 0
  279. /* And allow CONSTRUCTOR for vector initializers. */
  280. && !(code == CONSTRUCTOR))
  281. return;
  282. operator_id *op = new operator_id (code, id, nargs, tcc);
  283. id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
  284. if (*slot)
  285. fatal ("duplicate id definition");
  286. *slot = op;
  287. }
  288. /* Add a builtin identifier to the hash. */
  289. static void
  290. add_builtin (enum built_in_function code, const char *id)
  291. {
  292. fn_id *fn = new fn_id (code, id);
  293. id_base **slot = operators->find_slot_with_hash (fn, fn->hashval, INSERT);
  294. if (*slot)
  295. fatal ("duplicate id definition");
  296. *slot = fn;
  297. }
  298. /* Helper for easy comparing ID with tree code CODE. */
  299. static bool
  300. operator==(id_base &id, enum tree_code code)
  301. {
  302. if (operator_id *oid = dyn_cast <operator_id *> (&id))
  303. return oid->code == code;
  304. return false;
  305. }
  306. /* Lookup the identifier ID. */
  307. id_base *
  308. get_operator (const char *id)
  309. {
  310. id_base tem (id_base::CODE, id);
  311. id_base *op = operators->find_with_hash (&tem, tem.hashval);
  312. if (op)
  313. {
  314. /* If this is a user-defined identifier track whether it was used. */
  315. if (user_id *uid = dyn_cast<user_id *> (op))
  316. uid->used = true;
  317. return op;
  318. }
  319. /* Try all-uppercase. */
  320. char *id2 = xstrdup (id);
  321. for (unsigned i = 0; i < strlen (id2); ++i)
  322. id2[i] = TOUPPER (id2[i]);
  323. new (&tem) id_base (id_base::CODE, id2);
  324. op = operators->find_with_hash (&tem, tem.hashval);
  325. if (op)
  326. {
  327. free (id2);
  328. return op;
  329. }
  330. /* Try _EXPR appended. */
  331. id2 = (char *)xrealloc (id2, strlen (id2) + sizeof ("_EXPR") + 1);
  332. strcat (id2, "_EXPR");
  333. new (&tem) id_base (id_base::CODE, id2);
  334. op = operators->find_with_hash (&tem, tem.hashval);
  335. if (op)
  336. {
  337. free (id2);
  338. return op;
  339. }
  340. return 0;
  341. }
  342. /* Helper for the capture-id map. */
  343. struct capture_id_map_hasher : default_hashmap_traits
  344. {
  345. static inline hashval_t hash (const char *);
  346. static inline bool equal_keys (const char *, const char *);
  347. };
  348. inline hashval_t
  349. capture_id_map_hasher::hash (const char *id)
  350. {
  351. return htab_hash_string (id);
  352. }
  353. inline bool
  354. capture_id_map_hasher::equal_keys (const char *id1, const char *id2)
  355. {
  356. return strcmp (id1, id2) == 0;
  357. }
  358. typedef hash_map<const char *, unsigned, capture_id_map_hasher> cid_map_t;
  359. /* The AST produced by parsing of the pattern definitions. */
  360. struct dt_operand;
  361. struct capture_info;
  362. /* The base class for operands. */
  363. struct operand {
  364. enum op_type { OP_PREDICATE, OP_EXPR, OP_CAPTURE, OP_C_EXPR };
  365. operand (enum op_type type_) : type (type_) {}
  366. enum op_type type;
  367. virtual void gen_transform (FILE *, const char *, bool, int,
  368. const char *, capture_info *,
  369. dt_operand ** = 0,
  370. bool = true)
  371. { gcc_unreachable (); }
  372. };
  373. /* A predicate operand. Predicates are leafs in the AST. */
  374. struct predicate : public operand
  375. {
  376. predicate (predicate_id *p_) : operand (OP_PREDICATE), p (p_) {}
  377. predicate_id *p;
  378. };
  379. /* An operand that constitutes an expression. Expressions include
  380. function calls and user-defined predicate invocations. */
  381. struct expr : public operand
  382. {
  383. expr (id_base *operation_, bool is_commutative_ = false)
  384. : operand (OP_EXPR), operation (operation_),
  385. ops (vNULL), expr_type (NULL), is_commutative (is_commutative_),
  386. is_generic (false) {}
  387. void append_op (operand *op) { ops.safe_push (op); }
  388. /* The operator and its operands. */
  389. id_base *operation;
  390. vec<operand *> ops;
  391. /* An explicitely specified type - used exclusively for conversions. */
  392. const char *expr_type;
  393. /* Whether the operation is to be applied commutatively. This is
  394. later lowered to two separate patterns. */
  395. bool is_commutative;
  396. /* Whether the expression is expected to be in GENERIC form. */
  397. bool is_generic;
  398. virtual void gen_transform (FILE *f, const char *, bool, int,
  399. const char *, capture_info *,
  400. dt_operand ** = 0, bool = true);
  401. };
  402. /* An operator that is represented by native C code. This is always
  403. a leaf operand in the AST. This class is also used to represent
  404. the code to be generated for 'if' and 'with' expressions. */
  405. struct c_expr : public operand
  406. {
  407. /* A mapping of an identifier and its replacement. Used to apply
  408. 'for' lowering. */
  409. struct id_tab {
  410. const char *id;
  411. const char *oper;
  412. id_tab (const char *id_, const char *oper_): id (id_), oper (oper_) {}
  413. };
  414. c_expr (cpp_reader *r_, vec<cpp_token> code_, unsigned nr_stmts_,
  415. vec<id_tab> ids_, cid_map_t *capture_ids_)
  416. : operand (OP_C_EXPR), r (r_), code (code_), capture_ids (capture_ids_),
  417. nr_stmts (nr_stmts_), ids (ids_) {}
  418. /* cpplib tokens and state to transform this back to source. */
  419. cpp_reader *r;
  420. vec<cpp_token> code;
  421. cid_map_t *capture_ids;
  422. /* The number of statements parsed (well, the number of ';'s). */
  423. unsigned nr_stmts;
  424. /* The identifier replacement vector. */
  425. vec<id_tab> ids;
  426. virtual void gen_transform (FILE *f, const char *, bool, int,
  427. const char *, capture_info *,
  428. dt_operand ** = 0, bool = true);
  429. };
  430. /* A wrapper around another operand that captures its value. */
  431. struct capture : public operand
  432. {
  433. capture (unsigned where_, operand *what_)
  434. : operand (OP_CAPTURE), where (where_), what (what_) {}
  435. /* Identifier index for the value. */
  436. unsigned where;
  437. /* The captured value. */
  438. operand *what;
  439. virtual void gen_transform (FILE *f, const char *, bool, int,
  440. const char *, capture_info *,
  441. dt_operand ** = 0, bool = true);
  442. };
  443. template<>
  444. template<>
  445. inline bool
  446. is_a_helper <capture *>::test (operand *op)
  447. {
  448. return op->type == operand::OP_CAPTURE;
  449. }
  450. template<>
  451. template<>
  452. inline bool
  453. is_a_helper <predicate *>::test (operand *op)
  454. {
  455. return op->type == operand::OP_PREDICATE;
  456. }
  457. template<>
  458. template<>
  459. inline bool
  460. is_a_helper <c_expr *>::test (operand *op)
  461. {
  462. return op->type == operand::OP_C_EXPR;
  463. }
  464. template<>
  465. template<>
  466. inline bool
  467. is_a_helper <expr *>::test (operand *op)
  468. {
  469. return op->type == operand::OP_EXPR;
  470. }
  471. /* Helper to distinguish 'if' from 'with' expressions. */
  472. struct if_or_with
  473. {
  474. if_or_with (operand *cexpr_, source_location location_, bool is_with_)
  475. : location (location_), cexpr (cexpr_), is_with (is_with_) {}
  476. source_location location;
  477. operand *cexpr;
  478. bool is_with;
  479. };
  480. /* The main class of a pattern and its transform. This is used to
  481. represent both (simplify ...) and (match ...) kinds. The AST
  482. duplicates all outer 'if' and 'for' expressions here so each
  483. simplify can exist in isolation. */
  484. struct simplify
  485. {
  486. simplify (operand *match_, source_location match_location_,
  487. struct operand *result_, source_location result_location_,
  488. vec<if_or_with> ifexpr_vec_, vec<vec<user_id *> > for_vec_,
  489. cid_map_t *capture_ids_)
  490. : match (match_), match_location (match_location_),
  491. result (result_), result_location (result_location_),
  492. ifexpr_vec (ifexpr_vec_), for_vec (for_vec_),
  493. capture_ids (capture_ids_), capture_max (capture_ids_->elements () - 1) {}
  494. /* The expression that is matched against the GENERIC or GIMPLE IL. */
  495. operand *match;
  496. source_location match_location;
  497. /* For a (simplify ...) the expression produced when the pattern applies.
  498. For a (match ...) either NULL if it is a simple predicate or the
  499. single expression specifying the matched operands. */
  500. struct operand *result;
  501. source_location result_location;
  502. /* Collected 'if' expressions that need to evaluate to true to make
  503. the pattern apply. */
  504. vec<if_or_with> ifexpr_vec;
  505. /* Collected 'for' expression operators that have to be replaced
  506. in the lowering phase. */
  507. vec<vec<user_id *> > for_vec;
  508. /* A map of capture identifiers to indexes. */
  509. cid_map_t *capture_ids;
  510. int capture_max;
  511. };
  512. /* Debugging routines for dumping the AST. */
  513. DEBUG_FUNCTION void
  514. print_operand (operand *o, FILE *f = stderr, bool flattened = false)
  515. {
  516. if (capture *c = dyn_cast<capture *> (o))
  517. {
  518. fprintf (f, "@%u", c->where);
  519. if (c->what && flattened == false)
  520. {
  521. putc (':', f);
  522. print_operand (c->what, f, flattened);
  523. putc (' ', f);
  524. }
  525. }
  526. else if (predicate *p = dyn_cast<predicate *> (o))
  527. fprintf (f, "%s", p->p->id);
  528. else if (is_a<c_expr *> (o))
  529. fprintf (f, "c_expr");
  530. else if (expr *e = dyn_cast<expr *> (o))
  531. {
  532. fprintf (f, "(%s", e->operation->id);
  533. if (flattened == false)
  534. {
  535. putc (' ', f);
  536. for (unsigned i = 0; i < e->ops.length (); ++i)
  537. {
  538. print_operand (e->ops[i], f, flattened);
  539. putc (' ', f);
  540. }
  541. }
  542. putc (')', f);
  543. }
  544. else
  545. gcc_unreachable ();
  546. }
  547. DEBUG_FUNCTION void
  548. print_matches (struct simplify *s, FILE *f = stderr)
  549. {
  550. fprintf (f, "for expression: ");
  551. print_operand (s->match, f);
  552. putc ('\n', f);
  553. }
  554. /* AST lowering. */
  555. /* Lowering of commutative operators. */
  556. static void
  557. cartesian_product (const vec< vec<operand *> >& ops_vector,
  558. vec< vec<operand *> >& result, vec<operand *>& v, unsigned n)
  559. {
  560. if (n == ops_vector.length ())
  561. {
  562. vec<operand *> xv = v.copy ();
  563. result.safe_push (xv);
  564. return;
  565. }
  566. for (unsigned i = 0; i < ops_vector[n].length (); ++i)
  567. {
  568. v[n] = ops_vector[n][i];
  569. cartesian_product (ops_vector, result, v, n + 1);
  570. }
  571. }
  572. /* Lower OP to two operands in case it is marked as commutative. */
  573. static vec<operand *>
  574. commutate (operand *op)
  575. {
  576. vec<operand *> ret = vNULL;
  577. if (capture *c = dyn_cast <capture *> (op))
  578. {
  579. if (!c->what)
  580. {
  581. ret.safe_push (op);
  582. return ret;
  583. }
  584. vec<operand *> v = commutate (c->what);
  585. for (unsigned i = 0; i < v.length (); ++i)
  586. {
  587. capture *nc = new capture (c->where, v[i]);
  588. ret.safe_push (nc);
  589. }
  590. return ret;
  591. }
  592. expr *e = dyn_cast <expr *> (op);
  593. if (!e || e->ops.length () == 0)
  594. {
  595. ret.safe_push (op);
  596. return ret;
  597. }
  598. vec< vec<operand *> > ops_vector = vNULL;
  599. for (unsigned i = 0; i < e->ops.length (); ++i)
  600. ops_vector.safe_push (commutate (e->ops[i]));
  601. auto_vec< vec<operand *> > result;
  602. auto_vec<operand *> v (e->ops.length ());
  603. v.quick_grow_cleared (e->ops.length ());
  604. cartesian_product (ops_vector, result, v, 0);
  605. for (unsigned i = 0; i < result.length (); ++i)
  606. {
  607. expr *ne = new expr (e->operation);
  608. for (unsigned j = 0; j < result[i].length (); ++j)
  609. ne->append_op (result[i][j]);
  610. ret.safe_push (ne);
  611. }
  612. if (!e->is_commutative)
  613. return ret;
  614. for (unsigned i = 0; i < result.length (); ++i)
  615. {
  616. expr *ne = new expr (e->operation);
  617. // result[i].length () is 2 since e->operation is binary
  618. for (unsigned j = result[i].length (); j; --j)
  619. ne->append_op (result[i][j-1]);
  620. ret.safe_push (ne);
  621. }
  622. return ret;
  623. }
  624. /* Lower operations marked as commutative in the AST of S and push
  625. the resulting patterns to SIMPLIFIERS. */
  626. static void
  627. lower_commutative (simplify *s, vec<simplify *>& simplifiers)
  628. {
  629. vec<operand *> matchers = commutate (s->match);
  630. for (unsigned i = 0; i < matchers.length (); ++i)
  631. {
  632. simplify *ns = new simplify (matchers[i], s->match_location,
  633. s->result, s->result_location, s->ifexpr_vec,
  634. s->for_vec, s->capture_ids);
  635. simplifiers.safe_push (ns);
  636. }
  637. }
  638. /* Strip conditional conversios using operator OPER from O and its
  639. children if STRIP, else replace them with an unconditional convert. */
  640. operand *
  641. lower_opt_convert (operand *o, enum tree_code oper, bool strip)
  642. {
  643. if (capture *c = dyn_cast<capture *> (o))
  644. {
  645. if (c->what)
  646. return new capture (c->where, lower_opt_convert (c->what, oper, strip));
  647. else
  648. return c;
  649. }
  650. expr *e = dyn_cast<expr *> (o);
  651. if (!e)
  652. return o;
  653. if (*e->operation == oper)
  654. {
  655. if (strip)
  656. return lower_opt_convert (e->ops[0], oper, strip);
  657. expr *ne = new expr (get_operator ("CONVERT_EXPR"));
  658. ne->append_op (lower_opt_convert (e->ops[0], oper, strip));
  659. return ne;
  660. }
  661. expr *ne = new expr (e->operation, e->is_commutative);
  662. for (unsigned i = 0; i < e->ops.length (); ++i)
  663. ne->append_op (lower_opt_convert (e->ops[i], oper, strip));
  664. return ne;
  665. }
  666. /* Determine whether O or its children uses the conditional conversion
  667. operator OPER. */
  668. static bool
  669. has_opt_convert (operand *o, enum tree_code oper)
  670. {
  671. if (capture *c = dyn_cast<capture *> (o))
  672. {
  673. if (c->what)
  674. return has_opt_convert (c->what, oper);
  675. else
  676. return false;
  677. }
  678. expr *e = dyn_cast<expr *> (o);
  679. if (!e)
  680. return false;
  681. if (*e->operation == oper)
  682. return true;
  683. for (unsigned i = 0; i < e->ops.length (); ++i)
  684. if (has_opt_convert (e->ops[i], oper))
  685. return true;
  686. return false;
  687. }
  688. /* Lower conditional convert operators in O, expanding it to a vector
  689. if required. */
  690. static vec<operand *>
  691. lower_opt_convert (operand *o)
  692. {
  693. vec<operand *> v1 = vNULL, v2;
  694. v1.safe_push (o);
  695. enum tree_code opers[] = { CONVERT0, CONVERT1, CONVERT2 };
  696. /* Conditional converts are lowered to a pattern with the
  697. conversion and one without. The three different conditional
  698. convert codes are lowered separately. */
  699. for (unsigned i = 0; i < 3; ++i)
  700. {
  701. v2 = vNULL;
  702. for (unsigned j = 0; j < v1.length (); ++j)
  703. if (has_opt_convert (v1[j], opers[i]))
  704. {
  705. v2.safe_push (lower_opt_convert (v1[j], opers[i], false));
  706. v2.safe_push (lower_opt_convert (v1[j], opers[i], true));
  707. }
  708. if (v2 != vNULL)
  709. {
  710. v1 = vNULL;
  711. for (unsigned j = 0; j < v2.length (); ++j)
  712. v1.safe_push (v2[j]);
  713. }
  714. }
  715. return v1;
  716. }
  717. /* Lower conditional convert operators in the AST of S and push
  718. the resulting multiple patterns to SIMPLIFIERS. */
  719. static void
  720. lower_opt_convert (simplify *s, vec<simplify *>& simplifiers)
  721. {
  722. vec<operand *> matchers = lower_opt_convert (s->match);
  723. for (unsigned i = 0; i < matchers.length (); ++i)
  724. {
  725. simplify *ns = new simplify (matchers[i], s->match_location,
  726. s->result, s->result_location, s->ifexpr_vec,
  727. s->for_vec, s->capture_ids);
  728. simplifiers.safe_push (ns);
  729. }
  730. }
  731. /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
  732. GENERIC and a GIMPLE variant. */
  733. static vec<operand *>
  734. lower_cond (operand *o)
  735. {
  736. vec<operand *> ro = vNULL;
  737. if (capture *c = dyn_cast<capture *> (o))
  738. {
  739. if (c->what)
  740. {
  741. vec<operand *> lop = vNULL;
  742. lop = lower_cond (c->what);
  743. for (unsigned i = 0; i < lop.length (); ++i)
  744. ro.safe_push (new capture (c->where, lop[i]));
  745. return ro;
  746. }
  747. }
  748. expr *e = dyn_cast<expr *> (o);
  749. if (!e || e->ops.length () == 0)
  750. {
  751. ro.safe_push (o);
  752. return ro;
  753. }
  754. vec< vec<operand *> > ops_vector = vNULL;
  755. for (unsigned i = 0; i < e->ops.length (); ++i)
  756. ops_vector.safe_push (lower_cond (e->ops[i]));
  757. auto_vec< vec<operand *> > result;
  758. auto_vec<operand *> v (e->ops.length ());
  759. v.quick_grow_cleared (e->ops.length ());
  760. cartesian_product (ops_vector, result, v, 0);
  761. for (unsigned i = 0; i < result.length (); ++i)
  762. {
  763. expr *ne = new expr (e->operation);
  764. for (unsigned j = 0; j < result[i].length (); ++j)
  765. ne->append_op (result[i][j]);
  766. ro.safe_push (ne);
  767. /* If this is a COND with a captured expression or an
  768. expression with two operands then also match a GENERIC
  769. form on the compare. */
  770. if ((*e->operation == COND_EXPR
  771. || *e->operation == VEC_COND_EXPR)
  772. && ((is_a <capture *> (e->ops[0])
  773. && as_a <capture *> (e->ops[0])->what
  774. && is_a <expr *> (as_a <capture *> (e->ops[0])->what)
  775. && as_a <expr *>
  776. (as_a <capture *> (e->ops[0])->what)->ops.length () == 2)
  777. || (is_a <expr *> (e->ops[0])
  778. && as_a <expr *> (e->ops[0])->ops.length () == 2)))
  779. {
  780. expr *ne = new expr (e->operation);
  781. for (unsigned j = 0; j < result[i].length (); ++j)
  782. ne->append_op (result[i][j]);
  783. if (capture *c = dyn_cast <capture *> (ne->ops[0]))
  784. {
  785. expr *ocmp = as_a <expr *> (c->what);
  786. expr *cmp = new expr (ocmp->operation);
  787. for (unsigned j = 0; j < ocmp->ops.length (); ++j)
  788. cmp->append_op (ocmp->ops[j]);
  789. cmp->is_generic = true;
  790. ne->ops[0] = new capture (c->where, cmp);
  791. }
  792. else
  793. {
  794. expr *ocmp = as_a <expr *> (ne->ops[0]);
  795. expr *cmp = new expr (ocmp->operation);
  796. for (unsigned j = 0; j < ocmp->ops.length (); ++j)
  797. cmp->append_op (ocmp->ops[j]);
  798. cmp->is_generic = true;
  799. ne->ops[0] = cmp;
  800. }
  801. ro.safe_push (ne);
  802. }
  803. }
  804. return ro;
  805. }
  806. /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
  807. GENERIC and a GIMPLE variant. */
  808. static void
  809. lower_cond (simplify *s, vec<simplify *>& simplifiers)
  810. {
  811. vec<operand *> matchers = lower_cond (s->match);
  812. for (unsigned i = 0; i < matchers.length (); ++i)
  813. {
  814. simplify *ns = new simplify (matchers[i], s->match_location,
  815. s->result, s->result_location, s->ifexpr_vec,
  816. s->for_vec, s->capture_ids);
  817. simplifiers.safe_push (ns);
  818. }
  819. }
  820. /* In AST operand O replace operator ID with operator WITH. */
  821. operand *
  822. replace_id (operand *o, user_id *id, id_base *with)
  823. {
  824. /* Deep-copy captures and expressions, replacing operations as
  825. needed. */
  826. if (capture *c = dyn_cast<capture *> (o))
  827. {
  828. if (!c->what)
  829. return c;
  830. return new capture (c->where, replace_id (c->what, id, with));
  831. }
  832. else if (expr *e = dyn_cast<expr *> (o))
  833. {
  834. expr *ne = new expr (e->operation == id ? with : e->operation,
  835. e->is_commutative);
  836. ne->expr_type = e->expr_type;
  837. for (unsigned i = 0; i < e->ops.length (); ++i)
  838. ne->append_op (replace_id (e->ops[i], id, with));
  839. return ne;
  840. }
  841. /* For c_expr we simply record a string replacement table which is
  842. applied at code-generation time. */
  843. if (c_expr *ce = dyn_cast<c_expr *> (o))
  844. {
  845. vec<c_expr::id_tab> ids = ce->ids.copy ();
  846. ids.safe_push (c_expr::id_tab (id->id, with->id));
  847. return new c_expr (ce->r, ce->code, ce->nr_stmts, ids, ce->capture_ids);
  848. }
  849. return o;
  850. }
  851. /* Lower recorded fors for SIN and output to SIMPLIFIERS. */
  852. static void
  853. lower_for (simplify *sin, vec<simplify *>& simplifiers)
  854. {
  855. vec<vec<user_id *> >& for_vec = sin->for_vec;
  856. unsigned worklist_start = 0;
  857. auto_vec<simplify *> worklist;
  858. worklist.safe_push (sin);
  859. /* Lower each recorded for separately, operating on the
  860. set of simplifiers created by the previous one.
  861. Lower inner-to-outer so inner for substitutes can refer
  862. to operators replaced by outer fors. */
  863. for (int fi = for_vec.length () - 1; fi >= 0; --fi)
  864. {
  865. vec<user_id *>& ids = for_vec[fi];
  866. unsigned n_ids = ids.length ();
  867. unsigned max_n_opers = 0;
  868. for (unsigned i = 0; i < n_ids; ++i)
  869. if (ids[i]->substitutes.length () > max_n_opers)
  870. max_n_opers = ids[i]->substitutes.length ();
  871. unsigned worklist_end = worklist.length ();
  872. for (unsigned si = worklist_start; si < worklist_end; ++si)
  873. {
  874. simplify *s = worklist[si];
  875. for (unsigned j = 0; j < max_n_opers; ++j)
  876. {
  877. operand *match_op = s->match;
  878. operand *result_op = s->result;
  879. vec<if_or_with> ifexpr_vec = s->ifexpr_vec.copy ();
  880. for (unsigned i = 0; i < n_ids; ++i)
  881. {
  882. user_id *id = ids[i];
  883. id_base *oper = id->substitutes[j % id->substitutes.length ()];
  884. match_op = replace_id (match_op, id, oper);
  885. if (result_op)
  886. result_op = replace_id (result_op, id, oper);
  887. for (unsigned k = 0; k < s->ifexpr_vec.length (); ++k)
  888. ifexpr_vec[k].cexpr = replace_id (ifexpr_vec[k].cexpr,
  889. id, oper);
  890. }
  891. simplify *ns = new simplify (match_op, s->match_location,
  892. result_op, s->result_location,
  893. ifexpr_vec, vNULL, s->capture_ids);
  894. worklist.safe_push (ns);
  895. }
  896. }
  897. worklist_start = worklist_end;
  898. }
  899. /* Copy out the result from the last for lowering. */
  900. for (unsigned i = worklist_start; i < worklist.length (); ++i)
  901. simplifiers.safe_push (worklist[i]);
  902. }
  903. /* Lower the AST for everything in SIMPLIFIERS. */
  904. static void
  905. lower (vec<simplify *>& simplifiers, bool gimple)
  906. {
  907. auto_vec<simplify *> out_simplifiers;
  908. for (unsigned i = 0; i < simplifiers.length (); ++i)
  909. lower_opt_convert (simplifiers[i], out_simplifiers);
  910. simplifiers.truncate (0);
  911. for (unsigned i = 0; i < out_simplifiers.length (); ++i)
  912. lower_commutative (out_simplifiers[i], simplifiers);
  913. out_simplifiers.truncate (0);
  914. if (gimple)
  915. for (unsigned i = 0; i < simplifiers.length (); ++i)
  916. lower_cond (simplifiers[i], out_simplifiers);
  917. else
  918. out_simplifiers.safe_splice (simplifiers);
  919. simplifiers.truncate (0);
  920. for (unsigned i = 0; i < out_simplifiers.length (); ++i)
  921. lower_for (out_simplifiers[i], simplifiers);
  922. }
  923. /* The decision tree built for generating GIMPLE and GENERIC pattern
  924. matching code. It represents the 'match' expression of all
  925. simplifies and has those as its leafs. */
  926. /* Decision tree base class, used for DT_TRUE and DT_NODE. */
  927. struct dt_node
  928. {
  929. enum dt_type { DT_NODE, DT_OPERAND, DT_TRUE, DT_MATCH, DT_SIMPLIFY };
  930. enum dt_type type;
  931. unsigned level;
  932. vec<dt_node *> kids;
  933. dt_node (enum dt_type type_): type (type_), level (0), kids (vNULL) {}
  934. dt_node *append_node (dt_node *);
  935. dt_node *append_op (operand *, dt_node *parent = 0, unsigned pos = 0);
  936. dt_node *append_true_op (dt_node *parent = 0, unsigned pos = 0);
  937. dt_node *append_match_op (dt_operand *, dt_node *parent = 0, unsigned pos = 0);
  938. dt_node *append_simplify (simplify *, unsigned, dt_operand **);
  939. virtual void gen (FILE *, bool) {}
  940. void gen_kids (FILE *, bool);
  941. void gen_kids_1 (FILE *, bool,
  942. vec<dt_operand *>, vec<dt_operand *>, vec<dt_operand *>,
  943. vec<dt_operand *>, vec<dt_operand *>, vec<dt_node *>);
  944. };
  945. /* Generic decision tree node used for DT_OPERAND and DT_MATCH. */
  946. struct dt_operand : public dt_node
  947. {
  948. operand *op;
  949. dt_operand *match_dop;
  950. dt_operand *parent;
  951. unsigned pos;
  952. dt_operand (enum dt_type type, operand *op_, dt_operand *match_dop_,
  953. dt_operand *parent_ = 0, unsigned pos_ = 0)
  954. : dt_node (type), op (op_), match_dop (match_dop_),
  955. parent (parent_), pos (pos_) {}
  956. void gen (FILE *, bool);
  957. unsigned gen_predicate (FILE *, const char *, bool);
  958. unsigned gen_match_op (FILE *, const char *);
  959. unsigned gen_gimple_expr (FILE *);
  960. unsigned gen_generic_expr (FILE *, const char *);
  961. char *get_name (char *);
  962. void gen_opname (char *, unsigned);
  963. };
  964. /* Leaf node of the decision tree, used for DT_SIMPLIFY. */
  965. struct dt_simplify : public dt_node
  966. {
  967. simplify *s;
  968. unsigned pattern_no;
  969. dt_operand **indexes;
  970. dt_simplify (simplify *s_, unsigned pattern_no_, dt_operand **indexes_)
  971. : dt_node (DT_SIMPLIFY), s (s_), pattern_no (pattern_no_),
  972. indexes (indexes_) {}
  973. void gen (FILE *f, bool);
  974. };
  975. template<>
  976. template<>
  977. inline bool
  978. is_a_helper <dt_operand *>::test (dt_node *n)
  979. {
  980. return (n->type == dt_node::DT_OPERAND
  981. || n->type == dt_node::DT_MATCH);
  982. }
  983. /* A container for the actual decision tree. */
  984. struct decision_tree
  985. {
  986. dt_node *root;
  987. void insert (struct simplify *, unsigned);
  988. void gen_gimple (FILE *f = stderr);
  989. void gen_generic (FILE *f = stderr);
  990. void print (FILE *f = stderr);
  991. decision_tree () { root = new dt_node (dt_node::DT_NODE); }
  992. static dt_node *insert_operand (dt_node *, operand *, dt_operand **indexes,
  993. unsigned pos = 0, dt_node *parent = 0);
  994. static dt_node *find_node (vec<dt_node *>&, dt_node *);
  995. static bool cmp_node (dt_node *, dt_node *);
  996. static void print_node (dt_node *, FILE *f = stderr, unsigned = 0);
  997. };
  998. /* Compare two AST operands O1 and O2 and return true if they are equal. */
  999. bool
  1000. cmp_operand (operand *o1, operand *o2)
  1001. {
  1002. if (!o1 || !o2 || o1->type != o2->type)
  1003. return false;
  1004. if (o1->type == operand::OP_PREDICATE)
  1005. {
  1006. predicate *p1 = as_a<predicate *>(o1);
  1007. predicate *p2 = as_a<predicate *>(o2);
  1008. return p1->p == p2->p;
  1009. }
  1010. else if (o1->type == operand::OP_EXPR)
  1011. {
  1012. expr *e1 = static_cast<expr *>(o1);
  1013. expr *e2 = static_cast<expr *>(o2);
  1014. return (e1->operation == e2->operation
  1015. && e1->is_generic == e2->is_generic);
  1016. }
  1017. else
  1018. return false;
  1019. }
  1020. /* Compare two decision tree nodes N1 and N2 and return true if they
  1021. are equal. */
  1022. bool
  1023. decision_tree::cmp_node (dt_node *n1, dt_node *n2)
  1024. {
  1025. if (!n1 || !n2 || n1->type != n2->type)
  1026. return false;
  1027. if (n1 == n2)
  1028. return true;
  1029. if (n1->type == dt_node::DT_TRUE)
  1030. return false;
  1031. if (n1->type == dt_node::DT_OPERAND)
  1032. return cmp_operand ((as_a<dt_operand *> (n1))->op,
  1033. (as_a<dt_operand *> (n2))->op);
  1034. else if (n1->type == dt_node::DT_MATCH)
  1035. return ((as_a<dt_operand *> (n1))->match_dop
  1036. == (as_a<dt_operand *> (n2))->match_dop);
  1037. return false;
  1038. }
  1039. /* Search OPS for a decision tree node like P and return it if found. */
  1040. dt_node *
  1041. decision_tree::find_node (vec<dt_node *>& ops, dt_node *p)
  1042. {
  1043. /* We can merge adjacent DT_TRUE. */
  1044. if (p->type == dt_node::DT_TRUE
  1045. && !ops.is_empty ()
  1046. && ops.last ()->type == dt_node::DT_TRUE)
  1047. return ops.last ();
  1048. for (int i = ops.length () - 1; i >= 0; --i)
  1049. {
  1050. /* But we can't merge across DT_TRUE nodes as they serve as
  1051. pattern order barriers to make sure that patterns apply
  1052. in order of appearance in case multiple matches are possible. */
  1053. if (ops[i]->type == dt_node::DT_TRUE)
  1054. return NULL;
  1055. if (decision_tree::cmp_node (ops[i], p))
  1056. return ops[i];
  1057. }
  1058. return NULL;
  1059. }
  1060. /* Append N to the decision tree if it there is not already an existing
  1061. identical child. */
  1062. dt_node *
  1063. dt_node::append_node (dt_node *n)
  1064. {
  1065. dt_node *kid;
  1066. kid = decision_tree::find_node (kids, n);
  1067. if (kid)
  1068. return kid;
  1069. kids.safe_push (n);
  1070. n->level = this->level + 1;
  1071. return n;
  1072. }
  1073. /* Append OP to the decision tree. */
  1074. dt_node *
  1075. dt_node::append_op (operand *op, dt_node *parent, unsigned pos)
  1076. {
  1077. dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
  1078. dt_operand *n = new dt_operand (DT_OPERAND, op, 0, parent_, pos);
  1079. return append_node (n);
  1080. }
  1081. /* Append a DT_TRUE decision tree node. */
  1082. dt_node *
  1083. dt_node::append_true_op (dt_node *parent, unsigned pos)
  1084. {
  1085. dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
  1086. dt_operand *n = new dt_operand (DT_TRUE, 0, 0, parent_, pos);
  1087. return append_node (n);
  1088. }
  1089. /* Append a DT_MATCH decision tree node. */
  1090. dt_node *
  1091. dt_node::append_match_op (dt_operand *match_dop, dt_node *parent, unsigned pos)
  1092. {
  1093. dt_operand *parent_ = as_a<dt_operand *> (parent);
  1094. dt_operand *n = new dt_operand (DT_MATCH, 0, match_dop, parent_, pos);
  1095. return append_node (n);
  1096. }
  1097. /* Append S to the decision tree. */
  1098. dt_node *
  1099. dt_node::append_simplify (simplify *s, unsigned pattern_no,
  1100. dt_operand **indexes)
  1101. {
  1102. dt_simplify *n = new dt_simplify (s, pattern_no, indexes);
  1103. return append_node (n);
  1104. }
  1105. /* Insert O into the decision tree and return the decision tree node found
  1106. or created. */
  1107. dt_node *
  1108. decision_tree::insert_operand (dt_node *p, operand *o, dt_operand **indexes,
  1109. unsigned pos, dt_node *parent)
  1110. {
  1111. dt_node *q, *elm = 0;
  1112. if (capture *c = dyn_cast<capture *> (o))
  1113. {
  1114. unsigned capt_index = c->where;
  1115. if (indexes[capt_index] == 0)
  1116. {
  1117. if (c->what)
  1118. q = insert_operand (p, c->what, indexes, pos, parent);
  1119. else
  1120. {
  1121. q = elm = p->append_true_op (parent, pos);
  1122. goto at_assert_elm;
  1123. }
  1124. // get to the last capture
  1125. for (operand *what = c->what;
  1126. what && is_a<capture *> (what);
  1127. c = as_a<capture *> (what), what = c->what)
  1128. ;
  1129. if (!c->what)
  1130. {
  1131. unsigned cc_index = c->where;
  1132. dt_operand *match_op = indexes[cc_index];
  1133. dt_operand temp (dt_node::DT_TRUE, 0, 0);
  1134. elm = decision_tree::find_node (p->kids, &temp);
  1135. if (elm == 0)
  1136. {
  1137. dt_operand temp (dt_node::DT_MATCH, 0, match_op);
  1138. elm = decision_tree::find_node (p->kids, &temp);
  1139. }
  1140. }
  1141. else
  1142. {
  1143. dt_operand temp (dt_node::DT_OPERAND, c->what, 0);
  1144. elm = decision_tree::find_node (p->kids, &temp);
  1145. }
  1146. at_assert_elm:
  1147. gcc_assert (elm->type == dt_node::DT_TRUE
  1148. || elm->type == dt_node::DT_OPERAND
  1149. || elm->type == dt_node::DT_MATCH);
  1150. indexes[capt_index] = static_cast<dt_operand *> (elm);
  1151. return q;
  1152. }
  1153. else
  1154. {
  1155. p = p->append_match_op (indexes[capt_index], parent, pos);
  1156. if (c->what)
  1157. return insert_operand (p, c->what, indexes, 0, p);
  1158. else
  1159. return p;
  1160. }
  1161. }
  1162. p = p->append_op (o, parent, pos);
  1163. q = p;
  1164. if (expr *e = dyn_cast <expr *>(o))
  1165. {
  1166. for (unsigned i = 0; i < e->ops.length (); ++i)
  1167. q = decision_tree::insert_operand (q, e->ops[i], indexes, i, p);
  1168. }
  1169. return q;
  1170. }
  1171. /* Insert S into the decision tree. */
  1172. void
  1173. decision_tree::insert (struct simplify *s, unsigned pattern_no)
  1174. {
  1175. dt_operand **indexes = XCNEWVEC (dt_operand *, s->capture_max + 1);
  1176. dt_node *p = decision_tree::insert_operand (root, s->match, indexes);
  1177. p->append_simplify (s, pattern_no, indexes);
  1178. }
  1179. /* Debug functions to dump the decision tree. */
  1180. DEBUG_FUNCTION void
  1181. decision_tree::print_node (dt_node *p, FILE *f, unsigned indent)
  1182. {
  1183. if (p->type == dt_node::DT_NODE)
  1184. fprintf (f, "root");
  1185. else
  1186. {
  1187. fprintf (f, "|");
  1188. for (unsigned i = 0; i < indent; i++)
  1189. fprintf (f, "-");
  1190. if (p->type == dt_node::DT_OPERAND)
  1191. {
  1192. dt_operand *dop = static_cast<dt_operand *>(p);
  1193. print_operand (dop->op, f, true);
  1194. }
  1195. else if (p->type == dt_node::DT_TRUE)
  1196. fprintf (f, "true");
  1197. else if (p->type == dt_node::DT_MATCH)
  1198. fprintf (f, "match (%p)", (void *)((as_a<dt_operand *>(p))->match_dop));
  1199. else if (p->type == dt_node::DT_SIMPLIFY)
  1200. {
  1201. dt_simplify *s = static_cast<dt_simplify *> (p);
  1202. fprintf (f, "simplify_%u { ", s->pattern_no);
  1203. for (int i = 0; i <= s->s->capture_max; ++i)
  1204. fprintf (f, "%p, ", (void *) s->indexes[i]);
  1205. fprintf (f, " } ");
  1206. }
  1207. }
  1208. fprintf (stderr, " (%p), %u, %u\n", (void *) p, p->level, p->kids.length ());
  1209. for (unsigned i = 0; i < p->kids.length (); ++i)
  1210. decision_tree::print_node (p->kids[i], f, indent + 2);
  1211. }
  1212. DEBUG_FUNCTION void
  1213. decision_tree::print (FILE *f)
  1214. {
  1215. return decision_tree::print_node (root, f);
  1216. }
  1217. /* For GENERIC we have to take care of wrapping multiple-used
  1218. expressions with side-effects in save_expr and preserve side-effects
  1219. of expressions with omit_one_operand. Analyze captures in
  1220. match, result and with expressions and perform early-outs
  1221. on the outermost match expression operands for cases we cannot
  1222. handle. */
  1223. struct capture_info
  1224. {
  1225. capture_info (simplify *s);
  1226. void walk_match (operand *o, unsigned toplevel_arg, bool, bool);
  1227. void walk_result (operand *o, bool);
  1228. void walk_c_expr (c_expr *);
  1229. struct cinfo
  1230. {
  1231. bool expr_p;
  1232. bool cse_p;
  1233. bool force_no_side_effects_p;
  1234. bool cond_expr_cond_p;
  1235. unsigned long toplevel_msk;
  1236. int result_use_count;
  1237. };
  1238. auto_vec<cinfo> info;
  1239. unsigned long force_no_side_effects;
  1240. };
  1241. /* Analyze captures in S. */
  1242. capture_info::capture_info (simplify *s)
  1243. {
  1244. expr *e;
  1245. if (!s->result
  1246. || ((e = dyn_cast <expr *> (s->result))
  1247. && is_a <predicate_id *> (e->operation)))
  1248. {
  1249. force_no_side_effects = -1;
  1250. return;
  1251. }
  1252. force_no_side_effects = 0;
  1253. info.safe_grow_cleared (s->capture_max + 1);
  1254. e = as_a <expr *> (s->match);
  1255. for (unsigned i = 0; i < e->ops.length (); ++i)
  1256. walk_match (e->ops[i], i,
  1257. (i != 0 && *e->operation == COND_EXPR)
  1258. || *e->operation == TRUTH_ANDIF_EXPR
  1259. || *e->operation == TRUTH_ORIF_EXPR,
  1260. i == 0
  1261. && (*e->operation == COND_EXPR
  1262. || *e->operation == VEC_COND_EXPR));
  1263. walk_result (s->result, false);
  1264. for (unsigned i = 0; i < s->ifexpr_vec.length (); ++i)
  1265. if (s->ifexpr_vec[i].is_with)
  1266. walk_c_expr (as_a <c_expr *>(s->ifexpr_vec[i].cexpr));
  1267. }
  1268. /* Analyze captures in the match expression piece O. */
  1269. void
  1270. capture_info::walk_match (operand *o, unsigned toplevel_arg,
  1271. bool conditional_p, bool cond_expr_cond_p)
  1272. {
  1273. if (capture *c = dyn_cast <capture *> (o))
  1274. {
  1275. info[c->where].toplevel_msk |= 1 << toplevel_arg;
  1276. info[c->where].force_no_side_effects_p |= conditional_p;
  1277. info[c->where].cond_expr_cond_p |= cond_expr_cond_p;
  1278. /* Mark expr (non-leaf) captures and recurse. */
  1279. if (c->what
  1280. && is_a <expr *> (c->what))
  1281. {
  1282. info[c->where].expr_p = true;
  1283. walk_match (c->what, toplevel_arg, conditional_p, false);
  1284. }
  1285. }
  1286. else if (expr *e = dyn_cast <expr *> (o))
  1287. {
  1288. for (unsigned i = 0; i < e->ops.length (); ++i)
  1289. {
  1290. bool cond_p = conditional_p;
  1291. bool cond_expr_cond_p = false;
  1292. if (i != 0 && *e->operation == COND_EXPR)
  1293. cond_p = true;
  1294. else if (*e->operation == TRUTH_ANDIF_EXPR
  1295. || *e->operation == TRUTH_ORIF_EXPR)
  1296. cond_p = true;
  1297. if (i == 0
  1298. && (*e->operation == COND_EXPR
  1299. || *e->operation == VEC_COND_EXPR))
  1300. cond_expr_cond_p = true;
  1301. walk_match (e->ops[i], toplevel_arg, cond_p, cond_expr_cond_p);
  1302. }
  1303. }
  1304. else if (is_a <predicate *> (o))
  1305. {
  1306. /* Mark non-captured leafs toplevel arg for checking. */
  1307. force_no_side_effects |= 1 << toplevel_arg;
  1308. }
  1309. else
  1310. gcc_unreachable ();
  1311. }
  1312. /* Analyze captures in the result expression piece O. */
  1313. void
  1314. capture_info::walk_result (operand *o, bool conditional_p)
  1315. {
  1316. if (capture *c = dyn_cast <capture *> (o))
  1317. {
  1318. info[c->where].result_use_count++;
  1319. /* If we substitute an expression capture we don't know
  1320. which captures this will end up using (well, we don't
  1321. compute that). Force the uses to be side-effect free
  1322. which means forcing the toplevels that reach the
  1323. expression side-effect free. */
  1324. if (info[c->where].expr_p)
  1325. force_no_side_effects |= info[c->where].toplevel_msk;
  1326. /* Mark CSE capture capture uses as forced to have
  1327. no side-effects. */
  1328. if (c->what
  1329. && is_a <expr *> (c->what))
  1330. {
  1331. info[c->where].cse_p = true;
  1332. walk_result (c->what, true);
  1333. }
  1334. }
  1335. else if (expr *e = dyn_cast <expr *> (o))
  1336. {
  1337. for (unsigned i = 0; i < e->ops.length (); ++i)
  1338. {
  1339. bool cond_p = conditional_p;
  1340. if (i != 0 && *e->operation == COND_EXPR)
  1341. cond_p = true;
  1342. else if (*e->operation == TRUTH_ANDIF_EXPR
  1343. || *e->operation == TRUTH_ORIF_EXPR)
  1344. cond_p = true;
  1345. walk_result (e->ops[i], cond_p);
  1346. }
  1347. }
  1348. else if (c_expr *e = dyn_cast <c_expr *> (o))
  1349. walk_c_expr (e);
  1350. else
  1351. gcc_unreachable ();
  1352. }
  1353. /* Look for captures in the C expr E. */
  1354. void
  1355. capture_info::walk_c_expr (c_expr *e)
  1356. {
  1357. /* Give up for C exprs mentioning captures not inside TREE_TYPE (). */
  1358. unsigned p_depth = 0;
  1359. for (unsigned i = 0; i < e->code.length (); ++i)
  1360. {
  1361. const cpp_token *t = &e->code[i];
  1362. const cpp_token *n = i < e->code.length () - 1 ? &e->code[i+1] : NULL;
  1363. if (t->type == CPP_NAME
  1364. && strcmp ((const char *)CPP_HASHNODE
  1365. (t->val.node.node)->ident.str, "TREE_TYPE") == 0
  1366. && n->type == CPP_OPEN_PAREN)
  1367. p_depth++;
  1368. else if (t->type == CPP_CLOSE_PAREN
  1369. && p_depth > 0)
  1370. p_depth--;
  1371. else if (p_depth == 0
  1372. && t->type == CPP_ATSIGN
  1373. && (n->type == CPP_NUMBER
  1374. || n->type == CPP_NAME)
  1375. && !(n->flags & PREV_WHITE))
  1376. {
  1377. const char *id;
  1378. if (n->type == CPP_NUMBER)
  1379. id = (const char *)n->val.str.text;
  1380. else
  1381. id = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
  1382. info[*e->capture_ids->get(id)].force_no_side_effects_p = true;
  1383. }
  1384. }
  1385. }
  1386. /* Code generation off the decision tree and the refered AST nodes. */
  1387. bool
  1388. is_conversion (id_base *op)
  1389. {
  1390. return (*op == CONVERT_EXPR
  1391. || *op == NOP_EXPR
  1392. || *op == FLOAT_EXPR
  1393. || *op == FIX_TRUNC_EXPR
  1394. || *op == VIEW_CONVERT_EXPR);
  1395. }
  1396. /* Get the type to be used for generating operands of OP from the
  1397. various sources. */
  1398. static const char *
  1399. get_operand_type (id_base *op, const char *in_type,
  1400. const char *expr_type,
  1401. const char *other_oprnd_type)
  1402. {
  1403. /* Generally operands whose type does not match the type of the
  1404. expression generated need to know their types but match and
  1405. thus can fall back to 'other_oprnd_type'. */
  1406. if (is_conversion (op))
  1407. return other_oprnd_type;
  1408. else if (*op == REALPART_EXPR
  1409. || *op == IMAGPART_EXPR)
  1410. return other_oprnd_type;
  1411. else if (is_a <operator_id *> (op)
  1412. && strcmp (as_a <operator_id *> (op)->tcc, "tcc_comparison") == 0)
  1413. return other_oprnd_type;
  1414. else
  1415. {
  1416. /* Otherwise all types should match - choose one in order of
  1417. preference. */
  1418. if (expr_type)
  1419. return expr_type;
  1420. else if (in_type)
  1421. return in_type;
  1422. else
  1423. return other_oprnd_type;
  1424. }
  1425. }
  1426. /* Generate transform code for an expression. */
  1427. void
  1428. expr::gen_transform (FILE *f, const char *dest, bool gimple, int depth,
  1429. const char *in_type, capture_info *cinfo,
  1430. dt_operand **indexes, bool)
  1431. {
  1432. bool conversion_p = is_conversion (operation);
  1433. const char *type = expr_type;
  1434. char optype[64];
  1435. if (type)
  1436. /* If there was a type specification in the pattern use it. */
  1437. ;
  1438. else if (conversion_p)
  1439. /* For conversions we need to build the expression using the
  1440. outer type passed in. */
  1441. type = in_type;
  1442. else if (*operation == REALPART_EXPR
  1443. || *operation == IMAGPART_EXPR)
  1444. {
  1445. /* __real and __imag use the component type of its operand. */
  1446. sprintf (optype, "TREE_TYPE (TREE_TYPE (ops%d[0]))", depth);
  1447. type = optype;
  1448. }
  1449. else if (is_a <operator_id *> (operation)
  1450. && !strcmp (as_a <operator_id *> (operation)->tcc, "tcc_comparison"))
  1451. {
  1452. /* comparisons use boolean_type_node (or what gets in), but
  1453. their operands need to figure out the types themselves. */
  1454. sprintf (optype, "boolean_type_node");
  1455. type = optype;
  1456. }
  1457. else
  1458. {
  1459. /* Other operations are of the same type as their first operand. */
  1460. sprintf (optype, "TREE_TYPE (ops%d[0])", depth);
  1461. type = optype;
  1462. }
  1463. if (!type)
  1464. fatal ("two conversions in a row");
  1465. fprintf (f, "{\n");
  1466. fprintf (f, " tree ops%d[%u], res;\n", depth, ops.length ());
  1467. char op0type[64];
  1468. snprintf (op0type, 64, "TREE_TYPE (ops%d[0])", depth);
  1469. for (unsigned i = 0; i < ops.length (); ++i)
  1470. {
  1471. char dest[32];
  1472. snprintf (dest, 32, " ops%d[%u]", depth, i);
  1473. const char *optype
  1474. = get_operand_type (operation, in_type, expr_type,
  1475. i == 0 ? NULL : op0type);
  1476. ops[i]->gen_transform (f, dest, gimple, depth + 1, optype, cinfo, indexes,
  1477. ((!(*operation == COND_EXPR)
  1478. && !(*operation == VEC_COND_EXPR))
  1479. || i != 0));
  1480. }
  1481. const char *opr;
  1482. if (*operation == CONVERT_EXPR)
  1483. opr = "NOP_EXPR";
  1484. else
  1485. opr = operation->id;
  1486. if (gimple)
  1487. {
  1488. /* ??? Building a stmt can fail for various reasons here, seq being
  1489. NULL or the stmt referencing SSA names occuring in abnormal PHIs.
  1490. So if we fail here we should continue matching other patterns. */
  1491. fprintf (f, " code_helper tem_code = %s;\n"
  1492. " tree tem_ops[3] = { ", opr);
  1493. for (unsigned i = 0; i < ops.length (); ++i)
  1494. fprintf (f, "ops%d[%u]%s", depth, i,
  1495. i == ops.length () - 1 ? " };\n" : ", ");
  1496. fprintf (f, " gimple_resimplify%d (seq, &tem_code, %s, tem_ops, valueize);\n",
  1497. ops.length (), type);
  1498. fprintf (f, " res = maybe_push_res_to_seq (tem_code, %s, tem_ops, seq);\n"
  1499. " if (!res) return false;\n", type);
  1500. }
  1501. else
  1502. {
  1503. if (operation->kind == id_base::CODE)
  1504. fprintf (f, " res = fold_build%d_loc (loc, %s, %s",
  1505. ops.length(), opr, type);
  1506. else
  1507. fprintf (f, " res = build_call_expr_loc (loc, "
  1508. "builtin_decl_implicit (%s), %d", opr, ops.length());
  1509. for (unsigned i = 0; i < ops.length (); ++i)
  1510. fprintf (f, ", ops%d[%u]", depth, i);
  1511. fprintf (f, ");\n");
  1512. }
  1513. fprintf (f, "%s = res;\n", dest);
  1514. fprintf (f, "}\n");
  1515. }
  1516. /* Generate code for a c_expr which is either the expression inside
  1517. an if statement or a sequence of statements which computes a
  1518. result to be stored to DEST. */
  1519. void
  1520. c_expr::gen_transform (FILE *f, const char *dest,
  1521. bool, int, const char *, capture_info *,
  1522. dt_operand **, bool)
  1523. {
  1524. if (dest && nr_stmts == 1)
  1525. fprintf (f, "%s = ", dest);
  1526. unsigned stmt_nr = 1;
  1527. for (unsigned i = 0; i < code.length (); ++i)
  1528. {
  1529. const cpp_token *token = &code[i];
  1530. /* Replace captures for code-gen. */
  1531. if (token->type == CPP_ATSIGN)
  1532. {
  1533. const cpp_token *n = &code[i+1];
  1534. if ((n->type == CPP_NUMBER
  1535. || n->type == CPP_NAME)
  1536. && !(n->flags & PREV_WHITE))
  1537. {
  1538. if (token->flags & PREV_WHITE)
  1539. fputc (' ', f);
  1540. const char *id;
  1541. if (n->type == CPP_NUMBER)
  1542. id = (const char *)n->val.str.text;
  1543. else
  1544. id = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
  1545. fprintf (f, "captures[%u]", *capture_ids->get(id));
  1546. ++i;
  1547. continue;
  1548. }
  1549. }
  1550. if (token->flags & PREV_WHITE)
  1551. fputc (' ', f);
  1552. if (token->type == CPP_NAME)
  1553. {
  1554. const char *id = (const char *) NODE_NAME (token->val.node.node);
  1555. unsigned j;
  1556. for (j = 0; j < ids.length (); ++j)
  1557. {
  1558. if (strcmp (id, ids[j].id) == 0)
  1559. {
  1560. fprintf (f, "%s", ids[j].oper);
  1561. break;
  1562. }
  1563. }
  1564. if (j < ids.length ())
  1565. continue;
  1566. }
  1567. /* Output the token as string. */
  1568. char *tk = (char *)cpp_token_as_text (r, token);
  1569. fputs (tk, f);
  1570. if (token->type == CPP_SEMICOLON)
  1571. {
  1572. stmt_nr++;
  1573. if (dest && stmt_nr == nr_stmts)
  1574. fprintf (f, "\n %s = ", dest);
  1575. else
  1576. fputc ('\n', f);
  1577. }
  1578. }
  1579. }
  1580. /* Generate transform code for a capture. */
  1581. void
  1582. capture::gen_transform (FILE *f, const char *dest, bool gimple, int depth,
  1583. const char *in_type, capture_info *cinfo,
  1584. dt_operand **indexes, bool expand_compares)
  1585. {
  1586. if (what && is_a<expr *> (what))
  1587. {
  1588. if (indexes[where] == 0)
  1589. {
  1590. char buf[20];
  1591. sprintf (buf, "captures[%u]", where);
  1592. what->gen_transform (f, buf, gimple, depth, in_type, cinfo, NULL);
  1593. }
  1594. }
  1595. fprintf (f, "%s = captures[%u];\n", dest, where);
  1596. /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
  1597. with substituting a capture of that.
  1598. ??? Returning false here will also not allow any other patterns
  1599. to match. */
  1600. if (gimple && expand_compares
  1601. && cinfo->info[where].cond_expr_cond_p)
  1602. fprintf (f, "if (COMPARISON_CLASS_P (%s))\n"
  1603. " {\n"
  1604. " if (!seq) return false;\n"
  1605. " %s = gimple_build (seq, TREE_CODE (%s),"
  1606. " TREE_TYPE (%s), TREE_OPERAND (%s, 0),"
  1607. " TREE_OPERAND (%s, 1));\n"
  1608. " }\n", dest, dest, dest, dest, dest, dest);
  1609. }
  1610. /* Return the name of the operand representing the decision tree node.
  1611. Use NAME as space to generate it. */
  1612. char *
  1613. dt_operand::get_name (char *name)
  1614. {
  1615. if (!parent)
  1616. sprintf (name, "t");
  1617. else if (parent->level == 1)
  1618. sprintf (name, "op%u", pos);
  1619. else if (parent->type == dt_node::DT_MATCH)
  1620. return parent->get_name (name);
  1621. else
  1622. sprintf (name, "o%u%u", parent->level, pos);
  1623. return name;
  1624. }
  1625. /* Fill NAME with the operand name at position POS. */
  1626. void
  1627. dt_operand::gen_opname (char *name, unsigned pos)
  1628. {
  1629. if (!parent)
  1630. sprintf (name, "op%u", pos);
  1631. else
  1632. sprintf (name, "o%u%u", level, pos);
  1633. }
  1634. /* Generate matching code for the decision tree operand which is
  1635. a predicate. */
  1636. unsigned
  1637. dt_operand::gen_predicate (FILE *f, const char *opname, bool gimple)
  1638. {
  1639. predicate *p = as_a <predicate *> (op);
  1640. if (p->p->matchers.exists ())
  1641. {
  1642. /* If this is a predicate generated from a pattern mangle its
  1643. name and pass on the valueize hook. */
  1644. if (gimple)
  1645. fprintf (f, "if (gimple_%s (%s, valueize))\n", p->p->id, opname);
  1646. else
  1647. fprintf (f, "if (tree_%s (%s))\n", p->p->id, opname);
  1648. }
  1649. else
  1650. fprintf (f, "if (%s (%s))\n", p->p->id, opname);
  1651. fprintf (f, "{\n");
  1652. return 1;
  1653. }
  1654. /* Generate matching code for the decision tree operand which is
  1655. a capture-match. */
  1656. unsigned
  1657. dt_operand::gen_match_op (FILE *f, const char *opname)
  1658. {
  1659. char match_opname[20];
  1660. match_dop->get_name (match_opname);
  1661. fprintf (f, "if (%s == %s || operand_equal_p (%s, %s, 0))\n",
  1662. opname, match_opname, opname, match_opname);
  1663. fprintf (f, "{\n");
  1664. return 1;
  1665. }
  1666. /* Generate GIMPLE matching code for the decision tree operand. */
  1667. unsigned
  1668. dt_operand::gen_gimple_expr (FILE *f)
  1669. {
  1670. expr *e = static_cast<expr *> (op);
  1671. id_base *id = e->operation;
  1672. unsigned n_ops = e->ops.length ();
  1673. for (unsigned i = 0; i < n_ops; ++i)
  1674. {
  1675. char child_opname[20];
  1676. gen_opname (child_opname, i);
  1677. if (id->kind == id_base::CODE)
  1678. {
  1679. if (e->is_generic
  1680. || *id == REALPART_EXPR || *id == IMAGPART_EXPR
  1681. || *id == BIT_FIELD_REF || *id == VIEW_CONVERT_EXPR)
  1682. {
  1683. /* ??? If this is a memory operation we can't (and should not)
  1684. match this. The only sensible operand types are
  1685. SSA names and invariants. */
  1686. fprintf (f, "tree %s = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), %i);\n",
  1687. child_opname, i);
  1688. fprintf (f, "if ((TREE_CODE (%s) == SSA_NAME\n"
  1689. "|| is_gimple_min_invariant (%s))\n"
  1690. "&& (%s = do_valueize (valueize, %s)))\n"
  1691. "{\n", child_opname, child_opname, child_opname,
  1692. child_opname);
  1693. continue;
  1694. }
  1695. else
  1696. fprintf (f, "tree %s = gimple_assign_rhs%u (def_stmt);\n",
  1697. child_opname, i + 1);
  1698. }
  1699. else
  1700. fprintf (f, "tree %s = gimple_call_arg (def_stmt, %u);\n",
  1701. child_opname, i);
  1702. fprintf (f, "if ((%s = do_valueize (valueize, %s)))\n",
  1703. child_opname, child_opname);
  1704. fprintf (f, "{\n");
  1705. }
  1706. return n_ops;
  1707. }
  1708. /* Generate GENERIC matching code for the decision tree operand. */
  1709. unsigned
  1710. dt_operand::gen_generic_expr (FILE *f, const char *opname)
  1711. {
  1712. expr *e = static_cast<expr *> (op);
  1713. unsigned n_ops = e->ops.length ();
  1714. for (unsigned i = 0; i < n_ops; ++i)
  1715. {
  1716. char child_opname[20];
  1717. gen_opname (child_opname, i);
  1718. if (e->operation->kind == id_base::CODE)
  1719. fprintf (f, "tree %s = TREE_OPERAND (%s, %u);\n",
  1720. child_opname, opname, i);
  1721. else
  1722. fprintf (f, "tree %s = CALL_EXPR_ARG (%s, %u);\n",
  1723. child_opname, opname, i);
  1724. }
  1725. return 0;
  1726. }
  1727. /* Generate matching code for the children of the decision tree node. */
  1728. void
  1729. dt_node::gen_kids (FILE *f, bool gimple)
  1730. {
  1731. auto_vec<dt_operand *> gimple_exprs;
  1732. auto_vec<dt_operand *> generic_exprs;
  1733. auto_vec<dt_operand *> fns;
  1734. auto_vec<dt_operand *> generic_fns;
  1735. auto_vec<dt_operand *> preds;
  1736. auto_vec<dt_node *> others;
  1737. for (unsigned i = 0; i < kids.length (); ++i)
  1738. {
  1739. if (kids[i]->type == dt_node::DT_OPERAND)
  1740. {
  1741. dt_operand *op = as_a<dt_operand *> (kids[i]);
  1742. if (expr *e = dyn_cast <expr *> (op->op))
  1743. {
  1744. if (e->ops.length () == 0
  1745. && (!gimple || !(*e->operation == CONSTRUCTOR)))
  1746. generic_exprs.safe_push (op);
  1747. else if (e->operation->kind == id_base::FN)
  1748. {
  1749. if (gimple)
  1750. fns.safe_push (op);
  1751. else
  1752. generic_fns.safe_push (op);
  1753. }
  1754. else if (e->operation->kind == id_base::PREDICATE)
  1755. preds.safe_push (op);
  1756. else
  1757. {
  1758. if (gimple)
  1759. gimple_exprs.safe_push (op);
  1760. else
  1761. generic_exprs.safe_push (op);
  1762. }
  1763. }
  1764. else if (op->op->type == operand::OP_PREDICATE)
  1765. others.safe_push (kids[i]);
  1766. else
  1767. gcc_unreachable ();
  1768. }
  1769. else if (kids[i]->type == dt_node::DT_MATCH
  1770. || kids[i]->type == dt_node::DT_SIMPLIFY)
  1771. others.safe_push (kids[i]);
  1772. else if (kids[i]->type == dt_node::DT_TRUE)
  1773. {
  1774. /* A DT_TRUE operand serves as a barrier - generate code now
  1775. for what we have collected sofar. */
  1776. gen_kids_1 (f, gimple, gimple_exprs, generic_exprs,
  1777. fns, generic_fns, preds, others);
  1778. /* And output the true operand itself. */
  1779. kids[i]->gen (f, gimple);
  1780. gimple_exprs.truncate (0);
  1781. generic_exprs.truncate (0);
  1782. fns.truncate (0);
  1783. generic_fns.truncate (0);
  1784. preds.truncate (0);
  1785. others.truncate (0);
  1786. }
  1787. else
  1788. gcc_unreachable ();
  1789. }
  1790. /* Generate code for the remains. */
  1791. gen_kids_1 (f, gimple, gimple_exprs, generic_exprs,
  1792. fns, generic_fns, preds, others);
  1793. }
  1794. /* Generate matching code for the children of the decision tree node. */
  1795. void
  1796. dt_node::gen_kids_1 (FILE *f, bool gimple,
  1797. vec<dt_operand *> gimple_exprs,
  1798. vec<dt_operand *> generic_exprs,
  1799. vec<dt_operand *> fns,
  1800. vec<dt_operand *> generic_fns,
  1801. vec<dt_operand *> preds,
  1802. vec<dt_node *> others)
  1803. {
  1804. char buf[128];
  1805. char *kid_opname = buf;
  1806. unsigned exprs_len = gimple_exprs.length ();
  1807. unsigned gexprs_len = generic_exprs.length ();
  1808. unsigned fns_len = fns.length ();
  1809. unsigned gfns_len = generic_fns.length ();
  1810. if (exprs_len || fns_len || gexprs_len || gfns_len)
  1811. {
  1812. if (exprs_len)
  1813. gimple_exprs[0]->get_name (kid_opname);
  1814. else if (fns_len)
  1815. fns[0]->get_name (kid_opname);
  1816. else if (gfns_len)
  1817. generic_fns[0]->get_name (kid_opname);
  1818. else
  1819. generic_exprs[0]->get_name (kid_opname);
  1820. fprintf (f, "switch (TREE_CODE (%s))\n"
  1821. "{\n", kid_opname);
  1822. }
  1823. if (exprs_len || fns_len)
  1824. {
  1825. fprintf (f, "case SSA_NAME:\n");
  1826. fprintf (f, "if (do_valueize (valueize, %s) != NULL_TREE)\n", kid_opname);
  1827. fprintf (f, "{\n");
  1828. fprintf (f, "gimple def_stmt = SSA_NAME_DEF_STMT (%s);\n", kid_opname);
  1829. if (exprs_len)
  1830. {
  1831. fprintf (f, "if (is_gimple_assign (def_stmt))\n");
  1832. fprintf (f, "switch (gimple_assign_rhs_code (def_stmt))\n"
  1833. "{\n");
  1834. for (unsigned i = 0; i < exprs_len; ++i)
  1835. {
  1836. expr *e = as_a <expr *> (gimple_exprs[i]->op);
  1837. id_base *op = e->operation;
  1838. if (*op == CONVERT_EXPR || *op == NOP_EXPR)
  1839. fprintf (f, "CASE_CONVERT:\n");
  1840. else
  1841. fprintf (f, "case %s:\n", op->id);
  1842. fprintf (f, "{\n");
  1843. gimple_exprs[i]->gen (f, true);
  1844. fprintf (f, "break;\n"
  1845. "}\n");
  1846. }
  1847. fprintf (f, "default:;\n"
  1848. "}\n");
  1849. }
  1850. if (fns_len)
  1851. {
  1852. if (exprs_len)
  1853. fprintf (f, "else ");
  1854. fprintf (f, "if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL))\n"
  1855. "{\n"
  1856. "tree fndecl = gimple_call_fndecl (def_stmt);\n"
  1857. "switch (DECL_FUNCTION_CODE (fndecl))\n"
  1858. "{\n");
  1859. for (unsigned i = 0; i < fns_len; ++i)
  1860. {
  1861. expr *e = as_a <expr *>(fns[i]->op);
  1862. fprintf (f, "case %s:\n"
  1863. "{\n", e->operation->id);
  1864. fns[i]->gen (f, true);
  1865. fprintf (f, "break;\n"
  1866. "}\n");
  1867. }
  1868. fprintf (f, "default:;\n"
  1869. "}\n"
  1870. "}\n");
  1871. }
  1872. fprintf (f, "}\n"
  1873. "break;\n");
  1874. }
  1875. for (unsigned i = 0; i < generic_exprs.length (); ++i)
  1876. {
  1877. expr *e = as_a <expr *>(generic_exprs[i]->op);
  1878. id_base *op = e->operation;
  1879. if (*op == CONVERT_EXPR || *op == NOP_EXPR)
  1880. fprintf (f, "CASE_CONVERT:\n");
  1881. else
  1882. fprintf (f, "case %s:\n", op->id);
  1883. fprintf (f, "{\n");
  1884. generic_exprs[i]->gen (f, gimple);
  1885. fprintf (f, "break;\n"
  1886. "}\n");
  1887. }
  1888. if (gfns_len)
  1889. {
  1890. fprintf (f, "case CALL_EXPR:\n"
  1891. "{\n"
  1892. "tree fndecl = get_callee_fndecl (%s);\n"
  1893. "if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)\n"
  1894. "switch (DECL_FUNCTION_CODE (fndecl))\n"
  1895. "{\n", kid_opname);
  1896. for (unsigned j = 0; j < generic_fns.length (); ++j)
  1897. {
  1898. expr *e = as_a <expr *>(generic_fns[j]->op);
  1899. gcc_assert (e->operation->kind == id_base::FN);
  1900. fprintf (f, "case %s:\n"
  1901. "{\n", e->operation->id);
  1902. generic_fns[j]->gen (f, false);
  1903. fprintf (f, "break;\n"
  1904. "}\n");
  1905. }
  1906. fprintf (f, "default:;\n"
  1907. "}\n"
  1908. "break;\n"
  1909. "}\n");
  1910. }
  1911. /* Close switch (TREE_CODE ()). */
  1912. if (exprs_len || fns_len || gexprs_len || gfns_len)
  1913. fprintf (f, "default:;\n"
  1914. "}\n");
  1915. for (unsigned i = 0; i < preds.length (); ++i)
  1916. {
  1917. expr *e = as_a <expr *> (preds[i]->op);
  1918. predicate_id *p = as_a <predicate_id *> (e->operation);
  1919. preds[i]->get_name (kid_opname);
  1920. fprintf (f, "tree %s_pops[%d];\n", kid_opname, p->nargs);
  1921. fprintf (f, "if (%s_%s (%s, %s_pops%s))\n",
  1922. gimple ? "gimple" : "tree",
  1923. p->id, kid_opname, kid_opname,
  1924. gimple ? ", valueize" : "");
  1925. fprintf (f, "{\n");
  1926. for (int j = 0; j < p->nargs; ++j)
  1927. {
  1928. char child_opname[20];
  1929. preds[i]->gen_opname (child_opname, j);
  1930. fprintf (f, "tree %s = %s_pops[%d];\n", child_opname, kid_opname, j);
  1931. }
  1932. preds[i]->gen_kids (f, gimple);
  1933. fprintf (f, "}\n");
  1934. }
  1935. for (unsigned i = 0; i < others.length (); ++i)
  1936. others[i]->gen (f, gimple);
  1937. }
  1938. /* Generate matching code for the decision tree operand. */
  1939. void
  1940. dt_operand::gen (FILE *f, bool gimple)
  1941. {
  1942. char opname[20];
  1943. get_name (opname);
  1944. unsigned n_braces = 0;
  1945. if (type == DT_OPERAND)
  1946. switch (op->type)
  1947. {
  1948. case operand::OP_PREDICATE:
  1949. n_braces = gen_predicate (f, opname, gimple);
  1950. break;
  1951. case operand::OP_EXPR:
  1952. if (gimple)
  1953. n_braces = gen_gimple_expr (f);
  1954. else
  1955. n_braces = gen_generic_expr (f, opname);
  1956. break;
  1957. default:
  1958. gcc_unreachable ();
  1959. }
  1960. else if (type == DT_TRUE)
  1961. ;
  1962. else if (type == DT_MATCH)
  1963. n_braces = gen_match_op (f, opname);
  1964. else
  1965. gcc_unreachable ();
  1966. gen_kids (f, gimple);
  1967. for (unsigned i = 0; i < n_braces; ++i)
  1968. fprintf (f, "}\n");
  1969. }
  1970. /* Generate code for the '(if ...)', '(with ..)' and actual transform
  1971. step of a '(simplify ...)' or '(match ...)'. This handles everything
  1972. that is not part of the decision tree (simplify->match). */
  1973. void
  1974. dt_simplify::gen (FILE *f, bool gimple)
  1975. {
  1976. fprintf (f, "{\n");
  1977. output_line_directive (f, s->result_location);
  1978. if (s->capture_max >= 0)
  1979. fprintf (f, "tree captures[%u] ATTRIBUTE_UNUSED = {};\n",
  1980. s->capture_max + 1);
  1981. for (int i = 0; i <= s->capture_max; ++i)
  1982. if (indexes[i])
  1983. {
  1984. char opname[20];
  1985. fprintf (f, "captures[%u] = %s;\n", i, indexes[i]->get_name (opname));
  1986. }
  1987. unsigned n_braces = 0;
  1988. if (s->ifexpr_vec != vNULL)
  1989. {
  1990. for (unsigned i = 0; i < s->ifexpr_vec.length (); ++i)
  1991. {
  1992. if_or_with &w = s->ifexpr_vec[i];
  1993. if (w.is_with)
  1994. {
  1995. fprintf (f, "{\n");
  1996. output_line_directive (f, w.location);
  1997. w.cexpr->gen_transform (f, NULL, true, 1, "type", NULL);
  1998. n_braces++;
  1999. }
  2000. else
  2001. {
  2002. output_line_directive (f, w.location);
  2003. fprintf (f, "if (");
  2004. if (i == s->ifexpr_vec.length () - 1
  2005. || s->ifexpr_vec[i+1].is_with)
  2006. w.cexpr->gen_transform (f, NULL, true, 1, "type", NULL);
  2007. else
  2008. {
  2009. unsigned j = i;
  2010. do
  2011. {
  2012. if (j != i)
  2013. {
  2014. fprintf (f, "\n");
  2015. output_line_directive (f, s->ifexpr_vec[j].location);
  2016. fprintf (f, "&& ");
  2017. }
  2018. fprintf (f, "(");
  2019. s->ifexpr_vec[j].cexpr->gen_transform (f, NULL,
  2020. true, 1, "type",
  2021. NULL);
  2022. fprintf (f, ")");
  2023. ++j;
  2024. }
  2025. while (j < s->ifexpr_vec.length ()
  2026. && !s->ifexpr_vec[j].is_with);
  2027. i = j - 1;
  2028. }
  2029. fprintf (f, ")\n");
  2030. }
  2031. }
  2032. fprintf (f, "{\n");
  2033. n_braces++;
  2034. }
  2035. /* Analyze captures and perform early-outs on the incoming arguments
  2036. that cover cases we cannot handle. */
  2037. capture_info cinfo (s);
  2038. expr *e;
  2039. if (!gimple
  2040. && s->result
  2041. && !((e = dyn_cast <expr *> (s->result))
  2042. && is_a <predicate_id *> (e->operation)))
  2043. {
  2044. for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i)
  2045. if (cinfo.force_no_side_effects & (1 << i))
  2046. fprintf (f, "if (TREE_SIDE_EFFECTS (op%d)) return NULL_TREE;\n", i);
  2047. for (int i = 0; i <= s->capture_max; ++i)
  2048. if (cinfo.info[i].cse_p)
  2049. ;
  2050. else if (cinfo.info[i].force_no_side_effects_p
  2051. && (cinfo.info[i].toplevel_msk
  2052. & cinfo.force_no_side_effects) == 0)
  2053. fprintf (f, "if (TREE_SIDE_EFFECTS (captures[%d])) "
  2054. "return NULL_TREE;\n", i);
  2055. else if ((cinfo.info[i].toplevel_msk
  2056. & cinfo.force_no_side_effects) != 0)
  2057. /* Mark capture as having no side-effects if we had to verify
  2058. that via forced toplevel operand checks. */
  2059. cinfo.info[i].force_no_side_effects_p = true;
  2060. }
  2061. fprintf (f, "if (dump_file && (dump_flags & TDF_DETAILS)) "
  2062. "fprintf (dump_file, \"Applying pattern ");
  2063. output_line_directive (f, s->result_location, true);
  2064. fprintf (f, ", %%s:%%d\\n\", __FILE__, __LINE__);\n");
  2065. operand *result = s->result;
  2066. if (!result)
  2067. {
  2068. /* If there is no result then this is a predicate implementation. */
  2069. fprintf (f, "return true;\n");
  2070. }
  2071. else if (gimple)
  2072. {
  2073. /* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears
  2074. in outermost position). */
  2075. if (result->type == operand::OP_EXPR
  2076. && *as_a <expr *> (result)->operation == NON_LVALUE_EXPR)
  2077. result = as_a <expr *> (result)->ops[0];
  2078. if (result->type == operand::OP_EXPR)
  2079. {
  2080. expr *e = as_a <expr *> (result);
  2081. bool is_predicate = is_a <predicate_id *> (e->operation);
  2082. if (!is_predicate)
  2083. fprintf (f, "*res_code = %s;\n",
  2084. *e->operation == CONVERT_EXPR
  2085. ? "NOP_EXPR" : e->operation->id);
  2086. for (unsigned j = 0; j < e->ops.length (); ++j)
  2087. {
  2088. char dest[32];
  2089. snprintf (dest, 32, " res_ops[%d]", j);
  2090. const char *optype
  2091. = get_operand_type (e->operation,
  2092. "type", e->expr_type,
  2093. j == 0
  2094. ? NULL : "TREE_TYPE (res_ops[0])");
  2095. /* We need to expand GENERIC conditions we captured from
  2096. COND_EXPRs. */
  2097. bool expand_generic_cond_exprs_p
  2098. = (!is_predicate
  2099. /* But avoid doing that if the GENERIC condition is
  2100. valid - which it is in the first operand of COND_EXPRs
  2101. and VEC_COND_EXRPs. */
  2102. && ((!(*e->operation == COND_EXPR)
  2103. && !(*e->operation == VEC_COND_EXPR))
  2104. || j != 0));
  2105. e->ops[j]->gen_transform (f, dest, true, 1, optype, &cinfo,
  2106. indexes, expand_generic_cond_exprs_p);
  2107. }
  2108. /* Re-fold the toplevel result. It's basically an embedded
  2109. gimple_build w/o actually building the stmt. */
  2110. if (!is_predicate)
  2111. fprintf (f, "gimple_resimplify%d (seq, res_code, type, "
  2112. "res_ops, valueize);\n", e->ops.length ());
  2113. }
  2114. else if (result->type == operand::OP_CAPTURE
  2115. || result->type == operand::OP_C_EXPR)
  2116. {
  2117. result->gen_transform (f, "res_ops[0]", true, 1, "type",
  2118. &cinfo, indexes, false);
  2119. fprintf (f, "*res_code = TREE_CODE (res_ops[0]);\n");
  2120. if (is_a <capture *> (result)
  2121. && cinfo.info[as_a <capture *> (result)->where].cond_expr_cond_p)
  2122. {
  2123. /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
  2124. with substituting a capture of that. */
  2125. fprintf (f, "if (COMPARISON_CLASS_P (res_ops[0]))\n"
  2126. " {\n"
  2127. " tree tem = res_ops[0];\n"
  2128. " res_ops[0] = TREE_OPERAND (tem, 0);\n"
  2129. " res_ops[1] = TREE_OPERAND (tem, 1);\n"
  2130. " }\n");
  2131. }
  2132. }
  2133. else
  2134. gcc_unreachable ();
  2135. fprintf (f, "return true;\n");
  2136. }
  2137. else /* GENERIC */
  2138. {
  2139. bool is_predicate = false;
  2140. if (result->type == operand::OP_EXPR)
  2141. {
  2142. expr *e = as_a <expr *> (result);
  2143. is_predicate = is_a <predicate_id *> (e->operation);
  2144. /* Search for captures used multiple times in the result expression
  2145. and dependent on TREE_SIDE_EFFECTS emit a SAVE_EXPR. */
  2146. if (!is_predicate)
  2147. for (int i = 0; i < s->capture_max + 1; ++i)
  2148. {
  2149. if (!cinfo.info[i].force_no_side_effects_p
  2150. && cinfo.info[i].result_use_count > 1)
  2151. fprintf (f, " if (TREE_SIDE_EFFECTS (captures[%d]))\n"
  2152. " captures[%d] = save_expr (captures[%d]);\n",
  2153. i, i, i);
  2154. }
  2155. for (unsigned j = 0; j < e->ops.length (); ++j)
  2156. {
  2157. char dest[32];
  2158. if (is_predicate)
  2159. snprintf (dest, 32, "res_ops[%d]", j);
  2160. else
  2161. {
  2162. fprintf (f, " tree res_op%d;\n", j);
  2163. snprintf (dest, 32, " res_op%d", j);
  2164. }
  2165. const char *optype
  2166. = get_operand_type (e->operation,
  2167. "type", e->expr_type,
  2168. j == 0
  2169. ? NULL : "TREE_TYPE (res_op0)");
  2170. e->ops[j]->gen_transform (f, dest, false, 1, optype,
  2171. &cinfo, indexes);
  2172. }
  2173. if (is_predicate)
  2174. fprintf (f, "return true;\n");
  2175. else
  2176. {
  2177. fprintf (f, " tree res;\n");
  2178. /* Re-fold the toplevel result. Use non_lvalue to
  2179. build NON_LVALUE_EXPRs so they get properly
  2180. ignored when in GIMPLE form. */
  2181. if (*e->operation == NON_LVALUE_EXPR)
  2182. fprintf (f, " res = non_lvalue_loc (loc, res_op0);\n");
  2183. else
  2184. {
  2185. if (e->operation->kind == id_base::CODE)
  2186. fprintf (f, " res = fold_build%d_loc (loc, %s, type",
  2187. e->ops.length (),
  2188. *e->operation == CONVERT_EXPR
  2189. ? "NOP_EXPR" : e->operation->id);
  2190. else
  2191. fprintf (f, " res = build_call_expr_loc "
  2192. "(loc, builtin_decl_implicit (%s), %d",
  2193. e->operation->id, e->ops.length());
  2194. for (unsigned j = 0; j < e->ops.length (); ++j)
  2195. fprintf (f, ", res_op%d", j);
  2196. fprintf (f, ");\n");
  2197. }
  2198. }
  2199. }
  2200. else if (result->type == operand::OP_CAPTURE
  2201. || result->type == operand::OP_C_EXPR)
  2202. {
  2203. fprintf (f, " tree res;\n");
  2204. s->result->gen_transform (f, " res", false, 1, "type",
  2205. &cinfo, indexes);
  2206. }
  2207. else
  2208. gcc_unreachable ();
  2209. if (!is_predicate)
  2210. {
  2211. /* Search for captures not used in the result expression and dependent
  2212. on TREE_SIDE_EFFECTS emit omit_one_operand. */
  2213. for (int i = 0; i < s->capture_max + 1; ++i)
  2214. {
  2215. if (!cinfo.info[i].force_no_side_effects_p
  2216. && !cinfo.info[i].expr_p
  2217. && cinfo.info[i].result_use_count == 0)
  2218. fprintf (f, " if (TREE_SIDE_EFFECTS (captures[%d]))\n"
  2219. " res = build2_loc (loc, COMPOUND_EXPR, type,"
  2220. " fold_ignored_result (captures[%d]), res);\n",
  2221. i, i);
  2222. }
  2223. fprintf (f, " return res;\n");
  2224. }
  2225. }
  2226. for (unsigned i = 0; i < n_braces; ++i)
  2227. fprintf (f, "}\n");
  2228. fprintf (f, "}\n");
  2229. }
  2230. /* Main entry to generate code for matching GIMPLE IL off the decision
  2231. tree. */
  2232. void
  2233. decision_tree::gen_gimple (FILE *f)
  2234. {
  2235. for (unsigned n = 1; n <= 3; ++n)
  2236. {
  2237. fprintf (f, "\nstatic bool\n"
  2238. "gimple_simplify (code_helper *res_code, tree *res_ops,\n"
  2239. " gimple_seq *seq, tree (*valueize)(tree),\n"
  2240. " code_helper code, tree type");
  2241. for (unsigned i = 0; i < n; ++i)
  2242. fprintf (f, ", tree op%d", i);
  2243. fprintf (f, ")\n");
  2244. fprintf (f, "{\n");
  2245. fprintf (f, "switch (code.get_rep())\n"
  2246. "{\n");
  2247. for (unsigned i = 0; i < root->kids.length (); i++)
  2248. {
  2249. dt_operand *dop = static_cast<dt_operand *>(root->kids[i]);
  2250. expr *e = static_cast<expr *>(dop->op);
  2251. if (e->ops.length () != n)
  2252. continue;
  2253. if (*e->operation == CONVERT_EXPR
  2254. || *e->operation == NOP_EXPR)
  2255. fprintf (f, "CASE_CONVERT:\n");
  2256. else
  2257. fprintf (f, "case %s%s:\n",
  2258. is_a <fn_id *> (e->operation) ? "-" : "",
  2259. e->operation->id);
  2260. fprintf (f, "{\n");
  2261. dop->gen_kids (f, true);
  2262. fprintf (f, "break;\n");
  2263. fprintf (f, "}\n");
  2264. }
  2265. fprintf (f, "default:;\n"
  2266. "}\n");
  2267. fprintf (f, "return false;\n");
  2268. fprintf (f, "}\n");
  2269. }
  2270. }
  2271. /* Main entry to generate code for matching GENERIC IL off the decision
  2272. tree. */
  2273. void
  2274. decision_tree::gen_generic (FILE *f)
  2275. {
  2276. for (unsigned n = 1; n <= 3; ++n)
  2277. {
  2278. fprintf (f, "\ntree\n"
  2279. "generic_simplify (location_t loc, enum tree_code code, "
  2280. "tree type ATTRIBUTE_UNUSED");
  2281. for (unsigned i = 0; i < n; ++i)
  2282. fprintf (f, ", tree op%d", i);
  2283. fprintf (f, ")\n");
  2284. fprintf (f, "{\n");
  2285. fprintf (f, "switch (code)\n"
  2286. "{\n");
  2287. for (unsigned i = 0; i < root->kids.length (); i++)
  2288. {
  2289. dt_operand *dop = static_cast<dt_operand *>(root->kids[i]);
  2290. expr *e = static_cast<expr *>(dop->op);
  2291. if (e->ops.length () != n
  2292. /* Builtin simplifications are somewhat premature on
  2293. GENERIC. The following drops patterns with outermost
  2294. calls. It's easy to emit overloads for function code
  2295. though if necessary. */
  2296. || e->operation->kind != id_base::CODE)
  2297. continue;
  2298. operator_id *op_id = static_cast <operator_id *> (e->operation);
  2299. if (op_id->code == NOP_EXPR || op_id->code == CONVERT_EXPR)
  2300. fprintf (f, "CASE_CONVERT:\n");
  2301. else
  2302. fprintf (f, "case %s:\n", e->operation->id);
  2303. fprintf (f, "{\n");
  2304. dop->gen_kids (f, false);
  2305. fprintf (f, "break;\n"
  2306. "}\n");
  2307. }
  2308. fprintf (f, "default:;\n"
  2309. "}\n");
  2310. fprintf (f, "return NULL_TREE;\n");
  2311. fprintf (f, "}\n");
  2312. }
  2313. }
  2314. /* Output code to implement the predicate P from the decision tree DT. */
  2315. void
  2316. write_predicate (FILE *f, predicate_id *p, decision_tree &dt, bool gimple)
  2317. {
  2318. fprintf (f, "\nbool\n"
  2319. "%s%s (tree t%s%s)\n"
  2320. "{\n", gimple ? "gimple_" : "tree_", p->id,
  2321. p->nargs > 0 ? ", tree *res_ops" : "",
  2322. gimple ? ", tree (*valueize)(tree)" : "");
  2323. /* Conveniently make 'type' available. */
  2324. fprintf (f, "tree type = TREE_TYPE (t);\n");
  2325. if (!gimple)
  2326. fprintf (f, "if (TREE_SIDE_EFFECTS (t)) return false;\n");
  2327. dt.root->gen_kids (f, gimple);
  2328. fprintf (f, "return false;\n"
  2329. "}\n");
  2330. }
  2331. /* Write the common header for the GIMPLE/GENERIC IL matching routines. */
  2332. static void
  2333. write_header (FILE *f, const char *head)
  2334. {
  2335. fprintf (f, "/* Generated automatically by the program `genmatch' from\n");
  2336. fprintf (f, " a IL pattern matching and simplification description. */\n");
  2337. /* Include the header instead of writing it awkwardly quoted here. */
  2338. fprintf (f, "\n#include \"%s\"\n", head);
  2339. }
  2340. /* AST parsing. */
  2341. class parser
  2342. {
  2343. public:
  2344. parser (cpp_reader *);
  2345. private:
  2346. const cpp_token *next ();
  2347. const cpp_token *peek ();
  2348. const cpp_token *peek_ident (const char * = NULL);
  2349. const cpp_token *expect (enum cpp_ttype);
  2350. void eat_token (enum cpp_ttype);
  2351. const char *get_string ();
  2352. const char *get_ident ();
  2353. void eat_ident (const char *);
  2354. const char *get_number ();
  2355. id_base *parse_operation ();
  2356. operand *parse_capture (operand *);
  2357. operand *parse_expr ();
  2358. c_expr *parse_c_expr (cpp_ttype);
  2359. operand *parse_op ();
  2360. void record_operlist (source_location, user_id *);
  2361. void parse_pattern ();
  2362. void push_simplify (vec<simplify *>&, operand *, source_location,
  2363. operand *, source_location);
  2364. void parse_simplify (source_location, vec<simplify *>&, predicate_id *,
  2365. expr *);
  2366. void parse_for (source_location);
  2367. void parse_if (source_location);
  2368. void parse_predicates (source_location);
  2369. void parse_operator_list (source_location);
  2370. cpp_reader *r;
  2371. vec<if_or_with> active_ifs;
  2372. vec<vec<user_id *> > active_fors;
  2373. hash_set<user_id *> *oper_lists_set;
  2374. vec<user_id *> oper_lists;
  2375. cid_map_t *capture_ids;
  2376. public:
  2377. vec<simplify *> simplifiers;
  2378. vec<predicate_id *> user_predicates;
  2379. bool parsing_match_operand;
  2380. };
  2381. /* Lexing helpers. */
  2382. /* Read the next non-whitespace token from R. */
  2383. const cpp_token *
  2384. parser::next ()
  2385. {
  2386. const cpp_token *token;
  2387. do
  2388. {
  2389. token = cpp_get_token (r);
  2390. }
  2391. while (token->type == CPP_PADDING
  2392. && token->type != CPP_EOF);
  2393. return token;
  2394. }
  2395. /* Peek at the next non-whitespace token from R. */
  2396. const cpp_token *
  2397. parser::peek ()
  2398. {
  2399. const cpp_token *token;
  2400. unsigned i = 0;
  2401. do
  2402. {
  2403. token = cpp_peek_token (r, i++);
  2404. }
  2405. while (token->type == CPP_PADDING
  2406. && token->type != CPP_EOF);
  2407. /* If we peek at EOF this is a fatal error as it leaves the
  2408. cpp_reader in unusable state. Assume we really wanted a
  2409. token and thus this EOF is unexpected. */
  2410. if (token->type == CPP_EOF)
  2411. fatal_at (token, "unexpected end of file");
  2412. return token;
  2413. }
  2414. /* Peek at the next identifier token (or return NULL if the next
  2415. token is not an identifier or equal to ID if supplied). */
  2416. const cpp_token *
  2417. parser::peek_ident (const char *id)
  2418. {
  2419. const cpp_token *token = peek ();
  2420. if (token->type != CPP_NAME)
  2421. return 0;
  2422. if (id == 0)
  2423. return token;
  2424. const char *t = (const char *) CPP_HASHNODE (token->val.node.node)->ident.str;
  2425. if (strcmp (id, t) == 0)
  2426. return token;
  2427. return 0;
  2428. }
  2429. /* Read the next token from R and assert it is of type TK. */
  2430. const cpp_token *
  2431. parser::expect (enum cpp_ttype tk)
  2432. {
  2433. const cpp_token *token = next ();
  2434. if (token->type != tk)
  2435. fatal_at (token, "expected %s, got %s",
  2436. cpp_type2name (tk, 0), cpp_type2name (token->type, 0));
  2437. return token;
  2438. }
  2439. /* Consume the next token from R and assert it is of type TK. */
  2440. void
  2441. parser::eat_token (enum cpp_ttype tk)
  2442. {
  2443. expect (tk);
  2444. }
  2445. /* Read the next token from R and assert it is of type CPP_STRING and
  2446. return its value. */
  2447. const char *
  2448. parser::get_string ()
  2449. {
  2450. const cpp_token *token = expect (CPP_STRING);
  2451. return (const char *)token->val.str.text;
  2452. }
  2453. /* Read the next token from R and assert it is of type CPP_NAME and
  2454. return its value. */
  2455. const char *
  2456. parser::get_ident ()
  2457. {
  2458. const cpp_token *token = expect (CPP_NAME);
  2459. return (const char *)CPP_HASHNODE (token->val.node.node)->ident.str;
  2460. }
  2461. /* Eat an identifier token with value S from R. */
  2462. void
  2463. parser::eat_ident (const char *s)
  2464. {
  2465. const cpp_token *token = peek ();
  2466. const char *t = get_ident ();
  2467. if (strcmp (s, t) != 0)
  2468. fatal_at (token, "expected '%s' got '%s'\n", s, t);
  2469. }
  2470. /* Read the next token from R and assert it is of type CPP_NUMBER and
  2471. return its value. */
  2472. const char *
  2473. parser::get_number ()
  2474. {
  2475. const cpp_token *token = expect (CPP_NUMBER);
  2476. return (const char *)token->val.str.text;
  2477. }
  2478. /* Record an operator-list use for transparent for handling. */
  2479. void
  2480. parser::record_operlist (source_location loc, user_id *p)
  2481. {
  2482. if (!oper_lists_set->add (p))
  2483. {
  2484. if (!oper_lists.is_empty ()
  2485. && oper_lists[0]->substitutes.length () != p->substitutes.length ())
  2486. fatal_at (loc, "User-defined operator list does not have the "
  2487. "same number of entries as others used in the pattern");
  2488. oper_lists.safe_push (p);
  2489. }
  2490. }
  2491. /* Parse the operator ID, special-casing convert?, convert1? and
  2492. convert2? */
  2493. id_base *
  2494. parser::parse_operation ()
  2495. {
  2496. const cpp_token *id_tok = peek ();
  2497. const char *id = get_ident ();
  2498. const cpp_token *token = peek ();
  2499. if (strcmp (id, "convert0") == 0)
  2500. fatal_at (id_tok, "use 'convert?' here");
  2501. if (token->type == CPP_QUERY
  2502. && !(token->flags & PREV_WHITE))
  2503. {
  2504. if (strcmp (id, "convert") == 0)
  2505. id = "convert0";
  2506. else if (strcmp (id, "convert1") == 0)
  2507. ;
  2508. else if (strcmp (id, "convert2") == 0)
  2509. ;
  2510. else
  2511. fatal_at (id_tok, "non-convert operator conditionalized");
  2512. if (!parsing_match_operand)
  2513. fatal_at (id_tok, "conditional convert can only be used in "
  2514. "match expression");
  2515. eat_token (CPP_QUERY);
  2516. }
  2517. else if (strcmp (id, "convert1") == 0
  2518. || strcmp (id, "convert2") == 0)
  2519. fatal_at (id_tok, "expected '?' after conditional operator");
  2520. id_base *op = get_operator (id);
  2521. if (!op)
  2522. fatal_at (id_tok, "unknown operator %s", id);
  2523. user_id *p = dyn_cast<user_id *> (op);
  2524. if (p && p->is_oper_list)
  2525. record_operlist (id_tok->src_loc, p);
  2526. return op;
  2527. }
  2528. /* Parse a capture.
  2529. capture = '@'<number> */
  2530. struct operand *
  2531. parser::parse_capture (operand *op)
  2532. {
  2533. eat_token (CPP_ATSIGN);
  2534. const cpp_token *token = peek ();
  2535. const char *id = NULL;
  2536. if (token->type == CPP_NUMBER)
  2537. id = get_number ();
  2538. else if (token->type == CPP_NAME)
  2539. id = get_ident ();
  2540. else
  2541. fatal_at (token, "expected number or identifier");
  2542. unsigned next_id = capture_ids->elements ();
  2543. bool existed;
  2544. unsigned &num = capture_ids->get_or_insert (id, &existed);
  2545. if (!existed)
  2546. num = next_id;
  2547. return new capture (num, op);
  2548. }
  2549. /* Parse an expression
  2550. expr = '(' <operation>[capture][flag][type] <operand>... ')' */
  2551. struct operand *
  2552. parser::parse_expr ()
  2553. {
  2554. expr *e = new expr (parse_operation ());
  2555. const cpp_token *token = peek ();
  2556. operand *op;
  2557. bool is_commutative = false;
  2558. const char *expr_type = NULL;
  2559. if (token->type == CPP_COLON
  2560. && !(token->flags & PREV_WHITE))
  2561. {
  2562. eat_token (CPP_COLON);
  2563. token = peek ();
  2564. if (token->type == CPP_NAME
  2565. && !(token->flags & PREV_WHITE))
  2566. {
  2567. const char *s = get_ident ();
  2568. if (s[0] == 'c' && !s[1])
  2569. {
  2570. if (!parsing_match_operand)
  2571. fatal_at (token,
  2572. "flag 'c' can only be used in match expression");
  2573. is_commutative = true;
  2574. }
  2575. else if (s[1] != '\0')
  2576. {
  2577. if (parsing_match_operand)
  2578. fatal_at (token, "type can only be used in result expression");
  2579. expr_type = s;
  2580. }
  2581. else
  2582. fatal_at (token, "flag %s not recognized", s);
  2583. token = peek ();
  2584. }
  2585. else
  2586. fatal_at (token, "expected flag or type specifying identifier");
  2587. }
  2588. if (token->type == CPP_ATSIGN
  2589. && !(token->flags & PREV_WHITE))
  2590. op = parse_capture (e);
  2591. else
  2592. op = e;
  2593. do
  2594. {
  2595. const cpp_token *token = peek ();
  2596. if (token->type == CPP_CLOSE_PAREN)
  2597. {
  2598. if (e->operation->nargs != -1
  2599. && e->operation->nargs != (int) e->ops.length ())
  2600. fatal_at (token, "'%s' expects %u operands, not %u",
  2601. e->operation->id, e->operation->nargs, e->ops.length ());
  2602. if (is_commutative)
  2603. {
  2604. if (e->ops.length () == 2)
  2605. e->is_commutative = true;
  2606. else
  2607. fatal_at (token, "only binary operators or function with "
  2608. "two arguments can be marked commutative");
  2609. }
  2610. e->expr_type = expr_type;
  2611. return op;
  2612. }
  2613. e->append_op (parse_op ());
  2614. }
  2615. while (1);
  2616. }
  2617. /* Lex native C code delimited by START recording the preprocessing tokens
  2618. for later processing.
  2619. c_expr = ('{'|'(') <pp token>... ('}'|')') */
  2620. c_expr *
  2621. parser::parse_c_expr (cpp_ttype start)
  2622. {
  2623. const cpp_token *token;
  2624. cpp_ttype end;
  2625. unsigned opencnt;
  2626. vec<cpp_token> code = vNULL;
  2627. unsigned nr_stmts = 0;
  2628. eat_token (start);
  2629. if (start == CPP_OPEN_PAREN)
  2630. end = CPP_CLOSE_PAREN;
  2631. else if (start == CPP_OPEN_BRACE)
  2632. end = CPP_CLOSE_BRACE;
  2633. else
  2634. gcc_unreachable ();
  2635. opencnt = 1;
  2636. do
  2637. {
  2638. token = next ();
  2639. /* Count brace pairs to find the end of the expr to match. */
  2640. if (token->type == start)
  2641. opencnt++;
  2642. else if (token->type == end
  2643. && --opencnt == 0)
  2644. break;
  2645. /* This is a lame way of counting the number of statements. */
  2646. if (token->type == CPP_SEMICOLON)
  2647. nr_stmts++;
  2648. /* If this is possibly a user-defined identifier mark it used. */
  2649. if (token->type == CPP_NAME)
  2650. {
  2651. id_base *idb = get_operator ((const char *)CPP_HASHNODE
  2652. (token->val.node.node)->ident.str);
  2653. user_id *p;
  2654. if (idb && (p = dyn_cast<user_id *> (idb)) && p->is_oper_list)
  2655. record_operlist (token->src_loc, p);
  2656. }
  2657. /* Record the token. */
  2658. code.safe_push (*token);
  2659. }
  2660. while (1);
  2661. return new c_expr (r, code, nr_stmts, vNULL, capture_ids);
  2662. }
  2663. /* Parse an operand which is either an expression, a predicate or
  2664. a standalone capture.
  2665. op = predicate | expr | c_expr | capture */
  2666. struct operand *
  2667. parser::parse_op ()
  2668. {
  2669. const cpp_token *token = peek ();
  2670. struct operand *op = NULL;
  2671. if (token->type == CPP_OPEN_PAREN)
  2672. {
  2673. eat_token (CPP_OPEN_PAREN);
  2674. op = parse_expr ();
  2675. eat_token (CPP_CLOSE_PAREN);
  2676. }
  2677. else if (token->type == CPP_OPEN_BRACE)
  2678. {
  2679. op = parse_c_expr (CPP_OPEN_BRACE);
  2680. }
  2681. else
  2682. {
  2683. /* Remaining ops are either empty or predicates */
  2684. if (token->type == CPP_NAME)
  2685. {
  2686. const char *id = get_ident ();
  2687. id_base *opr = get_operator (id);
  2688. if (!opr)
  2689. fatal_at (token, "expected predicate name");
  2690. if (operator_id *code = dyn_cast <operator_id *> (opr))
  2691. {
  2692. if (code->nargs != 0)
  2693. fatal_at (token, "using an operator with operands as predicate");
  2694. /* Parse the zero-operand operator "predicates" as
  2695. expression. */
  2696. op = new expr (opr);
  2697. }
  2698. else if (user_id *code = dyn_cast <user_id *> (opr))
  2699. {
  2700. if (code->nargs != 0)
  2701. fatal_at (token, "using an operator with operands as predicate");
  2702. /* Parse the zero-operand operator "predicates" as
  2703. expression. */
  2704. op = new expr (opr);
  2705. }
  2706. else if (predicate_id *p = dyn_cast <predicate_id *> (opr))
  2707. op = new predicate (p);
  2708. else
  2709. fatal_at (token, "using an unsupported operator as predicate");
  2710. if (!parsing_match_operand)
  2711. fatal_at (token, "predicates are only allowed in match expression");
  2712. token = peek ();
  2713. if (token->flags & PREV_WHITE)
  2714. return op;
  2715. }
  2716. else if (token->type != CPP_COLON
  2717. && token->type != CPP_ATSIGN)
  2718. fatal_at (token, "expected expression or predicate");
  2719. /* optionally followed by a capture and a predicate. */
  2720. if (token->type == CPP_COLON)
  2721. fatal_at (token, "not implemented: predicate on leaf operand");
  2722. if (token->type == CPP_ATSIGN)
  2723. op = parse_capture (op);
  2724. }
  2725. return op;
  2726. }
  2727. /* Create a new simplify from the current parsing state and MATCH,
  2728. MATCH_LOC, RESULT and RESULT_LOC and push it to SIMPLIFIERS. */
  2729. void
  2730. parser::push_simplify (vec<simplify *>& simplifiers,
  2731. operand *match, source_location match_loc,
  2732. operand *result, source_location result_loc)
  2733. {
  2734. /* Build and push a temporary for for operator list uses in expressions. */
  2735. if (!oper_lists.is_empty ())
  2736. active_fors.safe_push (oper_lists);
  2737. simplifiers.safe_push
  2738. (new simplify (match, match_loc, result, result_loc,
  2739. active_ifs.copy (), active_fors.copy (), capture_ids));
  2740. if (!oper_lists.is_empty ())
  2741. active_fors.pop ();
  2742. }
  2743. /* Parse
  2744. simplify = 'simplify' <expr> <result-op>
  2745. or
  2746. match = 'match' <ident> <expr> [<result-op>]
  2747. with
  2748. <result-op> = <op> | <if> | <with>
  2749. <if> = '(' 'if' '(' <c-expr> ')' <result-op> ')'
  2750. <with> = '(' 'with' '{' <c-expr> '}' <result-op> ')'
  2751. and fill SIMPLIFIERS with the results. */
  2752. void
  2753. parser::parse_simplify (source_location match_location,
  2754. vec<simplify *>& simplifiers, predicate_id *matcher,
  2755. expr *result)
  2756. {
  2757. /* Reset the capture map. */
  2758. if (!capture_ids)
  2759. capture_ids = new cid_map_t;
  2760. /* Reset oper_lists and set. */
  2761. hash_set <user_id *> olist;
  2762. oper_lists_set = &olist;
  2763. oper_lists = vNULL;
  2764. const cpp_token *loc = peek ();
  2765. parsing_match_operand = true;
  2766. struct operand *match = parse_op ();
  2767. parsing_match_operand = false;
  2768. if (match->type == operand::OP_CAPTURE && !matcher)
  2769. fatal_at (loc, "outermost expression cannot be captured");
  2770. if (match->type == operand::OP_EXPR
  2771. && is_a <predicate_id *> (as_a <expr *> (match)->operation))
  2772. fatal_at (loc, "outermost expression cannot be a predicate");
  2773. const cpp_token *token = peek ();
  2774. /* If this if is immediately closed then it is part of a predicate
  2775. definition. Push it. */
  2776. if (token->type == CPP_CLOSE_PAREN)
  2777. {
  2778. if (!matcher)
  2779. fatal_at (token, "expected transform expression");
  2780. push_simplify (simplifiers, match, match_location,
  2781. result, token->src_loc);
  2782. return;
  2783. }
  2784. unsigned active_ifs_len = active_ifs.length ();
  2785. while (1)
  2786. {
  2787. if (token->type == CPP_OPEN_PAREN)
  2788. {
  2789. source_location paren_loc = token->src_loc;
  2790. eat_token (CPP_OPEN_PAREN);
  2791. if (peek_ident ("if"))
  2792. {
  2793. eat_ident ("if");
  2794. active_ifs.safe_push (if_or_with (parse_c_expr (CPP_OPEN_PAREN),
  2795. token->src_loc, false));
  2796. /* If this if is immediately closed then it contains a
  2797. manual matcher or is part of a predicate definition.
  2798. Push it. */
  2799. if (peek ()->type == CPP_CLOSE_PAREN)
  2800. {
  2801. if (!matcher)
  2802. fatal_at (token, "manual transform not implemented");
  2803. push_simplify (simplifiers, match, match_location,
  2804. result, paren_loc);
  2805. }
  2806. }
  2807. else if (peek_ident ("with"))
  2808. {
  2809. eat_ident ("with");
  2810. /* Parse (with c-expr expr) as (if-with (true) expr). */
  2811. c_expr *e = parse_c_expr (CPP_OPEN_BRACE);
  2812. e->nr_stmts = 0;
  2813. active_ifs.safe_push (if_or_with (e, token->src_loc, true));
  2814. }
  2815. else
  2816. {
  2817. operand *op = result;
  2818. if (!matcher)
  2819. op = parse_expr ();
  2820. push_simplify (simplifiers, match, match_location,
  2821. op, token->src_loc);
  2822. eat_token (CPP_CLOSE_PAREN);
  2823. /* A "default" result closes the enclosing scope. */
  2824. if (active_ifs.length () > active_ifs_len)
  2825. {
  2826. eat_token (CPP_CLOSE_PAREN);
  2827. active_ifs.pop ();
  2828. }
  2829. else
  2830. return;
  2831. }
  2832. }
  2833. else if (token->type == CPP_CLOSE_PAREN)
  2834. {
  2835. /* Close a scope if requested. */
  2836. if (active_ifs.length () > active_ifs_len)
  2837. {
  2838. eat_token (CPP_CLOSE_PAREN);
  2839. active_ifs.pop ();
  2840. token = peek ();
  2841. }
  2842. else
  2843. return;
  2844. }
  2845. else
  2846. {
  2847. if (matcher)
  2848. fatal_at (token, "expected match operand expression");
  2849. push_simplify (simplifiers, match, match_location,
  2850. matcher ? result : parse_op (), token->src_loc);
  2851. /* A "default" result closes the enclosing scope. */
  2852. if (active_ifs.length () > active_ifs_len)
  2853. {
  2854. eat_token (CPP_CLOSE_PAREN);
  2855. active_ifs.pop ();
  2856. }
  2857. else
  2858. return;
  2859. }
  2860. token = peek ();
  2861. }
  2862. }
  2863. /* Parsing of the outer control structures. */
  2864. /* Parse a for expression
  2865. for = '(' 'for' <subst>... <pattern> ')'
  2866. subst = <ident> '(' <ident>... ')' */
  2867. void
  2868. parser::parse_for (source_location)
  2869. {
  2870. auto_vec<const cpp_token *> user_id_tokens;
  2871. vec<user_id *> user_ids = vNULL;
  2872. const cpp_token *token;
  2873. unsigned min_n_opers = 0, max_n_opers = 0;
  2874. while (1)
  2875. {
  2876. token = peek ();
  2877. if (token->type != CPP_NAME)
  2878. break;
  2879. /* Insert the user defined operators into the operator hash. */
  2880. const char *id = get_ident ();
  2881. if (get_operator (id) != NULL)
  2882. fatal_at (token, "operator already defined");
  2883. user_id *op = new user_id (id);
  2884. id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
  2885. *slot = op;
  2886. user_ids.safe_push (op);
  2887. user_id_tokens.safe_push (token);
  2888. eat_token (CPP_OPEN_PAREN);
  2889. int arity = -1;
  2890. while ((token = peek_ident ()) != 0)
  2891. {
  2892. const char *oper = get_ident ();
  2893. id_base *idb = get_operator (oper);
  2894. if (idb == NULL)
  2895. fatal_at (token, "no such operator '%s'", oper);
  2896. if (*idb == CONVERT0 || *idb == CONVERT1 || *idb == CONVERT2)
  2897. fatal_at (token, "conditional operators cannot be used inside for");
  2898. if (arity == -1)
  2899. arity = idb->nargs;
  2900. else if (idb->nargs == -1)
  2901. ;
  2902. else if (idb->nargs != arity)
  2903. fatal_at (token, "operator '%s' with arity %d does not match "
  2904. "others with arity %d", oper, idb->nargs, arity);
  2905. user_id *p = dyn_cast<user_id *> (idb);
  2906. if (p && p->is_oper_list)
  2907. op->substitutes.safe_splice (p->substitutes);
  2908. else
  2909. op->substitutes.safe_push (idb);
  2910. }
  2911. op->nargs = arity;
  2912. token = expect (CPP_CLOSE_PAREN);
  2913. unsigned nsubstitutes = op->substitutes.length ();
  2914. if (nsubstitutes == 0)
  2915. fatal_at (token, "A user-defined operator must have at least "
  2916. "one substitution");
  2917. if (max_n_opers == 0)
  2918. {
  2919. min_n_opers = nsubstitutes;
  2920. max_n_opers = nsubstitutes;
  2921. }
  2922. else
  2923. {
  2924. if (nsubstitutes % min_n_opers != 0
  2925. && min_n_opers % nsubstitutes != 0)
  2926. fatal_at (token, "All user-defined identifiers must have a "
  2927. "multiple number of operator substitutions of the "
  2928. "smallest number of substitutions");
  2929. if (nsubstitutes < min_n_opers)
  2930. min_n_opers = nsubstitutes;
  2931. else if (nsubstitutes > max_n_opers)
  2932. max_n_opers = nsubstitutes;
  2933. }
  2934. }
  2935. unsigned n_ids = user_ids.length ();
  2936. if (n_ids == 0)
  2937. fatal_at (token, "for requires at least one user-defined identifier");
  2938. token = peek ();
  2939. if (token->type == CPP_CLOSE_PAREN)
  2940. fatal_at (token, "no pattern defined in for");
  2941. active_fors.safe_push (user_ids);
  2942. while (1)
  2943. {
  2944. token = peek ();
  2945. if (token->type == CPP_CLOSE_PAREN)
  2946. break;
  2947. parse_pattern ();
  2948. }
  2949. active_fors.pop ();
  2950. /* Remove user-defined operators from the hash again. */
  2951. for (unsigned i = 0; i < user_ids.length (); ++i)
  2952. {
  2953. if (!user_ids[i]->used)
  2954. warning_at (user_id_tokens[i],
  2955. "operator %s defined but not used", user_ids[i]->id);
  2956. operators->remove_elt (user_ids[i]);
  2957. }
  2958. }
  2959. /* Parse an identifier associated with a list of operators.
  2960. oprs = '(' 'define_operator_list' <ident> <ident>... ')' */
  2961. void
  2962. parser::parse_operator_list (source_location)
  2963. {
  2964. const cpp_token *token = peek ();
  2965. const char *id = get_ident ();
  2966. if (get_operator (id) != 0)
  2967. fatal_at (token, "operator %s already defined", id);
  2968. user_id *op = new user_id (id, true);
  2969. int arity = -1;
  2970. while ((token = peek_ident ()) != 0)
  2971. {
  2972. token = peek ();
  2973. const char *oper = get_ident ();
  2974. id_base *idb = get_operator (oper);
  2975. if (idb == 0)
  2976. fatal_at (token, "no such operator '%s'", oper);
  2977. if (arity == -1)
  2978. arity = idb->nargs;
  2979. else if (idb->nargs == -1)
  2980. ;
  2981. else if (arity != idb->nargs)
  2982. fatal_at (token, "operator '%s' with arity %d does not match "
  2983. "others with arity %d", oper, idb->nargs, arity);
  2984. /* We allow composition of multiple operator lists. */
  2985. if (user_id *p = dyn_cast<user_id *> (idb))
  2986. op->substitutes.safe_splice (p->substitutes);
  2987. else
  2988. op->substitutes.safe_push (idb);
  2989. }
  2990. if (op->substitutes.length () == 0)
  2991. fatal_at (token, "operator-list cannot be empty");
  2992. op->nargs = arity;
  2993. id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
  2994. *slot = op;
  2995. }
  2996. /* Parse an outer if expression.
  2997. if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */
  2998. void
  2999. parser::parse_if (source_location loc)
  3000. {
  3001. operand *ifexpr = parse_c_expr (CPP_OPEN_PAREN);
  3002. const cpp_token *token = peek ();
  3003. if (token->type == CPP_CLOSE_PAREN)
  3004. fatal_at (token, "no pattern defined in if");
  3005. active_ifs.safe_push (if_or_with (ifexpr, loc, false));
  3006. while (1)
  3007. {
  3008. const cpp_token *token = peek ();
  3009. if (token->type == CPP_CLOSE_PAREN)
  3010. break;
  3011. parse_pattern ();
  3012. }
  3013. active_ifs.pop ();
  3014. }
  3015. /* Parse a list of predefined predicate identifiers.
  3016. preds = '(' 'define_predicates' <ident>... ')' */
  3017. void
  3018. parser::parse_predicates (source_location)
  3019. {
  3020. do
  3021. {
  3022. const cpp_token *token = peek ();
  3023. if (token->type != CPP_NAME)
  3024. break;
  3025. add_predicate (get_ident ());
  3026. }
  3027. while (1);
  3028. }
  3029. /* Parse outer control structures.
  3030. pattern = <preds>|<for>|<if>|<simplify>|<match> */
  3031. void
  3032. parser::parse_pattern ()
  3033. {
  3034. /* All clauses start with '('. */
  3035. eat_token (CPP_OPEN_PAREN);
  3036. const cpp_token *token = peek ();
  3037. const char *id = get_ident ();
  3038. if (strcmp (id, "simplify") == 0)
  3039. {
  3040. parse_simplify (token->src_loc, simplifiers, NULL, NULL);
  3041. capture_ids = NULL;
  3042. }
  3043. else if (strcmp (id, "match") == 0)
  3044. {
  3045. bool with_args = false;
  3046. if (peek ()->type == CPP_OPEN_PAREN)
  3047. {
  3048. eat_token (CPP_OPEN_PAREN);
  3049. with_args = true;
  3050. }
  3051. const char *name = get_ident ();
  3052. id_base *id = get_operator (name);
  3053. predicate_id *p;
  3054. if (!id)
  3055. {
  3056. p = add_predicate (name);
  3057. user_predicates.safe_push (p);
  3058. }
  3059. else if ((p = dyn_cast <predicate_id *> (id)))
  3060. ;
  3061. else
  3062. fatal_at (token, "cannot add a match to a non-predicate ID");
  3063. /* Parse (match <id> <arg>... (match-expr)) here. */
  3064. expr *e = NULL;
  3065. if (with_args)
  3066. {
  3067. capture_ids = new cid_map_t;
  3068. e = new expr (p);
  3069. while (peek ()->type == CPP_ATSIGN)
  3070. e->append_op (parse_capture (NULL));
  3071. eat_token (CPP_CLOSE_PAREN);
  3072. }
  3073. if (p->nargs != -1
  3074. && ((e && e->ops.length () != (unsigned)p->nargs)
  3075. || (!e && p->nargs != 0)))
  3076. fatal_at (token, "non-matching number of match operands");
  3077. p->nargs = e ? e->ops.length () : 0;
  3078. parse_simplify (token->src_loc, p->matchers, p, e);
  3079. capture_ids = NULL;
  3080. }
  3081. else if (strcmp (id, "for") == 0)
  3082. parse_for (token->src_loc);
  3083. else if (strcmp (id, "if") == 0)
  3084. parse_if (token->src_loc);
  3085. else if (strcmp (id, "define_predicates") == 0)
  3086. {
  3087. if (active_ifs.length () > 0
  3088. || active_fors.length () > 0)
  3089. fatal_at (token, "define_predicates inside if or for is not supported");
  3090. parse_predicates (token->src_loc);
  3091. }
  3092. else if (strcmp (id, "define_operator_list") == 0)
  3093. {
  3094. if (active_ifs.length () > 0
  3095. || active_fors.length () > 0)
  3096. fatal_at (token, "operator-list inside if or for is not supported");
  3097. parse_operator_list (token->src_loc);
  3098. }
  3099. else
  3100. fatal_at (token, "expected %s'simplify', 'match', 'for' or 'if'",
  3101. active_ifs.length () == 0 && active_fors.length () == 0
  3102. ? "'define_predicates', " : "");
  3103. eat_token (CPP_CLOSE_PAREN);
  3104. }
  3105. /* Main entry of the parser. Repeatedly parse outer control structures. */
  3106. parser::parser (cpp_reader *r_)
  3107. {
  3108. r = r_;
  3109. active_ifs = vNULL;
  3110. active_fors = vNULL;
  3111. simplifiers = vNULL;
  3112. oper_lists_set = NULL;
  3113. oper_lists = vNULL;
  3114. capture_ids = NULL;
  3115. user_predicates = vNULL;
  3116. parsing_match_operand = false;
  3117. const cpp_token *token = next ();
  3118. while (token->type != CPP_EOF)
  3119. {
  3120. _cpp_backup_tokens (r, 1);
  3121. parse_pattern ();
  3122. token = next ();
  3123. }
  3124. }
  3125. /* Helper for the linemap code. */
  3126. static size_t
  3127. round_alloc_size (size_t s)
  3128. {
  3129. return s;
  3130. }
  3131. /* The genmatch generator progam. It reads from a pattern description
  3132. and outputs GIMPLE or GENERIC IL matching and simplification routines. */
  3133. int
  3134. main (int argc, char **argv)
  3135. {
  3136. cpp_reader *r;
  3137. progname = "genmatch";
  3138. if (argc < 2)
  3139. return 1;
  3140. bool gimple = true;
  3141. bool verbose = false;
  3142. char *input = argv[argc-1];
  3143. for (int i = 1; i < argc - 1; ++i)
  3144. {
  3145. if (strcmp (argv[i], "--gimple") == 0)
  3146. gimple = true;
  3147. else if (strcmp (argv[i], "--generic") == 0)
  3148. gimple = false;
  3149. else if (strcmp (argv[i], "-v") == 0)
  3150. verbose = true;
  3151. else
  3152. {
  3153. fprintf (stderr, "Usage: genmatch "
  3154. "[--gimple] [--generic] [-v] input\n");
  3155. return 1;
  3156. }
  3157. }
  3158. line_table = XCNEW (struct line_maps);
  3159. linemap_init (line_table, 0);
  3160. line_table->reallocator = xrealloc;
  3161. line_table->round_alloc_size = round_alloc_size;
  3162. r = cpp_create_reader (CLK_GNUC99, NULL, line_table);
  3163. cpp_callbacks *cb = cpp_get_callbacks (r);
  3164. cb->error = error_cb;
  3165. if (!cpp_read_main_file (r, input))
  3166. return 1;
  3167. cpp_define (r, gimple ? "GIMPLE=1": "GENERIC=1");
  3168. cpp_define (r, gimple ? "GENERIC=0": "GIMPLE=0");
  3169. /* Pre-seed operators. */
  3170. operators = new hash_table<id_base> (1024);
  3171. #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
  3172. add_operator (SYM, # SYM, # TYPE, NARGS);
  3173. #define END_OF_BASE_TREE_CODES
  3174. #include "tree.def"
  3175. add_operator (CONVERT0, "CONVERT0", "tcc_unary", 1);
  3176. add_operator (CONVERT1, "CONVERT1", "tcc_unary", 1);
  3177. add_operator (CONVERT2, "CONVERT2", "tcc_unary", 1);
  3178. #undef END_OF_BASE_TREE_CODES
  3179. #undef DEFTREECODE
  3180. /* Pre-seed builtin functions.
  3181. ??? Cannot use N (name) as that is targetm.emultls.get_address
  3182. for BUILT_IN_EMUTLS_GET_ADDRESS ... */
  3183. #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
  3184. add_builtin (ENUM, # ENUM);
  3185. #include "builtins.def"
  3186. #undef DEF_BUILTIN
  3187. /* Parse ahead! */
  3188. parser p (r);
  3189. if (gimple)
  3190. write_header (stdout, "gimple-match-head.c");
  3191. else
  3192. write_header (stdout, "generic-match-head.c");
  3193. /* Go over all predicates defined with patterns and perform
  3194. lowering and code generation. */
  3195. for (unsigned i = 0; i < p.user_predicates.length (); ++i)
  3196. {
  3197. predicate_id *pred = p.user_predicates[i];
  3198. lower (pred->matchers, gimple);
  3199. if (verbose)
  3200. for (unsigned i = 0; i < pred->matchers.length (); ++i)
  3201. print_matches (pred->matchers[i]);
  3202. decision_tree dt;
  3203. for (unsigned i = 0; i < pred->matchers.length (); ++i)
  3204. dt.insert (pred->matchers[i], i);
  3205. if (verbose)
  3206. dt.print (stderr);
  3207. write_predicate (stdout, pred, dt, gimple);
  3208. }
  3209. /* Lower the main simplifiers and generate code for them. */
  3210. lower (p.simplifiers, gimple);
  3211. if (verbose)
  3212. for (unsigned i = 0; i < p.simplifiers.length (); ++i)
  3213. print_matches (p.simplifiers[i]);
  3214. decision_tree dt;
  3215. for (unsigned i = 0; i < p.simplifiers.length (); ++i)
  3216. dt.insert (p.simplifiers[i], i);
  3217. if (verbose)
  3218. dt.print (stderr);
  3219. if (gimple)
  3220. dt.gen_gimple (stdout);
  3221. else
  3222. dt.gen_generic (stdout);
  3223. /* Finalize. */
  3224. cpp_finish (r, NULL);
  3225. cpp_destroy (r);
  3226. delete operators;
  3227. return 0;
  3228. }