BeModule.cpp 85 KB

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