MIParser.cpp 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615
  1. //===- MIParser.cpp - Machine instructions parser implementation ----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the parsing of machine instructions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/MIRParser/MIParser.h"
  13. #include "MILexer.h"
  14. #include "llvm/ADT/APInt.h"
  15. #include "llvm/ADT/APSInt.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringMap.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/ADT/StringSwitch.h"
  22. #include "llvm/ADT/Twine.h"
  23. #include "llvm/Analysis/MemoryLocation.h"
  24. #include "llvm/AsmParser/Parser.h"
  25. #include "llvm/AsmParser/SlotMapping.h"
  26. #include "llvm/CodeGen/MIRFormatter.h"
  27. #include "llvm/CodeGen/MIRPrinter.h"
  28. #include "llvm/CodeGen/MachineBasicBlock.h"
  29. #include "llvm/CodeGen/MachineFrameInfo.h"
  30. #include "llvm/CodeGen/MachineFunction.h"
  31. #include "llvm/CodeGen/MachineInstr.h"
  32. #include "llvm/CodeGen/MachineInstrBuilder.h"
  33. #include "llvm/CodeGen/MachineMemOperand.h"
  34. #include "llvm/CodeGen/MachineOperand.h"
  35. #include "llvm/CodeGen/MachineRegisterInfo.h"
  36. #include "llvm/CodeGen/RegisterBank.h"
  37. #include "llvm/CodeGen/RegisterBankInfo.h"
  38. #include "llvm/CodeGen/TargetInstrInfo.h"
  39. #include "llvm/CodeGen/TargetRegisterInfo.h"
  40. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  41. #include "llvm/IR/BasicBlock.h"
  42. #include "llvm/IR/Constants.h"
  43. #include "llvm/IR/DataLayout.h"
  44. #include "llvm/IR/DebugInfoMetadata.h"
  45. #include "llvm/IR/DebugLoc.h"
  46. #include "llvm/IR/Function.h"
  47. #include "llvm/IR/InstrTypes.h"
  48. #include "llvm/IR/Instructions.h"
  49. #include "llvm/IR/Intrinsics.h"
  50. #include "llvm/IR/Metadata.h"
  51. #include "llvm/IR/Module.h"
  52. #include "llvm/IR/ModuleSlotTracker.h"
  53. #include "llvm/IR/Type.h"
  54. #include "llvm/IR/Value.h"
  55. #include "llvm/IR/ValueSymbolTable.h"
  56. #include "llvm/MC/LaneBitmask.h"
  57. #include "llvm/MC/MCContext.h"
  58. #include "llvm/MC/MCDwarf.h"
  59. #include "llvm/MC/MCInstrDesc.h"
  60. #include "llvm/Support/AtomicOrdering.h"
  61. #include "llvm/Support/BranchProbability.h"
  62. #include "llvm/Support/Casting.h"
  63. #include "llvm/Support/ErrorHandling.h"
  64. #include "llvm/Support/LowLevelTypeImpl.h"
  65. #include "llvm/Support/MemoryBuffer.h"
  66. #include "llvm/Support/SMLoc.h"
  67. #include "llvm/Support/SourceMgr.h"
  68. #include "llvm/Target/TargetIntrinsicInfo.h"
  69. #include "llvm/Target/TargetMachine.h"
  70. #include <cassert>
  71. #include <cctype>
  72. #include <cstddef>
  73. #include <cstdint>
  74. #include <limits>
  75. #include <string>
  76. #include <utility>
  77. using namespace llvm;
  78. void PerTargetMIParsingState::setTarget(
  79. const TargetSubtargetInfo &NewSubtarget) {
  80. // If the subtarget changed, over conservatively assume everything is invalid.
  81. if (&Subtarget == &NewSubtarget)
  82. return;
  83. Names2InstrOpCodes.clear();
  84. Names2Regs.clear();
  85. Names2RegMasks.clear();
  86. Names2SubRegIndices.clear();
  87. Names2TargetIndices.clear();
  88. Names2DirectTargetFlags.clear();
  89. Names2BitmaskTargetFlags.clear();
  90. Names2MMOTargetFlags.clear();
  91. initNames2RegClasses();
  92. initNames2RegBanks();
  93. }
  94. void PerTargetMIParsingState::initNames2Regs() {
  95. if (!Names2Regs.empty())
  96. return;
  97. // The '%noreg' register is the register 0.
  98. Names2Regs.insert(std::make_pair("noreg", 0));
  99. const auto *TRI = Subtarget.getRegisterInfo();
  100. assert(TRI && "Expected target register info");
  101. for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
  102. bool WasInserted =
  103. Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
  104. .second;
  105. (void)WasInserted;
  106. assert(WasInserted && "Expected registers to be unique case-insensitively");
  107. }
  108. }
  109. bool PerTargetMIParsingState::getRegisterByName(StringRef RegName,
  110. Register &Reg) {
  111. initNames2Regs();
  112. auto RegInfo = Names2Regs.find(RegName);
  113. if (RegInfo == Names2Regs.end())
  114. return true;
  115. Reg = RegInfo->getValue();
  116. return false;
  117. }
  118. void PerTargetMIParsingState::initNames2InstrOpCodes() {
  119. if (!Names2InstrOpCodes.empty())
  120. return;
  121. const auto *TII = Subtarget.getInstrInfo();
  122. assert(TII && "Expected target instruction info");
  123. for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
  124. Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
  125. }
  126. bool PerTargetMIParsingState::parseInstrName(StringRef InstrName,
  127. unsigned &OpCode) {
  128. initNames2InstrOpCodes();
  129. auto InstrInfo = Names2InstrOpCodes.find(InstrName);
  130. if (InstrInfo == Names2InstrOpCodes.end())
  131. return true;
  132. OpCode = InstrInfo->getValue();
  133. return false;
  134. }
  135. void PerTargetMIParsingState::initNames2RegMasks() {
  136. if (!Names2RegMasks.empty())
  137. return;
  138. const auto *TRI = Subtarget.getRegisterInfo();
  139. assert(TRI && "Expected target register info");
  140. ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
  141. ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
  142. assert(RegMasks.size() == RegMaskNames.size());
  143. for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
  144. Names2RegMasks.insert(
  145. std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
  146. }
  147. const uint32_t *PerTargetMIParsingState::getRegMask(StringRef Identifier) {
  148. initNames2RegMasks();
  149. auto RegMaskInfo = Names2RegMasks.find(Identifier);
  150. if (RegMaskInfo == Names2RegMasks.end())
  151. return nullptr;
  152. return RegMaskInfo->getValue();
  153. }
  154. void PerTargetMIParsingState::initNames2SubRegIndices() {
  155. if (!Names2SubRegIndices.empty())
  156. return;
  157. const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
  158. for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
  159. Names2SubRegIndices.insert(
  160. std::make_pair(TRI->getSubRegIndexName(I), I));
  161. }
  162. unsigned PerTargetMIParsingState::getSubRegIndex(StringRef Name) {
  163. initNames2SubRegIndices();
  164. auto SubRegInfo = Names2SubRegIndices.find(Name);
  165. if (SubRegInfo == Names2SubRegIndices.end())
  166. return 0;
  167. return SubRegInfo->getValue();
  168. }
  169. void PerTargetMIParsingState::initNames2TargetIndices() {
  170. if (!Names2TargetIndices.empty())
  171. return;
  172. const auto *TII = Subtarget.getInstrInfo();
  173. assert(TII && "Expected target instruction info");
  174. auto Indices = TII->getSerializableTargetIndices();
  175. for (const auto &I : Indices)
  176. Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
  177. }
  178. bool PerTargetMIParsingState::getTargetIndex(StringRef Name, int &Index) {
  179. initNames2TargetIndices();
  180. auto IndexInfo = Names2TargetIndices.find(Name);
  181. if (IndexInfo == Names2TargetIndices.end())
  182. return true;
  183. Index = IndexInfo->second;
  184. return false;
  185. }
  186. void PerTargetMIParsingState::initNames2DirectTargetFlags() {
  187. if (!Names2DirectTargetFlags.empty())
  188. return;
  189. const auto *TII = Subtarget.getInstrInfo();
  190. assert(TII && "Expected target instruction info");
  191. auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
  192. for (const auto &I : Flags)
  193. Names2DirectTargetFlags.insert(
  194. std::make_pair(StringRef(I.second), I.first));
  195. }
  196. bool PerTargetMIParsingState::getDirectTargetFlag(StringRef Name,
  197. unsigned &Flag) {
  198. initNames2DirectTargetFlags();
  199. auto FlagInfo = Names2DirectTargetFlags.find(Name);
  200. if (FlagInfo == Names2DirectTargetFlags.end())
  201. return true;
  202. Flag = FlagInfo->second;
  203. return false;
  204. }
  205. void PerTargetMIParsingState::initNames2BitmaskTargetFlags() {
  206. if (!Names2BitmaskTargetFlags.empty())
  207. return;
  208. const auto *TII = Subtarget.getInstrInfo();
  209. assert(TII && "Expected target instruction info");
  210. auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
  211. for (const auto &I : Flags)
  212. Names2BitmaskTargetFlags.insert(
  213. std::make_pair(StringRef(I.second), I.first));
  214. }
  215. bool PerTargetMIParsingState::getBitmaskTargetFlag(StringRef Name,
  216. unsigned &Flag) {
  217. initNames2BitmaskTargetFlags();
  218. auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
  219. if (FlagInfo == Names2BitmaskTargetFlags.end())
  220. return true;
  221. Flag = FlagInfo->second;
  222. return false;
  223. }
  224. void PerTargetMIParsingState::initNames2MMOTargetFlags() {
  225. if (!Names2MMOTargetFlags.empty())
  226. return;
  227. const auto *TII = Subtarget.getInstrInfo();
  228. assert(TII && "Expected target instruction info");
  229. auto Flags = TII->getSerializableMachineMemOperandTargetFlags();
  230. for (const auto &I : Flags)
  231. Names2MMOTargetFlags.insert(std::make_pair(StringRef(I.second), I.first));
  232. }
  233. bool PerTargetMIParsingState::getMMOTargetFlag(StringRef Name,
  234. MachineMemOperand::Flags &Flag) {
  235. initNames2MMOTargetFlags();
  236. auto FlagInfo = Names2MMOTargetFlags.find(Name);
  237. if (FlagInfo == Names2MMOTargetFlags.end())
  238. return true;
  239. Flag = FlagInfo->second;
  240. return false;
  241. }
  242. void PerTargetMIParsingState::initNames2RegClasses() {
  243. if (!Names2RegClasses.empty())
  244. return;
  245. const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
  246. for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
  247. const auto *RC = TRI->getRegClass(I);
  248. Names2RegClasses.insert(
  249. std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
  250. }
  251. }
  252. void PerTargetMIParsingState::initNames2RegBanks() {
  253. if (!Names2RegBanks.empty())
  254. return;
  255. const RegisterBankInfo *RBI = Subtarget.getRegBankInfo();
  256. // If the target does not support GlobalISel, we may not have a
  257. // register bank info.
  258. if (!RBI)
  259. return;
  260. for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
  261. const auto &RegBank = RBI->getRegBank(I);
  262. Names2RegBanks.insert(
  263. std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
  264. }
  265. }
  266. const TargetRegisterClass *
  267. PerTargetMIParsingState::getRegClass(StringRef Name) {
  268. auto RegClassInfo = Names2RegClasses.find(Name);
  269. if (RegClassInfo == Names2RegClasses.end())
  270. return nullptr;
  271. return RegClassInfo->getValue();
  272. }
  273. const RegisterBank *PerTargetMIParsingState::getRegBank(StringRef Name) {
  274. auto RegBankInfo = Names2RegBanks.find(Name);
  275. if (RegBankInfo == Names2RegBanks.end())
  276. return nullptr;
  277. return RegBankInfo->getValue();
  278. }
  279. PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
  280. SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &T)
  281. : MF(MF), SM(&SM), IRSlots(IRSlots), Target(T) {
  282. }
  283. VRegInfo &PerFunctionMIParsingState::getVRegInfo(Register Num) {
  284. auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
  285. if (I.second) {
  286. MachineRegisterInfo &MRI = MF.getRegInfo();
  287. VRegInfo *Info = new (Allocator) VRegInfo;
  288. Info->VReg = MRI.createIncompleteVirtualRegister();
  289. I.first->second = Info;
  290. }
  291. return *I.first->second;
  292. }
  293. VRegInfo &PerFunctionMIParsingState::getVRegInfoNamed(StringRef RegName) {
  294. assert(RegName != "" && "Expected named reg.");
  295. auto I = VRegInfosNamed.insert(std::make_pair(RegName.str(), nullptr));
  296. if (I.second) {
  297. VRegInfo *Info = new (Allocator) VRegInfo;
  298. Info->VReg = MF.getRegInfo().createIncompleteVirtualRegister(RegName);
  299. I.first->second = Info;
  300. }
  301. return *I.first->second;
  302. }
  303. static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
  304. DenseMap<unsigned, const Value *> &Slots2Values) {
  305. int Slot = MST.getLocalSlot(V);
  306. if (Slot == -1)
  307. return;
  308. Slots2Values.insert(std::make_pair(unsigned(Slot), V));
  309. }
  310. /// Creates the mapping from slot numbers to function's unnamed IR values.
  311. static void initSlots2Values(const Function &F,
  312. DenseMap<unsigned, const Value *> &Slots2Values) {
  313. ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
  314. MST.incorporateFunction(F);
  315. for (const auto &Arg : F.args())
  316. mapValueToSlot(&Arg, MST, Slots2Values);
  317. for (const auto &BB : F) {
  318. mapValueToSlot(&BB, MST, Slots2Values);
  319. for (const auto &I : BB)
  320. mapValueToSlot(&I, MST, Slots2Values);
  321. }
  322. }
  323. const Value* PerFunctionMIParsingState::getIRValue(unsigned Slot) {
  324. if (Slots2Values.empty())
  325. initSlots2Values(MF.getFunction(), Slots2Values);
  326. return Slots2Values.lookup(Slot);
  327. }
  328. namespace {
  329. /// A wrapper struct around the 'MachineOperand' struct that includes a source
  330. /// range and other attributes.
  331. struct ParsedMachineOperand {
  332. MachineOperand Operand;
  333. StringRef::iterator Begin;
  334. StringRef::iterator End;
  335. std::optional<unsigned> TiedDefIdx;
  336. ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
  337. StringRef::iterator End,
  338. std::optional<unsigned> &TiedDefIdx)
  339. : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
  340. if (TiedDefIdx)
  341. assert(Operand.isReg() && Operand.isUse() &&
  342. "Only used register operands can be tied");
  343. }
  344. };
  345. class MIParser {
  346. MachineFunction &MF;
  347. SMDiagnostic &Error;
  348. StringRef Source, CurrentSource;
  349. SMRange SourceRange;
  350. MIToken Token;
  351. PerFunctionMIParsingState &PFS;
  352. /// Maps from slot numbers to function's unnamed basic blocks.
  353. DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
  354. public:
  355. MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
  356. StringRef Source);
  357. MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
  358. StringRef Source, SMRange SourceRange);
  359. /// \p SkipChar gives the number of characters to skip before looking
  360. /// for the next token.
  361. void lex(unsigned SkipChar = 0);
  362. /// Report an error at the current location with the given message.
  363. ///
  364. /// This function always return true.
  365. bool error(const Twine &Msg);
  366. /// Report an error at the given location with the given message.
  367. ///
  368. /// This function always return true.
  369. bool error(StringRef::iterator Loc, const Twine &Msg);
  370. bool
  371. parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
  372. bool parseBasicBlocks();
  373. bool parse(MachineInstr *&MI);
  374. bool parseStandaloneMBB(MachineBasicBlock *&MBB);
  375. bool parseStandaloneNamedRegister(Register &Reg);
  376. bool parseStandaloneVirtualRegister(VRegInfo *&Info);
  377. bool parseStandaloneRegister(Register &Reg);
  378. bool parseStandaloneStackObject(int &FI);
  379. bool parseStandaloneMDNode(MDNode *&Node);
  380. bool parseMachineMetadata();
  381. bool parseMDTuple(MDNode *&MD, bool IsDistinct);
  382. bool parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts);
  383. bool parseMetadata(Metadata *&MD);
  384. bool
  385. parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
  386. bool parseBasicBlock(MachineBasicBlock &MBB,
  387. MachineBasicBlock *&AddFalthroughFrom);
  388. bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
  389. bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
  390. bool parseNamedRegister(Register &Reg);
  391. bool parseVirtualRegister(VRegInfo *&Info);
  392. bool parseNamedVirtualRegister(VRegInfo *&Info);
  393. bool parseRegister(Register &Reg, VRegInfo *&VRegInfo);
  394. bool parseRegisterFlag(unsigned &Flags);
  395. bool parseRegisterClassOrBank(VRegInfo &RegInfo);
  396. bool parseSubRegisterIndex(unsigned &SubReg);
  397. bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
  398. bool parseRegisterOperand(MachineOperand &Dest,
  399. std::optional<unsigned> &TiedDefIdx,
  400. bool IsDef = false);
  401. bool parseImmediateOperand(MachineOperand &Dest);
  402. bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
  403. const Constant *&C);
  404. bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
  405. bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
  406. bool parseTypedImmediateOperand(MachineOperand &Dest);
  407. bool parseFPImmediateOperand(MachineOperand &Dest);
  408. bool parseMBBReference(MachineBasicBlock *&MBB);
  409. bool parseMBBOperand(MachineOperand &Dest);
  410. bool parseStackFrameIndex(int &FI);
  411. bool parseStackObjectOperand(MachineOperand &Dest);
  412. bool parseFixedStackFrameIndex(int &FI);
  413. bool parseFixedStackObjectOperand(MachineOperand &Dest);
  414. bool parseGlobalValue(GlobalValue *&GV);
  415. bool parseGlobalAddressOperand(MachineOperand &Dest);
  416. bool parseConstantPoolIndexOperand(MachineOperand &Dest);
  417. bool parseSubRegisterIndexOperand(MachineOperand &Dest);
  418. bool parseJumpTableIndexOperand(MachineOperand &Dest);
  419. bool parseExternalSymbolOperand(MachineOperand &Dest);
  420. bool parseMCSymbolOperand(MachineOperand &Dest);
  421. bool parseMDNode(MDNode *&Node);
  422. bool parseDIExpression(MDNode *&Expr);
  423. bool parseDILocation(MDNode *&Expr);
  424. bool parseMetadataOperand(MachineOperand &Dest);
  425. bool parseCFIOffset(int &Offset);
  426. bool parseCFIRegister(Register &Reg);
  427. bool parseCFIAddressSpace(unsigned &AddressSpace);
  428. bool parseCFIEscapeValues(std::string& Values);
  429. bool parseCFIOperand(MachineOperand &Dest);
  430. bool parseIRBlock(BasicBlock *&BB, const Function &F);
  431. bool parseBlockAddressOperand(MachineOperand &Dest);
  432. bool parseIntrinsicOperand(MachineOperand &Dest);
  433. bool parsePredicateOperand(MachineOperand &Dest);
  434. bool parseShuffleMaskOperand(MachineOperand &Dest);
  435. bool parseTargetIndexOperand(MachineOperand &Dest);
  436. bool parseDbgInstrRefOperand(MachineOperand &Dest);
  437. bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
  438. bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
  439. bool parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
  440. MachineOperand &Dest,
  441. std::optional<unsigned> &TiedDefIdx);
  442. bool parseMachineOperandAndTargetFlags(const unsigned OpCode,
  443. const unsigned OpIdx,
  444. MachineOperand &Dest,
  445. std::optional<unsigned> &TiedDefIdx);
  446. bool parseOffset(int64_t &Offset);
  447. bool parseIRBlockAddressTaken(BasicBlock *&BB);
  448. bool parseAlignment(uint64_t &Alignment);
  449. bool parseAddrspace(unsigned &Addrspace);
  450. bool parseSectionID(std::optional<MBBSectionID> &SID);
  451. bool parseBBID(std::optional<unsigned> &BBID);
  452. bool parseOperandsOffset(MachineOperand &Op);
  453. bool parseIRValue(const Value *&V);
  454. bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
  455. bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
  456. bool parseMachinePointerInfo(MachinePointerInfo &Dest);
  457. bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID);
  458. bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
  459. bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
  460. bool parsePreOrPostInstrSymbol(MCSymbol *&Symbol);
  461. bool parseHeapAllocMarker(MDNode *&Node);
  462. bool parsePCSections(MDNode *&Node);
  463. bool parseTargetImmMnemonic(const unsigned OpCode, const unsigned OpIdx,
  464. MachineOperand &Dest, const MIRFormatter &MF);
  465. private:
  466. /// Convert the integer literal in the current token into an unsigned integer.
  467. ///
  468. /// Return true if an error occurred.
  469. bool getUnsigned(unsigned &Result);
  470. /// Convert the integer literal in the current token into an uint64.
  471. ///
  472. /// Return true if an error occurred.
  473. bool getUint64(uint64_t &Result);
  474. /// Convert the hexadecimal literal in the current token into an unsigned
  475. /// APInt with a minimum bitwidth required to represent the value.
  476. ///
  477. /// Return true if the literal does not represent an integer value.
  478. bool getHexUint(APInt &Result);
  479. /// If the current token is of the given kind, consume it and return false.
  480. /// Otherwise report an error and return true.
  481. bool expectAndConsume(MIToken::TokenKind TokenKind);
  482. /// If the current token is of the given kind, consume it and return true.
  483. /// Otherwise return false.
  484. bool consumeIfPresent(MIToken::TokenKind TokenKind);
  485. bool parseInstruction(unsigned &OpCode, unsigned &Flags);
  486. bool assignRegisterTies(MachineInstr &MI,
  487. ArrayRef<ParsedMachineOperand> Operands);
  488. bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
  489. const MCInstrDesc &MCID);
  490. const BasicBlock *getIRBlock(unsigned Slot);
  491. const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
  492. /// Get or create an MCSymbol for a given name.
  493. MCSymbol *getOrCreateMCSymbol(StringRef Name);
  494. /// parseStringConstant
  495. /// ::= StringConstant
  496. bool parseStringConstant(std::string &Result);
  497. /// Map the location in the MI string to the corresponding location specified
  498. /// in `SourceRange`.
  499. SMLoc mapSMLoc(StringRef::iterator Loc);
  500. };
  501. } // end anonymous namespace
  502. MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
  503. StringRef Source)
  504. : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
  505. {}
  506. MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
  507. StringRef Source, SMRange SourceRange)
  508. : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source),
  509. SourceRange(SourceRange), PFS(PFS) {}
  510. void MIParser::lex(unsigned SkipChar) {
  511. CurrentSource = lexMIToken(
  512. CurrentSource.slice(SkipChar, StringRef::npos), Token,
  513. [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
  514. }
  515. bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
  516. bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
  517. const SourceMgr &SM = *PFS.SM;
  518. assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
  519. const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
  520. if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
  521. // Create an ordinary diagnostic when the source manager's buffer is the
  522. // source string.
  523. Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
  524. return true;
  525. }
  526. // Create a diagnostic for a YAML string literal.
  527. Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
  528. Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
  529. Source, std::nullopt, std::nullopt);
  530. return true;
  531. }
  532. SMLoc MIParser::mapSMLoc(StringRef::iterator Loc) {
  533. assert(SourceRange.isValid() && "Invalid source range");
  534. assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
  535. return SMLoc::getFromPointer(SourceRange.Start.getPointer() +
  536. (Loc - Source.data()));
  537. }
  538. typedef function_ref<bool(StringRef::iterator Loc, const Twine &)>
  539. ErrorCallbackType;
  540. static const char *toString(MIToken::TokenKind TokenKind) {
  541. switch (TokenKind) {
  542. case MIToken::comma:
  543. return "','";
  544. case MIToken::equal:
  545. return "'='";
  546. case MIToken::colon:
  547. return "':'";
  548. case MIToken::lparen:
  549. return "'('";
  550. case MIToken::rparen:
  551. return "')'";
  552. default:
  553. return "<unknown token>";
  554. }
  555. }
  556. bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
  557. if (Token.isNot(TokenKind))
  558. return error(Twine("expected ") + toString(TokenKind));
  559. lex();
  560. return false;
  561. }
  562. bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
  563. if (Token.isNot(TokenKind))
  564. return false;
  565. lex();
  566. return true;
  567. }
  568. // Parse Machine Basic Block Section ID.
  569. bool MIParser::parseSectionID(std::optional<MBBSectionID> &SID) {
  570. assert(Token.is(MIToken::kw_bbsections));
  571. lex();
  572. if (Token.is(MIToken::IntegerLiteral)) {
  573. unsigned Value = 0;
  574. if (getUnsigned(Value))
  575. return error("Unknown Section ID");
  576. SID = MBBSectionID{Value};
  577. } else {
  578. const StringRef &S = Token.stringValue();
  579. if (S == "Exception")
  580. SID = MBBSectionID::ExceptionSectionID;
  581. else if (S == "Cold")
  582. SID = MBBSectionID::ColdSectionID;
  583. else
  584. return error("Unknown Section ID");
  585. }
  586. lex();
  587. return false;
  588. }
  589. // Parse Machine Basic Block ID.
  590. bool MIParser::parseBBID(std::optional<unsigned> &BBID) {
  591. assert(Token.is(MIToken::kw_bb_id));
  592. lex();
  593. unsigned Value = 0;
  594. if (getUnsigned(Value))
  595. return error("Unknown BB ID");
  596. BBID = Value;
  597. lex();
  598. return false;
  599. }
  600. bool MIParser::parseBasicBlockDefinition(
  601. DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
  602. assert(Token.is(MIToken::MachineBasicBlockLabel));
  603. unsigned ID = 0;
  604. if (getUnsigned(ID))
  605. return true;
  606. auto Loc = Token.location();
  607. auto Name = Token.stringValue();
  608. lex();
  609. bool MachineBlockAddressTaken = false;
  610. BasicBlock *AddressTakenIRBlock = nullptr;
  611. bool IsLandingPad = false;
  612. bool IsInlineAsmBrIndirectTarget = false;
  613. bool IsEHFuncletEntry = false;
  614. std::optional<MBBSectionID> SectionID;
  615. uint64_t Alignment = 0;
  616. std::optional<unsigned> BBID;
  617. BasicBlock *BB = nullptr;
  618. if (consumeIfPresent(MIToken::lparen)) {
  619. do {
  620. // TODO: Report an error when multiple same attributes are specified.
  621. switch (Token.kind()) {
  622. case MIToken::kw_machine_block_address_taken:
  623. MachineBlockAddressTaken = true;
  624. lex();
  625. break;
  626. case MIToken::kw_ir_block_address_taken:
  627. if (parseIRBlockAddressTaken(AddressTakenIRBlock))
  628. return true;
  629. break;
  630. case MIToken::kw_landing_pad:
  631. IsLandingPad = true;
  632. lex();
  633. break;
  634. case MIToken::kw_inlineasm_br_indirect_target:
  635. IsInlineAsmBrIndirectTarget = true;
  636. lex();
  637. break;
  638. case MIToken::kw_ehfunclet_entry:
  639. IsEHFuncletEntry = true;
  640. lex();
  641. break;
  642. case MIToken::kw_align:
  643. if (parseAlignment(Alignment))
  644. return true;
  645. break;
  646. case MIToken::IRBlock:
  647. case MIToken::NamedIRBlock:
  648. // TODO: Report an error when both name and ir block are specified.
  649. if (parseIRBlock(BB, MF.getFunction()))
  650. return true;
  651. lex();
  652. break;
  653. case MIToken::kw_bbsections:
  654. if (parseSectionID(SectionID))
  655. return true;
  656. break;
  657. case MIToken::kw_bb_id:
  658. if (parseBBID(BBID))
  659. return true;
  660. break;
  661. default:
  662. break;
  663. }
  664. } while (consumeIfPresent(MIToken::comma));
  665. if (expectAndConsume(MIToken::rparen))
  666. return true;
  667. }
  668. if (expectAndConsume(MIToken::colon))
  669. return true;
  670. if (!Name.empty()) {
  671. BB = dyn_cast_or_null<BasicBlock>(
  672. MF.getFunction().getValueSymbolTable()->lookup(Name));
  673. if (!BB)
  674. return error(Loc, Twine("basic block '") + Name +
  675. "' is not defined in the function '" +
  676. MF.getName() + "'");
  677. }
  678. auto *MBB = MF.CreateMachineBasicBlock(BB);
  679. MF.insert(MF.end(), MBB);
  680. bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
  681. if (!WasInserted)
  682. return error(Loc, Twine("redefinition of machine basic block with id #") +
  683. Twine(ID));
  684. if (Alignment)
  685. MBB->setAlignment(Align(Alignment));
  686. if (MachineBlockAddressTaken)
  687. MBB->setMachineBlockAddressTaken();
  688. if (AddressTakenIRBlock)
  689. MBB->setAddressTakenIRBlock(AddressTakenIRBlock);
  690. MBB->setIsEHPad(IsLandingPad);
  691. MBB->setIsInlineAsmBrIndirectTarget(IsInlineAsmBrIndirectTarget);
  692. MBB->setIsEHFuncletEntry(IsEHFuncletEntry);
  693. if (SectionID) {
  694. MBB->setSectionID(*SectionID);
  695. MF.setBBSectionsType(BasicBlockSection::List);
  696. }
  697. if (BBID.has_value()) {
  698. // BBSectionsType is set to `List` if any basic blocks has `SectionID`.
  699. // Here, we set it to `Labels` if it hasn't been set above.
  700. if (!MF.hasBBSections())
  701. MF.setBBSectionsType(BasicBlockSection::Labels);
  702. MBB->setBBID(BBID.value());
  703. }
  704. return false;
  705. }
  706. bool MIParser::parseBasicBlockDefinitions(
  707. DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
  708. lex();
  709. // Skip until the first machine basic block.
  710. while (Token.is(MIToken::Newline))
  711. lex();
  712. if (Token.isErrorOrEOF())
  713. return Token.isError();
  714. if (Token.isNot(MIToken::MachineBasicBlockLabel))
  715. return error("expected a basic block definition before instructions");
  716. unsigned BraceDepth = 0;
  717. do {
  718. if (parseBasicBlockDefinition(MBBSlots))
  719. return true;
  720. bool IsAfterNewline = false;
  721. // Skip until the next machine basic block.
  722. while (true) {
  723. if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
  724. Token.isErrorOrEOF())
  725. break;
  726. else if (Token.is(MIToken::MachineBasicBlockLabel))
  727. return error("basic block definition should be located at the start of "
  728. "the line");
  729. else if (consumeIfPresent(MIToken::Newline)) {
  730. IsAfterNewline = true;
  731. continue;
  732. }
  733. IsAfterNewline = false;
  734. if (Token.is(MIToken::lbrace))
  735. ++BraceDepth;
  736. if (Token.is(MIToken::rbrace)) {
  737. if (!BraceDepth)
  738. return error("extraneous closing brace ('}')");
  739. --BraceDepth;
  740. }
  741. lex();
  742. }
  743. // Verify that we closed all of the '{' at the end of a file or a block.
  744. if (!Token.isError() && BraceDepth)
  745. return error("expected '}'"); // FIXME: Report a note that shows '{'.
  746. } while (!Token.isErrorOrEOF());
  747. return Token.isError();
  748. }
  749. bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
  750. assert(Token.is(MIToken::kw_liveins));
  751. lex();
  752. if (expectAndConsume(MIToken::colon))
  753. return true;
  754. if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
  755. return false;
  756. do {
  757. if (Token.isNot(MIToken::NamedRegister))
  758. return error("expected a named register");
  759. Register Reg;
  760. if (parseNamedRegister(Reg))
  761. return true;
  762. lex();
  763. LaneBitmask Mask = LaneBitmask::getAll();
  764. if (consumeIfPresent(MIToken::colon)) {
  765. // Parse lane mask.
  766. if (Token.isNot(MIToken::IntegerLiteral) &&
  767. Token.isNot(MIToken::HexLiteral))
  768. return error("expected a lane mask");
  769. static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
  770. "Use correct get-function for lane mask");
  771. LaneBitmask::Type V;
  772. if (getUint64(V))
  773. return error("invalid lane mask value");
  774. Mask = LaneBitmask(V);
  775. lex();
  776. }
  777. MBB.addLiveIn(Reg, Mask);
  778. } while (consumeIfPresent(MIToken::comma));
  779. return false;
  780. }
  781. bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
  782. assert(Token.is(MIToken::kw_successors));
  783. lex();
  784. if (expectAndConsume(MIToken::colon))
  785. return true;
  786. if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
  787. return false;
  788. do {
  789. if (Token.isNot(MIToken::MachineBasicBlock))
  790. return error("expected a machine basic block reference");
  791. MachineBasicBlock *SuccMBB = nullptr;
  792. if (parseMBBReference(SuccMBB))
  793. return true;
  794. lex();
  795. unsigned Weight = 0;
  796. if (consumeIfPresent(MIToken::lparen)) {
  797. if (Token.isNot(MIToken::IntegerLiteral) &&
  798. Token.isNot(MIToken::HexLiteral))
  799. return error("expected an integer literal after '('");
  800. if (getUnsigned(Weight))
  801. return true;
  802. lex();
  803. if (expectAndConsume(MIToken::rparen))
  804. return true;
  805. }
  806. MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
  807. } while (consumeIfPresent(MIToken::comma));
  808. MBB.normalizeSuccProbs();
  809. return false;
  810. }
  811. bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
  812. MachineBasicBlock *&AddFalthroughFrom) {
  813. // Skip the definition.
  814. assert(Token.is(MIToken::MachineBasicBlockLabel));
  815. lex();
  816. if (consumeIfPresent(MIToken::lparen)) {
  817. while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
  818. lex();
  819. consumeIfPresent(MIToken::rparen);
  820. }
  821. consumeIfPresent(MIToken::colon);
  822. // Parse the liveins and successors.
  823. // N.B: Multiple lists of successors and liveins are allowed and they're
  824. // merged into one.
  825. // Example:
  826. // liveins: $edi
  827. // liveins: $esi
  828. //
  829. // is equivalent to
  830. // liveins: $edi, $esi
  831. bool ExplicitSuccessors = false;
  832. while (true) {
  833. if (Token.is(MIToken::kw_successors)) {
  834. if (parseBasicBlockSuccessors(MBB))
  835. return true;
  836. ExplicitSuccessors = true;
  837. } else if (Token.is(MIToken::kw_liveins)) {
  838. if (parseBasicBlockLiveins(MBB))
  839. return true;
  840. } else if (consumeIfPresent(MIToken::Newline)) {
  841. continue;
  842. } else
  843. break;
  844. if (!Token.isNewlineOrEOF())
  845. return error("expected line break at the end of a list");
  846. lex();
  847. }
  848. // Parse the instructions.
  849. bool IsInBundle = false;
  850. MachineInstr *PrevMI = nullptr;
  851. while (!Token.is(MIToken::MachineBasicBlockLabel) &&
  852. !Token.is(MIToken::Eof)) {
  853. if (consumeIfPresent(MIToken::Newline))
  854. continue;
  855. if (consumeIfPresent(MIToken::rbrace)) {
  856. // The first parsing pass should verify that all closing '}' have an
  857. // opening '{'.
  858. assert(IsInBundle);
  859. IsInBundle = false;
  860. continue;
  861. }
  862. MachineInstr *MI = nullptr;
  863. if (parse(MI))
  864. return true;
  865. MBB.insert(MBB.end(), MI);
  866. if (IsInBundle) {
  867. PrevMI->setFlag(MachineInstr::BundledSucc);
  868. MI->setFlag(MachineInstr::BundledPred);
  869. }
  870. PrevMI = MI;
  871. if (Token.is(MIToken::lbrace)) {
  872. if (IsInBundle)
  873. return error("nested instruction bundles are not allowed");
  874. lex();
  875. // This instruction is the start of the bundle.
  876. MI->setFlag(MachineInstr::BundledSucc);
  877. IsInBundle = true;
  878. if (!Token.is(MIToken::Newline))
  879. // The next instruction can be on the same line.
  880. continue;
  881. }
  882. assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
  883. lex();
  884. }
  885. // Construct successor list by searching for basic block machine operands.
  886. if (!ExplicitSuccessors) {
  887. SmallVector<MachineBasicBlock*,4> Successors;
  888. bool IsFallthrough;
  889. guessSuccessors(MBB, Successors, IsFallthrough);
  890. for (MachineBasicBlock *Succ : Successors)
  891. MBB.addSuccessor(Succ);
  892. if (IsFallthrough) {
  893. AddFalthroughFrom = &MBB;
  894. } else {
  895. MBB.normalizeSuccProbs();
  896. }
  897. }
  898. return false;
  899. }
  900. bool MIParser::parseBasicBlocks() {
  901. lex();
  902. // Skip until the first machine basic block.
  903. while (Token.is(MIToken::Newline))
  904. lex();
  905. if (Token.isErrorOrEOF())
  906. return Token.isError();
  907. // The first parsing pass should have verified that this token is a MBB label
  908. // in the 'parseBasicBlockDefinitions' method.
  909. assert(Token.is(MIToken::MachineBasicBlockLabel));
  910. MachineBasicBlock *AddFalthroughFrom = nullptr;
  911. do {
  912. MachineBasicBlock *MBB = nullptr;
  913. if (parseMBBReference(MBB))
  914. return true;
  915. if (AddFalthroughFrom) {
  916. if (!AddFalthroughFrom->isSuccessor(MBB))
  917. AddFalthroughFrom->addSuccessor(MBB);
  918. AddFalthroughFrom->normalizeSuccProbs();
  919. AddFalthroughFrom = nullptr;
  920. }
  921. if (parseBasicBlock(*MBB, AddFalthroughFrom))
  922. return true;
  923. // The method 'parseBasicBlock' should parse the whole block until the next
  924. // block or the end of file.
  925. assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
  926. } while (Token.isNot(MIToken::Eof));
  927. return false;
  928. }
  929. bool MIParser::parse(MachineInstr *&MI) {
  930. // Parse any register operands before '='
  931. MachineOperand MO = MachineOperand::CreateImm(0);
  932. SmallVector<ParsedMachineOperand, 8> Operands;
  933. while (Token.isRegister() || Token.isRegisterFlag()) {
  934. auto Loc = Token.location();
  935. std::optional<unsigned> TiedDefIdx;
  936. if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
  937. return true;
  938. Operands.push_back(
  939. ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
  940. if (Token.isNot(MIToken::comma))
  941. break;
  942. lex();
  943. }
  944. if (!Operands.empty() && expectAndConsume(MIToken::equal))
  945. return true;
  946. unsigned OpCode, Flags = 0;
  947. if (Token.isError() || parseInstruction(OpCode, Flags))
  948. return true;
  949. // Parse the remaining machine operands.
  950. while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_pre_instr_symbol) &&
  951. Token.isNot(MIToken::kw_post_instr_symbol) &&
  952. Token.isNot(MIToken::kw_heap_alloc_marker) &&
  953. Token.isNot(MIToken::kw_pcsections) &&
  954. Token.isNot(MIToken::kw_cfi_type) &&
  955. Token.isNot(MIToken::kw_debug_location) &&
  956. Token.isNot(MIToken::kw_debug_instr_number) &&
  957. Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
  958. auto Loc = Token.location();
  959. std::optional<unsigned> TiedDefIdx;
  960. if (parseMachineOperandAndTargetFlags(OpCode, Operands.size(), MO, TiedDefIdx))
  961. return true;
  962. Operands.push_back(
  963. ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
  964. if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
  965. Token.is(MIToken::lbrace))
  966. break;
  967. if (Token.isNot(MIToken::comma))
  968. return error("expected ',' before the next machine operand");
  969. lex();
  970. }
  971. MCSymbol *PreInstrSymbol = nullptr;
  972. if (Token.is(MIToken::kw_pre_instr_symbol))
  973. if (parsePreOrPostInstrSymbol(PreInstrSymbol))
  974. return true;
  975. MCSymbol *PostInstrSymbol = nullptr;
  976. if (Token.is(MIToken::kw_post_instr_symbol))
  977. if (parsePreOrPostInstrSymbol(PostInstrSymbol))
  978. return true;
  979. MDNode *HeapAllocMarker = nullptr;
  980. if (Token.is(MIToken::kw_heap_alloc_marker))
  981. if (parseHeapAllocMarker(HeapAllocMarker))
  982. return true;
  983. MDNode *PCSections = nullptr;
  984. if (Token.is(MIToken::kw_pcsections))
  985. if (parsePCSections(PCSections))
  986. return true;
  987. unsigned CFIType = 0;
  988. if (Token.is(MIToken::kw_cfi_type)) {
  989. lex();
  990. if (Token.isNot(MIToken::IntegerLiteral))
  991. return error("expected an integer literal after 'cfi-type'");
  992. // getUnsigned is sufficient for 32-bit integers.
  993. if (getUnsigned(CFIType))
  994. return true;
  995. lex();
  996. // Lex past trailing comma if present.
  997. if (Token.is(MIToken::comma))
  998. lex();
  999. }
  1000. unsigned InstrNum = 0;
  1001. if (Token.is(MIToken::kw_debug_instr_number)) {
  1002. lex();
  1003. if (Token.isNot(MIToken::IntegerLiteral))
  1004. return error("expected an integer literal after 'debug-instr-number'");
  1005. if (getUnsigned(InstrNum))
  1006. return true;
  1007. lex();
  1008. // Lex past trailing comma if present.
  1009. if (Token.is(MIToken::comma))
  1010. lex();
  1011. }
  1012. DebugLoc DebugLocation;
  1013. if (Token.is(MIToken::kw_debug_location)) {
  1014. lex();
  1015. MDNode *Node = nullptr;
  1016. if (Token.is(MIToken::exclaim)) {
  1017. if (parseMDNode(Node))
  1018. return true;
  1019. } else if (Token.is(MIToken::md_dilocation)) {
  1020. if (parseDILocation(Node))
  1021. return true;
  1022. } else
  1023. return error("expected a metadata node after 'debug-location'");
  1024. if (!isa<DILocation>(Node))
  1025. return error("referenced metadata is not a DILocation");
  1026. DebugLocation = DebugLoc(Node);
  1027. }
  1028. // Parse the machine memory operands.
  1029. SmallVector<MachineMemOperand *, 2> MemOperands;
  1030. if (Token.is(MIToken::coloncolon)) {
  1031. lex();
  1032. while (!Token.isNewlineOrEOF()) {
  1033. MachineMemOperand *MemOp = nullptr;
  1034. if (parseMachineMemoryOperand(MemOp))
  1035. return true;
  1036. MemOperands.push_back(MemOp);
  1037. if (Token.isNewlineOrEOF())
  1038. break;
  1039. if (Token.isNot(MIToken::comma))
  1040. return error("expected ',' before the next machine memory operand");
  1041. lex();
  1042. }
  1043. }
  1044. const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
  1045. if (!MCID.isVariadic()) {
  1046. // FIXME: Move the implicit operand verification to the machine verifier.
  1047. if (verifyImplicitOperands(Operands, MCID))
  1048. return true;
  1049. }
  1050. MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
  1051. MI->setFlags(Flags);
  1052. unsigned NumExplicitOps = 0;
  1053. for (const auto &Operand : Operands) {
  1054. bool IsImplicitOp = Operand.Operand.isReg() && Operand.Operand.isImplicit();
  1055. if (!IsImplicitOp) {
  1056. if (!MCID.isVariadic() && NumExplicitOps >= MCID.getNumOperands() &&
  1057. !Operand.Operand.isValidExcessOperand())
  1058. return error(Operand.Begin, "too many operands for instruction");
  1059. ++NumExplicitOps;
  1060. }
  1061. MI->addOperand(MF, Operand.Operand);
  1062. }
  1063. if (assignRegisterTies(*MI, Operands))
  1064. return true;
  1065. if (PreInstrSymbol)
  1066. MI->setPreInstrSymbol(MF, PreInstrSymbol);
  1067. if (PostInstrSymbol)
  1068. MI->setPostInstrSymbol(MF, PostInstrSymbol);
  1069. if (HeapAllocMarker)
  1070. MI->setHeapAllocMarker(MF, HeapAllocMarker);
  1071. if (PCSections)
  1072. MI->setPCSections(MF, PCSections);
  1073. if (CFIType)
  1074. MI->setCFIType(MF, CFIType);
  1075. if (!MemOperands.empty())
  1076. MI->setMemRefs(MF, MemOperands);
  1077. if (InstrNum)
  1078. MI->setDebugInstrNum(InstrNum);
  1079. return false;
  1080. }
  1081. bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
  1082. lex();
  1083. if (Token.isNot(MIToken::MachineBasicBlock))
  1084. return error("expected a machine basic block reference");
  1085. if (parseMBBReference(MBB))
  1086. return true;
  1087. lex();
  1088. if (Token.isNot(MIToken::Eof))
  1089. return error(
  1090. "expected end of string after the machine basic block reference");
  1091. return false;
  1092. }
  1093. bool MIParser::parseStandaloneNamedRegister(Register &Reg) {
  1094. lex();
  1095. if (Token.isNot(MIToken::NamedRegister))
  1096. return error("expected a named register");
  1097. if (parseNamedRegister(Reg))
  1098. return true;
  1099. lex();
  1100. if (Token.isNot(MIToken::Eof))
  1101. return error("expected end of string after the register reference");
  1102. return false;
  1103. }
  1104. bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
  1105. lex();
  1106. if (Token.isNot(MIToken::VirtualRegister))
  1107. return error("expected a virtual register");
  1108. if (parseVirtualRegister(Info))
  1109. return true;
  1110. lex();
  1111. if (Token.isNot(MIToken::Eof))
  1112. return error("expected end of string after the register reference");
  1113. return false;
  1114. }
  1115. bool MIParser::parseStandaloneRegister(Register &Reg) {
  1116. lex();
  1117. if (Token.isNot(MIToken::NamedRegister) &&
  1118. Token.isNot(MIToken::VirtualRegister))
  1119. return error("expected either a named or virtual register");
  1120. VRegInfo *Info;
  1121. if (parseRegister(Reg, Info))
  1122. return true;
  1123. lex();
  1124. if (Token.isNot(MIToken::Eof))
  1125. return error("expected end of string after the register reference");
  1126. return false;
  1127. }
  1128. bool MIParser::parseStandaloneStackObject(int &FI) {
  1129. lex();
  1130. if (Token.isNot(MIToken::StackObject))
  1131. return error("expected a stack object");
  1132. if (parseStackFrameIndex(FI))
  1133. return true;
  1134. if (Token.isNot(MIToken::Eof))
  1135. return error("expected end of string after the stack object reference");
  1136. return false;
  1137. }
  1138. bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
  1139. lex();
  1140. if (Token.is(MIToken::exclaim)) {
  1141. if (parseMDNode(Node))
  1142. return true;
  1143. } else if (Token.is(MIToken::md_diexpr)) {
  1144. if (parseDIExpression(Node))
  1145. return true;
  1146. } else if (Token.is(MIToken::md_dilocation)) {
  1147. if (parseDILocation(Node))
  1148. return true;
  1149. } else
  1150. return error("expected a metadata node");
  1151. if (Token.isNot(MIToken::Eof))
  1152. return error("expected end of string after the metadata node");
  1153. return false;
  1154. }
  1155. bool MIParser::parseMachineMetadata() {
  1156. lex();
  1157. if (Token.isNot(MIToken::exclaim))
  1158. return error("expected a metadata node");
  1159. lex();
  1160. if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
  1161. return error("expected metadata id after '!'");
  1162. unsigned ID = 0;
  1163. if (getUnsigned(ID))
  1164. return true;
  1165. lex();
  1166. if (expectAndConsume(MIToken::equal))
  1167. return true;
  1168. bool IsDistinct = Token.is(MIToken::kw_distinct);
  1169. if (IsDistinct)
  1170. lex();
  1171. if (Token.isNot(MIToken::exclaim))
  1172. return error("expected a metadata node");
  1173. lex();
  1174. MDNode *MD;
  1175. if (parseMDTuple(MD, IsDistinct))
  1176. return true;
  1177. auto FI = PFS.MachineForwardRefMDNodes.find(ID);
  1178. if (FI != PFS.MachineForwardRefMDNodes.end()) {
  1179. FI->second.first->replaceAllUsesWith(MD);
  1180. PFS.MachineForwardRefMDNodes.erase(FI);
  1181. assert(PFS.MachineMetadataNodes[ID] == MD && "Tracking VH didn't work");
  1182. } else {
  1183. if (PFS.MachineMetadataNodes.count(ID))
  1184. return error("Metadata id is already used");
  1185. PFS.MachineMetadataNodes[ID].reset(MD);
  1186. }
  1187. return false;
  1188. }
  1189. bool MIParser::parseMDTuple(MDNode *&MD, bool IsDistinct) {
  1190. SmallVector<Metadata *, 16> Elts;
  1191. if (parseMDNodeVector(Elts))
  1192. return true;
  1193. MD = (IsDistinct ? MDTuple::getDistinct
  1194. : MDTuple::get)(MF.getFunction().getContext(), Elts);
  1195. return false;
  1196. }
  1197. bool MIParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
  1198. if (Token.isNot(MIToken::lbrace))
  1199. return error("expected '{' here");
  1200. lex();
  1201. if (Token.is(MIToken::rbrace)) {
  1202. lex();
  1203. return false;
  1204. }
  1205. do {
  1206. Metadata *MD;
  1207. if (parseMetadata(MD))
  1208. return true;
  1209. Elts.push_back(MD);
  1210. if (Token.isNot(MIToken::comma))
  1211. break;
  1212. lex();
  1213. } while (true);
  1214. if (Token.isNot(MIToken::rbrace))
  1215. return error("expected end of metadata node");
  1216. lex();
  1217. return false;
  1218. }
  1219. // ::= !42
  1220. // ::= !"string"
  1221. bool MIParser::parseMetadata(Metadata *&MD) {
  1222. if (Token.isNot(MIToken::exclaim))
  1223. return error("expected '!' here");
  1224. lex();
  1225. if (Token.is(MIToken::StringConstant)) {
  1226. std::string Str;
  1227. if (parseStringConstant(Str))
  1228. return true;
  1229. MD = MDString::get(MF.getFunction().getContext(), Str);
  1230. return false;
  1231. }
  1232. if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
  1233. return error("expected metadata id after '!'");
  1234. SMLoc Loc = mapSMLoc(Token.location());
  1235. unsigned ID = 0;
  1236. if (getUnsigned(ID))
  1237. return true;
  1238. lex();
  1239. auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
  1240. if (NodeInfo != PFS.IRSlots.MetadataNodes.end()) {
  1241. MD = NodeInfo->second.get();
  1242. return false;
  1243. }
  1244. // Check machine metadata.
  1245. NodeInfo = PFS.MachineMetadataNodes.find(ID);
  1246. if (NodeInfo != PFS.MachineMetadataNodes.end()) {
  1247. MD = NodeInfo->second.get();
  1248. return false;
  1249. }
  1250. // Forward reference.
  1251. auto &FwdRef = PFS.MachineForwardRefMDNodes[ID];
  1252. FwdRef = std::make_pair(
  1253. MDTuple::getTemporary(MF.getFunction().getContext(), std::nullopt), Loc);
  1254. PFS.MachineMetadataNodes[ID].reset(FwdRef.first.get());
  1255. MD = FwdRef.first.get();
  1256. return false;
  1257. }
  1258. static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
  1259. assert(MO.isImplicit());
  1260. return MO.isDef() ? "implicit-def" : "implicit";
  1261. }
  1262. static std::string getRegisterName(const TargetRegisterInfo *TRI,
  1263. Register Reg) {
  1264. assert(Reg.isPhysical() && "expected phys reg");
  1265. return StringRef(TRI->getName(Reg)).lower();
  1266. }
  1267. /// Return true if the parsed machine operands contain a given machine operand.
  1268. static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
  1269. ArrayRef<ParsedMachineOperand> Operands) {
  1270. for (const auto &I : Operands) {
  1271. if (ImplicitOperand.isIdenticalTo(I.Operand))
  1272. return true;
  1273. }
  1274. return false;
  1275. }
  1276. bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
  1277. const MCInstrDesc &MCID) {
  1278. if (MCID.isCall())
  1279. // We can't verify call instructions as they can contain arbitrary implicit
  1280. // register and register mask operands.
  1281. return false;
  1282. // Gather all the expected implicit operands.
  1283. SmallVector<MachineOperand, 4> ImplicitOperands;
  1284. for (MCPhysReg ImpDef : MCID.implicit_defs())
  1285. ImplicitOperands.push_back(MachineOperand::CreateReg(ImpDef, true, true));
  1286. for (MCPhysReg ImpUse : MCID.implicit_uses())
  1287. ImplicitOperands.push_back(MachineOperand::CreateReg(ImpUse, false, true));
  1288. const auto *TRI = MF.getSubtarget().getRegisterInfo();
  1289. assert(TRI && "Expected target register info");
  1290. for (const auto &I : ImplicitOperands) {
  1291. if (isImplicitOperandIn(I, Operands))
  1292. continue;
  1293. return error(Operands.empty() ? Token.location() : Operands.back().End,
  1294. Twine("missing implicit register operand '") +
  1295. printImplicitRegisterFlag(I) + " $" +
  1296. getRegisterName(TRI, I.getReg()) + "'");
  1297. }
  1298. return false;
  1299. }
  1300. bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
  1301. // Allow frame and fast math flags for OPCODE
  1302. while (Token.is(MIToken::kw_frame_setup) ||
  1303. Token.is(MIToken::kw_frame_destroy) ||
  1304. Token.is(MIToken::kw_nnan) ||
  1305. Token.is(MIToken::kw_ninf) ||
  1306. Token.is(MIToken::kw_nsz) ||
  1307. Token.is(MIToken::kw_arcp) ||
  1308. Token.is(MIToken::kw_contract) ||
  1309. Token.is(MIToken::kw_afn) ||
  1310. Token.is(MIToken::kw_reassoc) ||
  1311. Token.is(MIToken::kw_nuw) ||
  1312. Token.is(MIToken::kw_nsw) ||
  1313. Token.is(MIToken::kw_exact) ||
  1314. Token.is(MIToken::kw_nofpexcept)) {
  1315. // Mine frame and fast math flags
  1316. if (Token.is(MIToken::kw_frame_setup))
  1317. Flags |= MachineInstr::FrameSetup;
  1318. if (Token.is(MIToken::kw_frame_destroy))
  1319. Flags |= MachineInstr::FrameDestroy;
  1320. if (Token.is(MIToken::kw_nnan))
  1321. Flags |= MachineInstr::FmNoNans;
  1322. if (Token.is(MIToken::kw_ninf))
  1323. Flags |= MachineInstr::FmNoInfs;
  1324. if (Token.is(MIToken::kw_nsz))
  1325. Flags |= MachineInstr::FmNsz;
  1326. if (Token.is(MIToken::kw_arcp))
  1327. Flags |= MachineInstr::FmArcp;
  1328. if (Token.is(MIToken::kw_contract))
  1329. Flags |= MachineInstr::FmContract;
  1330. if (Token.is(MIToken::kw_afn))
  1331. Flags |= MachineInstr::FmAfn;
  1332. if (Token.is(MIToken::kw_reassoc))
  1333. Flags |= MachineInstr::FmReassoc;
  1334. if (Token.is(MIToken::kw_nuw))
  1335. Flags |= MachineInstr::NoUWrap;
  1336. if (Token.is(MIToken::kw_nsw))
  1337. Flags |= MachineInstr::NoSWrap;
  1338. if (Token.is(MIToken::kw_exact))
  1339. Flags |= MachineInstr::IsExact;
  1340. if (Token.is(MIToken::kw_nofpexcept))
  1341. Flags |= MachineInstr::NoFPExcept;
  1342. lex();
  1343. }
  1344. if (Token.isNot(MIToken::Identifier))
  1345. return error("expected a machine instruction");
  1346. StringRef InstrName = Token.stringValue();
  1347. if (PFS.Target.parseInstrName(InstrName, OpCode))
  1348. return error(Twine("unknown machine instruction name '") + InstrName + "'");
  1349. lex();
  1350. return false;
  1351. }
  1352. bool MIParser::parseNamedRegister(Register &Reg) {
  1353. assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
  1354. StringRef Name = Token.stringValue();
  1355. if (PFS.Target.getRegisterByName(Name, Reg))
  1356. return error(Twine("unknown register name '") + Name + "'");
  1357. return false;
  1358. }
  1359. bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
  1360. assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token");
  1361. StringRef Name = Token.stringValue();
  1362. // TODO: Check that the VReg name is not the same as a physical register name.
  1363. // If it is, then print a warning (when warnings are implemented).
  1364. Info = &PFS.getVRegInfoNamed(Name);
  1365. return false;
  1366. }
  1367. bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
  1368. if (Token.is(MIToken::NamedVirtualRegister))
  1369. return parseNamedVirtualRegister(Info);
  1370. assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
  1371. unsigned ID;
  1372. if (getUnsigned(ID))
  1373. return true;
  1374. Info = &PFS.getVRegInfo(ID);
  1375. return false;
  1376. }
  1377. bool MIParser::parseRegister(Register &Reg, VRegInfo *&Info) {
  1378. switch (Token.kind()) {
  1379. case MIToken::underscore:
  1380. Reg = 0;
  1381. return false;
  1382. case MIToken::NamedRegister:
  1383. return parseNamedRegister(Reg);
  1384. case MIToken::NamedVirtualRegister:
  1385. case MIToken::VirtualRegister:
  1386. if (parseVirtualRegister(Info))
  1387. return true;
  1388. Reg = Info->VReg;
  1389. return false;
  1390. // TODO: Parse other register kinds.
  1391. default:
  1392. llvm_unreachable("The current token should be a register");
  1393. }
  1394. }
  1395. bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
  1396. if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
  1397. return error("expected '_', register class, or register bank name");
  1398. StringRef::iterator Loc = Token.location();
  1399. StringRef Name = Token.stringValue();
  1400. // Was it a register class?
  1401. const TargetRegisterClass *RC = PFS.Target.getRegClass(Name);
  1402. if (RC) {
  1403. lex();
  1404. switch (RegInfo.Kind) {
  1405. case VRegInfo::UNKNOWN:
  1406. case VRegInfo::NORMAL:
  1407. RegInfo.Kind = VRegInfo::NORMAL;
  1408. if (RegInfo.Explicit && RegInfo.D.RC != RC) {
  1409. const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
  1410. return error(Loc, Twine("conflicting register classes, previously: ") +
  1411. Twine(TRI.getRegClassName(RegInfo.D.RC)));
  1412. }
  1413. RegInfo.D.RC = RC;
  1414. RegInfo.Explicit = true;
  1415. return false;
  1416. case VRegInfo::GENERIC:
  1417. case VRegInfo::REGBANK:
  1418. return error(Loc, "register class specification on generic register");
  1419. }
  1420. llvm_unreachable("Unexpected register kind");
  1421. }
  1422. // Should be a register bank or a generic register.
  1423. const RegisterBank *RegBank = nullptr;
  1424. if (Name != "_") {
  1425. RegBank = PFS.Target.getRegBank(Name);
  1426. if (!RegBank)
  1427. return error(Loc, "expected '_', register class, or register bank name");
  1428. }
  1429. lex();
  1430. switch (RegInfo.Kind) {
  1431. case VRegInfo::UNKNOWN:
  1432. case VRegInfo::GENERIC:
  1433. case VRegInfo::REGBANK:
  1434. RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
  1435. if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
  1436. return error(Loc, "conflicting generic register banks");
  1437. RegInfo.D.RegBank = RegBank;
  1438. RegInfo.Explicit = true;
  1439. return false;
  1440. case VRegInfo::NORMAL:
  1441. return error(Loc, "register bank specification on normal register");
  1442. }
  1443. llvm_unreachable("Unexpected register kind");
  1444. }
  1445. bool MIParser::parseRegisterFlag(unsigned &Flags) {
  1446. const unsigned OldFlags = Flags;
  1447. switch (Token.kind()) {
  1448. case MIToken::kw_implicit:
  1449. Flags |= RegState::Implicit;
  1450. break;
  1451. case MIToken::kw_implicit_define:
  1452. Flags |= RegState::ImplicitDefine;
  1453. break;
  1454. case MIToken::kw_def:
  1455. Flags |= RegState::Define;
  1456. break;
  1457. case MIToken::kw_dead:
  1458. Flags |= RegState::Dead;
  1459. break;
  1460. case MIToken::kw_killed:
  1461. Flags |= RegState::Kill;
  1462. break;
  1463. case MIToken::kw_undef:
  1464. Flags |= RegState::Undef;
  1465. break;
  1466. case MIToken::kw_internal:
  1467. Flags |= RegState::InternalRead;
  1468. break;
  1469. case MIToken::kw_early_clobber:
  1470. Flags |= RegState::EarlyClobber;
  1471. break;
  1472. case MIToken::kw_debug_use:
  1473. Flags |= RegState::Debug;
  1474. break;
  1475. case MIToken::kw_renamable:
  1476. Flags |= RegState::Renamable;
  1477. break;
  1478. default:
  1479. llvm_unreachable("The current token should be a register flag");
  1480. }
  1481. if (OldFlags == Flags)
  1482. // We know that the same flag is specified more than once when the flags
  1483. // weren't modified.
  1484. return error("duplicate '" + Token.stringValue() + "' register flag");
  1485. lex();
  1486. return false;
  1487. }
  1488. bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
  1489. assert(Token.is(MIToken::dot));
  1490. lex();
  1491. if (Token.isNot(MIToken::Identifier))
  1492. return error("expected a subregister index after '.'");
  1493. auto Name = Token.stringValue();
  1494. SubReg = PFS.Target.getSubRegIndex(Name);
  1495. if (!SubReg)
  1496. return error(Twine("use of unknown subregister index '") + Name + "'");
  1497. lex();
  1498. return false;
  1499. }
  1500. bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
  1501. if (!consumeIfPresent(MIToken::kw_tied_def))
  1502. return true;
  1503. if (Token.isNot(MIToken::IntegerLiteral))
  1504. return error("expected an integer literal after 'tied-def'");
  1505. if (getUnsigned(TiedDefIdx))
  1506. return true;
  1507. lex();
  1508. if (expectAndConsume(MIToken::rparen))
  1509. return true;
  1510. return false;
  1511. }
  1512. bool MIParser::assignRegisterTies(MachineInstr &MI,
  1513. ArrayRef<ParsedMachineOperand> Operands) {
  1514. SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
  1515. for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
  1516. if (!Operands[I].TiedDefIdx)
  1517. continue;
  1518. // The parser ensures that this operand is a register use, so we just have
  1519. // to check the tied-def operand.
  1520. unsigned DefIdx = *Operands[I].TiedDefIdx;
  1521. if (DefIdx >= E)
  1522. return error(Operands[I].Begin,
  1523. Twine("use of invalid tied-def operand index '" +
  1524. Twine(DefIdx) + "'; instruction has only ") +
  1525. Twine(E) + " operands");
  1526. const auto &DefOperand = Operands[DefIdx].Operand;
  1527. if (!DefOperand.isReg() || !DefOperand.isDef())
  1528. // FIXME: add note with the def operand.
  1529. return error(Operands[I].Begin,
  1530. Twine("use of invalid tied-def operand index '") +
  1531. Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
  1532. " isn't a defined register");
  1533. // Check that the tied-def operand wasn't tied elsewhere.
  1534. for (const auto &TiedPair : TiedRegisterPairs) {
  1535. if (TiedPair.first == DefIdx)
  1536. return error(Operands[I].Begin,
  1537. Twine("the tied-def operand #") + Twine(DefIdx) +
  1538. " is already tied with another register operand");
  1539. }
  1540. TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
  1541. }
  1542. // FIXME: Verify that for non INLINEASM instructions, the def and use tied
  1543. // indices must be less than tied max.
  1544. for (const auto &TiedPair : TiedRegisterPairs)
  1545. MI.tieOperands(TiedPair.first, TiedPair.second);
  1546. return false;
  1547. }
  1548. bool MIParser::parseRegisterOperand(MachineOperand &Dest,
  1549. std::optional<unsigned> &TiedDefIdx,
  1550. bool IsDef) {
  1551. unsigned Flags = IsDef ? RegState::Define : 0;
  1552. while (Token.isRegisterFlag()) {
  1553. if (parseRegisterFlag(Flags))
  1554. return true;
  1555. }
  1556. if (!Token.isRegister())
  1557. return error("expected a register after register flags");
  1558. Register Reg;
  1559. VRegInfo *RegInfo;
  1560. if (parseRegister(Reg, RegInfo))
  1561. return true;
  1562. lex();
  1563. unsigned SubReg = 0;
  1564. if (Token.is(MIToken::dot)) {
  1565. if (parseSubRegisterIndex(SubReg))
  1566. return true;
  1567. if (!Reg.isVirtual())
  1568. return error("subregister index expects a virtual register");
  1569. }
  1570. if (Token.is(MIToken::colon)) {
  1571. if (!Reg.isVirtual())
  1572. return error("register class specification expects a virtual register");
  1573. lex();
  1574. if (parseRegisterClassOrBank(*RegInfo))
  1575. return true;
  1576. }
  1577. MachineRegisterInfo &MRI = MF.getRegInfo();
  1578. if ((Flags & RegState::Define) == 0) {
  1579. if (consumeIfPresent(MIToken::lparen)) {
  1580. unsigned Idx;
  1581. if (!parseRegisterTiedDefIndex(Idx))
  1582. TiedDefIdx = Idx;
  1583. else {
  1584. // Try a redundant low-level type.
  1585. LLT Ty;
  1586. if (parseLowLevelType(Token.location(), Ty))
  1587. return error("expected tied-def or low-level type after '('");
  1588. if (expectAndConsume(MIToken::rparen))
  1589. return true;
  1590. if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
  1591. return error("inconsistent type for generic virtual register");
  1592. MRI.setRegClassOrRegBank(Reg, static_cast<RegisterBank *>(nullptr));
  1593. MRI.setType(Reg, Ty);
  1594. }
  1595. }
  1596. } else if (consumeIfPresent(MIToken::lparen)) {
  1597. // Virtual registers may have a tpe with GlobalISel.
  1598. if (!Reg.isVirtual())
  1599. return error("unexpected type on physical register");
  1600. LLT Ty;
  1601. if (parseLowLevelType(Token.location(), Ty))
  1602. return true;
  1603. if (expectAndConsume(MIToken::rparen))
  1604. return true;
  1605. if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
  1606. return error("inconsistent type for generic virtual register");
  1607. MRI.setRegClassOrRegBank(Reg, static_cast<RegisterBank *>(nullptr));
  1608. MRI.setType(Reg, Ty);
  1609. } else if (Reg.isVirtual()) {
  1610. // Generic virtual registers must have a type.
  1611. // If we end up here this means the type hasn't been specified and
  1612. // this is bad!
  1613. if (RegInfo->Kind == VRegInfo::GENERIC ||
  1614. RegInfo->Kind == VRegInfo::REGBANK)
  1615. return error("generic virtual registers must have a type");
  1616. }
  1617. if (Flags & RegState::Define) {
  1618. if (Flags & RegState::Kill)
  1619. return error("cannot have a killed def operand");
  1620. } else {
  1621. if (Flags & RegState::Dead)
  1622. return error("cannot have a dead use operand");
  1623. }
  1624. Dest = MachineOperand::CreateReg(
  1625. Reg, Flags & RegState::Define, Flags & RegState::Implicit,
  1626. Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
  1627. Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
  1628. Flags & RegState::InternalRead, Flags & RegState::Renamable);
  1629. return false;
  1630. }
  1631. bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
  1632. assert(Token.is(MIToken::IntegerLiteral));
  1633. const APSInt &Int = Token.integerValue();
  1634. if (auto SImm = Int.trySExtValue(); Int.isSigned() && SImm.has_value())
  1635. Dest = MachineOperand::CreateImm(*SImm);
  1636. else if (auto UImm = Int.tryZExtValue(); !Int.isSigned() && UImm.has_value())
  1637. Dest = MachineOperand::CreateImm(*UImm);
  1638. else
  1639. return error("integer literal is too large to be an immediate operand");
  1640. lex();
  1641. return false;
  1642. }
  1643. bool MIParser::parseTargetImmMnemonic(const unsigned OpCode,
  1644. const unsigned OpIdx,
  1645. MachineOperand &Dest,
  1646. const MIRFormatter &MF) {
  1647. assert(Token.is(MIToken::dot));
  1648. auto Loc = Token.location(); // record start position
  1649. size_t Len = 1; // for "."
  1650. lex();
  1651. // Handle the case that mnemonic starts with number.
  1652. if (Token.is(MIToken::IntegerLiteral)) {
  1653. Len += Token.range().size();
  1654. lex();
  1655. }
  1656. StringRef Src;
  1657. if (Token.is(MIToken::comma))
  1658. Src = StringRef(Loc, Len);
  1659. else {
  1660. assert(Token.is(MIToken::Identifier));
  1661. Src = StringRef(Loc, Len + Token.stringValue().size());
  1662. }
  1663. int64_t Val;
  1664. if (MF.parseImmMnemonic(OpCode, OpIdx, Src, Val,
  1665. [this](StringRef::iterator Loc, const Twine &Msg)
  1666. -> bool { return error(Loc, Msg); }))
  1667. return true;
  1668. Dest = MachineOperand::CreateImm(Val);
  1669. if (!Token.is(MIToken::comma))
  1670. lex();
  1671. return false;
  1672. }
  1673. static bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
  1674. PerFunctionMIParsingState &PFS, const Constant *&C,
  1675. ErrorCallbackType ErrCB) {
  1676. auto Source = StringValue.str(); // The source has to be null terminated.
  1677. SMDiagnostic Err;
  1678. C = parseConstantValue(Source, Err, *PFS.MF.getFunction().getParent(),
  1679. &PFS.IRSlots);
  1680. if (!C)
  1681. return ErrCB(Loc + Err.getColumnNo(), Err.getMessage());
  1682. return false;
  1683. }
  1684. bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
  1685. const Constant *&C) {
  1686. return ::parseIRConstant(
  1687. Loc, StringValue, PFS, C,
  1688. [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
  1689. return error(Loc, Msg);
  1690. });
  1691. }
  1692. bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
  1693. if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
  1694. return true;
  1695. lex();
  1696. return false;
  1697. }
  1698. // See LLT implementation for bit size limits.
  1699. static bool verifyScalarSize(uint64_t Size) {
  1700. return Size != 0 && isUInt<16>(Size);
  1701. }
  1702. static bool verifyVectorElementCount(uint64_t NumElts) {
  1703. return NumElts != 0 && isUInt<16>(NumElts);
  1704. }
  1705. static bool verifyAddrSpace(uint64_t AddrSpace) {
  1706. return isUInt<24>(AddrSpace);
  1707. }
  1708. bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
  1709. if (Token.range().front() == 's' || Token.range().front() == 'p') {
  1710. StringRef SizeStr = Token.range().drop_front();
  1711. if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
  1712. return error("expected integers after 's'/'p' type character");
  1713. }
  1714. if (Token.range().front() == 's') {
  1715. auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
  1716. if (!verifyScalarSize(ScalarSize))
  1717. return error("invalid size for scalar type");
  1718. Ty = LLT::scalar(ScalarSize);
  1719. lex();
  1720. return false;
  1721. } else if (Token.range().front() == 'p') {
  1722. const DataLayout &DL = MF.getDataLayout();
  1723. uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue();
  1724. if (!verifyAddrSpace(AS))
  1725. return error("invalid address space number");
  1726. Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
  1727. lex();
  1728. return false;
  1729. }
  1730. // Now we're looking for a vector.
  1731. if (Token.isNot(MIToken::less))
  1732. return error(Loc,
  1733. "expected sN, pA, <M x sN>, or <M x pA> for GlobalISel type");
  1734. lex();
  1735. if (Token.isNot(MIToken::IntegerLiteral))
  1736. return error(Loc, "expected <M x sN> or <M x pA> for vector type");
  1737. uint64_t NumElements = Token.integerValue().getZExtValue();
  1738. if (!verifyVectorElementCount(NumElements))
  1739. return error("invalid number of vector elements");
  1740. lex();
  1741. if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
  1742. return error(Loc, "expected <M x sN> or <M x pA> for vector type");
  1743. lex();
  1744. if (Token.range().front() != 's' && Token.range().front() != 'p')
  1745. return error(Loc, "expected <M x sN> or <M x pA> for vector type");
  1746. StringRef SizeStr = Token.range().drop_front();
  1747. if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
  1748. return error("expected integers after 's'/'p' type character");
  1749. if (Token.range().front() == 's') {
  1750. auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
  1751. if (!verifyScalarSize(ScalarSize))
  1752. return error("invalid size for scalar type");
  1753. Ty = LLT::scalar(ScalarSize);
  1754. } else if (Token.range().front() == 'p') {
  1755. const DataLayout &DL = MF.getDataLayout();
  1756. uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue();
  1757. if (!verifyAddrSpace(AS))
  1758. return error("invalid address space number");
  1759. Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
  1760. } else
  1761. return error(Loc, "expected <M x sN> or <M x pA> for vector type");
  1762. lex();
  1763. if (Token.isNot(MIToken::greater))
  1764. return error(Loc, "expected <M x sN> or <M x pA> for vector type");
  1765. lex();
  1766. Ty = LLT::fixed_vector(NumElements, Ty);
  1767. return false;
  1768. }
  1769. bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
  1770. assert(Token.is(MIToken::Identifier));
  1771. StringRef TypeStr = Token.range();
  1772. if (TypeStr.front() != 'i' && TypeStr.front() != 's' &&
  1773. TypeStr.front() != 'p')
  1774. return error(
  1775. "a typed immediate operand should start with one of 'i', 's', or 'p'");
  1776. StringRef SizeStr = Token.range().drop_front();
  1777. if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
  1778. return error("expected integers after 'i'/'s'/'p' type character");
  1779. auto Loc = Token.location();
  1780. lex();
  1781. if (Token.isNot(MIToken::IntegerLiteral)) {
  1782. if (Token.isNot(MIToken::Identifier) ||
  1783. !(Token.range() == "true" || Token.range() == "false"))
  1784. return error("expected an integer literal");
  1785. }
  1786. const Constant *C = nullptr;
  1787. if (parseIRConstant(Loc, C))
  1788. return true;
  1789. Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
  1790. return false;
  1791. }
  1792. bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
  1793. auto Loc = Token.location();
  1794. lex();
  1795. if (Token.isNot(MIToken::FloatingPointLiteral) &&
  1796. Token.isNot(MIToken::HexLiteral))
  1797. return error("expected a floating point literal");
  1798. const Constant *C = nullptr;
  1799. if (parseIRConstant(Loc, C))
  1800. return true;
  1801. Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
  1802. return false;
  1803. }
  1804. static bool getHexUint(const MIToken &Token, APInt &Result) {
  1805. assert(Token.is(MIToken::HexLiteral));
  1806. StringRef S = Token.range();
  1807. assert(S[0] == '0' && tolower(S[1]) == 'x');
  1808. // This could be a floating point literal with a special prefix.
  1809. if (!isxdigit(S[2]))
  1810. return true;
  1811. StringRef V = S.substr(2);
  1812. APInt A(V.size()*4, V, 16);
  1813. // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
  1814. // sure it isn't the case before constructing result.
  1815. unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
  1816. Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
  1817. return false;
  1818. }
  1819. static bool getUnsigned(const MIToken &Token, unsigned &Result,
  1820. ErrorCallbackType ErrCB) {
  1821. if (Token.hasIntegerValue()) {
  1822. const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
  1823. uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
  1824. if (Val64 == Limit)
  1825. return ErrCB(Token.location(), "expected 32-bit integer (too large)");
  1826. Result = Val64;
  1827. return false;
  1828. }
  1829. if (Token.is(MIToken::HexLiteral)) {
  1830. APInt A;
  1831. if (getHexUint(Token, A))
  1832. return true;
  1833. if (A.getBitWidth() > 32)
  1834. return ErrCB(Token.location(), "expected 32-bit integer (too large)");
  1835. Result = A.getZExtValue();
  1836. return false;
  1837. }
  1838. return true;
  1839. }
  1840. bool MIParser::getUnsigned(unsigned &Result) {
  1841. return ::getUnsigned(
  1842. Token, Result, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
  1843. return error(Loc, Msg);
  1844. });
  1845. }
  1846. bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
  1847. assert(Token.is(MIToken::MachineBasicBlock) ||
  1848. Token.is(MIToken::MachineBasicBlockLabel));
  1849. unsigned Number;
  1850. if (getUnsigned(Number))
  1851. return true;
  1852. auto MBBInfo = PFS.MBBSlots.find(Number);
  1853. if (MBBInfo == PFS.MBBSlots.end())
  1854. return error(Twine("use of undefined machine basic block #") +
  1855. Twine(Number));
  1856. MBB = MBBInfo->second;
  1857. // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
  1858. // we drop the <irname> from the bb.<id>.<irname> format.
  1859. if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
  1860. return error(Twine("the name of machine basic block #") + Twine(Number) +
  1861. " isn't '" + Token.stringValue() + "'");
  1862. return false;
  1863. }
  1864. bool MIParser::parseMBBOperand(MachineOperand &Dest) {
  1865. MachineBasicBlock *MBB;
  1866. if (parseMBBReference(MBB))
  1867. return true;
  1868. Dest = MachineOperand::CreateMBB(MBB);
  1869. lex();
  1870. return false;
  1871. }
  1872. bool MIParser::parseStackFrameIndex(int &FI) {
  1873. assert(Token.is(MIToken::StackObject));
  1874. unsigned ID;
  1875. if (getUnsigned(ID))
  1876. return true;
  1877. auto ObjectInfo = PFS.StackObjectSlots.find(ID);
  1878. if (ObjectInfo == PFS.StackObjectSlots.end())
  1879. return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
  1880. "'");
  1881. StringRef Name;
  1882. if (const auto *Alloca =
  1883. MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
  1884. Name = Alloca->getName();
  1885. if (!Token.stringValue().empty() && Token.stringValue() != Name)
  1886. return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
  1887. "' isn't '" + Token.stringValue() + "'");
  1888. lex();
  1889. FI = ObjectInfo->second;
  1890. return false;
  1891. }
  1892. bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
  1893. int FI;
  1894. if (parseStackFrameIndex(FI))
  1895. return true;
  1896. Dest = MachineOperand::CreateFI(FI);
  1897. return false;
  1898. }
  1899. bool MIParser::parseFixedStackFrameIndex(int &FI) {
  1900. assert(Token.is(MIToken::FixedStackObject));
  1901. unsigned ID;
  1902. if (getUnsigned(ID))
  1903. return true;
  1904. auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
  1905. if (ObjectInfo == PFS.FixedStackObjectSlots.end())
  1906. return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
  1907. Twine(ID) + "'");
  1908. lex();
  1909. FI = ObjectInfo->second;
  1910. return false;
  1911. }
  1912. bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
  1913. int FI;
  1914. if (parseFixedStackFrameIndex(FI))
  1915. return true;
  1916. Dest = MachineOperand::CreateFI(FI);
  1917. return false;
  1918. }
  1919. static bool parseGlobalValue(const MIToken &Token,
  1920. PerFunctionMIParsingState &PFS, GlobalValue *&GV,
  1921. ErrorCallbackType ErrCB) {
  1922. switch (Token.kind()) {
  1923. case MIToken::NamedGlobalValue: {
  1924. const Module *M = PFS.MF.getFunction().getParent();
  1925. GV = M->getNamedValue(Token.stringValue());
  1926. if (!GV)
  1927. return ErrCB(Token.location(), Twine("use of undefined global value '") +
  1928. Token.range() + "'");
  1929. break;
  1930. }
  1931. case MIToken::GlobalValue: {
  1932. unsigned GVIdx;
  1933. if (getUnsigned(Token, GVIdx, ErrCB))
  1934. return true;
  1935. if (GVIdx >= PFS.IRSlots.GlobalValues.size())
  1936. return ErrCB(Token.location(), Twine("use of undefined global value '@") +
  1937. Twine(GVIdx) + "'");
  1938. GV = PFS.IRSlots.GlobalValues[GVIdx];
  1939. break;
  1940. }
  1941. default:
  1942. llvm_unreachable("The current token should be a global value");
  1943. }
  1944. return false;
  1945. }
  1946. bool MIParser::parseGlobalValue(GlobalValue *&GV) {
  1947. return ::parseGlobalValue(
  1948. Token, PFS, GV,
  1949. [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
  1950. return error(Loc, Msg);
  1951. });
  1952. }
  1953. bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
  1954. GlobalValue *GV = nullptr;
  1955. if (parseGlobalValue(GV))
  1956. return true;
  1957. lex();
  1958. Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
  1959. if (parseOperandsOffset(Dest))
  1960. return true;
  1961. return false;
  1962. }
  1963. bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
  1964. assert(Token.is(MIToken::ConstantPoolItem));
  1965. unsigned ID;
  1966. if (getUnsigned(ID))
  1967. return true;
  1968. auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
  1969. if (ConstantInfo == PFS.ConstantPoolSlots.end())
  1970. return error("use of undefined constant '%const." + Twine(ID) + "'");
  1971. lex();
  1972. Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
  1973. if (parseOperandsOffset(Dest))
  1974. return true;
  1975. return false;
  1976. }
  1977. bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
  1978. assert(Token.is(MIToken::JumpTableIndex));
  1979. unsigned ID;
  1980. if (getUnsigned(ID))
  1981. return true;
  1982. auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
  1983. if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
  1984. return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
  1985. lex();
  1986. Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
  1987. return false;
  1988. }
  1989. bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
  1990. assert(Token.is(MIToken::ExternalSymbol));
  1991. const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
  1992. lex();
  1993. Dest = MachineOperand::CreateES(Symbol);
  1994. if (parseOperandsOffset(Dest))
  1995. return true;
  1996. return false;
  1997. }
  1998. bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) {
  1999. assert(Token.is(MIToken::MCSymbol));
  2000. MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue());
  2001. lex();
  2002. Dest = MachineOperand::CreateMCSymbol(Symbol);
  2003. if (parseOperandsOffset(Dest))
  2004. return true;
  2005. return false;
  2006. }
  2007. bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
  2008. assert(Token.is(MIToken::SubRegisterIndex));
  2009. StringRef Name = Token.stringValue();
  2010. unsigned SubRegIndex = PFS.Target.getSubRegIndex(Token.stringValue());
  2011. if (SubRegIndex == 0)
  2012. return error(Twine("unknown subregister index '") + Name + "'");
  2013. lex();
  2014. Dest = MachineOperand::CreateImm(SubRegIndex);
  2015. return false;
  2016. }
  2017. bool MIParser::parseMDNode(MDNode *&Node) {
  2018. assert(Token.is(MIToken::exclaim));
  2019. auto Loc = Token.location();
  2020. lex();
  2021. if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
  2022. return error("expected metadata id after '!'");
  2023. unsigned ID;
  2024. if (getUnsigned(ID))
  2025. return true;
  2026. auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
  2027. if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) {
  2028. NodeInfo = PFS.MachineMetadataNodes.find(ID);
  2029. if (NodeInfo == PFS.MachineMetadataNodes.end())
  2030. return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
  2031. }
  2032. lex();
  2033. Node = NodeInfo->second.get();
  2034. return false;
  2035. }
  2036. bool MIParser::parseDIExpression(MDNode *&Expr) {
  2037. assert(Token.is(MIToken::md_diexpr));
  2038. lex();
  2039. // FIXME: Share this parsing with the IL parser.
  2040. SmallVector<uint64_t, 8> Elements;
  2041. if (expectAndConsume(MIToken::lparen))
  2042. return true;
  2043. if (Token.isNot(MIToken::rparen)) {
  2044. do {
  2045. if (Token.is(MIToken::Identifier)) {
  2046. if (unsigned Op = dwarf::getOperationEncoding(Token.stringValue())) {
  2047. lex();
  2048. Elements.push_back(Op);
  2049. continue;
  2050. }
  2051. if (unsigned Enc = dwarf::getAttributeEncoding(Token.stringValue())) {
  2052. lex();
  2053. Elements.push_back(Enc);
  2054. continue;
  2055. }
  2056. return error(Twine("invalid DWARF op '") + Token.stringValue() + "'");
  2057. }
  2058. if (Token.isNot(MIToken::IntegerLiteral) ||
  2059. Token.integerValue().isSigned())
  2060. return error("expected unsigned integer");
  2061. auto &U = Token.integerValue();
  2062. if (U.ugt(UINT64_MAX))
  2063. return error("element too large, limit is " + Twine(UINT64_MAX));
  2064. Elements.push_back(U.getZExtValue());
  2065. lex();
  2066. } while (consumeIfPresent(MIToken::comma));
  2067. }
  2068. if (expectAndConsume(MIToken::rparen))
  2069. return true;
  2070. Expr = DIExpression::get(MF.getFunction().getContext(), Elements);
  2071. return false;
  2072. }
  2073. bool MIParser::parseDILocation(MDNode *&Loc) {
  2074. assert(Token.is(MIToken::md_dilocation));
  2075. lex();
  2076. bool HaveLine = false;
  2077. unsigned Line = 0;
  2078. unsigned Column = 0;
  2079. MDNode *Scope = nullptr;
  2080. MDNode *InlinedAt = nullptr;
  2081. bool ImplicitCode = false;
  2082. if (expectAndConsume(MIToken::lparen))
  2083. return true;
  2084. if (Token.isNot(MIToken::rparen)) {
  2085. do {
  2086. if (Token.is(MIToken::Identifier)) {
  2087. if (Token.stringValue() == "line") {
  2088. lex();
  2089. if (expectAndConsume(MIToken::colon))
  2090. return true;
  2091. if (Token.isNot(MIToken::IntegerLiteral) ||
  2092. Token.integerValue().isSigned())
  2093. return error("expected unsigned integer");
  2094. Line = Token.integerValue().getZExtValue();
  2095. HaveLine = true;
  2096. lex();
  2097. continue;
  2098. }
  2099. if (Token.stringValue() == "column") {
  2100. lex();
  2101. if (expectAndConsume(MIToken::colon))
  2102. return true;
  2103. if (Token.isNot(MIToken::IntegerLiteral) ||
  2104. Token.integerValue().isSigned())
  2105. return error("expected unsigned integer");
  2106. Column = Token.integerValue().getZExtValue();
  2107. lex();
  2108. continue;
  2109. }
  2110. if (Token.stringValue() == "scope") {
  2111. lex();
  2112. if (expectAndConsume(MIToken::colon))
  2113. return true;
  2114. if (parseMDNode(Scope))
  2115. return error("expected metadata node");
  2116. if (!isa<DIScope>(Scope))
  2117. return error("expected DIScope node");
  2118. continue;
  2119. }
  2120. if (Token.stringValue() == "inlinedAt") {
  2121. lex();
  2122. if (expectAndConsume(MIToken::colon))
  2123. return true;
  2124. if (Token.is(MIToken::exclaim)) {
  2125. if (parseMDNode(InlinedAt))
  2126. return true;
  2127. } else if (Token.is(MIToken::md_dilocation)) {
  2128. if (parseDILocation(InlinedAt))
  2129. return true;
  2130. } else
  2131. return error("expected metadata node");
  2132. if (!isa<DILocation>(InlinedAt))
  2133. return error("expected DILocation node");
  2134. continue;
  2135. }
  2136. if (Token.stringValue() == "isImplicitCode") {
  2137. lex();
  2138. if (expectAndConsume(MIToken::colon))
  2139. return true;
  2140. if (!Token.is(MIToken::Identifier))
  2141. return error("expected true/false");
  2142. // As far as I can see, we don't have any existing need for parsing
  2143. // true/false in MIR yet. Do it ad-hoc until there's something else
  2144. // that needs it.
  2145. if (Token.stringValue() == "true")
  2146. ImplicitCode = true;
  2147. else if (Token.stringValue() == "false")
  2148. ImplicitCode = false;
  2149. else
  2150. return error("expected true/false");
  2151. lex();
  2152. continue;
  2153. }
  2154. }
  2155. return error(Twine("invalid DILocation argument '") +
  2156. Token.stringValue() + "'");
  2157. } while (consumeIfPresent(MIToken::comma));
  2158. }
  2159. if (expectAndConsume(MIToken::rparen))
  2160. return true;
  2161. if (!HaveLine)
  2162. return error("DILocation requires line number");
  2163. if (!Scope)
  2164. return error("DILocation requires a scope");
  2165. Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
  2166. InlinedAt, ImplicitCode);
  2167. return false;
  2168. }
  2169. bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
  2170. MDNode *Node = nullptr;
  2171. if (Token.is(MIToken::exclaim)) {
  2172. if (parseMDNode(Node))
  2173. return true;
  2174. } else if (Token.is(MIToken::md_diexpr)) {
  2175. if (parseDIExpression(Node))
  2176. return true;
  2177. }
  2178. Dest = MachineOperand::CreateMetadata(Node);
  2179. return false;
  2180. }
  2181. bool MIParser::parseCFIOffset(int &Offset) {
  2182. if (Token.isNot(MIToken::IntegerLiteral))
  2183. return error("expected a cfi offset");
  2184. if (Token.integerValue().getMinSignedBits() > 32)
  2185. return error("expected a 32 bit integer (the cfi offset is too large)");
  2186. Offset = (int)Token.integerValue().getExtValue();
  2187. lex();
  2188. return false;
  2189. }
  2190. bool MIParser::parseCFIRegister(Register &Reg) {
  2191. if (Token.isNot(MIToken::NamedRegister))
  2192. return error("expected a cfi register");
  2193. Register LLVMReg;
  2194. if (parseNamedRegister(LLVMReg))
  2195. return true;
  2196. const auto *TRI = MF.getSubtarget().getRegisterInfo();
  2197. assert(TRI && "Expected target register info");
  2198. int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
  2199. if (DwarfReg < 0)
  2200. return error("invalid DWARF register");
  2201. Reg = (unsigned)DwarfReg;
  2202. lex();
  2203. return false;
  2204. }
  2205. bool MIParser::parseCFIAddressSpace(unsigned &AddressSpace) {
  2206. if (Token.isNot(MIToken::IntegerLiteral))
  2207. return error("expected a cfi address space literal");
  2208. if (Token.integerValue().isSigned())
  2209. return error("expected an unsigned integer (cfi address space)");
  2210. AddressSpace = Token.integerValue().getZExtValue();
  2211. lex();
  2212. return false;
  2213. }
  2214. bool MIParser::parseCFIEscapeValues(std::string &Values) {
  2215. do {
  2216. if (Token.isNot(MIToken::HexLiteral))
  2217. return error("expected a hexadecimal literal");
  2218. unsigned Value;
  2219. if (getUnsigned(Value))
  2220. return true;
  2221. if (Value > UINT8_MAX)
  2222. return error("expected a 8-bit integer (too large)");
  2223. Values.push_back(static_cast<uint8_t>(Value));
  2224. lex();
  2225. } while (consumeIfPresent(MIToken::comma));
  2226. return false;
  2227. }
  2228. bool MIParser::parseCFIOperand(MachineOperand &Dest) {
  2229. auto Kind = Token.kind();
  2230. lex();
  2231. int Offset;
  2232. Register Reg;
  2233. unsigned AddressSpace;
  2234. unsigned CFIIndex;
  2235. switch (Kind) {
  2236. case MIToken::kw_cfi_same_value:
  2237. if (parseCFIRegister(Reg))
  2238. return true;
  2239. CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
  2240. break;
  2241. case MIToken::kw_cfi_offset:
  2242. if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
  2243. parseCFIOffset(Offset))
  2244. return true;
  2245. CFIIndex =
  2246. MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
  2247. break;
  2248. case MIToken::kw_cfi_rel_offset:
  2249. if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
  2250. parseCFIOffset(Offset))
  2251. return true;
  2252. CFIIndex = MF.addFrameInst(
  2253. MCCFIInstruction::createRelOffset(nullptr, Reg, Offset));
  2254. break;
  2255. case MIToken::kw_cfi_def_cfa_register:
  2256. if (parseCFIRegister(Reg))
  2257. return true;
  2258. CFIIndex =
  2259. MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
  2260. break;
  2261. case MIToken::kw_cfi_def_cfa_offset:
  2262. if (parseCFIOffset(Offset))
  2263. return true;
  2264. CFIIndex =
  2265. MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset));
  2266. break;
  2267. case MIToken::kw_cfi_adjust_cfa_offset:
  2268. if (parseCFIOffset(Offset))
  2269. return true;
  2270. CFIIndex = MF.addFrameInst(
  2271. MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset));
  2272. break;
  2273. case MIToken::kw_cfi_def_cfa:
  2274. if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
  2275. parseCFIOffset(Offset))
  2276. return true;
  2277. CFIIndex =
  2278. MF.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg, Offset));
  2279. break;
  2280. case MIToken::kw_cfi_llvm_def_aspace_cfa:
  2281. if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
  2282. parseCFIOffset(Offset) || expectAndConsume(MIToken::comma) ||
  2283. parseCFIAddressSpace(AddressSpace))
  2284. return true;
  2285. CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMDefAspaceCfa(
  2286. nullptr, Reg, Offset, AddressSpace));
  2287. break;
  2288. case MIToken::kw_cfi_remember_state:
  2289. CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
  2290. break;
  2291. case MIToken::kw_cfi_restore:
  2292. if (parseCFIRegister(Reg))
  2293. return true;
  2294. CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
  2295. break;
  2296. case MIToken::kw_cfi_restore_state:
  2297. CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
  2298. break;
  2299. case MIToken::kw_cfi_undefined:
  2300. if (parseCFIRegister(Reg))
  2301. return true;
  2302. CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
  2303. break;
  2304. case MIToken::kw_cfi_register: {
  2305. Register Reg2;
  2306. if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
  2307. parseCFIRegister(Reg2))
  2308. return true;
  2309. CFIIndex =
  2310. MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
  2311. break;
  2312. }
  2313. case MIToken::kw_cfi_window_save:
  2314. CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
  2315. break;
  2316. case MIToken::kw_cfi_aarch64_negate_ra_sign_state:
  2317. CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
  2318. break;
  2319. case MIToken::kw_cfi_escape: {
  2320. std::string Values;
  2321. if (parseCFIEscapeValues(Values))
  2322. return true;
  2323. CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
  2324. break;
  2325. }
  2326. default:
  2327. // TODO: Parse the other CFI operands.
  2328. llvm_unreachable("The current token should be a cfi operand");
  2329. }
  2330. Dest = MachineOperand::CreateCFIIndex(CFIIndex);
  2331. return false;
  2332. }
  2333. bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
  2334. switch (Token.kind()) {
  2335. case MIToken::NamedIRBlock: {
  2336. BB = dyn_cast_or_null<BasicBlock>(
  2337. F.getValueSymbolTable()->lookup(Token.stringValue()));
  2338. if (!BB)
  2339. return error(Twine("use of undefined IR block '") + Token.range() + "'");
  2340. break;
  2341. }
  2342. case MIToken::IRBlock: {
  2343. unsigned SlotNumber = 0;
  2344. if (getUnsigned(SlotNumber))
  2345. return true;
  2346. BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
  2347. if (!BB)
  2348. return error(Twine("use of undefined IR block '%ir-block.") +
  2349. Twine(SlotNumber) + "'");
  2350. break;
  2351. }
  2352. default:
  2353. llvm_unreachable("The current token should be an IR block reference");
  2354. }
  2355. return false;
  2356. }
  2357. bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
  2358. assert(Token.is(MIToken::kw_blockaddress));
  2359. lex();
  2360. if (expectAndConsume(MIToken::lparen))
  2361. return true;
  2362. if (Token.isNot(MIToken::GlobalValue) &&
  2363. Token.isNot(MIToken::NamedGlobalValue))
  2364. return error("expected a global value");
  2365. GlobalValue *GV = nullptr;
  2366. if (parseGlobalValue(GV))
  2367. return true;
  2368. auto *F = dyn_cast<Function>(GV);
  2369. if (!F)
  2370. return error("expected an IR function reference");
  2371. lex();
  2372. if (expectAndConsume(MIToken::comma))
  2373. return true;
  2374. BasicBlock *BB = nullptr;
  2375. if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
  2376. return error("expected an IR block reference");
  2377. if (parseIRBlock(BB, *F))
  2378. return true;
  2379. lex();
  2380. if (expectAndConsume(MIToken::rparen))
  2381. return true;
  2382. Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
  2383. if (parseOperandsOffset(Dest))
  2384. return true;
  2385. return false;
  2386. }
  2387. bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
  2388. assert(Token.is(MIToken::kw_intrinsic));
  2389. lex();
  2390. if (expectAndConsume(MIToken::lparen))
  2391. return error("expected syntax intrinsic(@llvm.whatever)");
  2392. if (Token.isNot(MIToken::NamedGlobalValue))
  2393. return error("expected syntax intrinsic(@llvm.whatever)");
  2394. std::string Name = std::string(Token.stringValue());
  2395. lex();
  2396. if (expectAndConsume(MIToken::rparen))
  2397. return error("expected ')' to terminate intrinsic name");
  2398. // Find out what intrinsic we're dealing with, first try the global namespace
  2399. // and then the target's private intrinsics if that fails.
  2400. const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
  2401. Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
  2402. if (ID == Intrinsic::not_intrinsic && TII)
  2403. ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
  2404. if (ID == Intrinsic::not_intrinsic)
  2405. return error("unknown intrinsic name");
  2406. Dest = MachineOperand::CreateIntrinsicID(ID);
  2407. return false;
  2408. }
  2409. bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
  2410. assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
  2411. bool IsFloat = Token.is(MIToken::kw_floatpred);
  2412. lex();
  2413. if (expectAndConsume(MIToken::lparen))
  2414. return error("expected syntax intpred(whatever) or floatpred(whatever");
  2415. if (Token.isNot(MIToken::Identifier))
  2416. return error("whatever");
  2417. CmpInst::Predicate Pred;
  2418. if (IsFloat) {
  2419. Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
  2420. .Case("false", CmpInst::FCMP_FALSE)
  2421. .Case("oeq", CmpInst::FCMP_OEQ)
  2422. .Case("ogt", CmpInst::FCMP_OGT)
  2423. .Case("oge", CmpInst::FCMP_OGE)
  2424. .Case("olt", CmpInst::FCMP_OLT)
  2425. .Case("ole", CmpInst::FCMP_OLE)
  2426. .Case("one", CmpInst::FCMP_ONE)
  2427. .Case("ord", CmpInst::FCMP_ORD)
  2428. .Case("uno", CmpInst::FCMP_UNO)
  2429. .Case("ueq", CmpInst::FCMP_UEQ)
  2430. .Case("ugt", CmpInst::FCMP_UGT)
  2431. .Case("uge", CmpInst::FCMP_UGE)
  2432. .Case("ult", CmpInst::FCMP_ULT)
  2433. .Case("ule", CmpInst::FCMP_ULE)
  2434. .Case("une", CmpInst::FCMP_UNE)
  2435. .Case("true", CmpInst::FCMP_TRUE)
  2436. .Default(CmpInst::BAD_FCMP_PREDICATE);
  2437. if (!CmpInst::isFPPredicate(Pred))
  2438. return error("invalid floating-point predicate");
  2439. } else {
  2440. Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
  2441. .Case("eq", CmpInst::ICMP_EQ)
  2442. .Case("ne", CmpInst::ICMP_NE)
  2443. .Case("sgt", CmpInst::ICMP_SGT)
  2444. .Case("sge", CmpInst::ICMP_SGE)
  2445. .Case("slt", CmpInst::ICMP_SLT)
  2446. .Case("sle", CmpInst::ICMP_SLE)
  2447. .Case("ugt", CmpInst::ICMP_UGT)
  2448. .Case("uge", CmpInst::ICMP_UGE)
  2449. .Case("ult", CmpInst::ICMP_ULT)
  2450. .Case("ule", CmpInst::ICMP_ULE)
  2451. .Default(CmpInst::BAD_ICMP_PREDICATE);
  2452. if (!CmpInst::isIntPredicate(Pred))
  2453. return error("invalid integer predicate");
  2454. }
  2455. lex();
  2456. Dest = MachineOperand::CreatePredicate(Pred);
  2457. if (expectAndConsume(MIToken::rparen))
  2458. return error("predicate should be terminated by ')'.");
  2459. return false;
  2460. }
  2461. bool MIParser::parseShuffleMaskOperand(MachineOperand &Dest) {
  2462. assert(Token.is(MIToken::kw_shufflemask));
  2463. lex();
  2464. if (expectAndConsume(MIToken::lparen))
  2465. return error("expected syntax shufflemask(<integer or undef>, ...)");
  2466. SmallVector<int, 32> ShufMask;
  2467. do {
  2468. if (Token.is(MIToken::kw_undef)) {
  2469. ShufMask.push_back(-1);
  2470. } else if (Token.is(MIToken::IntegerLiteral)) {
  2471. const APSInt &Int = Token.integerValue();
  2472. ShufMask.push_back(Int.getExtValue());
  2473. } else
  2474. return error("expected integer constant");
  2475. lex();
  2476. } while (consumeIfPresent(MIToken::comma));
  2477. if (expectAndConsume(MIToken::rparen))
  2478. return error("shufflemask should be terminated by ')'.");
  2479. ArrayRef<int> MaskAlloc = MF.allocateShuffleMask(ShufMask);
  2480. Dest = MachineOperand::CreateShuffleMask(MaskAlloc);
  2481. return false;
  2482. }
  2483. bool MIParser::parseDbgInstrRefOperand(MachineOperand &Dest) {
  2484. assert(Token.is(MIToken::kw_dbg_instr_ref));
  2485. lex();
  2486. if (expectAndConsume(MIToken::lparen))
  2487. return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
  2488. if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
  2489. return error("expected unsigned integer for instruction index");
  2490. uint64_t InstrIdx = Token.integerValue().getZExtValue();
  2491. assert(InstrIdx <= std::numeric_limits<unsigned>::max() &&
  2492. "Instruction reference's instruction index is too large");
  2493. lex();
  2494. if (expectAndConsume(MIToken::comma))
  2495. return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
  2496. if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
  2497. return error("expected unsigned integer for operand index");
  2498. uint64_t OpIdx = Token.integerValue().getZExtValue();
  2499. assert(OpIdx <= std::numeric_limits<unsigned>::max() &&
  2500. "Instruction reference's operand index is too large");
  2501. lex();
  2502. if (expectAndConsume(MIToken::rparen))
  2503. return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
  2504. Dest = MachineOperand::CreateDbgInstrRef(InstrIdx, OpIdx);
  2505. return false;
  2506. }
  2507. bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
  2508. assert(Token.is(MIToken::kw_target_index));
  2509. lex();
  2510. if (expectAndConsume(MIToken::lparen))
  2511. return true;
  2512. if (Token.isNot(MIToken::Identifier))
  2513. return error("expected the name of the target index");
  2514. int Index = 0;
  2515. if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
  2516. return error("use of undefined target index '" + Token.stringValue() + "'");
  2517. lex();
  2518. if (expectAndConsume(MIToken::rparen))
  2519. return true;
  2520. Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
  2521. if (parseOperandsOffset(Dest))
  2522. return true;
  2523. return false;
  2524. }
  2525. bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
  2526. assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
  2527. lex();
  2528. if (expectAndConsume(MIToken::lparen))
  2529. return true;
  2530. uint32_t *Mask = MF.allocateRegMask();
  2531. do {
  2532. if (Token.isNot(MIToken::rparen)) {
  2533. if (Token.isNot(MIToken::NamedRegister))
  2534. return error("expected a named register");
  2535. Register Reg;
  2536. if (parseNamedRegister(Reg))
  2537. return true;
  2538. lex();
  2539. Mask[Reg / 32] |= 1U << (Reg % 32);
  2540. }
  2541. // TODO: Report an error if the same register is used more than once.
  2542. } while (consumeIfPresent(MIToken::comma));
  2543. if (expectAndConsume(MIToken::rparen))
  2544. return true;
  2545. Dest = MachineOperand::CreateRegMask(Mask);
  2546. return false;
  2547. }
  2548. bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
  2549. assert(Token.is(MIToken::kw_liveout));
  2550. uint32_t *Mask = MF.allocateRegMask();
  2551. lex();
  2552. if (expectAndConsume(MIToken::lparen))
  2553. return true;
  2554. while (true) {
  2555. if (Token.isNot(MIToken::NamedRegister))
  2556. return error("expected a named register");
  2557. Register Reg;
  2558. if (parseNamedRegister(Reg))
  2559. return true;
  2560. lex();
  2561. Mask[Reg / 32] |= 1U << (Reg % 32);
  2562. // TODO: Report an error if the same register is used more than once.
  2563. if (Token.isNot(MIToken::comma))
  2564. break;
  2565. lex();
  2566. }
  2567. if (expectAndConsume(MIToken::rparen))
  2568. return true;
  2569. Dest = MachineOperand::CreateRegLiveOut(Mask);
  2570. return false;
  2571. }
  2572. bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
  2573. MachineOperand &Dest,
  2574. std::optional<unsigned> &TiedDefIdx) {
  2575. switch (Token.kind()) {
  2576. case MIToken::kw_implicit:
  2577. case MIToken::kw_implicit_define:
  2578. case MIToken::kw_def:
  2579. case MIToken::kw_dead:
  2580. case MIToken::kw_killed:
  2581. case MIToken::kw_undef:
  2582. case MIToken::kw_internal:
  2583. case MIToken::kw_early_clobber:
  2584. case MIToken::kw_debug_use:
  2585. case MIToken::kw_renamable:
  2586. case MIToken::underscore:
  2587. case MIToken::NamedRegister:
  2588. case MIToken::VirtualRegister:
  2589. case MIToken::NamedVirtualRegister:
  2590. return parseRegisterOperand(Dest, TiedDefIdx);
  2591. case MIToken::IntegerLiteral:
  2592. return parseImmediateOperand(Dest);
  2593. case MIToken::kw_half:
  2594. case MIToken::kw_float:
  2595. case MIToken::kw_double:
  2596. case MIToken::kw_x86_fp80:
  2597. case MIToken::kw_fp128:
  2598. case MIToken::kw_ppc_fp128:
  2599. return parseFPImmediateOperand(Dest);
  2600. case MIToken::MachineBasicBlock:
  2601. return parseMBBOperand(Dest);
  2602. case MIToken::StackObject:
  2603. return parseStackObjectOperand(Dest);
  2604. case MIToken::FixedStackObject:
  2605. return parseFixedStackObjectOperand(Dest);
  2606. case MIToken::GlobalValue:
  2607. case MIToken::NamedGlobalValue:
  2608. return parseGlobalAddressOperand(Dest);
  2609. case MIToken::ConstantPoolItem:
  2610. return parseConstantPoolIndexOperand(Dest);
  2611. case MIToken::JumpTableIndex:
  2612. return parseJumpTableIndexOperand(Dest);
  2613. case MIToken::ExternalSymbol:
  2614. return parseExternalSymbolOperand(Dest);
  2615. case MIToken::MCSymbol:
  2616. return parseMCSymbolOperand(Dest);
  2617. case MIToken::SubRegisterIndex:
  2618. return parseSubRegisterIndexOperand(Dest);
  2619. case MIToken::md_diexpr:
  2620. case MIToken::exclaim:
  2621. return parseMetadataOperand(Dest);
  2622. case MIToken::kw_cfi_same_value:
  2623. case MIToken::kw_cfi_offset:
  2624. case MIToken::kw_cfi_rel_offset:
  2625. case MIToken::kw_cfi_def_cfa_register:
  2626. case MIToken::kw_cfi_def_cfa_offset:
  2627. case MIToken::kw_cfi_adjust_cfa_offset:
  2628. case MIToken::kw_cfi_escape:
  2629. case MIToken::kw_cfi_def_cfa:
  2630. case MIToken::kw_cfi_llvm_def_aspace_cfa:
  2631. case MIToken::kw_cfi_register:
  2632. case MIToken::kw_cfi_remember_state:
  2633. case MIToken::kw_cfi_restore:
  2634. case MIToken::kw_cfi_restore_state:
  2635. case MIToken::kw_cfi_undefined:
  2636. case MIToken::kw_cfi_window_save:
  2637. case MIToken::kw_cfi_aarch64_negate_ra_sign_state:
  2638. return parseCFIOperand(Dest);
  2639. case MIToken::kw_blockaddress:
  2640. return parseBlockAddressOperand(Dest);
  2641. case MIToken::kw_intrinsic:
  2642. return parseIntrinsicOperand(Dest);
  2643. case MIToken::kw_target_index:
  2644. return parseTargetIndexOperand(Dest);
  2645. case MIToken::kw_liveout:
  2646. return parseLiveoutRegisterMaskOperand(Dest);
  2647. case MIToken::kw_floatpred:
  2648. case MIToken::kw_intpred:
  2649. return parsePredicateOperand(Dest);
  2650. case MIToken::kw_shufflemask:
  2651. return parseShuffleMaskOperand(Dest);
  2652. case MIToken::kw_dbg_instr_ref:
  2653. return parseDbgInstrRefOperand(Dest);
  2654. case MIToken::Error:
  2655. return true;
  2656. case MIToken::Identifier:
  2657. if (const auto *RegMask = PFS.Target.getRegMask(Token.stringValue())) {
  2658. Dest = MachineOperand::CreateRegMask(RegMask);
  2659. lex();
  2660. break;
  2661. } else if (Token.stringValue() == "CustomRegMask") {
  2662. return parseCustomRegisterMaskOperand(Dest);
  2663. } else
  2664. return parseTypedImmediateOperand(Dest);
  2665. case MIToken::dot: {
  2666. const auto *TII = MF.getSubtarget().getInstrInfo();
  2667. if (const auto *Formatter = TII->getMIRFormatter()) {
  2668. return parseTargetImmMnemonic(OpCode, OpIdx, Dest, *Formatter);
  2669. }
  2670. [[fallthrough]];
  2671. }
  2672. default:
  2673. // FIXME: Parse the MCSymbol machine operand.
  2674. return error("expected a machine operand");
  2675. }
  2676. return false;
  2677. }
  2678. bool MIParser::parseMachineOperandAndTargetFlags(
  2679. const unsigned OpCode, const unsigned OpIdx, MachineOperand &Dest,
  2680. std::optional<unsigned> &TiedDefIdx) {
  2681. unsigned TF = 0;
  2682. bool HasTargetFlags = false;
  2683. if (Token.is(MIToken::kw_target_flags)) {
  2684. HasTargetFlags = true;
  2685. lex();
  2686. if (expectAndConsume(MIToken::lparen))
  2687. return true;
  2688. if (Token.isNot(MIToken::Identifier))
  2689. return error("expected the name of the target flag");
  2690. if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
  2691. if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
  2692. return error("use of undefined target flag '" + Token.stringValue() +
  2693. "'");
  2694. }
  2695. lex();
  2696. while (Token.is(MIToken::comma)) {
  2697. lex();
  2698. if (Token.isNot(MIToken::Identifier))
  2699. return error("expected the name of the target flag");
  2700. unsigned BitFlag = 0;
  2701. if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
  2702. return error("use of undefined target flag '" + Token.stringValue() +
  2703. "'");
  2704. // TODO: Report an error when using a duplicate bit target flag.
  2705. TF |= BitFlag;
  2706. lex();
  2707. }
  2708. if (expectAndConsume(MIToken::rparen))
  2709. return true;
  2710. }
  2711. auto Loc = Token.location();
  2712. if (parseMachineOperand(OpCode, OpIdx, Dest, TiedDefIdx))
  2713. return true;
  2714. if (!HasTargetFlags)
  2715. return false;
  2716. if (Dest.isReg())
  2717. return error(Loc, "register operands can't have target flags");
  2718. Dest.setTargetFlags(TF);
  2719. return false;
  2720. }
  2721. bool MIParser::parseOffset(int64_t &Offset) {
  2722. if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
  2723. return false;
  2724. StringRef Sign = Token.range();
  2725. bool IsNegative = Token.is(MIToken::minus);
  2726. lex();
  2727. if (Token.isNot(MIToken::IntegerLiteral))
  2728. return error("expected an integer literal after '" + Sign + "'");
  2729. if (Token.integerValue().getMinSignedBits() > 64)
  2730. return error("expected 64-bit integer (too large)");
  2731. Offset = Token.integerValue().getExtValue();
  2732. if (IsNegative)
  2733. Offset = -Offset;
  2734. lex();
  2735. return false;
  2736. }
  2737. bool MIParser::parseIRBlockAddressTaken(BasicBlock *&BB) {
  2738. assert(Token.is(MIToken::kw_ir_block_address_taken));
  2739. lex();
  2740. if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
  2741. return error("expected basic block after 'ir_block_address_taken'");
  2742. if (parseIRBlock(BB, MF.getFunction()))
  2743. return true;
  2744. lex();
  2745. return false;
  2746. }
  2747. bool MIParser::parseAlignment(uint64_t &Alignment) {
  2748. assert(Token.is(MIToken::kw_align) || Token.is(MIToken::kw_basealign));
  2749. lex();
  2750. if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
  2751. return error("expected an integer literal after 'align'");
  2752. if (getUint64(Alignment))
  2753. return true;
  2754. lex();
  2755. if (!isPowerOf2_64(Alignment))
  2756. return error("expected a power-of-2 literal after 'align'");
  2757. return false;
  2758. }
  2759. bool MIParser::parseAddrspace(unsigned &Addrspace) {
  2760. assert(Token.is(MIToken::kw_addrspace));
  2761. lex();
  2762. if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
  2763. return error("expected an integer literal after 'addrspace'");
  2764. if (getUnsigned(Addrspace))
  2765. return true;
  2766. lex();
  2767. return false;
  2768. }
  2769. bool MIParser::parseOperandsOffset(MachineOperand &Op) {
  2770. int64_t Offset = 0;
  2771. if (parseOffset(Offset))
  2772. return true;
  2773. Op.setOffset(Offset);
  2774. return false;
  2775. }
  2776. static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS,
  2777. const Value *&V, ErrorCallbackType ErrCB) {
  2778. switch (Token.kind()) {
  2779. case MIToken::NamedIRValue: {
  2780. V = PFS.MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
  2781. break;
  2782. }
  2783. case MIToken::IRValue: {
  2784. unsigned SlotNumber = 0;
  2785. if (getUnsigned(Token, SlotNumber, ErrCB))
  2786. return true;
  2787. V = PFS.getIRValue(SlotNumber);
  2788. break;
  2789. }
  2790. case MIToken::NamedGlobalValue:
  2791. case MIToken::GlobalValue: {
  2792. GlobalValue *GV = nullptr;
  2793. if (parseGlobalValue(Token, PFS, GV, ErrCB))
  2794. return true;
  2795. V = GV;
  2796. break;
  2797. }
  2798. case MIToken::QuotedIRValue: {
  2799. const Constant *C = nullptr;
  2800. if (parseIRConstant(Token.location(), Token.stringValue(), PFS, C, ErrCB))
  2801. return true;
  2802. V = C;
  2803. break;
  2804. }
  2805. case MIToken::kw_unknown_address:
  2806. V = nullptr;
  2807. return false;
  2808. default:
  2809. llvm_unreachable("The current token should be an IR block reference");
  2810. }
  2811. if (!V)
  2812. return ErrCB(Token.location(), Twine("use of undefined IR value '") + Token.range() + "'");
  2813. return false;
  2814. }
  2815. bool MIParser::parseIRValue(const Value *&V) {
  2816. return ::parseIRValue(
  2817. Token, PFS, V, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
  2818. return error(Loc, Msg);
  2819. });
  2820. }
  2821. bool MIParser::getUint64(uint64_t &Result) {
  2822. if (Token.hasIntegerValue()) {
  2823. if (Token.integerValue().getActiveBits() > 64)
  2824. return error("expected 64-bit integer (too large)");
  2825. Result = Token.integerValue().getZExtValue();
  2826. return false;
  2827. }
  2828. if (Token.is(MIToken::HexLiteral)) {
  2829. APInt A;
  2830. if (getHexUint(A))
  2831. return true;
  2832. if (A.getBitWidth() > 64)
  2833. return error("expected 64-bit integer (too large)");
  2834. Result = A.getZExtValue();
  2835. return false;
  2836. }
  2837. return true;
  2838. }
  2839. bool MIParser::getHexUint(APInt &Result) {
  2840. return ::getHexUint(Token, Result);
  2841. }
  2842. bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
  2843. const auto OldFlags = Flags;
  2844. switch (Token.kind()) {
  2845. case MIToken::kw_volatile:
  2846. Flags |= MachineMemOperand::MOVolatile;
  2847. break;
  2848. case MIToken::kw_non_temporal:
  2849. Flags |= MachineMemOperand::MONonTemporal;
  2850. break;
  2851. case MIToken::kw_dereferenceable:
  2852. Flags |= MachineMemOperand::MODereferenceable;
  2853. break;
  2854. case MIToken::kw_invariant:
  2855. Flags |= MachineMemOperand::MOInvariant;
  2856. break;
  2857. case MIToken::StringConstant: {
  2858. MachineMemOperand::Flags TF;
  2859. if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
  2860. return error("use of undefined target MMO flag '" + Token.stringValue() +
  2861. "'");
  2862. Flags |= TF;
  2863. break;
  2864. }
  2865. default:
  2866. llvm_unreachable("The current token should be a memory operand flag");
  2867. }
  2868. if (OldFlags == Flags)
  2869. // We know that the same flag is specified more than once when the flags
  2870. // weren't modified.
  2871. return error("duplicate '" + Token.stringValue() + "' memory operand flag");
  2872. lex();
  2873. return false;
  2874. }
  2875. bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
  2876. switch (Token.kind()) {
  2877. case MIToken::kw_stack:
  2878. PSV = MF.getPSVManager().getStack();
  2879. break;
  2880. case MIToken::kw_got:
  2881. PSV = MF.getPSVManager().getGOT();
  2882. break;
  2883. case MIToken::kw_jump_table:
  2884. PSV = MF.getPSVManager().getJumpTable();
  2885. break;
  2886. case MIToken::kw_constant_pool:
  2887. PSV = MF.getPSVManager().getConstantPool();
  2888. break;
  2889. case MIToken::FixedStackObject: {
  2890. int FI;
  2891. if (parseFixedStackFrameIndex(FI))
  2892. return true;
  2893. PSV = MF.getPSVManager().getFixedStack(FI);
  2894. // The token was already consumed, so use return here instead of break.
  2895. return false;
  2896. }
  2897. case MIToken::StackObject: {
  2898. int FI;
  2899. if (parseStackFrameIndex(FI))
  2900. return true;
  2901. PSV = MF.getPSVManager().getFixedStack(FI);
  2902. // The token was already consumed, so use return here instead of break.
  2903. return false;
  2904. }
  2905. case MIToken::kw_call_entry:
  2906. lex();
  2907. switch (Token.kind()) {
  2908. case MIToken::GlobalValue:
  2909. case MIToken::NamedGlobalValue: {
  2910. GlobalValue *GV = nullptr;
  2911. if (parseGlobalValue(GV))
  2912. return true;
  2913. PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
  2914. break;
  2915. }
  2916. case MIToken::ExternalSymbol:
  2917. PSV = MF.getPSVManager().getExternalSymbolCallEntry(
  2918. MF.createExternalSymbolName(Token.stringValue()));
  2919. break;
  2920. default:
  2921. return error(
  2922. "expected a global value or an external symbol after 'call-entry'");
  2923. }
  2924. break;
  2925. case MIToken::kw_custom: {
  2926. lex();
  2927. const auto *TII = MF.getSubtarget().getInstrInfo();
  2928. if (const auto *Formatter = TII->getMIRFormatter()) {
  2929. if (Formatter->parseCustomPseudoSourceValue(
  2930. Token.stringValue(), MF, PFS, PSV,
  2931. [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
  2932. return error(Loc, Msg);
  2933. }))
  2934. return true;
  2935. } else
  2936. return error("unable to parse target custom pseudo source value");
  2937. break;
  2938. }
  2939. default:
  2940. llvm_unreachable("The current token should be pseudo source value");
  2941. }
  2942. lex();
  2943. return false;
  2944. }
  2945. bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
  2946. if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
  2947. Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
  2948. Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
  2949. Token.is(MIToken::kw_call_entry) || Token.is(MIToken::kw_custom)) {
  2950. const PseudoSourceValue *PSV = nullptr;
  2951. if (parseMemoryPseudoSourceValue(PSV))
  2952. return true;
  2953. int64_t Offset = 0;
  2954. if (parseOffset(Offset))
  2955. return true;
  2956. Dest = MachinePointerInfo(PSV, Offset);
  2957. return false;
  2958. }
  2959. if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
  2960. Token.isNot(MIToken::GlobalValue) &&
  2961. Token.isNot(MIToken::NamedGlobalValue) &&
  2962. Token.isNot(MIToken::QuotedIRValue) &&
  2963. Token.isNot(MIToken::kw_unknown_address))
  2964. return error("expected an IR value reference");
  2965. const Value *V = nullptr;
  2966. if (parseIRValue(V))
  2967. return true;
  2968. if (V && !V->getType()->isPointerTy())
  2969. return error("expected a pointer IR value");
  2970. lex();
  2971. int64_t Offset = 0;
  2972. if (parseOffset(Offset))
  2973. return true;
  2974. Dest = MachinePointerInfo(V, Offset);
  2975. return false;
  2976. }
  2977. bool MIParser::parseOptionalScope(LLVMContext &Context,
  2978. SyncScope::ID &SSID) {
  2979. SSID = SyncScope::System;
  2980. if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
  2981. lex();
  2982. if (expectAndConsume(MIToken::lparen))
  2983. return error("expected '(' in syncscope");
  2984. std::string SSN;
  2985. if (parseStringConstant(SSN))
  2986. return true;
  2987. SSID = Context.getOrInsertSyncScopeID(SSN);
  2988. if (expectAndConsume(MIToken::rparen))
  2989. return error("expected ')' in syncscope");
  2990. }
  2991. return false;
  2992. }
  2993. bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
  2994. Order = AtomicOrdering::NotAtomic;
  2995. if (Token.isNot(MIToken::Identifier))
  2996. return false;
  2997. Order = StringSwitch<AtomicOrdering>(Token.stringValue())
  2998. .Case("unordered", AtomicOrdering::Unordered)
  2999. .Case("monotonic", AtomicOrdering::Monotonic)
  3000. .Case("acquire", AtomicOrdering::Acquire)
  3001. .Case("release", AtomicOrdering::Release)
  3002. .Case("acq_rel", AtomicOrdering::AcquireRelease)
  3003. .Case("seq_cst", AtomicOrdering::SequentiallyConsistent)
  3004. .Default(AtomicOrdering::NotAtomic);
  3005. if (Order != AtomicOrdering::NotAtomic) {
  3006. lex();
  3007. return false;
  3008. }
  3009. return error("expected an atomic scope, ordering or a size specification");
  3010. }
  3011. bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
  3012. if (expectAndConsume(MIToken::lparen))
  3013. return true;
  3014. MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
  3015. while (Token.isMemoryOperandFlag()) {
  3016. if (parseMemoryOperandFlag(Flags))
  3017. return true;
  3018. }
  3019. if (Token.isNot(MIToken::Identifier) ||
  3020. (Token.stringValue() != "load" && Token.stringValue() != "store"))
  3021. return error("expected 'load' or 'store' memory operation");
  3022. if (Token.stringValue() == "load")
  3023. Flags |= MachineMemOperand::MOLoad;
  3024. else
  3025. Flags |= MachineMemOperand::MOStore;
  3026. lex();
  3027. // Optional 'store' for operands that both load and store.
  3028. if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
  3029. Flags |= MachineMemOperand::MOStore;
  3030. lex();
  3031. }
  3032. // Optional synchronization scope.
  3033. SyncScope::ID SSID;
  3034. if (parseOptionalScope(MF.getFunction().getContext(), SSID))
  3035. return true;
  3036. // Up to two atomic orderings (cmpxchg provides guarantees on failure).
  3037. AtomicOrdering Order, FailureOrder;
  3038. if (parseOptionalAtomicOrdering(Order))
  3039. return true;
  3040. if (parseOptionalAtomicOrdering(FailureOrder))
  3041. return true;
  3042. LLT MemoryType;
  3043. if (Token.isNot(MIToken::IntegerLiteral) &&
  3044. Token.isNot(MIToken::kw_unknown_size) &&
  3045. Token.isNot(MIToken::lparen))
  3046. return error("expected memory LLT, the size integer literal or 'unknown-size' after "
  3047. "memory operation");
  3048. uint64_t Size = MemoryLocation::UnknownSize;
  3049. if (Token.is(MIToken::IntegerLiteral)) {
  3050. if (getUint64(Size))
  3051. return true;
  3052. // Convert from bytes to bits for storage.
  3053. MemoryType = LLT::scalar(8 * Size);
  3054. lex();
  3055. } else if (Token.is(MIToken::kw_unknown_size)) {
  3056. Size = MemoryLocation::UnknownSize;
  3057. lex();
  3058. } else {
  3059. if (expectAndConsume(MIToken::lparen))
  3060. return true;
  3061. if (parseLowLevelType(Token.location(), MemoryType))
  3062. return true;
  3063. if (expectAndConsume(MIToken::rparen))
  3064. return true;
  3065. Size = MemoryType.getSizeInBytes();
  3066. }
  3067. MachinePointerInfo Ptr = MachinePointerInfo();
  3068. if (Token.is(MIToken::Identifier)) {
  3069. const char *Word =
  3070. ((Flags & MachineMemOperand::MOLoad) &&
  3071. (Flags & MachineMemOperand::MOStore))
  3072. ? "on"
  3073. : Flags & MachineMemOperand::MOLoad ? "from" : "into";
  3074. if (Token.stringValue() != Word)
  3075. return error(Twine("expected '") + Word + "'");
  3076. lex();
  3077. if (parseMachinePointerInfo(Ptr))
  3078. return true;
  3079. }
  3080. uint64_t BaseAlignment =
  3081. (Size != MemoryLocation::UnknownSize ? PowerOf2Ceil(Size) : 1);
  3082. AAMDNodes AAInfo;
  3083. MDNode *Range = nullptr;
  3084. while (consumeIfPresent(MIToken::comma)) {
  3085. switch (Token.kind()) {
  3086. case MIToken::kw_align: {
  3087. // align is printed if it is different than size.
  3088. uint64_t Alignment;
  3089. if (parseAlignment(Alignment))
  3090. return true;
  3091. if (Ptr.Offset & (Alignment - 1)) {
  3092. // MachineMemOperand::getAlign never returns a value greater than the
  3093. // alignment of offset, so this just guards against hand-written MIR
  3094. // that specifies a large "align" value when it should probably use
  3095. // "basealign" instead.
  3096. return error("specified alignment is more aligned than offset");
  3097. }
  3098. BaseAlignment = Alignment;
  3099. break;
  3100. }
  3101. case MIToken::kw_basealign:
  3102. // basealign is printed if it is different than align.
  3103. if (parseAlignment(BaseAlignment))
  3104. return true;
  3105. break;
  3106. case MIToken::kw_addrspace:
  3107. if (parseAddrspace(Ptr.AddrSpace))
  3108. return true;
  3109. break;
  3110. case MIToken::md_tbaa:
  3111. lex();
  3112. if (parseMDNode(AAInfo.TBAA))
  3113. return true;
  3114. break;
  3115. case MIToken::md_alias_scope:
  3116. lex();
  3117. if (parseMDNode(AAInfo.Scope))
  3118. return true;
  3119. break;
  3120. case MIToken::md_noalias:
  3121. lex();
  3122. if (parseMDNode(AAInfo.NoAlias))
  3123. return true;
  3124. break;
  3125. case MIToken::md_range:
  3126. lex();
  3127. if (parseMDNode(Range))
  3128. return true;
  3129. break;
  3130. // TODO: Report an error on duplicate metadata nodes.
  3131. default:
  3132. return error("expected 'align' or '!tbaa' or '!alias.scope' or "
  3133. "'!noalias' or '!range'");
  3134. }
  3135. }
  3136. if (expectAndConsume(MIToken::rparen))
  3137. return true;
  3138. Dest = MF.getMachineMemOperand(Ptr, Flags, MemoryType, Align(BaseAlignment),
  3139. AAInfo, Range, SSID, Order, FailureOrder);
  3140. return false;
  3141. }
  3142. bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
  3143. assert((Token.is(MIToken::kw_pre_instr_symbol) ||
  3144. Token.is(MIToken::kw_post_instr_symbol)) &&
  3145. "Invalid token for a pre- post-instruction symbol!");
  3146. lex();
  3147. if (Token.isNot(MIToken::MCSymbol))
  3148. return error("expected a symbol after 'pre-instr-symbol'");
  3149. Symbol = getOrCreateMCSymbol(Token.stringValue());
  3150. lex();
  3151. if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
  3152. Token.is(MIToken::lbrace))
  3153. return false;
  3154. if (Token.isNot(MIToken::comma))
  3155. return error("expected ',' before the next machine operand");
  3156. lex();
  3157. return false;
  3158. }
  3159. bool MIParser::parseHeapAllocMarker(MDNode *&Node) {
  3160. assert(Token.is(MIToken::kw_heap_alloc_marker) &&
  3161. "Invalid token for a heap alloc marker!");
  3162. lex();
  3163. parseMDNode(Node);
  3164. if (!Node)
  3165. return error("expected a MDNode after 'heap-alloc-marker'");
  3166. if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
  3167. Token.is(MIToken::lbrace))
  3168. return false;
  3169. if (Token.isNot(MIToken::comma))
  3170. return error("expected ',' before the next machine operand");
  3171. lex();
  3172. return false;
  3173. }
  3174. bool MIParser::parsePCSections(MDNode *&Node) {
  3175. assert(Token.is(MIToken::kw_pcsections) &&
  3176. "Invalid token for a PC sections!");
  3177. lex();
  3178. parseMDNode(Node);
  3179. if (!Node)
  3180. return error("expected a MDNode after 'pcsections'");
  3181. if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
  3182. Token.is(MIToken::lbrace))
  3183. return false;
  3184. if (Token.isNot(MIToken::comma))
  3185. return error("expected ',' before the next machine operand");
  3186. lex();
  3187. return false;
  3188. }
  3189. static void initSlots2BasicBlocks(
  3190. const Function &F,
  3191. DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
  3192. ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
  3193. MST.incorporateFunction(F);
  3194. for (const auto &BB : F) {
  3195. if (BB.hasName())
  3196. continue;
  3197. int Slot = MST.getLocalSlot(&BB);
  3198. if (Slot == -1)
  3199. continue;
  3200. Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
  3201. }
  3202. }
  3203. static const BasicBlock *getIRBlockFromSlot(
  3204. unsigned Slot,
  3205. const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
  3206. return Slots2BasicBlocks.lookup(Slot);
  3207. }
  3208. const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
  3209. if (Slots2BasicBlocks.empty())
  3210. initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
  3211. return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
  3212. }
  3213. const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
  3214. if (&F == &MF.getFunction())
  3215. return getIRBlock(Slot);
  3216. DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
  3217. initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
  3218. return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
  3219. }
  3220. MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
  3221. // FIXME: Currently we can't recognize temporary or local symbols and call all
  3222. // of the appropriate forms to create them. However, this handles basic cases
  3223. // well as most of the special aspects are recognized by a prefix on their
  3224. // name, and the input names should already be unique. For test cases, keeping
  3225. // the symbol name out of the symbol table isn't terribly important.
  3226. return MF.getContext().getOrCreateSymbol(Name);
  3227. }
  3228. bool MIParser::parseStringConstant(std::string &Result) {
  3229. if (Token.isNot(MIToken::StringConstant))
  3230. return error("expected string constant");
  3231. Result = std::string(Token.stringValue());
  3232. lex();
  3233. return false;
  3234. }
  3235. bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
  3236. StringRef Src,
  3237. SMDiagnostic &Error) {
  3238. return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
  3239. }
  3240. bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS,
  3241. StringRef Src, SMDiagnostic &Error) {
  3242. return MIParser(PFS, Error, Src).parseBasicBlocks();
  3243. }
  3244. bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS,
  3245. MachineBasicBlock *&MBB, StringRef Src,
  3246. SMDiagnostic &Error) {
  3247. return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
  3248. }
  3249. bool llvm::parseRegisterReference(PerFunctionMIParsingState &PFS,
  3250. Register &Reg, StringRef Src,
  3251. SMDiagnostic &Error) {
  3252. return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
  3253. }
  3254. bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS,
  3255. Register &Reg, StringRef Src,
  3256. SMDiagnostic &Error) {
  3257. return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
  3258. }
  3259. bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
  3260. VRegInfo *&Info, StringRef Src,
  3261. SMDiagnostic &Error) {
  3262. return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
  3263. }
  3264. bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS,
  3265. int &FI, StringRef Src,
  3266. SMDiagnostic &Error) {
  3267. return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
  3268. }
  3269. bool llvm::parseMDNode(PerFunctionMIParsingState &PFS,
  3270. MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
  3271. return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
  3272. }
  3273. bool llvm::parseMachineMetadata(PerFunctionMIParsingState &PFS, StringRef Src,
  3274. SMRange SrcRange, SMDiagnostic &Error) {
  3275. return MIParser(PFS, Error, Src, SrcRange).parseMachineMetadata();
  3276. }
  3277. bool MIRFormatter::parseIRValue(StringRef Src, MachineFunction &MF,
  3278. PerFunctionMIParsingState &PFS, const Value *&V,
  3279. ErrorCallbackType ErrorCallback) {
  3280. MIToken Token;
  3281. Src = lexMIToken(Src, Token, [&](StringRef::iterator Loc, const Twine &Msg) {
  3282. ErrorCallback(Loc, Msg);
  3283. });
  3284. V = nullptr;
  3285. return ::parseIRValue(Token, PFS, V, ErrorCallback);
  3286. }