BeModule.cpp 78 KB

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