BeModule.cpp 80 KB

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