BeModule.cpp 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624
  1. #include "BeModule.h"
  2. #include "../Beef/BfCommon.h"
  3. #include "../Compiler/BfUtil.h"
  4. #include "BeefySysLib/util/BeefPerf.h"
  5. #include "../Compiler/BfIRCodeGen.h"
  6. #include "BeefySysLib/util/AllocDebug.h"
  7. USING_NS_BF;
  8. //////////////////////////////////////////////////////////////////////////
  9. void bpt(Beefy::BeType* t)
  10. {
  11. Beefy::String str;
  12. if (t->IsStruct())
  13. BeModule::StructToString(str, (BeStructType*)t);
  14. else
  15. {
  16. BeModule::ToString(str, t);
  17. str += "\n";
  18. }
  19. OutputDebugStrF("%s", str.c_str());
  20. if (t->IsPointer())
  21. {
  22. bpt(((BePointerType*)t)->mElementType);
  23. }
  24. }
  25. void bpv(Beefy::BeValue* val)
  26. {
  27. BeDumpContext dumpCtx;
  28. String str;
  29. dumpCtx.ToString(str, val);
  30. str += "\n";
  31. OutputDebugStr(str);
  32. auto type = val->GetType();
  33. if (type != NULL)
  34. bpt(type);
  35. }
  36. //////////////////////////////////////////////////////////////////////////
  37. void BeValueVisitor::VisitChild(BeValue* value)
  38. {
  39. if (value != NULL)
  40. value->Accept(this);
  41. }
  42. BeValue* BeInliner::Remap(BeValue* srcValue)
  43. {
  44. if (srcValue == NULL)
  45. return NULL;
  46. // Try to unwrap mdnode
  47. if (auto inlinedScope = BeValueDynCast<BeDbgInlinedScope>(srcValue))
  48. srcValue = inlinedScope->mScope;
  49. BeValue** valuePtr = NULL;
  50. /*auto itr = mValueMap.find(srcValue);
  51. if (itr != mValueMap.end())
  52. return itr->second;*/
  53. if (mValueMap.TryGetValue(srcValue, &valuePtr))
  54. return *valuePtr;
  55. BeMDNode* wrapMDNode = NULL;
  56. if (auto dbgFunction = BeValueDynCast<BeDbgFunction>(srcValue))
  57. {
  58. wrapMDNode = dbgFunction;
  59. }
  60. if (auto dbgLexBlock = BeValueDynCast<BeDbgLexicalBlock>(srcValue))
  61. {
  62. wrapMDNode = dbgLexBlock;
  63. }
  64. if (auto callInst = BeValueDynCast<BeCallInst>(srcValue))
  65. {
  66. if (callInst->mInlineResult != NULL)
  67. return Remap(callInst->mInlineResult);
  68. }
  69. if (wrapMDNode != NULL)
  70. {
  71. auto destMDNode = mOwnedValueVec->Alloc<BeDbgInlinedScope>();
  72. destMDNode->mScope = wrapMDNode;
  73. mValueMap[srcValue] = destMDNode;
  74. return destMDNode;
  75. }
  76. return srcValue;
  77. }
  78. // Insert 'mCallInst->mDbgLoc' at the head of the inline chain
  79. BeDbgLoc* BeInliner::ExtendInlineDbgLoc(BeDbgLoc* srcInlineAt)
  80. {
  81. if (srcInlineAt == NULL)
  82. return mCallInst->mDbgLoc;
  83. /*auto itr = mInlinedAtMap.find(srcInlineAt);
  84. if (itr != mInlinedAtMap.end())
  85. {
  86. return itr->second;
  87. }*/
  88. BeDbgLoc** dbgLocPtr = NULL;
  89. if (mInlinedAtMap.TryGetValue(srcInlineAt, &dbgLocPtr))
  90. return *dbgLocPtr;
  91. auto dbgLoc = mModule->mAlloc.Alloc<BeDbgLoc>();
  92. dbgLoc->mLine = srcInlineAt->mLine;
  93. dbgLoc->mColumn = srcInlineAt->mColumn;
  94. dbgLoc->mDbgScope = (BeMDNode*)Remap(srcInlineAt->mDbgScope);
  95. dbgLoc->mIdx = mModule->mCurDbgLocIdx++;
  96. dbgLoc->mDbgInlinedAt = ExtendInlineDbgLoc(srcInlineAt->mDbgInlinedAt);
  97. mInlinedAtMap[srcInlineAt] = dbgLoc;
  98. return dbgLoc;
  99. }
  100. void BeInliner::AddInst(BeInst* destInst, BeInst* srcInst)
  101. {
  102. if ((srcInst != NULL) && (srcInst->mDbgLoc != mSrcDbgLoc))
  103. {
  104. mSrcDbgLoc = srcInst->mDbgLoc;
  105. if (mSrcDbgLoc == NULL)
  106. {
  107. mDestDbgLoc = NULL;
  108. }
  109. else
  110. {
  111. BeDbgLoc* inlinedAt = ExtendInlineDbgLoc(mSrcDbgLoc->mDbgInlinedAt);
  112. mModule->SetCurrentDebugLocation(mSrcDbgLoc->mLine, mSrcDbgLoc->mColumn, (BeMDNode*)Remap(mSrcDbgLoc->mDbgScope), inlinedAt);
  113. mDestDbgLoc = mModule->mCurDbgLoc;
  114. }
  115. }
  116. if (srcInst != NULL)
  117. destInst->mName = srcInst->mName;
  118. destInst->mDbgLoc = mDestDbgLoc;
  119. destInst->mParentBlock = mDestBlock;
  120. mDestBlock->mInstructions.push_back(destInst);
  121. if (srcInst != NULL)
  122. mValueMap[srcInst] = destInst;
  123. }
  124. void BeInliner::Visit(BeValue* beValue)
  125. {
  126. }
  127. void BeInliner::Visit(BeBlock* beBlock)
  128. {
  129. }
  130. void BeInliner::Visit(BeArgument* beArgument)
  131. {
  132. }
  133. void BeInliner::Visit(BeInst* beInst)
  134. {
  135. BF_FATAL("Not handled");
  136. }
  137. void BeInliner::Visit(BeNopInst* nopInst)
  138. {
  139. auto destNopInst = AllocInst(nopInst);
  140. }
  141. void BeInliner::Visit(BeUnreachableInst* unreachableInst)
  142. {
  143. auto destUnreachableInst = AllocInst(unreachableInst);
  144. }
  145. void BeInliner::Visit(BeEnsureInstructionAtInst* ensureCodeAtInst)
  146. {
  147. auto destEnsureCodeAtInst = AllocInst(ensureCodeAtInst);
  148. }
  149. void BeInliner::Visit(BeUndefValueInst* undefValue)
  150. {
  151. auto destUndefValue = AllocInst(undefValue);
  152. destUndefValue->mType = undefValue->mType;
  153. }
  154. void BeInliner::Visit(BeExtractValueInst* extractValue)
  155. {
  156. auto destExtractValue = AllocInst(extractValue);
  157. destExtractValue->mAggVal = Remap(extractValue->mAggVal);
  158. destExtractValue->mIdx = extractValue->mIdx;
  159. }
  160. void BeInliner::Visit(BeInsertValueInst* insertValue)
  161. {
  162. auto destInsertValue = AllocInst(insertValue);
  163. destInsertValue->mAggVal = Remap(insertValue->mAggVal);
  164. destInsertValue->mMemberVal = Remap(insertValue->mMemberVal);
  165. destInsertValue->mIdx = insertValue->mIdx;
  166. }
  167. void BeInliner::Visit(BeNumericCastInst* castInst)
  168. {
  169. auto destCastInset = AllocInst(castInst);
  170. destCastInset->mValue = Remap(castInst->mValue);
  171. destCastInset->mToType = castInst->mToType;
  172. destCastInset->mValSigned = castInst->mValSigned;
  173. destCastInset->mToSigned = castInst->mToSigned;
  174. }
  175. void BeInliner::Visit(BeBitCastInst* castInst)
  176. {
  177. auto destCastInst = AllocInst(castInst);
  178. destCastInst->mValue = Remap(castInst->mValue);
  179. destCastInst->mToType = castInst->mToType;
  180. }
  181. void BeInliner::Visit(BeNegInst* negInst)
  182. {
  183. auto destNegInst = AllocInst(negInst);
  184. destNegInst->mValue = Remap(negInst->mValue);
  185. }
  186. void BeInliner::Visit(BeNotInst* notInst)
  187. {
  188. auto destNotInst = AllocInst(notInst);
  189. destNotInst->mValue = Remap(notInst->mValue);
  190. }
  191. void BeInliner::Visit(BeBinaryOpInst* binaryOpInst)
  192. {
  193. auto destBinaryOp = AllocInst(binaryOpInst);
  194. destBinaryOp->mOpKind = binaryOpInst->mOpKind;
  195. destBinaryOp->mLHS = Remap(binaryOpInst->mLHS);
  196. destBinaryOp->mRHS = Remap(binaryOpInst->mRHS);
  197. }
  198. void BeInliner::Visit(BeCmpInst* cmpInst)
  199. {
  200. auto destCmpInst = AllocInst(cmpInst);
  201. destCmpInst->mCmpKind = cmpInst->mCmpKind;
  202. destCmpInst->mLHS = Remap(cmpInst->mLHS);
  203. destCmpInst->mRHS = Remap(cmpInst->mRHS);
  204. }
  205. void BeInliner::Visit(BeFenceInst* fenceInst)
  206. {
  207. auto destFenceInst = AllocInst(fenceInst);
  208. }
  209. void BeInliner::Visit(BeStackSaveInst* stackSaveInst)
  210. {
  211. auto destStackSaveInst = AllocInst(stackSaveInst);
  212. }
  213. void BeInliner::Visit(BeStackRestoreInst* stackRestoreInst)
  214. {
  215. auto destStackRestoreInst = AllocInst(stackRestoreInst);
  216. destStackRestoreInst->mStackVal = Remap(stackRestoreInst);
  217. }
  218. void BeInliner::Visit(BeObjectAccessCheckInst* objectAccessCheckInst)
  219. {
  220. auto destObjectAccessCheckInst = AllocInst(objectAccessCheckInst);
  221. destObjectAccessCheckInst->mValue = Remap(objectAccessCheckInst->mValue);
  222. }
  223. void BeInliner::Visit(BeAllocaInst* allocaInst)
  224. {
  225. auto destAllocInst = AllocInst(allocaInst);
  226. destAllocInst->mType = allocaInst->mType;
  227. destAllocInst->mArraySize = Remap(allocaInst->mArraySize);
  228. destAllocInst->mAlign = allocaInst->mAlign;
  229. destAllocInst->mNoChkStk = allocaInst->mNoChkStk;
  230. destAllocInst->mForceMem = allocaInst->mForceMem;
  231. }
  232. void BeInliner::Visit(BeAliasValueInst* aliasValueInst)
  233. {
  234. auto destlifetimeStartInst = AllocInst(aliasValueInst);
  235. destlifetimeStartInst->mPtr = Remap(aliasValueInst->mPtr);
  236. }
  237. void BeInliner::Visit(BeLifetimeExtendInst* lifetimeExtendInst)
  238. {
  239. auto destlifetimeExtendInst = AllocInst(lifetimeExtendInst);
  240. destlifetimeExtendInst->mPtr = Remap(lifetimeExtendInst->mPtr);
  241. }
  242. void BeInliner::Visit(BeLifetimeStartInst* lifetimeStartInst)
  243. {
  244. auto destlifetimeStartInst = AllocInst(lifetimeStartInst);
  245. destlifetimeStartInst->mPtr = Remap(lifetimeStartInst->mPtr);
  246. }
  247. void BeInliner::Visit(BeLifetimeEndInst* lifetimeEndInst)
  248. {
  249. auto destlifetimeEndInst = AllocInst(lifetimeEndInst);
  250. destlifetimeEndInst->mPtr = Remap(lifetimeEndInst->mPtr);
  251. }
  252. void BeInliner::Visit(BeLifetimeFenceInst* lifetimeFenceInst)
  253. {
  254. auto destlifetimeFenceInst = AllocInst(lifetimeFenceInst);
  255. destlifetimeFenceInst->mFenceBlock = (BeBlock*)Remap(lifetimeFenceInst->mFenceBlock);
  256. destlifetimeFenceInst->mPtr = Remap(lifetimeFenceInst->mPtr);
  257. }
  258. void BeInliner::Visit(BeValueScopeStartInst* valueScopeStartInst)
  259. {
  260. auto destValueScopeStartInst = AllocInst(valueScopeStartInst);
  261. }
  262. void BeInliner::Visit(BeValueScopeRetainInst* valueScopeRetainInst)
  263. {
  264. auto destValueScopeRetainInst = AllocInst(valueScopeRetainInst);
  265. destValueScopeRetainInst->mValue = Remap(valueScopeRetainInst->mValue);
  266. }
  267. void BeInliner::Visit(BeValueScopeEndInst* valueScopeEndInst)
  268. {
  269. auto destValueScopeEndInst = AllocInst(valueScopeEndInst);
  270. destValueScopeEndInst->mScopeStart = (BeValueScopeStartInst*)Remap(valueScopeEndInst->mScopeStart);
  271. destValueScopeEndInst->mIsSoft = valueScopeEndInst->mIsSoft;
  272. }
  273. void BeInliner::Visit(BeLoadInst* loadInst)
  274. {
  275. auto destLoadInst = AllocInst(loadInst);
  276. destLoadInst->mTarget = Remap(loadInst->mTarget);
  277. }
  278. void BeInliner::Visit(BeStoreInst* storeInst)
  279. {
  280. auto destStoreInst = AllocInst(storeInst);
  281. destStoreInst->mPtr = Remap(storeInst->mPtr);
  282. destStoreInst->mVal = Remap(storeInst->mVal);
  283. }
  284. void BeInliner::Visit(BeSetCanMergeInst* setCanMergeInst)
  285. {
  286. auto destSetCanMergeInst = AllocInst(setCanMergeInst);
  287. destSetCanMergeInst->mVal = Remap(setCanMergeInst->mVal);
  288. }
  289. void BeInliner::Visit(BeMemSetInst* memSetInst)
  290. {
  291. auto destMemSetInst = AllocInst(memSetInst);
  292. destMemSetInst->mAddr = Remap(memSetInst->mAddr);
  293. destMemSetInst->mVal = Remap(memSetInst->mVal);
  294. destMemSetInst->mSize = Remap(memSetInst->mSize);
  295. destMemSetInst->mAlignment = memSetInst->mAlignment;
  296. }
  297. void BeInliner::Visit(BeGEPInst* gepInst)
  298. {
  299. auto destGEPInst = AllocInst(gepInst);
  300. destGEPInst->mPtr = Remap(gepInst->mPtr);
  301. destGEPInst->mIdx0 = Remap(gepInst->mIdx0);
  302. destGEPInst->mIdx1 = Remap(gepInst->mIdx1);
  303. }
  304. void BeInliner::Visit(BeBrInst* brInst)
  305. {
  306. auto destBrInst = AllocInst(brInst);
  307. destBrInst->mTargetBlock = (BeBlock*)Remap(brInst->mTargetBlock);
  308. destBrInst->mNoCollapse = brInst->mNoCollapse;
  309. destBrInst->mIsFake = brInst->mIsFake;
  310. }
  311. void BeInliner::Visit(BeCondBrInst* condBrInst)
  312. {
  313. auto destCondBrInst = AllocInst(condBrInst);
  314. destCondBrInst->mCond = Remap(condBrInst->mCond);
  315. destCondBrInst->mTrueBlock = (BeBlock*)Remap(condBrInst->mTrueBlock);
  316. destCondBrInst->mFalseBlock = (BeBlock*)Remap(condBrInst->mFalseBlock);
  317. }
  318. void BeInliner::Visit(BePhiIncoming* phiIncomingInst)
  319. {
  320. }
  321. void BeInliner::Visit(BePhiInst* phiInst)
  322. {
  323. auto destPhiInst = AllocInst(phiInst);
  324. for (auto incoming : phiInst->mIncoming)
  325. {
  326. auto destPhiIncoming = mAlloc->Alloc<BePhiIncoming>();
  327. destPhiIncoming->mValue = Remap(incoming->mValue);
  328. destPhiIncoming->mBlock = (BeBlock*)Remap(incoming->mBlock);
  329. destPhiInst->mIncoming.push_back(destPhiIncoming);
  330. }
  331. destPhiInst->mType = phiInst->mType;
  332. }
  333. void BeInliner::Visit(BeSwitchInst* switchInst)
  334. {
  335. auto destSwitchInst = AllocInstOwned(switchInst);
  336. destSwitchInst->mValue = Remap(switchInst->mValue);
  337. destSwitchInst->mDefaultBlock = (BeBlock*)Remap(switchInst->mDefaultBlock);
  338. for (auto& switchCase : switchInst->mCases)
  339. {
  340. BeSwitchCase destSwitchCase;
  341. destSwitchCase.mBlock = (BeBlock*)Remap(switchCase.mBlock);
  342. destSwitchCase.mValue = switchCase.mValue;
  343. destSwitchInst->mCases.push_back(destSwitchCase);
  344. }
  345. }
  346. void BeInliner::Visit(BeRetInst* retInst)
  347. {
  348. auto destRetInst = AllocInst(retInst);
  349. destRetInst->mRetValue = Remap(retInst->mRetValue);
  350. }
  351. void BeInliner::Visit(BeCallInst* callInst)
  352. {
  353. auto destCallInst = AllocInstOwned(callInst);
  354. destCallInst->mFunc = Remap(callInst->mFunc);
  355. for (auto& arg : callInst->mArgs)
  356. {
  357. auto copiedArg = arg;
  358. copiedArg.mValue = Remap(arg.mValue);
  359. destCallInst->mArgs.push_back(copiedArg);
  360. }
  361. destCallInst->mNoReturn = callInst->mNoReturn;
  362. destCallInst->mTailCall = callInst->mTailCall;
  363. }
  364. void BeInliner::Visit(BeDbgDeclareInst* dbgDeclareInst)
  365. {
  366. auto destDbgDeclareInst = AllocInst(dbgDeclareInst);
  367. destDbgDeclareInst->mDbgVar = (BeDbgVariable*)Remap(dbgDeclareInst->mDbgVar);
  368. destDbgDeclareInst->mValue = Remap(dbgDeclareInst->mValue);
  369. }
  370. //////////////////////////////////////////////////////////////////////////
  371. BeType* BeConstant::GetType()
  372. {
  373. return mType;
  374. }
  375. void BeConstant::GetData(BeConstData& data)
  376. {
  377. auto type = GetType();
  378. while ((((int)data.mData.size()) % type->mAlign) != 0)
  379. data.mData.push_back(0);
  380. if (type->IsComposite())
  381. {
  382. for (int i = 0; i < type->mSize; i++)
  383. data.mData.push_back(0); // Aggregate
  384. }
  385. else if (type->mTypeCode == BeTypeCode_Float)
  386. {
  387. float f = mDouble;
  388. data.mData.Insert(data.mData.mSize, (uint8*)&f, sizeof(float));
  389. }
  390. else
  391. {
  392. data.mData.Insert(data.mData.mSize, &mUInt8, type->mSize);
  393. }
  394. }
  395. void BeConstant::HashContent(BeHashContext& hashCtx)
  396. {
  397. hashCtx.Mixin(TypeId);
  398. mType->HashReference(hashCtx);
  399. if (mType->mTypeCode < BeTypeCode_Struct)
  400. {
  401. hashCtx.Mixin(mUInt64);
  402. }
  403. else if (mType->IsPointer())
  404. {
  405. if (mTarget != NULL)
  406. mTarget->HashReference(hashCtx);
  407. else
  408. hashCtx.Mixin(-1);
  409. }
  410. else if (mType->IsComposite())
  411. {
  412. // Zero-init
  413. BF_ASSERT(mTarget == NULL);
  414. }
  415. else
  416. BF_FATAL("NotImpl");
  417. }
  418. void BeStructConstant::GetData(BeConstData& data)
  419. {
  420. for (auto val : mMemberValues)
  421. val->GetData(data);
  422. }
  423. BeType* BeGEP1Constant::GetType()
  424. {
  425. BePointerType* ptrType = (BePointerType*)mTarget->GetType();
  426. return ptrType;
  427. }
  428. BeType* BeGEP2Constant::GetType()
  429. {
  430. BePointerType* ptrType = (BePointerType*)mTarget->GetType();
  431. BF_ASSERT(ptrType->mTypeCode == BeTypeCode_Pointer);
  432. if (ptrType->mElementType->mTypeCode == BeTypeCode_SizedArray)
  433. {
  434. BeSizedArrayType* arrayType = (BeSizedArrayType*)ptrType->mElementType;
  435. BF_ASSERT(arrayType->mTypeCode == BeTypeCode_SizedArray);
  436. return arrayType->mContext->GetPointerTo(arrayType->mElementType);
  437. }
  438. else if (ptrType->mElementType->mTypeCode == BeTypeCode_Vector)
  439. {
  440. BeVectorType* arrayType = (BeVectorType*)ptrType->mElementType;
  441. BF_ASSERT(arrayType->mTypeCode == BeTypeCode_Vector);
  442. return arrayType->mContext->GetPointerTo(arrayType->mElementType);
  443. }
  444. /*else if (ptrType->mElementType->IsPointer())
  445. {
  446. return ptrType->mElementType;
  447. }*/
  448. else
  449. {
  450. BeStructType* structType = (BeStructType*)ptrType->mElementType;
  451. BF_ASSERT(structType->mTypeCode == BeTypeCode_Struct);
  452. return structType->mContext->GetPointerTo(structType->mMembers[mIdx1].mType);
  453. }
  454. }
  455. BeType* BeExtractValueConstant::GetType()
  456. {
  457. BeType* type = mTarget->GetType();
  458. if (type->mTypeCode == BeTypeCode_SizedArray)
  459. {
  460. BeSizedArrayType* arrayType = (BeSizedArrayType*)type;
  461. BF_ASSERT(arrayType->mTypeCode == BeTypeCode_SizedArray);
  462. return arrayType->mElementType;
  463. }
  464. else if (type->mTypeCode == BeTypeCode_Vector)
  465. {
  466. BeVectorType* arrayType = (BeVectorType*)type;
  467. BF_ASSERT(arrayType->mTypeCode == BeTypeCode_Vector);
  468. return arrayType->mElementType;
  469. }
  470. /*else if (ptrType->mElementType->IsPointer())
  471. {
  472. return ptrType->mElementType;
  473. }*/
  474. else
  475. {
  476. BeStructType* structType = (BeStructType*)type;
  477. BF_ASSERT(structType->mTypeCode == BeTypeCode_Struct);
  478. return structType->mMembers[mIdx0].mType;
  479. }
  480. }
  481. BeType* BeGlobalVariable::GetType()
  482. {
  483. //if (mIsConstant)
  484. //return mType;
  485. return mModule->mContext->GetPointerTo(mType);
  486. }
  487. void BeFunction::HashContent(BeHashContext& hashCtx)
  488. {
  489. hashCtx.Mixin(TypeId);
  490. hashCtx.MixinStr(mName);
  491. hashCtx.Mixin(mLinkageType);
  492. hashCtx.Mixin(mAlwaysInline);
  493. hashCtx.Mixin(mNoUnwind);
  494. hashCtx.Mixin(mUWTable);
  495. hashCtx.Mixin(mNoReturn);
  496. hashCtx.Mixin(mNoFramePointerElim);
  497. hashCtx.Mixin(mIsDLLExport);
  498. hashCtx.Mixin(mIsDLLImport);
  499. hashCtx.Mixin(mCallingConv);
  500. for (auto block : mBlocks)
  501. block->HashReference(hashCtx);
  502. for (auto& param : mParams)
  503. {
  504. hashCtx.MixinStr(param.mName);
  505. hashCtx.Mixin(param.mNoAlias);
  506. hashCtx.Mixin(param.mNoCapture);
  507. hashCtx.Mixin(param.mStructRet);
  508. hashCtx.Mixin(param.mZExt);
  509. hashCtx.Mixin(param.mDereferenceableSize);
  510. hashCtx.Mixin(param.mByValSize);
  511. }
  512. if (mDbgFunction != NULL)
  513. mDbgFunction->HashReference(hashCtx);
  514. if (mRemapBindVar != NULL)
  515. mRemapBindVar->HashReference(hashCtx);
  516. }
  517. void BeBlock::HashContent(BeHashContext& hashCtx)
  518. {
  519. hashCtx.Mixin(TypeId);
  520. hashCtx.MixinStr(mName);
  521. hashCtx.Mixin(mInstructions.size());
  522. for (auto inst : mInstructions)
  523. inst->HashReference(hashCtx);
  524. }
  525. void BeInst::HashContent(BeHashContext& hashCtx)
  526. {
  527. HashInst(hashCtx);
  528. if (mName != NULL)
  529. hashCtx.MixinStr(mName);
  530. if (mDbgLoc != NULL)
  531. mDbgLoc->HashReference(hashCtx);
  532. }
  533. BeType* BeNumericCastInst::GetType()
  534. {
  535. return mToType;
  536. }
  537. BeType* BeBitCastInst::GetType()
  538. {
  539. return mToType;
  540. }
  541. BeType* BeNegInst::GetType()
  542. {
  543. return mValue->GetType();
  544. }
  545. BeType* BeNotInst::GetType()
  546. {
  547. return mValue->GetType();
  548. }
  549. BeType* BeBinaryOpInst::GetType()
  550. {
  551. return mLHS->GetType();
  552. }
  553. BeContext* BeInst::GetContext()
  554. {
  555. return mParentBlock->mFunction->mModule->mContext;
  556. }
  557. BeModule* BeInst::GetModule()
  558. {
  559. return mParentBlock->mFunction->mModule;
  560. }
  561. void BeInst::SetName(const StringImpl& name)
  562. {
  563. char* nameStr = (char*)GetModule()->mAlloc.AllocBytes((int)name.length() + 1);
  564. strcpy(nameStr, name.c_str());
  565. mName = nameStr;
  566. }
  567. BeType* BeLoadInst::GetType()
  568. {
  569. auto type = mTarget->GetType();
  570. BF_ASSERT(type->mTypeCode == BeTypeCode_Pointer);
  571. BePointerType* pointerType = (BePointerType*)type;
  572. return pointerType->mElementType;
  573. }
  574. BeType* BeUndefValueInst::GetType()
  575. {
  576. return mType;
  577. }
  578. BeType* BeExtractValueInst::GetType()
  579. {
  580. auto aggType = mAggVal->GetType();
  581. if (aggType->mTypeCode == BeTypeCode_SizedArray)
  582. {
  583. BeSizedArrayType* arrayType = (BeSizedArrayType*)aggType;
  584. return arrayType->mElementType;
  585. }
  586. if (aggType->mTypeCode == BeTypeCode_Vector)
  587. {
  588. BeVectorType* arrayType = (BeVectorType*)aggType;
  589. return arrayType->mElementType;
  590. }
  591. BF_ASSERT(aggType->mTypeCode == BeTypeCode_Struct);
  592. BeStructType* structType = (BeStructType*)aggType;
  593. return structType->mMembers[mIdx].mType;
  594. }
  595. BeType* BeInsertValueInst::GetType()
  596. {
  597. return mAggVal->GetType();
  598. }
  599. BeType* BeCmpInst::GetType()
  600. {
  601. return GetContext()->GetPrimitiveType(BeTypeCode_Boolean);
  602. }
  603. BeType* BeAllocaInst::GetType()
  604. {
  605. auto context = GetContext();
  606. return context->GetPointerTo(mType);
  607. }
  608. BeType * BeValueScopeStartInst::GetType()
  609. {
  610. auto context = GetContext();
  611. return context->GetPrimitiveType(BeTypeCode_Int32);
  612. }
  613. BeType* BeGEPInst::GetType()
  614. {
  615. if (mIdx1 == NULL)
  616. return mPtr->GetType();
  617. BePointerType* ptrType = (BePointerType*)mPtr->GetType();
  618. BF_ASSERT(ptrType->mTypeCode == BeTypeCode_Pointer);
  619. auto elementType = ptrType->mElementType;
  620. if (elementType->IsStruct())
  621. {
  622. BeStructType* structType = (BeStructType*)ptrType->mElementType;
  623. BF_ASSERT(structType->mTypeCode == BeTypeCode_Struct);
  624. auto constIdx1 = BeValueDynCast<BeConstant>(mIdx1);
  625. return GetContext()->GetPointerTo(structType->mMembers[constIdx1->mInt64].mType);
  626. }
  627. else if (elementType->IsSizedArray())
  628. {
  629. BeSizedArrayType* arrayType = (BeSizedArrayType*)ptrType->mElementType;
  630. return GetContext()->GetPointerTo(arrayType->mElementType);
  631. }
  632. else if (elementType->IsVector())
  633. {
  634. BeVectorType* arrayType = (BeVectorType*)ptrType->mElementType;
  635. return GetContext()->GetPointerTo(arrayType->mElementType);
  636. }
  637. else
  638. {
  639. BF_FATAL("Bad type");
  640. return NULL;
  641. }
  642. }
  643. bool BeBlock::IsEmpty()
  644. {
  645. return mInstructions.size() == 0;
  646. }
  647. BeType* BeCallInst::GetType()
  648. {
  649. if (mInlineResult != NULL)
  650. return mInlineResult->GetType();
  651. auto type = mFunc->GetType();
  652. if (type == NULL)
  653. {
  654. if (auto intrin = BeValueDynCast<BeIntrinsic>(mFunc))
  655. return intrin->mReturnType;
  656. return type;
  657. }
  658. while (type->mTypeCode == BeTypeCode_Pointer)
  659. type = ((BePointerType*)type)->mElementType;
  660. auto funcType = (BeFunctionType*)(type);
  661. if (funcType == NULL)
  662. return NULL;
  663. BF_ASSERT(funcType->mTypeCode == BeTypeCode_Function);
  664. return funcType->mReturnType;
  665. }
  666. BeType* BePhiInst::GetType()
  667. {
  668. return mType;
  669. }
  670. BeType* BeArgument::GetType()
  671. {
  672. return mModule->mActiveFunction->GetFuncType()->mParams[mArgIdx].mType;
  673. }
  674. void BeDbgDeclareInst::HashInst(BeHashContext& hashCtx)
  675. {
  676. hashCtx.Mixin(TypeId);
  677. mDbgVar->HashReference(hashCtx);
  678. mValue->HashReference(hashCtx);
  679. hashCtx.Mixin(mIsValue);
  680. }
  681. void BeDbgStructType::HashContent(BeHashContext& hashCtx)
  682. {
  683. hashCtx.Mixin(TypeId);
  684. hashCtx.MixinStr(mName);
  685. if (mDerivedFrom != NULL)
  686. mDerivedFrom->HashReference(hashCtx);
  687. for (auto member : mMembers)
  688. member->HashReference(hashCtx);
  689. for (auto method : mMethods)
  690. method->HashReference(hashCtx);
  691. hashCtx.Mixin(mIsFullyDefined);
  692. hashCtx.Mixin(mIsStatic);
  693. mDefFile->HashReference(hashCtx);
  694. hashCtx.Mixin(mDefLine);
  695. }
  696. //////////////////////////////////////////////////////////////////////////
  697. void BeDbgLexicalBlock::HashContent(BeHashContext& hashCtx)
  698. {
  699. hashCtx.Mixin(TypeId);
  700. mFile->HashReference(hashCtx);
  701. mScope->HashReference(hashCtx);
  702. }
  703. //////////////////////////////////////////////////////////////////////////
  704. BeDbgFunction* BeDbgLoc::GetDbgFunc()
  705. {
  706. auto checkScope = mDbgScope;
  707. while (checkScope != NULL)
  708. {
  709. if (auto inlinedScope = BeValueDynCast<BeDbgInlinedScope>(checkScope))
  710. {
  711. checkScope = inlinedScope->mScope;
  712. }
  713. if (auto dbgFunc = BeValueDynCast<BeDbgFunction>(checkScope))
  714. {
  715. return dbgFunc;
  716. }
  717. else if (auto dbgLexBlock = BeValueDynCast<BeDbgLexicalBlock>(checkScope))
  718. {
  719. checkScope = dbgLexBlock->mScope;
  720. }
  721. else
  722. return NULL;
  723. }
  724. return NULL;
  725. }
  726. BeDbgFile* BeDbgLoc::GetDbgFile()
  727. {
  728. auto checkScope = mDbgScope;
  729. while (checkScope != NULL)
  730. {
  731. if (auto inlinedScope = BeValueDynCast<BeDbgInlinedScope>(checkScope))
  732. {
  733. checkScope = inlinedScope->mScope;
  734. }
  735. if (auto dbgFile = BeValueDynCast<BeDbgFile>(checkScope))
  736. {
  737. return dbgFile;
  738. }
  739. else if (auto dbgStruct = BeValueDynCast<BeDbgStructType>(checkScope))
  740. {
  741. checkScope = dbgStruct->mScope;
  742. }
  743. else if (auto dbgEnum = BeValueDynCast<BeDbgEnumType>(checkScope))
  744. {
  745. checkScope = dbgEnum->mScope;
  746. }
  747. else if (auto dbgNamespace = BeValueDynCast<BeDbgNamespace>(checkScope))
  748. {
  749. checkScope = dbgNamespace->mScope;
  750. }
  751. else if (auto dbgFunc = BeValueDynCast<BeDbgFunction>(checkScope))
  752. {
  753. return dbgFunc->mFile;
  754. }
  755. else if (auto dbgLexBlock = BeValueDynCast<BeDbgLexicalBlock>(checkScope))
  756. {
  757. return dbgLexBlock->mFile;
  758. }
  759. else
  760. return NULL;
  761. }
  762. return NULL;
  763. }
  764. BeDbgLoc* BeDbgLoc::GetInlinedAt(int idx)
  765. {
  766. if (idx == -1)
  767. return this;
  768. auto checkDbgLoc = mDbgInlinedAt;
  769. for (int i = 0; i < idx; i++)
  770. checkDbgLoc = checkDbgLoc->mDbgInlinedAt;
  771. return checkDbgLoc;
  772. }
  773. BeDbgLoc* BeDbgLoc::GetRoot()
  774. {
  775. auto checkDbgLoc = this;
  776. while (checkDbgLoc->mDbgInlinedAt != NULL)
  777. checkDbgLoc = checkDbgLoc->mDbgInlinedAt;
  778. return checkDbgLoc;
  779. }
  780. int BeDbgLoc::GetInlineDepth()
  781. {
  782. int inlineDepth = 0;
  783. auto checkDbgLoc = mDbgInlinedAt;
  784. while (checkDbgLoc != NULL)
  785. {
  786. inlineDepth++;
  787. checkDbgLoc = checkDbgLoc->mDbgInlinedAt;
  788. }
  789. return inlineDepth;
  790. }
  791. int BeDbgLoc::GetInlineMatchDepth(BeDbgLoc* other)
  792. {
  793. int inlineDepth = GetInlineDepth();
  794. int otherInlineDepth = other->GetInlineDepth();
  795. int matchDepth = 0;
  796. while (true)
  797. {
  798. if ((matchDepth >= inlineDepth) || (matchDepth >= otherInlineDepth))
  799. break;
  800. int inlineIdx = inlineDepth - matchDepth - 1;
  801. int otherInlineIdx = otherInlineDepth - matchDepth - 1;
  802. auto inlinedAt = GetInlinedAt(inlineIdx);
  803. auto otherInlinedAt = other->GetInlinedAt(otherInlineIdx);
  804. if (inlinedAt != otherInlinedAt)
  805. break;
  806. if ((otherInlineIdx == 0) || (inlineIdx == 0))
  807. {
  808. // At the current scope, make sure we're refererring to the same method...
  809. auto funcScope = GetInlinedAt(inlineIdx - 1);
  810. auto otherFuncScope = other->GetInlinedAt(otherInlineIdx - 1);
  811. auto dbgFunc = funcScope->GetDbgFunc();
  812. auto otherDbgFunc = otherFuncScope->GetDbgFunc();
  813. if (dbgFunc != otherDbgFunc)
  814. {
  815. // Same invocation position but different method...
  816. break;
  817. }
  818. }
  819. matchDepth++;
  820. }
  821. /*int matchDepth = 0;
  822. while (true)
  823. {
  824. if ((matchDepth >= inlineDepth) || (matchDepth >= otherInlineDepth))
  825. break;
  826. if (GetInlinedAt(inlineDepth - matchDepth - 1) != other->GetInlinedAt(otherInlineDepth - matchDepth - 1))
  827. break;
  828. matchDepth++;
  829. }*/
  830. return matchDepth;
  831. }
  832. void BeDbgFunction::HashContent(BeHashContext& hashCtx)
  833. {
  834. hashCtx.Mixin(TypeId);
  835. if (mFile != NULL)
  836. mFile->HashReference(hashCtx);
  837. hashCtx.Mixin(mLine);
  838. hashCtx.MixinStr(mName);
  839. hashCtx.MixinStr(mLinkageName);
  840. mType->HashReference(hashCtx);
  841. for (auto genericArg : mGenericArgs)
  842. genericArg->HashReference(hashCtx);
  843. for (auto genericConstValueArgs : mGenericArgs)
  844. genericConstValueArgs->HashReference(hashCtx);
  845. if (mValue != NULL)
  846. mValue->HashReference(hashCtx);
  847. hashCtx.Mixin(mIsLocalToUnit);
  848. hashCtx.Mixin(mIsStaticMethod);
  849. hashCtx.Mixin(mFlags);
  850. hashCtx.Mixin(mVK);
  851. hashCtx.Mixin(mVIndex);
  852. hashCtx.Mixin(mVariables.size());
  853. for (auto& variable : mVariables)
  854. {
  855. if (variable == NULL)
  856. hashCtx.Mixin(-1);
  857. else
  858. variable->HashReference(hashCtx);
  859. }
  860. hashCtx.Mixin(mPrologSize);
  861. hashCtx.Mixin(mCodeLen);
  862. }
  863. void BeDbgStructType::SetMembers(SizedArrayImpl<BeMDNode*>& members)
  864. {
  865. mIsFullyDefined = true;
  866. BF_ASSERT(mMembers.size() == 0);
  867. for (auto member : members)
  868. {
  869. if (auto inheritance = BeValueDynCast<BeDbgInheritance>(member))
  870. {
  871. BF_ASSERT(mDerivedFrom == NULL);
  872. mDerivedFrom = inheritance->mBaseType;
  873. }
  874. else if (auto structMember = BeValueDynCast<BeDbgStructMember>(member))
  875. {
  876. mMembers.push_back(structMember);
  877. }
  878. else if (auto dbgMethod = BeValueDynCast<BeDbgFunction>(member))
  879. {
  880. dbgMethod->mIncludedAsMember = true;
  881. mMethods.push_back(dbgMethod);
  882. }
  883. else
  884. BF_FATAL("bad");
  885. }
  886. }
  887. void BeDbgEnumType::SetMembers(SizedArrayImpl<BeMDNode*>& members)
  888. {
  889. mIsFullyDefined = true;
  890. BF_ASSERT(mMembers.size() == 0);
  891. for (auto member : members)
  892. {
  893. if (auto enumMember = BeValueDynCast<BeDbgEnumMember>(member))
  894. {
  895. mMembers.push_back(enumMember);
  896. }
  897. else
  898. BF_FATAL("bad");
  899. }
  900. }
  901. //////////////////////////////////////////////////////////////////////////
  902. void BeDumpContext::ToString(StringImpl& str, BeValue* value, bool showType, bool mdDrillDown)
  903. {
  904. if (value == NULL)
  905. {
  906. str += "<null>";
  907. return;
  908. }
  909. if (auto mdNode = BeValueDynCast<BeMDNode>(value))
  910. {
  911. if (auto dbgInlinedScope = BeValueDynCast<BeDbgInlinedScope>(mdNode))
  912. {
  913. str += "Inlined:";
  914. ToString(str, dbgInlinedScope->mScope);
  915. return;
  916. }
  917. if (auto dbgType = BeValueDynCast<BeDbgTypeId>(mdNode))
  918. {
  919. str += StrFormat("DbgTypeId: %d", dbgType->mTypeId);
  920. return;
  921. }
  922. if (auto dbgVar = BeValueDynCast<BeDbgVariable>(mdNode))
  923. {
  924. ToString(str, dbgVar->mType);
  925. str += " ";
  926. str += dbgVar->mName;
  927. return;
  928. }
  929. if (auto dbgVar = BeValueDynCast<BeDbgVariable>(mdNode))
  930. {
  931. ToString(str, dbgVar->mType);
  932. str += " ";
  933. str += dbgVar->mName;
  934. return;
  935. }
  936. if (auto dbgFile = BeValueDynCast<BeDbgFile>(mdNode))
  937. {
  938. str += dbgFile->mFileName;
  939. return;
  940. }
  941. if (auto dbgFunc = BeValueDynCast<BeDbgFunction>(mdNode))
  942. {
  943. if (auto parentType = BeValueDynCast<BeDbgType>(dbgFunc->mScope))
  944. {
  945. ToString(str, dbgFunc->mScope);
  946. str += ".";
  947. }
  948. else if (auto dbgNamespace = BeValueDynCast<BeDbgNamespace>(dbgFunc->mScope))
  949. {
  950. ToString(str, dbgNamespace->mScope);
  951. str += ".";
  952. }
  953. str += dbgFunc->mName;
  954. if (mdDrillDown)
  955. {
  956. str += ":";
  957. ToString(str, dbgFunc->mFile, true, true);
  958. }
  959. return;
  960. }
  961. if (auto lexBlock = BeValueDynCast<BeDbgLexicalBlock>(mdNode))
  962. {
  963. str += "{";
  964. str += StrFormat("%d@", lexBlock->mId);
  965. ToString(str, lexBlock->mFile);
  966. str += ":";
  967. ToString(str, lexBlock->mScope);
  968. return;
  969. }
  970. if (auto dbgType = BeValueDynCast<BeDbgBasicType>(mdNode))
  971. {
  972. if (dbgType->mEncoding == llvm::dwarf::DW_ATE_address)
  973. {
  974. if (dbgType->mSize == 0)
  975. str += "void";
  976. else
  977. str += "addr";
  978. return;
  979. }
  980. else if (dbgType->mEncoding == llvm::dwarf::DW_ATE_signed)
  981. {
  982. str += "int";
  983. }
  984. else if (dbgType->mEncoding == llvm::dwarf::DW_ATE_unsigned)
  985. {
  986. str += "uint";
  987. }
  988. else if (dbgType->mEncoding == llvm::dwarf::DW_ATE_float)
  989. {
  990. if (dbgType->mSize == 4)
  991. str += "float";
  992. else
  993. str += "double";
  994. return;
  995. }
  996. else if (dbgType->mEncoding == llvm::dwarf::DW_ATE_unsigned_char)
  997. str += "uchar";
  998. else if (dbgType->mEncoding == llvm::dwarf::DW_ATE_signed_char)
  999. str += "char";
  1000. else if (dbgType->mEncoding == llvm::dwarf::DW_ATE_boolean)
  1001. {
  1002. if (dbgType->mSize == 1)
  1003. {
  1004. str += "bool";
  1005. return;
  1006. }
  1007. str += "bool";
  1008. }
  1009. else
  1010. str += "???";
  1011. str += StrFormat("%d", dbgType->mSize * 8);
  1012. return;
  1013. }
  1014. if (auto dbgType = BeValueDynCast<BeDbgPointerType>(mdNode))
  1015. {
  1016. ToString(str, dbgType->mElement);
  1017. str += "*";
  1018. return;
  1019. }
  1020. if (auto dbgType = BeValueDynCast<BeDbgReferenceType>(mdNode))
  1021. {
  1022. ToString(str, dbgType->mElement);
  1023. str += "&";
  1024. return;
  1025. }
  1026. if (auto dbgType = BeValueDynCast<BeDbgConstType>(mdNode))
  1027. {
  1028. str += "const ";
  1029. ToString(str, dbgType->mElement);
  1030. return;
  1031. }
  1032. if (auto dbgType = BeValueDynCast<BeDbgArtificialType>(mdNode))
  1033. {
  1034. str += "artificial ";
  1035. ToString(str, dbgType->mElement);
  1036. return;
  1037. }
  1038. if (auto dbgType = BeValueDynCast<BeDbgNamespace>(mdNode))
  1039. {
  1040. if ((BeValueDynCast<BeDbgStructType>(dbgType->mScope) != NULL) ||
  1041. (BeValueDynCast<BeDbgNamespace>(dbgType->mScope) != NULL))
  1042. {
  1043. ToString(str, dbgType->mScope);
  1044. str += ".";
  1045. str += dbgType->mName;
  1046. return;
  1047. }
  1048. else
  1049. str += dbgType->mName;
  1050. return;
  1051. }
  1052. if (auto dbgType = BeValueDynCast<BeDbgStructType>(mdNode))
  1053. {
  1054. if ((BeValueDynCast<BeDbgStructType>(dbgType->mScope) != NULL) ||
  1055. (BeValueDynCast<BeDbgNamespace>(dbgType->mScope) != NULL))
  1056. {
  1057. ToString(str, dbgType->mScope);
  1058. str += ".";
  1059. str += dbgType->mName;
  1060. return;
  1061. }
  1062. else
  1063. str += dbgType->mName;
  1064. return;
  1065. }
  1066. if (auto dbgType = BeValueDynCast<BeDbgEnumType>(mdNode))
  1067. {
  1068. if ((BeValueDynCast<BeDbgStructType>(dbgType->mScope) != NULL) ||
  1069. (BeValueDynCast<BeDbgNamespace>(dbgType->mScope) != NULL))
  1070. {
  1071. ToString(str, dbgType->mScope);
  1072. str += ".";
  1073. str += dbgType->mName;
  1074. return;
  1075. }
  1076. else
  1077. str += dbgType->mName;
  1078. return;
  1079. }
  1080. if (auto dbgType = BeValueDynCast<BeDbgArrayType>(mdNode))
  1081. {
  1082. ToString(str, dbgType->mElement);
  1083. str += "[";
  1084. str += StrFormat("%d", dbgType->mNumElements);
  1085. str += "]";
  1086. return;
  1087. }
  1088. if (auto dbgLoc = BeValueDynCast<BeDbgLoc>(mdNode))
  1089. {
  1090. str += StrFormat("@%d ", dbgLoc->mIdx);
  1091. ToString(str, dbgLoc->mDbgScope, true, true);
  1092. str += StrFormat(":%d:%d", dbgLoc->mLine + 1, dbgLoc->mColumn + 1);
  1093. if (dbgLoc->mDbgInlinedAt)
  1094. {
  1095. str += " inlined at ";
  1096. ToString(str, dbgLoc->mDbgInlinedAt, true, true);
  1097. }
  1098. return;
  1099. }
  1100. if (auto inheritance = BeValueDynCast<BeDbgInheritance>(mdNode))
  1101. {
  1102. str += "inherit ";
  1103. ToString(str, inheritance->mBaseType);
  1104. return;
  1105. }
  1106. if (auto dbgMember = BeValueDynCast<BeDbgStructMember>(mdNode))
  1107. {
  1108. if (dbgMember->mIsStatic)
  1109. str += "static ";
  1110. ToString(str, dbgMember->mType);
  1111. str += " ";
  1112. str += dbgMember->mName;
  1113. if (!dbgMember->mIsStatic)
  1114. {
  1115. str += " offset:";
  1116. str += StrFormat("%d", dbgMember->mOffset);
  1117. }
  1118. if (dbgMember->mStaticValue != NULL)
  1119. str += " " + ToString(dbgMember->mStaticValue);
  1120. return;
  1121. }
  1122. if (auto dbgMember = BeValueDynCast<BeDbgEnumMember>(mdNode))
  1123. {
  1124. str += dbgMember->mName;
  1125. str += " ";
  1126. str += StrFormat("%lld", dbgMember->mValue);
  1127. return;
  1128. }
  1129. str += "?MDNode?";
  1130. return;
  1131. }
  1132. if (auto globalVar = BeValueDynCast<BeGlobalVariable>(value))
  1133. {
  1134. ToString(str, globalVar->GetType());
  1135. str += " ";
  1136. str += globalVar->mName;
  1137. return;
  1138. }
  1139. if (auto constantGEP = BeValueDynCast<BeGEP1Constant>(value))
  1140. {
  1141. str += "ConstGEP1 ";
  1142. ToString(str, constantGEP->mTarget);
  1143. str += StrFormat(" %d %d", constantGEP->mIdx0);
  1144. return;
  1145. }
  1146. if (auto constantGEP = BeValueDynCast<BeGEP2Constant>(value))
  1147. {
  1148. str += "ConstGEP2 ";
  1149. ToString(str, constantGEP->mTarget);
  1150. str += StrFormat(" %d %d", constantGEP->mIdx0, constantGEP->mIdx1);
  1151. return;
  1152. }
  1153. if (auto constantExtract = BeValueDynCast<BeExtractValueConstant>(value))
  1154. {
  1155. str += "ConstExtract ";
  1156. ToString(str, constantExtract->mTarget);
  1157. str += StrFormat(" %d", constantExtract->mIdx0);
  1158. return;
  1159. }
  1160. if (auto arg = BeValueDynCast<BeArgument>(value))
  1161. {
  1162. auto activeFunction = arg->mModule->mActiveFunction;
  1163. auto& typeParam = activeFunction->GetFuncType()->mParams[arg->mArgIdx];
  1164. auto& param = activeFunction->mParams[arg->mArgIdx];
  1165. if (showType)
  1166. {
  1167. BeModule::ToString(str, typeParam.mType);
  1168. str += " %";
  1169. str += param.mName;
  1170. }
  1171. else
  1172. {
  1173. str += "%";
  1174. str += param.mName;
  1175. }
  1176. return;
  1177. }
  1178. if (auto func = BeValueDynCast<BeFunction>(value))
  1179. {
  1180. str += func->mName;
  1181. return;
  1182. }
  1183. if (auto constant = BeValueDynCast<BeStructConstant>(value))
  1184. {
  1185. if (showType)
  1186. {
  1187. BeModule::ToString(str, constant->mType);
  1188. str += " ";
  1189. }
  1190. str += "(";
  1191. switch (constant->mType->mTypeCode)
  1192. {
  1193. case BeTypeCode_Struct:
  1194. case BeTypeCode_SizedArray:
  1195. case BeTypeCode_Vector:
  1196. for (int valIdx = 0; valIdx < (int)constant->mMemberValues.size(); valIdx++)
  1197. {
  1198. if (valIdx > 0)
  1199. str += ", ";
  1200. ToString(str, constant->mMemberValues[valIdx], false);
  1201. }
  1202. break;
  1203. default:
  1204. BF_FATAL("NotImpl");
  1205. break;
  1206. }
  1207. str += ")";
  1208. return;
  1209. }
  1210. if (auto constant = BeValueDynCast<BeUndefConstant>(value))
  1211. {
  1212. if (showType)
  1213. {
  1214. BeModule::ToString(str, constant->mType);
  1215. str += " ";
  1216. }
  1217. str += "undef";
  1218. return;
  1219. }
  1220. if (auto constant = BeValueDynCast<BeCastConstant>(value))
  1221. {
  1222. ToString(str, constant->mType);
  1223. str += " cast ";
  1224. ToString(str, constant->mTarget);
  1225. return;
  1226. }
  1227. if (auto constant = BeValueDynCast<BeGEP1Constant>(value))
  1228. {
  1229. ToString(str, constant->GetType());
  1230. str += " gep (";
  1231. ToString(str, constant->mTarget);
  1232. str += StrFormat(", %d)", constant->mIdx0);
  1233. return;
  1234. }
  1235. if (auto constant = BeValueDynCast<BeGEP2Constant>(value))
  1236. {
  1237. ToString(str, constant->GetType());
  1238. str += " gep (";
  1239. ToString(str, constant->mTarget);
  1240. str += StrFormat(", %d, %d)", constant->mIdx0, constant->mIdx1);
  1241. return;
  1242. }
  1243. if (auto constant = BeValueDynCast<BeExtractValueConstant>(value))
  1244. {
  1245. ToString(str, constant->GetType());
  1246. str += " extract (";
  1247. ToString(str, constant->mTarget);
  1248. str += StrFormat(", %d)", constant->mIdx0);
  1249. return;
  1250. }
  1251. if (auto constant = BeValueDynCast<BeStructConstant>(value))
  1252. {
  1253. ToString(str, constant->GetType());
  1254. str += " (";
  1255. for (int i = 0; i < constant->mMemberValues.size(); i++)
  1256. {
  1257. if (i > 0)
  1258. str += ", ";
  1259. ToString(str, constant->mMemberValues[i]);
  1260. }
  1261. str += ")";
  1262. return;
  1263. }
  1264. if (auto constant = BeValueDynCast<BeStringConstant>(value))
  1265. {
  1266. ToString(str, constant->GetType());
  1267. str += " \"";
  1268. str += SlashString(constant->mString, true, true);
  1269. str += "\"";
  1270. return;
  1271. }
  1272. if (auto constant = BeValueDynCast<BeConstant>(value))
  1273. {
  1274. BeModule::ToString(str, constant->mType);
  1275. str += " ";
  1276. switch (constant->mType->mTypeCode)
  1277. {
  1278. case BeTypeCode_None:
  1279. return;
  1280. case BeTypeCode_NullPtr:
  1281. return;
  1282. case BeTypeCode_Boolean:
  1283. str += constant->mBool ? "true" : "false";
  1284. return;
  1285. case BeTypeCode_Int8:
  1286. case BeTypeCode_Int16:
  1287. case BeTypeCode_Int32:
  1288. case BeTypeCode_Int64:
  1289. str += StrFormat("%lld", constant->mInt64);
  1290. return;
  1291. case BeTypeCode_Float:
  1292. case BeTypeCode_Double:
  1293. str += StrFormat("%f", constant->mDouble);
  1294. return;
  1295. case BeTypeCode_Pointer:
  1296. if (constant->mTarget == NULL)
  1297. {
  1298. str += "null";
  1299. return;
  1300. }
  1301. else
  1302. {
  1303. str += "(";
  1304. ToString(str, constant->mTarget);
  1305. str += ")";
  1306. return;
  1307. }
  1308. case BeTypeCode_Struct:
  1309. case BeTypeCode_SizedArray:
  1310. case BeTypeCode_Vector:
  1311. str += "zeroinitializer";
  1312. return;
  1313. case BeTypeCode_Function:
  1314. BF_FATAL("Notimpl");
  1315. str += "<InvalidConstant>";
  1316. return;
  1317. default:
  1318. BF_FATAL("NotImpl");
  1319. }
  1320. }
  1321. if (auto intrin = BeValueDynCast<BeIntrinsic>(value))
  1322. {
  1323. str += "intrin:";
  1324. str += BfIRCodeGen::GetIntrinsicName((int)intrin->mKind);;
  1325. return;
  1326. }
  1327. if (auto callInst = BeValueDynCast<BeCallInst>(value))
  1328. {
  1329. if (callInst->mInlineResult != NULL)
  1330. {
  1331. str += "InlineResult: ";
  1332. ToString(str, callInst->mInlineResult);
  1333. return;
  1334. }
  1335. }
  1336. BeType* resultType = NULL;
  1337. const char* wantNamePtr = NULL;
  1338. if (auto instVal = BeValueDynCast<BeInst>(value))
  1339. {
  1340. resultType = instVal->GetType();
  1341. if ((instVal->mName != NULL) && (instVal->mName[0] != 0))
  1342. wantNamePtr = instVal->mName;
  1343. }
  1344. String* valueNamePtr = NULL;
  1345. if (mValueNameMap.TryGetValue(value, &valueNamePtr))
  1346. {
  1347. if (resultType != NULL)
  1348. {
  1349. BeModule::ToString(str, resultType);
  1350. str += " %";
  1351. }
  1352. else
  1353. str += "%";
  1354. str += *valueNamePtr;
  1355. return;
  1356. }
  1357. if (auto beBlock = BeValueDynCast<BeBlock>(value))
  1358. {
  1359. if (!beBlock->mName.IsEmpty())
  1360. wantNamePtr = beBlock->mName.c_str();
  1361. }
  1362. StringT<64> useName;
  1363. if (wantNamePtr != NULL)
  1364. useName += wantNamePtr;
  1365. while (true)
  1366. {
  1367. int* idxPtr = NULL;
  1368. if ((mSeenNames.TryAdd(useName, NULL, &idxPtr)) && (!useName.IsEmpty()))
  1369. break;
  1370. int checkIdx = (*idxPtr)++;
  1371. char str[32];
  1372. sprintf(str, "%d", checkIdx);
  1373. useName += str;
  1374. }
  1375. mValueNameMap[value] = useName;
  1376. if ((showType) && (resultType != NULL))
  1377. {
  1378. BeModule::ToString(str, resultType);
  1379. str += " %";
  1380. str += useName;
  1381. return;
  1382. }
  1383. else
  1384. {
  1385. str += "%";
  1386. str += useName;
  1387. return;
  1388. }
  1389. }
  1390. String BeDumpContext::ToString(BeValue* value, bool showType, bool mdDrillDown)
  1391. {
  1392. String str;
  1393. ToString(str, value, showType, mdDrillDown);
  1394. return str;
  1395. }
  1396. String BeDumpContext::ToString(BeType* type)
  1397. {
  1398. String str;
  1399. BeModule::ToString(str, type);
  1400. return str;
  1401. }
  1402. void BeDumpContext::ToString(StringImpl& str, BeType* type)
  1403. {
  1404. BeModule::ToString(str, type);
  1405. }
  1406. void BeDumpContext::ToString(StringImpl& str, BeDbgFunction* dbgFunction, bool showScope)
  1407. {
  1408. if (dbgFunction->mIsStaticMethod)
  1409. str += "static ";
  1410. if (dbgFunction->mIsLocalToUnit)
  1411. str += "internal ";
  1412. if (dbgFunction->mValue == NULL)
  1413. str += "external ";
  1414. if (dbgFunction->mVK > 0)
  1415. str += StrFormat("virtual(%d) ", dbgFunction->mVIndex);
  1416. str += ToString(dbgFunction->mType->mReturnType);
  1417. str += " ";
  1418. if ((showScope) && (dbgFunction->mScope != NULL))
  1419. {
  1420. if (auto parentType = BeValueDynCast<BeDbgType>(dbgFunction->mScope))
  1421. {
  1422. ToString(str, parentType);
  1423. str += ".";
  1424. }
  1425. }
  1426. bool needsQuote = false;
  1427. for (char c : dbgFunction->mName)
  1428. {
  1429. if ((c == '.') || (c == '<') || (c == '('))
  1430. needsQuote = true;
  1431. }
  1432. if (needsQuote)
  1433. str += "\"";
  1434. str += dbgFunction->mName;
  1435. if (needsQuote)
  1436. str += "\"";
  1437. if (!dbgFunction->mGenericArgs.IsEmpty())
  1438. {
  1439. str += "<";
  1440. for (int genericIdx = 0; genericIdx < dbgFunction->mGenericArgs.Count(); genericIdx++)
  1441. {
  1442. if (genericIdx > 0)
  1443. str += ", ";
  1444. str += ToString(dbgFunction->mGenericArgs[genericIdx]);
  1445. }
  1446. str += ">";
  1447. }
  1448. if (!dbgFunction->mGenericConstValueArgs.IsEmpty())
  1449. {
  1450. str += "<";
  1451. for (int genericIdx = 0; genericIdx < dbgFunction->mGenericConstValueArgs.Count(); genericIdx++)
  1452. {
  1453. if (genericIdx > 0)
  1454. str += ", ";
  1455. str += ToString(dbgFunction->mGenericConstValueArgs[genericIdx]);
  1456. }
  1457. str += ">";
  1458. }
  1459. str += "(";
  1460. for (int paramIdx = 0; paramIdx < (int)dbgFunction->mType->mParams.size(); paramIdx++)
  1461. {
  1462. if (paramIdx > 0)
  1463. str += ", ";
  1464. str += ToString(dbgFunction->mType->mParams[paramIdx]);
  1465. BeDbgVariable* variable = NULL;
  1466. if (paramIdx < dbgFunction->mVariables.Count())
  1467. variable = dbgFunction->mVariables[paramIdx];
  1468. if (variable != NULL)
  1469. {
  1470. if (variable->mParamNum == paramIdx)
  1471. {
  1472. str += " ";
  1473. str += variable->mName;
  1474. }
  1475. }
  1476. }
  1477. str += ")";
  1478. if (!dbgFunction->mLinkageName.IsEmpty())
  1479. {
  1480. str += " Link:";
  1481. str += dbgFunction->mLinkageName;
  1482. }
  1483. }
  1484. String BeDumpContext::ToString(BeDbgFunction* dbgFunction)
  1485. {
  1486. String str;
  1487. ToString(str, dbgFunction, false);
  1488. return str;
  1489. }
  1490. void BeDumpContext::ToString(StringImpl& str, int val)
  1491. {
  1492. char iStr[32];
  1493. sprintf(iStr, "%d", val);
  1494. str += iStr;
  1495. }
  1496. String BeDumpContext::ToString(int val)
  1497. {
  1498. return StrFormat("%d", val);
  1499. }
  1500. void BeDumpContext::ToString(StringImpl& str, BeCmpKind cmpKind)
  1501. {
  1502. switch (cmpKind)
  1503. {
  1504. case BeCmpKind_SLT: str += "slt"; return;
  1505. case BeCmpKind_ULT: str += "ult"; return;
  1506. case BeCmpKind_SLE: str += "sle"; return;
  1507. case BeCmpKind_ULE: str += "ule"; return;
  1508. case BeCmpKind_EQ: str += "eq"; return;
  1509. case BeCmpKind_NE: str += "ne"; return;
  1510. case BeCmpKind_SGT: str += "sgt"; return;
  1511. case BeCmpKind_UGT: str += "ugt"; return;
  1512. case BeCmpKind_SGE: str += "sge"; return;
  1513. case BeCmpKind_UGE: str += "uge"; return;
  1514. case BeCmpKind_NB: str += "nb"; return;
  1515. case BeCmpKind_NO: str += "no"; return;
  1516. default:
  1517. str += "???";
  1518. }
  1519. }
  1520. String BeDumpContext::ToString(BeCmpKind cmpKind)
  1521. {
  1522. String str;
  1523. ToString(str, cmpKind);
  1524. return str;
  1525. }
  1526. void BeDumpContext::ToString(StringImpl& str, BeBinaryOpKind opKind)
  1527. {
  1528. switch (opKind)
  1529. {
  1530. case BeBinaryOpKind_Add: str += "+"; return;
  1531. case BeBinaryOpKind_Subtract: str += "-"; return;
  1532. case BeBinaryOpKind_Multiply: str += "*"; return;
  1533. case BeBinaryOpKind_SDivide: str += "s/"; return;
  1534. case BeBinaryOpKind_UDivide: str += "u/"; return;
  1535. case BeBinaryOpKind_SModulus: str += "%"; return;
  1536. case BeBinaryOpKind_UModulus: str += "%"; return;
  1537. case BeBinaryOpKind_BitwiseAnd: str += "&"; return;
  1538. case BeBinaryOpKind_BitwiseOr: str += "|"; return;
  1539. case BeBinaryOpKind_ExclusiveOr: str += "^"; return;
  1540. case BeBinaryOpKind_LeftShift: str += "<<"; return;
  1541. case BeBinaryOpKind_RightShift: str += ">>"; return;
  1542. case BeBinaryOpKind_ARightShift: str += "A>>"; return;
  1543. default:
  1544. str += "???";
  1545. }
  1546. }
  1547. String BeDumpContext::ToString(BeBinaryOpKind opKind)
  1548. {
  1549. String str;
  1550. ToString(str, opKind);
  1551. return str;
  1552. }
  1553. //////////////////////////////////////////////////////////////////////////
  1554. void BeDbgFile::ToString(String& str)
  1555. {
  1556. str += mDirectory;
  1557. if (str.length() > 0)
  1558. {
  1559. if ((str[str.length() - 1] != '\\') && (str[str.length() - 1] != '/'))
  1560. str += "\\";
  1561. }
  1562. str += mFileName;
  1563. for (int i = 0; i < str.length(); i++)
  1564. if (str[i] == '/')
  1565. str = '\\';
  1566. }
  1567. void BeDbgFile::GetFilePath(String& outStr)
  1568. {
  1569. outStr.Append(mDirectory);
  1570. outStr.Append(DIR_SEP_CHAR);
  1571. outStr.Append(mFileName);
  1572. }
  1573. //////////////////////////////////////////////////////////////////////////
  1574. BeModule::BeModule(const StringImpl& moduleName, BeContext* context)
  1575. {
  1576. mBeIRCodeGen = NULL;
  1577. mModuleName = moduleName;
  1578. mContext = context;
  1579. mActiveBlock = NULL;
  1580. mInsertPos = -1;
  1581. mCurDbgLoc = NULL;
  1582. mLastDbgLoc = NULL;
  1583. mActiveFunction = NULL;
  1584. mDbgModule = NULL;
  1585. mCeMachine = NULL;
  1586. mPrevDbgLocInline = NULL;
  1587. mCurDbgLocIdx = 0;
  1588. mCurLexBlockId = 0;
  1589. }
  1590. void BeModule::Hash(BeHashContext& hashCtx)
  1591. {
  1592. hashCtx.MixinStr(mTargetTriple);
  1593. hashCtx.MixinStr(mTargetCPU);
  1594. hashCtx.Mixin(mConfigConsts64.size());
  1595. for (auto configConst : mConfigConsts64)
  1596. configConst->HashContent(hashCtx);
  1597. if (mDbgModule != NULL)
  1598. mDbgModule->HashReference(hashCtx);
  1599. if (!mFunctions.IsEmpty())
  1600. {
  1601. std::sort(mFunctions.begin(), mFunctions.end(), [](BeFunction* lhs, BeFunction* rhs)
  1602. {
  1603. return (lhs->mName < rhs->mName);
  1604. });
  1605. for (auto& beFunction : mFunctions)
  1606. {
  1607. if (!beFunction->mBlocks.IsEmpty())
  1608. beFunction->HashReference(hashCtx);
  1609. }
  1610. }
  1611. if (!mGlobalVariables.IsEmpty())
  1612. {
  1613. std::sort(mGlobalVariables.begin(), mGlobalVariables.end(), [](BeGlobalVariable* lhs, BeGlobalVariable* rhs)
  1614. {
  1615. return (lhs->mName < rhs->mName);
  1616. });
  1617. for (auto& beGlobalVar : mGlobalVariables)
  1618. {
  1619. if (beGlobalVar->mInitializer != NULL)
  1620. beGlobalVar->HashReference(hashCtx);
  1621. }
  1622. }
  1623. }
  1624. #define DELETE_ENTRY(i) delete mOwnedValues[i]; mOwnedValues[i] = NULL
  1625. BeModule::~BeModule()
  1626. {
  1627. delete mDbgModule;
  1628. }
  1629. void BeModule::StructToString(StringImpl& str, BeStructType* structType)
  1630. {
  1631. str += structType->mName;
  1632. str += " = type ";
  1633. if (!structType->mMembers.IsEmpty())
  1634. {
  1635. if (structType->mIsPacked)
  1636. str += "<";
  1637. str += "{";
  1638. for (int memberIdx = 0; memberIdx < (int)structType->mMembers.size(); memberIdx++)
  1639. {
  1640. if (memberIdx > 0)
  1641. str += ", ";
  1642. ToString(str, structType->mMembers[memberIdx].mType);
  1643. }
  1644. str += "}";
  1645. if (structType->mIsPacked)
  1646. str += ">";
  1647. }
  1648. else
  1649. {
  1650. str += "opaque";
  1651. if (structType->mSize > 0)
  1652. {
  1653. str += " size ";
  1654. str += StrFormat("%d", structType->mSize);
  1655. }
  1656. if (structType->mAlign > 0)
  1657. {
  1658. str += " align ";
  1659. str += StrFormat("%d", structType->mAlign);
  1660. }
  1661. }
  1662. str += "\n";
  1663. }
  1664. String BeModule::ToString(BeFunction* wantFunc)
  1665. {
  1666. Dictionary<int, BeDbgLoc*> dbgLocs;
  1667. String str;
  1668. SetAndRestoreValue<BeFunction*> prevActiveFunc(mActiveFunction, NULL);
  1669. BeDumpContext dc;
  1670. if (wantFunc == NULL)
  1671. {
  1672. str += "Module: "; str += mModuleName; str += "\n";
  1673. str += "Target: "; str += mTargetTriple; str += "\n";
  1674. if (mDbgModule != NULL)
  1675. {
  1676. str += "FileName: "; str += mDbgModule->mFileName; str += "\n";
  1677. str += "Directory: "; str += mDbgModule->mDirectory; str += "\n";
  1678. str += "Producer: "; str += mDbgModule->mProducer; str += "\n";
  1679. }
  1680. for (int i = 0; i < (int)mConfigConsts64.size(); i++)
  1681. {
  1682. if (i == 0)
  1683. str += "VirtualMethodOfs: ";
  1684. else if (i == 1)
  1685. str += "DynSlotOfs: ";
  1686. dc.ToString(str, mConfigConsts64[i]);
  1687. str += "\n";
  1688. }
  1689. str += "\n";
  1690. str += "; Types\n";
  1691. for (auto type : mContext->mTypes)
  1692. {
  1693. if (type->mTypeCode == BeTypeCode_Struct)
  1694. {
  1695. auto structType = (BeStructType*)type;
  1696. StructToString(str, structType);
  1697. }
  1698. }
  1699. str += "\n";
  1700. str += "; Global variables\n";
  1701. for (int gvIdx = 0; gvIdx < (int)mGlobalVariables.size(); gvIdx++)
  1702. {
  1703. auto gv = mGlobalVariables[gvIdx];
  1704. str += gv->mName;
  1705. str += " =";
  1706. if (gv->mInitializer == NULL)
  1707. str += " external";
  1708. if (gv->mLinkageType == BfIRLinkageType_Internal)
  1709. str += " internal";
  1710. if (gv->mIsConstant)
  1711. str += " constant";
  1712. if (gv->mIsTLS)
  1713. str += " tls";
  1714. if (gv->mInitializer != NULL)
  1715. {
  1716. str += " ";
  1717. str += dc.ToString(gv->mInitializer);
  1718. }
  1719. else
  1720. {
  1721. str += " ";
  1722. str += dc.ToString(gv->mType);
  1723. }
  1724. if (gv->mAlign != -1)
  1725. {
  1726. str += " align ";
  1727. str += StrFormat("%d", gv->mAlign);
  1728. }
  1729. str += "\n";
  1730. }
  1731. str += "\n";
  1732. if (mDbgModule != NULL)
  1733. {
  1734. if (!mDbgModule->mGlobalVariables.IsEmpty())
  1735. {
  1736. str += "; Global variable debug info\n";
  1737. for (auto dbgGlobalVar : mDbgModule->mGlobalVariables)
  1738. {
  1739. str += dbgGlobalVar->mName;
  1740. str += " = ";
  1741. if (dbgGlobalVar->mIsLocalToUnit)
  1742. str += "internal ";
  1743. dc.ToString(str, dbgGlobalVar->mType);
  1744. if (dbgGlobalVar->mValue != NULL)
  1745. {
  1746. str += " ";
  1747. dc.ToString(str, dbgGlobalVar->mValue);
  1748. }
  1749. if (dbgGlobalVar->mFile != NULL)
  1750. {
  1751. str += " @";
  1752. dc.ToString(str, dbgGlobalVar->mFile);
  1753. str += StrFormat(":%d", dbgGlobalVar->mLineNum);
  1754. }
  1755. if (!dbgGlobalVar->mLinkageName.IsEmpty())
  1756. {
  1757. str += " Link:";
  1758. str += dbgGlobalVar->mLinkageName;
  1759. }
  1760. str += "\n";
  1761. }
  1762. str += "\n";
  1763. }
  1764. str += "; Debug types\n";
  1765. for (auto dbgType : mDbgModule->mTypes)
  1766. {
  1767. if (auto dbgStructType = BeValueDynCast<BeDbgStructType>(dbgType))
  1768. {
  1769. dc.ToString(str, dbgStructType);
  1770. str += " = {";
  1771. if (dbgStructType->mSize != -1)
  1772. {
  1773. str += StrFormat("\n Size: %d", dbgStructType->mSize);
  1774. str += StrFormat("\n Align: %d", dbgStructType->mAlign);
  1775. }
  1776. if (dbgStructType->mDerivedFrom != NULL)
  1777. {
  1778. str += "\n Base: "; str += dc.ToString(dbgStructType->mDerivedFrom);
  1779. }
  1780. if (!dbgStructType->mMembers.IsEmpty())
  1781. {
  1782. str += "\n Members: {";
  1783. for (int memberIdx = 0; memberIdx < dbgStructType->mMembers.Count(); memberIdx++)
  1784. {
  1785. if (memberIdx > 0)
  1786. str += ", ";
  1787. str += "\n ";
  1788. dc.ToString(str, dbgStructType->mMembers[memberIdx]);
  1789. }
  1790. str += "}";
  1791. }
  1792. if (!dbgStructType->mMethods.IsEmpty())
  1793. {
  1794. str += "\n Methods: {";
  1795. for (int methodIdx = 0; methodIdx < dbgStructType->mMethods.Count(); methodIdx++)
  1796. {
  1797. if (methodIdx > 0)
  1798. str += ",";
  1799. str += "\n ";
  1800. dc.ToString(str, dbgStructType->mMethods[methodIdx], false);
  1801. }
  1802. str += "}";
  1803. }
  1804. str += "}\n";
  1805. }
  1806. else if (auto dbgEnumType = BeValueDynCast<BeDbgEnumType>(dbgType))
  1807. {
  1808. dc.ToString(str, dbgEnumType);
  1809. str += " = enum {";
  1810. if (dbgEnumType->mSize != -1)
  1811. {
  1812. str += StrFormat("\n Size: %d", dbgEnumType->mSize);
  1813. str += StrFormat("\n Align: %d", dbgEnumType->mAlign);
  1814. }
  1815. if (dbgEnumType->mElementType != NULL)
  1816. {
  1817. str += "\n Underlying: "; str += dc.ToString(dbgEnumType->mElementType);
  1818. }
  1819. if (!dbgEnumType->mMembers.IsEmpty())
  1820. {
  1821. str += "\n Members: {";
  1822. for (int memberIdx = 0; memberIdx < dbgEnumType->mMembers.Count(); memberIdx++)
  1823. {
  1824. if (memberIdx > 0)
  1825. str += ", ";
  1826. str += "\n ";
  1827. dc.ToString(str, dbgEnumType->mMembers[memberIdx]);
  1828. }
  1829. str += "}";
  1830. }
  1831. str += "}\n";
  1832. }
  1833. }
  1834. str += "\n";
  1835. str += "; Debug functions\n";
  1836. for (auto dbgFunc : mDbgModule->mFuncs)
  1837. {
  1838. if (!dbgFunc->mIncludedAsMember)
  1839. {
  1840. dc.ToString(str, dbgFunc, true);
  1841. str += "\n";
  1842. }
  1843. }
  1844. str += "\n";
  1845. }
  1846. str += "; Functions\n";
  1847. }
  1848. for (auto func : mFunctions)
  1849. {
  1850. if ((wantFunc != NULL) && (wantFunc != func))
  1851. continue;
  1852. mActiveFunction = func;
  1853. Dictionary<BeValue*, String> valueNameMap;
  1854. HashSet<String> seenNames;
  1855. auto funcType = func->GetFuncType();
  1856. if (func->mBlocks.size() == 0)
  1857. str += "declare ";
  1858. else
  1859. str += "define ";
  1860. ToString(str, func->GetFuncType()->mReturnType);
  1861. str += " ";
  1862. str += func->mName;
  1863. str += "(";
  1864. for (int paramIdx = 0; paramIdx < (int)funcType->mParams.size(); paramIdx++)
  1865. {
  1866. auto& typeParam = funcType->mParams[paramIdx];
  1867. auto& param = func->mParams[paramIdx];
  1868. if (paramIdx > 0)
  1869. str += ", ";
  1870. ToString(str, typeParam.mType);
  1871. if (param.mStructRet)
  1872. str += " sret";
  1873. if (param.mNoAlias)
  1874. str += " noalias";
  1875. if (param.mNoCapture)
  1876. str += " nocapture";
  1877. if (param.mZExt)
  1878. str += " zext";
  1879. if (param.mDereferenceableSize != -1)
  1880. str += StrFormat(" dereferenceable(%d)", param.mDereferenceableSize);
  1881. str += " ";
  1882. if (param.mName.empty())
  1883. param.mName = StrFormat("p%d", paramIdx);
  1884. dc.mSeenNames[param.mName] = 0;
  1885. str += "%" + param.mName;
  1886. }
  1887. if (funcType->mIsVarArg)
  1888. {
  1889. if (!funcType->mParams.IsEmpty())
  1890. str += ", ";
  1891. str += "...";
  1892. }
  1893. str += ")";
  1894. if (func->mAlwaysInline)
  1895. str += " AlwaysInline";
  1896. if (func->mNoUnwind)
  1897. str += " nounwind";
  1898. if (func->mUWTable)
  1899. str += " uwtable";
  1900. if (func->mNoReturn)
  1901. str += " noreturn";
  1902. if (func->mNoFramePointerElim)
  1903. str += " noframepointerelim";
  1904. if (func->mIsDLLExport)
  1905. str += " dllexport";
  1906. if (func->mBlocks.size() == 0)
  1907. {
  1908. str += "\n\n";
  1909. continue;
  1910. }
  1911. str += " {\n";
  1912. #define DISPLAY_INST0(typeName, name) \
  1913. case typeName::TypeId: { \
  1914. auto castedInst = (typeName*)inst; \
  1915. str += name; \
  1916. } \
  1917. break;
  1918. #define DISPLAY_INST1(typeName, name, member1) \
  1919. case typeName::TypeId: { \
  1920. auto castedInst = (typeName*)inst; \
  1921. str += name; \
  1922. str += " "; \
  1923. dc.ToString(str, castedInst->member1); \
  1924. } \
  1925. break;
  1926. #define DISPLAY_INST2(typeName, name, member1, member2) \
  1927. case typeName::TypeId: { \
  1928. auto castedInst = (typeName*)inst;\
  1929. str += name; \
  1930. str += " "; \
  1931. dc.ToString(str, castedInst->member1); \
  1932. str += ", "; \
  1933. dc.ToString(str, castedInst->member2); \
  1934. } \
  1935. break;
  1936. #define DISPLAY_INST2_OPEN(typeName, name, member1, member2) \
  1937. case typeName::TypeId: { \
  1938. auto castedInst = (typeName*)inst;\
  1939. str += name; \
  1940. str += " "; \
  1941. dc.ToString(str, castedInst->member1); \
  1942. str += ", "; \
  1943. dc.ToString(str, castedInst->member2); \
  1944. }
  1945. #define DISPLAY_INST3(typeName, name, member1, member2, member3) \
  1946. case typeName::TypeId: { \
  1947. auto castedInst = (typeName*)inst;\
  1948. str += name; \
  1949. str += " "; \
  1950. dc.ToString(str, castedInst->member1); \
  1951. str += ", "; \
  1952. dc.ToString(str, castedInst->member2); \
  1953. if ((std::is_pointer<decltype(castedInst->member3)>::value) && (castedInst->member3 != NULL)) \
  1954. { \
  1955. str += ", "; \
  1956. str += dc.ToString(castedInst->member3); \
  1957. } \
  1958. } \
  1959. break;
  1960. #define DISPLAY_INST4(typeName, name, member1, member2, member3, member4) \
  1961. case typeName::TypeId: { \
  1962. auto castedInst = (typeName*)inst;\
  1963. str += name; \
  1964. str += " "; \
  1965. dc.ToString(str, castedInst->member1); \
  1966. str += ", "; \
  1967. dc.ToString(str, castedInst->member2); \
  1968. if ((std::is_pointer<decltype(castedInst->member3)>::value) && (castedInst->member3 != NULL)) \
  1969. { \
  1970. str += ", "; \
  1971. dc.ToString(str, castedInst->member3); \
  1972. if ((std::is_pointer<decltype(castedInst->member4)>::value) && (castedInst->member4 != NULL)) \
  1973. { \
  1974. str += ", "; \
  1975. dc.ToString(str, castedInst->member4); \
  1976. } \
  1977. } \
  1978. } \
  1979. break;
  1980. HashSet<BeDbgLoc*> seenInlinedAt;
  1981. BeDbgLoc* lastDbgLoc = NULL;
  1982. HashSet<BeDbgLoc*> prevDbgLocs;
  1983. for (int blockIdx = 0; blockIdx < (int)func->mBlocks.size(); blockIdx++)
  1984. {
  1985. auto beBlock = func->mBlocks[blockIdx];
  1986. if (blockIdx > 0)
  1987. str += "\n";
  1988. dc.ToString(str, beBlock);
  1989. str += ":\n";
  1990. for (auto inst : beBlock->mInstructions)
  1991. {
  1992. if (inst->mDbgLoc != NULL)
  1993. {
  1994. if ((inst->mDbgLoc != lastDbgLoc) && (lastDbgLoc != NULL))
  1995. {
  1996. // if (inst->mDbgLoc->mIdx < lastDbgLoc->mIdx)
  1997. // {
  1998. // str += "WARNING: Out-of-order debug locations:\n";
  1999. // }
  2000. if ((inst->mDbgLoc->mDbgInlinedAt != lastDbgLoc->mDbgInlinedAt) && (inst->mDbgLoc->mDbgInlinedAt != NULL))
  2001. {
  2002. prevDbgLocs.Clear();
  2003. auto prevInlinedAt = lastDbgLoc->mDbgInlinedAt;
  2004. while (prevInlinedAt != NULL)
  2005. {
  2006. prevDbgLocs.Add(prevInlinedAt);
  2007. prevInlinedAt = prevInlinedAt->mDbgInlinedAt;
  2008. }
  2009. auto curInlinedAt = inst->mDbgLoc->mDbgInlinedAt;
  2010. if (!prevDbgLocs.Contains(curInlinedAt))
  2011. {
  2012. if (!seenInlinedAt.Add(curInlinedAt))
  2013. {
  2014. str += "WARNING: Adding new span of already-seen inlined location:\n";
  2015. }
  2016. }
  2017. }
  2018. }
  2019. lastDbgLoc = inst->mDbgLoc;
  2020. }
  2021. str += " ";
  2022. if (inst->CanBeReferenced())
  2023. {
  2024. str += dc.ToString(inst, false);
  2025. str += " = ";
  2026. }
  2027. switch (inst->GetTypeId())
  2028. {
  2029. DISPLAY_INST0(BeNopInst, "nop");
  2030. DISPLAY_INST0(BeUnreachableInst, "unreachable");
  2031. DISPLAY_INST0(BeEnsureInstructionAtInst, "ensureCodeAt");
  2032. DISPLAY_INST1(BeUndefValueInst, "undef", mType);
  2033. DISPLAY_INST2(BeExtractValueInst, "extractValue", mAggVal, mIdx);
  2034. DISPLAY_INST3(BeInsertValueInst, "insertValue", mAggVal, mMemberVal, mIdx);
  2035. DISPLAY_INST2_OPEN(BeNumericCastInst, "numericCast", mValue, mToType)
  2036. {
  2037. auto castedInst = (BeNumericCastInst*)inst;
  2038. if (castedInst->mValSigned)
  2039. str += " s->";
  2040. else
  2041. str += " u->";
  2042. if (castedInst->mToSigned)
  2043. str += "s";
  2044. else
  2045. str += "u";
  2046. }
  2047. break;
  2048. DISPLAY_INST2(BeBitCastInst, "bitCast", mValue, mToType);
  2049. DISPLAY_INST1(BeNegInst, "neg", mValue);
  2050. DISPLAY_INST1(BeNotInst, "not", mValue);
  2051. DISPLAY_INST3(BeBinaryOpInst, "binOp", mLHS, mOpKind, mRHS);
  2052. /*{
  2053. auto castedInst = (BeAddInst*)inst;
  2054. str += "add ";
  2055. str += dc.ToString(castedInst->mLHS);
  2056. str += ", ";
  2057. str += dc.ToString(castedInst->mRHS, false);
  2058. }
  2059. break;*/
  2060. DISPLAY_INST3(BeCmpInst, "cmp", mCmpKind, mLHS, mRHS);
  2061. DISPLAY_INST1(BeObjectAccessCheckInst, "objectAccessCheck", mValue);
  2062. case BeAllocaInst::TypeId:
  2063. {
  2064. auto castedInst = (BeAllocaInst*)inst;
  2065. str += "alloca ";
  2066. ToString(str, castedInst->mType);
  2067. if (castedInst->mArraySize != NULL)
  2068. {
  2069. str += ", ";
  2070. dc.ToString(str, castedInst->mArraySize);
  2071. }
  2072. str += ", align ";
  2073. dc.ToString(str, castedInst->mAlign);
  2074. }
  2075. break;
  2076. DISPLAY_INST1(BeAliasValueInst, "aliasvalue", mPtr);
  2077. DISPLAY_INST1(BeLifetimeStartInst, "lifetime.start", mPtr);
  2078. DISPLAY_INST1(BeLifetimeEndInst, "lifetime.end", mPtr);
  2079. DISPLAY_INST2(BeLifetimeFenceInst, "lifetime.fence", mFenceBlock, mPtr);
  2080. DISPLAY_INST0(BeValueScopeStartInst, "valueScope.start");
  2081. DISPLAY_INST1(BeValueScopeRetainInst, "valueScope.retain", mValue);
  2082. DISPLAY_INST1(BeValueScopeEndInst, ((BeValueScopeEndInst*)inst)->mIsSoft ? "valueScope.softEnd" : "valueScope.hardEnd", mScopeStart);
  2083. DISPLAY_INST1(BeLifetimeExtendInst, "lifetime.extend", mPtr);
  2084. case BeLoadInst::TypeId:
  2085. {
  2086. auto castedInst = (BeLoadInst*)inst;
  2087. str += "load ";
  2088. if (castedInst->mIsVolatile)
  2089. str += "volatile ";
  2090. ToString(str, inst->GetType());
  2091. str += ", ";
  2092. dc.ToString(str, castedInst->mTarget);
  2093. }
  2094. break;
  2095. case BeStoreInst::TypeId:
  2096. {
  2097. auto castedInst = (BeStoreInst*)inst;
  2098. str += "store ";
  2099. if (castedInst->mIsVolatile)
  2100. str += "volatile ";
  2101. dc.ToString(str, castedInst->mVal);
  2102. str += ", ";
  2103. dc.ToString(str, castedInst->mPtr);
  2104. }
  2105. break;
  2106. DISPLAY_INST1(BeSetCanMergeInst, "setCanMerge", mVal);
  2107. DISPLAY_INST4(BeMemSetInst, "memset", mAddr, mVal, mSize, mAlignment);
  2108. DISPLAY_INST0(BeFenceInst, "fence");
  2109. DISPLAY_INST0(BeStackSaveInst, "stackSave");
  2110. DISPLAY_INST1(BeStackRestoreInst, "stackRestore", mStackVal);
  2111. DISPLAY_INST3(BeGEPInst, "gep", mPtr, mIdx0, mIdx1);
  2112. //DISPLAY_INST1(BeBrInst, "br", mTargetBlock);
  2113. case BeBrInst::TypeId:
  2114. {
  2115. auto castedInst = (BeBrInst*)inst;
  2116. if (castedInst->mNoCollapse)
  2117. str += "br NoCollapse ";
  2118. else if (castedInst->mIsFake)
  2119. str += "br Fake ";
  2120. else
  2121. str += "br ";
  2122. dc.ToString(str, castedInst->mTargetBlock);
  2123. }
  2124. break;
  2125. DISPLAY_INST3(BeCondBrInst, "condbr", mCond, mTrueBlock, mFalseBlock);
  2126. case BeRetInst::TypeId:
  2127. {
  2128. auto castedInst = (BeRetInst*)inst;
  2129. str += "ret";
  2130. if (castedInst->mRetValue != NULL)
  2131. {
  2132. str += " ";
  2133. dc.ToString(str, castedInst->mRetValue);
  2134. }
  2135. else
  2136. str += " void";
  2137. }
  2138. break;
  2139. DISPLAY_INST2(BeSetRetInst, "setret", mRetValue, mReturnTypeId);
  2140. case BeCallInst::TypeId:
  2141. {
  2142. auto castedInst = (BeCallInst*)inst;
  2143. if (castedInst->mInlineResult != NULL)
  2144. {
  2145. str += "InlineResult: ";
  2146. dc.ToString(str, castedInst->mInlineResult);
  2147. break;
  2148. }
  2149. if (castedInst->mTailCall)
  2150. str += "tail ";
  2151. str += "call ";
  2152. dc.ToString(str, castedInst->mFunc);
  2153. str += "(";
  2154. for (int argIdx = 0; argIdx < (int)castedInst->mArgs.size(); argIdx++)
  2155. {
  2156. auto& arg = castedInst->mArgs[argIdx];
  2157. if (argIdx > 0)
  2158. str += ", ";
  2159. str += dc.ToString(arg.mValue);
  2160. if (arg.mStructRet)
  2161. str += " sret";
  2162. if (arg.mZExt)
  2163. str += " zext";
  2164. if (arg.mNoAlias)
  2165. str += " noalias";
  2166. if (arg.mNoCapture)
  2167. str += " nocapture";
  2168. if (arg.mDereferenceableSize != -1)
  2169. str += StrFormat(" dereferenceable(%d)", arg.mDereferenceableSize);
  2170. }
  2171. str += ")";
  2172. if (castedInst->mNoReturn)
  2173. str += " noreturn";
  2174. }
  2175. break;
  2176. case BePhiInst::TypeId:
  2177. {
  2178. auto castedInst = (BePhiInst*)inst;
  2179. str += "phi ";
  2180. dc.ToString(str, castedInst->mType);
  2181. str += " ";
  2182. for (int argIdx = 0; argIdx < (int)castedInst->mIncoming.size(); argIdx++)
  2183. {
  2184. if (argIdx > 0)
  2185. str += ", ";
  2186. str += "[";
  2187. dc.ToString(str, castedInst->mIncoming[argIdx]->mValue);
  2188. str += ", ";
  2189. dc.ToString(str, castedInst->mIncoming[argIdx]->mBlock);
  2190. str += "]";
  2191. }
  2192. }
  2193. break;
  2194. case BeSwitchInst::TypeId:
  2195. {
  2196. auto castedInst = (BeSwitchInst*)inst;
  2197. str += "switch ";
  2198. dc.ToString(str, castedInst->mValue);
  2199. str += " ";
  2200. dc.ToString(str, castedInst->mDefaultBlock);
  2201. str += " [";
  2202. for (int argIdx = 0; argIdx < (int)castedInst->mCases.size(); argIdx++)
  2203. {
  2204. str += "\n ";
  2205. dc.ToString(str, castedInst->mCases[argIdx].mValue);
  2206. str += ", ";
  2207. dc.ToString(str, castedInst->mCases[argIdx].mBlock);
  2208. }
  2209. str += "]";
  2210. }
  2211. break;
  2212. DISPLAY_INST2_OPEN(BeDbgDeclareInst, "DbgDeclare", mDbgVar, mValue);
  2213. {
  2214. auto castedInst = (BeDbgDeclareInst*)inst;
  2215. if (castedInst->mIsValue)
  2216. str += " <val>";
  2217. else
  2218. str += " <addr>";
  2219. if (auto dbgVariable = castedInst->mDbgVar)
  2220. {
  2221. switch (dbgVariable->mInitType)
  2222. {
  2223. case BfIRInitType_NotNeeded: str += " noinit"; break;
  2224. case BfIRInitType_NotNeeded_AliveOnDecl: str += " noinit_aliveondecl"; break;
  2225. case BfIRInitType_Uninitialized: str += " uninit"; break;
  2226. case BfIRInitType_Zero: str += " zero"; break;
  2227. }
  2228. if (dbgVariable->mScope != NULL)
  2229. {
  2230. str += " Scope:";
  2231. dc.ToString(str, dbgVariable->mScope);
  2232. }
  2233. }
  2234. }
  2235. break;
  2236. DISPLAY_INST1(BeComptimeError, "ComptimeError", mError);
  2237. DISPLAY_INST1(BeComptimeGetType, "ComptimeGetType", mTypeId);
  2238. DISPLAY_INST1(BeComptimeGetReflectType, "ComptimeGetReflectType", mTypeId);
  2239. DISPLAY_INST2(BeComptimeDynamicCastCheck, "ComptimeDynamicCastCheck", mValue, mTypeId);
  2240. DISPLAY_INST2(BeComptimeGetVirtualFunc, "ComptimeGetVirtualFunc", mValue, mVirtualTableIdx);
  2241. DISPLAY_INST3(BeComptimeGetInterfaceFunc, "ComptimeGetInterfaceFunc", mValue, mIFaceTypeId, mMethodIdx);
  2242. default:
  2243. BF_FATAL("Notimpl");
  2244. str += "<UNKNOWN INST>";
  2245. break;
  2246. }
  2247. if (inst->mDbgLoc != NULL)
  2248. {
  2249. dbgLocs[inst->mDbgLoc->mIdx] = inst->mDbgLoc;
  2250. str += StrFormat(" @%d", inst->mDbgLoc->mIdx);
  2251. auto dbgFile = inst->mDbgLoc->GetDbgFile();
  2252. if (dbgFile != NULL)
  2253. {
  2254. str += "[";
  2255. str += dbgFile->mFileName;
  2256. str += StrFormat(":%d", inst->mDbgLoc->mLine + 1);
  2257. if (inst->mDbgLoc->mDbgInlinedAt != NULL)
  2258. {
  2259. str += ",inl";
  2260. BeDbgLoc* inlinedAt = inst->mDbgLoc->mDbgInlinedAt;
  2261. while (inlinedAt != NULL)
  2262. {
  2263. str += StrFormat("#%d", inlinedAt->mIdx);
  2264. inlinedAt = inlinedAt->mDbgInlinedAt;
  2265. }
  2266. }
  2267. str += "]";
  2268. }
  2269. }
  2270. str += "\n";
  2271. }
  2272. }
  2273. str += "}\n";
  2274. if (func->mDbgFunction != NULL)
  2275. {
  2276. str += " DbgFunc: ";
  2277. dc.ToString(str, func->mDbgFunction);
  2278. str += "\n";
  2279. for (auto dbgVar : func->mDbgFunction->mVariables)
  2280. {
  2281. if (dbgVar == NULL)
  2282. continue;
  2283. str += StrFormat(" Var: ");
  2284. str += dbgVar->mName;
  2285. str += " ";
  2286. dc.ToString(str, dbgVar->mScope);
  2287. str += "\n";
  2288. }
  2289. }
  2290. str += "\n";
  2291. }
  2292. for (auto& dbgLocPair : dbgLocs)
  2293. {
  2294. auto dbgLoc = dbgLocPair.mValue;
  2295. dc.ToString(str, dbgLocPair.mValue);
  2296. str += "\n";
  2297. }
  2298. str += "\n";
  2299. return str;
  2300. }
  2301. void BeModule::Print()
  2302. {
  2303. OutputDebugStr(ToString());
  2304. }
  2305. void BeModule::Print(BeFunction* func)
  2306. {
  2307. OutputDebugStr(ToString(func));
  2308. }
  2309. void BeModule::PrintValue(BeValue* val)
  2310. {
  2311. BeDumpContext dumpCtx;
  2312. String str;
  2313. dumpCtx.ToString(str, val);
  2314. str += "\n";
  2315. OutputDebugStr(str);
  2316. auto type = val->GetType();
  2317. if (type != NULL)
  2318. bpt(type);
  2319. }
  2320. void BeModule::DoInlining(BeFunction* func)
  2321. {
  2322. //bool debugging = func->mName == "?Test@Program@bf@@CAXXZ";
  2323. //bool debugging = func->mName == "?TestA@TestClass@Bro@Dude@Hey@@SAX_J@Z";
  2324. //debugging |= func->mName == "?TestB@TestClass@Bro@Dude@Hey@@SAX_J@Z";
  2325. //debugging |= func->mName == "?TestC@TestClass@Bro@Dude@Hey@@SAX_J@Z";
  2326. // if (debugging)
  2327. // {
  2328. // Print(func);
  2329. // }
  2330. if (func->mDidInlinePass)
  2331. return;
  2332. // Set this true here so we don't recurse on the same function
  2333. func->mDidInlinePass = true;
  2334. int numHeadAllocas = 0;
  2335. bool inHeadAllocas = true;
  2336. int blockIdx = 0;
  2337. // From head to resume
  2338. std::unordered_multimap<BeBlock*, BeBlock*> inlineResumesMap;
  2339. // From resume to head
  2340. std::unordered_multimap<BeBlock*, BeBlock*> inlineHeadMap;
  2341. bool hadInlining = false;
  2342. std::function<void(int& blockIdx, BeBlock* endBlock, std::unordered_set<BeFunction*>& funcInlined)> _DoInlining;
  2343. _DoInlining = [&](int& blockIdx, BeBlock* endBlock, std::unordered_set<BeFunction*>& funcInlined)
  2344. {
  2345. for (; blockIdx < (int)func->mBlocks.size(); blockIdx++)
  2346. {
  2347. auto beBlock = func->mBlocks[blockIdx];
  2348. if (beBlock == endBlock)
  2349. {
  2350. // Let previous handler deal with this
  2351. --blockIdx;
  2352. return;
  2353. }
  2354. for (int instIdx = 0; instIdx < (int)beBlock->mInstructions.size(); instIdx++)
  2355. {
  2356. auto inst = beBlock->mInstructions[instIdx];
  2357. if (inHeadAllocas)
  2358. {
  2359. switch (inst->GetTypeId())
  2360. {
  2361. case BeAllocaInst::TypeId:
  2362. case BeNumericCastInst::TypeId:
  2363. case BeBitCastInst::TypeId:
  2364. numHeadAllocas++;
  2365. default:
  2366. inHeadAllocas = false;
  2367. }
  2368. }
  2369. if (auto phiInst = BeValueDynCast<BePhiInst>(inst))
  2370. {
  2371. for (auto incoming : phiInst->mIncoming)
  2372. {
  2373. bool found = false;
  2374. auto _CheckBlock = [&](BeBlock* checkBlock)
  2375. {
  2376. for (auto inst : checkBlock->mInstructions)
  2377. {
  2378. switch (inst->GetTypeId())
  2379. {
  2380. case BeBrInst::TypeId:
  2381. {
  2382. auto castedInst = (BeBrInst*)inst;
  2383. if (castedInst->mTargetBlock == beBlock)
  2384. {
  2385. found = true;
  2386. }
  2387. }
  2388. break;
  2389. case BeCondBrInst::TypeId:
  2390. {
  2391. auto castedInst = (BeCondBrInst*)inst;
  2392. if ((castedInst->mTrueBlock == beBlock) ||
  2393. (castedInst->mFalseBlock == beBlock))
  2394. {
  2395. found = true;
  2396. }
  2397. }
  2398. break;
  2399. case BeSwitchInst::TypeId:
  2400. {
  2401. auto castedInst = (BeSwitchInst*)inst;
  2402. if (castedInst->mDefaultBlock == beBlock)
  2403. found = true;
  2404. for (auto& caseVal : castedInst->mCases)
  2405. if (caseVal.mBlock == beBlock)
  2406. found = true;
  2407. }
  2408. break;
  2409. };
  2410. };
  2411. if (found)
  2412. {
  2413. incoming->mBlock = checkBlock;
  2414. return;
  2415. }
  2416. };
  2417. _CheckBlock(incoming->mBlock);
  2418. auto itr = inlineResumesMap.find(incoming->mBlock);
  2419. while (itr != inlineResumesMap.end())
  2420. {
  2421. if (found)
  2422. break;
  2423. if (itr->first != incoming->mBlock)
  2424. break;
  2425. auto checkBlock = itr->second;
  2426. _CheckBlock(checkBlock);
  2427. ++itr;
  2428. }
  2429. BF_ASSERT(found);
  2430. }
  2431. }
  2432. auto callInst = BeValueDynCast<BeCallInst>(inst);
  2433. if (callInst == NULL)
  2434. continue;
  2435. auto inlineFunc = BeValueDynCast<BeFunction>(callInst->mFunc);
  2436. if (inlineFunc == NULL)
  2437. continue;
  2438. if (inlineFunc == func)
  2439. continue;
  2440. if (!inlineFunc->mAlwaysInline)
  2441. continue;
  2442. if (inlineFunc->mBlocks.empty())
  2443. {
  2444. BF_FATAL("No content?");
  2445. continue;
  2446. }
  2447. // It's more efficient to do depth-first inlining so nested inlines will be pre-expanded
  2448. DoInlining(inlineFunc);
  2449. //TODO: Not needed anymore, right?
  2450. if (funcInlined.find(inlineFunc) != funcInlined.end())
  2451. continue; // Don't recursively inline
  2452. // Incase we have multiple inlines from the same location, those need to have unique dbgLocs
  2453. callInst->mDbgLoc = DupDebugLocation(callInst->mDbgLoc);
  2454. hadInlining = true;
  2455. BeInliner inliner;
  2456. inliner.mAlloc = &mAlloc;
  2457. inliner.mOwnedValueVec = &mOwnedValues;
  2458. inliner.mModule = this;
  2459. inliner.mSrcFunc = inlineFunc;
  2460. inliner.mDestFunc = func;
  2461. inliner.mCallInst = callInst;
  2462. if ((func->mDbgFunction != NULL) && (inlineFunc->mDbgFunction != NULL))
  2463. {
  2464. //BeDbgLexicalBlock
  2465. for (int srcVarIdx = 0; srcVarIdx < (int)inlineFunc->mDbgFunction->mVariables.size(); srcVarIdx++)
  2466. {
  2467. auto dbgGlobalVar = inlineFunc->mDbgFunction->mVariables[srcVarIdx];
  2468. if (dbgGlobalVar == NULL)
  2469. continue;
  2470. auto destDbgGlobalVar = mOwnedValues.Alloc<BeDbgVariable>();
  2471. destDbgGlobalVar->mName = dbgGlobalVar->mName;
  2472. destDbgGlobalVar->mType = dbgGlobalVar->mType;
  2473. destDbgGlobalVar->mInitType = dbgGlobalVar->mInitType;
  2474. if (dbgGlobalVar->mValue != NULL)
  2475. {
  2476. BF_ASSERT(BeValueDynCast<BeConstant>(dbgGlobalVar->mValue) != NULL);
  2477. destDbgGlobalVar->mValue = dbgGlobalVar->mValue;
  2478. }
  2479. else
  2480. BF_ASSERT(dbgGlobalVar->mValue == NULL);
  2481. destDbgGlobalVar->mScope = (BeMDNode*)inliner.Remap(dbgGlobalVar->mScope);
  2482. inliner.mValueMap[dbgGlobalVar] = destDbgGlobalVar;
  2483. func->mDbgFunction->mVariables.push_back(destDbgGlobalVar);
  2484. }
  2485. }
  2486. //int prevBlockSize = func->mBlocks.size();
  2487. // Split block, with calls that come after the call going into inlineResume
  2488. BeBlock* returnBlock = mOwnedValues.Alloc<BeBlock>();
  2489. returnBlock->mName = "inlineResume";
  2490. returnBlock->mFunction = func;
  2491. func->mBlocks.Insert(blockIdx + 1, returnBlock);
  2492. for (int srcIdx = instIdx + 1; srcIdx < (int)beBlock->mInstructions.size(); srcIdx++)
  2493. returnBlock->mInstructions.push_back(beBlock->mInstructions[srcIdx]);
  2494. beBlock->mInstructions.RemoveRange(instIdx, beBlock->mInstructions.size() - instIdx);
  2495. /*auto _InsertResume = (BeBlock* beBlock, BeBlock* returnBlock)[&]
  2496. {
  2497. inlineResumesMap.insert(std::make_pair(beBlock, returnBlock));
  2498. inlineHeadMap.insert(std::make_pair(returnBlock, beBlock));
  2499. auto prevHeadItr = inlineHeadMap.find(beBlock);
  2500. };
  2501. _InsertResume(beBlock, returnBlock);*/
  2502. auto headBlock = beBlock;
  2503. while (true)
  2504. {
  2505. auto itr = inlineHeadMap.find(headBlock);
  2506. if (itr == inlineHeadMap.end())
  2507. break;
  2508. headBlock = itr->second;
  2509. }
  2510. inlineResumesMap.insert(std::make_pair(headBlock, returnBlock));
  2511. inlineHeadMap.insert(std::make_pair(returnBlock, headBlock));
  2512. std::vector<BeBlock*> destBlocks;
  2513. for (int argIdx = 0; argIdx < (int)callInst->mArgs.size(); argIdx++)
  2514. {
  2515. auto& argVal = callInst->mArgs[argIdx];
  2516. inliner.mValueMap[GetArgument(argIdx)] = argVal.mValue;
  2517. }
  2518. for (int inlineBlockIdx = 0; inlineBlockIdx < (int)inlineFunc->mBlocks.size(); inlineBlockIdx++)
  2519. {
  2520. auto srcBlock = inlineFunc->mBlocks[inlineBlockIdx];
  2521. auto destBlock = mOwnedValues.Alloc<BeBlock>();
  2522. destBlock->mFunction = func;
  2523. destBlock->mName = inlineFunc->mName;
  2524. destBlock->mName += "_";
  2525. destBlock->mName += srcBlock->mName;
  2526. if (inlineBlockIdx == 0)
  2527. {
  2528. auto brInst = mAlloc.Alloc<BeBrInst>();
  2529. brInst->mDbgLoc = inst->mDbgLoc;
  2530. brInst->mTargetBlock = destBlock;
  2531. beBlock->mInstructions.push_back(brInst);
  2532. }
  2533. func->mBlocks.Insert(blockIdx + 1 + inlineBlockIdx, destBlock);
  2534. destBlocks.push_back(destBlock);
  2535. inliner.mValueMap[srcBlock] = destBlock;
  2536. }
  2537. bool inlineInHeadAllocas = true;
  2538. for (int inlineBlockIdx = 0; inlineBlockIdx < (int)inlineFunc->mBlocks.size(); inlineBlockIdx++)
  2539. {
  2540. auto srcBlock = inlineFunc->mBlocks[inlineBlockIdx];
  2541. auto destBlock = destBlocks[inlineBlockIdx];
  2542. inliner.mDestBlock = destBlock;
  2543. for (int srcInstIdx = 0; srcInstIdx < (int)srcBlock->mInstructions.size(); srcInstIdx++)
  2544. {
  2545. auto srcInst = srcBlock->mInstructions[srcInstIdx];
  2546. if (inlineInHeadAllocas)
  2547. {
  2548. if (srcInst->GetTypeId() == BeAllocaInst::TypeId)
  2549. {
  2550. BeAllocaInst* allocaInst = (BeAllocaInst*)srcInst;
  2551. auto destAlloca = mAlloc.Alloc<BeAllocaInst>();
  2552. destAlloca->mType = allocaInst->mType;
  2553. destAlloca->mArraySize = allocaInst->mArraySize;
  2554. destAlloca->mAlign = allocaInst->mAlign;
  2555. destAlloca->mNoChkStk = allocaInst->mNoChkStk;
  2556. destAlloca->mForceMem = allocaInst->mForceMem;
  2557. destAlloca->mName = allocaInst->mName;
  2558. auto destBlock = func->mBlocks[0];
  2559. destAlloca->mParentBlock = destBlock;
  2560. destBlock->mInstructions.Insert(numHeadAllocas, destAlloca);
  2561. numHeadAllocas++;
  2562. inliner.mValueMap[allocaInst] = destAlloca;
  2563. continue;
  2564. }
  2565. else
  2566. inlineInHeadAllocas = false;
  2567. }
  2568. if (auto storeInst = BeValueDynCast<BeStoreInst>(srcInst))
  2569. {
  2570. if (auto argVal = BeValueDynCast<BeArgument>(storeInst->mVal))
  2571. {
  2572. // This doesn't solve the 'SRET' issue of allowing a single-value return
  2573. // in a SRET function to directly map the returned value to the incoming
  2574. // SRET pointer, since that relies on setting a function-wide
  2575. // mCompositeRetVRegIdx value. Possible future optimization.
  2576. auto setCanMergeInst = mAlloc.Alloc<BeSetCanMergeInst>();
  2577. setCanMergeInst->mVal = inliner.Remap(storeInst->mPtr);
  2578. inliner.AddInst(setCanMergeInst, NULL);
  2579. }
  2580. }
  2581. if (auto retInst = BeValueDynCast<BeRetInst>(srcInst))
  2582. {
  2583. callInst->mInlineResult = inliner.Remap(retInst->mRetValue);
  2584. callInst->mFunc = NULL;
  2585. callInst->mArgs.clear();
  2586. callInst->mNoReturn = false;
  2587. callInst->mTailCall = false;
  2588. if (retInst->mRetValue != NULL)
  2589. {
  2590. // We want to ensure that we can step onto the closing brace to see the __return value
  2591. auto brInst = mAlloc.Alloc<BeEnsureInstructionAtInst>();
  2592. inliner.AddInst(brInst, retInst);
  2593. auto fenceInst = mAlloc.Alloc<BeLifetimeFenceInst>();
  2594. fenceInst->mFenceBlock = beBlock;
  2595. fenceInst->mPtr = callInst->mInlineResult;
  2596. inliner.AddInst(fenceInst, retInst);
  2597. }
  2598. auto brInst = mAlloc.Alloc<BeBrInst>();
  2599. brInst->mTargetBlock = returnBlock;
  2600. inliner.AddInst(brInst, retInst);
  2601. }
  2602. else
  2603. inliner.VisitChild(srcInst);
  2604. }
  2605. }
  2606. /*if (callInst->mInlineResult != NULL)
  2607. {
  2608. auto fenceInst = mAlloc.Alloc<BeLifetimeFenceInst>();
  2609. fenceInst->mPtr = callInst->mInlineResult;
  2610. beBlock->mInstructions.push_back(fenceInst);
  2611. }*/
  2612. auto inlinedFuncInlined = funcInlined;
  2613. inlinedFuncInlined.insert(inlineFunc);
  2614. _DoInlining(blockIdx, returnBlock, inlinedFuncInlined);
  2615. }
  2616. }
  2617. };
  2618. /*int prevDbgVars = 0;
  2619. if (func->mDbgFunction != NULL)
  2620. prevDbgVars = (int)func->mDbgFunction->mVariables.size();*/
  2621. std::unordered_set<BeFunction*> newFuncSet;
  2622. _DoInlining(blockIdx, NULL, newFuncSet);
  2623. /*if ((func->mDbgFunction != NULL) && (prevDbgVars != (int)func->mDbgFunction->mVariables.size()))
  2624. {
  2625. std::stable_sort(func->mDbgFunction->mVariables.begin(), func->mDbgFunction->mVariables.end(), [] (BeDbgVariable* lhs, BeDbgVariable* rhs)
  2626. {
  2627. BeDbgLoc* lhsInlinePos = NULL;
  2628. if (lhs->mDeclDbgLoc != NULL)
  2629. lhsInlinePos = lhs->mDeclDbgLoc->mDbgInlinedAt;
  2630. BeDbgLoc* rhsInlinePos = NULL;
  2631. if (rhs->mDeclDbgLoc != NULL)
  2632. rhsInlinePos = rhs->mDeclDbgLoc->mDbgInlinedAt;
  2633. if ((lhsInlinePos == NULL) || (rhsInlinePos == NULL))
  2634. {
  2635. if ((lhsInlinePos == NULL) && (rhsInlinePos != NULL))
  2636. return true;
  2637. return false;
  2638. }
  2639. return lhsInlinePos->mIdx < rhsInlinePos->mIdx;
  2640. });
  2641. }*/
  2642. }
  2643. void BeModule::DoInlining()
  2644. {
  2645. BP_ZONE("BeModule::DoInlining");
  2646. for (auto func : mFunctions)
  2647. {
  2648. DoInlining(func);
  2649. }
  2650. }
  2651. BeCmpKind BeModule::InvertCmp(BeCmpKind cmpKind)
  2652. {
  2653. switch (cmpKind)
  2654. {
  2655. case BeCmpKind_SLT:
  2656. return BeCmpKind_SGE;
  2657. case BeCmpKind_ULT:
  2658. return BeCmpKind_UGE;
  2659. case BeCmpKind_SLE:
  2660. return BeCmpKind_SGT;
  2661. case BeCmpKind_ULE:
  2662. return BeCmpKind_UGT;
  2663. case BeCmpKind_EQ:
  2664. return BeCmpKind_NE;
  2665. case BeCmpKind_NE:
  2666. return BeCmpKind_EQ;
  2667. case BeCmpKind_SGT:
  2668. return BeCmpKind_SLE;
  2669. case BeCmpKind_UGT:
  2670. return BeCmpKind_ULE;
  2671. case BeCmpKind_SGE:
  2672. return BeCmpKind_SLT;
  2673. case BeCmpKind_UGE:
  2674. return BeCmpKind_ULT;
  2675. }
  2676. return cmpKind;
  2677. }
  2678. BeCmpKind BeModule::SwapCmpSides(BeCmpKind cmpKind)
  2679. {
  2680. switch (cmpKind)
  2681. {
  2682. case BeCmpKind_SLT:
  2683. return BeCmpKind_SGT;
  2684. case BeCmpKind_ULT:
  2685. return BeCmpKind_UGT;
  2686. case BeCmpKind_SLE:
  2687. return BeCmpKind_SGE;
  2688. case BeCmpKind_ULE:
  2689. return BeCmpKind_UGE;
  2690. case BeCmpKind_EQ:
  2691. return BeCmpKind_EQ;
  2692. case BeCmpKind_NE:
  2693. return BeCmpKind_NE;
  2694. case BeCmpKind_SGT:
  2695. return BeCmpKind_SLT;
  2696. case BeCmpKind_UGT:
  2697. return BeCmpKind_ULT;
  2698. case BeCmpKind_SGE:
  2699. return BeCmpKind_SLE;
  2700. case BeCmpKind_UGE:
  2701. return BeCmpKind_ULE;
  2702. }
  2703. return cmpKind;
  2704. }
  2705. void BeModule::AddInst(BeInst* inst)
  2706. {
  2707. inst->mDbgLoc = mCurDbgLoc;
  2708. inst->mParentBlock = mActiveBlock;
  2709. if (mInsertPos == -1)
  2710. {
  2711. mActiveBlock->mInstructions.push_back(inst);
  2712. }
  2713. else
  2714. {
  2715. mActiveBlock->mInstructions.Insert(mInsertPos, inst);
  2716. mInsertPos++;
  2717. }
  2718. //inst->mFuncRelId = mActiveBlock->mFunction->mCurElementId++;
  2719. }
  2720. void BeModule::ToString(StringImpl& str, BeType* type)
  2721. {
  2722. if (type == NULL)
  2723. {
  2724. str += "<MissingType>";
  2725. return;
  2726. }
  2727. switch (type->mTypeCode)
  2728. {
  2729. case BeTypeCode_None:
  2730. str += "void";
  2731. return;
  2732. case BeTypeCode_NullPtr:
  2733. str += "null";
  2734. return;
  2735. case BeTypeCode_Boolean:
  2736. str += "bool";
  2737. return;
  2738. case BeTypeCode_Int8:
  2739. str += "i8";
  2740. return;
  2741. case BeTypeCode_Int16:
  2742. str += "i16";
  2743. return;
  2744. case BeTypeCode_Int32:
  2745. str += "i32";
  2746. return;
  2747. case BeTypeCode_Int64:
  2748. str += "i64";
  2749. return;
  2750. case BeTypeCode_Float:
  2751. str += "float";
  2752. return;
  2753. case BeTypeCode_Double:
  2754. str += "double";
  2755. return;
  2756. case BeTypeCode_Pointer:
  2757. ToString(str, ((BePointerType*)type)->mElementType);
  2758. str += "*";
  2759. return;
  2760. case BeTypeCode_Struct:
  2761. str += ((BeStructType*)type)->mName;
  2762. return;
  2763. case BeTypeCode_Function:
  2764. {
  2765. auto funcType = (BeFunctionType*)type;
  2766. ToString(str, funcType->mReturnType);
  2767. str += "(";
  2768. for (int paramIdx = 0; paramIdx < (int)funcType->mParams.size(); paramIdx++)
  2769. {
  2770. if (paramIdx > 0)
  2771. str += ", ";
  2772. ToString(str, funcType->mParams[paramIdx].mType);
  2773. }
  2774. if (funcType->mIsVarArg)
  2775. {
  2776. if (!funcType->mParams.IsEmpty())
  2777. str += ", ";
  2778. str += "...";
  2779. }
  2780. str += ")";
  2781. return;
  2782. }
  2783. case BeTypeCode_SizedArray:
  2784. {
  2785. auto arrayType = (BeSizedArrayType*)type;
  2786. ToString(str, arrayType->mElementType);
  2787. str += "[";
  2788. str += StrFormat("%d", arrayType->mLength);
  2789. str += "]";
  2790. return;
  2791. }
  2792. case BeTypeCode_Vector:
  2793. {
  2794. auto arrayType = (BeSizedArrayType*)type;
  2795. ToString(str, arrayType->mElementType);
  2796. str += "<";
  2797. str += StrFormat("%d", arrayType->mLength);
  2798. str += ">";
  2799. return;
  2800. }
  2801. }
  2802. str += "<UnknownType>";
  2803. }
  2804. void BeModule::SetActiveFunction(BeFunction* function)
  2805. {
  2806. mActiveFunction = function;
  2807. }
  2808. BeArgument* BeModule::GetArgument(int argIdx)
  2809. {
  2810. while ((int)argIdx >= mArgs.size())
  2811. {
  2812. auto arg = mAlloc.Alloc<BeArgument>();
  2813. arg->mModule = this;
  2814. arg->mArgIdx = (int)mArgs.size();
  2815. mArgs.push_back(arg);
  2816. }
  2817. return mArgs[argIdx];
  2818. }
  2819. BeBlock* BeModule::CreateBlock(const StringImpl& name)
  2820. {
  2821. auto block = mOwnedValues.Alloc<BeBlock>();
  2822. block->mName = name;
  2823. return block;
  2824. }
  2825. void BeModule::AddBlock(BeFunction* function, BeBlock* block)
  2826. {
  2827. block->mFunction = function;
  2828. function->mBlocks.push_back(block);
  2829. }
  2830. void BeModule::RemoveBlock(BeFunction* function, BeBlock* block)
  2831. {
  2832. bool didRemove = function->mBlocks.Remove(block);
  2833. BF_ASSERT(didRemove);
  2834. #ifdef _DEBUG
  2835. for (auto inst : block->mInstructions)
  2836. inst->mWasRemoved = true;
  2837. #endif
  2838. }
  2839. BeBlock* BeModule::GetInsertBlock()
  2840. {
  2841. return mActiveBlock;
  2842. }
  2843. void BeModule::SetInsertPoint(BeBlock* block)
  2844. {
  2845. mActiveBlock = block;
  2846. mInsertPos = -1;
  2847. }
  2848. void BeModule::SetInsertPointAtStart(BeBlock* block)
  2849. {
  2850. mActiveBlock = block;
  2851. mInsertPos = 0;
  2852. }
  2853. BeFunction* BeModule::CreateFunction(BeFunctionType* funcType, BfIRLinkageType linkageType, const StringImpl& name)
  2854. {
  2855. auto func = mOwnedValues.Alloc<BeFunction>();
  2856. func->mName = name;
  2857. func->mModule = this;
  2858. func->mType = mContext->GetPointerTo(funcType);
  2859. func->mLinkageType = linkageType;
  2860. func->mParams.Resize(funcType->mParams.size());
  2861. mFunctions.push_back(func);
  2862. #ifdef _DEBUG
  2863. // It IS possible hit this, especially if we have multiple intrinsics mapping to 'malloc' for example
  2864. //BF_ASSERT(mFunctionMap.TryAdd(name, func));
  2865. #endif
  2866. return func;
  2867. }
  2868. BeDbgLoc* BeModule::GetCurrentDebugLocation()
  2869. {
  2870. return mCurDbgLoc;
  2871. }
  2872. void BeModule::SetCurrentDebugLocation(BeDbgLoc* debugLoc)
  2873. {
  2874. mCurDbgLoc = debugLoc;
  2875. }
  2876. void BeModule::SetCurrentDebugLocation(int line, int column, BeMDNode* dbgScope, BeDbgLoc* dbgInlinedAt)
  2877. {
  2878. if (mCurDbgLoc == NULL)
  2879. mCurDbgLoc = mLastDbgLoc;
  2880. if ((mCurDbgLoc != NULL) &&
  2881. (mCurDbgLoc->mLine == line) &&
  2882. (mCurDbgLoc->mColumn == column) &&
  2883. (mCurDbgLoc->mDbgScope == dbgScope) &&
  2884. (mCurDbgLoc->mDbgInlinedAt == dbgInlinedAt))
  2885. return;
  2886. mCurDbgLoc = mAlloc.Alloc<BeDbgLoc>();
  2887. mCurDbgLoc->mLine = line;
  2888. mCurDbgLoc->mColumn = column;
  2889. mCurDbgLoc->mDbgScope = dbgScope;
  2890. mCurDbgLoc->mDbgInlinedAt = dbgInlinedAt;
  2891. mCurDbgLoc->mIdx = mCurDbgLocIdx++;
  2892. if ((dbgInlinedAt != NULL) && (!dbgInlinedAt->mHadInline))
  2893. {
  2894. dbgInlinedAt->mHadInline = true;
  2895. }
  2896. mLastDbgLoc = mCurDbgLoc;
  2897. }
  2898. BeDbgLoc* BeModule::DupDebugLocation(BeDbgLoc* dbgLoc)
  2899. {
  2900. if (dbgLoc == NULL)
  2901. return dbgLoc;
  2902. auto newDbgLoc = mAlloc.Alloc<BeDbgLoc>();
  2903. newDbgLoc->mLine = dbgLoc->mLine;
  2904. newDbgLoc->mColumn = dbgLoc->mColumn;
  2905. newDbgLoc->mDbgScope = dbgLoc->mDbgScope;
  2906. newDbgLoc->mDbgInlinedAt = dbgLoc->mDbgInlinedAt;
  2907. newDbgLoc->mIdx = mCurDbgLocIdx++;
  2908. if ((newDbgLoc->mDbgInlinedAt != NULL) && (!newDbgLoc->mDbgInlinedAt->mHadInline))
  2909. {
  2910. newDbgLoc->mDbgInlinedAt->mHadInline = true;
  2911. }
  2912. return newDbgLoc;
  2913. }
  2914. void BeModule::DupCurrentDebugLocation()
  2915. {
  2916. mCurDbgLoc = DupDebugLocation(mCurDbgLoc);
  2917. mLastDbgLoc = mCurDbgLoc;
  2918. }
  2919. BeNopInst* BeModule::CreateNop()
  2920. {
  2921. auto inst = mAlloc.Alloc<BeNopInst>();
  2922. AddInst(inst);
  2923. return inst;
  2924. }
  2925. BeUndefValueInst* BeModule::CreateUndefValue(BeType* type)
  2926. {
  2927. auto undefValue = AllocInst<BeUndefValueInst>();
  2928. undefValue->mType = type;
  2929. return undefValue;
  2930. }
  2931. BeNumericCastInst* BeModule::CreateNumericCast(BeValue* value, BeType* toType, bool valSigned, bool toSigned)
  2932. {
  2933. auto inst = mAlloc.Alloc<BeNumericCastInst>();
  2934. inst->mValue = value;
  2935. inst->mToType = toType;
  2936. inst->mValSigned = valSigned;
  2937. inst->mToSigned = toSigned;
  2938. BF_ASSERT(toType != NULL);
  2939. AddInst(inst);
  2940. return inst;
  2941. }
  2942. BeBitCastInst * BeModule::CreateBitCast(BeValue* value, BeType* toType)
  2943. {
  2944. auto inst = mAlloc.Alloc<BeBitCastInst>();
  2945. inst->mValue = value;
  2946. inst->mToType = toType;
  2947. AddInst(inst);
  2948. return inst;
  2949. }
  2950. BeCmpInst* BeModule::CreateCmp(BeCmpKind cmpKind, BeValue* lhs, BeValue* rhs)
  2951. {
  2952. auto inst = mAlloc.Alloc<BeCmpInst>();
  2953. inst->mCmpKind = cmpKind;
  2954. inst->mLHS = lhs;
  2955. inst->mRHS = rhs;
  2956. AddInst(inst);
  2957. return inst;
  2958. }
  2959. BeBinaryOpInst* BeModule::CreateBinaryOp(BeBinaryOpKind opKind, BeValue* lhs, BeValue* rhs, BfOverflowCheckKind overflowCheckKind)
  2960. {
  2961. #ifdef _DEBUG
  2962. auto leftType = lhs->GetType();
  2963. auto rightType = rhs->GetType();
  2964. BF_ASSERT(leftType == rightType);
  2965. #endif
  2966. auto inst = mAlloc.Alloc<BeBinaryOpInst>();
  2967. inst->mOpKind = opKind;
  2968. inst->mLHS = lhs;
  2969. inst->mRHS = rhs;
  2970. inst->mOverflowCheckKind = overflowCheckKind;
  2971. AddInst(inst);
  2972. return inst;
  2973. }
  2974. BeAllocaInst* BeModule::CreateAlloca(BeType* type)
  2975. {
  2976. auto inst = mAlloc.Alloc<BeAllocaInst>();
  2977. inst->mType = type;
  2978. AddInst(inst);
  2979. return inst;
  2980. }
  2981. BeLoadInst* BeModule::CreateLoad(BeValue* value, bool isVolatile)
  2982. {
  2983. auto inst = mAlloc.Alloc<BeLoadInst>();
  2984. inst->mTarget = value;
  2985. inst->mIsVolatile = isVolatile;
  2986. AddInst(inst);
  2987. return inst;
  2988. }
  2989. BeLoadInst* BeModule::CreateAlignedLoad(BeValue* value, int alignment, bool isVolatile)
  2990. {
  2991. BF_ASSERT(value->GetType()->IsPointer());
  2992. auto inst = mAlloc.Alloc<BeLoadInst>();
  2993. inst->mTarget = value;
  2994. inst->mIsVolatile = isVolatile;
  2995. AddInst(inst);
  2996. return inst;
  2997. }
  2998. BeStoreInst* BeModule::CreateStore(BeValue* val, BeValue* ptr, bool isVolatile)
  2999. {
  3000. BF_ASSERT(ptr->GetType()->IsPointer());
  3001. auto inst = mAlloc.Alloc<BeStoreInst>();
  3002. inst->mVal = val;
  3003. inst->mPtr = ptr;
  3004. inst->mIsVolatile = isVolatile;
  3005. AddInst(inst);
  3006. return inst;
  3007. }
  3008. BeStoreInst* BeModule::CreateAlignedStore(BeValue* val, BeValue* ptr, int alignment, bool isVolatile)
  3009. {
  3010. BF_ASSERT(ptr->GetType()->IsPointer());
  3011. auto inst = mAlloc.Alloc<BeStoreInst>();
  3012. inst->mVal = val;
  3013. inst->mPtr = ptr;
  3014. inst->mIsVolatile = isVolatile;
  3015. AddInst(inst);
  3016. return inst;
  3017. }
  3018. BeGEPInst* BeModule::CreateGEP(BeValue* ptr, BeValue* idx0, BeValue* idx1)
  3019. {
  3020. auto inst = mAlloc.Alloc<BeGEPInst>();
  3021. inst->mPtr = ptr;
  3022. inst->mIdx0 = idx0;
  3023. inst->mIdx1 = idx1;
  3024. AddInst(inst);
  3025. #ifdef _DEBUG
  3026. BF_ASSERT(ptr->GetType()->IsPointer());
  3027. inst->GetType();
  3028. #endif
  3029. return inst;
  3030. }
  3031. BeBrInst* BeModule::CreateBr(BeBlock* block)
  3032. {
  3033. auto inst = mAlloc.Alloc<BeBrInst>();
  3034. inst->mTargetBlock = block;
  3035. AddInst(inst);
  3036. return inst;
  3037. }
  3038. BeCondBrInst* BeModule::CreateCondBr(BeValue* cond, BeBlock* trueBlock, BeBlock* falseBlock)
  3039. {
  3040. auto inst = mAlloc.Alloc<BeCondBrInst>();
  3041. inst->mCond = cond;
  3042. inst->mTrueBlock = trueBlock;
  3043. inst->mFalseBlock = falseBlock;
  3044. AddInst(inst);
  3045. return inst;
  3046. }
  3047. BeRetInst* BeModule::CreateRetVoid()
  3048. {
  3049. auto inst = mAlloc.Alloc<BeRetInst>();
  3050. AddInst(inst);
  3051. return inst;
  3052. }
  3053. BeRetInst* BeModule::CreateRet(BeValue* value)
  3054. {
  3055. auto inst = mAlloc.Alloc<BeRetInst>();
  3056. inst->mRetValue = value;
  3057. AddInst(inst);
  3058. return inst;
  3059. }
  3060. BeSetRetInst* BeModule::CreateSetRet(BeValue* value, int returnTypeId)
  3061. {
  3062. auto inst = mAlloc.Alloc<BeSetRetInst>();
  3063. inst->mRetValue = value;
  3064. inst->mReturnTypeId = returnTypeId;
  3065. AddInst(inst);
  3066. return inst;
  3067. }
  3068. BeCallInst* BeModule::CreateCall(BeValue* func, const SizedArrayImpl<BeValue*>& args)
  3069. {
  3070. auto inst = mOwnedValues.Alloc<BeCallInst>();
  3071. inst->mFunc = func;
  3072. if (!args.IsEmpty())
  3073. {
  3074. inst->mArgs.resize(args.size());
  3075. for (int i = 0; i < (int)args.size(); i++)
  3076. inst->mArgs[i].mValue = args[i];
  3077. }
  3078. AddInst(inst);
  3079. return inst;
  3080. }
  3081. BeConstant* BeModule::GetConstant(BeType* type, double floatVal)
  3082. {
  3083. auto constant = mAlloc.Alloc<BeConstant>();
  3084. constant->mType = type;
  3085. constant->mDouble = floatVal;
  3086. return constant;
  3087. }
  3088. BeConstant* BeModule::GetConstant(BeType* type, int64 intVal)
  3089. {
  3090. auto constant = mAlloc.Alloc<BeConstant>();
  3091. constant->mType = type;
  3092. // Sign extend. One reason this is required is to for binary searching on switches
  3093. switch (type->mTypeCode)
  3094. {
  3095. case BeTypeCode_Int8:
  3096. constant->mInt64 = (int8)intVal;
  3097. break;
  3098. case BeTypeCode_Int16:
  3099. constant->mInt64 = (int16)intVal;
  3100. break;
  3101. case BeTypeCode_Int32:
  3102. constant->mInt64 = (int32)intVal;
  3103. break;
  3104. default:
  3105. constant->mInt64 = intVal;
  3106. }
  3107. return constant;
  3108. }
  3109. BeConstant* BeModule::GetConstant(BeType* type, bool boolVal)
  3110. {
  3111. auto constant = mAlloc.Alloc<BeConstant>();
  3112. constant->mType = type;
  3113. constant->mBool = boolVal;
  3114. return constant;
  3115. }
  3116. BeConstant* BeModule::GetConstantNull(BePointerType* type)
  3117. {
  3118. auto constant = mAlloc.Alloc<BeConstant>();
  3119. if (type == NULL)
  3120. constant->mType = mContext->GetPrimitiveType(BeTypeCode_NullPtr);
  3121. else
  3122. constant->mType = type;
  3123. return constant;
  3124. }
  3125. BeDbgReferenceType * BeDbgModule::CreateReferenceType(BeDbgType* elementType)
  3126. {
  3127. BeDbgType* useType = elementType->FindDerivedType(BeDbgReferenceType::TypeId);
  3128. if (useType == NULL)
  3129. {
  3130. auto dbgType = mTypes.Alloc<BeDbgReferenceType>();
  3131. dbgType->mElement = elementType;
  3132. elementType->mDerivedTypes.PushFront(dbgType, &mBeModule->mAlloc);
  3133. useType = dbgType;
  3134. }
  3135. return (BeDbgReferenceType*)useType;
  3136. }
  3137. void BeDbgModule::HashContent(BeHashContext & hashCtx)
  3138. {
  3139. hashCtx.Mixin(TypeId);
  3140. hashCtx.MixinStr(mFileName);
  3141. hashCtx.MixinStr(mDirectory);
  3142. hashCtx.MixinStr(mProducer);
  3143. BeDumpContext dc;
  3144. String lhsName;
  3145. String rhsName;
  3146. if (!mFuncs.IsEmpty())
  3147. {
  3148. auto _GetName = [&](BeDbgFunction* func, String& str)
  3149. {
  3150. if (!func->mLinkageName.IsEmpty())
  3151. str.Append(func->mLinkageName);
  3152. else
  3153. dc.ToString(str, func);
  3154. };
  3155. Array<BeDbgFunction*> unrefFuncs;
  3156. for (auto dbgFunc : mFuncs)
  3157. {
  3158. if ((!dbgFunc->mIncludedAsMember) && (dbgFunc->mHashId == -1))
  3159. unrefFuncs.Add(dbgFunc);
  3160. }
  3161. std::sort(unrefFuncs.begin(), unrefFuncs.end(), [&](BeDbgFunction* lhs, BeDbgFunction* rhs)
  3162. {
  3163. lhsName.Clear();
  3164. _GetName(lhs, lhsName);
  3165. rhsName.Clear();
  3166. _GetName(rhs, rhsName);
  3167. int cmp = String::Compare(lhsName, rhsName, false);
  3168. if (cmp != 0)
  3169. return cmp < 0;
  3170. if (lhs->mFile != rhs->mFile)
  3171. {
  3172. lhsName.Clear();
  3173. rhsName.Clear();
  3174. if (lhs->mFile != NULL)
  3175. lhs->mFile->ToString(lhsName);
  3176. if (rhs->mFile != NULL)
  3177. rhs->mFile->ToString(rhsName);
  3178. cmp = String::Compare(lhsName, rhsName, false);
  3179. if (cmp != 0)
  3180. return cmp < 0;
  3181. }
  3182. if (lhs->mLine != rhs->mLine)
  3183. return lhs->mLine < rhs->mLine;
  3184. return lhs->mIdx < rhs->mIdx;
  3185. });
  3186. hashCtx.Mixin(unrefFuncs.size());
  3187. for (auto dbgFunc : unrefFuncs)
  3188. {
  3189. if (dbgFunc->mHashId == -1)
  3190. dbgFunc->HashReference(hashCtx);
  3191. }
  3192. }
  3193. if (!mGlobalVariables.IsEmpty())
  3194. {
  3195. auto _GetName = [&](BeDbgGlobalVariable* func, String& str)
  3196. {
  3197. if (!func->mLinkageName.IsEmpty())
  3198. str.Append(func->mLinkageName);
  3199. else
  3200. dc.ToString(str, func);
  3201. };
  3202. std::sort(mGlobalVariables.begin(), mGlobalVariables.end(), [&](BeDbgGlobalVariable* lhs, BeDbgGlobalVariable* rhs)
  3203. {
  3204. lhsName.Clear();
  3205. _GetName(lhs, lhsName);
  3206. rhsName.Clear();
  3207. _GetName(rhs, rhsName);
  3208. return (lhsName < rhsName);
  3209. });
  3210. for (auto globalVar : mGlobalVariables)
  3211. globalVar->HashReference(hashCtx);
  3212. }
  3213. }